24f2b65a |
1 | <?php // $Id$ |
d2b6ba70 |
2 | // For most people, just lists the course categories |
3 | // Allows the admin to create, delete and rename course categories |
f9903ed0 |
4 | |
6bc502cc |
5 | require_once("../config.php"); |
6 | require_once("lib.php"); |
f9903ed0 |
7 | |
7ff99cb6 |
8 | $categoryedit = optional_param('categoryedit', -1,PARAM_BOOL); |
9d6a11f8 |
9 | $delete = optional_param('delete',0,PARAM_INT); |
10 | $hide = optional_param('hide',0,PARAM_INT); |
11 | $show = optional_param('show',0,PARAM_INT); |
9d6a11f8 |
12 | $move = optional_param('move',0,PARAM_INT); |
13 | $moveto = optional_param('moveto',-1,PARAM_INT); |
14 | $moveup = optional_param('moveup',0,PARAM_INT); |
15 | $movedown = optional_param('movedown',0,PARAM_INT); |
e2b347e9 |
16 | |
dfbf98cd |
17 | $sysctx = get_context_instance(CONTEXT_SYSTEM); |
18 | $context = $sysctx; |
7168423c |
19 | |
d2b6ba70 |
20 | if (!$site = get_site()) { |
ba6018a9 |
21 | print_error('siteisnotdefined', 'debug'); |
d2b6ba70 |
22 | } |
23 | |
29aab4ac |
24 | if ($CFG->forcelogin) { |
25 | require_login(); |
26 | } |
27 | |
dfbf98cd |
28 | if (has_capability('moodle/category:update', $sysctx)) { |
7ff99cb6 |
29 | if ($categoryedit !== -1) { |
30 | $USER->categoryediting = $categoryedit; |
d2b6ba70 |
31 | } |
9d6a11f8 |
32 | $adminediting = !empty($USER->categoryediting); |
33 | } else { |
34 | $adminediting = false; |
d2b6ba70 |
35 | } |
36 | |
fb31c40a |
37 | $stradministration = get_string('administration'); |
38 | $strcategories = get_string('categories'); |
39 | $strcategory = get_string('category'); |
40 | $strcourses = get_string('courses'); |
41 | $stredit = get_string('edit'); |
42 | $strdelete = get_string('delete'); |
43 | $straction = get_string('action'); |
44 | |
45 | |
d2b6ba70 |
46 | /// Unless it's an editing admin, just print the regular listing of courses/categories |
47 | |
48 | if (!$adminediting) { |
fb31c40a |
49 | |
50 | /// Print form for creating new categories |
51 | |
e4907498 |
52 | $countcategories = $DB->count_records('course_categories'); |
d2b6ba70 |
53 | |
e4907498 |
54 | if ($countcategories > 1 || ($countcategories == 1 && $DB->count_records('course') > 200)) { |
378ef1fe |
55 | $strcourses = get_string('courses'); |
56 | $strcategories = get_string('categories'); |
ba7472cc |
57 | |
58 | $navlinks = array(); |
59 | $navlinks[] = array('name'=>$strcategories,'link'=>'','type'=>'misc'); |
60 | $navigation = build_navigation($navlinks); |
61 | print_header("$site->shortname: $strcategories", $strcourses, $navigation, '', '', true, update_categories_button()); |
c0b85daf |
62 | print_heading($strcategories); |
317d5ddc |
63 | echo skip_main_destination(); |
44b843b6 |
64 | print_box_start('categorybox'); |
d2b6ba70 |
65 | print_whole_category_list(); |
44b843b6 |
66 | print_box_end(); |
38a10939 |
67 | print_course_search(); |
d2b6ba70 |
68 | } else { |
378ef1fe |
69 | $strfulllistofcourses = get_string('fulllistofcourses'); |
0be6f678 |
70 | print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, |
71 | build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))), |
10403748 |
72 | '', '', true, update_categories_button()); |
317d5ddc |
73 | echo skip_main_destination(); |
2cae9a4f |
74 | print_box_start('courseboxes'); |
35d0244a |
75 | print_courses(0); |
2cae9a4f |
76 | print_box_end(); |
d2b6ba70 |
77 | } |
c432fd32 |
78 | |
e2b347e9 |
79 | /// I am not sure this context in the next has_capability call is correct. |
dfbf98cd |
80 | if (isloggedin() and !isguest() and !has_capability('moodle/course:create', $sysctx) and $CFG->enablecourserequests) { // Print link to request a new course |
378ef1fe |
81 | print_single_button('request.php', NULL, get_string('courserequest'), 'get'); |
d1a66080 |
82 | } |
dfbf98cd |
83 | if (has_capability('moodle/course:create', $sysctx)) { // Print link to create a new course |
d9bb8caa |
84 | /// Get the 1st available category |
a5d424df |
85 | $options = array('category' => $DB->get_field('course_categories', 'id', array('parent'=>'0'))); |
d9bb8caa |
86 | print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); |
c432fd32 |
87 | } |
dfbf98cd |
88 | if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests)) { |
378ef1fe |
89 | print_single_button('pending.php',NULL, get_string('coursespending'),'get'); |
0705ff84 |
90 | } |
d2b6ba70 |
91 | print_footer(); |
92 | exit; |
93 | } |
94 | |
fb31c40a |
95 | /// From now on is all the admin/course creator functions |
d2b6ba70 |
96 | |
e2b347e9 |
97 | /// Delete a category if necessary |
0be6f678 |
98 | |
e2b347e9 |
99 | if (!empty($delete) and confirm_sesskey()) { |
100 | require_once('delete_category_form.php'); |
0be6f678 |
101 | |
e4907498 |
102 | if (!$deletecat = $DB->get_record('course_categories', array('id'=>$delete))) { |
9b34dc6e |
103 | print_error('invalidcategoryid'); |
e2b347e9 |
104 | } |
d2b6ba70 |
105 | |
e2b347e9 |
106 | $heading = get_string('deletecategory', '', format_string($deletecat->name)); |
d2b6ba70 |
107 | |
e2b347e9 |
108 | $context = get_context_instance(CONTEXT_COURSECAT, $delete); |
109 | require_capability('moodle/category:delete', $context); |
0be6f678 |
110 | |
e2b347e9 |
111 | $mform = new delete_category_form(null, $deletecat); |
112 | $mform->set_data(array('delete'=>$delete)); |
0be6f678 |
113 | |
e2b347e9 |
114 | if ($mform->is_cancelled()) { |
115 | redirect('index.php'); |
0be6f678 |
116 | |
294ce987 |
117 | } else if (!$data= $mform->get_data()) { |
e2b347e9 |
118 | require_once($CFG->libdir . '/questionlib.php'); |
119 | print_category_edit_header(); |
120 | print_heading($heading); |
121 | print_box(get_string('deletecategorycheck2'), 'generalbox boxwidthnormal boxaligncenter'); |
122 | if (question_context_has_any_questions($context)) { |
123 | print_box(get_string('deletecoursecategorywithquestions', 'question'), |
124 | 'generalbox boxwidthnormal boxaligncenter'); |
17c4a011 |
125 | } |
e2b347e9 |
126 | $mform->display(); |
127 | print_footer(); |
128 | exit(); |
d2b6ba70 |
129 | } |
e2b347e9 |
130 | |
131 | print_category_edit_header(); |
132 | print_heading($heading); |
133 | |
134 | if ($data->fulldelete) { |
135 | category_delete_full($deletecat, true); |
136 | } else { |
137 | category_delete_move($deletecat, $data->newparent, true); |
138 | } |
139 | |
140 | print_continue('index.php'); |
141 | |
142 | print_footer(); |
143 | die; |
ba2e5d73 |
144 | } |
0a263205 |
145 | |
e2b347e9 |
146 | /// Print headings |
147 | print_category_edit_header(); |
148 | print_heading($strcategories); |
149 | |
ba2e5d73 |
150 | |
d2b6ba70 |
151 | /// Create a default category if necessary |
152 | if (!$categories = get_categories()) { /// No category yet! |
153 | // Try and make one |
e4907498 |
154 | $tempcat = new object(); |
378ef1fe |
155 | $tempcat->name = get_string('miscellaneous'); |
e4907498 |
156 | if (!$tempcat->id = $DB->insert_record('course_categories', $tempcat)) { |
ba6018a9 |
157 | print_error('cannotsetupcategory'); |
d2b6ba70 |
158 | } |
1d28d87b |
159 | $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id); |
160 | mark_context_dirty('/'.SYSCONTEXTID); |
d2b6ba70 |
161 | } |
162 | |
163 | |
164 | /// Move a category to a new parent if required |
165 | |
7168423c |
166 | if (!empty($move) and ($moveto>=0) and confirm_sesskey()) { |
e4907498 |
167 | if ($tempcat = $DB->get_record('course_categories', array('id'=>$move))) { |
d2b6ba70 |
168 | if ($tempcat->parent != $moveto) { |
e4907498 |
169 | $newp = $DB->get_record('course_categories', array('id'=>$moveto)); |
88903eea |
170 | if (! move_category($tempcat, $newp)) { |
378ef1fe |
171 | notify('Could not update that category!'); |
0be6f678 |
172 | } |
d2b6ba70 |
173 | } |
174 | } |
175 | } |
176 | |
177 | |
0be6f678 |
178 | /// Hide or show a category |
7168423c |
179 | if ((!empty($hide) or !empty($show)) and confirm_sesskey()) { |
180 | if (!empty($hide)) { |
e4907498 |
181 | $tempcat = $DB->get_record('course_categories', array('id'=>$hide)); |
d2b6ba70 |
182 | $visible = 0; |
183 | } else { |
e4907498 |
184 | $tempcat = $DB->get_record('course_categories', array('id'=>$show)); |
d2b6ba70 |
185 | $visible = 1; |
186 | } |
187 | if ($tempcat) { |
e4907498 |
188 | if (!$DB->set_field('course_categories', 'visible', $visible, array('id'=>$tempcat->id))) { |
378ef1fe |
189 | notify('Could not update that category!'); |
d2b6ba70 |
190 | } |
e4907498 |
191 | if (!$DB->set_field('course', 'visible', $visible, array('category'=>$tempcat->id))) { |
378ef1fe |
192 | notify('Could not hide/show any courses in this category !'); |
d2b6ba70 |
193 | } |
194 | } |
195 | } |
196 | |
197 | |
198 | /// Move a category up or down |
199 | |
7168423c |
200 | if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) { |
0be6f678 |
201 | |
0cbe8111 |
202 | fix_course_sortorder(); |
d2b6ba70 |
203 | $swapcategory = NULL; |
d2b6ba70 |
204 | |
7168423c |
205 | if (!empty($moveup)) { |
e4907498 |
206 | if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) { |
0cbe8111 |
207 | if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) { |
208 | $swapcategory = reset($swapcategory); |
d2b6ba70 |
209 | } |
210 | } |
0cbe8111 |
211 | } else { |
e4907498 |
212 | if ($movecategory = $DB->get_record('course_categories', array('id'=>$movedown))) { |
0cbe8111 |
213 | if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) { |
214 | $swapcategory = reset($swapcategory); |
d2b6ba70 |
215 | } |
216 | } |
217 | } |
0cbe8111 |
218 | if ($swapcategory and $movecategory) { |
219 | $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id'=>$movecategory->id)); |
220 | $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id'=>$swapcategory->id)); |
d2b6ba70 |
221 | } |
d2b6ba70 |
222 | |
0cbe8111 |
223 | // finally reorder courses |
224 | fix_course_sortorder(); |
225 | } |
0be6f678 |
226 | |
0cbe8111 |
227 | /// This should not be needed anymore |
228 | //fix_course_sortorder(); |
d2b6ba70 |
229 | |
d2b6ba70 |
230 | /// Print out the categories with all the knobs |
231 | |
378ef1fe |
232 | $strcategories = get_string('categories'); |
233 | $strcourses = get_string('courses'); |
234 | $strmovecategoryto = get_string('movecategoryto'); |
235 | $stredit = get_string('edit'); |
d2b6ba70 |
236 | |
237 | $displaylist = array(); |
238 | $parentlist = array(); |
239 | |
378ef1fe |
240 | $displaylist[0] = get_string('top'); |
241 | make_categories_list($displaylist, $parentlist, ''); |
d2b6ba70 |
242 | |
fb31c40a |
243 | echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">'; |
378ef1fe |
244 | echo '<th class="header" scope="col">'.$strcategories.'</th>'; |
245 | echo '<th class="header" scope="col">'.$strcourses.'</th>'; |
246 | echo '<th class="header" scope="col">'.$stredit.'</th>'; |
247 | echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>'; |
248 | echo '</tr>'; |
d2b6ba70 |
249 | |
250 | print_category_edit(NULL, $displaylist, $parentlist); |
251 | |
378ef1fe |
252 | echo '</table>'; |
d2b6ba70 |
253 | |
571880d2 |
254 | echo '<div class="buttons">'; |
e2b347e9 |
255 | |
435f3044 |
256 | if (!empty($category->id)) { |
257 | // Print link to create a new course in current category |
258 | if (has_capability('moodle/course:create', $context)) { |
259 | $options = array(); |
dfbf98cd |
260 | $options['category'] = $category->id; |
261 | print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); |
435f3044 |
262 | } |
263 | }else{ |
e2b347e9 |
264 | if (has_capability('moodle/course:create', $sysctx)) { |
435f3044 |
265 | // print create course link to first category |
266 | $options = array(); |
a5d424df |
267 | $options = array('category' => $DB->get_field('course_categories', 'id', array('parent'=>'0'))); |
435f3044 |
268 | print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); |
dfbf98cd |
269 | } |
bbbf2d40 |
270 | } |
0be6f678 |
271 | |
86830be6 |
272 | // Print button for creating new categories |
273 | if (has_capability('moodle/category:create', $context)) { |
274 | unset($options); |
275 | if (!empty($category->id)) { |
276 | $options['id'] = $category->id; |
277 | } else { |
278 | $options['id'] = 0; |
279 | } |
280 | $options['categoryadd'] = 1; |
281 | print_single_button('editcategory.php', $options, get_string('addnewcategory'), 'get'); |
282 | } |
283 | |
dfbf98cd |
284 | if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests)) { |
fb31c40a |
285 | print_single_button('pending.php',NULL, get_string('coursespending'), 'get'); |
286 | } |
571880d2 |
287 | // admin page does not allow custom buttons in the navigation bar |
288 | echo '<div class="singlebutton">'; |
289 | echo update_categories_button(); |
290 | echo '</div></div>'; |
291 | |
c9e42aaa |
292 | print_footer(); |
d2b6ba70 |
293 | |
294 | function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) { |
295 | /// Recursive function to print all the categories ready for editing |
296 | |
810393c8 |
297 | global $CFG, $USER; |
d2b6ba70 |
298 | |
299 | static $str = ''; |
0be6f678 |
300 | |
d2b6ba70 |
301 | if (empty($str)) { |
86830be6 |
302 | $str->edit = get_string('edit'); |
378ef1fe |
303 | $str->delete = get_string('delete'); |
304 | $str->moveup = get_string('moveup'); |
305 | $str->movedown = get_string('movedown'); |
306 | $str->edit = get_string('editthiscategory'); |
307 | $str->hide = get_string('hide'); |
308 | $str->show = get_string('show'); |
d2b6ba70 |
309 | } |
0be6f678 |
310 | |
d2b6ba70 |
311 | if ($category) { |
378ef1fe |
312 | |
dfbf98cd |
313 | if (!isset($category->context)) { |
314 | $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id); |
315 | } |
0be6f678 |
316 | |
378ef1fe |
317 | echo '<tr><td align="left" class="name">'; |
d2b6ba70 |
318 | for ($i=0; $i<$depth;$i++) { |
378ef1fe |
319 | echo ' '; |
d2b6ba70 |
320 | } |
378ef1fe |
321 | $linkcss = $category->visible ? '' : ' class="dimmed" '; |
322 | echo '<a '.$linkcss.' title="'.$str->edit.'" '. |
323 | ' href="category.php?id='.$category->id.'&categoryedit=on&sesskey='.sesskey().'">'. |
6ba65fa0 |
324 | format_string($category->name).'</a>'; |
378ef1fe |
325 | echo '</td>'; |
d2b6ba70 |
326 | |
378ef1fe |
327 | echo '<td class="count">'.$category->coursecount.'</td>'; |
d2b6ba70 |
328 | |
378ef1fe |
329 | echo '<td class="icons">'; /// Print little icons |
d2b6ba70 |
330 | |
86830be6 |
331 | if (has_capability('moodle/category:update', $category->context)) { |
332 | echo '<a title="'.$str->edit.'" href="editcategory.php?id='.$category->id.'&sesskey='.sesskey().'"><img'. |
333 | ' src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$str->edit.'" /></a> '; |
334 | } |
335 | |
dfbf98cd |
336 | if (has_capability('moodle/category:delete', $category->context)) { |
378ef1fe |
337 | echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&sesskey='.sesskey().'"><img'. |
338 | ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$str->delete.'" /></a> '; |
61240489 |
339 | } |
0be6f678 |
340 | |
dfbf98cd |
341 | if (has_capability('moodle/category:visibility', $category->context)) { |
61240489 |
342 | if (!empty($category->visible)) { |
378ef1fe |
343 | echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&sesskey='.sesskey().'"><img'. |
344 | ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$str->hide.'" /></a> '; |
61240489 |
345 | } else { |
378ef1fe |
346 | echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&sesskey='.sesskey().'"><img'. |
347 | ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$str->show.'" /></a> '; |
61240489 |
348 | } |
349 | } |
d2b6ba70 |
350 | |
351 | if ($up) { |
378ef1fe |
352 | echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&sesskey='.sesskey().'"><img'. |
353 | ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$str->moveup.'" /></a> '; |
d2b6ba70 |
354 | } |
355 | if ($down) { |
378ef1fe |
356 | echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&sesskey='.sesskey().'"><img'. |
357 | ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$str->movedown.'" /></a> '; |
d2b6ba70 |
358 | } |
378ef1fe |
359 | echo '</td>'; |
d2b6ba70 |
360 | |
378ef1fe |
361 | echo '<td align="left">'; |
d2b6ba70 |
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]); |
367 | } |
368 | } |
378ef1fe |
369 | popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", $category->parent, '', '', '', false); |
370 | echo '</td>'; |
371 | echo '</tr>'; |
c2cb4545 |
372 | } else { |
378ef1fe |
373 | $category->id = '0'; |
c2cb4545 |
374 | } |
f9903ed0 |
375 | |
d2b6ba70 |
376 | if ($categories = get_categories($category->id)) { // Print all the children recursively |
377 | $countcats = count($categories); |
378 | $count = 0; |
379 | $first = true; |
380 | $last = false; |
381 | foreach ($categories as $cat) { |
382 | $count++; |
383 | if ($count == $countcats) { |
384 | $last = true; |
385 | } |
386 | $up = $first ? false : true; |
387 | $down = $last ? false : true; |
388 | $first = false; |
f9903ed0 |
389 | |
0be6f678 |
390 | print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down); |
d2b6ba70 |
391 | } |
392 | } |
393 | } |
e2b347e9 |
394 | |
395 | function print_category_edit_header() { |
396 | global $CFG; |
ab7ac6ac |
397 | global $SITE; |
e2b347e9 |
398 | |
399 | if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { |
400 | require_once($CFG->libdir.'/adminlib.php'); |
401 | admin_externalpage_setup('coursemgmt'); |
402 | admin_externalpage_print_header(); |
403 | } else { |
ab7ac6ac |
404 | print_header("$SITE->shortname:". get_string('categories'), get_string('courses'), |
e2b347e9 |
405 | build_navigation(array(array('name'=>get_string('categories'),'link'=>'','type'=>'misc'))), '', '', true, update_categories_button()); |
406 | } |
407 | } |
d2b6ba70 |
408 | ?> |