-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
2029 lines (1726 loc) · 88.6 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Definition of the grader report class
*
* @package gradereport_gradest
* @copyright 2007 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/grade/report/lib.php');
require_once($CFG->libdir.'/tablelib.php');
/**
* Class providing an API for the grader report building and displaying.
* @uses grade_report
* @copyright 2007 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_report_gradest extends grade_report {
/**
* The final grades.
* @var array $grades
*/
public $grades;
/**
* Contains all the grades for the course - even the ones not displayed in the grade tree.
*
* @var array $allgrades
*/
private $allgrades;
/**
* Contains all grade items expect GRADE_TYPE_NONE.
*
* @var array $allgradeitems
*/
private $allgradeitems;
/**
* Array of errors for bulk grades updating.
* @var array $gradeserror
*/
public $gradeserror = array();
// SQL-RELATED
/**
* The id of the grade_item by which this report will be sorted.
* @var int $sortitemid
*/
public $sortitemid;
/**
* Sortorder used in the SQL selections.
* @var int $sortorder
*/
public $sortorder;
/**
* An SQL fragment affecting the search for users.
* @var string $userselect
*/
public $userselect;
/**
* The bound params for $userselect
* @var array $userselectparams
*/
public $userselectparams = array();
/**
* List of collapsed categories from user preference
* @var array $collapsed
*/
public $collapsed;
/**
* A count of the rows, used for css classes.
* @var int $rowcount
*/
public $rowcount = 0;
/**
* Capability check caching
* @var boolean $canviewhidden
*/
public $canviewhidden;
/**
* Length at which feedback will be truncated (to the nearest word) and an ellipsis be added.
* TODO replace this by a report preference
* @var int $feedback_trunc_length
*/
protected $feedback_trunc_length = 50;
/**
* Allow category grade overriding
* @var bool $overridecat
*/
protected $overridecat;
/**
* Constructor. Sets local copies of user preferences and initialises grade_tree.
* @param int $courseid
* @param object $gpr grade plugin return tracking object
* @param string $context
* @param int $page The current page being viewed (when report is paged)
* @param int $sortitemid The id of the grade_item by which to sort the table
*/
public function __construct($courseid, $gpr, $context, $page=null, $sortitemid=null) {
global $CFG;
parent::__construct($courseid, $gpr, $context, $page);
$this->canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($this->course->id));
// load collapsed settings for this report
$this->collapsed = static::get_collapsed_preferences($this->course->id);
if (empty($CFG->enableoutcomes)) {
$nooutcomes = false;
} else {
$nooutcomes = get_user_preferences('grade_report_shownooutcomes');
}
// if user report preference set or site report setting set use it, otherwise use course or site setting
$switch = $this->get_pref('aggregationposition');
if ($switch == '') {
$switch = grade_get_setting($this->courseid, 'aggregationposition', $CFG->grade_aggregationposition);
}
// Grab the grade_tree for this course
$this->gtree = new grade_tree($this->courseid, true, $switch, $this->collapsed, $nooutcomes);
$this->sortitemid = $sortitemid;
// base url for sorting by first/last name
$this->baseurl = new moodle_url('index.php', array('id' => $this->courseid));
$studentsperpage = $this->get_students_per_page();
if (!empty($this->page) && !empty($studentsperpage)) {
$this->baseurl->params(array('perpage' => $studentsperpage, 'page' => $this->page));
}
$this->pbarurl = new moodle_url('/grade/report/gradest/index.php', array('id' => $this->courseid));
$this->setup_groups();
$this->setup_users();
$this->setup_sortitemid();
$this->overridecat = (bool)get_config('moodle', 'grade_overridecat');
}
/**
* Processes the data sent by the form (grades and feedbacks).
* Caller is responsible for all access control checks
* @param array $data form submission (with magic quotes)
* @return array empty array if success, array of warnings if something fails.
*/
public function process_data($data) {
global $DB;
$warnings = array();
$separategroups = false;
$mygroups = array();
if ($this->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $this->context)) {
$separategroups = true;
$mygroups = groups_get_user_groups($this->course->id);
$mygroups = $mygroups[0]; // ignore groupings
// reorder the groups fro better perf below
$current = array_search($this->currentgroup, $mygroups);
if ($current !== false) {
unset($mygroups[$current]);
array_unshift($mygroups, $this->currentgroup);
}
}
$viewfullnames = has_capability('moodle/site:viewfullnames', $this->context);
// always initialize all arrays
$queue = array();
$this->load_users();
$this->load_final_grades();
// Were any changes made?
$changedgrades = false;
$timepageload = clean_param($data->timepageload, PARAM_INT);
foreach ($data as $varname => $students) {
$needsupdate = false;
// skip, not a grade nor feedback
if (strpos($varname, 'grade') === 0) {
$datatype = 'grade';
} else if (strpos($varname, 'feedback') === 0) {
$datatype = 'feedback';
} else {
continue;
}
foreach ($students as $userid => $items) {
$userid = clean_param($userid, PARAM_INT);
foreach ($items as $itemid => $postedvalue) {
$itemid = clean_param($itemid, PARAM_INT);
// Was change requested?
$oldvalue = $this->grades[$userid][$itemid];
if ($datatype === 'grade') {
// If there was no grade and there still isn't
if (is_null($oldvalue->finalgrade) && $postedvalue == -1) {
// -1 means no grade
continue;
}
// If the grade item uses a custom scale
if (!empty($oldvalue->grade_item->scaleid)) {
if ((int)$oldvalue->finalgrade === (int)$postedvalue) {
continue;
}
} else {
// The grade item uses a numeric scale
// Format the finalgrade from the DB so that it matches the grade from the client
if ($postedvalue === format_float($oldvalue->finalgrade, $oldvalue->grade_item->get_decimals())) {
continue;
}
}
$changedgrades = true;
} else if ($datatype === 'feedback') {
// If quick grading is on, feedback needs to be compared without line breaks.
if ($this->get_pref('quickgrading')) {
$oldvalue->feedback = preg_replace("/\r\n|\r|\n/", "", $oldvalue->feedback);
}
if (($oldvalue->feedback === $postedvalue) or ($oldvalue->feedback === null and empty($postedvalue))) {
continue;
}
}
if (!$gradeitem = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$this->courseid))) {
print_error('invalidgradeitemid');
}
// Pre-process grade
if ($datatype == 'grade') {
$feedback = false;
$feedbackformat = false;
if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
if ($postedvalue == -1) { // -1 means no grade
$finalgrade = null;
} else {
$finalgrade = $postedvalue;
}
} else {
$finalgrade = unformat_float($postedvalue);
}
$errorstr = '';
$skip = false;
$dategraded = $oldvalue->get_dategraded();
if (!empty($dategraded) && $timepageload < $dategraded) {
// Warn if the grade was updated while we were editing this form.
$errorstr = 'gradewasmodifiedduringediting';
$skip = true;
} else if (!is_null($finalgrade)) {
// Warn if the grade is out of bounds.
$bounded = $gradeitem->bounded_grade($finalgrade);
if ($bounded > $finalgrade) {
$errorstr = 'lessthanmin';
} else if ($bounded < $finalgrade) {
$errorstr = 'morethanmax';
}
}
if ($errorstr) {
$userfields = 'id, ' . get_all_user_name_fields(true);
$user = $DB->get_record('user', array('id' => $userid), $userfields);
$gradestr = new stdClass();
$gradestr->username = fullname($user, $viewfullnames);
$gradestr->itemname = $gradeitem->get_name();
$warnings[] = get_string($errorstr, 'grades', $gradestr);
if ($skip) {
// Skipping the update of this grade it failed the tests above.
continue;
}
}
} else if ($datatype == 'feedback') {
$finalgrade = false;
$trimmed = trim($postedvalue);
if (empty($trimmed)) {
$feedback = null;
} else {
$feedback = $postedvalue;
}
}
// group access control
if ($separategroups) {
// note: we can not use $this->currentgroup because it would fail badly
// when having two browser windows each with different group
$sharinggroup = false;
foreach ($mygroups as $groupid) {
if (groups_is_member($groupid, $userid)) {
$sharinggroup = true;
break;
}
}
if (!$sharinggroup) {
// either group membership changed or somebody is hacking grades of other group
$warnings[] = get_string('errorsavegrade', 'grades');
continue;
}
}
$gradeitem->update_final_grade($userid, $finalgrade, 'gradebook', $feedback, FORMAT_MOODLE);
// We can update feedback without reloading the grade item as it doesn't affect grade calculations
if ($datatype === 'feedback') {
$this->grades[$userid][$itemid]->feedback = $feedback;
}
}
}
}
if ($changedgrades) {
// If a final grade was overriden reload grades so dependent grades like course total will be correct
$this->grades = null;
}
return $warnings;
}
/**
* Setting the sort order, this depends on last state
* all this should be in the new table class that we might need to use
* for displaying grades.
*/
private function setup_sortitemid() {
global $SESSION;
if (!isset($SESSION->gradeuserreport)) {
$SESSION->gradeuserreport = new stdClass();
}
if ($this->sortitemid) {
if (!isset($SESSION->gradeuserreport->sort)) {
if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
$this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
} else {
$this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
}
} else {
// this is the first sort, i.e. by last name
if (!isset($SESSION->gradeuserreport->sortitemid)) {
if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
$this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
} else {
$this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
}
} else if ($SESSION->gradeuserreport->sortitemid == $this->sortitemid) {
// same as last sort
if ($SESSION->gradeuserreport->sort == 'ASC') {
$this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
} else {
$this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
}
} else {
if ($this->sortitemid == 'firstname' || $this->sortitemid == 'lastname') {
$this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
} else {
$this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
}
}
}
$SESSION->gradeuserreport->sortitemid = $this->sortitemid;
} else {
// not requesting sort, use last setting (for paging)
if (isset($SESSION->gradeuserreport->sortitemid)) {
$this->sortitemid = $SESSION->gradeuserreport->sortitemid;
} else {
$this->sortitemid = 'lastname';
}
if (isset($SESSION->gradeuserreport->sort)) {
$this->sortorder = $SESSION->gradeuserreport->sort;
} else {
$this->sortorder = 'ASC';
}
}
}
/**
* pulls out the userids of the users to be display, and sorts them
*/
public function load_users() {
global $CFG, $DB;
if (!empty($this->users)) {
return;
}
$this->setup_users();
// Limit to users with a gradeable role.
list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
// Check the status of showing only active enrolments.
$coursecontext = $this->context->get_course_context(true);
$defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
$showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
$showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
// Limit to users with an active enrollment.
list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context, '', 0, $showonlyactiveenrol);
// Fields we need from the user table.
$userfields = user_picture::fields('u', get_extra_user_fields($this->context));
// We want to query both the current context and parent contexts.
list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
// If the user has clicked one of the sort asc/desc arrows.
if (is_numeric($this->sortitemid)) {
$params = array_merge(array('gitemid' => $this->sortitemid), $gradebookrolesparams, $this->userwheresql_params,
$this->groupwheresql_params, $enrolledparams, $relatedctxparams);
$sortjoin = "LEFT JOIN {grade_grades} g ON g.userid = u.id AND g.itemid = $this->sortitemid";
$sort = "g.finalgrade $this->sortorder, u.idnumber, u.lastname, u.firstname, u.email";
} else {
$sortjoin = '';
switch($this->sortitemid) {
case 'lastname':
$sort = "u.lastname $this->sortorder, u.firstname $this->sortorder, u.idnumber, u.email";
break;
case 'firstname':
$sort = "u.firstname $this->sortorder, u.lastname $this->sortorder, u.idnumber, u.email";
break;
case 'email':
$sort = "u.email $this->sortorder, u.firstname, u.lastname, u.idnumber";
break;
case 'idnumber':
default:
$sort = "u.idnumber $this->sortorder, u.firstname, u.lastname, u.email";
break;
}
$params = array_merge($gradebookrolesparams, $this->userwheresql_params, $this->groupwheresql_params, $enrolledparams, $relatedctxparams);
}
$sql = "SELECT $userfields
FROM {user} u
JOIN ($enrolledsql) je ON je.id = u.id
$this->groupsql
$sortjoin
JOIN (
SELECT DISTINCT ra.userid
FROM {role_assignments} ra
WHERE ra.roleid IN ($this->gradebookroles)
AND ra.contextid $relatedctxsql
) rainner ON rainner.userid = u.id
AND u.deleted = 0
$this->userwheresql
$this->groupwheresql
ORDER BY $sort";
$studentsperpage = $this->get_students_per_page();
$this->users = $DB->get_records_sql($sql, $params, $studentsperpage * $this->page, $studentsperpage);
if (empty($this->users)) {
$this->userselect = '';
$this->users = array();
$this->userselect_params = array();
} else {
list($usql, $uparams) = $DB->get_in_or_equal(array_keys($this->users), SQL_PARAMS_NAMED, 'usid0');
$this->userselect = "AND g.userid $usql";
$this->userselect_params = $uparams;
// First flag everyone as not suspended.
foreach ($this->users as $user) {
$this->users[$user->id]->suspendedenrolment = false;
}
// If we want to mix both suspended and not suspended users, let's find out who is suspended.
if (!$showonlyactiveenrol) {
$sql = "SELECT ue.userid
FROM {user_enrolments} ue
JOIN {enrol} e ON e.id = ue.enrolid
WHERE ue.userid $usql
AND ue.status = :uestatus
AND e.status = :estatus
AND e.courseid = :courseid
AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)
GROUP BY ue.userid";
$time = time();
$params = array_merge($uparams, array('estatus' => ENROL_INSTANCE_ENABLED, 'uestatus' => ENROL_USER_ACTIVE,
'courseid' => $coursecontext->instanceid, 'now1' => $time, 'now2' => $time));
$useractiveenrolments = $DB->get_records_sql($sql, $params);
foreach ($this->users as $user) {
$this->users[$user->id]->suspendedenrolment = !array_key_exists($user->id, $useractiveenrolments);
}
}
}
return $this->users;
}
/**
* Load all grade items.
*/
protected function get_allgradeitems() {
if (!empty($this->allgradeitems)) {
return $this->allgradeitems;
}
$allgradeitems = grade_item::fetch_all(array('courseid' => $this->courseid));
// But hang on - don't include ones which are set to not show the grade at all.
$this->allgradeitems = array_filter($allgradeitems, function($item) {
return $item->gradetype != GRADE_TYPE_NONE;
});
return $this->allgradeitems;
}
/**
* we supply the userids in this query, and get all the grades
* pulls out all the grades, this does not need to worry about paging
*/
public function load_final_grades() {
global $CFG, $DB;
if (!empty($this->grades)) {
return;
}
if (empty($this->users)) {
return;
}
// please note that we must fetch all grade_grades fields if we want to construct grade_grade object from it!
$params = array_merge(array('courseid'=>$this->courseid), $this->userselect_params);
$sql = "SELECT g.*
FROM {grade_items} gi,
{grade_grades} g
WHERE g.itemid = gi.id AND gi.courseid = :courseid {$this->userselect}";
$userids = array_keys($this->users);
$allgradeitems = $this->get_allgradeitems();
if ($grades = $DB->get_records_sql($sql, $params)) {
foreach ($grades as $graderec) {
$grade = new grade_grade($graderec, false);
if (!empty($allgradeitems[$graderec->itemid])) {
// Note: Filter out grades which have a grade type of GRADE_TYPE_NONE.
// Only grades without this type are present in $allgradeitems.
$this->allgrades[$graderec->userid][$graderec->itemid] = $grade;
}
if (in_array($graderec->userid, $userids) and array_key_exists($graderec->itemid, $this->gtree->get_items())) { // some items may not be present!!
$this->grades[$graderec->userid][$graderec->itemid] = $grade;
$this->grades[$graderec->userid][$graderec->itemid]->grade_item = $this->gtree->get_item($graderec->itemid); // db caching
}
}
}
// prefil grades that do not exist yet
foreach ($userids as $userid) {
foreach ($this->gtree->get_items() as $itemid => $unused) {
if (!isset($this->grades[$userid][$itemid])) {
$this->grades[$userid][$itemid] = new grade_grade();
$this->grades[$userid][$itemid]->itemid = $itemid;
$this->grades[$userid][$itemid]->userid = $userid;
$this->grades[$userid][$itemid]->grade_item = $this->gtree->get_item($itemid); // db caching
$this->allgrades[$userid][$itemid] = $this->grades[$userid][$itemid];
}
}
}
// Pre fill grades for any remaining items which might be collapsed.
foreach ($userids as $userid) {
foreach ($allgradeitems as $itemid => $gradeitem) {
if (!isset($this->allgrades[$userid][$itemid])) {
$this->allgrades[$userid][$itemid] = new grade_grade();
$this->allgrades[$userid][$itemid]->itemid = $itemid;
$this->allgrades[$userid][$itemid]->userid = $userid;
$this->allgrades[$userid][$itemid]->grade_item = $gradeitem;
}
}
}
}
/**
* Gets html toggle
* @deprecated since Moodle 2.4 as it appears not to be used any more.
*/
public function get_toggles_html() {
throw new coding_exception('get_toggles_html() can not be used any more');
}
/**
* Prints html toggle
* @deprecated since 2.4 as it appears not to be used any more.
* @param unknown $type
*/
public function print_toggle($type) {
throw new coding_exception('print_toggle() can not be used any more');
}
/**
* Builds and returns the rows that will make up the left part of the grader report
* This consists of student names and icons, links to user reports and id numbers, as well
* as header cells for these columns. It also includes the fillers required for the
* categories displayed on the right side of the report.
* @param boolean $displayaverages whether to display average rows in the table
* @return array Array of html_table_row objects
*/
public function get_left_rows($displayaverages) {
global $CFG, $USER, $OUTPUT;
$rows = array();
$showuserimage = $this->get_pref('showuserimage');
// FIXME: MDL-52678 This get_capability_info is hacky and we should have an API for inserting grade row links instead.
$canseeuserreport = false;
$canseesingleview = false;
if (get_capability_info('gradereport/'.$CFG->grade_profilereport.':view')) {
$canseeuserreport = has_capability('gradereport/'.$CFG->grade_profilereport.':view', $this->context);
}
if (get_capability_info('gradereport/singleview:view')) {
$canseesingleview = has_all_capabilities(array('gradereport/singleview:view', 'moodle/grade:viewall',
'moodle/grade:edit'), $this->context);
}
$hasuserreportcell = $canseeuserreport || $canseesingleview;
$viewfullnames = has_capability('moodle/site:viewfullnames', $this->context);
$strfeedback = $this->get_lang_string("feedback");
$strgrade = $this->get_lang_string('grade');
$extrafields = get_extra_user_fields($this->context);
$arrows = $this->get_sort_arrows($extrafields);
$colspan = 1 + $hasuserreportcell + count($extrafields);
$levels = count($this->gtree->levels) - 1;
$fillercell = new html_table_cell();
$fillercell->header = true;
$fillercell->attributes['scope'] = 'col';
$fillercell->attributes['class'] = 'cell topleft';
$fillercell->text = html_writer::span(get_string('participants'), 'accesshide');
$fillercell->colspan = $colspan;
$fillercell->rowspan = $levels;
$row = new html_table_row(array($fillercell));
$rows[] = $row;
for ($i = 1; $i < $levels; $i++) {
$row = new html_table_row();
$rows[] = $row;
}
$headerrow = new html_table_row();
$headerrow->attributes['class'] = 'heading';
$studentheader = new html_table_cell();
$studentheader->attributes['class'] = 'header';
$studentheader->scope = 'col';
$studentheader->header = true;
$studentheader->id = 'studentheader';
if ($hasuserreportcell) {
$studentheader->colspan = 2;
}
$studentheader->text = $arrows['studentname'];
$headerrow->cells[] = $studentheader;
foreach ($extrafields as $field) {
$fieldheader = new html_table_cell();
$fieldheader->attributes['class'] = 'header userfield user' . $field;
$fieldheader->scope = 'col';
$fieldheader->header = true;
$fieldheader->text = $arrows[$field];
$headerrow->cells[] = $fieldheader;
}
$rows[] = $headerrow;
$rows = $this->get_left_icons_row($rows, $colspan);
$suspendedstring = null;
foreach ($this->users as $userid => $user) {
$userrow = new html_table_row();
$userrow->id = 'fixed_user_'.$userid;
$usercell = new html_table_cell();
$usercell->attributes['class'] = 'header user';
$usercell->header = true;
$usercell->scope = 'row';
if ($showuserimage) {
$usercell->text = $OUTPUT->user_picture($user, array('visibletoscreenreaders' => false));
}
$fullname = fullname($user, $viewfullnames);
$usercell->text .= html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $this->course->id)), $fullname, array(
'class' => 'username',
));
if (!empty($user->suspendedenrolment)) {
$usercell->attributes['class'] .= ' usersuspended';
//may be lots of suspended users so only get the string once
if (empty($suspendedstring)) {
$suspendedstring = get_string('userenrolmentsuspended', 'grades');
}
$icon = $OUTPUT->pix_icon('i/enrolmentsuspended', $suspendedstring);
$usercell->text .= html_writer::tag('span', $icon, array('class'=>'usersuspendedicon'));
}
$userrow->cells[] = $usercell;
$userreportcell = new html_table_cell();
$userreportcell->attributes['class'] = 'userreport';
$userreportcell->header = false;
if ($canseeuserreport) {
$a = new stdClass();
$a->user = $fullname;
$strgradesforuser = get_string('gradesforuser', 'grades', $a);
$url = new moodle_url('/grade/report/'.$CFG->grade_profilereport.'/index.php', array('userid' => $user->id, 'id' => $this->course->id));
$userreportcell->text .= $OUTPUT->action_icon($url, new pix_icon('t/grades', $strgradesforuser));
}
if ($canseesingleview) {
$url = new moodle_url('/grade/report/singleview/index.php', array('id' => $this->course->id, 'itemid' => $user->id, 'item' => 'user'));
$singleview = $OUTPUT->action_icon($url, new pix_icon('t/editstring', get_string('singleview', 'grades', $fullname)));
$userreportcell->text .= $singleview;
}
if ($userreportcell->text) {
$userrow->cells[] = $userreportcell;
}
foreach ($extrafields as $field) {
$fieldcell = new html_table_cell();
$fieldcell->attributes['class'] = 'userfield user' . $field;
$fieldcell->header = false;
$fieldcell->text = $user->{$field};
$userrow->cells[] = $fieldcell;
}
$userrow->attributes['data-uid'] = $userid;
$rows[] = $userrow;
}
$rows = $this->get_left_range_row($rows, $colspan);
if ($displayaverages) {
$rows = $this->get_left_avg_row($rows, $colspan, true);
$rows = $this->get_left_avg_row($rows, $colspan);
}
return $rows;
}
/**
* Builds and returns the rows that will make up the right part of the grader report
* @param boolean $displayaverages whether to display average rows in the table
* @return array Array of html_table_row objects
*/
public function get_right_rows($displayaverages) {
global $CFG, $USER, $OUTPUT, $DB, $PAGE;
$rows = array();
$this->rowcount = 0;
$numrows = count($this->gtree->get_levels());
$numusers = count($this->users);
$gradetabindex = 1;
$columnstounset = array();
$strgrade = $this->get_lang_string('grade');
$strfeedback = $this->get_lang_string("feedback");
$arrows = $this->get_sort_arrows();
$jsarguments = array(
'cfg' => array('ajaxenabled'=>false),
'items' => array(),
'users' => array(),
'feedback' => array(),
'grades' => array()
);
$jsscales = array();
// Get preferences once.
$showactivityicons = $this->get_pref('showactivityicons');
$quickgrading = $this->get_pref('quickgrading');
$showquickfeedback = $this->get_pref('showquickfeedback');
$enableajax = $this->get_pref('enableajax');
$showanalysisicon = $this->get_pref('showanalysisicon');
// Get strings which are re-used inside the loop.
$strftimedatetimeshort = get_string('strftimedatetimeshort');
$strexcludedgrades = get_string('excluded', 'grades');
$strerror = get_string('error');
$viewfullnames = has_capability('moodle/site:viewfullnames', $this->context);
$name = '';
foreach ($this->gtree->get_levels() as $key => $row) {
$headingrow = new html_table_row();
$headingrow->attributes['class'] = 'heading_name_row';
foreach ($row as $columnkey => $element) {
$sortlink = clone($this->baseurl);
if (isset($element['object']->id)) {
$sortlink->param('sortitemid', $element['object']->id);
}
$eid = $element['eid'];
$object = $element['object'];
$type = $element['type'];
$categorystate = @$element['categorystate'];
if (!empty($element['colspan'])) {
$colspan = $element['colspan'];
} else {
$colspan = 1;
}
if (!empty($element['depth'])) {
$catlevel = 'catlevel'.$element['depth'];
if(!empty($element['children']) && !empty($element['categories'])) {
}
} else {
$catlevel = '';
}
// Element is a filler
if ($type == 'filler' or $type == 'fillerfirst' or $type == 'fillerlast') {
$fillercell = new html_table_cell();
$fillercell->attributes['class'] = $type . ' ' . $catlevel;
$fillercell->colspan = $colspan;
$fillercell->text = ' ';
// This is a filler cell; don't use a <th>, it'll confuse screen readers.
$fillercell->header = false;
$headingrow->cells[] = $fillercell;
} else if ($type == 'category') {
// Make sure the grade category has a grade total or at least has child grade items.
if (grade_tree::can_output_item($element)) {
// Element is a category.
$categorycell = new html_table_cell();
$name = clean_param($element['object']->get_name(), PARAM_ALPHANUMEXT);
$courseheaderid = 'courseheader_' . $name;
$categorycell->attributes['class'] = 'category ' . $catlevel . ' ' . $name;
$categorycell->colspan = $colspan;
$categorycell->text = $this->get_course_header($element);
$categorycell->header = true;
$categorycell->scope = 'col';
// Print icons.
if ($USER->gradeediting[$this->courseid]) {
$categorycell->text .= $this->get_icons($element);
}
$headingrow->cells[] = $categorycell;
}
} else {
// Element is a grade_item
if ($element['object']->id == $this->sortitemid) {
if ($this->sortorder == 'ASC') {
$arrow = $this->get_sort_arrow('up', $sortlink);
} else {
$arrow = $this->get_sort_arrow('down', $sortlink);
}
} else {
$arrow = $this->get_sort_arrow('move', $sortlink);
}
$headerlink = $this->gtree->get_element_header($element, true, false, false, false, true);
$headericon = $this->gtree->get_element_header($element, false, true, false, false, true);
$itemcell = new html_table_cell();
$itemcell->attributes['class'] = $type . ' ' . $catlevel . ' highlightable'. ' i'. $element['object']->id . ' '. $name;
$itemcell->attributes['data-itemid'] = $element['object']->id;
if ($element['object']->is_hidden()) {
$itemcell->attributes['class'] .= ' dimmed_text';
}
$singleview = '';
// FIXME: MDL-52678 This is extremely hacky we should have an API for inserting grade column links.
if (get_capability_info('gradereport/singleview:view')) {
if (has_all_capabilities(array('gradereport/singleview:view', 'moodle/grade:viewall',
'moodle/grade:edit'), $this->context)) {
$url = new moodle_url('/grade/report/singleview/index.php', array(
'id' => $this->course->id,
'item' => 'grade',
'itemid' => $element['object']->id));
$singleview = $OUTPUT->action_icon(
$url,
new pix_icon('t/editstring', get_string('singleview', 'grades', $element['object']->get_name()))
);
}
}
$itemcell->colspan = $colspan;
//get type icon only
$headericon = substr($headericon, strpos($headericon, '<img'), 1000);
$headericon = substr($headericon, 0, strpos($headericon, '>') + 1);
/*
$headericonsrc = substr($headericon, strpos($headericon, 'src="') + 5, 1000);
$headericonsrc = substr($headericonsrc, 0, strpos($headericonsrc, '"'));
$itemcell->text = '<div style="width:80px; height:60px; position: absolute; margin-left: 15px; opacity:0.3;
background-image:url(' . $headericonsrc . ');
background-repeat: no-repeat; background-size: contain;"></div>
';
*/
$itemcell->text .= $headericon . '<br><b>' . $headerlink . '</b>';
/*
$itemcell->text .= '<div class="d-flex"><div style="width:32px;">' . $headericon . '</div><div style="width:60px;">' . $arrow . $singleview;
$itemcell->text .= '<a href="#empty"><i class="icon fa fa-gear" title="Edit" aria-label="Sort in descending order"></i></a>';
$itemcell->text .= '<a href="#empty"><i class="icon fa fa-eye fa-fw sorticon" title="Hide grade" aria-label="Sort in descending order"></i></a>';
$itemcell->text .= '</div></div>';
*/
$itemcell->text .= '<br><a href="#" tabindex="0" class=" dropdown-toggle icon-no-margin" id="dropdown-2" aria-label="Edit" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true" aria-controls="action-menu-2-menu">
Edit
<b class="caret"></b>
</a>';
//$itemcell->text = $headericonsrc;
$itemcell->header = true;
$itemcell->scope = 'col';
$headingrow->cells[] = $itemcell;
}
}
$rows[] = $headingrow;
}
$rows = $this->get_right_icons_row($rows);
// Preload scale objects for items with a scaleid and initialize tab indices
$scaleslist = array();
$tabindices = array();
foreach ($this->gtree->get_items() as $itemid => $item) {
$scale = null;
if (!empty($item->scaleid)) {
$scaleslist[] = $item->scaleid;
$jsarguments['items'][$itemid] = array('id'=>$itemid, 'name'=>$item->get_name(true), 'type'=>'scale', 'scale'=>$item->scaleid, 'decimals'=>$item->get_decimals());
} else {
$jsarguments['items'][$itemid] = array('id'=>$itemid, 'name'=>$item->get_name(true), 'type'=>'value', 'scale'=>false, 'decimals'=>$item->get_decimals());
}
$tabindices[$item->id]['grade'] = $gradetabindex;
$tabindices[$item->id]['feedback'] = $gradetabindex + $numusers;
$gradetabindex += $numusers * 2;
}
$scalesarray = array();
if (!empty($scaleslist)) {
$scalesarray = $DB->get_records_list('scale', 'id', $scaleslist);
}
$jsscales = $scalesarray;
// Get all the grade items if the user can not view hidden grade items.
// It is possible that the user is simply viewing the 'Course total' by switching to the 'Aggregates only' view
// and that this user does not have the ability to view hidden items. In this case we still need to pass all the
// grade items (in case one has been hidden) as the course total shown needs to be adjusted for this particular
// user.
if (!$this->canviewhidden) {
$allgradeitems = $this->get_allgradeitems();
}
foreach ($this->users as $userid => $user) {