/** * Common setup for all pages for editing questions. * @param string $edittab code for this edit tab * @param boolean $requirecmid require cmid? default false * @param boolean $requirecourseid require courseid, if cmid is not given? default true * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars */ function question_edit_setup($edittab, $requirecmid = false, $requirecourseid = true) { global $COURSE, $QUESTION_EDITTABCAPS; //$thispageurl is used to construct urls for all question edit pages we link to from this page. It contains an array //of parameters that are passed from page to page. $thispageurl = new moodle_url(); if ($requirecmid) { $cmid = required_param('cmid', PARAM_INT); } else { $cmid = optional_param('cmid', 0, PARAM_INT); } if ($cmid) { list($module, $cm) = get_module_from_cmid($cmid); $courseid = $cm->course; $thispageurl->params(compact('cmid')); require_login($courseid, false, $cm); $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid); } else { $module = null; $cm = null; if ($requirecourseid) { $courseid = required_param('courseid', PARAM_INT); } else { $courseid = optional_param('courseid', 0, PARAM_INT); } if ($courseid) { $thispageurl->params(compact('courseid')); require_login($courseid, false); $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid); } else { $thiscontext = null; } } if ($thiscontext) { $contexts = new question_edit_contexts($thiscontext); $contexts->require_one_edit_tab_cap($edittab); } else { $contexts = null; } $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT); //pass 'cat' from page to page and when 'category' comes from a drop down menu //then we also reset the qpage so we go to page 1 of //a new cat. $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE); // if empty will be set up later if ($category = optional_param('category', 0, PARAM_SEQUENCE)) { if ($pagevars['cat'] != $category) { // is this a move to a new category? $pagevars['cat'] = $category; $pagevars['qpage'] = 0; } } if ($pagevars['cat']) { $thispageurl->param('cat', $pagevars['cat']); } if ($pagevars['qpage'] > -1) { $thispageurl->param('qpage', $pagevars['qpage']); } else { $pagevars['qpage'] = 0; } $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT); if ($pagevars['qperpage'] > -1) { $thispageurl->param('qperpage', $pagevars['qperpage']); } else { $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE; } $sortoptions = array('alpha' => 'name, qtype ASC', 'typealpha' => 'qtype, name ASC', 'age' => 'id ASC'); if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA)) { $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder]; $pagevars['qsortorder'] = $sortorder; $thispageurl->param('qsortorder', $sortorder); } else { $pagevars['qsortorderdecoded'] = $sortoptions['typealpha']; $pagevars['qsortorder'] = 'typealpha'; } $defaultcategory = question_make_default_categories($contexts->all()); $contextlistarr = array(); foreach ($contexts->having_one_edit_tab_cap($edittab) as $context) { $contextlistarr[] = "'{$context->id}'"; } $contextlist = join($contextlistarr, ' ,'); if (!empty($pagevars['cat'])) { $catparts = explode(',', $pagevars['cat']); if (!$catparts[0] || FALSE !== array_search($catparts[1], $contextlistarr) || !count_records_select("question_categories", "id = '" . $catparts[0] . "' AND contextid = {$catparts['1']}")) { print_error('invalidcategory', 'quiz'); } } else { $category = $defaultcategory; $pagevars['cat'] = "{$category->id},{$category->contextid}"; } if (($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) { $pagevars['recurse'] = $recurse; $thispageurl->param('recurse', $recurse); } else { $pagevars['recurse'] = 1; } if (($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) { $pagevars['showhidden'] = $showhidden; $thispageurl->param('showhidden', $showhidden); } else { $pagevars['showhidden'] = 0; } if (($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) { $pagevars['showquestiontext'] = $showquestiontext; $thispageurl->param('showquestiontext', $showquestiontext); } else { $pagevars['showquestiontext'] = 0; } //category list page $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT); if ($pagevars['cpage'] < 1) { $pagevars['cpage'] = 1; } if ($pagevars['cpage'] != 1) { $thispageurl->param('cpage', $pagevars['cpage']); } return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars); }
/** * 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); }
list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('import', '/question/import.php'); // get display strings $txt = new stdClass(); $txt->importerror = get_string('importerror', 'question'); $txt->importquestions = get_string('importquestions', 'question'); list($catid, $catcontext) = explode(',', $pagevars['cat']); if (!($category = $DB->get_record("question_categories", array('id' => $catid)))) { print_error('nocategory', 'question'); } $categorycontext = context::instance_by_id($category->contextid); $category->context = $categorycontext; //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); } //==========
/** * 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(); } }
/** * Print all the controls for adding questions directly into the * specific page in the edit tab of edit.php * * @param unknown_type $quiz * @param unknown_type $pageurl * @param unknown_type $page * @param unknown_type $hasattempts */ function quiz_print_pagecontrols($quiz, $pageurl, $page, $hasattempts) { global $CFG; static $randombuttoncount = 0; $randombuttoncount++; echo '<div class="pagecontrols">'; // Get the current context $thiscontext = get_context_instance(CONTEXT_COURSE, $quiz->course); $contexts = new question_edit_contexts($thiscontext); // Get the default category. $defaultcategory = question_make_default_categories($contexts->all()); // Create the url the question page will return to $returnurl_addtoquiz = new moodle_url($pageurl->out(true), array('addonpage' => $page)); // Print a button linking to the choose question type page. $newquestionparams = array('returnurl' => $returnurl_addtoquiz->out(false), 'cmid' => $quiz->cmid, 'appendqnumstring' => 'addquestion'); create_new_question_button($defaultcategory->id, $newquestionparams, get_string('addaquestion', 'quiz'), get_string('createquestionandadd', 'quiz'), $hasattempts); if ($hasattempts) { $disabled = 'disabled="disabled"'; } else { $disabled = ''; } ?> <div class="singlebutton"> <form class="randomquestionform" action="<?php echo $CFG->wwwroot; ?> /mod/quiz/addrandom.php" method="get"> <div> <input type="hidden" class="addonpage_formelement" name="addonpage_form" value="<?php echo $page; ?> " /> <input type="hidden" name="cmid" value="<?php echo $quiz->cmid; ?> " /> <input type="hidden" name="courseid" value="<?php echo $quiz->course; ?> " /> <input type="hidden" name="returnurl" value="<?php echo urlencode($pageurl->out(true)); ?> " /> <input type="submit" id="addrandomdialoglaunch_<?php echo $randombuttoncount; ?> " value="<?php echo get_string('addarandomquestion', 'quiz'); ?> " <?php echo " {$disabled}"; ?> /> </div> </form> </div> <?php helpbutton('random', get_string('random', 'quiz'), 'quiz', true, false, ''); ?> <?php echo "\n</div>"; }
/** * Common setup for all pages for editing questions. * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'. * @param string $edittab code for this edit tab * @param bool $requirecmid require cmid? default false * @param bool $requirecourseid require courseid, if cmid is not given? default true * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars */ function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirecourseid = true) { global $DB, $PAGE; $thispageurl = new moodle_url($baseurl); $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained. if ($requirecmid) { $cmid = required_param('cmid', PARAM_INT); } else { $cmid = optional_param('cmid', 0, PARAM_INT); } if ($cmid) { list($module, $cm) = get_module_from_cmid($cmid); $courseid = $cm->course; $thispageurl->params(compact('cmid')); require_login($courseid, false, $cm); $thiscontext = context_module::instance($cmid); } else { $module = null; $cm = null; if ($requirecourseid) { $courseid = required_param('courseid', PARAM_INT); } else { $courseid = optional_param('courseid', 0, PARAM_INT); } if ($courseid) { $thispageurl->params(compact('courseid')); require_login($courseid, false); $thiscontext = context_course::instance($courseid); } else { $thiscontext = null; } } if ($thiscontext) { $contexts = new question_edit_contexts($thiscontext); $contexts->require_one_edit_tab_cap($edittab); } else { $contexts = null; } $PAGE->set_pagelayout('admin'); $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT); //pass 'cat' from page to page and when 'category' comes from a drop down menu //then we also reset the qpage so we go to page 1 of //a new cat. $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE); // if empty will be set up later if ($category = optional_param('category', 0, PARAM_SEQUENCE)) { if ($pagevars['cat'] != $category) { // is this a move to a new category? $pagevars['cat'] = $category; $pagevars['qpage'] = 0; } } if ($pagevars['cat']) { $thispageurl->param('cat', $pagevars['cat']); } if (strpos($baseurl, '/question/') === 0) { navigation_node::override_active_url($thispageurl); } if ($pagevars['qpage'] > -1) { $thispageurl->param('qpage', $pagevars['qpage']); } else { $pagevars['qpage'] = 0; } $pagevars['qperpage'] = question_get_display_preference('qperpage', DEFAULT_QUESTIONS_PER_PAGE, PARAM_INT, $thispageurl); for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) { $param = 'qbs' . $i; if (!($sort = optional_param($param, '', PARAM_TEXT))) { break; } $thispageurl->param($param, $sort); } $defaultcategory = question_make_default_categories($contexts->all()); $contextlistarr = array(); foreach ($contexts->having_one_edit_tab_cap($edittab) as $context) { $contextlistarr[] = "'{$context->id}'"; } $contextlist = join($contextlistarr, ' ,'); if (!empty($pagevars['cat'])) { $catparts = explode(',', $pagevars['cat']); if (!$catparts[0] || false !== array_search($catparts[1], $contextlistarr) || !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) { print_error('invalidcategory', 'question'); } } else { $category = $defaultcategory; $pagevars['cat'] = "{$category->id},{$category->contextid}"; } // Display options. $pagevars['recurse'] = question_get_display_preference('recurse', 1, PARAM_BOOL, $thispageurl); $pagevars['showhidden'] = question_get_display_preference('showhidden', 0, PARAM_BOOL, $thispageurl); $pagevars['qbshowtext'] = question_get_display_preference('qbshowtext', 0, PARAM_BOOL, $thispageurl); // Category list page. $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT); if ($pagevars['cpage'] != 1) { $thispageurl->param('cpage', $pagevars['cpage']); } return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars); }
/** * 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(); } }
/** * Common setup for all pages for editing questions. * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'. * @param string $edittab code for this edit tab * @param bool $requirecmid require cmid? default false * @param bool $requirecourseid require courseid, if cmid is not given? default true * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars */ function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirecourseid = true) { global $DB, $PAGE; $thispageurl = new moodle_url($baseurl); $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained. if ($requirecmid) { $cmid = required_param('cmid', PARAM_INT); } else { $cmid = optional_param('cmid', 0, PARAM_INT); } if ($cmid) { list($module, $cm) = get_module_from_cmid($cmid); $courseid = $cm->course; $thispageurl->params(compact('cmid')); require_login($courseid, false, $cm); $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid); } else { $module = null; $cm = null; if ($requirecourseid) { $courseid = required_param('courseid', PARAM_INT); } else { $courseid = optional_param('courseid', 0, PARAM_INT); } if ($courseid) { $thispageurl->params(compact('courseid')); require_login($courseid, false); $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid); } else { $thiscontext = null; } } if ($thiscontext) { $contexts = new question_edit_contexts($thiscontext); $contexts->require_one_edit_tab_cap($edittab); } else { $contexts = null; } $PAGE->set_pagelayout('admin'); $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT); //pass 'cat' from page to page and when 'category' comes from a drop down menu //then we also reset the qpage so we go to page 1 of //a new cat. $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE); // if empty will be set up later if ($category = optional_param('category', 0, PARAM_SEQUENCE)) { if ($pagevars['cat'] != $category) { // is this a move to a new category? $pagevars['cat'] = $category; $pagevars['qpage'] = 0; } } if ($pagevars['cat']) { $thispageurl->param('cat', $pagevars['cat']); } if (strpos($baseurl, '/question/') === 0) { navigation_node::override_active_url($thispageurl); } if ($pagevars['qpage'] > -1) { $thispageurl->param('qpage', $pagevars['qpage']); } else { $pagevars['qpage'] = 0; } $pagevars['qperpage'] = optional_param('qperpage', -1, PARAM_INT); if ($pagevars['qperpage'] > -1) { $thispageurl->param('qperpage', $pagevars['qperpage']); } else { $pagevars['qperpage'] = DEFAULT_QUESTIONS_PER_PAGE; } $sortoptions = array('alpha' => 'name, qtype ASC', 'typealpha' => 'qtype, name ASC', 'age' => 'id ASC'); if ($sortorder = optional_param('qsortorder', '', PARAM_ALPHA)) { $pagevars['qsortorderdecoded'] = $sortoptions[$sortorder]; $pagevars['qsortorder'] = $sortorder; $thispageurl->param('qsortorder', $sortorder); } else { $pagevars['qsortorderdecoded'] = $sortoptions['typealpha']; $pagevars['qsortorder'] = 'typealpha'; } $defaultcategory = question_make_default_categories($contexts->all()); $contextlistarr = array(); foreach ($contexts->having_one_edit_tab_cap($edittab) as $context) { $contextlistarr[] = "'{$context->id}'"; } $contextlist = join($contextlistarr, ' ,'); if (!empty($pagevars['cat'])) { $catparts = explode(',', $pagevars['cat']); if (!$catparts[0] || false !== array_search($catparts[1], $contextlistarr) || !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) { print_error('invalidcategory', 'question'); } } else { $category = $defaultcategory; $pagevars['cat'] = "{$category->id},{$category->contextid}"; } if (($recurse = optional_param('recurse', -1, PARAM_BOOL)) != -1) { $pagevars['recurse'] = $recurse; $thispageurl->param('recurse', $recurse); } else { $pagevars['recurse'] = 1; } if (($showhidden = optional_param('showhidden', -1, PARAM_BOOL)) != -1) { $pagevars['showhidden'] = $showhidden; $thispageurl->param('showhidden', $showhidden); } else { $pagevars['showhidden'] = 0; } if (($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL)) != -1) { $pagevars['showquestiontext'] = $showquestiontext; $thispageurl->param('showquestiontext', $showquestiontext); } else { $pagevars['showquestiontext'] = 0; } //category list page $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT); if ($pagevars['cpage'] != 1) { $thispageurl->param('cpage', $pagevars['cpage']); } return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars); }
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package quiz */ require_once "../../config.php"; require_once "locallib.php"; $id = required_param('id', PARAM_INT); if (!($course = $DB->get_record('course', array('id' => $id)))) { print_error('invalidcourseid'); } $coursecontext = get_context_instance(CONTEXT_COURSE, $id); require_login($course->id); add_to_log($course->id, "quiz", "view all", "index.php?id={$course->id}", ""); // Print the header $strquizzes = get_string("modulenameplural", "quiz"); $streditquestions = ''; $editqcontexts = new question_edit_contexts($coursecontext); if ($editqcontexts->have_one_edit_tab_cap('questions')) { $streditquestions = "<form target=\"_parent\" method=\"get\" action=\"{$CFG->wwwroot}/question/edit.php\">\n <div>\n <input type=\"hidden\" name=\"courseid\" value=\"{$course->id}\" />\n <input type=\"submit\" value=\"" . get_string("editquestions", "quiz") . "\" />\n </div>\n </form>"; } $navlinks = array(); $navlinks[] = array('name' => $strquizzes, 'link' => '', 'type' => 'activity'); $navigation = build_navigation($navlinks); print_header_simple($strquizzes, '', $navigation, '', '', true, $streditquestions, navmenu($course)); // Get all the appropriate data if (!($quizzes = get_all_instances_in_course("quiz", $course))) { notice(get_string('thereareno', 'moodle', $strquizzes), "../../course/view.php?id={$course->id}"); die; } // Configure table for displaying the list of instances. $headings = array(get_string('name'), get_string('quizcloses', 'quiz')); $align = array('left', 'left');
/** * Return random question form. * @param \moodle_url $thispageurl the canonical URL of this page. * @param \question_edit_contexts $contexts the relevant question bank contexts. * @param array $pagevars the variables from {@link \question_edit_setup()}. * @return string HTML to output. */ protected function random_question_form(\moodle_url $thispageurl, \question_edit_contexts $contexts, array $pagevars) { if (!$contexts->have_cap('moodle/question:useall')) { return ''; } $randomform = new \quiz_add_random_form(new \moodle_url('/mod/quiz/addrandom.php'), array('contexts' => $contexts, 'cat' => $pagevars['cat'])); $randomform->set_data(array('category' => $pagevars['cat'], 'returnurl' => $thispageurl->out_as_local_url(true), 'randomnumber' => 1, 'cmid' => $thispageurl->param('cmid'))); return html_writer::div($randomform->render(), 'randomquestionformforpopup'); }
if (empty($quiz)) { if (empty($attemptobj)) { print_error('cannotcallscript'); } $quiz = $attemptobj->get_quiz(); $cm = $attemptobj->get_cm(); } if (!isset($currenttab)) { $currenttab = ''; } if (!isset($cm)) { $cm = get_coursemodule_from_instance('quiz', $quiz->id); } $context = get_context_instance(CONTEXT_MODULE, $cm->id); if (!isset($contexts)) { $contexts = new question_edit_contexts($context); } $tabs = array(); $row = array(); $inactive = array(); $activated = array(); $stredit = get_string('edit'); if (has_capability('mod/quiz:view', $context)) { $row[] = new tabobject('info', "{$CFG->wwwroot}/mod/quiz/view.php?id={$cm->id}", get_string('info', 'quiz')); } if (has_capability('mod/quiz:viewreports', $context)) { $row[] = new tabobject('reports', "{$CFG->wwwroot}/mod/quiz/report.php?q={$quiz->id}", get_string('results', 'quiz')); } if (has_capability('mod/quiz:preview', $context)) { $strpreview = get_string('preview', 'quiz'); $row[] = new tabobject('preview', "{$CFG->wwwroot}/mod/quiz/startattempt.php?cmid={$cm->id}&sesskey=" . sesskey(), "<img src=\"{$CFG->pixpath}/t/preview.gif\" class=\"iconsmall\" alt=\"{$strpreview}\" /> {$strpreview}", $strpreview);
* @author Tim Hunt and others. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package quiz */ if (empty($quiz)) { error('You cannot call this script in that way'); } if (!isset($currenttab)) { $currenttab = ''; } if (!isset($cm)) { $cm = get_coursemodule_from_instance('quiz', $quiz->id); } $context = get_context_instance(CONTEXT_MODULE, $cm->id); if (!isset($contexts)) { $contexts = new question_edit_contexts($context); } $tabs = array(); $row = array(); $inactive = array(); $activated = array(); if (has_capability('mod/quiz:view', $context)) { $row[] = new tabobject('info', "{$CFG->wwwroot}/mod/quiz/view.php?q={$quiz->id}", get_string('info', 'quiz')); } if (has_capability('mod/quiz:viewreports', $context)) { $row[] = new tabobject('reports', "{$CFG->wwwroot}/mod/quiz/report.php?q={$quiz->id}", get_string('results', 'quiz')); } if (has_capability('mod/quiz:preview', $context)) { $row[] = new tabobject('preview', "{$CFG->wwwroot}/mod/quiz/attempt.php?q={$quiz->id}", get_string('preview', 'quiz')); } if (has_capability('mod/quiz:manage', $context)) {
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'); } }
* @author Tim Hunt and others. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package quiz */ if (empty($quiz)) { error('You cannot call this script in that way'); } if (!isset($currenttab)) { $currenttab = ''; } if (!isset($cm)) { $cm = get_coursemodule_from_instance('quiz', $quiz->id); } $context = get_context_instance(CONTEXT_MODULE, $cm->id); if (!isset($contexts)) { $contexts = new question_edit_contexts($context); } $tabs = array(); $row = array(); $inactive = array(); $activated = array(); if (has_capability('mod/quiz:view', $context)) { $row[] = new tabobject('info', "{$CFG->wwwroot}/mod/quiz/view.php?q={$quiz->id}", get_string('info', 'quiz')); } if (has_capability('mod/quiz:viewreports', $context)) { $row[] = new tabobject('reports', "{$CFG->wwwroot}/mod/quiz/report.php?q={$quiz->id}", get_string('results', 'quiz')); } if (has_capability('mod/quiz:preview', $context)) { $row[] = new tabobject('preview', "{$CFG->wwwroot}/mod/quiz/attempt.php?q={$quiz->id}", get_string('preview', 'quiz')); } if ($contexts->have_one_edit_tab_cap('editq')) {
/** * @param array $row tab objects * @param question_edit_contexts $contexts object representing contexts available from this context * @param string $querystring to append to urls * */ function questionbank_navigation_tabs(&$row, $contexts, $querystring) { global $CFG, $QUESTION_EDITTABCAPS; $tabs = array('questions' => array("{$CFG->wwwroot}/question/edit.php?{$querystring}", get_string('questions', 'quiz'), get_string('editquestions', 'quiz')), 'categories' => array("{$CFG->wwwroot}/question/category.php?{$querystring}", get_string('categories', 'quiz'), get_string('editqcats', 'quiz')), 'import' => array("{$CFG->wwwroot}/question/import.php?{$querystring}", get_string('import', 'quiz'), get_string('importquestions', 'quiz')), 'export' => array("{$CFG->wwwroot}/question/export.php?{$querystring}", get_string('export', 'quiz'), get_string('exportquestions', 'quiz'))); foreach ($tabs as $tabname => $tabparams) { if ($contexts->have_one_edit_tab_cap($tabname)) { $row[] = new tabobject($tabname, $tabparams[0], $tabparams[1], $tabparams[2]); } } }
function game_get_contexts() { global $CFG, $COURSE; require $CFG->dirroot . '/question/editlib.php'; $thiscontext = get_context_instance(CONTEXT_COURSE, $COURSE->id); $contexts = new question_edit_contexts($thiscontext); $caps = array('moodle/question:viewmine', 'moodle/question:viewall'); return $contexts->having_one_cap($caps); }
/** * 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(); } }
if ($movecontext && !$id) { print_error('questiondoesnotexist', 'question', $returnurl); } if ($cmid) { list($module, $cm) = get_module_from_cmid($cmid); require_login($cm->course, false, $cm); $thiscontext = context_module::instance($cmid); } elseif ($courseid) { require_login($courseid, false); $thiscontext = context_course::instance($courseid); $module = null; $cm = null; } else { print_error('missingcourseorcmid', 'question'); } $contexts = new question_edit_contexts($thiscontext); $PAGE->set_pagelayout('admin'); if (optional_param('addcancel', false, PARAM_BOOL)) { redirect($returnurl); } if ($id) { if (!($question = $DB->get_record('question', array('id' => $id)))) { print_error('questiondoesnotexist', 'question', $returnurl); } get_question_options($question, true); } else { if ($categoryid && $qtype) { // only for creating new questions $question = new stdClass(); $question->category = $categoryid; $question->qtype = $qtype;
* @copyright 2015 the Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once __DIR__ . '/../../../config.php'; require_once $CFG->libdir . '/questionlib.php'; require_once $CFG->dirroot . '/question/format/xml/format.php'; require_once __DIR__ . '/locallib.php'; // Get the parameters from the URL. $questionid = required_param('questionid', PARAM_INT); // Load the necessary data. $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));