Skip to content
Permalink
Browse files

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.
  • Loading branch information...
merrill-oakland committed Feb 10, 2015
1 parent 95751e8 commit 1245bfd5f0007f5e295a819a614a41dc6d217737
Showing with 97 additions and 1 deletion.
  1. +1 −1 mod/quiz/lib.php
  2. +96 −0 mod/quiz/tests/lib_test.php
@@ -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 @@ public function test_quiz_delete_instance() {
$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'));
}
}

0 comments on commit 1245bfd

Please sign in to comment.
You can’t perform that action at this time.