-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
402 lines (337 loc) · 13.8 KB
/
lib.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?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/>.
/**
* Module functions definitions.
*
* @package local_uca_mycourses
* @author Université Clermont Auvergne - Anthony Durif
* @copyright 2018 Université Clermont Auvergne
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
//======================================================================
// MY COURSES FUNCTIONS
//======================================================================
/**
* Returns our courses list on json format.
* @param $all boolean to indicate if we want all the courses or not (some roles has to be excluded).
* @return string json of courses.
*/
function get_my_courses_json_tree($all = true) {
return json_encode(get_my_courses_tree($all));
}
/**
* Returns our courses list on a 'tree' format with course categories.
* @param $all boolean to indicate if we want all the courses or not.
* @return array the tree of courses.
*/
function get_my_courses_tree($all = true) {
global $CFG;
include_once($CFG->dirroot.'/enrol/locallib.php');
$courses = get_my_courses_list($all);
$categories = array();
$root = array();
foreach ($courses as $course) {
// We create the parent course categories.
$cat = core_course_category::get($course->category, MUST_EXIST,true);
$parents = array_reverse($cat->get_parents());
if (!isset($categories[$course->category])) {
$categorie = new stdClass();
$categorie->id = $course->category;
$categorie->text = $cat->get_formatted_name();
$categorie->type = 'category';
$categorie->courses = array();
$categorie->children = array();
$categories[$course->category] = $categorie;
for ($i=0; $i < count($parents); $i++) {
if (!isset($categories[$parents[$i]])) {
$parent = core_course_category::get($parents[$i], MUST_EXIST,true);
$categorie = new StdClass();
$categorie->id = $parents[$i];
$categorie->text = $parent->get_formatted_name();
$categorie->type = 'category';
$categorie->courses = array();
$categorie->children = array();
$categories[$parents[$i]] = $categorie;
}
}
}
$parents = array_reverse($parents);
$parents[] = $course->category;
// Children course categories.
for ($i=0; $i < count($parents)-1; $i++) {
if (!in_array($parents[$i+1], $categories[$parents[$i]]->children)) {
$categories[$parents[$i]]->children[] = $parents[$i + 1];
}
}
// Where is the root node of the tree.
$rootCat = (0 == count($parents)) ? $course->category : $parents[0];
if (!in_array($rootCat, $root)) {
$root[] = $rootCat;
}
$course_node = new stdClass();
$course_node->text = $course->fullname;
$course->is_bookmarked = course_bookmarked($course);
$course_node->type = 'course';
$course_node->data = json_encode($course);
$course_node->children = [];
$categories[$course->category]->courses[$course->fullname] = $course;
// ksort($categories[$course->category]->courses);
}
$listeCategories = array();
foreach ($root as $cat) {
$listeCategories[] = get_category($cat, $categories);
}
foreach ($listeCategories as $cate) {
put_courses_in_tree($cate);
}
return array_values($listeCategories);
}
/**
* Returns the list of the current user courses.
* @param $all boolean to indicate if we want all the courses or not.
* @return array the courses.
*/
function get_my_courses_list($all = true) {
global $USER;
// Get courses from a core moodle function.
$my_courses = enrol_get_my_courses('enddate', 'fullname ASC,visible DESC,sortorder ASC');
$to_exclude = explode(',', get_config('block_uca_mycourses', 'roles_to_exclude'));
foreach ($my_courses as $key => $course) {
$to_unset = false;
// Check if the course is visible (unless necessary management capability ?).
if (!$course->visible && !can_manage_course($course) && !$all) {
$to_unset = true;
}
// Check if the course is finished. If it is we do not display it anymore.
if (!get_uca_mycourses_finished_courses_display() && $course->enddate != 0 && $course->enddate < time()) {
$to_unset = true;
}
$course->is_bookmark = course_bookmarked($course);
// We don't want all user courses.
if (!$all && count($to_exclude) > 0) {
$context = context_course::instance($course->id, true);
$roles = get_user_roles($context, $USER->id, true);
foreach ($roles as $role) {
if (in_array($role->roleid, $to_exclude)) {
$to_unset = true;
break;
}
}
}
if ($to_unset) {
unset($my_courses[$key]);
}
}
return $my_courses;
}
/**
* Constructs a course category available in the course tree.
* @param $id the id of the category.
* @param $categories list of the course category.
* @return array the category we create in the tree.
*/
function get_category($id, $categories) {
$category = $categories[$id];
$children = array();
if ($category->children) {
foreach ($category->children as $child_id) {
$children[$categories[$child_id]->text . $child_id] = get_category($child_id, $categories);
}
}
ksort($children);
$category->children = array_values($children);
return $category;
}
/**
* Puts our courses in the course categories tree we just create.
* @param $category the current course category.
*/
function put_courses_in_tree($category) {
if (isset($category->courses)) {
foreach ($category->courses as $course) {
$cc = new stdClass();
$cc->text = $course->fullname;
$cc->data = json_encode($course);
$cc->type = 'course';
$cc->children = [];
$category->children[] = $cc;
}
foreach ($category->children as $child) {
put_courses_in_tree($child);
}
}
}
/**
* Returns if the current user has rights to manage the course given in parameters.
* @param $course the course we test.
* @return bool true if the user has necessary rights and false in other cases.
*/
function can_manage_course($course) {
$context = context_course::instance($course->id);
return has_capability('moodle/course:update', $context);
}
//======================================================================
// COURSE BOOKMARKS FUNCTIONS
//======================================================================
/**
* Check if the current user has course bookmarks json defined in the database.
* @return boolean true if the current user has bookmarks and false in other cases.
*/
function user_has_bookmarks() {
return (get_user_preferences('uca_mycourses_bookmarks') != null);
}
/**
* Check if the current user has course bookmarks json defined in the database with active bookmarks (at least one course has been added to the bookmarks).
* @return boolean true if the current user has bookmarks and false in other cases.
*/
function has_active_bookmarks() {
if (user_has_bookmarks()) {
$json_default = sprintf('[{"text":"%s", "type":"root","children":[]}]', get_string('bookmarks_root_folder', 'local_uca_mycourses'));
$bookmarks_bdd = get_user_preferences('uca_mycourses_bookmarks');
// The json equals the default json used as model <=> no active bookmark.
if ($bookmarks_bdd === $json_default) {
return false;
}
$array = json_decode($bookmarks_bdd);
foreach ($array[0]->children as $child) {
if ($child->type == 'bookmark') {
return true;
}
if (isset($child->children)) {
foreach ($child->children as $grandchild) {
if ($grandchild->type == 'bookmark') {
return true;
}
}
}
}
return false;
} else {
return false;
}
}
/**
* Check if we display or not the user's bookmarks in the block. We display them if bookmarks exist and the user has not given cons-indications.
* @return boolean true if the bookmarks can be display (true by default) and false in other cases
*/
function show_bookmarks() {
if (!has_active_bookmarks()) {
return false;
}
return (get_user_preferences('uca_mycourses_show_bookmarks', '?') == '?') ? true
: (get_user_preferences('uca_mycourses_show_bookmarks') != null && get_user_preferences('uca_mycourses_show_bookmarks') != "0");
}
/**
* Check if we the current user want to follow course name in his bookmarks names.
* @return boolean true if he wants to and false (by default) in other cases
*/
function update_bookmarks_names()
{
if (!has_active_bookmarks()) {
return false;
}
return (get_user_preferences('uca_mycourses_update_bookmarks_names') === null) ? false
: (get_user_preferences('uca_mycourses_update_bookmarks_names') != null && get_user_preferences('uca_mycourses_update_bookmarks_names') != "0");
}
/**
* Returns the user's bookmarks on a json format used by the jstree plugin.
* @return string json string which represents the user's bookmarks used by the jstree plugin.
* (or a default json if the user has no bookmarks).
*/
function get_mybookmarks_json_tree() {
$json_default = sprintf('[{"text":"%s", "type":"root","children":[]}]', get_string('bookmarks_root_folder', 'local_uca_mycourses'));
return (user_has_bookmarks()) ? get_user_preferences('uca_mycourses_bookmarks') : $json_default;
}
/**
* Check if the course in parameters is in the current user's list of bookmarks.
* @param $course the course to test.
* @return boolean true if the given course is in the bookmarks' list of the current user.
*/
function course_bookmarked($course) {
if (!user_has_bookmarks()) {
return false;
}
$bookmarks = json_decode(get_user_preferences('uca_mycourses_bookmarks'));
foreach ($bookmarks[0]->children as $b) {
if ($b->type == 'bookmark') {
if($b->data->id == $course->id) {
return true;
}
}
if ($b->type == 'folder') {
foreach ($b->children as $c) {
if ($c->data->id == $course->id) {
return true;
}
}
}
}
return false;
}
//======================================================================
// UCA_MYCOURSES BLOCK FUNCTIONS
//======================================================================
/**
* Returns the type of view for the part 'My courses' of the block according to the user preferences.
* By default if we have more than x courses then we use the "tree" view else we use the "list" view.
* @return string the view type
*/
function get_uca_mycourses_block_view() {
return (!is_null(get_user_preferences('uca_mycourses_view'))) ? get_user_preferences('uca_mycourses_view')
: ((count(get_my_courses_list()) > get_config('block_uca_mycourses', 'list_view_limit')) ? 'tree' : 'list');
}
/**
* Returns if the user wants to display the finished courses or not in his block.
* By default the value is set to true.
* @return bool the view type
*/
function get_uca_mycourses_finished_courses_display()
{
return (key_exists('uca_mycourses_display_finished_courses', get_user_preferences())) ? boolval(get_user_preferences('uca_mycourses_display_finished_courses')) : true;
}
/**
* Returns the content of the block "My courses".
* @return string the content of the block.
*/
function local_uca_mycourses_render_block_output($page) {
global $CFG;
require_once($CFG->dirroot.'/local/uca_mycourses/classes/uca_renderer.php');
require_once($CFG->dirroot.'/local/uca_mycourses/classes/uca_url_helper.php');
$renderer = new uca_renderer($page);
$page->requires->jquery();
$page->requires->css('/local/uca_mycourses/styles.css');
$show_bookmarks = show_bookmarks();
$json_my_bookmarks = get_mybookmarks_json_tree();
$page->requires->css('/local/uca_mycourses/jstree/dist/themes/default/style.min.css');
$page->requires->js('/local/uca_mycourses/jstree/dist/jstree.js', true);
if (get_uca_mycourses_block_view() == 'tree') {
$json_my_courses = get_my_courses_json_tree(false);
$my_courses = array();
} else {
$my_courses = get_my_courses_list(false);
$json_my_courses = '';
}
$finished_courses = get_uca_mycourses_finished_courses_display();
$content = $renderer->render_from_template('local_uca_mycourses/render_block', array(
'show_bookmarks' => $show_bookmarks,
'finished_courses' => $finished_courses,
'visible' => (count($my_courses) > 0 || $json_my_courses != null) ? true : null,
'json_courses' => $json_my_courses,
'courses' => array_values($my_courses),
'json_bookmarks' => $json_my_bookmarks,
));
return $content;
}