Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

tool_processors/document_processor.class.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Process documents before pass it to search listing scripts.
6
 *
7
 * @package chamilo.include.search
8
 */
9
class document_processor extends search_processor
10
{
11
    public function __construct($rows)
12
    {
13
        $this->rows = $rows;
14
    }
15
16
    public function process()
17
    {
18
        $results = [];
19
        foreach ($this->rows as $row_val) {
20
            $search_show_unlinked_results = (api_get_setting('search_show_unlinked_results') == 'true');
21
            $course_visible_for_user = api_is_course_visible_for_user(null, $row_val['courseid']);
22
            // can view course?
23
            if ($course_visible_for_user || $search_show_unlinked_results) {
24
                // is visible?
25
                $visibility = api_get_item_visibility(api_get_course_info($row_val['courseid']), TOOL_DOCUMENT, $row_val['xapian_data'][SE_DATA]['doc_id']);
26
                if ($visibility) {
27
                    list($thumbnail, $image, $name, $author, $url) = $this->get_information($row_val['courseid'], $row_val['xapian_data'][SE_DATA]['doc_id']);
28
                    $result = [
29
                        'toolid' => TOOL_DOCUMENT,
30
                        'score' => $row_val['score'],
31
                        'url' => $url,
32
                        'thumbnail' => $thumbnail,
33
                        'image' => $image,
34
                        'title' => $name,
35
                        'author' => $author,
36
                    ];
37
                    if ($course_visible_for_user) {
38
                        $results[] = $result;
39
                    } else { // course not visible for user
40
                        if ($search_show_unlinked_results) {
41
                            $result['url'] = '';
42
                            $results[] = $result;
43
                        }
44
                    }
45
                }
46
            }
47
        }
48
49
        // get information to sort
50
        foreach ($results as $key => $row) {
51
            $score[$key] = $row['score'];
52
        }
53
54
        // Sort results with score descending
55
        array_multisort($score, SORT_DESC, $results);
0 ignored issues
show
SORT_DESC cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        array_multisort($score, /** @scrutinizer ignore-type */ SORT_DESC, $results);
Loading history...
56
57
        return $results;
58
    }
59
60
    /**
61
     * Get document information.
62
     */
63
    private function get_information($course_id, $doc_id)
64
    {
65
        $course_information = api_get_course_info($course_id);
66
        $course_id = $course_information['real_id'];
67
        $course_path = $course_information['path'];
68
        if (!empty($course_information)) {
69
            $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
70
            $doc_table = Database::get_course_table(TABLE_DOCUMENT);
71
72
            $doc_id = intval($doc_id);
73
            $sql = "SELECT * FROM       $doc_table
74
                    WHERE      $doc_table.id = $doc_id AND c_id = $course_id
75
                    LIMIT 1";
76
            $dk_result = Database::query($sql);
77
78
            $sql = "SELECT insert_user_id FROM       $item_property_table
79
                    WHERE   ref = $doc_id AND tool = '".TOOL_DOCUMENT."' AND c_id = $course_id
80
                    LIMIT 1";
81
            $name = '';
82
            if ($row = Database::fetch_array($dk_result)) {
83
                $name = $row['title'];
84
                $url = api_get_path(WEB_COURSE_PATH).'%s/document%s';
85
                $url = sprintf($url, $course_path, $row['path']);
86
                // Get the image path
87
                $icon = choose_image(basename($row['path']));
88
                $thumbnail = Display::returnIconPath($icon);
89
                $image = $thumbnail;
90
                //FIXME: use big images
91
                // get author
92
                $author = '';
93
                $item_result = Database::query($sql);
94
                if ($row = Database::fetch_array($item_result)) {
95
                    $user_data = api_get_user_info($row['insert_user_id']);
96
                    $author = api_get_person_name($user_data['firstName'], $user_data['lastName']);
97
                }
98
            }
99
100
            return [$thumbnail, $image, $name, $author, $url]; // FIXME: is it posible to get an author here?
101
        } else {
102
            return [];
103
        }
104
    }
105
}
106