Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / course / category.php
blob751063662d2cd5b9434cc7f59e86b6428ab04e01
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);
21 if (!$site = get_site()) {
22 error("Site isn't defined!");
25 $context = get_context_instance(CONTEXT_COURSECAT, $id);
27 if ($CFG->forcelogin) {
28 require_login();
31 if (!$category = get_record("course_categories", "id", $id)) {
32 error("Category not known!");
35 if (has_capability('moodle/course:create', $context)) {
36 if ($categoryedit !== -1) {
37 $USER->categoryediting = $categoryedit;
39 $navbaritem = update_category_button($category->id);
40 $creatorediting = !empty($USER->categoryediting);
41 $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $creatorediting);
43 } else {
44 if (!$category->visible) {
45 error(get_string('notavailable', 'error'));
47 $navbaritem = print_course_search("", true, "navbar");
48 $adminediting = false;
49 $creatorediting = false;
53 if (has_capability('moodle/category:update', $context)) {
54 /// Rename the category if requested
55 if (!empty($rename) and confirm_sesskey()) {
56 $category->name = $rename;
57 if (! set_field("course_categories", "name", $category->name, "id", $category->id)) {
58 notify("An error occurred while renaming the category");
62 /// Resort the category if requested
64 if ($resort and confirm_sesskey()) {
65 if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
66 // move it off the range
67 $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
68 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
69 $count = $count->max + 100;
70 begin_sql();
71 foreach ($courses as $course) {
72 set_field('course', 'sortorder', $count, 'id', $course->id);
73 $count++;
75 commit_sql();
76 fix_course_sortorder($category->id);
82 /// Print headings
84 $numcategories = count_records("course_categories");
86 $stradministration = get_string("administration");
87 $strcategories = get_string("categories");
88 $strcategory = get_string("category");
89 $strcourses = get_string("courses");
91 if ($creatorediting) {
92 if ($adminediting) {
93 // modify this to treat this as an admin page
95 require_once($CFG->libdir.'/adminlib.php');
96 $adminroot = admin_get_root();
97 admin_externalpage_setup('coursemgmt', $adminroot);
98 admin_externalpage_print_header($adminroot);
99 } else {
100 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
101 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
103 } else {
104 print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
105 "<a href=\"index.php\">$strcategories</a> -> $category->name", "", "", true, $navbaritem);
108 /// Print button to turn editing off
109 if ($adminediting) {
110 echo '<div class="categoryediting button" align="right">'.update_category_button($category->id).'</div>';
113 /// Print link to roles
115 if (has_capability('moodle/role:assign', $context)) {
116 echo '<div class="rolelink" align="right"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
117 $context->id.'">'.get_string('assignroles','role').'</a></div>';
119 /// Print the category selector
121 $displaylist = array();
122 $parentlist = array();
124 make_categories_list($displaylist, $parentlist, "");
126 echo '<div class="categorypicker">';
127 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
128 echo '</div>';
131 /// Editing functions
133 if ($adminediting) {
135 /// Move a specified course to a new category
137 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
139 if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
140 error("Error finding the category");
144 $courses = array();
145 foreach ( $data as $key => $value ) {
146 if (preg_match('/^c\d+$/', $key)) {
147 array_push($courses, substr($key, 1));
150 move_courses($courses, $data->moveto);
153 /// Hide or show a course
155 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
156 if (!empty($hide)) {
157 $course = get_record("course", "id", $hide);
158 $visible = 0;
159 } else {
160 $course = get_record("course", "id", $show);
161 $visible = 1;
163 if ($course) {
164 if (! set_field("course", "visible", $visible, "id", $course->id)) {
165 notify("Could not update that course!");
171 /// Move a course up or down
173 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
175 $movecourse = NULL;
176 $swapcourse = NULL;
178 // ensure the course order has no gaps
179 // and isn't at 0
180 fix_course_sortorder($category->id);
182 // we are going to need to know the range
183 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
184 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
185 $max = $max->max + 100;
187 if (!empty($moveup)) {
188 $movecourse = get_record('course', 'id', $moveup);
189 $swapcourse = get_record('course',
190 'category', $category->id,
191 'sortorder', $movecourse->sortorder - 1);
192 } else {
193 $movecourse = get_record('course', 'id', $movedown);
194 $swapcourse = get_record('course',
195 'category', $category->id,
196 'sortorder', $movecourse->sortorder + 1);
199 if ($swapcourse and $movecourse) { // Renumber everything for robustness
200 begin_sql();
201 if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id)
202 && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
203 && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
204 )) {
205 notify("Could not update that course!");
207 commit_sql();
212 } // End of editing stuff
214 /// Print out all the sub-categories
215 if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
216 $firstentry = true;
217 foreach ($subcategories as $subcategory) {
218 if ($subcategory->visible or has_capability('moodle/course:create', $context)) {
219 $subcategorieswereshown = true;
220 if ($firstentry) {
221 echo '<table align="center" border="0" cellspacing="2" cellpadding="4" class="generalbox">';
222 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
223 echo '<tr><td nowrap="nowrap">';
224 $firstentry = false;
226 $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
227 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
228 $subcategory->name.'</a><br />';
231 if (!$firstentry) {
232 echo "</td></tr></table>";
233 echo "<br />";
238 /// Print out all the courses
239 unset($course); // To avoid unwanted language effects later
241 $courses = get_courses_page($category->id, 'c.sortorder ASC',
242 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.teacher,c.guest,c.password',
243 $totalcount, $page*$perpage, $perpage);
244 $numcourses = count($courses);
246 if (!$courses) {
247 if (empty($subcategorieswereshown)) {
248 print_heading(get_string("nocoursesyet"));
251 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$creatorediting) {
252 print_courses($category, "80%");
254 } else {
255 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&");
257 $strcourses = get_string("courses");
258 $strselect = get_string("select");
259 $stredit = get_string("edit");
260 $strdelete = get_string("delete");
261 $strbackup = get_string("backup");
262 $strrestore = get_string("restore");
263 $strmoveup = get_string("moveup");
264 $strmovedown = get_string("movedown");
265 $strupdate = get_string("update");
266 $strhide = get_string("hide");
267 $strshow = get_string("show");
268 $strsummary = get_string("summary");
269 $strsettings = get_string("settings");
270 $strassignteachers = get_string("assignteachers");
271 $strallowguests = get_string("allowguests");
272 $strrequireskey = get_string("requireskey");
275 echo '<form id="movecourses" action="category.php" method="post">';
276 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
277 echo '<table align="center" border="0" cellspacing="2" cellpadding="4" class="generalbox"><tr>';
278 echo '<th scope="col">'.$strcourses.'</th>';
279 if ($creatorediting) {
280 echo '<th scope="col">'.$stredit.'</th>';
281 if ($adminediting) {
282 echo '<th scope="col">'.$strselect.'</th>';
284 } else {
285 echo '<th scope="col">&nbsp;</th>';
287 echo '</tr>';
290 $count = 0;
291 $abletomovecourses = false; // for now
293 // Checking if we are at the first or at the last page, to allow courses to
294 // be moved up and down beyond the paging border
295 if ($totalcount > $perpage) {
296 $atfirstpage = ($page == 0);
297 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
298 } else {
299 $atfirstpage = true;
300 $atlastpage = true;
303 foreach ($courses as $acourse) {
305 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
307 $count++;
308 $up = ($count > 1 || !$atfirstpage);
309 $down = ($count < $numcourses || !$atlastpage);
311 $linkcss = $acourse->visible ? "" : ' class="dimmed" ';
312 echo '<tr>';
313 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'.$acourse->fullname.'</a></td>';
314 if ($creatorediting) {
315 echo "<td>";
316 if (has_capability('moodle/course:update', $coursecontext)) {
317 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.
318 $acourse->id.'">'.
319 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> '; }
321 // role assignment link
322 if (has_capability('moodle/role:assign', $coursecontext)) {
323 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>';
326 if (has_capability('moodle/course:delete', $coursecontext)) {
327 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
328 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
331 if (has_capability('moodle/course:visibility', $coursecontext)) {
332 if (!empty($acourse->visible)) {
333 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&amp;page='.$page.
334 '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
335 '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
336 } else {
337 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
338 '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
339 '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
343 if (has_capability('moodle/site:backup', $coursecontext)) {
344 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
345 '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
348 if (has_capability('moodle/site:restore', $coursecontext)) {
349 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
350 '&amp;wdir=/backupdata">'.
351 '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
354 if (has_capability('moodle/category:update', $context)) {
355 if ($up) {
356 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
357 '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
358 '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
359 } else {
360 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
363 if ($down) {
364 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
365 '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
366 '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
367 } else {
368 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
370 $abletomovecourses = true;
373 echo '</td>';
374 echo '<td align="center">';
375 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
376 echo '</td>';
377 } else {
378 echo '<td align="right">';
379 if (!empty($acourse->guest)) {
380 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
381 $strallowguests.'" alt="" class="icon" src="'.
382 $CFG->pixpath.'/i/user.gif" alt="'.$strallowguests.'" /></a>';
384 if (!empty($acourse->password)) {
385 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
386 $strrequireskey.'" alt="" class="icon" src="'.
387 $CFG->pixpath.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
389 if (!empty($acourse->summary)) {
390 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
391 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath.'/i/info.gif" />',
392 400, 500, $strsummary);
394 echo "</td>";
396 echo "</tr>";
399 if ($abletomovecourses) {
400 echo '<tr><td colspan="3" align="right">';
401 echo '<br />';
402 unset($displaylist[$category->id]);
403 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: getElementById('movecourses').submit()");
404 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
405 echo '</td></tr>';
408 echo '</table>';
409 echo '</form>';
410 echo '<br />';
413 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $numcourses > 1) { /// Print button to re-sort courses by name
414 unset($options);
415 $options['id'] = $category->id;
416 $options['resort'] = 'name';
417 $options['sesskey'] = $USER->sesskey;
418 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
421 if (has_capability('moodle/course:create', $context)) { /// Print button to create a new course
422 unset($options);
423 $options['category'] = $category->id;
424 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
425 echo '<br />';
428 if (has_capability('moodle/category:update', $context)) { /// Print form to rename the category
429 $strrename= get_string('rename');
430 echo '<form id="renameform" action="category.php" method="post">';
431 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
432 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
433 echo '<input type="text" size="30" name="rename" value="'.s($category->name).'" alt="'.$strrename.'" />';
434 echo '<input type="submit" value="'.$strrename.'" />';
435 echo "</form>";
436 echo "<br />";
439 print_course_search();
441 if ($adminediting) {
442 admin_externalpage_print_footer($adminroot);
443 } else {
444 print_footer();