moodle_page: MDL-12212 reimplement user_is_editing, deprecate isediting
[moodle.git] / course / index.php
CommitLineData
24f2b65a 1<?php // $Id$
d2b6ba70 2 // For most people, just lists the course categories
3 // Allows the admin to create, delete and rename course categories
f9903ed0 4
6bc502cc 5 require_once("../config.php");
6 require_once("lib.php");
f9903ed0 7
7ff99cb6 8 $categoryedit = optional_param('categoryedit', -1,PARAM_BOOL);
9d6a11f8 9 $delete = optional_param('delete',0,PARAM_INT);
10 $hide = optional_param('hide',0,PARAM_INT);
11 $show = optional_param('show',0,PARAM_INT);
9d6a11f8 12 $move = optional_param('move',0,PARAM_INT);
13 $moveto = optional_param('moveto',-1,PARAM_INT);
14 $moveup = optional_param('moveup',0,PARAM_INT);
15 $movedown = optional_param('movedown',0,PARAM_INT);
e2b347e9 16
8ed5dd63 17 if ($CFG->forcelogin) {
18 require_login();
19 }
7168423c 20
d2b6ba70 21 if (!$site = get_site()) {
ba6018a9 22 print_error('siteisnotdefined', 'debug');
d2b6ba70 23 }
24
8ed5dd63 25 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
29aab4ac 26
8ed5dd63 27 if (update_category_button()) {
7ff99cb6 28 if ($categoryedit !== -1) {
830dd6e9 29 $USER->editing = $categoryedit;
d2b6ba70 30 }
830dd6e9 31 $adminediting = $PAGE->user_is_editing();
9d6a11f8 32 } else {
33 $adminediting = false;
d2b6ba70 34 }
35
fb31c40a 36 $stradministration = get_string('administration');
37 $strcategories = get_string('categories');
38 $strcategory = get_string('category');
39 $strcourses = get_string('courses');
40 $stredit = get_string('edit');
41 $strdelete = get_string('delete');
42 $straction = get_string('action');
43
44
d2b6ba70 45/// Unless it's an editing admin, just print the regular listing of courses/categories
d2b6ba70 46 if (!$adminediting) {
fb31c40a 47
8ed5dd63 48 /// Print form for creating new categories
e4907498 49 $countcategories = $DB->count_records('course_categories');
d2b6ba70 50
e4907498 51 if ($countcategories > 1 || ($countcategories == 1 && $DB->count_records('course') > 200)) {
378ef1fe 52 $strcourses = get_string('courses');
53 $strcategories = get_string('categories');
ba7472cc 54
55 $navlinks = array();
56 $navlinks[] = array('name'=>$strcategories,'link'=>'','type'=>'misc');
57 $navigation = build_navigation($navlinks);
8ed5dd63 58 print_header("$site->shortname: $strcategories", $strcourses, $navigation, '', '', true, update_category_button());
c0b85daf 59 print_heading($strcategories);
317d5ddc 60 echo skip_main_destination();
44b843b6 61 print_box_start('categorybox');
d2b6ba70 62 print_whole_category_list();
44b843b6 63 print_box_end();
38a10939 64 print_course_search();
d2b6ba70 65 } else {
378ef1fe 66 $strfulllistofcourses = get_string('fulllistofcourses');
0be6f678 67 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses,
68 build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))),
8ed5dd63 69 '', '', true, update_category_button());
317d5ddc 70 echo skip_main_destination();
2cae9a4f 71 print_box_start('courseboxes');
35d0244a 72 print_courses(0);
2cae9a4f 73 print_box_end();
d2b6ba70 74 }
c432fd32 75
8ed5dd63 76 echo '<div class="buttons">';
8ed5dd63 77 if (has_capability('moodle/course:create', $systemcontext)) {
78 /// Print link to create a new course
d9bb8caa 79 /// Get the 1st available category
8ed5dd63 80 $options = array('category' => $CFG->defaultrequestcategory);
d9bb8caa 81 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
c432fd32 82 }
77eddcd5 83 print_course_request_buttons($systemcontext);
8ed5dd63 84 echo '</div>';
d2b6ba70 85 print_footer();
86 exit;
87 }
8ed5dd63 88/// Everything else is editing on mode.
d2b6ba70 89
8ed5dd63 90/// Delete a category.
e2b347e9 91 if (!empty($delete) and confirm_sesskey()) {
e4907498 92 if (!$deletecat = $DB->get_record('course_categories', array('id'=>$delete))) {
9b34dc6e 93 print_error('invalidcategoryid');
e2b347e9 94 }
e2b347e9 95 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
8ed5dd63 96 require_capability('moodle/category:manage', $context);
97 require_capability('moodle/category:manage', get_category_or_system_context($deletecat->parent));
0be6f678 98
8ed5dd63 99 $heading = get_string('deletecategory', '', format_string($deletecat->name));
100 require_once('delete_category_form.php');
e2b347e9 101 $mform = new delete_category_form(null, $deletecat);
102 $mform->set_data(array('delete'=>$delete));
0be6f678 103
e2b347e9 104 if ($mform->is_cancelled()) {
105 redirect('index.php');
0be6f678 106
294ce987 107 } else if (!$data= $mform->get_data()) {
e2b347e9 108 require_once($CFG->libdir . '/questionlib.php');
109 print_category_edit_header();
110 print_heading($heading);
e2b347e9 111 $mform->display();
8ed5dd63 112 admin_externalpage_print_footer();
e2b347e9 113 exit();
d2b6ba70 114 }
e2b347e9 115
116 print_category_edit_header();
117 print_heading($heading);
118
2f37e108 119 if ($data->fulldelete) {
a3a1708f 120 $deletedcourses = category_delete_full($deletecat, true);
121
122 foreach($deletedcourses as $course) {
123 notify(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
124 }
125 notify(get_string('coursecategorydeleted', '', format_string($deletecat->name)), 'notifysuccess');
126
e2b347e9 127 } else {
128 category_delete_move($deletecat, $data->newparent, true);
129 }
130
8ed5dd63 131 // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
132 if ($delete == $CFG->defaultrequestcategory) {
d02bcc3c 133 set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent'=>0)));
8ed5dd63 134 }
135
e2b347e9 136 print_continue('index.php');
137
8ed5dd63 138 admin_externalpage_print_footer();
e2b347e9 139 die;
ba2e5d73 140 }
0a263205 141
e2b347e9 142/// Print headings
143 print_category_edit_header();
144 print_heading($strcategories);
145
d2b6ba70 146/// Create a default category if necessary
147 if (!$categories = get_categories()) { /// No category yet!
148 // Try and make one
e4907498 149 $tempcat = new object();
378ef1fe 150 $tempcat->name = get_string('miscellaneous');
e4907498 151 if (!$tempcat->id = $DB->insert_record('course_categories', $tempcat)) {
ba6018a9 152 print_error('cannotsetupcategory');
d2b6ba70 153 }
1d28d87b 154 $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id);
155 mark_context_dirty('/'.SYSCONTEXTID);
7a9d505b 156 fix_course_sortorder(); // Required to build course_categories.depth and .path.
d2b6ba70 157 }
158
d2b6ba70 159/// Move a category to a new parent if required
8ed5dd63 160 if (!empty($move) and ($moveto >= 0) and confirm_sesskey()) {
161 if ($cattomove = $DB->get_record('course_categories', array('id'=>$move))) {
162 require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent));
163 if ($cattomove->parent != $moveto) {
164 $newparent = $DB->get_record('course_categories', array('id'=>$moveto));
165 require_capability('moodle/category:manage', get_category_or_system_context($moveto));
90ee78e2 166 move_category($cattomove, $newparent);
d2b6ba70 167 }
168 }
169 }
170
0be6f678 171/// Hide or show a category
7168423c 172 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
173 if (!empty($hide)) {
e4907498 174 $tempcat = $DB->get_record('course_categories', array('id'=>$hide));
d2b6ba70 175 $visible = 0;
176 } else {
e4907498 177 $tempcat = $DB->get_record('course_categories', array('id'=>$show));
d2b6ba70 178 $visible = 1;
179 }
8ed5dd63 180 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
d2b6ba70 181 if ($tempcat) {
90ee78e2 182 $DB->set_field('course_categories', 'visible', $visible, array('id'=>$tempcat->id));
183 $DB->set_field('course', 'visible', $visible, array('category' => $tempcat->id));
d2b6ba70 184 }
185 }
186
d2b6ba70 187/// Move a category up or down
7168423c 188 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
0cbe8111 189 fix_course_sortorder();
d2b6ba70 190 $swapcategory = NULL;
d2b6ba70 191
7168423c 192 if (!empty($moveup)) {
8ed5dd63 193 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveup));
e4907498 194 if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) {
0cbe8111 195 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) {
196 $swapcategory = reset($swapcategory);
d2b6ba70 197 }
198 }
0cbe8111 199 } else {
8ed5dd63 200 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $movedown));
e4907498 201 if ($movecategory = $DB->get_record('course_categories', array('id'=>$movedown))) {
0cbe8111 202 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) {
203 $swapcategory = reset($swapcategory);
d2b6ba70 204 }
205 }
206 }
0cbe8111 207 if ($swapcategory and $movecategory) {
208 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id'=>$movecategory->id));
209 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id'=>$swapcategory->id));
d2b6ba70 210 }
d2b6ba70 211
0cbe8111 212 // finally reorder courses
213 fix_course_sortorder();
214 }
0be6f678 215
d2b6ba70 216/// Print out the categories with all the knobs
378ef1fe 217 $strcategories = get_string('categories');
218 $strcourses = get_string('courses');
219 $strmovecategoryto = get_string('movecategoryto');
220 $stredit = get_string('edit');
d2b6ba70 221
222 $displaylist = array();
223 $parentlist = array();
224
378ef1fe 225 $displaylist[0] = get_string('top');
8ed5dd63 226 make_categories_list($displaylist, $parentlist);
d2b6ba70 227
fb31c40a 228 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
378ef1fe 229 echo '<th class="header" scope="col">'.$strcategories.'</th>';
230 echo '<th class="header" scope="col">'.$strcourses.'</th>';
231 echo '<th class="header" scope="col">'.$stredit.'</th>';
232 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
233 echo '</tr>';
d2b6ba70 234
235 print_category_edit(NULL, $displaylist, $parentlist);
378ef1fe 236 echo '</table>';
d2b6ba70 237
571880d2 238 echo '<div class="buttons">';
8ed5dd63 239 if (has_capability('moodle/course:create', $systemcontext)) {
240 // print create course link to first category
241 $options = array();
242 $options = array('category' => $CFG->defaultrequestcategory);
243 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
bbbf2d40 244 }
0be6f678 245
86830be6 246 // Print button for creating new categories
8ed5dd63 247 if (has_capability('moodle/category:manage', $systemcontext)) {
248 $options = array();
249 $options['parent'] = 0;
86830be6 250 print_single_button('editcategory.php', $options, get_string('addnewcategory'), 'get');
251 }
252
77eddcd5 253 print_course_request_buttons($systemcontext);
04c53106 254 echo '</div>';
571880d2 255
8ed5dd63 256 admin_externalpage_print_footer();
d2b6ba70 257
258function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
259/// Recursive function to print all the categories ready for editing
260
810393c8 261 global $CFG, $USER;
d2b6ba70 262
8ed5dd63 263 static $str = NULL;
0be6f678 264
8ed5dd63 265 if (is_null($str)) {
266 $str = new stdClass;
86830be6 267 $str->edit = get_string('edit');
378ef1fe 268 $str->delete = get_string('delete');
269 $str->moveup = get_string('moveup');
270 $str->movedown = get_string('movedown');
271 $str->edit = get_string('editthiscategory');
272 $str->hide = get_string('hide');
273 $str->show = get_string('show');
8ed5dd63 274 $str->spacer = '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
d2b6ba70 275 }
0be6f678 276
8ed5dd63 277 if (!empty($category)) {
378ef1fe 278
dfbf98cd 279 if (!isset($category->context)) {
280 $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id);
281 }
0be6f678 282
378ef1fe 283 echo '<tr><td align="left" class="name">';
d2b6ba70 284 for ($i=0; $i<$depth;$i++) {
378ef1fe 285 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
d2b6ba70 286 }
378ef1fe 287 $linkcss = $category->visible ? '' : ' class="dimmed" ';
288 echo '<a '.$linkcss.' title="'.$str->edit.'" '.
289 ' href="category.php?id='.$category->id.'&amp;categoryedit=on&amp;sesskey='.sesskey().'">'.
6ba65fa0 290 format_string($category->name).'</a>';
378ef1fe 291 echo '</td>';
d2b6ba70 292
378ef1fe 293 echo '<td class="count">'.$category->coursecount.'</td>';
d2b6ba70 294
378ef1fe 295 echo '<td class="icons">'; /// Print little icons
d2b6ba70 296
8ed5dd63 297 if (has_capability('moodle/category:manage', $category->context)) {
298 echo '<a title="'.$str->edit.'" href="editcategory.php?id='.$category->id.'"><img'.
86830be6 299 ' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$str->edit.'" /></a> ';
86830be6 300
378ef1fe 301 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
302 ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$str->delete.'" /></a> ';
0be6f678 303
61240489 304 if (!empty($category->visible)) {
378ef1fe 305 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
306 ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$str->hide.'" /></a> ';
61240489 307 } else {
378ef1fe 308 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
309 ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$str->show.'" /></a> ';
61240489 310 }
d2b6ba70 311
8ed5dd63 312 if ($up) {
313 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
314 ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
315 } else {
316 echo $str->spacer;
317 }
318 if ($down) {
319 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
320 ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
321 } else {
322 echo $str->spacer;
323 }
d2b6ba70 324 }
378ef1fe 325 echo '</td>';
d2b6ba70 326
378ef1fe 327 echo '<td align="left">';
8ed5dd63 328 if (has_capability('moodle/category:manage', $category->context)) {
329 $tempdisplaylist = $displaylist;
330 unset($tempdisplaylist[$category->id]);
331 foreach ($parentslist as $key => $parents) {
332 if (in_array($category->id, $parents)) {
333 unset($tempdisplaylist[$key]);
334 }
d2b6ba70 335 }
973d2660 336 popup_form ("index.php?move=$category->id&amp;sesskey=".sesskey()."&amp;moveto=", $tempdisplaylist, "moveform$category->id", $category->parent, '', '', '', false);
d2b6ba70 337 }
378ef1fe 338 echo '</td>';
339 echo '</tr>';
c2cb4545 340 } else {
378ef1fe 341 $category->id = '0';
c2cb4545 342 }
f9903ed0 343
d2b6ba70 344 if ($categories = get_categories($category->id)) { // Print all the children recursively
345 $countcats = count($categories);
346 $count = 0;
347 $first = true;
348 $last = false;
349 foreach ($categories as $cat) {
350 $count++;
351 if ($count == $countcats) {
352 $last = true;
353 }
354 $up = $first ? false : true;
355 $down = $last ? false : true;
356 $first = false;
f9903ed0 357
0be6f678 358 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);
d2b6ba70 359 }
360 }
361}
e2b347e9 362
363function print_category_edit_header() {
364 global $CFG;
ab7ac6ac 365 global $SITE;
e2b347e9 366
8ed5dd63 367 require_once($CFG->libdir.'/adminlib.php');
92f00846 368 admin_externalpage_setup('coursemgmt', update_category_button());
8ed5dd63 369 admin_externalpage_print_header();
e2b347e9 370}
d2b6ba70 371?>