I am writing a custom Moodle (2.6) web service for taking up the moodle quiz, that requires to create an attempt or start an attempt. I written the API to start an attempt as well as get the questions for a particular quiz. However I m not able to figure out how to save the User responses for the question attempt. (Question attempt step data). Will appreciate if anyone can help me.
//Fetch all the user attempts
$attempts = quiz_get_user_attempts($quizObj->get_quizid(), 3, 'all', true);
//Get the last attempt
$lastattempt = end($attempts);
// Delete any previous preview attempts belonging to this user.
quiz_delete_previews($quizObj->get_quiz(), $USER->id);
//fetch the quiz usage object
$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 ($lastAttemptStatus == quiz_attempt::FINISHED) {
$attemptnumber = $lastattempt->attempt + 1;
$attempt = quiz_start_new_attempt($quizObj, $quba, $attempt, $attemptnumber, $timenow);
} elseif ($lastAttemptStatus == quiz_attempt::IN_PROGRESS) {
$attempt = quiz_start_attempt_built_on_last($quba, $attempt, $lastattempt);
}
// It is here Lets say I would like to hardcode (get param) the responses from the user for a quiz for a specific question must go.. and I have add these responses as attempt step data..
$transaction = $DB->start_delegated_transaction();
$attempt = quiz_attempt_save_started($quizObj, $quba, $attempt);
quiz_fire_attempt_started_event($attempt, $quizObj);
$transaction->allow_commit();
$lastattempt = end($attempts);
For saving quiz attempts you need to call mod_quiz_save_attempt API with question and answers response in parameters.
For eg:
http://**yourdomain.com**/webservice/rest/server.php?wstoken=**yourToken**&wsfunction=mod_quiz_save_attempt&moodlewsrestformat=json&attemptid=4545&data[0][name]=slots&data[0][value]=2&data[1][name]=q87124:2_:sequencecheck&data[1][value]=1&data[2][name]=q87124:2_answer&data[2][value]=2
For more details refer this link