renaming constant
[moodle-pu.git] / grade / edit / edit_tree.php
blob5fbf92a7adacea983e66e8ed0b05e8a69bc59f06
1 <?php
2 // $Id$
4 ///////////////////////////////////////////////////////////////////////////
5 // //
6 // NOTICE OF COPYRIGHT //
7 // //
8 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
9 // http://moodle.com //
10 // //
11 // Copyright (C) 2001-2007 Martin Dougiamas http://dougiamas.com //
12 // //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 require_once '../../config.php';
28 require_once $CFG->libdir.'/gradelib.php';
30 $courseid = required_param('id', PARAM_INT);
31 $action = optional_param('action', 0, PARAM_ALPHA);
32 $eid = optional_param('eid', 0, PARAM_ALPHANUM);
35 /// Make sure they can even access this course
37 if (!$course = get_record('course', 'id', $courseid)) {
38 print_error('nocourseid');
41 require_login($course);
43 $context = get_context_instance(CONTEXT_COURSE, $course->id);
44 //require_capability() here!!
46 // default return url
47 $returnurl = 'edit_tree.php?id='.$course->id;
49 // get the grading tree object
50 // note: total must be first for moving to work correctly, if you want it last moving code must be rewritten!
51 $gtree = new grade_tree($courseid, false, false, false);
53 if (empty($eid)) {
54 $element = null;
55 $object = null;
57 } else {
58 if (!$element = $gtree->locate_element($eid)) {
59 error('Incorrect element id!', $returnurl);
61 $object = $element['object'];
65 $strgrades = get_string('grades');
66 $strgraderreport = get_string('graderreport', 'grades');
67 $strcategoriesedit = get_string('categoriesedit', 'grades');
69 $nav = array(array('name'=>$strgrades,'link'=>$CFG->wwwroot.'/grade/index.php?id='.$courseid, 'type'=>'misc'),
70 array('name'=>$strcategoriesedit, 'link'=>'', 'type'=>'misc'));
72 $navigation = build_navigation($nav);
73 $moving = false;
75 switch ($action) {
76 case 'edit':
77 if ($eid and confirm_sesskey()) {
78 if ($element['type'] == 'category') {
79 redirect('edit_category.php?courseid='.$course->id.'&amp;id='.$object->id);
80 } else {
81 redirect('edit_item.php?courseid='.$course->id.'&amp;id='.$object->id);
84 break;
86 case 'delete':
87 if ($eid) {
88 $confirm = optional_param('confirm', 0, PARAM_BOOL);
90 if ($confirm and confirm_sesskey()) {
91 $object->delete('grade/report/grader/category');
92 redirect($returnurl);
94 } else {
95 print_header_simple($strgrades . ': ' . $strgraderreport, ': ' . $strcategoriesedit, $navigation, '', '', true, '', navmenu($course));
96 $strdeletecheckfull = get_string('deletecheck', '', $object->get_name());
97 $optionsyes = array('eid'=>$eid, 'confirm'=>1, 'sesskey'=>sesskey(), 'id'=>$course->id, 'action'=>'delete');
98 $optionsno = array('id'=>$course->id);
99 notice_yesno($strdeletecheckfull, 'edit_tree.php', 'edit_tree.php', $optionsyes, $optionsno, 'post', 'get');
100 print_footer($course);
101 die;
104 break;
106 case 'autosort':
107 //TODO: implement autosorting based on order of mods on course page, categories first, manual items last
108 break;
110 case 'synclegacy':
111 grade_grab_legacy_grades($course->id);
112 redirect($returnurl);
114 case 'move':
115 if ($eid and confirm_sesskey()) {
116 $moveafter = required_param('moveafter', PARAM_ALPHANUM);
117 if(!$after_el = $gtree->locate_element($moveafter)) {
118 error('Incorect element id in moveafter', $returnurl);
120 $after = $after_el['object'];
121 $parent = $after->get_parent_category();
122 $sortorder = $after->get_sortorder();
124 $object->set_parent($parent->id);
125 $object->move_after_sortorder($sortorder);
127 redirect($returnurl);
129 break;
131 case 'moveselect':
132 if ($eid and confirm_sesskey()) {
133 $moving = $eid;
135 break;
137 case 'hide':
138 if ($eid and confirm_sesskey()) {
139 $object->set_hidden(1);
140 redirect($returnurl);
142 break;
144 case 'show':
145 if ($eid and confirm_sesskey()) {
146 $object->set_hidden(0);
147 redirect($returnurl);
149 break;
151 case 'lock':
152 if ($eid and confirm_sesskey()) {
153 //TODO: add error handling in redirect
154 $object->set_locked(1);
155 redirect($returnurl);
157 break;
159 case 'unlock':
160 if ($eid and confirm_sesskey()) {
161 $object->set_locked(0);
162 redirect($returnurl);
164 break;
166 default:
167 break;
170 print_header_simple($strgrades . ': ' . $strgraderreport, ': ' . $strcategoriesedit, $navigation, '', '', true, '', navmenu($course));
172 print_heading(get_string('categoriesedit', 'grades'));
174 // Add tabs
175 // TODO: implement return support - use tabs from the report plugin
176 /*$currenttab = 'editcategory';
177 include('tabs.php');*/
179 print_box_start('gradetreebox generalbox');
180 echo '<ul id="grade_tree">';
181 print_grade_tree($gtree->top_element, $moving);
182 echo '</ul>';
183 print_box_end();
185 echo '<div class="buttons">';
186 if ($moving) {
187 print_single_button('edit_tree.php', array('id'=>$course->id), get_string('cancel'), 'get');
188 } else {
189 print_single_button('edit_category.php', array('courseid'=>$course->id), get_string('addcategory', 'grades'), 'get');
190 print_single_button('edit_item.php', array('courseid'=>$course->id), get_string('additem', 'grades'), 'get');
191 print_single_button('edit_tree.php', array('id'=>$course->id, 'action'=>'autosort'), get_string('autosort', 'grades'), 'get');
192 print_single_button('edit_tree.php', array('id'=>$course->id, 'action'=>'synclegacy'), get_string('synclegacygrades', 'grades'), 'get');
194 echo '</div>';
195 print_footer($course);
196 die;
201 function print_grade_tree($element, $moving) {
202 global $CFG, $COURSE;
204 /// fetch needed strings
205 $strmove = get_string('move');
206 $strmovehere = get_string('movehere');
207 $stredit = get_string('edit');
208 $strdelete = get_string('delete');
209 $strhide = get_string('hide');
210 $strshow = get_string('show');
211 $strlock = get_string('lock', 'grades');
212 $strunlock = get_string('unlock', 'grades');
214 $object = $element['object'];
215 $eid = $element['eid'];
217 /// prepare actions
218 $actions = '<a href="edit_tree.php?id='.$COURSE->id.'&amp;action=edit&amp;eid='.$eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" title="'.$stredit.'"/></a>';
220 if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
221 $actions .= '<a href="edit_tree.php?id='.$COURSE->id.'&amp;action=delete&amp;eid='.$eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" title="'.$strdelete.'"/></a>';
222 $actions .= '<a href="edit_tree.php?id='.$COURSE->id.'&amp;action=moveselect&amp;eid='.$eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/move.gif" class="iconsmall" alt="'.$strmove.'" title="'.$strmove.'"/></a>';
225 if ($object->is_locked()) {
226 $actions .= '<a href="edit_tree.php?id='.$COURSE->id.'&amp;action=unlock&amp;eid='.$eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/unlock.gif" class="iconsmall" alt="'.$strunlock.'" title="'.$strunlock.'"/></a>';
227 } else {
228 $actions .= '<a href="edit_tree.php?id='.$COURSE->id.'&amp;action=lock&amp;eid='.$eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/lock.gif" class="iconsmall" alt="'.$strlock.'" title="'.$strlock.'"/></a>';
231 if ($object->is_hidden()) {
232 $name = '<span class="dimmed_text">'.$object->get_name().'</span>';
233 $actions .= '<a href="edit_tree.php?id='.$COURSE->id.'&amp;action=show&amp;eid='.$eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" title="'.$strshow.'"/></a>';
234 } else {
235 $name = $object->get_name();
236 $actions .= '<a href="edit_tree.php?id='.$COURSE->id.'&amp;action=hide&amp;eid='.$eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" title="'.$strhide.'"/></a>';
239 /// prepare icon
240 $icon = '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="icon" alt=""/>';
241 switch ($element['type']) {
242 case 'item':
243 if ($object->itemtype == 'mod') {
244 $icon = '<img src="'.$CFG->modpixpath.'/'.$object->itemmodule.'/icon.gif" class="icon" alt="'.get_string('modulename', $object->itemmodule).'"/>';
245 } else if ($object->itemtype == 'manual') {
246 //TODO: add manual grading icon
247 $icon = '<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon" alt="'.get_string('manualgrade', 'grades').'"/>'; // TODO: localize
249 break;
250 case 'courseitem':
251 case 'categoryitem':
252 $icon = '<img src="'.$CFG->pixpath.'/i/category_grade.gif" class="icon" alt="'.get_string('categorygrade').'"/>'; // TODO: localize
253 break;
254 case 'category':
255 $icon = '<img src="'.$CFG->pixpath.'/f/folder.gif" class="icon" alt="'.get_string('category').'"/>';
256 break;
259 /// prepare move target if needed
260 $moveto = '';
261 if ($moving) {
262 $actions = ''; // no action icons when moving
263 $moveto = '<li><a href="edit_tree.php?id='.$COURSE->id.'&amp;action=move&amp;eid='.$moving.'&amp;moveafter='.$eid.'&amp;sesskey='.sesskey().'"><img class="movetarget" src="'.$CFG->wwwroot.'/pix/movehere.gif" alt="'.$strmovehere.'" title="'.$strmovehere.'" /></a></li>';
266 /// print the list items now
267 if ($moving == $eid) {
268 // do not diplay children
269 echo '<li class="'.$element['type'].' moving">'.$icon.$name.'('.get_string('move').')</li>';
271 } else if ($element['type'] != 'category') {
272 echo '<li class="'.$element['type'].'">'.$icon.$name.$actions.'</li>'.$moveto;
274 } else {
275 echo '<li class="'.$element['type'].'">'.$icon.$name.$actions;
276 echo '<ul class="catlevel'.$element['depth'].'">';
277 foreach($element['children'] as $child_el) {
278 print_grade_tree($child_el, $moving);
280 echo '</ul></li>';
281 if ($element['depth'] > 1) {
282 echo $moveto; // can not move after the top category