Fixed a little typo
[moodle-pu.git] / grade / edit / tree / index.php
blobe0366d06e5fec3080e4e582451f2008fbedc540e
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once '../../../config.php';
27 require_once $CFG->dirroot.'/grade/lib.php';
28 require_once $CFG->dirroot.'/grade/report/lib.php'; // for preferences
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);
42 $context = get_context_instance(CONTEXT_COURSE, $course->id);
43 require_capability('moodle/grade:manage', $context);
45 /// return tracking object
46 $gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'tree', 'courseid'=>$courseid));
47 $returnurl = $gpr->get_return_url(null);
49 //first make sure we have proper final grades - we need it for locking changes
50 grade_regrade_final_grades($courseid);
52 // get the grading tree object
53 // note: total must be first for moving to work correctly, if you want it last moving code must be rewritten!
54 $gtree = new grade_tree($courseid, false, false);
56 if (empty($eid)) {
57 $element = null;
58 $object = null;
60 } else {
61 if (!$element = $gtree->locate_element($eid)) {
62 error('Incorrect element id!', $returnurl);
64 $object = $element['object'];
67 $switch = grade_report::get_pref('aggregationposition');
69 $strgrades = get_string('grades');
70 $strgraderreport = get_string('graderreport', 'grades');
71 $strcategoriesedit = get_string('categoriesedit', 'grades');
72 $strcategoriesanditems = get_string('categoriesanditems', 'grades');
74 $navigation = grade_build_nav(__FILE__, $strcategoriesanditems, array('courseid' => $courseid));
75 $moving = false;
77 switch ($action) {
78 case 'delete':
79 if ($eid) {
80 $confirm = optional_param('confirm', 0, PARAM_BOOL);
82 if ($confirm and confirm_sesskey()) {
83 $object->delete('grade/report/grader/category');
84 redirect($returnurl);
86 } else {
87 print_header_simple($strgrades . ': ' . $strgraderreport, ': ' . $strcategoriesedit, $navigation, '', '', true, '', navmenu($course));
88 $strdeletecheckfull = get_string('deletecheck', '', $object->get_name());
89 $optionsyes = array('eid'=>$eid, 'confirm'=>1, 'sesskey'=>sesskey(), 'id'=>$course->id, 'action'=>'delete');
90 $optionsno = array('id'=>$course->id);
91 notice_yesno($strdeletecheckfull, 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
92 print_footer($course);
93 die;
96 break;
98 case 'autosort':
99 //TODO: implement autosorting based on order of mods on course page, categories first, manual items last
100 break;
102 case 'synclegacy':
103 grade_grab_legacy_grades($course->id);
104 redirect($returnurl);
106 case 'move':
107 if ($eid and confirm_sesskey()) {
108 $moveafter = required_param('moveafter', PARAM_ALPHANUM);
109 if(!$after_el = $gtree->locate_element($moveafter)) {
110 error('Incorect element id in moveafter', $returnurl);
112 $after = $after_el['object'];
113 $parent = $after->get_parent_category();
114 $sortorder = $after->get_sortorder();
116 $object->set_parent($parent->id);
117 $object->move_after_sortorder($sortorder);
119 redirect($returnurl);
121 break;
123 case 'moveselect':
124 if ($eid and confirm_sesskey()) {
125 $moving = $eid;
127 break;
129 default:
130 break;
133 print_header_simple($strgrades . ': ' . $strgraderreport, ': ' . $strcategoriesedit, $navigation, '', '', true, '', navmenu($course));
135 /// Print the plugin selector at the top
136 print_grade_plugin_selector($courseid, 'edit', 'tree');
138 print_heading(get_string('categoriesedit', 'grades'));
141 print_box_start('gradetreebox generalbox');
142 echo '<ul id="grade_tree">';
143 print_grade_tree($gtree, $gtree->top_element, $moving, $gpr, $switch);
144 echo '</ul>';
145 print_box_end();
147 echo '<div class="buttons">';
148 if ($moving) {
149 print_single_button('index.php', array('id'=>$course->id), get_string('cancel'), 'get');
150 } else {
151 print_single_button('category.php', array('courseid'=>$course->id), get_string('addcategory', 'grades'), 'get');
152 print_single_button('item.php', array('courseid'=>$course->id), get_string('additem', 'grades'), 'get');
153 if (!empty($CFG->enableoutcomes)) {
154 print_single_button('outcomeitem.php', array('courseid'=>$course->id), get_string('addoutcomeitem', 'grades'), 'get');
156 //print_single_button('index.php', array('id'=>$course->id, 'action'=>'autosort'), get_string('autosort', 'grades'), 'get');
157 print_single_button('index.php', array('id'=>$course->id, 'action'=>'synclegacy'), get_string('synclegacygrades', 'grades'), 'get');
159 echo '</div>';
160 print_footer($course);
161 die;
163 function print_grade_tree(&$gtree, $element, $moving, &$gpr, $switch, $switchedlast=false) {
164 global $CFG, $COURSE;
166 /// fetch needed strings
167 $strmove = get_string('move');
168 $strmovehere = get_string('movehere');
169 $strdelete = get_string('delete');
171 $object = $element['object'];
172 $eid = $element['eid'];
174 /// prepare actions
175 $actions = $gtree->get_edit_icon($element, $gpr);
177 if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
178 $actions .= '<a href="index.php?id='.$COURSE->id.'&amp;action=delete&amp;eid='
179 . $eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'
180 . $strdelete.'" title="'.$strdelete.'"/></a>';
181 $actions .= '<a href="index.php?id='.$COURSE->id.'&amp;action=moveselect&amp;eid='
182 . $eid.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/t/move.gif" class="iconsmall" alt="'
183 . $strmove.'" title="'.$strmove.'"/></a>';
186 $actions .= $gtree->get_locking_icon($element, $gpr);
188 $name = $object->get_name();
190 //TODO: improve outcome visualisation
191 if ($element['type'] == 'item' and !empty($object->outcomeid)) {
192 $name = $name.' ('.get_string('outcome', 'grades').')';
195 if ($object->is_hidden()) {
196 $name = '<span class="dimmed_text">'.$name.'</span>';
198 $actions .= $gtree->get_hiding_icon($element, $gpr);
200 /// prepare icon
201 $icon = '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="icon" alt=""/>';
202 $last = '';
203 $catcourseitem = false;
204 switch ($element['type']) {
205 case 'item':
206 if ($object->itemtype == 'mod') {
207 $icon = '<img src="'.$CFG->modpixpath.'/'.$object->itemmodule.'/icon.gif" class="icon" alt="'
208 . get_string('modulename', $object->itemmodule).'"/>';
209 } else if ($object->itemtype == 'manual') {
210 //TODO: add manual grading icon
211 if (empty($object->outcomeid)) {
212 $icon = '<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon" alt="'
213 . get_string('manualgrade', 'grades').'"/>'; // TODO: localize
214 } else {
215 $icon = '<img src="'.$CFG->pixpath.'/i/outcomes.gif" class="icon" alt="'
216 . get_string('outcome', 'grades').'"/>';
220 break;
221 case 'courseitem':
222 case 'categoryitem':
223 $icon = '<img src="'.$CFG->pixpath.'/i/category_grade.gif" class="icon" alt="'.get_string('categorygrade').'"/>'; // TODO: localize
224 $catcourseitem = true;
225 break;
226 case 'category':
227 $icon = '<img src="'.$CFG->pixpath.'/f/folder.gif" class="icon" alt="'.get_string('category').'"/>';
228 break;
231 /// prepare move target if needed
232 $moveto = '';
233 if ($moving) {
234 $actions = ''; // no action icons when moving
235 $moveto = '<li><a href="index.php?id='.$COURSE->id.'&amp;action=move&amp;eid='.$moving.'&amp;moveafter='
236 . $eid.'&amp;sesskey='.sesskey().'"><img class="movetarget" src="'.$CFG->wwwroot.'/pix/movehere.gif" alt="'
237 . $strmovehere.'" title="'.$strmovehere.'" /></a></li>';
240 /// print the list items now
241 if ($moving == $eid) {
242 // do not diplay children
243 echo '<li class="'.$element['type'].' moving">'.$icon.$name.'('.get_string('move').')</li>';
245 } else if ($element['type'] != 'category') {
246 if ($catcourseitem and $switch) {
247 if ($switchedlast) {
248 echo '<li class="'.$element['type'].'">'.$icon.$name.$actions.'</li>';
249 } else {
250 echo $moveto;
252 } else {
253 echo '<li class="'.$element['type'].'">'.$icon.$name.$actions.'</li>'.$moveto;
256 } else {
257 echo '<li class="'.$element['type'].'">'.$icon.$name.$actions;
258 echo '<ul class="catlevel'.$element['depth'].'">';
259 $last = null;
260 foreach($element['children'] as $child_el) {
261 if ($switch and empty($last)) {
262 $last = $child_el;
264 print_grade_tree($gtree, $child_el, $moving, $gpr, $switch);
266 if ($last) {
267 print_grade_tree($gtree, $last, $moving, $gpr, $switch, true);
269 echo '</ul></li>';
270 if ($element['depth'] > 1) {
271 echo $moveto; // can not move after the top category