diff --git a/course/dnduploadlib.php b/course/dnduploadlib.php index 62054d9b070..5d146235ce6 100644 --- a/course/dnduploadlib.php +++ b/course/dnduploadlib.php @@ -660,10 +660,6 @@ class dndupload_ajax_processor { )); $event->trigger(); - add_to_log($this->course->id, $mod->modname, "add", - "view.php?id=$mod->id", - "$instanceid", $mod->id); - $this->send_response($mod); } diff --git a/course/modlib.php b/course/modlib.php index ced162c0549..454202e505f 100644 --- a/course/modlib.php +++ b/course/modlib.php @@ -160,10 +160,6 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) { )); $event->trigger(); - add_to_log($course->id, $moduleinfo->modulename, "add", - "view.php?id=$moduleinfo->coursemodule", - "$moduleinfo->instance", $moduleinfo->coursemodule); - $moduleinfo = edit_module_post_actions($moduleinfo, $course); $transaction->allow_commit(); @@ -541,10 +537,6 @@ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) { )); $event->trigger(); - add_to_log($course->id, $moduleinfo->modulename, "update", - "view.php?id=$moduleinfo->coursemodule", - "$moduleinfo->instance", $moduleinfo->coursemodule); - $moduleinfo = edit_module_post_actions($moduleinfo, $course); return array($cm, $moduleinfo); diff --git a/course/resources.php b/course/resources.php index 4691d959185..af1179684c4 100644 --- a/course/resources.php +++ b/course/resources.php @@ -34,7 +34,7 @@ require_course_login($course, true); // get list of all resource-like modules $allmodules = $DB->get_records('modules', array('visible'=>1)); -$modules = array(); +$availableresources = array(); foreach ($allmodules as $key=>$module) { $modname = $module->name; $libfile = "$CFG->dirroot/mod/$modname/lib.php"; @@ -46,11 +46,15 @@ foreach ($allmodules as $key=>$module) { continue; } - $modules[$modname] = get_string('modulename', $modname); - //some hacky nasic logging - add_to_log($course->id, $modname, 'view all', "index.php?id=$course->id", ''); + $availableresources[] = $modname; } +// Triger view event. +$event = \core\event\course_resources_list_viewed::create(array('context' => context_course::instance($course->id))); +$event->set_legacy_logdata($availableresources); +$event->add_record_snapshot('course', $course); +$event->trigger(); + $strresources = get_string('resources'); $strname = get_string('name'); $strintro = get_string('moduleintro'); @@ -67,10 +71,10 @@ $usesections = course_format_uses_sections($course->format); $cms = array(); $resources = array(); foreach ($modinfo->cms as $cm) { - if (!$cm->uservisible) { + if (!in_array($cm->modname, $availableresources)) { continue; } - if (!array_key_exists($cm->modname, $modules)) { + if (!$cm->uservisible) { continue; } if (!$cm->has_view()) { @@ -127,11 +131,7 @@ foreach ($cms as $cm) { } $extra = empty($cm->extra) ? '' : $cm->extra; - if (!empty($cm->icon)) { - $icon = ''.get_string('modulename', $cm->modname).' '; - } else { - $icon = ''.get_string('modulename', $cm->modname).' '; - } + $icon = ''.$cm->get_module_type_name().' '; if (isset($resource->intro) && isset($resource->introformat)) { $intro = format_module_intro('resource', $resource, $cm->id); @@ -142,7 +142,7 @@ foreach ($cms as $cm) { $class = $cm->visible ? '' : 'class="dimmed"'; // hidden modules are dimmed $table->data[] = array ( $printsection, - "wwwroot/mod/$cm->modname/view.php?id=$cm->id\">".$icon.format_string($resource->name)."", + "url."\">".$icon.$cm->get_formatted_name()."", $intro); } diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 4e1a3b05663..79dd3ef1561 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1943,7 +1943,10 @@ class core_course_courselib_testcase extends advanced_testcase { $eventdata->userid = $USER->id; $this->assertEventLegacyData($eventdata, $event); - $arr = array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"); + $arr = array( + array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$cm->id", "assign $cm->instance"), + array($cm->course, "assign", "add", "view.php?id=$cm->id", $cm->instance, $cm->id) + ); $this->assertEventLegacyLogData($arr, $event); $this->assertEventContextNotUsed($event); @@ -2049,7 +2052,10 @@ class core_course_courselib_testcase extends advanced_testcase { $eventdata->userid = $USER->id; $this->assertEventLegacyData($eventdata, $event); - $arr = array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"); + $arr = array( + array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$cm->id", "forum $cm->instance"), + array($cm->course, "forum", "update", "view.php?id=$cm->id", $cm->instance, $cm->id) + ); $this->assertEventLegacyLogData($arr, $event); $this->assertEventContextNotUsed($event); } @@ -2360,4 +2366,35 @@ class core_course_courselib_testcase extends advanced_testcase { $dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder', 'id'); $this->assertEquals(array_keys($dbcourses), array_keys($courses)); } + + public function test_view_resources_list() { + $this->resetAfterTest(); + + $course = self::getDataGenerator()->create_course(); + $coursecontext = context_course::instance($course->id); + + $event = \core\event\course_resources_list_viewed::create(array('context' => context_course::instance($course->id))); + $event->set_legacy_logdata(array('book', 'page', 'resource')); + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_resources_list_viewed', $event); + $this->assertEquals(null, $event->objecttable); + $this->assertEquals(null, $event->objectid); + $this->assertEquals($course->id, $event->courseid); + $this->assertEquals($coursecontext->id, $event->contextid); + $expecteddesc = "User with id '$event->userid' viewed list of resources in course with id '$event->courseid'"; + $this->assertEquals($expecteddesc, $event->get_description()); + $expectedlegacydata = array( + array($course->id, "book", "view all", 'index.php?id=' . $course->id, ''), + array($course->id, "page", "view all", 'index.php?id=' . $course->id, ''), + array($course->id, "resource", "view all", 'index.php?id=' . $course->id, ''), + ); + $this->assertEventLegacyLogData($expectedlegacydata, $event); + $this->assertEventContextNotUsed($event); + } } diff --git a/lib/classes/event/base.php b/lib/classes/event/base.php index 8d9cdf62a15..33870ceec19 100644 --- a/lib/classes/event/base.php +++ b/lib/classes/event/base.php @@ -615,7 +615,14 @@ abstract class base implements \IteratorAggregate { if ($data = $this->get_legacy_logdata()) { $manager = get_log_manager(); if (method_exists($manager, 'legacy_add_to_log')) { - call_user_func_array(array($manager, 'legacy_add_to_log'), $data); + if (is_array($data[0])) { + // Some events require several entries in 'log' table. + foreach ($data as $d) { + call_user_func_array(array($manager, 'legacy_add_to_log'), $d); + } + } else { + call_user_func_array(array($manager, 'legacy_add_to_log'), $data); + } } } } diff --git a/lib/classes/event/course_module_created.php b/lib/classes/event/course_module_created.php index 474caee7c5d..c73f7113540 100644 --- a/lib/classes/event/course_module_created.php +++ b/lib/classes/event/course_module_created.php @@ -110,8 +110,12 @@ class course_module_created extends base { * @return array of parameters to be passed to legacy add_to_log() function. */ protected function get_legacy_logdata() { - return array ($this->courseid, "course", "add mod", "../mod/" . $this->other['modulename'] . "/view.php?id=" . + $log1 = array($this->courseid, "course", "add mod", "../mod/" . $this->other['modulename'] . "/view.php?id=" . $this->objectid, $this->other['modulename'] . " " . $this->other['instanceid']); + $log2 = array($this->courseid, $this->other['modulename'], "add", + "view.php?id={$this->objectid}", + "{$this->other['instanceid']}", $this->objectid); + return array($log1, $log2); } /** diff --git a/lib/classes/event/course_module_updated.php b/lib/classes/event/course_module_updated.php index 6ae6752c457..e1069d65c79 100644 --- a/lib/classes/event/course_module_updated.php +++ b/lib/classes/event/course_module_updated.php @@ -110,8 +110,12 @@ class course_module_updated extends base { * @return array of parameters to be passed to legacy add_to_log() function. */ protected function get_legacy_logdata() { - return array ($this->courseid, "course", "update mod", "../mod/" . $this->other['modulename'] . "/view.php?id=" . + $log1 = array($this->courseid, "course", "update mod", "../mod/" . $this->other['modulename'] . "/view.php?id=" . $this->objectid, $this->other['modulename'] . " " . $this->other['instanceid']); + $log2 = array($this->courseid, $this->other['modulename'], "update", + "view.php?id={$this->objectid}", + "{$this->other['instanceid']}", $this->objectid); + return array($log1, $log2); } /** diff --git a/lib/classes/event/course_resources_list_viewed.php b/lib/classes/event/course_resources_list_viewed.php new file mode 100644 index 00000000000..24e046a2fc0 --- /dev/null +++ b/lib/classes/event/course_resources_list_viewed.php @@ -0,0 +1,119 @@ +. + +/** + * Event for viewing the list of course resources. + * + * @package core + * @copyright 2014 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; +defined('MOODLE_INTERNAL') || die(); + +/** + * Event for viewing the list of course resources. + * + * @package core + * @copyright 2014 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_resources_list_viewed extends base { + + /** @var array list of resource types for legacy logging */ + private $resourceslist = null; + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'r'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return "User with id '$this->userid' viewed list of resources in course with id '$this->courseid'"; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcoursemoduleinstancelistviewed', 'core'); + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url("/course/resources.php", array('id' => $this->courseid)); + } + + /** + * List of resource types enabled in the system. This is used for legacy logging to log one record for each resource type. + * + * There is no public getter for this data because it does not depend on the + * course. It always includes the list of all resource types in the system + * even when some of them are not present in the course. + * + * @param array $data + */ + public function set_legacy_logdata($data) { + $this->resourceslist = $data; + } + + /** + * Return the legacy event log data. + * + * @return array|null + */ + protected function get_legacy_logdata() { + if (empty($this->resourceslist)) { + return null; + } + $logs = array(); + foreach ($this->resourceslist as $resourcename) { + $logs[] = array($this->courseid, $resourcename, 'view all', 'index.php?id=' . $this->courseid, ''); + } + return $logs; + } + + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + if ($this->contextlevel != CONTEXT_COURSE) { + throw new \coding_exception('Context passed must be course context.'); + } + } + +}