MDL-36331 core_grade: fixed up 2 incorrect default return URLs
[moodle.git] / grade / edit / tree / calculation.php
CommitLineData
e060e33d 1<?php
e060e33d 2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
8ad36f4c 16
a153c9f2
AD
17/**
18 * Edit a calculated grade item
19 *
20 * @package core_grades
21 * @copyright 2007 Petr Skoda
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
78ad5f3f 25require_once '../../../config.php';
3af29899 26require_once $CFG->dirroot.'/grade/lib.php';
76317c73 27require_once $CFG->libdir.'/mathslib.php';
6fb39a98 28require_once 'calculation_form.php';
920a0fb2 29
b31a9f3d 30$courseid = required_param('courseid', PARAM_INT);
31$id = required_param('id', PARAM_INT);
32$section = optional_param('section', 'calculation', PARAM_ALPHA);
33$idnumbers = optional_param('idnumbers', null, PARAM_RAW);
920a0fb2 34
a6855934 35$url = new moodle_url('/grade/edit/tree/calculation.php', array('id'=>$id, 'courseid'=>$courseid));
beebcf26 36if ($section !== 'calculation') {
37 $url->param('section', $section);
38}
beebcf26 39$PAGE->set_url($url);
40
5c75a0a3 41if (!$course = $DB->get_record('course', array('id' => $courseid))) {
46fa6810 42 print_error('nocourseid');
43}
920a0fb2 44
46fa6810 45require_login($course);
46fa6810 46$context = get_context_instance(CONTEXT_COURSE, $course->id);
f10fac96 47require_capability('moodle/grade:manage', $context);
46fa6810 48
49// default return url
3af29899 50$gpr = new grade_plugin_return();
11694f39 51$returnurl = $gpr->get_return_url($CFG->wwwroot.'/grade/report/index.php?id='.$course->id);
46fa6810 52
53if (!$grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$course->id))) {
1bb0aa0f 54 print_error('invaliditemid');
46fa6810 55}
56
ff1bc603 57// activity items and items without grade can not have calculation
58if ($grade_item->is_external_item() or ($grade_item->gradetype != GRADE_TYPE_VALUE and $grade_item->gradetype != GRADE_TYPE_SCALE)) {
5c75a0a3 59 redirect($returnurl, get_string('errornocalculationallowed', 'grades'));
6fb39a98 60}
46fa6810 61
b31a9f3d 62$mform = new edit_calculation_form(null, array('gpr'=>$gpr, 'itemid' => $grade_item->id));
46fa6810 63
64if ($mform->is_cancelled()) {
65 redirect($returnurl);
66
11a14999 67}
68
76317c73 69$calculation = calc_formula::localize($grade_item->calculation);
70$calculation = grade_item::denormalize_formula($calculation, $grade_item->courseid);
11a14999 71$mform->set_data(array('courseid'=>$grade_item->courseid, 'calculation'=>$calculation, 'id'=>$grade_item->id, 'itemname'=>$grade_item->itemname));
46fa6810 72
b31a9f3d 73$errors = array();
74
294ce987 75if ($data = $mform->get_data()) {
76317c73 76 $calculation = calc_formula::unlocalize($data->calculation);
77 $grade_item->set_calculation($calculation);
b31a9f3d 78
46fa6810 79 redirect($returnurl);
b31a9f3d 80
81} elseif (!empty($section) AND $section='idnumbers' AND !empty($idnumbers)) { // Handle idnumbers separately (non-mform)
82 //first validate and store the new idnumbers
83 foreach ($idnumbers as $giid => $value) {
84 if ($gi = grade_item::fetch(array('id' => $giid))) {
85 if ($gi->itemtype == 'mod') {
86 $cm = get_coursemodule_from_instance($gi->itemmodule, $gi->iteminstance, $gi->courseid);
87 } else {
88 $cm = null;
89 }
90
204175c5 91 if (!grade_verify_idnumber($value, $COURSE->id, $gi, $cm)) {
b31a9f3d 92 $errors[$giid] = get_string('idnumbertaken');
93 continue;
94 }
95
294ce987 96 if (empty($gi->idnumber) and !$gi->add_idnumber($idnumbers[$gi->id])) {
b31a9f3d 97 $errors[$giid] = get_string('error');
98 continue;
99 }
100 } else {
101 $errors[$giid] = 'Could not fetch the grade_item with id=' . $giid;
102 }
103 }
46fa6810 104}
105
b31a9f3d 106$gtree = new grade_tree($course->id, false, false);
107
46fa6810 108$strgrades = get_string('grades');
109$strgraderreport = get_string('graderreport', 'grades');
110$strcalculationedit = get_string('editcalculation', 'grades');
111
7cc378fd 112grade_build_nav(__FILE__, $strcalculationedit, array('courseid' => $courseid));
113$PAGE->set_title($strgrades . ': ' . $strgraderreport);
02114f13 114$PAGE->set_heading($course->fullname);
7cc378fd 115echo $OUTPUT->header();
46fa6810 116
117$mform->display();
b31a9f3d 118// Now show the gradetree with the idnumbers add/edit form
119echo '
120<form class="mform" id="mform2" method="post" action="' . $CFG->wwwroot . '/grade/edit/tree/calculation.php?courseid='.$courseid.'&amp;id='.$id.'">
e1f096fd 121 <div style="display: none;">
b31a9f3d 122 <input type="hidden" value="'.$id.'" name="id"/>
123 <input type="hidden" value="'.$courseid.'" name="courseid"/>
124 <input type="hidden" value="'.$gpr->type.'" name="gpr_type"/>
125 <input type="hidden" value="'.$gpr->plugin.'" name="gpr_plugin"/>
126 <input type="hidden" value="'.$gpr->courseid.'" name="gpr_courseid"/>
127 <input type="hidden" value="'.sesskey().'" name="sesskey"/>
128 <input type="hidden" value="idnumbers" name="section"/>
129 </div>
130
e1f096fd 131 <fieldset id="idnumbers" class="clearfix">
132 <legend class="ftoggler">'.get_string('idnumbers', 'grades').'</legend>
b31a9f3d 133 <div class="fcontainer clearfix">
134 <ul>
135 ' . get_grade_tree($gtree, $gtree->top_element, $id, $errors) . '
136 </ul>
137 </div>
138 </fieldset>
139 <div class="fitem" style="text-align: center;">
140 <input id="id_addidnumbers" type="submit" value="'.get_string('addidnumbers', 'grades').'" name="addidnumbers" />
141 </div>
142</form>';
46fa6810 143
5a931394 144echo $OUTPUT->footer();
b31a9f3d 145die();
146
147
148/**
149 * Simplified version of the print_grade_tree() recursive function found in grade/edit/tree/index.php
150 * Only prints a tree with a basic icon for each element, and an edit field for
151 * items without an idnumber.
152 * @param object $gtree
153 * @param object $element
154 * @param int $current_itemid The itemid of this page: should be excluded from the tree
155 * @param array $errors An array of idnumbers => error
156 * @return string
157 */
158function get_grade_tree(&$gtree, $element, $current_itemid=null, $errors=null) {
159 global $CFG;
160
161 $object = $element['object'];
162 $eid = $element['eid'];
163 $type = $element['type'];
164 $grade_item = $object->get_grade_item();
165
166 $name = $object->get_name();
167 $return_string = '';
168
169 //TODO: improve outcome visualisation
170 if ($type == 'item' and !empty($object->outcomeid)) {
171 $name = $name.' ('.get_string('outcome', 'grades').')';
172 }
173
174 $idnumber = $object->get_idnumber();
175
176 // Don't show idnumber or input field for current item if given to function. Highlight the item instead.
177 if ($type != 'category') {
178 if (is_null($current_itemid) OR $grade_item->id != $current_itemid) {
179 if ($idnumber) {
180 $name .= ": [[$idnumber]]";
181 } else {
182 $closingdiv = '';
183 if (!empty($errors[$grade_item->id])) {
184 $name .= '<div class="error"><span class="error">' . $errors[$grade_item->id].'</span><br />'."\n";
185 $closingdiv = "</div>\n";
186 }
aa15bd56 187 $name .= '<label class="accesshide" for="id_idnumber_' . $grade_item->id . '">' . get_string('gradeitems', 'grades') .'</label>';
b31a9f3d 188 $name .= '<input class="idnumber" id="id_idnumber_'.$grade_item->id.'" type="text" name="idnumbers['.$grade_item->id.']" />' . "\n";
189 $name .= $closingdiv;
190 }
191 } else {
192 $name = "<strong>$name</strong>";
193 }
194 }
195
6cc3e350 196 $icon = $gtree->get_element_icon($element, true);
b31a9f3d 197 $last = '';
d297269d 198 $catcourseitem = ($element['type'] == 'courseitem' or $element['type'] == 'categoryitem');
b31a9f3d 199
200 if ($type != 'category') {
201 $return_string .= '<li class="'.$type.'">'.$icon.$name.'</li>' . "\n";
202 } else {
203 $return_string .= '<li class="'.$type.'">'.$icon.$name . "\n";
204 $return_string .= '<ul class="catlevel'.$element['depth'].'">'."\n";
205 $last = null;
206 foreach($element['children'] as $child_el) {
207 $return_string .= get_grade_tree($gtree, $child_el, $current_itemid, $errors);
208 }
209 if ($last) {
210 $return_string .= get_grade_tree($gtree, $last, $current_itemid, $errors);
211 }
212 $return_string .= '</ul></li>'."\n";
213 }
214
215 return $return_string;
216}
217
6c3ef410 218