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