Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Khoa Nguyen Dang committed Oct 31, 2023
1 parent 7b1dd5d commit 1e4f577
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 33 deletions.
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
6 changes: 1 addition & 5 deletions classes/completion/custom_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,11 @@ public function get_sort_order(): array {
* @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.
* @return bool Status check, true if update_state is triggered.
*/
public static function update_state(stdClass $course, $cm, ?int $userid = null): bool {
public static function trigger_completion_state_update(stdClass $course, $cm, ?int $userid = null): void {
$completion = new \completion_info($course);
if ($completion->is_enabled($cm) && $cm->completion != COMPLETION_TRACKING_MANUAL) {
$completion->update_state($cm, COMPLETION_UNKNOWN, $userid);
return true;
}

return false;
}
}
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 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');
99 changes: 75 additions & 24 deletions tests/custom_completion_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

/**
Expand Down Expand Up @@ -73,54 +74,104 @@ public function test_get_custom_rule_descriptions() {
* Test update state function in custom_completion.
*
* @dataProvider test_update_provider
* @covers ::update_state
* @covers ::trigger_completion_state_update
* @param int $completion The completion type: 0 - Do not indicate activity completion,
* 1 - Students can manually mark the activity as completed, 2 - Show activity as complete when conditions are met.
* @param bool $expected Expected result when run update.
*/
public function test_update_state(int $completion, bool $expected): void {
public function test_trigger_completion_state_update(int $completion, bool $expected): void {
$this->resetAfterTest();
global $DB;
global $DB, $CFG;
$CFG->enablecompletion = true;

$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);

$user = $this->getDataGenerator()->create_user();
$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($user->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');

$studentquiz = $this->getDataGenerator()->create_module('studentquiz', [
// Prepare studentquiz.
$studentquizdata = [
'course' => $course->id,
'completion' => $completion,
]);
'completionpoint' => 1,
'completion' => COMPLETION_TRACKING_AUTOMATIC,
'completionview' => 1,
];

$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));
$completioninfo = new \completion_info($cm->get_course());

$cm = cm_info::create(get_coursemodule_from_id('studentquiz', $studentquiz->cmid));
$completioninfo = new \completion_info($course);
$customcompletion = new custom_completion($cm, $user->id, $completioninfo->get_core_completion_state($cm, $user->id));
$status = $customcompletion::update_state($course, $cm, $user->id);
$completion = new \mod_studentquiz\completion\custom_completion($cm, $student->id,
$completioninfo->get_core_completion_state($cm, $student->id));
$oldstate = $completion->get_state('completionpoint');

$this->assertEquals($expected, $status);
$questions = $questiongenerator->create_question('truefalse', null,
['name' => 'Student 1 Question', 'category' => $studentquiz->categoryid]);
$questions = \question_bank::load_question($questions->id);
$attempt = mod_studentquiz_generate_attempt([$questions->id], $studentquiz, $student->id);
var_dump($attempt); die;
$questionids = explode(',', $attempt->ids);

$questionusage = \question_engine::load_questions_usage_by_activity($attempt->questionusageid);
$post = $questionusage->prepare_simulated_post_data([1 => ['answer' => 1, '-submit' => 1]]);
$questionusage->process_all_actions(null, $post);

mod_studentquiz_add_question_to_attempt($questionusage, $this->studentquiz, $questionids, 1);


$raterecord = new \stdClass();
$raterecord->rate = 5;
$raterecord->studentquizquestionid = $studentquiz->id;
$raterecord->userid = $student->id;
\mod_studentquiz\utils::save_rate($raterecord);
$studentquizquestions = studentquiz_question::get_studentquiz_question_from_question($questions);
\mod_studentquiz\completion\custom_completion::trigger_completion_state_update(
$course, $cm, $student->id
);

$status = $completion->get_state('completionpoint');
$this->assertEquals(COMPLETION_COMPLETE, $status);
}

/**
* Data provider for test_update_state() test cases.
* Data provider for test_trigger_completion_state_update() test cases.
*
* @coversNothing
* @return array List of data sets (test cases)
*/
public static function test_update_provider(): array {
return [
'Do not indicate activity completion' => [
0,
false,
],
'Students can manually mark the activity as completed' => [
1,
false,
],
'Show activity as complete when conditions are met' => [
2,
true,
false,
],
// 'Students can manually mark the activity as completed' => [
// 1,
// false,
// ],
// 'Show activity as complete when conditions are met' => [
// 2,
// true,
// ],
];
}
}

0 comments on commit 1e4f577

Please sign in to comment.