Example #1
0
 // Unset any variables we know are not responses
 unset($responses->id);
 unset($responses->q);
 unset($responses->oldpage);
 unset($responses->newpage);
 unset($responses->review);
 unset($responses->questionids);
 unset($responses->saveattempt);
 // responses get saved anway
 unset($responses->finishattempt);
 // same as $finishattempt
 unset($responses->markall);
 unset($responses->forcenewattempt);
 // extract responses
 // $actions is an array indexed by the questions ids
 $actions = question_extract_responses($questions, $responses, $event);
 // Process each question in turn
 $questionidarray = explode(',', $questionids);
 $success = true;
 foreach ($questionidarray as $i) {
     if (!isset($actions[$i])) {
         $actions[$i]->responses = array('' => '');
         $actions[$i]->event = QUESTION_EVENTOPEN;
     }
     $actions[$i]->timestamp = $timestamp;
     if (question_process_responses($questions[$i], $states[$i], $actions[$i], $quiz, $attempt)) {
         save_question_session($questions[$i], $states[$i]);
     } else {
         $success = false;
     }
 }
Example #2
0
function game_sudoku_checkanswers()
{
    $responses = data_submitted();
    $actions = question_extract_responses($questions, $responses, $event);
}
Example #3
0
    // Create a new item in the history of question states (don't simplify!)
    $states[$historylength + 1] = array();
    $states[$historylength + 1][$id] = clone $states[$historylength][$id];
    $historylength++;
    $curstate =& $states[$historylength][$id];
    $curstate->changed = false;
    // Process the responses
    unset($form['id']);
    unset($form['quizid']);
    unset($form['continue']);
    unset($form['markall']);
    unset($form['finishattempt']);
    unset($form['back']);
    unset($form['startagain']);
    $event = $finishattempt ? QUESTION_EVENTCLOSE : QUESTION_EVENTSUBMIT;
    if ($actions = question_extract_responses($questions, $form, $event)) {
        $actions[$id]->timestamp = 0;
        // We do not care about timelimits here
        if (!question_process_responses($questions[$id], $curstate, $actions[$id], $quiz, $attempt)) {
            unset($SESSION->quizpreview);
            print_error('errorprocessingresponses', 'question', $url->out());
        }
        if (!$curstate->changed) {
            // Update the current state rather than creating a new one
            $historylength--;
            unset($states[$historylength]);
            $states = array_values($states);
            $curstate =& $states[$historylength][$id];
        }
    }
} else {
Example #4
0
function game_hiddenpicture_check_questions($id, $game, &$attempt, &$hiddenpicture, $finishattempt)
{
    global $QTYPES, $DB;
    $responses = data_submitted();
    $offsetquestions = game_sudoku_compute_offsetquestions($game->sourcemodule, $attempt, $numbers, $correctquestions);
    $questionlist = game_sudoku_getquestionlist($offsetquestions);
    $questions = game_sudoku_getquestions($questionlist);
    $actions = question_extract_responses($questions, $responses, QUESTION_EVENTSUBMIT);
    $correct = $wrong = 0;
    foreach ($questions as $question) {
        if (!array_key_exists($question->id, $actions)) {
            //no answered
            continue;
        }
        unset($state);
        unset($cmoptions);
        $question->maxgrade = 100;
        $state->responses = $actions[$question->id]->responses;
        $state->event = QUESTION_EVENTGRADE;
        $cmoptions = array();
        $QTYPES[$question->qtype]->grade_responses($question, $state, $cmoptions);
        unset($query);
        $select = "attemptid={$attempt->id}";
        $select .= " AND questionid={$question->id}";
        if (($query->id = $DB->get_field_select('game_queries', 'id', $select)) == 0) {
            print_error("problem game_hiddenpicture_check_questions (select={$select})");
        }
        $answertext = $state->responses[''];
        if ($answertext != '') {
            $grade = $state->raw_grade;
            if ($grade < 50) {
                //wrong answer
                game_update_queries($game, $attempt, $query, $grade / 100, $answertext);
                $wrong++;
            } else {
                //correct answer
                game_update_queries($game, $attempt, $query, 1, $answertext);
                $correct++;
            }
        }
    }
    $hiddenpicture->correct += $correct;
    $hiddenpicture->wrong += $wrong;
    if (!$DB->update_record('game_hiddenpicture', $hiddenpicture)) {
        print_error('game_hiddenpicture_check_questions: error updating in game_hiddenpicture');
    }
    $attempt->score = game_hidden_picture_computescore($game, $hiddenpicture);
    if (!$DB->update_record('game_attempts', $attempt)) {
        print_error('game_hiddenpicture_check_questions: error updating in game_attempt');
    }
    game_sudoku_check_last($id, $game, $attempt, $hiddenpicture, $finishattempt);
    return true;
}
Example #5
0
    $event = QUESTION_EVENTCLOSE;
} else {
    $event = QUESTION_EVENTSAVE;
}
/// Unset any variables we know are not responses
unset($responses->id);
unset($responses->q);
unset($responses->oldpage);
unset($responses->newpage);
unset($responses->review);
unset($responses->questionids);
unset($responses->finishattempt);
// same as $finishattempt
unset($responses->forcenewattempt);
/// Extract the responses. $actions will be an array indexed by the questions ids.
$actions = question_extract_responses($attemptobj->get_questions($submittedquestionids), $responses, $event);
/// Process each question in turn
$success = true;
$attempt = $attemptobj->get_attempt();
foreach ($submittedquestionids as $id) {
    if (!isset($actions[$id])) {
        $actions[$id]->responses = array('' => '');
        $actions[$id]->event = QUESTION_EVENTOPEN;
    }
    $actions[$id]->timestamp = $timenow;
    /// If a particular question was submitted, update the nexturl to go back to that question.
    if ($actions[$id]->event == QUESTION_EVENTSUBMIT) {
        $nexturl = $attemptobj->attempt_url($id);
    }
    $state = $attemptobj->get_question_state($id);
    if (question_process_responses($attemptobj->get_question($id), $state, $actions[$id], $attemptobj->get_quiz(), $attempt)) {
Example #6
0
function game_snakes_check_questions($id, $game, $attempt, $snakes)
{
    global $QTYPES, $CFG, $DB;
    $responses = data_submitted();
    if ($responses->queryid != $snakes->queryid) {
        game_snakes_play($id, $game, $attempt, $snakes);
        return;
    }
    $questionlist = $DB->get_field('game_queries', 'questionid', array('id' => $responses->queryid));
    $questions = game_sudoku_getquestions($questionlist);
    $actions = question_extract_responses($questions, $responses, QUESTION_EVENTSUBMIT);
    $correct = false;
    $query = '';
    foreach ($questions as $question) {
        if (!array_key_exists($question->id, $actions)) {
            //no answered
            continue;
        }
        unset($state);
        unset($cmoptions);
        $question->maxgrade = 100;
        $state->responses = $actions[$question->id]->responses;
        $state->event = QUESTION_EVENTGRADE;
        $state->responses[''] = game_upper($state->responses['']);
        $cmoptions = array();
        $QTYPES[$question->qtype]->grade_responses($question, $state, $cmoptions);
        unset($query);
        $query->id = $snakes->queryid;
        $grade = $state->raw_grade;
        if ($grade < 50) {
            //wrong answer
            game_update_queries($game, $attempt, $query, 0, '');
            continue;
        }
        //correct answer
        $correct = true;
        game_update_queries($game, $attempt, $query, 1, '');
    }
    //set the grade of the whole game
    game_snakes_position($id, $game, $attempt, $snakes, $correct, $query);
}
function game_sudoku_check_questions($id, $game, $attempt, $sudoku, $finishattempt)
{
    global $QTYPES, $CFG;
    $responses = data_submitted();
    $offsetquestions = game_sudoku_compute_offsetquestions($game->sourcemodule, $attempt, $numbers, $correctquestions);
    $questionlist = game_sudoku_getquestionlist($offsetquestions);
    $questions = game_sudoku_getquestions($questionlist);
    $actions = question_extract_responses($questions, $responses, QUESTION_EVENTSUBMIT);
    foreach ($questions as $question) {
        if (!array_key_exists($question->id, $actions)) {
            //no answered
            continue;
        }
        unset($state);
        unset($cmoptions);
        $question->maxgrade = 100;
        $state->responses = $actions[$question->id]->responses;
        $state->event = QUESTION_EVENTGRADE;
        $cmoptions = array();
        $QTYPES[$question->qtype]->grade_responses($question, $state, $cmoptions);
        unset($query);
        $select = "attemptid={$attempt->id} and score < 0.5";
        $select .= " AND questionid={$question->id}";
        if (($query->id = get_field_select('game_queries', 'id', $select)) == 0) {
            die("problem game_sudoku_check_questions (select={$select})");
            continue;
        }
        $answertext = $state->responses[''];
        $grade = $state->raw_grade;
        if ($grade < 50) {
            //wrong answer
            game_update_queries($game, $attempt, $query, $grade / 100, $answertext);
            continue;
        }
        //correct answer
        game_update_queries($game, $attempt, $query, 1, $answertext);
    }
    game_sudoku_check_last($id, $game, $attempt, $sudoku, $finishattempt);
}
function game_bookquiz_check_questions($id, $game, $attempt, $bookquiz)
{
    global $QTYPES, $CFG, $USER;
    $responses = data_submitted();
    $questionlist = $responses->questionids;
    $questions = game_sudoku_getquestions($questionlist);
    $actions = question_extract_responses($questions, $responses, QUESTION_EVENTSUBMIT);
    $scorequestion = 0;
    $scoreattempt = 0;
    foreach ($questions as $question) {
        if (!array_key_exists($question->id, $actions)) {
            //no answered
            continue;
        }
        unset($state);
        unset($cmoptions);
        $question->maxgrade = 100;
        $state->responses = $actions[$question->id]->responses;
        $state->event = QUESTION_EVENTGRADE;
        $cmoptions = array();
        $QTYPES[$question->qtype]->grade_responses($question, $state, $cmoptions);
        if ($state->raw_grade < 50) {
            continue;
        }
        //found one correct answer
        $chapterid = $responses->chapterid;
        if (!get_field('game_bookquiz_chapters', 'id', 'attemptid', $attempt->id, 'chapterid', $chapterid)) {
            unset($newrec);
            $newrec->attemptid = $attempt->id;
            $newrec->chapterid = $chapterid;
            if (!insert_record('game_bookquiz_chapters', $newrec, false)) {
                print_object($newrec);
                error("Can't insert to table game_bookquiz_chapters");
            }
        }
        //Have to go to next page.
        $bookquiz->lastchapterid = $responses->nextchapterid;
        if (!set_field('game_bookquiz', 'lastchapterid', $bookquiz->lastchapterid, 'id', $bookquiz->id)) {
            error("Can't compute next chapter");
        }
        $scorequestion = 1;
        $scoreattempt = $_POST['scoreattempt'];
        break;
    }
    $query->id = 0;
    $query->attemptid = $attempt->id;
    $query->gameid = $game->id;
    $query->userid = $USER->id;
    $query->sourcemodule = 'question';
    $query->questionid = $question->id;
    $query->glossaryentryid = 0;
    $query->questiontext = $question->questiontext;
    $query->timelastattempt = time();
    game_update_queries($game, $attempt, $query, $scorequestion, '');
    game_updateattempts($game, $attempt, $scoreattempt, 0);
    game_bookquiz_continue($id, $game, $attempt, $bookquiz);
}
function evaluate_quiz($acode, $jobid, $newattempt, $blended)
{
    global $USER;
    global $CFG;
    mtrace("Evaluation QUIZ Processing..." . "<BR><BR>");
    try {
        print "New Attempt is: " . $newattempt . "<BR/>";
        $detected_userid = find_userid($acode, $jobid);
        if ($detected_userid == null or $detected_userid == '') {
            throw new EvaluationError(get_string('ErrorUserIDEmpty', 'blended'), EvaluationError::USERID_IS_EMPTY);
        }
        $user_reg = blended_get_user($detected_userid, $blended);
        if ($user_reg == null) {
            throw new EvaluationError(get_string('ErrorUserNotInCourse', 'blended'), EvaluationError::USER_NOT_IN_THIS_COURSE);
        }
        $userid = $user_reg->id;
        mtrace('Obtained USERID value: ' . $userid . " OK. <BR/>");
        $quiz = get_quiz($acode);
        $attempts = quiz_get_user_attempts($quiz->id, $userid, 'all', true);
        mtrace("Searching quiz... Success." . "<BR/>");
        $uniqueid = get_uniqueid($acode);
        mtrace('Obtained uniqueid: OK. <BR/>');
        $timestamp = get_timestamp($acode);
        mtrace('Obtained timestamp: OK. <BR/>');
        if (!get_record('quiz_attempts', 'uniqueid', $uniqueid)) {
            $newattempt = true;
        } else {
            $newattempt = false;
            mtrace("User {$userid} had opened this attempt already.");
        }
        $attemptnumber = 1;
        if ($newattempt == false) {
            mtrace('Obtaining user attempt...<BR/>');
            set_attempt_unfinished($uniqueid);
            $attempt = quiz_get_user_attempt_unfinished($quiz->id, $userid);
        } elseif ($newattempt == true) {
            mtrace('Creating new attempt...<BR/>');
            $attempt = create_new_attempt($quiz, $attemptnumber, $userid, $acode, $uniqueid, $timestamp);
            // Save the attempt
            if (!insert_record('quiz_attempts', $attempt)) {
                throw new EvaluationError(get_string('ErrorCouldNotCreateAttempt', 'blended'), EvaluationError::CREATE_QUIZ_ATTEMPT_ERROR);
            }
            // Actualizamos el estado de las imágenes para indicar que ya está creado un nuevo attempt
            update_images_status($acode, $jobid);
        }
        update_question_attempts($uniqueid);
        // /*
        mtrace('<BR>Getting questions and question options... ');
        $questions = get_questions($attempt, $quiz);
        if (!get_question_options($questions)) {
            error('Could not load question options');
        }
        mtrace('Success! <BR>');
        //	print ("<BR>He obtenido questions: ");
        //print_object($questions);
        $lastattemptid = false;
        //	 if ($attempt->attempt > 1 and $quiz->attemptonlast and !$attempt->preview) {
        // Find the previous attempt
        //      if (!$lastattemptid = get_field('quiz_attempts', 'uniqueid', 'quiz', $attempt->quiz, 'userid', $attempt->userid, 'attempt', $attempt->attempt-1)) {
        //        error('Could not find previous attempt to build on');
        //  }
        //}
        //print ('He obtenido lastattemptid');
        mtrace('Getting question states... ');
        if (!($states = get_question_states($questions, $quiz, $attempt, $lastattemptid))) {
            error('Could not restore question sessions');
        }
        mtrace('Success! <BR>');
        mtrace('Getting responses... <BR>');
        $responses = get_responses($acode, $jobid, $attempt);
        //print('Estas son las responses:');
        //print_object($responses);
        //$timestamp=time();
        $event = 8;
        $actions = question_extract_responses($questions, $responses, $event);
        $questionids = get_questionids($acode);
        //	print $questionids;
        $questionidarray = explode(',', $questionids);
        $success = true;
        mtrace('<BR> Processing responses and saving session... ');
        foreach ($questionidarray as $i) {
            if (!isset($actions[$i])) {
                $actions[$i]->responses = array('' => '');
                $actions[$i]->event = QUESTION_EVENTOPEN;
            }
            $actions[$i]->timestamp = $timestamp;
            if (question_process_responses($questions[$i], $states[$i], $actions[$i], $quiz, $attempt)) {
                save_question_session($questions[$i], $states[$i]);
            } else {
                $success = false;
            }
        }
        mtrace('Success! <BR>');
        // Set the attempt to be finished
        $timestamp = time();
        //$attempt->timefinish = $timestamp;
        // Update the quiz attempt and the overall grade for the quiz
        mtrace('<BR> Finishing the attempt... ');
        // print_object ($attempt);
        if (set_field('quiz_attempts', 'timefinish', $timestamp, 'uniqueid', $uniqueid) == false) {
            throw new EvaluationError('Unable to finish the quiz attempt!', EvaluationError::FINISH_QUIZ_ATTEMPT_ERROR);
        }
        mtrace('Success! <BR>');
        if ($attempt->attempt > 1 || $attempt->timefinish > 0 and !$attempt->preview) {
            mtrace('<BR> Saving quiz grade... ');
            quiz_save_best_grade($quiz, $userid);
        }
        mtrace('Success! <BR>');
        // */
        mtrace("Process Done. <BR><BR>");
        mtrace("<center> Your quiz has been succesfully evaluated!! </center>");
    } catch (EvaluationError $e) {
        throw $e;
    }
    return;
}