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 $categorytheme= optional_param('categorytheme', false, PARAM_SAFEDIR
);
22 if ($CFG->forcelogin
) {
26 if (!$site = get_site()) {
27 error("Site isn't defined!");
31 error("Category not known!");
34 if (!$context = get_context_instance(CONTEXT_COURSECAT
, $id)) {
35 error("Category not known!");
38 if (!$category = get_record("course_categories", "id", $id)) {
39 error("Category not known!");
42 if (has_capability('moodle/course:create', $context)) {
43 if ($categoryedit !== -1) {
44 $USER->categoryediting
= $categoryedit;
46 $navbaritem = update_category_button($category->id
);
47 $creatorediting = !empty($USER->categoryediting
);
48 $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
)) and $creatorediting);
51 if (!$category->visible
) {
52 print_error('notavailable', 'error');
54 $navbaritem = print_course_search("", true, "navbar");
55 $adminediting = false;
56 $creatorediting = false;
59 if (has_capability('moodle/category:update', $context)) {
60 /// Rename the category if requested
61 if (!empty($rename) and confirm_sesskey()) {
62 if (! set_field("course_categories", "name", $rename, "id", $category->id
)) {
63 notify("An error occurred while renaming the category");
65 $category->name
= stripslashes($rename);
67 events_trigger('course_category_updated', $category);
70 /// Set the category theme if requested
71 if (($categorytheme !== false) and confirm_sesskey()) {
72 $category->theme
= $categorytheme;
73 if (! set_field('course_categories', 'theme', $category->theme
, 'id', $category->id
)) {
74 notify('An error occurred while setting the theme');
78 /// Resort the category if requested
80 if ($resort and confirm_sesskey()) {
81 if ($courses = get_courses($category->id
, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
82 // move it off the range
83 $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
84 FROM ' . $CFG->prefix
. 'course WHERE category=' . $category->id
);
85 $count = $count->max +
100;
87 foreach ($courses as $course) {
88 set_field('course', 'sortorder', $count, 'id', $course->id
);
92 fix_course_sortorder($category->id
);
97 if(! empty($CFG->allowcategorythemes
) ){
98 if(isset($category->theme
)){
99 // specifying theme here saves us some dbqs
100 theme_setup($category->theme
);
106 $numcategories = count_records("course_categories");
108 $stradministration = get_string("administration");
109 $strcategories = get_string("categories");
110 $strcategory = get_string("category");
111 $strcourses = get_string("courses");
114 $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc');
115 $navlinks[] = array('name' => format_string($category->name
), 'link' => null, 'type' => 'misc');
116 $navigation = build_navigation($navlinks);
118 if ($creatorediting) {
120 // modify this to treat this as an admin page
122 require_once($CFG->libdir
.'/adminlib.php');
123 admin_externalpage_setup('coursemgmt');
124 admin_externalpage_print_header();
126 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, "", "", true, $navbaritem);
129 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, "", "", true, $navbaritem);
132 /// Print button to turn editing off
134 echo '<div class="categoryediting button">'.update_category_button($category->id
).'</div>';
137 /// Print link to roles
139 if (has_capability('moodle/role:assign', $context)) {
140 echo '<div class="rolelink"><a href="'.$CFG->wwwroot
.'/'.$CFG->admin
.'/roles/assign.php?contextid='.
141 $context->id
.'">'.get_string('assignroles','role').'</a></div>';
143 /// Print the category selector
145 $displaylist = array();
146 $parentlist = array();
148 make_categories_list($displaylist, $parentlist, "");
150 echo '<div class="categorypicker">';
151 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id
, '', '', '', false, 'self', $strcategories.':');
154 /// Print current category description
155 if (!$creatorediting && $category->description
) {
157 echo format_text($category->description
); // for multilang filter
161 /// Editing functions
163 if ($creatorediting) {
164 /// Move a specified course to a new category
166 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
168 // user must have category update in both cats to perform this
169 require_capability('moodle/category:update', $context);
170 require_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT
, $moveto));
172 if (! $destcategory = get_record("course_categories", "id", $data->moveto
)) {
173 error("Error finding the category");
178 foreach ( $data as $key => $value ) {
179 if (preg_match('/^c\d+$/', $key)) {
180 array_push($courses, substr($key, 1));
183 move_courses($courses, $data->moveto
);
186 /// Hide or show a course
188 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
189 require_capability('moodle/course:visibility', $context);
191 $course = get_record("course", "id", $hide);
194 $course = get_record("course", "id", $show);
198 if (! set_field("course", "visible", $visible, "id", $course->id
)) {
199 notify("Could not update that course!");
205 /// Move a course up or down
207 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
208 require_capability('moodle/category:update', $context);
212 // ensure the course order has no gaps
214 fix_course_sortorder($category->id
);
216 // we are going to need to know the range
217 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
218 FROM ' . $CFG->prefix
. 'course WHERE category=' . $category->id
);
219 $max = $max->max +
100;
221 if (!empty($moveup)) {
222 $movecourse = get_record('course', 'id', $moveup);
223 $swapcourse = get_record('course',
224 'category', $category->id
,
225 'sortorder', $movecourse->sortorder
- 1);
227 $movecourse = get_record('course', 'id', $movedown);
228 $swapcourse = get_record('course',
229 'category', $category->id
,
230 'sortorder', $movecourse->sortorder +
1);
233 if ($swapcourse and $movecourse) { // Renumber everything for robustness
235 if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id
)
236 && set_field("course", "sortorder", $swapcourse->sortorder
, "id", $movecourse->id
)
237 && set_field("course", "sortorder", $movecourse->sortorder
, "id", $swapcourse->id
)
239 notify("Could not update that course!");
246 } // End of editing stuff
249 if ($creatorediting) {
250 echo '<div class="buttons">';
251 if (has_capability('moodle/category:update', $context)) { // Print button to update this category
253 $options['id'] = $category->id
;
254 print_single_button('editcategory.php', $options, get_string('editcategorythis'), 'get');
257 if (has_capability('moodle/category:create', $context)) { // Print button for creating new categories
259 $options['categoryadd'] = 1;
260 $options['id'] = $id;
261 print_single_button('editcategory.php', $options, get_string('addsubcategory'), 'get');
266 /// Print out all the sub-categories
267 if ($subcategories = get_records("course_categories", "parent", $category->id
, "sortorder ASC")) {
269 foreach ($subcategories as $subcategory) {
270 if ($subcategory->visible
or has_capability('moodle/course:create', $context)) {
271 $subcategorieswereshown = true;
273 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
274 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
275 echo '<tr><td style="white-space: nowrap">';
278 $catlinkcss = $subcategory->visible ?
"" : " class=\"dimmed\" ";
279 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id
.'">'.
280 format_string($subcategory->name
).'</a><br />';
284 echo "</td></tr></table>";
290 /// Print out all the courses
291 unset($course); // To avoid unwanted language effects later
293 $courses = get_courses_page($category->id
, 'c.sortorder ASC',
294 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
295 $totalcount, $page*$perpage, $perpage);
296 $numcourses = count($courses);
299 if (empty($subcategorieswereshown)) {
300 print_heading(get_string("nocoursesyet"));
303 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE
and !$page and !$creatorediting) {
304 print_box_start('courseboxes');
305 print_courses($category);
309 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&perpage=$perpage&");
311 $strcourses = get_string("courses");
312 $strselect = get_string("select");
313 $stredit = get_string("edit");
314 $strdelete = get_string("delete");
315 $strbackup = get_string("backup");
316 $strrestore = get_string("restore");
317 $strmoveup = get_string("moveup");
318 $strmovedown = get_string("movedown");
319 $strupdate = get_string("update");
320 $strhide = get_string("hide");
321 $strshow = get_string("show");
322 $strsummary = get_string("summary");
323 $strsettings = get_string("settings");
324 $strassignteachers = get_string("assignteachers");
325 $strallowguests = get_string("allowguests");
326 $strrequireskey = get_string("requireskey");
329 echo '<form id="movecourses" action="category.php" method="post"><div>';
330 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
331 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
332 echo '<th class="header" scope="col">'.$strcourses.'</th>';
333 if ($creatorediting) {
334 echo '<th class="header" scope="col">'.$stredit.'</th>';
336 echo '<th class="header" scope="col">'.$strselect.'</th>';
339 echo '<th class="header" scope="col"> </th>';
345 $abletomovecourses = false; // for now
347 // Checking if we are at the first or at the last page, to allow courses to
348 // be moved up and down beyond the paging border
349 if ($totalcount > $perpage) {
350 $atfirstpage = ($page == 0);
352 $atlastpage = (($page +
1) == ceil($totalcount / $perpage));
361 foreach ($courses as $acourse) {
362 if (isset($acourse->context
)) {
363 $coursecontext = $acourse->context
;
365 $coursecontext = get_context_instance(CONTEXT_COURSE
, $acourse->id
);
369 $up = ($count > 1 ||
!$atfirstpage);
370 $down = ($count < $numcourses ||
!$atlastpage);
372 $linkcss = $acourse->visible ?
"" : ' class="dimmed" ';
374 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id
.'">'. format_string($acourse->fullname
) .'</a></td>';
375 if ($creatorediting) {
377 if (has_capability('moodle/course:update', $coursecontext)) {
378 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot
.'/course/edit.php?id='.
380 '<img src="'.$CFG->pixpath
.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> '; }
382 // role assignment link
383 if (has_capability('moodle/role:assign', $coursecontext)) {
384 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>';
387 if (can_delete_course($acourse->id
)) {
388 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id
.'">'.
389 '<img src="'.$CFG->pixpath
.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
392 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
393 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
394 if (!empty($acourse->visible
)) {
395 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id
.'&page='.$page.
396 '&perpage='.$perpage.'&hide='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
397 '<img src="'.$CFG->pixpath
.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
399 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id
.'&page='.$page.
400 '&perpage='.$perpage.'&show='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
401 '<img src="'.$CFG->pixpath
.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
405 if (has_capability('moodle/site:backup', $coursecontext)) {
406 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id
.'">'.
407 '<img src="'.$CFG->pixpath
.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
410 if (has_capability('moodle/site:restore', $coursecontext)) {
411 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id
.
412 '&wdir=/backupdata">'.
413 '<img src="'.$CFG->pixpath
.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
416 if (has_capability('moodle/category:update', $context)) {
418 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id
.'&page='.$page.
419 '&perpage='.$perpage.'&moveup='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
420 '<img src="'.$CFG->pixpath
.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
422 echo '<img src="'.$CFG->wwwroot
.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
426 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id
.'&page='.$page.
427 '&perpage='.$perpage.'&movedown='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
428 '<img src="'.$CFG->pixpath
.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
430 echo '<img src="'.$CFG->wwwroot
.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
432 $abletomovecourses = true;
436 echo '<td align="center">';
437 echo '<input type="checkbox" name="c'.$acourse->id
.'" />';
440 echo '<td align="right">';
441 if (!empty($acourse->guest
)) {
442 echo '<a href="view.php?id='.$acourse->id
.'"><img title="'.
443 $strallowguests.'" class="icon" src="'.
444 $CFG->pixpath
.'/i/user.gif" alt="'.$strallowguests.'" /></a>';
446 if (!empty($acourse->password
)) {
447 echo '<a href="view.php?id='.$acourse->id
.'"><img title="'.
448 $strrequireskey.'" class="icon" src="'.
449 $CFG->pixpath
.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
451 if (!empty($acourse->summary
)) {
452 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
453 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath
.'/i/info.gif" />',
454 400, 500, $strsummary);
461 if ($abletomovecourses) {
462 echo '<tr><td colspan="3" align="right">';
464 unset($displaylist[$category->id
]);
466 // loop and unset categories the user can't move into
468 foreach ($displaylist as $did=>$dlist) {
469 if (!has_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT
, $did))) {
470 unset($displaylist[$did]);
474 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: submitFormById('movecourses')");
475 echo '<input type="hidden" name="id" value="'.$category->id
.'" />';
480 echo '</div></form>';
484 echo '<div class="buttons">';
485 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM
)) and $numcourses > 1) { /// Print button to re-sort courses by name
487 $options['id'] = $category->id
;
488 $options['resort'] = 'name';
489 $options['sesskey'] = $USER->sesskey
;
490 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
493 if (has_capability('moodle/course:create', $context)) { /// Print button to create a new course
495 $options['category'] = $category->id
;
496 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
502 print_course_search();