MDL-36576 - change generalbox classname to generaltable to fix rounded corners on...
[moodle.git] / course / index.php
CommitLineData
d9cb06dc 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * For most people, just lists the course categories
20 * Allows the admin to create, delete and rename course categories
21 *
22 * @copyright 1999 Martin Dougiamas http://dougiamas.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @package course
25 */
26
27require_once("../config.php");
28require_once("lib.php");
29
30$categoryedit = optional_param('categoryedit', -1,PARAM_BOOL);
31$delete = optional_param('delete',0,PARAM_INT);
32$hide = optional_param('hide',0,PARAM_INT);
33$show = optional_param('show',0,PARAM_INT);
34$move = optional_param('move',0,PARAM_INT);
35$moveto = optional_param('moveto',-1,PARAM_INT);
36$moveup = optional_param('moveup',0,PARAM_INT);
37$movedown = optional_param('movedown',0,PARAM_INT);
38
3f77c158 39$site = get_site();
d2b6ba70 40
d9cb06dc 41$systemcontext = get_context_instance(CONTEXT_SYSTEM);
29aab4ac 42
a6855934 43$PAGE->set_url('/course/index.php');
d9cb06dc 44$PAGE->set_context($systemcontext);
c07dfe70 45$PAGE->set_pagelayout('admin');
f474a4e5 46
d9cb06dc 47if (can_edit_in_category()) {
48 if ($categoryedit !== -1) {
49 $USER->editing = $categoryedit;
50 }
51 require_login();
52 $adminediting = $PAGE->user_is_editing();
53} else {
54 if ($CFG->forcelogin) {
ebff4779 55 require_login();
d2b6ba70 56 }
d9cb06dc 57 $adminediting = false;
58}
d2b6ba70 59
d9cb06dc 60$stradministration = get_string('administration');
61$strcategories = get_string('categories');
62$strcategory = get_string('category');
63$strcourses = get_string('courses');
64$stredit = get_string('edit');
65$strdelete = get_string('delete');
66$straction = get_string('action');
2ab47386 67$strfulllistofcourses = get_string('fulllistofcourses');
fb31c40a 68
69
d2b6ba70 70/// Unless it's an editing admin, just print the regular listing of courses/categories
d9cb06dc 71if (!$adminediting) {
72
73/// Print form for creating new categories
74 $countcategories = $DB->count_records('course_categories');
75
76 if ($countcategories > 1 || ($countcategories == 1 && $DB->count_records('course') > 200)) {
77 $strcourses = get_string('courses');
78 $strcategories = get_string('categories');
79
80 $PAGE->navbar->add($strcategories);
81 $PAGE->set_title("$site->shortname: $strcategories");
fb66c52e 82 $PAGE->set_heading($COURSE->fullname);
d9cb06dc 83 $PAGE->set_button(update_category_button());
84 echo $OUTPUT->header();
85 echo $OUTPUT->heading($strcategories);
86 echo $OUTPUT->skip_link_target();
87 echo $OUTPUT->box_start('categorybox');
88 print_whole_category_list();
89 echo $OUTPUT->box_end();
90 print_course_search();
91 } else {
92 $PAGE->navbar->add($strfulllistofcourses);
93 $PAGE->set_title("$site->shortname: $strfulllistofcourses");
fb66c52e 94 $PAGE->set_heading($COURSE->fullname);
d9cb06dc 95 $PAGE->set_button(update_category_button());
96 echo $OUTPUT->header();
97 echo $OUTPUT->skip_link_target();
98 echo $OUTPUT->box_start('courseboxes');
99 print_courses(0);
100 echo $OUTPUT->box_end();
101 }
c432fd32 102
d9cb06dc 103 echo $OUTPUT->container_start('buttons');
104 if (has_capability('moodle/course:create', $systemcontext)) {
105 /// Print link to create a new course
106 /// Get the 1st available category
107 $options = array('category' => $CFG->defaultrequestcategory);
5c2ed7e2 108 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
d2b6ba70 109 }
d9cb06dc 110 print_course_request_buttons($systemcontext);
111 echo $OUTPUT->container_end();
112 echo $OUTPUT->footer();
113 exit;
114}
8ed5dd63 115/// Everything else is editing on mode.
d9cb06dc 116require_once($CFG->libdir.'/adminlib.php');
117admin_externalpage_setup('coursemgmt');
d2b6ba70 118
8ed5dd63 119/// Delete a category.
d9cb06dc 120if (!empty($delete) and confirm_sesskey()) {
121 if (!$deletecat = $DB->get_record('course_categories', array('id'=>$delete))) {
122 print_error('invalidcategoryid');
123 }
124 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
125 require_capability('moodle/category:manage', $context);
126 require_capability('moodle/category:manage', get_category_or_system_context($deletecat->parent));
127
63390481 128 $heading = get_string('deletecategory', 'moodle', format_string($deletecat->name, true, array('context' => $context)));
d9cb06dc 129 require_once('delete_category_form.php');
130 $mform = new delete_category_form(null, $deletecat);
131 $mform->set_data(array('delete'=>$delete));
132
133 if ($mform->is_cancelled()) {
134 redirect('index.php');
e2b347e9 135
d9cb06dc 136 } else if (!$data= $mform->get_data()) {
137 require_once($CFG->libdir . '/questionlib.php');
61ef8f9f 138 echo $OUTPUT->header();
7c5286cd 139 echo $OUTPUT->heading($heading);
d9cb06dc 140 $mform->display();
141 echo $OUTPUT->footer();
142 exit();
143 }
e2b347e9 144
61ef8f9f 145 echo $OUTPUT->header();
d9cb06dc 146 echo $OUTPUT->heading($heading);
98b369f8 147
d9cb06dc 148 if ($data->fulldelete) {
149 $deletedcourses = category_delete_full($deletecat, true);
98b369f8 150
d9cb06dc 151 foreach($deletedcourses as $course) {
152 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
e2b347e9 153 }
63390481 154 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($deletecat->name, true, array('context' => $context))), 'notifysuccess');
e2b347e9 155
d9cb06dc 156 } else {
157 category_delete_move($deletecat, $data->newparent, true);
158 }
e2b347e9 159
d9cb06dc 160 // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
161 if ($delete == $CFG->defaultrequestcategory) {
162 set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent'=>0)));
ba2e5d73 163 }
0a263205 164
d9cb06dc 165 echo $OUTPUT->continue_button('index.php');
166
167 echo $OUTPUT->footer();
168 die;
169}
170
d2b6ba70 171/// Create a default category if necessary
d9cb06dc 172if (!$categories = get_categories()) { /// No category yet!
173 // Try and make one
fbaea88f 174 $tempcat = new stdClass();
d9cb06dc 175 $tempcat->name = get_string('miscellaneous');
176 $tempcat->id = $DB->insert_record('course_categories', $tempcat);
177 $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id);
178 mark_context_dirty('/'.SYSCONTEXTID);
179 fix_course_sortorder(); // Required to build course_categories.depth and .path.
950a11e8 180 set_config('defaultrequestcategory', $tempcat->id);
d9cb06dc 181}
d2b6ba70 182
d2b6ba70 183/// Move a category to a new parent if required
d9cb06dc 184if (!empty($move) and ($moveto >= 0) and confirm_sesskey()) {
185 if ($cattomove = $DB->get_record('course_categories', array('id'=>$move))) {
186 require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent));
187 if ($cattomove->parent != $moveto) {
188 $newparent = $DB->get_record('course_categories', array('id'=>$moveto));
189 require_capability('moodle/category:manage', get_category_or_system_context($moveto));
190 move_category($cattomove, $newparent);
d2b6ba70 191 }
192 }
d9cb06dc 193}
d2b6ba70 194
0be6f678 195/// Hide or show a category
df997f84
PS
196if ($hide and confirm_sesskey()) {
197 if ($tempcat = $DB->get_record('course_categories', array('id'=>$hide))) {
198 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
199 if ($tempcat->visible == 1) {
200 course_category_hide($tempcat);
201 }
d9cb06dc 202 }
df997f84
PS
203} else if ($show and confirm_sesskey()) {
204 if ($tempcat = $DB->get_record('course_categories', array('id'=>$show))) {
205 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
206 if ($tempcat->visible == 0) {
207 course_category_show($tempcat);
208 }
d2b6ba70 209 }
d9cb06dc 210}
d2b6ba70 211
d2b6ba70 212/// Move a category up or down
d9cb06dc 213if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
214 fix_course_sortorder();
215 $swapcategory = NULL;
216
217 if (!empty($moveup)) {
218 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveup));
219 if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) {
e818c6fc 220 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder DESC', '*', 0, 1)) {
d9cb06dc 221 $swapcategory = reset($swapcategory);
d2b6ba70 222 }
223 }
d9cb06dc 224 } else {
225 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $movedown));
226 if ($movecategory = $DB->get_record('course_categories', array('id'=>$movedown))) {
227 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) {
228 $swapcategory = reset($swapcategory);
229 }
d2b6ba70 230 }
d9cb06dc 231 }
232 if ($swapcategory and $movecategory) {
233 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id'=>$movecategory->id));
234 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id'=>$swapcategory->id));
0cbe8111 235 }
0be6f678 236
d9cb06dc 237 // finally reorder courses
238 fix_course_sortorder();
239}
240
1fb3d044 241/// Print headings
61ef8f9f 242echo $OUTPUT->header();
d9cb06dc 243echo $OUTPUT->heading($strcategories);
1fb3d044 244
d2b6ba70 245/// Print out the categories with all the knobs
d9cb06dc 246$strcategories = get_string('categories');
247$strcourses = get_string('courses');
248$strmovecategoryto = get_string('movecategoryto');
249$stredit = get_string('edit');
250
251$displaylist = array();
252$parentlist = array();
253
254$displaylist[0] = get_string('top');
255make_categories_list($displaylist, $parentlist);
256
02e5529b 257echo '<table class="generaltable editcourse boxaligncenter"><tr class="header">';
d9cb06dc 258echo '<th class="header" scope="col">'.$strcategories.'</th>';
259echo '<th class="header" scope="col">'.$strcourses.'</th>';
260echo '<th class="header" scope="col">'.$stredit.'</th>';
261echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
262echo '</tr>';
263
264print_category_edit(NULL, $displaylist, $parentlist);
265echo '</table>';
266
267echo '<div class="buttons">';
268if (has_capability('moodle/course:create', $systemcontext)) {
269 // print create course link to first category
d9cb06dc 270 $options = array('category' => $CFG->defaultrequestcategory);
7feebf6f 271 $options['returnto'] = 'topcat';
5c2ed7e2 272 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
d9cb06dc 273}
0be6f678 274
d9cb06dc 275// Print button for creating new categories
276if (has_capability('moodle/category:manage', $systemcontext)) {
5c2ed7e2
PS
277 $options = array('parent'=>0);
278 echo $OUTPUT->single_button(new moodle_url('editcategory.php', $options), get_string('addnewcategory'), 'get');
d9cb06dc 279}
86830be6 280
d9cb06dc 281print_course_request_buttons($systemcontext);
282echo '</div>';
571880d2 283
d9cb06dc 284echo $OUTPUT->footer();
d2b6ba70 285
286function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
287/// Recursive function to print all the categories ready for editing
288
6b608f8f 289 global $CFG, $USER, $OUTPUT;
d2b6ba70 290
8ed5dd63 291 static $str = NULL;
0be6f678 292
8ed5dd63 293 if (is_null($str)) {
294 $str = new stdClass;
86830be6 295 $str->edit = get_string('edit');
378ef1fe 296 $str->delete = get_string('delete');
297 $str->moveup = get_string('moveup');
298 $str->movedown = get_string('movedown');
299 $str->edit = get_string('editthiscategory');
300 $str->hide = get_string('hide');
301 $str->show = get_string('show');
864e2805 302 $str->cohorts = get_string('cohorts', 'cohort');
f8e9c93a 303 $str->spacer = $OUTPUT->spacer().' ';
d2b6ba70 304 }
0be6f678 305
8ed5dd63 306 if (!empty($category)) {
378ef1fe 307
dfbf98cd 308 if (!isset($category->context)) {
309 $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id);
310 }
0be6f678 311
378ef1fe 312 echo '<tr><td align="left" class="name">';
d2b6ba70 313 for ($i=0; $i<$depth;$i++) {
378ef1fe 314 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
d2b6ba70 315 }
378ef1fe 316 $linkcss = $category->visible ? '' : ' class="dimmed" ';
317 echo '<a '.$linkcss.' title="'.$str->edit.'" '.
318 ' href="category.php?id='.$category->id.'&amp;categoryedit=on&amp;sesskey='.sesskey().'">'.
63390481 319 format_string($category->name, true, array('context' => $category->context)).'</a>';
378ef1fe 320 echo '</td>';
d2b6ba70 321
378ef1fe 322 echo '<td class="count">'.$category->coursecount.'</td>';
d2b6ba70 323
378ef1fe 324 echo '<td class="icons">'; /// Print little icons
d2b6ba70 325
8ed5dd63 326 if (has_capability('moodle/category:manage', $category->context)) {
327 echo '<a title="'.$str->edit.'" href="editcategory.php?id='.$category->id.'"><img'.
b5d0cafc 328 ' src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.$str->edit.'" /></a> ';
86830be6 329
378ef1fe 330 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 331 ' src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.$str->delete.'" /></a> ';
0be6f678 332
61240489 333 if (!empty($category->visible)) {
378ef1fe 334 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 335 ' src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$str->hide.'" /></a> ';
61240489 336 } else {
378ef1fe 337 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 338 ' src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$str->show.'" /></a> ';
61240489 339 }
864e2805
PS
340
341 if (has_capability('moodle/cohort:manage', $category->context) or has_capability('moodle/cohort:view', $category->context)) {
342 echo '<a title="'.$str->cohorts.'" href="'.$CFG->wwwroot.'/cohort/index.php?contextid='.$category->context->id.'"><img'.
343 ' src="'.$OUTPUT->pix_url('i/cohort') . '" class="iconsmall" alt="'.$str->cohorts.'" /></a> ';
344 }
d2b6ba70 345
8ed5dd63 346 if ($up) {
347 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 348 ' src="'.$OUTPUT->pix_url('t/up') . '" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
8ed5dd63 349 } else {
350 echo $str->spacer;
351 }
352 if ($down) {
353 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 354 ' src="'.$OUTPUT->pix_url('t/down') . '" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
8ed5dd63 355 } else {
356 echo $str->spacer;
357 }
d2b6ba70 358 }
378ef1fe 359 echo '</td>';
d2b6ba70 360
378ef1fe 361 echo '<td align="left">';
8ed5dd63 362 if (has_capability('moodle/category:manage', $category->context)) {
363 $tempdisplaylist = $displaylist;
364 unset($tempdisplaylist[$category->id]);
365 foreach ($parentslist as $key => $parents) {
366 if (in_array($category->id, $parents)) {
367 unset($tempdisplaylist[$key]);
368 }
d2b6ba70 369 }
f8dab966
PS
370 $popupurl = new moodle_url("index.php?move=$category->id&sesskey=".sesskey());
371 $select = new single_select($popupurl, 'moveto', $tempdisplaylist, $category->parent, null, "moveform$category->id");
0fa95a3c 372 $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
f8dab966 373 echo $OUTPUT->render($select);
d2b6ba70 374 }
378ef1fe 375 echo '</td>';
376 echo '</tr>';
c2cb4545 377 } else {
b85b25eb 378 $category = new stdClass();
378ef1fe 379 $category->id = '0';
c2cb4545 380 }
f9903ed0 381
d2b6ba70 382 if ($categories = get_categories($category->id)) { // Print all the children recursively
383 $countcats = count($categories);
384 $count = 0;
385 $first = true;
386 $last = false;
387 foreach ($categories as $cat) {
388 $count++;
389 if ($count == $countcats) {
390 $last = true;
391 }
392 $up = $first ? false : true;
393 $down = $last ? false : true;
394 $first = false;
f9903ed0 395
0be6f678 396 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);
d2b6ba70 397 }
398 }
950a11e8 399}