MDL-57815 mod_feedback: New Web Service mod_feedback_get_items

This commit is contained in:
Juan Leyva 2017-02-02 13:41:29 +01:00
parent d52973ae19
commit bb676b74fe
5 changed files with 341 additions and 1 deletions

View File

@ -30,6 +30,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;
/**
* Feedback external functions
@ -159,6 +160,28 @@ class mod_feedback_external extends external_api {
return array($feedback, $course, $cm, $context);
}
/**
* Utility function for validating access to feedback.
*
* @param stdClass $feedback feedback object
* @param stdClass $course course object
* @param stdClass $cm course module
* @param stdClass $context context object
* @throws moodle_exception
* @since Moodle 3.3
*/
protected static function validate_feedback_access($feedback, $course, $cm, $context) {
$feedbackcompletion = new mod_feedback_completion($feedback, $cm, $course->id);
if (!$feedbackcompletion->can_complete()) {
throw new required_capability_exception($context, 'mod/feedback:complete', 'nopermission', '');
}
if (!$feedbackcompletion->is_open()) {
throw new moodle_exception('feedback_is_not_open', 'feedback');
}
}
/**
* Describes the parameters for get_feedback_access_information.
*
@ -358,4 +381,70 @@ class mod_feedback_external extends external_api {
)
);
}
/**
* Describes the parameters for get_items.
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_items_parameters() {
return new external_function_parameters (
array(
'feedbackid' => new external_value(PARAM_INT, 'Feedback instance id'),
)
);
}
/**
* Returns the items (questions) in the given feedback.
*
* @param int $feedbackid feedback instance id
* @return array of warnings and feedbacks
* @since Moodle 3.3
*/
public static function get_items($feedbackid) {
global $PAGE;
$params = array('feedbackid' => $feedbackid);
$params = self::validate_parameters(self::get_items_parameters(), $params);
$warnings = array();
list($feedback, $course, $cm, $context) = self::validate_feedback($params['feedbackid']);
self::validate_feedback_access($feedback, $course, $cm, $context);
$feedbackstructure = new mod_feedback_structure($feedback, $cm, $course->id);
$returneditems = array();
if ($items = $feedbackstructure->get_items()) {
foreach ($items as $item) {
$itemnumber = empty($item->itemnr) ? null : $item->itemnr;
unset($item->itemnr); // Added by the function, not part of the record.
$exporter = new feedback_item_exporter($item, array('context' => $context, 'itemnumber' => $itemnumber));
$returneditems[] = $exporter->export($PAGE->get_renderer('core'));
}
}
$result = array(
'items' => $returneditems,
'warnings' => $warnings
);
return $result;
}
/**
* Describes the get_items return value.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_items_returns() {
return new external_single_structure(
array(
'items' => new external_multiple_structure(
feedback_item_exporter::get_read_structure()
),
'warnings' => new external_warnings(),
)
);
}
}

View File

@ -0,0 +1,178 @@
<?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 item (question).
*
* @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 mod_feedback\feedback;
use core\external\exporter;
use renderer_base;
use core_files\external\stored_file_exporter;
/**
* Class for exporting a feedback item (question).
*
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class feedback_item_exporter extends exporter {
protected static function define_properties() {
return array(
'id' => array(
'type' => PARAM_INT,
'description' => 'The record id.',
),
'feedback' => array(
'type' => PARAM_INT,
'description' => 'The feedback instance id this records belongs to.',
'default' => 0,
),
'template' => array(
'type' => PARAM_INT,
'description' => 'If it belogns to a template, the template id.',
'default' => 0,
),
'name' => array(
'type' => PARAM_RAW,
'description' => 'The item name.',
),
'label' => array(
'type' => PARAM_NOTAGS,
'description' => 'The item label.',
),
'presentation' => array(
'type' => PARAM_RAW,
'description' => 'The text describing the item or the available possible answers.',
),
'typ' => array(
'type' => PARAM_ALPHA,
'description' => 'The type of the item.',
),
'hasvalue' => array(
'type' => PARAM_INT,
'description' => 'Whether it has a value or not.',
'default' => 0,
),
'position' => array(
'type' => PARAM_INT,
'description' => 'The position in the list of questions.',
'default' => 0,
),
'required' => array(
'type' => PARAM_BOOL,
'description' => 'Whether is a item (question) required or not.',
'default' => 0,
),
'dependitem' => array(
'type' => PARAM_INT,
'description' => 'The item id this item depend on.',
'default' => 0,
),
'dependvalue' => array(
'type' => PARAM_RAW,
'description' => 'The depend value.',
),
'options' => array(
'type' => PARAM_ALPHA,
'description' => 'Different additional settings for the item (question).',
),
);
}
protected static function define_related() {
return array(
'context' => 'context',
'itemnumber' => 'int?'
);
}
protected static function define_other_properties() {
return array(
'itemfiles' => array(
'type' => stored_file_exporter::read_properties_definition(),
'multiple' => true
),
'itemnumber' => array(
'type' => PARAM_INT,
'description' => 'The item position number',
'null' => NULL_ALLOWED
),
'otherdata' => array(
'type' => PARAM_RAW,
'description' => 'Additional data that may be required by external functions',
'null' => NULL_ALLOWED
),
);
}
protected function get_other_values(renderer_base $output) {
$context = $this->related['context'];
$itemobj = feedback_get_item_class($this->data->typ);
$values = array(
'itemfiles' => array(),
'itemnumber' => $this->related['itemnumber'],
'otherdata' => $itemobj->get_data_for_external($this->data),
);
$fs = get_file_storage();
$files = array();
$itemfiles = $fs->get_area_files($context->id, 'mod_feedback', 'item', $this->data->id, 'filename', false);
if (!empty($itemfiles)) {
foreach ($itemfiles as $storedfile) {
$fileexporter = new stored_file_exporter($storedfile, array('context' => $context));
$files[] = $fileexporter->export($output);
}
$values['itemfiles'] = $files;
}
return $values;
}
/**
* Get the formatting parameters for the name.
*
* @return array
*/
protected function get_format_parameters_for_name() {
return [
'component' => 'mod_feedback',
'filearea' => 'item',
'itemid' => $this->data->id
];
}
/**
* Get the formatting parameters for the presentation.
*
* @return array
*/
protected function get_format_parameters_for_presentation() {
return [
'component' => 'mod_feedback',
'filearea' => 'item',
'itemid' => $this->data->id
];
}
}

View File

@ -61,4 +61,12 @@ $functions = array(
'capabilities' => 'mod/feedback:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_feedback_get_items' => array(
'classname' => 'mod_feedback_external',
'methodname' => 'get_items',
'description' => 'Returns the items (questions) in the given feedback.',
'type' => 'read',
'capabilities' => 'mod/feedback:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);

View File

@ -68,6 +68,34 @@ class mod_feedback_external_testcase extends externallib_advanced_testcase {
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
}
/**
* Helper method to add items to an existing feedback.
*
* @param stdClass $feedback feedback instance
* @param integer $pagescount the number of pages we want in the feedback
* @return array list of items created
*/
public function populate_feedback($feedback, $pagescount = 1) {
$feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
$itemscreated = [];
// Create at least one page.
$itemscreated[] = $feedbackgenerator->create_item_label($feedback);
$itemscreated[] = $feedbackgenerator->create_item_info($feedback);
$itemscreated[] = $feedbackgenerator->create_item_numeric($feedback);
// Check if we want more pages.
for ($i = 1; $i < $pagescount; $i++) {
$itemscreated[] = $feedbackgenerator->create_item_pagebreak($feedback);
$itemscreated[] = $feedbackgenerator->create_item_multichoice($feedback);
$itemscreated[] = $feedbackgenerator->create_item_multichoicerated($feedback);
$itemscreated[] = $feedbackgenerator->create_item_textarea($feedback);
$itemscreated[] = $feedbackgenerator->create_item_textfield($feedback);
$itemscreated[] = $feedbackgenerator->create_item_numeric($feedback);
}
return $itemscreated;
}
/**
* Test test_mod_feedback_get_feedbacks_by_courses
@ -222,6 +250,12 @@ class mod_feedback_external_testcase extends externallib_advanced_testcase {
$this->assertTrue($result['isopen']);
$this->assertTrue($result['isanonymous']);
$this->assertFalse($result['isalreadysubmitted']);
// Add some items to the feedback and check is not empty any more.
self::populate_feedback($this->feedback);
$result = mod_feedback_external::get_feedback_access_information($this->feedback->id);
$result = external_api::clean_returnvalue(mod_feedback_external::get_feedback_access_information_returns(), $result);
$this->assertFalse($result['isempty']);
}
/**
@ -301,4 +335,35 @@ class mod_feedback_external_testcase extends externallib_advanced_testcase {
$result = external_api::clean_returnvalue(mod_feedback_external::get_current_completed_tmp_returns(), $result);
$this->assertEquals($record['id'], $result['feedback']['id']);
}
/**
* Test get_items.
*/
public function test_get_items() {
// Test user with full capabilities.
$this->setUser($this->student);
// Add questions to the feedback, we are adding 2 pages of questions.
$itemscreated = self::populate_feedback($this->feedback, 2);
$result = mod_feedback_external::get_items($this->feedback->id);
$result = external_api::clean_returnvalue(mod_feedback_external::get_items_returns(), $result);
$this->assertCount(count($itemscreated), $result['items']);
$index = 1;
foreach ($result['items'] as $key => $item) {
if (is_numeric($itemscreated[$key])) {
continue; // Page break.
}
// Cannot compare directly the exporter and the db object (exporter have more fields).
$this->assertEquals($itemscreated[$key]->id, $item['id']);
$this->assertEquals($itemscreated[$key]->typ, $item['typ']);
$this->assertEquals($itemscreated[$key]->name, $item['name']);
$this->assertEquals($itemscreated[$key]->label, $item['label']);
if ($item['hasvalue']) {
$this->assertEquals($index, $item['itemnumber']);
$index++;
}
}
}
}

View File

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