MDL-49324 grades: Remove category aggregation change backend
[moodle.git] / grade / edit / tree / index.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/>.
f13002d5 16
1c1f64a2 17/**
53914e44 18 * The Gradebook setup page.
1c1f64a2 19 *
a153c9f2 20 * @package core_grades
1c1f64a2 21 * @copyright 2008 Nicolas Connault
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';
eedd3aa2 27require_once $CFG->dirroot.'/grade/report/lib.php'; // for preferences
dc482cfa 28require_once $CFG->dirroot.'/grade/edit/tree/lib.php';
f13002d5 29
dc482cfa 30$courseid = required_param('id', PARAM_INT);
31$action = optional_param('action', 0, PARAM_ALPHA);
32$eid = optional_param('eid', 0, PARAM_ALPHANUM);
5bfa0d79 33
160fe5ff 34$url = new moodle_url('/grade/edit/tree/index.php', array('id' => $courseid));
160fe5ff 35$PAGE->set_url($url);
4d5059d4 36$PAGE->set_pagelayout('admin');
1c1f64a2 37
b78bb4f7 38/// Make sure they can even access this course
aaf54d21 39if (!$course = $DB->get_record('course', array('id' => $courseid))) {
c31f631b 40 print_error('nocourseid');
b78bb4f7 41}
42
f13002d5 43require_login($course);
d4060472 44$context = context_course::instance($course->id);
7a6b7acf 45require_capability('moodle/grade:manage', $context);
46
d1ae61fe 47// todo $PAGE->requires->js_module() should be used here instead
d1ae61fe
DM
48$PAGE->requires->js('/grade/edit/tree/functions.js');
49
7a6b7acf 50/// return tracking object
78ad5f3f 51$gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'tree', 'courseid'=>$courseid));
7a6b7acf 52$returnurl = $gpr->get_return_url(null);
b78bb4f7 53
7a6b7acf 54//first make sure we have proper final grades - we need it for locking changes
e61f25a4
DW
55$normalisationmessage = null;
56
57$originalweights = grade_helper::fetch_all_natural_weights_for_course($courseid);
58
e61f25a4
DW
59$alteredweights = grade_helper::fetch_all_natural_weights_for_course($courseid);
60
61if (array_diff($originalweights, $alteredweights)) {
62 $normalisationmessage = get_string('weightsadjusted', 'grades');
63}
64
f13002d5 65// get the grading tree object
d3f14e42 66// note: total must be first for moving to work correctly, if you want it last moving code must be rewritten!
938e00b6 67$gtree = new grade_tree($courseid, false, false);
210611f6 68
f13002d5 69if (empty($eid)) {
70 $element = null;
71 $object = null;
5bfa0d79 72
f13002d5 73} else {
74 if (!$element = $gtree->locate_element($eid)) {
1bb0aa0f 75 print_error('invalidelementid', '', $returnurl);
f13002d5 76 }
7a6b7acf 77 $object = $element['object'];
f13002d5 78}
b3ac6c3e 79
e0724506 80$switch = grade_get_setting($course->id, 'aggregationposition', $CFG->grade_aggregationposition);
b3ac6c3e 81
826c5f86 82$strgrades = get_string('grades');
83$strgraderreport = get_string('graderreport', 'grades');
b3ac6c3e 84
f13002d5 85$moving = false;
54a007e8 86$movingeid = false;
f13002d5 87
54a007e8 88if ($action == 'moveselect') {
89 if ($eid and confirm_sesskey()) {
90 $movingeid = $eid;
91 $moving=true;
92 }
93}
94
95$grade_edit_tree = new grade_edit_tree($gtree, $movingeid, $gpr);
dc482cfa 96
f13002d5 97switch ($action) {
f13002d5 98 case 'delete':
653a8648 99 if ($eid && confirm_sesskey()) {
dc482cfa 100 if (!$grade_edit_tree->element_deletable($element)) {
23160003 101 // no deleting of external activities - they would be recreated anyway!
796a41b5 102 // exception is activity without grading or misconfigured activities
61a5ee16 103 break;
104 }
f943f4fd 105 $confirm = optional_param('confirm', 0, PARAM_BOOL);
106
cd3a391c 107 if ($confirm) {
f943f4fd 108 $object->delete('grade/report/grader/category');
109 redirect($returnurl);
110
111 } else {
7cc378fd 112 $PAGE->set_title($strgrades . ': ' . $strgraderreport);
02114f13 113 $PAGE->set_heading($course->fullname);
7cc378fd 114 echo $OUTPUT->header();
f943f4fd 115 $strdeletecheckfull = get_string('deletecheck', '', $object->get_name());
116 $optionsyes = array('eid'=>$eid, 'confirm'=>1, 'sesskey'=>sesskey(), 'id'=>$course->id, 'action'=>'delete');
117 $optionsno = array('id'=>$course->id);
dc6896ef 118 $formcontinue = new single_button(new moodle_url('index.php', $optionsyes), get_string('yes'));
3ba60ee1 119 $formcancel = new single_button(new moodle_url('index.php', $optionsno), get_string('no'), 'get');
03fcc729 120 echo $OUTPUT->confirm($strdeletecheckfull, $formcontinue, $formcancel);
5a931394 121 echo $OUTPUT->footer();
f943f4fd 122 die;
123 }
124 }
f13002d5 125 break;
126
739196ba 127 case 'autosort':
128 //TODO: implement autosorting based on order of mods on course page, categories first, manual items last
129 break;
130
f13002d5 131 case 'move':
132 if ($eid and confirm_sesskey()) {
133 $moveafter = required_param('moveafter', PARAM_ALPHANUM);
dc482cfa 134 $first = optional_param('first', false, PARAM_BOOL); // If First is set to 1, it means the target is the first child of the category $moveafter
135
f13002d5 136 if(!$after_el = $gtree->locate_element($moveafter)) {
1bb0aa0f 137 print_error('invalidelementid', '', $returnurl);
f13002d5 138 }
dc482cfa 139
f13002d5 140 $after = $after_el['object'];
f13002d5 141 $sortorder = $after->get_sortorder();
142
dc482cfa 143 if (!$first) {
144 $parent = $after->get_parent_category();
145 $object->set_parent($parent->id);
146 } else {
147 $object->set_parent($after->id);
148 }
149
f13002d5 150 $object->move_after_sortorder($sortorder);
151
152 redirect($returnurl);
153 }
154 break;
155
f13002d5 156 default:
157 break;
b3ac6c3e 158}
b3ac6c3e 159
5e212c33
AD
160//if we go straight to the db to update an element we need to recreate the tree as
161// $grade_edit_tree has already been constructed.
162//Ideally we could do the updates through $grade_edit_tree to avoid recreating it
163$recreatetree = false;
164
cd3a391c 165if ($data = data_submitted() and confirm_sesskey()) {
dc482cfa 166 // Perform bulk actions first
cd3a391c 167 if (!empty($data->bulkmove)) {
dc482cfa 168 $elements = array();
f13002d5 169
dc482cfa 170 foreach ($data as $key => $value) {
ddb9c95f 171 if (preg_match('/select_(ig[0-9]*)/', $key, $matches)) {
dc482cfa 172 $elements[] = $matches[1];
173 }
174 }
6cc3e350 175
dc482cfa 176 $grade_edit_tree->move_elements($elements, $returnurl);
6cc3e350 177 }
178
b79bf7a2 179 // Update weights (extra credits) on categories and items.
dc482cfa 180 foreach ($data as $key => $value) {
b79bf7a2
MG
181 if (preg_match('/^weight_([0-9]+)$/', $key, $matches)) {
182 $aid = $matches[1];
126d92d3 183
cd3a391c 184 $value = unformat_float($value);
61cca0b7 185 $value = clean_param($value, PARAM_FLOAT);
126d92d3 186
32ee0f83 187 $grade_item = grade_item::fetch(array('id' => $aid, 'courseid' => $courseid));
cd3a391c 188
b6a1e366 189 // Convert weight to aggregation coef2.
b79bf7a2
MG
190 $aggcoef = $grade_item->get_coefstring();
191 if ($aggcoef == 'aggregationcoefextraweightsum') {
6c37563e
FM
192 // The field 'weight' should only be sent when the checkbox 'weighoverride' is checked,
193 // so there is not need to set weightoverride here, it is done below.
b79bf7a2 194 $value = $value / 100.0;
b79bf7a2
MG
195 $grade_item->aggregationcoef2 = $value;
196 } else if ($aggcoef == 'aggregationcoefweight' || $aggcoef == 'aggregationcoefextraweight') {
197 $grade_item->aggregationcoef = $value;
45da5361 198 }
f13002d5 199
8ad3ee9c 200 $grade_item->update();
f13002d5 201
6c37563e
FM
202 $recreatetree = true;
203
204 // Grade item checkbox inputs.
205 } elseif (preg_match('/^(weightoverride)_([0-9]+)$/', $key, $matches)) {
206 $param = $matches[1];
207 $aid = $matches[2];
208 $value = clean_param($value, PARAM_BOOL);
209
210 $grade_item = grade_item::fetch(array('id' => $aid, 'courseid' => $courseid));
211 $grade_item->$param = $value;
212
213 $grade_item->update();
214
5e212c33 215 $recreatetree = true;
f13002d5 216 }
217 }
32ee0f83 218
e61f25a4
DW
219 $originalweights = grade_helper::fetch_all_natural_weights_for_course($courseid);
220
e61f25a4
DW
221 $alteredweights = grade_helper::fetch_all_natural_weights_for_course($courseid);
222 if (array_diff($originalweights, $alteredweights)) {
223 $normalisationmessage = get_string('weightsadjusted', 'grades');
519429b2 224 }
f13002d5 225}
226
0a802c9c
AN
227if (grade_regrade_final_grades_if_required($course)) {
228 $recreatetree = true;
30be6c84
DW
229}
230
53914e44 231print_grade_page_head($courseid, 'settings', 'setup', get_string('gradebooksetup', 'grades'));
76ea4e1f 232
dc482cfa 233// Print Table of categories and items
291fa827 234echo $OUTPUT->box_start('gradetreebox generalbox');
796a41b5 235
dc482cfa 236echo '<form id="gradetreeform" method="post" action="'.$returnurl.'">';
237echo '<div>';
238echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
796a41b5 239
5e212c33
AD
240//did we update something in the db and thus invalidate $grade_edit_tree?
241if ($recreatetree) {
242 $grade_edit_tree = new grade_edit_tree($gtree, $movingeid, $gpr);
243}
519429b2
AG
244// Check to see if we have a normalisation message to send.
245if (!empty($normalisationmessage)) {
246 echo $OUTPUT->notification($normalisationmessage, 'notifymessage');
247}
5e212c33 248
16be8974 249echo html_writer::table($grade_edit_tree->table);
5c75a0a3 250
dc482cfa 251echo '<div id="gradetreesubmit">';
252if (!$moving) {
253 echo '<input class="advanced" type="submit" value="'.get_string('savechanges').'" />';
254}
255
19d1a53b 256// We don't print a bulk move menu if there are no other categories than course category
54a007e8 257if (!$moving && count($grade_edit_tree->categories) > 1) {
dc482cfa 258 echo '<br /><br />';
259 echo '<input type="hidden" name="bulkmove" value="0" id="bulkmoveinput" />';
ef93efbf 260 $attributes = array('id'=>'menumoveafter', 'class' => 'ignoredirty');
f8e371f0 261 echo html_writer::label(get_string('moveselectedto', 'grades'), 'menumoveafter');
064527d6 262 echo html_writer::select($grade_edit_tree->categories, 'moveafter', '', array(''=>'choosedots'), $attributes);
c80877aa 263 $OUTPUT->add_action_handler(new component_action('change', 'submit_bulk_move'), 'menumoveafter');
2d65597b 264 echo '<div id="noscriptgradetreeform" class="hiddenifjs">
dc482cfa 265 <input type="submit" value="'.get_string('go').'" />
5fdb159e 266 </div>';
dc482cfa 267}
268
269echo '</div>';
796a41b5 270
dc482cfa 271echo '</div></form>';
272
291fa827 273echo $OUTPUT->box_end();
dc482cfa 274
275// Print action buttons
90723839 276echo $OUTPUT->container_start('buttons mdl-align');
dc482cfa 277
278if ($moving) {
5c2ed7e2 279 echo $OUTPUT->single_button(new moodle_url('index.php', array('id'=>$course->id)), get_string('cancel'), 'get');
dc482cfa 280} else {
5c2ed7e2
PS
281 echo $OUTPUT->single_button(new moodle_url('category.php', array('courseid'=>$course->id)), get_string('addcategory', 'grades'), 'get');
282 echo $OUTPUT->single_button(new moodle_url('item.php', array('courseid'=>$course->id)), get_string('additem', 'grades'), 'get');
dc482cfa 283
284 if (!empty($CFG->enableoutcomes)) {
5c2ed7e2 285 echo $OUTPUT->single_button(new moodle_url('outcomeitem.php', array('courseid'=>$course->id)), get_string('addoutcomeitem', 'grades'), 'get');
796a41b5 286 }
287
5c2ed7e2 288 //echo $OUTPUT->(new moodle_url('index.php', array('id'=>$course->id, 'action'=>'autosort')), get_string('autosort', 'grades'), 'get');
796a41b5 289}
f13002d5 290
03fcc729 291echo $OUTPUT->container_end();
dc482cfa 292
742d9165
MN
293$PAGE->requires->yui_module('moodle-core-formchangechecker',
294 'M.core_formchangechecker.init',
295 array(array(
296 'formid' => 'gradetreeform'
297 ))
298);
299$PAGE->requires->string_for_js('changesmadereallygoaway', 'moodle');
300
5a931394 301echo $OUTPUT->footer();
dc482cfa 302die;
303
6c3ef410 304