first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+187
View File
@@ -0,0 +1,187 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_blog\external;
use core_external\external_api;
use core_external\external_format_value;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use context_system;
use context_course;
use context_module;
use moodle_exception;
/**
* This is the external method for adding a blog post entry.
*
* @package core_blog
* @copyright 2024 Juan Leyva <juan@moodle.com>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class add_entry extends external_api {
/**
* Parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'subject' => new external_value(PARAM_TEXT, 'Blog subject'),
'summary' => new external_value(PARAM_RAW, 'Blog post content'),
'summaryformat' => new external_format_value('summary'),
'options' => new external_multiple_structure (
new external_single_structure(
[
'name' => new external_value(PARAM_ALPHANUM,
'The allowed keys (value format) are:
inlineattachmentsid (int); the draft file area id for inline attachments. Default to 0.
attachmentsid (int); the draft file area id for attachments. Default to 0.
publishstate (str); the publish state of the entry (draft, site or public). Default to site.
courseassoc (int); the course id to associate the entry with. Default to 0.
modassoc (int); the module id to associate the entry with. Default to 0.
tags (str); the tags to associate the entry with, comma separated. Default to empty.'),
'value' => new external_value(PARAM_RAW, 'the value of the option (validated inside the function)'),
]
), 'Optional settings', VALUE_DEFAULT, []
),
]);
}
/**
* Add the indicated glossary entry.
*
* @param string $subject the glossary subject
* @param string $summary the subject summary
* @param int $summaryformat the subject summary format
* @param array $options additional settings
* @return array with result and warnings
* @throws moodle_exception
*/
public static function execute(string $subject, string $summary, int $summaryformat,
array $options = []): array {
global $DB, $CFG;
require_once($CFG->dirroot . '/blog/lib.php');
require_once($CFG->dirroot . '/blog/locallib.php');
$params = self::validate_parameters(self::execute_parameters(), compact('subject', 'summary',
'summaryformat', 'options'));
if (empty($CFG->enableblogs)) {
throw new moodle_exception('blogdisable', 'blog');
}
$context = context_system::instance();
if (!has_capability('moodle/blog:create', $context)) {
throw new \moodle_exception('cannoteditentryorblog', 'blog');
}
// Prepare the entry object.
$entrydata = new \stdClass();
$entrydata->subject = $params['subject'];
$entrydata->summary_editor = [
'text' => $params['summary'],
'format' => $params['summaryformat'],
];
$entrydata->publishstate = 'site';
// Options.
foreach ($params['options'] as $option) {
$name = trim($option['name']);
switch ($name) {
case 'inlineattachmentsid':
$entrydata->summary_editor['itemid'] = clean_param($option['value'], PARAM_INT);
break;
case 'attachmentsid':
$entrydata->attachment_filemanager = clean_param($option['value'], PARAM_INT);
break;
case 'publishstate':
$entrydata->publishstate = clean_param($option['value'], PARAM_ALPHA);
$applicable = \blog_entry::get_applicable_publish_states();
if (empty($applicable[$entrydata->publishstate])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
break;
case 'courseassoc':
case 'modassoc':
$entrydata->{$name} = clean_param($option['value'], PARAM_INT);
if (!$CFG->useblogassociations) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
break;
case 'tags':
$entrydata->tags = clean_param($option['value'], PARAM_TAGLIST);
// Convert to the expected format.
$entrydata->tags = explode(',', $entrydata->tags);
break;
default:
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
}
// Validate course association. We need to convert the course id to context.
if (!empty($entrydata->courseassoc)) {
$coursecontext = context_course::instance($entrydata->courseassoc);
$entrydata->courseid = $entrydata->courseassoc;
$entrydata->courseassoc = $coursecontext->id; // Convert to context.
$context = $coursecontext;
}
// Validate mod association.
if (!empty($entrydata->modassoc)) {
$modcontext = context_module::instance($entrydata->modassoc);
if (!empty($coursecontext) && $coursecontext->id != $modcontext->get_course_context(true)->id) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'modassoc');
}
$entrydata->coursemoduleid = $entrydata->modassoc;
$entrydata->modassoc = $modcontext->id; // Convert to context.
$context = $modcontext;
}
// Validate context for where the blog entry is going to be posted.
self::validate_context($context);
[$summaryoptions, $attachmentoptions] = blog_get_editor_options();
$blogentry = new \blog_entry(null, $entrydata, null);
$blogentry->add();
$blogentry->edit((array) $entrydata, null, $summaryoptions, $attachmentoptions);
return [
'entryid' => $blogentry->id,
'warnings' => [],
];
}
/**
* Return.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'entryid' => new external_value(PARAM_INT, 'The new entry id.'),
'warnings' => new external_warnings(),
]);
}
}
+104
View File
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
namespace core_blog\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use context_course;
use moodle_exception;
/**
* This is the external method for deleting a blog post entry.
*
* @package core_blog
* @copyright 2024 Juan Leyva <juan@moodle.com>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_entry extends external_api {
/**
* Describes the external function parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'entryid' => new external_value(PARAM_INT, 'The entry id to remove.'),
]
);
}
/**
* Deletes a blog entry.
*
* @param int $entryid The entry id to remove.
* @throws moodle_exception;
* @return array result of the operation
*/
public static function execute(int $entryid): array {
global $USER, $CFG;
require_once($CFG->dirroot . '/blog/lib.php');
require_once($CFG->dirroot . '/blog/locallib.php');
$params = self::validate_parameters(self::execute_parameters(), ['entryid' => $entryid]);
if (empty($CFG->enableblogs)) {
throw new moodle_exception('blogdisable', 'blog');
}
if (!$entry = new \blog_entry($params['entryid'])) {
throw new moodle_exception('wrongentryid', 'blog');
}
$courseid = !empty($entry->courseid) ? $entry->courseid : SITEID;
$context = context_course::instance($courseid);
self::validate_context($context);
if (!blog_user_can_edit_entry($entry)) {
throw new \moodle_exception('nopermissionstodeleteentry', 'blog');
}
$entry->delete();
$result = [
'status' => true,
'warnings' => [],
];
return $result;
}
/**
* Return.
*
* @return external_single_structure
*/
public static function execute_returns() : external_single_structure {
return new external_single_structure(
[
'status' => new external_value(PARAM_BOOL, 'True indicates the entry was deleted.'),
'warnings' => new external_warnings(),
]
);
}
}
+89
View File
@@ -0,0 +1,89 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_blog\external;
use context_system;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
/**
* External blog API implementation
*
* @package core_blog
* @copyright 2024 Juan Leyva <juan@moodle.com>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_access_information extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters.
* @since Moodle 4.4
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([]);
}
/**
* Convenience function to retrieve some permissions information for the blogs system.
*
* @return array The access information
* @since Moodle 4.4
*/
public static function execute(): array {
$context = context_system::instance();
self::validate_context($context);
return [
'canview' => has_capability('moodle/blog:view', $context),
'cansearch' => has_capability('moodle/blog:search', $context),
'canviewdrafts' => has_capability('moodle/blog:viewdrafts', $context),
'cancreate' => has_capability('moodle/blog:create', $context),
'canmanageentries' => has_capability('moodle/blog:manageentries', $context),
'canmanageexternal' => has_capability('moodle/blog:manageexternal', $context),
'warnings' => [],
];
}
/**
* Returns description of method result value.
*
* @return external_single_structure.
* @since Moodle 4.4
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure(
[
'canview' => new external_value(PARAM_BOOL, 'Whether the user can view blogs'),
'cansearch' => new external_value(PARAM_BOOL, 'Whether the user can search blogs'),
'canviewdrafts' => new external_value(PARAM_BOOL, 'Whether the user can view drafts'),
'cancreate' => new external_value(PARAM_BOOL, 'Whether the user can create blog entries'),
'canmanageentries' => new external_value(PARAM_BOOL, 'Whether the user can manage blog entries'),
'canmanageexternal' => new external_value(PARAM_BOOL, 'Whether the user can manage external blogs'),
'warnings' => new external_warnings(),
]
);
}
}
+207
View File
@@ -0,0 +1,207 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class for exporting a blog post (entry).
*
* @package core_blog
* @copyright 2018 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_blog\external;
defined('MOODLE_INTERNAL') || die();
use core\external\exporter;
use core_external\util as external_util;
use core_external\external_files;
use renderer_base;
use context_system;
use core_tag\external\tag_item_exporter;
/**
* Class for exporting a blog post (entry).
*
* @copyright 2018 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class post_exporter extends exporter {
/**
* Return the list of properties.
*
* @return array list of properties
*/
protected static function define_properties() {
return array(
'id' => array(
'type' => PARAM_INT,
'null' => NULL_ALLOWED,
'description' => 'Post/entry id.',
),
'module' => array(
'type' => PARAM_ALPHANUMEXT,
'null' => NULL_NOT_ALLOWED,
'description' => 'Where it was published the post (blog, blog_external...).',
),
'userid' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'Post author.',
),
'courseid' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'Course where the post was created.',
),
'groupid' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'Group post was created for.',
),
'moduleid' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'Module id where the post was created (not used anymore).',
),
'coursemoduleid' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'Course module id where the post was created.',
),
'subject' => array(
'type' => PARAM_TEXT,
'null' => NULL_NOT_ALLOWED,
'description' => 'Post subject.',
),
'summary' => array(
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
'description' => 'Post summary.',
),
'content' => array(
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
'description' => 'Post content.',
),
'uniquehash' => array(
'type' => PARAM_RAW,
'null' => NULL_NOT_ALLOWED,
'description' => 'Post unique hash.',
),
'rating' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'Post rating.',
),
'format' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'Post content format.',
),
'summaryformat' => array(
'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
'type' => PARAM_INT,
'default' => FORMAT_MOODLE,
'description' => 'Format for the summary field.',
),
'attachment' => array(
'type' => PARAM_RAW,
'null' => NULL_ALLOWED,
'description' => 'Post atachment.',
),
'publishstate' => array(
'type' => PARAM_ALPHA,
'null' => NULL_NOT_ALLOWED,
'default' => 'draft',
'description' => 'Post publish state.',
),
'lastmodified' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'When it was last modified.',
),
'created' => array(
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 0,
'description' => 'When it was created.',
),
'usermodified' => array(
'type' => PARAM_INT,
'null' => NULL_ALLOWED,
'description' => 'User that updated the post.',
),
);
}
protected static function define_related() {
return array(
'context' => 'context'
);
}
protected static function define_other_properties() {
return array(
'summaryfiles' => array(
'type' => external_files::get_properties_for_exporter(),
'multiple' => true
),
'attachmentfiles' => array(
'type' => external_files::get_properties_for_exporter(),
'multiple' => true,
'optional' => true
),
'tags' => array(
'type' => tag_item_exporter::read_properties_definition(),
'description' => 'Tags.',
'multiple' => true,
'optional' => true,
),
'canedit' => array(
'type' => PARAM_BOOL,
'description' => 'Whether the user can edit the post.',
'optional' => true,
),
);
}
protected function get_other_values(renderer_base $output) {
global $CFG;
require_once($CFG->dirroot . '/blog/lib.php');
$context = context_system::instance(); // Files always on site context.
$values['summaryfiles'] = external_util::get_area_files($context->id, 'blog', 'post', $this->data->id);
$values['attachmentfiles'] = external_util::get_area_files($context->id, 'blog', 'attachment', $this->data->id);
if ($this->data->module == 'blog_external') {
// For external blogs, the content field has the external blog id.
$values['tags'] = \core_tag\external\util::get_item_tags('core', 'blog_external', $this->data->content);
} else {
$values['tags'] = \core_tag\external\util::get_item_tags('core', 'post', $this->data->id);
}
$values['canedit'] = blog_user_can_edit_entry($this->data);
return $values;
}
}
+159
View File
@@ -0,0 +1,159 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
namespace core_blog\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use context_system;
use context_course;
use moodle_exception;
/**
* This is the external method for preparing a blog entry to be edited.
*
* @package core_blog
* @copyright 2024 Juan Leyva <juan@moodle.com>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class prepare_entry_for_edition extends external_api {
/**
* Describes the external function parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'entryid' => new external_value(PARAM_INT, 'The entry id to edit.'),
]
);
}
/**
* Prepare a draft area for editing a blog entry.
*
* @param int $entryid The entry id to edit.
* @throws moodle_exception;
* @return array draft area information
*/
public static function execute(int $entryid): array {
global $USER, $CFG;
require_once($CFG->dirroot . '/blog/lib.php');
require_once($CFG->dirroot . '/blog/locallib.php');
$params = self::validate_parameters(
self::execute_parameters(),
[
'entryid' => $entryid,
]
);
if (empty($CFG->enableblogs)) {
throw new moodle_exception('blogdisable', 'blog');
}
if (!$entry = new \blog_entry($params['entryid'])) {
throw new moodle_exception('wrongentryid', 'blog');
}
$courseid = !empty($entry->courseid) ? $entry->courseid : SITEID;
$context = context_course::instance($courseid);
$sitecontext = context_system::instance();
self::validate_context($context);
if (!blog_user_can_edit_entry($entry)) {
throw new \moodle_exception('cannoteditentryorblog', 'blog');
}
[$summaryoptions, $attachmentoptions] = blog_get_editor_options($entry);
$entry = file_prepare_standard_editor($entry, 'summary', $summaryoptions, $sitecontext, 'blog', 'post', $entry->id);
$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $sitecontext,
'blog', 'attachment', $entry->id);
// Just get a structure compatible with external API.
array_walk($attachmentoptions, function(&$item, $key) use(&$attachmentoptions) {
if (!is_scalar($item)) {
unset($attachmentoptions[$key]);
return;
}
$item = ['name' => $key, 'value' => $item];
});
array_walk($summaryoptions, function(&$item, $key) use(&$summaryoptions) {
if (!is_scalar($item)) {
unset($summaryoptions[$key]);
return;
}
$item = ['name' => $key, 'value' => $item];
});
$result = [
'inlineattachmentsid' => $entry->summary_editor['itemid'],
'attachmentsid' => $entry->attachment_filemanager,
'areas' => [
[
'area' => 'summary',
'options' => $summaryoptions,
],
[
'area' => 'attachment',
'options' => $attachmentoptions,
],
],
'warnings' => [],
];
return $result;
}
/**
* Return.
*
* @return external_single_structure
*/
public static function execute_returns() : external_single_structure {
return new external_single_structure(
[
'inlineattachmentsid' => new external_value(PARAM_INT, 'Draft item id for the text editor.'),
'attachmentsid' => new external_value(PARAM_INT, 'Draft item id for the file manager.'),
'areas' => new external_multiple_structure(
new external_single_structure(
[
'area' => new external_value(PARAM_ALPHA, 'File area name.'),
'options' => new external_multiple_structure(
new external_single_structure(
[
'name' => new external_value(PARAM_RAW, 'Name of option.'),
'value' => new external_value(PARAM_RAW, 'Value of option.'),
]
), 'Draft file area options.'
),
]
), 'File areas including options'
),
'warnings' => new external_warnings(),
]
);
}
}
+202
View File
@@ -0,0 +1,202 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_blog\external;
use core_external\external_api;
use core_external\external_format_value;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use context_system;
use context_course;
use context_module;
use moodle_exception;
/**
* This is the external method for updating a blog post entry.
*
* @package core_blog
* @copyright 2024 Juan Leyva <juan@moodle.com>
* @category external
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class update_entry extends external_api {
/**
* Parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'entryid' => new external_value(PARAM_INT, 'Blog entry id to update'),
'subject' => new external_value(PARAM_TEXT, 'Blog subject'),
'summary' => new external_value(PARAM_RAW, 'Blog post content'),
'summaryformat' => new external_format_value('summary'),
'options' => new external_multiple_structure (
new external_single_structure(
[
'name' => new external_value(PARAM_ALPHANUM,
'The allowed keys (value format) are:
inlineattachmentsid (int); the draft file area id for inline attachments. Default to 0.
attachmentsid (int); the draft file area id for attachments. Default to 0.
publishstate (str); the publish state of the entry (draft, site or public). Default to site.
courseassoc (int); the course id to associate the entry with. Default to 0.
modassoc (int); the module id to associate the entry with. Default to 0.
tags (str); the tags to associate the entry with, comma separated. Default to empty.'),
'value' => new external_value(PARAM_RAW, 'the value of the option (validated inside the function)'),
]
), 'Optional settings', VALUE_DEFAULT, []
),
]);
}
/**
* Update the indicated glossary entry.
*
* @param int $entryid The entry to update
* @param string $subject the glossary subject
* @param string $summary the subject summary
* @param int $summaryformat the subject summary format
* @param array $options additional settings
* @return array with result and warnings
* @throws moodle_exception
*/
public static function execute(int $entryid, string $subject, string $summary, int $summaryformat,
array $options = []): array {
global $DB, $CFG;
require_once($CFG->dirroot . '/blog/lib.php');
require_once($CFG->dirroot . '/blog/locallib.php');
$params = self::validate_parameters(self::execute_parameters(), compact('entryid', 'subject', 'summary',
'summaryformat', 'options'));
if (empty($CFG->enableblogs)) {
throw new moodle_exception('blogdisable', 'blog');
}
if (!$entry = new \blog_entry($params['entryid'])) {
throw new moodle_exception('wrongentryid', 'blog');
}
if (!blog_user_can_edit_entry($entry)) {
throw new \moodle_exception('cannoteditentryorblog', 'blog');
}
// Prepare the entry object.
$entrydata = new \stdClass();
$entrydata->id = $entry->id;
$entrydata->subject = $params['subject'];
$entrydata->summary_editor = [
'text' => $params['summary'],
'format' => $params['summaryformat'],
];
$entrydata->publishstate = $entry->publishstate;
$entrydata->courseassoc = $entry->courseassoc;
$entrydata->modassoc = $entry->modassoc;
$entrydata->tags = \core_tag_tag::get_item_tags_array('core', 'post', $entry->id);
// Options.
foreach ($params['options'] as $option) {
$name = trim($option['name']);
switch ($name) {
case 'inlineattachmentsid':
$entrydata->summary_editor['itemid'] = clean_param($option['value'], PARAM_INT);
break;
case 'attachmentsid':
$entrydata->attachment_filemanager = clean_param($option['value'], PARAM_INT);
break;
case 'publishstate':
$entrydata->publishstate = clean_param($option['value'], PARAM_ALPHA);
$applicable = \blog_entry::get_applicable_publish_states();
if (empty($applicable[$entrydata->publishstate])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
break;
case 'courseassoc':
case 'modassoc':
$entrydata->{$name} = clean_param($option['value'], PARAM_INT);
if (!$CFG->useblogassociations) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
break;
case 'tags':
$entrydata->tags = clean_param($option['value'], PARAM_TAGLIST);
// Convert to the expected format.
$entrydata->tags = explode(',', $entrydata->tags);
break;
default:
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
}
$context = context_system::instance();
// Validate course association. We need to convert the course id to context.
if (isset($entrydata->courseassoc)) {
$entrydata->courseid = $entrydata->courseassoc;
if (!empty($entrydata->courseid)) {
$coursecontext = context_course::instance($entrydata->courseassoc);
$entrydata->courseassoc = $coursecontext->id; // Convert to context.
$context = $coursecontext;
}
}
// Validate mod association.
if (isset($entrydata->modassoc)) {
$entrydata->coursemoduleid = $entrydata->modassoc;
if (!empty($entrydata->coursemoduleid)) {
$modcontext = context_module::instance($entrydata->modassoc);
if (!empty($coursecontext) && $coursecontext->id != $modcontext->get_course_context(true)->id) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'modassoc');
}
$entrydata->modassoc = $modcontext->id; // Convert to context.
$context = $modcontext;
}
}
// Validate context. It might be upated because of the new association.
self::validate_context($context);
[$summaryoptions, $attachmentoptions] = blog_get_editor_options($entrydata);
$entry->edit((array) $entrydata, null, $summaryoptions, $attachmentoptions);
return [
'status' => true,
'warnings' => [],
];
}
/**
* Return.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'status' => new external_value(PARAM_BOOL, 'The update result, true if everything went well.'),
'warnings' => new external_warnings(),
]);
}
}