MDL-47501 gradebook: Add an indication that the grade letters was overridden
[moodle.git] / grade / edit / letter / index.php
CommitLineData
e060e33d 1<?php
e060e33d 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/>.
8ad36f4c 16
1c1f64a2 17/**
18 * List of grade letters.
19 *
a153c9f2 20 * @package core_grades
1c1f64a2 21 * @copyright 2008 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
284abb09 25require_once '../../../config.php';
26require_once $CFG->dirroot.'/grade/lib.php';
27require_once $CFG->libdir.'/gradelib.php';
28
54caa598 29$contextid = optional_param('id', SYSCONTEXTID, PARAM_INT);
284abb09 30$action = optional_param('action', '', PARAM_ALPHA);
54caa598 31$edit = optional_param('edit', false, PARAM_BOOL); //are we editing?
284abb09 32
54caa598 33$PAGE->set_url('/grade/edit/letter/index.php', array('id' => $contextid));
1c1f64a2 34
54caa598
AD
35list($context, $course, $cm) = get_context_info_array($contextid);
36$contextid = null;//now we have a context object throw away the $contextid from the params
9376f5a6 37
54caa598
AD
38//if viewing
39if (!$edit) {
40 if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:manageletters', $context)) {
41 print_error('nopermissiontoviewletergrade');
42 }
43} else {//else we're editing
44 require_capability('moodle/grade:manageletters', $context);
9376f5a6 45}
284abb09 46
54caa598
AD
47$returnurl = null;
48$editparam = null;
49if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) {
50 require_once $CFG->libdir.'/adminlib.php';
51 require_login();
52
53 admin_externalpage_setup('letters');
653f91f9 54
54caa598
AD
55 $admin = true;
56 $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
57 $editparam = '?edit=1';
58} else if ($context->contextlevel == CONTEXT_COURSE) {
59
60 $PAGE->set_pagelayout('standard');//calling this here to make blocks display
653f91f9 61
54caa598
AD
62 require_login($context->instanceid, false, $cm);
63
64 $admin = false;
65 $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->id;
66 $editparam = '&edit=1';
67
68 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id));
69} else {
70 print_error('invalidcourselevel');
71}
284abb09 72
73$strgrades = get_string('grades');
74$pagename = get_string('letters', 'grades');
75
284abb09 76$letters = grade_get_letters($context);
54caa598
AD
77$num = count($letters) + 3;
78
af14ef65
GPL
79$override = $DB->record_exists('grade_letters', array('contextid' => $context->id));
80
54caa598
AD
81//if were viewing the letters
82if (!$edit) {
83
84 $data = array();
85
86 $max = 100;
87 foreach($letters as $boundary=>$letter) {
88 $line = array();
89 $line[] = format_float($max,2).' %';
90 $line[] = format_float($boundary,2).' %';
91 $line[] = format_string($letter);
92 $data[] = $line;
93 $max = $boundary - 0.01;
94 }
95
96 print_grade_page_head($COURSE->id, 'letter', 'view', get_string('gradeletters', 'grades'));
97
af14ef65
GPL
98 if (!empty($override)) {
99 echo $OUTPUT->notification(get_string('gradeletteroverridden', 'grades'), 'notifymessage');
100 }
101
54caa598
AD
102 $stredit = get_string('editgradeletters', 'grades');
103 $editlink = html_writer::nonempty_tag('div', html_writer::link($returnurl.$editparam, $stredit), array('class'=>'mdl-align'));
104 echo $editlink;
105
106 $table = new html_table();
107 $table->head = array(get_string('max', 'grades'), get_string('min', 'grades'), get_string('letter', 'grades'));
108 $table->size = array('30%', '30%', '40%');
109 $table->align = array('left', 'left', 'left');
110 $table->width = '30%';
111 $table->data = $data;
112 $table->tablealign = 'center';
113 echo html_writer::table($table);
114
115 echo $editlink;
116} else { //else we're editing
117 require_once('edit_form.php');
118
ace9051c 119 $data = new stdClass();
54caa598
AD
120 $data->id = $context->id;
121
122 $i = 1;
123 foreach ($letters as $boundary=>$letter) {
124 $gradelettername = 'gradeletter'.$i;
125 $gradeboundaryname = 'gradeboundary'.$i;
126
127 $data->$gradelettername = $letter;
128 $data->$gradeboundaryname = $boundary;
129 $i++;
130 }
af14ef65 131 $data->override = $override;
54caa598
AD
132
133 $mform = new edit_letter_form($returnurl.$editparam, array('num'=>$num, 'admin'=>$admin));
134 $mform->set_data($data);
135
136 if ($mform->is_cancelled()) {
137 redirect($returnurl);
138
139 } else if ($data = $mform->get_data()) {
140 if (!$admin and empty($data->override)) {
141 $DB->delete_records('grade_letters', array('contextid' => $context->id));
142 redirect($returnurl);
143 }
144
145 $letters = array();
1d693611 146 for ($i=1; $i < $num+1; $i++) {
54caa598
AD
147 $gradelettername = 'gradeletter'.$i;
148 $gradeboundaryname = 'gradeboundary'.$i;
149
4cc977a6 150 if (property_exists($data, $gradeboundaryname) and $data->$gradeboundaryname != -1) {
54caa598
AD
151 $letter = trim($data->$gradelettername);
152 if ($letter == '') {
153 continue;
154 }
1d693611 155
b53a658b 156 $boundary = floatval($data->$gradeboundaryname);
1d693611 157 if ($boundary < 0 || $boundary > 100) {
b53a658b 158 continue; // Skip if out of range.
1d693611
HH
159 }
160
a9a02e91 161 // The keys need to be strings so floats are not truncated.
1d693611 162 $letters[number_format($boundary, 5)] = $letter;
54caa598
AD
163 }
164 }
54caa598 165
1d693611
HH
166 $pool = array();
167 if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC')) {
168 foreach ($records as $r) {
b53a658b 169 // Will re-use the lowerboundary to avoid duplicate during the update process.
1d693611
HH
170 $pool[number_format($r->lowerboundary, 5)] = $r;
171 }
54caa598
AD
172 }
173
1d693611 174 foreach ($letters as $boundary => $letter) {
ace9051c 175 $record = new stdClass();
54caa598
AD
176 $record->letter = $letter;
177 $record->lowerboundary = $boundary;
178 $record->contextid = $context->id;
179
1d693611 180 if (isset($pool[$boundary])) {
b53a658b
FM
181 // Re-use the existing boundary to avoid key constraint.
182 if ($letter != $pool[$boundary]->letter) {
183 // The letter has been assigned to another boundary, we update it.
1d693611
HH
184 $record->id = $pool[$boundary]->id;
185 $DB->update_record('grade_letters', $record);
1d693611 186 }
b53a658b
FM
187 unset($pool[$boundary]); // Remove the letter from the pool.
188 } else if ($candidate = array_pop($pool)) {
189 // The boundary is new, we update a random record from the pool.
1d693611 190 $record->id = $candidate->id;
54caa598
AD
191 $DB->update_record('grade_letters', $record);
192 } else {
b53a658b 193 // No records were found, this must be a new letter.
54caa598
AD
194 $DB->insert_record('grade_letters', $record);
195 }
196 }
197
b53a658b 198 // Delete the unused records.
1d693611
HH
199 foreach($pool as $leftover) {
200 $DB->delete_records('grade_letters', array('id' => $leftover->id));
54caa598
AD
201 }
202
203 redirect($returnurl);
204 }
205
206 print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'));
207
208 $mform->display();
284abb09 209}
210
6c3ef410 211echo $OUTPUT->footer();