Updated the 19 build version to 20100904
[moodle.git] / course / category.php
blobd85ce7d873b39d86f6b9a40c193b3d1d0eee0ed4
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 $resort = optional_param('resort', 0, PARAM_BOOL);
20 if ($CFG->forcelogin) {
21 require_login();
24 if (!$site = get_site()) {
25 error('Site isn\'t defined!');
28 if (empty($id)) {
29 error("Category not known!");
32 if (!$context = get_context_instance(CONTEXT_COURSECAT, $id)) {
33 error("Category not known!");
36 if (!$category = get_record("course_categories", "id", $id)) {
37 error("Category not known!");
39 if (!$category->visible) {
40 require_capability('moodle/category:viewhiddencategories', $context);
43 if (update_category_button($category->id)) {
44 if ($categoryedit !== -1) {
45 $USER->categoryediting = $categoryedit;
47 $editingon = !empty($USER->categoryediting);
48 $navbaritem = update_category_button($category->id); // Must call this again after updating the state.
49 } else {
50 $navbaritem = print_course_search("", true, "navbar");
51 $editingon = false;
54 // Process any category actions.
55 if (has_capability('moodle/category:manage', $context)) {
56 /// Resort the category if requested
57 if ($resort and confirm_sesskey()) {
58 if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
59 // move it off the range
61 $sortorderresult = get_record_sql('SELECT MIN(sortorder) AS min, 1
62 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
63 $sortordermin = $sortorderresult->min;
65 $sortorderresult = get_record_sql('SELECT MAX(sortorder) AS max, 1
66 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
67 $sortorder = $sortordermax = $sortorderresult->max + 100;
69 //place the courses above the maximum existing sortorder to avoid duplicate index errors
70 //after they've been sorted we'll shift them down again
71 begin_sql();
72 foreach ($courses as $course) {
73 set_field('course', 'sortorder', $sortorder, 'id', $course->id);
74 $sortorder++;
76 commit_sql();
78 //shift course sortorder back down the amount we moved them up
79 execute_sql('UPDATE '. $CFG->prefix .'course SET sortorder = sortorder-'.($sortordermax-$sortordermin).
80 ' WHERE category='.$category->id);
82 fix_course_sortorder($category->id);
87 if(!empty($CFG->allowcategorythemes) && isset($category->theme)) {
88 // specifying theme here saves us some dbqs
89 theme_setup($category->theme);
92 /// Print headings
93 $numcategories = count_records('course_categories');
95 $stradministration = get_string('administration');
96 $strcategories = get_string('categories');
97 $strcategory = get_string('category');
98 $strcourses = get_string('courses');
100 $navlinks = array();
101 $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc');
102 $navlinks[] = array('name' => format_string($category->name), 'link' => null, 'type' => 'misc');
103 $navigation = build_navigation($navlinks);
105 if ($editingon && update_category_button()) {
106 // Integrate into the admin tree only if the user can edit categories at the top level,
107 // otherwise the admin block does not appear to this user, and you get an error.
108 require_once($CFG->libdir.'/adminlib.php');
109 admin_externalpage_setup('coursemgmt', $navbaritem, array('id' => $id,
110 'page' => $page, 'perpage' => $perpage), $CFG->wwwroot . '/course/category.php');
111 admin_externalpage_print_header();
112 } else {
113 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, '', '', true, $navbaritem);
116 /// Print link to roles
117 if (has_capability('moodle/role:assign', $context)) {
118 echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
119 $context->id.'">'.get_string('assignroles','role').'</a></div>';
122 /// Print the category selector
123 $displaylist = array();
124 $notused = array();
125 make_categories_list($displaylist, $notused);
127 echo '<div class="categorypicker">';
128 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
129 echo '</div>';
131 /// Print current category description
132 if (!$editingon && $category->description) {
133 print_box_start();
134 echo format_text($category->description); // for multilang filter
135 print_box_end();
138 /// Process any course actions.
139 if ($editingon) {
140 /// Move a specified course to a new category
141 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
142 // user must have category update in both cats to perform this
143 require_capability('moodle/category:manage', $context);
144 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
146 if (!$destcategory = get_record('course_categories', 'id', $data->moveto)) {
147 error('Error finding the category');
150 $courses = array();
151 foreach ($data as $key => $value) {
152 if (preg_match('/^c\d+$/', $key)) {
153 $courseid = substr($key, 1);
154 array_push($courses, $courseid);
156 // check this course's category
157 if ($movingcourse = get_record('course', 'id', $courseid)) {
158 if ($movingcourse->category != $id ) {
159 error('The course doesn\'t belong to this category');
161 } else {
162 error('Error finding the course');
166 move_courses($courses, $data->moveto);
169 /// Hide or show a course
170 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
171 if (!empty($hide)) {
172 $course = get_record('course', 'id', $hide);
173 $visible = 0;
174 } else {
175 $course = get_record('course', 'id', $show);
176 $visible = 1;
179 if ($course) {
180 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
181 require_capability('moodle/course:visibility', $coursecontext);
182 if (!set_field('course', 'visible', $visible, 'id', $course->id)) {
183 notify('Could not update that course!');
188 /// Move a course up or down
189 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
190 require_capability('moodle/category:manage', $context);
191 $movecourse = NULL;
192 $swapcourse = NULL;
194 // ensure the course order has no gaps and isn't at 0
195 fix_course_sortorder($category->id);
197 // we are going to need to know the range
198 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
199 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
200 $max = $max->max + 100;
202 if (!empty($moveup)) {
203 $movecourse = get_record('course', 'id', $moveup);
204 $swapcourse = get_record('course', 'category', $category->id,
205 'sortorder', $movecourse->sortorder - 1);
206 } else {
207 $movecourse = get_record('course', 'id', $movedown);
208 $swapcourse = get_record('course', 'category', $category->id,
209 'sortorder', $movecourse->sortorder + 1);
211 if ($swapcourse and $movecourse) {
212 // check course's category
213 if ($movecourse->category != $id) {
214 error('The course doesn\'t belong to this category');
216 // 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();
228 } // End of editing stuff
230 if ($editingon && has_capability('moodle/category:manage', $context)) {
231 echo '<div class="buttons">';
233 // Print button to update this category
234 $options = array('id' => $category->id);
235 print_single_button($CFG->wwwroot.'/course/editcategory.php', $options, get_string('editcategorythis'), 'get');
237 // Print button for creating new categories
238 $options = array('parent' => $category->id);
239 print_single_button($CFG->wwwroot.'/course/editcategory.php', $options, get_string('addsubcategory'), 'get');
241 echo '</div>';
244 /// Print out all the sub-categories
245 if ($subcategories = get_records('course_categories', 'parent', $category->id, 'sortorder ASC')) {
246 $firstentry = true;
247 foreach ($subcategories as $subcategory) {
248 if ($subcategory->visible || has_capability('moodle/category:viewhiddencategories', $context)) {
249 $subcategorieswereshown = true;
250 if ($firstentry) {
251 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
252 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
253 echo '<tr><td style="white-space: nowrap">';
254 $firstentry = false;
256 $catlinkcss = $subcategory->visible ? '' : 'class="dimmed" ';
257 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
258 format_string($subcategory->name).'</a><br />';
261 if (!$firstentry) {
262 echo '</td></tr></table>';
263 echo '<br />';
268 /// Print out all the courses
269 $courses = get_courses_page($category->id, 'c.sortorder ASC',
270 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
271 $totalcount, $page*$perpage, $perpage);
272 $numcourses = count($courses);
274 if (!$courses) {
275 if (empty($subcategorieswereshown)) {
276 print_heading(get_string("nocoursesyet"));
279 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
280 print_box_start('courseboxes');
281 print_courses($category);
282 print_box_end();
284 } else {
285 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&amp;");
287 $strcourses = get_string('courses');
288 $strselect = get_string('select');
289 $stredit = get_string('edit');
290 $strdelete = get_string('delete');
291 $strbackup = get_string('backup');
292 $strrestore = get_string('restore');
293 $strmoveup = get_string('moveup');
294 $strmovedown = get_string('movedown');
295 $strupdate = get_string('update');
296 $strhide = get_string('hide');
297 $strshow = get_string('show');
298 $strsummary = get_string('summary');
299 $strsettings = get_string('settings');
300 $strassignteachers = get_string('assignteachers');
301 $strallowguests = get_string('allowguests');
302 $strrequireskey = get_string('requireskey');
305 echo '<form id="movecourses" action="category.php" method="post"><div>';
306 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
307 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
308 echo '<th class="header" scope="col">'.$strcourses.'</th>';
309 if ($editingon) {
310 echo '<th class="header" scope="col">'.$stredit.'</th>';
311 echo '<th class="header" scope="col">'.$strselect.'</th>';
312 } else {
313 echo '<th class="header" scope="col">&nbsp;</th>';
315 echo '</tr>';
318 $count = 0;
319 $abletomovecourses = false; // for now
321 // Checking if we are at the first or at the last page, to allow courses to
322 // be moved up and down beyond the paging border
323 if ($totalcount > $perpage) {
324 $atfirstpage = ($page == 0);
325 if ($perpage > 0) {
326 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
327 } else {
328 $atlastpage = true;
330 } else {
331 $atfirstpage = true;
332 $atlastpage = true;
335 $spacer = '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
336 foreach ($courses as $acourse) {
337 if (isset($acourse->context)) {
338 $coursecontext = $acourse->context;
339 } else {
340 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
343 $count++;
344 $up = ($count > 1 || !$atfirstpage);
345 $down = ($count < $numcourses || !$atlastpage);
347 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
348 echo '<tr>';
349 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
350 if ($editingon) {
351 echo '<td>';
352 if (has_capability('moodle/course:update', $coursecontext)) {
353 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.$acourse->id.'">'.
354 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> ';
355 } else {
356 echo $spacer;
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.'">'.
362 '<img src="'.$CFG->pixpath.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a> ';
363 } else {
364 echo $spacer;
367 if (can_delete_course($acourse->id)) {
368 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
369 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
370 } else {
371 echo $spacer;
374 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
375 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
376 if (!empty($acourse->visible)) {
377 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&amp;page='.$page.
378 '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
379 '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
380 } else {
381 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
382 '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
383 '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
385 } else {
386 echo $spacer;
389 if (has_capability('moodle/site:backup', $coursecontext)) {
390 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
391 '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
392 } else {
393 echo $spacer;
396 if (has_capability('moodle/site:restore', $coursecontext)) {
397 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
398 '&amp;wdir=/backupdata">'.
399 '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
400 } else {
401 echo $spacer;
404 if (has_capability('moodle/category:manage', $context)) {
405 if ($up) {
406 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
407 '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
408 '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
409 } else {
410 echo $spacer;
413 if ($down) {
414 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
415 '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
416 '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
417 } else {
418 echo $spacer;
420 $abletomovecourses = true;
421 } else {
422 echo $spacer, $spacer;
425 echo '</td>';
426 echo '<td align="center">';
427 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
428 echo '</td>';
429 } else {
430 echo '<td align="right">';
431 if (!empty($acourse->guest)) {
432 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
433 $strallowguests.'" class="icon" src="'.
434 $CFG->pixpath.'/i/guest.gif" alt="'.$strallowguests.'" /></a>';
436 if (!empty($acourse->password)) {
437 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
438 $strrequireskey.'" class="icon" src="'.
439 $CFG->pixpath.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
441 if (!empty($acourse->summary)) {
442 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
443 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath.'/i/info.gif" />',
444 400, 500, $strsummary);
446 echo "</td>";
448 echo "</tr>";
451 if ($abletomovecourses) {
452 $movetocategories = array();
453 $notused = array();
454 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
455 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
456 echo '<tr><td colspan="3" align="right">';
457 choose_from_menu($movetocategories, 'moveto', $category->id, '', "javascript:submitFormById('movecourses')");
458 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
459 echo '</td></tr>';
462 echo '</table>';
463 echo '</div></form>';
464 echo '<br />';
467 echo '<div class="buttons">';
468 if (has_capability('moodle/category:manage', $context) and $numcourses > 1) {
469 /// Print button to re-sort courses by name
470 unset($options);
471 $options['id'] = $category->id;
472 $options['resort'] = 'name';
473 $options['sesskey'] = $USER->sesskey;
474 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
477 if (has_capability('moodle/course:create', $context)) {
478 /// Print button to create a new course
479 unset($options);
480 $options['category'] = $category->id;
481 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
484 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->enablecourserequests) {
485 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
487 echo '</div>';
489 print_course_search();
491 print_footer();