mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 17:06:53 +02:00
MDL-57915 mod_data: Move view completion and event code to function
This commit is contained in:
parent
1034421264
commit
023ffa656d
3 changed files with 74 additions and 14 deletions
|
@ -4101,3 +4101,33 @@ function data_set_config(&$database, $key, $value) {
|
|||
$DB->set_field('data', 'config', $database->config, ['id' => $database->id]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the activity completed (if required) and trigger the course_module_viewed event.
|
||||
*
|
||||
* @param stdClass $data data object
|
||||
* @param stdClass $course course object
|
||||
* @param stdClass $cm course module object
|
||||
* @param stdClass $context context object
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
function data_view($data, $course, $cm, $context) {
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/completionlib.php');
|
||||
|
||||
// Trigger course_module_viewed event.
|
||||
$params = array(
|
||||
'context' => $context,
|
||||
'objectid' => $data->id
|
||||
);
|
||||
|
||||
$event = \mod_data\event\course_module_viewed::create($params);
|
||||
$event->add_record_snapshot('course_modules', $cm);
|
||||
$event->add_record_snapshot('course', $course);
|
||||
$event->add_record_snapshot('data', $data);
|
||||
$event->trigger();
|
||||
|
||||
// Completion.
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
}
|
||||
|
|
|
@ -879,4 +879,46 @@ class mod_data_lib_testcase extends advanced_testcase {
|
|||
$config = json_decode($database->config);
|
||||
$this->assertEquals($value, $config->$key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test data_view
|
||||
* @return void
|
||||
*/
|
||||
public function test_data_view() {
|
||||
global $CFG;
|
||||
|
||||
$CFG->enablecompletion = 1;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
||||
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
|
||||
array('completion' => 2, 'completionview' => 1));
|
||||
$context = context_module::instance($data->cmid);
|
||||
$cm = get_coursemodule_from_instance('data', $data->id);
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
data_view($data, $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_data\event\course_module_viewed', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
$moodleurl = new \moodle_url('/mod/data/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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
require_once(__DIR__ . '/../../config.php');
|
||||
require_once($CFG->dirroot . '/mod/data/lib.php');
|
||||
require_once($CFG->libdir . '/rsslib.php');
|
||||
require_once($CFG->libdir . '/completionlib.php');
|
||||
|
||||
/// One of these is necessary!
|
||||
$id = optional_param('id', 0, PARAM_INT); // course module id
|
||||
|
@ -255,15 +254,8 @@
|
|||
set_user_preference('data_perpage_'.$data->id, $perpage);
|
||||
}
|
||||
|
||||
$params = array(
|
||||
'context' => $context,
|
||||
'objectid' => $data->id
|
||||
);
|
||||
$event = \mod_data\event\course_module_viewed::create($params);
|
||||
$event->add_record_snapshot('course_modules', $cm);
|
||||
$event->add_record_snapshot('course', $course);
|
||||
$event->add_record_snapshot('data', $data);
|
||||
$event->trigger();
|
||||
// Completion and trigger events.
|
||||
data_view($data, $course, $cm, $context);
|
||||
|
||||
$urlparams = array('d' => $data->id);
|
||||
if ($record) {
|
||||
|
@ -300,10 +292,6 @@
|
|||
$PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
|
||||
}
|
||||
|
||||
// Mark as viewed
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
|
||||
/// Print the page header
|
||||
// Note: MDL-19010 there will be further changes to printing header and blocks.
|
||||
// The code will be much nicer than this eventually.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue