-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SV-26 Set up the plugin to work on the mobile app
- Loading branch information
Showing
11 changed files
with
416 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?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; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
require_once($CFG->dirroot . '/mod/sortvoting/lib.php'); | ||
|
||
/** | ||
* Class mobile | ||
* | ||
* @package mod_sortvoting | ||
* @copyright 2024 Odei Alba <[email protected]> | ||
* @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']; | ||
}); | ||
|
||
// Result of existing votes. | ||
$existingvotes = sortvoting_get_response_data($sortvoting); | ||
$maxvotescount = empty($existingvotes) ? 0 : (int) max(array_column($existingvotes, 'votescount')); | ||
|
||
$data = [ | ||
'sortvoting' => $sortvoting, | ||
'allowupdate' => $allowupdate, | ||
'options' => $optionsclean, | ||
'max' => count($options), | ||
'votes' => $existingvotes, | ||
'maxvotescount' => $maxvotescount, | ||
]; | ||
|
||
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' => '', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
* Documentation: {@link https://moodledev.io/general/app/development/plugins-development-guide} | ||
* | ||
* @package mod_sortvoting | ||
* @copyright 2024 Odei Alba <[email protected]> | ||
* @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', | ||
], | ||
], | ||
'lang' => [ | ||
['pluginname', 'mod_sortvoting'], | ||
['instructions', 'mod_sortvoting'], | ||
['sortvotingname', 'mod_sortvoting'], | ||
['votesuccess', 'mod_sortvoting'], | ||
['position', 'mod_sortvoting'], | ||
['option', 'mod_sortvoting'], | ||
['xvotesreceived', 'mod_sortvoting'], | ||
['save', 'core'], | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// 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/>. | ||
|
||
/** | ||
* AMD module used when saving a new sort voting on mobile. | ||
* | ||
* @copyright 2024 Odei Alba <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
const context = this; | ||
|
||
/** | ||
* Class to handle sort votings. | ||
*/ | ||
class AddonModSortVotingProvider { | ||
/** | ||
* Send the responses to a sort voting. | ||
* | ||
* @param sortvotingid Sort voting ID to submit. | ||
* @param votes The responses to send. | ||
* @param siteId Site ID. If not defined, current site. | ||
* @return Promise resolved with boolean: true if responses sent to server, false and rejected if failure. | ||
*/ | ||
submitResponses(sortvotingid, votes, siteId) { | ||
siteId = siteId || context.CoreSitesProvider.getCurrentSiteId(); | ||
|
||
// TODO: Add offline option. | ||
// Now try to delete the responses in the server. | ||
return this.submitResponsesOnline(sortvotingid, votes, siteId).then(() => { | ||
return true; | ||
}).catch((error) => { | ||
// The WebService has thrown an error, this means that responses cannot be submitted. | ||
return Promise.reject(error); | ||
}); | ||
} | ||
|
||
/** | ||
* Send responses from a sort voting to Moodle. It will fail if offline or cannot connect. | ||
* | ||
* @param sortvotingid Sort voting ID to submit. | ||
* @param votes The responses to send. | ||
* @param siteId Site ID. If not defined, current site. | ||
* @return Promise resolved if deleted, rejected if failure. | ||
*/ | ||
submitResponsesOnline(sortvotingid, votes, siteId) { | ||
return context.CoreSitesProvider.getSite(siteId).then((site) => { | ||
var params = { | ||
sortvotingid: sortvotingid, | ||
votes: votes | ||
}; | ||
|
||
return site.write('mod_sortvoting_save_vote', params).then((response) => { | ||
if (!response || response.success === false) { | ||
// Couldn't save the responses. Reject the promise. | ||
var error = response && response.warnings && response.warnings[0] ? | ||
response.warnings[0] : new context.CoreError(''); | ||
|
||
return Promise.reject(error); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
} | ||
|
||
const sortVotingProvider = new AddonModSortVotingProvider(); | ||
|
||
const result = { | ||
sortVotingProvider: sortVotingProvider, | ||
}; | ||
|
||
result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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/>. | ||
|
||
/** | ||
* AMD module used when saving a new sort voting on mobile. | ||
* | ||
* @copyright 2024 Odei Alba <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
this.submitResponses = () => { | ||
// TODO: Check if all elements of the positions array are unique. | ||
// TODO: Add allowupdate option. | ||
let promise; | ||
promise = Promise.resolve(); | ||
promise.then(() => { | ||
// Show loading modal. | ||
return this.CoreDomUtilsProvider.showModalLoading('core.sending', true); | ||
}).then((modal) => { | ||
var options = document.getElementsByName('option[]'); | ||
|
||
// Build votes and positions arrays for later processing. | ||
var votes = []; | ||
var positions = []; | ||
options.forEach(function(option) { | ||
positions.push(option.value); | ||
votes.push({ | ||
'position': option.value, | ||
'optionid': option.getAttribute('data-optionid') | ||
}); | ||
}); | ||
|
||
return this.sortVotingProvider.submitResponses(this.module.instance, votes).then(() => { | ||
// Responses have been sent to server or stored to be sent later. | ||
this.CoreDomUtilsProvider.showToast(this.TranslateService.instant('plugin.mod_sortVoting.votesuccess')); | ||
|
||
// Check completion since it could be configured to complete once the user submits a vote. | ||
this.CoreCourseProvider.checkModuleCompletion(this.courseId, this.module.completiondata); | ||
|
||
// Data has been sent, refresh the content. | ||
return this.refreshContent(true); | ||
}).catch((message) => { | ||
this.CoreDomUtilsProvider.showErrorModalDefault(message, 'Error submitting responses.', true); | ||
}).finally(() => { | ||
modal.dismiss(); | ||
}); | ||
}).catch(() => { | ||
// User cancelled, ignore. | ||
}); | ||
}; |
Oops, something went wrong.