new string_manager identified coding problem - wrong get_string() params again -...
[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);
f474a4e5 45
d9cb06dc 46if (can_edit_in_category()) {
47 if ($categoryedit !== -1) {
48 $USER->editing = $categoryedit;
49 }
50 require_login();
51 $adminediting = $PAGE->user_is_editing();
52} else {
53 if ($CFG->forcelogin) {
ebff4779 54 require_login();
d2b6ba70 55 }
d9cb06dc 56 $adminediting = false;
57}
d2b6ba70 58
d9cb06dc 59$stradministration = get_string('administration');
60$strcategories = get_string('categories');
61$strcategory = get_string('category');
62$strcourses = get_string('courses');
63$stredit = get_string('edit');
64$strdelete = get_string('delete');
65$straction = get_string('action');
2ab47386 66$strfulllistofcourses = get_string('fulllistofcourses');
fb31c40a 67
68
d2b6ba70 69/// Unless it's an editing admin, just print the regular listing of courses/categories
d9cb06dc 70if (!$adminediting) {
71
72/// Print form for creating new categories
73 $countcategories = $DB->count_records('course_categories');
74
75 if ($countcategories > 1 || ($countcategories == 1 && $DB->count_records('course') > 200)) {
76 $strcourses = get_string('courses');
77 $strcategories = get_string('categories');
78
79 $PAGE->navbar->add($strcategories);
80 $PAGE->set_title("$site->shortname: $strcategories");
81 $PAGE->set_heading($strcourses);
82 $PAGE->set_button(update_category_button());
83 echo $OUTPUT->header();
84 echo $OUTPUT->heading($strcategories);
85 echo $OUTPUT->skip_link_target();
86 echo $OUTPUT->box_start('categorybox');
87 print_whole_category_list();
88 echo $OUTPUT->box_end();
89 print_course_search();
90 } else {
91 $PAGE->navbar->add($strfulllistofcourses);
92 $PAGE->set_title("$site->shortname: $strfulllistofcourses");
93 $PAGE->set_heading($strfulllistofcourses);
94 $PAGE->set_button(update_category_button());
95 echo $OUTPUT->header();
96 echo $OUTPUT->skip_link_target();
97 echo $OUTPUT->box_start('courseboxes');
98 print_courses(0);
99 echo $OUTPUT->box_end();
100 }
c432fd32 101
d9cb06dc 102 echo $OUTPUT->container_start('buttons');
103 if (has_capability('moodle/course:create', $systemcontext)) {
104 /// Print link to create a new course
105 /// Get the 1st available category
106 $options = array('category' => $CFG->defaultrequestcategory);
5c2ed7e2 107 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
d2b6ba70 108 }
d9cb06dc 109 print_course_request_buttons($systemcontext);
110 echo $OUTPUT->container_end();
111 echo $OUTPUT->footer();
112 exit;
113}
8ed5dd63 114/// Everything else is editing on mode.
d9cb06dc 115require_once($CFG->libdir.'/adminlib.php');
116admin_externalpage_setup('coursemgmt');
d2b6ba70 117
8ed5dd63 118/// Delete a category.
d9cb06dc 119if (!empty($delete) and confirm_sesskey()) {
120 if (!$deletecat = $DB->get_record('course_categories', array('id'=>$delete))) {
121 print_error('invalidcategoryid');
122 }
123 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
124 require_capability('moodle/category:manage', $context);
125 require_capability('moodle/category:manage', get_category_or_system_context($deletecat->parent));
126
97194123 127 $heading = get_string('deletecategory', 'moodle', format_string($deletecat->name));
d9cb06dc 128 require_once('delete_category_form.php');
129 $mform = new delete_category_form(null, $deletecat);
130 $mform->set_data(array('delete'=>$delete));
131
132 if ($mform->is_cancelled()) {
133 redirect('index.php');
e2b347e9 134
d9cb06dc 135 } else if (!$data= $mform->get_data()) {
136 require_once($CFG->libdir . '/questionlib.php');
61ef8f9f 137 echo $OUTPUT->header();
7c5286cd 138 echo $OUTPUT->heading($heading);
d9cb06dc 139 $mform->display();
140 echo $OUTPUT->footer();
141 exit();
142 }
e2b347e9 143
61ef8f9f 144 echo $OUTPUT->header();
d9cb06dc 145 echo $OUTPUT->heading($heading);
98b369f8 146
d9cb06dc 147 if ($data->fulldelete) {
148 $deletedcourses = category_delete_full($deletecat, true);
98b369f8 149
d9cb06dc 150 foreach($deletedcourses as $course) {
151 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
e2b347e9 152 }
d9cb06dc 153 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($deletecat->name)), 'notifysuccess');
e2b347e9 154
d9cb06dc 155 } else {
156 category_delete_move($deletecat, $data->newparent, true);
157 }
e2b347e9 158
d9cb06dc 159 // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
160 if ($delete == $CFG->defaultrequestcategory) {
161 set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent'=>0)));
ba2e5d73 162 }
0a263205 163
d9cb06dc 164 echo $OUTPUT->continue_button('index.php');
165
166 echo $OUTPUT->footer();
167 die;
168}
169
d2b6ba70 170/// Create a default category if necessary
d9cb06dc 171if (!$categories = get_categories()) { /// No category yet!
172 // Try and make one
173 $tempcat = new object();
174 $tempcat->name = get_string('miscellaneous');
175 $tempcat->id = $DB->insert_record('course_categories', $tempcat);
176 $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id);
177 mark_context_dirty('/'.SYSCONTEXTID);
178 fix_course_sortorder(); // Required to build course_categories.depth and .path.
179}
d2b6ba70 180
d2b6ba70 181/// Move a category to a new parent if required
d9cb06dc 182if (!empty($move) and ($moveto >= 0) and confirm_sesskey()) {
183 if ($cattomove = $DB->get_record('course_categories', array('id'=>$move))) {
184 require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent));
185 if ($cattomove->parent != $moveto) {
186 $newparent = $DB->get_record('course_categories', array('id'=>$moveto));
187 require_capability('moodle/category:manage', get_category_or_system_context($moveto));
188 move_category($cattomove, $newparent);
d2b6ba70 189 }
190 }
d9cb06dc 191}
d2b6ba70 192
0be6f678 193/// Hide or show a category
d9cb06dc 194if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
195 if (!empty($hide)) {
196 $tempcat = $DB->get_record('course_categories', array('id'=>$hide));
197 $visible = 0;
198 } else {
199 $tempcat = $DB->get_record('course_categories', array('id'=>$show));
200 $visible = 1;
201 }
202 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
203 if ($tempcat) {
204 $DB->set_field('course_categories', 'visible', $visible, array('id'=>$tempcat->id));
205 $DB->set_field('course', 'visible', $visible, array('category' => $tempcat->id));
d2b6ba70 206 }
d9cb06dc 207}
d2b6ba70 208
d2b6ba70 209/// Move a category up or down
d9cb06dc 210if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
211 fix_course_sortorder();
212 $swapcategory = NULL;
213
214 if (!empty($moveup)) {
215 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveup));
216 if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) {
217 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) {
218 $swapcategory = reset($swapcategory);
d2b6ba70 219 }
220 }
d9cb06dc 221 } else {
222 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $movedown));
223 if ($movecategory = $DB->get_record('course_categories', array('id'=>$movedown))) {
224 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) {
225 $swapcategory = reset($swapcategory);
226 }
d2b6ba70 227 }
d9cb06dc 228 }
229 if ($swapcategory and $movecategory) {
230 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id'=>$movecategory->id));
231 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id'=>$swapcategory->id));
0cbe8111 232 }
0be6f678 233
d9cb06dc 234 // finally reorder courses
235 fix_course_sortorder();
236}
237
1fb3d044 238/// Print headings
61ef8f9f 239echo $OUTPUT->header();
d9cb06dc 240echo $OUTPUT->heading($strcategories);
1fb3d044 241
d2b6ba70 242/// Print out the categories with all the knobs
d9cb06dc 243$strcategories = get_string('categories');
244$strcourses = get_string('courses');
245$strmovecategoryto = get_string('movecategoryto');
246$stredit = get_string('edit');
247
248$displaylist = array();
249$parentlist = array();
250
251$displaylist[0] = get_string('top');
252make_categories_list($displaylist, $parentlist);
253
254echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
255echo '<th class="header" scope="col">'.$strcategories.'</th>';
256echo '<th class="header" scope="col">'.$strcourses.'</th>';
257echo '<th class="header" scope="col">'.$stredit.'</th>';
258echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
259echo '</tr>';
260
261print_category_edit(NULL, $displaylist, $parentlist);
262echo '</table>';
263
264echo '<div class="buttons">';
265if (has_capability('moodle/course:create', $systemcontext)) {
266 // print create course link to first category
d9cb06dc 267 $options = array('category' => $CFG->defaultrequestcategory);
5c2ed7e2 268 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
d9cb06dc 269}
0be6f678 270
d9cb06dc 271// Print button for creating new categories
272if (has_capability('moodle/category:manage', $systemcontext)) {
5c2ed7e2
PS
273 $options = array('parent'=>0);
274 echo $OUTPUT->single_button(new moodle_url('editcategory.php', $options), get_string('addnewcategory'), 'get');
d9cb06dc 275}
86830be6 276
d9cb06dc 277print_course_request_buttons($systemcontext);
278echo '</div>';
571880d2 279
d9cb06dc 280echo $OUTPUT->footer();
d2b6ba70 281
282function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
283/// Recursive function to print all the categories ready for editing
284
6b608f8f 285 global $CFG, $USER, $OUTPUT;
d2b6ba70 286
8ed5dd63 287 static $str = NULL;
0be6f678 288
8ed5dd63 289 if (is_null($str)) {
290 $str = new stdClass;
86830be6 291 $str->edit = get_string('edit');
378ef1fe 292 $str->delete = get_string('delete');
293 $str->moveup = get_string('moveup');
294 $str->movedown = get_string('movedown');
295 $str->edit = get_string('editthiscategory');
296 $str->hide = get_string('hide');
297 $str->show = get_string('show');
8ed5dd63 298 $str->spacer = '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
d2b6ba70 299 }
0be6f678 300
8ed5dd63 301 if (!empty($category)) {
378ef1fe 302
dfbf98cd 303 if (!isset($category->context)) {
304 $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id);
305 }
0be6f678 306
378ef1fe 307 echo '<tr><td align="left" class="name">';
d2b6ba70 308 for ($i=0; $i<$depth;$i++) {
378ef1fe 309 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
d2b6ba70 310 }
378ef1fe 311 $linkcss = $category->visible ? '' : ' class="dimmed" ';
312 echo '<a '.$linkcss.' title="'.$str->edit.'" '.
313 ' href="category.php?id='.$category->id.'&amp;categoryedit=on&amp;sesskey='.sesskey().'">'.
6ba65fa0 314 format_string($category->name).'</a>';
378ef1fe 315 echo '</td>';
d2b6ba70 316
378ef1fe 317 echo '<td class="count">'.$category->coursecount.'</td>';
d2b6ba70 318
378ef1fe 319 echo '<td class="icons">'; /// Print little icons
d2b6ba70 320
8ed5dd63 321 if (has_capability('moodle/category:manage', $category->context)) {
322 echo '<a title="'.$str->edit.'" href="editcategory.php?id='.$category->id.'"><img'.
b5d0cafc 323 ' src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.$str->edit.'" /></a> ';
86830be6 324
378ef1fe 325 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 326 ' src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.$str->delete.'" /></a> ';
0be6f678 327
61240489 328 if (!empty($category->visible)) {
378ef1fe 329 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 330 ' src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$str->hide.'" /></a> ';
61240489 331 } else {
378ef1fe 332 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 333 ' src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$str->show.'" /></a> ';
61240489 334 }
d2b6ba70 335
8ed5dd63 336 if ($up) {
337 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 338 ' src="'.$OUTPUT->pix_url('t/up') . '" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
8ed5dd63 339 } else {
340 echo $str->spacer;
341 }
342 if ($down) {
343 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
b5d0cafc 344 ' src="'.$OUTPUT->pix_url('t/down') . '" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
8ed5dd63 345 } else {
346 echo $str->spacer;
347 }
d2b6ba70 348 }
378ef1fe 349 echo '</td>';
d2b6ba70 350
378ef1fe 351 echo '<td align="left">';
8ed5dd63 352 if (has_capability('moodle/category:manage', $category->context)) {
353 $tempdisplaylist = $displaylist;
354 unset($tempdisplaylist[$category->id]);
355 foreach ($parentslist as $key => $parents) {
356 if (in_array($category->id, $parents)) {
357 unset($tempdisplaylist[$key]);
358 }
d2b6ba70 359 }
f8dab966
PS
360 $popupurl = new moodle_url("index.php?move=$category->id&sesskey=".sesskey());
361 $select = new single_select($popupurl, 'moveto', $tempdisplaylist, $category->parent, null, "moveform$category->id");
362 echo $OUTPUT->render($select);
d2b6ba70 363 }
378ef1fe 364 echo '</td>';
365 echo '</tr>';
c2cb4545 366 } else {
378ef1fe 367 $category->id = '0';
c2cb4545 368 }
f9903ed0 369
d2b6ba70 370 if ($categories = get_categories($category->id)) { // Print all the children recursively
371 $countcats = count($categories);
372 $count = 0;
373 $first = true;
374 $last = false;
375 foreach ($categories as $cat) {
376 $count++;
377 if ($count == $countcats) {
378 $last = true;
379 }
380 $up = $first ? false : true;
381 $down = $last ? false : true;
382 $first = false;
f9903ed0 383
0be6f678 384 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);
d2b6ba70 385 }
386 }
d9cb06dc 387}