From d272cc210d86d7fed98d6b6b1980e6807d8b0dd4 Mon Sep 17 00:00:00 2001
From: Damyon Wiese <damyon@moodle.com>
Date: Thu, 30 Oct 2014 13:13:25 +0800
Subject: [PATCH] NOBUG: This is test code to generate quizes with lots of
 attempts in a course.

---
 admin/tool/generator/classes/course_backend.php | 93 +++++++++++++++++++++++++
 admin/tool/generator/lang/en/tool_generator.php |  2 +
 question/engine/tests/helpers.php               |  4 +-
 3 files changed, 97 insertions(+), 2 deletions(-)

diff --git a/admin/tool/generator/classes/course_backend.php b/admin/tool/generator/classes/course_backend.php
index 554530e..724d227 100644
--- a/admin/tool/generator/classes/course_backend.php
+++ b/admin/tool/generator/classes/course_backend.php
@@ -79,6 +79,21 @@ class tool_generator_course_backend extends tool_generator_backend {
     private static $paramforumposts = array(2, 2, 5, 10, 10, 10);
 
     /**
+     * @var array Number of quizzes in course
+     */
+    private static $paramquizzes = array(2, 2, 5, 10, 10, 10);
+
+    /**
+     * @var array Number of questions per quiz
+     */
+    private static $paramquizquestions = array(2, 2, 5, 10, 10, 10);
+
+    /**
+     * @var array Number of attempts per quiz
+     */
+    private static $paramquizattempts = array(1, 50, 500, 5000, 25000, 50000);
+
+    /**
      * @var string Course shortname
      */
     private $shortname;
@@ -188,6 +203,7 @@ class tool_generator_course_backend extends tool_generator_backend {
         $this->create_small_files();
         $this->create_big_files();
         $this->create_forum();
+        $this->create_quizzes();
 
         // Log total time.
         $this->log('coursecompleted', round(microtime(true) - $entirestart, 1));
@@ -324,6 +340,83 @@ class tool_generator_course_backend extends tool_generator_backend {
     }
 
     /**
+     * Creates a number of Quiz activities with questions and attempts.
+     */
+    private function create_quizzes() {
+        global $CFG;
+        // Set up generator.
+        require_once($CFG->dirroot . '/mod/quiz/locallib.php');
+
+        $quizgenerator = $this->generator->get_plugin_generator('mod_quiz');
+        $questiongenerator = $this->generator->get_plugin_generator('core_question');
+        $cat = $questiongenerator->create_question_category();
+
+        $questiontypes = array('shortanswer', 'numerical');
+        $questions = array();
+        $answers = array();
+
+        // Create all the questions in the question bank.
+        $number = self::$paramquizquestions[$this->size];
+        $this->log('createquestions', $number, true);
+        for ($i = 0; $i < $number; $i++) {
+            $idx = $i % count($questiontypes);
+
+            $question = $questiongenerator->create_question($questiontypes[$idx], null, array('category' => $cat->id));
+
+            $questions[] = $question;
+            $answers[$i + 1] = array('answer' => '3.14');
+            $this->dot($i, $number);
+        }
+        $this->end_log();
+
+        // Create quizzes.
+        $number = self::$paramquizzes[$this->size];
+        $this->log('createquizzes', $number, true);
+        for ($i = 0; $i < $number; $i++) {
+            $record = array('course' => $this->course);
+            $options = array('section' => $this->get_target_section());
+            $quiz = $quizgenerator->create_instance($record, $options);
+
+            // Add all the questions to each quiz.
+            foreach ($questions as $question) {
+                quiz_add_quiz_question($question->id, $quiz);
+            }
+
+            // Add up all the grades for all the questions.
+            quiz_update_sumgrades($quiz);
+
+            // Add attempts.
+            $attemptnumber = self::$paramquizattempts[$this->size];
+            for ($j = 0; $j < $attemptnumber; $j++) {
+                $userid = $this->userids[$j + 1];
+                $quizobj = quiz::create($quiz->id, $userid);
+
+                // Start the attempt.
+                $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, 1, false, $timenow, false, $userid);
+
+                quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
+                quiz_attempt_save_started($quizobj, $quba, $attempt);
+
+                // Process some responses from the student.
+                $attemptobj = quiz_attempt::create($attempt->id);
+
+                $attemptobj->process_submitted_actions($timenow, false, $answers);
+
+                // Finish the attempt.
+                $attemptobj = quiz_attempt::create($attempt->id);
+                $attemptobj->process_finish($timenow, false);
+            }
+
+            $this->dot($i, $number);
+        }
+        $this->end_log();
+    }
+
+    /**
      * Creates a number of Assignment activities.
      */
     private function create_assignments() {
diff --git a/admin/tool/generator/lang/en/tool_generator.php b/admin/tool/generator/lang/en/tool_generator.php
index edaab03..af73d0a 100644
--- a/admin/tool/generator/lang/en/tool_generator.php
+++ b/admin/tool/generator/lang/en/tool_generator.php
@@ -72,6 +72,8 @@ $string['progress_checkaccounts'] = 'Checking user accounts ({$a})';
 $string['progress_coursecompleted'] = 'Course completed ({$a}s)';
 $string['progress_createaccounts'] = 'Creating user accounts ({$a->from} - {$a->to})';
 $string['progress_createassignments'] = 'Creating assignments ({$a})';
+$string['progress_createquestions'] = 'Creating questions ({$a})';
+$string['progress_createquizzes'] = 'Creating quizzes ({$a})';
 $string['progress_createbigfiles'] = 'Creating big files ({$a})';
 $string['progress_createcourse'] = 'Creating course {$a}';
 $string['progress_createforum'] = 'Creating forum ({$a} posts)';
diff --git a/question/engine/tests/helpers.php b/question/engine/tests/helpers.php
index 5516338..66f5901 100644
--- a/question/engine/tests/helpers.php
+++ b/question/engine/tests/helpers.php
@@ -478,14 +478,14 @@ abstract class testing_db_record_builder {
  * @copyright  2009 The Open University
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-abstract class data_loading_method_test_base extends advanced_testcase {
+abstract class data_loading_method_test_base extends stdClass {
     public function build_db_records(array $table) {
         return testing_db_record_builder::build_db_records($table);
     }
 }
 
 
-abstract class question_testcase extends advanced_testcase {
+abstract class question_testcase extends stdClass {
 
     public function assert($expectation, $compare, $notused = '') {
 
-- 
1.8.3.2

