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

search/tool_processors/link_processor.class.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Process links before pass it to search listing scripts.
6
 *
7
 * @package chamilo.include.search
8
 */
9
class link_processor extends search_processor
10
{
11
    public $links = [];
12
13
    public function __construct($rows)
14
    {
15
        $this->rows = $rows;
16
17
        // group all links together
18
        foreach ($rows as $row_id => $row_val) {
19
            $link_id = $row_val['xapian_data'][SE_DATA]['link_id'];
20
            $courseid = $row_val['courseid'];
21
            $item = [
22
                'courseid' => $courseid,
23
                'score' => $row_val['score'],
24
                'link_id' => $link_id,
25
                'row_id' => $row_id,
26
            ];
27
            $this->links[$courseid]['links'][] = $item;
28
            $this->links[$courseid]['total_score'] += $row_val['score'];
29
        }
30
    }
31
32
    public function process()
33
    {
34
        $results = [];
35
        foreach ($this->links as $courseCode => $one_course_links) {
36
            $course_info = api_get_course_info($courseCode);
37
            $search_show_unlinked_results = (api_get_setting('search_show_unlinked_results') == 'true');
38
            $course_visible_for_user = api_is_course_visible_for_user(null, $courseCode);
39
            // can view course?
40
            if ($course_visible_for_user || $search_show_unlinked_results) {
41
                $result = null;
42
                foreach ($one_course_links['links'] as $one_link) {
43
                    // is visible?
44
                    $visibility = api_get_item_visibility($course_info, TOOL_LINK, $one_link['link_id']);
45
                    if ($visibility) {
46
                        // if one is visible let show the result for a course
47
                        // also asume all data of this item like the data of the whole group of links(Ex. author)
48
                        list($thumbnail, $image, $name, $author, $url) = $this->get_information($courseCode, $one_link['link_id']);
49
                        $result_tmp = [
50
                            'toolid' => TOOL_LINK,
51
                            'score' => $one_course_links['total_score'] / (count($one_course_links) - 1), // not count total_score array item
52
                            'url' => $url,
53
                            'thumbnail' => $thumbnail,
54
                            'image' => $image,
55
                            'title' => $name,
56
                            'author' => $author,
57
                        ];
58
                        if ($course_visible_for_user) {
59
                            $result = $result_tmp;
60
                        } else { // course not visible for user
61
                            if ($search_show_unlinked_results) {
62
                                $result_tmp['url'] = '';
63
                                $result = $result_tmp;
64
                            }
65
                        }
66
                        break;
67
                    }
68
                }
69
                if (!is_null($result)) {
70
                    // if there is at least one link item found show link to course Links tool page
71
                    $results[] = $result;
72
                }
73
            }
74
        }
75
76
        // get information to sort
77
        foreach ($results as $key => $row) {
78
            $score[$key] = $row['score'];
79
        }
80
81
        // Sort results with score descending
82
        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

82
        array_multisort($score, /** @scrutinizer ignore-type */ SORT_DESC, $results);
Loading history...
83
84
        return $results;
85
    }
86
87
    /**
88
     * Get document information.
89
     */
90
    private function get_information($course_id, $link_id)
91
    {
92
        $course_information = api_get_course_info($course_id);
93
        $course_id = $course_information['real_id'];
94
        $course_id_alpha = $course_information['id'];
95
        if (!empty($course_information)) {
96
            $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
97
98
            $link_id = intval($link_id);
99
            $sql = "SELECT insert_user_id FROM $item_property_table
100
              		WHERE ref = $link_id AND tool = '".TOOL_LINK."' AND c_id = $course_id
101
              		LIMIT 1";
102
103
            $name = get_lang('Links');
104
            $url = api_get_path(WEB_PATH).'main/link/link.php?cidReq=%s';
105
            $url = sprintf($url, $course_id_alpha);
106
            // Get the image path
107
            $thumbnail = Display::returnIconPath('link.png');
108
            $image = $thumbnail; //FIXME: use big images
109
            // get author
110
            $author = '';
111
            $item_result = Database::query($sql);
112
            if ($row = Database::fetch_array($item_result)) {
113
                $user_data = api_get_user_info($row['insert_user_id']);
114
                $author = api_get_person_name($user_data['firstName'], $user_data['lastName']);
115
            }
116
117
            return [$thumbnail, $image, $name, $author, $url];
118
        } else {
119
            return [];
120
        }
121
    }
122
}
123