-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Khoa Nguyen Dang
committed
Oct 31, 2023
1 parent
7b1dd5d
commit 1e4f577
Showing
6 changed files
with
80 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
use advanced_testcase; | ||
use mod_studentquiz\completion\custom_completion; | ||
use mod_studentquiz\local\studentquiz_question; | ||
use cm_info; | ||
|
||
/** | ||
|
@@ -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, | ||
// ], | ||
]; | ||
} | ||
} |