Merge branch 'MDL-45740_master' of git://github.com/markn86/moodle
authorDan Poltawski <dan@moodle.com>
Mon, 1 Feb 2016 11:02:22 +0000 (11:02 +0000)
committerDan Poltawski <dan@moodle.com>
Mon, 1 Feb 2016 11:02:22 +0000 (11:02 +0000)
mod/choice/classes/event/answer_deleted.php [new file with mode: 0644]
mod/choice/classes/event/report_downloaded.php [new file with mode: 0644]
mod/choice/lang/en/choice.php
mod/choice/lib.php
mod/choice/report.php
mod/choice/tests/events_test.php
mod/choice/version.php
mod/choice/view.php

diff --git a/mod/choice/classes/event/answer_deleted.php b/mod/choice/classes/event/answer_deleted.php
new file mode 100644 (file)
index 0000000..7b8cb18
--- /dev/null
@@ -0,0 +1,114 @@
+<?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_choice answer deleted event.
+ *
+ * @package    mod_choice
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_choice\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_choice answer updated event class.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - int choiceid: id of choice.
+ *      - int optionid: id of the option.
+ * }
+ *
+ * @package    mod_choice
+ * @since      Moodle 3.1
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class answer_deleted extends \core\event\base {
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has deleted the option with id '" . $this->other['optionid'] . "' for the
+            user with id '$this->relateduserid' from the choice activity with course module id '$this->contextinstanceid'.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventanswerdeleted', 'mod_choice');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/choice/view.php', array('id' => $this->contextinstanceid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'choice_answers';
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->other['choiceid'])) {
+            throw new \coding_exception('The \'choiceid\' value must be set in other.');
+        }
+
+        if (!isset($this->other['optionid'])) {
+            throw new \coding_exception('The \'optionid\' value must be set in other.');
+        }
+    }
+
+    public static function get_objectid_mapping() {
+        return array('db' => 'choice_answers', 'restore' => \core\event\base::NOT_MAPPED);
+    }
+
+    public static function get_other_mapping() {
+        $othermapped = array();
+        $othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
+        $othermapped['optionid'] = array('db' => 'choice_options', 'restore' => 'choice_option');
+
+        return $othermapped;
+    }
+}
diff --git a/mod/choice/classes/event/report_downloaded.php b/mod/choice/classes/event/report_downloaded.php
new file mode 100644 (file)
index 0000000..96f64c0
--- /dev/null
@@ -0,0 +1,114 @@
+<?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_choice report viewed event.
+ *
+ * @package mod_choice
+ * @copyright 2016 Stephen Bourget
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_choice\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_choice report viewed event class.
+ *
+ * @property-read array $other {
+ *      Extra information about the event.
+ *
+ *      - string content: The content we are viewing.
+ *      - string format: The report format
+ *      - int choiced: The id of the choice
+ * }
+ *
+ * @package    mod_choice
+ * @since      Moodle 3.1
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class report_downloaded extends \core\event\base {
+
+    /**
+     * Init method.
+     */
+    protected function init() {
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventreportdownloaded', 'mod_choice');
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has downloaded the report in the '".$this->other['format']."' format for
+            the choice activity with course module id '$this->contextinstanceid'";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/choice/report.php', array('id' => $this->contextinstanceid));
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        // Report format downloaded.
+        if (!isset($this->other['content'])) {
+            throw new \coding_exception('The \'content\' value must be set in other.');
+        }
+        // Report format downloaded.
+        if (!isset($this->other['format'])) {
+            throw new \coding_exception('The \'format\' value must be set in other.');
+        }
+        // ID of the choice activity.
+        if (!isset($this->other['choiceid'])) {
+            throw new \coding_exception('The \'choiceid\' value must be set in other.');
+        }
+    }
+
+    public static function get_objectid_mapping() {
+        return false;
+    }
+
+    public static function get_other_mapping() {
+        $othermapped = array();
+        $othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
+
+        return $othermapped;
+    }
+}
index 4f5bbc6..53a5bf4 100644 (file)
@@ -33,7 +33,9 @@ $string['displayhorizontal'] = 'Display horizontally';
 $string['displaymode'] = 'Display mode for the options';
 $string['displayvertical'] = 'Display vertically';
 $string['eventanswercreated'] = 'Choice made';
+$string['eventanswerdeleted'] = 'Choice answer deleted';
 $string['eventanswerupdated'] = 'Choice updated';
+$string['eventreportdownloaded'] = 'Choice report downloaded';
 $string['eventreportviewed'] = 'Choice report viewed';
 $string['expired'] = 'Sorry, this activity closed on {$a} and is no longer available';
 $string['atleastoneoption'] = 'You need to provide at least one possible answer.';
index 900cead..0deb0a8 100644 (file)
@@ -513,7 +513,7 @@ function prepare_choice_show_results($choice, $course, $cm, $allresponses) {
  * @return bool
  */
 function choice_delete_responses($attemptids, $choice, $cm, $course) {
-    global $DB, $CFG;
+    global $DB, $CFG, $USER;
     require_once($CFG->libdir.'/completionlib.php');
 
     if(!is_array($attemptids) || empty($attemptids)) {
@@ -526,16 +526,36 @@ function choice_delete_responses($attemptids, $choice, $cm, $course) {
         }
     }
 
+    $context = context_module::instance($cm->id);
     $completion = new completion_info($course);
     foreach($attemptids as $attemptid) {
         if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid))) {
+            // Trigger the event answer deleted.
+            $eventdata = array();
+            $eventdata['objectid'] = $todelete->id;
+            $eventdata['context'] = $context;
+            $eventdata['userid'] = $USER->id;
+            $eventdata['courseid'] = $course->id;
+            $eventdata['relateduserid'] = $todelete->userid;
+            $eventdata['other'] = array();
+            $eventdata['other']['choiceid'] = $choice->id;
+            $eventdata['other']['optionid'] = $todelete->optionid;
+            $event = \mod_choice\event\answer_deleted::create($eventdata);
+            $event->add_record_snapshot('course', $course);
+            $event->add_record_snapshot('course_modules', $cm);
+            $event->add_record_snapshot('choice', $choice);
+            $event->add_record_snapshot('choice_answers', $todelete);
+            $event->trigger();
+
             $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid));
-            // Update completion state
-            if ($completion->is_enabled($cm) && $choice->completionsubmit) {
-                $completion->update_state($cm, COMPLETION_INCOMPLETE, $attemptid);
-            }
         }
     }
+
+    // Update completion state.
+    if ($completion->is_enabled($cm) && $choice->completionsubmit) {
+        $completion->update_state($cm, COMPLETION_INCOMPLETE);
+    }
+
     return true;
 }
 
index 9620cf1..59ac1b6 100644 (file)
         }
     } else {
         $groupmode = groups_get_activity_groupmode($cm);
+
+        // Trigger the report downloaded event.
+        $eventdata = array();
+        $eventdata['context'] = $context;
+        $eventdata['courseid'] = $course->id;
+        $eventdata['other']['content'] = 'choicereportcontentviewed';
+        $eventdata['other']['format'] = $download;
+        $eventdata['other']['choiceid'] = $choice->id;
+        $event = \mod_choice\event\report_downloaded::create($eventdata);
+        $event->trigger();
+
     }
 
     // Check if we want to include responses from inactive users.
index 71bb599..d383ed4 100644 (file)
@@ -201,6 +201,42 @@ class mod_choice_events_testcase extends advanced_testcase {
         $this->assertEventContextNotUsed($event);
     }
 
+    /**
+     * Test to ensure that event data is being stored correctly.
+     */
+    public function test_answer_deleted() {
+        global $DB, $USER;
+        // Generate user data.
+        $user = $this->getDataGenerator()->create_user();
+
+        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
+
+        // Create the first answer.
+        choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm);
+        // Get the users response.
+        $answer = $DB->get_record('choice_answers', array('userid' => $user->id, 'choiceid' => $this->choice->id),
+                '*', $strictness = IGNORE_MULTIPLE);
+
+        // Redirect event.
+        $sink = $this->redirectEvents();
+        // Now delete the answer.
+        choice_delete_responses(array($answer->id), $this->choice, $this->cm, $this->course);
+
+        // Get our event event.
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Data checking.
+        $this->assertInstanceOf('\mod_choice\event\answer_deleted', $event);
+        $this->assertEquals($USER->id, $event->userid);
+        $this->assertEquals($user->id, $event->relateduserid);
+        $this->assertEquals(context_module::instance($this->choice->cmid), $event->get_context());
+        $this->assertEquals($this->choice->id, $event->other['choiceid']);
+        $this->assertEquals($answer->optionid, $event->other['optionid']);
+        $this->assertEventContextNotUsed($event);
+        $sink->close();
+    }
+
     /**
      * Test to ensure that event data is being stored correctly.
      */
@@ -238,6 +274,43 @@ class mod_choice_events_testcase extends advanced_testcase {
         $sink->close();
     }
 
+    /**
+     * Test to ensure that event data is being stored correctly.
+     */
+    public function test_report_downloaded() {
+        global $USER;
+
+        $this->resetAfterTest();
+
+        // Generate user data.
+        $this->setAdminUser();
+
+        $eventdata = array();
+        $eventdata['context'] = $this->context;
+        $eventdata['courseid'] = $this->course->id;
+        $eventdata['other']['content'] = 'choicereportcontentviewed';
+        $eventdata['other']['format'] = 'csv';
+        $eventdata['other']['choiceid'] = $this->choice->id;
+
+        // This is fired in a page view so we can't run this through a function.
+        $event = \mod_choice\event\report_downloaded::create($eventdata);
+
+        // Redirect event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $event = $sink->get_events();
+
+        // Data checking.
+        $this->assertCount(1, $event);
+        $this->assertInstanceOf('\mod_choice\event\report_downloaded', $event[0]);
+        $this->assertEquals($USER->id, $event[0]->userid);
+        $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
+        $this->assertEquals('csv', $event[0]->other['format']);
+        $this->assertEquals($this->choice->id, $event[0]->other['choiceid']);
+        $this->assertEventContextNotUsed($event[0]);
+        $sink->close();
+    }
+
     /**
      * Test to ensure that event data is being stored correctly.
      */
index 0b2d8c6..b6b732f 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015111600;       // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2016010300;       // The current module version (Date: YYYYMMDDXX)
 $plugin->requires  = 2015111000;    // Requires this Moodle version
 $plugin->component = 'mod_choice';     // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 0;
index 98d4840..7ced17e 100644 (file)
@@ -40,13 +40,10 @@ if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL,
         and $choiceavailable) {
     $answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
     if ($answercount > 0) {
-        $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
-
-        // Update completion state
-        $completion = new completion_info($course);
-        if ($completion->is_enabled($cm) && $choice->completionsubmit) {
-            $completion->update_state($cm, COMPLETION_INCOMPLETE);
-        }
+        $choiceanswers = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id),
+            '', 'id');
+        $todelete = array_keys($choiceanswers);
+        choice_delete_responses($todelete, $choice, $cm, $course);
         redirect("view.php?id=$cm->id");
     }
 }