Bumping version for 1.8.9 release. Thinking we might have to go to x.x.10 for the...
[moodle.git] / course / category.php
blob8193e751f004112e37f0872dad09c5b43a21eec6
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");
9 $id = required_param('id', PARAM_INT); // Category id
10 $page = optional_param('page', 0, PARAM_INT); // which page to show
11 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
12 $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
13 $hide = optional_param('hide', 0, PARAM_INT);
14 $show = optional_param('show', 0, PARAM_INT);
15 $moveup = optional_param('moveup', 0, PARAM_INT);
16 $movedown = optional_param('movedown', 0, PARAM_INT);
17 $moveto = optional_param('moveto', 0, PARAM_INT);
18 $rename = optional_param('rename', '', PARAM_TEXT);
19 $resort = optional_param('resort', 0, PARAM_BOOL);
20 $addsubcategory=optional_param('addsubcategory', '', PARAM_NOTAGS);
22 if (!$site = get_site()) {
23 error("Site isn't defined!");
26 $context = get_context_instance(CONTEXT_COURSECAT, $id);
28 if ($CFG->forcelogin) {
29 require_login();
32 if (!$category = get_record("course_categories", "id", $id)) {
33 error("Category not known!");
36 if (has_capability('moodle/course:create', $context)) {
37 if ($categoryedit !== -1) {
38 $USER->categoryediting = $categoryedit;
40 $navbaritem = update_category_button($category->id);
41 $creatorediting = !empty($USER->categoryediting);
42 $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $creatorediting);
44 } else {
45 if (!$category->visible) {
46 error(get_string('notavailable', 'error'));
48 $navbaritem = print_course_search("", true, "navbar");
49 $adminediting = false;
50 $creatorediting = false;
54 if (has_capability('moodle/category:create', $context)) {
55 if (!empty($addsubcategory) and confirm_sesskey()) {
56 $subcategory = new stdClass;
57 $subcategory->name = $addsubcategory;
58 $subcategory->sortorder = 999;
59 $subcategory->parent = $id;
60 if (!insert_record('course_categories', $subcategory )) {
61 notify( "Could not insert the new subcategory '$addsubcategory' " );
66 if (has_capability('moodle/category:update', $context)) {
67 /// Rename the category if requested
68 if (!empty($rename) and confirm_sesskey()) {
69 $category->name = $rename;
70 if (! set_field("course_categories", "name", $category->name, "id", $category->id)) {
71 notify("An error occurred while renaming the category");
75 /// Resort the category if requested
77 if ($resort and confirm_sesskey()) {
78 if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
79 // move it off the range
80 $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
81 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
82 $count = $count->max + 100;
83 begin_sql();
84 foreach ($courses as $course) {
85 set_field('course', 'sortorder', $count, 'id', $course->id);
86 $count++;
88 commit_sql();
89 fix_course_sortorder($category->id);
95 /// Print headings
97 $numcategories = count_records("course_categories");
99 $stradministration = get_string("administration");
100 $strcategories = get_string("categories");
101 $strcategory = get_string("category");
102 $strcourses = get_string("courses");
104 if ($creatorediting) {
105 if ($adminediting) {
106 // modify this to treat this as an admin page
108 require_once($CFG->libdir.'/adminlib.php');
109 $adminroot = admin_get_root();
110 admin_externalpage_setup('coursemgmt', $adminroot);
111 admin_externalpage_print_header($adminroot);
112 } else {
113 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
114 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
116 } else {
117 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
118 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
121 /// Print button to turn editing off
122 if ($adminediting) {
123 echo '<div class="categoryediting button" align="right">'.update_category_button($category->id).'</div>';
126 /// Print link to roles
128 if (has_capability('moodle/role:assign', $context)) {
129 echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
130 $context->id.'">'.get_string('assignroles','role').'</a></div>';
132 /// Print the category selector
134 $displaylist = array();
135 $parentlist = array();
137 make_categories_list($displaylist, $parentlist, "");
139 echo '<div class="categorypicker">';
140 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
141 echo '</div>';
144 /// Editing functions
146 if ($creatorediting) {
147 /// Move a specified course to a new category
149 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
151 // user must have category update in both cats to perform this
152 require_capability('moodle/category:update', $context);
153 require_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $moveto));
155 if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
156 error("Error finding the category");
160 $courses = array();
161 foreach ( $data as $key => $value ) {
162 if (preg_match('/^c\d+$/', $key)) {
163 array_push($courses, substr($key, 1));
166 move_courses($courses, $data->moveto);
169 /// Hide or show a course
171 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
172 require_capability('moodle/course:visibility', $context);
173 if (!empty($hide)) {
174 $course = get_record("course", "id", $hide);
175 $visible = 0;
176 } else {
177 $course = get_record("course", "id", $show);
178 $visible = 1;
180 if ($course) {
181 if (! set_field("course", "visible", $visible, "id", $course->id)) {
182 notify("Could not update that course!");
188 /// Move a course up or down
190 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
191 require_capability('moodle/category:update', $context);
192 $movecourse = NULL;
193 $swapcourse = NULL;
195 // ensure the course order has no gaps
196 // and isn't at 0
197 fix_course_sortorder($category->id);
199 // we are going to need to know the range
200 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
201 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
202 $max = $max->max + 100;
204 if (!empty($moveup)) {
205 $movecourse = get_record('course', 'id', $moveup);
206 $swapcourse = get_record('course',
207 'category', $category->id,
208 'sortorder', $movecourse->sortorder - 1);
209 } else {
210 $movecourse = get_record('course', 'id', $movedown);
211 $swapcourse = get_record('course',
212 'category', $category->id,
213 'sortorder', $movecourse->sortorder + 1);
216 if ($swapcourse and $movecourse) { // Renumber everything for robustness
217 begin_sql();
218 if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id)
219 && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
220 && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
221 )) {
222 notify("Could not update that course!");
224 commit_sql();
229 } // End of editing stuff
231 /// Print out all the sub-categories
232 if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
233 $firstentry = true;
234 foreach ($subcategories as $subcategory) {
235 if ($subcategory->visible or has_capability('moodle/course:create', $context)) {
236 $subcategorieswereshown = true;
237 if ($firstentry) {
238 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
239 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
240 echo '<tr><td style="white-space: nowrap">';
241 $firstentry = false;
243 $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
244 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
245 format_string($subcategory->name).'</a><br />';
248 if (!$firstentry) {
249 echo "</td></tr></table>";
250 echo "<br />";
254 /// print option to add a subcategory
255 if (has_capability('moodle/category:create', $context)) {
256 $straddsubcategory = get_string('addsubcategory');
257 echo '<div class="addcategory">';
258 echo '<form id="addform" action="category.php" method="post">';
259 echo '<fieldset class="invisiblefieldset">';
260 echo '<input type="text" size="30" alt="'.$straddsubcategory.'" name="addsubcategory" />';
261 echo '<input type="submit" value="'.$straddsubcategory.'" />';
262 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
263 echo '<input type="hidden" name="id" value="'.$id.'" />';
264 // echo '<input type="hidden" name="categoryedit" value="'.$categoryedit.'" />';
265 echo '</fieldset>';
266 echo '</form>';
267 echo '</div>';
270 /// Print out all the courses
271 unset($course); // To avoid unwanted language effects later
273 $courses = get_courses_page($category->id, 'c.sortorder ASC',
274 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
275 $totalcount, $page*$perpage, $perpage);
276 $numcourses = count($courses);
278 if (!$courses) {
279 if (empty($subcategorieswereshown)) {
280 print_heading(get_string("nocoursesyet"));
283 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$creatorediting) {
284 print_box_start('courseboxes');
285 print_courses($category);
286 print_box_end();
288 } else {
289 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&amp;");
291 $strcourses = get_string("courses");
292 $strselect = get_string("select");
293 $stredit = get_string("edit");
294 $strdelete = get_string("delete");
295 $strbackup = get_string("backup");
296 $strrestore = get_string("restore");
297 $strmoveup = get_string("moveup");
298 $strmovedown = get_string("movedown");
299 $strupdate = get_string("update");
300 $strhide = get_string("hide");
301 $strshow = get_string("show");
302 $strsummary = get_string("summary");
303 $strsettings = get_string("settings");
304 $strassignteachers = get_string("assignteachers");
305 $strallowguests = get_string("allowguests");
306 $strrequireskey = get_string("requireskey");
309 echo '<form id="movecourses" action="category.php" method="post"><div>';
310 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
311 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
312 echo '<th class="header" scope="col">'.$strcourses.'</th>';
313 if ($creatorediting) {
314 echo '<th class="header" scope="col">'.$stredit.'</th>';
315 if ($adminediting) {
316 echo '<th class="header" scope="col">'.$strselect.'</th>';
318 } else {
319 echo '<th class="header" scope="col">&nbsp;</th>';
321 echo '</tr>';
324 $count = 0;
325 $abletomovecourses = false; // for now
327 // Checking if we are at the first or at the last page, to allow courses to
328 // be moved up and down beyond the paging border
329 if ($totalcount > $perpage) {
330 $atfirstpage = ($page == 0);
331 if ($perpage > 0) {
332 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
333 } else {
334 $atlastpage = true;
336 } else {
337 $atfirstpage = true;
338 $atlastpage = true;
341 foreach ($courses as $acourse) {
343 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
345 $count++;
346 $up = ($count > 1 || !$atfirstpage);
347 $down = ($count < $numcourses || !$atlastpage);
349 $linkcss = $acourse->visible ? "" : ' class="dimmed" ';
350 echo '<tr>';
351 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
352 if ($creatorediting) {
353 echo "<td>";
354 if (has_capability('moodle/course:update', $coursecontext)) {
355 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.
356 $acourse->id.'">'.
357 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> '; }
359 // role assignment link
360 if (has_capability('moodle/role:assign', $coursecontext)) {
361 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>';
364 if (can_delete_course($acourse->id)) {
365 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
366 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
369 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
370 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
371 if (!empty($acourse->visible)) {
372 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&amp;page='.$page.
373 '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
374 '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
375 } else {
376 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
377 '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
378 '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
382 if (has_capability('moodle/site:backup', $coursecontext)) {
383 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
384 '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
387 if (has_capability('moodle/site:restore', $coursecontext)) {
388 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
389 '&amp;wdir=/backupdata">'.
390 '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
393 if (has_capability('moodle/category:update', $context)) {
394 if ($up) {
395 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
396 '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
397 '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
398 } else {
399 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
402 if ($down) {
403 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
404 '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
405 '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
406 } else {
407 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
409 $abletomovecourses = true;
412 echo '</td>';
413 echo '<td align="center">';
414 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
415 echo '</td>';
416 } else {
417 echo '<td align="right">';
418 if (!empty($acourse->guest)) {
419 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
420 $strallowguests.'" class="icon" src="'.
421 $CFG->pixpath.'/i/user.gif" alt="'.$strallowguests.'" /></a>';
423 if (!empty($acourse->password)) {
424 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
425 $strrequireskey.'" class="icon" src="'.
426 $CFG->pixpath.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
428 if (!empty($acourse->summary)) {
429 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
430 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath.'/i/info.gif" />',
431 400, 500, $strsummary);
433 echo "</td>";
435 echo "</tr>";
438 if ($abletomovecourses) {
439 echo '<tr><td colspan="3" align="right">';
440 echo '<br />';
441 unset($displaylist[$category->id]);
443 // loop and unset categories the user can't move into
445 foreach ($displaylist as $did=>$dlist) {
446 if (!has_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $did))) {
447 unset($displaylist[$did]);
451 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: submitFormById('movecourses')");
452 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
453 echo '</td></tr>';
456 echo '</table>';
457 echo '</div></form>';
458 echo '<br />';
461 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $numcourses > 1) { /// Print button to re-sort courses by name
462 unset($options);
463 $options['id'] = $category->id;
464 $options['resort'] = 'name';
465 $options['sesskey'] = $USER->sesskey;
466 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
469 if (has_capability('moodle/course:create', $context)) { /// Print button to create a new course
470 unset($options);
471 $options['category'] = $category->id;
472 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
473 echo '<br />';
476 if (has_capability('moodle/category:update', $context)) { /// Print form to rename the category
477 $strrename= get_string('rename');
478 echo '<form id="renameform" action="category.php" method="post"><div>';
479 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
480 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
481 echo '<input type="text" size="30" name="rename" value="'.htmlspecialchars($category->name).'" alt="'.$strrename.'" />';
482 echo '<input type="submit" value="'.$strrename.'" />';
483 echo '</div></form>';
484 echo '<br />';
487 print_course_search();
489 if ($adminediting) {
490 admin_externalpage_print_footer($adminroot);
491 } else {
492 print_footer();