Merge branch 'wip-MDL-29086-master' of git://github.com/abgreeve/moodle
[moodle.git] / course / index.php
blobe87312848e292d8123e05f415ef7b858bd28362d
1 <?php
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.
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/>.
18 /**
19 * For most people, just lists the course categories
20 * Allows the admin to create, delete and rename course categories
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
27 require_once("../config.php");
28 require_once("lib.php");
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);
39 $site = get_site();
41 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
43 $PAGE->set_url('/course/index.php');
44 $PAGE->set_context($systemcontext);
45 $PAGE->set_pagelayout('admin');
47 if (can_edit_in_category()) {
48 if ($categoryedit !== -1) {
49 $USER->editing = $categoryedit;
51 require_login();
52 $adminediting = $PAGE->user_is_editing();
53 } else {
54 if ($CFG->forcelogin) {
55 require_login();
57 $adminediting = false;
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');
67 $strfulllistofcourses = get_string('fulllistofcourses');
70 /// Unless it's an editing admin, just print the regular listing of courses/categories
71 if (!$adminediting) {
73 /// Print form for creating new categories
74 $countcategories = $DB->count_records('course_categories');
76 if ($countcategories > 1 || ($countcategories == 1 && $DB->count_records('course') > 200)) {
77 $strcourses = get_string('courses');
78 $strcategories = get_string('categories');
80 $PAGE->navbar->add($strcategories);
81 $PAGE->set_title("$site->shortname: $strcategories");
82 $PAGE->set_heading($COURSE->fullname);
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");
94 $PAGE->set_heading($COURSE->fullname);
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();
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);
108 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
110 print_course_request_buttons($systemcontext);
111 echo $OUTPUT->container_end();
112 echo $OUTPUT->footer();
113 exit;
115 /// Everything else is editing on mode.
116 require_once($CFG->libdir.'/adminlib.php');
117 admin_externalpage_setup('coursemgmt');
119 /// Delete a category.
120 if (!empty($delete) and confirm_sesskey()) {
121 if (!$deletecat = $DB->get_record('course_categories', array('id'=>$delete))) {
122 print_error('invalidcategoryid');
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));
128 $heading = get_string('deletecategory', 'moodle', format_string($deletecat->name, true, array('context' => $context)));
129 require_once('delete_category_form.php');
130 $mform = new delete_category_form(null, $deletecat);
131 $mform->set_data(array('delete'=>$delete));
133 if ($mform->is_cancelled()) {
134 redirect('index.php');
136 } else if (!$data= $mform->get_data()) {
137 require_once($CFG->libdir . '/questionlib.php');
138 echo $OUTPUT->header();
139 echo $OUTPUT->heading($heading);
140 $mform->display();
141 echo $OUTPUT->footer();
142 exit();
145 echo $OUTPUT->header();
146 echo $OUTPUT->heading($heading);
148 if ($data->fulldelete) {
149 $deletedcourses = category_delete_full($deletecat, true);
151 foreach($deletedcourses as $course) {
152 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
154 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($deletecat->name, true, array('context' => $context))), 'notifysuccess');
156 } else {
157 category_delete_move($deletecat, $data->newparent, true);
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)));
165 echo $OUTPUT->continue_button('index.php');
167 echo $OUTPUT->footer();
168 die;
171 /// Create a default category if necessary
172 if (!$categories = get_categories()) { /// No category yet!
173 // Try and make one
174 $tempcat = new stdClass();
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.
182 /// Move a category to a new parent if required
183 if (!empty($move) and ($moveto >= 0) and confirm_sesskey()) {
184 if ($cattomove = $DB->get_record('course_categories', array('id'=>$move))) {
185 require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent));
186 if ($cattomove->parent != $moveto) {
187 $newparent = $DB->get_record('course_categories', array('id'=>$moveto));
188 require_capability('moodle/category:manage', get_category_or_system_context($moveto));
189 move_category($cattomove, $newparent);
194 /// Hide or show a category
195 if ($hide and confirm_sesskey()) {
196 if ($tempcat = $DB->get_record('course_categories', array('id'=>$hide))) {
197 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
198 if ($tempcat->visible == 1) {
199 course_category_hide($tempcat);
202 } else if ($show and confirm_sesskey()) {
203 if ($tempcat = $DB->get_record('course_categories', array('id'=>$show))) {
204 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
205 if ($tempcat->visible == 0) {
206 course_category_show($tempcat);
211 /// Move a category up or down
212 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
213 fix_course_sortorder();
214 $swapcategory = NULL;
216 if (!empty($moveup)) {
217 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveup));
218 if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) {
219 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder DESC', '*', 0, 1)) {
220 $swapcategory = reset($swapcategory);
223 } else {
224 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $movedown));
225 if ($movecategory = $DB->get_record('course_categories', array('id'=>$movedown))) {
226 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) {
227 $swapcategory = reset($swapcategory);
231 if ($swapcategory and $movecategory) {
232 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id'=>$movecategory->id));
233 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id'=>$swapcategory->id));
236 // finally reorder courses
237 fix_course_sortorder();
240 /// Print headings
241 echo $OUTPUT->header();
242 echo $OUTPUT->heading($strcategories);
244 /// Print out the categories with all the knobs
245 $strcategories = get_string('categories');
246 $strcourses = get_string('courses');
247 $strmovecategoryto = get_string('movecategoryto');
248 $stredit = get_string('edit');
250 $displaylist = array();
251 $parentlist = array();
253 $displaylist[0] = get_string('top');
254 make_categories_list($displaylist, $parentlist);
256 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
257 echo '<th class="header" scope="col">'.$strcategories.'</th>';
258 echo '<th class="header" scope="col">'.$strcourses.'</th>';
259 echo '<th class="header" scope="col">'.$stredit.'</th>';
260 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
261 echo '</tr>';
263 print_category_edit(NULL, $displaylist, $parentlist);
264 echo '</table>';
266 echo '<div class="buttons">';
267 if (has_capability('moodle/course:create', $systemcontext)) {
268 // print create course link to first category
269 $options = array('category' => $CFG->defaultrequestcategory);
270 $options['returnto'] = 'topcat';
271 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
274 // Print button for creating new categories
275 if (has_capability('moodle/category:manage', $systemcontext)) {
276 $options = array('parent'=>0);
277 echo $OUTPUT->single_button(new moodle_url('editcategory.php', $options), get_string('addnewcategory'), 'get');
280 print_course_request_buttons($systemcontext);
281 echo '</div>';
283 echo $OUTPUT->footer();
285 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
286 /// Recursive function to print all the categories ready for editing
288 global $CFG, $USER, $OUTPUT;
290 static $str = NULL;
292 if (is_null($str)) {
293 $str = new stdClass;
294 $str->edit = get_string('edit');
295 $str->delete = get_string('delete');
296 $str->moveup = get_string('moveup');
297 $str->movedown = get_string('movedown');
298 $str->edit = get_string('editthiscategory');
299 $str->hide = get_string('hide');
300 $str->show = get_string('show');
301 $str->cohorts = get_string('cohorts', 'cohort');
302 $str->spacer = $OUTPUT->spacer().' ';
305 if (!empty($category)) {
307 if (!isset($category->context)) {
308 $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id);
311 echo '<tr><td align="left" class="name">';
312 for ($i=0; $i<$depth;$i++) {
313 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
315 $linkcss = $category->visible ? '' : ' class="dimmed" ';
316 echo '<a '.$linkcss.' title="'.$str->edit.'" '.
317 ' href="category.php?id='.$category->id.'&amp;categoryedit=on&amp;sesskey='.sesskey().'">'.
318 format_string($category->name, true, array('context' => $category->context)).'</a>';
319 echo '</td>';
321 echo '<td class="count">'.$category->coursecount.'</td>';
323 echo '<td class="icons">'; /// Print little icons
325 if (has_capability('moodle/category:manage', $category->context)) {
326 echo '<a title="'.$str->edit.'" href="editcategory.php?id='.$category->id.'"><img'.
327 ' src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.$str->edit.'" /></a> ';
329 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
330 ' src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.$str->delete.'" /></a> ';
332 if (!empty($category->visible)) {
333 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
334 ' src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$str->hide.'" /></a> ';
335 } else {
336 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
337 ' src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$str->show.'" /></a> ';
340 if (has_capability('moodle/cohort:manage', $category->context) or has_capability('moodle/cohort:view', $category->context)) {
341 echo '<a title="'.$str->cohorts.'" href="'.$CFG->wwwroot.'/cohort/index.php?contextid='.$category->context->id.'"><img'.
342 ' src="'.$OUTPUT->pix_url('i/cohort') . '" class="iconsmall" alt="'.$str->cohorts.'" /></a> ';
345 if ($up) {
346 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
347 ' src="'.$OUTPUT->pix_url('t/up') . '" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
348 } else {
349 echo $str->spacer;
351 if ($down) {
352 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
353 ' src="'.$OUTPUT->pix_url('t/down') . '" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
354 } else {
355 echo $str->spacer;
358 echo '</td>';
360 echo '<td align="left">';
361 if (has_capability('moodle/category:manage', $category->context)) {
362 $tempdisplaylist = $displaylist;
363 unset($tempdisplaylist[$category->id]);
364 foreach ($parentslist as $key => $parents) {
365 if (in_array($category->id, $parents)) {
366 unset($tempdisplaylist[$key]);
369 $popupurl = new moodle_url("index.php?move=$category->id&sesskey=".sesskey());
370 $select = new single_select($popupurl, 'moveto', $tempdisplaylist, $category->parent, null, "moveform$category->id");
371 echo $OUTPUT->render($select);
373 echo '</td>';
374 echo '</tr>';
375 } else {
376 $category = new stdClass();
377 $category->id = '0';
380 if ($categories = get_categories($category->id)) { // Print all the children recursively
381 $countcats = count($categories);
382 $count = 0;
383 $first = true;
384 $last = false;
385 foreach ($categories as $cat) {
386 $count++;
387 if ($count == $countcats) {
388 $last = true;
390 $up = $first ? false : true;
391 $down = $last ? false : true;
392 $first = false;
394 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);