MDL-57918 mod_data: New WS mod_data_get_entry
[moodle.git] / mod / data / classes / external / record_exporter.php
blob7fecd44474a801f81be69b460c4a36163ad6c7e9
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Class for exporting record data.
20 * @package mod_data
21 * @copyright 2017 Juan Leyva <juan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace mod_data\external;
25 defined('MOODLE_INTERNAL') || die();
27 use core\external\exporter;
28 use renderer_base;
29 use core_user;
31 /**
32 * Class for exporting record data.
34 * @copyright 2017 Juan Leyva <juan@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class record_exporter extends exporter {
39 protected static function define_properties() {
41 return array(
42 'id' => array(
43 'type' => PARAM_INT,
44 'description' => 'Record id.',
46 'userid' => array(
47 'type' => PARAM_INT,
48 'description' => 'The id of the user who created the record.',
49 'default' => 0,
51 'groupid' => array(
52 'type' => PARAM_INT,
53 'description' => 'The group id this record belongs to (0 for no groups).',
54 'default' => 0,
56 'dataid' => array(
57 'type' => PARAM_INT,
58 'description' => 'The database id this record belongs to.',
59 'default' => 0,
61 'timecreated' => array(
62 'type' => PARAM_INT,
63 'description' => 'Time the record was created.',
64 'default' => 0,
66 'timemodified' => array(
67 'type' => PARAM_INT,
68 'description' => 'Last time the record was modified.',
69 'default' => 0,
71 'approved' => array(
72 'type' => PARAM_BOOL,
73 'description' => 'Whether the entry has been approved (if the database is configured in that way).',
74 'default' => 0,
79 protected static function define_related() {
80 return array(
81 'database' => 'stdClass',
82 'user' => 'stdClass?',
83 'context' => 'context',
84 'contents' => 'stdClass[]?',
88 protected static function define_other_properties() {
89 return array(
90 'canmanageentry' => array(
91 'type' => PARAM_BOOL,
92 'description' => 'Whether the current user can manage this entry',
94 'fullname' => array(
95 'type' => PARAM_TEXT,
96 'description' => 'The user who created the entry fullname.',
97 'optional' => true,
99 'contents' => array(
100 'type' => content_exporter::read_properties_definition(),
101 'description' => 'The record contents.',
102 'multiple' => true,
103 'optional' => true,
108 protected function get_other_values(renderer_base $output) {
109 global $PAGE;
111 $values = array(
112 'canmanageentry' => data_user_can_manage_entry($this->data, $this->related['database'], $this->related['context']),
115 if (!empty($this->related['user']) and !empty($this->related['user']->id)) {
116 $values['fullname'] = fullname($this->related['user']);
117 } else if ($this->data->userid) {
118 $user = core_user::get_user($this->data->userid);
119 $values['fullname'] = fullname($user);
122 if (!empty($this->related['contents'])) {
123 $contents = [];
124 foreach ($this->related['contents'] as $content) {
125 $related = array('context' => $this->related['context']);
126 $exporter = new content_exporter($content, $related);
127 $contents[] = $exporter->export($PAGE->get_renderer('core'));
129 $values['contents'] = $contents;
131 return $values;