Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SV-50 Emit event when user submits vote #54

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions classes/event/sortvoting_vote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
// This file is part of the mod_sortvoting plugin for 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/>.

namespace mod_sortvoting\event;

/**
* Event sortvoting_vote
*
* @package mod_sortvoting
* @copyright 2024 Odei Alba <odeialba@odeialba.com>
* @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<string>
*/
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]);
}
}
5 changes: 5 additions & 0 deletions classes/output/mobile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
[
Expand Down
1 change: 1 addition & 0 deletions lang/en/sortvoting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
29 changes: 27 additions & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
Loading