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';
53 admin_externalpage_setup('letters');
56 $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
57 $editparam = '?edit=1';
58 } else if ($context->contextlevel == CONTEXT_COURSE) {
60 $PAGE->set_pagelayout('standard');//calling this here to make blocks display
62 require_login($context->instanceid, false, $cm);
65 $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->id;
66 $editparam = '&edit=1';
68 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id));
70 print_error('invalidcourselevel');
73 $strgrades = get_string('grades');
74 $pagename = get_string('letters', 'grades');
76 $letters = grade_get_letters($context);
77 $num = count($letters) + 3;
79 //if were viewing the letters
85 foreach($letters as $boundary=>$letter) {
87 $line[] = format_float($max,2).' %';
88 $line[] = format_float($boundary,2).' %';
89 $line[] = format_string($letter);
91 $max = $boundary - 0.01;
94 print_grade_page_head($COURSE->id, 'letter', 'view', get_string('gradeletters', 'grades'));
96 $stredit = get_string('editgradeletters', 'grades');
97 $editlink = html_writer::nonempty_tag('div', html_writer::link($returnurl.$editparam, $stredit), array('class'=>'mdl-align'));
100 $table = new html_table();
101 $table->head = array(get_string('max', 'grades'), get_string('min', 'grades'), get_string('letter', 'grades'));
102 $table->size = array('30%', '30%', '40%');
103 $table->align = array('left', 'left', 'left');
104 $table->width = '30%';
105 $table->data = $data;
106 $table->tablealign = 'center';
107 echo html_writer::table($table);
110 } else { //else we're editing
111 require_once('edit_form.php');
113 $data = new stdClass();
114 $data->id = $context->id;
117 foreach ($letters as $boundary=>$letter) {
118 $gradelettername = 'gradeletter'.$i;
119 $gradeboundaryname = 'gradeboundary'.$i;
121 $data->$gradelettername = $letter;
122 $data->$gradeboundaryname = $boundary;
125 $data->override = $DB->record_exists('grade_letters', array('contextid' => $context->id));
127 $mform = new edit_letter_form($returnurl.$editparam, array('num'=>$num, 'admin'=>$admin));
128 $mform->set_data($data);
130 if ($mform->is_cancelled()) {
131 redirect($returnurl);
133 } else if ($data = $mform->get_data()) {
134 if (!$admin and empty($data->override)) {
135 $DB->delete_records('grade_letters', array('contextid' => $context->id));
136 redirect($returnurl);
140 for ($i=1; $i < $num+1; $i++) {
141 $gradelettername = 'gradeletter'.$i;
142 $gradeboundaryname = 'gradeboundary'.$i;
144 if (property_exists($data, $gradeboundaryname) and $data->$gradeboundaryname != -1) {
145 $letter = trim($data->$gradelettername);
150 $boundary = floatval($data->$gradeboundaryname);
151 if ($boundary < 0 || $boundary > 100) {
152 continue; // Skip if out of range.
155 // The keys need to be strings so floats are not truncated.
156 $letters[number_format($boundary, 5)] = $letter;
161 if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC')) {
162 foreach ($records as $r) {
163 // Will re-use the lowerboundary to avoid duplicate during the update process.
164 $pool[number_format($r->lowerboundary, 5)] = $r;
168 foreach ($letters as $boundary => $letter) {
169 $record = new stdClass();
170 $record->letter = $letter;
171 $record->lowerboundary = $boundary;
172 $record->contextid = $context->id;
174 if (isset($pool[$boundary])) {
175 // Re-use the existing boundary to avoid key constraint.
176 if ($letter != $pool[$boundary]->letter) {
177 // The letter has been assigned to another boundary, we update it.
178 $record->id = $pool[$boundary]->id;
179 $DB->update_record('grade_letters', $record);
181 unset($pool[$boundary]); // Remove the letter from the pool.
182 } else if ($candidate = array_pop($pool)) {
183 // The boundary is new, we update a random record from the pool.
184 $record->id = $candidate->id;
185 $DB->update_record('grade_letters', $record);
187 // No records were found, this must be a new letter.
188 $DB->insert_record('grade_letters', $record);
192 // Delete the unused records.
193 foreach($pool as $leftover) {
194 $DB->delete_records('grade_letters', array('id' => $leftover->id));
197 redirect($returnurl);
200 print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'));
205 echo $OUTPUT->footer();