MDL-47919 tool_monitor: adjustments to behat tests
[moodle.git] / blocks / course_overview / locallib.php
blob746ccf4eee1c96157962ea2ca9eedb4ae8ab3a19
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.
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
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
25 define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE', '0');
26 define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_ONLY_PARENT_NAME', '1');
27 define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_FULL_PATH', '2');
29 /**
30 * Display overview for courses
32 * @param array $courses courses for which overview needs to be shown
33 * @return array html overview
35 function block_course_overview_get_overviews($courses) {
36 $htmlarray = array();
37 if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
38 // Split courses list into batches with no more than MAX_MODINFO_CACHE_SIZE courses in one batch.
39 // Otherwise we exceed the cache limit in get_fast_modinfo() and rebuild it too often.
40 if (defined('MAX_MODINFO_CACHE_SIZE') && MAX_MODINFO_CACHE_SIZE > 0 && count($courses) > MAX_MODINFO_CACHE_SIZE) {
41 $batches = array_chunk($courses, MAX_MODINFO_CACHE_SIZE, true);
42 } else {
43 $batches = array($courses);
45 foreach ($batches as $courses) {
46 foreach ($modules as $fname) {
47 $fname($courses, $htmlarray);
51 return $htmlarray;
54 /**
55 * Sets user preference for maximum courses to be displayed in course_overview block
57 * @param int $number maximum courses which should be visible
59 function block_course_overview_update_mynumber($number) {
60 set_user_preference('course_overview_number_of_courses', $number);
63 /**
64 * Sets user course sorting preference in course_overview block
66 * @param array $sortorder list of course ids
68 function block_course_overview_update_myorder($sortorder) {
69 $value = implode(',', $sortorder);
70 if (core_text::strlen($value) > 1333) {
71 // The value won't fit into the user preference. Remove courses in the end of the list (mostly likely user won't even notice).
72 $value = preg_replace('/,[\d]*$/', '', core_text::substr($value, 0, 1334));
74 set_user_preference('course_overview_course_sortorder', $value);
77 /**
78 * Gets user course sorting preference in course_overview block
80 * @return array list of course ids
82 function block_course_overview_get_myorder() {
83 if ($value = get_user_preferences('course_overview_course_sortorder')) {
84 return explode(',', $value);
86 // If preference was not found, look in the old location and convert if found.
87 $order = array();
88 if ($value = get_user_preferences('course_overview_course_order')) {
89 $order = unserialize($value);
90 block_course_overview_update_myorder($order);
91 unset_user_preference('course_overview_course_order');
93 return $order;
96 /**
97 * Returns shortname of activities in course
99 * @param int $courseid id of course for which activity shortname is needed
100 * @return string|bool list of child shortname
102 function block_course_overview_get_child_shortnames($courseid) {
103 global $DB;
104 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
105 $sql = "SELECT c.id, c.shortname, $ctxselect
106 FROM {enrol} e
107 JOIN {course} c ON (c.id = e.customint1)
108 JOIN {context} ctx ON (ctx.instanceid = e.customint1)
109 WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
110 $params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
112 if ($results = $DB->get_records_sql($sql, $params)) {
113 $shortnames = array();
114 // Preload the context we will need it to format the category name shortly.
115 foreach ($results as $res) {
116 context_helper::preload_from_record($res);
117 $context = context_course::instance($res->id);
118 $shortnames[] = format_string($res->shortname, true, $context);
120 $total = count($shortnames);
121 $suffix = '';
122 if ($total > 10) {
123 $shortnames = array_slice($shortnames, 0, 10);
124 $diff = $total - count($shortnames);
125 if ($diff > 1) {
126 $suffix = get_string('shortnamesufixprural', 'block_course_overview', $diff);
127 } else {
128 $suffix = get_string('shortnamesufixsingular', 'block_course_overview', $diff);
131 $shortnames = get_string('shortnameprefix', 'block_course_overview', implode('; ', $shortnames));
132 $shortnames .= $suffix;
135 return isset($shortnames) ? $shortnames : false;
139 * Returns maximum number of courses which will be displayed in course_overview block
141 * @param bool $showallcourses if set true all courses will be visible.
142 * @return int maximum number of courses
144 function block_course_overview_get_max_user_courses($showallcourses = false) {
145 // Get block configuration
146 $config = get_config('block_course_overview');
147 $limit = $config->defaultmaxcourses;
149 // If max course is not set then try get user preference
150 if (empty($config->forcedefaultmaxcourses)) {
151 if ($showallcourses) {
152 $limit = 0;
153 } else {
154 $limit = get_user_preferences('course_overview_number_of_courses', $limit);
157 return $limit;
161 * Return sorted list of user courses
163 * @param bool $showallcourses if set true all courses will be visible.
164 * @return array list of sorted courses and count of courses.
166 function block_course_overview_get_sorted_courses($showallcourses = false) {
167 global $USER;
169 $limit = block_course_overview_get_max_user_courses($showallcourses);
171 $courses = enrol_get_my_courses();
172 $site = get_site();
174 if (array_key_exists($site->id,$courses)) {
175 unset($courses[$site->id]);
178 foreach ($courses as $c) {
179 if (isset($USER->lastcourseaccess[$c->id])) {
180 $courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
181 } else {
182 $courses[$c->id]->lastaccess = 0;
186 // Get remote courses.
187 $remotecourses = array();
188 if (is_enabled_auth('mnet')) {
189 $remotecourses = get_my_remotecourses();
191 // Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses
192 foreach ($remotecourses as $id => $val) {
193 $remoteid = $val->remoteid * -1;
194 $val->id = $remoteid;
195 $courses[$remoteid] = $val;
198 $order = block_course_overview_get_myorder();
200 $sortedcourses = array();
201 $counter = 0;
202 // Get courses in sort order into list.
203 foreach ($order as $key => $cid) {
204 if (($counter >= $limit) && ($limit != 0)) {
205 break;
208 // Make sure user is still enroled.
209 if (isset($courses[$cid])) {
210 $sortedcourses[$cid] = $courses[$cid];
211 $counter++;
214 // Append unsorted courses if limit allows
215 foreach ($courses as $c) {
216 if (($limit != 0) && ($counter >= $limit)) {
217 break;
219 if (!in_array($c->id, $order)) {
220 $sortedcourses[$c->id] = $c;
221 $counter++;
225 // From list extract site courses for overview
226 $sitecourses = array();
227 foreach ($sortedcourses as $key => $course) {
228 if ($course->id > 0) {
229 $sitecourses[$key] = $course;
232 return array($sortedcourses, $sitecourses, count($courses));