MDL-39962 - Events: Replace the legacy event workshop_viewed
[moodle.git] / mod / workshop / view.php
index 7272cfc..dfa245f 100644 (file)
@@ -41,30 +41,37 @@ $sorthow    = optional_param('sorthow', 'ASC', PARAM_ALPHA);
 $eval       = optional_param('eval', null, PARAM_PLUGIN);
 
 if ($id) {
-    $cm         = get_coursemodule_from_id('workshop', $id, 0, false, MUST_EXIST);
-    $course     = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
-    $workshop   = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
+    $cm             = get_coursemodule_from_id('workshop', $id, 0, false, MUST_EXIST);
+    $course         = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
+    $workshoprecord = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
 } else {
-    $workshop   = $DB->get_record('workshop', array('id' => $w), '*', MUST_EXIST);
-    $course     = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
-    $cm         = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
+    $workshoprecord = $DB->get_record('workshop', array('id' => $w), '*', MUST_EXIST);
+    $course         = $DB->get_record('course', array('id' => $workshoprecord->course), '*', MUST_EXIST);
+    $cm             = get_coursemodule_from_instance('workshop', $workshoprecord->id, $course->id, false, MUST_EXIST);
 }
 
 require_login($course, true, $cm);
 require_capability('mod/workshop:view', $PAGE->context);
 
-$workshop = new workshop($workshop, $cm, $course);
-$workshop->log('view');
+$workshop = new workshop($workshoprecord, $cm, $course);
 
 // Mark viewed
 $completion = new completion_info($course);
 $completion->set_module_viewed($cm);
 
-// Fire the event
-events_trigger('workshop_viewed', (object)array(
-    'workshop' => $workshop,
-    'user' => $USER,
-));
+$eventdata = array();
+$eventdata['objectid']         = $workshop->id;
+$eventdata['context']          = $workshop->context;
+$eventdata['courseid']         = $course->id;
+$eventdata['other']['content'] = $workshop->phase;
+
+$PAGE->set_url($workshop->view_url());
+$event = \mod_workshop\event\course_module_viewed::create($eventdata);
+$event->add_record_snapshot('course', $course);
+$event->add_record_snapshot('workshop', $workshoprecord);
+$event->add_record_snapshot('course_modules', $cm);
+$event->set_page_detail();
+$event->trigger();
 
 // If the phase is to be switched, do it asap. This just has to happen after triggering
 // the event so that the scheduled allocator had a chance to allocate submissions.
@@ -82,7 +89,6 @@ if (!is_null($editmode) && $PAGE->user_allowed_editing()) {
     $USER->editing = $editmode;
 }
 
-$PAGE->set_url($workshop->view_url());
 $PAGE->set_title($workshop->name);
 $PAGE->set_heading($course->fullname);