Proyectos de Subversion Moodle

Rev

| 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
 * 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
 */
24
 
25
namespace gradereport_singleview\local\ui;
26
 
27
defined('MOODLE_INTERNAL') || die;
28
 
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, be_readonly {
38
 
39
    /**
40
     * Name of this input
41
     * @var string $name
42
     */
43
    public $name = 'finalgrade';
44
 
45
    /**
46
     * Get the value for this input.
47
     *
48
     * @return null|string The value based on the grade_grade.
49
     */
50
    public function get_value(): ?string {
51
        $this->label = $this->grade->grade_item->itemname;
52
 
53
        $val = $this->grade->finalgrade;
54
        if ($this->grade->grade_item->scaleid) {
55
            return $val ? (int)$val : -1;
56
        } else {
57
            return $val ? format_float($val, $this->grade->grade_item->get_decimals()) : '';
58
        }
59
    }
60
 
61
    /**
62
     * Get the label for this input.
63
     *
64
     * @return string The label for this form input.
65
     */
66
    public function get_label(): string {
67
        if (!isset($this->grade->label)) {
68
            $this->grade->label = '';
69
        }
70
        return $this->grade->label;
71
    }
72
 
73
    /**
74
     * Is this input field disabled.
75
     *
76
     * @return bool Set disabled on this input or not.
77
     */
78
    public function is_disabled(): bool {
79
        $locked = 0;
80
        $gradeitemlocked = 0;
81
        $overridden = 0;
82
 
83
        // Disable editing if grade item or grade score is locked
84
        // if any of these items are set,  then we will disable editing
85
        // at some point, we might want to show the reason for the lock
86
        // this code could be simplified, but its more readable for steve's little mind.
87
 
88
        if (!empty($this->grade->locked)) {
89
            $locked = 1;
90
        }
91
        if (!empty($this->grade->grade_item->locked)) {
92
            $gradeitemlocked = 1;
93
        }
94
        if ($this->grade->grade_item->is_overridable_item() and !$this->grade->is_overridden()) {
95
            $overridden = 1;
96
        }
97
        return ($locked || $gradeitemlocked || $overridden);
98
    }
99
 
100
    /**
101
     * Return true if this is read-only.
102
     *
103
     * @return bool
104
     */
105
    public function is_readonly(): bool {
106
        global $USER;
107
        return empty($USER->editing);
108
    }
109
 
110
    /**
111
     * Create the element for this column.
112
     *
113
     * @return element
114
     */
115
    public function determine_format(): element {
116
        global $CFG;
117
 
118
        if ($this->grade->grade_item->load_scale()) {
119
            $scale = $this->grade->grade_item->load_scale();
120
 
121
            $options = [-1 => get_string('nograde')];
122
 
123
            foreach ($scale->scale_items as $i => $name) {
124
                $options[$i + 1] = $name;
125
            }
126
 
127
            return new dropdown_attribute(
128
                $this->get_name(),
129
                $options,
130
                $this->get_label(),
131
                $this->get_value(),
132
                $this->is_disabled(),
133
                $this->is_readonly()
134
            );
135
        } else {
136
            $textattribute = new text_attribute(
137
                $this->get_name(),
138
                $this->get_value(),
139
                $this->get_label(),
140
                $this->is_disabled(),
141
                $this->is_readonly()
142
            );
143
 
144
            // Set min/max attributes, if applicable.
145
            $textattribute->set_type('number');
146
            $gradeitem = $this->grade->grade_item;
147
            $decimals = $gradeitem->get_decimals();
148
 
149
            // Min attribute.
150
            $minvalue = null;
151
            if (isset($gradeitem->grademin)) {
152
                $minvalue = format_float($gradeitem->grademin, $decimals);
153
            }
154
            $textattribute->set_min($minvalue);
155
 
156
            // Max attribute.
157
            $maxvalue = null;
158
            if (isset($gradeitem->grademax) && empty($CFG->unlimitedgrades)) {
159
                $maxvalue = format_float($gradeitem->grademax, $decimals);
160
            }
161
            $textattribute->set_max($maxvalue);
162
 
163
            return $textattribute;
164
        }
165
    }
166
 
167
    /**
168
     * Save the altered value for this field.
169
     *
170
     * @param string $value The new value.
171
     * @return string Any error string
172
     */
173
    public function set($value) {
174
        $userid = $this->grade->userid;
175
        $gradeitem = $this->grade->grade_item;
176
 
177
        $feedback = false;
178
        $feedbackformat = false;
179
        if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
180
            $value = (int)unformat_float($value);
181
            if ($value == -1) {
182
                $finalgrade = null;
183
            } else {
184
                $finalgrade = $value;
185
            }
186
        } else {
187
            $finalgrade = unformat_float($value);
188
        }
189
 
190
        $errorstr = '';
191
        if ($finalgrade) {
192
            $bounded = $gradeitem->bounded_grade($finalgrade);
193
            if ($bounded > $finalgrade) {
194
                $errorstr = 'lessthanmin';
195
            } else if ($bounded < $finalgrade) {
196
                $errorstr = 'morethanmax';
197
            }
198
        }
199
 
200
        if ($errorstr) {
201
            $user = get_complete_user_data('id', $userid);
202
            $gradestr = new stdClass;
203
            if (has_capability('moodle/site:viewfullnames', \context_course::instance($gradeitem->courseid))) {
204
                $gradestr->username = fullname($user, true);
205
            } else {
206
                $gradestr->username = fullname($user);
207
            }
208
            $gradestr->itemname = $this->grade->grade_item->get_name();
209
            $errorstr = get_string($errorstr, 'grades', $gradestr);
210
            return $errorstr;
211
        }
212
 
213
        // Only update grades if there are no errors.
214
        $gradeitem->update_final_grade($userid, $finalgrade, 'singleview', $feedback, FORMAT_MOODLE,
215
            null, null, true);
216
        return '';
217
    }
218
}