MDL-57816 mod_feedback: New WS mod_feedback_launch_feedback
This commit is contained in:
committed by
Eloy Lafuente (stronk7)
parent
a2ccbae441
commit
951ddedf6f
@@ -168,9 +168,10 @@ class mod_feedback_external extends external_api {
|
||||
* @param stdClass $cm course module
|
||||
* @param stdClass $context context object
|
||||
* @throws moodle_exception
|
||||
* @return feedback_completion feedback completion instance
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
protected static function validate_feedback_access($feedback, $course, $cm, $context) {
|
||||
protected static function validate_feedback_access($feedback, $course, $cm, $context, $checksubmit = false) {
|
||||
$feedbackcompletion = new mod_feedback_completion($feedback, $cm, $course->id);
|
||||
|
||||
if (!$feedbackcompletion->can_complete()) {
|
||||
@@ -180,6 +181,15 @@ class mod_feedback_external extends external_api {
|
||||
if (!$feedbackcompletion->is_open()) {
|
||||
throw new moodle_exception('feedback_is_not_open', 'feedback');
|
||||
}
|
||||
|
||||
if ($feedbackcompletion->is_empty()) {
|
||||
throw new moodle_exception('no_items_available_yet', 'feedback');
|
||||
}
|
||||
|
||||
if ($checksubmit && !$feedbackcompletion->can_submit()) {
|
||||
throw new moodle_exception('this_feedback_is_already_submitted', 'feedback');
|
||||
}
|
||||
return $feedbackcompletion;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -447,4 +457,63 @@ class mod_feedback_external extends external_api {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the parameters for launch_feedback.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public static function launch_feedback_parameters() {
|
||||
return new external_function_parameters (
|
||||
array(
|
||||
'feedbackid' => new external_value(PARAM_INT, 'Feedback instance id'),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts or continues a feedback submission
|
||||
*
|
||||
* @param array $feedbackid feedback instance id
|
||||
* @return array of warnings and launch information
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public static function launch_feedback($feedbackid) {
|
||||
global $PAGE;
|
||||
|
||||
$params = array('feedbackid' => $feedbackid);
|
||||
$params = self::validate_parameters(self::launch_feedback_parameters(), $params);
|
||||
$warnings = array();
|
||||
|
||||
list($feedback, $course, $cm, $context) = self::validate_feedback($params['feedbackid']);
|
||||
// Check we can do a new submission (or continue an existing).
|
||||
$feedbackcompletion = self::validate_feedback_access($feedback, $course, $cm, $context, true);
|
||||
|
||||
$gopage = $feedbackcompletion->get_resume_page();
|
||||
if ($gopage === null) {
|
||||
$gopage = -1; // Last page.
|
||||
}
|
||||
|
||||
$result = array(
|
||||
'gopage' => $gopage,
|
||||
'warnings' => $warnings
|
||||
);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the launch_feedback return value.
|
||||
*
|
||||
* @return external_single_structure
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public static function launch_feedback_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'gopage' => new external_value(PARAM_INT, 'The next page to go (-1 if we were already in the last page). 0 for first page.'),
|
||||
'warnings' => new external_warnings(),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,4 +69,12 @@ $functions = array(
|
||||
'capabilities' => 'mod/feedback:view',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
|
||||
),
|
||||
'mod_feedback_launch_feedback' => array(
|
||||
'classname' => 'mod_feedback_external',
|
||||
'methodname' => 'launch_feedback',
|
||||
'description' => 'Starts or continues a feedback submission.',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'mod/feedback:complete',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
|
||||
),
|
||||
);
|
||||
|
||||
@@ -366,4 +366,59 @@ class mod_feedback_external_testcase extends externallib_advanced_testcase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test launch_feedback.
|
||||
*/
|
||||
public function test_launch_feedback() {
|
||||
global $DB;
|
||||
|
||||
// Test user with full capabilities.
|
||||
$this->setUser($this->student);
|
||||
|
||||
// Add questions to the feedback, we are adding 2 pages of questions.
|
||||
$itemscreated = self::populate_feedback($this->feedback, 2);
|
||||
|
||||
// First try a feedback we didn't attempt.
|
||||
$result = mod_feedback_external::launch_feedback($this->feedback->id);
|
||||
$result = external_api::clean_returnvalue(mod_feedback_external::launch_feedback_returns(), $result);
|
||||
$this->assertEquals(0, $result['gopage']);
|
||||
|
||||
// Now, try a feedback that we attempted.
|
||||
// Force non anonymous.
|
||||
$DB->set_field('feedback', 'anonymous', 0, array('id' => $this->feedback->id));
|
||||
// Add a completed_tmp record.
|
||||
$record = [
|
||||
'feedback' => $this->feedback->id,
|
||||
'userid' => $this->student->id,
|
||||
'guestid' => '',
|
||||
'timemodified' => time() - DAYSECS,
|
||||
'random_response' => 0,
|
||||
'anonymous_response' => 2,
|
||||
'courseid' => $this->course->id,
|
||||
];
|
||||
$record['id'] = $DB->insert_record('feedback_completedtmp', (object) $record);
|
||||
|
||||
// Add a response to the feedback for each question type with possible values.
|
||||
$response = [
|
||||
'course_id' => $this->course->id,
|
||||
'item' => $itemscreated[1]->id, // First item is the info question.
|
||||
'completed' => $record['id'],
|
||||
'tmp_completed' => $record['id'],
|
||||
'value' => 'A',
|
||||
];
|
||||
$DB->insert_record('feedback_valuetmp', (object) $response);
|
||||
$response = [
|
||||
'course_id' => $this->course->id,
|
||||
'item' => $itemscreated[2]->id, // Second item is the numeric question.
|
||||
'completed' => $record['id'],
|
||||
'tmp_completed' => $record['id'],
|
||||
'value' => 5,
|
||||
];
|
||||
$DB->insert_record('feedback_valuetmp', (object) $response);
|
||||
|
||||
$result = mod_feedback_external::launch_feedback($this->feedback->id);
|
||||
$result = external_api::clean_returnvalue(mod_feedback_external::launch_feedback_returns(), $result);
|
||||
$this->assertEquals(1, $result['gopage']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2016120505; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->version = 2016120506; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->requires = 2016112900; // Requires this Moodle version
|
||||
$plugin->component = 'mod_feedback'; // Full name of the plugin (used for diagnostics)
|
||||
$plugin->cron = 0;
|
||||
|
||||
Reference in New Issue
Block a user