Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 adding 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 add_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
            'subject' => new external_value(PARAM_TEXT, 'Blog subject'),
49
            'summary' => new external_value(PARAM_RAW, 'Blog post content'),
50
            'summaryformat' => new external_format_value('summary'),
51
            'options' => new external_multiple_structure (
52
                new external_single_structure(
53
                    [
54
                        'name' => new external_value(PARAM_ALPHANUM,
55
                            'The allowed keys (value format) are:
56
                            inlineattachmentsid (int); the draft file area id for inline attachments. Default to 0.
57
                            attachmentsid (int); the draft file area id for attachments. Default to 0.
58
                            publishstate (str); the publish state of the entry (draft, site or public). Default to site.
59
                            courseassoc (int); the course id to associate the entry with. Default to 0.
60
                            modassoc (int); the module id to associate the entry with. Default to 0.
61
                            tags (str); the tags to associate the entry with, comma separated. Default to empty.'),
62
                        'value' => new external_value(PARAM_RAW, 'the value of the option (validated inside the function)'),
63
                    ]
64
                ), 'Optional settings', VALUE_DEFAULT, []
65
            ),
66
        ]);
67
    }
68
 
69
    /**
70
     * Add the indicated glossary entry.
71
     *
72
     * @param string $subject    the glossary subject
73
     * @param string $summary the subject summary
74
     * @param int $summaryformat the subject summary format
75
     * @param array $options    additional settings
76
     * @return array with result and warnings
77
     * @throws moodle_exception
78
     */
79
    public static function execute(string $subject, string $summary, int $summaryformat,
80
            array $options = []): array {
81
 
82
        global $DB, $CFG;
83
        require_once($CFG->dirroot . '/blog/lib.php');
84
        require_once($CFG->dirroot . '/blog/locallib.php');
85
 
86
        $params = self::validate_parameters(self::execute_parameters(), compact('subject', 'summary',
87
            'summaryformat', 'options'));
88
 
89
        if (empty($CFG->enableblogs)) {
90
            throw new moodle_exception('blogdisable', 'blog');
91
        }
92
 
93
        $context = context_system::instance();
94
 
95
        if (!has_capability('moodle/blog:create', $context)) {
96
            throw new \moodle_exception('cannoteditentryorblog', 'blog');
97
        }
98
 
99
        // Prepare the entry object.
100
        $entrydata = new \stdClass();
101
        $entrydata->subject = $params['subject'];
102
        $entrydata->summary_editor = [
103
            'text' => $params['summary'],
104
            'format' => $params['summaryformat'],
105
        ];
106
        $entrydata->publishstate = 'site';
107
 
108
        // Options.
109
        foreach ($params['options'] as $option) {
110
            $name = trim($option['name']);
111
            switch ($name) {
112
                case 'inlineattachmentsid':
113
                    $entrydata->summary_editor['itemid'] = clean_param($option['value'], PARAM_INT);
114
                    break;
115
                case 'attachmentsid':
116
                    $entrydata->attachment_filemanager = clean_param($option['value'], PARAM_INT);
117
                    break;
118
                case 'publishstate':
119
                    $entrydata->publishstate = clean_param($option['value'], PARAM_ALPHA);
120
                    $applicable = \blog_entry::get_applicable_publish_states();
121
                    if (empty($applicable[$entrydata->publishstate])) {
122
                        throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
123
                    }
124
                    break;
125
                case 'courseassoc':
126
                case 'modassoc':
127
                    $entrydata->{$name} = clean_param($option['value'], PARAM_INT);
128
                    if (!$CFG->useblogassociations) {
129
                        throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
130
                    }
131
                    break;
132
                case 'tags':
133
                    $entrydata->tags = clean_param($option['value'], PARAM_TAGLIST);
134
                    // Convert to the expected format.
135
                    $entrydata->tags = explode(',', $entrydata->tags);
136
                    break;
137
                default:
138
                    throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
139
            }
140
        }
141
 
142
        // Validate course association. We need to convert the course id to context.
143
        if (!empty($entrydata->courseassoc)) {
144
            $coursecontext = context_course::instance($entrydata->courseassoc);
145
            $entrydata->courseid = $entrydata->courseassoc;
146
            $entrydata->courseassoc = $coursecontext->id;   // Convert to context.
147
            $context = $coursecontext;
148
        }
149
 
150
        // Validate mod association.
151
        if (!empty($entrydata->modassoc)) {
152
            $modcontext = context_module::instance($entrydata->modassoc);
153
            if (!empty($coursecontext) && $coursecontext->id != $modcontext->get_course_context(true)->id) {
154
                throw new moodle_exception('errorinvalidparam', 'webservice', '', 'modassoc');
155
            }
156
            $entrydata->coursemoduleid = $entrydata->modassoc;
157
            $entrydata->modassoc = $modcontext->id; // Convert to context.
158
            $context = $modcontext;
159
        }
160
 
161
        // Validate context for where the blog entry is going to be posted.
162
        self::validate_context($context);
163
 
164
        [$summaryoptions, $attachmentoptions] = blog_get_editor_options();
165
 
166
        $blogentry = new \blog_entry(null, $entrydata, null);
167
        $blogentry->add();
168
        $blogentry->edit((array) $entrydata, null, $summaryoptions, $attachmentoptions);
169
 
170
        return [
171
            'entryid' => $blogentry->id,
172
            'warnings' => [],
173
        ];
174
    }
175
 
176
    /**
177
     * Return.
178
     *
179
     * @return external_single_structure
180
     */
181
    public static function execute_returns(): external_single_structure {
182
        return new external_single_structure([
183
            'entryid' => new external_value(PARAM_INT, 'The new entry id.'),
184
            'warnings' => new external_warnings(),
185
        ]);
186
    }
187
}