-
Notifications
You must be signed in to change notification settings - Fork 28
/
lib.php
833 lines (720 loc) · 29.5 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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
<?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/>.
/**
* Library of interface functions and constants for module ratingallocate
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
* All the ratingallocate specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod_ratingallocate
* @copyright 2014 M Schulze, T Reischmann, C Usener
* @copyright based on code by Stefan Koegel copyright (C) 2013 Stefan Koegel
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* example constant
*/
define('RATINGALLOCATE_MOD_NAME', 'ratingallocate');
define('RATINGALLOCATE_EVENT_TYPE_START', 'start');
define('RATINGALLOCATE_EVENT_TYPE_STOP', 'stop');
require_once(dirname(__FILE__) . '/db/db_structure.php');
require_once(dirname(__FILE__) . '/locallib.php');
use mod_ratingallocate\db as this_db;
// //////////////////////////////////////////////////////////////////////////////
// Moodle core API //
// //////////////////////////////////////////////////////////////////////////////
/**
* Returns the information on whether the module supports a feature
*
* @param string $feature
* FEATURE_xx constant for requested feature
* @return mixed true if the feature is supported, null if unknown
* @see plugin_supports() in lib/moodlelib.php
*/
function ratingallocate_supports($feature) {
if (defined('FEATURE_MOD_PURPOSE')) {
if ($feature == FEATURE_MOD_PURPOSE) {
return MOD_PURPOSE_ADMINISTRATION;
}
}
switch ($feature) {
case FEATURE_MOD_INTRO :
return true;
case FEATURE_SHOW_DESCRIPTION :
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
default :
return null;
}
}
/**
* Saves a new instance of the ratingallocate into the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param object $ratingallocate
* An object from the form in mod_form.php
* @param mod_ratingallocate_mod_form $mform
* @return int The id of the newly inserted ratingallocate record
*/
function ratingallocate_add_instance(stdClass $ratingallocate, ?mod_ratingallocate_mod_form $mform) {
global $DB, $COURSE;
$ratingallocate->timecreated = time();
$transaction = $DB->start_delegated_transaction();
try {
$ratingallocate->{this_db\ratingallocate::SETTING} = json_encode($ratingallocate->strategyopt);
// Insert instance to get ID for children.
$id = $DB->insert_record(this_db\ratingallocate::TABLE, $ratingallocate);
$ratingallocate->id = $id;
ratingallocate_set_events($ratingallocate);
$transaction->allow_commit();
return $id;
} catch (Exception $e) {
$transaction->rollback($e);
}
}
/**
* Updates an instance of the ratingallocate in the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param object $ratingallocate
* An object from the form in mod_form.php
* @param mod_ratingallocate_mod_form $mform
* @return boolean Success/Fail
*/
function ratingallocate_update_instance(stdClass $ratingallocate, ?mod_ratingallocate_mod_form $mform) {
global $DB;
$ratingallocate->timemodified = time();
$ratingallocate->id = $ratingallocate->instance;
try {
$transaction = $DB->start_delegated_transaction();
// Serialize strategy settings.
$ratingallocate->setting = json_encode($ratingallocate->strategyopt);
$bool = $DB->update_record('ratingallocate', $ratingallocate);
// Create or update the new events.
ratingallocate_set_events($ratingallocate);
$transaction->allow_commit();
return $bool;
} catch (Exception $e) {
$transaction->rollback($e);
}
}
/**
* Removes an instance of the ratingallocate from the database
*
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id
* Id of the module instance
* @return boolean Success/Failure
*/
function ratingallocate_delete_instance($id) {
global $DB;
if (!$ratingallocate = $DB->get_record('ratingallocate', [
'id' => $id,
])) {
return false;
}
// Delete any dependent records here # .
$DB->delete_records('ratingallocate_allocations', [
'ratingallocateid' => $ratingallocate->id,
]);
$deleteids = array_keys($DB->get_records('ratingallocate_choices', [
'ratingallocateid' => $ratingallocate->id,
], '', 'id'));
if (!empty($deleteids)) {
list ($insql, $params) = $DB->get_in_or_equal($deleteids);
$DB->delete_records_select('ratingallocate_group_choices',
'choiceid ' . $insql, $params);
$DB->delete_records_select('ratingallocate_ch_gengroups',
'choiceid ' . $insql, $params);
}
$DB->delete_records('ratingallocate_groupings', [
'ratingallocateid' => $ratingallocate->id,
]);
$DB->delete_records_list('ratingallocate_ratings', 'choiceid', $deleteids);
$DB->delete_records('ratingallocate_choices', [
'ratingallocateid' => $ratingallocate->id,
]);
// Delete associated events.
$DB->delete_records('event', ['modulename' => 'ratingallocate', 'instance' => $ratingallocate->id]);
$DB->delete_records('ratingallocate', [
'id' => $ratingallocate->id,
]);
return true;
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in ratingallocate activities and print it out.
* Return true if there was output, or false is there was none.
*
* @return boolean
*/
function ratingallocate_print_recent_activity($course, $viewfullnames, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Prepares the recent activity data
*
* This callback function is supposed to populate the passed array with
* custom activity records. These records are then rendered into HTML via
* {@link ratingallocate_print_recent_mod_activity()}.
*
* @param array $activities sequentially indexed array of objects with the 'cmid' property
* @param int $index the index in the $activities to use for the next record
* @param int $timestart append activity since this time
* @param int $courseid the id of the course we produce the report for
* @param int $cmid course module id
* @param int $userid check for a particular user's activity only, defaults to 0 (all users)
* @param int $groupid check for a particular group's activity only, defaults to 0 (all groups)
* @return void adds items into $activities and increases $index
*/
function ratingallocate_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid,
$userid = 0, $groupid = 0) {
}
/**
* Prints single activity item prepared by {@see ratingallocate_get_recent_mod_activity()}
*
* @return void
*/
function ratingallocate_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
}
/**
* Returns all other caps used in the module
*
* @return array e.g. ['moodle/site:accessallgroups']
*/
function ratingallocate_get_extra_capabilities() {
return[];
}
// //////////////////////////////////////////////////////////////////////////////
// File API //
// //////////////////////////////////////////////////////////////////////////////
/**
* Returns the lists of all browsable file areas within the given module context
*
* The file area 'intro' for the activity introduction field is added automatically
* by {@link file_browser::get_file_info_context_module()}
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @return array of [(string)filearea] => (string)description
*/
function ratingallocate_get_file_areas($course, $cm, $context) {
return[];
}
/**
* File browsing support for ratingallocate file areas
*
* @param file_browser $browser
* @param array $areas
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info instance or null if not found
* @package mod_ratingallocate
* @category files
*
*/
function ratingallocate_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
return null;
}
/**
* Serves the files from the ratingallocate file areas
*
* @param stdClass $course
* the course object
* @param stdClass $cm
* the course module object
* @param stdClass $context
* the ratingallocate's context
* @param string $filearea
* the name of the file area
* @param array $args
* extra arguments (itemid, path)
* @param bool $forcedownload
* whether or not force download
* @param array $options
* additional options affecting the file serving
* @category files
*
* @package mod_ratingallocate
*/
function ratingallocate_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload, array $options = []) {
global $DB, $CFG;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
if ($filearea !== 'choice_attachment') {
return false;
}
require_login($course, true, $cm);
if (!has_capability('mod/ratingallocate:view', $context)) {
return false;
}
$itemid = array_shift($args);
$filename = array_pop($args);
if (!$args) {
// Empty path, use root.
$filepath = '/';
} else {
// Assemble filepath.
$filepath = '/' . implode('/', $args) . '/';
}
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'mod_ratingallocate', $filearea, $itemid, $filepath, $filename);
if (!$file) {
return false; // The file does not exist.
}
// Send the file to the browser. Cache lifetime of 1 day, no filtering.
send_stored_file($file, 86400, 0, $forcedownload, $options);
}
// //////////////////////////////////////////////////////////////////////////////
// Navigation API //
// //////////////////////////////////////////////////////////////////////////////
/**
* Extends the global navigation tree by adding ratingallocate nodes if there is a relevant content
*
* This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
*
* @param navigation_node $navref
* An object representing the navigation tree node of the ratingallocate module instance
* @param stdClass $course
* @param stdClass $module
* @param cm_info $cm
*/
function ratingallocate_extend_navigation(navigation_node $navref, stdclass $course, stdclass $module, cm_info $cm) {
}
/**
* Extends the settings navigation with the ratingallocate settings
*
* This function is called when the context for the page is a ratingallocate module. This is not called by AJAX
* so it is safe to rely on the $PAGE.
*
* @param settings_navigation $settingsnav
* {@link settings_navigation}
* @param navigation_node $ratingallocatenode
* {@link navigation_node}
*/
function ratingallocate_extend_settings_navigation(settings_navigation $settingsnav, ?navigation_node $ratingallocatenode) {
$hassecondary = $settingsnav->get_page()->has_secondary_navigation();
if (!$context = context_module::instance($settingsnav->get_page()->cm->id, IGNORE_MISSING)) {
throw new \moodle_exception('badcontext');
}
if (has_capability('mod/ratingallocate:modify_choices', $context)) {
$choicenode = navigation_node::create(get_string('choice_navigation', RATINGALLOCATE_MOD_NAME),
new moodle_url('/mod/ratingallocate/view.php', ['id' => $settingsnav->get_page()->cm->id,
'action' => ACTION_SHOW_CHOICES]),
navigation_node::TYPE_CUSTOM, null, 'mod_ratingallocate_choices');
$ratingallocatenode->add_node($choicenode);
}
if (has_capability('mod/ratingallocate:start_distribution', $context)) {
$reportsnode = navigation_node::create(get_string('reports_group', RATINGALLOCATE_MOD_NAME),
new moodle_url('/mod/ratingallocate/view.php', ['id' => $settingsnav->get_page()->cm->id,
'action' => ACTION_SHOW_RATINGS_AND_ALLOCATION_TABLE]),
navigation_node::TYPE_CUSTOM, null, 'mod_ratingallocate_reports');
$ratingallocatenode->add_node($reportsnode);
}
}
/**
* This standard function will check all instances of this module
* and make sure there are up-to-date events created for each of them.
* If courseid = 0, then every ratingallocate event in the site is checked, else
* only ratingallocate events belonging to the course specified are checked.
* This function is used, in its new format, by restore_refresh_events()
*
* @param int $courseid
* @param int|stdClass $instance Ratingallocate module instance or ID.
* @param int|stdClass $cm Course module object or ID (not used in this module).
* @return bool
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
function ratingallocate_refresh_events($courseid = 0, $instance = null, $cm = null): bool {
global $DB;
// If we have instance information then we can just update the one event instead of updating all events.
if (isset($instance)) {
if (!is_object($instance)) {
$instance = $DB->get_record('ratingallocate', ['id' => $instance], '*', MUST_EXIST);
}
ratingallocate_set_events($instance);
return true;
}
if ($courseid) {
if (! $ratingallocates = $DB->get_records('ratingallocate', ['course' => $courseid])) {
return true;
}
} else {
if (! $ratingallocates = $DB->get_records('ratingallocate')) {
return true;
}
}
foreach ($ratingallocates as $ratingallocate) {
ratingallocate_set_events($ratingallocate);
}
return true;
}
/**
* Creates events for accesstimestart and accestimestop of a ratingallocate instance
*
* @param $ratingallocate
* @return void
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
function ratingallocate_set_events($ratingallocate) {
global $DB, $CFG;
require_once($CFG->dirroot.'/calendar/lib.php');
// Get CMID if not sent as part of $ratingallocate.
if (!isset($ratingallocate->coursemodule)) {
$cm = get_fast_modinfo($ratingallocate->course)->instances['ratingallocate'][$ratingallocate->id];
$ratingallocate->coursemodule = $cm->id;
}
// Ratingallocate-accessstart calendar events.
$eventid = $DB->get_field('event', 'id',
['modulename' => 'ratingallocate', 'instance' => $ratingallocate->id, 'eventtype' => RATINGALLOCATE_EVENT_TYPE_START]);
$timestart = $DB->get_field('ratingallocate', 'accesstimestart', ['id' => $ratingallocate->id]);
if (isset($timestart) && $timestart > 0) {
$event = new stdClass();
$event->eventtype = RATINGALLOCATE_EVENT_TYPE_START;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->name = get_string('calendarstart', RATINGALLOCATE_MOD_NAME, $ratingallocate->name);
$event->description = format_module_intro('ratingallocate', $ratingallocate, $ratingallocate->coursemodule, false);
$event->format = FORMAT_HTML;
$event->instance = $ratingallocate->id;
$event->timestart = $timestart;
$event->timesort = $timestart;
// Visibility should depend on the user.
if (isset($ratingallocate->visible)) {
$event->visible = $ratingallocate->visible;
} else {
$event->visible = instance_is_visible('ratingallocate', $ratingallocate);
}
$event->timeduration = 0;
if ($eventid) {
// Calendar event exists so update it.
$event->id = $eventid;
$calendarevent = calendar_event::load($event->id);
$calendarevent->update($event, false);
} else {
// Event doesn't exist so create one.
$event->courseid = $ratingallocate->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'ratingallocate';
$event->instance = $ratingallocate->id;
$event->eventtype = RATINGALLOCATE_EVENT_TYPE_START;
calendar_event::create($event, false);
}
} else if ($eventid) {
// Delete calendarevent as it is no longer needed.
$calendarevent = calendar_event::load($eventid);
$calendarevent->delete();
}
// Ratingallocate-accessstop calendar events.
$eventid = $DB->get_field('event', 'id',
['modulename' => 'ratingallocate', 'instance' => $ratingallocate->id, 'eventtype' => RATINGALLOCATE_EVENT_TYPE_STOP]);
$timestop = $DB->get_field('ratingallocate', 'accesstimestop', ['id' => $ratingallocate->id]);
if (isset($timestop) && $timestop > 0) {
$event = new stdClass();
$event->eventtype = RATINGALLOCATE_EVENT_TYPE_STOP;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->name = get_string('calendarstop', RATINGALLOCATE_MOD_NAME, $ratingallocate->name);
$event->description = format_module_intro('ratingallocate', $ratingallocate, $ratingallocate->coursemodule, false);
$event->format = FORMAT_HTML;
$event->instance = $ratingallocate->id;
$event->timestart = $timestop;
$event->timesort = $timestop;
// Visibility should depend on the user.
if (isset($ratingallocate->visible)) {
$event->visible = $ratingallocate->visible;
} else {
$event->visible = instance_is_visible('ratingallocate', $ratingallocate);
}
$event->timeduration = 0;
if ($eventid) {
// Calendar event exists so update it.
$event->id = $eventid;
$calendarevent = calendar_event::load($event->id);
$calendarevent->update($event, false);
} else {
// Event doesn't exist so create one.
$event->courseid = $ratingallocate->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'ratingallocate';
$event->instance = $ratingallocate->id;
calendar_event::create($event, false);
}
} else if ($eventid) {
// Delete calendarevent as it is no longer needed.
$calendarevent = calendar_event::load($eventid);
$calendarevent->delete();
}
}
/**
* Is the event visible?
*
* @param calendar_event
* @return bool
* @throws moodle_exception
* @throws dml_exception
*/
function mod_ratingallocate_core_is_event_visible(calendar_event $event): bool {
global $DB, $USER;
$instance = $event->instance;
if (!$instance) {
return false;
}
$ratingallocaterecord = $DB->get_record('ratingallocate', ['id' => $instance]);
$modinfo = get_fast_modinfo($event->courseid)->instances['ratingallocate'][$instance];
$context = context_module::instance($modinfo->id);
$course = get_course($event->courseid);
$ratingallocate = new ratingallocate($ratingallocaterecord, $course, $modinfo, $context);
$raters = $ratingallocate->get_raters_in_course();
return in_array($USER, $raters);
}
/**
* This function will update the ratingallocate module according to the event that has been modified.
*
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
function mod_ratingallocate_core_calendar_event_timestart_updated (\calendar_event $event, \stdClass $ratingallocate) {
global $CFG, $DB;
if (empty($event->eventtype) || $event->modulename != 'ratingallocate') {
return;
}
if ($event->instance != $ratingallocate->id) {
return;
}
if (!in_array($event->eventtype, [RATINGALLOCATE_EVENT_TYPE_STOP, RATINGALLOCATE_EVENT_TYPE_START])) {
return;
}
$courseid = $event->courseid;
$modulename = $event->modulename;
$instanceid = $event->instance;
$modified = false;
$coursemodule = get_fast_modinfo($courseid)->instances[$modulename][$instanceid];
$context = context_module::instance($coursemodule->id);
// The user does not have the capability to modify this activity.
if (!has_capability('moodle/course:manageactivities', $context)) {
return;
}
$timeopen = $DB->get_field('ratingallocate', 'accesstimestart', ['id' => $ratingallocate->id]);
$timeclose = $DB->get_field('ratingallocate', 'accesstimestop', ['id' => $ratingallocate->id]);
// Modify the dates for accesstimestart and accesstimestop if the event was dragged.
if ($event->eventtype == RATINGALLOCATE_EVENT_TYPE_START) {
if ($timeopen != $event->timestart) {
$ratingallocate->accesstimestart = $event->timestart;
$modified = true;
}
} else if ($event->eventtype == RATINGALLOCATE_EVENT_TYPE_STOP) {
if ($timeclose != $event->timestart) {
$ratingallocate->accesstimestop = $event->timestart;
$publishtime = $DB->get_field('ratingallocate', 'publishdate', ['id' => $ratingallocate->id]);
// Modify the estimated publication date if it is now before the accesstimestop.
if ($publishtime && $publishtime <= $ratingallocate->accesstimestop) {
$ratingallocate->publishdate = $ratingallocate->accesstimestop + 2 * 24 * 60 * 60;
}
$modified = true;
}
}
if ($modified) {
$ratingallocate->timemodified = time();
$DB->update_record('ratingallocate', $ratingallocate);
$event = \core\event\course_module_updated::create_from_cm($coursemodule, $context);
$event->trigger();
}
}
/**
* Calculates the minimum and maximum range of dates this event can be in
* according to the settings of the ratingallocate instance.
*
* @param calendar_event $event
* @param stdClass $instance
* @return array
* @throws coding_exception
* @throws dml_exception
*/
function mod_ratingallocate_core_calendar_get_valid_event_timestart_range (\calendar_event $event, \stdClass $instance): array {
global $DB;
$mindate = null;
$maxdate = null;
$timeopen = $DB->get_field('ratingallocate', 'accesstimestart', ['id' => $instance->id]);
$timeclose = $DB->get_field('ratingallocate', 'accesstimestop', ['id' => $instance->id]);
if ($event->eventtype == RATINGALLOCATE_EVENT_TYPE_START) {
if (!empty($timeclose)) {
$maxdate = [$timeclose, get_string('openafterclose', RATINGALLOCATE_MOD_NAME)];
}
} else if ($event->eventtype == RATINGALLOCATE_EVENT_TYPE_STOP) {
if (!empty($timeopen)) {
$mindate = [$timeopen, get_string('closebeforeopen', RATINGALLOCATE_MOD_NAME)];
}
}
return [$mindate, $maxdate];
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
* This function will remove all ratings and allocations
* and clean up any related data.
*
* @param $data stdClass the data submitted from the reset course.
* @return array status array
*/
function ratingallocate_reset_userdata($data) {
global $CFG, $DB;
$componentstr = get_string('modulenameplural', RATINGALLOCATE_MOD_NAME);
$status = [];
$params = ['courseid' => $data->courseid];
if (!empty($data->reset_ratings_and_allocations)) {
// Delete all ratings.
$ratingidssql = "SELECT r.id FROM {ratingallocate_ratings} r
INNER JOIN {ratingallocate_choices} c ON r.choiceid=c.id
INNER JOIN {ratingallocate} ra ON c.ratingallocateid=ra.id
WHERE ra.course= :courseid";
$DB->delete_records_select('ratingallocate_ratings', "id IN ($ratingidssql)", $params);
// Delete all allocations.
$allocationidssql = "SELECT a.id FROM {ratingallocate_allocations} a
INNER JOIN {ratingallocate} r ON a.ratingallocateid=r.id
WHERE r.course= :courseid";
$DB->delete_records_select('ratingallocate_allocations', "id IN ($allocationidssql)", $params);
$status[] = [
'component' => $componentstr,
'item' => get_string('ratings_and_allocations_deleted', RATINGALLOCATE_MOD_NAME),
'error' => false];
}
// Updating dates - shift may be negative too.
if ($data->timeshift) {
// Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
// See MDL-9367.
shift_course_mod_dates(RATINGALLOCATE_MOD_NAME, ['accesstimestart', 'accesstimestop'], $data->timeshift, $data->courseid);
$status[] = ['component' => $componentstr, 'item' => get_string('datechanged'), 'error'
=> false];
}
return $status;
}
/**
* Called by course/reset.php.
*
* @param MoodleQuickForm $mform form passed by reference
*/
function ratingallocate_reset_course_form_definition($mform) {
$mform->addElement('header', 'ratingallocateheader', get_string('modulenameplural', RATINGALLOCATE_MOD_NAME));
$mform->addElement('advcheckbox', 'reset_ratings_and_allocations',
get_string('remove_ratings_and_allocations', RATINGALLOCATE_MOD_NAME));
}
/**
* Course reset form defaults.
* @return array the defaults.
*/
function ratingallocate_reset_course_form_defaults($course) {
return ['reset_ratings_and_allocations' => 1];
}
/**
* Add a get_coursemodule_info function in case any ratingallocate type wants to add 'extra' information
* for the course (see resource).
*
* Given a course_module object, this function returns any "extra" information that may be needed
* when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
*
* @param stdClass $coursemodule The coursemodule object (record).
* @return cached_cm_info An object on information that the courses
* will know about (most noticeably, an icon).
* @throws dml_exception
*/
function ratingallocate_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, intro, introformat, accesstimestart, accesstimestop, completionvote, completionallocation';
if (!$ratingallocate = $DB->get_record(RATINGALLOCATE_MOD_NAME, $dbparams, $fields)) {
return false;
}
$result = new cached_cm_info();
$result->name = $ratingallocate->name;
if ($coursemodule->showdescription) {
// Convert intro to html. Do not filter cached version, filters run at display time.
$result->content = format_module_intro(RATINGALLOCATE_MOD_NAME, $ratingallocate, $coursemodule->id, false);
}
// Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionvote'] = $ratingallocate->completionvote;
$result->customdata['customcompletionrules']['completionallocation'] = $ratingallocate->completionallocation;
}
// Populate some other values that can be used in calendar or on dashboard.
if ($ratingallocate->accesstimestart) {
$result->customdata['accesstimestart'] = $ratingallocate->accesstimestart;
}
if ($ratingallocate->accesstimestop) {
$result->customdata['accesstimestop'] = $ratingallocate->accesstimestop;
}
return $result;
}
/**
* Callback which returns human-readable strings describing the active completion custom rules for the module instance.
*
* @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules']
* @return array $descriptions the array of descriptions for the custom rules.
*/
function mod_ratingallocate_get_completion_active_rule_descriptions($cm) {
// Values will be present in cm_info, and we assume these are up to date.
if (empty($cm->customdata['customcompletionrules']) || $cm->completion != COMPLETION_TRACKING_AUTOMATIC) {
return [];
}
$descriptions = [];
foreach ($cm->customdata['customcompletionrules'] as $key => $val) {
switch ($key) {
case 'completionvote':
if ($val == 1) {
$descriptions[] = get_string('copletionvotedesc', RATINGALLOCATE_MOD_NAME);
}
break;
case 'completionallocation':
if ($val == 1) {
$descriptions[] = get_string('copletionallocationdesc', RATINGALLOCATE_MOD_NAME);
}
break;
default:
break;
}
}
return $descriptions;
}