diff --git a/lib/db/services.php b/lib/db/services.php index 6afdfdd03a8..23862ed3ae1 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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, diff --git a/mod/choice/classes/external.php b/mod/choice/classes/external.php new file mode 100644 index 00000000000..2071ecb2203 --- /dev/null +++ b/mod/choice/classes/external.php @@ -0,0 +1,170 @@ +. + +/** + * Choice module external API + * + * @package mod_choice + * @category external + * @copyright 2015 Costantino Cito + * @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 + * @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(), + ) + ); + } + +} diff --git a/mod/choice/db/services.php b/mod/choice/db/services.php new file mode 100644 index 00000000000..b894723c2c1 --- /dev/null +++ b/mod/choice/db/services.php @@ -0,0 +1,39 @@ +. + +/** + * Choice external functions and service definitions. + * + * @package mod_choice + * @category external + * @copyright 2015 Juan Leyva + * @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' => '' + ), + +); diff --git a/mod/choice/lib.php b/mod/choice/lib.php index 8d620763081..51cda3b0f9c 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -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; +} diff --git a/mod/choice/version.php b/mod/choice/version.php index bb646e37c75..0263b8d4b94 100644 --- a/mod/choice/version.php +++ b/mod/choice/version.php @@ -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; diff --git a/mod/choice/view.php b/mod/choice/view.php index 500db553788..6c3c53af7c3 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -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'); diff --git a/version.php b/version.php index 42e683c808d..faf17f0586e 100644 --- a/version.php +++ b/version.php @@ -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.