예제 #1
0
    /**
     * Create a quiz, add five questions to the quiz
     * which are all on one page and return the quiz object.
     */
    private function get_quiz_object() {
        global $SITE;
        $this->resetAfterTest(true);

        // Make a quiz.
        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');

        $quiz = $quizgenerator->create_instance(array(
                'course' => $SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2));

        // Create five questions.
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
        $cat = $questiongenerator->create_question_category();

        $shortanswer = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
        $numerical = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
        $essay = $questiongenerator->create_question('essay', null, array('category' => $cat->id));
        $truefalse = $questiongenerator->create_question('truefalse', null, array('category' => $cat->id));
        $match = $questiongenerator->create_question('match', null, array('category' => $cat->id));

        // Add them to the quiz.
        quiz_add_quiz_question($shortanswer->id, $quiz);
        quiz_add_quiz_question($numerical->id, $quiz);
        quiz_add_quiz_question($essay->id, $quiz);
        quiz_add_quiz_question($truefalse->id, $quiz);
        quiz_add_quiz_question($match->id, $quiz);

        // Return the quiz object.
        return \mod_quiz\structure::create_for($quiz);
    }
예제 #2
0
    /**
     * Test removing slots from a quiz.
     */
    public function test_quiz_remove_slot() {
        global $SITE, $DB;

        $this->resetAfterTest(true);
        $this->setAdminUser();

        // Setup a quiz with 1 standard and 1 random question.
        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
        $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));

        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
        $cat = $questiongenerator->create_question_category();
        $standardq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));

        quiz_add_quiz_question($standardq->id, $quiz);
        quiz_add_random_questions($quiz, 0, $cat->id, 1, false);

        // Get the random question.
        $randomq = $DB->get_record('question', array('qtype' => 'random'));

        $structure = \mod_quiz\structure::create_for($quiz);

        // Check that the setup looks right.
        $this->assertEquals(2, $structure->get_question_count());
        $this->assertEquals($standardq->id, $structure->get_question_in_slot(1)->questionid);
        $this->assertEquals($randomq->id, $structure->get_question_in_slot(2)->questionid);

        // Remove the standard question.
        $structure->remove_slot($quiz, 1);

        $alteredstructure = \mod_quiz\structure::create_for($quiz);

        // Check the new ordering, and that the slot number was updated.
        $this->assertEquals(1, $alteredstructure->get_question_count());
        $this->assertEquals($randomq->id, $alteredstructure->get_question_in_slot(1)->questionid);

        // Check that the ordinary question was not deleted.
        $this->assertTrue($DB->record_exists('question', array('id' => $standardq->id)));

        // Remove the random question.
        $structure->remove_slot($quiz, 1);
        $alteredstructure = \mod_quiz\structure::create_for($quiz);

        // Check that new ordering.
        $this->assertEquals(0, $alteredstructure->get_question_count());

        // Check that the random question was deleted.
        $this->assertFalse($DB->record_exists('question', array('id' => $randomq->id)));
    }