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