MDL-29794 Initial support for re-using a shared grading form
[moodle.git] / grade / edit / letter / index.php
CommitLineData
e060e33d 1<?php
284abb09 2
e060e33d 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/>.
8ad36f4c 17
1c1f64a2 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 */
25
284abb09 26require_once '../../../config.php';
27require_once $CFG->dirroot.'/grade/lib.php';
28require_once $CFG->libdir.'/gradelib.php';
29
54caa598 30$contextid = optional_param('id', SYSCONTEXTID, PARAM_INT);
284abb09 31$action = optional_param('action', '', PARAM_ALPHA);
54caa598 32$edit = optional_param('edit', false, PARAM_BOOL); //are we editing?
284abb09 33
54caa598 34$PAGE->set_url('/grade/edit/letter/index.php', array('id' => $contextid));
1c1f64a2 35
54caa598
AD
36list($context, $course, $cm) = get_context_info_array($contextid);
37$contextid = null;//now we have a context object throw away the $contextid from the params
9376f5a6 38
54caa598
AD
39//if viewing
40if (!$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);
9376f5a6 46}
284abb09 47
54caa598
AD
48$returnurl = null;
49$editparam = null;
50if ($context->contextlevel == CONTEXT_SYSTEM or $context->contextlevel == CONTEXT_COURSECAT) {
51 require_once $CFG->libdir.'/adminlib.php';
52 require_login();
53
54 admin_externalpage_setup('letters');
653f91f9 55
54caa598
AD
56 $admin = true;
57 $returnurl = "$CFG->wwwroot/grade/edit/letter/index.php";
58 $editparam = '?edit=1';
59} else if ($context->contextlevel == CONTEXT_COURSE) {
60
61 $PAGE->set_pagelayout('standard');//calling this here to make blocks display
653f91f9 62
54caa598
AD
63 require_login($context->instanceid, false, $cm);
64
65 $admin = false;
66 $returnurl = $CFG->wwwroot.'/grade/edit/letter/index.php?id='.$context->id;
67 $editparam = '&edit=1';
68
69 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'letter', 'courseid'=>$course->id));
70} else {
71 print_error('invalidcourselevel');
72}
284abb09 73
74$strgrades = get_string('grades');
75$pagename = get_string('letters', 'grades');
76
284abb09 77$letters = grade_get_letters($context);
54caa598
AD
78$num = count($letters) + 3;
79
80//if were viewing the letters
81if (!$edit) {
82
83 $data = array();
84
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 }
94
95 print_grade_page_head($COURSE->id, 'letter', 'view', get_string('gradeletters', 'grades'));
96
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;
100
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);
109
110 echo $editlink;
111} else { //else we're editing
112 require_once('edit_form.php');
113
ace9051c 114 $data = new stdClass();
54caa598
AD
115 $data->id = $context->id;
116
117 $i = 1;
118 foreach ($letters as $boundary=>$letter) {
119 $gradelettername = 'gradeletter'.$i;
120 $gradeboundaryname = 'gradeboundary'.$i;
121
122 $data->$gradelettername = $letter;
123 $data->$gradeboundaryname = $boundary;
124 $i++;
125 }
126 $data->override = $DB->record_exists('grade_letters', array('contextid' => $context->id));
127
128 $mform = new edit_letter_form($returnurl.$editparam, array('num'=>$num, 'admin'=>$admin));
129 $mform->set_data($data);
130
131 if ($mform->is_cancelled()) {
132 redirect($returnurl);
133
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 }
139
140 $letters = array();
141 for($i=1; $i<$num+1; $i++) {
142 $gradelettername = 'gradeletter'.$i;
143 $gradeboundaryname = 'gradeboundary'.$i;
144
4cc977a6 145 if (property_exists($data, $gradeboundaryname) and $data->$gradeboundaryname != -1) {
54caa598
AD
146 $letter = trim($data->$gradelettername);
147 if ($letter == '') {
148 continue;
149 }
150 $letters[$data->$gradeboundaryname] = $letter;
151 }
152 }
153 krsort($letters, SORT_NUMERIC);
154
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 }
159
160 foreach($letters as $boundary=>$letter) {
ace9051c 161 $record = new stdClass();
54caa598
AD
162 $record->letter = $letter;
163 $record->lowerboundary = $boundary;
164 $record->contextid = $context->id;
165
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 }
173
174 foreach($old_ids as $old_id) {
175 $DB->delete_records('grade_letters', array('id' => $old_id));
176 }
177
178 redirect($returnurl);
179 }
180
181 print_grade_page_head($COURSE->id, 'letter', 'edit', get_string('editgradeletters', 'grades'));
182
183 $mform->display();
284abb09 184}
185
6c3ef410 186echo $OUTPUT->footer();