2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * List of grade letters.
20 * @package core_grades
21 * @copyright 2008 Nicolas Connault
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once '../../../config.php';
26 require_once $CFG->dirroot
.'/grade/lib.php';
27 require_once $CFG->libdir
.'/gradelib.php';
29 $contextid = optional_param('id', SYSCONTEXTID
, PARAM_INT
);
30 $action = optional_param('action', '', PARAM_ALPHA
);
31 $edit = optional_param('edit', false, PARAM_BOOL
); //are we editing?
33 $PAGE->set_url('/grade/edit/letter/index.php', array('id' => $contextid));
35 list($context, $course, $cm) = get_context_info_array($contextid);
36 $contextid = null;//now we have a context object throw away the $contextid from the params
40 if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:manageletters', $context)) {
41 print_error('nopermissiontoviewletergrade');
43 } else {//else we're editing
44 require_capability('moodle/grade:manageletters', $context);
49 if ($context->contextlevel
== CONTEXT_SYSTEM
or $context->contextlevel
== CONTEXT_COURSECAT
) {
50 require_once $CFG->libdir
.'/adminlib.php';
52 admin_externalpage_setup('letters');
55 $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
56 $editparam = '?edit=1';
57 } else if ($context->contextlevel
== CONTEXT_COURSE
) {
59 $PAGE->set_pagelayout('standard');//calling this here to make blocks display
61 require_login($context->instanceid
, false, $cm);
64 $returnurl = $CFG->wwwroot
.'/grade/edit/letter/index.php?id='.$context->id
;
65 $editparam = '&edit=1';
67 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id
));
69 print_error('invalidcourselevel');
72 $strgrades = get_string('grades');
73 $pagename = get_string('letters', 'grades');
75 $letters = grade_get_letters($context);
76 $num = count($letters) +
3;
78 $override = $DB->record_exists('grade_letters', array('contextid' => $context->id
));
80 //if were viewing the letters
82 $heading = get_string('gradeletters', 'grades');
83 $actionbar = new \core_grades\output\
grade_letters_action_bar($context);
86 echo $OUTPUT->header();
87 $renderer = $PAGE->get_renderer('core_grades');
88 echo $renderer->render_action_bar($actionbar);
89 echo $OUTPUT->heading($heading);
91 print_grade_page_head($course->id
, 'letter', 'view', $heading, false, false,
92 true, null, null, null, $actionbar);
98 foreach($letters as $boundary=>$letter) {
100 $line[] = format_float($max,2).' %';
101 $line[] = format_float($boundary,2).' %';
102 $line[] = format_string($letter);
104 $max = $boundary - 0.01;
107 if (!empty($override)) {
108 echo $OUTPUT->notification(get_string('gradeletteroverridden', 'grades'), 'notifymessage');
111 $table = new html_table();
112 $table->id
= 'grade-letters-view';
113 $table->head
= array(get_string('max', 'grades'), get_string('min', 'grades'), get_string('letter', 'grades'));
114 $table->size
= array('30%', '30%', '40%');
115 $table->align
= array('left', 'left', 'left');
116 $table->width
= '30%';
117 $table->data
= $data;
118 $table->tablealign
= 'center';
119 echo html_writer
::table($table);
121 } else { //else we're editing
122 require_once('edit_form.php');
124 $data = new stdClass();
125 $data->id
= $context->id
;
128 foreach ($letters as $boundary=>$letter) {
129 $gradelettername = 'gradeletter'.$i;
130 $gradeboundaryname = 'gradeboundary'.$i;
132 $data->$gradelettername = $letter;
133 $data->$gradeboundaryname = $boundary;
136 $data->override
= $override;
138 $mform = new edit_letter_form($returnurl.$editparam, array('num'=>$num, 'admin'=>$admin));
139 $mform->set_data($data);
141 if ($mform->is_cancelled()) {
142 redirect($returnurl);
144 } else if ($data = $mform->get_data()) {
146 // Make sure we are updating the cache.
147 $cache = cache
::make('core', 'grade_letters');
149 if (!$admin and empty($data->override
)) {
150 $records = $DB->get_records('grade_letters', array('contextid' => $context->id
));
151 foreach ($records as $record) {
152 $DB->delete_records('grade_letters', array('id' => $record->id
));
153 // Trigger the letter grade deleted event.
154 $event = \core\event\grade_letter_deleted
::create(array(
155 'objectid' => $record->id
,
156 'context' => $context,
161 // Make sure we clear the cache for this context.
162 $cache->delete($context->id
);
163 redirect($returnurl);
167 for ($i=1; $i < $num+
1; $i++
) {
168 $gradelettername = 'gradeletter'.$i;
169 $gradeboundaryname = 'gradeboundary'.$i;
171 if (property_exists($data, $gradeboundaryname) and $data->$gradeboundaryname != -1) {
172 $letter = trim($data->$gradelettername);
177 $boundary = floatval($data->$gradeboundaryname);
178 if ($boundary < 0 ||
$boundary > 100) {
179 continue; // Skip if out of range.
182 // The keys need to be strings so floats are not truncated.
183 $letters[number_format($boundary, 5)] = $letter;
188 if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id
), 'lowerboundary ASC')) {
189 foreach ($records as $r) {
190 // Will re-use the lowerboundary to avoid duplicate during the update process.
191 $pool[number_format($r->lowerboundary
, 5)] = $r;
195 foreach ($letters as $boundary => $letter) {
196 $record = new stdClass();
197 $record->letter
= $letter;
198 $record->lowerboundary
= $boundary;
199 $record->contextid
= $context->id
;
201 if (isset($pool[$boundary])) {
202 // Re-use the existing boundary to avoid key constraint.
203 if ($letter != $pool[$boundary]->letter
) {
204 // The letter has been assigned to another boundary, we update it.
205 $record->id
= $pool[$boundary]->id
;
206 $DB->update_record('grade_letters', $record);
207 // Trigger the letter grade updated event.
208 $event = \core\event\grade_letter_updated
::create(array(
209 'objectid' => $record->id
,
210 'context' => $context,
214 unset($pool[$boundary]); // Remove the letter from the pool.
215 } else if ($candidate = array_pop($pool)) {
216 // The boundary is new, we update a random record from the pool.
217 $record->id
= $candidate->id
;
218 $DB->update_record('grade_letters', $record);
219 // Trigger the letter grade updated event.
220 $event = \core\event\grade_letter_updated
::create(array(
221 'objectid' => $record->id
,
222 'context' => $context,
226 // No records were found, this must be a new letter.
227 $newid = $DB->insert_record('grade_letters', $record);
228 // Trigger the letter grade added event.
229 $event = \core\event\grade_letter_created
::create(array(
230 'objectid' => $newid,
231 'context' => $context,
237 // Cache the changed letters.
238 if (!empty($letters)) {
240 // For some reason, the cache saves it in the order in which they were entered
241 // but we really want to order them in descending order so we sort it here.
243 $cache->set($context->id
, $letters);
246 // Delete the unused records.
247 foreach($pool as $leftover) {
248 $DB->delete_records('grade_letters', array('id' => $leftover->id
));
249 // Trigger the letter grade deleted event.
250 $event = \core\event\grade_letter_deleted
::create(array(
251 'objectid' => $leftover->id
,
252 'context' => $context,
257 redirect($returnurl);
260 print_grade_page_head($COURSE->id
, 'letter', 'edit', get_string('editgradeletters', 'grades'),
261 false, false, false);
266 echo $OUTPUT->footer();