MDL-36983 fix incorrect test method name
[moodle.git] / course / index.php
bloba1a5e56995759a0717163a025653be67ec16c109
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 = context_system::instance();
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 = context_coursecat::instance($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 = context_coursecat::instance($tempcat->id);
178 mark_context_dirty('/'.SYSCONTEXTID);
179 fix_course_sortorder(); // Required to build course_categories.depth and .path.
180 set_config('defaultrequestcategory', $tempcat->id);
183 /// Move a category to a new parent if required
184 if (!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);
195 /// Hide or show a category
196 if ($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);
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);
212 /// Move a category up or down
213 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
214 fix_course_sortorder();
215 $swapcategory = NULL;
217 if (!empty($moveup)) {
218 require_capability('moodle/category:manage', context_coursecat::instance($moveup));
219 if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) {
220 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder DESC', '*', 0, 1)) {
221 $swapcategory = reset($swapcategory);
224 } else {
225 require_capability('moodle/category:manage', context_coursecat::instance($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);
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));
237 // finally reorder courses
238 fix_course_sortorder();
241 /// Print headings
242 echo $OUTPUT->header();
243 echo $OUTPUT->heading($strcategories);
245 /// Print out the categories with all the knobs
246 $strcategories = get_string('categories');
247 $strcourses = get_string('courses');
248 $strmovecategoryto = get_string('movecategoryto');
249 $stredit = get_string('edit');
251 $displaylist = array();
252 $parentlist = array();
254 $displaylist[0] = get_string('top');
255 make_categories_list($displaylist, $parentlist);
257 echo '<table class="generaltable editcourse boxaligncenter"><tr class="header">';
258 echo '<th class="header" scope="col">'.$strcategories.'</th>';
259 echo '<th class="header" scope="col">'.$strcourses.'</th>';
260 echo '<th class="header" scope="col">'.$stredit.'</th>';
261 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
262 echo '</tr>';
264 print_category_edit(NULL, $displaylist, $parentlist);
265 echo '</table>';
267 echo '<div class="buttons">';
268 if (has_capability('moodle/course:create', $systemcontext)) {
269 // print create course link to first category
270 $options = array('category' => $CFG->defaultrequestcategory);
271 $options['returnto'] = 'topcat';
272 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
275 // Print button for creating new categories
276 if (has_capability('moodle/category:manage', $systemcontext)) {
277 $options = array('parent'=>0);
278 echo $OUTPUT->single_button(new moodle_url('editcategory.php', $options), get_string('addnewcategory'), 'get');
281 print_course_request_buttons($systemcontext);
282 echo '</div>';
284 echo $OUTPUT->footer();
286 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
287 /// Recursive function to print all the categories ready for editing
289 global $CFG, $USER, $OUTPUT;
291 static $str = NULL;
293 if (is_null($str)) {
294 $str = new stdClass;
295 $str->edit = get_string('edit');
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');
302 $str->cohorts = get_string('cohorts', 'cohort');
303 $str->spacer = $OUTPUT->spacer().' ';
306 if (!empty($category)) {
308 if (!isset($category->context)) {
309 $category->context = context_coursecat::instance($category->id);
312 echo '<tr><td align="left" class="name">';
313 for ($i=0; $i<$depth;$i++) {
314 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
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().'">'.
319 format_string($category->name, true, array('context' => $category->context)).'</a>';
320 echo '</td>';
322 echo '<td class="count">'.$category->coursecount.'</td>';
324 echo '<td class="icons">'; /// Print little icons
326 if (has_capability('moodle/category:manage', $category->context)) {
327 echo '<a title="'.$str->edit.'" href="editcategory.php?id='.$category->id.'"><img'.
328 ' src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.$str->edit.'" /></a> ';
330 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
331 ' src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.$str->delete.'" /></a> ';
333 if (!empty($category->visible)) {
334 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
335 ' src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$str->hide.'" /></a> ';
336 } else {
337 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
338 ' src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$str->show.'" /></a> ';
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('t/cohort') . '" class="iconsmall" alt="'.$str->cohorts.'" /></a> ';
346 if ($up) {
347 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
348 ' src="'.$OUTPUT->pix_url('t/up') . '" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
349 } else {
350 echo $str->spacer;
352 if ($down) {
353 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
354 ' src="'.$OUTPUT->pix_url('t/down') . '" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
355 } else {
356 echo $str->spacer;
359 echo '</td>';
361 echo '<td align="left">';
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]);
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");
372 $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
373 echo $OUTPUT->render($select);
375 echo '</td>';
376 echo '</tr>';
377 } else {
378 $category = new stdClass();
379 $category->id = '0';
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;
392 $up = $first ? false : true;
393 $down = $last ? false : true;
394 $first = false;
396 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);