Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Definition of a class to represent an individual user's grade
19
 *
20
 * @package   core_grades
21
 * @category  grade
22
 * @copyright 2006 Nicolas Connault
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
require_once('grade_object.php');
29
 
30
/**
31
 * grade_grades is an object mapped to DB table {prefix}grade_grades
32
 *
33
 * @package   core_grades
34
 * @category  grade
35
 * @copyright 2006 Nicolas Connault
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class grade_grade extends grade_object {
39
 
40
    /**
41
     * The DB table.
42
     * @var string $table
43
     */
44
    public $table = 'grade_grades';
45
 
46
    /**
47
     * Array of required table fields, must start with 'id'.
48
     * @var array $required_fields
49
     */
50
    public $required_fields = array('id', 'itemid', 'userid', 'rawgrade', 'rawgrademax', 'rawgrademin',
51
                                 'rawscaleid', 'usermodified', 'finalgrade', 'hidden', 'locked',
52
                                 'locktime', 'exported', 'overridden', 'excluded', 'timecreated',
1441 ariadna 53
                                 'timemodified', 'aggregationstatus', 'aggregationweight', 'deductedmark');
1 efrain 54
 
55
    /**
56
     * Array of optional fields with default values (these should match db defaults)
57
     * @var array $optional_fields
58
     */
59
    public $optional_fields = array('feedback'=>null, 'feedbackformat'=>0, 'information'=>null, 'informationformat'=>0);
60
 
61
    /**
62
     * The id of the grade_item this grade belongs to.
63
     * @var int $itemid
64
     */
65
    public $itemid;
66
 
67
    /**
68
     * The grade_item object referenced by $this->itemid.
69
     * @var grade_item $grade_item
70
     */
71
    public $grade_item;
72
 
73
    /**
74
     * The id of the user this grade belongs to.
75
     * @var int $userid
76
     */
77
    public $userid;
78
 
79
    /**
80
     * The grade value of this raw grade, if such was provided by the module.
81
     * @var float $rawgrade
82
     */
83
    public $rawgrade;
84
 
85
    /**
86
     * The maximum allowable grade when this grade was created.
87
     * @var float $rawgrademax
88
     */
89
    public $rawgrademax = 100;
90
 
91
    /**
92
     * The minimum allowable grade when this grade was created.
93
     * @var float $rawgrademin
94
     */
95
    public $rawgrademin = 0;
96
 
97
    /**
98
     * id of the scale, if this grade is based on a scale.
99
     * @var int $rawscaleid
100
     */
101
    public $rawscaleid;
102
 
103
    /**
104
     * The userid of the person who last modified this grade.
105
     * @var int $usermodified
106
     */
107
    public $usermodified;
108
 
109
    /**
110
     * The final value of this grade.
111
     * @var float $finalgrade
112
     */
113
    public $finalgrade;
114
 
115
    /**
116
     * 0 if visible, 1 always hidden or date not visible until
117
     * @var float $hidden
118
     */
119
    public $hidden = 0;
120
 
121
    /**
122
     * 0 not locked, date when the item was locked
123
     * @var float locked
124
     */
125
    public $locked = 0;
126
 
127
    /**
128
     * 0 no automatic locking, date when to lock the grade automatically
129
     * @var float $locktime
130
     */
131
    public $locktime = 0;
132
 
133
    /**
134
     * Exported flag
135
     * @var bool $exported
136
     */
137
    public $exported = 0;
138
 
139
    /**
140
     * Overridden flag
141
     * @var bool $overridden
142
     */
143
    public $overridden = 0;
144
 
145
    /**
146
     * Grade excluded from aggregation functions
147
     * @var bool $excluded
148
     */
149
    public $excluded = 0;
150
 
151
    /**
152
     * TODO: HACK: create a new field datesubmitted - the date of submission if any (MDL-31377)
153
     * @var bool $timecreated
154
     */
155
    public $timecreated = null;
156
 
157
    /**
158
     * TODO: HACK: create a new field dategraded - the date of grading (MDL-31378)
159
     * @var bool $timemodified
160
     */
161
    public $timemodified = null;
162
 
163
    /**
164
     * Aggregation status flag. Can be one of 'unknown', 'dropped', 'novalue' or 'used'.
165
     * @var string $aggregationstatus
166
     */
167
    public $aggregationstatus = 'unknown';
168
 
169
    /**
170
     * Aggregation weight is the specific weight used in the aggregation calculation for this grade.
171
     * @var float $aggregationweight
172
     */
173
    public $aggregationweight = null;
174
 
175
    /**
176
     * Feedback files to copy.
177
     *
178
     * Example -
179
     *
180
     * [
181
     *     'contextid' => 1,
182
     *     'component' => 'mod_xyz',
183
     *     'filearea' => 'mod_xyz_feedback',
184
     *     'itemid' => 2
185
     * ];
186
     *
187
     * @var array
188
     */
189
    public $feedbackfiles = [];
190
 
191
    /**
192
     * Feedback content.
193
     * @var string $feedback
194
     */
195
    public $feedback;
196
 
197
    /**
198
     * Feedback format.
199
     * @var int $feedbackformat
200
     */
201
    public $feedbackformat = FORMAT_PLAIN;
202
 
203
    /**
204
     * Information text.
205
     * @var string $information
206
     */
207
    public $information;
208
 
209
    /**
210
     * Information text format.
211
     * @var int $informationformat
212
     */
213
    public $informationformat = FORMAT_PLAIN;
214
 
215
    /**
216
     * label text.
217
     * @var string $label
218
     */
219
    public $label;
220
 
1441 ariadna 221
    /** @var float $deductedmark mark deducted from final grade */
222
    public float $deductedmark = 0;
223
 
1 efrain 224
    /**
1441 ariadna 225
     * Date when this grade was last graded.
226
     * @var int $dategraded
227
     */
228
    public $dategraded = null;
229
 
230
    /**
1 efrain 231
     * Returns array of grades for given grade_item+users
232
     *
233
     * @param grade_item $grade_item
234
     * @param array $userids
235
     * @param bool $include_missing include grades that do not exist yet
236
     * @return array userid=>grade_grade array
237
     */
238
    public static function fetch_users_grades($grade_item, $userids, $include_missing=true) {
239
        global $DB;
240
 
241
        // hmm, there might be a problem with length of sql query
242
        // if there are too many users requested - we might run out of memory anyway
243
        $limit = 2000;
244
        $count = count($userids);
245
        if ($count > $limit) {
246
            $half = (int)($count/2);
247
            $first  = array_slice($userids, 0, $half);
248
            $second = array_slice($userids, $half);
249
            return grade_grade::fetch_users_grades($grade_item, $first, $include_missing) + grade_grade::fetch_users_grades($grade_item, $second, $include_missing);
250
        }
251
 
252
        list($user_ids_cvs, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'uid0');
253
        $params['giid'] = $grade_item->id;
254
        $result = array();
255
        if ($grade_records = $DB->get_records_select('grade_grades', "itemid=:giid AND userid $user_ids_cvs", $params)) {
256
            foreach ($grade_records as $record) {
257
                $result[$record->userid] = new grade_grade($record, false);
258
            }
259
        }
260
        if ($include_missing) {
261
            foreach ($userids as $userid) {
262
                if (!array_key_exists($userid, $result)) {
263
                    $grade_grade = new grade_grade();
264
                    $grade_grade->userid = $userid;
265
                    $grade_grade->itemid = $grade_item->id;
266
                    $result[$userid] = $grade_grade;
267
                }
268
            }
269
        }
270
 
271
        return $result;
272
    }
273
 
274
    /**
275
     * Loads the grade_item object referenced by $this->itemid and saves it as $this->grade_item for easy access
276
     *
277
     * @return ?grade_item The grade_item instance referenced by $this->itemid
278
     */
279
    public function load_grade_item() {
280
        if (empty($this->itemid)) {
281
            debugging('Missing itemid');
282
            $this->grade_item = null;
283
            return null;
284
        }
285
 
286
        if (empty($this->grade_item)) {
287
            $this->grade_item = grade_item::fetch(array('id'=>$this->itemid));
288
 
289
        } else if ($this->grade_item->id != $this->itemid) {
290
            debugging('Itemid mismatch');
291
            $this->grade_item = grade_item::fetch(array('id'=>$this->itemid));
292
        }
293
 
294
        if (empty($this->grade_item)) {
295
            debugging("Missing grade item id $this->itemid", DEBUG_DEVELOPER);
296
        }
297
 
298
        return $this->grade_item;
299
    }
300
 
301
    /**
302
     * Is grading object editable?
303
     *
304
     * @return bool
305
     */
306
    public function is_editable() {
307
        if ($this->is_locked()) {
308
            return false;
309
        }
310
 
311
        $grade_item = $this->load_grade_item();
312
 
313
        if ($grade_item->gradetype == GRADE_TYPE_NONE) {
314
            return false;
315
        }
316
 
317
        if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
318
            return (bool)get_config('moodle', 'grade_overridecat');
319
        }
320
 
321
        return true;
322
    }
323
 
324
    /**
325
     * Check grade lock status. Uses both grade item lock and grade lock.
326
     * Internally any date in locked field (including future ones) means locked,
327
     * the date is stored for logging purposes only.
328
     *
329
     * @return bool True if locked, false if not
330
     */
331
    public function is_locked() {
332
        $this->load_grade_item();
333
        if (empty($this->grade_item)) {
334
            return !empty($this->locked);
335
        } else {
336
            return !empty($this->locked) or $this->grade_item->is_locked();
337
        }
338
    }
339
 
340
    /**
341
     * Checks if grade overridden
342
     *
343
     * @return bool True if grade is overriden
344
     */
345
    public function is_overridden() {
346
        return !empty($this->overridden);
347
    }
348
 
349
    /**
350
     * Returns timestamp of submission related to this grade, null if not submitted.
351
     *
352
     * @return int Timestamp
353
     */
354
    public function get_datesubmitted() {
355
        //TODO: HACK - create new fields (MDL-31379)
356
        return $this->timecreated;
357
    }
358
 
359
    /**
360
     * Returns the weight this grade contributed to the aggregated grade
361
     *
362
     * @return float|null
363
     */
364
    public function get_aggregationweight() {
365
        return $this->aggregationweight;
366
    }
367
 
368
    /**
369
     * Set aggregationweight.
370
     *
371
     * @param float $aggregationweight
372
     * @return void
373
     */
374
    public function set_aggregationweight($aggregationweight) {
375
        $this->aggregationweight = $aggregationweight;
376
        $this->update();
377
    }
378
 
379
    /**
380
     * Returns the info on how this value was used in the aggregated grade
381
     *
382
     * @return string One of 'dropped', 'excluded', 'novalue', 'used' or 'extra'
383
     */
384
    public function get_aggregationstatus() {
385
        return $this->aggregationstatus;
386
    }
387
 
388
    /**
389
     * Set aggregationstatus flag
390
     *
391
     * @param string $aggregationstatus
392
     * @return void
393
     */
394
    public function set_aggregationstatus($aggregationstatus) {
395
        $this->aggregationstatus = $aggregationstatus;
396
        $this->update();
397
    }
398
 
399
    /**
400
     * Returns the minimum and maximum number of points this grade is graded with respect to.
401
     *
402
     * @since  Moodle 2.8.7, 2.9.1
403
     * @return array A list containing, in order, the minimum and maximum number of points.
404
     */
405
    protected function get_grade_min_and_max() {
406
        global $CFG;
407
        $this->load_grade_item();
408
 
409
        // When the following setting is turned on we use the grade_grade raw min and max values.
410
        $minmaxtouse = grade_get_setting($this->grade_item->courseid, 'minmaxtouse', $CFG->grade_minmaxtouse);
411
 
412
        // Check to see if the gradebook is frozen. This allows grades to not be altered at all until a user verifies that they
413
        // wish to update the grades.
414
        $gradebookcalculationsfreeze = 'gradebook_calculations_freeze_' . $this->grade_item->courseid;
415
        // Gradebook is frozen, run through old code.
416
        if (isset($CFG->$gradebookcalculationsfreeze) && (int)$CFG->$gradebookcalculationsfreeze <= 20150627) {
417
            // Only aggregate items use separate min grades.
418
            if ($minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_GRADE || $this->grade_item->is_aggregate_item()) {
419
                return array($this->rawgrademin, $this->rawgrademax);
420
            } else {
421
                return array($this->grade_item->grademin, $this->grade_item->grademax);
422
            }
423
        } else {
424
            // Only aggregate items use separate min grades, unless they are calculated grade items.
425
            if (($this->grade_item->is_aggregate_item() && !$this->grade_item->is_calculated())
426
                    || $minmaxtouse == GRADE_MIN_MAX_FROM_GRADE_GRADE) {
427
                return array($this->rawgrademin, $this->rawgrademax);
428
            } else {
429
                return array($this->grade_item->grademin, $this->grade_item->grademax);
430
            }
431
        }
432
    }
433
 
434
    /**
435
     * Returns the minimum number of points this grade is graded with.
436
     *
437
     * @since  Moodle 2.8.7, 2.9.1
438
     * @return float The minimum number of points
439
     */
440
    public function get_grade_min() {
441
        list($min, $max) = $this->get_grade_min_and_max();
442
 
443
        return $min;
444
    }
445
 
446
    /**
447
     * Returns the maximum number of points this grade is graded with respect to.
448
     *
449
     * @since  Moodle 2.8.7, 2.9.1
450
     * @return float The maximum number of points
451
     */
452
    public function get_grade_max() {
453
        list($min, $max) = $this->get_grade_min_and_max();
454
 
455
        return $max;
456
    }
457
 
458
    /**
459
     * Returns timestamp when last graded, null if no grade present
460
     *
461
     * @return ?int
462
     */
463
    public function get_dategraded() {
464
        //TODO: HACK - create new fields (MDL-31379)
465
        if (is_null($this->finalgrade) and is_null($this->feedback)) {
466
            return null; // no grade == no date
467
        } else if ($this->overridden) {
468
            return $this->overridden;
469
        } else {
470
            return $this->timemodified;
471
        }
472
    }
473
 
474
    /**
475
     * Set the overridden status of grade
476
     *
477
     * @param bool $state requested overridden state
478
     * @param bool $refresh refresh grades from external activities if needed
479
     * @return bool true is db state changed
480
     */
481
    public function set_overridden($state, $refresh = true) {
482
        if (empty($this->overridden) and $state) {
483
            $this->overridden = time();
484
            $this->update(null, true);
485
            return true;
486
 
487
        } else if (!empty($this->overridden) and !$state) {
488
            $this->overridden = 0;
489
            $this->update(null, true);
490
 
491
            if ($refresh) {
492
                //refresh when unlocking
493
                $this->grade_item->refresh_grades($this->userid);
494
            }
495
 
496
            return true;
497
        }
498
        return false;
499
    }
500
 
501
    /**
502
     * Checks if grade excluded from aggregation functions
503
     *
504
     * @return bool True if grade is excluded from aggregation
505
     */
506
    public function is_excluded() {
507
        return !empty($this->excluded);
508
    }
509
 
510
    /**
511
     * Set the excluded status of grade
512
     *
513
     * @param bool $state requested excluded state
514
     * @return bool True is database state changed
515
     */
516
    public function set_excluded($state) {
517
        if (empty($this->excluded) and $state) {
518
            $this->excluded = time();
519
            $this->update();
520
            return true;
521
 
522
        } else if (!empty($this->excluded) and !$state) {
523
            $this->excluded = 0;
524
            $this->update();
525
            return true;
526
        }
527
        return false;
528
    }
529
 
530
    /**
531
     * Lock/unlock this grade.
532
     *
533
     * @param int $lockedstate 0, 1 or a timestamp int(10) after which date the item will be locked.
534
     * @param bool $cascade Ignored param
535
     * @param bool $refresh Refresh grades when unlocking
536
     * @return bool True if successful, false if can not set new lock state for grade
537
     */
538
    public function set_locked($lockedstate, $cascade=false, $refresh=true) {
539
        $this->load_grade_item();
540
 
541
        if ($lockedstate) {
542
            if ($this->grade_item->needsupdate) {
543
                //can not lock grade if final not calculated!
544
                return false;
545
            }
546
 
547
            $this->locked = time();
548
            $this->update();
549
 
550
            return true;
551
 
552
        } else {
553
            if (!empty($this->locked) and $this->locktime < time()) {
554
                //we have to reset locktime or else it would lock up again
555
                $this->locktime = 0;
556
            }
557
 
558
            // remove the locked flag
559
            $this->locked = 0;
560
            $this->update();
561
 
562
            if ($refresh and !$this->is_overridden()) {
563
                //refresh when unlocking and not overridden
564
                $this->grade_item->refresh_grades($this->userid);
565
            }
566
 
567
            return true;
568
        }
569
    }
570
 
571
    /**
572
     * Lock the grade if needed. Make sure this is called only when final grades are valid
573
     *
574
     * @param array $items array of all grade item ids
575
     * @return void
576
     */
577
    public static function check_locktime_all($items) {
578
        global $CFG, $DB;
579
 
580
        $now = time(); // no rounding needed, this is not supposed to be called every 10 seconds
581
        list($usql, $params) = $DB->get_in_or_equal($items);
582
        $params[] = $now;
583
        $rs = $DB->get_recordset_select('grade_grades', "itemid $usql AND locked = 0 AND locktime > 0 AND locktime < ?", $params);
584
        foreach ($rs as $grade) {
585
            $grade_grade = new grade_grade($grade, false);
586
            $grade_grade->locked = time();
587
            $grade_grade->update('locktime');
588
        }
589
        $rs->close();
590
    }
591
 
592
    /**
593
     * Set the locktime for this grade.
594
     *
595
     * @param int $locktime timestamp for lock to activate
596
     * @return void
597
     */
598
    public function set_locktime($locktime) {
599
        $this->locktime = $locktime;
600
        $this->update();
601
    }
602
 
603
    /**
604
     * Get the locktime for this grade.
605
     *
606
     * @return int $locktime timestamp for lock to activate
607
     */
608
    public function get_locktime() {
609
        $this->load_grade_item();
610
 
611
        $item_locktime = $this->grade_item->get_locktime();
612
 
613
        if (empty($this->locktime) or ($item_locktime and $item_locktime < $this->locktime)) {
614
            return $item_locktime;
615
 
616
        } else {
617
            return $this->locktime;
618
        }
619
    }
620
 
621
    /**
622
     * Check grade hidden status. Uses data from both grade item and grade.
623
     *
624
     * @return bool true if hidden, false if not
625
     */
626
    public function is_hidden() {
627
        $this->load_grade_item();
628
        if (empty($this->grade_item)) {
629
            return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time());
630
        } else {
631
            return $this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time()) or $this->grade_item->is_hidden();
632
        }
633
    }
634
 
635
    /**
636
     * Check grade hidden status. Uses data from both grade item and grade.
637
     *
638
     * @return bool true if hiddenuntil, false if not
639
     */
640
    public function is_hiddenuntil() {
641
        $this->load_grade_item();
642
 
643
        if ($this->hidden == 1 or $this->grade_item->hidden == 1) {
644
            return false; //always hidden
645
        }
646
 
647
        if ($this->hidden > 1 or $this->grade_item->hidden > 1) {
648
            return true;
649
        }
650
 
651
        return false;
652
    }
653
 
654
    /**
655
     * Check grade hidden status. Uses data from both grade item and grade.
656
     *
657
     * @return int 0 means visible, 1 hidden always, timestamp hidden until
658
     */
659
    public function get_hidden() {
660
        $this->load_grade_item();
661
 
662
        $item_hidden = $this->grade_item->get_hidden();
663
 
664
        if ($item_hidden == 1) {
665
            return 1;
666
 
667
        } else if ($item_hidden == 0) {
668
            return $this->hidden;
669
 
670
        } else {
671
            if ($this->hidden == 0) {
672
                return $item_hidden;
673
            } else if ($this->hidden == 1) {
674
                return 1;
675
            } else if ($this->hidden > $item_hidden) {
676
                return $this->hidden;
677
            } else {
678
                return $item_hidden;
679
            }
680
        }
681
    }
682
 
683
    /**
684
     * Set the hidden status of grade, 0 mean visible, 1 always hidden, number means date to hide until.
685
     *
686
     * @param int $hidden new hidden status
687
     * @param bool $cascade ignored
688
     */
689
    public function set_hidden($hidden, $cascade=false) {
690
       $this->hidden = $hidden;
691
       $this->update();
692
    }
693
 
694
    /**
695
     * Finds and returns a grade_grade instance based on params.
696
     *
697
     * @param array $params associative arrays varname=>value
698
     * @return grade_grade Returns a grade_grade instance or false if none found
699
     */
700
    public static function fetch($params) {
701
        return grade_object::fetch_helper('grade_grades', 'grade_grade', $params);
702
    }
703
 
704
    /**
705
     * Finds and returns all grade_grade instances based on params.
706
     *
707
     * @param array $params associative arrays varname=>value
708
     * @return array array of grade_grade instances or false if none found.
709
     */
710
    public static function fetch_all($params) {
711
        return grade_object::fetch_all_helper('grade_grades', 'grade_grade', $params);
712
    }
713
 
714
    /**
715
     * Given a float value situated between a source minimum and a source maximum, converts it to the
716
     * corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
717
     * for the formula :-)
718
     *
719
     * @param float $rawgrade
720
     * @param float $source_min
721
     * @param float $source_max
722
     * @param float $target_min
723
     * @param float $target_max
724
     * @return ?float Converted value
725
     */
726
    public static function standardise_score($rawgrade, $source_min, $source_max, $target_min, $target_max) {
727
        if (is_null($rawgrade)) {
728
          return null;
729
        }
730
 
731
        if ($source_max == $source_min or $target_min == $target_max) {
732
            // prevent division by 0
733
            return $target_max;
734
        }
735
 
736
        $factor = ($rawgrade - $source_min) / ($source_max - $source_min);
737
        $diff = $target_max - $target_min;
738
        $standardised_value = $factor * $diff + $target_min;
739
        return $standardised_value;
740
    }
741
 
742
    /**
743
     * Given an array like this:
744
     * $a = array(1=>array(2, 3),
745
     *            2=>array(4),
746
     *            3=>array(1),
747
     *            4=>array())
748
     * this function fully resolves the dependencies so each value will be an array of
749
     * the all items this item depends on and their dependencies (and their dependencies...).
750
     * It should not explode if there are circular dependencies.
751
     * The dependency depth array will list the number of branches in the tree above each leaf.
752
     *
753
     * @param array $dependson Array to flatten
754
     * @param array $dependencydepth Array of itemids => depth. Initially these should be all set to 1.
755
     * @return bool|null
756
     */
757
    protected static function flatten_dependencies_array(&$dependson, &$dependencydepth) {
758
        // Flatten the nested dependencies - this will handle recursion bombs because it removes duplicates.
759
        $somethingchanged = true;
760
        // First of all, delete any incorrect (not array or individual null) dependency, they aren't welcome.
761
        // TODO: Maybe we should report about this happening, it shouldn't if all dependencies are correct and consistent.
762
        foreach ($dependson as $itemid => $depends) {
763
            $depends = is_array($depends) ? $depends : []; // Only arrays are accepted.
764
            $dependson[$itemid] = array_filter($depends, function($val) { // Only not-null values are accepted.
765
                return !is_null($val);
766
            });
767
        }
768
        while ($somethingchanged) {
769
            $somethingchanged = false;
770
 
771
            foreach ($dependson as $itemid => $depends) {
772
                // Make a copy so we can tell if it changed.
773
                $before = $dependson[$itemid];
774
                foreach ($depends as $subitemid => $subdepends) {
775
                    $dependson[$itemid] = array_unique(array_merge($depends, $dependson[$subdepends] ?? []));
776
                    sort($dependson[$itemid], SORT_NUMERIC);
777
                }
778
                if ($before != $dependson[$itemid]) {
779
                    $somethingchanged = true;
780
                    if (!isset($dependencydepth[$itemid])) {
781
                        $dependencydepth[$itemid] = 1;
782
                    } else {
783
                        $dependencydepth[$itemid]++;
784
                    }
785
                }
786
            }
787
        }
788
    }
789
 
790
    /**
791
     * Return array of grade item ids that are either hidden or indirectly depend
792
     * on hidden grades, excluded grades are not returned.
793
     * THIS IS A REALLY BIG HACK! to be replaced by conditional aggregation of hidden grades in 2.0
794
     *
795
     * @param array $grade_grades all course grades of one user, & used for better internal caching
796
     * @param array $grade_items array of grade items, & used for better internal caching
797
     * @return array This is an array of following arrays:
798
     *      unknown => list of item ids that may be affected by hiding (with the ITEM ID as both the key and the value) - for BC with old gradereport plugins
799
     *      unknowngrades => list of item ids that may be affected by hiding (with the calculated grade as the value)
800
     *      altered => list of item ids that are definitely affected by hiding (with the calculated grade as the value)
801
     *      alteredgrademax => for each item in altered or unknown, the new value of the grademax
802
     *      alteredgrademin => for each item in altered or unknown, the new value of the grademin
803
     *      alteredgradestatus => for each item with a modified status - the value of the new status
804
     *      alteredgradeweight => for each item with a modified weight - the value of the new weight
805
     */
806
    public static function get_hiding_affected(&$grade_grades, &$grade_items) {
807
        global $CFG;
808
 
809
        if (count($grade_grades) !== count($grade_items)) {
810
            throw new \moodle_exception('invalidarraysize', 'debug', '', 'grade_grade::get_hiding_affected()!');
811
        }
812
 
813
        $dependson = array();
814
        $todo = array();
815
        $unknown = array();  // can not find altered
816
        $altered = array();  // altered grades
817
        $alteredgrademax = array();  // Altered grade max values.
818
        $alteredgrademin = array();  // Altered grade min values.
819
        $alteredaggregationstatus = array();  // Altered aggregation status.
820
        $alteredaggregationweight = array();  // Altered aggregation weight.
821
        $dependencydepth = array();
822
 
823
        $hiddenfound = false;
824
        foreach($grade_grades as $itemid=>$unused) {
825
            $grade_grade =& $grade_grades[$itemid];
826
            // We need the immediate dependencies of all every grade_item so we can calculate nested dependencies.
827
            $dependson[$grade_grade->itemid] = $grade_items[$grade_grade->itemid]->depends_on();
828
            if ($grade_grade->is_excluded()) {
829
                //nothing to do, aggregation is ok
830
                continue;
831
            } else if ($grade_grade->is_hidden()) {
832
                $hiddenfound = true;
833
                $altered[$grade_grade->itemid] = null;
834
                $alteredaggregationstatus[$grade_grade->itemid] = 'dropped';
835
                $alteredaggregationweight[$grade_grade->itemid] = 0;
836
            } else if ($grade_grade->is_overridden()) {
837
                // No need to recalculate overridden grades.
838
                continue;
839
            } else {
840
                if (!empty($dependson[$grade_grade->itemid])) {
841
                    $dependencydepth[$grade_grade->itemid] = 1;
842
                    $todo[] = $grade_grade->itemid;
843
                }
844
            }
845
        }
846
 
847
        // Flatten the dependency tree and count number of branches to each leaf.
848
        self::flatten_dependencies_array($dependson, $dependencydepth);
849
 
850
        if (!$hiddenfound) {
851
            return array('unknown' => array(),
852
                         'unknowngrades' => array(),
853
                         'altered' => array(),
854
                         'alteredgrademax' => array(),
855
                         'alteredgrademin' => array(),
856
                         'alteredaggregationstatus' => array(),
857
                         'alteredaggregationweight' => array());
858
        }
859
        // This line ensures that $dependencydepth has the same number of items as $todo.
860
        $dependencydepth = array_intersect_key($dependencydepth, array_flip($todo));
861
        // We need to resort the todo list by the dependency depth. This guarantees we process the leaves, then the branches.
862
        array_multisort($dependencydepth, $todo);
863
 
864
        $max = count($todo);
865
        $hidden_precursors = null;
866
        for($i=0; $i<$max; $i++) {
867
            $found = false;
868
            foreach($todo as $key=>$do) {
869
                $hidden_precursors = array_intersect($dependson[$do], array_keys($unknown));
870
                if ($hidden_precursors) {
871
                    // this item depends on hidden grade indirectly
872
                    $unknown[$do] = $grade_grades[$do]->finalgrade;
873
                    unset($todo[$key]);
874
                    $found = true;
875
                    continue;
876
 
877
                } else if (!array_intersect($dependson[$do], $todo)) {
878
                    $hidden_precursors = array_intersect($dependson[$do], array_keys($altered));
879
                    // If the dependency is a sum aggregation, we need to process it as if it had hidden items.
880
                    // The reason for this, is that the code will recalculate the maxgrade by removing ungraded
881
                    // items and accounting for 'drop x grades' and then stored back in our virtual grade_items.
882
                    // This recalculation is necessary because there will be a call to:
883
                    //              $grade_category->aggregate_values_and_adjust_bounds
884
                    // for the top level grade that will depend on knowing what that caclulated grademax is
885
                    // and it finds that value by checking the virtual grade_items.
886
                    $issumaggregate = false;
887
                    if ($grade_items[$do]->itemtype == 'category') {
888
                        $issumaggregate = $grade_items[$do]->load_item_category()->aggregation == GRADE_AGGREGATE_SUM;
889
                    }
890
                    if (!$hidden_precursors && !$issumaggregate) {
891
                        unset($todo[$key]);
892
                        $found = true;
893
                        continue;
894
 
895
                    } else {
896
                        // depends on altered grades - we should try to recalculate if possible
897
                        if ($grade_items[$do]->is_calculated() or
898
                            (!$grade_items[$do]->is_category_item() and !$grade_items[$do]->is_course_item()) or
899
                            ($grade_items[$do]->is_category_item() and $grade_items[$do]->is_locked())
900
                        ) {
901
                            // This is a grade item that is not a category or course and has been affected by grade hiding.
902
                            // Or a grade item that is a category and it is locked.
903
                            // I guess this means it is a calculation that needs to be recalculated.
904
                            $unknown[$do] = $grade_grades[$do]->finalgrade;
905
                            unset($todo[$key]);
906
                            $found = true;
907
                            continue;
908
 
909
                        } else {
910
                            // This is a grade category (or course).
911
                            $grade_category = $grade_items[$do]->load_item_category();
912
 
913
                            // Build a new list of the grades in this category.
914
                            $values = array();
915
                            $immediatedepends = $grade_items[$do]->depends_on();
916
                            foreach ($immediatedepends as $itemid) {
917
                                if (array_key_exists($itemid, $altered)) {
918
                                    //nulling an altered precursor
919
                                    $values[$itemid] = $altered[$itemid];
920
                                    if (is_null($values[$itemid])) {
921
                                        // This means this was a hidden grade item removed from the result.
922
                                        unset($values[$itemid]);
923
                                    }
924
                                } elseif (empty($values[$itemid])) {
925
                                    $values[$itemid] = $grade_grades[$itemid]->finalgrade;
926
                                }
927
                            }
928
 
929
                            foreach ($values as $itemid=>$value) {
930
                                if ($grade_grades[$itemid]->is_excluded()) {
931
                                    unset($values[$itemid]);
932
                                    $alteredaggregationstatus[$itemid] = 'excluded';
933
                                    $alteredaggregationweight[$itemid] = null;
934
                                    continue;
935
                                }
936
                                // The grade min/max may have been altered by hiding.
937
                                $grademin = $grade_items[$itemid]->grademin;
938
                                if (isset($alteredgrademin[$itemid])) {
939
                                    $grademin = $alteredgrademin[$itemid];
940
                                }
941
                                $grademax = $grade_items[$itemid]->grademax;
942
                                if (isset($alteredgrademax[$itemid])) {
943
                                    $grademax = $alteredgrademax[$itemid];
944
                                }
945
                                $values[$itemid] = grade_grade::standardise_score($value, $grademin, $grademax, 0, 1);
946
                            }
947
 
948
                            if ($grade_category->aggregateonlygraded) {
949
                                foreach ($values as $itemid=>$value) {
950
                                    if (is_null($value)) {
951
                                        unset($values[$itemid]);
952
                                        $alteredaggregationstatus[$itemid] = 'novalue';
953
                                        $alteredaggregationweight[$itemid] = null;
954
                                    }
955
                                }
956
                            } else {
957
                                foreach ($values as $itemid=>$value) {
958
                                    if (is_null($value)) {
959
                                        $values[$itemid] = 0;
960
                                    }
961
                                }
962
                            }
963
 
964
                            // limit and sort
965
                            $allvalues = $values;
966
                            $grade_category->apply_limit_rules($values, $grade_items);
967
 
968
                            $moredropped = array_diff($allvalues, $values);
969
                            foreach ($moredropped as $drop => $unused) {
970
                                $alteredaggregationstatus[$drop] = 'dropped';
971
                                $alteredaggregationweight[$drop] = null;
972
                            }
973
 
974
                            foreach ($values as $itemid => $val) {
975
                                if ($grade_category->is_extracredit_used() && ($grade_items[$itemid]->aggregationcoef > 0)) {
976
                                    $alteredaggregationstatus[$itemid] = 'extra';
977
                                }
978
                            }
979
 
980
                            asort($values, SORT_NUMERIC);
981
 
982
                            // let's see we have still enough grades to do any statistics
983
                            if (count($values) == 0) {
984
                                // not enough attempts yet
985
                                $altered[$do] = null;
986
                                unset($todo[$key]);
987
                                $found = true;
988
                                continue;
989
                            }
990
 
991
                            $usedweights = array();
992
                            $adjustedgrade = $grade_category->aggregate_values_and_adjust_bounds($values, $grade_items, $usedweights);
993
 
994
                            // recalculate the rawgrade back to requested range
995
                            $finalgrade = grade_grade::standardise_score($adjustedgrade['grade'],
996
                                                                         0,
997
                                                                         1,
998
                                                                         $adjustedgrade['grademin'],
999
                                                                         $adjustedgrade['grademax']);
1000
 
1001
                            foreach ($usedweights as $itemid => $weight) {
1002
                                if (!isset($alteredaggregationstatus[$itemid])) {
1003
                                    $alteredaggregationstatus[$itemid] = 'used';
1004
                                }
1005
                                $alteredaggregationweight[$itemid] = $weight;
1006
                            }
1007
 
1008
                            $finalgrade = $grade_items[$do]->bounded_grade($finalgrade);
1009
                            $alteredgrademin[$do] = $adjustedgrade['grademin'];
1010
                            $alteredgrademax[$do] = $adjustedgrade['grademax'];
1011
                            // We need to muck with the "in-memory" grade_items records so
1012
                            // that subsequent calculations will use the adjusted grademin and grademax.
1013
                            $grade_items[$do]->grademin = $adjustedgrade['grademin'];
1014
                            $grade_items[$do]->grademax = $adjustedgrade['grademax'];
1015
 
1016
                            $altered[$do] = $finalgrade;
1017
                            unset($todo[$key]);
1018
                            $found = true;
1019
                            continue;
1020
                        }
1021
                    }
1022
                }
1023
            }
1024
            if (!$found) {
1025
                break;
1026
            }
1027
        }
1028
 
1029
        return array('unknown' => array_combine(array_keys($unknown), array_keys($unknown)), // Left for BC in case some gradereport plugins expect it.
1030
                     'unknowngrades' => $unknown,
1031
                     'altered' => $altered,
1032
                     'alteredgrademax' => $alteredgrademax,
1033
                     'alteredgrademin' => $alteredgrademin,
1034
                     'alteredaggregationstatus' => $alteredaggregationstatus,
1035
                     'alteredaggregationweight' => $alteredaggregationweight);
1036
    }
1037
 
1038
    /**
1039
     * Returns true if the grade's value is superior or equal to the grade item's gradepass value, false otherwise.
1040
     *
1041
     * @param grade_item $grade_item An optional grade_item of which gradepass value we can use, saves having to load the grade_grade's grade_item
1042
     * @return ?bool
1043
     */
1044
    public function is_passed($grade_item = null) {
1045
        if (empty($grade_item)) {
1046
            if (!isset($this->grade_item)) {
1047
                $this->load_grade_item();
1048
            }
1049
        } else {
1050
            $this->grade_item = $grade_item;
1051
            $this->itemid = $grade_item->id;
1052
        }
1053
 
1054
        // Return null if finalgrade is null
1055
        if (is_null($this->finalgrade)) {
1056
            return null;
1057
        }
1058
 
1059
        // Return null if gradepass == grademin, gradepass is null, or grade item is a scale and gradepass is 0.
1060
        if (is_null($this->grade_item->gradepass)) {
1061
            return null;
1062
        } else if ($this->grade_item->gradepass == $this->grade_item->grademin) {
1063
            return null;
1064
        } else if ($this->grade_item->gradetype == GRADE_TYPE_SCALE && !grade_floats_different($this->grade_item->gradepass, 0.0)) {
1065
            return null;
1066
        }
1067
 
1068
        return $this->finalgrade >= $this->grade_item->gradepass;
1069
    }
1070
 
1071
    /**
1072
     * In addition to update() as defined in grade_object rounds the float numbers using php function,
1073
     * the reason is we need to compare the db value with computed number to skip updates if possible.
1074
     *
1075
     * @param string $source from where was the object inserted (mod/forum, manual, etc.)
1076
     * @param bool $isbulkupdate If bulk grade update is happening.
1077
     * @return bool success
1078
     */
1079
    public function update($source=null, $isbulkupdate = false) {
1080
        $this->rawgrade = grade_floatval($this->rawgrade);
1081
        $this->finalgrade = grade_floatval($this->finalgrade);
1082
        $this->rawgrademin = grade_floatval($this->rawgrademin);
1083
        $this->rawgrademax = grade_floatval($this->rawgrademax);
1084
        return parent::update($source, $isbulkupdate);
1085
    }
1086
 
1087
 
1088
    /**
1089
     * Handles adding feedback files in the gradebook.
1090
     *
1091
     * @param int|null $historyid
1092
     */
1441 ariadna 1093
    protected function add_feedback_files(?int $historyid = null) {
1 efrain 1094
        global $CFG;
1095
 
1096
        // We only support feedback files for modules atm.
1097
        if ($this->grade_item && $this->grade_item->is_external_item()) {
1098
            $context = $this->get_context();
1099
            $this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);
1100
 
1101
            if (empty($CFG->disablegradehistory) && $historyid) {
1102
                $this->copy_feedback_files($context, GRADE_HISTORY_FEEDBACK_FILEAREA, $historyid);
1103
            }
1104
        }
1105
 
1106
        return $this->id;
1107
    }
1108
 
1109
    /**
1110
     * Handles updating feedback files in the gradebook.
1111
     *
1112
     * @param int|null $historyid
1113
     */
1441 ariadna 1114
    protected function update_feedback_files(?int $historyid = null) {
1 efrain 1115
        global $CFG;
1116
 
1117
        // We only support feedback files for modules atm.
1118
        if ($this->grade_item && $this->grade_item->is_external_item()) {
1119
            $context = $this->get_context();
1120
 
1121
            $fs = new file_storage();
1122
            $fs->delete_area_files($context->id, GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA, $this->id);
1123
 
1124
            $this->copy_feedback_files($context, GRADE_FEEDBACK_FILEAREA, $this->id);
1125
 
1126
            if (empty($CFG->disablegradehistory) && $historyid) {
1127
                $this->copy_feedback_files($context, GRADE_HISTORY_FEEDBACK_FILEAREA, $historyid);
1128
            }
1129
        }
1130
 
1131
        return true;
1132
    }
1133
 
1134
    /**
1135
     * Handles deleting feedback files in the gradebook.
1136
     */
1137
    protected function delete_feedback_files() {
1138
        // We only support feedback files for modules atm.
1139
        if ($this->grade_item && $this->grade_item->is_external_item()) {
1140
            $context = $this->get_context();
1141
 
1142
            $fs = new file_storage();
1143
            $fs->delete_area_files($context->id, GRADE_FILE_COMPONENT, GRADE_FEEDBACK_FILEAREA, $this->id);
1144
 
1145
            // Grade history only gets deleted when we delete the whole grade item.
1146
        }
1147
 
1148
        return true;
1149
    }
1150
 
1151
    /**
1152
     * Deletes the grade_grade instance from the database.
1153
     *
1154
     * @param string $source The location the deletion occurred (mod/forum, manual, etc.).
1155
     * @return bool Returns true if the deletion was successful, false otherwise.
1156
     */
1157
    public function delete($source = null) {
1158
        global $DB;
1159
        try {
1160
            $transaction = $DB->start_delegated_transaction();
1161
            $success = parent::delete($source);
1162
            // If the grade was deleted successfully trigger a grade_deleted event.
1163
            if ($success && !empty($this->grade_item)) {
1164
                $this->load_grade_item();
1165
                \core\event\grade_deleted::create_from_grade($this)->trigger();
1166
            }
1167
            $transaction->allow_commit();
1168
        } catch (Exception $e) {
1169
            $transaction->rollback($e);
1170
        }
1171
        return $success;
1172
    }
1173
 
1174
    /**
1175
     * Used to notify the completion system (if necessary) that a user's grade
1176
     * has changed, and clear up a possible score cache.
1177
     *
1178
     * @param bool $deleted True if grade was actually deleted
1179
     * @param bool $isbulkupdate If bulk grade update is happening.
1180
     */
1181
    protected function notify_changed($deleted, $isbulkupdate = false) {
1182
        global $CFG;
1183
 
1184
        // Condition code may cache the grades for conditional availability of
1185
        // modules or sections. (This code should use a hook for communication
1186
        // with plugin, but hooks are not implemented at time of writing.)
1187
        if (!empty($CFG->enableavailability) && class_exists('\availability_grade\callbacks')) {
1188
            \availability_grade\callbacks::grade_changed($this->userid);
1189
        }
1190
 
1191
        require_once($CFG->libdir.'/completionlib.php');
1192
 
1193
        // Bail out immediately if completion is not enabled for site (saves loading
1194
        // grade item & requiring the restore stuff).
1195
        if (!completion_info::is_enabled_for_site()) {
1196
            return;
1197
        }
1198
 
1199
        // Ignore during restore, as completion data will be updated anyway and
1200
        // doing it now will result in incorrect dates (it will say they got the
1201
        // grade completion now, instead of the correct time).
1202
        if (class_exists('restore_controller', false) && restore_controller::is_executing()) {
1203
            return;
1204
        }
1205
 
1206
        // Load information about grade item, exit if the grade item is missing.
1207
        if (!$this->load_grade_item()) {
1208
            return;
1209
        }
1210
 
1211
        // Only course-modules have completion data
1212
        if ($this->grade_item->itemtype!='mod') {
1213
            return;
1214
        }
1215
 
1216
        // Use $COURSE if available otherwise get it via item fields
1217
        $course = get_course($this->grade_item->courseid, false);
1218
 
1219
        // Bail out if completion is not enabled for course
1220
        $completion = new completion_info($course);
1221
        if (!$completion->is_enabled()) {
1222
            return;
1223
        }
1224
 
1225
        // Get course-module
1226
        $cm = get_coursemodule_from_instance($this->grade_item->itemmodule,
1227
              $this->grade_item->iteminstance, $this->grade_item->courseid);
1228
        // If the course-module doesn't exist, display a warning...
1229
        if (!$cm) {
1230
            // ...unless the grade is being deleted in which case it's likely
1231
            // that the course-module was just deleted too, so that's okay.
1232
            if (!$deleted) {
1233
                debugging("Couldn't find course-module for module '" .
1234
                        $this->grade_item->itemmodule . "', instance '" .
1235
                        $this->grade_item->iteminstance . "', course '" .
1236
                        $this->grade_item->courseid . "'");
1237
            }
1238
            return;
1239
        }
1240
 
1241
        // Pass information on to completion system
1242
        $completion->inform_grade_changed($cm, $this->grade_item, $this, $deleted, $isbulkupdate);
1243
    }
1244
 
1245
    /**
1246
     * Get some useful information about how this grade_grade is reflected in the aggregation
1247
     * for the grade_category. For example this could be an extra credit item, and it could be
1248
     * dropped because it's in the X lowest or highest.
1249
     *
1250
     * @return array(status, weight) - A keyword and a numerical weight that represents how this grade was included in the aggregation.
1251
     */
1252
    function get_aggregation_hint() {
1253
        return array('status' => $this->get_aggregationstatus(),
1254
                     'weight' => $this->get_aggregationweight());
1255
    }
1256
 
1257
    /**
1258
     * Handles copying feedback files to a specified gradebook file area.
1259
     *
1260
     * @param context $context
1261
     * @param string $filearea
1262
     * @param int $itemid
1263
     */
1264
    private function copy_feedback_files(context $context, string $filearea, int $itemid) {
1265
        if ($this->feedbackfiles) {
1266
            $filestocopycontextid = $this->feedbackfiles['contextid'];
1267
            $filestocopycomponent = $this->feedbackfiles['component'];
1268
            $filestocopyfilearea = $this->feedbackfiles['filearea'];
1269
            $filestocopyitemid = $this->feedbackfiles['itemid'];
1270
 
1271
            $fs = new file_storage();
1272
            if ($filestocopy = $fs->get_area_files($filestocopycontextid, $filestocopycomponent, $filestocopyfilearea,
1273
                    $filestocopyitemid)) {
1274
                foreach ($filestocopy as $filetocopy) {
1275
                    $destination = [
1276
                        'contextid' => $context->id,
1277
                        'component' => GRADE_FILE_COMPONENT,
1278
                        'filearea' => $filearea,
1279
                        'itemid' => $itemid
1280
                    ];
1281
                    $fs->create_file_from_storedfile($destination, $filetocopy);
1282
                }
1283
            }
1284
        }
1285
    }
1286
 
1287
    /**
1288
     * Determine the correct context for this grade_grade.
1289
     *
1290
     * @return context
1291
     */
1292
    public function get_context() {
1293
        $this->load_grade_item();
1294
        return $this->grade_item->get_context();
1295
    }
1441 ariadna 1296
 
1297
    /**
1298
     * Whether the penalty is applied to this final grade.
1299
     *
1300
     * @return bool whether penalty is applied
1301
     */
1302
    public function is_penalty_applied_to_final_grade(): bool {
1303
        // Return false if the grade is overridden.
1304
        if ($this->is_overridden()) {
1305
            return false;
1306
        }
1307
        return $this->deductedmark > 0;
1308
    }
1 efrain 1309
}