MDL-47006 core_grades: Change category and item forms back to using
[moodle.git] / grade / edit / tree / item.php
blob837b5f81f7bec1882602a461bb5a4b782679827e
1 <?php
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.
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/>.
17 /**
18 * Edit the grade options for an individual grade item
20 * @package core_grades
21 * @copyright 2007 Petr Skoda
22 * @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->dirroot.'/grade/report/lib.php';
29 require_once 'item_form.php';
31 $courseid = required_param('courseid', PARAM_INT);
32 $id = optional_param('id', 0, PARAM_INT);
34 $url = new moodle_url('/grade/edit/tree/item.php', array('courseid'=>$courseid));
35 if ($id !== 0) {
36 $url->param('id', $id);
38 $PAGE->set_url($url);
39 $PAGE->set_pagelayout('admin');
41 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
42 print_error('nocourseid');
45 require_login($course);
46 $context = context_course::instance($course->id);
47 require_capability('moodle/grade:manage', $context);
49 // default return url
50 $gpr = new grade_plugin_return();
51 $returnurl = $gpr->get_return_url('index.php?id='.$course->id);
53 $heading = get_string('itemsedit', 'grades');
55 if ($grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$courseid))) {
56 // redirect if outcomeid present
57 if (!empty($grade_item->outcomeid) && !empty($CFG->enableoutcomes)) {
58 $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?id='.$id.'&amp;courseid='.$courseid;
59 redirect($gpr->add_url_params($url));
61 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
62 $grade_category = $grade_item->get_item_category();
63 $url = $CFG->wwwroot.'/grade/edit/tree/category.php?id='.$grade_category->id.'&amp;courseid='.$courseid;
64 redirect($gpr->add_url_params($url));
67 $item = $grade_item->get_record_data();
68 $parent_category = $grade_item->get_parent_category();
69 $item->parentcategory = $parent_category->id;
71 } else {
72 $heading = get_string('newitem', 'grades');
73 $grade_item = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual'), false);
74 $item = $grade_item->get_record_data();
75 $parent_category = grade_category::fetch_course_category($courseid);
76 $item->parentcategory = $parent_category->id;
78 $decimalpoints = $grade_item->get_decimals();
80 if ($item->hidden > 1) {
81 $item->hiddenuntil = $item->hidden;
82 $item->hidden = 0;
83 } else {
84 $item->hiddenuntil = 0;
87 $item->locked = !empty($item->locked);
89 $item->grademax = format_float($item->grademax, $decimalpoints);
90 $item->grademin = format_float($item->grademin, $decimalpoints);
91 $item->gradepass = format_float($item->gradepass, $decimalpoints);
92 $item->multfactor = format_float($item->multfactor, 4);
93 $item->plusfactor = format_float($item->plusfactor, 4);
95 if ($parent_category->aggregation == GRADE_AGGREGATE_SUM or $parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN2) {
96 $item->aggregationcoef = $item->aggregationcoef == 0 ? 0 : 1;
97 } else {
98 $item->aggregationcoef = format_float($item->aggregationcoef, 4);
100 if ($parent_category->aggregation == GRADE_AGGREGATE_SUM) {
101 $item->aggregationcoef2 = format_float($item->aggregationcoef2 * 100.0);
103 $item->cancontrolvisibility = $grade_item->can_control_visibility();
105 $mform = new edit_item_form(null, array('current'=>$item, 'gpr'=>$gpr));
107 if ($mform->is_cancelled()) {
108 redirect($returnurl);
110 } else if ($data = $mform->get_data(false)) {
111 // If unset, give the aggregationcoef a default based on parent aggregation method
112 if (!isset($data->aggregationcoef) || $data->aggregationcoef == '') {
113 if ($parent_category->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
114 $data->aggregationcoef = 1;
115 } else {
116 $data->aggregationcoef = 0;
120 if (!isset($data->gradepass) || $data->gradepass == '') {
121 $data->gradepass = 0;
124 if (!isset($data->grademin) || $data->grademin == '') {
125 $data->grademin = 0;
128 $hidden = empty($data->hidden) ? 0: $data->hidden;
129 $hiddenuntil = empty($data->hiddenuntil) ? 0: $data->hiddenuntil;
130 unset($data->hidden);
131 unset($data->hiddenuntil);
133 $locked = empty($data->locked) ? 0: $data->locked;
134 $locktime = empty($data->locktime) ? 0: $data->locktime;
135 unset($data->locked);
136 unset($data->locktime);
138 $convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef', 'aggregationcoef2');
139 foreach ($convert as $param) {
140 if (property_exists($data, $param)) {
141 $data->$param = unformat_float($data->$param);
144 if (isset($data->aggregationcoef2) && $parent_category->aggregation == GRADE_AGGREGATE_SUM) {
145 $data->aggregationcoef2 = $data->aggregationcoef2 / 100.0;
148 $grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid));
149 grade_item::set_properties($grade_item, $data);
150 $grade_item->outcomeid = null;
152 // Handle null decimals value
153 if (!property_exists($data, 'decimals') or $data->decimals < 0) {
154 $grade_item->decimals = null;
157 if (empty($grade_item->id)) {
158 $grade_item->itemtype = 'manual'; // all new items to be manual only
159 $grade_item->insert();
161 // set parent if needed
162 if (isset($data->parentcategory)) {
163 $grade_item->set_parent($data->parentcategory, 'gradebook');
166 } else {
167 $grade_item->update();
170 // update hiding flag
171 if ($hiddenuntil) {
172 $grade_item->set_hidden($hiddenuntil, false);
173 } else {
174 $grade_item->set_hidden($hidden, false);
177 $grade_item->set_locktime($locktime); // locktime first - it might be removed when unlocking
178 $grade_item->set_locked($locked, false, true);
180 redirect($returnurl);
183 $return = false;
184 $buttons = false;
185 $shownavigation = false;
186 print_grade_page_head($courseid, 'edittree', null, $heading, $return, $buttons, $shownavigation);
188 $mform->display();
190 echo $OUTPUT->footer();