1 |
efrain |
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 |
namespace core_blog\external;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use core_external\external_format_value;
|
|
|
21 |
use core_external\external_function_parameters;
|
|
|
22 |
use core_external\external_multiple_structure;
|
|
|
23 |
use core_external\external_single_structure;
|
|
|
24 |
use core_external\external_value;
|
|
|
25 |
use core_external\external_warnings;
|
|
|
26 |
use context_system;
|
|
|
27 |
use context_course;
|
|
|
28 |
use context_module;
|
|
|
29 |
use moodle_exception;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* This is the external method for updating a blog post entry.
|
|
|
33 |
*
|
|
|
34 |
* @package core_blog
|
|
|
35 |
* @copyright 2024 Juan Leyva <juan@moodle.com>
|
|
|
36 |
* @category external
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
class update_entry extends external_api {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Parameters.
|
|
|
43 |
*
|
|
|
44 |
* @return external_function_parameters
|
|
|
45 |
*/
|
|
|
46 |
public static function execute_parameters(): external_function_parameters {
|
|
|
47 |
return new external_function_parameters([
|
|
|
48 |
'entryid' => new external_value(PARAM_INT, 'Blog entry id to update'),
|
|
|
49 |
'subject' => new external_value(PARAM_TEXT, 'Blog subject'),
|
|
|
50 |
'summary' => new external_value(PARAM_RAW, 'Blog post content'),
|
|
|
51 |
'summaryformat' => new external_format_value('summary'),
|
|
|
52 |
'options' => new external_multiple_structure (
|
|
|
53 |
new external_single_structure(
|
|
|
54 |
[
|
|
|
55 |
'name' => new external_value(PARAM_ALPHANUM,
|
|
|
56 |
'The allowed keys (value format) are:
|
|
|
57 |
inlineattachmentsid (int); the draft file area id for inline attachments. Default to 0.
|
|
|
58 |
attachmentsid (int); the draft file area id for attachments. Default to 0.
|
|
|
59 |
publishstate (str); the publish state of the entry (draft, site or public). Default to site.
|
|
|
60 |
courseassoc (int); the course id to associate the entry with. Default to 0.
|
|
|
61 |
modassoc (int); the module id to associate the entry with. Default to 0.
|
|
|
62 |
tags (str); the tags to associate the entry with, comma separated. Default to empty.'),
|
|
|
63 |
'value' => new external_value(PARAM_RAW, 'the value of the option (validated inside the function)'),
|
|
|
64 |
]
|
|
|
65 |
), 'Optional settings', VALUE_DEFAULT, []
|
|
|
66 |
),
|
|
|
67 |
]);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Update the indicated glossary entry.
|
|
|
72 |
*
|
|
|
73 |
* @param int $entryid The entry to update
|
|
|
74 |
* @param string $subject the glossary subject
|
|
|
75 |
* @param string $summary the subject summary
|
|
|
76 |
* @param int $summaryformat the subject summary format
|
|
|
77 |
* @param array $options additional settings
|
|
|
78 |
* @return array with result and warnings
|
|
|
79 |
* @throws moodle_exception
|
|
|
80 |
*/
|
|
|
81 |
public static function execute(int $entryid, string $subject, string $summary, int $summaryformat,
|
|
|
82 |
array $options = []): array {
|
|
|
83 |
|
|
|
84 |
global $DB, $CFG;
|
|
|
85 |
require_once($CFG->dirroot . '/blog/lib.php');
|
|
|
86 |
require_once($CFG->dirroot . '/blog/locallib.php');
|
|
|
87 |
|
|
|
88 |
$params = self::validate_parameters(self::execute_parameters(), compact('entryid', 'subject', 'summary',
|
|
|
89 |
'summaryformat', 'options'));
|
|
|
90 |
|
|
|
91 |
if (empty($CFG->enableblogs)) {
|
|
|
92 |
throw new moodle_exception('blogdisable', 'blog');
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if (!$entry = new \blog_entry($params['entryid'])) {
|
|
|
96 |
throw new moodle_exception('wrongentryid', 'blog');
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if (!blog_user_can_edit_entry($entry)) {
|
|
|
100 |
throw new \moodle_exception('cannoteditentryorblog', 'blog');
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
// Prepare the entry object.
|
|
|
104 |
$entrydata = new \stdClass();
|
|
|
105 |
$entrydata->id = $entry->id;
|
|
|
106 |
$entrydata->subject = $params['subject'];
|
|
|
107 |
$entrydata->summary_editor = [
|
|
|
108 |
'text' => $params['summary'],
|
|
|
109 |
'format' => $params['summaryformat'],
|
|
|
110 |
];
|
|
|
111 |
$entrydata->publishstate = $entry->publishstate;
|
|
|
112 |
$entrydata->courseassoc = $entry->courseassoc;
|
|
|
113 |
$entrydata->modassoc = $entry->modassoc;
|
|
|
114 |
$entrydata->tags = \core_tag_tag::get_item_tags_array('core', 'post', $entry->id);
|
|
|
115 |
|
|
|
116 |
// Options.
|
|
|
117 |
foreach ($params['options'] as $option) {
|
|
|
118 |
$name = trim($option['name']);
|
|
|
119 |
switch ($name) {
|
|
|
120 |
case 'inlineattachmentsid':
|
|
|
121 |
$entrydata->summary_editor['itemid'] = clean_param($option['value'], PARAM_INT);
|
|
|
122 |
break;
|
|
|
123 |
case 'attachmentsid':
|
|
|
124 |
$entrydata->attachment_filemanager = clean_param($option['value'], PARAM_INT);
|
|
|
125 |
break;
|
|
|
126 |
case 'publishstate':
|
|
|
127 |
$entrydata->publishstate = clean_param($option['value'], PARAM_ALPHA);
|
|
|
128 |
$applicable = \blog_entry::get_applicable_publish_states();
|
|
|
129 |
if (empty($applicable[$entrydata->publishstate])) {
|
|
|
130 |
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
|
|
|
131 |
}
|
|
|
132 |
break;
|
|
|
133 |
case 'courseassoc':
|
|
|
134 |
case 'modassoc':
|
|
|
135 |
$entrydata->{$name} = clean_param($option['value'], PARAM_INT);
|
|
|
136 |
if (!$CFG->useblogassociations) {
|
|
|
137 |
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
|
|
|
138 |
}
|
|
|
139 |
break;
|
|
|
140 |
case 'tags':
|
|
|
141 |
$entrydata->tags = clean_param($option['value'], PARAM_TAGLIST);
|
|
|
142 |
// Convert to the expected format.
|
|
|
143 |
$entrydata->tags = explode(',', $entrydata->tags);
|
|
|
144 |
break;
|
|
|
145 |
default:
|
|
|
146 |
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
$context = context_system::instance();
|
|
|
151 |
|
|
|
152 |
// Validate course association. We need to convert the course id to context.
|
|
|
153 |
if (isset($entrydata->courseassoc)) {
|
|
|
154 |
$entrydata->courseid = $entrydata->courseassoc;
|
|
|
155 |
|
|
|
156 |
if (!empty($entrydata->courseid)) {
|
|
|
157 |
$coursecontext = context_course::instance($entrydata->courseassoc);
|
|
|
158 |
|
|
|
159 |
$entrydata->courseassoc = $coursecontext->id; // Convert to context.
|
|
|
160 |
$context = $coursecontext;
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
// Validate mod association.
|
|
|
165 |
if (isset($entrydata->modassoc)) {
|
|
|
166 |
$entrydata->coursemoduleid = $entrydata->modassoc;
|
|
|
167 |
|
|
|
168 |
if (!empty($entrydata->coursemoduleid)) {
|
|
|
169 |
$modcontext = context_module::instance($entrydata->modassoc);
|
|
|
170 |
if (!empty($coursecontext) && $coursecontext->id != $modcontext->get_course_context(true)->id) {
|
|
|
171 |
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'modassoc');
|
|
|
172 |
}
|
|
|
173 |
$entrydata->modassoc = $modcontext->id; // Convert to context.
|
|
|
174 |
$context = $modcontext;
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
// Validate context. It might be upated because of the new association.
|
|
|
179 |
self::validate_context($context);
|
|
|
180 |
|
|
|
181 |
[$summaryoptions, $attachmentoptions] = blog_get_editor_options($entrydata);
|
|
|
182 |
|
|
|
183 |
$entry->edit((array) $entrydata, null, $summaryoptions, $attachmentoptions);
|
|
|
184 |
|
|
|
185 |
return [
|
|
|
186 |
'status' => true,
|
|
|
187 |
'warnings' => [],
|
|
|
188 |
];
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Return.
|
|
|
193 |
*
|
|
|
194 |
* @return external_single_structure
|
|
|
195 |
*/
|
|
|
196 |
public static function execute_returns(): external_single_structure {
|
|
|
197 |
return new external_single_structure([
|
|
|
198 |
'status' => new external_value(PARAM_BOOL, 'The update result, true if everything went well.'),
|
|
|
199 |
'warnings' => new external_warnings(),
|
|
|
200 |
]);
|
|
|
201 |
}
|
|
|
202 |
}
|