MDL-51687 mod_folder: Trigger event when downloading all files.
authorAndrew Hancox <andrewdchancox@googlemail.com>
Wed, 7 Oct 2015 10:52:08 +0000 (11:52 +0100)
committerAndrew Nicols <andrew@nicols.co.uk>
Tue, 1 Mar 2016 04:27:55 +0000 (12:27 +0800)
mod/folder/classes/event/all_files_downloaded.php [new file with mode: 0644]
mod/folder/download_folder.php
mod/folder/lang/en/folder.php
mod/folder/lib.php
mod/folder/tests/events_test.php

diff --git a/mod/folder/classes/event/all_files_downloaded.php b/mod/folder/classes/event/all_files_downloaded.php
new file mode 100644 (file)
index 0000000..27e9d21
--- /dev/null
@@ -0,0 +1,95 @@
+<?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/>.
+
+/**
+ * The mod_folder course module downloaded event.
+ *
+ * @package   mod_folder
+ * @copyright 2015 Andrew Hancox <andrewdchancox@googlemail.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_folder\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_folder course module viewed event class.
+ *
+ * @package    mod_folder
+ * @since      Moodle 3.1
+ * @copyright  2015 Andrew Hancox <andrewdchancox@googlemail.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class all_files_downloaded extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+        $this->data['objecttable'] = 'folder';
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' downloaded a zip archive containing all the files from the folder activity with " .
+        "course module id '$this->contextinstanceid'.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventallfilesdownloaded', 'folder');
+    }
+
+    /**
+     * Get URL related to the action.
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url("/mod/folder/view.php", array('id' => $this->contextinstanceid));
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        // Make sure this class is never used without proper object details.
+        if (empty($this->objectid) || empty($this->objecttable)) {
+            throw new \coding_exception('The course_module_viewed event must define objectid and object table.');
+        }
+        // Make sure the context level is set to module.
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context level must be CONTEXT_MODULE.');
+        }
+    }
+}
index a1a8631..ef6d101 100644 (file)
@@ -40,9 +40,7 @@ if (!$downloadable) {
     print_error('cannotdownloaddir', 'repository');
 }
 
-// Completion.
-$completion = new completion_info($course);
-$completion->set_module_viewed($cm);
+folder_downloaded($folder, $course, $cm, $context);
 
 $fs = get_file_storage();
 $file = $fs->get_file($context->id, 'mod_folder', 'content', 0, '/', '.');
index 9559ac9..abca28b 100644 (file)
@@ -26,6 +26,7 @@
 $string['contentheader'] = 'Content';
 $string['dnduploadmakefolder'] = 'Unzip files and create folder';
 $string['downloadfolder'] = 'Download folder';
+$string['eventallfilesdownloaded'] = 'Zip archive of folder downloaded';
 $string['eventfolderupdated'] = 'Folder updated';
 $string['folder:addinstance'] = 'Add a new folder';
 $string['folder:managefiles'] = 'Manage files in folder module';
index dd7c30c..bd6a47f 100644 (file)
@@ -530,4 +530,29 @@ function folder_get_directory_size($directory) {
     }
 
     return $size;
-}
\ No newline at end of file
+}
+
+/**
+ * Mark the activity completed (if required) and trigger the all_files_downloaded event.
+ *
+ * @param  stdClass $folder     folder object
+ * @param  stdClass $course     course object
+ * @param  stdClass $cm         course module object
+ * @param  stdClass $context    context object
+ * @since Moodle 3.1
+ */
+function folder_downloaded($folder, $course, $cm, $context) {
+    $params = array(
+        'context' => $context,
+        'objectid' => $folder->id
+    );
+    $event = \mod_folder\event\all_files_downloaded::create($params);
+    $event->add_record_snapshot('course_modules', $cm);
+    $event->add_record_snapshot('course', $course);
+    $event->add_record_snapshot('folder', $folder);
+    $event->trigger();
+
+    // Completion.
+    $completion = new completion_info($course);
+    $completion->set_module_viewed($cm);
+}
index a7b54a4..71723dc 100644 (file)
@@ -68,4 +68,31 @@ class mod_folder_events_testcase extends advanced_testcase {
         $this->assertEventLegacyLogData($expected, $event);
         $this->assertEventContextNotUsed($event);
     }
+
+    /**
+     * Test the folder updated event.
+     *
+     * There is no external API for updating a folder, so the unit test will simply create
+     * and trigger the event and ensure the legacy log data is returned as expected.
+     */
+    public function test_all_files_downloaded() {
+        $this->setAdminUser();
+        $course = $this->getDataGenerator()->create_course();
+        $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
+        $context = context_module::instance($folder->cmid);
+        $cm = get_coursemodule_from_id('folder', $folder->cmid, $course->id, true, MUST_EXIST);
+
+        $sink = $this->redirectEvents();
+        folder_downloaded($folder, $course, $cm, $context);
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_folder\event\all_files_downloaded', $event);
+        $this->assertEquals(context_module::instance($folder->cmid), $event->get_context());
+        $this->assertEquals($folder->id, $event->objectid);
+        $expected = array($course->id, 'folder', 'edit', 'edit.php?id=' . $folder->cmid, $folder->id, $folder->cmid);
+        $this->assertEventContextNotUsed($event);
+    }
 }