Skip to content

Commit

Permalink
MDL-57813 mod_feedback: New WS mod_feedback_get_last_completed
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Apr 6, 2017
1 parent 007b7ca commit 94478c0
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
61 changes: 61 additions & 0 deletions mod/feedback/classes/external.php
Expand Up @@ -33,6 +33,7 @@
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
Expand Down Expand Up @@ -1181,4 +1182,64 @@ public static function get_responses_analysis_returns() {
)
);
}

/**
* 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(),
)
);
}
}
8 changes: 8 additions & 0 deletions mod/feedback/db/services.php
Expand Up @@ -133,4 +133,12 @@
'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)
),
);
87 changes: 87 additions & 0 deletions mod/feedback/tests/external_test.php
Expand Up @@ -833,4 +833,91 @@ public function test_get_responses_analysis_non_anonymous() {
$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);
}
}
2 changes: 1 addition & 1 deletion mod/feedback/version.php
Expand Up @@ -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;
Expand Down

0 comments on commit 94478c0

Please sign in to comment.