/** * Test the question category created event. */ public function test_question_category_created() { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); $contexts = new question_edit_contexts(context_module::instance($quiz->cmid)); $defaultcategoryobj = question_make_default_categories(array($contexts->lowest())); $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid; $qcobject = new question_category_object(1, new moodle_url('/mod/quiz/edit.php', array('cmid' => $quiz->cmid)), $contexts->having_one_edit_tab_cap('categories'), $defaultcategoryobj->id, $defaultcategory, null, $contexts->having_cap('moodle/question:add')); // Trigger and capture the event. $sink = $this->redirectEvents(); $categoryid = $qcobject->add_category($defaultcategory, 'newcategory', '', true); $events = $sink->get_events(); $event = reset($events); // Check that the event data is valid. $this->assertInstanceOf('\\core\\event\\question_category_created', $event); $this->assertEquals(context_module::instance($quiz->cmid), $event->get_context()); $expected = array($course->id, 'quiz', 'addcategory', 'view.php?id=' . $quiz->cmid, $categoryid, $quiz->cmid); $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); }
require_capability('mod/quiz:manage', $contexts->lowest()); if (!$contexts->having_cap('moodle/question:useall')) { print_error('nopermissions', '', '', 'use'); } $PAGE->set_url($thispageurl); if ($returnurl) { $returnurl = new moodle_url($returnurl); } else { $returnurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cmid)); } if ($scrollpos) { $returnurl->param('scrollpos', $scrollpos); } $defaultcategoryobj = question_make_default_categories($contexts->all()); $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid; $qcobject = new question_category_object($pagevars['cpage'], $thispageurl, $contexts->having_one_edit_tab_cap('categories'), $defaultcategoryobj->id, $defaultcategory, null, $contexts->having_cap('moodle/question:add')); $mform = new quiz_add_random_form(new moodle_url('/mod/quiz/addrandom.php'), $contexts); if ($mform->is_cancelled()) { redirect($returnurl); } if ($data = $mform->get_data()) { if (!empty($data->existingcategory)) { list($categoryid) = explode(',', $data->category); $includesubcategories = !empty($data->includesubcategories); $returnurl->param('cat', $data->category); } else { if (!empty($data->newcategory)) { list($parentid, $contextid) = explode(',', $data->parent); $categoryid = $qcobject->add_category($data->parent, $data->name, '', true); $includesubcategories = 0; add_to_log($quiz->course, 'quiz', 'addcategory', 'view.php?id=' . $cm->id, $categoryid, $cm->id);
// get values from form for actions on this page $param = new stdClass(); $param->moveup = optional_param('moveup', 0, PARAM_INT); $param->movedown = optional_param('movedown', 0, PARAM_INT); $param->moveupcontext = optional_param('moveupcontext', 0, PARAM_INT); $param->movedowncontext = optional_param('movedowncontext', 0, PARAM_INT); $param->tocontext = optional_param('tocontext', 0, PARAM_INT); $param->left = optional_param('left', 0, PARAM_INT); $param->right = optional_param('right', 0, PARAM_INT); $param->delete = optional_param('delete', 0, PARAM_INT); $param->confirm = optional_param('confirm', 0, PARAM_INT); $param->cancel = optional_param('cancel', '', PARAM_ALPHA); $param->move = optional_param('move', 0, PARAM_INT); $param->moveto = optional_param('moveto', 0, PARAM_INT); $param->edit = optional_param('edit', 0, PARAM_INT); $qcobject = new question_category_object($pagevars['cpage'], $thispageurl, $contexts->having_one_edit_tab_cap('categories'), $param->edit, $pagevars['cat'], $param->delete, $contexts->having_cap('moodle/question:add')); $streditingcategories = get_string('editcategories', 'quiz'); if ($param->left || $param->right || $param->moveup || $param->movedown || $param->moveupcontext || $param->movedowncontext) { confirm_sesskey(); foreach ($qcobject->editlists as $list) { //processing of these actions is handled in the method where appropriate and page redirects. $list->process_actions($param->left, $param->right, $param->moveup, $param->movedown, $param->moveupcontext, $param->movedowncontext, $param->tocontext); } } if ($param->delete && ($questionstomove = count_records("question", "category", $param->delete))) { if (!($category = get_record("question_categories", "id", $param->delete))) { // security error("No such category {$param->delete}!", $thispageurl->out()); } $categorycontext = get_context_instance_by_id($category->contextid); $qcobject->moveform = new question_move_form($thispageurl, array('contexts' => array($categorycontext), 'currentcat' => $param->delete));
// get values from form for actions on this page $param = new stdClass(); $param->moveup = optional_param('moveup', 0, PARAM_INT); $param->movedown = optional_param('movedown', 0, PARAM_INT); $param->moveupcontext = optional_param('moveupcontext', 0, PARAM_INT); $param->movedowncontext = optional_param('movedowncontext', 0, PARAM_INT); $param->tocontext = optional_param('tocontext', 0, PARAM_INT); $param->left = optional_param('left', 0, PARAM_INT); $param->right = optional_param('right', 0, PARAM_INT); $param->delete = optional_param('delete', 0, PARAM_INT); $param->confirm = optional_param('confirm', 0, PARAM_INT); $param->cancel = optional_param('cancel', '', PARAM_ALPHA); $param->move = optional_param('move', 0, PARAM_INT); $param->moveto = optional_param('moveto', 0, PARAM_INT); $param->edit = optional_param('edit', 0, PARAM_INT); $qcobject = new question_category_object($pagevars['cpage'], $thispageurl, $contexts->having_one_edit_tab_cap('categories'), $param->edit, $pagevars['cat'], $param->delete, $contexts->having_cap('moodle/question:add')); $streditingcategories = get_string('editcategories', 'quiz'); if ($param->left || $param->right || $param->moveup || $param->movedown || $param->moveupcontext || $param->movedowncontext) { confirm_sesskey(); foreach ($qcobject->editlists as $list) { //processing of these actions is handled in the method where appropriate and page redirects. $list->process_actions($param->left, $param->right, $param->moveup, $param->movedown, $param->moveupcontext, $param->movedowncontext, $param->tocontext); } } if ($param->delete && ($questionstomove = $DB->count_records("question", array("category" => $param->delete)))) { if (!($category = $DB->get_record("question_categories", array("id" => $param->delete)))) { // security print_error('nocate', 'question', $thispageurl->out(), $param->delete); } $categorycontext = get_context_instance_by_id($category->contextid); $qcobject->moveform = new question_move_form($thispageurl, array('contexts' => array($categorycontext), 'currentcat' => $param->delete));
$param->confirm = optional_param('confirm', 0, PARAM_INT); $param->cancel = optional_param('cancel', '', PARAM_ALPHA); $param->move = optional_param('move', 0, PARAM_INT); $param->moveto = optional_param('moveto', 0, PARAM_INT); $param->publish = optional_param('publish', 0, PARAM_INT); $param->addcategory = optional_param('addcategory', '', PARAM_NOTAGS); $param->edit = optional_param('edit', 0, PARAM_INT); $param->updateid = optional_param('updateid', 0, PARAM_INT); $param->page = optional_param('page', 1, PARAM_INT); if (!($course = get_record("course", "id", $id))) { error("Course ID is incorrect"); } $context = get_context_instance(CONTEXT_COURSE, $id); require_login($course->id, false); require_capability('moodle/question:managecategory', $context); $qcobject = new question_category_object(); $qcobject->set_course($course); // Page header // TODO: generalise this to any activity if (isset($SESSION->modform->instance) and $quiz = get_record('quiz', 'id', $SESSION->modform->instance)) { $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id)) ? update_module_button($SESSION->modform->cmid, $course->id, get_string('modulename', 'quiz')) : ""; print_header_simple(get_string('editcategories', 'quiz'), '', "<a href=\"{$CFG->wwwroot}/mod/quiz/index.php?id={$course->id}\">" . get_string('modulenameplural', 'quiz') . '</a>' . " -> <a href=\"{$CFG->wwwroot}/mod/quiz/view.php?q={$quiz->id}\">" . format_string($quiz->name) . '</a>' . ' -> ' . get_string('editcategories', 'quiz'), "", "", true, $strupdatemodule); $currenttab = 'edit'; $mode = 'categories'; include $CFG->dirroot . '/mod/quiz/tabs.php'; } else { print_header_simple(get_string('editcategories', 'quiz'), '', get_string('editcategories', 'quiz')); // print tabs $currenttab = 'categories'; include 'tabs.php'; }