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
list($feedback, $course, $cm, $context) = self::validate_feedback($params['feedbackid']);
+ if ($feedback->anonymous != FEEDBACK_ANONYMOUS_NO || $feedback->course == SITEID) {
+ throw new moodle_exception('anonymous', 'feedback');
+ }
+
// Check permissions.
require_capability('mod/feedback:viewreports', $context);
)
);
}
+
+ /**
+ * 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(),
+ )
+ );
+ }
}
mod_feedback_external::get_non_respondents($this->feedback->id);
}
+ /**
+ * Test get_non_respondents from an anonymous feedback.
+ */
+ public function test_get_non_respondents_from_anonymous_feedback() {
+ $this->setUser($this->student);
+ $this->expectException('moodle_exception');
+ $this->expectExceptionMessage(get_string('anonymous', 'feedback'));
+ mod_feedback_external::get_non_respondents($this->feedback->id);
+ }
+
/**
* Test get_non_respondents.
*/
public function test_get_non_respondents() {
+ global $DB;
+
+ // Force non anonymous.
+ $DB->set_field('feedback', 'anonymous', FEEDBACK_ANONYMOUS_NO, array('id' => $this->feedback->id));
+
// Create another student.
$anotherstudent = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($anotherstudent->id, $this->course->id, $this->studentrole->id, 'manual');
$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);
+ }
}