fix for MDL-5958 from Elijah (HSU)
[moodle.git] / question / category_class.php
CommitLineData
107e7612 1<?php // $Id$
516cf3eb 2/**
bc649d80 3 * Class question_category_object
4 *
5 * Used for handling changes to the question categories
6 *
7 * @version $Id$
8 * @author Martin Dougiamas and many others. {@link http://moodle.org}
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package question
11 */
516cf3eb 12
13// number of categories to display on page
14define( "PAGE_LENGTH",25 );
15
dc1f00de 16class question_category_object {
516cf3eb 17
18 var $str;
19 var $pixpath;
20 var $edittable;
21 var $newtable;
22 var $tab;
23 var $tabsize = 3;
24 var $categories;
25 var $categorystrings;
26 var $defaultcategory;
27 var $course;
28 var $topcount;
29
bc649d80 30 /**
31 * Constructor
32 *
33 * Gets necessary strings and sets relevant path information
34 */
dc1f00de 35 function question_category_object() {
516cf3eb 36 global $CFG;
37
38 $this->tab = str_repeat('&nbsp;', $this->tabsize);
39
40 $this->str->course = get_string('course');
41 $this->str->category = get_string('category', 'quiz');
42 $this->str->categoryinfo = get_string('categoryinfo', 'quiz');
43 $this->str->questions = get_string('questions', 'quiz');
44 $this->str->add = get_string('add');
45 $this->str->delete = get_string('delete');
46 $this->str->moveup = get_string('moveup');
47 $this->str->movedown = get_string('movedown');
48 $this->str->edit = get_string('editthiscategory');
49 $this->str->hide = get_string('hide');
50 $this->str->publish = get_string('publish', 'quiz');
51 $this->str->order = get_string('order');
52 $this->str->parent = get_string('parent', 'quiz');
53 $this->str->add = get_string('add');
54 $this->str->action = get_string('action');
55 $this->str->top = get_string('top', 'quiz');
56 $this->str->addcategory = get_string('addcategory', 'quiz');
57 $this->str->editcategory = get_string('editcategory', 'quiz');
58 $this->str->cancel = get_string('cancel');
59 $this->str->editcategories = get_string('editcategories', 'quiz');
2befe778 60 $this->str->page = get_string('page');
516cf3eb 61 $this->pixpath = $CFG->pixpath;
62
63 }
64
bc649d80 65 /**
66 * Sets the course for this object
67 *
68 * @param object course
69 */
516cf3eb 70 function set_course($course) {
71 $this->course = $course;
72 }
73
bc649d80 74 /**
75 * Displays the user interface
76 *
77 * @param object modform
78 * @param int $page page number to display (0=don't paginate)
79 */
516cf3eb 80 function display_user_interface($page=0) {
81 $this->initialize();
82
83 /// Interface for adding a new category:
84 print_heading_with_help($this->str->addcategory, 'categories_edit', 'quiz');
85 $this->output_new_table();
86 echo '<br />';
87
88 /// Interface for editing existing categories
89 print_heading_with_help($this->str->editcategories, 'categories', 'quiz');
90 $this->output_edit_table($page);
91 if ($this->topcount>PAGE_LENGTH) {
92 $this->display_page_numbers($page);
93 }
94 echo '<br />';
95
96 }
97
98
bc649d80 99 /**
100 * Initializes this classes general category-related variables
101 */
516cf3eb 102 function initialize() {
103
104 /// Get the existing categories
4f48fb42 105 if (!$this->defaultcategory = get_default_question_category($this->course->id)) {
516cf3eb 106 error("Error: Could not find or make a category!");
107 }
108
dc1f00de 109 $this->categories = $this->get_question_categories(null, "parent, sortorder, name ASC");
516cf3eb 110
111 $this->categories = $this->arrange_categories($this->categories);
112
113 // create the array of id=>full_name strings
114 $this->categorystrings = $this->expanded_category_strings($this->categories);
115
116 // for pagination calculate number of 'top' categories and hence number of pages
117 // (pagination only based on top categories)
118 $count = 0;
119 foreach( $this->categories as $category ) {
120 if ($category->parent==0) {
121 ++$count;
122 }
123 }
124 $this->topcount = $count;
125 $this->pagecount = (integer) ceil( $count / PAGE_LENGTH );
126 }
127
bc649d80 128 /**
129 * display list of page numbers for navigation
130 */
516cf3eb 131 function display_page_numbers( $page=0 ) {
132 global $USER;
133
134 echo "<div class=\"paging\">{$this->str->page}:\n";
135 foreach (range(1,$this->pagecount) as $currentpage) {
136 if ($page == $currentpage) {
137 echo " $currentpage \n";
138 }
139 else {
140 echo "<a href=\"category.php?id={$this->course->id}&amp;page=$currentpage&amp;sesskey={$USER->sesskey}\">";
141 echo " $currentpage </a>\n";
142 }
143 }
144 echo "</div>";
145 }
146
bc649d80 147 /**
148 * Outputs a table to allow entry of a new category
149 */
516cf3eb 150 function output_new_table() {
151 global $USER;
152 $publishoptions[0] = get_string("no");
153 $publishoptions[1] = get_string("yes");
154
155 $this->newtable->head = array ($this->str->parent, $this->str->category, $this->str->categoryinfo, $this->str->publish, $this->str->action);
156 $this->newtable->width = 200;
157 $this->newtable->data[] = array();
158 $this->newtable->tablealign = 'center';
159
160 /// Each section below adds a data cell to the table row
161
162
163 $viableparents[0] = $this->str->top;
164 $viableparents = $viableparents + $this->categorystrings;
165 $this->newtable->align['parent'] = "left";
166 $this->newtable->wrap['parent'] = "nowrap";
167 $row['parent'] = choose_from_menu ($viableparents, "newparent", $this->str->top, "", "", "", true);
168
169 $this->newtable->align['category'] = "left";
170 $this->newtable->wrap['category'] = "nowrap";
171 $row['category'] = '<input type="text" name="newcategory" value="" size="15" />';
172
173 $this->newtable->align['info'] = "left";
174 $this->newtable->wrap['info'] = "nowrap";
175 $row['info'] = '<input type="text" name="newinfo" value="" size="50" />';
176
177 $this->newtable->align['publish'] = "left";
178 $this->newtable->wrap['publish'] = "nowrap";
179 $row['publish'] = choose_from_menu ($publishoptions, "newpublish", "", "", "", "", true);
180
181 $this->newtable->align['action'] = "left";
182 $this->newtable->wrap['action'] = "nowrap";
183 $row['action'] = '<input type="submit" value="' . $this->str->add . '" />';
184
185
186 $this->newtable->data[] = $row;
187
188 // wrap the table in a form and output it
189 echo '<form action="category.php" method="post">';
09275894 190 echo '<fieldset class="invisiblefieldset">';
516cf3eb 191 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
192 echo '<input type="hidden" name="id" value="'. $this->course->id . '" />';
193 echo '<input type="hidden" name="addcategory" value="true" />';
194 print_table($this->newtable);
09275894 195 echo '</fieldset>';
516cf3eb 196 echo '</form>';
197 }
198
bc649d80 199 /**
200 * Outputs a table to allow editing/rearranging of existing categories
201 *
202 * $this->initialize() must have already been called
203 *
204 * @param object course
205 * @param int $page page to display (0=do not paginate)
206 */
516cf3eb 207 function output_edit_table($page=0) {
208 $this->edittable->head = array ($this->str->category, $this->str->categoryinfo, $this->str->questions, $this->str->publish,
209 $this->str->delete, $this->str->order, $this->str->parent);
210 $this->edittable->width = 200;
211 $this->edittable->tablealign = 'center';
212
213 $courses = $this->course->shortname;
214
215 // if pagination required work out range
216 if (!empty($page)) {
217 $firstcat = ($page-1) * PAGE_LENGTH + 1;
218 $lastcat = $firstcat + PAGE_LENGTH - 1;
219 }
220 else {
221 $firstcat = 1;
222 $lastcat = $this->topcount;
223 }
224//echo "$firstcat $lastcat $page"; die;
225 $this->build_edit_table_body($this->categories, $page, $firstcat, $lastcat);
226 print_table($this->edittable);
227 }
bc649d80 228
229 /**
230 * Recursively builds up the edit-categories table body
231 *
232 * @param array categories contains category objects in a tree representation
233 * @param mixed courses String with shortname of course | array containing courseid=>shortname
234 * @param int depth controls the indenting
235 */
516cf3eb 236 function build_edit_table_body($categories, $page = 0, $firstcat = 1, $lastcat = 99999, $depth = 0) {
237 $countcats = count($categories);
238 $count = 0;
239 $first = true;
240 $last = false;
241 $topcount = 0;
242
243 foreach ($categories as $category) {
244 $count++;
245 if ($count == $countcats) {
246 $last = true;
247 }
248 // check if this category is on the display page
249 if ($depth==0) {
250 $topcount++;
251 if (($topcount<$firstcat) or ($topcount>$lastcat)) {
252 continue;
253 }
254 }
255 $up = $first ? false : true;
256 $down = $last ? false : true;
257 $first = false;
f5565b69 258 $this->edit_question_category_row($category, $depth, $up, $down, $page);
516cf3eb 259 if (isset($category->children)) {
260 $this->build_edit_table_body($category->children, $page, $firstcat, $lastcat, $depth + 1);
261 }
262 }
263 }
264
bc649d80 265 /**
266 * gets all the courseids for the given categories
267 *
268 * @param array categories contains category objects in a tree representation
269 * @return array courseids flat array in form categoryid=>courseid
270 */
516cf3eb 271 function get_course_ids($categories) {
272 $courseids = array();
273 foreach ($categories as $key=>$cat) {
274 $courseids[$key] = $cat->course;
275 if (!empty($cat->children)) {
276 $courseids = array_merge($courseids, $this->get_course_ids($cat->children));
277 }
278 }
279 return $courseids;
280 }
281
bc649d80 282 /**
283 * Constructs each row of the edit-categories table
284 *
285 * @param object category
286 * @param int depth controls the indenting
287 * @param string shortname short name of the course
288 * @param boolean up can it be moved up?
289 * @param boolean down can it be moved down?
290 * @param int page page number
291 */
f5565b69 292 function edit_question_category_row($category, $depth, $up = false, $down = false, $page = 0) {
516cf3eb 293 global $USER;
294 $fill = str_repeat($this->tab, $depth);
295
d4228481 296 $linkcss = $category->publish ? ' class="published" ' : ' class="unpublished" ';
516cf3eb 297
298 if (!empty($page)) {
299 $pagelink="&amp;page=$page";
300 }
301 else {
302 $pagelink="";
303 }
304
305 /// Each section below adds a data cell to this table row
306
307 $this->edittable->align["$category->id.name"] = "left";
308 $this->edittable->wrap["$category->id.name"] = "nowrap";
8b9e8fc3 309 $row["$category->id.name"] = '<a ' . $linkcss . ' title="' . $this->str->edit. '" href="category.php?id=' . $this->course->id .
0d905d9f 310 '&amp;edit=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/edit.gif" class="iconsmall"
516cf3eb 311 alt="' .$this->str->edit. '" /> ' . $fill . $category->name . '</a>';
312
313 $this->edittable->align["$category->id.info"] = "left";
314 $this->edittable->wrap["$category->id.info"] = "nowrap";
8b9e8fc3 315 $row["$category->id.info"] = '<a ' . $linkcss . ' title="' . $this->str->edit .'" href="category.php?id=' . $this->course->id .
516cf3eb 316 '&amp;edit=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'">' . $category->info . '</a>';
317
318 $this->edittable->align["$category->id.qcount"] = "center";
319 $row["$category->id.qcount"] = $category->questioncount;
320
321 $this->edittable->align["$category->id.publish"] = "center";
322 $this->edittable->wrap["$category->id.publish"] = "nowrap";
323 if (!empty($category->publish)) {
324 $row["$category->id.publish"] = '<a title="' . $this->str->hide . '" href="category.php?id=' . $this->course->id . '&amp;hide=' . $category->id .
0d905d9f 325 '&amp;sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/hide.gif" class="iconsmall" alt="' .$this->str->hide. '" /></a> ';
516cf3eb 326 } else {
327 $row["$category->id.publish"] = '<a title="' . $this->str->publish . '" href="category.php?id=' . $this->course->id . '&amp;publish=' . $category->id .
0d905d9f 328 '&amp;sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/show.gif" class="iconsmall" alt="' .$this->str->publish. '" /></a> ';
516cf3eb 329 }
330
331 if ($category->id != $this->defaultcategory->id) {
332 $this->edittable->align["$category->id.delete"] = "center";
333 $this->edittable->wrap["$category->id.delete"] = "nowrap";
334 $row["$category->id.delete"] = '<a title="' . $this->str->delete . '" href="category.php?id=' . $this->course->id .
0d905d9f 335 '&amp;delete=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/delete.gif" class="iconsmall" alt="' .$this->str->delete. '" /></a> ';
516cf3eb 336 } else {
337 $row["$category->id.delete"] = '';
338 }
339
340 $this->edittable->align["$category->id.order"] = "left";
341 $this->edittable->wrap["$category->id.order"] = "nowrap";
342 $icons = '';
343 if ($up) {
344 $icons .= '<a title="' . $this->str->moveup .'" href="category.php?id=' . $this->course->id . '&amp;moveup=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'">
0d905d9f 345 <img src="' . $this->pixpath . '/t/up.gif" class="iconsmall" alt="' . $this->str->moveup. '" /></a> ';
516cf3eb 346 }
347 if ($down) {
348 $icons .= '<a title="' . $this->str->movedown .'" href="category.php?id=' . $this->course->id . '&amp;movedown=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'">
0d905d9f 349 <img src="' . $this->pixpath . '/t/down.gif" class="iconsmall" alt="' .$this->str->movedown. '" /></a> ';
516cf3eb 350 }
351 $row["$category->id.order"]= $icons;
352
353 $this->edittable->align["$category->id.moveto"] = "left";
354 $this->edittable->wrap["$category->id.moveto"] = "nowrap";
355 if ($category->id != $this->defaultcategory->id) {
356 $viableparents = $this->categorystrings;
357 $this->set_viable_parents($viableparents, $category);
358 $viableparents = array(0=>$this->str->top) + $viableparents;
359
360 $row["$category->id.moveto"] = popup_form ("category.php?id={$this->course->id}&amp;move={$category->id}&amp;sesskey=$USER->sesskey$pagelink&amp;moveto=",
361 $viableparents, "moveform{$category->id}", "$category->parent", "", "", "", true);
362 } else {
363 $row["$category->id.moveto"]='---';
364 }
365
366
367 $this->edittable->data[$category->id] = $row;
368 }
369
370
371 function edit_single_category($categoryid,$page=1) {
372 /// Interface for adding a new category
373 global $USER;
374 $this->initialize();
375
376 /// Interface for editing existing categories
dc1f00de 377 if ($category = get_record("question_categories", "id", $categoryid)) {
516cf3eb 378 echo '<h2 align="center">';
379 echo $this->str->edit;
380 helpbutton("categories_edit", $this->str->editcategory, "quiz");
381 echo '</h2>';
382 echo '<table width="100%"><tr><td>';
383 $this->output_edit_single_table($category,$page);
384 echo '</td></tr></table>';
385 echo '<p><div align="center"><form action="category.php" method="get">
09275894 386 <fieldset class="invisiblefieldset">
516cf3eb 387 <input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />
388 <input type="hidden" name="id" value="' . $this->course->id . '" />
09275894 389 <input type="submit" value="' . $this->str->cancel . '" />
390 </fieldset>
391 </form>
392 </div></p>';
516cf3eb 393 print_footer($this->course);
394 exit;
395 } else {
396 error("Category $categoryid not found", "category.php?id={$this->course->id}");
397 }
398 }
399
bc649d80 400 /**
401 * Outputs a table to allow editing of an existing category
402 *
403 * @param object category
404 * @param int page current page
405 */
516cf3eb 406 function output_edit_single_table($category, $page=1) {
407 global $USER;
408 $publishoptions[0] = get_string("no");
409 $publishoptions[1] = get_string("yes");
410 $strupdate = get_string('update');
411
4abc23b2 412 $edittable = new stdClass;
516cf3eb 413
414 $edittable->head = array ($this->str->parent, $this->str->category, $this->str->categoryinfo, $this->str->publish, $this->str->action);
415 $edittable->width = 200;
416 $edittable->data[] = array();
417 $edittable->tablealign = 'center';
418
419 /// Each section below adds a data cell to the table row
420
421 $viableparents = $this->categorystrings;
422 $this->set_viable_parents($viableparents, $category);
423 $viableparents = array(0=>$this->str->top) + $viableparents;
424 $edittable->align['parent'] = "left";
425 $edittable->wrap['parent'] = "nowrap";
426 $row['parent'] = choose_from_menu ($viableparents, "updateparent", "{$category->parent}", "", "", "", true);
427
428 $edittable->align['category'] = "left";
429 $edittable->wrap['category'] = "nowrap";
430 $row['category'] = '<input type="text" name="updatename" value="' . $category->name . '" size="15" />';
431
432 $edittable->align['info'] = "left";
433 $edittable->wrap['info'] = "nowrap";
434 $row['info'] = '<input type="text" name="updateinfo" value="' . $category->info . '" size="50" />';
435
436 $edittable->align['publish'] = "left";
437 $edittable->wrap['publish'] = "nowrap";
438 $selected = (boolean)$category->publish ? 1 : 0;
439 $row['publish'] = choose_from_menu ($publishoptions, "updatepublish", $selected, "", "", "", true);
440
441 $edittable->align['action'] = "left";
442 $edittable->wrap['action'] = "nowrap";
443 $row['action'] = '<input type="submit" value="' . $strupdate . '" />';
444
445 $edittable->data[] = $row;
446
447 // wrap the table in a form and output it
448 echo '<p><form action="category.php" method="post">';
09275894 449 echo '<fieldset class="invisiblefieldset">';
516cf3eb 450 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
451 echo '<input type="hidden" name="id" value="'. $this->course->id . '" />';
452 echo '<input type="hidden" name="updateid" value="' . $category->id . '" />';
453 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />";
454 print_table($edittable);
09275894 455 echo '</fieldset>';
516cf3eb 456 echo '</form></p>';
457 }
458
bc649d80 459 /**
460 * Creates an array of "full-path" category strings
461 * Structure:
462 * key => string
463 * where key is the category id, and string contains the name of all ancestors as well as the particular category name
464 * E.g. '123'=>'Language / English / Grammar / Modal Verbs"
465 *
466 * @param array $categories an array containing categories arranged in a tree structure
467 */
516cf3eb 468 function expanded_category_strings($categories, $parent=null) {
469 $prefix = is_null($parent) ? '' : "$parent / ";
470 $categorystrings = array();
471 foreach ($categories as $key => $category) {
472 $expandedname = "$prefix$category->name";
473 $categorystrings[$key] = $expandedname;
474 if (isset($category->children)) {
475 $categorystrings = $categorystrings + $this->expanded_category_strings($category->children, $expandedname);
476 }
477 }
478 return $categorystrings;
479 }
480
bc649d80 481 /**
482 * Arranges the categories into a hierarchical tree
483 *
484 * If a category has children, it's "children" property holds an array of children
485 * The questioncount for each category is also calculated
486 *
487 * @param array records a flat list of the categories
488 * @return array categorytree a hierarchical list of the categories
489 */
516cf3eb 490 function arrange_categories($records) {
4f48fb42 491 //TODO: get the question count for all records with one sql statement: select category, count(*) from question group by category
516cf3eb 492 $levels = array();
493
494 // build a levels array, which places each record according to it's depth from the top level
495 $parents = array(0);
496 while (!empty($parents)) {
497 $children = array();
498 foreach ($records as $record) {
499 if (in_array($record->parent, $parents)) {
500 $children[] = $record->id;
501 }
502 }
503 if (!empty($children)) {
504 $levels[] = $children;
505 }
506 $parents = $children;
507 }
508 // if there is no hierarchy (e.g., if all records have parent == 0), set level[0] to these keys
509 if (empty($levels)) {
510 $levels[0] = array_keys($records);
511 }
512
513 // build a hierarchical array that depicts the parent-child relationships of the categories
514 $categorytree = array();
515 for ($index = count($levels) - 1; $index >= 0; $index--) {
516 foreach($levels[$index] as $key) {
517 $parentkey = $records[$key]->parent;
4f48fb42 518 if (!($records[$key]->questioncount = count_records('question', 'category', $records[$key]->id, 'hidden', 0, 'parent', '0'))) {
516cf3eb 519 $records[$key]->questioncount = 0;
520 }
521 if ($parentkey == 0) {
522 $categorytree[$key] = $records[$key];
523 } else {
524 $records[$parentkey]->children[$key] = $records[$key];
525 }
526 }
527 }
528 return $categorytree;
529 }
530
bc649d80 531 /**
532 * Sets the viable parents
533 *
534 * Viable parents are any except for the category itself, or any of it's descendants
535 * The parentstrings parameter is passed by reference and changed by this function.
536 *
537 * @param array parentstrings a list of parentstrings
538 * @param object category
539 */
516cf3eb 540 function set_viable_parents(&$parentstrings, $category) {
541
542 unset($parentstrings[$category->id]);
543 if (isset($category->children)) {
544 foreach ($category->children as $child) {
545 $this->set_viable_parents($parentstrings, $child);
546 }
547 }
548 }
549
bc649d80 550 /**
551 * Gets question categories
552 *
553 * @param int parent - if given, restrict records to those with this parent id.
554 * @param string sort - [[sortfield [,sortfield]] {ASC|DESC}]
555 * @return array categories
556 */
dc1f00de 557 function get_question_categories($parent=null, $sort="sortorder ASC") {
516cf3eb 558
559 if (is_null($parent)) {
dc1f00de 560 $categories = get_records('question_categories', 'course', "{$this->course->id}", $sort);
516cf3eb 561 } else {
562 $select = "parent = '$parent' AND course = '{$this->course->id}'";
dc1f00de 563 $categories = get_records_select('question_categories', $select, $sort);
516cf3eb 564 }
565 return $categories;
566 }
567
bc649d80 568 /**
569 * Deletes an existing question category
570 *
571 * @param int deletecat id of category to delete
572 * @param int destcategoryid id of category which will inherit the orphans of deletecat
573 */
516cf3eb 574 function delete_category($deletecat, $destcategoryid = null) {
575 global $USER;
576
dc1f00de 577 if (!$category = get_record("question_categories", "id", $deletecat)) { // security
516cf3eb 578 error("No such category $deletecat!", "category.php?id={$this->course->id}");
579 }
580
581 if (!is_null($destcategoryid)) { // Need to move some questions before deleting the category
dc1f00de 582 if (!$category2 = get_record("question_categories", "id", $destcategoryid)) { // security
516cf3eb 583 error("No such category $destcategoryid!", "category.php?id={$this->course->id}");
584 }
4abc23b2 585 if (! set_field('question', 'category', $destcategoryid, 'category', $deletecat)) {
516cf3eb 586 error("Error while moving questions from category '$category->name' to '$category2->name'", "category.php?id={$this->course->id}");
587 }
588
589 } else {
590 // todo: delete any hidden questions that are not actually in use any more
4f48fb42 591 if ($count = count_records("question", "category", $category->id)) {
4abc23b2 592 $vars = new stdClass;
516cf3eb 593 $vars->name = $category->name;
594 $vars->count = $count;
595 print_simple_box(get_string("categorymove", "quiz", $vars), "center");
596 $this->initialize();
597 $categorystrings = $this->categorystrings;
598 unset ($categorystrings[$category->id]);
599 echo "<p><div align=\"center\"><form action=\"category.php\" method=\"get\">";
09275894 600 echo '<fieldset class="invisiblefieldset">';
516cf3eb 601 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
602 echo "<input type=\"hidden\" name=\"id\" value=\"{$this->course->id}\" />";
603 echo "<input type=\"hidden\" name=\"delete\" value=\"$category->id\" />";
604 choose_from_menu($categorystrings, "confirm", "", "");
605 echo "<input type=\"submit\" value=\"". get_string("categorymoveto", "quiz") . "\" />";
606 echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->str->cancel}\" />";
09275894 607 echo '</fieldset>';
516cf3eb 608 echo "</form></div></p>";
609 print_footer($this->course);
610 exit;
611 }
612 }
516cf3eb 613
614 /// Send the children categories to live with their grandparent
dc1f00de 615 if ($childcats = get_records("question_categories", "parent", $category->id)) {
516cf3eb 616 foreach ($childcats as $childcat) {
dc1f00de 617 if (! set_field("question_categories", "parent", $category->parent, "id", $childcat->id)) {
516cf3eb 618 error("Could not update a child category!", "category.php?id={$this->course->id}");
619 }
620 }
621 }
622
623 /// Finally delete the category itself
dc1f00de 624 if (delete_records("question_categories", "id", $category->id)) {
bc649d80 625 notify(get_string("categorydeleted", "quiz", $category->name), 'notifysuccess');
516cf3eb 626 }
627 }
628
bc649d80 629 /**
630 * Moves a category up or down in the display order
631 *
632 * @param string direction up|down
633 * @param int categoryid id of category to move
634 */
516cf3eb 635 function move_category_up_down ($direction, $categoryid) {
636 /// Move a category up or down
637 $swapcategory = NULL;
638 $movecategory = NULL;
639
640 if ($direction == 'up') {
dc1f00de 641 if ($movecategory = get_record("question_categories", "id", $categoryid)) {
642 $categories = $this->get_question_categories("$movecategory->parent", 'parent, sortorder, name');
516cf3eb 643
644 foreach ($categories as $category) {
645 if ($category->id == $movecategory->id) {
646 break;
647 }
648 $swapcategory = $category;
649 }
650 }
651 }
652 if ($direction == 'down') {
dc1f00de 653 if ($movecategory = get_record("question_categories", "id", $categoryid)) {
654 $categories = $this->get_question_categories("$movecategory->parent", 'parent, sortorder, name');
516cf3eb 655 $choosenext = false;
656 foreach ($categories as $category) {
657 if ($choosenext) {
658 $swapcategory = $category;
659 break;
660 }
661 if ($category->id == $movecategory->id) {
662 $choosenext = true;
663 }
664 }
665 }
666 }
667 if ($swapcategory and $movecategory) { // Renumber everything for robustness
668 $count=0;
669 foreach ($categories as $category) {
670 $count++;
671 if ($category->id == $swapcategory->id) {
672 $category = $movecategory;
673 } else if ($category->id == $movecategory->id) {
674 $category = $swapcategory;
675 }
dc1f00de 676 if (! set_field("question_categories", "sortorder", $count, "id", $category->id)) {
516cf3eb 677 notify("Could not update that category!");
678 }
679 }
680 }
681 }
682
bc649d80 683 /**
684 * Changes the parent of a category
685 *
686 * @param int categoryid
687 * @param int parentid
688 */
516cf3eb 689 function move_category($categoryid, $parentid) {
690 /// Move a category to a new parent
691
dc1f00de 692 if ($tempcat = get_record("question_categories", "id", $categoryid)) {
516cf3eb 693 if ($tempcat->parent != $parentid) {
dc1f00de 694 if (! set_field("question_categories", "parent", $parentid, "id", $tempcat->id)) {
516cf3eb 695 notify("Could not update that category!");
696 }
697 }
698 }
699 }
700
bc649d80 701 /**
702 * Changes the published status of a category
703 *
704 * @param boolean publish
705 * @param int categoryid
706 */
516cf3eb 707 function publish_category($publish, $categoryid) {
708 /// Hide or publish a category
709
710 $publish = ($publish == false) ? 0 : 1;
dc1f00de 711 $tempcat = get_record("question_categories", "id", $categoryid);
516cf3eb 712 if ($tempcat) {
dc1f00de 713 if (! set_field("question_categories", "publish", $publish, "id", $tempcat->id)) {
516cf3eb 714 notify("Could not update that category!");
715 }
716 }
717 }
718
bc649d80 719 /**
720 * Creates a new category with given params
721 *
722 * @param int $newparent id of the parent category
723 * @param string $newcategory the name for the new category
724 * @param string $newinfo the info field for the new category
725 * @param int $newpublish whether to publish the category
726 * @param int $newcourse the id of the associated course
727 */
516cf3eb 728 function add_category($newparent, $newcategory, $newinfo, $newpublish, $newcourse) {
bc649d80 729 if (empty($newcategory)) {
730 notify(get_string('categorynamecantbeblank', 'quiz'), 'notifyproblem');
731 return false;
732 }
516cf3eb 733
734 if ($newparent) {
735 // first check that the parent category is in the correct course
dc1f00de 736 if(!(get_field('question_categories', 'course', 'id', $newparent) == $newcourse)) {
516cf3eb 737 return false;
738 }
739 }
740
741 $cat = NULL;
742 $cat->parent = $newparent;
743 $cat->name = $newcategory;
744 $cat->info = $newinfo;
745 $cat->publish = $newpublish;
746 $cat->course = $newcourse;
747 $cat->sortorder = 999;
748 $cat->stamp = make_unique_id_code();
dc1f00de 749 if (!insert_record("question_categories", $cat)) {
f5565b69 750 error("Could not insert the new question category '$newcategory'", "category.php?id={$newcourse}");
516cf3eb 751 } else {
bc649d80 752 notify(get_string("categoryadded", "quiz", $newcategory), 'notifysuccess');
516cf3eb 753 }
516cf3eb 754 }
755
bc649d80 756 /**
757 * Updates an existing category with given params
758 *
759 * @param int updateid
760 * @param int updateparent
761 * @param string updatename
762 * @param string updateinfo
763 * @param int updatepublish
764 * @param int courseid the id of the associated course
765 */
516cf3eb 766 function update_category($updateid, $updateparent, $updatename, $updateinfo, $updatepublish, $courseid) {
bc649d80 767 if (empty($updatename)) {
768 notify(get_string('categorynamecantbeblank', 'quiz'), 'notifyproblem');
769 return false;
770 }
516cf3eb 771
772 $cat = NULL;
773 $cat->id = $updateid;
774 $cat->parent = $updateparent;
775 $cat->name = $updatename;
776 $cat->info = $updateinfo;
777 $cat->publish = $updatepublish;
dc1f00de 778 if (!update_record("question_categories", $cat)) {
516cf3eb 779 error("Could not update the category '$updatename'", "category.php?id={$courseid}");
780 } else {
bc649d80 781 notify(get_string("categoryupdated", 'quiz'), 'notifysuccess');
516cf3eb 782 }
783 }
516cf3eb 784}
785
786?>