MDL-11328 Added the missing variable
[moodle-pu.git] / course / category.php
blobffa1d8332e863c831b438b48c92677c374aa57fe
1 <?php // $Id$
2 // Displays the top level category or all courses
3 // In editing mode, allows the admin to edit a category,
4 // and rearrange courses
6 require_once("../config.php");
7 require_once("lib.php");
8 require_once('category_add_form.php');
10 $id = required_param('id', PARAM_INT); // Category id
11 $page = optional_param('page', 0, PARAM_INT); // which page to show
12 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
13 $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
14 $hide = optional_param('hide', 0, PARAM_INT);
15 $show = optional_param('show', 0, PARAM_INT);
16 $moveup = optional_param('moveup', 0, PARAM_INT);
17 $movedown = optional_param('movedown', 0, PARAM_INT);
18 $moveto = optional_param('moveto', 0, PARAM_INT);
19 $rename = optional_param('rename', '', PARAM_NOTAGS);
20 $resort = optional_param('resort', 0, PARAM_BOOL);
21 $categorytheme= optional_param('categorytheme', false, PARAM_CLEAN);
23 if (!$site = get_site()) {
24 error("Site isn't defined!");
27 $context = get_context_instance(CONTEXT_COURSECAT, $id);
29 if ($CFG->forcelogin) {
30 require_login();
33 if (!$category = get_record("course_categories", "id", $id)) {
34 error("Category not known!");
37 if (has_capability('moodle/course:create', $context)) {
38 if ($categoryedit !== -1) {
39 $USER->categoryediting = $categoryedit;
41 $navbaritem = update_category_button($category->id);
42 $creatorediting = !empty($USER->categoryediting);
43 $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $creatorediting);
45 } else {
46 if (!$category->visible) {
47 error(get_string('notavailable', 'error'));
49 $navbaritem = print_course_search("", true, "navbar");
50 $adminediting = false;
51 $creatorediting = false;
54 $mform = new sub_category_add_form();
55 if (has_capability('moodle/category:create', $context)) {
56 if ($form = $mform->get_data()) {
57 $subcategory = new stdClass;
58 $subcategory->name = $form->addcategory;
59 $subcategory->description = $form->description;
60 $subcategory->sortorder = 999;
61 $subcategory->parent = $id;
62 if (!insert_record('course_categories', $subcategory )) {
63 notify( "Could not insert the new subcategory '$addsubcategory' " );
68 if (has_capability('moodle/category:update', $context)) {
69 /// Rename the category if requested
70 if (!empty($rename) and confirm_sesskey()) {
71 $category->name = $rename;
72 if (! set_field("course_categories", "name", $category->name, "id", $category->id)) {
73 notify("An error occurred while renaming the category");
75 // MDL-9983
76 events_trigger('category_updated', $category);
79 /// Set the category theme if requested
80 if (($categorytheme !== false) and confirm_sesskey()) {
81 $category->theme = $categorytheme;
82 if (! set_field('course_categories', 'theme', $category->theme, 'id', $category->id)) {
83 notify('An error occurred while setting the theme');
84 } else {
85 theme_setup();
89 /// Resort the category if requested
91 if ($resort and confirm_sesskey()) {
92 if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
93 // move it off the range
94 $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
95 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
96 $count = $count->max + 100;
97 begin_sql();
98 foreach ($courses as $course) {
99 set_field('course', 'sortorder', $count, 'id', $course->id);
100 $count++;
102 commit_sql();
103 fix_course_sortorder($category->id);
109 /// Print headings
111 $numcategories = count_records("course_categories");
113 $stradministration = get_string("administration");
114 $strcategories = get_string("categories");
115 $strcategory = get_string("category");
116 $strcourses = get_string("courses");
118 $navlinks = array();
119 $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc');
120 $navlinks[] = array('name' => $category->name, 'link' => null, 'type' => 'misc');
121 $navigation = build_navigation($navlinks);
123 if ($creatorediting) {
124 if ($adminediting) {
125 // modify this to treat this as an admin page
127 require_once($CFG->libdir.'/adminlib.php');
128 admin_externalpage_setup('coursemgmt');
129 admin_externalpage_print_header();
130 } else {
131 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, "", "", true, $navbaritem);
133 } else {
134 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, "", "", true, $navbaritem);
137 /// Print button to turn editing off
138 if ($adminediting) {
139 echo '<div class="categoryediting button" align="right">'.update_category_button($category->id).'</div>';
142 /// Print link to roles
144 if (has_capability('moodle/role:assign', $context)) {
145 echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
146 $context->id.'">'.get_string('assignroles','role').'</a></div>';
148 /// Print the category selector
150 $displaylist = array();
151 $parentlist = array();
153 make_categories_list($displaylist, $parentlist, "");
155 echo '<div class="categorypicker">';
156 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
157 echo '</div>';
159 /// Print current category description
160 if ($category->description) {
161 print_box_start();
162 print_heading(get_string('description'));
163 echo $category->description;
164 print_box_end();
167 /// Editing functions
169 if ($creatorediting) {
170 /// Move a specified course to a new category
172 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
174 // user must have category update in both cats to perform this
175 require_capability('moodle/category:update', $context);
176 require_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $moveto));
178 if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
179 error("Error finding the category");
183 $courses = array();
184 foreach ( $data as $key => $value ) {
185 if (preg_match('/^c\d+$/', $key)) {
186 array_push($courses, substr($key, 1));
189 move_courses($courses, $data->moveto);
192 /// Hide or show a course
194 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
195 require_capability('moodle/course:visibility', $context);
196 if (!empty($hide)) {
197 $course = get_record("course", "id", $hide);
198 $visible = 0;
199 } else {
200 $course = get_record("course", "id", $show);
201 $visible = 1;
203 if ($course) {
204 if (! set_field("course", "visible", $visible, "id", $course->id)) {
205 notify("Could not update that course!");
211 /// Move a course up or down
213 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
214 require_capability('moodle/category:update', $context);
215 $movecourse = NULL;
216 $swapcourse = NULL;
218 // ensure the course order has no gaps
219 // and isn't at 0
220 fix_course_sortorder($category->id);
222 // we are going to need to know the range
223 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
224 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
225 $max = $max->max + 100;
227 if (!empty($moveup)) {
228 $movecourse = get_record('course', 'id', $moveup);
229 $swapcourse = get_record('course',
230 'category', $category->id,
231 'sortorder', $movecourse->sortorder - 1);
232 } else {
233 $movecourse = get_record('course', 'id', $movedown);
234 $swapcourse = get_record('course',
235 'category', $category->id,
236 'sortorder', $movecourse->sortorder + 1);
239 if ($swapcourse and $movecourse) { // Renumber everything for robustness
240 begin_sql();
241 if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id)
242 && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
243 && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
244 )) {
245 notify("Could not update that course!");
247 commit_sql();
252 } // End of editing stuff
254 /// Print out all the sub-categories
255 if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
256 $firstentry = true;
257 foreach ($subcategories as $subcategory) {
258 if ($subcategory->visible or has_capability('moodle/course:create', $context)) {
259 $subcategorieswereshown = true;
260 if ($firstentry) {
261 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
262 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
263 echo '<tr><td style="white-space: nowrap">';
264 $firstentry = false;
266 $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
267 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
268 format_string($subcategory->name).'</a><br />';
271 if (!$firstentry) {
272 echo "</td></tr></table>";
273 echo "<br />";
277 /// print option to add a subcategory
278 if (has_capability('moodle/category:create', $context) && $creatorediting) {
279 $cat->id = $id;
280 $mform->set_data($cat);
281 $mform->display();
284 /// Print out all the courses
285 unset($course); // To avoid unwanted language effects later
287 $courses = get_courses_page($category->id, 'c.sortorder ASC',
288 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
289 $totalcount, $page*$perpage, $perpage);
290 $numcourses = count($courses);
292 if (!$courses) {
293 if (empty($subcategorieswereshown)) {
294 print_heading(get_string("nocoursesyet"));
297 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$creatorediting) {
298 print_box_start('courseboxes');
299 print_courses($category);
300 print_box_end();
302 } else {
303 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&amp;");
305 $strcourses = get_string("courses");
306 $strselect = get_string("select");
307 $stredit = get_string("edit");
308 $strdelete = get_string("delete");
309 $strbackup = get_string("backup");
310 $strrestore = get_string("restore");
311 $strmoveup = get_string("moveup");
312 $strmovedown = get_string("movedown");
313 $strupdate = get_string("update");
314 $strhide = get_string("hide");
315 $strshow = get_string("show");
316 $strsummary = get_string("summary");
317 $strsettings = get_string("settings");
318 $strassignteachers = get_string("assignteachers");
319 $strallowguests = get_string("allowguests");
320 $strrequireskey = get_string("requireskey");
323 echo '<form id="movecourses" action="category.php" method="post"><div>';
324 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
325 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
326 echo '<th class="header" scope="col">'.$strcourses.'</th>';
327 if ($creatorediting) {
328 echo '<th class="header" scope="col">'.$stredit.'</th>';
329 if ($adminediting) {
330 echo '<th class="header" scope="col">'.$strselect.'</th>';
332 } else {
333 echo '<th class="header" scope="col">&nbsp;</th>';
335 echo '</tr>';
338 $count = 0;
339 $abletomovecourses = false; // for now
341 // Checking if we are at the first or at the last page, to allow courses to
342 // be moved up and down beyond the paging border
343 if ($totalcount > $perpage) {
344 $atfirstpage = ($page == 0);
345 if ($perpage > 0) {
346 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
347 } else {
348 $atlastpage = true;
350 } else {
351 $atfirstpage = true;
352 $atlastpage = true;
355 foreach ($courses as $acourse) {
356 if (isset($acourse->context)) {
357 $coursecontext = $acourse->context;
358 } else {
359 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
362 $count++;
363 $up = ($count > 1 || !$atfirstpage);
364 $down = ($count < $numcourses || !$atlastpage);
366 $linkcss = $acourse->visible ? "" : ' class="dimmed" ';
367 echo '<tr>';
368 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
369 if ($creatorediting) {
370 echo "<td>";
371 if (has_capability('moodle/course:update', $coursecontext)) {
372 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.
373 $acourse->id.'">'.
374 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> '; }
376 // role assignment link
377 if (has_capability('moodle/role:assign', $coursecontext)) {
378 echo'<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$coursecontext->id.'"><img src="'.$CFG->pixpath.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a>';
381 if (can_delete_course($acourse->id)) {
382 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
383 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
386 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
387 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
388 if (!empty($acourse->visible)) {
389 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&amp;page='.$page.
390 '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
391 '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
392 } else {
393 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
394 '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
395 '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
399 if (has_capability('moodle/site:backup', $coursecontext)) {
400 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
401 '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
404 if (has_capability('moodle/site:restore', $coursecontext)) {
405 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
406 '&amp;wdir=/backupdata">'.
407 '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
410 if (has_capability('moodle/category:update', $context)) {
411 if ($up) {
412 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
413 '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
414 '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
415 } else {
416 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
419 if ($down) {
420 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
421 '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
422 '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
423 } else {
424 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
426 $abletomovecourses = true;
429 echo '</td>';
430 echo '<td align="center">';
431 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
432 echo '</td>';
433 } else {
434 echo '<td align="right">';
435 if (!empty($acourse->guest)) {
436 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
437 $strallowguests.'" class="icon" src="'.
438 $CFG->pixpath.'/i/user.gif" alt="'.$strallowguests.'" /></a>';
440 if (!empty($acourse->password)) {
441 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
442 $strrequireskey.'" class="icon" src="'.
443 $CFG->pixpath.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
445 if (!empty($acourse->summary)) {
446 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
447 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath.'/i/info.gif" />',
448 400, 500, $strsummary);
450 echo "</td>";
452 echo "</tr>";
455 if ($abletomovecourses) {
456 echo '<tr><td colspan="3" align="right">';
457 echo '<br />';
458 unset($displaylist[$category->id]);
460 // loop and unset categories the user can't move into
462 foreach ($displaylist as $did=>$dlist) {
463 if (!has_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $did))) {
464 unset($displaylist[$did]);
468 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: submitFormById('movecourses')");
469 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
470 echo '</td></tr>';
473 echo '</table>';
474 echo '</div></form>';
475 echo '<br />';
478 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $numcourses > 1) { /// Print button to re-sort courses by name
479 unset($options);
480 $options['id'] = $category->id;
481 $options['resort'] = 'name';
482 $options['sesskey'] = $USER->sesskey;
483 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
486 if (has_capability('moodle/course:create', $context)) { /// Print button to create a new course
487 unset($options);
488 $options['category'] = $category->id;
489 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
490 echo '<br />';
493 if (has_capability('moodle/category:update', $context)) { /// Print form to rename the category
494 $strrename= get_string('rename');
495 echo '<form id="renameform" action="category.php" method="post"><div>';
496 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
497 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
498 echo '<input type="text" size="30" name="rename" value="'.format_string($category->name).'" alt="'.$strrename.'" />';
499 echo '<input type="submit" value="'.$strrename.'" />';
500 echo '</div></form>';
501 echo '<br />';
503 if (!empty($CFG->allowcategorythemes)) {
504 $choices = array();
505 $choices[''] = get_string('default');
506 $choices += get_list_of_themes();
508 echo '<form id="themeform" action="category.php" method="post"><div>';
509 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
510 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
511 choose_from_menu($choices, 'categorytheme', $category->theme);
512 echo '<input type="submit" value="'.get_string('setcategorytheme').'" />';
513 echo '</div></form>';
514 echo '<br />';
519 print_course_search();
521 print_footer();