3 * Allows a teacher to create, edit and delete categories
5 * @author Martin Dougiamas and many others.
6 * {@link http://moodle.org}
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package questionbank
11 require_once("../config.php");
12 require_once($CFG->dirroot
."/question/editlib.php");
13 require_once($CFG->dirroot
."/question/category_class.php");
15 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
16 question_edit_setup('categories', '/question/category.php');
18 // get values from form for actions on this page
19 $param = new stdClass();
20 $param->moveup
= optional_param('moveup', 0, PARAM_INT
);
21 $param->movedown
= optional_param('movedown', 0, PARAM_INT
);
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
);
25 $param->left
= optional_param('left', 0, PARAM_INT
);
26 $param->right
= optional_param('right', 0, PARAM_INT
);
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
);
32 $param->edit
= optional_param('edit', 0, PARAM_INT
);
34 $url = new moodle_url($thispageurl);
35 foreach ((array)$param as $key=>$value) {
36 if (($key !== 'cancel' && $value !== 0) ||
($key === 'cancel' && $value !== '')) {
37 $url->param($key, $value);
41 $PAGE->set_pagelayout('standard');
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'));
46 $streditingcategories = get_string('editcategories', 'quiz');
47 if ($param->left ||
$param->right ||
$param->moveup ||
$param->movedown||
$param->moveupcontext ||
$param->movedowncontext
){
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
);
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
57 print_error('nocate', 'question', $thispageurl->out(), $param->delete
);
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()){
63 redirect($thispageurl);
64 } elseif ($formdata = $qcobject->moveform
->get_data()) {
65 /// 'confirm' is the category to move existing questions to
66 list($tocategoryid, $tocontextid) = explode(',', $formdata->category
);
67 $qcobject->move_questions_and_delete_category($formdata->delete
, $tocategoryid);
68 $thispageurl->remove_params('cat', 'category');
69 redirect($thispageurl);
74 if ($qcobject->catform
->is_cancelled()) {
75 redirect($thispageurl);
76 } else if ($catformdata = $qcobject->catform
->get_data()) {
77 if (!$catformdata->id
) {//new category
78 $qcobject->add_category($catformdata->parent
, $catformdata->name
, $catformdata->info
);
80 $qcobject->update_category($catformdata->id
, $catformdata->parent
, $catformdata->name
, $catformdata->info
);
82 redirect($thispageurl);
83 } else if ((!empty($param->delete
) and (!$questionstomove) and confirm_sesskey())) {
84 $qcobject->delete_category($param->delete
);//delete the category now no questions to move
85 $thispageurl->remove_params('cat', 'category');
86 redirect($thispageurl);
90 $PAGE->navbar
->add(get_string('editingcategory', 'question'));
93 $PAGE->set_title($streditingcategories);
94 $PAGE->set_heading($COURSE->fullname
);
95 echo $OUTPUT->header();
98 if (!empty($param->edit
)) {
99 $qcobject->edit_single_category($param->edit
);
100 } else if ($questionstomove){
101 $qcobject->display_move_form($questionstomove, $category);
103 // display the user interface
104 $qcobject->display_user_interface();
106 echo $OUTPUT->footer();