diff --git a/classes/event/sortvoting_vote.php b/classes/event/sortvoting_vote.php new file mode 100644 index 0000000..4261c13 --- /dev/null +++ b/classes/event/sortvoting_vote.php @@ -0,0 +1,72 @@ +. + +namespace mod_sortvoting\event; + +/** + * Event sortvoting_vote + * + * @package mod_sortvoting + * @copyright 2024 Odei Alba + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class sortvoting_vote extends \core\event\base { + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->data['crud'] = 'u'; + $this->data['edulevel'] = self::LEVEL_PARTICIPATING; + $this->data['objecttable'] = 'sortvoting'; + } + + /** + * Summary of get_objectid_mapping + * @return array + */ + public static function get_objectid_mapping() { + return ['db' => 'sortvoting', 'restore' => 'sortvoting']; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' voted or updated the vote for the SortVoting activity with the " . + "course module id '$this->contextinstanceid'."; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventvoteupdated', 'mod_sortvoting'); + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/sortvoting/view.php', ['id' => $this->contextinstanceid]); + } +} diff --git a/classes/output/mobile.php b/classes/output/mobile.php index 64fab4d..71f8143 100644 --- a/classes/output/mobile.php +++ b/classes/output/mobile.php @@ -104,6 +104,11 @@ public static function mobile_sort_voting_view($args): array { 'votes' => $existingvotes, ]; + // Completion and trigger events. + $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST); + $modulecontext = context_module::instance($cm->id); + sortvoting_view($sortvoting, $course, $cm, $modulecontext); + return [ 'templates' => [ [ diff --git a/lang/en/sortvoting.php b/lang/en/sortvoting.php index 4529e08..57ccf51 100644 --- a/lang/en/sortvoting.php +++ b/lang/en/sortvoting.php @@ -37,6 +37,7 @@ $string['completiondetail:submit'] = 'Submit vote'; $string['completionsubmit'] = 'Student must submit a vote to complete this activity'; $string['errorduplicatedposition'] = 'All positions must be unique.'; +$string['eventvoteupdated'] = 'Vote saved/updated'; $string['instructions'] = 'Change the options below into the desired order, so your preferred choice is at the top and your least favourite choice is at the bottom.'; $string['modulename'] = 'Preference Sort Voting'; $string['modulenameplural'] = 'Preference Sort Votings'; diff --git a/lib.php b/lib.php index 60a1e3e..48215c6 100644 --- a/lib.php +++ b/lib.php @@ -236,8 +236,9 @@ function sortvoting_user_submit_response($sortvoting, array $votes, $course, $cm // Save votes in sortvoting_answers table. $DB->insert_records('sortvoting_answers', $answers); - // Update completion state. - sortvoting_update_completion($sortvoting, $course, $cm); + // Completion and trigger events. + $modulecontext = context_module::instance($cm->id); + sortvoting_vote($sortvoting, $course, $cm, $modulecontext); } /** @@ -535,6 +536,30 @@ function sortvoting_view($sortvoting, $course, $cm, $context) { $completion->set_module_viewed($cm); } +/** + * Mark the activity completed (if required) and trigger the sortvoting_vote event. + * + * @param stdClass $sortvoting sortvoting object + * @param stdClass $course course object + * @param stdClass $cm course module object + * @param stdClass $context context object + */ +function sortvoting_vote($sortvoting, $course, $cm, $context) { + // Trigger sortvoting_vote event. + $params = [ + 'objectid' => $sortvoting->id, + 'context' => $context, + ]; + $event = \mod_sortvoting\event\sortvoting_vote::create($params); + $event->add_record_snapshot('course_modules', $cm); + $event->add_record_snapshot('course', $course); + $event->add_record_snapshot('sortvoting', $sortvoting); + $event->trigger(); + + // Completion update. + sortvoting_update_completion($sortvoting, $course, $cm); +} + /** * This function extends the settings navigation block for the site. *