MDL-64656 core_tag: Return tags in modules and blog
[moodle.git] / mod / data / classes / external / record_exporter.php
CommitLineData
5b901787
JL
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.
13//
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/>.
16
17/**
18 * Class for exporting record data.
19 *
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
23 */
24namespace mod_data\external;
25defined('MOODLE_INTERNAL') || die();
26
27use core\external\exporter;
28use renderer_base;
771effef 29use core_user;
6c344ff2 30use core_tag\external\tag_item_exporter;
5b901787
JL
31
32/**
33 * Class for exporting record data.
34 *
35 * @copyright 2017 Juan Leyva <juan@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class record_exporter extends exporter {
39
40 protected static function define_properties() {
41
42 return array(
43 'id' => array(
44 'type' => PARAM_INT,
45 'description' => 'Record id.',
46 ),
47 'userid' => array(
48 'type' => PARAM_INT,
49 'description' => 'The id of the user who created the record.',
50 'default' => 0,
51 ),
52 'groupid' => array(
53 'type' => PARAM_INT,
54 'description' => 'The group id this record belongs to (0 for no groups).',
55 'default' => 0,
56 ),
57 'dataid' => array(
58 'type' => PARAM_INT,
59 'description' => 'The database id this record belongs to.',
60 'default' => 0,
61 ),
62 'timecreated' => array(
63 'type' => PARAM_INT,
64 'description' => 'Time the record was created.',
65 'default' => 0,
66 ),
67 'timemodified' => array(
68 'type' => PARAM_INT,
69 'description' => 'Last time the record was modified.',
70 'default' => 0,
71 ),
72 'approved' => array(
73 'type' => PARAM_BOOL,
74 'description' => 'Whether the entry has been approved (if the database is configured in that way).',
75 'default' => 0,
76 ),
77 );
78 }
79
80 protected static function define_related() {
81 return array(
82 'database' => 'stdClass',
771effef 83 'user' => 'stdClass?',
5b901787
JL
84 'context' => 'context',
85 'contents' => 'stdClass[]?',
86 );
87 }
88
89 protected static function define_other_properties() {
90 return array(
91 'canmanageentry' => array(
92 'type' => PARAM_BOOL,
93 'description' => 'Whether the current user can manage this entry',
94 ),
95 'fullname' => array(
96 'type' => PARAM_TEXT,
97 'description' => 'The user who created the entry fullname.',
771effef 98 'optional' => true,
5b901787
JL
99 ),
100 'contents' => array(
101 'type' => content_exporter::read_properties_definition(),
102 'description' => 'The record contents.',
103 'multiple' => true,
104 'optional' => true,
105 ),
6c344ff2
JL
106 'tags' => array(
107 'type' => tag_item_exporter::read_properties_definition(),
108 'description' => 'Tags.',
109 'multiple' => true,
110 'optional' => true,
111 ),
5b901787
JL
112 );
113 }
114
115 protected function get_other_values(renderer_base $output) {
116 global $PAGE;
117
118 $values = array(
119 'canmanageentry' => data_user_can_manage_entry($this->data, $this->related['database'], $this->related['context']),
5b901787 120 );
771effef
JL
121
122 if (!empty($this->related['user']) and !empty($this->related['user']->id)) {
123 $values['fullname'] = fullname($this->related['user']);
124 } else if ($this->data->userid) {
125 $user = core_user::get_user($this->data->userid);
126 $values['fullname'] = fullname($user);
127 }
128
5b901787
JL
129 if (!empty($this->related['contents'])) {
130 $contents = [];
131 foreach ($this->related['contents'] as $content) {
132 $related = array('context' => $this->related['context']);
133 $exporter = new content_exporter($content, $related);
134 $contents[] = $exporter->export($PAGE->get_renderer('core'));
135 }
136 $values['contents'] = $contents;
137 }
6c344ff2
JL
138
139 $values['tags'] = \core_tag\external\util::get_item_tags('mod_data', 'data_records', $this->data->id);
140
5b901787
JL
141 return $values;
142 }
143}