-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlib.php
More file actions
1382 lines (1244 loc) · 59 KB
/
lib.php
File metadata and controls
1382 lines (1244 loc) · 59 KB
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/>.
/**
* Grading method controller for the multigraders plugin
*
* @package gradingform_multigraders
* @copyright 2018 Lucian Pricop <contact@lucianpricop.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_external\external_multiple_structure;
use core_external\external_value;
use core_external\external_single_structure;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/grade/grading/form/lib.php');
require_once $CFG->libdir.'/mathslib.php';
require_once($CFG->libdir . '/messagelib.php');
require_once $CFG->dirroot.'/grade/lib.php';
/**
* This controller encapsulates the multi grading logic
*
* @package gradingform_multigraders
* @copyright 2018 Lucian Pricop <contact@lucianpricop.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradingform_multigraders_controller extends gradingform_controller {
// Modes of displaying the form (used in gradingform_multigraders_renderer).
/** form display mode: For editing (moderator or teacher creates a form) */
const DISPLAY_EDIT_FULL = 1;
/** form display mode: Preview the form design with hidden fields */
const DISPLAY_EDIT_FROZEN = 2;
/** form display mode: Preview the form design (for person with manage permission) */
const DISPLAY_PREVIEW = 3;
/** form display mode: Preview the form (for people being graded) */
const DISPLAY_PREVIEW_GRADED = 8;
/** form display mode: For evaluation, enabled (teacher grades a student) */
const DISPLAY_EVAL = 4;
/** form display mode: For evaluation, enabled and allow to change the final (admin grades a student) */
const DISPLAY_EVAL_FULL = 9;
/** form display mode: For evaluation, with hidden fields */
const DISPLAY_EVAL_FROZEN = 5;
/** form display mode: Teacher reviews filled form */
const DISPLAY_REVIEW = 6;
/** form display mode: Display filled form (i.e. students see their grades) */
const DISPLAY_VIEW = 7;
/** @var stdClass|false the definition structure */
protected $moduleinstance = false;
/**
* Extends the module settings navigation with the multigraders settings
*
* This function is called when the context for the page is an activity module with the
* FEATURE_ADVANCED_GRADING, the user has the permission moodle/grade:managegradingforms
* and there is an area with the active grading method set to 'multigraders'.
*
* @param settings_navigation $settingsnav {@link settings_navigation}
* @param navigation_node $node {@link navigation_node}
*/
public function extend_settings_navigation(settings_navigation $settingsnav, navigation_node $node=null) {
$node->add(get_string('editdefinition', 'gradingform_multigraders'),
$this->get_editor_url(), settings_navigation::TYPE_CUSTOM,
null, null, new pix_icon('icon', '', 'gradingform_multigraders'));
}
/**
* Extends the module navigation
*
* This function is called when the context for the page is an activity module with the
* FEATURE_ADVANCED_GRADING and there is an area with the active grading method set to the given plugin.
*
* @param global_navigation $navigation {@link global_navigation}
* @param navigation_node $node {@link navigation_node}
* @return void
*/
public function extend_navigation(global_navigation $navigation, navigation_node $node=null) {
// no need to extra details in menu
}
/**
* Saves the definition into the database
*
* @see parent::update_definition()
* @param stdClass $newdefinition definition data as coming from gradingform_multigraders_controller::get_data()
* @param int $usermodified optional userid of the author of the definition, defaults to the current user
*/
public function update_definition(stdClass $newdefinition, $usermodified = null) {
$newdefinition->status = gradingform_controller::DEFINITION_STATUS_READY;
$changes = $this->update_or_check_definition($newdefinition, $usermodified, true);
if ($changes == 5) {
$this->mark_for_regrade();
}
}
/**
* Either saves the definition into the database or check if it has been changed.
*
* Returns the level of changes:
* 0 - no changes
* 1 - changes made
* 5 - major changes made - all students require manual re-grading
*
* @param stdClass $newdefinition definition data as coming from gradingform_multigraders_controller::get_data()
* @param int|null $usermodified optional userid of the author of the definition, defaults to the current user
* @param bool $doupdate if true actually updates DB, otherwise performs a check
* @return int
*/
public function update_or_check_definition(stdClass $newdefinition, $usermodified = null, $doupdate = false) {
global $DB;
// Firstly update the common definition data in the {grading_definition} table.
if ($this->definition === false) {
if (!$doupdate) {
// If we create the new definition there is no such thing as re-grading anyway.
return 5;
}
// If definition does not exist yet, create a blank one
// (we need id to save files embedded in description).
parent::update_definition(new stdClass(), $usermodified);
parent::load_definition();
}
// Reload the definition from the database.
$this->get_definition(true);
$haschanges = Array();
$newdefinition->blind_marking = isset($newdefinition->blind_marking) ? $newdefinition->blind_marking : 0;
$newdefinition->show_intermediary_to_students = isset($newdefinition->show_intermediary_to_students) ? $newdefinition->show_intermediary_to_students : 0;
$newdefinition->show_notify_student_box = isset($newdefinition->show_notify_student_box) ? $newdefinition->show_notify_student_box : 0;
if(isset($newdefinition->secondary_graders_id_list)){
$set= $newdefinition->secondary_graders_id_list;
$setd = implode(',', $set);
}else{
$definitionExtras = $DB->get_record('gradingform_multigraders_def', array('id' => $newdefinition->copiedfromid), '*');
$setd = $definitionExtras->secondary_graders_id_list;
$newdefinition->auto_calculate_final_method = $definitionExtras->auto_calculate_final_method;
$newdefinition->criteria['text'] = $definitionExtras->criteria;
}
// implode ids with comma
$newdefinition->secondary_graders_id_list = $setd;// stored in database table.
foreach (array('status', 'name', 'description', 'secondary_graders_id_list', 'criteria', 'blind_marking', 'show_intermediary_to_students', 'auto_calculate_final_method','show_notify_student_box') as $key) {
if (isset($newdefinition->$key) && isset($this->definition->$key) && $newdefinition->$key != $this->definition->$key) {
$haschanges[1] = true;
}
}
/*if (isset($newdefinition->no_of_graders) && $newdefinition->no_of_graders != $this->definition->no_of_graders) {
$haschanges[5] = true;
}*/
if ($usermodified && $usermodified != $this->definition->usermodified) {
$haschanges[1] = true;
}
if (!count($haschanges)) {
return 0;
}
if ($doupdate) {
parent::update_definition($newdefinition, $usermodified);
// add/update attributes in custom table
$data = new stdClass();
$data->id = $this->definition->id;
if (!isset($newdefinition->blind_marking)) {
$data->blind_marking = 0;
$data->show_intermediary_to_students = 1;
$data ->show_notify_student_box = 0;
$data->auto_calculate_final_method = 0;
$data->secondary_graders_id_list = Array();
$data->criteria = '';
} else {
$data->blind_marking = $newdefinition->blind_marking;
$data->show_intermediary_to_students = $newdefinition->show_intermediary_to_students;
$data->show_notify_student_box = $newdefinition->show_notify_student_box;
$data->auto_calculate_final_method = $newdefinition->auto_calculate_final_method;
$data->secondary_graders_id_list = $newdefinition->secondary_graders_id_list;
$data->criteria = $newdefinition->criteria['text'];
}
if (isset($this->definition->empty)) {
$DB->insert_record_raw('gradingform_multigraders_def', $data, false, false, true);
} else{
$DB->update_record('gradingform_multigraders_def', $data);
}
$this->load_definition();
}
// Return the maximum level of changes.
$changelevels = array_keys($haschanges);
sort($changelevels);
return array_pop($changelevels);
}
/**
* Marks all instances filled with this form with the status INSTANCE_STATUS_NEEDUPDATE
*/
public function mark_for_regrade() {
global $DB;
// if ($this->has_active_instances()) {
$conditions = array('definitionid' => $this->definition->id,
'status' => gradingform_instance::INSTANCE_STATUS_ACTIVE);
$DB->set_field('grading_instances', 'status', gradingform_instance::INSTANCE_STATUS_NEEDUPDATE, $conditions);
// change the final grade type from final to intermediary
/*$results = $DB->get_records('grading_instances',
array('definitionid' => $this->definition->id),null,'id,itemid');
$arrItemIDs = Array();
foreach ($results as $record) {
$arrItemIDs[$record->itemid] = 1;
}
foreach (array_keys($arrItemIDs) as $itemID) {
$conditions = array('itemid' => $itemID);
$DB->set_field('gradingform_multigraders_gra', 'type', gradingform_multigraders_instance::GRADE_TYPE_INTERMEDIARY, $conditions);
}*/
// }
}
/**
* Loads the form definition if it exists
*
*/
protected function load_definition() {
global $DB;
// Check to see if the user prefs have changed - putting here as this function is called on post even when
// validation on the page fails. - hard to find a better place to locate this as it is specific to the form.
// Get definition.
$definition = $DB->get_record('grading_definitions', array('areaid' => $this->areaid,
'method' => $this->get_method_name()), '*');
if (!$definition) {
// The definition doesn't have to exist. It may be that we are only now creating it.
$this->definition = false;
return false;
}
$this->definition = $definition;
$definitionExtras = $DB->get_record('gradingform_multigraders_def', array('id' => $this->definition->id), '*');
if (!$definitionExtras) {
//Populate with defaults
$this->definition->blind_marking = 0;
$this->definition->show_intermediary_to_students = 1;
$this->definition->show_notify_student_box = 0;
$this->definition->auto_calculate_final_method = 0;
$this->definition->secondary_graders_id_list = Array();
$this->definition->empty = true;
$this->definition->criteria = '';
}else{
$this->definition->blind_marking = $definitionExtras->blind_marking;
$this->definition->show_intermediary_to_students = $definitionExtras->show_intermediary_to_students;
$this->definition->show_notify_student_box = $definitionExtras->show_notify_student_box;
$this->definition->auto_calculate_final_method = $definitionExtras->auto_calculate_final_method;
$this->definition->secondary_graders_id_list = $definitionExtras->secondary_graders_id_list;
$this->definition->criteria = $definitionExtras->criteria;
unset($this->definition->empty);
}
$this->definition = $definition;
if (empty($this->moduleinstance)) { // Only set if empty.
$modulename = $this->get_component();
$context = $this->get_context();
if (stripos($modulename, 'mod_') === 0) {
$dbman = $DB->get_manager();
$modulename = substr($modulename, 4);
if ($dbman->table_exists($modulename)) {
$cm = get_coursemodule_from_id($modulename, $context->instanceid);
if (!empty($cm)) { // This should only occur when the course is being deleted.
$this->moduleinstance = $DB->get_record($modulename, array("id"=>$cm->instance));
}
}
}
}
}
/**
* Returns the default options for display
*
* @return array
*/
public static function get_default_options() {
$options = array(
'alwaysshowdefinition' => 1
);
return $options;
}
/**
* Gets the options of the definition, fills the missing options with default values
*
* @return array
*/
public function get_options() {
$options = self::get_default_options();
if (!empty($this->definition->options)) {
$thisoptions = json_decode($this->definition->options);
foreach ($thisoptions as $option => $value) {
$options[$option] = $value;
}
}
return $options;
}
/**
* Converts the current definition into an object suitable for the editor form's set_data()
*
* @return stdClass
*/
public function get_definition_for_editing() {
$definition = $this->get_definition();
$properties = new stdClass();
$properties->areaid = $this->areaid;
if (isset($this->moduleinstance->grade)) {
$properties->modulegrade = $this->moduleinstance->grade;
}
if ($definition) {
foreach (array('id', 'name', 'description','secondary_graders_id_list','criteria', 'blind_marking','show_intermediary_to_students','auto_calculate_final_method','show_notify_student_box') as $key) {
$properties->$key = $definition->$key;
if($key == 'criteria'){
$properties->$key = Array();
$properties->$key['text'] = $definition->criteria;
$properties->$key['format'] = 1;
}
}
/*$options = self::description_form_field_options($this->get_context());
$properties = file_prepare_standard_editor($properties, 'description', $options, $this->get_context(),
'grading', 'description', $definition->id);*/
}
return $properties;
}
/**
* Returns the form definition suitable for cloning into another area
*
* @see parent::get_definition_copy()
* @param gradingform_controller $target the controller of the new copy
* @return stdClass definition structure to pass to the target's {@link update_definition()}
*/
public function get_definition_copy(gradingform_controller $target) {
$new = parent::get_definition_copy($target);
$old = $this->get_definition_for_editing();
return $new;
}
/**
* Options for displaying the form description field in the form
*
* @param context $context
* @return array options for the form description field
*/
public static function description_form_field_options($context) {
global $CFG;
return array(
'maxfiles' => -1,
'maxbytes' => get_max_upload_file_size($CFG->maxbytes),
'context' => $context,
);
}
/**
* Formats the definition description for display on page
*
* @return string
*/
public function get_formatted_description_multigraders() {
if (!isset($this->definition->description)) {
return '';
}
$context = $this->get_context();
$formatoptions = array(
'noclean' => false,
'trusted' => false,
'filter' => true,
'context' => $context
);
$text = get_string('pluginname','gradingform_multigraders');
$text .= "\n".$this->definition->description;
if($this->definition->criteria) {
$text .= "\n" . get_string('criteria', 'gradingform_multigraders') . ": ";
$text .= $this->definition->criteria;
}
if(isset($this->definition->secondary_graders_id_list) &&
$this->definition->secondary_graders_id_list != ''){
//transform list of grader ids into name list
$mainuserfields = user_picture::fields();
$dbUsers = get_users(true,'',true,null,'lastname ASC,firstname ASC',
$firstinitial='', $lastinitial='', $page=0, $recordsperpage=100, $fields=$mainuserfields,
$extraselect='id IN ('.$this->definition->secondary_graders_id_list.')');
$secondaryGraders = '';
foreach($dbUsers as $id => $oUser){
$secondaryGraders .= fullname($oUser).', ';
}
$secondaryGraders = substr($secondaryGraders,0,-2);
$text .= "\n".get_string('secondary_graders_list','gradingform_multigraders',$secondaryGraders);
}
if(isset($this->definition->blind_marking) && $this->definition->blind_marking == 1){
$text .= "\n".get_string('blind_marking_explained','gradingform_multigraders');
}
if(isset($this->definition->show_intermediary_to_students) && $this->definition->show_intermediary_to_students == 1){
$text .= "\n".get_string('show_intermediary_to_students_explained','gradingform_multigraders');
}
/*if(isset($this->definition->previous_graders_cant_change) && $this->definition->previous_graders_cant_change == 1){
$text .= "\n".get_string('previous_graders_cant_change_explained','gradingform_multigraders');
}*/
if(isset($this->definition->auto_calculate_final_method)){
$text .= "\n".get_string('auto_calculate_final_method','gradingform_multigraders').": ";
switch($this->definition->auto_calculate_final_method){
case 0:
$text .= get_string('auto_calculate_final_method_0','gradingform_multigraders');
break;
case 1:
$text .= get_string('auto_calculate_final_method_1','gradingform_multigraders');
break;
case 2:
$text .= get_string('auto_calculate_final_method_2','gradingform_multigraders');
break;
case 3:
$text .= get_string('auto_calculate_final_method_3','gradingform_multigraders');
break;
}
}
return format_text($text, FORMAT_MOODLE, $formatoptions);
}
/**
* Returns the plugin renderer
*
* @param moodle_page $page the target page
* @return gradingform_multigraders_renderer
*/
public function get_renderer(moodle_page $page) {
return $page->get_renderer('gradingform_'. $this->get_method_name());
}
/**
* Returns the HTML code displaying the preview of the grading form
*
* @param moodle_page $page the target page
* @return string
*/
public function render_preview(moodle_page $page) {
if (!$this->is_form_defined()) {
throw new coding_exception('It is the caller\'s responsibility to make sure that the form is actually defined');
}
// Check if current user is able to see preview
$options = $this->get_options();
if (empty($options['alwaysshowdefinition']) && !has_capability('moodle/grade:managegradingforms', $page->context)) {
return '';
}
$mode = gradingform_multigraders_controller::DISPLAY_VIEW;
if (has_capability('moodle/grade:manage', $page->context)) {
$mode = gradingform_multigraders_controller::DISPLAY_EVAL_FULL;
}elseif (has_capability('moodle/grade:edit', $page->context)) {
$mode = gradingform_multigraders_controller::DISPLAY_EVAL;
}elseif (has_capability('moodle/grade:viewall', $page->context)) {
$mode = gradingform_multigraders_controller::DISPLAY_VIEW;
}elseif (has_capability('moodle/grade:view', $page->context)) {
$mode = gradingform_multigraders_controller::DISPLAY_VIEW;
}
if($mode == gradingform_multigraders_controller::DISPLAY_VIEW){
$html = get_string('pluginname','gradingform_multigraders');
if($this->definition->criteria){
$html = $this->definition->criteria;
}
return $html;
}
return $this->get_formatted_description_multigraders($page);
}
/**
* Deletes the form definition and all the associated information
*/
protected function delete_plugin_definition() {
global $DB;
// Get the list of instances.
$instances = array_keys($DB->get_records('grading_instances', array('definitionid' => $this->definition->id), '', 'id'));
// Delete instances.
$DB->delete_records_list('gradingform_multigraders_gra', 'id', $instances);
//delete extra defition details
$DB->delete_records('gradingform_multigraders_def', array('id' => $this->definition->id));
}
/**
* If instanceid is specified and grading instance exists and it is created by this rater for
* this item, this instance is returned.
* If there exists a draft for this raterid+itemid, take this draft (this is the change from parent)
* Otherwise new instance is created for the specified rater and itemid
*
* @param int $instanceid
* @param int $raterid
* @param int $itemid
* @return gradingform_instance
*/
public function get_or_create_instance($instanceid, $raterid, $itemid) {
global $DB;
if ($instanceid &&
$instance = $DB->get_record('grading_instances',
array('id' => $instanceid, 'raterid' => $raterid, 'itemid' => $itemid), '*', IGNORE_MISSING)) {
return $this->get_instance($instance);
}
if ($itemid && $raterid) {
$params = array('definitionid' => $this->definition->id, 'raterid' => $raterid, 'itemid' => $itemid);
if ($rs = $DB->get_records('grading_instances', $params, 'timemodified DESC', '*', 0, 1)) {
$record = reset($rs);
$currentinstance = $this->get_current_instance($raterid, $itemid);
if ($record->status == gradingform_multigraders_instance::INSTANCE_STATUS_INCOMPLETE &&
(!$currentinstance || $record->timemodified > $currentinstance->get_data('timemodified'))) {
$record->isrestored = true;
return $this->get_instance($record);
}
}
}
return $this->create_instance($raterid, $itemid);
}
/**
* Returns html code to be included in student's feedback.
*
* @param moodle_page $page
* @param int $itemid
* @param array $gradinginfo result of function grade_get_grades
* @param string $defaultcontent default string to be returned if no active grading is found
* @param bool $cangrade whether current user has capability to grade in this context
* @return string
*/
public function render_grade($page, $itemid, $gradinginfo, $defaultcontent, $cangrade) {
return $this->get_renderer($page)->display_instances($this->get_active_instances($itemid),$defaultcontent, $cangrade);
}
// Full-text search support.
/**
* Prepare the part of the search query to append to the FROM statement
*
* @param string $gdid the alias of grading_definitions.id column used by the caller
* @return string
*/
public static function sql_search_from_tables($gdid) {
return "";
//return " LEFT JOIN {gradingform_multigraders_criteria} gc ON (gc.definitionid = $gdid)";
}
/**
* Prepare the parts of the SQL WHERE statement to search for the given token
*
* The returned array consists of the list of SQL comparisons and the list of
* respective parameters for the comparisons. The returned chunks will be joined
* with other conditions using the OR operator.
*
* @param string $token token to search for
* @return array An array containing two more arrays
* Array of search SQL fragments
* Array of params for the search fragments
*/
public static function sql_search_where($token) {
global $DB;
$subsql = array();
$params = array();
return array($subsql, $params);
}
/**
* @return array An array containing 1 key/value pairs which hold the external_multiple_structure
* @see gradingform_controller::get_external_definition_details()
* @since Moodle 2.5
*/
public static function get_external_definition_details() {
$grades_criteria = new external_multiple_structure(
new external_single_structure(
array(
'secondary_graders_id_list' => new external_value(PARAM_TEXT, '', VALUE_REQUIRED),
'criteria' => new external_value(PARAM_RAW, '', VALUE_REQUIRED),
'blind_marking' => new external_value(PARAM_INT, 'if blind grading is enabled', VALUE_REQUIRED),
'show_intermediary_to_students' => new external_value(PARAM_INT, 'if intermediary grades are shown to students', VALUE_REQUIRED),
'auto_calculate_final_method' => new external_value(PARAM_INT, 'method of calculating the final grade', VALUE_REQUIRED),
)
));
return array('grades_criteria' => $grades_criteria);
}
/**
* Returns an array that defines the structure of the form's filling. This function is used by
* the web service function core_grading_external::get_gradingform_instances().
*
* @return An array containing a single key/value pair with the 'grades' external_multiple_structure
* @see gradingform_controller::get_external_instance_filling_details()
* @since Moodle 2.6
*/
public static function get_external_instance_filling_details() {
$grades = new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'filling id'),
'instanceid' => new external_value(PARAM_INT, 'instance id'),
'itemid' => new external_value(PARAM_INT, 'item id', VALUE_OPTIONAL),
'grader' => new external_value(PARAM_INT, 'grader id', VALUE_OPTIONAL),
'grade' => new external_value(PARAM_FLOAT, 'the grade',VALUE_OPTIONAL),
'feedback' => new external_value(PARAM_RAW, 'feedback', VALUE_OPTIONAL),
'type' => new external_value(PARAM_INT, 'type', VALUE_OPTIONAL),
'visible_to_students' => new external_value(PARAM_INT, 'visible_to_students', VALUE_OPTIONAL),
'outcomes' => new external_value(PARAM_RAW, 'outcomes', VALUE_OPTIONAL),
'require_second_grader' => new external_value(PARAM_INT, 'require_second_grader', VALUE_OPTIONAL)
)
), 'grade', VALUE_OPTIONAL
);
return array('grades' => $grades);
}
/**
* Publish all selected student marks
* @param string $error The error messages
* @param array $data The list of selected userid, grader and itemid
*/
public static function update_multigraders_feedback($data,&$error)
{
global $DB, $PAGE, $USER;
$grade=0;
$grader=$USER->id;
$user = $DB->get_record('user', array('id' => $data['userid']));
$user_name= fullname($user);
if($data['grader'] == -1){
$error = get_string('err_notgraded','gradingform_multigraders', $user_name);
}
else
{
$sql = 'SELECT id,instanceid,itemid, grader, grade,type
FROM public.mdl_gradingform_multigraders_gra
where itemid='.$data['itemid'].'
order by id asc
limit 1';
$categories = $DB->get_recordset_sql($sql);
foreach ($categories as $category) {
if($category->grader == $grader)
{
$grade= $category->grade;
if($category->type == 0)
{
$gradeType = gradingform_multigraders_instance::GRADE_TYPE_FINAL;
//Grade is published
$newrecord = array('id' => $category->id,'type' => $gradeType);
$DB->update_record('gradingform_multigraders_gra', $newrecord,TRUE);
}
}
else
{
$error = get_string('err_grader_intermediary','gradingform_multigraders', $user_name);
}
}
}
return $grade;
}
}
/**
* Class to manage one form grading instance.
*
* Stores information and performs actions like update, copy, validate, submit, etc.
*
* @package gradingform_multigraders
* @copyright 2018 Lucian Pricop <contact@lucianpricop.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradingform_multigraders_instance extends gradingform_instance {
/** @var array of errors per <grader><record type> values */
public $validationErrors;
/** @var array of options defined in the gradingform definition */
public $options;
/** @var stdClass with minRange of maxRange of the grading range */
protected $gradeRange;
/** @var array of grades for this instance */
protected $instanceGrades;
/** @var string debugging log */
protected $log;
/** Intermediate grade type - e.g not the final one */
const GRADE_TYPE_INTERMEDIARY = 0;
/** Final grade */
const GRADE_TYPE_FINAL = 1;
/**
* Creates a gradingform_multigraders instance
*
* @param gradingform_controller $controller
* @param stdClass $data
*/
public function __construct($controller, $data) {
parent::__construct($controller,$data);
$definition = $this->get_controller()->get_definition();
$this->options = new stdClass();
if($definition) {
foreach (array('secondary_graders_id_list','criteria', 'blind_marking','show_intermediary_to_students','auto_calculate_final_method','show_notify_student_box') as $key) {
if (isset($definition->$key)) {
$this->options->$key = $definition->$key;
}
}
}
$this->log = '';
}
/**
* Returns a class with minGrade and maxGrade for attributes
* @param bool $forceRefresh
* @return stdClass
*/
public function getGradeRange($forceRefresh = false){
if($this->gradeRange == null || $forceRefresh) {
$graderange = array_values($this->get_controller()->get_grade_range());
if (!empty($graderange)) {
$this->gradeRange = new stdClass();
sort($graderange);
$this->gradeRange->minGrade = $graderange[0];
$this->gradeRange->maxGrade = $graderange[count($graderange) - 1];
$cutPos = strpos($this->gradeRange->minGrade,'/');
if($cutPos !== FALSE){
$this->gradeRange->minGrade = floatval(substr($this->gradeRange->minGrade,0,$cutPos));
}
$cutPos = strpos($this->gradeRange->maxGrade,'/');
if($cutPos !== FALSE){
$this->gradeRange->maxGrade = floatval(substr($this->gradeRange->maxGrade,$cutPos+1));
}
if($this->gradeRange->minGrade == 1){
$this->gradeRange->minGrade = 0;
}
}
}
return $this->gradeRange;
}
/**
* Returns the item id of the current instance
* @return int itemid
*/
public function getItemID(){
return intval($this->get_data('itemid'));
}
/**
* Deletes this (INCOMPLETE) instance from database.
*/
public function cancel() {
global $DB;
parent::cancel();
$DB->delete_records('gradingform_multigraders_gra', array('instanceid' => $this->get_id()));
}
/**
* Duplicates the instance before editing (optionally substitutes raterid and/or itemid with
* the specified values)
*
* @param int $raterid value for raterid in the duplicate
* @param int $itemid value for itemid in the duplicate
* @return int id of the new instance
*/
public function copy($raterid, $itemid) {
global $DB,$USER;
$instanceid = parent::copy($raterid, $itemid);
/*$currentgrade = $this->get_instance_grades();
foreach ($currentgrade['grades'] as $grader => $record) {
if($grader == $USER->id) {
$record['instanceid'] = $instanceid;
$record['itemid'] = $itemid;
$DB->insert_record('gradingform_multigraders_gra', $record);
}
}*/
return $instanceid;
}
/**
* Determines whether the submitted form was empty.
*
* @param array $elementvalue value of element submitted from the form
* @return boolean true if the form is empty
*/
public function is_empty_form($elementvalue) {
/*if (!isset($elementvalue['grade']) && !isset($elementvalue['feedback'])) {
return true;
}*/
return false;//let update handle the form submit
}
/**
* Validates that form contains valid grade
*
* @param array $elementvalue value of element as came in form submit
* @return boolean true if the form data is validated and contains no errors
*/
public function validate_grading_element($elementvalue) {
global $USER;
$this->log .= 'validate_grading_element multigraders_delete_all:'.$elementvalue['multigraders_delete_all'].'. ';
if(isset($elementvalue['multigraders_delete_all']) && $elementvalue['multigraders_delete_all']=='true') {
$this->log .= 'validate_grading_element ret true. ';;
return true;
}
if (!isset($elementvalue['grade']) || !is_string($elementvalue['grade'])){
return true;
}
// Reset validation errors.
$this->validationErrors = Array();
if(!isset($elementvalue['grader'])){
$elementvalue['grader'] = $USER->id;
}
if (!is_numeric($elementvalue['grade']) || $elementvalue['grade'] < 0) {
$this->validationErrors[$elementvalue['grader'].$elementvalue['type']] = get_string('err_gradeinvalid','gradingform_multigraders');
return false;
}
$elementvalue['grade'] = floatval($elementvalue['grade']);
if($this->getGradeRange()) {
if ($this->getGradeRange()->minGrade && $elementvalue['grade'] < $this->getGradeRange()->minGrade
||
$this->getGradeRange()->maxGrade && $elementvalue['grade'] > $this->getGradeRange()->maxGrade) {
ob_start();
var_dump($this->getGradeRange());
$echo = ob_get_contents();
ob_end_clean();
$this->validationErrors[$elementvalue['grader'] . $elementvalue['type']] = $elementvalue['grade'] . ' ' . get_string('err_gradeoutofbounds', 'gradingform_multigraders') . ' ' . $echo;
return false;
}
}
return true;
}
/**
* Retrieves from DB and returns the data for this form
*
* @param bool $force whether to force DB query even if the data is cached
* @return array
*/
public function get_instance_grades($force = false) {
global $DB;
if ($this->instanceGrades === null || $force) {
$records = $DB->get_records('gradingform_multigraders_gra', array('itemid' => $this->getItemID()), 'timestamp');
$this->instanceGrades = array('grades' => array());
foreach ($records as $record) {
$record->grade = (float)$record->grade; // Strip trailing 0.
$record->type = intval($record->type);
$record->visible_to_students = (intval($record->visible_to_students)==1); //make DB int val into boolean.
$record->require_second_grader = (intval($record->require_second_grader)==1); //make DB int val into boolean.
$record->outcomes = json_decode($record->outcomes); //transform outcomes from JSON to object
$this->instanceGrades['grades'][$record->grader] = $record;
}
}
return $this->instanceGrades;
}
/**
* Updates the instance with the data received from grading form. This function may be
* called via AJAX when grading is not yet completed, so it does not change the
* status of the instance.
*
* @param array $data
*/
public function update($data) {
global $DB,$USER;
$currentFormData = $this->get_instance_grades();
$currentRecordID = null;
$firstGradeRecord = null;
$currentRecord = null;
$finalGradeRecord = null;
//check first if an admin wants to delete everything for this grade
//check if multigraders_delete_all parameter was sent
if(isset($data['multigraders_delete_all']) && $data['multigraders_delete_all']=='true') {
//check if user is admin
$systemcontext = context_system::instance();
if(has_capability('moodle/site:config', $systemcontext)) {
$this->log .= 'update() moodle/site:config is true. ';
parent::update($data);
$DB->delete_records('gradingform_multigraders_gra',array('itemid' => $this->getItemID()));
$this->data->rawgrade = -1;
$newdata = new stdClass();
$newdata->id = $this->get_id();
$newdata->rawgrade = -1;
$DB->update_record('grading_instances', $newdata);
}
$this->get_instance_grades(true);
return;
}
foreach ($currentFormData['grades'] as $grader=> $record) {
if(!$firstGradeRecord){
$firstGradeRecord = $record;
}
if($grader == $USER->id){
$currentRecord = $record;
}
if($record->type == gradingform_multigraders_instance::GRADE_TYPE_FINAL){
$finalGradeRecord = $record;
}
}
//if the final grade is already added for this instance and it wasn't given by the current teacher, then they can't edit anything.
if($finalGradeRecord !==null && $currentRecord === null ){
return;
}
//if the final grade is already added for this instance, but by a different teacher, don't allow any saves
if($finalGradeRecord !==null && $finalGradeRecord->grader != $USER->id){
return;
}
$outcomes = null;
if(isset($data['outcome'])) {
$outcomes = json_encode($data['outcome']);
}
if(isset($data['grade_hidden'])){
$data['grade'] = $data['grade_hidden'];
}
//updating instanceid for all records of the same item
$conditions = array('itemid' => $data['itemid']);
$DB->set_field('gradingform_multigraders_gra', 'instanceid', $this->get_id(), $conditions);
$gradeType = gradingform_multigraders_instance::GRADE_TYPE_INTERMEDIARY;
$gradingFinal = false;
if(isset($data['grading_final'])){
$gradingFinal = true;
if(isset($data['final_grade'])) {
$gradeType = gradingform_multigraders_instance::GRADE_TYPE_FINAL;
}
}
//adding a new record
if(isset($data['grade']) &&
$data['grade'] !='' &&
is_array($data['grade']) &&
isset($data['grade'][$USER->id])) {
$data['grade'] = $data['grade'][$USER->id];
}
if($currentRecord !== null){
$currentRecordID = $DB->get_field('gradingform_multigraders_gra','id',
array('itemid' => $data['itemid'],'grader' => $USER->id));
}
parent::update($data);
$newrecord = array('instanceid' => $this->get_id(),
'itemid' => $data['itemid'],
'grader' => $USER->id,
'grade' => $data['grade'],
'feedback' => $data['feedback'] ? ($data['feedback'][$USER->id] ? $data['feedback'][$USER->id]['text'] : '') : '',
'type' => $gradeType,
'timestamp' => time(),
'visible_to_students' => $data['visible_to_students'],
'require_second_grader' => $data['require_second_grader'],
'outcomes' => $outcomes);
if($currentRecordID){
$newrecord['id'] = $currentRecordID;
unset($newrecord['timestamp']);
$DB->update_record('gradingform_multigraders_gra', $newrecord);
}else {
$DB->insert_record('gradingform_multigraders_gra', $newrecord);
}
//grade type is not null only when the grading owner(or first/final grader) is saving the data
if($gradingFinal){
if($gradeType == gradingform_multigraders_instance::GRADE_TYPE_FINAL) {
$this->data->rawgrade = $data['grade'];
$this->data->grade = $data['grade'];
}else{
$this->data->rawgrade = -1;
}