-
Notifications
You must be signed in to change notification settings - Fork 8
/
locallib.php
4063 lines (3605 loc) · 175 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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 mod_checkmark for Moodle - http://moodle.org/
//
// It 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.
//
// It 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/>.
/**
* This file contains checkmark-class with all logic-methods used by checkmark
*
* @package mod_checkmark
* @author Philipp Hager, extended and maintained by Daniel Binder
* @copyright 2019 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_checkmark\submission;
use mod_checkmark\submissionstable;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/mod/checkmark/lib.php');
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/gradelib.php');
require_once($CFG->dirroot . '/mod/checkmark/submission_form.php');
require_once($CFG->dirroot . '/mod/checkmark/grading_form.php');
/**
* This class provides all the basic functionality for a checkmark-module
*
* @package mod_checkmark
* @author Philipp Hager, extended and maintained by Daniel Binder
* @copyright 2019 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class checkmark {
/** FILTER_ALL */
const FILTER_ALL = 1;
/** FILTER_SUBMITTED */
const FILTER_SUBMITTED = 2;
/** FILTER_REQUIRE_GRADING */
const FILTER_REQUIRE_GRADING = 3;
/** FILTER_SELECTED */
const FILTER_SELECTED = 4;
/** FILTER ATTENDANT USERS */
const FILTER_ATTENDANT = 5;
/** FILTER ABSENT USERS */
const FILTER_ABSENT = 6;
/** FILTER UNKNOWN */
const FILTER_UNKNOWN = 7;
/** FILTER EXTENSION */
const FILTER_EXTENSION = 8;
/** FILER NOT SUBMITTED */
const FILTER_NOT_SUBMITTED = 9;
/** FILER PRESENTATIONGRADING */
const FILTER_PRESENTATIONGRADING = 10;
/** FILER NO PRESENTATIONGRADING */
const FILTER_NO_PRESENTATIONGRADING = 11;
/** DELIMITER Used to connect example-names, example-grades, submission-examplenumbers! */
const DELIMITER = ',';
/** EMPTYBOX UTF-8 empty box = ☐ = '☐'! */
const EMPTYBOX = '';
/** CHECKEDBOX UTF-8 box with x-mark = ☒ = '☒'! */
const CHECKEDBOX = 'X';
/** FORCED_EMPTYBOX UTF-8 empty box surrounded by parenthesis = (☐) = '(☐)'! */
const FORCED_EMPTYBOX = '()';
/** FORCED_EMPTYBOX UTF-8 box with x-mark surrounded by parenthesis = (☒) = '(☒)'! */
const FORCED_CHECKEDBOX = '(X)';
/** @var object */
public $cm;
/** @var object */
public $course;
/** @var object */
public $checkmark;
/** @var bool */
public $usehtmleditor;
/** @var int */
public $defaultformat;
/** @var object */
public $context;
/** @var object[] cached examples for this instance */
public $examples;
/** @var object contains overridden dates (for current user only!) */
public $overrides = false;
/** @var checkmark_renderer the custom renderer for this module */
private $output;
/**
* Constructor for the checkmark class
*
* Constructor for the checkmark class.
* If cmid is set create the cm, course, checkmark objects.
* If the checkmark is hidden and the user is not a teacher then
* this prints a page header and notice.
*
* @param string|int $cmid the current course module id - not set for new checkmarks
* @param object $checkmark usually null, but if we have it we pass it to save db access
* @param object $cm usually null, but if we have it we pass it to save db access
* @param object $course usually null, but if we have it we pass it to save db access
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
public function __construct($cmid = 'staticonly', $checkmark = null, $cm = null, $course = null) {
global $COURSE, $DB, $USER;
if ($cmid == 'staticonly') {
// Use static functions only!
return;
}
if ($cm) {
$this->cm = $cm;
} else if (!$this->cm = get_coursemodule_from_id('checkmark', $cmid)) {
print_error('invalidcoursemodule');
}
$this->context = context_module::instance($this->cm->id);
if ($course) {
$this->course = $course;
} else if ($this->cm->course == $COURSE->id) {
$this->course = $COURSE;
} else if (!$this->course = $DB->get_record('course', ['id' => $this->cm->course])) {
print_error('invalidid', 'checkmark');
}
if ($checkmark) {
$this->checkmark = $checkmark;
} else if (!$this->checkmark = $DB->get_record('checkmark', ['id' => $this->cm->instance])) {
print_error('invalidid', 'checkmark');
}
// Check for overridden dates!
if ($overridden = checkmark_get_overridden_dates($this->checkmark->id, $USER->id, $this->checkmark->course)) {
$this->overrides = $overridden;
}
// Ensure compatibility with modedit checkmark obj!
$this->checkmark->cmidnumber = $this->cm->idnumber;
$this->checkmark->course = $this->course->id;
/*
* Visibility handled by require_login() with $cm parameter
* get current group only when really needed!
*/
// Set up things for a HTML editor if it's needed!
$this->defaultformat = editors_get_preferred_format();
// We cache examples now...
$this->get_examples();
}
/**
* Lazy load the page renderer and expose the renderer to plugins.
*
* @return checkmark_renderer
*/
public function get_renderer() {
global $PAGE;
if ($this->output) {
return $this->output;
}
$this->output = $PAGE->get_renderer('mod_checkmark', null, RENDERER_TARGET_GENERAL);
return $this->output;
}
/**
* Standardizes course module, checkmark and course data objects and checks for login state!
*
* @param int $id course module id or 0 (either $id or $c have to be set!)
* @param int $c checkmark instance id or 0 (either $id or $c have to be set!)
* @param moodle_url $url current url of the viewed page
* @return object[] Returns array with coursemodule, checkmark and course objects
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
* @throws require_login_exception
*/
public static function init_checks($id, $c, $url) {
global $PAGE, $DB;
if ($id) {
if (!$cm = get_coursemodule_from_id('checkmark', $id)) {
print_error('invalidcoursemodule');
}
if (!$checkmark = $DB->get_record('checkmark', array('id' => $cm->instance))) {
print_error('invalidid', 'checkmark');
}
if (!$course = $DB->get_record('course', array('id' => $checkmark->course))) {
print_error('coursemisconf', 'checkmark');
}
$url->param('id', $id);
} else {
if (!$checkmark = $DB->get_record('checkmark', array('id' => $c))) {
print_error('invalidcoursemodule');
}
if (!$course = $DB->get_record('course', array('id' => $checkmark->course))) {
print_error('coursemisconf', 'checkmark');
}
if (!$cm = get_coursemodule_from_instance('checkmark', $checkmark->id, $course->id)) {
print_error('invalidcoursemodule');
}
$url->param('id', $cm->id);
}
$PAGE->set_url($url);
require_login($course->id, false, $cm);
return array($cm, $checkmark, $course);
}
/**
* Get the examples for this checkmark from the DB
*
* Adds the prefix if set and flexible naming is used
*
* @return \mod_checkmark\example[] checkmark's examples from the DB (raw records)
* @throws dml_exception
*/
public function get_examples() {
if (!isset($this->checkmark->examples)) {
$exampleprefix = $this->checkmark->exampleprefix;
$this->checkmark->examples = self::get_examples_static($this->checkmark->id, $exampleprefix);
}
return $this->checkmark->examples;
}
/**
* Get the examples for this checkmark from the DB
*
* Adds the prefix if set and flexible naming is used
*
* @param object|int $checkmarkid the checkmark object containing ID or the ID itself
* @param string|false $exampleprefix If you already have it, you can save 1 query by setting it!
* @return \mod_checkmark\example[] checkmark's examples from the DB (raw records)
* @throws dml_exception
*/
public static function get_examples_static($checkmarkid, $exampleprefix = false) {
global $DB;
$records = $DB->get_records('checkmark_examples', array('checkmarkid' => $checkmarkid), 'id ASC');
if ($exampleprefix === false) {
$exampleprefix = $DB->get_field('checkmark', 'exampleprefix', ['id' => $checkmarkid]);
}
$examples = [];
foreach ($records as $key => $cur) {
$examples[$key] = new \mod_checkmark\example($key, $cur->name, $cur->grade, $exampleprefix);
}
return $examples;
}
/**
* print_summary() returns a short statistic over the actual checked examples in this checkmark
* You've checked out X from a maximum of Y examples. (A out of B points)
*
* @return string short summary
* @throws coding_exception
* @throws dml_exception
*/
public function print_summary() {
global $USER;
$submission = $this->get_submission($USER->id, false); // Get the submission!
$a = checkmark_getsubmissionstats($submission, $this->checkmark);
$output =
html_writer::tag('div', get_string('checkmark_summary', 'checkmark', $a), ['class' => 'chkmrksubmissionsummary']) .
html_writer::empty_tag('br');
return $output;
}
/**
* print_student_answer($userid) returns a short HTML-coded string
* with the checked examples in black an unchecked ones lined through and in a light grey.
*
* @param int $userid The user-ID to print the student anwer for.
* @return string checked examples
* @throws coding_exception
* @throws dml_exception
*/
public function print_student_answer($userid) {
$output = '';
if (!$submission = $this->get_submission($userid)) {
return get_string('nosubmission', 'checkmark');
}
foreach ($submission->get_examples() as $example) {
if ($output != '') {
$output .= ', ';
} else {
$output .= get_string('strexamples', 'checkmark') . ': ';
}
if ($example->is_checked()) { // Is it checked?
$class = 'checked';
} else {
$class = 'unchecked';
}
$output .= html_writer::tag('span', $example->shortname, array('class' => $class));
}
// Wrapper!
return html_writer::tag('div', $output, array('class' => 'examplelist'));
}
/**
* Every view for checkmark (teacher/student/etc.)
*
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
* @throws required_capability_exception
*/
public function view() {
global $OUTPUT, $USER, $PAGE;
$edit = optional_param('edit', 0, PARAM_BOOL);
$saved = optional_param('saved', 0, PARAM_BOOL);
$late = optional_param('late', 0, PARAM_BOOL);
$context = context_module::instance($this->cm->id);
require_capability('mod/checkmark:view', $context);
/* TRIGGER THE VIEW EVENT */
$event = \mod_checkmark\event\course_module_viewed::create(array(
'objectid' => $this->cm->instance,
'context' => context_module::instance($this->cm->id),
'other' => array(
'name' => $this->checkmark->name,
),
));
$event->add_record_snapshot('course', $this->course);
// In the next line you can use $PAGE->activityrecord if you have set it, or skip this line if you don't have a record.
$event->add_record_snapshot($PAGE->cm->modname, $this->checkmark);
$event->trigger();
/* END OF VIEW EVENT */
$submission = $this->get_submission($USER->id, false);
$feedback = $this->get_feedback($USER->id);
// Guest can not submit nor edit an checkmark (bug: 4604)!
if (!is_enrolled($this->context, $USER, 'mod/checkmark:submit')) {
$editable = false;
} else {
$editable = $this->isopen() && (!$submission || $this->checkmark->resubmit || ($feedback === false));
}
$editmode = ($editable and $edit);
$data = new stdClass();
$data->id = $this->cm->id;
$data->checkmarkid = $this->checkmark->id;
$data->edit = $editmode;
$data->examples = $this->get_examples();
$data->editable = $editable;
if ($submission) {
$data->sid = $submission->get_id();
if (!empty($submission->get_examples())) {
foreach ($submission->get_examples() as $key => $example) {
$name = 'example' . $key;
$data->$name = empty($example->is_checked()) ? 0 : 1;
}
}
} else {
$data->sid = null;
}
$mform = new checkmark_submission_form(null, $data);
// Prepare form and process submitted data!
if ($mform->is_cancelled()) {
$url = new moodle_url('/course/view.php', array('id' => $PAGE->course->id), "section-" . $PAGE->cm->sectionnum);
redirect($url);
}
if (($formdata = $mform->get_data()) && $editable) {
// Create the submission if needed & return its id!
$submission = $this->get_submission($USER->id, true);
$formarray = json_decode(json_encode($formdata), true);
foreach ($submission->get_examples() as $key => $example) {
$name = $key;
if (isset($formarray[$name]) && ($formarray[$name] != 0)) {
$submission->get_example($key)->set_state(\mod_checkmark\example::CHECKED);
} else {
$submission->get_example($key)->set_state(\mod_checkmark\example::UNCHECKED);
}
}
$this->update_submission($submission);
$this->email_teachers($submission);
// Redirect to get updated submission date!
redirect(new moodle_url($PAGE->url, array('id' => $this->cm->id, 'saved' => 1)));
} else if ($formdata && !$editable) {
// Redirect to get error message!
redirect(new moodle_url($PAGE->url, array('id' => $this->cm->id, 'late' => 1)));
}
$this->view_header();
if ($saved) {
\core\notification::success(get_string('submissionsaved', 'checkmark'));
}
if ($late) {
\core\notification::error(get_string('latesubmissionwarning', 'checkmark'));
}
echo $this->get_intro();
echo "\n";
// Print grading summary only when user has mod/checkmark:grade capability.
if (has_capability('mod/checkmark:grade', $this->context)) {
echo html_writer::div($this->get_renderer()->render_checkmark_grading_summary($this->create_grading_summary()));
}
echo html_writer::tag('div', $this->submittedlink(), array('class' => 'text-info text-center'));
echo $OUTPUT->container_start('studentview');
$previewform = new MoodleQuickForm('optionspref', 'post', '#', '');
$content = '';
$content .= $this->get_dates();
$content .= "\n";
$content .= $this->get_attendancehint();
$content .= "\n";
if ($editable && has_capability('mod/checkmark:submit', $context, $USER, false) && !empty($mform)) {
$content .= $OUTPUT->box_start('generalbox boxaligncenter', 'checkmarkform');
$content .= $this->print_summary();
$content .= $mform->render();
$content .= $OUTPUT->box_end();
$content .= "\n";
} else {
$content .= $OUTPUT->box_start('generalbox boxaligncenter', 'checkmark');
// Display overview!
if (has_capability('mod/checkmark:view_preview', $context) ||
has_capability('mod/checkmark:submit', $context, $USER, false)) {
$content .= $this->print_summary();
$content .= html_writer::start_tag('div', array('class' => 'mform'));
$content .= html_writer::start_tag('div', array('class' => 'clearfix'));
$content .= $this->print_user_submission($USER->id, true);
$content .= html_writer::end_tag('div');
$content .= html_writer::end_tag('div');
}
$content .= $OUTPUT->box_end();
$content .= "\n";
}
if (has_capability('mod/checkmark:grade', $this->context)) {
$previewform->addElement('header', 'studentpreview',
get_string('studentpreview', 'checkmark'));
$previewform->addElement('html', $content);
$previewform->display();
} else {
echo $content;
}
echo $OUTPUT->container_end();
$this->view_feedback();
echo "\n";
$this->view_footer();
echo "\n";
}
/**
* Utility function to add a row of data to a table with 2 columns where the first column is the table's header.
* Modified the table param and does not return a value.
*
* @param html_table $table The table to append the row of data to
* @param string $first The first column text
* @param string $second The second column text
* @param array $firstattributes The first column attributes (optional)
* @param array $secondattributes The second column attributes (optional)
* @return void
*/
private function add_table_row_tuple(html_table $table, $first, $second, $firstattributes = [],
$secondattributes = []) {
$row = new html_table_row();
$cell1 = new html_table_cell($first);
$cell1->header = true;
if (!empty($firstattributes)) {
$cell1->attributes = $firstattributes;
}
$cell2 = new html_table_cell($second);
if (!empty($secondattributes)) {
$cell2->attributes = $secondattributes;
}
$row->cells = array($cell1, $cell2);
$table->data[] = $row;
}
/**
* Display the header and top of a page
*
* This is used by the view() method to print the header of view.php but
* it can be used on other pages in which case the string to denote the
* page in the navigation trail should be passed as an argument
*
* @param string $subpage Description of subpage to be used in navigation trail
* @throws coding_exception
*/
public function view_header($subpage = '') {
global $CFG, $PAGE, $OUTPUT;
if ($subpage) {
$PAGE->navbar->add($subpage);
}
$pagetitle = strip_tags($this->course->shortname . ': ' . get_string('modulename', 'checkmark') .
': ' . format_string($this->checkmark->name, true));
$PAGE->set_heading($this->course->fullname);
echo $OUTPUT->header();
groups_print_activity_menu($this->cm,
$CFG->wwwroot . '/mod/checkmark/view.php?id=' . $this->cm->id);
}
/**
* Creates a gradingsummary object for use in the gradingsummary table
*
* @return \mod_checkmark\gradingsummary
* @throws coding_exception
*/
public function create_grading_summary() {
$participantcount = submissionstable::count_userids($this->context, $this->checkmark->id,
null, self::FILTER_ALL);
$submittedcount = submissionstable::count_userids($this->context, $this->checkmark->id,
null, self::FILTER_SUBMITTED);
$needsgrading = submissionstable::count_userids($this->context, $this->checkmark->id,
null, self::FILTER_REQUIRE_GRADING);
$cangrade = has_capability('mod/checkmark:grade', $this->context);
$attendantcount = -1;
$absencecount = -1;
$needattendanceentrycount = -1;
$presentationgradingcount = -1;
if ($this->checkmark->trackattendance) {
$attendantcount = submissionstable::count_userids($this->context, $this->checkmark->id,
null, self::FILTER_ATTENDANT);
$absencecount = submissionstable::count_userids($this->context, $this->checkmark->id,
null, self::FILTER_ABSENT);
$needattendanceentrycount = submissionstable::count_userids($this->context, $this->checkmark->id,
null, self::FILTER_UNKNOWN);
}
if ($this->checkmark->presentationgrading) {
$presentationgradingcount = submissionstable::count_userids($this->context, $this->checkmark->id,
null, self::FILTER_PRESENTATIONGRADING);
}
$summary = new \mod_checkmark\gradingsummary($participantcount, $this->checkmark->timeavailable, $submittedcount,
$needsgrading, $this->checkmark->timedue, $this->checkmark->cutoffdate, $this->cm->id,
$this->course->startdate, $cangrade, $this->cm->visible, $attendantcount,
$absencecount, $needattendanceentrycount, $presentationgradingcount);
return $summary;
}
/**
* Display the checkmark intro
*
* The default implementation prints the checkmark description in a box
*/
public function get_intro() {
global $OUTPUT;
$content = '';
$content .= $OUTPUT->container_start('description');
$content .= $OUTPUT->heading($this->checkmark->name, 3);
$notoverridden = (!$this->overrides || $this->overrides->timeavailable === null);
$cmptime = $notoverridden ? $this->checkmark->timeavailable : $this->overrides->timeavailable;
if ($this->checkmark->alwaysshowdescription || (time() > $cmptime)) {
$introattachments = $this->get_introattachments();
if (!empty($this->checkmark->intro || !empty($introattachments))) {
$content .= $OUTPUT->box_start('generalbox boxaligncenter', 'intro');
$content .= format_module_intro('checkmark', $this->checkmark, $this->cm->id);
$content .= $introattachments;
$content .= $OUTPUT->box_end();
}
}
$content .= $OUTPUT->container_end();
return $content;
}
/**
* Print intro attachment files if there are any
*/
public function get_introattachments() {
if ($files = $this->get_renderer()->checkmark_files($this->context, 0,
CHECKMARK_INTROATTACHMENT_FILEAREA, 'mod_checkmark')) {
return $files;
}
return '';
}
/**
* Display the checkmark dates
*
* Prints the checkmark start and end dates in a box.
*
* @throws coding_exception
*/
public function get_dates() {
global $OUTPUT;
$content = '';
if (!$this->checkmark->timeavailable && !$this->checkmark->timedue && (!$this->overrides ||
($this->overrides && !$this->overrides->timeavailable && !$this->overrides->timedue))) {
return;
}
$content .= $OUTPUT->box_start('generalbox boxaligncenter', 'dates');
$table = new html_table();
$table->attributes['class'] = 'table-condensed';
$rows = [];
if (($this->checkmark->timeavailable || ($this->overrides && $this->overrides->timeavailable &&
($this->overrides->timeavailable !== $this->checkmark->timeavailable))) &&
(!$this->overrides || $this->overrides->timeavailable !== 0)) {
$row = [new html_table_cell(get_string('availabledate', 'checkmark') . ':')];
$row[0]->attributes['class'] = 'title';
if ($this->checkmark->timeavailable) {
$timeavailable = userdate($this->checkmark->timeavailable);
if ($this->overrides && $this->overrides->timeavailable) {
$timeavailable = userdate($this->overrides->timeavailable);
}
$row[1] = new html_table_cell($timeavailable);
} else {
$row[1] = new html_table_cell(userdate($this->overrides->timeavailable));
}
$rows[] = new html_table_row($row);
}
if (($this->checkmark->timedue || ($this->overrides && $this->overrides->timedue &&
($this->overrides->timedue !== $this->checkmark->timedue))) &&
(!$this->overrides || $this->overrides->timedue !== 0)) {
$row = [new html_table_cell(get_string('duedate', 'checkmark') . ':')];
$row[0]->attributes['class'] = 'title';
if ($this->checkmark->timedue) {
$due = userdate($this->checkmark->timedue);
if ($this->overrides && $this->overrides->timedue) {
$due = userdate($this->overrides->timedue);
}
$row[1] = new html_table_cell($due);
} else {
$row[1] = new html_table_cell(userdate($this->overrides->timedue));
}
$rows[] = new html_table_row($row);
}
$table->data = $rows;
$content .= html_writer::table($table);
$content .= $OUTPUT->box_end();
return $content;
}
/**
* Display the hint if attendance is tracked and linked to grades
*
* @throws coding_exception
*/
public function get_attendancehint() {
global $OUTPUT;
if (!$this->checkmark->trackattendance || !$this->checkmark->attendancegradelink) {
return;
}
return $OUTPUT->box(get_string('attendancegradelink_hint', 'checkmark'), 'generalbox', 'attendancehint');
}
/**
* Display the bottom and footer of a page
*
* This default method just prints the footer.
*/
public function view_footer() {
global $OUTPUT;
echo $OUTPUT->footer();
}
/**
* Display the feedback to the student
*
* This default method prints the teacher picture and name, date when marked,
* grade and teacher submissioncomment.
*
* @param object $feedback The feedback object or null in which case it will be loaded
* @throws coding_exception
* @throws dml_exception
* @throws moodle_exception
*/
public function view_feedback($feedback = null) {
global $USER, $CFG, $DB, $OUTPUT;
require_once($CFG->libdir . '/gradelib.php');
if (!is_enrolled($this->context, $USER, 'mod/checkmark:view')) {
// Can not submit checkmarks -> no feedback!
return;
}
if (!$feedback) { // Get feedback for this checkmark!
$userid = $USER->id;
$feedback = $this->get_feedback($USER->id);
} else {
$userid = $feedback->userid;
}
$gradinginfo = grade_get_grades($this->course->id, 'mod', 'checkmark',
$this->checkmark->id, $userid);
$item = $gradinginfo->items[CHECKMARK_GRADE_ITEM];
$grade = $item->grades[$userid];
$attendanceitem = false;
$attendancegrade = false;
if ($this->checkmark->trackattendance && $this->checkmark->attendancegradebook) {
$attendanceitem = $gradinginfo->items[CHECKMARK_ATTENDANCE_ITEM];
$attendancegrade = $attendanceitem->grades[$userid];
}
$presentationitem = false;
$presentationgrade = false;
if ($this->checkmark->presentationgrading && $this->checkmark->presentationgradebook) {
$presentationitem = $gradinginfo->items[CHECKMARK_PRESENTATION_ITEM];
$presentationgrade = $presentationitem->grades[$userid];
}
if (($feedback == false)
&& (!$item || !$grade || (($grade->grade == null) && ($grade->feedback == null)))
&& (!$attendanceitem || !$attendancegrade || ($attendancegrade->grade == null))
&& (empty($presentationitem) || empty($presentationgrade)
|| (($presentationgrade->grade == null) && ($presentationgrade->feedback == null)))) {
return;
} else if ($feedback == false) {
$feedback = new stdClass();
$feedback->timemodified = 0;
if ($item && $grade) {
$feedback->graderid = $grade->usermodified;
$feedback->timemodified = $grade->dategraded;
$feedback->grade = $grade->grade;
$feedback->feedback = $grade->feedback;
$feedback->format = $grade->feedbackformat;
} else {
$feedback->grade = null;
$feedback->format = 1;
}
if (!empty($attendanceitem) && $attendancegrade) {
if ($attendancegrade->dategraded > $feedback->timemodified) {
$feedback->graderid = $attendancegrade->usermodified;
$feedback->timemodified = $attendancegrade->dategraded;
}
$feedback->attendance = $attendancegrade->grade;
} else {
$feedback->attendance = null;
}
if (!empty($presentationitem) && $presentationgrade) {
if ($presentationgrade->dategraded > $feedback->timemodified) {
$feedback->graderid = $presentationgrade->usermodified;
$feedback->timemodified = $presentationgrade->dategraded;
}
$feedback->presentationgrade = $presentationgrade->grade;
$feedback->presentationfeedback = $presentationgrade->feedback;
$feedback->presentationformat = $presentationgrade->feedbackformat;
} else {
$feedback->presentationgrade = null;
$feedback->presentationfeedback = null;
$feedback->presentationformat = 1;
}
}
// Check if the user can submit?
$cansubmit = has_capability('mod/checkmark:submit', $this->context, $userid, false);
// If not then check if the user still has the view cap and has a previous submission?
$cansubmit = $cansubmit || (($feedback !== false) && has_capability('mod/checkmark:view', $this->context, $userid, false));
if (!$cansubmit) {
// Can not submit checkmarks -> no feedback!
return;
}
if (($grade->hidden || $grade->grade === false)
&& (!$this->checkmark->trackattendance || ($this->checkmark->attendancegradebook && $attendanceitem->hidden))
&& (!$this->checkmark->presentationgrading || ($this->checkmark->presentationgradebook
&& $presentationitem->hidden))) { // Hidden or error!
return;
}
if ($grade->grade === null && empty($grade->str_feedback)
&& (!$this->checkmark->trackattendance || $feedback->attendance === null)
&& (!$this->checkmark->presentationgrading
|| (!$this->checkmark->presentationgrade && $feedback->presentationfeedback === null)
|| ($this->checkmark->presentationgrade && $feedback->presentationgrade === null
&& $feedback->presentationfeedback === null))) { // Nothing to show yet!
return;
}
$dategraded = $grade->dategraded;
$gradedby = $grade->usermodified;
$showfeedback = false;
if (empty($gradedby)) {
// Only show attendance or presentationgrade!
$gradedby = $feedback->graderid;
$dategraded = $feedback->timemodified;
if (!$grader = $DB->get_record('user', array('id' => $gradedby))) {
print_error('cannotfindteacher');
}
} else {
// We need the teacher info!
if (!$grader = $DB->get_record('user', array('id' => $gradedby))) {
print_error('cannotfindteacher');
}
$showfeedback = true;
}
// Print the feedback!
echo $OUTPUT->heading(get_string('feedbackfromteacher', 'checkmark', fullname($grader)));
if ($grader) {
$userpicture = $OUTPUT->user_picture($grader);
$from = html_writer::tag('div', html_writer::tag('strong', fullname($grader)), array('class' => 'fullname'));
} else {
$userpicture = '';
$from = '';
}
$from .= html_writer::tag('div', html_writer::tag('strong', userdate($dategraded)), array('class' => 'time'));
$topic = html_writer::tag('div', $from, array('class' => 'from'));
$row = html_writer::tag('td', $userpicture, array('class' => 'left picture'));
$row .= html_writer::tag('td', $topic, array('class' => 'topic'));
$tablecontent = html_writer::tag('tr', $row);
// Second row!
if ($showfeedback) {
if ($this->checkmark->grade) {
$content = html_writer::tag('div', html_writer::tag('strong', get_string('grade') . ': ') . $grade->str_long_grade,
array('class' => 'grade'));
} else {
$content = '';
}
$content .= html_writer::tag('div', '', array('class' => 'clearer')) .
html_writer::tag('div', $grade->str_feedback, array('class' => 'comment'));
$row = html_writer::tag('td', $content, array('class' => 'content', 'colspan' => 2));
$tablecontent .= html_writer::tag('tr', $row);
}
if ($this->checkmark->trackattendance) {
if ($attendanceitem && ($attendancegrade->locked || $attendancegrade->overridden)) {
$feedback->attendance = $attendancegrade->grade;
}
if ($feedback->attendance == 1) {
$attendancestr = strtolower(get_string('attendant', 'checkmark'));
} else if ($feedback->attendance == 0 && $feedback->attendance !== null) {
$attendancestr = strtolower(get_string('absent', 'checkmark'));
} else {
$attendancestr = strtolower(get_string('unknown', 'checkmark'));
}
$attendance = checkmark_get_attendance_symbol($feedback->attendance) . $attendancestr;
// Third row --> attendance info!
$row = html_writer::tag('td', html_writer::tag('strong', get_string('attendance', 'checkmark') . ': ') . $attendance,
array('class' => 'content', 'colspan' => 2));
$tablecontent .= html_writer::tag('tr', $row);
}
if ($this->checkmark->presentationgrading) {
if ($this->checkmark->presentationgrade && $this->checkmark->presentationgradebook) {
$presgrade = $presentationgrade->str_long_grade;
} else if (!empty($this->checkmark->presentationgrade)) {
$presgrade = $this->display_grade($feedback->presentationgrade, CHECKMARK_PRESENTATION_ITEM);
} else {
$presgrade = "";
}
if ($this->checkmark->presentationgradebook) {
$presfeedback = $presentationgrade->str_feedback;
} else {
$presfeedback = $feedback->presentationfeedback;
}
if ($presgrade != "" || $presfeedback != "") {
$content = html_writer::tag('div', html_writer::tag('strong', get_string('presentationgrade', 'checkmark') . ': ') .
$presgrade, array('class' => 'grade')) .
html_writer::tag('div', '', array('class' => 'clearer')) .
html_writer::tag('div', $presfeedback, array('class' => 'comment'));
$row = html_writer::tag('td', $content, array('class' => 'content', 'colspan' => 2));
$tablecontent .= html_writer::tag('tr', $row);
}
}
echo html_writer::tag('table', $tablecontent, array('cellspacing' => 0, 'class' => 'feedback'));
}
/**
* Returns a link with info about the state of the checkmark submissions
*
* This is used by view_header to put this link at the top right of the page.
* For teachers it gives the number of submitted checkmarks with a link
* For students it gives the time of their submission.
*
* @param bool $allgroups print all groups info if user can access all groups, suitable for index.php
* @return string
* @throws coding_exception
* @throws dml_exception
*/
public function submittedlink($allgroups = false) {
global $USER, $CFG;
$submitted = '';
$urlbase = $CFG->wwwroot . '/mod/checkmark/';
$context = context_module::instance($this->cm->id);
if (has_capability('mod/checkmark:grade', $context)) {
$submitted = html_writer::tag('a', get_string('viewsubmissions', 'checkmark'), [
'class' => 'btn btn-secondary',
'href' => $urlbase . 'submissions.php?id=' . $this->cm->id,
'id' => 'submissions'
]);
} else {
if (isloggedin()) {
if ($submission = $this->get_submission($USER->id)) {
if ($submission->get_timemodified()) {
$date = userdate($submission->get_timemodified());
if ($this->overrides && $this->overrides->timedue !== null) {
$timedue = $this->overrides->timedue;
} else {
$timedue = $this->checkmark->timedue;
}
if ($submission->get_timemodified() <= $timedue || empty($timedue)) {
$submitted = html_writer::tag('span', $date, ['class' => 'text-success']);
} else {
$submitted = html_writer::tag('span', $date, ['class' => 'text-error']);
}
}
}
}
}
return $submitted;
}
/**
* Override available from date, due date or cut off date for certain users or groups!
*
* @param int[] $entities Array of userids or groupids to override dates for
* @param int $timeavailable
* @param int $timedue
* @param int $cutoffdate
* @param string $mode \overrideform::USER for using userids or \overrideform::GROUP for using group ids
* @throws dml_exception
*/
public function override_dates(array $entities, int $timeavailable, int $timedue, int $cutoffdate,
string $mode = \mod_checkmark\overrideform::USER) {
global $DB, $USER;
require_capability('mod/checkmark:manageoverrides', $this->context);
$cmgroupmode = groups_get_activity_groupmode($this->cm);
// Checks if current user is allowed to access all groups of the course.
$accessallgroups = ($cmgroupmode == NOGROUPS) ||
has_capability('moodle/site:accessallgroups', $this->context);
// Groups the current user is part of for checking valid requests if !$accessallgroups.
$usergroups = groups_get_all_groups($this->cm->course, $USER->id);
if (empty($entities) || !is_array($entities)) {
return;
}
$entities = array_unique($entities);
$record = new stdClass();
if ($timeavailable == $this->checkmark->timeavailable) {
$record->timeavailable = null;
} else if (empty($timeavailable)) {
$record->timeavailable = 0;
} else {
$record->timeavailable = $timeavailable;
}
if ($timedue == $this->checkmark->timedue) {
$record->timedue = null;
} else if (empty($timedue)) {
$record->timedue = 0;
} else {
$record->timedue = $timedue;
}
if ($cutoffdate == $this->checkmark->cutoffdate) {
$record->cutoffdate = null;
} else if (empty($cutoffdate)) {
$record->cutoffdate = 0;
} else {
$record->cutoffdate = $cutoffdate;
}
$record->modifierid = $USER->id;
$record->checkmarkid = $this->cm->instance;