Merge branch 'MDL-68565' of https://github.com/paulholden/moodle
[moodle.git] / blog / classes / external / post_exporter.php
blobe608c32b754bce10e2a87b32a8ebe92f766d78df
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 a blog post (entry).
20 * @package core_blog
21 * @copyright 2018 Juan Leyva <juan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_blog\external;
25 defined('MOODLE_INTERNAL') || die();
27 use core\external\exporter;
28 use external_util;
29 use external_files;
30 use renderer_base;
31 use context_system;
32 use core_tag\external\tag_item_exporter;
34 /**
35 * Class for exporting a blog post (entry).
37 * @copyright 2018 Juan Leyva <juan@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class post_exporter extends exporter {
42 /**
43 * Return the list of properties.
45 * @return array list of properties
47 protected static function define_properties() {
48 return array(
49 'id' => array(
50 'type' => PARAM_INT,
51 'null' => NULL_ALLOWED,
52 'description' => 'Post/entry id.',
54 'module' => array(
55 'type' => PARAM_ALPHANUMEXT,
56 'null' => NULL_NOT_ALLOWED,
57 'description' => 'Where it was published the post (blog, blog_external...).',
59 'userid' => array(
60 'type' => PARAM_INT,
61 'null' => NULL_NOT_ALLOWED,
62 'default' => 0,
63 'description' => 'Post author.',
65 'courseid' => array(
66 'type' => PARAM_INT,
67 'null' => NULL_NOT_ALLOWED,
68 'default' => 0,
69 'description' => 'Course where the post was created.',
71 'groupid' => array(
72 'type' => PARAM_INT,
73 'null' => NULL_NOT_ALLOWED,
74 'default' => 0,
75 'description' => 'Group post was created for.',
77 'moduleid' => array(
78 'type' => PARAM_INT,
79 'null' => NULL_NOT_ALLOWED,
80 'default' => 0,
81 'description' => 'Module id where the post was created (not used anymore).',
83 'coursemoduleid' => array(
84 'type' => PARAM_INT,
85 'null' => NULL_NOT_ALLOWED,
86 'default' => 0,
87 'description' => 'Course module id where the post was created.',
89 'subject' => array(
90 'type' => PARAM_TEXT,
91 'null' => NULL_NOT_ALLOWED,
92 'description' => 'Post subject.',
94 'summary' => array(
95 'type' => PARAM_RAW,
96 'null' => NULL_ALLOWED,
97 'description' => 'Post summary.',
99 'content' => array(
100 'type' => PARAM_RAW,
101 'null' => NULL_ALLOWED,
102 'description' => 'Post content.',
104 'uniquehash' => array(
105 'type' => PARAM_RAW,
106 'null' => NULL_NOT_ALLOWED,
107 'description' => 'Post unique hash.',
109 'rating' => array(
110 'type' => PARAM_INT,
111 'null' => NULL_NOT_ALLOWED,
112 'default' => 0,
113 'description' => 'Post rating.',
115 'format' => array(
116 'type' => PARAM_INT,
117 'null' => NULL_NOT_ALLOWED,
118 'default' => 0,
119 'description' => 'Post content format.',
121 'summaryformat' => array(
122 'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
123 'type' => PARAM_INT,
124 'default' => FORMAT_MOODLE,
125 'description' => 'Format for the summary field.',
127 'attachment' => array(
128 'type' => PARAM_RAW,
129 'null' => NULL_ALLOWED,
130 'description' => 'Post atachment.',
132 'publishstate' => array(
133 'type' => PARAM_ALPHA,
134 'null' => NULL_NOT_ALLOWED,
135 'default' => 'draft',
136 'description' => 'Post publish state.',
138 'lastmodified' => array(
139 'type' => PARAM_INT,
140 'null' => NULL_NOT_ALLOWED,
141 'default' => 0,
142 'description' => 'When it was last modified.',
144 'created' => array(
145 'type' => PARAM_INT,
146 'null' => NULL_NOT_ALLOWED,
147 'default' => 0,
148 'description' => 'When it was created.',
150 'usermodified' => array(
151 'type' => PARAM_INT,
152 'null' => NULL_ALLOWED,
153 'description' => 'User that updated the post.',
158 protected static function define_related() {
159 return array(
160 'context' => 'context'
164 protected static function define_other_properties() {
165 return array(
166 'summaryfiles' => array(
167 'type' => external_files::get_properties_for_exporter(),
168 'multiple' => true
170 'attachmentfiles' => array(
171 'type' => external_files::get_properties_for_exporter(),
172 'multiple' => true,
173 'optional' => true
175 'tags' => array(
176 'type' => tag_item_exporter::read_properties_definition(),
177 'description' => 'Tags.',
178 'multiple' => true,
179 'optional' => true,
184 protected function get_other_values(renderer_base $output) {
185 $context = context_system::instance(); // Files always on site context.
187 $values['summaryfiles'] = external_util::get_area_files($context->id, 'blog', 'post', $this->data->id);
188 $values['attachmentfiles'] = external_util::get_area_files($context->id, 'blog', 'attachment', $this->data->id);
189 if ($this->data->module == 'blog_external') {
190 // For external blogs, the content field has the external blog id.
191 $values['tags'] = \core_tag\external\util::get_item_tags('core', 'blog_external', $this->data->content);
192 } else {
193 $values['tags'] = \core_tag\external\util::get_item_tags('core', 'post', $this->data->id);
196 return $values;