bc159de98c6c664628d3b2379240de404615537a
[moodle.git] / blocks / course_overview / locallib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Helper functions for course_overview block
19  *
20  * @package    block_course_overview
21  * @copyright  2012 Adam Olley <adam.olley@netspot.com.au>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 /**
26  * Display overview for courses
27  *
28  * @param array $courses courses for which overview needs to be shown
29  * @return array html overview
30  */
31 function block_course_overview_get_overviews($courses) {
32     $htmlarray = array();
33     if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
34         foreach ($modules as $fname) {
35             $fname($courses,$htmlarray);
36         }
37     }
38     return $htmlarray;
39 }
41 /**
42  * Sets user preference for maximum courses to be displayed in course_overview block
43  *
44  * @param int $number maximum courses which should be visible
45  */
46 function block_course_overview_update_mynumber($number) {
47     set_user_preference('course_overview_number_of_courses', $number);
48 }
50 /**
51  * Sets user course sorting preference in course_overview block
52  *
53  * @param array $sortorder sort order of course
54  */
55 function block_course_overview_update_myorder($sortorder) {
56     set_user_preference('course_overview_course_order', serialize($sortorder));
57 }
59 /**
60  * Returns shortname of activities in course
61  *
62  * @param int $courseid id of course for which activity shortname is needed
63  * @return string|bool list of child shortname
64  */
65 function block_course_overview_get_child_shortnames($courseid) {
66     global $DB;
67     $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
68     $sql = "SELECT c.id, c.shortname, $ctxselect
69             FROM {enrol} e
70             JOIN {course} c ON (c.id = e.customint1)
71             JOIN {context} ctx ON (ctx.instanceid = e.customint1)
72             WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
73     $params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
75     if ($results = $DB->get_records_sql($sql, $params)) {
76         $shortnames = array();
77         // Preload the context we will need it to format the category name shortly.
78         foreach ($results as $res) {
79             context_helper::preload_from_record($res);
80             $context = context_course::instance($res->id);
81             $shortnames[] = format_string($res->shortname, true, $context);
82         }
83         $total = count($shortnames);
84         $suffix = '';
85         if ($total > 10) {
86             $shortnames = array_slice($shortnames, 0, 10);
87             $diff = $total - count($shortnames);
88             if ($diff > 1) {
89                 $suffix = get_string('shortnamesufixprural', 'block_course_overview', $diff);
90             } else {
91                 $suffix = get_string('shortnamesufixsingular', 'block_course_overview', $diff);
92             }
93         }
94         $shortnames = get_string('shortnameprefix', 'block_course_overview', implode('; ', $shortnames));
95         $shortnames .= $suffix;
96     }
98     return isset($shortnames) ? $shortnames : false;
99 }
101 /**
102  * Returns maximum number of courses which will be displayed in course_overview block
103  *
104  * @return int maximum number of courses
105  */
106 function block_course_overview_get_max_user_courses() {
107     // Get block configuration
108     $config = get_config('block_course_overview');
109     $limit = $config->defaultmaxcourses;
111     // If max course is not set then try get user preference
112     if (empty($config->forcedefaultmaxcourses)) {
113         $limit = get_user_preferences('course_overview_number_of_courses', $limit);
114     }
115     return $limit;
118 /**
119  * Return sorted list of user courses
120  *
121  * @return array list of sorted courses and count of courses.
122  */
123 function block_course_overview_get_sorted_courses() {
124     global $USER;
126     $limit = block_course_overview_get_max_user_courses();
128     $courses = enrol_get_my_courses('id, shortname, fullname, modinfo, sectioncache');
129     $site = get_site();
131     if (array_key_exists($site->id,$courses)) {
132         unset($courses[$site->id]);
133     }
135     foreach ($courses as $c) {
136         if (isset($USER->lastcourseaccess[$c->id])) {
137             $courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
138         } else {
139             $courses[$c->id]->lastaccess = 0;
140         }
141     }
143     // Get remote courses.
144     $remotecourses = array();
145     if (is_enabled_auth('mnet')) {
146         $remotecourses = get_my_remotecourses();
147     }
148     // Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses
149     foreach ($remotecourses as $id => $val) {
150         $remoteid = $val->remoteid * -1;
151         $val->id = $remoteid;
152         $courses[$remoteid] = $val;
153     }
155     $order = array();
156     if (!is_null($usersortorder = get_user_preferences('course_overview_course_order'))) {
157         $order = unserialize($usersortorder);
158     }
160     $sortedcourses = array();
161     $counter = 0;
162     // Get courses in sort order into list.
163     foreach ($order as $key => $cid) {
164         if (($counter >= $limit) && ($limit != 0)) {
165             break;
166         }
168         // Make sure user is still enroled.
169         if (isset($courses[$cid])) {
170             $sortedcourses[$cid] = $courses[$cid];
171             $counter++;
172         }
173     }
174     // Append unsorted courses if limit allows
175     foreach ($courses as $c) {
176         if (($limit != 0) && ($counter >= $limit)) {
177             break;
178         }
179         if (!in_array($c->id, $order)) {
180             $sortedcourses[$c->id] = $c;
181             $counter++;
182         }
183     }
185     // From list extract site courses for overview
186     $sitecourses = array();
187     foreach ($sortedcourses as $key => $course) {
188         if ($course->id > 0) {
189             $sitecourses[$key] = $course;
190         }
191     }
192     return array($sortedcourses, $sitecourses, count($courses));