MDL-44810 editor_atto: restore selection after button press
[moodle.git] / blocks / course_overview / locallib.php
blob5bd7f4264c2276900c7c3e3ce6666cba58f5c5b5
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 /**
26 * Display overview for courses
28 * @param array $courses courses for which overview needs to be shown
29 * @return array html overview
31 function block_course_overview_get_overviews($courses) {
32 $htmlarray = array();
33 if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
34 // Split courses list into batches with no more than MAX_MODINFO_CACHE_SIZE courses in one batch.
35 // Otherwise we exceed the cache limit in get_fast_modinfo() and rebuild it too often.
36 if (defined('MAX_MODINFO_CACHE_SIZE') && MAX_MODINFO_CACHE_SIZE > 0 && count($courses) > MAX_MODINFO_CACHE_SIZE) {
37 $batches = array_chunk($courses, MAX_MODINFO_CACHE_SIZE, true);
38 } else {
39 $batches = array($courses);
41 foreach ($batches as $courses) {
42 foreach ($modules as $fname) {
43 $fname($courses, $htmlarray);
47 return $htmlarray;
50 /**
51 * Sets user preference for maximum courses to be displayed in course_overview block
53 * @param int $number maximum courses which should be visible
55 function block_course_overview_update_mynumber($number) {
56 set_user_preference('course_overview_number_of_courses', $number);
59 /**
60 * Sets user course sorting preference in course_overview block
62 * @param array $sortorder sort order of course
64 function block_course_overview_update_myorder($sortorder) {
65 set_user_preference('course_overview_course_order', serialize($sortorder));
68 /**
69 * Returns shortname of activities in course
71 * @param int $courseid id of course for which activity shortname is needed
72 * @return string|bool list of child shortname
74 function block_course_overview_get_child_shortnames($courseid) {
75 global $DB;
76 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
77 $sql = "SELECT c.id, c.shortname, $ctxselect
78 FROM {enrol} e
79 JOIN {course} c ON (c.id = e.customint1)
80 JOIN {context} ctx ON (ctx.instanceid = e.customint1)
81 WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
82 $params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
84 if ($results = $DB->get_records_sql($sql, $params)) {
85 $shortnames = array();
86 // Preload the context we will need it to format the category name shortly.
87 foreach ($results as $res) {
88 context_helper::preload_from_record($res);
89 $context = context_course::instance($res->id);
90 $shortnames[] = format_string($res->shortname, true, $context);
92 $total = count($shortnames);
93 $suffix = '';
94 if ($total > 10) {
95 $shortnames = array_slice($shortnames, 0, 10);
96 $diff = $total - count($shortnames);
97 if ($diff > 1) {
98 $suffix = get_string('shortnamesufixprural', 'block_course_overview', $diff);
99 } else {
100 $suffix = get_string('shortnamesufixsingular', 'block_course_overview', $diff);
103 $shortnames = get_string('shortnameprefix', 'block_course_overview', implode('; ', $shortnames));
104 $shortnames .= $suffix;
107 return isset($shortnames) ? $shortnames : false;
111 * Returns maximum number of courses which will be displayed in course_overview block
113 * @param bool $showallcourses if set true all courses will be visible.
114 * @return int maximum number of courses
116 function block_course_overview_get_max_user_courses($showallcourses = false) {
117 // Get block configuration
118 $config = get_config('block_course_overview');
119 $limit = $config->defaultmaxcourses;
121 // If max course is not set then try get user preference
122 if (empty($config->forcedefaultmaxcourses)) {
123 if ($showallcourses) {
124 $limit = 0;
125 } else {
126 $limit = get_user_preferences('course_overview_number_of_courses', $limit);
129 return $limit;
133 * Return sorted list of user courses
135 * @param bool $showallcourses if set true all courses will be visible.
136 * @return array list of sorted courses and count of courses.
138 function block_course_overview_get_sorted_courses($showallcourses = false) {
139 global $USER;
141 $limit = block_course_overview_get_max_user_courses($showallcourses);
143 $courses = enrol_get_my_courses();
144 $site = get_site();
146 if (array_key_exists($site->id,$courses)) {
147 unset($courses[$site->id]);
150 foreach ($courses as $c) {
151 if (isset($USER->lastcourseaccess[$c->id])) {
152 $courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
153 } else {
154 $courses[$c->id]->lastaccess = 0;
158 // Get remote courses.
159 $remotecourses = array();
160 if (is_enabled_auth('mnet')) {
161 $remotecourses = get_my_remotecourses();
163 // Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses
164 foreach ($remotecourses as $id => $val) {
165 $remoteid = $val->remoteid * -1;
166 $val->id = $remoteid;
167 $courses[$remoteid] = $val;
170 $order = array();
171 if (!is_null($usersortorder = get_user_preferences('course_overview_course_order'))) {
172 $order = unserialize($usersortorder);
175 $sortedcourses = array();
176 $counter = 0;
177 // Get courses in sort order into list.
178 foreach ($order as $key => $cid) {
179 if (($counter >= $limit) && ($limit != 0)) {
180 break;
183 // Make sure user is still enroled.
184 if (isset($courses[$cid])) {
185 $sortedcourses[$cid] = $courses[$cid];
186 $counter++;
189 // Append unsorted courses if limit allows
190 foreach ($courses as $c) {
191 if (($limit != 0) && ($counter >= $limit)) {
192 break;
194 if (!in_array($c->id, $order)) {
195 $sortedcourses[$c->id] = $c;
196 $counter++;
200 // From list extract site courses for overview
201 $sitecourses = array();
202 foreach ($sortedcourses as $key => $course) {
203 if ($course->id > 0) {
204 $sitecourses[$key] = $course;
207 return array($sortedcourses, $sitecourses, count($courses));