Skip to content

Commit

Permalink
Adding the fixes from pull request #734 for Moodle 4.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Aug 5, 2024
1 parent c4ce6ce commit b1f755c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
15 changes: 13 additions & 2 deletions classes/modules/turnitin_quiz.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions tests/modules/turnitin_assign_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/modules/turnitin_forum_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
18 changes: 13 additions & 5 deletions tests/modules/turnitin_quiz_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b1f755c

Please sign in to comment.