mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
Merge branch 'MDL-50944-master' of git://github.com/jleyva/moodle
This commit is contained in:
commit
5760a1b083
@ -1167,6 +1167,10 @@ $services = array(
|
||||
'mod_chat_get_chat_latest_messages',
|
||||
'mod_chat_view_chat',
|
||||
'mod_book_view_book',
|
||||
'mod_choice_get_choice_results',
|
||||
'mod_choice_get_choice_options',
|
||||
'mod_choice_submit_choice_response',
|
||||
'mod_choice_view_choice',
|
||||
),
|
||||
'enabled' => 0,
|
||||
'restrictedusers' => 0,
|
||||
|
454
mod/choice/classes/external.php
Normal file
454
mod/choice/classes/external.php
Normal file
@ -0,0 +1,454 @@
|
||||
<?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(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the parameters for mod_choice_get_choice_options.
|
||||
*
|
||||
* @return external_external_function_parameters
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function get_choice_options_parameters() {
|
||||
return new external_function_parameters (array('choiceid' => new external_value(PARAM_INT, 'choice instance id')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns options for a specific choice
|
||||
*
|
||||
* @param int $choiceid the choice instance id
|
||||
* @return array of options details
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function get_choice_options($choiceid) {
|
||||
global $USER;
|
||||
$warnings = array();
|
||||
$params = self::validate_parameters(self::get_choice_options_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);
|
||||
|
||||
require_capability('mod/choice:choose', $context);
|
||||
|
||||
$groupmode = groups_get_activity_groupmode($cm);
|
||||
$onlyactive = $choice->includeinactive ? false : true;
|
||||
$allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
|
||||
|
||||
$timenow = time();
|
||||
$choiceopen = true;
|
||||
$showpreview = false;
|
||||
|
||||
if ($choice->timeclose != 0) {
|
||||
if ($choice->timeopen > $timenow) {
|
||||
$choiceopen = false;
|
||||
$warnings[1] = get_string("notopenyet", "choice", userdate($choice->timeopen));
|
||||
if ($choice->showpreview) {
|
||||
$warnings[2] = get_string('previewonly', 'choice', userdate($choice->timeopen));
|
||||
$showpreview = true;
|
||||
}
|
||||
}
|
||||
if ($timenow > $choice->timeclose) {
|
||||
$choiceopen = false;
|
||||
$warnings[3] = get_string("expired", "choice", userdate($choice->timeclose));
|
||||
}
|
||||
}
|
||||
$optionsarray = array();
|
||||
|
||||
if ($choiceopen or $showpreview) {
|
||||
|
||||
$options = choice_prepare_options($choice, $USER, $cm, $allresponses);
|
||||
|
||||
foreach ($options['options'] as $option) {
|
||||
$optionarr = array();
|
||||
$optionarr['id'] = $option->attributes->value;
|
||||
$optionarr['text'] = format_string($option->text, true, array('context' => $context));
|
||||
$optionarr['maxanswers'] = $option->maxanswers;
|
||||
$optionarr['displaylayout'] = $option->displaylayout;
|
||||
$optionarr['countanswers'] = $option->countanswers;
|
||||
foreach (array('checked', 'disabled') as $field) {
|
||||
if (property_exists($option->attributes, $field) and $option->attributes->$field == 1) {
|
||||
$optionarr[$field] = 1;
|
||||
} else {
|
||||
$optionarr[$field] = 0;
|
||||
}
|
||||
}
|
||||
// When showpreview is active, we show options as disabled.
|
||||
if ($showpreview or ($optionarr['checked'] == 1 and !$choice->allowupdate)) {
|
||||
$optionarr['disabled'] = 1;
|
||||
}
|
||||
$optionsarray[] = $optionarr;
|
||||
}
|
||||
}
|
||||
foreach ($warnings as $key => $message) {
|
||||
$warnings[$key] = array(
|
||||
'item' => 'choice',
|
||||
'itemid' => $cm->id,
|
||||
'warningcode' => $key,
|
||||
'message' => $message
|
||||
);
|
||||
}
|
||||
return array(
|
||||
'options' => $optionsarray,
|
||||
'warnings' => $warnings
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the get_choice_results return value.
|
||||
*
|
||||
* @return external_multiple_structure
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function get_choice_options_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'options' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'id' => new external_value(PARAM_INT, 'option id'),
|
||||
'text' => new external_value(PARAM_RAW, 'text of the choice'),
|
||||
'maxanswers' => new external_value(PARAM_INT, 'maximum number of answers'),
|
||||
'displaylayout' => new external_value(PARAM_BOOL, 'true for orizontal, otherwise vertical'),
|
||||
'countanswers' => new external_value(PARAM_INT, 'number of answers'),
|
||||
'checked' => new external_value(PARAM_BOOL, 'we already answered'),
|
||||
'disabled' => new external_value(PARAM_BOOL, 'option disabled'),
|
||||
)
|
||||
), 'Options'
|
||||
),
|
||||
'warnings' => new external_warnings(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the parameters for submit_choice_response.
|
||||
*
|
||||
* @return external_external_function_parameters
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function submit_choice_response_parameters() {
|
||||
return new external_function_parameters (
|
||||
array(
|
||||
'choiceid' => new external_value(PARAM_INT, 'choice instance id'),
|
||||
'responses' => new external_multiple_structure(
|
||||
new external_value(PARAM_INT, 'answer id'),
|
||||
'Array of response ids'
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit choice responses
|
||||
*
|
||||
* @param int $choiceid the choice instance id
|
||||
* @param array $responses the response ids
|
||||
* @return array ansers informatinon and warnings
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function submit_choice_response($choiceid, $responses) {
|
||||
global $USER;
|
||||
|
||||
$warnings = array();
|
||||
$params = self::validate_parameters(self::submit_choice_response_parameters(),
|
||||
array(
|
||||
'choiceid' => $choiceid,
|
||||
'responses' => $responses
|
||||
));
|
||||
|
||||
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);
|
||||
|
||||
require_capability('mod/choice:choose', $context);
|
||||
|
||||
$timenow = time();
|
||||
if ($choice->timeclose != 0) {
|
||||
if ($choice->timeopen > $timenow) {
|
||||
throw new moodle_exception("notopenyet", "choice", '', userdate($choice->timeopen));
|
||||
} else if ($timenow > $choice->timeclose) {
|
||||
throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose));
|
||||
}
|
||||
}
|
||||
if (!choice_get_my_response($choice) or $choice->allowupdate) {
|
||||
// When a single response is given, we convert the array to a simple variable
|
||||
// in order to avoid choice_user_submit_response to check with allowmultiple even
|
||||
// for a single response.
|
||||
if (count($params['responses']) == 1) {
|
||||
$params['responses'] = reset($params['responses']);
|
||||
}
|
||||
choice_user_submit_response($params['responses'], $choice, $USER->id, $course, $cm);
|
||||
} else {
|
||||
throw new moodle_exception('missingrequiredcapability', 'webservice', '', 'allowupdate');
|
||||
}
|
||||
$answers = choice_get_my_response($choice);
|
||||
|
||||
return array(
|
||||
'answers' => $answers,
|
||||
'warnings' => $warnings
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the submit_choice_response return value.
|
||||
*
|
||||
* @return external_multiple_structure
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function submit_choice_response_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'answers' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'id' => new external_value(PARAM_INT, 'answer id'),
|
||||
'choiceid' => new external_value(PARAM_INT, 'choiceid'),
|
||||
'userid' => new external_value(PARAM_INT, 'user id'),
|
||||
'optionid' => new external_value(PARAM_INT, 'optionid'),
|
||||
'timemodified' => new external_value(PARAM_INT, 'time of last modification')
|
||||
), 'Answers'
|
||||
)
|
||||
),
|
||||
'warnings' => new external_warnings(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function view_choice_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'choiceid' => new external_value(PARAM_INT, 'choice instance id')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the course module viewed event and update the module completion status.
|
||||
*
|
||||
* @param int $choiceid the choice instance id
|
||||
* @return array of warnings and status result
|
||||
* @since Moodle 3.0
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public static function view_choice($choiceid) {
|
||||
global $CFG;
|
||||
|
||||
$params = self::validate_parameters(self::view_choice_parameters(),
|
||||
array(
|
||||
'choiceid' => $choiceid
|
||||
));
|
||||
$warnings = array();
|
||||
|
||||
// Request and permission validation.
|
||||
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);
|
||||
|
||||
// Trigger course_module_viewed event and completion.
|
||||
choice_view($choice, $course, $cm, $context);
|
||||
|
||||
$result = array();
|
||||
$result['status'] = true;
|
||||
$result['warnings'] = $warnings;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
public static function view_choice_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
|
||||
'warnings' => new external_warnings()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
62
mod/choice/db/services.php
Normal file
62
mod/choice/db/services.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?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' => ''
|
||||
),
|
||||
|
||||
'mod_choice_get_choice_options' => array(
|
||||
'classname' => 'mod_choice_external',
|
||||
'methodname' => 'get_choice_options',
|
||||
'description' => 'Retrieve options for a specific choice.',
|
||||
'type' => 'read',
|
||||
'capabilities' => 'mod/choice:choose'
|
||||
),
|
||||
|
||||
'mod_choice_submit_choice_response' => array(
|
||||
'classname' => 'mod_choice_external',
|
||||
'methodname' => 'submit_choice_response',
|
||||
'description' => 'Submit responses to a specific choice item.',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'mod/choice:choose'
|
||||
),
|
||||
|
||||
'mod_choice_view_choice' => array(
|
||||
'classname' => 'mod_choice_external',
|
||||
'methodname' => 'view_choice',
|
||||
'description' => 'Trigger the course module viewed event and update the module completion status.',
|
||||
'type' => 'write',
|
||||
'capabilities' => ''
|
||||
),
|
||||
);
|
@ -921,3 +921,75 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the activity completed (if required) and trigger the course_module_viewed event.
|
||||
*
|
||||
* @param stdClass $choice choice object
|
||||
* @param stdClass $course course object
|
||||
* @param stdClass $cm course module object
|
||||
* @param stdClass $context context object
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
function choice_view($choice, $course, $cm, $context) {
|
||||
|
||||
// Trigger course_module_viewed event.
|
||||
$params = array(
|
||||
'context' => $context,
|
||||
'objectid' => $choice->id
|
||||
);
|
||||
|
||||
$event = \mod_choice\event\course_module_viewed::create($params);
|
||||
$event->add_record_snapshot('course_modules', $cm);
|
||||
$event->add_record_snapshot('course', $course);
|
||||
$event->add_record_snapshot('choice', $choice);
|
||||
$event->trigger();
|
||||
|
||||
// Completion.
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
}
|
||||
|
353
mod/choice/tests/externallib_test.php
Normal file
353
mod/choice/tests/externallib_test.php
Normal file
@ -0,0 +1,353 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* External choice functions unit tests
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
||||
require_once($CFG->dirroot . '/mod/choice/lib.php');
|
||||
|
||||
/**
|
||||
* External choice functions unit tests
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
class mod_choice_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test get_choice_results
|
||||
*/
|
||||
public function test_get_choice_results() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$params = new stdClass();
|
||||
$params->course = $course->id;
|
||||
$params->option = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza');
|
||||
$params->name = 'First Choice Activity';
|
||||
$params->showresults = CHOICE_SHOWRESULTS_AFTER_ANSWER;
|
||||
$params->publish = 1;
|
||||
$params->allowmultiple = 1;
|
||||
$params->showunanswered = 1;
|
||||
$choice = self::getDataGenerator()->create_module('choice', $params);
|
||||
|
||||
$cm = get_coursemodule_from_id('choice', $choice->cmid);
|
||||
$choiceinstance = choice_get_choice($cm->instance);
|
||||
$options = array_keys($choiceinstance->option);
|
||||
$student1 = $this->getDataGenerator()->create_user();
|
||||
$student2 = $this->getDataGenerator()->create_user();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
|
||||
// Enroll Students in Course1.
|
||||
self::getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
|
||||
self::getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
|
||||
|
||||
$this->setUser($student1);
|
||||
$myanswer = $options[2];
|
||||
choice_user_submit_response($myanswer, $choice, $student1->id, $course, $cm);
|
||||
$results = mod_choice_external::get_choice_results($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
||||
|
||||
// Create an array with optionID as Key.
|
||||
$resultsarr = array();
|
||||
foreach ($results['options'] as $option) {
|
||||
$resultsarr[$option['id']] = $option['userresponses'];
|
||||
}
|
||||
// The stundent1 is the userid who choosed the myanswer(option 3).
|
||||
$this->assertEquals($resultsarr[$myanswer][0]['userid'], $student1->id);
|
||||
// The stundent2 is the userid who didn't answered yet.
|
||||
$this->assertEquals($resultsarr[0][0]['userid'], $student2->id);
|
||||
|
||||
// As Stundent2 we cannot see results (until we answered).
|
||||
$this->setUser($student2);
|
||||
$results = mod_choice_external::get_choice_results($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
||||
// We do not retrieve any response!
|
||||
foreach ($results['options'] as $option) {
|
||||
$this->assertCount(0, $option['userresponses']);
|
||||
}
|
||||
|
||||
$timenow = time();
|
||||
// We can see results only after activity close (even if we didn't answered).
|
||||
$choice->showresults = CHOICE_SHOWRESULTS_AFTER_CLOSE;
|
||||
// Set timeopen and timeclose in the past.
|
||||
$choice->timeopen = $timenow - (60 * 60 * 24 * 3);
|
||||
$choice->timeclose = $timenow + (60 * 60 * 24 * 2);
|
||||
$DB->update_record('choice', $choice);
|
||||
|
||||
$results = mod_choice_external::get_choice_results($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
||||
// We do not retrieve any response (activity is still open).
|
||||
foreach ($results['options'] as $option) {
|
||||
$this->assertCount(0, $option['userresponses']);
|
||||
}
|
||||
|
||||
// We close the activity (setting timeclose in the past).
|
||||
$choice->timeclose = $timenow - (60 * 60 * 24 * 2);
|
||||
$DB->update_record('choice', $choice);
|
||||
// Now as Stundent2 we will see results!
|
||||
$results = mod_choice_external::get_choice_results($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
||||
// Create an array with optionID as Key.
|
||||
$resultsarr = array();
|
||||
foreach ($results['options'] as $option) {
|
||||
$resultsarr[$option['id']] = $option['userresponses'];
|
||||
}
|
||||
// The stundent1 is the userid who choosed the myanswer(option 3).
|
||||
$this->assertEquals($resultsarr[$myanswer][0]['userid'], $student1->id);
|
||||
// The stundent2 is the userid who didn't answered yet.
|
||||
$this->assertEquals($resultsarr[0][0]['userid'], $student2->id);
|
||||
|
||||
// Do not publish user names!
|
||||
$choice->publish = 0;
|
||||
$DB->update_record('choice', $choice);
|
||||
$results = mod_choice_external::get_choice_results($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_results_returns(), $results);
|
||||
// Create an array with optionID as Key.
|
||||
$resultsarr = array();
|
||||
// Does not show any user response!
|
||||
foreach ($results['options'] as $option) {
|
||||
$this->assertCount(0, $option['userresponses']);
|
||||
$resultsarr[$option['id']] = $option;
|
||||
}
|
||||
// But we can see totals and percentages.
|
||||
$this->assertEquals(1, $resultsarr[$myanswer]['numberofuser']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_choice_options
|
||||
*/
|
||||
public function test_get_choice_options() {
|
||||
global $DB;
|
||||
|
||||
// Warningcodes.
|
||||
$notopenyet = 1;
|
||||
$previewonly = 2;
|
||||
$expired = 3;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$timenow = time();
|
||||
$timeopen = $timenow + (60 * 60 * 24 * 2);
|
||||
$timeclose = $timenow + (60 * 60 * 24 * 7);
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$possibleoptions = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza');
|
||||
$params = array();
|
||||
$params['course'] = $course->id;
|
||||
$params['option'] = $possibleoptions;
|
||||
$params['name'] = 'First Choice Activity';
|
||||
$params['showpreview'] = 0;
|
||||
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('mod_choice');
|
||||
$choice = $generator->create_instance($params);
|
||||
|
||||
$student1 = $this->getDataGenerator()->create_user();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
// Enroll Students in Course.
|
||||
self::getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
|
||||
$this->setUser($student1);
|
||||
|
||||
$results = mod_choice_external::get_choice_options($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results);
|
||||
// We should retrieve all options.
|
||||
$this->assertCount(count($possibleoptions), $results['options']);
|
||||
|
||||
// Here we force timeopen/close in the future.
|
||||
$choice->timeopen = $timeopen;
|
||||
$choice->timeclose = $timeclose;
|
||||
$DB->update_record('choice', $choice);
|
||||
|
||||
$results = mod_choice_external::get_choice_options($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results);
|
||||
// We should retrieve no options.
|
||||
$this->assertCount(0, $results['options']);
|
||||
$this->assertEquals($notopenyet, $results['warnings'][0]['warningcode']);
|
||||
|
||||
// Here we see the options because of preview!
|
||||
$choice->showpreview = 1;
|
||||
$DB->update_record('choice', $choice);
|
||||
$results = mod_choice_external::get_choice_options($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results);
|
||||
// We should retrieve all options.
|
||||
$this->assertCount(count($possibleoptions), $results['options']);
|
||||
|
||||
foreach ($results['options'] as $option) {
|
||||
// Each option is disabled as this is only the preview!
|
||||
$this->assertEquals(1, $option['disabled']);
|
||||
}
|
||||
$warnings = array();
|
||||
foreach ($results['warnings'] as $warning) {
|
||||
$warnings[$warning['warningcode']] = $warning['message'];
|
||||
}
|
||||
$this->assertTrue(isset($warnings[$previewonly]));
|
||||
$this->assertTrue(isset($warnings[$notopenyet]));
|
||||
|
||||
// Simulate activity as opened!
|
||||
$choice->timeopen = $timenow - (60 * 60 * 24 * 3);
|
||||
$choice->timeclose = $timenow + (60 * 60 * 24 * 2);
|
||||
$DB->update_record('choice', $choice);
|
||||
$cm = get_coursemodule_from_id('choice', $choice->cmid);
|
||||
$choiceinstance = choice_get_choice($cm->instance);
|
||||
$optionsids = array_keys($choiceinstance->option);
|
||||
$myanswerid = $optionsids[2];
|
||||
choice_user_submit_response($myanswerid, $choice, $student1->id, $course, $cm);
|
||||
|
||||
$results = mod_choice_external::get_choice_options($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results);
|
||||
// We should retrieve all options.
|
||||
$this->assertCount(count($possibleoptions), $results['options']);
|
||||
foreach ($results['options'] as $option) {
|
||||
// When we answered and we cannot update our choice.
|
||||
if ($option['id'] == $myanswerid and !$choice->allowupdate) {
|
||||
$this->assertEquals(1, $option['disabled']);
|
||||
$this->assertEquals(1, $option['checked']);
|
||||
} else {
|
||||
$this->assertEquals(0, $option['disabled']);
|
||||
}
|
||||
}
|
||||
|
||||
// Set timeopen and timeclose as older than today!
|
||||
// We simulate what happens when the activity is closed.
|
||||
$choice->timeopen = $timenow - (60 * 60 * 24 * 3);
|
||||
$choice->timeclose = $timenow - (60 * 60 * 24 * 2);
|
||||
$DB->update_record('choice', $choice);
|
||||
$results = mod_choice_external::get_choice_options($choice->id);
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::get_choice_options_returns(), $results);
|
||||
// We should retrieve no options.
|
||||
$this->assertCount(0, $results['options']);
|
||||
$this->assertEquals($expired, $results['warnings'][0]['warningcode']);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test submit_choice_response
|
||||
*/
|
||||
public function test_submit_choice_response() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$params = new stdClass();
|
||||
$params->course = $course->id;
|
||||
$params->option = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza');
|
||||
$params->name = 'First Choice Activity';
|
||||
$params->showresults = CHOICE_SHOWRESULTS_ALWAYS;
|
||||
$params->allowmultiple = 1;
|
||||
$params->showunanswered = 1;
|
||||
$choice = self::getDataGenerator()->create_module('choice', $params);
|
||||
$cm = get_coursemodule_from_id('choice', $choice->cmid);
|
||||
$choiceinstance = choice_get_choice($cm->instance);
|
||||
$options = array_keys($choiceinstance->option);
|
||||
$student1 = $this->getDataGenerator()->create_user();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
|
||||
// Enroll Students in Course1.
|
||||
self::getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
|
||||
|
||||
$this->setUser($student1);
|
||||
$myresponse = $options[2];
|
||||
$results = mod_choice_external::submit_choice_response($choice->id, array($myresponse));
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$results = external_api::clean_returnvalue(mod_choice_external::submit_choice_response_returns(), $results);
|
||||
$myanswers = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $student1->id));
|
||||
$myanswer = reset($myanswers);
|
||||
$this->assertEquals($results['answers'][0]['id'], $myanswer->id);
|
||||
$this->assertEquals($results['answers'][0]['choiceid'], $myanswer->choiceid);
|
||||
$this->assertEquals($results['answers'][0]['userid'], $myanswer->userid);
|
||||
$this->assertEquals($results['answers'][0]['timemodified'], $myanswer->timemodified);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test view_choice
|
||||
*/
|
||||
public function test_view_choice() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
|
||||
$context = context_module::instance($choice->cmid);
|
||||
$cm = get_coursemodule_from_instance('choice', $choice->id);
|
||||
|
||||
// Test invalid instance id.
|
||||
try {
|
||||
mod_choice_external::view_choice(0);
|
||||
$this->fail('Exception expected due to invalid mod_choice instance id.');
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertEquals('invalidcoursemodule', $e->errorcode);
|
||||
}
|
||||
|
||||
// Test not-enrolled user.
|
||||
$user = self::getDataGenerator()->create_user();
|
||||
$this->setUser($user);
|
||||
try {
|
||||
mod_choice_external::view_choice($choice->id);
|
||||
$this->fail('Exception expected due to not enrolled user.');
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertEquals('requireloginerror', $e->errorcode);
|
||||
}
|
||||
|
||||
// Test user with full capabilities.
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
$result = mod_choice_external::view_choice($choice->id);
|
||||
$result = external_api::clean_returnvalue(mod_choice_external::view_choice_returns(), $result);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$event = array_shift($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
$moodlechoice = new \moodle_url('/mod/choice/view.php', array('id' => $cm->id));
|
||||
$this->assertEquals($moodlechoice, $event->get_url());
|
||||
$this->assertEventContextNotUsed($event);
|
||||
$this->assertNotEmpty($event->get_name());
|
||||
|
||||
}
|
||||
}
|
175
mod/choice/tests/lib_test.php
Normal file
175
mod/choice/tests/lib_test.php
Normal file
@ -0,0 +1,175 @@
|
||||
<?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 library functions tests
|
||||
*
|
||||
* @package mod_choice
|
||||
* @category test
|
||||
* @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();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
||||
require_once($CFG->dirroot . '/mod/choice/lib.php');
|
||||
|
||||
/**
|
||||
* Choice module library functions tests
|
||||
*
|
||||
* @package mod_choice
|
||||
* @category test
|
||||
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.0
|
||||
*/
|
||||
class mod_choice_lib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test choice_view
|
||||
* @return void
|
||||
*/
|
||||
public function test_choice_view() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
|
||||
$context = context_module::instance($choice->cmid);
|
||||
$cm = get_coursemodule_from_instance('choice', $choice->id);
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
choice_view($choice, $course, $cm, $context);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$event = array_shift($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
$url = new \moodle_url('/mod/choice/view.php', array('id' => $cm->id));
|
||||
$this->assertEquals($url, $event->get_url());
|
||||
$this->assertEventContextNotUsed($event);
|
||||
$this->assertNotEmpty($event->get_name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test choice_can_view_results
|
||||
* @return void
|
||||
*/
|
||||
public function test_choice_can_view_results() {
|
||||
global $DB, $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
|
||||
$context = context_module::instance($choice->cmid);
|
||||
$cm = get_coursemodule_from_instance('choice', $choice->id);
|
||||
|
||||
// Default values are false, user cannot view results.
|
||||
$canview = choice_can_view_results($choice);
|
||||
$this->assertFalse($canview);
|
||||
|
||||
// Show results forced.
|
||||
$choice->showresults = CHOICE_SHOWRESULTS_ALWAYS;
|
||||
$DB->update_record('choice', $choice);
|
||||
$canview = choice_can_view_results($choice);
|
||||
$this->assertTrue($canview);
|
||||
|
||||
// Show results after closing.
|
||||
$choice->showresults = CHOICE_SHOWRESULTS_AFTER_CLOSE;
|
||||
$DB->update_record('choice', $choice);
|
||||
$canview = choice_can_view_results($choice);
|
||||
$this->assertFalse($canview);
|
||||
|
||||
$choice->timeclose = time() - HOURSECS;
|
||||
$DB->update_record('choice', $choice);
|
||||
$canview = choice_can_view_results($choice);
|
||||
$this->assertTrue($canview);
|
||||
|
||||
// Show results after answering.
|
||||
$choice->timeclose = 0;
|
||||
$choice->showresults = CHOICE_SHOWRESULTS_AFTER_ANSWER;
|
||||
$DB->update_record('choice', $choice);
|
||||
$canview = choice_can_view_results($choice);
|
||||
$this->assertFalse($canview);
|
||||
|
||||
// Get the first option.
|
||||
$choicewithoptions = choice_get_choice($choice->id);
|
||||
$optionids = array_keys($choicewithoptions->option);
|
||||
|
||||
choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm);
|
||||
|
||||
$canview = choice_can_view_results($choice);
|
||||
$this->assertTrue($canview);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test choice_get_my_response
|
||||
* @return void
|
||||
*/
|
||||
public function test_choice_get_my_response() {
|
||||
global $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
|
||||
$context = context_module::instance($choice->cmid);
|
||||
$cm = get_coursemodule_from_instance('choice', $choice->id);
|
||||
|
||||
$choicewithoptions = choice_get_choice($choice->id);
|
||||
$optionids = array_keys($choicewithoptions->option);
|
||||
|
||||
choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm);
|
||||
$responses = choice_get_my_response($choice, $course, $cm, $context);
|
||||
$this->assertCount(1, $responses);
|
||||
$response = array_shift($responses);
|
||||
$this->assertEquals($optionids[0], $response->optionid);
|
||||
|
||||
// Multiple responses.
|
||||
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, 'allowmultiple' => 1));
|
||||
$context = context_module::instance($choice->cmid);
|
||||
$cm = get_coursemodule_from_instance('choice', $choice->id);
|
||||
|
||||
$choicewithoptions = choice_get_choice($choice->id);
|
||||
$optionids = array_keys($choicewithoptions->option);
|
||||
|
||||
choice_user_submit_response($optionids, $choice, $USER->id, $course, $cm);
|
||||
$responses = choice_get_my_response($choice, $course, $cm, $context);
|
||||
$this->assertCount(count($optionids), $responses);
|
||||
foreach ($responses as $resp) {
|
||||
$this->assertContains($resp->optionid, $optionids);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
@ -51,10 +51,6 @@ if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL,
|
||||
$PAGE->set_title($choice->name);
|
||||
$PAGE->set_heading($course->fullname);
|
||||
|
||||
// Mark viewed by user (if required)
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
|
||||
/// Submit any new data if there is any
|
||||
if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
|
||||
$timenow = time();
|
||||
@ -83,6 +79,9 @@ if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && conf
|
||||
}
|
||||
}
|
||||
|
||||
// Completion and trigger events.
|
||||
choice_view($choice, $course, $cm, $context);
|
||||
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading(format_string($choice->name), 2, null);
|
||||
|
||||
@ -99,11 +98,6 @@ $eventdata = array();
|
||||
$eventdata['objectid'] = $choice->id;
|
||||
$eventdata['context'] = $context;
|
||||
|
||||
$event = \mod_choice\event\course_module_viewed::create($eventdata);
|
||||
$event->add_record_snapshot('course_modules', $cm);
|
||||
$event->add_record_snapshot('course', $course);
|
||||
$event->trigger();
|
||||
|
||||
/// Check to see if groups are being used in this choice
|
||||
$groupmode = groups_get_activity_groupmode($cm);
|
||||
|
||||
@ -129,7 +123,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 +188,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 = 2015090900.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2015090901.00; // 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