d3d9f7f09b2461cacc1e856291507c66d0ed6b0b
[moodle.git] / grade / report / singleview / classes / local / ui / finalgrade.php
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/>.
17 /**
18  * UI element representing the finalgrade column.
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  */
25 namespace gradereport_singleview\local\ui;
27 defined('MOODLE_INTERNAL') || die;
29 use stdClass;
30 /**
31  * UI element representing the finalgrade column.
32  *
33  * @package   gradereport_singleview
34  * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
35  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class finalgrade extends grade_attribute_format implements unique_value, be_disabled {
39     /** @var string $name Name of this input */
40     public $name = 'finalgrade';
42     /**
43      * Get the value for this input.
44      *
45      * @return string The value based on the grade_grade.
46      */
47     public function get_value() {
48         $this->label = $this->grade->grade_item->itemname;
50         $isoverridden = $this->grade->is_overridden();
51         if (!empty($isoverridden)) {
52             $val = $this->grade->finalgrade;
53         } else {
54             $val = $this->grade->rawgrade;
55         }
57         if ($this->grade->grade_item->scaleid) {
58             return $val ? (int)$val : -1;
59         } else {
60             return $val ? format_float($val, $this->grade->grade_item->get_decimals()) : '';
61         }
62     }
64     /**
65      * Get the label for this input.
66      *
67      * @return string The label for this form input.
68      */
69     public function get_label() {
70         if (!isset($this->grade->label)) {
71             $this->grade->label = '';
72         }
73         return $this->grade->label;
74     }
76     /**
77      * Is this input field disabled.
78      *
79      * @return bool Set disabled on this input or not.
80      */
81     public function is_disabled() {
82         $locked = 0;
83         $gradeitemlocked = 0;
84         $overridden = 0;
86         // Disable editing if grade item or grade score is locked
87         // if any of these items are set,  then we will disable editing
88         // at some point, we might want to show the reason for the lock
89         // this code could be simplified, but its more readable for steve's little mind.
91         if (!empty($this->grade->locked)) {
92             $locked = 1;
93         }
94         if (!empty($this->grade->grade_item->locked)) {
95             $gradeitemlocked = 1;
96         }
97         if ($this->grade->grade_item->is_overridable_item() and !$this->grade->is_overridden()) {
98             $overridden = 1;
99         }
100         return ($locked || $gradeitemlocked || $overridden);
101     }
103     /**
104      * Create the element for this column.
105      *
106      * @return element
107      */
108     public function determine_format() {
109         if ($this->grade->grade_item->load_scale()) {
110             $scale = $this->grade->grade_item->load_scale();
112             $options = array(-1 => get_string('nograde'));
114             foreach ($scale->scale_items as $i => $name) {
115                 $options[$i + 1] = $name;
116             }
118             return new dropdown_attribute(
119                 $this->get_name(),
120                 $options,
121                 $this->get_label(),
122                 $this->get_value(),
123                 $this->is_disabled(),
124                 $this->get_tabindex()
125             );
126         } else {
127             return new text_attribute(
128                 $this->get_name(),
129                 $this->get_value(),
130                 $this->get_label(),
131                 $this->is_disabled(),
132                 $this->get_tabindex()
133             );
134         }
135     }
137     /**
138      * Save the altered value for this field.
139      *
140      * @param string $value The new value.
141      * @return string Any error string
142      */
143     public function set($value) {
144         global $DB;
146         $userid = $this->grade->userid;
147         $gradeitem = $this->grade->grade_item;
149         $feedback = false;
150         $feedbackformat = false;
151         if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
152             if ($value == -1) {
153                 $finalgrade = null;
154             } else {
155                 $finalgrade = $value;
156             }
157         } else {
158             $finalgrade = unformat_float($value);
159         }
161         $errorstr = '';
162         if ($finalgrade) {
163             $bounded = $gradeitem->bounded_grade($finalgrade);
164             if ($bounded > $finalgrade) {
165                 $errorstr = 'lessthanmin';
166             } else if ($bounded < $finalgrade) {
167                 $errorstr = 'morethanmax';
168             }
169         }
171         if ($errorstr) {
172             $user = $DB->get_record('user', array('id' => $userid), 'id, firstname, alternatename, lastname');
173             $gradestr = new stdClass;
174             if (!empty($user->alternatename)) {
175                 $gradestr->username = $user->alternatename . ' (' . $user->firstname . ') ' . $user->lastname;
176             } else {
177                 $gradestr->username = $user->firstname . ' ' . $user->lastname;
178             }
179             $gradestr->itemname = $this->grade->grade_item->get_name();
180             $errorstr = get_string($errorstr, 'grades', $gradestr);
181         }
183         $gradeitem->update_final_grade($userid, $finalgrade, 'singleview', $feedback, FORMAT_MOODLE);
184         return $errorstr;
185     }