3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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);
41 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
43 $PAGE->set_url('/course/index.php');
44 $PAGE->set_context($systemcontext);
46 if (can_edit_in_category()) {
47 if ($categoryedit !== -1) {
48 $USER->editing = $categoryedit;
51 $adminediting = $PAGE->user_is_editing();
53 if ($CFG->forcelogin) {
56 $adminediting = false;
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');
66 $strfulllistofcourses = get_string('fulllistofcourses');
69 /// Unless it's an editing admin, just print the regular listing of courses/categories
72 /// Print form for creating new categories
73 $countcategories = $DB->count_records('course_categories');
75 if ($countcategories > 1 || ($countcategories == 1 && $DB->count_records('course') > 200)) {
76 $strcourses = get_string('courses');
77 $strcategories = get_string('categories');
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();
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');
99 echo $OUTPUT->box_end();
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);
107 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
109 print_course_request_buttons($systemcontext);
110 echo $OUTPUT->container_end();
111 echo $OUTPUT->footer();
114 /// Everything else is editing on mode.
115 require_once($CFG->libdir.'/adminlib.php');
116 admin_externalpage_setup('coursemgmt');
118 /// Delete a category.
119 if (!empty($delete) and confirm_sesskey()) {
120 if (!$deletecat = $DB->get_record('course_categories', array('id'=>$delete))) {
121 print_error('invalidcategoryid');
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));
127 $heading = get_string('deletecategory', 'moodle', format_string($deletecat->name));
128 require_once('delete_category_form.php');
129 $mform = new delete_category_form(null, $deletecat);
130 $mform->set_data(array('delete'=>$delete));
132 if ($mform->is_cancelled()) {
133 redirect('index.php');
135 } else if (!$data= $mform->get_data()) {
136 require_once($CFG->libdir . '/questionlib.php');
137 echo $OUTPUT->header();
138 echo $OUTPUT->heading($heading);
140 echo $OUTPUT->footer();
144 echo $OUTPUT->header();
145 echo $OUTPUT->heading($heading);
147 if ($data->fulldelete) {
148 $deletedcourses = category_delete_full($deletecat, true);
150 foreach($deletedcourses as $course) {
151 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
153 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($deletecat->name)), 'notifysuccess');
156 category_delete_move($deletecat, $data->newparent, true);
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)));
164 echo $OUTPUT->continue_button('index.php');
166 echo $OUTPUT->footer();
170 /// Create a default category if necessary
171 if (!$categories = get_categories()) { /// No category yet!
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.
181 /// Move a category to a new parent if required
182 if (!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);
193 /// Hide or show a category
194 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
196 $tempcat = $DB->get_record('course_categories', array('id'=>$hide));
199 $tempcat = $DB->get_record('course_categories', array('id'=>$show));
202 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
204 $DB->set_field('course_categories', 'visible', $visible, array('id'=>$tempcat->id));
205 $DB->set_field('course', 'visible', $visible, array('category' => $tempcat->id));
209 /// Move a category up or down
210 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
211 fix_course_sortorder();
212 $swapcategory = NULL;
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);
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);
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));
234 // finally reorder courses
235 fix_course_sortorder();
239 echo $OUTPUT->header();
240 echo $OUTPUT->heading($strcategories);
242 /// Print out the categories with all the knobs
243 $strcategories = get_string('categories');
244 $strcourses = get_string('courses');
245 $strmovecategoryto = get_string('movecategoryto');
246 $stredit = get_string('edit');
248 $displaylist = array();
249 $parentlist = array();
251 $displaylist[0] = get_string('top');
252 make_categories_list($displaylist, $parentlist);
254 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
255 echo '<th class="header" scope="col">'.$strcategories.'</th>';
256 echo '<th class="header" scope="col">'.$strcourses.'</th>';
257 echo '<th class="header" scope="col">'.$stredit.'</th>';
258 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
261 print_category_edit(NULL, $displaylist, $parentlist);
264 echo '<div class="buttons">';
265 if (has_capability('moodle/course:create', $systemcontext)) {
266 // print create course link to first category
267 $options = array('category' => $CFG->defaultrequestcategory);
268 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
271 // Print button for creating new categories
272 if (has_capability('moodle/category:manage', $systemcontext)) {
273 $options = array('parent'=>0);
274 echo $OUTPUT->single_button(new moodle_url('editcategory.php', $options), get_string('addnewcategory'), 'get');
277 print_course_request_buttons($systemcontext);
280 echo $OUTPUT->footer();
282 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
283 /// Recursive function to print all the categories ready for editing
285 global $CFG, $USER, $OUTPUT;
291 $str->edit = get_string('edit');
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');
298 $str->spacer = '<img src="'.$CFG->wwwroot.'/pix/spacer.gif" class="iconsmall" alt="" /> ';
301 if (!empty($category)) {
303 if (!isset($category->context)) {
304 $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id);
307 echo '<tr><td align="left" class="name">';
308 for ($i=0; $i<$depth;$i++) {
309 echo ' ';
311 $linkcss = $category->visible ? '' : ' class="dimmed" ';
312 echo '<a '.$linkcss.' title="'.$str->edit.'" '.
313 ' href="category.php?id='.$category->id.'&categoryedit=on&sesskey='.sesskey().'">'.
314 format_string($category->name).'</a>';
317 echo '<td class="count">'.$category->coursecount.'</td>';
319 echo '<td class="icons">'; /// Print little icons
321 if (has_capability('moodle/category:manage', $category->context)) {
322 echo '<a title="'.$str->edit.'" href="editcategory.php?id='.$category->id.'"><img'.
323 ' src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.$str->edit.'" /></a> ';
325 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&sesskey='.sesskey().'"><img'.
326 ' src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.$str->delete.'" /></a> ';
328 if (!empty($category->visible)) {
329 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&sesskey='.sesskey().'"><img'.
330 ' src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$str->hide.'" /></a> ';
332 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&sesskey='.sesskey().'"><img'.
333 ' src="'.$OUTPUT->pix_url('t/show') . '" class="iconsmall" alt="'.$str->show.'" /></a> ';
337 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&sesskey='.sesskey().'"><img'.
338 ' src="'.$OUTPUT->pix_url('t/up') . '" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
343 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&sesskey='.sesskey().'"><img'.
344 ' src="'.$OUTPUT->pix_url('t/down') . '" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
351 echo '<td align="left">';
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]);
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);
370 if ($categories = get_categories($category->id)) { // Print all the children recursively
371 $countcats = count($categories);
375 foreach ($categories as $cat) {
377 if ($count == $countcats) {
380 $up = $first ? false : true;
381 $down = $last ? false : true;
384 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);