MDL-29794 Initial support for re-using a shared grading form
[moodle.git] / grade / edit / letter / index.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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.
14 //
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/>.
18 /**
19  * List of grade letters.
20  *
21  * @package   moodlecore
22  * @copyright 2008 Nicolas Connault
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
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
39 //if viewing
40 if (!$edit) {
41     if (!has_capability('moodle/grade:manage', $context) and !has_capability('moodle/grade:manageletters', $context)) {
42         print_error('nopermissiontoviewletergrade');
43     }
44 } else {//else we're editing
45     require_capability('moodle/grade:manageletters', $context);
46 }
48 $returnurl = null;
49 $editparam = null;
50 if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) {
51     require_once $CFG->libdir.'/adminlib.php';
52     require_login();
54     admin_externalpage_setup('letters');
56     $admin = true;
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);
65     $admin = false;
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));
70 } else {
71     print_error('invalidcourselevel');
72 }
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
81 if (!$edit) {
83     $data = array();
85     $max = 100;
86     foreach($letters as $boundary=>$letter) {
87         $line = array();
88         $line[] = format_float($max,2).' %';
89         $line[] = format_float($boundary,2).' %';
90         $line[] = format_string($letter);
91         $data[] = $line;
92         $max = $boundary - 0.01;
93     }
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'));
99     echo $editlink;
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);
110     echo $editlink;
111 } else { //else we're editing
112     require_once('edit_form.php');
114     $data = new stdClass();
115     $data->id = $context->id;
117     $i = 1;
118     foreach ($letters as $boundary=>$letter) {
119         $gradelettername = 'gradeletter'.$i;
120         $gradeboundaryname = 'gradeboundary'.$i;
122         $data->$gradelettername   = $letter;
123         $data->$gradeboundaryname = $boundary;
124         $i++;
125     }
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);
138         }
140         $letters = array();
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);
147                 if ($letter == '') {
148                     continue;
149                 }
150                 $letters[$data->$gradeboundaryname] = $letter;
151             }
152         }
153         krsort($letters, SORT_NUMERIC);
155         $old_ids = array();
156         if ($records = $DB->get_records('grade_letters', array('contextid' => $context->id), 'lowerboundary ASC', 'id')) {
157             $old_ids = array_keys($records);
158         }
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);
169             } else {
170                 $DB->insert_record('grade_letters', $record);
171             }
172         }
174         foreach($old_ids as $old_id) {
175             $DB->delete_records('grade_letters', array('id' => $old_id));
176         }
178         redirect($returnurl);
179     }
181     print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'));
183     $mform->display();
186 echo $OUTPUT->footer();