3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * List of grade letters.
22 * @copyright 2008 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once '../../../config.php';
27 require_once $CFG->dirroot.'/grade/lib.php';
28 require_once $CFG->libdir.'/gradelib.php';
30 $contextid = optional_param('id', SYSCONTEXTID, PARAM_INT);
31 $action = optional_param('action', '', PARAM_ALPHA);
32 $edit = optional_param('edit', false, PARAM_BOOL); //are we editing?
34 $PAGE->set_url('/grade/edit/letter/index.php', array('id' => $contextid));
36 list($context, $course, $cm) = get_context_info_array($contextid);
37 $contextid = null;//now we have a context object throw away the $contextid from the params
41 if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:manageletters', $context)) {
42 print_error('nopermissiontoviewletergrade');
44 } else {//else we're editing
45 require_capability('moodle/grade:manageletters', $context);
50 if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) {
51 require_once $CFG->libdir.'/adminlib.php';
54 admin_externalpage_setup('letters');
57 $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
58 $editparam = '?edit=1';
59 } else if ($context->contextlevel == CONTEXT_COURSE) {
61 $PAGE->set_pagelayout('standard');//calling this here to make blocks display
63 require_login($context->instanceid, false, $cm);
66 $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->id;
67 $editparam = '&edit=1';
69 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id));
71 print_error('invalidcourselevel');
74 $strgrades = get_string('grades');
75 $pagename = get_string('letters', 'grades');
77 $letters = grade_get_letters($context);
78 $num = count($letters) + 3;
80 //if were viewing the letters
86 foreach($letters as $boundary=>$letter) {
88 $line[] = format_float($max,2).' %';
89 $line[] = format_float($boundary,2).' %';
90 $line[] = format_string($letter);
92 $max = $boundary - 0.01;
95 print_grade_page_head($COURSE->id, 'letter', 'view', get_string('gradeletters', 'grades'));
97 $stredit = get_string('editgradeletters', 'grades');
98 $editlink = html_writer::nonempty_tag('div', html_writer::link($returnurl.$editparam, $stredit), array('class'=>'mdl-align'));
101 $table = new html_table();
102 $table->head = array(get_string('max', 'grades'), get_string('min', 'grades'), get_string('letter', 'grades'));
103 $table->size = array('30%', '30%', '40%');
104 $table->align = array('left', 'left', 'left');
105 $table->width = '30%';
106 $table->data = $data;
107 $table->tablealign = 'center';
108 echo html_writer::table($table);
111 } else { //else we're editing
112 require_once('edit_form.php');
114 $data = new stdClass();
115 $data->id = $context->id;
118 foreach ($letters as $boundary=>$letter) {
119 $gradelettername = 'gradeletter'.$i;
120 $gradeboundaryname = 'gradeboundary'.$i;
122 $data->$gradelettername = $letter;
123 $data->$gradeboundaryname = $boundary;
126 $data->override = $DB->record_exists('grade_letters', array('contextid' => $context->id));
128 $mform = new edit_letter_form($returnurl.$editparam, array('num'=>$num, 'admin'=>$admin));
129 $mform->set_data($data);
131 if ($mform->is_cancelled()) {
132 redirect($returnurl);
134 } else if ($data = $mform->get_data()) {
135 if (!$admin and empty($data->override)) {
136 $DB->delete_records('grade_letters', array('contextid' => $context->id));
137 redirect($returnurl);
141 for($i=1; $i<$num+1; $i++) {
142 $gradelettername = 'gradeletter'.$i;
143 $gradeboundaryname = 'gradeboundary'.$i;
145 if (property_exists($data, $gradeboundaryname) and $data->$gradeboundaryname != -1) {
146 $letter = trim($data->$gradelettername);
150 $letters[$data->$gradeboundaryname] = $letter;
153 krsort($letters, SORT_NUMERIC);
156 if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC', 'id')) {
157 $old_ids = array_keys($records);
160 foreach($letters as $boundary=>$letter) {
161 $record = new stdClass();
162 $record->letter = $letter;
163 $record->lowerboundary = $boundary;
164 $record->contextid = $context->id;
166 if ($old_id = array_pop($old_ids)) {
167 $record->id = $old_id;
168 $DB->update_record('grade_letters', $record);
170 $DB->insert_record('grade_letters', $record);
174 foreach($old_ids as $old_id) {
175 $DB->delete_records('grade_letters', array('id' => $old_id));
178 redirect($returnurl);
181 print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'));
186 echo $OUTPUT->footer();