weekly release 4.1dev
[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
724578a0 33$url = new moodle_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);
724578a0
MG
45 navigation_node::override_active_url($url);
46 $url->param('edit', 1);
47 $PAGE->navbar->add(get_string('editgradeletters', 'grades'), $url);
9376f5a6 48}
724578a0 49$PAGE->set_url($url);
284abb09 50
54caa598
AD
51$returnurl = null;
52$editparam = null;
53if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) {
54 require_once $CFG->libdir.'/adminlib.php';
54caa598
AD
55
56 admin_externalpage_setup('letters');
653f91f9 57
54caa598
AD
58 $admin = true;
59 $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
60 $editparam = '?edit=1';
aa1c2e23 61 $PAGE->set_primary_active_tab('siteadminnode');
54caa598
AD
62} else if ($context->contextlevel == CONTEXT_COURSE) {
63
64 $PAGE->set_pagelayout('standard');//calling this here to make blocks display
653f91f9 65
54caa598
AD
66 require_login($context->instanceid, false, $cm);
67
68 $admin = false;
69 $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->id;
70 $editparam = '&edit=1';
71
72 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id));
73} else {
74 print_error('invalidcourselevel');
75}
284abb09 76
77$strgrades = get_string('grades');
78$pagename = get_string('letters', 'grades');
79
284abb09 80$letters = grade_get_letters($context);
54caa598
AD
81$num = count($letters) + 3;
82
af14ef65
GPL
83$override = $DB->record_exists('grade_letters', array('contextid' => $context->id));
84
54caa598
AD
85//if were viewing the letters
86if (!$edit) {
08e90eee
MG
87 $heading = get_string('gradeletters', 'grades');
88 $actionbar = new \core_grades\output\grade_letters_action_bar($context);
89
90 if ($admin) {
91 echo $OUTPUT->header();
92 $renderer = $PAGE->get_renderer('core_grades');
93 echo $renderer->render_action_bar($actionbar);
94 echo $OUTPUT->heading($heading);
95 } else {
96 print_grade_page_head($course->id, 'letter', 'view', $heading, false, false,
97 true, null, null, null, $actionbar);
98 }
54caa598
AD
99
100 $data = array();
101
102 $max = 100;
103 foreach($letters as $boundary=>$letter) {
104 $line = array();
105 $line[] = format_float($max,2).' %';
106 $line[] = format_float($boundary,2).' %';
107 $line[] = format_string($letter);
108 $data[] = $line;
109 $max = $boundary - 0.01;
110 }
111
af14ef65
GPL
112 if (!empty($override)) {
113 echo $OUTPUT->notification(get_string('gradeletteroverridden', 'grades'), 'notifymessage');
114 }
115
54caa598 116 $table = new html_table();
cb2cf2fe 117 $table->id = 'grade-letters-view';
54caa598
AD
118 $table->head = array(get_string('max', 'grades'), get_string('min', 'grades'), get_string('letter', 'grades'));
119 $table->size = array('30%', '30%', '40%');
120 $table->align = array('left', 'left', 'left');
121 $table->width = '30%';
122 $table->data = $data;
123 $table->tablealign = 'center';
124 echo html_writer::table($table);
125
54caa598
AD
126} else { //else we're editing
127 require_once('edit_form.php');
128
ace9051c 129 $data = new stdClass();
54caa598
AD
130 $data->id = $context->id;
131
132 $i = 1;
133 foreach ($letters as $boundary=>$letter) {
134 $gradelettername = 'gradeletter'.$i;
135 $gradeboundaryname = 'gradeboundary'.$i;
136
137 $data->$gradelettername = $letter;
138 $data->$gradeboundaryname = $boundary;
139 $i++;
140 }
af14ef65 141 $data->override = $override;
54caa598
AD
142
143 $mform = new edit_letter_form($returnurl.$editparam, array('num'=>$num, 'admin'=>$admin));
144 $mform->set_data($data);
145
146 if ($mform->is_cancelled()) {
147 redirect($returnurl);
148
149 } else if ($data = $mform->get_data()) {
f1f5ba36
MB
150
151 // Make sure we are updating the cache.
152 $cache = cache::make('core', 'grade_letters');
153
54caa598 154 if (!$admin and empty($data->override)) {
b4993d29
SB
155 $records = $DB->get_records('grade_letters', array('contextid' => $context->id));
156 foreach ($records as $record) {
ac334832 157 $DB->delete_records('grade_letters', array('id' => $record->id));
b4993d29
SB
158 // Trigger the letter grade deleted event.
159 $event = \core\event\grade_letter_deleted::create(array(
160 'objectid' => $record->id,
161 'context' => $context,
162 ));
163 $event->trigger();
164 }
f1f5ba36
MB
165
166 // Make sure we clear the cache for this context.
167 $cache->delete($context->id);
54caa598
AD
168 redirect($returnurl);
169 }
170
171 $letters = array();
1d693611 172 for ($i=1; $i < $num+1; $i++) {
54caa598
AD
173 $gradelettername = 'gradeletter'.$i;
174 $gradeboundaryname = 'gradeboundary'.$i;
175
4cc977a6 176 if (property_exists($data, $gradeboundaryname) and $data->$gradeboundaryname != -1) {
54caa598
AD
177 $letter = trim($data->$gradelettername);
178 if ($letter == '') {
179 continue;
180 }
1d693611 181
b53a658b 182 $boundary = floatval($data->$gradeboundaryname);
1d693611 183 if ($boundary < 0 || $boundary > 100) {
b53a658b 184 continue; // Skip if out of range.
1d693611
HH
185 }
186
a9a02e91 187 // The keys need to be strings so floats are not truncated.
1d693611 188 $letters[number_format($boundary, 5)] = $letter;
54caa598
AD
189 }
190 }
54caa598 191
1d693611
HH
192 $pool = array();
193 if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC')) {
194 foreach ($records as $r) {
b53a658b 195 // Will re-use the lowerboundary to avoid duplicate during the update process.
1d693611
HH
196 $pool[number_format($r->lowerboundary, 5)] = $r;
197 }
54caa598
AD
198 }
199
1d693611 200 foreach ($letters as $boundary => $letter) {
ace9051c 201 $record = new stdClass();
54caa598
AD
202 $record->letter = $letter;
203 $record->lowerboundary = $boundary;
204 $record->contextid = $context->id;
205
1d693611 206 if (isset($pool[$boundary])) {
b53a658b
FM
207 // Re-use the existing boundary to avoid key constraint.
208 if ($letter != $pool[$boundary]->letter) {
209 // The letter has been assigned to another boundary, we update it.
1d693611
HH
210 $record->id = $pool[$boundary]->id;
211 $DB->update_record('grade_letters', $record);
31647f0c
SB
212 // Trigger the letter grade updated event.
213 $event = \core\event\grade_letter_updated::create(array(
214 'objectid' => $record->id,
215 'context' => $context,
216 ));
217 $event->trigger();
1d693611 218 }
b53a658b
FM
219 unset($pool[$boundary]); // Remove the letter from the pool.
220 } else if ($candidate = array_pop($pool)) {
221 // The boundary is new, we update a random record from the pool.
1d693611 222 $record->id = $candidate->id;
54caa598 223 $DB->update_record('grade_letters', $record);
31647f0c
SB
224 // Trigger the letter grade updated event.
225 $event = \core\event\grade_letter_updated::create(array(
226 'objectid' => $record->id,
227 'context' => $context,
228 ));
229 $event->trigger();
54caa598 230 } else {
b53a658b 231 // No records were found, this must be a new letter.
31647f0c
SB
232 $newid = $DB->insert_record('grade_letters', $record);
233 // Trigger the letter grade added event.
234 $event = \core\event\grade_letter_created::create(array(
235 'objectid' => $newid,
236 'context' => $context,
237 ));
238 $event->trigger();
54caa598
AD
239 }
240 }
241
60fe1f8c 242 // Cache the changed letters.
f1f5ba36 243 if (!empty($letters)) {
60fe1f8c
EL
244
245 // For some reason, the cache saves it in the order in which they were entered
246 // but we really want to order them in descending order so we sort it here.
f1f5ba36
MB
247 krsort($letters);
248 $cache->set($context->id, $letters);
249 }
250
b53a658b 251 // Delete the unused records.
1d693611
HH
252 foreach($pool as $leftover) {
253 $DB->delete_records('grade_letters', array('id' => $leftover->id));
31647f0c
SB
254 // Trigger the letter grade deleted event.
255 $event = \core\event\grade_letter_deleted::create(array(
256 'objectid' => $leftover->id,
257 'context' => $context,
258 ));
259 $event->trigger();
54caa598
AD
260 }
261
262 redirect($returnurl);
263 }
264
08e90eee
MG
265 print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'),
266 false, false, false);
54caa598
AD
267
268 $mform->display();
284abb09 269}
270
6c3ef410 271echo $OUTPUT->footer();