MDL-57813 mod_feedback: New WS mod_feedback_get_last_completed

This commit is contained in:
Juan Leyva 2017-03-17 12:04:56 +01:00
parent 007b7cad82
commit 94478c083d
4 changed files with 157 additions and 1 deletions

View File

@ -33,6 +33,7 @@ use mod_feedback\external\feedback_completedtmp_exporter;
use mod_feedback\external\feedback_item_exporter;
use mod_feedback\external\feedback_valuetmp_exporter;
use mod_feedback\external\feedback_value_exporter;
use mod_feedback\external\feedback_completed_exporter;
/**
* Feedback external functions
@ -1181,4 +1182,64 @@ class mod_feedback_external extends external_api {
)
);
}
/**
* Describes the parameters for get_last_completed.
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_last_completed_parameters() {
return new external_function_parameters (
array(
'feedbackid' => new external_value(PARAM_INT, 'Feedback instance id'),
)
);
}
/**
* Retrieves the last completion record for the current user.
*
* @param int $feedbackid feedback instance id
* @return array of warnings and the last completed record
* @since Moodle 3.3
* @throws moodle_exception
*/
public static function get_last_completed($feedbackid) {
global $PAGE;
$params = array('feedbackid' => $feedbackid);
$params = self::validate_parameters(self::get_last_completed_parameters(), $params);
$warnings = array();
list($feedback, $course, $cm, $context) = self::validate_feedback($params['feedbackid']);
$feedbackcompletion = new mod_feedback_completion($feedback, $cm, $course->id);
if ($feedbackcompletion->is_anonymous()) {
throw new moodle_exception('anonymous', 'feedback');
}
if ($completed = $feedbackcompletion->find_last_completed()) {
$exporter = new feedback_completed_exporter($completed);
return array(
'completed' => $exporter->export($PAGE->get_renderer('core')),
'warnings' => $warnings,
);
}
throw new moodle_exception('not_completed_yet', 'feedback');
}
/**
* Describes the get_last_completed return value.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_last_completed_returns() {
return new external_single_structure(
array(
'completed' => feedback_completed_exporter::get_read_structure(),
'warnings' => new external_warnings(),
)
);
}
}

View File

@ -133,4 +133,12 @@ $functions = array(
'capabilities' => 'mod/feedback:viewreports',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_feedback_get_last_completed' => array(
'classname' => 'mod_feedback_external',
'methodname' => 'get_last_completed',
'description' => 'Retrieves the last completion record for the current user.',
'type' => 'read',
'capabilities' => 'mod/feedback:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);

View File

@ -833,4 +833,91 @@ class mod_feedback_external_testcase extends externallib_advanced_testcase {
$this->assertNotEmpty($attempt['userid']); // Is not anonymous.
}
}
/**
* Test get_last_completed for feedback anonymous not completed.
*/
public function test_get_last_completed_anonymous_not_completed() {
global $DB;
// Force anonymous.
$DB->set_field('feedback', 'anonymous', FEEDBACK_ANONYMOUS_YES, array('id' => $this->feedback->id));
// Test user with full capabilities that didn't complete the feedback.
$this->setUser($this->student);
$this->expectExceptionMessage(get_string('anonymous', 'feedback'));
$this->expectException('moodle_exception');
mod_feedback_external::get_last_completed($this->feedback->id);
}
/**
* Test get_last_completed for feedback anonymous and completed.
*/
public function test_get_last_completed_anonymous_completed() {
global $DB;
// Force anonymous.
$DB->set_field('feedback', 'anonymous', FEEDBACK_ANONYMOUS_YES, array('id' => $this->feedback->id));
// Add one completion record..
$record = [
'feedback' => $this->feedback->id,
'userid' => $this->student->id,
'timemodified' => time() - DAYSECS,
'random_response' => 0,
'anonymous_response' => FEEDBACK_ANONYMOUS_YES,
'courseid' => $this->course->id,
];
$record['id'] = $DB->insert_record('feedback_completed', (object) $record);
// Test user with full capabilities.
$this->setUser($this->student);
$this->expectExceptionMessage(get_string('anonymous', 'feedback'));
$this->expectException('moodle_exception');
mod_feedback_external::get_last_completed($this->feedback->id);
}
/**
* Test get_last_completed for feedback not anonymous and completed.
*/
public function test_get_last_completed_not_anonymous_completed() {
global $DB;
// Force non anonymous.
$DB->set_field('feedback', 'anonymous', FEEDBACK_ANONYMOUS_NO, array('id' => $this->feedback->id));
// Add one completion record..
$record = [
'feedback' => $this->feedback->id,
'userid' => $this->student->id,
'timemodified' => time() - DAYSECS,
'random_response' => 0,
'anonymous_response' => FEEDBACK_ANONYMOUS_NO,
'courseid' => $this->course->id,
];
$record['id'] = $DB->insert_record('feedback_completed', (object) $record);
// Test user with full capabilities.
$this->setUser($this->student);
$result = mod_feedback_external::get_last_completed($this->feedback->id);
$result = external_api::clean_returnvalue(mod_feedback_external::get_last_completed_returns(), $result);
$this->assertEquals($record, $result['completed']);
}
/**
* Test get_last_completed for feedback not anonymous and not completed.
*/
public function test_get_last_completed_not_anonymous_not_completed() {
global $DB;
// Force anonymous.
$DB->set_field('feedback', 'anonymous', FEEDBACK_ANONYMOUS_NO, array('id' => $this->feedback->id));
// Test user with full capabilities that didn't complete the feedback.
$this->setUser($this->student);
$this->expectExceptionMessage(get_string('not_completed_yet', 'feedback'));
$this->expectException('moodle_exception');
mod_feedback_external::get_last_completed($this->feedback->id);
}
}

View File

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2017032803; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2017032804; // 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;