MDL-41540 remove use of $USER
authorJamie Pratt <me@jamiep.org>
Tue, 3 Sep 2013 05:15:00 +0000 (12:15 +0700)
committerJamie Pratt <me@jamiep.org>
Tue, 10 Sep 2013 07:36:30 +0000 (14:36 +0700)
by quiz_create_attempt internally.

mod/quiz/locallib.php
mod/quiz/tests/attempt_walkthrough_from_csv_test.php
mod/quiz/tests/attempt_walkthrough_test.php

index 51b34d7..61313bf 100644 (file)
@@ -86,12 +86,17 @@ define('QUIZ_SHOWIMAGE_LARGE', 2);
  *         if $attemptnumber > 1 and $quiz->attemptonlast is true.
  * @param int $timenow the time the attempt was started at.
  * @param bool $ispreview whether this new attempt is a preview.
+ * @param int $userid  the id of the user attempting this quiz.
  *
  * @return object the newly created attempt object.
  */
-function quiz_create_attempt(quiz $quizobj, $attemptnumber, $lastattempt, $timenow, $ispreview = false) {
+function quiz_create_attempt(quiz $quizobj, $attemptnumber, $lastattempt, $timenow, $ispreview = false, $userid = null) {
     global $USER;
 
+    if ($userid === null) {
+        $userid = $USER->id;
+    }
+
     $quiz = $quizobj->get_quiz();
     if ($quiz->sumgrades < 0.000005 && $quiz->grade > 0.000005) {
         throw new moodle_exception('cannotstartgradesmismatch', 'quiz',
@@ -103,7 +108,7 @@ function quiz_create_attempt(quiz $quizobj, $attemptnumber, $lastattempt, $timen
         // We are not building on last attempt so create a new attempt.
         $attempt = new stdClass();
         $attempt->quiz = $quiz->id;
-        $attempt->userid = $USER->id;
+        $attempt->userid = $userid;
         $attempt->preview = 0;
         $attempt->layout = quiz_clean_layout($quiz->questions, true);
         if ($quiz->shufflequestions) {
index d88a48d..6ab298f 100644 (file)
@@ -231,14 +231,13 @@ class mod_quiz_attempt_walkthrough_from_csv_testcase extends advanced_testcase {
             if (!$user = $DB->get_record('user', $username)) {
                 $user = $this->getDataGenerator()->create_user($username);
             }
-            $this->setUser($user);
             // Start the attempt.
             $quizobj = quiz::create($this->quiz->id, $user->id);
             $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);
+            $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user->id);
             // Select variant and / or random sub question.
             if (!isset($step['variants'])) {
                 $step['variants'] = array();
index d67f3b8..862823a 100644 (file)
@@ -68,7 +68,6 @@ class mod_quiz_attempt_walkthrough_testcase extends advanced_testcase {
 
         // Make a user to do the quiz.
         $user1 = $this->getDataGenerator()->create_user();
-        $this->setUser($user1);
 
         $quizobj = quiz::create($quiz->id, $user1->id);
 
@@ -77,7 +76,7 @@ class mod_quiz_attempt_walkthrough_testcase extends advanced_testcase {
         $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 
         $timenow = time();
-        $attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
+        $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user1->id);
 
         quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);