| 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 |  * An override grade checkbox element
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package   gradereport_singleview
 | 
        
           |  |  | 21 |  * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
 | 
        
           |  |  | 22 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | namespace gradereport_singleview\local\ui;
 | 
        
           |  |  | 26 |   | 
        
           | 1441 | ariadna | 27 | use context_course;
 | 
        
           |  |  | 28 |   | 
        
           | 1 | efrain | 29 | defined('MOODLE_INTERNAL') || die;
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 |  * An override grade checkbox element
 | 
        
           |  |  | 33 |  *
 | 
        
           |  |  | 34 |  * @package   gradereport_singleview
 | 
        
           |  |  | 35 |  * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
 | 
        
           |  |  | 36 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 37 |  */
 | 
        
           |  |  | 38 | class override extends grade_attribute_format implements be_checked, be_disabled, be_readonly {
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * The name for this input
 | 
        
           |  |  | 42 |      * @var string $name
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     public $name = 'override';
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |     /**
 | 
        
           |  |  | 47 |      * Is this input checked
 | 
        
           |  |  | 48 |      *
 | 
        
           |  |  | 49 |      * @return bool
 | 
        
           |  |  | 50 |      */
 | 
        
           |  |  | 51 |     public function is_checked(): bool {
 | 
        
           |  |  | 52 |         return $this->grade->is_overridden();
 | 
        
           |  |  | 53 |     }
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |     /**
 | 
        
           |  |  | 56 |      * Is this input disabled
 | 
        
           |  |  | 57 |      *
 | 
        
           |  |  | 58 |      * @return bool
 | 
        
           |  |  | 59 |      */
 | 
        
           |  |  | 60 |     public function is_disabled(): bool {
 | 
        
           |  |  | 61 |         $lockedgrade = $lockedgradeitem = 0;
 | 
        
           |  |  | 62 |         if (!empty($this->grade->locked)) {
 | 
        
           |  |  | 63 |             $lockedgrade = 1;
 | 
        
           |  |  | 64 |         }
 | 
        
           |  |  | 65 |         if (!empty($this->grade->grade_item->locked)) {
 | 
        
           |  |  | 66 |             $lockedgradeitem = 1;
 | 
        
           |  |  | 67 |         }
 | 
        
           |  |  | 68 |         return ($lockedgrade || $lockedgradeitem);
 | 
        
           |  |  | 69 |     }
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |     /**
 | 
        
           |  |  | 72 |      * Return true if this is read-only.
 | 
        
           |  |  | 73 |      *
 | 
        
           |  |  | 74 |      * @return bool
 | 
        
           |  |  | 75 |      */
 | 
        
           |  |  | 76 |     public function is_readonly(): bool {
 | 
        
           |  |  | 77 |         global $USER;
 | 
        
           |  |  | 78 |         return empty($USER->editing);
 | 
        
           |  |  | 79 |     }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 |     /**
 | 
        
           |  |  | 82 |      * Get the label for this form element.
 | 
        
           |  |  | 83 |      *
 | 
        
           |  |  | 84 |      * @return string
 | 
        
           |  |  | 85 |      */
 | 
        
           |  |  | 86 |     public function get_label(): string {
 | 
        
           |  |  | 87 |         if (!isset($this->grade->label)) {
 | 
        
           |  |  | 88 |             $this->grade->label = '';
 | 
        
           |  |  | 89 |         }
 | 
        
           |  |  | 90 |         return $this->grade->label;
 | 
        
           |  |  | 91 |     }
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |     /**
 | 
        
           |  |  | 94 |      * Generate the element for this form input.
 | 
        
           |  |  | 95 |      *
 | 
        
           |  |  | 96 |      * @return element
 | 
        
           |  |  | 97 |      */
 | 
        
           |  |  | 98 |     public function determine_format(): element {
 | 
        
           | 1441 | ariadna | 99 |         // If the grade is hidden and the user does not have permission to view hidden grades,
 | 
        
           |  |  | 100 |         // then we don't show the override checkbox.
 | 
        
           |  |  | 101 |         $allowhiddenoverride = (!$this->grade->is_hidden() && !$this->grade->grade_item->is_hidden()) ||
 | 
        
           |  |  | 102 |             has_capability('moodle/grade:viewhidden', context_course::instance($this->grade->grade_item->courseid));
 | 
        
           |  |  | 103 |         if (!$allowhiddenoverride || !$this->grade->grade_item->is_overridable_item()) {
 | 
        
           | 1 | efrain | 104 |             return new empty_element();
 | 
        
           |  |  | 105 |         }
 | 
        
           |  |  | 106 |         return new checkbox_attribute(
 | 
        
           |  |  | 107 |             $this->get_name(),
 | 
        
           |  |  | 108 |             $this->get_label(),
 | 
        
           |  |  | 109 |             $this->is_checked(),
 | 
        
           |  |  | 110 |             $this->is_disabled(),
 | 
        
           |  |  | 111 |             $this->is_readonly()
 | 
        
           |  |  | 112 |         );
 | 
        
           |  |  | 113 |     }
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |     /**
 | 
        
           |  |  | 116 |      * Save the modified value of this form element.
 | 
        
           |  |  | 117 |      *
 | 
        
           |  |  | 118 |      * @param string $value The new value to set
 | 
        
           |  |  | 119 |      * @return mixed string|false Any error message
 | 
        
           |  |  | 120 |      */
 | 
        
           |  |  | 121 |     public function set($value) {
 | 
        
           |  |  | 122 |         if (empty($this->grade->id)) {
 | 
        
           |  |  | 123 |             return false;
 | 
        
           |  |  | 124 |         }
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |         $state = !($value == 0);
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |         $this->grade->set_overridden($state);
 | 
        
           |  |  | 129 |         $this->grade->grade_item->force_regrading();
 | 
        
           |  |  | 130 |         return false;
 | 
        
           |  |  | 131 |     }
 | 
        
           |  |  | 132 | }
 |