/** * Trigger course viewed event. This API function is used when course view actions happens, * usually in course/view.php but also in external functions. * * @param stdClass $context course context object * @param int $sectionnumber section number * @since Moodle 2.9 */ function course_view($context, $sectionnumber = 0) { $eventdata = array('context' => $context); if (!empty($sectionnumber)) { $eventdata['other']['coursesectionnumber'] = $sectionnumber; } $event = \core\event\course_viewed::create($eventdata); $event->trigger(); }
redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php'); } if (get_home_page() != HOMEPAGE_SITE) { // Redirect logged-in users to My Moodle overview if required if (optional_param('setdefaulthome', false, PARAM_BOOL)) { set_user_preference('user_home_page_preference', HOMEPAGE_SITE); } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 1) { redirect($CFG->wwwroot .'/my/'); } else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)) { $PAGE->settingsnav->get('usercurrentsettings')->add(get_string('makethismyhome'), new moodle_url('/', array('setdefaulthome'=>true)), navigation_node::TYPE_SETTING); } } $eventparams = array('context' => context_course::instance(SITEID)); $event = \core\event\course_viewed::create($eventparams); $event->trigger(); /// If the hub plugin is installed then we let it take over the homepage here if (file_exists($CFG->dirroot.'/local/hub/lib.php') and get_config('local_hub', 'hubenabled')) { require_once($CFG->dirroot.'/local/hub/lib.php'); $hub = new local_hub(); $continue = $hub->display_homepage(); //display_homepage() return true if the hub home page is not displayed //mostly when search form is not displayed for not logged users if (empty($continue)) { exit; } } $PAGE->set_pagetype('site-index');
/** * This tests if writing of the events to the table tool_monitor_events is working fine. */ public function test_flush() { global $DB; $this->resetAfterTest(); // Create the necessary items for testing. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $course2 = $this->getDataGenerator()->create_course(); $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); // Fire a bunch of events. // Trigger a bunch of other events. $eventparams = array('context' => context_course::instance($course->id)); for ($i = 0; $i < 5; $i++) { \core\event\course_viewed::create($eventparams)->trigger(); \mod_quiz\event\course_module_instance_list_viewed::create($eventparams)->trigger(); \mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger(); } // Confirm that nothing was stored in the tool_monitor_events table // as we do not have any subscriptions associated for the above events. $this->assertEquals(0, $DB->count_records('tool_monitor_events')); // Now, let's create a rule so an event can be stored. $rule = new stdClass(); $rule->courseid = $course->id; $rule->plugin = 'mod_book'; $rule->eventname = '\\mod_book\\event\\course_module_instance_list_viewed'; $rule = $monitorgenerator->create_rule($rule); // Let's subscribe to this rule. $sub = new stdClass(); $sub->courseid = $course->id; $sub->ruleid = $rule->id; $sub->userid = $user->id; $monitorgenerator->create_subscription($sub); // Again, let's just fire more events to make sure they still aren't stored. for ($i = 0; $i < 5; $i++) { \core\event\course_viewed::create($eventparams)->trigger(); \mod_quiz\event\course_module_instance_list_viewed::create($eventparams)->trigger(); \mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger(); } // Fire the event we want to capture. $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course); $event->trigger(); // Check that the event data is valid. $events = $DB->get_records('tool_monitor_events'); $this->assertEquals(1, count($events)); $monitorevent = array_pop($events); $this->assertEquals($event->eventname, $monitorevent->eventname); $this->assertEquals($event->contextid, $monitorevent->contextid); $this->assertEquals($event->contextlevel, $monitorevent->contextlevel); $this->assertEquals($event->get_url()->out(), $monitorevent->link); $this->assertEquals($event->courseid, $monitorevent->courseid); $this->assertEquals($event->timecreated, $monitorevent->timecreated); // Remove the stored events. $DB->delete_records('tool_monitor_events'); // Now, let's create a site wide rule. $rule = new stdClass(); $rule->courseid = 0; $rule->plugin = 'mod_book'; $rule->eventname = '\\mod_book\\event\\course_module_instance_list_viewed'; $rule = $monitorgenerator->create_rule($rule); // Let's subscribe to this rule. $sub = new stdClass(); $sub->courseid = 0; $sub->ruleid = $rule->id; $sub->userid = $user->id; $monitorgenerator->create_subscription($sub); // Fire the event we want to capture - but in a different course. $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course2); $event->trigger(); // Check that the event data is valid. $events = $DB->get_records('tool_monitor_events'); $this->assertEquals(1, count($events)); $monitorevent = array_pop($events); $this->assertEquals($event->eventname, $monitorevent->eventname); $this->assertEquals($event->contextid, $monitorevent->contextid); $this->assertEquals($event->contextlevel, $monitorevent->contextlevel); $this->assertEquals($event->get_url()->out(), $monitorevent->link); $this->assertEquals($event->courseid, $monitorevent->courseid); $this->assertEquals($event->timecreated, $monitorevent->timecreated); }
$modnamesplural = get_module_types_names(true); $modnamesused = $modinfo->get_used_module_names(); $mods = $modinfo->get_cms(); $sections = $modinfo->get_section_info_all(); // CAUTION, hacky fundamental variable defintion to follow! // Note that because of the way course fromats are constructed though // inclusion we pass parameters around this way.. $displaysection = $section; // Include the actual course format. require($CFG->dirroot .'/course/format/'. $course->format .'/format.php'); // Content wrapper end. echo html_writer::end_tag('div'); // Trigger course viewed event. // We don't trust $context here. Course format inclusion above executes in the global space. We can't assume // anything after that point. $eventdata = array('context' => context_course::instance($course->id)); if (!empty($section) && (int)$section == $section) { $eventdata['other'] = array('coursesectionid' => $section); } $event = \core\event\course_viewed::create($eventdata); $event->trigger(); // Include course AJAX include_course_ajax($course, $modnamesused); echo $OUTPUT->footer();
/** * There is no api involved so the best we can do is test legacy data by triggering event manually. */ public function test_course_viewed() { $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $context = context_course::instance($course->id); // First try with no optional parameters. $eventparams = array(); $eventparams['context'] = $context; $event = \core\event\course_viewed::create($eventparams); // Trigger and capture the event. $sink = $this->redirectEvents(); $event->trigger(); $events = $sink->get_events(); $event = reset($events); $this->assertInstanceOf('\\core\\event\\course_viewed', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $expected = array($course->id, 'course', 'view', 'view.php?id=' . $course->id, $course->id); $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); // Now try with optional parameters. $sectionid = 34; $eventparams = array(); $eventparams['context'] = $context; $eventparams['other'] = array('coursesectionid' => $sectionid); $event = \core\event\course_viewed::create($eventparams); // Trigger and capture the event. $sink = $this->redirectEvents(); $event->trigger(); $loggeddata = $event->get_data(); $events = $sink->get_events(); $event = reset($events); $this->assertInstanceOf('\\core\\event\\course_viewed', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $expected = array($course->id, 'course', 'view section', 'view.php?id=' . $course->id . '&section=' . $sectionid, $sectionid); $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); delete_course($course->id, false); $restored = \core\event\base::restore($loggeddata, array('origin' => 'web', 'ip' => '127.0.0.1')); $this->assertInstanceOf('\\core\\event\\course_viewed', $restored); $this->assertNull($restored->get_url()); }
/** * @param string $ftype * * @throws \coding_exception */ public function test_forum_high_volume_posts($ftype = 'forum') { global $DB; // Disabled for general use. $this->markTestIncomplete('This test has to be enabled manually in code.'); $forums = []; // Teacher count. $teacherc = 0; // User 1 count. $user1c = 0; // User 2 count. $user2c = 0; $sturole = $DB->get_record('role', array('shortname' => 'student')); $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); $totalcourses = 20; $totalforums = 0; $forumspercourse = 40; $discussionsperforum = 20; for ($c = 0; $c < $totalcourses; $c++) { // Create course. $tmpcourse = $this->getDataGenerator()->create_course(); // Enrol user1 as student. $this->getDataGenerator()->enrol_user($this->user1->id, $tmpcourse->id, $sturole->id); // Enrol user2 as student to the first 10 courses. if ($c < 10) { $this->getDataGenerator()->enrol_user($this->user2->id, $tmpcourse->id, $sturole->id); } // Enrol teacher 1 and teacher 2. $this->getDataGenerator()->enrol_user($this->teacher1->id, $tmpcourse->id, $teacherrole->id); $this->getDataGenerator()->enrol_user($this->teacher2->id, $tmpcourse->id, $teacherrole->id); // Log user visited course for each user. $users = [$this->teacher1, $this->teacher2, $this->user1, $this->user2]; foreach ($users as $user) { $eventparams = ['userid' => $user->id, 'context' => \context_course::instance($tmpcourse->id)]; $event = \core\event\course_viewed::create($eventparams); $event->trigger(); } // All discussions and posts are made by teacher2, so that the count for teacher1, user1 // and user2 are not affected by not being able to see their own posts. for ($f = 0; $f < $forumspercourse; $f++) { $totalforums++; $record = new \stdClass(); $record->course = $tmpcourse->id; $forums[$f] = $this->getDataGenerator()->create_module($ftype, $record); for ($d = 0; $d < $discussionsperforum; $d++) { $discussion = $this->create_discussion($ftype, $tmpcourse->id, $this->teacher2->id, $forums[$f]->id); $teacherc++; $user1c++; if ($c < $discussionsperforum / 2) { $user2c++; } $post = $this->create_post($ftype, $tmpcourse->id, $this->teacher2->id, $forums[$f]->id, $discussion->id); $teacherc++; $user1c++; if ($c < $discussionsperforum / 2) { $user2c++; } $this->create_reply($ftype, $this->teacher2->id, $post); $teacherc++; $user1c++; if ($c < $discussionsperforum / 2) { $user2c++; } } } } $start = microtime(true); // Check teacher viewable posts. $starttchl10 = microtime(true); $this->assert_user_activity($this->teacher1, 10); $timetchl10 = microtime(true) - $starttchl10; // Check user1 viewable posts. $startu1l10 = microtime(true); $this->assert_user_activity($this->user1, 10); $timeu1l10 = microtime(true) - $startu1l10; // Check user2 viewable posts. $startu2l10 = microtime(true); $this->assert_user_activity($this->user2, 10); $timeu2l10 = microtime(true) - $startu2l10; // Forum limit when getting recent activity. $maxforums = user_forums::$forumlimit; $xforums = $totalforums > $maxforums ? $maxforums : $totalforums; $maxposts = $xforums * $discussionsperforum * 3; // 3 posts per discussion. $xteacherc = $teacherc > $maxposts ? $maxposts : $teacherc; $xuser1c = $user1c > $maxposts ? $maxposts : $user1c; $xuser2c = $user2c > $maxposts ? $maxposts : $user2c; // Check teacher viewable posts. $starttchnl = microtime(true); $this->assert_user_activity($this->teacher1, $xteacherc, 0); $timetchnl = microtime(true) - $starttchnl; // Check user1 viewable posts. $startu1nl = microtime(true); $this->assert_user_activity($this->user1, $xuser1c, 0); $timeu1nl = microtime(true) - $startu1nl; // Check user2 viewable posts. $startu2nl = microtime(true); $this->assert_user_activity($this->user2, $xuser2c, 0); $timeu2nl = microtime(true) - $startu2nl; $end = microtime(true); if (get_class($this) === "theme_snap\\tests\\theme_snap_recent_forum_activity_test") { mtrace('Recent ' . $ftype . ' activity test - sql mode'); } else { mtrace('Recent ' . $ftype . ' activity test - non-sql mode'); } mtrace('Teacher (limited to 10 posts) time = ' . round($timetchl10, 2) . ' seconds'); mtrace('User1 (limited to 10 posts) time = ' . round($timeu1l10, 2) . ' seconds'); mtrace('User2 (limited to 10 posts) time = ' . round($timeu2l10, 2) . ' seconds'); mtrace('Teacher (limited to ' . $xteacherc . ' posts) time = ' . round($timetchnl, 2) . ' seconds'); mtrace('User1 (limited to ' . $xuser1c . ' posts) time = ' . round($timeu1nl, 2) . ' seconds'); mtrace('User2 (limited to ' . $xuser2c . ' posts) time = ' . round($timeu2nl, 2) . ' seconds'); mtrace('High volume time = ' . round($end - $start, 2) . ' seconds'); mtrace('Total posts made = ' . $teacherc); }