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 = 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;
52 $adminediting = $PAGE->user_is_editing();
54 if ($CFG->forcelogin
) {
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.
72 $showaddcoursebutton = true;
73 // Print form for creating new categories.
74 $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($COURSE->fullname
);
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($COURSE->fullname
);
94 $PAGE->set_button(update_category_button());
95 echo $OUTPUT->header();
96 echo $OUTPUT->skip_link_target();
97 echo $OUTPUT->box_start('courseboxes');
98 $showaddcoursebutton = print_courses(0);
99 echo $OUTPUT->box_end();
102 echo $OUTPUT->container_start('buttons');
103 if (has_capability('moodle/course:create', $systemcontext) && $showaddcoursebutton) {
104 // Print link to create a new course, for the 1st available category.
105 $options = array('category' => $CFG->defaultrequestcategory
);
106 echo $OUTPUT->single_button(new moodle_url('edit.php', $options), get_string('addnewcourse'), 'get');
108 print_course_request_buttons($systemcontext);
109 echo $OUTPUT->container_end();
110 echo $OUTPUT->footer();
113 /// Everything else is editing on mode.
114 require_once($CFG->libdir
.'/adminlib.php');
115 admin_externalpage_setup('coursemgmt');
117 /// Delete a category.
118 if (!empty($delete) and confirm_sesskey()) {
119 if (!$deletecat = $DB->get_record('course_categories', array('id'=>$delete))) {
120 print_error('invalidcategoryid');
122 $context = context_coursecat
::instance($delete);
123 require_capability('moodle/category:manage', $context);
124 require_capability('moodle/category:manage', get_category_or_system_context($deletecat->parent
));
126 $heading = get_string('deletecategory', 'moodle', format_string($deletecat->name
, true, array('context' => $context)));
127 require_once('delete_category_form.php');
128 $mform = new delete_category_form(null, $deletecat);
129 $mform->set_data(array('delete'=>$delete));
131 if ($mform->is_cancelled()) {
132 redirect('index.php');
134 } else if (!$data= $mform->get_data()) {
135 require_once($CFG->libdir
. '/questionlib.php');
136 echo $OUTPUT->header();
137 echo $OUTPUT->heading($heading);
139 echo $OUTPUT->footer();
143 echo $OUTPUT->header();
144 echo $OUTPUT->heading($heading);
146 if ($data->fulldelete
) {
147 $deletedcourses = category_delete_full($deletecat, true);
149 foreach($deletedcourses as $course) {
150 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname
), 'notifysuccess');
152 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($deletecat->name
, true, array('context' => $context))), 'notifysuccess');
155 category_delete_move($deletecat, $data->newparent
, true);
158 // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
159 if ($delete == $CFG->defaultrequestcategory
) {
160 set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent'=>0)));
163 echo $OUTPUT->continue_button('index.php');
165 echo $OUTPUT->footer();
169 /// Create a default category if necessary
170 if (!$categories = get_categories()) { /// No category yet!
172 $tempcat = new stdClass();
173 $tempcat->name
= get_string('miscellaneous');
174 $tempcat->id
= $DB->insert_record('course_categories', $tempcat);
175 $tempcat->context
= context_coursecat
::instance($tempcat->id
);
176 mark_context_dirty('/'.SYSCONTEXTID
);
177 fix_course_sortorder(); // Required to build course_categories.depth and .path.
178 set_config('defaultrequestcategory', $tempcat->id
);
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 ($hide and confirm_sesskey()) {
195 if ($tempcat = $DB->get_record('course_categories', array('id'=>$hide))) {
196 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent
));
197 if ($tempcat->visible
== 1) {
198 course_category_hide($tempcat);
201 } else if ($show and confirm_sesskey()) {
202 if ($tempcat = $DB->get_record('course_categories', array('id'=>$show))) {
203 require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent
));
204 if ($tempcat->visible
== 0) {
205 course_category_show($tempcat);
210 /// Move a category up or down
211 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
212 fix_course_sortorder();
213 $swapcategory = NULL;
215 if (!empty($moveup)) {
216 require_capability('moodle/category:manage', context_coursecat
::instance($moveup));
217 if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) {
218 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", array($movecategory->sortorder
, $movecategory->parent
), 'sortorder DESC', '*', 0, 1)) {
219 $swapcategory = reset($swapcategory);
223 require_capability('moodle/category:manage', context_coursecat
::instance($movedown));
224 if ($movecategory = $DB->get_record('course_categories', array('id'=>$movedown))) {
225 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder
, $movecategory->parent
), 'sortorder ASC', '*', 0, 1)) {
226 $swapcategory = reset($swapcategory);
230 if ($swapcategory and $movecategory) {
231 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder
, array('id'=>$movecategory->id
));
232 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder
, array('id'=>$swapcategory->id
));
233 add_to_log(SITEID
, "category", "move", "editcategory.php?id=$movecategory->id", $movecategory->id
);
236 // finally reorder courses
237 fix_course_sortorder();
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="generaltable 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>';
263 print_category_edit(NULL, $displaylist, $parentlist);
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);
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;
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
= context_coursecat
::instance($category->id
);
311 echo '<tr><td align="left" class="name">';
312 for ($i=0; $i<$depth;$i++
) {
313 echo ' ';
315 $linkcss = $category->visible ?
'' : ' class="dimmed" ';
316 echo '<a '.$linkcss.' title="'.$str->edit
.'" '.
317 ' href="category.php?id='.$category->id
.'&categoryedit=on&sesskey='.sesskey().'">'.
318 format_string($category->name
, true, array('context' => $category->context
)).'</a>';
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
.'&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
.'&sesskey='.sesskey().'"><img'.
334 ' src="'.$OUTPUT->pix_url('t/hide') . '" class="iconsmall" alt="'.$str->hide
.'" /></a> ';
336 echo '<a title="'.$str->show
.'" href="index.php?show='.$category->id
.'&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('t/cohort') . '" class="iconsmall" alt="'.$str->cohorts
.'" /></a> ';
346 echo '<a title="'.$str->moveup
.'" href="index.php?moveup='.$category->id
.'&sesskey='.sesskey().'"><img'.
347 ' src="'.$OUTPUT->pix_url('t/up') . '" class="iconsmall" alt="'.$str->moveup
.'" /></a> ';
352 echo '<a title="'.$str->movedown
.'" href="index.php?movedown='.$category->id
.'&sesskey='.sesskey().'"><img'.
353 ' src="'.$OUTPUT->pix_url('t/down') . '" class="iconsmall" alt="'.$str->movedown
.'" /></a> ';
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 $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
372 echo $OUTPUT->render($select);
377 $category = new stdClass();
381 if ($categories = get_categories($category->id
)) { // Print all the children recursively
382 $countcats = count($categories);
386 foreach ($categories as $cat) {
388 if ($count == $countcats) {
391 $up = $first ?
false : true;
392 $down = $last ?
false : true;
395 print_category_edit($cat, $displaylist, $parentslist, $depth+
1, $up, $down);