forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
4144 lines (3645 loc) · 155 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/>.
/**
* Functions used by gradebook plugins and reports.
*
* @package core_grades
* @copyright 2009 Petr Skoda and Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir . '/gradelib.php');
require_once($CFG->dirroot . '/grade/export/lib.php');
use \core_grades\output\action_bar;
use \core_grades\output\general_action_bar;
/**
* This class iterates over all users that are graded in a course.
* Returns detailed info about users and their grades.
*
* @author Petr Skoda <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class graded_users_iterator {
/**
* The couse whose users we are interested in
*/
protected $course;
/**
* An array of grade items or null if only user data was requested
*/
protected $grade_items;
/**
* The group ID we are interested in. 0 means all groups.
*/
protected $groupid;
/**
* A recordset of graded users
*/
protected $users_rs;
/**
* A recordset of user grades (grade_grade instances)
*/
protected $grades_rs;
/**
* Array used when moving to next user while iterating through the grades recordset
*/
protected $gradestack;
/**
* The first field of the users table by which the array of users will be sorted
*/
protected $sortfield1;
/**
* Should sortfield1 be ASC or DESC
*/
protected $sortorder1;
/**
* The second field of the users table by which the array of users will be sorted
*/
protected $sortfield2;
/**
* Should sortfield2 be ASC or DESC
*/
protected $sortorder2;
/**
* Should users whose enrolment has been suspended be ignored?
*/
protected $onlyactive = false;
/**
* Enable user custom fields
*/
protected $allowusercustomfields = false;
/**
* List of suspended users in course. This includes users whose enrolment status is suspended
* or enrolment has expired or not started.
*/
protected $suspendedusers = array();
/**
* Constructor
*
* @param object $course A course object
* @param array $grade_items array of grade items, if not specified only user info returned
* @param int $groupid iterate only group users if present
* @param string $sortfield1 The first field of the users table by which the array of users will be sorted
* @param string $sortorder1 The order in which the first sorting field will be sorted (ASC or DESC)
* @param string $sortfield2 The second field of the users table by which the array of users will be sorted
* @param string $sortorder2 The order in which the second sorting field will be sorted (ASC or DESC)
*/
public function __construct($course, $grade_items=null, $groupid=0,
$sortfield1='lastname', $sortorder1='ASC',
$sortfield2='firstname', $sortorder2='ASC') {
$this->course = $course;
$this->grade_items = $grade_items;
$this->groupid = $groupid;
$this->sortfield1 = $sortfield1;
$this->sortorder1 = $sortorder1;
$this->sortfield2 = $sortfield2;
$this->sortorder2 = $sortorder2;
$this->gradestack = array();
}
/**
* Initialise the iterator
*
* @return boolean success
*/
public function init() {
global $CFG, $DB;
$this->close();
export_verify_grades($this->course->id);
$course_item = grade_item::fetch_course_item($this->course->id);
if ($course_item->needsupdate) {
// Can not calculate all final grades - sorry.
return false;
}
$coursecontext = context_course::instance($this->course->id);
list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
list($gradebookroles_sql, $params) = $DB->get_in_or_equal(explode(',', $CFG->gradebookroles), SQL_PARAMS_NAMED, 'grbr');
list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, '', 0, $this->onlyactive);
$params = array_merge($params, $enrolledparams, $relatedctxparams);
if ($this->groupid) {
$groupsql = "INNER JOIN {groups_members} gm ON gm.userid = u.id";
$groupwheresql = "AND gm.groupid = :groupid";
// $params contents: gradebookroles
$params['groupid'] = $this->groupid;
} else {
$groupsql = "";
$groupwheresql = "";
}
if (empty($this->sortfield1)) {
// We must do some sorting even if not specified.
$ofields = ", u.id AS usrt";
$order = "usrt ASC";
} else {
$ofields = ", u.$this->sortfield1 AS usrt1";
$order = "usrt1 $this->sortorder1";
if (!empty($this->sortfield2)) {
$ofields .= ", u.$this->sortfield2 AS usrt2";
$order .= ", usrt2 $this->sortorder2";
}
if ($this->sortfield1 != 'id' and $this->sortfield2 != 'id') {
// User order MUST be the same in both queries,
// must include the only unique user->id if not already present.
$ofields .= ", u.id AS usrt";
$order .= ", usrt ASC";
}
}
$userfields = 'u.*';
$customfieldssql = '';
if ($this->allowusercustomfields && !empty($CFG->grade_export_customprofilefields)) {
$customfieldscount = 0;
$customfieldsarray = grade_helper::get_user_profile_fields($this->course->id, $this->allowusercustomfields);
foreach ($customfieldsarray as $field) {
if (!empty($field->customid)) {
$customfieldssql .= "
LEFT JOIN (SELECT * FROM {user_info_data}
WHERE fieldid = :cf$customfieldscount) cf$customfieldscount
ON u.id = cf$customfieldscount.userid";
$userfields .= ", cf$customfieldscount.data AS customfield_{$field->customid}";
$params['cf'.$customfieldscount] = $field->customid;
$customfieldscount++;
}
}
}
$users_sql = "SELECT $userfields $ofields
FROM {user} u
JOIN ($enrolledsql) je ON je.id = u.id
$groupsql $customfieldssql
JOIN (
SELECT DISTINCT ra.userid
FROM {role_assignments} ra
WHERE ra.roleid $gradebookroles_sql
AND ra.contextid $relatedctxsql
) rainner ON rainner.userid = u.id
WHERE u.deleted = 0
$groupwheresql
ORDER BY $order";
$this->users_rs = $DB->get_recordset_sql($users_sql, $params);
if (!$this->onlyactive) {
$context = context_course::instance($this->course->id);
$this->suspendedusers = get_suspended_userids($context);
} else {
$this->suspendedusers = array();
}
if (!empty($this->grade_items)) {
$itemids = array_keys($this->grade_items);
list($itemidsql, $grades_params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED, 'items');
$params = array_merge($params, $grades_params);
$grades_sql = "SELECT g.* $ofields
FROM {grade_grades} g
JOIN {user} u ON g.userid = u.id
JOIN ($enrolledsql) je ON je.id = u.id
$groupsql
JOIN (
SELECT DISTINCT ra.userid
FROM {role_assignments} ra
WHERE ra.roleid $gradebookroles_sql
AND ra.contextid $relatedctxsql
) rainner ON rainner.userid = u.id
WHERE u.deleted = 0
AND g.itemid $itemidsql
$groupwheresql
ORDER BY $order, g.itemid ASC";
$this->grades_rs = $DB->get_recordset_sql($grades_sql, $params);
} else {
$this->grades_rs = false;
}
return true;
}
/**
* Returns information about the next user
* @return mixed array of user info, all grades and feedback or null when no more users found
*/
public function next_user() {
if (!$this->users_rs) {
return false; // no users present
}
if (!$this->users_rs->valid()) {
if ($current = $this->_pop()) {
// this is not good - user or grades updated between the two reads above :-(
}
return false; // no more users
} else {
$user = $this->users_rs->current();
$this->users_rs->next();
}
// find grades of this user
$grade_records = array();
while (true) {
if (!$current = $this->_pop()) {
break; // no more grades
}
if (empty($current->userid)) {
break;
}
if ($current->userid != $user->id) {
// grade of the next user, we have all for this user
$this->_push($current);
break;
}
$grade_records[$current->itemid] = $current;
}
$grades = array();
$feedbacks = array();
if (!empty($this->grade_items)) {
foreach ($this->grade_items as $grade_item) {
if (!isset($feedbacks[$grade_item->id])) {
$feedbacks[$grade_item->id] = new stdClass();
}
if (array_key_exists($grade_item->id, $grade_records)) {
$feedbacks[$grade_item->id]->feedback = $grade_records[$grade_item->id]->feedback;
$feedbacks[$grade_item->id]->feedbackformat = $grade_records[$grade_item->id]->feedbackformat;
unset($grade_records[$grade_item->id]->feedback);
unset($grade_records[$grade_item->id]->feedbackformat);
$grades[$grade_item->id] = new grade_grade($grade_records[$grade_item->id], false);
} else {
$feedbacks[$grade_item->id]->feedback = '';
$feedbacks[$grade_item->id]->feedbackformat = FORMAT_MOODLE;
$grades[$grade_item->id] =
new grade_grade(array('userid'=>$user->id, 'itemid'=>$grade_item->id), false);
}
$grades[$grade_item->id]->grade_item = $grade_item;
}
}
// Set user suspended status.
$user->suspendedenrolment = isset($this->suspendedusers[$user->id]);
$result = new stdClass();
$result->user = $user;
$result->grades = $grades;
$result->feedbacks = $feedbacks;
return $result;
}
/**
* Close the iterator, do not forget to call this function
*/
public function close() {
if ($this->users_rs) {
$this->users_rs->close();
$this->users_rs = null;
}
if ($this->grades_rs) {
$this->grades_rs->close();
$this->grades_rs = null;
}
$this->gradestack = array();
}
/**
* Should all enrolled users be exported or just those with an active enrolment?
*
* @param bool $onlyactive True to limit the export to users with an active enrolment
*/
public function require_active_enrolment($onlyactive = true) {
if (!empty($this->users_rs)) {
debugging('Calling require_active_enrolment() has no effect unless you call init() again', DEBUG_DEVELOPER);
}
$this->onlyactive = $onlyactive;
}
/**
* Allow custom fields to be included
*
* @param bool $allow Whether to allow custom fields or not
* @return void
*/
public function allow_user_custom_fields($allow = true) {
if ($allow) {
$this->allowusercustomfields = true;
} else {
$this->allowusercustomfields = false;
}
}
/**
* Add a grade_grade instance to the grade stack
*
* @param grade_grade $grade Grade object
*
* @return void
*/
private function _push($grade) {
array_push($this->gradestack, $grade);
}
/**
* Remove a grade_grade instance from the grade stack
*
* @return grade_grade current grade object
*/
private function _pop() {
global $DB;
if (empty($this->gradestack)) {
if (empty($this->grades_rs) || !$this->grades_rs->valid()) {
return null; // no grades present
}
$current = $this->grades_rs->current();
$this->grades_rs->next();
return $current;
} else {
return array_pop($this->gradestack);
}
}
}
/**
* Print a selection popup form of the graded users in a course.
*
* @deprecated since 2.0
*
* @param int $course id of the course
* @param string $actionpage The page receiving the data from the popoup form
* @param int $userid id of the currently selected user (or 'all' if they are all selected)
* @param int $groupid id of requested group, 0 means all
* @param int $includeall bool include all option
* @param bool $return If true, will return the HTML, otherwise, will print directly
* @return null
*/
function print_graded_users_selector($course, $actionpage, $userid=0, $groupid=0, $includeall=true, $return=false) {
global $CFG, $USER, $OUTPUT;
return $OUTPUT->render(grade_get_graded_users_select(substr($actionpage, 0, strpos($actionpage, '/')), $course, $userid, $groupid, $includeall));
}
function grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall) {
global $USER, $CFG;
if (is_null($userid)) {
$userid = $USER->id;
}
$coursecontext = context_course::instance($course->id);
$defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
$showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
$showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $coursecontext);
$menu = array(); // Will be a list of userid => user name
$menususpendedusers = array(); // Suspended users go to a separate optgroup.
$gui = new graded_users_iterator($course, null, $groupid);
$gui->require_active_enrolment($showonlyactiveenrol);
$gui->init();
$label = get_string('selectauser', 'grades');
if ($includeall) {
$menu[0] = get_string('allusers', 'grades');
$label = get_string('selectalloroneuser', 'grades');
}
while ($userdata = $gui->next_user()) {
$user = $userdata->user;
$userfullname = fullname($user);
if ($user->suspendedenrolment) {
$menususpendedusers[$user->id] = $userfullname;
} else {
$menu[$user->id] = $userfullname;
}
}
$gui->close();
if ($includeall) {
$menu[0] .= " (" . (count($menu) + count($menususpendedusers) - 1) . ")";
}
if (!empty($menususpendedusers)) {
$menu[] = array(get_string('suspendedusers') => $menususpendedusers);
}
$gpr = new grade_plugin_return(array('type' => 'report', 'course' => $course, 'groupid' => $groupid));
$select = new single_select(
new moodle_url('/grade/report/'.$report.'/index.php', $gpr->get_options()),
'userid', $menu, $userid
);
$select->label = $label;
$select->formid = 'choosegradeuser';
return $select;
}
/**
* Hide warning about changed grades during upgrade to 2.8.
*
* @param int $courseid The current course id.
*/
function hide_natural_aggregation_upgrade_notice($courseid) {
unset_config('show_sumofgrades_upgrade_' . $courseid);
}
/**
* Hide warning about changed grades during upgrade from 2.8.0-2.8.6 and 2.9.0.
*
* @param int $courseid The current course id.
*/
function grade_hide_min_max_grade_upgrade_notice($courseid) {
unset_config('show_min_max_grades_changed_' . $courseid);
}
/**
* Use the grade min and max from the grade_grade.
*
* This is reserved for core use after an upgrade.
*
* @param int $courseid The current course id.
*/
function grade_upgrade_use_min_max_from_grade_grade($courseid) {
grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_GRADE);
grade_force_full_regrading($courseid);
// Do this now, because it probably happened to late in the page load to be happen automatically.
grade_regrade_final_grades($courseid);
}
/**
* Use the grade min and max from the grade_item.
*
* This is reserved for core use after an upgrade.
*
* @param int $courseid The current course id.
*/
function grade_upgrade_use_min_max_from_grade_item($courseid) {
grade_set_setting($courseid, 'minmaxtouse', GRADE_MIN_MAX_FROM_GRADE_ITEM);
grade_force_full_regrading($courseid);
// Do this now, because it probably happened to late in the page load to be happen automatically.
grade_regrade_final_grades($courseid);
}
/**
* Hide warning about changed grades during upgrade to 2.8.
*
* @param int $courseid The current course id.
*/
function hide_aggregatesubcats_upgrade_notice($courseid) {
unset_config('show_aggregatesubcats_upgrade_' . $courseid);
}
/**
* Hide warning about changed grades due to bug fixes
*
* @param int $courseid The current course id.
*/
function hide_gradebook_calculations_freeze_notice($courseid) {
unset_config('gradebook_calculations_freeze_' . $courseid);
}
/**
* Print warning about changed grades during upgrade to 2.8.
*
* @param int $courseid The current course id.
* @param context $context The course context.
* @param string $thispage The relative path for the current page. E.g. /grade/report/user/index.php
* @param boolean $return return as string
*
* @return nothing or string if $return true
*/
function print_natural_aggregation_upgrade_notice($courseid, $context, $thispage, $return=false) {
global $CFG, $OUTPUT;
$html = '';
// Do not do anything if they cannot manage the grades of this course.
if (!has_capability('moodle/grade:manage', $context)) {
return $html;
}
$hidesubcatswarning = optional_param('seenaggregatesubcatsupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
$showsubcatswarning = get_config('core', 'show_aggregatesubcats_upgrade_' . $courseid);
$hidenaturalwarning = optional_param('seensumofgradesupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
$shownaturalwarning = get_config('core', 'show_sumofgrades_upgrade_' . $courseid);
$hideminmaxwarning = optional_param('seenminmaxupgradedgrades', false, PARAM_BOOL) && confirm_sesskey();
$showminmaxwarning = get_config('core', 'show_min_max_grades_changed_' . $courseid);
$useminmaxfromgradeitem = optional_param('useminmaxfromgradeitem', false, PARAM_BOOL) && confirm_sesskey();
$useminmaxfromgradegrade = optional_param('useminmaxfromgradegrade', false, PARAM_BOOL) && confirm_sesskey();
$minmaxtouse = grade_get_setting($courseid, 'minmaxtouse', $CFG->grade_minmaxtouse);
$gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
$acceptgradebookchanges = optional_param('acceptgradebookchanges', false, PARAM_BOOL) && confirm_sesskey();
// Hide the warning if the user told it to go away.
if ($hidenaturalwarning) {
hide_natural_aggregation_upgrade_notice($courseid);
}
// Hide the warning if the user told it to go away.
if ($hidesubcatswarning) {
hide_aggregatesubcats_upgrade_notice($courseid);
}
// Hide the min/max warning if the user told it to go away.
if ($hideminmaxwarning) {
grade_hide_min_max_grade_upgrade_notice($courseid);
$showminmaxwarning = false;
}
if ($useminmaxfromgradegrade) {
// Revert to the new behaviour, we now use the grade_grade for min/max.
grade_upgrade_use_min_max_from_grade_grade($courseid);
grade_hide_min_max_grade_upgrade_notice($courseid);
$showminmaxwarning = false;
} else if ($useminmaxfromgradeitem) {
// Apply the new logic, we now use the grade_item for min/max.
grade_upgrade_use_min_max_from_grade_item($courseid);
grade_hide_min_max_grade_upgrade_notice($courseid);
$showminmaxwarning = false;
}
if (!$hidenaturalwarning && $shownaturalwarning) {
$message = get_string('sumofgradesupgradedgrades', 'grades');
$hidemessage = get_string('upgradedgradeshidemessage', 'grades');
$urlparams = array( 'id' => $courseid,
'seensumofgradesupgradedgrades' => true,
'sesskey' => sesskey());
$goawayurl = new moodle_url($thispage, $urlparams);
$goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
$html .= $OUTPUT->notification($message, 'notifysuccess');
$html .= $goawaybutton;
}
if (!$hidesubcatswarning && $showsubcatswarning) {
$message = get_string('aggregatesubcatsupgradedgrades', 'grades');
$hidemessage = get_string('upgradedgradeshidemessage', 'grades');
$urlparams = array( 'id' => $courseid,
'seenaggregatesubcatsupgradedgrades' => true,
'sesskey' => sesskey());
$goawayurl = new moodle_url($thispage, $urlparams);
$goawaybutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
$html .= $OUTPUT->notification($message, 'notifysuccess');
$html .= $goawaybutton;
}
if ($showminmaxwarning) {
$hidemessage = get_string('upgradedgradeshidemessage', 'grades');
$urlparams = array( 'id' => $courseid,
'seenminmaxupgradedgrades' => true,
'sesskey' => sesskey());
$goawayurl = new moodle_url($thispage, $urlparams);
$hideminmaxbutton = $OUTPUT->single_button($goawayurl, $hidemessage, 'get');
$moreinfo = html_writer::link(get_docs_url(get_string('minmaxtouse_link', 'grades')), get_string('moreinfo'),
array('target' => '_blank'));
if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_ITEM) {
// Show the message that there were min/max issues that have been resolved.
$message = get_string('minmaxupgradedgrades', 'grades') . ' ' . $moreinfo;
$revertmessage = get_string('upgradedminmaxrevertmessage', 'grades');
$urlparams = array('id' => $courseid,
'useminmaxfromgradegrade' => true,
'sesskey' => sesskey());
$reverturl = new moodle_url($thispage, $urlparams);
$revertbutton = $OUTPUT->single_button($reverturl, $revertmessage, 'get');
$html .= $OUTPUT->notification($message);
$html .= $revertbutton . $hideminmaxbutton;
} else if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_GRADE) {
// Show the warning that there are min/max issues that have not be resolved.
$message = get_string('minmaxupgradewarning', 'grades') . ' ' . $moreinfo;
$fixmessage = get_string('minmaxupgradefixbutton', 'grades');
$urlparams = array('id' => $courseid,
'useminmaxfromgradeitem' => true,
'sesskey' => sesskey());
$fixurl = new moodle_url($thispage, $urlparams);
$fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get');
$html .= $OUTPUT->notification($message);
$html .= $fixbutton . $hideminmaxbutton;
}
}
if ($gradebookcalculationsfreeze) {
if ($acceptgradebookchanges) {
// Accept potential changes in grades caused by extra credit bug MDL-49257.
hide_gradebook_calculations_freeze_notice($courseid);
$courseitem = grade_item::fetch_course_item($courseid);
$courseitem->force_regrading();
grade_regrade_final_grades($courseid);
$html .= $OUTPUT->notification(get_string('gradebookcalculationsuptodate', 'grades'), 'notifysuccess');
} else {
// Show the warning that there may be extra credit weights problems.
$a = new stdClass();
$a->gradebookversion = $gradebookcalculationsfreeze;
if (preg_match('/(\d{8,})/', $CFG->release, $matches)) {
$a->currentversion = $matches[1];
} else {
$a->currentversion = $CFG->release;
}
$a->url = get_docs_url('Gradebook_calculation_changes');
$message = get_string('gradebookcalculationswarning', 'grades', $a);
$fixmessage = get_string('gradebookcalculationsfixbutton', 'grades');
$urlparams = array('id' => $courseid,
'acceptgradebookchanges' => true,
'sesskey' => sesskey());
$fixurl = new moodle_url($thispage, $urlparams);
$fixbutton = $OUTPUT->single_button($fixurl, $fixmessage, 'get');
$html .= $OUTPUT->notification($message);
$html .= $fixbutton;
}
}
if (!empty($html)) {
$html = html_writer::tag('div', $html, array('class' => 'core_grades_notices'));
}
if ($return) {
return $html;
} else {
echo $html;
}
}
/**
* grade_get_plugin_info
*
* @param int $courseid The course id
* @param string $active_type type of plugin on current page - import, export, report or edit
* @param string $active_plugin active plugin type - grader, user, cvs, ...
*
* @return array
*/
function grade_get_plugin_info($courseid, $active_type, $active_plugin) {
global $CFG, $SITE;
$context = context_course::instance($courseid);
$plugin_info = array();
$count = 0;
$active = '';
$url_prefix = $CFG->wwwroot . '/grade/';
// Language strings
$plugin_info['strings'] = grade_helper::get_plugin_strings();
if ($reports = grade_helper::get_plugins_reports($courseid)) {
$plugin_info['report'] = $reports;
}
if ($settings = grade_helper::get_info_manage_settings($courseid)) {
$plugin_info['settings'] = $settings;
}
if ($scale = grade_helper::get_info_scales($courseid)) {
$plugin_info['scale'] = array('view'=>$scale);
}
if ($outcomes = grade_helper::get_info_outcomes($courseid)) {
$plugin_info['outcome'] = $outcomes;
}
if ($letters = grade_helper::get_info_letters($courseid)) {
$plugin_info['letter'] = $letters;
}
if ($imports = grade_helper::get_plugins_import($courseid)) {
$plugin_info['import'] = $imports;
}
if ($exports = grade_helper::get_plugins_export($courseid)) {
$plugin_info['export'] = $exports;
}
// Let other plugins add plugins here so that we get extra tabs
// in the gradebook.
$callbacks = get_plugins_with_function('extend_gradebook_plugininfo', 'lib.php');
foreach ($callbacks as $plugins) {
foreach ($plugins as $pluginfunction) {
$plugin_info = $pluginfunction($plugin_info, $courseid);
}
}
foreach ($plugin_info as $plugin_type => $plugins) {
if (!empty($plugins->id) && $active_plugin == $plugins->id) {
$plugin_info['strings']['active_plugin_str'] = $plugins->string;
break;
}
foreach ($plugins as $plugin) {
if (is_a($plugin, grade_plugin_info::class)) {
if ($plugin_type === $active_type && $active_plugin == $plugin->id) {
$plugin_info['strings']['active_plugin_str'] = $plugin->string;
}
}
}
}
return $plugin_info;
}
/**
* Load a valid list of gradable users in a course.
*
* @param int $courseid The course ID.
* @param int|null $groupid The group ID (optional).
* @param bool $onlyactiveenrol Include only active enrolments.
* @return array $users A list of enrolled gradable users.
*/
function get_gradable_users(int $courseid, ?int $groupid = null, bool $onlyactiveenrol = false): array {
$course = get_course($courseid);
// Create a graded_users_iterator because it will properly check the groups etc.
$gui = new graded_users_iterator($course, null, $groupid);
$gui->require_active_enrolment($onlyactiveenrol);
$gui->init();
// Flatten the users.
$users = [];
while ($user = $gui->next_user()) {
$users[$user->user->id] = $user->user;
}
$gui->close();
return $users;
}
/**
* A simple class containing info about grade plugins.
* Can be subclassed for special rules
*
* @package core_grades
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_plugin_info {
/**
* A unique id for this plugin
*
* @var mixed
*/
public $id;
/**
* A URL to access this plugin
*
* @var mixed
*/
public $link;
/**
* The name of this plugin
*
* @var mixed
*/
public $string;
/**
* Another grade_plugin_info object, parent of the current one
*
* @var mixed
*/
public $parent;
/**
* Constructor
*
* @param int $id A unique id for this plugin
* @param string $link A URL to access this plugin
* @param string $string The name of this plugin
* @param object $parent Another grade_plugin_info object, parent of the current one
*
* @return void
*/
public function __construct($id, $link, $string, $parent=null) {
$this->id = $id;
$this->link = $link;
$this->string = $string;
$this->parent = $parent;
}
}
/**
* Prints the page headers, breadcrumb trail, page heading, (optional) navigation and for any gradebook page.
* All gradebook pages MUST use these functions in favour of the usual print_header(), print_header_simple(),
* print_heading() etc.
*
* @param int $courseid Course id
* @param string $active_type The type of the current page (report, settings,
* import, export, scales, outcomes, letters)
* @param string|null $active_plugin The plugin of the current page (grader, fullview etc...)
* @param string|bool $heading The heading of the page.
* @param boolean $return Whether to return (true) or echo (false) the HTML generated by this function
* @param string|bool $buttons Additional buttons to display on the page
* @param boolean $shownavigation should the gradebook navigation be shown?
* @param string|null $headerhelpidentifier The help string identifier if required.
* @param string|null $headerhelpcomponent The component for the help string.
* @param stdClass|null $user The user object for use with the user context header.
* @param action_bar|null $actionbar The actions bar which will be displayed on the page if $shownavigation is set
* to true. If $actionbar is not explicitly defined, the general action bar
* (\core_grades\output\general_action_bar) will be used by default.
* @param null $unused This parameter has been deprecated since 4.3 and should not be used anymore.
* @return string HTML code or nothing if $return == false
*/
function print_grade_page_head(int $courseid, string $active_type, ?string $active_plugin = null, string|bool $heading = false,
bool $return = false, $buttons = false, bool $shownavigation = true, ?string $headerhelpidentifier = null,
?string $headerhelpcomponent = null, ?stdClass $user = null, ?action_bar $actionbar = null, $unused = null) {
global $CFG, $OUTPUT, $PAGE, $USER;
if ($heading !== false) {
// Make sure to trim heading, including the non-breaking space character.
$heading = str_replace(" ", " ", $heading);
$heading = trim($heading);
}
if ($unused !== null) {
debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
}
// Put a warning on all gradebook pages if the course has modules currently scheduled for background deletion.
require_once($CFG->dirroot . '/course/lib.php');
if (course_modules_pending_deletion($courseid, true)) {
\core\notification::add(get_string('gradesmoduledeletionpendingwarning', 'grades'),
\core\output\notification::NOTIFY_WARNING);
}
if ($active_type === 'preferences') {
// In Moodle 2.8 report preferences were moved under 'settings'. Allow backward compatibility for 3rd party grade reports.
$active_type = 'settings';
}
$plugin_info = grade_get_plugin_info($courseid, $active_type, $active_plugin);
// Determine the string of the active plugin.
$stractive_type = $plugin_info['strings'][$active_type];
$stractiveplugin = ($active_plugin) ? $plugin_info['strings']['active_plugin_str'] : $heading;
if ($active_type == 'report') {
$PAGE->set_pagelayout('report');
} else {
$PAGE->set_pagelayout('admin');
}
$coursecontext = context_course::instance($courseid);
// Title will be constituted by information starting from the unique identifying information for the page.
if ($heading) {
// If heading is supplied, use this for the page title.
$uniquetitle = $heading;
} else if (in_array($active_type, ['report', 'settings'])) {
// For grade reports or settings pages of grade plugins, use the plugin name for the unique title.
$uniquetitle = $stractiveplugin;
// But if editing mode is turned on, check if the report plugin has an editing mode title string and use it if present.
if ($PAGE->user_is_editing() && $active_type === 'report') {
$strcomponent = "gradereport_{$active_plugin}";
if (get_string_manager()->string_exists('editingmode_title', $strcomponent)) {
$uniquetitle = get_string('editingmode_title', $strcomponent);
}
}
} else {
$uniquetitle = $stractive_type . ': ' . $stractiveplugin;
}
$titlecomponents = [
$uniquetitle,
$coursecontext->get_context_name(false),
];
$PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $titlecomponents));
$PAGE->set_heading($PAGE->course->fullname);
$PAGE->set_secondary_active_tab('grades');
if ($buttons instanceof single_button) {
$buttons = $OUTPUT->render($buttons);
}
$PAGE->set_button($buttons);
if ($courseid != SITEID) {
grade_extend_settings($plugin_info, $courseid);
}
// Set the current report as active in the breadcrumbs.
if ($active_plugin !== null && $reportnav = $PAGE->settingsnav->find($active_plugin, navigation_node::TYPE_SETTING)) {
$reportnav->make_active();
}
$returnval = $OUTPUT->header();
if (!$return) {
echo $returnval;
}
if ($shownavigation) {
$renderer = $PAGE->get_renderer('core_grades');
// If the navigation action bar is not explicitly defined, use the general (default) action bar.
if (!$actionbar) {
$actionbar = new general_action_bar($PAGE->context, $PAGE->url, $active_type, $active_plugin);
}
if ($return) {
$returnval .= $renderer->render_action_bar($actionbar);
} else {
echo $renderer->render_action_bar($actionbar);
}
}
$output = '';
// Add a help dialogue box if provided.
if (isset($headerhelpidentifier) && !empty($heading)) {
$output = $OUTPUT->heading_with_help($heading, $headerhelpidentifier, $headerhelpcomponent);
} else if (isset($user)) {
$renderer = $PAGE->get_renderer('core_grades');
// If the user is viewing their own grade report, no need to show the "Message"
// and "Add to contact" buttons in the user heading.
$showuserbuttons = $user->id != $USER->id;
$output = $renderer->user_heading($user, $courseid, $showuserbuttons);
} else if (!empty($heading)) {
$output = $OUTPUT->heading($heading);
}
if ($return) {
$returnval .= $output;
} else {
echo $output;
}
$returnval .= print_natural_aggregation_upgrade_notice($courseid, $coursecontext, $PAGE->url, $return);