MDL-57919 mod_data: New WS mod_data_get_fields

This commit is contained in:
Juan Leyva 2017-02-13 12:39:37 +01:00
parent aefe9c5faa
commit a934c89639
5 changed files with 187 additions and 1 deletions

View File

@ -32,6 +32,7 @@ require_once($CFG->dirroot . "/mod/data/locallib.php");
use mod_data\external\database_summary_exporter;
use mod_data\external\record_exporter;
use mod_data\external\content_exporter;
use mod_data\external\field_exporter;
/**
* Database module external functions
@ -567,4 +568,78 @@ class mod_data_external extends external_api {
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_fields_parameters() {
return new external_function_parameters(
array(
'databaseid' => new external_value(PARAM_INT, 'Database instance id.'),
)
);
}
/**
* Return the list of configured fields for the given database.
*
* @param int $databaseid the database id
* @return array of warnings and the fields
* @since Moodle 3.3
* @throws moodle_exception
*/
public static function get_fields($databaseid) {
global $PAGE;
$params = array('databaseid' => $databaseid);
$params = self::validate_parameters(self::get_fields_parameters(), $params);
$warnings = array();
list($database, $course, $cm, $context) = self::validate_database($params['databaseid']);
// Check database is open in time.
$canmanageentries = has_capability('mod/data:manageentries', $context);
data_require_time_available($database, $canmanageentries);
$fieldinstances = data_get_field_instances($database);
foreach ($fieldinstances as $fieldinstance) {
$record = $fieldinstance->field;
// Now get the configs the user can see with his current permissions.
$configs = $fieldinstance->get_config_for_external();
foreach ($configs as $name => $value) {
// Overwrite.
$record->{$name} = $value;
}
$exporter = new field_exporter($record, array('context' => $context));
$fields[] = $exporter->export($PAGE->get_renderer('core'));
}
$result = array(
'fields' => $fields,
'warnings' => $warnings
);
return $result;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 3.3
*/
public static function get_fields_returns() {
return new external_single_structure(
array(
'fields' => new external_multiple_structure(
field_exporter::get_read_structure()
),
'warnings' => new external_warnings()
)
);
}
}

View File

@ -0,0 +1,85 @@
<?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 field data.
*
* @package mod_data
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_data\external;
defined('MOODLE_INTERNAL') || die();
use core\external\exporter;
/**
* Class for exporting field data.
*
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class field_exporter extends exporter {
protected static function define_properties() {
$properties = array(
'id' => array(
'type' => PARAM_INT,
'description' => 'Field id.',
),
'dataid' => array(
'type' => PARAM_INT,
'description' => 'The field type of the content.',
'default' => 0,
),
'type' => array(
'type' => PARAM_PLUGIN,
'description' => 'The field type.',
),
'name' => array(
'type' => PARAM_TEXT,
'description' => 'The field name.',
),
'description' => array(
'type' => PARAM_RAW,
'description' => 'The field description.',
),
'required' => array(
'type' => PARAM_BOOL,
'description' => 'Whether is a field required or not.',
'default' => 0,
),
);
// Field possible parameters.
for ($i = 1; $i <= 10; $i++) {
$properties["param$i"] = array(
'type' => PARAM_RAW,
'description' => 'Field parameters',
'null' => NULL_ALLOWED,
);
}
return $properties;
}
protected static function define_related() {
// Context is required for text formatting.
return array(
'context' => 'context',
);
}
}

View File

@ -67,4 +67,12 @@ $functions = array(
'capabilities' => 'mod/data:viewentry',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_data_get_fields' => array(
'classname' => 'mod_data_external',
'methodname' => 'get_fields',
'description' => 'Return the list of configured fields for the given database.',
'type' => 'read',
'capabilities' => 'mod/data:viewentry',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
);

View File

@ -601,4 +601,22 @@ class mod_data_external_testcase extends externallib_advanced_testcase {
$this->expectException('moodle_exception');
$result = mod_data_external::get_entry($entry21);
}
/**
* Test get_fields.
*/
public function test_get_fields() {
global $DB;
list($entry11, $entry12, $entry13, $entry21) = self::populate_database_with_entries();
$this->setUser($this->student1);
$result = mod_data_external::get_fields($this->data->id);
$result = external_api::clean_returnvalue(mod_data_external::get_fields_returns(), $result);
// Basically compare we retrieve all the fields and the correct values.
$fields = $DB->get_records('data_fields', array('dataid' => $this->data->id), 'id');
foreach ($result['fields'] as $field) {
$this->assertEquals($field, (array) $fields[$field['id']]);
}
}
}

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_data'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;