MDL-8857
[moodle-linuxchix.git] / course / category.php
blob09215d0accf285801bc2eb248903123a49a81fdb
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 style="white-space: nowrap">';
224 $firstentry = false;
226 $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
227 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
228 format_string($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_box_start('courseboxes');
253 print_courses($category);
254 print_box_end();
256 } else {
257 print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&amp;perpage=$perpage&");
259 $strcourses = get_string("courses");
260 $strselect = get_string("select");
261 $stredit = get_string("edit");
262 $strdelete = get_string("delete");
263 $strbackup = get_string("backup");
264 $strrestore = get_string("restore");
265 $strmoveup = get_string("moveup");
266 $strmovedown = get_string("movedown");
267 $strupdate = get_string("update");
268 $strhide = get_string("hide");
269 $strshow = get_string("show");
270 $strsummary = get_string("summary");
271 $strsettings = get_string("settings");
272 $strassignteachers = get_string("assignteachers");
273 $strallowguests = get_string("allowguests");
274 $strrequireskey = get_string("requireskey");
277 echo '<form id="movecourses" action="category.php" method="post">';
278 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
279 echo '<table align="center" border="0" cellspacing="2" cellpadding="4" class="generalbox"><tr>';
280 echo '<th scope="col">'.$strcourses.'</th>';
281 if ($creatorediting) {
282 echo '<th scope="col">'.$stredit.'</th>';
283 if ($adminediting) {
284 echo '<th scope="col">'.$strselect.'</th>';
286 } else {
287 echo '<th scope="col">&nbsp;</th>';
289 echo '</tr>';
292 $count = 0;
293 $abletomovecourses = false; // for now
295 // Checking if we are at the first or at the last page, to allow courses to
296 // be moved up and down beyond the paging border
297 if ($totalcount > $perpage) {
298 $atfirstpage = ($page == 0);
299 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
300 } else {
301 $atfirstpage = true;
302 $atlastpage = true;
305 foreach ($courses as $acourse) {
307 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
309 $count++;
310 $up = ($count > 1 || !$atfirstpage);
311 $down = ($count < $numcourses || !$atlastpage);
313 $linkcss = $acourse->visible ? "" : ' class="dimmed" ';
314 echo '<tr>';
315 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($acourse->fullname) .'</a></td>';
316 if ($creatorediting) {
317 echo "<td>";
318 if (has_capability('moodle/course:update', $coursecontext)) {
319 echo '<a title="'.$strsettings.'" href="'.$CFG->wwwroot.'/course/edit.php?id='.
320 $acourse->id.'">'.
321 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$stredit.'" /></a> '; }
323 // role assignment link
324 if (has_capability('moodle/role:assign', $coursecontext)) {
325 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>';
328 if (can_delete_course($acourse->id)) {
329 echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
330 '<img src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$strdelete.'" /></a> ';
333 if (has_capability('moodle/course:visibility', $coursecontext)) {
334 if (!empty($acourse->visible)) {
335 echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&amp;page='.$page.
336 '&amp;perpage='.$perpage.'&amp;hide='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
337 '<img src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$strhide.'" /></a> ';
338 } else {
339 echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&amp;page='.$page.
340 '&amp;perpage='.$perpage.'&amp;show='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
341 '<img src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$strshow.'" /></a> ';
345 if (has_capability('moodle/site:backup', $coursecontext)) {
346 echo '<a title="'.$strbackup.'" href="../backup/backup.php?id='.$acourse->id.'">'.
347 '<img src="'.$CFG->pixpath.'/t/backup.gif" class="iconsmall" alt="'.$strbackup.'" /></a> ';
350 if (has_capability('moodle/site:restore', $coursecontext)) {
351 echo '<a title="'.$strrestore.'" href="../files/index.php?id='.$acourse->id.
352 '&amp;wdir=/backupdata">'.
353 '<img src="'.$CFG->pixpath.'/t/restore.gif" class="iconsmall" alt="'.$strrestore.'" /></a> ';
356 if (has_capability('moodle/category:update', $context)) {
357 if ($up) {
358 echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&amp;page='.$page.
359 '&amp;perpage='.$perpage.'&amp;moveup='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
360 '<img src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$strmoveup.'" /></a> ';
361 } else {
362 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
365 if ($down) {
366 echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&amp;page='.$page.
367 '&amp;perpage='.$perpage.'&amp;movedown='.$acourse->id.'&amp;sesskey='.$USER->sesskey.'">'.
368 '<img src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$strmovedown.'" /></a> ';
369 } else {
370 echo '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
372 $abletomovecourses = true;
375 echo '</td>';
376 echo '<td align="center">';
377 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
378 echo '</td>';
379 } else {
380 echo '<td align="right">';
381 if (!empty($acourse->guest)) {
382 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
383 $strallowguests.'" class="icon" src="'.
384 $CFG->pixpath.'/i/user.gif" alt="'.$strallowguests.'" /></a>';
386 if (!empty($acourse->password)) {
387 echo '<a href="view.php?id='.$acourse->id.'"><img title="'.
388 $strrequireskey.'" class="icon" src="'.
389 $CFG->pixpath.'/i/key.gif" alt="'.$strrequireskey.'" /></a>';
391 if (!empty($acourse->summary)) {
392 link_to_popup_window ("/course/info.php?id=$acourse->id", "courseinfo",
393 '<img alt="'.get_string('info').'" class="icon" src="'.$CFG->pixpath.'/i/info.gif" />',
394 400, 500, $strsummary);
396 echo "</td>";
398 echo "</tr>";
401 if ($abletomovecourses) {
402 echo '<tr><td colspan="3" align="right">';
403 echo '<br />';
404 unset($displaylist[$category->id]);
405 choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript: getElementById('movecourses').submit()");
406 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
407 echo '</td></tr>';
410 echo '</table>';
411 echo '</form>';
412 echo '<br />';
415 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $numcourses > 1) { /// Print button to re-sort courses by name
416 unset($options);
417 $options['id'] = $category->id;
418 $options['resort'] = 'name';
419 $options['sesskey'] = $USER->sesskey;
420 print_single_button('category.php', $options, get_string('resortcoursesbyname'), 'get');
423 if (has_capability('moodle/course:create', $context)) { /// Print button to create a new course
424 unset($options);
425 $options['category'] = $category->id;
426 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
427 echo '<br />';
430 if (has_capability('moodle/category:update', $context)) { /// Print form to rename the category
431 $strrename= get_string('rename');
432 echo '<form id="renameform" action="category.php" method="post">';
433 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
434 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
435 echo '<input type="text" size="30" name="rename" value="'.format_string($category->name).'" alt="'.$strrename.'" />';
436 echo '<input type="submit" value="'.$strrename.'" />';
437 echo "</form>";
438 echo "<br />";
441 print_course_search();
443 if ($adminediting) {
444 admin_externalpage_print_footer($adminroot);
445 } else {
446 print_footer();