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
60 $count = get_record_sql('SELECT MIN(sortorder) AS min, 1
61 FROM ' . $CFG->prefix
. 'course WHERE category=' . $category->id
);
64 foreach ($courses as $course) {
65 set_field('course', 'sortorder', $count, 'id', $course->id
);
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
);
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');
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();
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();
112 make_categories_list($displaylist, $notused);
114 echo '<div class="categorypicker">';
115 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id
, '', '', '', false, 'self', $strcategories.':');
118 /// Print current category description
119 if (!$editingon && $category->description
) {
121 echo format_text($category->description
); // for multilang filter
125 /// Process any course actions.
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');
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');
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()) {
159 $course = get_record('course', 'id', $hide);
162 $course = get_record('course', 'id', $show);
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);
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);
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
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
)
209 notify('Could not update that course!');
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');
231 /// Print out all the sub-categories
232 if ($subcategories = get_records('course_categories', 'parent', $category->id
, 'sortorder ASC')) {
234 foreach ($subcategories as $subcategory) {
235 if ($subcategory->visible ||
has_capability('moodle/category:viewhiddencategories', $context)) {
236 $subcategorieswereshown = true;
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">';
243 $catlinkcss = $subcategory->visible ?
'' : 'class="dimmed" ';
244 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id
.'">'.
245 format_string($subcategory->name
).'</a><br />';
249 echo '</td></tr></table>';
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);
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);
272 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&perpage=$perpage&");
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>';
297 echo '<th class="header" scope="col">'.$stredit.'</th>';
298 echo '<th class="header" scope="col">'.$strselect.'</th>';
300 echo '<th class="header" scope="col"> </th>';
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);
313 $atlastpage = (($page +
1) == ceil($totalcount / $perpage));
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
;
327 $coursecontext = get_context_instance(CONTEXT_COURSE
, $acourse->id
);
331 $up = ($count > 1 ||
!$atfirstpage);
332 $down = ($count < $numcourses ||
!$atlastpage);
334 $linkcss = $acourse->visible ?
'' : ' class="dimmed" ';
336 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id
.'">'. format_string($acourse->fullname
) .'</a></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> ';
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> ';
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> ';
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
.'&page='.$page.
365 '&perpage='.$perpage.'&hide='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
366 '<img src="'.$CFG->pixpath
.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
368 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id
.'&page='.$page.
369 '&perpage='.$perpage.'&show='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
370 '<img src="'.$CFG->pixpath
.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
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> ';
383 if (has_capability('moodle/site:restore', $coursecontext)) {
384 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id
.
385 '&wdir=/backupdata">'.
386 '<img src="'.$CFG->pixpath
.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
391 if (has_capability('moodle/category:manage', $context)) {
393 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id
.'&page='.$page.
394 '&perpage='.$perpage.'&moveup='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
395 '<img src="'.$CFG->pixpath
.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
401 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id
.'&page='.$page.
402 '&perpage='.$perpage.'&movedown='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
403 '<img src="'.$CFG->pixpath
.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
407 $abletomovecourses = true;
409 echo $spacer, $spacer;
413 echo '<td align="center">';
414 echo '<input type="checkbox" name="c'.$acourse->id
.'" />';
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);
438 if ($abletomovecourses) {
439 $movetocategories = 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
.'" />';
450 echo '</div></form>';
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
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
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
));
476 print_course_search();