-
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.
Merge pull request #52 from odeialba/SV-26
SV-26 Set up the plugin to work on the mobile app
- Loading branch information
Showing
12 changed files
with
521 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?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, | ||
]; | ||
|
||
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 | ||
* | ||
* @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'], | ||
], | ||
], | ||
]; |
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,86 @@ | ||
// 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 <odeialba@odeialba.com> | ||
* @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) { | ||
// TODO: Add warnings array to save_vote returns. | ||
// 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; |
Oops, something went wrong.