/** * 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); }
//this page can be called without courseid or cmid in which case //we get the context from the category object. if ($contexts === null) { // need to get the course from the chosen category $contexts = new question_edit_contexts($categorycontext); $thiscontext = $contexts->lowest(); if ($thiscontext->contextlevel == CONTEXT_COURSE) { require_login($thiscontext->instanceid, false); } elseif ($thiscontext->contextlevel == CONTEXT_MODULE) { list($module, $cm) = get_module_from_cmid($thiscontext->instanceid); require_login($cm->course, false, $cm); } $contexts->require_one_edit_tab_cap($edittab); } $PAGE->set_url($thispageurl); $import_form = new question_import_form($thispageurl, array('contexts' => $contexts->having_one_edit_tab_cap('import'), 'defaultcategory' => $pagevars['cat'])); if ($import_form->is_cancelled()) { redirect($thispageurl); } //========== // PAGE HEADER //========== $PAGE->set_title($txt->importquestions); $PAGE->set_heading($COURSE->fullname); echo $OUTPUT->header(); // file upload form sumitted if ($form = $import_form->get_data()) { // file checks out ok $fileisgood = false; // work out if this is an uploaded file // or one from the filesarea.
/** * Called by pluginfile.php to serve files related to the 'question' core * component and for files belonging to qtypes. * * For files that relate to questions in a question_attempt, then we delegate to * a function in the component that owns the attempt (for example in the quiz, * or in core question preview) to get necessary inforation. * * (Note that, at the moment, all question file areas relate to questions in * attempts, so the If at the start of the last paragraph is always true.) * * Does not return, either calls send_file_not_found(); or serves the file. * * @package core_question * @category files * @param stdClass $course course settings object * @param stdClass $context context object * @param string $component the name of the component we are serving files for. * @param string $filearea the name of the file area. * @param array $args the remaining bits of the file path. * @param bool $forcedownload whether the user must be forced to download the file. * @param array $options additional options affecting the file serving */ function question_pluginfile($course, $context, $component, $filearea, $args, $forcedownload, array $options = array()) { global $DB, $CFG; // Special case, sending a question bank export. if ($filearea === 'export') { list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm); require_once $CFG->dirroot . '/question/editlib.php'; $contexts = new question_edit_contexts($context); // check export capability $contexts->require_one_edit_tab_cap('export'); $category_id = (int) array_shift($args); $format = array_shift($args); $cattofile = array_shift($args); $contexttofile = array_shift($args); $filename = array_shift($args); // load parent class for import/export require_once $CFG->dirroot . '/question/format.php'; require_once $CFG->dirroot . '/question/editlib.php'; require_once $CFG->dirroot . '/question/format/' . $format . '/format.php'; $classname = 'qformat_' . $format; if (!class_exists($classname)) { send_file_not_found(); } $qformat = new $classname(); if (!($category = $DB->get_record('question_categories', array('id' => $category_id)))) { send_file_not_found(); } $qformat->setCategory($category); $qformat->setContexts($contexts->having_one_edit_tab_cap('export')); $qformat->setCourse($course); if ($cattofile == 'withcategories') { $qformat->setCattofile(true); } else { $qformat->setCattofile(false); } if ($contexttofile == 'withcontexts') { $qformat->setContexttofile(true); } else { $qformat->setContexttofile(false); } if (!$qformat->exportpreprocess()) { send_file_not_found(); print_error('exporterror', 'question', $thispageurl->out()); } // export data to moodle file pool if (!($content = $qformat->exportprocess(true))) { send_file_not_found(); } send_file($content, $filename, 0, 0, true, true, $qformat->mime_type()); } // Normal case, a file belonging to a question. $qubaidorpreview = array_shift($args); // Two sub-cases: 1. A question being previewed outside an attempt/usage. if ($qubaidorpreview === 'preview') { $previewcontextid = (int) array_shift($args); $previewcomponent = array_shift($args); $questionid = (int) array_shift($args); $previewcontext = context_helper::instance_by_id($previewcontextid); $result = component_callback($previewcomponent, 'question_preview_pluginfile', array($previewcontext, $questionid, $context, $component, $filearea, $args, $forcedownload, $options), 'newcallbackmissing'); if ($result === 'newcallbackmissing' && ($filearea = 'questiontext')) { // Fall back to the legacy callback for backwards compatibility. debugging("Component {$previewcomponent} does not define the expected " . "{$previewcomponent}_question_preview_pluginfile callback. Falling back to the deprecated " . "{$previewcomponent}_questiontext_preview_pluginfile callback.", DEBUG_DEVELOPER); component_callback($previewcomponent, 'questiontext_preview_pluginfile', array($previewcontext, $questionid, $args, $forcedownload, $options)); } send_file_not_found(); } // 2. A question being attempted in the normal way. $qubaid = (int) $qubaidorpreview; $slot = (int) array_shift($args); $module = $DB->get_field('question_usages', 'component', array('id' => $qubaid)); if ($module === 'core_question_preview') { require_once $CFG->dirroot . '/question/previewlib.php'; return question_preview_question_pluginfile($course, $context, $component, $filearea, $qubaid, $slot, $args, $forcedownload, $options); } else { $dir = core_component::get_component_directory($module); if (!file_exists("{$dir}/lib.php")) { send_file_not_found(); } include_once "{$dir}/lib.php"; $filefunction = $module . '_question_pluginfile'; if (function_exists($filefunction)) { $filefunction($course, $context, $component, $filearea, $qubaid, $slot, $args, $forcedownload, $options); } // Okay, we're here so lets check for function without 'mod_'. if (strpos($module, 'mod_') === 0) { $filefunctionold = substr($module, 4) . '_question_pluginfile'; if (function_exists($filefunctionold)) { $filefunctionold($course, $context, $component, $filearea, $qubaid, $slot, $args, $forcedownload, $options); } } send_file_not_found(); } }
/** * Called by pluginfile.php to serve files related to the 'question' core * component and for files belonging to qtypes. * * For files that relate to questions in a question_attempt, then we delegate to * a function in the component that owns the attempt (for example in the quiz, * or in core question preview) to get necessary inforation. * * (Note that, at the moment, all question file areas relate to questions in * attempts, so the If at the start of the last paragraph is always true.) * * Does not return, either calls send_file_not_found(); or serves the file. * * @param object $course course settings object * @param object $context context object * @param string $component the name of the component we are serving files for. * @param string $filearea the name of the file area. * @param array $args the remaining bits of the file path. * @param bool $forcedownload whether the user must be forced to download the file. */ function question_pluginfile($course, $context, $component, $filearea, $args, $forcedownload) { global $DB, $CFG; list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm); if ($filearea === 'export') { require_once $CFG->dirroot . '/question/editlib.php'; $contexts = new question_edit_contexts($context); // check export capability $contexts->require_one_edit_tab_cap('export'); $category_id = (int) array_shift($args); $format = array_shift($args); $cattofile = array_shift($args); $contexttofile = array_shift($args); $filename = array_shift($args); // load parent class for import/export require_once $CFG->dirroot . '/question/format.php'; require_once $CFG->dirroot . '/question/editlib.php'; require_once $CFG->dirroot . '/question/format/' . $format . '/format.php'; $classname = 'qformat_' . $format; if (!class_exists($classname)) { send_file_not_found(); } $qformat = new $classname(); if (!($category = $DB->get_record('question_categories', array('id' => $category_id)))) { send_file_not_found(); } $qformat->setCategory($category); $qformat->setContexts($contexts->having_one_edit_tab_cap('export')); $qformat->setCourse($course); if ($cattofile == 'withcategories') { $qformat->setCattofile(true); } else { $qformat->setCattofile(false); } if ($contexttofile == 'withcontexts') { $qformat->setContexttofile(true); } else { $qformat->setContexttofile(false); } if (!$qformat->exportpreprocess()) { send_file_not_found(); print_error('exporterror', 'question', $thispageurl->out()); } // export data to moodle file pool if (!($content = $qformat->exportprocess(true))) { send_file_not_found(); } //DEBUG //echo '<textarea cols=90 rows=20>'; //echo $content; //echo '</textarea>'; //die; send_file($content, $filename, 0, 0, true, true, $qformat->mime_type()); } $attemptid = (int) array_shift($args); $questionid = (int) array_shift($args); if ($attemptid === 0) { // preview require_once $CFG->dirroot . '/question/previewlib.php'; return question_preview_question_pluginfile($course, $context, $component, $filearea, $attemptid, $questionid, $args, $forcedownload); } else { $module = $DB->get_field('question_attempts', 'modulename', array('id' => $attemptid)); $dir = get_component_directory($module); if (!file_exists("{$dir}/lib.php")) { send_file_not_found(); } include_once "{$dir}/lib.php"; $filefunction = $module . '_question_pluginfile'; if (!function_exists($filefunction)) { send_file_not_found(); } $filefunction($course, $context, $component, $filearea, $attemptid, $questionid, $args, $forcedownload); send_file_not_found(); } }
/** * Called by pluginfile.php to serve files related to the 'question' core * component and for files belonging to qtypes. * * For files that relate to questions in a question_attempt, then we delegate to * a function in the component that owns the attempt (for example in the quiz, * or in core question preview) to get necessary inforation. * * (Note that, at the moment, all question file areas relate to questions in * attempts, so the If at the start of the last paragraph is always true.) * * Does not return, either calls send_file_not_found(); or serves the file. * * @package core_question * @category files * @param stdClass $course course settings object * @param stdClass $context context object * @param string $component the name of the component we are serving files for. * @param string $filearea the name of the file area. * @param array $args the remaining bits of the file path. * @param bool $forcedownload whether the user must be forced to download the file. * @param array $options additional options affecting the file serving */ function question_pluginfile($course, $context, $component, $filearea, $args, $forcedownload, array $options = array()) { global $DB, $CFG; if ($filearea === 'questiontext_preview') { $component = array_shift($args); $questionid = array_shift($args); component_callback($component, 'questiontext_preview_pluginfile', array($context, $questionid, $args, $forcedownload, $options)); send_file_not_found(); } if ($filearea === 'export') { list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm); require_once $CFG->dirroot . '/question/editlib.php'; $contexts = new question_edit_contexts($context); // check export capability $contexts->require_one_edit_tab_cap('export'); $category_id = (int) array_shift($args); $format = array_shift($args); $cattofile = array_shift($args); $contexttofile = array_shift($args); $filename = array_shift($args); // load parent class for import/export require_once $CFG->dirroot . '/question/format.php'; require_once $CFG->dirroot . '/question/editlib.php'; require_once $CFG->dirroot . '/question/format/' . $format . '/format.php'; $classname = 'qformat_' . $format; if (!class_exists($classname)) { send_file_not_found(); } $qformat = new $classname(); if (!($category = $DB->get_record('question_categories', array('id' => $category_id)))) { send_file_not_found(); } $qformat->setCategory($category); $qformat->setContexts($contexts->having_one_edit_tab_cap('export')); $qformat->setCourse($course); if ($cattofile == 'withcategories') { $qformat->setCattofile(true); } else { $qformat->setCattofile(false); } if ($contexttofile == 'withcontexts') { $qformat->setContexttofile(true); } else { $qformat->setContexttofile(false); } if (!$qformat->exportpreprocess()) { send_file_not_found(); print_error('exporterror', 'question', $thispageurl->out()); } // export data to moodle file pool if (!($content = $qformat->exportprocess(true))) { send_file_not_found(); } send_file($content, $filename, 0, 0, true, true, $qformat->mime_type()); } $qubaid = (int) array_shift($args); $slot = (int) array_shift($args); $module = $DB->get_field('question_usages', 'component', array('id' => $qubaid)); if ($module === 'core_question_preview') { require_once $CFG->dirroot . '/question/previewlib.php'; return question_preview_question_pluginfile($course, $context, $component, $filearea, $qubaid, $slot, $args, $forcedownload, $options); } else { $dir = get_component_directory($module); if (!file_exists("{$dir}/lib.php")) { send_file_not_found(); } include_once "{$dir}/lib.php"; $filefunction = $module . '_question_pluginfile'; if (function_exists($filefunction)) { $filefunction($course, $context, $component, $filearea, $qubaid, $slot, $args, $forcedownload, $options); } // Okay, we're here so lets check for function without 'mod_'. if (strpos($module, 'mod_') === 0) { $filefunctionold = substr($module, 4) . '_question_pluginfile'; if (function_exists($filefunctionold)) { $filefunctionold($course, $context, $component, $filearea, $qubaid, $slot, $args, $forcedownload, $options); } } send_file_not_found(); } }
function load_questions($category, $importfilename, $contextid) { // Load all the questions from the given import file into the given category // The category from the import file will be ignored if present. global $COURSE; $qformat = new qformat_xml(); $qformat->setCategory($category); $systemcontext = context::instance_by_id($contextid); $contexts = new question_edit_contexts($systemcontext); $qformat->setContexts($contexts->having_one_edit_tab_cap('import')); $qformat->setCourse($COURSE); $qformat->setFilename($importfilename); $qformat->setRealfilename($importfilename); $qformat->setMatchgrades('error'); $qformat->setCatfromfile(false); $qformat->setContextfromfile(false); $qformat->setStoponerror(true); // Do anything before that we need to if (!$qformat->importpreprocess()) { throw new coding_exception('Upgrade failed: error preprocessing prototype upload'); } // Process the given file if (!$qformat->importprocess($category)) { throw new coding_exception('Upgrade failed: error uploading prototype questions'); } // In case anything needs to be done after if (!$qformat->importpostprocess()) { throw new coding_exception('Upgrade failed: error postprocessing prototype upload'); } }
$questiondata = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST); get_question_options($questiondata); $question = question_bank::load_question($questionid); // Process any other URL parameters, and do require_login. list($context, $seed, $urlparams) = qtype_stack_setup_question_test_page($question); $contexts = new question_edit_contexts($context); // Check permissions. question_require_capability_on($questiondata, 'edit'); require_sesskey(); // Initialise $PAGE. $nexturl = new moodle_url('/question/type/stack/questiontestrun.php', $urlparams); $PAGE->set_url($nexturl); // Since this script always ends in a redirect. $PAGE->set_heading($COURSE->fullname); $PAGE->set_pagelayout('admin'); // Set up the export format. $qformat = new qformat_xml(); $filename = question_default_export_filename($COURSE, $questiondata) . $qformat->export_file_extension(); $qformat->setContexts($contexts->having_one_edit_tab_cap('export')); $qformat->setCourse($COURSE); $qformat->setQuestions(array($questiondata)); $qformat->setCattofile(false); $qformat->setContexttofile(false); // Do the export. if (!$qformat->exportpreprocess()) { send_file_not_found(); } if (!($content = $qformat->exportprocess(true))) { send_file_not_found(); } send_file($content, $filename, 0, 0, true, true, $qformat->mime_type());
//we get the context from the category object. if ($contexts === null) { // need to get the course from the chosen category $contexts = new question_edit_contexts($categorycontext); $thiscontext = $contexts->lowest(); if ($thiscontext->contextlevel == CONTEXT_COURSE){ require_login($thiscontext->instanceid, false); } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){ list($module, $cm) = get_module_from_cmid($thiscontext->instanceid); require_login($cm->course, false, $cm); } $contexts->require_one_edit_tab_cap($edittab); } $PAGE->set_url($thispageurl->out()); $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'), 'defaultcategory'=>$pagevars['cat'])); if ($import_form->is_cancelled()){ redirect($thispageurl); } //========== // PAGE HEADER //========== $PAGE->set_title($txt->importquestions); $PAGE->set_heading($COURSE->fullname); echo $OUTPUT->header(); // file upload form sumitted if ($form = $import_form->get_data()) {