} $moveselectedpos = $pagebreakpositions[$moveselectedonpage - 1]; // Do the move. 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')));
if ($lastattempt && !$lastattempt->preview && !$quizobj->is_preview_user()) { $lastattemptid = $lastattempt->id; $attemptnumber = $lastattempt->attempt + 1; } else { $lastattempt = false; $lastattemptid = false; $attemptnumber = 1; } /// Check access. $messages = $accessmanager->prevent_access() + $accessmanager->prevent_new_attempt($attemptnumber - 1, $lastattempt); if (!$quizobj->is_preview_user() && $messages) { print_error('attempterror', 'quiz', $quizobj->view_url(), $accessmanager->print_messages($messages, true)); } $accessmanager->do_password_check($quizobj->is_preview_user()); /// Delete any previous preview attempts belonging to this user. quiz_delete_previews($quiz, $USER->id); /// Create the new attempt and initialize the question sessions $attempt = quiz_create_attempt($quiz, $attemptnumber, $lastattempt, time(), $quizobj->is_preview_user()); /// Save the attempt in the database. if (!($attempt->id = $DB->insert_record('quiz_attempts', $attempt))) { quiz_error($quiz, 'newattemptfail'); } /// Log the new attempt. if ($attempt->preview) { add_to_log($course->id, 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(), $quizobj->get_quizid(), $quizobj->get_cmid()); } else { add_to_log($course->id, 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id, $quizobj->get_quizid(), $quizobj->get_cmid()); } /// Fully load all the questions in this quiz. $quizobj->preload_questions(); $quizobj->load_questions();
/** * 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); }
/** * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will update an existing instance with new data. * * @param object $quiz the data that came from the form. * @return mixed true on success, false or a string error message on failure. */ function quiz_update_instance($quiz, $mform) { global $CFG, $DB; require_once $CFG->dirroot . '/mod/quiz/locallib.php'; // Process the options from the form. $result = quiz_process_options($quiz); if ($result && is_string($result)) { return $result; } // Get the current value, so we can see what changed. $oldquiz = $DB->get_record('quiz', array('id' => $quiz->instance)); // We need two values from the existing DB record that are not in the form, // in some of the function calls below. $quiz->sumgrades = $oldquiz->sumgrades; $quiz->grade = $oldquiz->grade; // Repaginate, if asked to. if (!$quiz->shufflequestions && !empty($quiz->repaginatenow)) { $quiz->questions = quiz_repaginate(quiz_clean_layout($oldquiz->questions, true), $quiz->questionsperpage); } unset($quiz->repaginatenow); // Update the database. $quiz->id = $quiz->instance; $DB->update_record('quiz', $quiz); // Do the processing required after an add or an update. quiz_after_add_or_update($quiz); if ($oldquiz->grademethod != $quiz->grademethod) { quiz_update_all_final_grades($quiz); quiz_update_grades($quiz); } $quizdateschanged = $oldquiz->timelimit != $quiz->timelimit || $oldquiz->timeclose != $quiz->timeclose || $oldquiz->graceperiod != $quiz->graceperiod; if ($quizdateschanged) { quiz_update_open_attempts(array('quizid' => $quiz->id)); } // Delete any previous preview attempts. quiz_delete_previews($quiz); return true; }
$accessmanager->setup_attempt_page($PAGE); if (empty($quizobj->get_quiz()->showblocks)) { $PAGE->blocks->show_only_fake_blocks(); } echo $output->start_attempt_page($quizobj, $mform); die; } } // Pre-flight check passed. $accessmanager->notify_preflight_check_passed($currentattemptid); } if ($currentattemptid) { redirect($quizobj->attempt_url($currentattemptid, $page)); } // Delete any previous preview attempts belonging to this user. quiz_delete_previews($quizobj->get_quiz(), $USER->id); $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); // Create the new attempt and initialize the question sessions $timenow = time(); // Update time now, in case the server is running really slowly. $attempt = quiz_create_attempt($quizobj, $attemptnumber, $lastattempt, $timenow, $quizobj->is_preview_user()); if (!($quizobj->get_quiz()->attemptonlast && $lastattempt)) { // Starting a normal, new, quiz attempt. // Fully load all the questions in this quiz. $quizobj->preload_questions(); $quizobj->load_questions(); // Add them all to the $quba. $idstoslots = array(); $questionsinuse = array_keys($quizobj->get_questions()); foreach ($quizobj->get_questions() as $i => $questiondata) {
/** * 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); }
/** * Prepare and start a new attempt deleting the previous preview attempts. * * @param quiz $quizobj quiz object * @param int $attemptnumber the attempt number * @param object $lastattempt last attempt object * @param bool $offlineattempt whether is an offline attempt or not * @return object the new attempt * @since Moodle 3.1 */ function quiz_prepare_and_start_new_attempt(quiz $quizobj, $attemptnumber, $lastattempt, $offlineattempt = false) { global $DB, $USER; // Delete any previous preview attempts belonging to this user. quiz_delete_previews($quizobj->get_quiz(), $USER->id); $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); // Create the new attempt and initialize the question sessions $timenow = time(); // Update time now, in case the server is running really slowly. $attempt = quiz_create_attempt($quizobj, $attemptnumber, $lastattempt, $timenow, $quizobj->is_preview_user()); if (!($quizobj->get_quiz()->attemptonlast && $lastattempt)) { $attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow); } else { $attempt = quiz_start_attempt_built_on_last($quba, $attempt, $lastattempt); } $transaction = $DB->start_delegated_transaction(); // Init the timemodifiedoffline for offline attempts. if ($offlineattempt) { $attempt->timemodifiedoffline = $attempt->timemodified; } $attempt = quiz_attempt_save_started($quizobj, $quba, $attempt); $transaction->allow_commit(); return $attempt; }
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"); }
private static function accessmanager_process($quizobj, $accessmanager, $forcenew, $uid) { self::new_preview_request($quizobj, $accessmanager, $forcenew, $uid); // Look for an existing attempt. $attempts = quiz_get_user_attempts($quizobj->get_quizid(), $uid, 'all', true); $lastattempt = end($attempts); // Get number for the next or unfinished attempt. if ($lastattempt && !$lastattempt->preview && !$quizobj->is_preview_user()) { $attemptnumber = $lastattempt->attempt + 1; } else { $lastattempt = false; $attemptnumber = 1; } $currentattemptid = null; $accessmanager->notify_preflight_check_passed($currentattemptid); // Delete any previous preview attempts belonging to this user. quiz_delete_previews($quizobj->get_quiz(), $uid); $res = new stdClass(); $res->lastattempt = $lastattempt; $res->attemptnumber = $attemptnumber; return $res; }
/** * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will update an existing instance with new data. * * @param object $quiz the data that came from the form. * @return mixed true on success, false or a string error message on failure. */ function quiz_update_instance($quiz, $mform) { global $CFG, $DB; // Process the options from the form. $result = quiz_process_options($quiz); if ($result && is_string($result)) { return $result; } $oldquiz = $DB->get_record('quiz', array('id' => $quiz->instance)); // Repaginate, if asked to. if (!$quiz->shufflequestions && !empty($quiz->repaginatenow)) { require_once($CFG->dirroot . '/mod/quiz/locallib.php'); $quiz->questions = quiz_repaginate(quiz_clean_layout($oldquiz->questions, true), $quiz->questionsperpage); } unset($quiz->repaginatenow); // Update the database. $quiz->id = $quiz->instance; $DB->update_record('quiz', $quiz); // Do the processing required after an add or an update. quiz_after_add_or_update($quiz); if ($oldquiz->grademethod != $quiz->grademethod) { require_once($CFG->dirroot . '/mod/quiz/locallib.php'); $quiz->sumgrades = $oldquiz->sumgrades; $quiz->grade = $oldquiz->grade; quiz_update_all_final_grades($quiz); quiz_update_grades($quiz); } // Delete any previous preview attempts. quiz_delete_previews($quiz); return true; }