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 $options = new stdClass();
135 $options->noclean
= true;
136 $options->para
= false;
137 echo format_text($category->description
, FORMAT_MOODLE
, $options); // for multilang filter
141 /// Process any course actions.
143 /// Move a specified course to a new category
144 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
145 // user must have category update in both cats to perform this
146 require_capability('moodle/category:manage', $context);
147 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT
, $moveto));
149 if (!$destcategory = get_record('course_categories', 'id', $data->moveto
)) {
150 error('Error finding the category');
154 foreach ($data as $key => $value) {
155 if (preg_match('/^c\d+$/', $key)) {
156 $courseid = substr($key, 1);
157 array_push($courses, $courseid);
159 // check this course's category
160 if ($movingcourse = get_record('course', 'id', $courseid)) {
161 if ($movingcourse->category
!= $id ) {
162 error('The course doesn\'t belong to this category');
165 error('Error finding the course');
169 move_courses($courses, $data->moveto
);
172 /// Hide or show a course
173 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
175 $course = get_record('course', 'id', $hide);
178 $course = get_record('course', 'id', $show);
183 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
);
184 require_capability('moodle/course:visibility', $coursecontext);
185 if (!set_field('course', 'visible', $visible, 'id', $course->id
)) {
186 notify('Could not update that course!');
191 /// Move a course up or down
192 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
193 require_capability('moodle/category:manage', $context);
197 // ensure the course order has no gaps and isn't at 0
198 fix_course_sortorder($category->id
);
200 // we are going to need to know the range
201 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
202 FROM ' . $CFG->prefix
. 'course WHERE category=' . $category->id
);
203 $max = $max->max +
100;
205 if (!empty($moveup)) {
206 $movecourse = get_record('course', 'id', $moveup);
207 $swapcourse = get_record('course', 'category', $category->id
,
208 'sortorder', $movecourse->sortorder
- 1);
210 $movecourse = get_record('course', 'id', $movedown);
211 $swapcourse = get_record('course', 'category', $category->id
,
212 'sortorder', $movecourse->sortorder +
1);
214 if ($swapcourse and $movecourse) {
215 // check course's category
216 if ($movecourse->category
!= $id) {
217 error('The course doesn\'t belong to this category');
219 // Renumber everything for robustness
221 if (!( set_field('course', 'sortorder', $max, 'id', $swapcourse->id
)
222 && set_field('course', 'sortorder', $swapcourse->sortorder
, 'id', $movecourse->id
)
223 && set_field('course', 'sortorder', $movecourse->sortorder
, 'id', $swapcourse->id
)
225 notify('Could not update that course!');
231 } // End of editing stuff
233 if ($editingon && has_capability('moodle/category:manage', $context)) {
234 echo '<div class="buttons">';
236 // Print button to update this category
237 $options = array('id' => $category->id
);
238 print_single_button($CFG->wwwroot
.'/course/editcategory.php', $options, get_string('editcategorythis'), 'get');
240 // Print button for creating new categories
241 $options = array('parent' => $category->id
);
242 print_single_button($CFG->wwwroot
.'/course/editcategory.php', $options, get_string('addsubcategory'), 'get');
247 /// Print out all the sub-categories
248 if ($subcategories = get_records('course_categories', 'parent', $category->id
, 'sortorder ASC')) {
250 foreach ($subcategories as $subcategory) {
251 if ($subcategory->visible ||
has_capability('moodle/category:viewhiddencategories', $context)) {
252 $subcategorieswereshown = true;
254 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
255 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
256 echo '<tr><td style="white-space: nowrap">';
259 $catlinkcss = $subcategory->visible ?
'' : 'class="dimmed" ';
260 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id
.'">'.
261 format_string($subcategory->name
).'</a><br />';
265 echo '</td></tr></table>';
271 /// Print out all the courses
272 $courses = get_courses_page($category->id
, 'c.sortorder ASC',
273 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
274 $totalcount, $page*$perpage, $perpage);
275 $numcourses = count($courses);
278 if (empty($subcategorieswereshown)) {
279 print_heading(get_string("nocoursesyet"));
282 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE
and !$page and !$editingon) {
283 print_box_start('courseboxes');
284 print_courses($category);
288 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&perpage=$perpage&");
290 $strcourses = get_string('courses');
291 $strselect = get_string('select');
292 $stredit = get_string('edit');
293 $strdelete = get_string('delete');
294 $strbackup = get_string('backup');
295 $strrestore = get_string('restore');
296 $strmoveup = get_string('moveup');
297 $strmovedown = get_string('movedown');
298 $strupdate = get_string('update');
299 $strhide = get_string('hide');
300 $strshow = get_string('show');
301 $strsummary = get_string('summary');
302 $strsettings = get_string('settings');
303 $strassignteachers = get_string('assignteachers');
304 $strallowguests = get_string('allowguests');
305 $strrequireskey = get_string('requireskey');
308 echo '<form id="movecourses" action="category.php" method="post"><div>';
309 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
310 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
311 echo '<th class="header" scope="col">'.$strcourses.'</th>';
313 echo '<th class="header" scope="col">'.$stredit.'</th>';
314 echo '<th class="header" scope="col">'.$strselect.'</th>';
316 echo '<th class="header" scope="col"> </th>';
322 $abletomovecourses = false; // for now
324 // Checking if we are at the first or at the last page, to allow courses to
325 // be moved up and down beyond the paging border
326 if ($totalcount > $perpage) {
327 $atfirstpage = ($page == 0);
329 $atlastpage = (($page +
1) == ceil($totalcount / $perpage));
338 $spacer = '<img src="'.$CFG->wwwroot
.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
339 foreach ($courses as $acourse) {
340 if (isset($acourse->context
)) {
341 $coursecontext = $acourse->context
;
343 $coursecontext = get_context_instance(CONTEXT_COURSE
, $acourse->id
);
347 $up = ($count > 1 ||
!$atfirstpage);
348 $down = ($count < $numcourses ||
!$atlastpage);
350 $linkcss = $acourse->visible ?
'' : ' class="dimmed" ';
352 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id
.'">'. format_string($acourse->fullname
) .'</a></td>';
355 if (has_capability('moodle/course:update', $coursecontext)) {
356 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot
.'/course/edit.php?id='.$acourse->id
.'">'.
357 '<img src="'.$CFG->pixpath
.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> ';
362 // role assignment link
363 if (has_capability('moodle/role:assign', $coursecontext)) {
364 echo '<a title="'.get_string('assignroles', 'role').'" href="'.$CFG->wwwroot
.'/'.$CFG->admin
.'/roles/assign.php?contextid='.$coursecontext->id
.'">'.
365 '<img src="'.$CFG->pixpath
.'/i/roles.gif" class="iconsmall" alt="'.get_string('assignroles', 'role').'" /></a> ';
370 if (can_delete_course($acourse->id
)) {
371 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id
.'">'.
372 '<img src="'.$CFG->pixpath
.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
377 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
378 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
379 if (!empty($acourse->visible
)) {
380 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id
.'&page='.$page.
381 '&perpage='.$perpage.'&hide='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
382 '<img src="'.$CFG->pixpath
.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
384 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id
.'&page='.$page.
385 '&perpage='.$perpage.'&show='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
386 '<img src="'.$CFG->pixpath
.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
392 if (has_capability('moodle/site:backup', $coursecontext)) {
393 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id
.'">'.
394 '<img src="'.$CFG->pixpath
.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
399 if (has_capability('moodle/site:restore', $coursecontext)) {
400 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id
.
401 '&wdir=/backupdata">'.
402 '<img src="'.$CFG->pixpath
.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
407 if (has_capability('moodle/category:manage', $context)) {
409 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id
.'&page='.$page.
410 '&perpage='.$perpage.'&moveup='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
411 '<img src="'.$CFG->pixpath
.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
417 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id
.'&page='.$page.
418 '&perpage='.$perpage.'&movedown='.$acourse->id
.'&sesskey='.$USER->sesskey
.'">'.
419 '<img src="'.$CFG->pixpath
.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
423 $abletomovecourses = true;
425 echo $spacer, $spacer;
429 echo '<td align="center">';
430 echo '<input type="checkbox" name="c'.$acourse->id
.'" />';
433 echo '<td align="right">';
434 if (!empty($acourse->guest
)) {
435 echo '<a href="view.php?id='.$acourse->id
.'"><img title="'.
436 $strallowguests.'" class="icon" src="'.
437 $CFG->pixpath
.'/i/guest.gif" alt="'.$strallowguests.'" /></a>';
439 if (!empty($acourse->password
)) {
440 echo '<a href="view.php?id='.$acourse->id
.'"><img title="'.
441 $strrequireskey.'" class="icon" src="'.
442 $CFG->pixpath
.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
444 if (!empty($acourse->summary
)) {
445 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
446 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath
.'/i/info.gif" />',
447 400, 500, $strsummary);
454 if ($abletomovecourses) {
455 $movetocategories = array();
457 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
458 $movetocategories[$category->id
] = get_string('moveselectedcoursesto');
459 echo '<tr><td colspan="3" align="right">';
460 choose_from_menu($movetocategories, 'moveto', $category->id
, '', "javascript:submitFormById('movecourses')");
461 echo '<input type="hidden" name="id" value="'.$category->id
.'" />';
466 echo '</div></form>';
470 echo '<div class="buttons">';
471 if (has_capability('moodle/category:manage', $context) and $numcourses > 1) {
472 /// Print button to re-sort courses by name
474 $options['id'] = $category->id
;
475 $options['resort'] = 'name';
476 $options['sesskey'] = $USER->sesskey
;
477 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
480 if (has_capability('moodle/course:create', $context)) {
481 /// Print button to create a new course
483 $options['category'] = $category->id
;
484 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
487 if (!empty($CFG->enablecourserequests
) && $category->id
== $CFG->enablecourserequests
) {
488 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM
));
492 print_course_search();