MDL-9115 Added new strings to lang/en_utf8/group.php (where in roles.php before!...
[moodle-pu.git] / question / category_class.php
blob49ce7af4a68315705d5ee83ee00f8c8288f208a0
1 <?php // $Id$
2 /**
3 * Class representing question categories
5 * @author Martin Dougiamas and many others. {@link http://moodle.org}
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package questionbank
8 */
10 // number of categories to display on page
11 define( "PAGE_LENGTH",25 );
13 /**
14 * Class representing question categories
16 * @package questionbank
18 class question_category_object {
20 var $str;
21 var $pixpath;
22 var $edittable;
23 var $newtable;
24 var $tab;
25 var $tabsize = 3;
26 var $categories;
27 var $categorystrings;
28 var $defaultcategory;
29 var $course;
30 var $topcount;
32 /**
33 * Constructor
35 * Gets necessary strings and sets relevant path information
37 function question_category_object() {
38 global $CFG;
40 $this->tab = str_repeat('&nbsp;', $this->tabsize);
42 $this->str->course = get_string('course');
43 $this->str->category = get_string('category', 'quiz');
44 $this->str->categoryinfo = get_string('categoryinfo', 'quiz');
45 $this->str->questions = get_string('questions', 'quiz');
46 $this->str->add = get_string('add');
47 $this->str->delete = get_string('delete');
48 $this->str->moveup = get_string('moveup');
49 $this->str->movedown = get_string('movedown');
50 $this->str->edit = get_string('editthiscategory');
51 $this->str->hide = get_string('hide');
52 $this->str->publish = get_string('publish', 'quiz');
53 $this->str->order = get_string('order');
54 $this->str->parent = get_string('parent', 'quiz');
55 $this->str->add = get_string('add');
56 $this->str->action = get_string('action');
57 $this->str->top = get_string('top', 'quiz');
58 $this->str->addcategory = get_string('addcategory', 'quiz');
59 $this->str->editcategory = get_string('editcategory', 'quiz');
60 $this->str->cancel = get_string('cancel');
61 $this->str->editcategories = get_string('editcategories', 'quiz');
62 $this->str->page = get_string('page');
63 $this->pixpath = $CFG->pixpath;
67 /**
68 * Sets the course for this object
70 * @param object course
72 function set_course($course) {
73 $this->course = $course;
76 /**
77 * Displays the user interface
79 * @param object modform
80 * @param int $page page number to display (0=don't paginate)
82 function display_user_interface($page=0) {
83 $this->initialize();
85 /// Interface for adding a new category:
86 print_heading_with_help($this->str->addcategory, 'categories_edit', 'quiz');
87 $this->output_new_table();
88 echo '<br />';
90 /// Interface for editing existing categories
91 print_heading_with_help($this->str->editcategories, 'categories', 'quiz');
92 $this->output_edit_table($page);
93 if ($this->topcount>PAGE_LENGTH) {
94 $this->display_page_numbers($page);
96 echo '<br />';
102 * Initializes this classes general category-related variables
104 function initialize() {
106 /// Get the existing categories
107 if (!$this->defaultcategory = get_default_question_category($this->course->id)) {
108 error("Error: Could not find or make a category!");
111 $this->categories = $this->get_question_categories(null, "parent, sortorder, name ASC");
113 $this->categories = $this->arrange_categories($this->categories);
115 // create the array of id=>full_name strings
116 $this->categorystrings = $this->expanded_category_strings($this->categories);
118 // for pagination calculate number of 'top' categories and hence number of pages
119 // (pagination only based on top categories)
120 $count = 0;
121 foreach( $this->categories as $category ) {
122 if ($category->parent==0) {
123 ++$count;
126 $this->topcount = $count;
127 $this->pagecount = (integer) ceil( $count / PAGE_LENGTH );
131 * display list of page numbers for navigation
133 function display_page_numbers( $page=0 ) {
134 global $USER;
136 echo "<div class=\"paging\">{$this->str->page}:\n";
137 foreach (range(1,$this->pagecount) as $currentpage) {
138 if ($page == $currentpage) {
139 echo " $currentpage \n";
141 else {
142 echo "<a href=\"category.php?id={$this->course->id}&amp;page=$currentpage&amp;sesskey={$USER->sesskey}\">";
143 echo " $currentpage </a>\n";
146 echo "</div>";
150 * Outputs a table to allow entry of a new category
152 function output_new_table() {
153 global $USER;
154 $publishoptions[0] = get_string("no");
155 $publishoptions[1] = get_string("yes");
157 $this->newtable->head = array ($this->str->parent, $this->str->category, $this->str->categoryinfo, $this->str->publish, $this->str->action);
158 $this->newtable->width = 200;
159 $this->newtable->data[] = array();
160 $this->newtable->tablealign = 'center';
162 /// Each section below adds a data cell to the table row
165 $viableparents[0] = $this->str->top;
166 $viableparents = $viableparents + $this->categorystrings;
167 $this->newtable->align['parent'] = "left";
168 $this->newtable->wrap['parent'] = "nowrap";
169 $row['parent'] = choose_from_menu ($viableparents, "newparent", $this->str->top, "", "", "", true);
171 $this->newtable->align['category'] = "left";
172 $this->newtable->wrap['category'] = "nowrap";
173 $row['category'] = '<input type="text" name="newcategory" value="" size="15" />';
175 $this->newtable->align['info'] = "left";
176 $this->newtable->wrap['info'] = "nowrap";
177 $row['info'] = '<input type="text" name="newinfo" value="" size="50" />';
179 $this->newtable->align['publish'] = "left";
180 $this->newtable->wrap['publish'] = "nowrap";
181 $row['publish'] = choose_from_menu ($publishoptions, "newpublish", "", "", "", "", true);
183 $this->newtable->align['action'] = "left";
184 $this->newtable->wrap['action'] = "nowrap";
185 $row['action'] = '<input type="submit" value="' . $this->str->add . '" />';
188 $this->newtable->data[] = $row;
190 // wrap the table in a form and output it
191 echo '<form action="category.php" method="post">';
192 echo '<fieldset class="invisiblefieldset" style="display: block">';
193 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
194 echo '<input type="hidden" name="id" value="'. $this->course->id . '" />';
195 echo '<input type="hidden" name="addcategory" value="true" />';
196 print_table($this->newtable);
197 echo '</fieldset>';
198 echo '</form>';
202 * Outputs a table to allow editing/rearranging of existing categories
204 * $this->initialize() must have already been called
206 * @param object course
207 * @param int $page page to display (0=do not paginate)
209 function output_edit_table($page=0) {
210 $this->edittable->head = array ($this->str->category, $this->str->categoryinfo, $this->str->questions, $this->str->publish,
211 $this->str->delete, $this->str->order, $this->str->parent);
212 $this->edittable->width = 200;
213 $this->edittable->tablealign = 'center';
215 $courses = $this->course->shortname;
217 // if pagination required work out range
218 if (!empty($page)) {
219 $firstcat = ($page-1) * PAGE_LENGTH + 1;
220 $lastcat = $firstcat + PAGE_LENGTH - 1;
222 else {
223 $firstcat = 1;
224 $lastcat = $this->topcount;
226 //echo "$firstcat $lastcat $page"; die;
227 $this->build_edit_table_body($this->categories, $page, $firstcat, $lastcat);
228 print_table($this->edittable);
232 * Recursively builds up the edit-categories table body
234 * @param array categories contains category objects in a tree representation
235 * @param mixed courses String with shortname of course | array containing courseid=>shortname
236 * @param int depth controls the indenting
238 function build_edit_table_body($categories, $page = 0, $firstcat = 1, $lastcat = 99999, $depth = 0) {
239 $countcats = count($categories);
240 $count = 0;
241 $first = true;
242 $last = false;
243 $topcount = 0;
245 foreach ($categories as $category) {
246 $count++;
247 if ($count == $countcats) {
248 $last = true;
250 // check if this category is on the display page
251 if ($depth==0) {
252 $topcount++;
253 if (($topcount<$firstcat) or ($topcount>$lastcat)) {
254 continue;
257 $up = $first ? false : true;
258 $down = $last ? false : true;
259 $first = false;
260 $this->edit_question_category_row($category, $depth, $up, $down, $page);
261 if (isset($category->children)) {
262 $this->build_edit_table_body($category->children, $page, $firstcat, $lastcat, $depth + 1);
268 * gets all the courseids for the given categories
270 * @param array categories contains category objects in a tree representation
271 * @return array courseids flat array in form categoryid=>courseid
273 function get_course_ids($categories) {
274 $courseids = array();
275 foreach ($categories as $key=>$cat) {
276 $courseids[$key] = $cat->course;
277 if (!empty($cat->children)) {
278 $courseids = array_merge($courseids, $this->get_course_ids($cat->children));
281 return $courseids;
285 * Constructs each row of the edit-categories table
287 * @param object category
288 * @param int depth controls the indenting
289 * @param string shortname short name of the course
290 * @param boolean up can it be moved up?
291 * @param boolean down can it be moved down?
292 * @param int page page number
294 function edit_question_category_row($category, $depth, $up = false, $down = false, $page = 0) {
295 global $USER;
296 $fill = str_repeat($this->tab, $depth);
298 $linkcss = $category->publish ? ' class="published" ' : ' class="unpublished" ';
300 if (!empty($page)) {
301 $pagelink="&amp;page=$page";
303 else {
304 $pagelink="";
307 /// Each section below adds a data cell to this table row
309 $this->edittable->align["$category->id.name"] = "left";
310 $this->edittable->wrap["$category->id.name"] = "nowrap";
311 $row["$category->id.name"] = '<a ' . $linkcss . ' title="' . $this->str->edit. '" href="category.php?id=' . $this->course->id .
312 '&amp;edit=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/edit.gif" class="iconsmall"
313 alt="' .$this->str->edit. '" /> ' . $fill . $category->name . '</a>';
315 $this->edittable->align["$category->id.info"] = "left";
316 $this->edittable->wrap["$category->id.info"] = "nowrap";
317 $row["$category->id.info"] = '<a ' . $linkcss . ' title="' . $this->str->edit .'" href="category.php?id=' . $this->course->id .
318 '&amp;edit=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'">' . $category->info . '</a>';
320 $this->edittable->align["$category->id.qcount"] = "center";
321 $row["$category->id.qcount"] = $category->questioncount;
323 $this->edittable->align["$category->id.publish"] = "center";
324 $this->edittable->wrap["$category->id.publish"] = "nowrap";
325 if (!empty($category->publish)) {
326 $row["$category->id.publish"] = '<a title="' . $this->str->hide . '" href="category.php?id=' . $this->course->id . '&amp;hide=' . $category->id .
327 '&amp;sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/hide.gif" class="iconsmall" alt="' .$this->str->hide. '" /></a> ';
328 } else {
329 $row["$category->id.publish"] = '<a title="' . $this->str->publish . '" href="category.php?id=' . $this->course->id . '&amp;publish=' . $category->id .
330 '&amp;sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/show.gif" class="iconsmall" alt="' .$this->str->publish. '" /></a> ';
333 if ($category->id != $this->defaultcategory->id) {
334 $this->edittable->align["$category->id.delete"] = "center";
335 $this->edittable->wrap["$category->id.delete"] = "nowrap";
336 $row["$category->id.delete"] = '<a title="' . $this->str->delete . '" href="category.php?id=' . $this->course->id .
337 '&amp;delete=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'"><img src="' . $this->pixpath . '/t/delete.gif" class="iconsmall" alt="' .$this->str->delete. '" /></a> ';
338 } else {
339 $row["$category->id.delete"] = '';
342 $this->edittable->align["$category->id.order"] = "left";
343 $this->edittable->wrap["$category->id.order"] = "nowrap";
344 $icons = '';
345 if ($up) {
346 $icons .= '<a title="' . $this->str->moveup .'" href="category.php?id=' . $this->course->id . '&amp;moveup=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'">
347 <img src="' . $this->pixpath . '/t/up.gif" class="iconsmall" alt="' . $this->str->moveup. '" /></a> ';
349 if ($down) {
350 $icons .= '<a title="' . $this->str->movedown .'" href="category.php?id=' . $this->course->id . '&amp;movedown=' . $category->id . '&amp;sesskey='.$USER->sesskey.$pagelink.'">
351 <img src="' . $this->pixpath . '/t/down.gif" class="iconsmall" alt="' .$this->str->movedown. '" /></a> ';
353 $row["$category->id.order"]= $icons;
355 $this->edittable->align["$category->id.moveto"] = "left";
356 $this->edittable->wrap["$category->id.moveto"] = "nowrap";
357 if ($category->id != $this->defaultcategory->id) {
358 $viableparents = $this->categorystrings;
359 $this->set_viable_parents($viableparents, $category);
360 $viableparents = array(0=>$this->str->top) + $viableparents;
362 $row["$category->id.moveto"] = popup_form ("category.php?id={$this->course->id}&amp;move={$category->id}&amp;sesskey=$USER->sesskey$pagelink&amp;moveto=",
363 $viableparents, "moveform{$category->id}", "$category->parent", "", "", "", true);
364 } else {
365 $row["$category->id.moveto"]='---';
369 $this->edittable->data[$category->id] = $row;
373 function edit_single_category($categoryid,$page=1) {
374 /// Interface for adding a new category
375 global $USER;
376 $this->initialize();
378 /// Interface for editing existing categories
379 if ($category = get_record("question_categories", "id", $categoryid)) {
380 echo '<h2 align="center">';
381 echo $this->str->edit;
382 helpbutton("categories_edit", $this->str->editcategory, "quiz");
383 echo '</h2>';
384 echo '<table width="100%"><tr><td>';
385 $this->output_edit_single_table($category,$page);
386 echo '</td></tr></table>';
387 echo '<p><div align="center"><form action="category.php" method="get">
388 <div>
389 <input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />
390 <input type="hidden" name="id" value="' . $this->course->id . '" />
391 <input type="submit" value="' . $this->str->cancel . '" />
392 </div>
393 </form>
394 </div></p>';
395 print_footer($this->course);
396 exit;
397 } else {
398 error("Category $categoryid not found", "category.php?id={$this->course->id}");
403 * Outputs a table to allow editing of an existing category
405 * @param object category
406 * @param int page current page
408 function output_edit_single_table($category, $page=1) {
409 global $USER;
410 $publishoptions[0] = get_string("no");
411 $publishoptions[1] = get_string("yes");
412 $strupdate = get_string('update');
414 $edittable = new stdClass;
416 $edittable->head = array ($this->str->parent, $this->str->category, $this->str->categoryinfo, $this->str->publish, $this->str->action);
417 $edittable->width = 200;
418 $edittable->data[] = array();
419 $edittable->tablealign = 'center';
421 /// Each section below adds a data cell to the table row
423 $viableparents = $this->categorystrings;
424 $this->set_viable_parents($viableparents, $category);
425 $viableparents = array(0=>$this->str->top) + $viableparents;
426 $edittable->align['parent'] = "left";
427 $edittable->wrap['parent'] = "nowrap";
428 $row['parent'] = choose_from_menu ($viableparents, "updateparent", "{$category->parent}", "", "", "", true);
430 $edittable->align['category'] = "left";
431 $edittable->wrap['category'] = "nowrap";
432 $row['category'] = '<input type="text" name="updatename" value="' . format_string($category->name) . '" size="15" />';
434 $edittable->align['info'] = "left";
435 $edittable->wrap['info'] = "nowrap";
436 $row['info'] = '<input type="text" name="updateinfo" value="' . $category->info . '" size="50" />';
438 $edittable->align['publish'] = "left";
439 $edittable->wrap['publish'] = "nowrap";
440 $selected = (boolean)$category->publish ? 1 : 0;
441 $row['publish'] = choose_from_menu ($publishoptions, "updatepublish", $selected, "", "", "", true);
443 $edittable->align['action'] = "left";
444 $edittable->wrap['action'] = "nowrap";
445 $row['action'] = '<input type="submit" value="' . $strupdate . '" />';
447 $edittable->data[] = $row;
449 // wrap the table in a form and output it
450 echo '<p><form action="category.php" method="post">';
451 echo '<fieldset class="invisiblefieldset">';
452 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
453 echo '<input type="hidden" name="id" value="'. $this->course->id . '" />';
454 echo '<input type="hidden" name="updateid" value="' . $category->id . '" />';
455 echo "<input type=\"hidden\" name=\"page\" value=\"$page\" />";
456 print_table($edittable);
457 echo '</fieldset>';
458 echo '</form></p>';
462 * Creates an array of "full-path" category strings
463 * Structure:
464 * key => string
465 * where key is the category id, and string contains the name of all ancestors as well as the particular category name
466 * E.g. '123'=>'Language / English / Grammar / Modal Verbs"
468 * @param array $categories an array containing categories arranged in a tree structure
470 function expanded_category_strings($categories, $parent=null) {
471 $prefix = is_null($parent) ? '' : "$parent / ";
472 $categorystrings = array();
473 foreach ($categories as $key => $category) {
474 $expandedname = "$prefix$category->name";
475 $categorystrings[$key] = $expandedname;
476 if (isset($category->children)) {
477 $categorystrings = $categorystrings + $this->expanded_category_strings($category->children, $expandedname);
480 return $categorystrings;
484 * Arranges the categories into a hierarchical tree
486 * If a category has children, it's "children" property holds an array of children
487 * The questioncount for each category is also calculated
489 * @param array records a flat list of the categories
490 * @return array categorytree a hierarchical list of the categories
492 function arrange_categories($records) {
493 //TODO: get the question count for all records with one sql statement: select category, count(*) from question group by category
494 $levels = array();
496 // build a levels array, which places each record according to it's depth from the top level
497 $parents = array(0);
498 while (!empty($parents)) {
499 $children = array();
500 foreach ($records as $record) {
501 if (in_array($record->parent, $parents)) {
502 $children[] = $record->id;
505 if (!empty($children)) {
506 $levels[] = $children;
508 $parents = $children;
510 // if there is no hierarchy (e.g., if all records have parent == 0), set level[0] to these keys
511 if (empty($levels)) {
512 $levels[0] = array_keys($records);
515 // build a hierarchical array that depicts the parent-child relationships of the categories
516 $categorytree = array();
517 for ($index = count($levels) - 1; $index >= 0; $index--) {
518 foreach($levels[$index] as $key) {
519 $parentkey = $records[$key]->parent;
520 if (!($records[$key]->questioncount = count_records('question', 'category', $records[$key]->id, 'hidden', 0, 'parent', '0'))) {
521 $records[$key]->questioncount = 0;
523 if ($parentkey == 0) {
524 $categorytree[$key] = $records[$key];
525 } else {
526 $records[$parentkey]->children[$key] = $records[$key];
530 return $categorytree;
534 * Sets the viable parents
536 * Viable parents are any except for the category itself, or any of it's descendants
537 * The parentstrings parameter is passed by reference and changed by this function.
539 * @param array parentstrings a list of parentstrings
540 * @param object category
542 function set_viable_parents(&$parentstrings, $category) {
544 unset($parentstrings[$category->id]);
545 if (isset($category->children)) {
546 foreach ($category->children as $child) {
547 $this->set_viable_parents($parentstrings, $child);
553 * Gets question categories
555 * @param int parent - if given, restrict records to those with this parent id.
556 * @param string sort - [[sortfield [,sortfield]] {ASC|DESC}]
557 * @return array categories
559 function get_question_categories($parent=null, $sort="sortorder ASC") {
561 if (is_null($parent)) {
562 $categories = get_records('question_categories', 'course', "{$this->course->id}", $sort);
563 } else {
564 $select = "parent = '$parent' AND course = '{$this->course->id}'";
565 $categories = get_records_select('question_categories', $select, $sort);
567 return $categories;
571 * Deletes an existing question category
573 * @param int deletecat id of category to delete
574 * @param int destcategoryid id of category which will inherit the orphans of deletecat
576 function delete_category($deletecat, $destcategoryid = null) {
577 global $USER;
579 if (!$category = get_record("question_categories", "id", $deletecat)) { // security
580 error("No such category $deletecat!", "category.php?id={$this->course->id}");
583 if (!is_null($destcategoryid)) { // Need to move some questions before deleting the category
584 if (!$category2 = get_record("question_categories", "id", $destcategoryid)) { // security
585 error("No such category $destcategoryid!", "category.php?id={$this->course->id}");
587 if (! set_field('question', 'category', $destcategoryid, 'category', $deletecat)) {
588 error("Error while moving questions from category '" . format_string($category->name) . "' to '$category2->name'", "category.php?id={$this->course->id}");
591 } else {
592 // todo: delete any hidden questions that are not actually in use any more
593 if ($count = count_records("question", "category", $category->id)) {
594 $vars = new stdClass;
595 $vars->name = $category->name;
596 $vars->count = $count;
597 print_simple_box(get_string("categorymove", "quiz", $vars), "center");
598 $this->initialize();
599 $categorystrings = $this->categorystrings;
600 unset ($categorystrings[$category->id]);
601 echo "<p><div align=\"center\"><form action=\"category.php\" method=\"get\">";
602 echo '<fieldset class="invisiblefieldset">';
603 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
604 echo "<input type=\"hidden\" name=\"id\" value=\"{$this->course->id}\" />";
605 echo "<input type=\"hidden\" name=\"delete\" value=\"$category->id\" />";
606 choose_from_menu($categorystrings, "confirm", "", "");
607 echo "<input type=\"submit\" value=\"". get_string("categorymoveto", "quiz") . "\" />";
608 echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->str->cancel}\" />";
609 echo '</fieldset>';
610 echo "</form></div></p>";
611 print_footer($this->course);
612 exit;
616 /// Send the children categories to live with their grandparent
617 if ($childcats = get_records("question_categories", "parent", $category->id)) {
618 foreach ($childcats as $childcat) {
619 if (! set_field("question_categories", "parent", $category->parent, "id", $childcat->id)) {
620 error("Could not update a child category!", "category.php?id={$this->course->id}");
625 /// Finally delete the category itself
626 if (delete_records("question_categories", "id", $category->id)) {
627 notify(get_string("categorydeleted", "quiz", format_string($category->name)), 'notifysuccess');
632 * Moves a category up or down in the display order
634 * @param string direction up|down
635 * @param int categoryid id of category to move
637 function move_category_up_down ($direction, $categoryid) {
638 /// Move a category up or down
639 $swapcategory = NULL;
640 $movecategory = NULL;
642 if ($direction == 'up') {
643 if ($movecategory = get_record("question_categories", "id", $categoryid)) {
644 $categories = $this->get_question_categories("$movecategory->parent", 'parent, sortorder, name');
646 foreach ($categories as $category) {
647 if ($category->id == $movecategory->id) {
648 break;
650 $swapcategory = $category;
654 if ($direction == 'down') {
655 if ($movecategory = get_record("question_categories", "id", $categoryid)) {
656 $categories = $this->get_question_categories("$movecategory->parent", 'parent, sortorder, name');
657 $choosenext = false;
658 foreach ($categories as $category) {
659 if ($choosenext) {
660 $swapcategory = $category;
661 break;
663 if ($category->id == $movecategory->id) {
664 $choosenext = true;
669 if ($swapcategory and $movecategory) { // Renumber everything for robustness
670 $count=0;
671 foreach ($categories as $category) {
672 $count++;
673 if ($category->id == $swapcategory->id) {
674 $category = $movecategory;
675 } else if ($category->id == $movecategory->id) {
676 $category = $swapcategory;
678 if (! set_field("question_categories", "sortorder", $count, "id", $category->id)) {
679 notify("Could not update that category!");
686 * Changes the parent of a category
688 * @param int categoryid
689 * @param int parentid
691 function move_category($categoryid, $parentid) {
692 /// Move a category to a new parent
694 if ($tempcat = get_record("question_categories", "id", $categoryid)) {
695 if ($tempcat->parent != $parentid) {
696 if (! set_field("question_categories", "parent", $parentid, "id", $tempcat->id)) {
697 notify("Could not update that category!");
704 * Changes the published status of a category
706 * @param boolean publish
707 * @param int categoryid
709 function publish_category($publish, $categoryid) {
710 /// Hide or publish a category
712 $publish = ($publish == false) ? 0 : 1;
713 $tempcat = get_record("question_categories", "id", $categoryid);
714 if ($tempcat) {
715 if (! set_field("question_categories", "publish", $publish, "id", $tempcat->id)) {
716 notify("Could not update that category!");
722 * Creates a new category with given params
724 * @param int $newparent id of the parent category
725 * @param string $newcategory the name for the new category
726 * @param string $newinfo the info field for the new category
727 * @param int $newpublish whether to publish the category
728 * @param int $newcourse the id of the associated course
730 function add_category($newparent, $newcategory, $newinfo, $newpublish, $newcourse) {
731 if (empty($newcategory)) {
732 notify(get_string('categorynamecantbeblank', 'quiz'), 'notifyproblem');
733 return false;
736 if ($newparent) {
737 // first check that the parent category is in the correct course
738 if(!(get_field('question_categories', 'course', 'id', $newparent) == $newcourse)) {
739 return false;
743 $cat = NULL;
744 $cat->parent = $newparent;
745 $cat->name = $newcategory;
746 $cat->info = $newinfo;
747 $cat->publish = $newpublish;
748 $cat->course = $newcourse;
749 $cat->sortorder = 999;
750 $cat->stamp = make_unique_id_code();
751 if (!insert_record("question_categories", $cat)) {
752 error("Could not insert the new question category '$newcategory'", "category.php?id={$newcourse}");
753 } else {
754 notify(get_string("categoryadded", "quiz", $newcategory), 'notifysuccess');
759 * Updates an existing category with given params
761 * @param int updateid
762 * @param int updateparent
763 * @param string updatename
764 * @param string updateinfo
765 * @param int updatepublish
766 * @param int courseid the id of the associated course
768 function update_category($updateid, $updateparent, $updatename, $updateinfo, $updatepublish, $courseid) {
769 if (empty($updatename)) {
770 notify(get_string('categorynamecantbeblank', 'quiz'), 'notifyproblem');
771 return false;
774 $cat = NULL;
775 $cat->id = $updateid;
776 $cat->parent = $updateparent;
777 $cat->name = $updatename;
778 $cat->info = $updateinfo;
779 $cat->publish = $updatepublish;
780 if (!update_record("question_categories", $cat)) {
781 error("Could not update the category '$updatename'", "category.php?id={$courseid}");
782 } else {
783 notify(get_string("categoryupdated", 'quiz'), 'notifysuccess');