mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
MDL-50944 mod_choice: New Web Service mod_choice_get_choice_results
This commit is contained in:
parent
e28004e614
commit
b87f31db53
@ -1144,6 +1144,7 @@ $services = array(
|
||||
'mod_page_view_page',
|
||||
'mod_resource_view_resource',
|
||||
'mod_folder_view_folder',
|
||||
'mod_choice_get_choice_results',
|
||||
),
|
||||
'enabled' => 0,
|
||||
'restrictedusers' => 0,
|
||||
|
170
mod/choice/classes/external.php
Normal file
170
mod/choice/classes/external.php
Normal file
@ -0,0 +1,170 @@
|
||||
<?php
|
||||
// This file is part of 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/>.
|
||||
|
||||
/**
|
||||
* Choice module external API
|
||||
*
|
||||
* @package mod_choice
|
||||
* @category external
|
||||
* @copyright 2015 Costantino Cito <ccito@cvaconsulting.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
require_once($CFG->libdir . '/externallib.php');
|
||||
require_once($CFG->dirroot . '/mod/choice/lib.php');
|
||||
|
||||
/**
|
||||
* Choice module external functions
|
||||
*
|
||||
* @package mod_choice
|
||||
* @category external
|
||||
* @copyright 2015 Costantino Cito <ccito@cvaconsulting.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
class mod_choice_external extends external_api {
|
||||
|
||||
/**
|
||||
* Describes the parameters for get_choices_by_courses.
|
||||
*
|
||||
* @return external_external_function_parameters
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function get_choice_results_parameters() {
|
||||
return new external_function_parameters (array('choiceid' => new external_value(PARAM_INT, 'choice instance id')));
|
||||
}
|
||||
/**
|
||||
* Returns user's results for a specific choice
|
||||
* and a list of those users that did not answered yet.
|
||||
*
|
||||
* @param int $choiceid the choice instance id
|
||||
* @return array of responses details
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function get_choice_results($choiceid) {
|
||||
global $USER;
|
||||
|
||||
$params = self::validate_parameters(self::get_choice_results_parameters(), array('choiceid' => $choiceid));
|
||||
|
||||
if (!$choice = choice_get_choice($params['choiceid'])) {
|
||||
throw new moodle_exception("invalidcoursemodule", "error");
|
||||
}
|
||||
list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice');
|
||||
|
||||
$context = context_module::instance($cm->id);
|
||||
self::validate_context($context);
|
||||
|
||||
$groupmode = groups_get_activity_groupmode($cm);
|
||||
// Check if we have to include responses from inactive users.
|
||||
$onlyactive = $choice->includeinactive ? false : true;
|
||||
$users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
|
||||
// Show those who haven't answered the question.
|
||||
if (!empty($choice->showunanswered)) {
|
||||
$choice->option[0] = get_string('notanswered', 'choice');
|
||||
$choice->maxanswers[0] = 0;
|
||||
}
|
||||
$results = prepare_choice_show_results($choice, $course, $cm, $users);
|
||||
|
||||
$options = array();
|
||||
$fullnamecap = has_capability('moodle/site:viewfullnames', $context);
|
||||
foreach ($results->options as $optionid => $option) {
|
||||
|
||||
$userresponses = array();
|
||||
$numberofuser = 0;
|
||||
$percentageamount = 0;
|
||||
if (property_exists($option, 'user') and
|
||||
(has_capability('mod/choice:readresponses', $context) or choice_can_view_results($choice))) {
|
||||
$numberofuser = count($option->user);
|
||||
$percentageamount = ((float)$numberofuser / (float)$results->numberofuser) * 100.0;
|
||||
if ($choice->publish) {
|
||||
foreach ($option->user as $userresponse) {
|
||||
$response = array();
|
||||
$response['userid'] = $userresponse->id;
|
||||
$response['fullname'] = fullname($userresponse, $fullnamecap);
|
||||
$usercontext = context_user::instance($userresponse->id, IGNORE_MISSING);
|
||||
if ($usercontext) {
|
||||
$profileimageurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null,
|
||||
'/', 'f1')->out(false);
|
||||
} else {
|
||||
$profileimageurl = '';
|
||||
}
|
||||
$response['profileimageurl'] = $profileimageurl;
|
||||
// Add optional properties.
|
||||
foreach (array('answerid', 'timemodified') as $field) {
|
||||
if (property_exists($userresponse, 'answerid')) {
|
||||
$response[$field] = $userresponse->$field;
|
||||
}
|
||||
}
|
||||
$userresponses[] = $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$options[] = array('id' => $optionid,
|
||||
'text' => format_string($option->text, true, array('context' => $context)),
|
||||
'maxanswer' => $option->maxanswer,
|
||||
'userresponses' => $userresponses,
|
||||
'numberofuser' => $numberofuser,
|
||||
'percentageamount' => $percentageamount
|
||||
);
|
||||
}
|
||||
|
||||
$warnings = array();
|
||||
return array(
|
||||
'options' => $options,
|
||||
'warnings' => $warnings
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the get_choice_results return value.
|
||||
*
|
||||
* @return external_single_structure
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function get_choice_results_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'options' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'id' => new external_value(PARAM_INT, 'choice instance id'),
|
||||
'text' => new external_value(PARAM_RAW, 'text of the choice'),
|
||||
'maxanswer' => new external_value(PARAM_INT, 'maximum number of answers'),
|
||||
'userresponses' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'user id'),
|
||||
'fullname' => new external_value(PARAM_NOTAGS, 'user full name'),
|
||||
'profileimageurl' => new external_value(PARAM_URL, 'profile user image url'),
|
||||
'answerid' => new external_value(PARAM_INT, 'answer id', VALUE_OPTIONAL),
|
||||
'timemodified' => new external_value(PARAM_INT, 'time of modification', VALUE_OPTIONAL),
|
||||
), 'User responses'
|
||||
)
|
||||
),
|
||||
'numberofuser' => new external_value(PARAM_INT, 'number of users answers'),
|
||||
'percentageamount' => new external_value(PARAM_FLOAT, 'percentage of users answers')
|
||||
), 'Options'
|
||||
)
|
||||
),
|
||||
'warnings' => new external_warnings(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
39
mod/choice/db/services.php
Normal file
39
mod/choice/db/services.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
// This file is part of 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/>.
|
||||
|
||||
/**
|
||||
* Choice external functions and service definitions.
|
||||
*
|
||||
* @package mod_choice
|
||||
* @category external
|
||||
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
$functions = array(
|
||||
|
||||
'mod_choice_get_choice_results' => array(
|
||||
'classname' => 'mod_choice_external',
|
||||
'methodname' => 'get_choice_results',
|
||||
'description' => 'Retrieve users results for a given choice.',
|
||||
'type' => 'read',
|
||||
'capabilities' => ''
|
||||
),
|
||||
|
||||
);
|
@ -921,3 +921,47 @@ function choice_print_overview($courses, &$htmlarray) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get my responses on a given choice.
|
||||
*
|
||||
* @param stdClass $choice Choice record
|
||||
* @return array of choice answers records
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
function choice_get_my_response($choice) {
|
||||
global $DB, $USER;
|
||||
return $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if we are allowd to view the choice results.
|
||||
*
|
||||
* @param stdClass $choice Choice record
|
||||
* @param rows|null $current my choice responses
|
||||
* @param bool|null $choiceopen if the choice is open
|
||||
* @return bool true if we can view the results, false otherwise.
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
function choice_can_view_results($choice, $current = null, $choiceopen = null) {
|
||||
|
||||
if (is_null($choiceopen)) {
|
||||
$timenow = time();
|
||||
if ($choice->timeclose != 0 && $timenow > $choice->timeclose) {
|
||||
$choiceopen = false;
|
||||
} else {
|
||||
$choiceopen = true;
|
||||
}
|
||||
}
|
||||
if (empty($current)) {
|
||||
$current = choice_get_my_response($choice);
|
||||
}
|
||||
|
||||
if ($choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
|
||||
($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and !empty($current)) or
|
||||
($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2015051100; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->version = 2015051101; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->requires = 2015050500; // Requires this Moodle version
|
||||
$plugin->component = 'mod_choice'; // Full name of the plugin (used for diagnostics)
|
||||
$plugin->cron = 0;
|
||||
|
@ -129,7 +129,7 @@ if ($choice->intro) {
|
||||
}
|
||||
|
||||
$timenow = time();
|
||||
$current = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
|
||||
$current = choice_get_my_response($choice);
|
||||
//if user has already made a selection, and they are not allowed to update it or if choice is not open, show their selected answer.
|
||||
if (isloggedin() && (!empty($current)) &&
|
||||
(empty($choice->allowupdate) || ($timenow > $choice->timeclose)) ) {
|
||||
@ -194,9 +194,7 @@ if (!$choiceformshown) {
|
||||
}
|
||||
|
||||
// print the results at the bottom of the screen
|
||||
if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
|
||||
($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current) or
|
||||
($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) {
|
||||
if (choice_can_view_results($choice, $current, $choiceopen)) {
|
||||
|
||||
if (!empty($choice->showunanswered)) {
|
||||
$choice->option[0] = get_string('notanswered', 'choice');
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2015082800.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2015082800.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user