MDL-58229 mod_feedback: New WS get_unfinished_responses

This commit is contained in:
Juan Leyva 2017-03-14 10:55:16 +01:00 committed by Eloy Lafuente (stronk7)
parent 82b2cd7d19
commit ac9f0a6800
6 changed files with 201 additions and 7 deletions

View file

@ -216,6 +216,23 @@ class mod_feedback_completion extends mod_feedback_structure {
}
}
/**
* Retrieves responses from an unfinished attempt.
*
* @return array the responses (from the feedback_valuetmp table)
* @since Moodle 3.3
*/
public function get_unfinished_responses() {
global $DB;
$responses = array();
$completedtmp = $this->get_current_completed_tmp();
if ($completedtmp) {
$responses = $DB->get_records('feedback_valuetmp', ['completed' => $completedtmp->id]);
}
return $responses;
}
/**
* Returns all temporary values for this feedback or just a value for an item
* @param stdClass $item
@ -224,12 +241,10 @@ class mod_feedback_completion extends mod_feedback_structure {
protected function get_values_tmp($item = null) {
global $DB;
if ($this->valuestmp === null) {
$completedtmp = $this->get_current_completed_tmp();
if ($completedtmp) {
$this->valuestmp = $DB->get_records_menu('feedback_valuetmp',
['completed' => $completedtmp->id], '', 'item, value');
} else {
$this->valuestmp = array();
$this->valuestmp = array();
$responses = $this->get_unfinished_responses();
foreach ($responses as $r) {
$this->valuestmp[$r->item] = $r->value;
}
}
if ($item) {

View file

@ -31,6 +31,7 @@ require_once("$CFG->libdir/externallib.php");
use mod_feedback\external\feedback_summary_exporter;
use mod_feedback\external\feedback_completedtmp_exporter;
use mod_feedback\external\feedback_item_exporter;
use mod_feedback\external\feedback_valuetmp_exporter;
/**
* Feedback external functions
@ -831,4 +832,66 @@ class mod_feedback_external extends external_api {
)
);
}
/**
* Describes the parameters for get_unfinished_responses.
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_unfinished_responses_parameters() {
return new external_function_parameters (
array(
'feedbackid' => new external_value(PARAM_INT, 'Feedback instance id.'),
)
);
}
/**
* Retrieves responses from the current unfinished attempt.
*
* @param array $feedbackid feedback instance id
* @return array of warnings and launch information
* @since Moodle 3.3
*/
public static function get_unfinished_responses($feedbackid) {
global $PAGE;
$params = array('feedbackid' => $feedbackid);
$params = self::validate_parameters(self::get_unfinished_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();
$unfinished = $feedbackcompletion->get_unfinished_responses();
foreach ($unfinished as $u) {
$exporter = new feedback_valuetmp_exporter($u);
$responses[] = $exporter->export($PAGE->get_renderer('core'));
}
$result = array(
'responses' => $responses,
'warnings' => $warnings
);
return $result;
}
/**
* Describes the get_unfinished_responses return value.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_unfinished_responses_returns() {
return new external_single_structure(
array(
'responses' => new external_multiple_structure(
feedback_valuetmp_exporter::get_read_structure()
),
'warnings' => new external_warnings(),
)
);
}
}

View file

@ -0,0 +1,70 @@
<?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 tmp 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 tmp response.
*
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class feedback_valuetmp_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_completedtmp table.',
),
'tmp_completed' => array(
'type' => PARAM_INT,
'description' => 'Old field - not used anymore.',
),
'value' => array(
'type' => PARAM_RAW,
'description' => 'The response value.',
),
);
}
}