return $this->valuestmp;
}
+ /**
+ * Retrieves responses from an finished attempt.
+ *
+ * @return array the responses (from the feedback_value table)
+ * @since Moodle 3.3
+ */
+ public function get_finished_responses() {
+ global $DB;
+ $responses = array();
+
+ if ($this->completed) {
+ $responses = $DB->get_records('feedback_value', ['completed' => $this->completed->id]);
+ }
+ return $responses;
+ }
+
/**
* Returns all completed values for this feedback or just a value for an item
* @param stdClass $item
protected function get_values($item = null) {
global $DB;
if ($this->values === null) {
- if ($this->completed) {
- $this->values = $DB->get_records_menu('feedback_value',
- ['completed' => $this->completed->id], '', 'item, value');
- } else {
- $this->values = array();
+ $this->values = array();
+ $responses = $this->get_finished_responses();
+ foreach ($responses as $r) {
+ $this->values[$r->item] = $r->value;
}
}
if ($item) {
*
* @return stdClass record from feedback_completed or false if not found
*/
- protected function find_last_completed() {
+ public function find_last_completed() {
global $USER, $DB;
- if (isloggedin() || isguestuser()) {
+ if (!isloggedin() || isguestuser()) {
// Not possible to retrieve completed feedback for guests.
return false;
}
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;
/**
* Feedback external functions
)
);
}
+
+ /**
+ * Describes the parameters for get_finished_responses.
+ *
+ * @return external_function_parameters
+ * @since Moodle 3.3
+ */
+ public static function get_finished_responses_parameters() {
+ return new external_function_parameters (
+ array(
+ 'feedbackid' => new external_value(PARAM_INT, 'Feedback instance id.'),
+ )
+ );
+ }
+
+ /**
+ * Retrieves responses from the last finished attempt.
+ *
+ * @param array $feedbackid feedback instance id
+ * @return array of warnings and the responses
+ * @since Moodle 3.3
+ */
+ public static function get_finished_responses($feedbackid) {
+ global $PAGE;
+
+ $params = array('feedbackid' => $feedbackid);
+ $params = self::validate_parameters(self::get_finished_responses_parameters(), $params);
+ $warnings = $itemsdata = array();
+
+ list($feedback, $course, $cm, $context) = self::validate_feedback($params['feedbackid']);
+ $feedbackcompletion = new mod_feedback_completion($feedback, $cm, $course->id);
+
+ $responses = array();
+ // Load and get the responses from the last completed feedback.
+ $feedbackcompletion->find_last_completed();
+ $unfinished = $feedbackcompletion->get_finished_responses();
+ foreach ($unfinished as $u) {
+ $exporter = new feedback_value_exporter($u);
+ $responses[] = $exporter->export($PAGE->get_renderer('core'));
+ }
+
+ $result = array(
+ 'responses' => $responses,
+ 'warnings' => $warnings
+ );
+ return $result;
+ }
+
+ /**
+ * Describes the get_finished_responses return value.
+ *
+ * @return external_single_structure
+ * @since Moodle 3.3
+ */
+ public static function get_finished_responses_returns() {
+ return new external_single_structure(
+ array(
+ 'responses' => new external_multiple_structure(
+ feedback_value_exporter::get_read_structure()
+ ),
+ 'warnings' => new external_warnings(),
+ )
+ );
+ }
}
--- /dev/null
+<?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/>.
+
+/**
+ * Class for exporting a feedback response.
+ *
+ * @package mod_feedback
+ * @copyright 2017 Juan Leyva <juan@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace mod_feedback\external;
+defined('MOODLE_INTERNAL') || die();
+
+use core\external\exporter;
+
+/**
+ * Class for exporting a feedback response.
+ *
+ * @copyright 2017 Juan Leyva <juan@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class feedback_value_exporter extends exporter {
+
+ /**
+ * Return the list of properties.
+ *
+ * @return array list of properties
+ */
+ protected static function define_properties() {
+ return array(
+ 'id' => array(
+ 'type' => PARAM_INT,
+ 'description' => 'The record id.',
+ ),
+ 'course_id' => array(
+ 'type' => PARAM_INT,
+ 'description' => 'The course id this record belongs to.',
+ ),
+ 'item' => array(
+ 'type' => PARAM_INT,
+ 'description' => 'The item id that was responded.',
+ ),
+ 'completed' => array(
+ 'type' => PARAM_INT,
+ 'description' => 'Reference to the feedback_completed table.',
+ ),
+ 'tmp_completed' => array(
+ 'type' => PARAM_INT,
+ 'description' => 'Old field - not used anymore.',
+ ),
+ 'value' => array(
+ 'type' => PARAM_RAW,
+ 'description' => 'The response value.',
+ ),
+ );
+ }
+}
'capabilities' => 'mod/feedback:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
+ 'mod_feedback_get_finished_responses' => array(
+ 'classname' => 'mod_feedback_external',
+ 'methodname' => 'get_finished_responses',
+ 'description' => 'Retrieves responses from the last finished attempt.',
+ 'type' => 'read',
+ 'capabilities' => 'mod/feedback:view',
+ 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+ ),
);
}
}
}
+
+ /**
+ * Test get_finished_responses.
+ */
+ public function test_get_finished_responses() {
+ // Test user with full capabilities.
+ $this->setUser($this->student);
+
+ // Create a very simple feedback.
+ $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
+ $numericitem = $feedbackgenerator->create_item_numeric($this->feedback);
+ $textfielditem = $feedbackgenerator->create_item_textfield($this->feedback);
+
+ $pagedata = [
+ ['name' => $numericitem->typ .'_'. $numericitem->id, 'value' => 5],
+ ['name' => $textfielditem->typ .'_'. $textfielditem->id, 'value' => 'abc'],
+ ];
+
+ // Process the feedback, there is only one page so the feedback will be completed.
+ $result = mod_feedback_external::process_page($this->feedback->id, 0, $pagedata);
+ $result = external_api::clean_returnvalue(mod_feedback_external::process_page_returns(), $result);
+ $this->assertTrue($result['completed']);
+
+ // Retrieve the responses.
+ $result = mod_feedback_external::get_finished_responses($this->feedback->id);
+ $result = external_api::clean_returnvalue(mod_feedback_external::get_finished_responses_returns(), $result);
+ // Check that ids and responses match.
+ foreach ($result['responses'] as $r) {
+ if ($r['item'] == $numericitem->id) {
+ $this->assertEquals(5, $r['value']);
+ } else {
+ $this->assertEquals($textfielditem->id, $r['item']);
+ $this->assertEquals('abc', $r['value']);
+ }
+ }
+ }
}
defined('MOODLE_INTERNAL') || die();
-$plugin->version = 2016120510; // The current module version (Date: YYYYMMDDXX)
+$plugin->version = 2016120511; // 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;