/** * Put the specified questions on the specified pages of a given quiz. * * Give the question name in the first column, and that page number in the * second column. You may optionally give the desired maximum mark for each * question in a third column. * * @param string $quizname the name of the quiz to add questions to. * @param TableNode $data information about the questions to add. * * @Given /^quiz "([^"]*)" contains the following questions:$/ */ public function quiz_contains_the_following_questions($quizname, TableNode $data) { global $DB; $quiz = $DB->get_record('quiz', array('name' => $quizname), '*', MUST_EXIST); // The action depends on the field type. foreach ($data->getRows() as $questiondata) { if (count($questiondata) < 2 || count($questiondata) > 3) { throw new ExpectationException('When adding questions to a quiz, you should give 2 or three 3 things: ' . ' the question name, the page number, and optionally a the maxiumum mark. ' . count($questiondata) . ' values passed.', $this->getSession()); } list($questionname, $rawpage) = $questiondata; if (!isset($questiondata[2]) || $questiondata[2] === '') { $maxmark = null; } else { $maxmark = clean_param($questiondata[2], PARAM_FLOAT); if (!is_numeric($questiondata[2]) || $maxmark < 0) { throw new ExpectationException('When adding questions to a quiz, the max mark must be a positive number.', $this->getSession()); } } $page = clean_param($rawpage, PARAM_INT); if ($page <= 0 || (string) $page !== $rawpage) { throw new ExpectationException('When adding questions to a quiz, the page number must be a positive integer.', $this->getSession()); } $questionid = $DB->get_field('question', 'id', array('name' => $questionname), MUST_EXIST); quiz_add_quiz_question($questionid, $quiz, $page, $maxmark); } quiz_update_sumgrades($quiz); }
array_splice($questions, $moveselectedpos, 0, $selectedquestionids); $quiz->questions = implode(',', $questions); // Update the database. $DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id)); $deletepreviews = true; } // If rescaling is required save the new maximum. $maxgrade = unformat_float(optional_param('maxgrade', -1, PARAM_RAW)); if ($maxgrade >= 0) { quiz_set_grade($maxgrade, $quiz); } if ($deletepreviews) { quiz_delete_previews($quiz); } if ($recomputesummarks) { quiz_update_sumgrades($quiz); quiz_update_all_attempt_sumgrades($quiz); quiz_update_all_final_grades($quiz); quiz_update_grades($quiz, 0, true); } redirect($afteractionurl); } $questionbank->process_actions($thispageurl, $cm); // End of process commands ===================================================== $PAGE->requires->yui2_lib('container'); $PAGE->requires->yui2_lib('dragdrop'); $PAGE->requires->skip_link_to('questionbank', get_string('skipto', 'access', get_string('questionbank', 'question'))); $PAGE->requires->skip_link_to('quizcontentsblock', get_string('skipto', 'access', get_string('questionsinthisquiz', 'quiz'))); $PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name))); $PAGE->set_heading($course->fullname); $node = $PAGE->settingsnav->find('mod_quiz_edit', navigation_node::TYPE_SETTING);
/** * Update the database after $quiz->questions has been changed. For example, * this deletes preview attempts and updates $quiz->sumgrades. * @param $quiz the quiz object. */ function quiz_save_new_layout($quiz) { global $DB; $DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id)); quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); }
/** * Put the specified questions on the specified pages of a given quiz. * * The first row should be column names: * | question | page | maxmark | * The first two of those are required. The others are optional. * * question needs to uniquely match a question name. * page is a page number. Must start at 1, and on each following * row should be the same as the previous, or one more. * maxmark What the question is marked out of. Defaults to question.defaultmark. * * Then there should be a number of rows of data, one for each question you want to add. * * For backwards-compatibility reasons, specifying the column names is optional * (but strongly encouraged). If not specified, the columns are asseumed to be * | question | page | maxmark |. * * @param string $quizname the name of the quiz to add questions to. * @param TableNode $data information about the questions to add. * * @Given /^quiz "([^"]*)" contains the following questions:$/ */ public function quiz_contains_the_following_questions($quizname, TableNode $data) { global $CFG, $DB; require_once __DIR__ . '/../../editlib.php'; $quiz = $DB->get_record('quiz', array('name' => $quizname), '*', MUST_EXIST); // Deal with backwards-compatibility, optional first row. $firstrow = $data->getRow(0); if (!in_array('question', $firstrow) && !in_array('page', $firstrow)) { if (count($firstrow) == 2) { $headings = array('question', 'page'); } else { if (count($firstrow) == 3) { $headings = array('question', 'page', 'maxmark'); } else { throw new ExpectationException('When adding questions to a quiz, you should give 2 or three 3 things: ' . ' the question name, the page number, and optionally the maxiumum mark. ' . count($firstrow) . ' values passed.', $this->getSession()); } } $rows = $data->getRows(); array_unshift($rows, $headings); $data->setRows($rows); } // Add the questions. $lastpage = 0; foreach ($data->getHash() as $questiondata) { if (!array_key_exists('question', $questiondata)) { throw new ExpectationException('When adding questions to a quiz, ' . 'the question name column is required.', $this->getSession()); } if (!array_key_exists('page', $questiondata)) { throw new ExpectationException('When adding questions to a quiz, ' . 'the page number column is required.', $this->getSession()); } // Question id. $questionid = $DB->get_field('question', 'id', array('name' => $questiondata['question']), MUST_EXIST); // Page number. $page = clean_param($questiondata['page'], PARAM_INT); if ($page <= 0 || (string) $page !== $questiondata['page']) { throw new ExpectationException('The page number for question "' . $questiondata['question'] . '" must be a positive integer.', $this->getSession()); } if ($page < $lastpage || $page > $lastpage + 1) { throw new ExpectationException('When adding questions to a quiz, ' . 'the page number for each question must either be the same, ' . 'or one more, then the page number for the previous question.', $this->getSession()); } $lastpage = $page; // Max mark. if (!array_key_exists('maxmark', $questiondata) || $questiondata['maxmark'] === '') { $maxmark = null; } else { $maxmark = clean_param($questiondata['maxmark'], PARAM_FLOAT); if (!is_numeric($questiondata['maxmark']) || $maxmark < 0) { throw new ExpectationException('The max mark for question "' . $questiondata['question'] . '" must be a positive number.', $this->getSession()); } } // Add the question. quiz_add_quiz_question($questionid, $quiz, $page, $maxmark); } quiz_update_sumgrades($quiz); }
/** * This adds a particular question to the supplied quiz. Based on /mod/quiz/edit.php * * @param int $questionid * @param stdClass $quiz * @return void */ public function add_question_to_quiz($questionid, $quiz) { quiz_require_question_use($questionid); quiz_add_quiz_question($questionid, $quiz, 0); quiz_delete_previews($quiz); quiz_update_sumgrades($quiz); }
function RWSAAQRand() { global $CFG; global $DB; global $RWSUID; RWSCMAuth(); RWSCRAuth(); RWSCMUSvc(); RWSCMMaint(); $r_pm = RWSGSOpt("quizid", PARAM_ALPHANUM); if ($r_pm === false || strlen($r_pm) == 0) { RWSSErr("2067"); } $r_qzmi = intval($r_pm); $r_cmod = RWSCMUQuiz($r_qzmi); $r_cid = $r_cmod->course; RWSCMUCourse($r_cid, true); $r_mr = $DB->get_record("modules", array("id" => $r_cmod->module)); if ($r_mr === false) { RWSSErr("2043"); } $r_qiz = $DB->get_record($r_mr->name, array("id" => $r_cmod->instance)); if ($r_qiz === false) { RWSSErr("2044"); } $r_pm = RWSGSOpt("qcatid", PARAM_ALPHANUM); if ($r_pm === false || strlen($r_pm) == 0) { RWSSErr("2064"); } $r_qci = intval($r_pm); $r_qca = $DB->get_record("question_categories", array("id" => $r_qci)); if ($r_qca === false) { RWSSErr("2065"); } if (respondusws_floatcompare($CFG->version, 2013111800, 2) >= 0) { $r_ctx = context::instance_by_id($r_qca->contextid); } else { $r_ctx = get_context_instance_by_id($r_qca->contextid); } $r_qcci = RWSGCFCat($r_ctx); if ($r_qcci != $r_cid) { if (is_siteadmin($RWSUID)) { if ($r_qcci != SITEID) { RWSSErr("2109"); } } else { RWSSErr("2084"); } } $r_pm = RWSGSOpt("qcount", PARAM_ALPHANUM); if ($r_pm === false || strlen($r_pm) == 0) { RWSSErr("2085"); } $r_qct = intval($r_pm); if ($r_qct <= 0) { RWSSErr("2085"); } $r_pm = RWSGSOpt("qgrade", PARAM_NOTAGS); if ($r_pm === false || strlen($r_pm) == 0) { RWSSErr("2086"); } $r_qg = round(floatval($r_pm)); if ($r_qg <= 0) { RWSSErr("2086"); } $r_mr = $DB->get_record("modules", array("id" => $r_cmod->module)); if ($r_mr === false) { RWSSErr("2043"); } $r_qiz = $DB->get_record($r_mr->name, array("id" => $r_cmod->instance)); if ($r_qiz === false) { RWSSErr("2044"); } if (!isset($r_qiz->instance)) { $r_qiz->instance = $r_qiz->id; } $r_aerr = 0; for ($r_i = 0; $r_i < $r_qct; $r_i++) { $r_qst = new stdClass(); $r_qst->qtype = RWSRND; $r_qst->parent = 0; $r_qst->hidden = 0; $r_qst->length = 1; $r_qst->questiontext = 1; if (respondusws_floatcompare($CFG->version, 2011070100, 2) >= 0) { $r_rqt = question_bank::get_qtype("random"); $r_qst->name = $r_rqt->question_name($r_qca, !empty($r_qst->questiontext)); } else { $r_qst->name = random_qtype::question_name($r_qca, !empty($r_qst->questiontext)); } $r_qst->questiontextformat = FORMAT_HTML; $r_qst->penalty = 0; if (respondusws_floatcompare($CFG->version, 2011070100, 2) >= 0) { $r_qst->defaultmark = $r_qg; } else { $r_qst->defaultgrade = $r_qg; } $r_qst->generalfeedback = ""; $r_qst->generalfeedbackformat = FORMAT_HTML; $r_qst->category = $r_qca->id; $r_qst->stamp = make_unique_id_code(); $r_qst->createdby = $RWSUID; $r_qst->modifiedby = $RWSUID; $r_qst->timecreated = time(); $r_qst->timemodified = time(); $r_qst->id = $DB->insert_record("question", $r_qst); $DB->set_field("question", "parent", $r_qst->id, array("id" => $r_qst->id)); $r_h = question_hash($r_qst); $DB->set_field("question", "version", $r_h, array("id" => $r_qst->id)); if (respondusws_floatcompare($CFG->version, 2011070100, 2) >= 0) { quiz_add_quiz_question($r_qst->id, $r_qiz); } else { $r_ok = quiz_add_quiz_question($r_qst->id, $r_qiz); if (!$r_ok) { $DB->delete_records("question", array("id" => $r_qst->id)); $r_aerr++; } } } if ($r_aerr > 0) { RWSSErr("2087,{$r_aerr}"); } if ($r_aerr < $r_qct) { quiz_delete_previews($r_qiz); } if (respondusws_floatcompare($CFG->version, 2014051200, 2) >= 0) { quiz_update_sumgrades($r_qiz); } else { $r_qiz->grades = quiz_get_all_question_grades($r_qiz); $r_sumg = array_sum($r_qiz->grades); $DB->set_field("quiz", "sumgrades", $r_sumg, array("id" => $r_qiz->id)); } RWSSStat("1006"); }