1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core_blog\external;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use core_external\external_function_parameters;
|
|
|
21 |
use core_external\external_multiple_structure;
|
|
|
22 |
use core_external\external_single_structure;
|
|
|
23 |
use core_external\external_value;
|
|
|
24 |
use core_external\external_warnings;
|
|
|
25 |
use context_system;
|
|
|
26 |
use context_course;
|
|
|
27 |
use moodle_exception;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* This is the external method for preparing a blog entry to be edited.
|
|
|
31 |
*
|
|
|
32 |
* @package core_blog
|
|
|
33 |
* @copyright 2024 Juan Leyva <juan@moodle.com>
|
|
|
34 |
* @category external
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class prepare_entry_for_edition extends external_api {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Describes the external function parameters.
|
|
|
41 |
*
|
|
|
42 |
* @return external_function_parameters
|
|
|
43 |
*/
|
|
|
44 |
public static function execute_parameters(): external_function_parameters {
|
|
|
45 |
return new external_function_parameters(
|
|
|
46 |
[
|
|
|
47 |
'entryid' => new external_value(PARAM_INT, 'The entry id to edit.'),
|
|
|
48 |
]
|
|
|
49 |
);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Prepare a draft area for editing a blog entry.
|
|
|
54 |
*
|
|
|
55 |
* @param int $entryid The entry id to edit.
|
|
|
56 |
* @throws moodle_exception;
|
|
|
57 |
* @return array draft area information
|
|
|
58 |
*/
|
|
|
59 |
public static function execute(int $entryid): array {
|
|
|
60 |
global $USER, $CFG;
|
|
|
61 |
|
|
|
62 |
require_once($CFG->dirroot . '/blog/lib.php');
|
|
|
63 |
require_once($CFG->dirroot . '/blog/locallib.php');
|
|
|
64 |
|
|
|
65 |
$params = self::validate_parameters(
|
|
|
66 |
self::execute_parameters(),
|
|
|
67 |
[
|
|
|
68 |
'entryid' => $entryid,
|
|
|
69 |
]
|
|
|
70 |
);
|
|
|
71 |
|
|
|
72 |
if (empty($CFG->enableblogs)) {
|
|
|
73 |
throw new moodle_exception('blogdisable', 'blog');
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
if (!$entry = new \blog_entry($params['entryid'])) {
|
|
|
77 |
throw new moodle_exception('wrongentryid', 'blog');
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$courseid = !empty($entry->courseid) ? $entry->courseid : SITEID;
|
|
|
81 |
$context = context_course::instance($courseid);
|
|
|
82 |
$sitecontext = context_system::instance();
|
|
|
83 |
|
|
|
84 |
self::validate_context($context);
|
|
|
85 |
|
|
|
86 |
if (!blog_user_can_edit_entry($entry)) {
|
|
|
87 |
throw new \moodle_exception('cannoteditentryorblog', 'blog');
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
[$summaryoptions, $attachmentoptions] = blog_get_editor_options($entry);
|
|
|
91 |
|
|
|
92 |
$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
|
|
|
93 |
$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext,
|
|
|
94 |
'blog', 'attachment', $entry->id);
|
|
|
95 |
|
|
|
96 |
// Just get a structure compatible with external API.
|
|
|
97 |
array_walk($attachmentoptions, function(&$item, $key) use(&$attachmentoptions) {
|
|
|
98 |
if (!is_scalar($item)) {
|
|
|
99 |
unset($attachmentoptions[$key]);
|
|
|
100 |
return;
|
|
|
101 |
}
|
|
|
102 |
$item = ['name' => $key, 'value' => $item];
|
|
|
103 |
});
|
|
|
104 |
array_walk($summaryoptions, function(&$item, $key) use(&$summaryoptions) {
|
|
|
105 |
if (!is_scalar($item)) {
|
|
|
106 |
unset($summaryoptions[$key]);
|
|
|
107 |
return;
|
|
|
108 |
}
|
|
|
109 |
$item = ['name' => $key, 'value' => $item];
|
|
|
110 |
});
|
|
|
111 |
|
|
|
112 |
$result = [
|
|
|
113 |
'inlineattachmentsid' => $entry->summary_editor['itemid'],
|
|
|
114 |
'attachmentsid' => $entry->attachment_filemanager,
|
|
|
115 |
'areas' => [
|
|
|
116 |
[
|
|
|
117 |
'area' => 'summary',
|
|
|
118 |
'options' => $summaryoptions,
|
|
|
119 |
],
|
|
|
120 |
[
|
|
|
121 |
'area' => 'attachment',
|
|
|
122 |
'options' => $attachmentoptions,
|
|
|
123 |
],
|
|
|
124 |
],
|
|
|
125 |
'warnings' => [],
|
|
|
126 |
];
|
|
|
127 |
return $result;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Return.
|
|
|
132 |
*
|
|
|
133 |
* @return external_single_structure
|
|
|
134 |
*/
|
|
|
135 |
public static function execute_returns() : external_single_structure {
|
|
|
136 |
return new external_single_structure(
|
|
|
137 |
[
|
|
|
138 |
'inlineattachmentsid' => new external_value(PARAM_INT, 'Draft item id for the text editor.'),
|
|
|
139 |
'attachmentsid' => new external_value(PARAM_INT, 'Draft item id for the file manager.'),
|
|
|
140 |
'areas' => new external_multiple_structure(
|
|
|
141 |
new external_single_structure(
|
|
|
142 |
[
|
|
|
143 |
'area' => new external_value(PARAM_ALPHA, 'File area name.'),
|
|
|
144 |
'options' => new external_multiple_structure(
|
|
|
145 |
new external_single_structure(
|
|
|
146 |
[
|
|
|
147 |
'name' => new external_value(PARAM_RAW, 'Name of option.'),
|
|
|
148 |
'value' => new external_value(PARAM_RAW, 'Value of option.'),
|
|
|
149 |
]
|
|
|
150 |
), 'Draft file area options.'
|
|
|
151 |
),
|
|
|
152 |
]
|
|
|
153 |
), 'File areas including options'
|
|
|
154 |
),
|
|
|
155 |
'warnings' => new external_warnings(),
|
|
|
156 |
]
|
|
|
157 |
);
|
|
|
158 |
}
|
|
|
159 |
}
|