mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-49160 quiz: Make sure to not select an outcome grade item
When we are computing completion, make sure we select only the true grade_item, not outcome grade items.
This commit is contained in:
parent
95751e81ac
commit
1245bfd5f0
2 changed files with 97 additions and 1 deletions
|
@ -1841,7 +1841,7 @@ function quiz_get_completion_state($course, $cm, $userid, $type) {
|
|||
if ($quiz->completionpass) {
|
||||
require_once($CFG->libdir . '/gradelib.php');
|
||||
$item = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod',
|
||||
'itemmodule' => 'quiz', 'iteminstance' => $cm->instance));
|
||||
'itemmodule' => 'quiz', 'iteminstance' => $cm->instance, 'outcomeid' => null));
|
||||
if ($item) {
|
||||
$grades = grade_grade::fetch_users_grades($item, array($userid), false);
|
||||
if (!empty($grades[$userid])) {
|
||||
|
|
|
@ -126,4 +126,100 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
|||
$count = $DB->count_records('quiz', array('id' => $quiz->id));
|
||||
$this->assertEquals(0, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test checking the completion state of a quiz.
|
||||
*/
|
||||
public function test_quiz_get_completion_state() {
|
||||
global $CFG, $DB;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Enable completion before creating modules, otherwise the completion data is not written in DB.
|
||||
$CFG->enablecompletion = true;
|
||||
|
||||
// Create a course and student.
|
||||
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
|
||||
$passstudent = $this->getDataGenerator()->create_user();
|
||||
$failstudent = $this->getDataGenerator()->create_user();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$this->assertNotEmpty($studentrole);
|
||||
|
||||
// Enrol students.
|
||||
$this->assertTrue($this->getDataGenerator()->enrol_user($passstudent->id, $course->id, $studentrole->id));
|
||||
$this->assertTrue($this->getDataGenerator()->enrol_user($failstudent->id, $course->id, $studentrole->id));
|
||||
|
||||
// Make a scale and an outcome.
|
||||
$scale = $this->getDataGenerator()->create_scale();
|
||||
$data = array('courseid' => $course->id,
|
||||
'fullname' => 'Team work',
|
||||
'shortname' => 'Team work',
|
||||
'scaleid' => $scale->id);
|
||||
$outcome = $this->getDataGenerator()->create_grade_outcome($data);
|
||||
|
||||
// Make a quiz with the outcome on.
|
||||
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
||||
$data = array('course' => $course->id,
|
||||
'outcome_'.$outcome->id => 1,
|
||||
'grade' => 100.0,
|
||||
'questionsperpage' => 0,
|
||||
'sumgrades' => 1,
|
||||
'completion' => COMPLETION_TRACKING_AUTOMATIC,
|
||||
'completionpass' => 1);
|
||||
$quiz = $quizgenerator->create_instance($data);
|
||||
$cm = get_coursemodule_from_id('quiz', $quiz->cmid);
|
||||
|
||||
// Create a couple of questions.
|
||||
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||
|
||||
$cat = $questiongenerator->create_question_category();
|
||||
$question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
|
||||
quiz_add_quiz_question($question->id, $quiz);
|
||||
|
||||
$quizobj = quiz::create($quiz->id, $passstudent->id);
|
||||
|
||||
// Set grade to pass.
|
||||
$item = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod',
|
||||
'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 'outcomeid' => null));
|
||||
$item->gradepass = 80;
|
||||
$item->update();
|
||||
|
||||
// Start the passing attempt.
|
||||
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
||||
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
||||
|
||||
$timenow = time();
|
||||
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $passstudent->id);
|
||||
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
||||
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
||||
|
||||
// Process some responses from the student.
|
||||
$attemptobj = quiz_attempt::create($attempt->id);
|
||||
$tosubmit = array(1 => array('answer' => '3.14'));
|
||||
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
||||
|
||||
// Finish the attempt.
|
||||
$attemptobj = quiz_attempt::create($attempt->id);
|
||||
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
||||
$attemptobj->process_finish($timenow, false);
|
||||
|
||||
// Start the failing attempt.
|
||||
$timenow = time();
|
||||
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $failstudent->id);
|
||||
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
||||
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
||||
|
||||
// Process some responses from the student.
|
||||
$attemptobj = quiz_attempt::create($attempt->id);
|
||||
$tosubmit = array(1 => array('answer' => '0'));
|
||||
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
|
||||
|
||||
// Finish the attempt.
|
||||
$attemptobj = quiz_attempt::create($attempt->id);
|
||||
$this->assertTrue($attemptobj->has_response_to_at_least_one_graded_question());
|
||||
$attemptobj->process_finish($timenow, false);
|
||||
|
||||
// Check the results.
|
||||
$this->assertTrue(quiz_get_completion_state($course, $cm, $passstudent->id, 'return'));
|
||||
$this->assertFalse(quiz_get_completion_state($course, $cm, $failstudent->id, 'return'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue