Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / course / category.php
blob07a0d3d9ba694285bba4e1f6fbc4fa87965b48b9
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);
21 $addsubcategory=optional_param('addsubcategory', '', PARAM_NOTAGS);
23 if (!$site = get_site()) {
24 error("Site isn't defined!");
27 $context = get_context_instance(CONTEXT_COURSECAT, $id);
29 if ($CFG->forcelogin) {
30 require_login();
33 if (!$category = get_record("course_categories", "id", $id)) {
34 error("Category not known!");
37 if (has_capability('moodle/course:create', $context)) {
38 if ($categoryedit !== -1) {
39 $USER->categoryediting = $categoryedit;
41 $navbaritem = update_category_button($category->id);
42 $creatorediting = !empty($USER->categoryediting);
43 $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $creatorediting);
45 } else {
46 if (!$category->visible) {
47 error(get_string('notavailable', 'error'));
49 $navbaritem = print_course_search("", true, "navbar");
50 $adminediting = false;
51 $creatorediting = false;
55 if (has_capability('moodle/category:create', $context)) {
56 if (!empty($addsubcategory) and confirm_sesskey()) {
57 $subcategory = new stdClass;
58 $subcategory->name = $addsubcategory;
59 $subcategory->sortorder = 999;
60 $subcategory->parent = $id;
61 if (!insert_record('course_categories', $subcategory )) {
62 notify( "Could not insert the new subcategory '$addsubcategory' " );
67 if (has_capability('moodle/category:update', $context)) {
68 /// Rename the category if requested
69 if (!empty($rename) and confirm_sesskey()) {
70 $category->name = $rename;
71 if (! set_field("course_categories", "name", $category->name, "id", $category->id)) {
72 notify("An error occurred while renaming the category");
74 // MDL-9983
75 events_trigger('category_updated', $category);
78 /// Set the category theme if requested
79 if (($categorytheme !== false) and confirm_sesskey()) {
80 $category->theme = $categorytheme;
81 if (! set_field('course_categories', 'theme', $category->theme, 'id', $category->id)) {
82 notify('An error occurred while setting the theme');
83 } else {
84 theme_setup();
88 /// Resort the category if requested
90 if ($resort and confirm_sesskey()) {
91 if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
92 // move it off the range
93 $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
94 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
95 $count = $count->max + 100;
96 begin_sql();
97 foreach ($courses as $course) {
98 set_field('course', 'sortorder', $count, 'id', $course->id);
99 $count++;
101 commit_sql();
102 fix_course_sortorder($category->id);
108 /// Print headings
110 $numcategories = count_records("course_categories");
112 $stradministration = get_string("administration");
113 $strcategories = get_string("categories");
114 $strcategory = get_string("category");
115 $strcourses = get_string("courses");
117 if ($creatorediting) {
118 if ($adminediting) {
119 // modify this to treat this as an admin page
121 require_once($CFG->libdir.'/adminlib.php');
122 admin_externalpage_setup('coursemgmt');
123 admin_externalpage_print_header();
124 } else {
125 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
126 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
128 } else {
129 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
130 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
133 /// Print button to turn editing off
134 if ($adminediting) {
135 echo '<div class="categoryediting button" align="right">'.update_category_button($category->id).'</div>';
138 /// Print link to roles
140 if (has_capability('moodle/role:assign', $context)) {
141 echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
142 $context->id.'">'.get_string('assignroles','role').'</a></div>';
144 /// Print the category selector
146 $displaylist = array();
147 $parentlist = array();
149 make_categories_list($displaylist, $parentlist, "");
151 echo '<div class="categorypicker">';
152 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
153 echo '</div>';
156 /// Editing functions
158 if ($creatorediting) {
159 /// Move a specified course to a new category
161 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
163 // user must have category update in both cats to perform this
164 require_capability('moodle/category:update', $context);
165 require_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $moveto));
167 if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
168 error("Error finding the category");
172 $courses = array();
173 foreach ( $data as $key => $value ) {
174 if (preg_match('/^c\d+$/', $key)) {
175 array_push($courses, substr($key, 1));
178 move_courses($courses, $data->moveto);
181 /// Hide or show a course
183 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
184 require_capability('moodle/course:visibility', $context);
185 if (!empty($hide)) {
186 $course = get_record("course", "id", $hide);
187 $visible = 0;
188 } else {
189 $course = get_record("course", "id", $show);
190 $visible = 1;
192 if ($course) {
193 if (! set_field("course", "visible", $visible, "id", $course->id)) {
194 notify("Could not update that course!");
200 /// Move a course up or down
202 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
203 require_capability('moodle/category:update', $context);
204 $movecourse = NULL;
205 $swapcourse = NULL;
207 // ensure the course order has no gaps
208 // and isn't at 0
209 fix_course_sortorder($category->id);
211 // we are going to need to know the range
212 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
213 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
214 $max = $max->max + 100;
216 if (!empty($moveup)) {
217 $movecourse = get_record('course', 'id', $moveup);
218 $swapcourse = get_record('course',
219 'category', $category->id,
220 'sortorder', $movecourse->sortorder - 1);
221 } else {
222 $movecourse = get_record('course', 'id', $movedown);
223 $swapcourse = get_record('course',
224 'category', $category->id,
225 'sortorder', $movecourse->sortorder + 1);
228 if ($swapcourse and $movecourse) { // Renumber everything for robustness
229 begin_sql();
230 if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id)
231 && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
232 && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
233 )) {
234 notify("Could not update that course!");
236 commit_sql();
241 } // End of editing stuff
243 /// Print out all the sub-categories
244 if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
245 $firstentry = true;
246 foreach ($subcategories as $subcategory) {
247 if ($subcategory->visible or has_capability('moodle/course:create', $context)) {
248 $subcategorieswereshown = true;
249 if ($firstentry) {
250 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
251 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
252 echo '<tr><td style="white-space: nowrap">';
253 $firstentry = false;
255 $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
256 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
257 format_string($subcategory->name).'</a><br />';
260 if (!$firstentry) {
261 echo "</td></tr></table>";
262 echo "<br />";
266 /// print option to add a subcategory
267 if (has_capability('moodle/category:create', $context)) {
268 $straddsubcategory = get_string('addsubcategory');
269 echo '<div class="addcategory">';
270 echo '<form id="addform" action="category.php" method="post">';
271 echo '<fieldset class="invisiblefieldset">';
272 echo '<input type="text" size="30" alt="'.$straddsubcategory.'" name="addsubcategory" />';
273 echo '<input type="submit" value="'.$straddsubcategory.'" />';
274 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
275 echo '<input type="hidden" name="id" value="'.$id.'" />';
276 // echo '<input type="hidden" name="categoryedit" value="'.$categoryedit.'" />';
277 echo '</fieldset>';
278 echo '</form>';
279 echo '</div>';
282 /// Print out all the courses
283 unset($course); // To avoid unwanted language effects later
285 $courses = get_courses_page($category->id, 'c.sortorder ASC',
286 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
287 $totalcount, $page*$perpage, $perpage);
288 $numcourses = count($courses);
290 if (!$courses) {
291 if (empty($subcategorieswereshown)) {
292 print_heading(get_string("nocoursesyet"));
295 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$creatorediting) {
296 print_box_start('courseboxes');
297 print_courses($category);
298 print_box_end();
300 } else {
301 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&amp;");
303 $strcourses = get_string("courses");
304 $strselect = get_string("select");
305 $stredit = get_string("edit");
306 $strdelete = get_string("delete");
307 $strbackup = get_string("backup");
308 $strrestore = get_string("restore");
309 $strmoveup = get_string("moveup");
310 $strmovedown = get_string("movedown");
311 $strupdate = get_string("update");
312 $strhide = get_string("hide");
313 $strshow = get_string("show");
314 $strsummary = get_string("summary");
315 $strsettings = get_string("settings");
316 $strassignteachers = get_string("assignteachers");
317 $strallowguests = get_string("allowguests");
318 $strrequireskey = get_string("requireskey");
321 echo '<form id="movecourses" action="category.php" method="post"><div>';
322 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
323 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
324 echo '<th class="header" scope="col">'.$strcourses.'</th>';
325 if ($creatorediting) {
326 echo '<th class="header" scope="col">'.$stredit.'</th>';
327 if ($adminediting) {
328 echo '<th class="header" scope="col">'.$strselect.'</th>';
330 } else {
331 echo '<th class="header" scope="col">&nbsp;</th>';
333 echo '</tr>';
336 $count = 0;
337 $abletomovecourses = false; // for now
339 // Checking if we are at the first or at the last page, to allow courses to
340 // be moved up and down beyond the paging border
341 if ($totalcount > $perpage) {
342 $atfirstpage = ($page == 0);
343 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
344 } else {
345 $atfirstpage = true;
346 $atlastpage = true;
349 foreach ($courses as $acourse) {
351 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
353 $count++;
354 $up = ($count > 1 || !$atfirstpage);
355 $down = ($count < $numcourses || !$atlastpage);
357 $linkcss = $acourse->visible ? "" : ' class="dimmed" ';
358 echo '<tr>';
359 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
360 if ($creatorediting) {
361 echo "<td>";
362 if (has_capability('moodle/course:update', $coursecontext)) {
363 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.
364 $acourse->id.'">'.
365 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> '; }
367 // role assignment link
368 if (has_capability('moodle/role:assign', $coursecontext)) {
369 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>';
372 if (can_delete_course($acourse->id)) {
373 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
374 '<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.'&amp;page='.$page.
381 '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
382 '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
383 } else {
384 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
385 '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
386 '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
390 if (has_capability('moodle/site:backup', $coursecontext)) {
391 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
392 '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
395 if (has_capability('moodle/site:restore', $coursecontext)) {
396 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
397 '&amp;wdir=/backupdata">'.
398 '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
401 if (has_capability('moodle/category:update', $context)) {
402 if ($up) {
403 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
404 '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
405 '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
406 } else {
407 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
410 if ($down) {
411 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
412 '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
413 '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
414 } else {
415 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
417 $abletomovecourses = true;
420 echo '</td>';
421 echo '<td align="center">';
422 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
423 echo '</td>';
424 } else {
425 echo '<td align="right">';
426 if (!empty($acourse->guest)) {
427 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
428 $strallowguests.'" class="icon" src="'.
429 $CFG->pixpath.'/i/user.gif" alt="'.$strallowguests.'" /></a>';
431 if (!empty($acourse->password)) {
432 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
433 $strrequireskey.'" class="icon" src="'.
434 $CFG->pixpath.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
436 if (!empty($acourse->summary)) {
437 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
438 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath.'/i/info.gif" />',
439 400, 500, $strsummary);
441 echo "</td>";
443 echo "</tr>";
446 if ($abletomovecourses) {
447 echo '<tr><td colspan="3" align="right">';
448 echo '<br />';
449 unset($displaylist[$category->id]);
451 // loop and unset categories the user can't move into
453 foreach ($displaylist as $did=>$dlist) {
454 if (!has_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $did))) {
455 unset($displaylist[$did]);
459 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: submitFormById('movecourses')");
460 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
461 echo '</td></tr>';
464 echo '</table>';
465 echo '</div></form>';
466 echo '<br />';
469 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $numcourses > 1) { /// Print button to re-sort courses by name
470 unset($options);
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)) { /// Print button to create a new course
478 unset($options);
479 $options['category'] = $category->id;
480 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
481 echo '<br />';
484 if (has_capability('moodle/category:update', $context)) { /// Print form to rename the category
485 $strrename= get_string('rename');
486 echo '<form id="renameform" action="category.php" method="post"><div>';
487 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
488 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
489 echo '<input type="text" size="30" name="rename" value="'.format_string($category->name).'" alt="'.$strrename.'" />';
490 echo '<input type="submit" value="'.$strrename.'" />';
491 echo '</div></form>';
492 echo '<br />';
494 if (!empty($CFG->allowcategorythemes)) {
495 $choices = array();
496 $choices[''] = get_string('default');
497 $choices += get_list_of_themes();
499 echo '<form id="themeform" action="category.php" method="post"><div>';
500 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
501 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
502 choose_from_menu($choices, 'categorytheme', $category->theme);
503 echo '<input type="submit" value="'.get_string('setcategorytheme').'" />';
504 echo '</div></form>';
505 echo '<br />';
510 print_course_search();
512 print_footer();