From b1f755cd95d3ef19214c3299df535ba195eea9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luca=20B=C3=B6sch?= Date: Mon, 5 Aug 2024 22:44:12 +0200 Subject: [PATCH] Adding the fixes from pull request #734 for Moodle 4.1. --- classes/modules/turnitin_quiz.class.php | 15 +++++++++++++-- tests/modules/turnitin_assign_test.php | 6 ++++++ tests/modules/turnitin_forum_test.php | 9 +++++++++ tests/modules/turnitin_quiz_test.php | 18 +++++++++++++----- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/classes/modules/turnitin_quiz.class.php b/classes/modules/turnitin_quiz.class.php index 9746bffc..3802630d 100644 --- a/classes/modules/turnitin_quiz.class.php +++ b/classes/modules/turnitin_quiz.class.php @@ -150,8 +150,13 @@ public function update_mark($attemptid, $identifier, $userid, $grade, $quizgrade global $DB; $transaction = $DB->start_delegated_transaction(); + if (class_exists('\mod_quiz\quiz_attempt')) { + $quizattemptclass = '\mod_quiz\quiz_attempt'; + } else { + $quizattemptclass = 'quiz_attempt'; + } - $attempt = \mod_quiz\quiz_attempt::create($attemptid); + $attempt = $quizattemptclass::create($attemptid); $quba = question_engine::load_questions_usage_by_activity($attempt->get_uniqueid()); // Loop through each question slot. @@ -177,7 +182,13 @@ public function update_mark($attemptid, $identifier, $userid, $grade, $quizgrade $update->sumgrades = $quba->get_total_mark(); $DB->update_record('quiz_attempts', $update); - $attempt->get_quizobj()->get_grade_calculator()->recompute_final_grade($userid); + if (class_exists('\mod_quiz\grade_calculator')) { + // Support Moodle 4.3+. + $attempt->get_quizobj()->get_grade_calculator()->recompute_final_grade($userid); + } else { + // Support older Moodle versions. + quiz_save_best_grade($attempt->get_quiz(), $userid); + } $transaction->allow_commit(); } diff --git a/tests/modules/turnitin_assign_test.php b/tests/modules/turnitin_assign_test.php index ca8697d1..0f43aacd 100644 --- a/tests/modules/turnitin_assign_test.php +++ b/tests/modules/turnitin_assign_test.php @@ -46,6 +46,12 @@ final class turnitin_assign_test extends \advanced_testcase { */ public $assign; + /** @var stdClass created in setUp. */ + protected $course; + + /** @var stdClass created in setUp. */ + protected $assign; + /** * Create a course and assignment module instance */ diff --git a/tests/modules/turnitin_forum_test.php b/tests/modules/turnitin_forum_test.php index d7071ce4..42e3eada 100644 --- a/tests/modules/turnitin_forum_test.php +++ b/tests/modules/turnitin_forum_test.php @@ -51,6 +51,15 @@ final class turnitin_forum_test extends \advanced_testcase { */ public $post; + /** @var stdClass created in setUp. */ + protected $forum; + + /** @var stdClass created in setUp. */ + protected $discussion; + + /** @var stdClass created in setUp. */ + protected $post; + /** * Create a course and forum module instance */ diff --git a/tests/modules/turnitin_quiz_test.php b/tests/modules/turnitin_quiz_test.php index a1f6967f..39a435d0 100644 --- a/tests/modules/turnitin_quiz_test.php +++ b/tests/modules/turnitin_quiz_test.php @@ -58,8 +58,16 @@ public function test_update_mark(): void { 'layout' => '1,0', ]); - $quizobj = \mod_quiz\quiz_settings::create($quiz->id, $user->id); - $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context()); + if (class_exists('\mod_quiz\quiz_settings')) { + $quizsettingsclass = '\mod_quiz\quiz_settings'; + $quizattemptclass = '\mod_quiz\quiz_attempt'; + } else { + $quizsettingsclass = 'quiz'; + $quizattemptclass = 'quiz_attempt'; + } + + $quizobj = $quizsettingsclass::create($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); $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); @@ -72,11 +80,11 @@ public function test_update_mark(): void { $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user->id); quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow); quiz_attempt_save_started($quizobj, $quba, $attempt); - $attemptobj = \mod_quiz\quiz_attempt::create($attempt->id); + $attemptobj = $quizattemptclass::create($attempt->id); $attemptobj->process_finish($timenow, false); // Expect no marks or grade for the attempt yet. - $attemptobj = \mod_quiz\quiz_attempt::create($attempt->id); + $attemptobj = $quizattemptclass::create($attempt->id); $this->assertEquals(0.0, $attemptobj->get_sum_marks()); $grade = quiz_get_best_grade($quiz, $user->id); $this->assertEquals(0.0, $grade); @@ -89,7 +97,7 @@ public function test_update_mark(): void { $tiiquiz->update_mark($attempt->id, $identifier, $user->id, 75, $quiz->grade); // Reload the attempt and check the total marks and grade are as we expect it. - $attemptobj = \mod_quiz\quiz_attempt::create($attempt->id); + $attemptobj = $quizattemptclass::create($attempt->id); $this->assertEquals(0.75, $attemptobj->get_sum_marks()); $grade = quiz_get_best_grade($quiz, $user->id); $this->assertEquals(75.0, $grade);