Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StudentQuiz: There are some issues of Add completion criteria #732619 #474

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion attempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@

$transaction->allow_commit();
// Update completion state.
\mod_studentquiz\completion\custom_completion::update_state($COURSE, $cm);
\mod_studentquiz\completion\custom_completion::trigger_completion_state_update($COURSE, $cm);

// Navigate accordingly. If no navigation button has been submitted, then there has been a question answer attempt.
if (optional_param('next', null, PARAM_BOOL)) {
Expand Down
10 changes: 7 additions & 3 deletions classes/completion/custom_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function get_state(string $rule): int {
$report = new mod_studentquiz_report($this->cm->id, $this->userid);
$userstats = $report->get_user_stats();

if (!$userstats) {
return COMPLETION_INCOMPLETE;
}

switch ($rule) {
case 'completionpoint':
$status = $studentquiz->completionpoint <= (int) $userstats->points;
Expand Down Expand Up @@ -94,15 +98,15 @@ public function get_sort_order(): array {
}

/**
* Update completion state for a given user on a given StudentQuiz.
* Trigger completion state update for a given user on a given StudentQuiz.
*
* @param stdClass $course The course containing the StudentQuiz to update.
* @param stdClass|cm_info $cm The cm for the StudentQuiz to update.
* @param int|null $userid The user to update state for.
*/
public static function update_state(stdClass $course, $cm, ?int $userid = null): void {
public static function trigger_completion_state_update(stdClass $course, $cm, ?int $userid = null): void {
$completion = new \completion_info($course);
if ($completion->is_enabled($cm)) {
if ($completion->is_enabled($cm) && $cm->completion != COMPLETION_TRACKING_MANUAL) {
$completion->update_state($cm, COMPLETION_UNKNOWN, $userid);
}
}
Expand Down
2 changes: 1 addition & 1 deletion classes/external/update_question_state.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static function execute($courseid, $cmid, $studentquizquestionid, $state)
}

// Update completion state.
\mod_studentquiz\completion\custom_completion::update_state($course, $cm,
\mod_studentquiz\completion\custom_completion::trigger_completion_state_update($course, $cm,
$studentquizquestion->get_question()->createdby);

$result = [];
Expand Down
2 changes: 1 addition & 1 deletion classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function question_created(\core\event\question_created $event) {
mod_studentquiz_ensure_studentquiz_question_record($event->objectid, $event->contextinstanceid);
utils::ensure_question_version_status_is_correct($event->objectid);
// Update completion state.
\mod_studentquiz\completion\custom_completion::update_state($COURSE, $cm);
\mod_studentquiz\completion\custom_completion::trigger_completion_state_update($COURSE, $cm);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lang/en/studentquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

$string['completionquestionapproved'] = 'Minimum number of unique approved questions required:';
$string['completionquestionapprovedgroup'] = 'Require created approved questions';
$string['completionquestionapprovedgroup_help'] = 'the minimum number of unique questions that a student must author and be approved before the activity is completed. This option can be used with either the Question publishing "Requires approval before publishing" or "Auto-approval" setting, but won\'t be as effective with the latter setting, in case auto-approved questions are later hidden, deleted, or otherwise removed.';
$string['completionquestionapprovedgroup_help'] = 'The minimum number of unique questions that a student must author and be approved before the activity is completed. This option can be used with either the Question publishing "Requires approval before publishing" or "Auto-approval" setting, but won\'t be as effective with the latter setting, in case auto-approved questions are later hidden, deleted, or otherwise removed.';

$string['completionquestionpublished'] = 'Minimum number of unique authored questions required:';
$string['completionquestionpublishedgroup'] = 'Require published questions';
Expand Down
2 changes: 1 addition & 1 deletion save.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
$studentquizquestionid, null, $studentquiz);

// Update completion state.
\mod_studentquiz\completion\custom_completion::update_state(
\mod_studentquiz\completion\custom_completion::trigger_completion_state_update(
$course, $cm, $studentquizquestion->get_question()->createdby);

header('Content-Type: text/html; charset=utf-8');
60 changes: 60 additions & 0 deletions tests/custom_completion_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

use advanced_testcase;
use mod_studentquiz\completion\custom_completion;
use mod_studentquiz\local\studentquiz_question;
use cm_info;
use mod_studentquiz\local\studentquiz_helper;

/**
* Class for unit testing mod_studentquiz/custom_completion.
Expand Down Expand Up @@ -69,4 +71,62 @@ public function test_get_custom_rule_descriptions() {
}
}

/**
* Test trigger completion state update function in custom_completion.
*
* @covers ::trigger_completion_state_update
*/
public function test_trigger_completion_state_update(): void {
$this->resetAfterTest();
global $DB;

$generator = $this->getDataGenerator();
$questiongenerator = $generator->get_plugin_generator('core_question');

// Prepare course.
$course = $generator->create_course([
'enablecompletion' => COMPLETION_ENABLED
]);

// Prepare users.
$student = $generator->create_user(['firstname' => 'Student', 'lastname' => '1',
'email' => '[email protected]']);

// Users enrolments.
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');

// Prepare studentquiz.
$studentquizdata = [
'course' => $course->id,
'completionquestionapproved' => 1,
'completion' => COMPLETION_TRACKING_AUTOMATIC,
];

$cmid = $generator->create_module('studentquiz', $studentquizdata)->cmid;
$studentquiz = mod_studentquiz_load_studentquiz($cmid, \context_module::instance($cmid)->id);

// Prepare question.
$this->setUser($student);
$cm = cm_info::create(get_coursemodule_from_id('studentquiz', $cmid));
$questions = $questiongenerator->create_question('truefalse', null,
['name' => 'Student 1 Question', 'category' => $studentquiz->categoryid]);
$questions = \question_bank::load_question($questions->id);
$studentquizquestions = studentquiz_question::get_studentquiz_question_from_question($questions);

// The completion data should be empty.
$count = $DB->count_records('course_modules_completion');
$this->assertEquals(0, $count);

// Approve question.
$this->setAdminUser();
$studentquizquestions->change_state_visibility(studentquiz_helper::STATE_APPROVED);
\mod_studentquiz\completion\custom_completion::trigger_completion_state_update(
$course, $cm, $student->id
);

// The completion data should exist.
$count = $DB->count_records('course_modules_completion');
$this->assertEquals(1, $count);
}
}
Loading