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

Release Version 1.0.8 #55

Merged
merged 6 commits 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
1 change: 1 addition & 0 deletions .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ jobs:
env:
DB: ${{ matrix.database }}
MOODLE_BRANCH: ${{ matrix.moodle-branch }}
MUSTACHE_IGNORE_NAMES: 'mobile*.mustache'

- name: PHP Lint
if: ${{ always() }}
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 1.0.8 - 2024-01-30
### Changed
- Instructions removed when the user can't vote.
### Added
- Mobile app support.
- Event for when a user votes.

## 1.0.7 - 2023-11-10
### Changed
- Added more precision to the calculation of the position on the results.
Expand Down
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]);
}
}
123 changes: 123 additions & 0 deletions classes/output/mobile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?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\output;
use context_module;

defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/sortvoting/lib.php');

/**
* Class mobile
*
* @package mod_sortvoting
* @copyright 2024 Odei Alba <odeialba@odeialba.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mobile {
/**
* Returns the javascript needed to initialize SortVoting in the app.
*
* @param array $args Arguments from tool_mobile_get_content WS
* @return array javascript
*/
public static function mobile_init($args) {
global $CFG;

return [
'templates' => [],
'javascript' => file_get_contents($CFG->dirroot . "/mod/sortvoting/mobile/js/init.js"),
];
}

/**
* Returns the SortVoting form view for the mobile app.
*
* @param mixed $args
* @return array HTML, javascript and other data.
*/
public static function mobile_sort_voting_view($args): array {
global $OUTPUT, $DB, $CFG;
$args = (object) $args;

$cm = get_coursemodule_from_id('sortvoting', $args->cmid);
$sortvotingid = $cm->instance;
$userid = $args->userid;

$sortvoting = $DB->get_record("sortvoting", ["id" => $sortvotingid]);
$options = $DB->get_records('sortvoting_options', ['sortvotingid' => $sortvotingid], 'id ASC');
$existingvotes = $DB->get_records_menu(
'sortvoting_answers',
[
'sortvotingid' => $sortvotingid,
'userid' => $userid,
],
'id ASC',
'optionid, position'
);

$allowupdate = true;
if (!$sortvoting->allowupdate && count($existingvotes) === count($options)) {
$allowupdate = false;
}

$defaultposition = (count($existingvotes) > 0) ? (count($existingvotes) + 1) : 1;
$optionsclean = [];
foreach ($options as $option) {
$position = isset($existingvotes[$option->id]) ? $existingvotes[$option->id] : $defaultposition++;
$optionsclean[] = [
'id' => $option->id,
'text' => $option->text,
'position' => $position,
];
}

// Sort $optionsclean by position.
usort($optionsclean, function ($a, $b) {
return $a['position'] <=> $b['position'];
});

$canseeresults = \mod_sortvoting\permission::can_see_results($sortvoting, context_module::instance($cm->id));

// Result of existing votes.
$existingvotes = $canseeresults ? sortvoting_get_response_data($sortvoting) : [];

$data = [
'description' => html_to_text($sortvoting->intro),
'allowupdate' => $allowupdate,
'options' => $optionsclean,
'max' => count($options),
'canseeresults' => $canseeresults,
'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' => [
[
'id' => 'main',
'html' => $OUTPUT->render_from_template('mod_sortvoting/mobile_sort_voting_view', $data),
],
],
'javascript' => file_get_contents($CFG->dirroot . "/mod/sortvoting/mobile/js/mobile_sortvoting.js"),
'otherdata' => '',
];
}
}
2 changes: 1 addition & 1 deletion classes/output/sort_voting_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(\stdClass $sortvoting) {
*/
public function export_for_template(renderer_base $output): array {
$existingvotes = sortvoting_get_response_data($this->sortvoting);
$maxvotescount = (int) max(array_column($existingvotes, 'votescount'));
$maxvotescount = empty($existingvotes) ? 0 : (int) max(array_column($existingvotes, 'votescount'));

return ['votes' => $existingvotes, 'maxvotescount' => $maxvotescount];
}
Expand Down
54 changes: 54 additions & 0 deletions db/mobile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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/>.

/**
* Mobile app areas for Preference Sort Voting
*
* @package mod_sortvoting
* @copyright 2024 Odei Alba <odeialba@odeialba.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$addons = [
'mod_sortvoting' => [
"handlers" => [ // Different places where the add-on will display content.
'coursesortvotingview' => [ // Handler unique name (can be anything).
'displaydata' => [
'title' => 'pluginname',
'icon' => $CFG->wwwroot . '/mod/sortvoting/pix/monologo.png',
'class' => '',
],
'delegate' => 'CoreCourseModuleDelegate', // Delegate (where to display the link to the add-on).
'method' => 'mobile_sort_voting_view', // Main function in \mod_sortvoting\output\mobile.
'init' => 'mobile_init',
'styles' => [
'url' => $CFG->wwwroot . '/mod/sortvoting/style/mobile.css',
'version' => '0.1',
],
],
],
'lang' => [
['pluginname', 'mod_sortvoting'],
['instructions', 'mod_sortvoting'],
['votesuccess', 'mod_sortvoting'],
['position', 'mod_sortvoting'],
['option', 'mod_sortvoting'],
['save', 'core'],
],
],
];
1 change: 1 addition & 0 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
'type' => 'write',
'capabilities' => 'mod/sortvoting:vote',
'ajax' => true,
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE, 'local_mobile'],
],
];
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
31 changes: 28 additions & 3 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 @@ -386,7 +387,7 @@ function sortvoting_get_response_data(stdClass $sortvoting, bool $onlyactive = t

$position = 1;
$previousvote = null;
$maxvotescount = (int) max(array_column($existingvotes, 'votescount'));
$maxvotescount = empty($existingvotes) ? 0 : (int) max(array_column($existingvotes, 'votescount'));
foreach ($existingvotes as $key => $vote) {
if ($previousvote !== null && $previousvote->avg !== $vote->avg) {
$position++;
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
Loading