-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.php
616 lines (577 loc) · 21.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<?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/>.
/**
* Local plugin "Learning Tools" - lib file.
*
* @package local_learningtools
* @copyright bdecent GmbH 2021
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_user\output\myprofile\tree;
/**
* Defines learningtools nodes for my profile navigation tree.
*
* @param \core_user\output\myprofile\tree $tree Tree object
* @param stdClass $user user object
* @param bool $iscurrentuser is the user viewing profile, current user ?
* @param stdClass $course course object
*
* @return bool
*/
function local_learningtools_myprofile_navigation(tree $tree, $user, $iscurrentuser, $course) {
// Get the learningtools category.
if (!array_key_exists('learningtools', $tree->__get('categories'))) {
// Create the category.
$categoryname = get_string('learningtools', 'local_learningtools');
$category = new core_user\output\myprofile\category('learningtools', $categoryname, 'privacyandpolicies');
$tree->add_category($category);
} else {
// Get the existing category.
$category = $tree->__get('categories')['learningtools'];
}
return true;
}
/**
* Adds ltools action in each page to the given navigation node if caps are met.
*
* @param object $settingnav navigation node
* @param object $context context
* @return navigation_node Returns the question branch that was added
*/
function local_learningtools_extend_settings_navigation($settingnav, $context) {
global $PAGE, $CFG;
$context = context_system::instance();
$ltoolsjs = array();
// Content of fab button html.
$fabbuttonhtml = json_encode(local_learningtools_get_learningtools_info());
$ltoolsjs['disappertimenotify'] = get_config('local_learningtools', 'notificationdisapper');
$PAGE->requires->data_for_js('ltools', $ltoolsjs);
$PAGE->requires->data_for_js('fabbuttonhtml', $fabbuttonhtml, true);
$loggedin = false;
if (isloggedin() && !isguestuser()) {
$loggedin = true;
}
$viewcapability = array('loggedin' => $loggedin);
$PAGE->requires->js_call_amd('local_learningtools/learningtoolsinfo', 'init', $viewcapability);
// List of subplugins.
// Load available subplugins javascript.
$subplugins = local_learningtools_get_subplugins();
foreach ($subplugins as $shortname => $plugin) {
if (method_exists($plugin, 'load_js')) {
$plugin->load_js();
}
// Required load tools function.
if (method_exists($plugin, 'required_load_data')) {
$plugin->required_load_data();
}
}
}
/**
* Get the type of instance.
* @param object $record list of the page info.
* @return object instance object.
*/
function local_learningtools_check_instanceof_block($record) {
$data = new stdClass;
if ($record->contextlevel == CONTEXT_SYSTEM) { // System level.
$data->instance = 'system';
} else if ($record->contextlevel == CONTEXT_USER) { // User level.
$data->instance = 'user';
} else if ($record->contextlevel == CONTEXT_COURSE) { // Course level.
$data->instance = 'course';
$data->courseid = $record->course;
$data->contextid = $record->contextid;
} else if ($record->contextlevel == CONTEXT_MODULE) { // Mod level.
$data->instance = 'mod';
$data->courseid = $record->course;
$data->contextid = $record->contextid;
$data->coursemodule = local_learningtools_get_coursemodule_id($record);
} else if ($record->contextlevel == CONTEXT_BLOCK) { // Context blocklevel.
$data->instance = 'block';
} else {
$data->instance = '';
}
return $data;
}
/**
* Get the course module id.
* @param int $contextid context id
* @param int $contextlevel context level
* @return int course module id
*/
function local_learningtools_get_moduleid($contextid, $contextlevel) {
$coursemodule = 0;
if ($contextlevel == CONTEXT_MODULE) {
$record = new stdClass;
$record->contextid = $contextid;
$record->contextlevel = $contextlevel;
$coursemodule = local_learningtools_get_coursemodule_id($record);
}
return $coursemodule;
}
/**
* Get the course module Id.
* @param stdclass $record list of the page info.
* @return int course module id.
*/
function local_learningtools_get_coursemodule_id($record) {
global $DB;
$contextinfo = $DB->get_record('context', array('id' => $record->contextid, 'contextlevel' => $record->contextlevel));
return isset($contextinfo->instanceid) ? $contextinfo->instanceid : 0;
}
/**
* Get the courses name.
* @param array $courses courseids
* @param string $url page url
* @param int $selectcourse selected course id
* @param int $userid user id.
* @param int $usercourseid course id.
* @return array list of the course info.
*/
function local_learningtools_get_courses_name($courses, $url = '', $selectcourse = 0, $userid= 0, $usercourseid = 0) {
$courseids = [];
$courseinfo = [];
$courseids = $courses;
if (!empty($courseids)) {
foreach ($courseids as $courseid) {
$course = get_course($courseid);
$course = new core_course_list_element($course);
if ($url) {
$list = [];
$list['id'] = $course->id;
$list['name'] = $course->get_formatted_name();
$urlparams = array('selectcourse' => $course->id);
if ($userid) {
$urlparams['userid'] = $userid;
}
if ($usercourseid) {
$urlparams['courseid'] = $usercourseid;
}
$url = new moodle_url($url, $urlparams);
$list['url'] = $url->out(false);
if ($course->id == $selectcourse) {
$list['selected'] = "selected";
} else {
$list['selected'] = "";
}
$courseinfo[] = $list;
} else {
$courseinfo[$course->id] = $course->get_formatted_name();
}
}
}
return $courseinfo;
}
/**
* Get the course name.
* @param int $courseid course id
* @return string course name
*/
function local_learningtools_get_course_name($courseid) {
$course = get_course($courseid);
$course = new core_course_list_element($course);
return $course->get_formatted_name();
}
/**
* Get the course category name.
* @param int $courseid course id.
* @return string category name.
*/
function local_learningtools_get_course_categoryname($courseid) {
$course = get_course($courseid);
$category = \core_course_category::get($course->category);
return $category->get_formatted_name();
}
/**
* Get the course module name
* @param object $data instance data
* @param bool $mod return which type of name
* @return string modulename | instance name
*/
function local_learningtools_get_module_name($data, $mod = false) {
global $DB;
$coursemoduleinfo = $DB->get_record('course_modules', array('id' => $data->coursemodule));
if (empty($coursemoduleinfo)) {
return "";
}
$moduleinfo = $DB->get_record('modules', array('id' => $coursemoduleinfo->module));
if ($moduleinfo) {
if ($mod) {
return $moduleinfo->name;
}
// Get module instance name.
$report = get_coursemodule_from_instance($moduleinfo->name, $coursemoduleinfo->instance, $data->courseid);
return $report->name;
}
return "";
}
/**
* Get the course module current section.
* @param int $courseid course id
* @param int $modid coursemodule id
* @return string|bool section name.
*/
function local_learningtools_get_mod_section($courseid, $modid) {
global $DB;
$sections = $DB->get_records('course_sections', array('course' => $courseid));
$sectionname = [];
$sectionmod = [];
if (!empty($sections)) {
foreach ($sections as $key => $value) {
$sequence = '';
if (!empty($value->name)) {
$sectionname[$value->id] = $value->name;
} else {
if ($value->section == 0) {
$sectionname[$value->id] = get_string('general', 'local_learningtools');
} else {
$sectionname[$value->id] = get_string('topic', 'local_learningtools', $value->section);
}
}
if ($value->sequence) {
$sequence = explode(',', $value->sequence);
}
$sectionmod[$value->id] = isset($sequence) ? $sequence : '';
}
}
if ($sectionname && $sectionmod) {
foreach ($sectionmod as $key => $value) {
if (!empty($value)) {
if ( is_numeric(array_search($modid, $value)) ) {
return $sectionname[$key];
}
}
}
}
return '';
}
/**
* Get list of available sub plugins.
*
* @return array $plugins List of available subplugins.
*/
function local_learningtools_get_subplugins() {
global $DB, $PAGE, $SITE;
$learningtools = $DB->get_records('local_learningtools_products', array('status' => 1), 'sort');
if (!empty($learningtools)) {
foreach ($learningtools as $tool) {
$plugin = 'ltool_'.$tool->shortname;
$classname = "\\$plugin\\$tool->shortname";
if (class_exists($classname)) {
$plugins[$tool->shortname] = new $classname();
}
}
return isset($plugins) ? $plugins : [];
}
return [];
}
/**
* Display fab button html.
* @return string fab button html content.
*/
function local_learningtools_get_learningtools_info() {
global $PAGE, $SITE, $USER;
$content = '';
// Visiblity of learningtools.
$fabvisiablestatus = get_config('local_learningtools', 'fabbuttonvisible');
if ($fabvisiablestatus == 'allcourses') {
if (empty($PAGE->course->id) || ($PAGE->course->id == $SITE->id)) {
return '';
}
} else if ($fabvisiablestatus == 'specificcate') {
if (isset($PAGE->course->category) && !empty($PAGE->course->category)) {
$visiblecategories = explode(",", get_config('local_learningtools', 'visiblecategories'));
if (!in_array($PAGE->course->category, $visiblecategories)) {
return '';
}
} else {
return '';
}
}
// Disable of activities.
$disablemodstatus = get_config('local_learningtools', 'disablemod');
if ($disablemodstatus != 0) {
if (isset($PAGE->cm->module) && !empty($PAGE->cm->module)) {
$visiblemods = explode(",", get_config('local_learningtools', 'disablemod'));
if (in_array($PAGE->cm->module, $visiblemods)) {
return '';
}
}
}
$contentinner = '';
// Get list of ltool sub plugins.
$subplugins = local_learningtools_get_subplugins();
$context = context_system::instance();
$stickytools = '';
if (!empty($subplugins)) {
foreach ($subplugins as $shortname => $toolobj) {
$capability = 'ltool/'.$toolobj->shortname.':create'. $toolobj->shortname;
if ($toolobj->contextlevel == 'system') {
if (has_capability($capability, $context)) {
if (get_config('ltool_' . $toolobj->shortname, 'sticky')) {
$stickytools .= $toolobj->render_template();
} else if (get_config('local_learningtools', 'showactive')) {
if (method_exists($toolobj, 'tool_active_condition')) {
if ($toolobj->tool_active_condition()) {
$stickytools .= $toolobj->tool_active_condition();
} else {
$contentinner .= $toolobj->render_template();
}
}
} else {
$contentinner .= $toolobj->render_template();
}
}
} else {
if (get_config('ltool_' . $toolobj->shortname, 'sticky')) {
$stickytools .= $toolobj->render_template();
} else if (get_config('local_learningtools', 'showactive')) {
$activetool = $toolobj->render_template();
if (method_exists($toolobj, 'tool_active_condition')) {
if ($toolobj->tool_active_condition()) {
$stickytools .= $toolobj->tool_active_condition();
$activetool = '';
}
}
$contentinner .= $activetool;
} else {
$contentinner .= $toolobj->render_template();
}
}
}
}
$stickytoolstatus = local_learningtools_get_stickytool_status();
$stickyclass = '';
$fabiconclass = "fa fa-magic";
if ($stickytoolstatus && !empty($stickytools)) {
$fabiconclass = "fa fa-angle-double-up";
$stickyclass = 'sticky-tool';
}
$fabbackiconcolor = get_config('local_learningtools', 'fabiconbackcolor');
$fabiconcolor = get_config('local_learningtools', 'fabiconcolor');
$content .= html_writer::start_tag('div', array('class' => 'learningtools-action-info'));
$content .= html_writer::start_tag('div', array('class' => "floating-button $stickyclass"));
$content .= html_writer::start_tag('div', array('class' => 'list-learningtools'));
$content .= $contentinner;
$content .= html_writer::end_tag('div');
$content .= html_writer::start_tag('button', array("class" => "btn btn-primary",
'id' => 'tool-action-button', 'style' => "background:$fabbackiconcolor;") );
$content .= html_writer::start_tag('i', array('class' => $fabiconclass, 'style' => "color:$fabiconcolor;"));
$content .= html_writer::end_tag('i');
$content .= html_writer::end_tag("button");
$content .= html_writer::start_tag('div', array('class' => 'sticky-tools-list'));
$content .= $stickytools;
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('div');
return $content;
}
/**
* Get sticky tools.
* @return void
*/
function local_learningtools_get_stickytool_status() {
$subplugins = local_learningtools_get_subplugins();
$stickystatus = false;
if (!empty($subplugins)) {
foreach ($subplugins as $shortname => $toolobj) {
$capability = 'ltool/'.$toolobj->shortname.':create'. $toolobj->shortname;
if ($toolobj->contextlevel == 'system') {
if (has_capability($capability, context_system::instance())) {
if (get_config('ltool_' . $toolobj->shortname, 'sticky') ||
get_config('local_learningtools', 'showactive')) {
$stickystatus = true;
}
}
} else {
if (get_config('ltool_' . $toolobj->shortname, 'sticky') ||
get_config('local_learningtools', 'showactive')) {
$stickystatus = true;
}
}
}
}
return $stickystatus;
}
/**
* Get the students in course.
* @param int $courseid course id
* @return array students user ids.
*/
function local_learningtools_get_students_incourse($courseid) {
global $DB;
$coursecontext = context_course::instance($courseid);
$studentids = array_keys(get_enrolled_users($coursecontext, 'local/learningtools:studentcontroller'));
return $studentids;
}
/**
* Find the logged in user is assigned into any relative roles to the shared user.
* @param int $childuserid userid
* @param string $capability
* @return object|bool
*/
function local_learningtools_is_parentforchild(int $childuserid, string $capability='') {
global $USER;
$usercontext = \context_user::instance($childuserid); // USER - child id.
$usercontextroles = get_user_roles($usercontext, $USER->id); // Loggedin - parent.
if (!empty($capability)) {
return local_learningtools_has_viewtool_capability_role($usercontextroles, $capability);
}
return (!empty($usercontextroles)) ? $usercontextroles : false;
}
/**
* Check the tool capability for parents.
* @param array $assignedroles list of the roles.
* @param string $capability acces capability.
* @return bool stauts
*/
function local_learningtools_has_viewtool_capability_role($assignedroles, string $capability) {
$roles = [];
if (empty($assignedroles)) {
return false;
}
foreach ($assignedroles as $assignid => $role) {
$roles[] = $role->roleid;
}
$roleshascaps = get_roles_with_capability($capability);
$result = array_intersect($roles, array_keys($roleshascaps));
return !empty($result) ? true : false;
}
/**
* Get the tool instance view url.
* @param object $row list of the tool record
* @return string view html
*/
function local_learningtools_get_instance_tool_view_url($row) {
global $OUTPUT;
$data = local_learningtools_check_instanceof_block($row);
if (!isset($data->instance)) {
return '';
}
if ($data->instance == 'course') {
$courseurl = new moodle_url('/course/view.php', array('id' => $data->courseid));
$viewurl = $OUTPUT->single_button($courseurl, get_string('viewcourse', 'local_learningtools'), 'get');
} else if ($data->instance == 'mod') {
$viewurl = $OUTPUT->single_button($row->pageurl, get_string('viewactivity', 'local_learningtools'), 'get');
} else {
$viewurl = $OUTPUT->single_button($row->pageurl, get_string('viewpage', 'local_learningtools'), 'get');
}
return $viewurl;
}
/**
* Get the event level course id.
* @param object $context context object
* @param int $courseid related course id
* @return string view html
*/
function local_learningtools_get_eventlevel_courseid($context, $courseid) {
$course = 0;
if ($context->contextlevel == CONTEXT_COURSE || $context->contextlevel == CONTEXT_MODULE) {
return $courseid;
} else {
return $course;
}
}
/**
* Add the learningtools in db.
* @param string $plugin plugin name
* @return void
*/
function local_learningtools_add_learningtools_plugin($plugin) {
global $DB;
$strpluginname = get_string('pluginname', 'ltool_' . $plugin);
if (!$DB->record_exists('local_learningtools_products', array('shortname' => $plugin)) ) {
$existrecords = $DB->get_records('local_learningtools_products', null);
$maxrecord = $DB->get_record_sql('SELECT MAX(sort) AS value FROM {local_learningtools_products}', null);
$sortval = !empty($existrecords) ? $maxrecord->value + 1 : 1;
$record = new stdClass;
$record->shortname = $plugin;
$record->name = $strpluginname;
$record->status = 1;
$record->sort = $sortval;
$record->timecreated = time();
$DB->insert_record('local_learningtools_products', $record);
}
}
/**
* Remove the learningtools in db.
* @param string $plugin ltool plugin shortname
* @return void
*/
function local_learningtools_delete_ltool_table($plugin) {
global $DB;
if ($DB->record_exists('local_learningtools_products', array('shortname' => $plugin)) ) {
$DB->delete_records('local_learningtools_products', array('shortname' => $plugin));
}
}
/**
* Clean the assignment page userlist id.
* @param string $pageurl pageurl
* @param object $cm course module id.
* @return string pageurl
*/
function local_learningtools_clean_mod_assign_userlistid($pageurl, $cm) {
if (!empty($cm->id)) {
$data = new stdClass;
$data->coursemodule = $cm->id;
$modname = local_learningtools_get_module_name($data, true);
if ($modname == 'assign') {
$parsed = parse_url($pageurl);
if (isset($parsed['query'])) {
$query = $parsed['query'];
parse_str($query, $params);
unset($params['useridlistid']);
};
$url = $parsed['scheme'] . "://" . $parsed['host'] . $parsed['path'];
$urlparams = isset($parsed['query']) ? '?' . http_build_query($params, '', '&') : '';
$url = $url . $urlparams;
return $url;
} else {
return $pageurl;
}
} else {
return $pageurl;
}
}
/**
* Is the event visible?
*
* This is used to determine global visibility of an event in all places throughout Moodle.
*
* @param calendar_event $event
* @param int $requestinguserid User id to use for all capability checks, etc. Set to 0 for current user (default).
* @return bool Returns true if the event is visible to the current user, false otherwise.
*/
function local_learningtools_core_calendar_is_event_visible($event, $requestinguserid) {
global $DB;
$userid = $DB->get_field('event', 'userid', ['id' => $event->id]);
return ($userid == $requestinguserid) ? true : false;
}
/**
* Can access the Course tool for current page.
*
* @return void
*/
function local_learningtools_can_visible_tool_incourse() {
global $SITE, $PAGE, $USER;
$access = false;
if (!empty($PAGE->course->id) && $PAGE->course->id != $SITE->id) {
if (can_access_course($PAGE->course, $USER, '', true)) {
$access = true;
}
}
return $access;
}