Updated the 19 build version to 20100127
[moodle.git] / course / category.php
blobd84d21180e8dfb65e961c6d18e674bf6f200ce9f
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
60 $count = get_record_sql('SELECT MIN(sortorder) AS min, 1
61 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
62 $count = $count->min;
63 begin_sql();
64 foreach ($courses as $course) {
65 set_field('course', 'sortorder', $count, 'id', $course->id);
66 $count++;
68 commit_sql();
69 fix_course_sortorder($category->id);
74 if(!empty($CFG->allowcategorythemes) && isset($category->theme)) {
75 // specifying theme here saves us some dbqs
76 theme_setup($category->theme);
79 /// Print headings
80 $numcategories = count_records('course_categories');
82 $stradministration = get_string('administration');
83 $strcategories = get_string('categories');
84 $strcategory = get_string('category');
85 $strcourses = get_string('courses');
87 $navlinks = array();
88 $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc');
89 $navlinks[] = array('name' => format_string($category->name), 'link' => null, 'type' => 'misc');
90 $navigation = build_navigation($navlinks);
92 if ($editingon && update_category_button()) {
93 // Integrate into the admin tree only if the user can edit categories at the top level,
94 // otherwise the admin block does not appear to this user, and you get an error.
95 require_once($CFG->libdir.'/adminlib.php');
96 admin_externalpage_setup('coursemgmt', $navbaritem, array('id' => $id,
97 'page' => $page, 'perpage' => $perpage), $CFG->wwwroot . '/course/category.php');
98 admin_externalpage_print_header();
99 } else {
100 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, '', '', true, $navbaritem);
103 /// Print link to roles
104 if (has_capability('moodle/role:assign', $context)) {
105 echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
106 $context->id.'">'.get_string('assignroles','role').'</a></div>';
109 /// Print the category selector
110 $displaylist = array();
111 $notused = array();
112 make_categories_list($displaylist, $notused);
114 echo '<div class="categorypicker">';
115 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
116 echo '</div>';
118 /// Print current category description
119 if (!$editingon && $category->description) {
120 print_box_start();
121 echo format_text($category->description); // for multilang filter
122 print_box_end();
125 /// Process any course actions.
126 if ($editingon) {
127 /// Move a specified course to a new category
128 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
129 // user must have category update in both cats to perform this
130 require_capability('moodle/category:manage', $context);
131 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
133 if (!$destcategory = get_record('course_categories', 'id', $data->moveto)) {
134 error('Error finding the category');
137 $courses = array();
138 foreach ($data as $key => $value) {
139 if (preg_match('/^c\d+$/', $key)) {
140 $courseid = substr($key, 1);
141 array_push($courses, $courseid);
143 // check this course's category
144 if ($movingcourse = get_record('course', 'id', $courseid)) {
145 if ($movingcourse->category != $id ) {
146 error('The course doesn\'t belong to this category');
148 } else {
149 error('Error finding the course');
153 move_courses($courses, $data->moveto);
156 /// Hide or show a course
157 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
158 if (!empty($hide)) {
159 $course = get_record('course', 'id', $hide);
160 $visible = 0;
161 } else {
162 $course = get_record('course', 'id', $show);
163 $visible = 1;
166 if ($course) {
167 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
168 require_capability('moodle/course:visibility', $coursecontext);
169 if (!set_field('course', 'visible', $visible, 'id', $course->id)) {
170 notify('Could not update that course!');
175 /// Move a course up or down
176 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
177 require_capability('moodle/category:manage', $context);
178 $movecourse = NULL;
179 $swapcourse = NULL;
181 // ensure the course order has no gaps and isn't at 0
182 fix_course_sortorder($category->id);
184 // we are going to need to know the range
185 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
186 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
187 $max = $max->max + 100;
189 if (!empty($moveup)) {
190 $movecourse = get_record('course', 'id', $moveup);
191 $swapcourse = get_record('course', 'category', $category->id,
192 'sortorder', $movecourse->sortorder - 1);
193 } else {
194 $movecourse = get_record('course', 'id', $movedown);
195 $swapcourse = get_record('course', 'category', $category->id,
196 'sortorder', $movecourse->sortorder + 1);
198 if ($swapcourse and $movecourse) {
199 // check course's category
200 if ($movecourse->category != $id) {
201 error('The course doesn\'t belong to this category');
203 // Renumber everything for robustness
204 begin_sql();
205 if (!( set_field('course', 'sortorder', $max, 'id', $swapcourse->id)
206 && set_field('course', 'sortorder', $swapcourse->sortorder, 'id', $movecourse->id)
207 && set_field('course', 'sortorder', $movecourse->sortorder, 'id', $swapcourse->id)
208 )) {
209 notify('Could not update that course!');
211 commit_sql();
215 } // End of editing stuff
217 if ($editingon && has_capability('moodle/category:manage', $context)) {
218 echo '<div class="buttons">';
220 // Print button to update this category
221 $options = array('id' => $category->id);
222 print_single_button($CFG->wwwroot.'/course/editcategory.php', $options, get_string('editcategorythis'), 'get');
224 // Print button for creating new categories
225 $options = array('parent' => $category->id);
226 print_single_button($CFG->wwwroot.'/course/editcategory.php', $options, get_string('addsubcategory'), 'get');
228 echo '</div>';
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 || has_capability('moodle/category:viewhiddencategories', $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 />';
255 /// Print out all the courses
256 $courses = get_courses_page($category->id, 'c.sortorder ASC',
257 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
258 $totalcount, $page*$perpage, $perpage);
259 $numcourses = count($courses);
261 if (!$courses) {
262 if (empty($subcategorieswereshown)) {
263 print_heading(get_string("nocoursesyet"));
266 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
267 print_box_start('courseboxes');
268 print_courses($category);
269 print_box_end();
271 } else {
272 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&amp;");
274 $strcourses = get_string('courses');
275 $strselect = get_string('select');
276 $stredit = get_string('edit');
277 $strdelete = get_string('delete');
278 $strbackup = get_string('backup');
279 $strrestore = get_string('restore');
280 $strmoveup = get_string('moveup');
281 $strmovedown = get_string('movedown');
282 $strupdate = get_string('update');
283 $strhide = get_string('hide');
284 $strshow = get_string('show');
285 $strsummary = get_string('summary');
286 $strsettings = get_string('settings');
287 $strassignteachers = get_string('assignteachers');
288 $strallowguests = get_string('allowguests');
289 $strrequireskey = get_string('requireskey');
292 echo '<form id="movecourses" action="category.php" method="post"><div>';
293 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
294 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
295 echo '<th class="header" scope="col">'.$strcourses.'</th>';
296 if ($editingon) {
297 echo '<th class="header" scope="col">'.$stredit.'</th>';
298 echo '<th class="header" scope="col">'.$strselect.'</th>';
299 } else {
300 echo '<th class="header" scope="col">&nbsp;</th>';
302 echo '</tr>';
305 $count = 0;
306 $abletomovecourses = false; // for now
308 // Checking if we are at the first or at the last page, to allow courses to
309 // be moved up and down beyond the paging border
310 if ($totalcount > $perpage) {
311 $atfirstpage = ($page == 0);
312 if ($perpage > 0) {
313 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
314 } else {
315 $atlastpage = true;
317 } else {
318 $atfirstpage = true;
319 $atlastpage = true;
322 $spacer = '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
323 foreach ($courses as $acourse) {
324 if (isset($acourse->context)) {
325 $coursecontext = $acourse->context;
326 } else {
327 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
330 $count++;
331 $up = ($count > 1 || !$atfirstpage);
332 $down = ($count < $numcourses || !$atlastpage);
334 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
335 echo '<tr>';
336 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
337 if ($editingon) {
338 echo '<td>';
339 if (has_capability('moodle/course:update', $coursecontext)) {
340 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.$acourse->id.'">'.
341 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> ';
342 } else {
343 echo $spacer;
346 // role assignment link
347 if (has_capability('moodle/role:assign', $coursecontext)) {
348 echo '<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.$coursecontext->id.'">'.
349 '<img src="'.$CFG->pixpath.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a> ';
350 } else {
351 echo $spacer;
354 if (can_delete_course($acourse->id)) {
355 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
356 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
357 } else {
358 echo $spacer;
361 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
362 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
363 if (!empty($acourse->visible)) {
364 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&amp;page='.$page.
365 '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
366 '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
367 } else {
368 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
369 '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
370 '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
372 } else {
373 echo $spacer;
376 if (has_capability('moodle/site:backup', $coursecontext)) {
377 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
378 '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
379 } else {
380 echo $spacer;
383 if (has_capability('moodle/site:restore', $coursecontext)) {
384 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
385 '&amp;wdir=/backupdata">'.
386 '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
387 } else {
388 echo $spacer;
391 if (has_capability('moodle/category:manage', $context)) {
392 if ($up) {
393 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
394 '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
395 '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
396 } else {
397 echo $spacer;
400 if ($down) {
401 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
402 '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
403 '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
404 } else {
405 echo $spacer;
407 $abletomovecourses = true;
408 } else {
409 echo $spacer, $spacer;
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/guest.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 $movetocategories = array();
440 $notused = array();
441 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
442 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
443 echo '<tr><td colspan="3" align="right">';
444 choose_from_menu($movetocategories, 'moveto', $category->id, '', "javascript:submitFormById('movecourses')");
445 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
446 echo '</td></tr>';
449 echo '</table>';
450 echo '</div></form>';
451 echo '<br />';
454 echo '<div class="buttons">';
455 if (has_capability('moodle/category:manage', $context) and $numcourses > 1) {
456 /// Print button to re-sort courses by name
457 unset($options);
458 $options['id'] = $category->id;
459 $options['resort'] = 'name';
460 $options['sesskey'] = $USER->sesskey;
461 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
464 if (has_capability('moodle/course:create', $context)) {
465 /// Print button to create a new course
466 unset($options);
467 $options['category'] = $category->id;
468 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
471 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->enablecourserequests) {
472 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
474 echo '</div>';
476 print_course_search();
478 print_footer();