public static function make(quiz $quizobj, $timenow, $canignoretimelimits)
 {
     if (empty($quizobj->get_quiz()->offlinemode_enabled) || !self::is_compatible_behaviour($quizobj->get_quiz()->preferredbehaviour)) {
         return null;
     }
     return new self($quizobj, $timenow);
 }
Exemplo n.º 2
0
    public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
        if (empty($quizobj->get_quiz()->delay1) && empty($quizobj->get_quiz()->delay2)) {
            return null;
        }

        return new self($quizobj, $timenow);
    }
Exemplo n.º 3
0
    /**
     * Render the edit page
     *
     * @param \quiz $quizobj object containing all the quiz settings information.
     * @param structure $structure object containing the structure of the quiz.
     * @param \question_edit_contexts $contexts the relevant question bank contexts.
     * @param \moodle_url $pageurl the canonical URL of this page.
     * @param array $pagevars the variables from {@link question_edit_setup()}.
     * @return string HTML to output.
     */
    public function edit_page(\quiz $quizobj, structure $structure,
            \question_edit_contexts $contexts, \moodle_url $pageurl, array $pagevars) {
        $output = '';

        // Page title.
        $output .= $this->heading_with_help(get_string('editingquizx', 'quiz',
                format_string($quizobj->get_quiz_name())), 'editingquiz', 'quiz', '',
                get_string('basicideasofquiz', 'quiz'), 2);

        // Information at the top.
        $output .= $this->quiz_state_warnings($structure);
        $output .= $this->quiz_information($structure);
        $output .= $this->maximum_grade_input($quizobj->get_quiz(), $this->page->url);
        $output .= $this->repaginate_button($structure, $pageurl);
        $output .= $this->total_marks($quizobj->get_quiz());

        // Show the questions organised into sections and pages.
        $output .= $this->start_section_list();

        $sections = $structure->get_quiz_sections();
        $lastsection = end($sections);
        foreach ($sections as $section) {
            $output .= $this->start_section($section);
            $output .= $this->questions_in_section($structure, $section, $contexts, $pagevars, $pageurl);
            if ($section === $lastsection) {
                $output .= \html_writer::start_div('last-add-menu');
                $output .= html_writer::tag('span', $this->add_menu_actions($structure, 0,
                        $pageurl, $contexts, $pagevars), array('class' => 'add-menu-outer'));
                $output .= \html_writer::end_div();
            }
            $output .= $this->end_section();
        }

        $output .= $this->end_section_list();

        // Inialise the JavaScript.
        $this->initialise_editing_javascript($quizobj->get_course(), $quizobj->get_quiz());

        // Include the contents of any other popups required.
        if ($structure->can_be_edited()) {
            $popups = '';

            $popups .= $this->question_bank_loading();
            $this->page->requires->yui_module('moodle-mod_quiz-quizquestionbank',
                    'M.mod_quiz.quizquestionbank.init',
                    array('class' => 'questionbank', 'cmid' => $structure->get_cmid()));

            $popups .= $this->random_question_form($pageurl, $contexts, $pagevars);
            $this->page->requires->yui_module('moodle-mod_quiz-randomquestion',
                    'M.mod_quiz.randomquestion.init');

            $output .= html_writer::div($popups, 'mod_quiz_edit_forms');

            // Include the question chooser.
            $output .= $this->question_chooser();
            $this->page->requires->yui_module('moodle-mod_quiz-questionchooser', 'M.mod_quiz.init_questionchooser');
        }

        return $output;
    }
Exemplo n.º 4
0
 public static function make(quiz $quizobj, $timenow, $canignoretimelimits)
 {
     if (empty($quizobj->get_quiz()->honestycheckrequired)) {
         return null;
     }
     return new self($quizobj, $timenow);
 }
Exemplo n.º 5
0
 /**
  * Create an instance of this class representing the structure of a given quiz.
  * @param \quiz $quizobj the quiz.
  * @return structure
  */
 public static function create_for_quiz($quizobj)
 {
     $structure = self::create();
     $structure->quizobj = $quizobj;
     $structure->populate_structure($quizobj->get_quiz());
     return $structure;
 }
Exemplo n.º 6
0
 public static function make(quiz $quizobj, $timenow, $canignoretimelimits)
 {
     if ($quizobj->get_quiz()->browsersecurity !== 'safebrowser') {
         return null;
     }
     return new self($quizobj, $timenow);
 }
 public static function make(quiz $quizobj, $timenow, $canignoretimelimits)
 {
     if (empty($quizobj->get_quiz()->reattemptchecker)) {
         return null;
     }
     return new self($quizobj, $timenow);
 }
 public static function make(quiz $quizobj, $timenow, $canignoretimelimits)
 {
     global $THEME;
     if (empty($quizobj->get_quiz()->gradebycategory)) {
         return null;
     }
     return new self($quizobj, $timenow);
 }
Exemplo n.º 9
0
 public static function make(quiz $quizobj, $timenow, $canignoretimelimits)
 {
     global $CFG;
     // If mobile services are off, the user won't be able to use any external app.
     if (empty($CFG->enablemobilewebservice) or empty($quizobj->get_quiz()->allowofflineattempts)) {
         return null;
     }
     return new self($quizobj, $timenow);
 }
Exemplo n.º 10
0
/**
 * Creates an object to represent a new attempt at a quiz
 *
 * Creates an attempt object to represent an attempt at the quiz by the current
 * user starting at the current time. The ->id field is not set. The object is
 * NOT written to the database.
 *
 * @param object $quizobj the quiz object to create an attempt for.
 * @param int $attemptnumber the sequence number for the attempt.
 * @param object $lastattempt the previous attempt by this user, if any. Only needed
 *         if $attemptnumber > 1 and $quiz->attemptonlast is true.
 * @param int $timenow the time the attempt was started at.
 * @param bool $ispreview whether this new attempt is a preview.
 *
 * @return object the newly created attempt object.
 */
function quiz_create_attempt(quiz $quizobj, $attemptnumber, $lastattempt, $timenow, $ispreview = false) {
    global $USER;

    $quiz = $quizobj->get_quiz();
    if ($quiz->sumgrades < 0.000005 && $quiz->grade > 0.000005) {
        throw new moodle_exception('cannotstartgradesmismatch', 'quiz',
                new moodle_url('/mod/quiz/view.php', array('q' => $quiz->id)),
                    array('grade' => quiz_format_grade($quiz, $quiz->grade)));
    }

    if ($attemptnumber == 1 || !$quiz->attemptonlast) {
        // We are not building on last attempt so create a new attempt.
        $attempt = new stdClass();
        $attempt->quiz = $quiz->id;
        $attempt->userid = $USER->id;
        $attempt->preview = 0;
        $attempt->layout = quiz_clean_layout($quiz->questions, true);
        if ($quiz->shufflequestions) {
            $attempt->layout = quiz_repaginate($attempt->layout, $quiz->questionsperpage, true);
        }
    } else {
        // Build on last attempt.
        if (empty($lastattempt)) {
            print_error('cannotfindprevattempt', 'quiz');
        }
        $attempt = $lastattempt;
    }

    $attempt->attempt = $attemptnumber;
    $attempt->timestart = $timenow;
    $attempt->timefinish = 0;
    $attempt->timemodified = $timenow;
    $attempt->state = quiz_attempt::IN_PROGRESS;

    // If this is a preview, mark it as such.
    if ($ispreview) {
        $attempt->preview = 1;
    }

    $timeclose = $quizobj->get_access_manager($timenow)->get_end_time($attempt);
    if ($timeclose === false || $ispreview) {
        $attempt->timecheckstate = null;
    } else {
        $attempt->timecheckstate = $timeclose;
    }

    return $attempt;
}
Exemplo n.º 11
0
 public function start_attempt_page(quiz $quizobj, mod_quiz_preflight_check_form $mform) {
     $output = '';
     $output .= $this->header();
     $output .= $this->quiz_intro($quizobj->get_quiz(), $quizobj->get_cm());
     ob_start();
     $mform->display();
     $output .= ob_get_clean();
     $output .= $this->footer();
     return $output;
 }
Exemplo n.º 12
0
 /**
  * Create an instance of this rule for a particular quiz.
  * @param quiz $quizobj information about the quiz in question.
  * @param int $timenow the time that should be considered as 'now'.
  */
 public function __construct($quizobj, $timenow)
 {
     $this->quizobj = $quizobj;
     $this->quiz = $quizobj->get_quiz();
     $this->timenow = $timenow;
 }
Exemplo n.º 13
0
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();
/// Create initial states for all questions in this quiz.
if (!($states = get_question_states($quizobj->get_questions(), $quizobj->get_quiz(), $attempt, $lastattemptid))) {
    print_error('cannotrestore', 'quiz');
}
/// Save all the newly created states.
foreach ($quizobj->get_questions() as $i => $question) {
    save_question_session($question, $states[$i]);
}
/// Redirect to the attempt page.
redirect($quizobj->attempt_url($attempt->id));
Exemplo n.º 14
0
/**
 * 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;
}
Exemplo n.º 15
0
/**
 * The save started question usage and quiz attempt in db and log the started attempt.
 *
 * @param quiz                       $quizobj
 * @param question_usage_by_activity $quba
 * @param object                     $attempt
 * @return object                    attempt object with uniqueid and id set.
 */
function quiz_attempt_save_started($quizobj, $quba, $attempt)
{
    global $DB;
    // Save the attempt in the database.
    question_engine::save_questions_usage_by_activity($quba);
    $attempt->uniqueid = $quba->get_id();
    $attempt->id = $DB->insert_record('quiz_attempts', $attempt);
    // Params used by the events below.
    $params = array('objectid' => $attempt->id, 'relateduserid' => $attempt->userid, 'courseid' => $quizobj->get_courseid(), 'context' => $quizobj->get_context());
    // Decide which event we are using.
    if ($attempt->preview) {
        $params['other'] = array('quizid' => $quizobj->get_quizid());
        $event = \mod_quiz\event\attempt_preview_started::create($params);
    } else {
        $event = \mod_quiz\event\attempt_started::create($params);
    }
    // Trigger the event.
    $event->add_record_snapshot('quiz', $quizobj->get_quiz());
    $event->add_record_snapshot('quiz_attempts', $attempt);
    $event->trigger();
    return $attempt;
}
Exemplo n.º 16
0
 public function start_attempt_page(quiz $quizobj, mod_quiz_preflight_check_form $mform)
 {
     $output = '';
     $output .= $this->header();
     $output .= $this->heading(format_string($quizobj->get_quiz_name(), true, array("context" => $quizobj->get_context())));
     $output .= $this->quiz_intro($quizobj->get_quiz(), $quizobj->get_cm());
     ob_start();
     $mform->display();
     $output .= ob_get_clean();
     $output .= $this->footer();
     return $output;
 }
Exemplo n.º 17
0
/**
 * Fire an event to tell the rest of Moodle a quiz attempt has started.
 *
 * @param object $attempt
 * @param quiz   $quizobj
 */
function quiz_fire_attempt_started_event($attempt, $quizobj) {
    // Trigger event.
    $eventdata = array();
    $eventdata['context'] = $quizobj->get_context();
    $eventdata['courseid'] = $quizobj->get_courseid();
    $eventdata['relateduserid'] = $attempt->userid;
    $eventdata['objectid'] = $attempt->id;
    $event = \mod_quiz\event\attempt_started::create($eventdata);
    $event->add_record_snapshot('quiz', $quizobj->get_quiz());
    $event->add_record_snapshot('quiz_attempts', $attempt);
    $event->trigger();
}