-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
3512 lines (3087 loc) · 119 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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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 functions and constants for module evaluation
* includes the main-part of evaluation-functions
*
* @package mod_evaluation
* @copyright Andreas Grabs for mod_evaluation
* @copyright by Harry.Bleckert@ASH-Berlin.eu for ASH Berlin
* + forked from mod_feedback 12/2021
*
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/evaluation/locallib.php');
// Include forms lib.
require_once($CFG->libdir . '/formslib.php');
define('EVALUATION_ANONYMOUS_YES', 1);
define('EVALUATION_ANONYMOUS_NO', 2);
define('EVALUATION_MIN_ANONYMOUS_COUNT_IN_GROUP', 2);
define('EVALUATION_DECIMAL', '.');
define('EVALUATION_THOUSAND', ',');
define('EVALUATION_RESETFORM_RESET', 'evaluation_reset_data_');
define('EVALUATION_RESETFORM_DROP', 'evaluation_drop_evaluation_');
define('EVALUATION_MAX_PIX_LENGTH', '400'); //max. Breite des grafischen Balkens in der Auswertung
define('EVALUATION_DEFAULT_PAGE_COUNT', 20);
// Event types.
define('EVALUATION_EVENT_TYPE_OPEN', 'open');
define('EVALUATION_EVENT_TYPE_CLOSE', 'close');
/**
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
* @uses FEATURE_MOD_INTRO
* @uses FEATURE_COMPLETION_TRACKS_VIEWS
* @uses FEATURE_GRADE_HAS_GRADE
* @uses FEATURE_GRADE_OUTCOMES
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
*/
function evaluation_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* this will create a new instance and return the id number
* of the new instance.
*
* @param object $evaluation the object given by mod_evaluation_mod_form
* @return int
* @global object
*/
function evaluation_add_instance($evaluation) {
global $CFG, $DB;
$evaluation->timemodified = time();
$evaluation->id = '';
if (empty($evaluation->site_after_submit)) {
$evaluation->site_after_submit = '';
}
if (!isset($evaluation->min_results) or empty($evaluation->min_results)) {
$evaluation->min_results = 3;
}
if (!isset($evaluation->privileged_users) or empty($evaluation->privileged_users)) {
$evaluation->privileged_users = '';
}
if (!isset($evaluation->filter_course_of_studies) or empty($evaluation->filter_course_of_studies)) {
$evaluation->filter_course_of_studies = '';
}
if (!isset($evaluation->filter_courses) or empty($evaluation->filter_courses)) {
$evaluation->filter_courses = '';
}
/*if (empty($evaluation->autoreminders)) {
$evaluation->autoreminders = 1;
}*/
if (empty($evaluation->semester)) {
$evaluation->semester = evaluation_get_current_semester();
}
if ($CFG->ash) {
if (empty($evaluation->sort_tag)) {
$evaluation->sort_tag = "ASH";
}
if (empty($evaluation->sendermail)) {
$evaluation->sendermail = "khayat@ash-berlin.eu";
}
if (empty($evaluation->sendername)) {
$evaluation->sendername = "ASH Berlin (Qualitätsmanagement)";
}
if (empty($evaluation->signature)) {
$evaluation->signature = "Berthe Khayat und Harry Bleckert für das Evaluationsteam";
}
}
// Convert participant_roles array to string if it's an array
if (!empty($evaluation->participant_roles) && is_array($evaluation->participant_roles)) {
$evaluation->participant_roles = implode(',', $evaluation->participant_roles);
}
else if (empty($evaluation->participant_roles)) {
$evaluation->participant_roles = '5';
}
//saving the evaluation in db
$evaluationid = $DB->insert_record("evaluation", $evaluation);
$evaluation->id = $evaluationid;
evaluation_set_events($evaluation);
if (!isset($evaluation->coursemodule)) {
$cm = get_coursemodule_from_id('evaluation', $evaluation->id);
$evaluation->coursemodule = $cm->id;
}
$context = context_module::instance($evaluation->coursemodule);
if (!empty($evaluation->completionexpected)) {
\core_completion\api::update_completion_date_event($evaluation->coursemodule, 'evaluation', $evaluation->id,
$evaluation->completionexpected);
}
$editoroptions = evaluation_get_editor_options();
// process the custom wysiwyg editor in page_after_submit
if ($draftitemid = $evaluation->page_after_submit_editor['itemid']) {
$evaluation->page_after_submit = file_save_draft_area_files($draftitemid, $context->id,
'mod_evaluation', 'page_after_submit',
0, $editoroptions,
$evaluation->page_after_submit_editor['text']);
$evaluation->page_after_submitformat = $evaluation->page_after_submit_editor['format'];
}
$DB->update_record('evaluation', $evaluation);
return $evaluationid;
}
/**
* this will update a given instance
*
* @param object $evaluation the object given by mod_evaluation_mod_form
* @return boolean
* @global object
*/
function evaluation_update_instance($evaluation) {
global $CFG,$DB;
$evaluation->timemodified = time();
$evaluation->id = $evaluation->instance;
if (empty($evaluation->site_after_submit)) {
$evaluation->site_after_submit = '';
}
if (!isset($evaluation->filter_course_of_studies) or empty($evaluation->filter_course_of_studies)) {
$evaluation->filter_course_of_studies = '';
}
if (!isset($evaluation->privileged_users) or empty($evaluation->privileged_users)) {
$evaluation->privileged_users = '';
}
/*if (empty($evaluation->autoreminders)) {
$evaluation->autoreminders = 1;
}*/
if (empty($evaluation->semester)) {
$evaluation->semester = evaluation_get_current_semester();
}
if ($CFG->ash) {
if (empty($evaluation->sort_tag)) {
$evaluation->sort_tag = "ASH";
}
if (empty($evaluation->sendermail)) {
$evaluation->sendermail = "khayat@ash-berlin.eu";
}
if (empty($evaluation->sendername)) {
$evaluation->sendername = "ASH Berlin (Qualitätsmanagement)";
}
if (empty($evaluation->signature)) {
$evaluation->signature = "Berthe Khayat und Harry Bleckert für das Evaluationsteam";
}
}
// Convert participant_roles array to string if it's an array
if (!empty($evaluation->participant_roles) && is_array($evaluation->participant_roles)) {
$evaluation->participant_roles = implode(',', $evaluation->participant_roles);
}
else if (empty($evaluation->participant_roles)) {
$evaluation->participant_roles = '5';
}
//save the evaluation into the db
$DB->update_record("evaluation", $evaluation);
//create or update the new events
evaluation_set_events($evaluation);
$completionexpected = (!empty($evaluation->completionexpected)) ? $evaluation->completionexpected : null;
\core_completion\api::update_completion_date_event($evaluation->coursemodule, 'evaluation', $evaluation->id,
$completionexpected);
$context = context_module::instance($evaluation->coursemodule);
$editoroptions = evaluation_get_editor_options();
// process the custom wysiwyg editor in page_after_submit
if ($draftitemid = $evaluation->page_after_submit_editor['itemid']) {
$evaluation->page_after_submit = file_save_draft_area_files($draftitemid, $context->id,
'mod_evaluation', 'page_after_submit',
0, $editoroptions,
$evaluation->page_after_submit_editor['text']);
$evaluation->page_after_submitformat = $evaluation->page_after_submit_editor['format'];
}
$DB->update_record('evaluation', $evaluation);
return true;
}
/**
* Serves the files included in evaluation items like label. Implements needed access control ;-)
*
* There are two situations in general where the files will be sent.
* 1) filearea = item, 2) filearea = template
*
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $filearea file area
* @param array $args extra arguments
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if file not found, does not return if found - justsend the file
* @package mod_evaluation
* @category files
*/
function evaluation_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
global $CFG, $DB;
if ($filearea === 'item' or $filearea === 'template') {
$itemid = (int) array_shift($args);
//get the item what includes the file
if (!$item = $DB->get_record('evaluation_item', array('id' => $itemid))) {
return false;
}
$evaluationid = $item->evaluation;
$templateid = $item->template;
}
if ($filearea === 'page_after_submit' or $filearea === 'item') {
if (!$evaluation = $DB->get_record("evaluation", array("id" => $cm->instance))) {
return false;
}
$evaluationid = $evaluation->id;
//if the filearea is "item" so we check the permissions like view/complete the evaluation
$canload = false;
//first check whether the user has the complete capability
if (has_capability('mod/evaluation:complete', $context)) {
$canload = true;
}
//now we check whether the user has the view capability
if (has_capability('mod/evaluation:view', $context)) {
$canload = true;
}
//if the evaluation is on frontpage and anonymous and the fullanonymous is allowed
//so the file can be loaded too.
if (isset($CFG->evaluation_allowfullanonymous)
and $CFG->evaluation_allowfullanonymous
and $course->id == SITEID
and $evaluation->anonymous == EVALUATION_ANONYMOUS_YES) {
$canload = true;
}
if (!$canload) {
return false;
}
} else if ($filearea === 'template') { //now we check files in templates
if (!$template = $DB->get_record('evaluation_template', array('id' => $templateid))) {
return false;
}
//if the file is not public so the capability edititems has to be there
if (!$template->ispublic) {
if (!has_capability('mod/evaluation:edititems', $context)) {
return false;
}
} else { //on public templates, at least the user has to be logged in
if (!isloggedin()) {
return false;
}
}
} else {
return false;
}
if ($context->contextlevel == CONTEXT_MODULE) {
if ($filearea !== 'item' and $filearea !== 'page_after_submit') {
return false;
}
}
if ($context->contextlevel == CONTEXT_COURSE || $context->contextlevel == CONTEXT_SYSTEM) {
if ($filearea !== 'template') {
return false;
}
}
$relativepath = implode('/', $args);
if ($filearea === 'page_after_submit') {
$fullpath = "/{$context->id}/mod_evaluation/$filearea/$relativepath";
} else {
$fullpath = "/{$context->id}/mod_evaluation/$filearea/{$item->id}/$relativepath";
}
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// finally send the file
send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
return false;
}
/**
* this will delete a given instance.
* all referenced data also will be deleted
*
* @param int $id the instanceid of evaluation
* @return boolean
* @global object
*/
function evaluation_delete_instance($id) {
global $DB;
//get all referenced items
$evaluationitems = $DB->get_records('evaluation_item', array('evaluation' => $id));
//deleting all referenced items and values
if (is_array($evaluationitems)) {
foreach ($evaluationitems as $evaluationitem) {
$DB->delete_records("evaluation_value", array("item" => $evaluationitem->id));
$DB->delete_records("evaluation_valuetmp", array("item" => $evaluationitem->id));
}
if ($delitems = $DB->get_records("evaluation_item", array("evaluation" => $id))) {
foreach ($delitems as $delitem) {
evaluation_delete_item($delitem->id, false);
}
}
}
//deleting the completeds
$DB->delete_records("evaluation_completed", array("evaluation" => $id));
//deleting the unfinished completeds
$DB->delete_records("evaluation_completedtmp", array("evaluation" => $id));
//deleting old events
$DB->delete_records('event', array('modulename' => 'evaluation', 'instance' => $id));
// deleting evaluation_users_la
return $DB->delete_records("evaluation_users_la", array("id" => $id));
// deleting evaluation_enrolment
return $DB->delete_records("evaluation_enrolment", array("id" => $id));
}
/**
* Return a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
* $return->time = the time they did it
* $return->info = a short text description
*
* @param stdClass $course
* @param stdClass $user
* @param cm_info|stdClass $mod
* @param stdClass $evaluation
* @return stdClass
*/
function evaluation_user_outline($course, $user, $mod, $evaluation) {
global $DB;
$outline = (object) ['info' => '', 'time' => 0];
if ($evaluation->anonymous != EVALUATION_ANONYMOUS_NO) {
// Do not disclose any user info if evaluation is anonymous.
return $outline;
}
$params = array('userid' => $user->id, 'evaluation' => $evaluation->id,
'anonymous_response' => EVALUATION_ANONYMOUS_NO);
$status = null;
$context = context_module::instance($mod->id);
if ($completed = $DB->get_record('evaluation_completed', $params)) {
// User has completed evaluation.
$outline->info = get_string('completed', 'evaluation');
$outline->time = $completed->timemodified;
} else if ($completedtmp = $DB->get_record('evaluation_completedtmp', $params)) {
// User has started but not completed evaluation.
$outline->info = get_string('started', 'evaluation');
$outline->time = $completedtmp->timemodified;
} else if (has_capability('mod/evaluation:complete', $context, $user)) {
// User has not started evaluation but has capability to do so.
$outline->info = get_string('not_started', 'evaluation');
}
return $outline;
}
/**
* Returns all users who has completed a specified evaluation since a given time
* many thanks to Manolescu Dorel, who contributed these two functions
*
* @param array $activities Passed by reference
* @param int $index Passed by reference
* @param int $timemodified Timestamp
* @param int $courseid
* @param int $cmid
* @param int $userid
* @param int $groupid
* @return void
* @global object
* @global object
* @global object
* @global object
* @uses CONTEXT_MODULE
*/
function evaluation_get_recent_mod_activity(&$activities, &$index,
$timemodified, $courseid,
$cmid, $userid = "", $groupid = "") {
global $CFG, $COURSE, $USER, $DB;
if ($COURSE->id == $courseid) {
$course = $COURSE;
} else {
$course = $DB->get_record('course', array('id' => $courseid));
}
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->cms[$cmid];
$sqlargs = array();
$userfields = user_picture::fields('u', null, 'useridagain');
$sql = " SELECT fk . * , fc . * , $userfields
FROM {evaluation_completed} fc
JOIN {evaluation} fk ON fk.id = fc.evaluation
JOIN {user} u ON u.id = fc.userid ";
if ($groupid) {
$sql .= " JOIN {groups_members} gm ON gm.userid=u.id ";
}
$sql .= " WHERE fc.timemodified > ?
AND fk.id = ?
AND fc.anonymous_response = ?";
$sqlargs[] = $timemodified;
$sqlargs[] = $cm->instance;
$sqlargs[] = EVALUATION_ANONYMOUS_NO;
if ($userid) {
$sql .= " AND u.id = ? ";
$sqlargs[] = $userid;
}
if ($groupid) {
$sql .= " AND gm.groupid = ? ";
$sqlargs[] = $groupid;
}
if (!$evaluationitems = $DB->get_records_sql($sql, $sqlargs)) {
return;
}
$cm_context = context_module::instance($cm->id);
if (!has_capability('mod/evaluation:view', $cm_context)) {
return;
}
$accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
$viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
$groupmode = groups_get_activity_groupmode($cm, $course);
$aname = format_string($cm->name, true);
foreach ($evaluationitems as $evaluationitem) {
if ($evaluationitem->userid != $USER->id) {
if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
$usersgroups = groups_get_all_groups($course->id,
$evaluationitem->userid,
$cm->groupingid);
if (!is_array($usersgroups)) {
continue;
}
$usersgroups = array_keys($usersgroups);
$intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid));
if (empty($intersect)) {
continue;
}
}
}
$tmpactivity = new stdClass();
$tmpactivity->type = 'evaluation';
$tmpactivity->cmid = $cm->id;
$tmpactivity->name = $aname;
$tmpactivity->sectionnum = $cm->sectionnum;
$tmpactivity->timestamp = $evaluationitem->timemodified;
$tmpactivity->content = new stdClass();
$tmpactivity->content->evaluationid = $evaluationitem->id;
$tmpactivity->content->evaluationuserid = $evaluationitem->userid;
$tmpactivity->user = user_picture::unalias($evaluationitem, null, 'useridagain');
$tmpactivity->user->fullname = fullname($evaluationitem, $viewfullnames);
$activities[$index++] = $tmpactivity;
}
return;
}
/**
* Prints all users who has completed a specified evaluation since a given time
* many thanks to Manolescu Dorel, who contributed these two functions
*
* @param object $activity
* @param int $courseid
* @param string $detail
* @param array $modnames
* @return void Output is echo'd
* @global object
*/
function evaluation_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
global $CFG, $OUTPUT;
echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
echo "<tr><td class=\"userpicture\" valign=\"top\">";
echo $OUTPUT->user_picture($activity->user, array('courseid' => $courseid));
echo "</td><td>";
if ($detail) {
$modname = $modnames[$activity->type];
echo '<div class="title">';
echo $OUTPUT->image_icon('icon', $modname, $activity->type);
echo "<a href=\"$CFG->wwwroot/mod/evaluation/view.php?id={$activity->cmid}\">{$activity->name}</a>";
echo '</div>';
}
echo '<div class="title">';
echo '</div>';
echo '<div class="user">';
echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&course=$courseid\">"
. "{$activity->user->fullname}</a> - " . userdate($activity->timestamp);
echo '</div>';
echo "</td></tr></table>";
return;
}
/**
* Obtains the automatic completion state for this evaluation based on the condition
* in evaluation settings.
*
* @param object $course Course
* @param object $cm Course-module
* @param int $userid User ID
* @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
* @return bool True if completed, false if not, $type if conditions not set.
*/
function evaluation_get_completion_state($course, $cm, $userid, $type) {
global $CFG, $DB;
// Get evaluation details
$evaluation = $DB->get_record('evaluation', array('id' => $cm->instance), '*', MUST_EXIST);
// If completion option is enabled, evaluate it and return true/false
if ($evaluation->completionsubmit) {
$params = array('userid' => $userid, 'evaluation' => $evaluation->id);
return $DB->record_exists('evaluation_completed', $params);
} else {
// Completion option is not enabled so just return $type
return $type;
}
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param stdClass $course
* @param stdClass $user
* @param cm_info|stdClass $mod
* @param stdClass $evaluation
*/
function evaluation_user_complete($course, $user, $mod, $evaluation) {
global $DB;
if ($evaluation->anonymous != EVALUATION_ANONYMOUS_NO) {
// Do not disclose any user info if evaluation is anonymous.
return;
}
$params = array('userid' => $user->id, 'evaluation' => $evaluation->id,
'anonymous_response' => EVALUATION_ANONYMOUS_NO);
$url = $status = null;
$context = context_module::instance($mod->id);
if ($completed = $DB->get_record('evaluation_completed', $params)) {
// User has completed evaluation.
if (has_capability('mod/evaluation:viewreports', $context)) {
$url = new moodle_url('/mod/evaluation/show_entries.php',
['id' => $mod->id, 'userid' => $user->id,
'showcompleted' => $completed->id]);
}
$status = get_string('completedon', 'evaluation', userdate($completed->timemodified));
} else if ($completedtmp = $DB->get_record('evaluation_completedtmp', $params)) {
// User has started but not completed evaluation.
$status = get_string('startedon', 'evaluation', userdate($completedtmp->timemodified));
} else if (has_capability('mod/evaluation:complete', $context, $user)) {
// User has not started evaluation but has capability to do so.
$status = get_string('not_started', 'evaluation');
}
if ($url && $status) {
echo html_writer::link($url, $status);
} else if ($status) {
echo html_writer::div($status);
}
}
/**
* @return bool true
*/
function evaluation_cron() {
return true;
}
/**
* @deprecated since Moodle 3.8
*/
function evaluation_scale_used() {
throw new coding_exception('evaluation_scale_used() can not be used anymore. Plugins can implement ' .
'<modname>_scale_used_anywhere, all implementations of <modname>_scale_used are now ignored');
}
/**
* Checks if scale is being used by any instance of evaluation
*
* This is used to find out if scale used anywhere
*
* @param $scaleid int
* @return boolean True if the scale is used by any assignment
*/
function evaluation_scale_used_anywhere($scaleid) {
return false;
}
/**
* List the actions that correspond to a view of this module.
* This is used by the participation report.
*
* Note: This is not used by new logging system. Event with
* crud = 'r' and edulevel = LEVEL_PARTICIPATING will
* be considered as view action.
*
* @return array
*/
function evaluation_get_view_actions() {
return array('view', 'view all');
}
/**
* List the actions that correspond to a post of this module.
* This is used by the participation report.
*
* Note: This is not used by new logging system. Event with
* crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
* will be considered as post action.
*
* @return array
*/
function evaluation_get_post_actions() {
return array('submit');
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
* This function will remove all responses from the specified evaluation
* and clean up any related data.
*
* @param object $data the data submitted from the reset course.
* @return array status array
* @uses EVALUATION_RESETFORM_RESET
* @uses EVALUATION_RESETFORM_DROP
* @global object
* @global object
*/
function evaluation_reset_userdata($data) {
global $CFG, $DB;
$resetevaluations = array();
$dropevaluations = array();
$status = array();
$componentstr = get_string('modulenameplural', 'evaluation');
//get the relevant entries from $data
foreach ($data as $key => $value) {
switch (true) {
case substr($key, 0, strlen(EVALUATION_RESETFORM_RESET)) == EVALUATION_RESETFORM_RESET:
if ($value == 1) {
$templist = explode('_', $key);
if (isset($templist[3])) {
$resetevaluations[] = intval($templist[3]);
}
}
break;
case substr($key, 0, strlen(EVALUATION_RESETFORM_DROP)) == EVALUATION_RESETFORM_DROP:
if ($value == 1) {
$templist = explode('_', $key);
if (isset($templist[3])) {
$dropevaluations[] = intval($templist[3]);
}
}
break;
}
}
//reset the selected evaluations
foreach ($resetevaluations as $id) {
$evaluation = $DB->get_record('evaluation', array('id' => $id));
evaluation_delete_all_completeds($evaluation);
$status[] = array('component' => $componentstr . ':' . $evaluation->name,
'item' => get_string('resetting_data', 'evaluation'),
'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.
$shifterror = !shift_course_mod_dates('evaluation', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => $shifterror);
}
return $status;
}
/**
* Called by course/reset.php
*
* @param object $mform form passed by reference
* @uses EVALUATION_RESETFORM_RESET
* @global object
*/
function evaluation_reset_course_form_definition(&$mform) {
global $COURSE, $DB;
$mform->addElement('header', 'evaluationheader', get_string('modulenameplural', 'evaluation'));
if (!$evaluations = $DB->get_records('evaluation', array('course' => $COURSE->id), 'name')) {
return;
}
$mform->addElement('static', 'hint', get_string('resetting_data', 'evaluation'));
foreach ($evaluations as $evaluation) {
$mform->addElement('checkbox', EVALUATION_RESETFORM_RESET . $evaluation->id, $evaluation->name);
}
}
/**
* Course reset form defaults.
*
* @param object $course
* @uses EVALUATION_RESETFORM_RESET
* @global object
*/
function evaluation_reset_course_form_defaults($course) {
global $DB;
$return = array();
if (!$evaluations = $DB->get_records('evaluation', array('course' => $course->id), 'name')) {
return;
}
foreach ($evaluations as $evaluation) {
$return[EVALUATION_RESETFORM_RESET . $evaluation->id] = true;
}
return $return;
}
/**
* Called by course/reset.php and shows the formdata by coursereset.
* it prints checkboxes for each evaluation available at the given course
* there are two checkboxes:
* 1) delete userdata and keep the evaluation
* 2) delete userdata and drop the evaluation
*
* @param object $course
* @return void
* @uses EVALUATION_RESETFORM_DROP
* @global object
* @uses EVALUATION_RESETFORM_RESET
*/
function evaluation_reset_course_form($course) {
global $DB, $OUTPUT;
echo get_string('resetting_evaluations', 'evaluation');
echo ':<br />';
if (!$evaluations = $DB->get_records('evaluation', array('course' => $course->id), 'name')) {
return;
}
foreach ($evaluations as $evaluation) {
echo '<p>';
echo get_string('name', 'evaluation') . ': ' . $evaluation->name . '<br />';
echo html_writer::checkbox(EVALUATION_RESETFORM_RESET . $evaluation->id,
1, true,
get_string('resetting_data', 'evaluation'));
echo '<br />';
echo html_writer::checkbox(EVALUATION_RESETFORM_DROP . $evaluation->id,
1, false,
get_string('drop_evaluation', 'evaluation'));
echo '</p>';
}
}
/**
* This gets an array with default options for the editor
*
* @return array the options
*/
function evaluation_get_editor_options() {
return array('maxfiles' => EDITOR_UNLIMITED_FILES,
'trusttext' => true);
}
/**
* this function is called by {@link evaluation_delete_userdata()}
* it drops the evaluation-instance from the course_module table
*
* @param int $id the id from the coursemodule
* @return boolean
* @global object
*/
function evaluation_delete_course_module($id) {
global $DB;
if (!$cm = $DB->get_record('course_modules', array('id' => $id))) {
return true;
}
return $DB->delete_records('course_modules', array('id' => $cm->id));
}
////////////////////////////////////////////////
//functions to handle capabilities
////////////////////////////////////////////////
/**
* @deprecated since 3.1
*/
function evaluation_get_context() {
throw new coding_exception('evaluation_get_context() can not be used anymore.');
}
/**
* returns true if the current role is faked by switching role feature
*
* @return boolean
* @global object
*/
function evaluation_check_is_switchrole() {
global $USER;
if (isset($USER->switchrole) and
is_array($USER->switchrole) and
safeCount($USER->switchrole) > 0) {
return true;
}
return false;
}
/**
* count users which have not completed the evaluation
*
* @param cm_info $cm Course-module object
* @param int $group single groupid
* @param string $sort
* @param int $startpage
* @param int $pagecount
* @param bool $includestatus to return if the user started or not the evaluation among the complete user record
* @return array array of user ids or user objects when $includestatus set to true
* @uses CONTEXT_MODULE
* @global object
*/
function evaluation_get_incomplete_users(cm_info $cm,
$group = false,
$sort = '',
$startpage = false,
$pagecount = false,
$includestatus = false) {
global $DB;
$context = context_module::instance($cm->id);
//first get all user who can complete this evaluation
$cap = 'mod/evaluation:complete';
$allnames = get_all_user_name_fields(true, 'u');
$fields = 'u.id, ' . $allnames . ', u.picture, u.email, u.imagealt';
if (!$allusers = get_users_by_capability($context,
$cap,
$fields,
$sort,
'',
'',
$group,
'',
true)) {
return false;
}
// Filter users that are not in the correct group/grouping.
$info = new \core_availability\info_module($cm);
$allusersrecords = $info->filter_user_list($allusers);
$allusers = array_keys($allusersrecords);
//now get all completeds
$params = array('evaluation' => $cm->instance);
if ($completedusers = $DB->get_records_menu('evaluation_completed', $params, '', 'id, userid')) {
// Now strike all completedusers from allusers.
$allusers = array_diff($allusers, $completedusers);
}
//for paging I use array_slice()
if ($startpage !== false and $pagecount !== false) {
$allusers = array_slice($allusers, $startpage, $pagecount);
}
// Check if we should return the full users objects.
if ($includestatus) {
$userrecords = [];
$startedusers = $DB->get_records_menu('evaluation_completedtmp', ['evaluation' => $cm->instance], '', 'id, userid');
$startedusers = array_flip($startedusers);
foreach ($allusers as $userid) {
$allusersrecords[$userid]->evaluationstarted = isset($startedusers[$userid]);
$userrecords[] = $allusersrecords[$userid];
}
return $userrecords;
} else { // Return just user ids.
return $allusers;
}
}
/**
* count users which have not completed the evaluation