forked from moodlehq/moodle-block_course_overview
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlocallib.php
355 lines (312 loc) · 11.2 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper functions for course_overview block
*
* @package block_course_overview
* @copyright 2012 Adam Olley <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE', '0');
define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_ONLY_PARENT_NAME', '1');
define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_FULL_PATH', '2');
define('BLOCKS_COURSE_OVERVIEW_REORDER_NONE', '0');
define('BLOCKS_COURSE_OVERVIEW_REORDER_FULLNAME', '1');
define('BLOCKS_COURSE_OVERVIEW_REORDER_SHORTNAME', '2');
define('BLOCKS_COURSE_OVERVIEW_REORDER_ID', '3');
define('BLOCKS_COURSE_OVERVIEW_REORDER_IDDESC', '4');
define('BLOCKS_COURSE_OVERVIEW_DEFAULT_FAVOURITES', '1');
define('BLOCKS_COURSE_OVERVIEW_DEFAULT_COURSES', '2');
/**
* Display overview for courses
*
* @param array $courses courses for which overview needs to be shown
* @return array html overview
*/
function block_course_overview_get_overviews($courses) {
global $CFG;
// Tab may not have any courses.
if (!$courses) {
return array();
}
// Disable debugging mode because all course modules show debugging message in their print_overview.
$debugmode = $CFG->debug;
$CFG->debug ^= E_DEPRECATED;
$htmlarray = array();
if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
// Split courses list into batches with no more than MAX_MODINFO_CACHE_SIZE courses in one batch.
// Otherwise we exceed the cache limit in get_fast_modinfo() and rebuild it too often.
if (defined('MAX_MODINFO_CACHE_SIZE') && MAX_MODINFO_CACHE_SIZE > 0 && count($courses) > MAX_MODINFO_CACHE_SIZE) {
$batches = array_chunk($courses, MAX_MODINFO_CACHE_SIZE, true);
} else {
$batches = array($courses);
}
foreach ($batches as $courses) {
foreach ($modules as $fname) {
$fname($courses, $htmlarray);
}
}
}
$CFG->debug = $debugmode;
return $htmlarray;
}
/**
* Sets user preference for maximum courses to be displayed in course_overview block
*
* @param int $number maximum courses which should be visible
*/
function block_course_overview_update_mynumber($number) {
set_user_preference('course_overview_number_of_courses', $number);
}
/**
* Sets user course sorting preference in course_overview block
*
* @param array $sortorder list of course ids
*/
function block_course_overview_update_myorder($sortorder) {
$value = implode(',', $sortorder);
if (core_text::strlen($value) > 1333) {
// The value won't fit into the user preference.
// Remove courses in the end of the list (mostly likely user won't even notice).
$value = preg_replace('/,[\d]*$/', '', core_text::substr($value, 0, 1334));
}
set_user_preference('course_overview_course_sortorder', $value);
}
/**
* Gets user course sorting preference in course_overview block
*
* @return array list of course ids
*/
function block_course_overview_get_myorder() {
if ($value = get_user_preferences('course_overview_course_sortorder')) {
return explode(',', $value);
}
// If preference was not found, look in the old location and convert if found.
$order = array();
if ($value = get_user_preferences('course_overview_course_order')) {
$order = unserialize_array($value);
block_course_overview_update_myorder($order);
unset_user_preference('course_overview_course_order');
}
return $order;
}
/**
* Get the list of course favourites
*
* @return array list of course ids
*/
function block_course_overview_get_favourites() {
if ($value = get_user_preferences('course_overview_favourites')) {
return explode(',', $value);
} else {
return array();
}
}
/**
* Sets favourites
*
* @param array $favourites list of course ids
*/
function block_course_overview_update_favourites($favourites) {
$value = implode(',', $favourites);
if (core_text::strlen($value) > 1333) {
// The value won't fit into the user preference.
// Remove courses in the end of the list (mostly likely user won't even notice).
$value = preg_replace('/,[\d]*$/', '', core_text::substr($value, 0, 1334));
}
set_user_preference('course_overview_favourites', $value);
}
/**
* Get sort order preference
* @return int
*/
function block_course_overview_get_sortorder() {
if ($value = get_user_preferences('course_overview_sortorder')) {
return $value;
} else {
return BLOCKS_COURSE_OVERVIEW_REORDER_NONE;
}
}
/**
* Set sort order preference
* @param int $sortorder
*/
function block_course_overview_update_sortorder($sortorder) {
set_user_preference('course_overview_sortorder', $sortorder);
}
/**
* Sets user preference for max number of courses to show in course_overview block
* @param int $usersetmaxcourses
*/
function block_course_overview_update_usersetmaxcourses($usersetmaxcourses) {
set_user_preference('course_overview_usersetmaxcourses', $usersetmaxcourses);
}
/**
* Gets user preference for max number of courses to show in course_overview block
* @return int
*/
function block_course_overview_get_usersetmaxcourses() {
if ($value = get_user_preferences('course_overview_usersetmaxcourses')) {
return $value;
} else {
return get_config('block_course_overview')->setmaxcourses;
}
}
/**
* Return sorted list of user courses
*
* @param bool $favourites tab selected
* @param bool $keepfavourites setting, show favs in course tab
* @param array $exlude list of courses not to include (i.e. favs in courses list)A
* @return array list of sorted courses and count of courses.
*/
function block_course_overview_get_sorted_courses($favourites, $keepfavourites = false, $exclude = []) {
global $USER;
// Bodge... don't regenerate course list.
static $courses = [];
$sortorder = block_course_overview_get_sortorder();
if (!$courses) {
// Get courses in order.
if ($sortorder == BLOCKS_COURSE_OVERVIEW_REORDER_FULLNAME) {
$sort = 'fullname ASC';
} else if ($sortorder == BLOCKS_COURSE_OVERVIEW_REORDER_SHORTNAME) {
$sort = 'shortname ASC';
} else if ($sortorder == BLOCKS_COURSE_OVERVIEW_REORDER_ID) {
$sort = 'id ASC';
} else if ($sortorder == BLOCKS_COURSE_OVERVIEW_REORDER_IDDESC) {
$sort = 'id DESC';
} else {
$sort = 'visible DESC,sortorder ASC';
}
$courses = enrol_get_my_courses(null, $sort);
$site = get_site();
if (array_key_exists($site->id, $courses)) {
unset($courses[$site->id]);
}
foreach ($courses as $c) {
if (isset($USER->lastcourseaccess[$c->id])) {
$courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
} else {
$courses[$c->id]->lastaccess = 0;
}
}
// Get remote courses.
$remotecourses = array();
if (is_enabled_auth('mnet')) {
$remotecourses = get_my_remotecourses();
}
// Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses.
foreach ($remotecourses as $id => $val) {
$remoteid = $val->remoteid * -1;
$val->id = $remoteid;
$courses[$remoteid] = $val;
}
}
if ($favourites) {
$order = block_course_overview_get_favourites();
} else {
$order = block_course_overview_get_myorder();
}
$sortedcourses = array();
// Accept list as-is or order by preference list.
if (!$favourites && ($sortorder != BLOCKS_COURSE_OVERVIEW_REORDER_NONE)) {
$sortedcourses = $courses;
} else {
$counter = 0;
// Get courses in sort order into list.
foreach ($order as $key => $cid) {
// Make sure user is still enroled.
if (isset($courses[$cid])) {
$sortedcourses[$cid] = $courses[$cid];
$counter++;
}
}
// Append unsorted courses if limit allows & not favourites.
if (!$favourites) {
foreach ($courses as $c) {
if (!in_array($c->id, $order)) {
$sortedcourses[$c->id] = $c;
$counter++;
}
}
}
}
// If this is the courses tab and we are excluding favourites.
if (!$favourites && !$keepfavourites) {
foreach ($sortedcourses as $c) {
if (in_array($c->id, $exclude)) {
unset($sortedcourses[$c->id]);
}
}
}
// From list extract site courses for overview.
$sitecourses = array();
foreach ($sortedcourses as $key => $course) {
if ($course->id > 0) {
$sitecourses[$key] = $course;
}
}
// Implement pagination.
$maxcourses = block_course_overview_get_usersetmaxcourses();
if ($favourites) {
$page = optional_param('ssf_pagenum', 0, PARAM_INT);
} else {
$page = optional_param('ssc_pagenum', 0, PARAM_INT);
}
$page = $page <= 0 ? 0 : $page;
$first = $page * $maxcourses;
$sortedcoursessliced = array_slice($sortedcourses, $first, $maxcourses, true);
return array($sortedcoursessliced, $sitecourses, count($sortedcourses));
}
/**
* Add a course to favourites
* @param int $favourite id of course
*/
function block_course_overview_add_favourite($favourite) {
// Add to fabourites list.
$favourites = block_course_overview_get_favourites();
if (!in_array($favourite, $favourites)) {
array_unshift($favourites, $favourite);
}
block_course_overview_update_favourites($favourites);
// Remove from courses list.
$order = block_course_overview_get_myorder();
$key = array_search($favourite, $order);
if ($key !== false) {
unset($order[$key]);
}
block_course_overview_update_myorder($order);
}
/**
* Remove course from favourites
* @param int $favourite id of course
*/
function block_course_overview_remove_favourite($favourite) {
// Remove from favourites list.
$order = block_course_overview_get_favourites();
$key = array_search($favourite, $order);
if ($key !== false) {
unset($order[$key]);
}
block_course_overview_update_favourites($order);
// Add to courses list.
$order = block_course_overview_get_myorder();
if (!in_array($favourite, $order)) {
$order[] = $favourite;
}
block_course_overview_update_myorder($order);
}