Call theme_setup() after change so new theme displays correctly
[moodle-linuxchix.git] / course / category.php
blobe082827e9c3678b4d4cb9e0ff15855ca6d6f9bd2
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 $rename = optional_param('rename', '', PARAM_NOTAGS);
19 $resort = optional_param('resort', 0, PARAM_BOOL);
20 $categorytheme= optional_param('categorytheme', false, PARAM_CLEAN);
22 if (!$site = get_site()) {
23 error("Site isn't defined!");
26 $context = get_context_instance(CONTEXT_COURSECAT, $id);
28 if ($CFG->forcelogin) {
29 require_login();
32 if (!$category = get_record("course_categories", "id", $id)) {
33 error("Category not known!");
36 if (has_capability('moodle/course:create', $context)) {
37 if ($categoryedit !== -1) {
38 $USER->categoryediting = $categoryedit;
40 $navbaritem = update_category_button($category->id);
41 $creatorediting = !empty($USER->categoryediting);
42 $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $creatorediting);
44 } else {
45 if (!$category->visible) {
46 error(get_string('notavailable', 'error'));
48 $navbaritem = print_course_search("", true, "navbar");
49 $adminediting = false;
50 $creatorediting = false;
54 if (has_capability('moodle/category:update', $context)) {
55 /// Rename the category if requested
56 if (!empty($rename) and confirm_sesskey()) {
57 $category->name = $rename;
58 if (! set_field("course_categories", "name", $category->name, "id", $category->id)) {
59 notify("An error occurred while renaming the category");
63 /// Set the category theme if requested
64 if (($categorytheme !== false) and confirm_sesskey()) {
65 $category->theme = $categorytheme;
66 if (! set_field('course_categories', 'theme', $category->theme, 'id', $category->id)) {
67 notify('An error occurred while setting the theme');
68 } else {
69 theme_setup();
73 /// Resort the category if requested
75 if ($resort and confirm_sesskey()) {
76 if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
77 // move it off the range
78 $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
79 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
80 $count = $count->max + 100;
81 begin_sql();
82 foreach ($courses as $course) {
83 set_field('course', 'sortorder', $count, 'id', $course->id);
84 $count++;
86 commit_sql();
87 fix_course_sortorder($category->id);
93 /// Print headings
95 $numcategories = count_records("course_categories");
97 $stradministration = get_string("administration");
98 $strcategories = get_string("categories");
99 $strcategory = get_string("category");
100 $strcourses = get_string("courses");
102 if ($creatorediting) {
103 if ($adminediting) {
104 // modify this to treat this as an admin page
106 require_once($CFG->libdir.'/adminlib.php');
107 admin_externalpage_setup('coursemgmt');
108 admin_externalpage_print_header();
109 } else {
110 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
111 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
113 } else {
114 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
115 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
118 /// Print button to turn editing off
119 if ($adminediting) {
120 echo '<div class="categoryediting button" align="right">'.update_category_button($category->id).'</div>';
123 /// Print link to roles
125 if (has_capability('moodle/role:assign', $context)) {
126 echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
127 $context->id.'">'.get_string('assignroles','role').'</a></div>';
129 /// Print the category selector
131 $displaylist = array();
132 $parentlist = array();
134 make_categories_list($displaylist, $parentlist, "");
136 echo '<div class="categorypicker">';
137 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
138 echo '</div>';
141 /// Editing functions
143 if ($creatorediting) {
144 /// Move a specified course to a new category
146 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
148 // user must have category update in both cats to perform this
149 require_capability('moodle/category:update', $context);
150 require_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $moveto));
152 if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
153 error("Error finding the category");
157 $courses = array();
158 foreach ( $data as $key => $value ) {
159 if (preg_match('/^c\d+$/', $key)) {
160 array_push($courses, substr($key, 1));
163 move_courses($courses, $data->moveto);
166 /// Hide or show a course
168 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
169 require_capability('moodle/course:visibility', $context);
170 if (!empty($hide)) {
171 $course = get_record("course", "id", $hide);
172 $visible = 0;
173 } else {
174 $course = get_record("course", "id", $show);
175 $visible = 1;
177 if ($course) {
178 if (! set_field("course", "visible", $visible, "id", $course->id)) {
179 notify("Could not update that course!");
185 /// Move a course up or down
187 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
188 require_capability('moodle/category:update', $context);
189 $movecourse = NULL;
190 $swapcourse = NULL;
192 // ensure the course order has no gaps
193 // and isn't at 0
194 fix_course_sortorder($category->id);
196 // we are going to need to know the range
197 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
198 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
199 $max = $max->max + 100;
201 if (!empty($moveup)) {
202 $movecourse = get_record('course', 'id', $moveup);
203 $swapcourse = get_record('course',
204 'category', $category->id,
205 'sortorder', $movecourse->sortorder - 1);
206 } else {
207 $movecourse = get_record('course', 'id', $movedown);
208 $swapcourse = get_record('course',
209 'category', $category->id,
210 'sortorder', $movecourse->sortorder + 1);
213 if ($swapcourse and $movecourse) { // Renumber everything for robustness
214 begin_sql();
215 if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id)
216 && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
217 && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
218 )) {
219 notify("Could not update that course!");
221 commit_sql();
226 } // End of editing stuff
228 /// Print out all the sub-categories
229 if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
230 $firstentry = true;
231 foreach ($subcategories as $subcategory) {
232 if ($subcategory->visible or has_capability('moodle/course:create', $context)) {
233 $subcategorieswereshown = true;
234 if ($firstentry) {
235 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
236 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
237 echo '<tr><td style="white-space: nowrap">';
238 $firstentry = false;
240 $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
241 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
242 format_string($subcategory->name).'</a><br />';
245 if (!$firstentry) {
246 echo "</td></tr></table>";
247 echo "<br />";
252 /// Print out all the courses
253 unset($course); // To avoid unwanted language effects later
255 $courses = get_courses_page($category->id, 'c.sortorder ASC',
256 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
257 $totalcount, $page*$perpage, $perpage);
258 $numcourses = count($courses);
260 if (!$courses) {
261 if (empty($subcategorieswereshown)) {
262 print_heading(get_string("nocoursesyet"));
265 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$creatorediting) {
266 print_box_start('courseboxes');
267 print_courses($category);
268 print_box_end();
270 } else {
271 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&amp;");
273 $strcourses = get_string("courses");
274 $strselect = get_string("select");
275 $stredit = get_string("edit");
276 $strdelete = get_string("delete");
277 $strbackup = get_string("backup");
278 $strrestore = get_string("restore");
279 $strmoveup = get_string("moveup");
280 $strmovedown = get_string("movedown");
281 $strupdate = get_string("update");
282 $strhide = get_string("hide");
283 $strshow = get_string("show");
284 $strsummary = get_string("summary");
285 $strsettings = get_string("settings");
286 $strassignteachers = get_string("assignteachers");
287 $strallowguests = get_string("allowguests");
288 $strrequireskey = get_string("requireskey");
291 echo '<form id="movecourses" action="category.php" method="post"><div>';
292 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
293 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
294 echo '<th class="header" scope="col">'.$strcourses.'</th>';
295 if ($creatorediting) {
296 echo '<th class="header" scope="col">'.$stredit.'</th>';
297 if ($adminediting) {
298 echo '<th class="header" scope="col">'.$strselect.'</th>';
300 } else {
301 echo '<th class="header" scope="col">&nbsp;</th>';
303 echo '</tr>';
306 $count = 0;
307 $abletomovecourses = false; // for now
309 // Checking if we are at the first or at the last page, to allow courses to
310 // be moved up and down beyond the paging border
311 if ($totalcount > $perpage) {
312 $atfirstpage = ($page == 0);
313 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
314 } else {
315 $atfirstpage = true;
316 $atlastpage = true;
319 foreach ($courses as $acourse) {
321 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
323 $count++;
324 $up = ($count > 1 || !$atfirstpage);
325 $down = ($count < $numcourses || !$atlastpage);
327 $linkcss = $acourse->visible ? "" : ' class="dimmed" ';
328 echo '<tr>';
329 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
330 if ($creatorediting) {
331 echo "<td>";
332 if (has_capability('moodle/course:update', $coursecontext)) {
333 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.
334 $acourse->id.'">'.
335 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> '; }
337 // role assignment link
338 if (has_capability('moodle/role:assign', $coursecontext)) {
339 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>';
342 if (can_delete_course($acourse->id)) {
343 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
344 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
347 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
348 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
349 if (!empty($acourse->visible)) {
350 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&amp;page='.$page.
351 '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
352 '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
353 } else {
354 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
355 '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
356 '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
360 if (has_capability('moodle/site:backup', $coursecontext)) {
361 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
362 '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
365 if (has_capability('moodle/site:restore', $coursecontext)) {
366 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
367 '&amp;wdir=/backupdata">'.
368 '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
371 if (has_capability('moodle/category:update', $context)) {
372 if ($up) {
373 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
374 '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
375 '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
376 } else {
377 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
380 if ($down) {
381 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
382 '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
383 '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
384 } else {
385 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
387 $abletomovecourses = true;
390 echo '</td>';
391 echo '<td align="center">';
392 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
393 echo '</td>';
394 } else {
395 echo '<td align="right">';
396 if (!empty($acourse->guest)) {
397 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
398 $strallowguests.'" class="icon" src="'.
399 $CFG->pixpath.'/i/user.gif" alt="'.$strallowguests.'" /></a>';
401 if (!empty($acourse->password)) {
402 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
403 $strrequireskey.'" class="icon" src="'.
404 $CFG->pixpath.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
406 if (!empty($acourse->summary)) {
407 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
408 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath.'/i/info.gif" />',
409 400, 500, $strsummary);
411 echo "</td>";
413 echo "</tr>";
416 if ($abletomovecourses) {
417 echo '<tr><td colspan="3" align="right">';
418 echo '<br />';
419 unset($displaylist[$category->id]);
421 // loop and unset categories the user can't move into
423 foreach ($displaylist as $did=>$dlist) {
424 if (!has_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $did))) {
425 unset($displaylist[$did]);
429 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: getElementById('movecourses').submit()");
430 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
431 echo '</td></tr>';
434 echo '</table>';
435 echo '</div></form>';
436 echo '<br />';
439 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $numcourses > 1) { /// Print button to re-sort courses by name
440 unset($options);
441 $options['id'] = $category->id;
442 $options['resort'] = 'name';
443 $options['sesskey'] = $USER->sesskey;
444 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
447 if (has_capability('moodle/course:create', $context)) { /// Print button to create a new course
448 unset($options);
449 $options['category'] = $category->id;
450 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
451 echo '<br />';
454 if (has_capability('moodle/category:update', $context)) { /// Print form to rename the category
455 $strrename= get_string('rename');
456 echo '<form id="renameform" action="category.php" method="post"><div>';
457 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
458 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
459 echo '<input type="text" size="30" name="rename" value="'.format_string($category->name).'" alt="'.$strrename.'" />';
460 echo '<input type="submit" value="'.$strrename.'" />';
461 echo '</div></form>';
462 echo '<br />';
464 if (!empty($CFG->allowcategorythemes)) {
465 $choices = array();
466 $choices[''] = get_string('default');
467 $choices += get_list_of_themes();
469 echo '<form id="themeform" action="category.php" method="post"><div>';
470 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
471 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
472 choose_from_menu($choices, 'categorytheme', $category->theme);
473 echo '<input type="submit" value="'.get_string('setcategorytheme').'" />';
474 echo '</div></form>';
475 echo '<br />';
480 print_course_search();
482 print_footer();