/**
 * Delete the current preview, if any, and redirect to start a new preview.
 * @param int $previewid
 * @param int $questionid
 * @param object $displayoptions
 * @param object $context
 */
function restart_preview($previewid, $questionid, $displayoptions, $context)
{
    global $DB;
    if ($previewid) {
        $transaction = $DB->start_delegated_transaction();
        question_engine::delete_questions_usage_by_activity($previewid);
        $transaction->allow_commit();
    }
    redirect(question_preview_url($questionid, $displayoptions->behaviour, $displayoptions->maxmark, $displayoptions, $displayoptions->variant, $context));
}
Exemplo n.º 2
0
 protected function delete_quba()
 {
     question_engine::delete_questions_usage_by_activity($this->quba->get_id());
     $this->quba = null;
 }
Exemplo n.º 3
0
/**
 * Delete a quiz attempt.
 * @param mixed $attempt an integer attempt id or an attempt object
 *      (row of the quiz_attempts table).
 * @param object $quiz the quiz object.
 */
function quiz_delete_attempt($attempt, $quiz) {
    global $DB;
    if (is_numeric($attempt)) {
        if (!$attempt = $DB->get_record('quiz_attempts', array('id' => $attempt))) {
            return;
        }
    }

    if ($attempt->quiz != $quiz->id) {
        debugging("Trying to delete attempt $attempt->id which belongs to quiz $attempt->quiz " .
                "but was passed quiz $quiz->id.");
        return;
    }

    question_engine::delete_questions_usage_by_activity($attempt->uniqueid);
    $DB->delete_records('quiz_attempts', array('id' => $attempt->id));

    // Search quiz_attempts for other instances by this user.
    // If none, then delete record for this quiz, this user from quiz_grades
    // else recalculate best grade.
    $userid = $attempt->userid;
    if (!$DB->record_exists('quiz_attempts', array('userid' => $userid, 'quiz' => $quiz->id))) {
        $DB->delete_records('quiz_grades', array('userid' => $userid, 'quiz' => $quiz->id));
    } else {
        quiz_save_best_grade($quiz, $userid);
    }

    quiz_update_grades($quiz, $userid);
}
Exemplo n.º 4
0
/**
 * Delete a quiz attempt.
 * @param mixed $attempt an integer attempt id or an attempt object
 *      (row of the quiz_attempts table).
 * @param object $quiz the quiz object.
 */
function quiz_delete_attempt($attempt, $quiz)
{
    global $DB;
    if (is_numeric($attempt)) {
        if (!($attempt = $DB->get_record('quiz_attempts', array('id' => $attempt)))) {
            return;
        }
    }
    if ($attempt->quiz != $quiz->id) {
        debugging("Trying to delete attempt {$attempt->id} which belongs to quiz {$attempt->quiz} " . "but was passed quiz {$quiz->id}.");
        return;
    }
    if (!isset($quiz->cmid)) {
        $cm = get_coursemodule_from_instance('quiz', $quiz->id, $quiz->course);
        $quiz->cmid = $cm->id;
    }
    question_engine::delete_questions_usage_by_activity($attempt->uniqueid);
    $DB->delete_records('quiz_attempts', array('id' => $attempt->id));
    // Log the deletion of the attempt.
    $params = array('objectid' => $attempt->id, 'relateduserid' => $attempt->userid, 'context' => context_module::instance($quiz->cmid), 'other' => array('quizid' => $quiz->id));
    $event = \mod_quiz\event\attempt_deleted::create($params);
    $event->add_record_snapshot('quiz_attempts', $attempt);
    $event->trigger();
    // Search quiz_attempts for other instances by this user.
    // If none, then delete record for this quiz, this user from quiz_grades
    // else recalculate best grade.
    $userid = $attempt->userid;
    if (!$DB->record_exists('quiz_attempts', array('userid' => $userid, 'quiz' => $quiz->id))) {
        $DB->delete_records('quiz_grades', array('userid' => $userid, 'quiz' => $quiz->id));
    } else {
        quiz_save_best_grade($quiz, $userid);
    }
    quiz_update_grades($quiz, $userid);
}
 /**
  * Deletes a given paper copy question_usage_by_id; enforcing permissions and removing quiz_attempts, if applicable.
  * If preserve_associated is not set, and the paper copy QUBA is associated with a quiz attempt, the quiz attempt will be deleted.
  *
  * @param int   $usage_id               The ID of the QUBA to be deleted.
  * @param bool  $preserve_associated    If true, then any quiz with an associated quiz_attempt will not be deleted.
  *
  */
 protected function delete_usage($usage_id, $preserve_associated = false)
 {
     //require the user to be able to delete quizzes
     require_capability('mod/quiz:deleteattempts', $this->context);
     //if the usage is associated with a quiz object, remove the association before deleting
     if ($this->is_associated($usage_id)) {
         //if we've been instructed to preserve associated usages, then return without deleting the usage
         if ($preserve_associated) {
             return;
         }
         //otherwise, disassociate the usage
         $this->disassociate_usage($usage_id);
     }
     //delete the usage
     question_engine::delete_questions_usage_by_activity($usage_id);
 }
Exemplo n.º 6
0
/**
 * Deletes the question usages by activity for an offlinequiz. This function must not be
 * called if the offline quiz has attempts or scanned pages
 *
 * @param object $offlinequiz
 */
function offlinequiz_delete_template_usages($offlinequiz, $deletefiles = true)
{
    global $CFG, $DB, $OUTPUT;
    if ($groups = $DB->get_records('offlinequiz_groups', array('offlinequizid' => $offlinequiz->id), 'number', '*', 0, $offlinequiz->numgroups)) {
        foreach ($groups as $group) {
            if ($group->templateusageid) {
                question_engine::delete_questions_usage_by_activity($group->templateusageid);
                $group->templateusageid = 0;
                $DB->set_field('offlinequiz_groups', 'templateusageid', 0, array('id' => $group->id));
            }
        }
    }
    // Also delete the PDF forms if they have been created.
    if ($deletefiles) {
        return offlinequiz_delete_pdf_forms($offlinequiz);
    } else {
        return $offlinequiz;
    }
}