MDL-52669 mod_quiz: Move logging in view.php to a new API function
This commit is contained in:
@@ -2122,3 +2122,29 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number,
|
||||
quiz_add_quiz_question($question->id, $quiz, $addonpage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the activity completed (if required) and trigger the course_module_viewed event.
|
||||
*
|
||||
* @param stdClass $quiz quiz object
|
||||
* @param stdClass $course course object
|
||||
* @param stdClass $cm course module object
|
||||
* @param stdClass $context context object
|
||||
* @since Moodle 3.1
|
||||
*/
|
||||
function quiz_view($quiz, $course, $cm, $context) {
|
||||
|
||||
$params = array(
|
||||
'objectid' => $quiz->id,
|
||||
'context' => $context
|
||||
);
|
||||
|
||||
$event = \mod_quiz\event\course_module_viewed::create($params);
|
||||
$event->add_record_snapshot('quiz', $quiz);
|
||||
$event->trigger();
|
||||
|
||||
// Completion.
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
||||
* @copyright 2008 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_quiz_locallib_testcase extends basic_testcase {
|
||||
class mod_quiz_locallib_testcase extends advanced_testcase {
|
||||
|
||||
public function test_quiz_rescale_grade() {
|
||||
$quiz = new stdClass();
|
||||
@@ -154,4 +154,45 @@ class mod_quiz_locallib_testcase extends basic_testcase {
|
||||
$this->assertEquals('<span class="questionname">The question name</span> ' .
|
||||
'<span class="questiontext">What sort of INEQUALITY is x < y[?]</span>', $summary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test quiz_view
|
||||
* @return void
|
||||
*/
|
||||
public function test_quiz_view() {
|
||||
global $CFG;
|
||||
|
||||
$CFG->enablecompletion = 1;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
||||
$quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
|
||||
array('completion' => 2, 'completionview' => 1));
|
||||
$context = context_module::instance($quiz->cmid);
|
||||
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
quiz_view($quiz, $course, $cm, $context);
|
||||
|
||||
$events = $sink->get_events();
|
||||
// 2 additional events thanks to completion.
|
||||
$this->assertCount(3, $events);
|
||||
$event = array_shift($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_quiz\event\course_module_viewed', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
$moodleurl = new \moodle_url('/mod/quiz/view.php', array('id' => $cm->id));
|
||||
$this->assertEquals($moodleurl, $event->get_url());
|
||||
$this->assertEventContextNotUsed($event);
|
||||
$this->assertNotEmpty($event->get_name());
|
||||
// Check completion status.
|
||||
$completion = new completion_info($course);
|
||||
$completiondata = $completion->get_data($cm);
|
||||
$this->assertEquals(1, $completiondata->completionstate);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-11
@@ -69,17 +69,8 @@ $accessmanager = new quiz_access_manager($quizobj, $timenow,
|
||||
has_capability('mod/quiz:ignoretimelimits', $context, null, false));
|
||||
$quiz = $quizobj->get_quiz();
|
||||
|
||||
// Log this request.
|
||||
$params = array(
|
||||
'objectid' => $quiz->id,
|
||||
'context' => $context
|
||||
);
|
||||
$event = \mod_quiz\event\course_module_viewed::create($params);
|
||||
$event->add_record_snapshot('quiz', $quiz);
|
||||
$event->trigger();
|
||||
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
// Trigger course_module_viewed event and completion.
|
||||
quiz_view($quiz, $course, $cm, $context);
|
||||
|
||||
// Initialize $PAGE, compute blocks.
|
||||
$PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id));
|
||||
|
||||
Reference in New Issue
Block a user