-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
3737 lines (3304 loc) · 194 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 Assessment module for 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/>.
/**
* assessment module library
*
* @package mod
* @subpackage assessment
* @author Andy Chan, CITE, HKU <[email protected]>
* @copyright 2012 Andy Chan <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once ('rubric/lib.php');
require_once($CFG->libdir.'/formslib.php');
class assessment_base {
var $cm;
var $course;
var $activitydetail;
var $assessment;
var $strassessment;
var $strassessments;
var $strlastmodified;
var $pagetitle;
var $usehtmleditor;
var $defaultformat;
var $context;
var $type;
var $rubric;
/**
* Constructor
*/
function __construct($cmid='staticonly', $assessment=NULL, $cm=NULL, $course=NULL) {
global $COURSE, $DB;
if ($cmid == 'staticonly') {
//use static functions only!
return;
}
global $CFG;
if ($cm) {
$this->cm = $cm;
} else if (! $this->cm = get_coursemodule_from_id('assessment', $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', array('id'=>$this->cm->course))) {
print_error('coursemisconf', 'assessment');
}
if ($assessment) {
$this->assessment = $assessment;
} else if (! $this->assessment = $DB->get_record('assessment', array('id'=>$this->cm->instance))) {
print_error('invalidid', 'assessment');
}
if ($teacherassessment = $DB->get_record('assessment_types', array('assessmentid'=>$this->assessment->id, 'type'=>0))) {
$this->assessment->teacher = $teacherassessment->id;
$this->assessment->teacherweight = $teacherassessment->weight;
$this->assessment->teachertimestart = $teacherassessment->timestart;
$this->assessment->teachertimeend = $teacherassessment->timeend;
$this->assessment->teachertimepublish = $teacherassessment->timepublish;
}
if ($selfassessment = $DB->get_record('assessment_types', array('assessmentid'=>$this->assessment->id, 'type'=>1))) {
$this->assessment->self = $selfassessment->id;
$this->assessment->selfweight = $selfassessment->weight;
$this->assessment->selftimestart = $selfassessment->timestart;
$this->assessment->selftimeend = $selfassessment->timeend;
$this->assessment->selftimepublish = $selfassessment->timepublish;
}
if ($peerassessment = $DB->get_record('assessment_types', array('assessmentid'=>$this->assessment->id, 'type'=>2))) {
$this->assessment->peer = $peerassessment->id;
$this->assessment->peerweight = $peerassessment->weight;
$this->assessment->peernum = $peerassessment->peernum;
$this->assessment->peergroupmode = $peerassessment->peergroupmode;
$this->assessment->peertimestart = $peerassessment->timestart;
$this->assessment->peertimeend = $peerassessment->timeend;
$this->assessment->peertimepublish = $peerassessment->timepublish;
}
$this->assessment->cmidnumber = $this->cm->id; // compatibility with modedit assessment obj
$this->assessment->courseid = $this->course->id; // compatibility with modedit assessment obj
$this->strassessment = get_string('modulename', 'assessment');
$this->strassessments = get_string('modulenameplural', 'assessment');
$this->strlastmodified = get_string('lastmodified');
$this->pagetitle = strip_tags($this->course->shortname.': '.$this->strassessment.': '.format_string($this->assessment->name,true));
$this->rubric = new rubric($this->assessment->rubricid, $this->assessment);
if ($this->usehtmleditor) {
$this->defaultformat = FORMAT_HTML;
} else {
$this->defaultformat = FORMAT_MOODLE;
}
}
function add_instance($assessment) {
global $CFG, $COURSE, $DB;
$assessment->timemodified = time();
if ($assessment->rubricid != 0) {
$selected_rubric = new rubric($assessment->rubricid);
$assessment->grade = $selected_rubric->points;
}
$assessment->id = $DB->insert_record("assessment", $assessment);
if ($assessment->teacher == 1) {
$assessment_teacher = new stdClass();
$assessment_teacher->assessmentid = $assessment->id;
$assessment_teacher->type = 0;
$assessment_teacher->timestart = $assessment->teacherstart;
$assessment_teacher->timeend = $assessment->teacherend;
$assessment_teacher->timepublish = $assessment->teacherpublish;
$assessment_teacher->weight = $assessment->teacherweight;
$assessment_teacher->peernum = 0;
$assessment_teacher->peergroupmode = 0;
$DB->insert_record('assessment_types', $assessment_teacher);
}
if ($assessment->self == 1) {
$assessment_self = new stdClass();
$assessment_self->assessmentid = $assessment->id;
$assessment_self->type = 1;
$assessment_self->timestart = $assessment->selfstart;
$assessment_self->timeend = $assessment->selfend;
$assessment_self->timepublish = $assessment->selfpublish;
$assessment_self->weight = $assessment->selfweight;
$assessment_self->peernum = 0;
$assessment_self->peergroupmode = 0;
$DB->insert_record('assessment_types', $assessment_self);
}
if ($assessment->peer == 1) {
$assessment_peer = new stdClass();
$assessment_peer->assessmentid = $assessment->id;
$assessment_peer->type = 2;
$assessment_peer->timestart = $assessment->peerstart;
$assessment_peer->timeend = $assessment->peerend;
$assessment_peer->timepublish = $assessment->peerpublish;
$assessment_peer->weight = $assessment->peerweight;
$assessment_peer->peernum = $assessment->peernum;
$assessment_peer->peergroupmode = $assessment->peergroupmode;
$DB->insert_record('assessment_types', $assessment_peer);
}
# May have to add extra stuff in here #
if ($assessment->forum) {
$this->add_discussions($assessment);
}
return $assessment->id;
}
function add_discussions($assessment) {
global $CFG, $COURSE, $DB;
// If there is discussion already exist, skip adding new discussions all together
// !!!!!May have to do it more properly by only skipping existing discussion and create new discussion for new users
if ($DB->get_records("assessment_discussions", array("assessmentid"=>$assessment->id))) {
return true;
}
require_once('locallib.php');
if (isset($assessment->workmode) && $assessment->workmode == 1) {
require_once($CFG->libdir.'/grouplib.php');
$groupsincourse = groups_get_all_groups($COURSE->id);
foreach ($groupsincourse as $singlegroup) {
$groupmembers = groups_get_members($singlegroup->id);
if (!empty($groupmembers)) {
$first_groupmember = array_shift($groupmembers);
$first_groupmember_id = $first_groupmember->id;
} else {
continue;
}
$discussion = new stdClass();
$discussion->assessmentid = $assessment->id;
$discussionintro = get_string('discussionintro', 'assessment');
$discussion->name = str_replace('studentname', $singlegroup->name, $discussionintro);
$discussion->intro = get_string('pleasereply', 'assessment');
$discussion->format = 0;
$discussion->userid = $first_groupmember_id;
$discussion->groupid = $singlegroup->id;
if (! $discussionid = assessment_add_discussion($discussion, $discussion->intro)) {
print_error('erroradddiscussion', 'assessment');
}
if (! $DB->set_field("assessment_discussions", "userid", $first_groupmember_id, array("id"=>$discussionid))) {
print_error('errormodifydiscussionauthor', 'assessment');
}
if (! $DB->set_field("assessment_posts", "userid", $first_groupmember_id, array("discussionid"=>$discussionid))) {
print_error('errormodifypostauthor', 'assessment');
}
}
} else {
/// Get all ppl that are allowed to submit assessment
$context = context_course::instance($COURSE->id);
if ($users = get_users_by_capability($context, 'mod/assessment:submit', 'u.id', '', '', '', '', '', false)) {
$users = array_keys($users);
} else {
// No users!
return true;
}
$fullnameformat = get_config('', 'fullnamedisplay');
$prefix = 'u.';
if ($fullnameformat == 'lastname firstname') {
$selectfullname = $prefix.'lastname, " ", '.$prefix.'firstname';
} else if ($fullnameformat == 'firstname lastname') {
$selectfullname = $prefix.'firstname, " ", '.$prefix.'lastname';
} else {
$selectfullname = $prefix.'lastname, " ", '.$prefix.'firstname';
}
$selectfullname = 'CONCAT('.$selectfullname.')';
list($in_sql, $in_params) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED);
$sql = "SELECT u.id, ".$selectfullname." AS fullname FROM {user} u ";
$sql .= "WHERE u.id $in_sql";
$ausers = $DB->get_records_sql($sql, $in_params);
foreach ($ausers as $auser) {
$discussion = new stdClass();
$discussion->assessmentid = $assessment->id;
$discussionintro = get_string('discussionintro', 'assessment');
$discussion->name = str_replace('studentname', $auser->fullname, $discussionintro);
$discussion->intro = get_string('pleasereply', 'assessment');
$discussion->format = 0;
$discussion->userid = $auser->id;
$discussion->groupid = -1;
if (! $discussionid = assessment_add_discussion($discussion, $discussion->intro)) {
print_error('erroradddiscussion', 'assessment');
}
if (! $DB->set_field("assessment_discussions", "userid", $auser->id, array("id"=>$discussionid))) {
print_error('errormodifydiscussionauthor', 'assessment');
}
if (! $DB->set_field("assessment_posts", "userid", $auser->id, array("discussionid"=>$discussionid))) {
print_error('errormodifypostauthor', 'assessment');
}
}
}
return true;
}
function delete_instance($assessment) {
global $DB;
$result = true;
# Delete any dependent records here #
// delete forum
if ($assessment->forum) {
$result = $this->delete_forum($assessment);
}
// delete all submissions with all attachments - ignore errors
$this->delete_submission_files($assessment);
// Delete grade related records
if ($allassessmentids = $DB->get_records('assessment_types', array('assessmentid'=>$assessment->id), '', 'id')) {
$allassessmentids = array_keys($allassessmentids);
list($in_sql1, $in_params1) = $DB->get_in_or_equal($allassessmentids, SQL_PARAMS_NAMED);
if ($allgradeids = $DB->get_records_select('assessment_grades', 'assessmentid '.$in_sql1, $in_params1)) {
$allgradeids = array_keys($allgradeids);
list($in_sql2, $in_params2) = $DB->get_in_or_equal($allgradeids, SQL_PARAMS_NAMED);
$deletegradespec = $DB->delete_records_select('assessment_grade_specs', 'gradeid '.$in_sql2, $in_params2);
}
$deletegrades = $DB->delete_records_select('assessment_grades', 'assessmentid '.$in_sql1, $in_params1);
}
if (! $DB->delete_records("assessment_types", array("assessmentid"=>$assessment->id))) {
$result = false;
}
if (! $DB->delete_records("assessment_submissions", array("assessmentid"=>$assessment->id))) {
$result = false;
}
if (! $DB->delete_records("assessment", array("id"=>$assessment->id))) {
$result = false;
}
assessment_grade_item_delete($assessment);
return $result;
}
function delete_submission_files($assessment) {
// now get rid of all files
$fs = get_file_storage();
if ($cm = get_coursemodule_from_instance('assessment', $assessment->id)) {
$context = context_module::instance($cm->id);
$fs->delete_area_files($context->id);
}
}
function delete_forum($assessment) {
global $DB;
$result = true;
if ($discussions = $DB->get_records("assessment_discussions", array("assessmentid"=>$assessment->id))) {
foreach ($discussions as $discussion) {
if (! $DB->delete_records("assessment_posts", array("discussionid"=>$discussion->id))) {
$result = false;
}
}
}
if (! $DB->delete_records("assessment_discussions", array("assessmentid"=>$assessment->id))) {
$result = false;
}
if (! $DB->delete_records("assessment_read", array("assessmentid"=>$assessment->id))) {
$result = false;
}
return $result;
}
function update_instance($assessment) {
global $CFG, $DB;
$assessment->timemodified = time();
$assessment->id = $assessment->instance;
if ($assessment->teacher == 1) {
$assessment_teacher = new stdClass();
$assessment_teacher->assessmentid = $assessment->id;
$assessment_teacher->type = 0;
$assessment_teacher->timestart = $assessment->teacherstart;
$assessment_teacher->timeend = $assessment->teacherend;
$assessment_teacher->timepublish = $assessment->teacherpublish;
$assessment_teacher->weight = $assessment->teacherweight;
$assessment_teacher->peernum = 0;
$assessment_teacher->peergroupmode = 0;
if ($teacher = $DB->get_record("assessment_types", array("assessmentid"=>$assessment->id, "type"=>0))) {
$assessment_teacher->id = $teacher->id;
$DB->update_record("assessment_types", $assessment_teacher);
} else {
$assessment_teacher->id = $DB->insert_record("assessment_types", $assessment_teacher);
}
$assessment->teacher = $assessment_teacher->id;
} else {
$DB->delete_records("assessment_types", array("assessmentid"=>$assessment->id, "type"=>0));
}
if ($assessment->self == 1) {
$assessment_self = new stdClass();
$assessment_self->assessmentid = $assessment->id;
$assessment_self->type = 1;
$assessment_self->timestart = $assessment->selfstart;
$assessment_self->timeend = $assessment->selfend;
$assessment_self->timepublish = $assessment->selfpublish;
$assessment_self->weight = $assessment->selfweight;
$assessment_self->peernum = 0;
$assessment_self->peergroupmode = 0;
if ($self = $DB->get_record("assessment_types", array("assessmentid"=>$assessment->id, "type"=>1))) {
$assessment_self->id = $self->id;
$DB->update_record("assessment_types", $assessment_self);
} else {
$assessment_self->id = $DB->insert_record("assessment_types", $assessment_self);
}
$assessment->self = $assessment_self->id;
} else {
$DB->delete_records("assessment_types", array("assessmentid"=>$assessment->id, "type"=>1));
}
if ($assessment->peer == 1) {
$assessment_peer = new stdClass();
$assessment_peer->assessmentid = $assessment->id;
$assessment_peer->type = 2;
$assessment_peer->timestart = $assessment->peerstart;
$assessment_peer->timeend = $assessment->peerend;
$assessment_peer->timepublish = $assessment->peerpublish;
$assessment_peer->weight = $assessment->peerweight;
$assessment_peer->peernum = $assessment->peernum;
$assessment_peer->peergroupmode = $assessment->peergroupmode;
if ($peer = $DB->get_record("assessment_types", array("assessmentid"=>$assessment->id, "type"=>2))) {
$assessment_peer->id = $peer->id;
$DB->update_record("assessment_types", $assessment_peer);
} else {
$assessment_peer->id = $DB->insert_record("assessment_types", $assessment_peer);
}
$assessment->peer = $assessment_peer->id;
} else {
$DB->delete_records("assessment_types", array("assessmentid"=>$assessment->id, "type"=>2));
}
if ($assessment->rubricid != 0) {
$selected_rubric = new rubric($assessment->rubricid);
$assessment->grade = $selected_rubric->points;
}
// Add a new forum
if ($assessment->forum != 0) {
$this->add_discussions($assessment);
} else {
$this->delete_forum($assessment);
}
assessment_grade_item_update($assessment);
return $DB->update_record("assessment", $assessment);
}
function view() {
global $CFG, $DB, $USER, $SESSION, $OUTPUT, $PAGE;
require_once($CFG->libdir.'/gradelib.php');
$course = $this->course;
$assessment = $this->assessment;
$cm = $this->cm;
$context = $this->context;
require_capability('mod/assessment:view', $context);
$PAGE->requires->js_init_call('initRubricStr', array(get_string('hiderubric', 'assessment'), get_string('showrubric', 'assessment')));
$this->view_header();
$course_context = context_course::instance($course->id);
if (has_capability('gradereport/grader:view', $course_context) && has_capability('moodle/grade:viewall', $course_context)) {
echo '<div class="allcoursegrades"><a href="view_gradebook.php?id=' . $course->id . '">'
. get_string('seeallcoursegrades', 'grades') . '</a></div>';
}
$is_submit_files = $assessment->numfiles;
$this->view_intro();
$perpage = 100;
$pageno = optional_param('page', 0, PARAM_INT);
$width = 800;
$currentgroup = '';
/// find out current groups mode
$groupmode = groups_get_activity_groupmode($cm);
$currentgroup = groups_get_activity_group($cm, true);
if (!$allowgroups = groups_get_activity_allowed_groups($cm)) $allowgroups = array();
$groupids = array_keys($allowgroups);
/// Get all ppl that are allowed to submit assessment
if ($users = get_users_by_capability($context, 'mod/assessment:submit', 'u.id', 'u.id ASC', '', '', $currentgroup, '', false)) {
$users = array_keys($users);
}
if ($users and !empty($CFG->enablegroupings) and $cm->groupmembersonly) {
if ($groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id ASC')) {
$users = array_intersect($users, array_keys($groupingusers));
}
}
/*************************************************/
// Work mode is group instead of individual
if ($assessment->workmode != 0) {
$workmode = $assessment->workmode = 'group';
$workmodeids = $groupids;
$totalnum = empty($groupids) ? 0 : count($groupids);
$tablecol_name = $assessment->peergroupmode == 1 ? 'fullname' : 'groupname';
} else {
$workmode = $assessment->workmode = 'user';
$workmodeids = $users;
$totalnum = empty($users) ? 0 : count($users);
$tablecol_name = 'fullname';
}
/*************************************************/
$assessment_status = $this->get_assessment_status();
if (has_capability('mod/assessment:teachergrade', $context) || has_capability('mod/assessment:teacherview', $context)) {
// Filter store in session variable
$filter = optional_param('filter', 0, PARAM_TEXT);
if ($filter !== 0) {
// append assessment id so that each assessment have its own
$SESSION->mod_assessment_teacher_view_where = $filter."_".$assessment->id;
} else {
if (isset($SESSION->mod_assessment_teacher_view_where)) {
$session_temp = explode("_", $SESSION->mod_assessment_teacher_view_where);
$filter = $session_temp[0];
} else {
$filter = 'all';
}
}
$viewer = 'teacher';
if ($assessment->rubricid) {
echo $OUTPUT->heading(get_string('rubric', 'assessment'), 2, 'main teacherviewheading');
$this->rubric->view();
}
$tablecolumns = array();
$tableheaders = array();
$tablecolumns[] = $tablecol_name;
$tableheaders[] = get_string($tablecol_name, 'assessment');
$tablecolumns[] = 'class';
$tableheaders[] = get_string('class', 'assessment');
$tablecolumns[] = 'classno';
$tableheaders[] = get_string('classno', 'assessment');
if (isset($assessment->peer) && $assessment->peergroupmode == 1) {
$tablecolumns[] = 'groupname';
$tableheaders[] = get_string('groupname', 'assessment');
}
if (isset($assessment->teacher)) {
$tablecolumns[] = 'agt_grade';
$tableheaders[] = get_string('teacherassessment', 'assessment');
}
if (isset($assessment->self)) {
$tablecolumns[] = 'ags_grade';
$tableheaders[] = get_string('selfassessment', 'assessment');
}
if (isset($assessment->peer)) {
$tablecolumns[] = 'avgpeergrade';
$tableheaders[] = get_string('peerassessment', 'assessment');
}
$tablecolumns[] = 'finalgrade';
$tableheaders[] = get_string('finalgrade', 'assessment');
$tablecolumns[] = 'forum';
$tableheaders[] = get_string('forum', 'assessment');
require_once($CFG->libdir.'/tablelib.php');
$table = new flexible_table('mod-assessment-grades', 'List of submissions and assessments', $filter);
$table->define_columns($tablecolumns);
$table->define_headers($tableheaders);
$table->define_baseurl($CFG->wwwroot.'/mod/assessment/view.php?id='.$cm->id.'&currentgroup='.$currentgroup);
$table->sortable(true, 'lastname'); //sorted by lastname by default
$table->collapsible(false);
$table->initialbars(false);
$table->column_suppress($tablecol_name);
$table->column_class($tablecol_name, $tablecol_name);
if (isset($assessment->teacher)) $table->column_class('agt_grade', 'textaligncenter grade teacherassessment');
if (isset($assessment->self)) $table->column_class('ags_grade', 'textaligncenter grade selfassessment');
if (isset($assessment->peer)) $table->column_class('avgpeergrade', 'textaligncenter grade peerassessment');
$table->column_class('class', 'textaligncenter class');
$table->column_class('classno', 'textaligncenter classno');
$table->column_class('finalgrade', 'textaligncenter finalgrade');
$table->column_class('forum', 'textaligncenter forum');
$table->set_attribute('cellspacing', '0');
$table->set_attribute('id', 'attempts');
$table->set_attribute('class', 'assessment_grades');
$table->set_attribute('width', '100%');
$table->no_sorting('forum');
// Start working -- this is necessary as soon as the niceties are over
$table->setup();
if ($where = $table->get_sql_where()) {
if (is_array($where) && trim($where[0])== "")
$where = "";
else
$where .= " AND ";
}
$peersortsql = '';
if ($sort = $table->get_sql_sort()) {
$sort = " ORDER BY ".$sort;
// if sort by average grade of peer assessment, need to construct a sub-query to sort with
if (strpos($sort, 'avgpeergrade') !== FALSE) {
$peersortsql = "(SELECT AVG(grade) AS avgpeergrade
FROM {assessment_grades}
WHERE
type = 2 AND
assessmentid = (SELECT id FROM {assessment_types} WHERE type = 2 AND assessmentid = ".$assessment->id.") AND
userid = u.id
GROUP BY userid) AS avgpeergrade, ";
}
}
// Calculate final grade on the fly with SQL,
// to deal with sort by final grade, and replace old method of getting final grade
$finalgradesort = array();
$finalgradesortsql = '';
// COALESCE(): used to replace NULL with zero, so that it can be multiply
if (isset($assessment->teacher))
$finalgradesort[] = "COALESCE(agt.grade, 0)*".$assessment->teacherweight;
if (isset($assessment->self))
$finalgradesort[] = "COALESCE(ags.grade, 0)*".$assessment->selfweight;
if (isset($assessment->peer)) {
$finalgradesort[] = "COALESCE(
(SELECT AVG(grade) FROM {assessment_grades}
WHERE
type = 2 AND assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 2 AND assessmentid = ".$assessment->id.") AND
userid = u.id
GROUP BY userid)
,0)*".$assessment->peerweight;
}
if (sizeof($finalgradesort)) {
$finalgradesortsql = ", (" . implode('+', $finalgradesort) . ") AS finalgrade ";
}
if (strpos($filter, 'peer') === FALSE)
$where .= ' '.$this->get_sql_filter_teacher_view($filter, $assessment->id);
// Get Teacher and Self assessment first, return user name even there is no assessment
$more_names = "u.lastnamephonetic, u.firstnamephonetic, u.middlename, u.alternatename, ";
$select = "SELECT u.id, u.firstname, u.lastname, $more_names $peersortsql
(SELECT data FROM {user_info_data} WHERE userid = u.id AND fieldid = (SELECT id FROM {user_info_field} WHERE shortname = \"chiname\")) as chiname,
(SELECT data FROM {user_info_data} WHERE userid = u.id AND fieldid = (SELECT id FROM {user_info_field} WHERE shortname = \"class\")) as class,
(SELECT data FROM {user_info_data} WHERE userid = u.id AND fieldid = (SELECT id FROM {user_info_field} WHERE shortname = \"classno\")) as classno,
agt.id AS agt_id, agt.groupid AS agt_groupid,
agt.marker AS agt_marker, agt.grade AS agt_grade, agt.type AS agt_type,
agt.timemodified AS agt_timemodified, agt.comment AS agt_comment,
ags.id AS ags_id, ags.groupid AS ags_groupid,
ags.marker AS ags_marker, ags.grade AS ags_grade, ags.type AS ags_type,
ags.timemodified AS ags_timemodified, ags.comment AS ags_comment
".$finalgradesortsql;
$sql = "FROM {user} u
LEFT JOIN {assessment_grades} agt ON u.id = agt.userid
AND agt.type = 0 AND agt.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 0 AND assessmentid = ".$assessment->id.")
LEFT JOIN {assessment_grades} ags ON u.id = ags.userid
AND ags.type = 1 AND ags.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 1 AND assessmentid = ".$assessment->id.")";
if (trim($where) != '') $sql .= ' WHERE'.$where;
$table->pagesize($perpage, count($users));
///offset used to calculate index of student in that particular query, needed for the pop up to know who's next
$offset = $pageno * $perpage;
$strupdate = get_string('update');
$strgrade = get_string('grade');
$teachergradednum = 0;
$selfgradednum = 0;
$sql_filter = '';
$in_params = array();
if ($users) {
if (isset($assessment->teacher)) {
$query_params = array('assessmentid'=>$assessment->teacher, 'type'=>0);
list($in_sql, $in_params) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED);
$params = array_merge($in_params, $query_params);
$teachergradednum = $DB->count_records_sql("SELECT COUNT(*) FROM {assessment_grades} WHERE userid $in_sql AND type = :type AND assessmentid = :assessmentid", $params);
}
if (isset($assessment->self)) {
$query_params = array('assessmentid'=>$assessment->self, 'type'=>1);
list($in_sql, $in_params) = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED);
$params = array_merge($in_params, $query_params);
$selfgradednum = $DB->count_records_sql("SELECT COUNT(*) FROM {assessment_grades} WHERE userid $in_sql AND type = :type AND assessmentid = :assessmentid", $params);
}
$sql_filter = ' u.id IN ('.implode(',',$users).') ';
}
if (isset($assessment->peer)) {
// calculate the numbers for peer assessment summary
$peergradedallnum = 0;
$peergradedsomenum = 0;
$peergradednonenum = 0;
if ($users) {
foreach ($users as $singleuserid) {
$user_peer_marked = array();
$params = array();
$params['agp_type'] = 2;
$params['agp_marker'] = $singleuserid;
$params['agp_userid'] = $singleuserid;
$params['assessmentid'] = $assessment->id;
$peersql = "SELECT COUNT(*) AS peermarked
FROM {assessment_grades} agp
WHERE agp.type = :agp_type AND agp.marker = :agp_marker AND agp.userid <> :agp_userid AND agp.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 2 AND assessmentid = :assessmentid)";
$user_peer_marked = $DB->get_record_sql($peersql, $params);
$peer_info['marker'] = 'user';
$peer_info['done'] = $user_peer_marked->peermarked;
$peer_info['total'] = $assessment->peernum;
// counting the number for Summary display
if ($peer_info['done'] == $peer_info['total']) {
$peergradedallnum++;
} else if ($peer_info['done'] == 0) {
$peergradednonenum++;
} else {
$peergradedsomenum++;
}
}
}
if (strpos($filter, 'peer') !== FALSE) {
$peerfilterids = $this->get_peer_filter_teacher_view($filter, $assessment->id, $users, $this->assessment->peernum);
if (sizeof($peerfilterids) > 0) {
$sql_filter = " u.id IN (".implode(',',$peerfilterids).") ";
}
}
}
if (trim($where) != '') {
if (substr(trim($where), strlen(trim($where))-3) != 'AND')
$sql .= " AND";
$sql .= $sql_filter;
} else {
$sql .= empty($sql_filter) ? " " : " WHERE".$sql_filter;
}
/*************************************************/
// Work mode is group instead of individual
$markertype = 'user';
$markerids = $users;
$peergroupmode_para = '';
if ($workmode == 'group') {
if ($peersortsql != '') {
$peersortsql = "(SELECT AVG(grade) AS avgpeergrade
FROM {assessment_grades}
WHERE
type = 2 AND
assessmentid = (SELECT id FROM {assessment_types} WHERE type = 2 AND assessmentid = ".$assessment->id.") AND
groupid = g.id
GROUP BY groupid) AS avgpeergrade, ";
}
$finalgradesort = array();
$finalgradesortsql = '';
// COALESCE(): used to replace NULL with zero, so that it can be multiply
if (isset($assessment->teacher))
$finalgradesort[] = "COALESCE(agt.grade, 0)*".$assessment->teacherweight;
if (isset($assessment->self))
$finalgradesort[] = "COALESCE(ags.grade, 0)*".$assessment->selfweight;
if (isset($assessment->peer)) {
$finalgradesort[] = "COALESCE(
(SELECT AVG(grade) FROM {assessment_grades}
WHERE
type = 2 AND assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 2 AND assessmentid = ".$assessment->id.") AND
groupid = g.id
GROUP BY groupid)
,0)*".$assessment->peerweight;
}
if (sizeof($finalgradesort)) {
$finalgradesortsql = ", (" . implode('+', $finalgradesort) . ") AS finalgrade ";
}
if (isset($assessment->teacher)) {
$query_params = array('type'=>0, 'assessmentid'=>$assessment->teacher);
list($in_sql, $in_params) = $DB->get_in_or_equal($groupids, SQL_PARAMS_NAMED);
$params = array_merge($in_params, $query_params);
$teachergradednum = $DB->count_records_sql("SELECT COUNT(*) FROM {assessment_grades} WHERE groupid $in_sql AND type = :type AND assessmentid = :assessmentid", $params);
}
if (isset($assessment->self)) {
$query_params['type'] = 1;
$query_params['assessmentid'] = $assessment->self;
$params = array_merge($in_params, $query_params);
$selfgradednum = $DB->count_records_sql("SELECT COUNT(*) FROM {assessment_grades} WHERE groupid $in_sql AND type = :type AND assessmentid = :assessmentid", $params);
}
if (isset($assessment->peer)) {
// calculate the numbers for peer assessment summary
$peergradedallnum = 0;
$peergradedsomenum = 0;
$peergradednonenum = 0;
$sql_filter = '';
//// Individual assess group: need to display list of student instead of group
if ($assessment->peergroupmode == 1) {
$select = "SELECT u.id, u.firstname, u.lastname, $more_names $peersortsql
(SELECT data FROM {user_info_data} WHERE userid = u.id AND fieldid = (SELECT id FROM {user_info_field} WHERE shortname = \"chiname\")) as chiname,
(SELECT data FROM {user_info_data} WHERE userid = u.id AND fieldid = (SELECT id FROM {user_info_field} WHERE shortname = \"class\")) as class,
(SELECT data FROM {user_info_data} WHERE userid = u.id AND fieldid = (SELECT id FROM {user_info_field} WHERE shortname = \"classno\")) as classno,
g.id AS groupid, g.name AS groupname,
agt.id AS agt_id, agt.groupid AS agt_groupid,
agt.marker AS agt_marker, agt.grade AS agt_grade, agt.type AS agt_type,
agt.timemodified AS agt_timemodified, agt.comment AS agt_comment,
ags.id AS ags_id, ags.groupid AS ags_groupid,
ags.marker AS ags_marker, ags.grade AS ags_grade, ags.type AS ags_type,
ags.timemodified AS ags_timemodified, ags.comment AS ags_comment
".$finalgradesortsql;
$sql = "FROM {user} u
LEFT JOIN {groups} g ON g.id =
(SELECT groupid FROM {groups_members} WHERE userid = u.id AND groupid IN (".implode(',', $groupids)."))
LEFT JOIN {assessment_grades} agt ON g.id = agt.groupid
AND agt.type = 0 AND agt.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 0 AND assessmentid = ".$assessment->id.")
LEFT JOIN {assessment_grades} ags ON g.id = ags.groupid
AND ags.type = 1 AND ags.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 1 AND assessmentid = ".$assessment->id.")";
if (trim($where) != '') $sql .= ' WHERE'.$where;
$sql_filter = " u.id IN (".implode(',',$markerids).") ";
if (strpos($filter, 'peer') !== FALSE) {
$peerfilterids = $this->get_peer_filter_teacher_view($filter, $assessment->id, $markerids, $this->assessment->peernum);
if (sizeof($peerfilterids) > 0) {
$sql_filter = " u.id IN (".implode(',',$peerfilterids).") ";
}
}
$peergroupmode_para = '&peergroupmode=1';
//// Group assess group
} else if ($assessment->peergroupmode == 2) {
$select = "SELECT g.id, g.name AS groupname, ".$peersortsql."
agt.id AS agt_id, agt.groupid AS agt_groupid,
agt.marker AS agt_marker, agt.grade AS agt_grade, agt.type AS agt_type,
agt.timemodified AS agt_timemodified, agt.comment AS agt_comment,
ags.id AS ags_id, ags.groupid AS ags_groupid,
ags.marker AS ags_marker, ags.grade AS ags_grade, ags.type AS ags_type,
ags.timemodified AS ags_timemodified, ags.comment AS ags_comment
".$finalgradesortsql;
$sql = "FROM {groups} g
LEFT JOIN {assessment_grades} agt ON g.id = agt.groupid
AND agt.type = 0 AND agt.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 0 AND assessmentid = ".$assessment->id.")
LEFT JOIN {assessment_grades} ags ON g.id = ags.groupid
AND ags.type = 1 AND ags.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 1 AND assessmentid = ".$assessment->id.")";
if (trim($where) != '') $sql .= " WHERE".$where;
$markerids = $groupids;
$markertype = 'group';
$peergroupmode_para = '&peergroupmode=2';
$sql_filter = " g.id IN (".implode(',',$markerids).") ";
if (strpos($filter, 'peer') !== FALSE) {
$peerfilterids = $this->get_peer_filter_teacher_view($filter, $assessment->id, $markerids, $this->assessment->peernum);
if (sizeof($peerfilterids) > 0) {
$sql_filter = " g.id IN (".implode(',',$peerfilterids).") ";
}
}
$sort = str_replace(array('firstname','lastname'), 'groupname', $sort);
}
foreach ($markerids as $singlemarkerid) {
$group_peer_marked = array();
$peersql = "SELECT COUNT(*) AS peermarked
FROM {assessment_grades} agp
WHERE agp.type = 2 AND agp.marker = $singlemarkerid AND agp.".$markertype."id <> $singlemarkerid AND agp.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 2 AND assessmentid = $assessment->id)";
$group_peer_marked = $DB->get_record_sql($peersql);
$peer_info['marker'] = $markertype;
$peer_info['done'] = $group_peer_marked->peermarked;
$peer_info['total'] = $assessment->peernum;
// counting the number for Summary display
if ($peer_info['done'] == $peer_info['total']) {
$peergradedallnum++;
} else if ($peer_info['done'] == 0) {
$peergradednonenum++;
} else {
$peergradedsomenum++;
}
}
if (trim($where) != '') {
$sql .= $sql_filter;
} else {
$sql .= empty($sql_filter) ? " " : " WHERE".$sql_filter;
}
}
}
/*************************************************/
if (!$users) {
// No users avaialble who can submit their work, so no need to query any data at all
} else if (($ausers = $DB->get_records_sql($select.$sql.$sort, null, $table->get_page_start(), $table->get_page_size())) !== false) {
// Get forum discussion links for each student
if ($assessment->forum) {
$ausers_arr = array_keys($ausers);
if ($workmode == 'group' && $assessment->peergroupmode == 1) $ausers_arr = $groupids;
if ($assessment->forum > 1) {
$discussionids = $this->get_forum_discussions_old($ausers_arr);
$discussion_url = '/mod/forum/discuss.php?d=';
} else {
$discussionids = $this->get_forum_discussions($ausers_arr);
$discussion_url = '/mod/assessment/discuss.php?d=';
}
}
foreach ($ausers as $auser) {
$workmodeid = $auser->id;
if (isset($assessment->peer) && $assessment->peer) {
// only students belong to group can do this activity if it's groupwork
if ($assessment->peergroupmode==1) {
if (!$auser->groupid) continue;
$workmodeid = $auser->groupid;
$markertype = 'user';
}
// get peer assessment grade (user as marker)
$auser_peer_marked = array();
$sql = "SELECT
COUNT(*) AS peermarked
FROM {assessment_grades} agp
WHERE agp.type = 2 AND agp.marker = ".$auser->id." AND agp.".$workmode."id <> ".$workmodeid." AND agp.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 2 AND assessmentid = ".$assessment->id.")";
$auser_peer_marked = $DB->get_record_sql($sql);
// get peer assessment grade (marked by other peers)
$auser_peer_markedby = array();
$sql = "SELECT
COUNT(*) AS peermarkedby, AVG(grade) AS avggrade
FROM {assessment_grades} agp
WHERE agp.type = 2 AND agp.".$workmode."id = ".$workmodeid." AND agp.marker <> ".$auser->id." AND agp.assessmentid =
(SELECT id FROM {assessment_types} WHERE type = 2 AND assessmentid = ".$assessment->id.")
";
$auser_peer_markedby = $DB->get_record_sql($sql);
$peer_info = array('user'=>$auser->id, 'course'=>$course->id);
$peer_info['marker'] = 'user';
$peer_info['done'] = $auser_peer_marked->peermarked;
$peer_info['total'] = $this->assessment->peernum;
$peer_grade_marked = $this->display_grade('unknown', 0, false, $peer_info, NULL);
$popup_url = '/mod/assessment/assessment_grades.php?id='.$this->cm->id
.'&'.$workmode.'id='.$workmodeid. '&'.$markertype.'id='.$auser->id.$peergroupmode_para.'&mode=peersummary&type=2';
$peer_grade_marked_text = get_string('topeers', 'assessment').': ';
if ($auser_peer_marked->peermarked == 0) {
$peer_grade_marked = get_string('topeers', 'assessment').': '.$peer_grade_marked;
$peer_grade_marked_text .= get_string('notgraded', 'assessment').'">'.$peer_grade_marked.'</a>'.chr(13);
$peer_grade_marked_text = '<a href="javascript://" title="'.$peer_grade_marked_text;
} else {
$peer_grade_marked_text .= $auser_peer_marked->peermarked.'/'.$this->assessment->peernum.' '.get_string('graded', 'assessment');
$peer_grade_marked = $peer_grade_marked_text;
$peer_grade_marked_text = $OUTPUT->action_link($popup_url."&ismarker=1", $peer_grade_marked,
new popup_action('click', $popup_url."&ismarker=1", "grade".$auser->id, array('height' => 600, 'width' => $width)),
array('title'=>$peer_grade_marked_text));
}
$peer_grade_markedby_text = get_string('bypeers', 'assessment').': ';
$peer_info['marker'] = 'others';
$peer_info['done'] = $auser_peer_markedby->peermarkedby;
$peer_info['total'] = $this->assessment->peernum;
if ($auser_peer_markedby->peermarkedby == 0) {
$peer_grade_markedby = $this->display_grade('unknown', 0, false, $peer_info, 'scorecard');
$peer_grade_markedby = get_string('bypeers', 'assessment').': '.$peer_grade_markedby;
$peer_grade_markedby_text .= get_string('notgraded', 'assessment').'">'.$peer_grade_markedby.'</a>'.chr(13);
$peer_grade_markedby_text = '<a href="javascript://" title="'.$peer_grade_markedby_text;
} else {
$peer_grade_markedby = $this->display_grade(round($auser_peer_markedby->avggrade, 1), 0, false, $peer_info, 'scorecard');
$peer_grade_markedby_text .= $auser_peer_markedby->peermarkedby.'/'.$this->assessment->peernum.' '.get_string('graded', 'assessment');
$peer_grade_markedby_text .= ' ('.$this->display_grade(round($auser_peer_markedby->avggrade, 1)).')';
$peer_grade_markedby = $peer_grade_markedby_text;
$peer_grade_markedby_text = $OUTPUT->action_link($popup_url."&ismarker=0", $peer_grade_markedby,
new popup_action('click', $popup_url."&ismarker=0", "grade".$auser->id, array('height' => 600, 'width' => $width)),
array('title'=>$peer_grade_markedby_text));
}
$peer_grade = $peer_grade_marked_text;
$peer_grade .= '<br />';
$peer_grade .= $peer_grade_markedby_text;
}
$popup_url = '/mod/assessment/assessment_grades.php?id='.$this->cm->id
.'&'.$workmode.'id='.$workmodeid.'&mode=single'.'&offset='.$offset++;
if (isset($assessment->teacher)) {
$teacher_grade = chr(13);
$teacher_grade_text = '';
if ($auser->agt_id) {