Example #1
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.
 * @return object                The newly created attempt object.
 * @param object $quiz           The quiz to create an attempt for.
 * @param integer $attemptnumber The sequence number for the attempt.
 */
function quiz_create_attempt($quiz, $attemptnumber)
{
    global $USER, $CFG;
    if (!$attemptnumber > 1 or !$quiz->attemptonlast or !($attempt = get_record('quiz_attempts', 'quiz', $quiz->id, 'userid', $USER->id, 'attempt', $attemptnumber - 1))) {
        // we are not building on last attempt so create a new attempt
        $attempt->quiz = $quiz->id;
        $attempt->userid = $USER->id;
        $attempt->preview = 0;
        if ($quiz->shufflequestions) {
            $attempt->layout = quiz_repaginate($quiz->questions, $quiz->questionsperpage, true);
        } else {
            $attempt->layout = $quiz->questions;
        }
    }
    $timenow = time();
    $attempt->attempt = $attemptnumber;
    $attempt->sumgrades = 0.0;
    $attempt->timestart = $timenow;
    $attempt->timefinish = 0;
    $attempt->timemodified = $timenow;
    $attempt->uniqueid = question_new_attempt_uniqueid();
    return $attempt;
}
 $quiz = get_record("quiz", "id", $q);
 if (!$quiz) {
     error("Refereced quiz is not found: id=" . $q);
 }
 //TODO: cambiar por question_create_uniqueid_attempt
 // Get number for the next or unfinished attempt
 /*if(!$attemptnumber = (int)get_field_sql('SELECT MAX(attempt)+1 FROM ' .
 		"{$CFG->prefix}blended_attempts WHERE quiz = '{$quiz->id}' AND " .
 		"userid = '{$USER->id}'")) 
  	{
      	$attemptnumber = 1;
  	}
  	*/
 $timestamp = time();
 for ($i = 0; $i < $numberpaperquiz; $i++) {
     $attemptnumber = question_new_attempt_uniqueid('blended');
     $attempts[$i] = blended_generate_attempt($attemptnumber, $quiz, $USER, $timestamp);
     $jobs[$i]->quiz = $quiz->id;
     $jobs[$i]->quiz_name = $quiz->name;
     $jobs[$i]->blended = $blended->id;
     $jobs[$i]->userid = $USER->id;
     $jobs[$i]->status = JOB_STATE_WAITING;
     $jobs[$i]->timestamp = $timestamp;
     $jobs[$i]->attempt_id = $attempts[$i]->id;
     // encode options in this column
     $options = array();
     $option['identifyLabel'] = $identifyLabel;
     $option['columns'] = $columns;
     $option['logourl'] = $logourl['value'];
     $option['fontsize'] = $fontsize;
     $optStr = array_implode('=', ',', $option);
Example #3
0
function quiz_attempts_restore_mods($quiz_id, $info, $restore)
{
    global $CFG;
    $status = true;
    //Get the quiz_attempts array
    if (array_key_exists('ATTEMPTS', $info['MOD']['#'])) {
        $attempts = $info['MOD']['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
    } else {
        $attempts = array();
    }
    //Iterate over attempts
    for ($i = 0; $i < sizeof($attempts); $i++) {
        $att_info = $attempts[$i];
        //traverse_xmlize($att_info);                                                                 //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //We'll need this later!!
        $oldid = backup_todb($att_info['#']['ID']['0']['#']);
        $olduserid = backup_todb($att_info['#']['USERID']['0']['#']);
        //Now, build the ATTEMPTS record structure
        $attempt = new stdClass();
        $attempt->quiz = $quiz_id;
        $attempt->userid = backup_todb($att_info['#']['USERID']['0']['#']);
        $attempt->attempt = backup_todb($att_info['#']['ATTEMPTNUM']['0']['#']);
        $attempt->sumgrades = backup_todb($att_info['#']['SUMGRADES']['0']['#']);
        $attempt->timestart = backup_todb($att_info['#']['TIMESTART']['0']['#']);
        $attempt->timefinish = backup_todb($att_info['#']['TIMEFINISH']['0']['#']);
        $attempt->timemodified = backup_todb($att_info['#']['TIMEMODIFIED']['0']['#']);
        $attempt->layout = backup_todb($att_info['#']['LAYOUT']['0']['#']);
        $attempt->preview = backup_todb($att_info['#']['PREVIEW']['0']['#']);
        //We have to recode the userid field
        $user = backup_getid($restore->backup_unique_code, "user", $attempt->userid);
        if ($user) {
            $attempt->userid = $user->new_id;
        }
        //Set the uniqueid field
        $attempt->uniqueid = question_new_attempt_uniqueid();
        //We have to recode the layout field (a list of questions id and pagebreaks)
        $attempt->layout = quiz_recode_layout($attempt->layout, $restore);
        //The structure is equal to the db, so insert the quiz_attempts
        $newid = insert_record("quiz_attempts", $attempt);
        //Do some output
        if (($i + 1) % 10 == 0) {
            if (!defined('RESTORE_SILENTLY')) {
                echo ".";
                if (($i + 1) % 200 == 0) {
                    echo "<br />";
                }
            }
            backup_flush(300);
        }
        if ($newid) {
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, "quiz_attempts", $oldid, $newid);
            //Now process question_states
            // This function is defined in question/restorelib.php
            $status = question_states_restore_mods($attempt->uniqueid, $att_info, $restore);
        } else {
            $status = false;
        }
    }
    return $status;
}
function quiz_attempts_restore_pre15_mods($quiz_id, $info, $restore, $quizquestions)
{
    global $CFG;
    $status = true;
    //Get the quiz_attempts array
    $attempts = $info['MOD']['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
    //Iterate over attempts
    for ($i = 0; $i < sizeof($attempts); $i++) {
        $att_info = $attempts[$i];
        //traverse_xmlize($att_info);                                                                 //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //We'll need this later!!
        $oldid = backup_todb($att_info['#']['ID']['0']['#']);
        $olduserid = backup_todb($att_info['#']['USERID']['0']['#']);
        //Now, build the ATTEMPTS record structure
        $attempt->quiz = $quiz_id;
        $attempt->userid = backup_todb($att_info['#']['USERID']['0']['#']);
        $attempt->attempt = backup_todb($att_info['#']['ATTEMPTNUM']['0']['#']);
        $attempt->sumgrades = backup_todb($att_info['#']['SUMGRADES']['0']['#']);
        $attempt->timestart = backup_todb($att_info['#']['TIMESTART']['0']['#']);
        $attempt->timefinish = backup_todb($att_info['#']['TIMEFINISH']['0']['#']);
        $attempt->timemodified = backup_todb($att_info['#']['TIMEMODIFIED']['0']['#']);
        //We have to recode the userid field
        $user = backup_getid($restore->backup_unique_code, "user", $attempt->userid);
        if ($user) {
            $attempt->userid = $user->new_id;
        }
        //Set the layout field (inherited from quiz by default)
        $attempt->layout = $quizquestions;
        //Set the preview field (code from upgrade)
        $cm = get_coursemodule_from_instance('quiz', $quiz_id);
        if (has_capability('mod/quiz:preview', get_context_instance(CONTEXT_MODULE, $cm->id))) {
            $attempt->preview = 1;
        }
        //Set the uniqueid field
        $attempt->uniqueid = question_new_attempt_uniqueid();
        //The structure is equal to the db, so insert the quiz_attempts
        $newid = insert_record("quiz_attempts", $attempt);
        //Do some output
        if (($i + 1) % 10 == 0) {
            if (!defined('RESTORE_SILENTLY')) {
                echo ".";
                if (($i + 1) % 200 == 0) {
                    echo "<br />";
                }
            }
            backup_flush(300);
        }
        if ($newid) {
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, "quiz_attempts", $oldid, $newid);
            //Now process question_states (old quiz_responses table)
            $status = question_states_restore_pre15_mods($newid, $att_info, $restore);
        } else {
            $status = false;
        }
    }
    return $status;
}
Example #5
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 $quiz the quiz to create an attempt for.
 * @param integer $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 integer $timenow the time the attempt was started at.
 * @param boolean $ispreview whether this new attempt is a preview.
 *
 * @return object the newly created attempt object.
 */
function quiz_create_attempt($quiz, $attemptnumber, $lastattempt, $timenow, $ispreview = false)
{
    global $USER;
    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;
        if ($quiz->shufflequestions) {
            $attempt->layout = quiz_clean_layout(quiz_repaginate($quiz->questions, $quiz->questionsperpage, true), true);
        } else {
            $attempt->layout = quiz_clean_layout($quiz->questions, true);
        }
    } else {
        /// Build on last attempt.
        if (empty($lastattempt)) {
            print_error('cannotfindprevattempt', 'quiz');
        }
        $attempt = $lastattempt;
    }
    $attempt->attempt = $attemptnumber;
    $attempt->sumgrades = 0.0;
    $attempt->timestart = $timenow;
    $attempt->timefinish = 0;
    $attempt->timemodified = $timenow;
    $attempt->uniqueid = question_new_attempt_uniqueid();
    /// If this is a preview, mark it as such.
    if ($ispreview) {
        $attempt->preview = 1;
    }
    return $attempt;
}
 protected function process_quiz_attempt($data)
 {
     global $DB;
     $data = (object) $data;
     $oldid = $data->id;
     $olduniqueid = $data->uniqueid;
     $data->quiz = $this->get_new_parentid('quiz');
     $data->attempt = $data->attemptnum;
     $data->uniqueid = question_new_attempt_uniqueid('quiz');
     $data->userid = $this->get_mappingid('user', $data->userid);
     $data->timestart = $this->apply_date_offset($data->timestart);
     $data->timefinish = $this->apply_date_offset($data->timefinish);
     $data->timemodified = $this->apply_date_offset($data->timemodified);
     $data->layout = $this->questions_recode_layout($data->layout);
     $newitemid = $DB->insert_record('quiz_attempts', $data);
     // Save quiz_attempt->uniqueid as quiz_attempt mapping, both question_states and
     // question_sessions have Fk to it and not to quiz_attempts->id at all.
     $this->set_mapping('quiz_attempt', $olduniqueid, $data->uniqueid, false);
     // Also save quiz_attempt->id mapping, because logs use it
     $this->set_mapping('quiz_attempt_id', $oldid, $newitemid, false);
 }