Skip to content

Commit

Permalink
MDL-40907 events: view list of resources in course
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Apr 8, 2014
1 parent c3ba899 commit 957944d
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 12 deletions.
24 changes: 12 additions & 12 deletions course/resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

// 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";
Expand All @@ -46,11 +46,15 @@
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');
Expand All @@ -67,10 +71,10 @@
$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()) {
Expand Down Expand Up @@ -127,11 +131,7 @@
}

$extra = empty($cm->extra) ? '' : $cm->extra;
if (!empty($cm->icon)) {
$icon = '<img src="'.$OUTPUT->pix_url($cm->icon).'" class="activityicon" alt="'.get_string('modulename', $cm->modname).'" /> ';
} else {
$icon = '<img src="'.$OUTPUT->pix_url('icon', $cm->modname).'" class="activityicon" alt="'.get_string('modulename', $cm->modname).'" /> ';
}
$icon = '<img src="'.$cm->get_icon_url().'" class="activityicon" alt="'.$cm->get_module_type_name().'" /> ';

if (isset($resource->intro) && isset($resource->introformat)) {
$intro = format_module_intro('resource', $resource, $cm->id);
Expand All @@ -142,7 +142,7 @@
$class = $cm->visible ? '' : 'class="dimmed"'; // hidden modules are dimmed
$table->data[] = array (
$printsection,
"<a $class $extra href=\"$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id\">".$icon.format_string($resource->name)."</a>",
"<a $class $extra href=\"".$cm->url."\">".$icon.$cm->get_formatted_name()."</a>",
$intro);
}

Expand Down
31 changes: 31 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2360,4 +2360,35 @@ public function test_course_change_sortorder_by_one() {
$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);
}
}
119 changes: 119 additions & 0 deletions lib/classes/event/course_resources_list_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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.');
}
}

}

0 comments on commit 957944d

Please sign in to comment.