mirrored from git://git.moodle.org/moodle.git
/
lib.php
936 lines (813 loc) · 37.8 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
<?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/>.
/**
* File containing the grade_report class
*
* @package core_grades
* @copyright 2007 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_user\fields;
require_once($CFG->libdir.'/gradelib.php');
/**
* An abstract class containing variables and methods used by all or most reports.
* @copyright 2007 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class grade_report {
/**
* The courseid.
* @var int $courseid
*/
public $courseid;
/**
* The course.
* @var object $course
*/
public $course;
/** Grade plugin return tracking object.
* @var object $gpr
*/
public $gpr;
/**
* The context.
*
* @var context $context
*/
public $context;
/**
* The grade_tree object.
* @var grade_tree $gtree
*/
public $gtree;
/**
* User preferences related to this report.
* @var array $prefs
*/
public $prefs = array();
/**
* The roles for this report.
* @var string $gradebookroles
*/
public $gradebookroles;
/**
* base url for sorting by first/last name.
* @var string $baseurl
*/
public $baseurl;
/**
* base url for paging.
* @var string $pbarurl
*/
public $pbarurl;
/**
* Current page (for paging).
* @var int $page
*/
public $page;
// GROUP VARIABLES (including SQL)
/**
* The current group being displayed.
* @var int $currentgroup
*/
public $currentgroup;
/**
* The current groupname being displayed.
* @var string $currentgroupname
*/
public $currentgroupname;
/**
* Current course group mode
* @var int $groupmode
*/
public $groupmode;
/**
* A HTML select element used to select the current group.
* @var string $group_selector
*/
public $group_selector;
/**
* An SQL fragment used to add linking information to the group tables.
* @var string $groupsql
*/
protected $groupsql;
/**
* An SQL constraint to append to the queries used by this object to build the report.
* @var string $groupwheresql
*/
protected $groupwheresql;
/**
* The ordered params for $groupwheresql
* @var array $groupwheresql_params
*/
protected $groupwheresql_params = array();
// USER VARIABLES (including SQL).
/**
* An SQL constraint to append to the queries used by this object to build the report.
* @var string $userwheresql
*/
protected $userwheresql;
/**
* The ordered params for $userwheresql
* @var array $userwheresql_params
*/
protected $userwheresql_params = array();
/**
* To store user data
* @var stdClass $user
*/
public $user;
/**
* show course/category totals if they contain hidden items
* @var array $showtotalsifcontainhidden
*/
public $showtotalsifcontainhidden = [];
/**
* To store a link to preferences page
* @var string $preferences_page
*/
protected $preferences_page;
/**
* If the user is wanting to search for a particular user within searchable fields their needle will be placed here.
* @var string $usersearch
*/
protected string $usersearch = '';
/**
* If the user is wanting to show only one particular user their id will be placed here.
* @var int $userid
*/
protected int $userid = -1;
/**
* 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)
*/
public function __construct($courseid, $gpr, $context, $page=null) {
global $CFG, $COURSE, $DB;
if (empty($CFG->gradebookroles)) {
throw new \moodle_exception('norolesdefined', 'grades');
}
$this->courseid = $courseid;
if ($this->courseid == $COURSE->id) {
$this->course = $COURSE;
} else {
$this->course = $DB->get_record('course', array('id' => $this->courseid));
}
$this->gpr = $gpr;
$this->context = $context;
$this->page = $page;
// roles to be displayed in the gradebook
$this->gradebookroles = $CFG->gradebookroles;
// Set up link to preferences page
$this->preferences_page = $CFG->wwwroot.'/grade/report/grader/preferences.php?id='.$courseid;
// init gtree in child class
// Set any url params.
$this->usersearch = optional_param('searchvalue', '', PARAM_NOTAGS);
$this->userid = optional_param('userid', -1, PARAM_INT);
}
/**
* Given the name of a user preference (without grade_report_ prefix), locally saves then returns
* the value of that preference. If the preference has already been fetched before,
* the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
* is given (site default).
* Can be called statically, but then doesn't benefit from caching
* @param string $pref The name of the preference (do not include the grade_report_ prefix)
* @param int $objectid An optional itemid or categoryid to check for a more fine-grained preference
* @return mixed The value of the preference
*/
public function get_pref($pref, $objectid=null) {
global $CFG;
$fullprefname = 'grade_report_' . $pref;
$shortprefname = 'grade_' . $pref;
$retval = null;
if (!isset($this) OR get_class($this) != 'grade_report') {
if (!empty($objectid)) {
$retval = get_user_preferences($fullprefname . $objectid, self::get_pref($pref));
} else if (isset($CFG->$fullprefname)) {
$retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
} else if (isset($CFG->$shortprefname)) {
$retval = get_user_preferences($fullprefname, $CFG->$shortprefname);
} else {
$retval = null;
}
} else {
if (empty($this->prefs[$pref.$objectid])) {
if (!empty($objectid)) {
$retval = get_user_preferences($fullprefname . $objectid);
if (empty($retval)) {
// No item pref found, we are returning the global preference
$retval = $this->get_pref($pref);
$objectid = null;
}
} else {
$retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
}
$this->prefs[$pref.$objectid] = $retval;
} else {
$retval = $this->prefs[$pref.$objectid];
}
}
return $retval;
}
/**
* Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value,
* the preference will be removed in favour of a higher-level preference.
* @param string $pref The name of the preference.
* @param mixed $pref_value The value of the preference.
* @param int $itemid An optional itemid to which the preference will be assigned
* @return bool Success or failure.
*/
public function set_pref($pref, $pref_value='default', $itemid=null) {
$fullprefname = 'grade_report_' . $pref;
if ($pref_value == 'default') {
return unset_user_preference($fullprefname.$itemid);
} else {
return set_user_preference($fullprefname.$itemid, $pref_value);
}
}
/**
* Handles form data sent by this report for this report. Abstract method to implement in all children.
* @abstract
* @param array $data
* @return mixed True or array of errors
*/
abstract public function process_data($data);
/**
* Processes a single action against a category, grade_item or grade.
* @param string $target Sortorder
* @param string $action Which action to take (edit, delete etc...)
* @return
*/
abstract public function process_action($target, $action);
/**
* Add additional links specific to plugin
* @param context_course $context Course context
* @param int $courseid Course ID
* @param array $element An array representing an element in the grade_tree
* @param grade_plugin_return $gpr A grade_plugin_return object
* @param string $mode Mode (user or grade item)
* @param stdClass $templatecontext Template context
* @param bool $otherplugins If we need to insert links to other plugins
* @return ?stdClass Updated template context
*/
public static function get_additional_context(context_course $context, int $courseid, array $element,
grade_plugin_return $gpr, string $mode, stdClass $templatecontext, bool $otherplugins = false): ?stdClass {
if (!$otherplugins) {
$component = 'gradereport_' . $gpr->plugin;
$params = [$context, $courseid, $element, $gpr, $mode, $templatecontext];
return component_callback($component, 'get_report_link', $params);
} else {
// Loop through all installed grade reports.
foreach (core_component::get_plugin_list('gradereport') as $plugin => $plugindir) {
$params = [$context, $courseid, $element, $gpr, $mode, $templatecontext];
$component = 'gradereport_' . $plugin;
$templatecontextupdated = component_callback($component, 'get_report_link', $params);
if ($templatecontextupdated) {
$templatecontext = $templatecontextupdated;
}
}
return $templatecontext;
}
}
/**
* First checks the cached language strings, then returns match if found, or uses get_string()
* to get it from the DB, caches it then returns it.
*
* @deprecated since 4.2
* @todo MDL-77307 This will be deleted in Moodle 4.6.
* @param string $strcode
* @param string $section Optional language section
* @return string
*/
public function get_lang_string($strcode, $section=null) {
debugging('grade_report::get_lang_string() is deprecated, please use' .
' grade_helper::get_lang_string() instead.', DEBUG_DEVELOPER);
if (empty($this->lang_strings[$strcode])) {
$this->lang_strings[$strcode] = get_string($strcode, $section);
}
return $this->lang_strings[$strcode];
}
/**
* Fetches and returns a count of all the users that will be shown on this page.
* @param boolean $groups include groups limit
* @param boolean $users include users limit - default false, used for searching purposes
* @return int Count of users
*/
public function get_numusers($groups = true, $users = false) {
global $CFG, $DB;
$userwheresql = "";
$groupsql = "";
$groupwheresql = "";
// Limit to users with a gradeable role.
list($gradebookrolessql, $gradebookrolesparams) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
// Limit to users with an active enrollment.
list($enrolledsql, $enrolledparams) = get_enrolled_sql($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');
$params = array_merge($gradebookrolesparams, $enrolledparams, $relatedctxparams);
if ($users) {
$userwheresql = $this->userwheresql;
$params = array_merge($params, $this->userwheresql_params);
}
if ($groups) {
$groupsql = $this->groupsql;
$groupwheresql = $this->groupwheresql;
$params = array_merge($params, $this->groupwheresql_params);
}
$sql = "SELECT DISTINCT u.id
FROM {user} u
JOIN ($enrolledsql) je
ON je.id = u.id
JOIN {role_assignments} ra
ON u.id = ra.userid
$groupsql
WHERE ra.roleid $gradebookrolessql
AND u.deleted = 0
$userwheresql
$groupwheresql
AND ra.contextid $relatedctxsql";
$selectedusers = $DB->get_records_sql($sql, $params);
$count = 0;
// Check if user's enrolment is active and should be displayed.
if (!empty($selectedusers)) {
$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);
if ($showonlyactiveenrol) {
$useractiveenrolments = get_enrolled_users($coursecontext, '', 0, 'u.id', null, 0, 0, true);
}
foreach ($selectedusers as $id => $value) {
if (!$showonlyactiveenrol || ($showonlyactiveenrol && array_key_exists($id, $useractiveenrolments))) {
$count++;
}
}
}
return $count;
}
/**
* Shows support for being used as a 'Grades' report.
*/
public static function supports_mygrades() {
return false;
}
/**
* Sets up this object's group variables, mainly to restrict the selection of users to display.
*/
protected function setup_groups() {
// find out current groups mode
if ($this->groupmode = groups_get_course_groupmode($this->course)) {
if (empty($this->gpr->groupid)) {
$this->currentgroup = groups_get_course_group($this->course, true);
} else {
$this->currentgroup = $this->gpr->groupid;
}
$this->group_selector = groups_print_course_menu($this->course, $this->pbarurl, true);
if ($this->groupmode == SEPARATEGROUPS and !$this->currentgroup and !has_capability('moodle/site:accessallgroups', $this->context)) {
$this->currentgroup = -2; // means can not access any groups at all
}
if ($this->currentgroup) {
if ($group = groups_get_group($this->currentgroup)) {
$this->currentgroupname = $group->name;
}
$this->groupsql = " JOIN {groups_members} gm ON gm.userid = u.id ";
$this->groupwheresql = " AND gm.groupid = :gr_grpid ";
$this->groupwheresql_params = array('gr_grpid'=>$this->currentgroup);
}
}
}
/**
* Sets up this report's user criteria to restrict the selection of users to display.
*/
public function setup_users() {
global $SESSION, $DB;
$filterfirstnamekey = "filterfirstname-{$this->context->id}";
$filtersurnamekey = "filtersurname-{$this->context->id}";
$this->userwheresql = "";
$this->userwheresql_params = array();
if (!empty($SESSION->gradereport[$filterfirstnamekey])) {
$this->userwheresql .= ' AND '.$DB->sql_like('u.firstname', ':firstname', false, false);
$this->userwheresql_params['firstname'] = $SESSION->gradereport[$filterfirstnamekey] . '%';
}
if (!empty($SESSION->gradereport[$filtersurnamekey])) {
$this->userwheresql .= ' AND '.$DB->sql_like('u.lastname', ':lastname', false, false);
$this->userwheresql_params['lastname'] = $SESSION->gradereport[$filtersurnamekey] . '%';
}
// When a user wants to view a particular user rather than a set of users.
// By omission when selecting one user, also allow passing the search value around.
if ($this->userid !== -1) {
$this->userwheresql .= ' AND '.$DB->sql_equal('u.id', ':uid');
$this->userwheresql_params['uid'] = $this->userid;
}
// A user wants to return a subset of learners that match their search criteria.
if ($this->usersearch !== '' && $this->userid === -1) {
// Get the fields for all contexts because there is a special case later where it allows
// matches of fields you can't access if they are on your own account.
$userfields = fields::for_identity(null)->with_userpic();
['mappings' => $mappings] = (array)$userfields->get_sql('u', true);
[
'where' => $keywordswhere,
'params' => $keywordsparams,
] = $this->get_users_search_sql($mappings, (array)$userfields);
$this->userwheresql = " AND $keywordswhere";
$this->userwheresql_params = array_merge($this->userwheresql_params, $keywordsparams);
}
}
/**
* Prepare SQL where clause and associated parameters for any user searching being performed.
* This mostly came from core_user\table\participants_search with some slight modifications four our use case.
*
* @param array $mappings Array of field mappings (fieldname => SQL code for the value)
* @param array $userfields An array that we cast from user profile fields to search within.
* @return array SQL query data in the format ['where' => '', 'params' => []].
*/
protected function get_users_search_sql(array $mappings, array $userfields): array {
global $DB, $USER;
$canviewfullnames = has_capability('moodle/site:viewfullnames', $this->context);
$params = [];
$searchkey1 = 'search01';
$searchkey2 = 'search02';
$searchkey3 = 'search03';
$conditions = [];
// Search by fullname.
[$fullname, $fullnameparams] = fields::get_sql_fullname('u', $canviewfullnames);
$conditions[] = $DB->sql_like($fullname, ':' . $searchkey1, false, false);
$params = array_merge($params, $fullnameparams);
// Search by email.
$email = $DB->sql_like('email', ':' . $searchkey2, false, false);
if (!in_array('email', $userfields)) {
$maildisplay = 'maildisplay0';
$userid1 = 'userid01';
// Prevent users who hide their email address from being found by others
// who aren't allowed to see hidden email addresses.
$email = "(". $email ." AND (" .
"u.maildisplay <> :$maildisplay " .
"OR u.id = :$userid1". // Users can always find themselves.
"))";
$params[$maildisplay] = core_user::MAILDISPLAY_HIDE;
$params[$userid1] = $USER->id;
}
$conditions[] = $email;
// Search by idnumber.
$idnumber = $DB->sql_like('idnumber', ':' . $searchkey3, false, false);
if (!in_array('idnumber', $userfields)) {
$userid2 = 'userid02';
// Users who aren't allowed to see idnumbers should at most find themselves
// when searching for an idnumber.
$idnumber = "(". $idnumber . " AND u.id = :$userid2)";
$params[$userid2] = $USER->id;
}
$conditions[] = $idnumber;
// Search all user identify fields.
$extrasearchfields = fields::get_identity_fields(null);
foreach ($extrasearchfields as $fieldindex => $extrasearchfield) {
if (in_array($extrasearchfield, ['email', 'idnumber', 'country'])) {
// Already covered above.
continue;
}
// The param must be short (max 32 characters) so don't include field name.
$param = $searchkey3 . '_ident' . $fieldindex;
$fieldsql = $mappings[$extrasearchfield];
$condition = $DB->sql_like($fieldsql, ':' . $param, false, false);
$params[$param] = "%$this->usersearch%";
if (!in_array($extrasearchfield, $userfields)) {
// User cannot see this field, but allow match if their own account.
$userid3 = 'userid03_ident' . $fieldindex;
$condition = "(". $condition . " AND u.id = :$userid3)";
$params[$userid3] = $USER->id;
}
$conditions[] = $condition;
}
$where = "(". implode(" OR ", $conditions) .") ";
$params[$searchkey1] = "%$this->usersearch%";
$params[$searchkey2] = "%$this->usersearch%";
$params[$searchkey3] = "%$this->usersearch%";
return [
'where' => $where,
'params' => $params,
];
}
/**
* Returns an arrow icon inside an <a> tag, for the purpose of sorting a column.
* @param string $direction
* @param moodle_url|null $sortlink
*/
protected function get_sort_arrow(string $direction = 'down', ?moodle_url $sortlink = null) {
global $OUTPUT;
$pix = ['up' => 't/sort_desc', 'down' => 't/sort_asc'];
$matrix = ['up' => 'desc', 'down' => 'asc'];
$strsort = grade_helper::get_lang_string($matrix[$direction], 'moodle');
$arrow = $OUTPUT->pix_icon($pix[$direction], '', '', ['class' => 'sorticon']);
return html_writer::link($sortlink, $arrow, ['title' => $strsort, 'aria-label' => $strsort]);
}
/**
* Optionally blank out course/category totals if they contain any hidden items
* @param string $courseid the course id
* @param string $course_item an instance of grade_item
* @param string $finalgrade the grade for the course_item
* @return array[] containing values for 'grade', 'grademax', 'grademin', 'aggregationstatus' and 'aggregationweight'
*/
protected function blank_hidden_total_and_adjust_bounds($courseid, $course_item, $finalgrade) {
global $CFG, $DB;
static $hiding_affected = null;//array of items in this course affected by hiding
// If we're dealing with multiple users we need to know when we've moved on to a new user.
static $previous_userid = null;
// If we're dealing with multiple courses we need to know when we've moved on to a new course.
static $previous_courseid = null;
$coursegradegrade = grade_grade::fetch(array('userid'=>$this->user->id, 'itemid'=>$course_item->id));
$grademin = $course_item->grademin;
$grademax = $course_item->grademax;
if ($coursegradegrade) {
$grademin = $coursegradegrade->get_grade_min();
$grademax = $coursegradegrade->get_grade_max();
} else {
$coursegradegrade = new grade_grade(array('userid'=>$this->user->id, 'itemid'=>$course_item->id), false);
}
$hint = $coursegradegrade->get_aggregation_hint();
$aggregationstatus = $hint['status'];
$aggregationweight = $hint['weight'];
if (!is_array($this->showtotalsifcontainhidden)) {
debugging('showtotalsifcontainhidden should be an array', DEBUG_DEVELOPER);
$this->showtotalsifcontainhidden = array($courseid => $this->showtotalsifcontainhidden);
}
if ($this->showtotalsifcontainhidden[$courseid] == GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN) {
return array('grade' => $finalgrade,
'grademin' => $grademin,
'grademax' => $grademax,
'aggregationstatus' => $aggregationstatus,
'aggregationweight' => $aggregationweight);
}
// If we've moved on to another course or user, reload the grades.
if ($previous_userid != $this->user->id || $previous_courseid != $courseid) {
$hiding_affected = null;
$previous_userid = $this->user->id;
$previous_courseid = $courseid;
}
if (!$hiding_affected) {
$items = grade_item::fetch_all(array('courseid'=>$courseid));
$grades = array();
$sql = "SELECT g.*
FROM {grade_grades} g
JOIN {grade_items} gi ON gi.id = g.itemid
WHERE g.userid = {$this->user->id} AND gi.courseid = {$courseid}";
if ($gradesrecords = $DB->get_records_sql($sql)) {
foreach ($gradesrecords as $grade) {
$grades[$grade->itemid] = new grade_grade($grade, false);
}
unset($gradesrecords);
}
foreach ($items as $itemid => $unused) {
if (!isset($grades[$itemid])) {
$grade_grade = new grade_grade();
$grade_grade->userid = $this->user->id;
$grade_grade->itemid = $items[$itemid]->id;
$grades[$itemid] = $grade_grade;
}
$grades[$itemid]->grade_item =& $items[$itemid];
}
$hiding_affected = grade_grade::get_hiding_affected($grades, $items);
}
//if the item definitely depends on a hidden item
if (array_key_exists($course_item->id, $hiding_affected['altered']) ||
array_key_exists($course_item->id, $hiding_affected['alteredgrademin']) ||
array_key_exists($course_item->id, $hiding_affected['alteredgrademax']) ||
array_key_exists($course_item->id, $hiding_affected['alteredaggregationstatus']) ||
array_key_exists($course_item->id, $hiding_affected['alteredaggregationweight'])) {
if (!$this->showtotalsifcontainhidden[$courseid] && array_key_exists($course_item->id, $hiding_affected['altered'])) {
// Hide the grade, but only when it has changed.
$finalgrade = null;
} else {
//use reprocessed marks that exclude hidden items
if (array_key_exists($course_item->id, $hiding_affected['altered'])) {
$finalgrade = $hiding_affected['altered'][$course_item->id];
}
if (array_key_exists($course_item->id, $hiding_affected['alteredgrademin'])) {
$grademin = $hiding_affected['alteredgrademin'][$course_item->id];
}
if (array_key_exists($course_item->id, $hiding_affected['alteredgrademax'])) {
$grademax = $hiding_affected['alteredgrademax'][$course_item->id];
}
if (array_key_exists($course_item->id, $hiding_affected['alteredaggregationstatus'])) {
$aggregationstatus = $hiding_affected['alteredaggregationstatus'][$course_item->id];
}
if (array_key_exists($course_item->id, $hiding_affected['alteredaggregationweight'])) {
$aggregationweight = $hiding_affected['alteredaggregationweight'][$course_item->id];
}
if (!$this->showtotalsifcontainhidden[$courseid]) {
// If the course total is hidden we must hide the weight otherwise
// it can be used to compute the course total.
$aggregationstatus = 'unknown';
$aggregationweight = null;
}
}
} else if (array_key_exists($course_item->id, $hiding_affected['unknowngrades'])) {
//not sure whether or not this item depends on a hidden item
if (!$this->showtotalsifcontainhidden[$courseid]) {
//hide the grade
$finalgrade = null;
} else {
//use reprocessed marks that exclude hidden items
$finalgrade = $hiding_affected['unknowngrades'][$course_item->id];
if (array_key_exists($course_item->id, $hiding_affected['alteredgrademin'])) {
$grademin = $hiding_affected['alteredgrademin'][$course_item->id];
}
if (array_key_exists($course_item->id, $hiding_affected['alteredgrademax'])) {
$grademax = $hiding_affected['alteredgrademax'][$course_item->id];
}
if (array_key_exists($course_item->id, $hiding_affected['alteredaggregationstatus'])) {
$aggregationstatus = $hiding_affected['alteredaggregationstatus'][$course_item->id];
}
if (array_key_exists($course_item->id, $hiding_affected['alteredaggregationweight'])) {
$aggregationweight = $hiding_affected['alteredaggregationweight'][$course_item->id];
}
}
}
return array('grade' => $finalgrade, 'grademin' => $grademin, 'grademax' => $grademax, 'aggregationstatus'=>$aggregationstatus, 'aggregationweight'=>$aggregationweight);
}
/**
* Optionally blank out course/category totals if they contain any hidden items
* @deprecated since Moodle 2.8 - Call blank_hidden_total_and_adjust_bounds instead.
* @param string $courseid the course id
* @param string $course_item an instance of grade_item
* @param string $finalgrade the grade for the course_item
* @return string The new final grade
*/
protected function blank_hidden_total($courseid, $course_item, $finalgrade) {
// Note it is flawed to call this function directly because
// the aggregated grade does not make sense without the updated min and max information.
debugging('grade_report::blank_hidden_total() is deprecated.
Call grade_report::blank_hidden_total_and_adjust_bounds instead.', DEBUG_DEVELOPER);
$result = $this->blank_hidden_total_and_adjust_bounds($courseid, $course_item, $finalgrade);
return $result['grade'];
}
/**
* Calculate average grade for a given grade item.
* Based on calculate_averages function from grade/report/user/lib.php
*
* @param grade_item $gradeitem Grade item
* @param array $info Ungraded grade items counts and report preferences.
* @return array Average grade and meancount.
*/
public static function calculate_average(grade_item $gradeitem, array $info): array {
$meanselection = $info['report']['meanselection'];
$totalcount = $info['report']['totalcount'];
$ungradedcounts = $info['ungradedcounts'];
$sumarray = $info['sumarray'];
if (empty($sumarray[$gradeitem->id])) {
$sumarray[$gradeitem->id] = 0;
}
if (empty($ungradedcounts[$gradeitem->id])) {
$ungradedcounts = 0;
} else {
$ungradedcounts = $ungradedcounts[$gradeitem->id]->count;
}
// If they want the averages to include all grade items.
if ($meanselection == GRADE_REPORT_MEAN_GRADED) {
$meancount = $totalcount - $ungradedcounts;
} else {
// Bump up the sum by the number of ungraded items * grademin.
$sumarray[$gradeitem->id] += ($ungradedcounts * $gradeitem->grademin);
$meancount = $totalcount;
}
$aggr['meancount'] = $meancount;
if (empty($sumarray[$gradeitem->id]) || $meancount == 0) {
$aggr['average'] = null;
} else {
$sum = $sumarray[$gradeitem->id];
$aggr['average'] = $sum / $meancount;
}
return $aggr;
}
/**
* Get ungraded grade items info and sum of all grade items in a course.
* Based on calculate_averages function from grade/report/user/lib.php
*
* @return array Ungraded grade items counts with report preferences.
*/
public function ungraded_counts(): array {
global $DB;
$groupid = null;
if (isset($this->gpr->groupid)) {
$groupid = $this->gpr->groupid;
}
$info = [];
$info['report'] = [
'averagesdisplaytype' => $this->get_pref('averagesdisplaytype'),
'averagesdecimalpoints' => $this->get_pref('averagesdecimalpoints'),
'meanselection' => $this->get_pref('meanselection'),
'shownumberofgrades' => $this->get_pref('shownumberofgrades'),
'totalcount' => $this->get_numusers(!is_null($groupid)),
];
// 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');
// Limit to users with a gradeable role ie students.
list($gradebookrolessql, $gradebookrolesparams) =
$DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0');
// Limit to users with an active enrolment.
$defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
$showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
$showonlyactiveenrol = $showonlyactiveenrol ||
!has_capability('moodle/course:viewsuspendedusers', $this->context);
list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context, '', 0, $showonlyactiveenrol);
$params = array_merge($this->groupwheresql_params, $gradebookrolesparams, $enrolledparams, $relatedctxparams);
$params['courseid'] = $this->courseid;
// Aggregate on whole course only.
if (empty($groupid)) {
$this->groupsql = null;
$this->groupwheresql = null;
}
// Empty grades must be evaluated as grademin, NOT always 0.
// This query returns a count of ungraded grades (NULL finalgrade OR no matching record in grade_grades table).
// No join condition when joining grade_items and user to get a grade item row for every user.
// Then left join with grade_grades and look for rows with null final grade
// (which includes grade items with no grade_grade).
$sql = "SELECT gi.id, COUNT(u.id) AS count
FROM {grade_items} gi
JOIN {user} u ON u.deleted = 0
JOIN ($enrolledsql) je ON je.id = u.id
JOIN (
SELECT DISTINCT ra.userid
FROM {role_assignments} ra
WHERE ra.roleid $gradebookrolessql
AND ra.contextid $relatedctxsql
) rainner ON rainner.userid = u.id
LEFT JOIN {grade_grades} gg
ON (gg.itemid = gi.id AND gg.userid = u.id AND gg.finalgrade IS NOT NULL AND gg.hidden = 0)
$this->groupsql
WHERE gi.courseid = :courseid
AND gg.finalgrade IS NULL
$this->groupwheresql
GROUP BY gi.id";
$info['ungradedcounts'] = $DB->get_records_sql($sql, $params);
// Find sums of all grade items in course.
$sql = "SELECT gg.itemid, SUM(gg.finalgrade) AS sum
FROM {grade_items} gi
JOIN {grade_grades} gg ON gg.itemid = gi.id
JOIN {user} u ON u.id = gg.userid
JOIN ($enrolledsql) je ON je.id = gg.userid
JOIN (
SELECT DISTINCT ra.userid
FROM {role_assignments} ra
WHERE ra.roleid $gradebookrolessql
AND ra.contextid $relatedctxsql
) rainner ON rainner.userid = u.id
$this->groupsql
WHERE gi.courseid = :courseid
AND u.deleted = 0
AND gg.finalgrade IS NOT NULL
AND gg.hidden = 0
$this->groupwheresql
GROUP BY gg.itemid";
$sumarray = [];
$sums = $DB->get_recordset_sql($sql, $params);
foreach ($sums as $itemid => $csum) {
$sumarray[$itemid] = grade_floatval($csum->sum);
}
$sums->close();
$info['sumarray'] = $sumarray;
return $info;
}
/**
* Get grade item type names in a course to use in filter dropdown.
*
* @return array Item types.
*/
public function item_types(): array {
global $DB, $CFG;
$modnames = [];
$sql = "(SELECT gi.itemmodule
FROM {grade_items} gi
WHERE gi.courseid = :courseid1
AND gi.itemmodule IS NOT NULL)
UNION
(SELECT gi1.itemtype
FROM {grade_items} gi1
WHERE gi1.courseid = :courseid2
AND gi1.itemtype = 'manual')";
$itemtypes = $DB->get_records_sql($sql, ['courseid1' => $this->courseid, 'courseid2' => $this->courseid]);
foreach ($itemtypes as $itemtype => $value) {
if (file_exists("$CFG->dirroot/mod/$itemtype/lib.php")) {
$modnames[$itemtype] = get_string("modulename", $itemtype, null, true);
} else if ($itemtype == 'manual') {
$modnames[$itemtype] = get_string('manualitem', 'grades', null, true);
}
}
return $modnames;
}
}