MDL-20636 Fix new stdClass -> stdClass(), and trailing whitespace.
[moodle.git] / question / category.php
CommitLineData
aeb15530 1<?php
516cf3eb 2/**
bc649d80 3 * Allows a teacher to create, edit and delete categories
4 *
bc649d80 5 * @author Martin Dougiamas and many others.
6 * {@link http://moodle.org}
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
4323d029 8 * @package questionbank
bc649d80 9 */
516cf3eb 10
e586cfb4 11 require_once("../config.php");
271e6dec 12 require_once($CFG->dirroot."/question/editlib.php");
13 require_once($CFG->dirroot."/question/category_class.php");
516cf3eb 14
453b28d8
TH
15 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
16 question_edit_setup('categories', '/question/category.php');
b72ff476 17
18 // get values from form for actions on this page
516cf3eb 19 $param = new stdClass();
dac786f3 20 $param->moveup = optional_param('moveup', 0, PARAM_INT);
21 $param->movedown = optional_param('movedown', 0, PARAM_INT);
271e6dec 22 $param->moveupcontext = optional_param('moveupcontext', 0, PARAM_INT);
23 $param->movedowncontext = optional_param('movedowncontext', 0, PARAM_INT);
24 $param->tocontext = optional_param('tocontext', 0, PARAM_INT);
dac786f3 25 $param->left = optional_param('left', 0, PARAM_INT);
26 $param->right = optional_param('right', 0, PARAM_INT);
dac786f3 27 $param->delete = optional_param('delete', 0, PARAM_INT);
28 $param->confirm = optional_param('confirm', 0, PARAM_INT);
29 $param->cancel = optional_param('cancel', '', PARAM_ALPHA);
30 $param->move = optional_param('move', 0, PARAM_INT);
31 $param->moveto = optional_param('moveto', 0, PARAM_INT);
dac786f3 32 $param->edit = optional_param('edit', 0, PARAM_INT);
dac786f3 33
a6855934 34 $url = new moodle_url($thispageurl);
7f093351 35 foreach ((array)$param as $key=>$value) {
36 if (($key !== 'cancel' && $value !== 0) || ($key === 'cancel' && $value !== '')) {
37 $url->param($key, $value);
38 }
39 }
40 $PAGE->set_url($url);
56ed242b 41 $PAGE->set_pagelayout('standard');
e2c80339 42
271e6dec 43 $qcobject = new question_category_object($pagevars['cpage'], $thispageurl, $contexts->having_one_edit_tab_cap('categories'), $param->edit, $pagevars['cat'], $param->delete,
44 $contexts->having_cap('moodle/question:add'));
516cf3eb 45
b72ff476 46 $streditingcategories = get_string('editcategories', 'quiz');
271e6dec 47 if ($param->left || $param->right || $param->moveup || $param->movedown|| $param->moveupcontext || $param->movedowncontext){
81d1f091 48 require_sesskey();
271e6dec 49 foreach ($qcobject->editlists as $list){
50 //processing of these actions is handled in the method where appropriate and page redirects.
51 $list->process_actions($param->left, $param->right, $param->moveup, $param->movedown,
52 $param->moveupcontext, $param->movedowncontext, $param->tocontext);
53 }
54 }
f34488b2 55 if ($param->delete && ($questionstomove = $DB->count_records("question", array("category" => $param->delete)))){
56 if (!$category = $DB->get_record("question_categories", array("id" => $param->delete))) { // security
0be2c858 57 print_error('nocate', 'question', $thispageurl->out(), $param->delete);
271e6dec 58 }
59 $categorycontext = get_context_instance_by_id($category->contextid);
60 $qcobject->moveform = new question_move_form($thispageurl,
61 array('contexts'=>array($categorycontext), 'currentcat'=>$param->delete));
62 if ($qcobject->moveform->is_cancelled()){
e42f153c 63 redirect($thispageurl);
271e6dec 64 } elseif ($formdata = $qcobject->moveform->get_data()) {
65 /// 'confirm' is the category to move existing questions to
767fac29 66 list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
67 $qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid);
21cbf9da 68 $thispageurl->remove_params('cat', 'category');
e42f153c 69 redirect($thispageurl);
271e6dec 70 }
71 } else {
72 $questionstomove = 0;
73 }
21cbf9da 74 if ($qcobject->catform->is_cancelled()) {
e42f153c 75 redirect($thispageurl);
21cbf9da 76 } else if ($catformdata = $qcobject->catform->get_data()) {
271e6dec 77 if (!$catformdata->id) {//new category
78 $qcobject->add_category($catformdata->parent, $catformdata->name, $catformdata->info);
79 } else {
80 $qcobject->update_category($catformdata->id, $catformdata->parent, $catformdata->name, $catformdata->info);
81 }
e42f153c 82 redirect($thispageurl);
21cbf9da 83 } else if ((!empty($param->delete) and (!$questionstomove) and confirm_sesskey())) {
271e6dec 84 $qcobject->delete_category($param->delete);//delete the category now no questions to move
21cbf9da 85 $thispageurl->remove_params('cat', 'category');
e42f153c 86 redirect($thispageurl);
271e6dec 87 }
271e6dec 88
56ed242b 89 if ($param->edit){
d412354c 90 $PAGE->navbar->add(get_string('editingcategory', 'question'));
271e6dec 91 }
b72ff476 92
d412354c 93 $PAGE->set_title($streditingcategories);
0f56fde2 94 $PAGE->set_heading($COURSE->fullname);
d412354c 95 echo $OUTPUT->header();
b72ff476 96
271e6dec 97 // display UI
98 if (!empty($param->edit)) {
99 $qcobject->edit_single_category($param->edit);
100 } else if ($questionstomove){
101 $qcobject->display_move_form($questionstomove, $category);
102 } else {
103 // display the user interface
104 $qcobject->display_user_interface();
516cf3eb 105 }
9b59580b 106 echo $OUTPUT->footer();
aeb15530 107