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
) {
24 if (!$site = get_site()) {
25 error('Site isn\'t defined!');
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.
50 $navbaritem = print_course_search("", true, "navbar");
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
72 foreach ($courses as $course) {
73 set_field('course', 'sortorder', $sortorder, 'id', $course->id
);
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
);
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');
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();
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();
125 make_categories_list($displaylist, $notused);
127 echo '<div class="categorypicker">';
128 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id
, '', '', '', false, 'self', $strcategories.':');
131 /// Print current category description
132 if (!$editingon && $category->description
) {
134 echo format_text($category->description
); // for multilang filter
138 /// Process any course actions.
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');
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');
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()) {
172 $course = get_record('course', 'id', $hide);
175 $course = get_record('course', 'id', $show);
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);
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);
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
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
)
222 notify('Could not update that course!');
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');
244 /// Print out all the sub-categories
245 if ($subcategories = get_records('course_categories', 'parent', $category->id
, 'sortorder ASC')) {
247 foreach ($subcategories as $subcategory) {
248 if ($subcategory->visible ||
has_capability('moodle/category:viewhiddencategories', $context)) {
249 $subcategorieswereshown = true;
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">';
256 $catlinkcss = $subcategory->visible ?
'' : 'class="dimmed" ';
257 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id
.'">'.
258 format_string($subcategory->name
).'</a><br />';
262 echo '</td></tr></table>';
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);
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);
285 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&perpage=$perpage&");
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>';
310 echo '<th class="header" scope="col">'.$stredit.'</th>';
311 echo '<th class="header" scope="col">'.$strselect.'</th>';
313 echo '<th class="header" scope="col"> </th>';
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);
326 $atlastpage = (($page +
1) == ceil($totalcount / $perpage));
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
;
340 $coursecontext = get_context_instance(CONTEXT_COURSE
, $acourse->id
);
344 $up = ($count > 1 ||
!$atfirstpage);
345 $down = ($count < $numcourses ||
!$atlastpage);
347 $linkcss = $acourse->visible ?
'' : ' class="dimmed" ';
349 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id
.'">'. format_string($acourse->fullname
) .'</a></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> ';
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> ';
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> ';
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
.'&page='.$page.
378 '&perpage='.$perpage.'&hide='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
379 '<img src="'.$CFG->pixpath
.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
381 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id
.'&page='.$page.
382 '&perpage='.$perpage.'&show='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
383 '<img src="'.$CFG->pixpath
.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
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> ';
396 if (has_capability('moodle/site:restore', $coursecontext)) {
397 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id
.
398 '&wdir=/backupdata">'.
399 '<img src="'.$CFG->pixpath
.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
404 if (has_capability('moodle/category:manage', $context)) {
406 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id
.'&page='.$page.
407 '&perpage='.$perpage.'&moveup='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
408 '<img src="'.$CFG->pixpath
.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
414 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id
.'&page='.$page.
415 '&perpage='.$perpage.'&movedown='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
416 '<img src="'.$CFG->pixpath
.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
420 $abletomovecourses = true;
422 echo $spacer, $spacer;
426 echo '<td align="center">';
427 echo '<input type="checkbox" name="c'.$acourse->id
.'" />';
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);
451 if ($abletomovecourses) {
452 $movetocategories = 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
.'" />';
463 echo '</div></form>';
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
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
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
));
489 print_course_search();