/**
  * Test get_attempt_data
  */
 public function test_get_attempt_data()
 {
     global $DB;
     // Create a new quiz with one attempt started.
     list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(true);
     $quizobj = $attemptobj->get_quizobj();
     $quizobj->preload_questions();
     $quizobj->load_questions();
     $questions = $quizobj->get_questions();
     $this->setUser($this->student);
     // We receive one question per page.
     $result = mod_quiz_external::get_attempt_data($attempt->id, 0);
     $result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
     $this->assertEquals($attempt, (object) $result['attempt']);
     $this->assertEquals(1, $result['nextpage']);
     $this->assertCount(0, $result['messages']);
     $this->assertCount(1, $result['questions']);
     $this->assertEquals(1, $result['questions'][0]['slot']);
     $this->assertEquals(1, $result['questions'][0]['number']);
     $this->assertEquals('numerical', $result['questions'][0]['type']);
     $this->assertEquals('todo', $result['questions'][0]['state']);
     $this->assertEquals(get_string('notyetanswered', 'question'), $result['questions'][0]['status']);
     $this->assertFalse($result['questions'][0]['flagged']);
     $this->assertEquals(0, $result['questions'][0]['page']);
     $this->assertEmpty($result['questions'][0]['mark']);
     $this->assertEquals(1, $result['questions'][0]['maxmark']);
     // Now try the last page.
     $result = mod_quiz_external::get_attempt_data($attempt->id, 1);
     $result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
     $this->assertEquals($attempt, (object) $result['attempt']);
     $this->assertEquals(-1, $result['nextpage']);
     $this->assertCount(0, $result['messages']);
     $this->assertCount(1, $result['questions']);
     $this->assertEquals(2, $result['questions'][0]['slot']);
     $this->assertEquals(2, $result['questions'][0]['number']);
     $this->assertEquals('numerical', $result['questions'][0]['type']);
     $this->assertEquals('todo', $result['questions'][0]['state']);
     $this->assertEquals(get_string('notyetanswered', 'question'), $result['questions'][0]['status']);
     $this->assertFalse($result['questions'][0]['flagged']);
     $this->assertEquals(1, $result['questions'][0]['page']);
     // Finish previous attempt.
     $attemptobj->process_finish(time(), false);
     // Change setting and expect two pages.
     $quiz->questionsperpage = 4;
     $DB->update_record('quiz', $quiz);
     quiz_repaginate_questions($quiz->id, $quiz->questionsperpage);
     // Start with new attempt with the new layout.
     $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
     $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
     $timenow = time();
     $attempt = quiz_create_attempt($quizobj, 2, false, $timenow, false, $this->student->id);
     quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
     quiz_attempt_save_started($quizobj, $quba, $attempt);
     // We receive two questions per page.
     $result = mod_quiz_external::get_attempt_data($attempt->id, 0);
     $result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
     $this->assertCount(2, $result['questions']);
     $this->assertEquals(-1, $result['nextpage']);
     // Check questions looks good.
     $found = 0;
     foreach ($questions as $question) {
         foreach ($result['questions'] as $rquestion) {
             if ($rquestion['slot'] == $question->slot) {
                 $this->assertTrue(strpos($rquestion['html'], "qid={$question->id}") !== false);
                 $found++;
             }
         }
     }
     $this->assertEquals(2, $found);
 }
 /**
  * Test get_attempt_data with blocked questions.
  * @since 3.2
  */
 public function test_get_attempt_data_with_blocked_questions()
 {
     global $DB;
     // Create a new quiz with one attempt started and using immediatefeedback.
     list($quiz, $context, $quizobj, $attempt, $attemptobj) = $this->create_quiz_with_questions(true, false, 'immediatefeedback');
     $quizobj = $attemptobj->get_quizobj();
     // Make second question blocked by the first one.
     $structure = $quizobj->get_structure();
     $slots = $structure->get_slots();
     $structure->update_question_dependency(end($slots)->id, true);
     $quizobj->preload_questions();
     $quizobj->load_questions();
     $questions = $quizobj->get_questions();
     $this->setUser($this->student);
     // We receive one question per page.
     $result = mod_quiz_external::get_attempt_data($attempt->id, 0);
     $result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
     $this->assertEquals($attempt, (object) $result['attempt']);
     $this->assertCount(1, $result['questions']);
     $this->assertEquals(1, $result['questions'][0]['slot']);
     $this->assertEquals(1, $result['questions'][0]['number']);
     $this->assertEquals(false, $result['questions'][0]['blockedbyprevious']);
     // Now try the last page.
     $result = mod_quiz_external::get_attempt_data($attempt->id, 1);
     $result = external_api::clean_returnvalue(mod_quiz_external::get_attempt_data_returns(), $result);
     $this->assertEquals($attempt, (object) $result['attempt']);
     $this->assertCount(1, $result['questions']);
     $this->assertEquals(2, $result['questions'][0]['slot']);
     $this->assertEquals(2, $result['questions'][0]['number']);
     $this->assertEquals(true, $result['questions'][0]['blockedbyprevious']);
 }