diff --git a/admin/tool/mobile/classes/api.php b/admin/tool/mobile/classes/api.php index b12a32abbe8..c7edfc3ec89 100644 --- a/admin/tool/mobile/classes/api.php +++ b/admin/tool/mobile/classes/api.php @@ -393,6 +393,12 @@ class api { $settings->tool_dataprivacy_showdataretentionsummary = get_config('tool_dataprivacy', 'showdataretentionsummary'); } + if (empty($section) || $section === 'blog') { + $settings->useblogassociations = $CFG->useblogassociations; + $settings->bloglevel = $CFG->bloglevel; + $settings->blogusecomments = $CFG->blogusecomments; + } + if (empty($section) || $section === 'h5psettings') { \core_h5p\local\library\autoloader::register(); $customcss = \core_h5p\file_storage::get_custom_styles(); diff --git a/admin/tool/mobile/tests/externallib_test.php b/admin/tool/mobile/tests/externallib_test.php index c93dd4a9e1b..a0ce7027c3f 100644 --- a/admin/tool/mobile/tests/externallib_test.php +++ b/admin/tool/mobile/tests/externallib_test.php @@ -270,6 +270,10 @@ class externallib_test extends externallib_advanced_testcase { $expected[] = ['name' => 'tool_dataprivacy_contactdataprotectionofficer', 'value' => get_config('tool_dataprivacy', 'contactdataprotectionofficer')]; $expected[] = ['name' => 'tool_dataprivacy_showdataretentionsummary', 'value' => get_config('tool_dataprivacy', 'showdataretentionsummary')]; + $expected[] = ['name' => 'useblogassociations', 'value' => $CFG->useblogassociations]; + $expected[] = ['name' => 'bloglevel', 'value' => $CFG->bloglevel]; + $expected[] = ['name' => 'blogusecomments', 'value' => $CFG->blogusecomments]; + $this->assertCount(0, $result['warnings']); $this->assertEquals($expected, $result['settings']); @@ -282,6 +286,7 @@ class externallib_test extends externallib_advanced_testcase { $customcss = \core_h5p\file_storage::get_custom_styles(); $expected[] = ['name' => 'h5pcustomcssurl', 'value' => $customcss['cssurl']->out() . '?ver=' . $customcss['cssversion']]; + $this->assertCount(0, $result['warnings']); $this->assertEquals($expected, $result['settings']); diff --git a/blog/classes/external/post_exporter.php b/blog/classes/external/post_exporter.php index fcc97b88d2b..a8616a7cc28 100644 --- a/blog/classes/external/post_exporter.php +++ b/blog/classes/external/post_exporter.php @@ -178,10 +178,18 @@ class post_exporter extends exporter { '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); @@ -192,6 +200,7 @@ class post_exporter extends exporter { } 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; } diff --git a/blog/edit.php b/blog/edit.php index 2c77cbb998d..581abca02b3 100644 --- a/blog/edit.php +++ b/blog/edit.php @@ -54,9 +54,7 @@ $entry = new stdClass(); $entry->id = null; if ($id) { - if (!$entry = new blog_entry($id)) { - throw new \moodle_exception('wrongentryid', 'blog'); - } + $entry = new blog_entry($id); // Will trigger exception if not found. $userid = $entry->userid; } else { $userid = $USER->id; @@ -125,7 +123,7 @@ if ($action === 'delete') { comment::init(); if (empty($entry->id)) { - throw new \moodle_exception('wrongentryid', 'blog'); + throw new \moodle_exception('wrongentryid'); } if (data_submitted() && $confirm && confirm_sesskey()) { // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability. @@ -190,9 +188,7 @@ if (!empty($entry->id)) { } } -$summaryoptions = array('maxfiles' => 99, 'maxbytes' => $CFG->maxbytes, 'trusttext' => true, 'context' => $sitecontext, - 'subdirs' => file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id)); -$attachmentoptions = array('subdirs' => false, 'maxfiles' => 99, 'maxbytes' => $CFG->maxbytes); +[$summaryoptions, $attachmentoptions] = blog_get_editor_options($entry); $blogeditform = new blog_edit_form(null, compact('entry', 'summaryoptions', @@ -232,7 +228,7 @@ if ($blogeditform->is_cancelled()) { case 'edit': if (empty($entry->id)) { - throw new \moodle_exception('wrongentryid', 'blog'); + throw new \moodle_exception('wrongentryid'); } $entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions); @@ -272,7 +268,7 @@ switch ($action) { case 'edit': if (empty($entry->id)) { - throw new \moodle_exception('wrongentryid', 'blog'); + throw new \moodle_exception('wrongentryid'); } $strformheading = get_string('updateentrywithid', 'blog'); diff --git a/blog/lib.php b/blog/lib.php index 0d4bb4ed8bf..b98ac2e4766 100644 --- a/blog/lib.php +++ b/blog/lib.php @@ -1306,3 +1306,26 @@ function blog_validate_access($courseid, $modid, $groupid, $entryid, $userid) { } return array($courseid, $userid); } + +/** + * Get blog editor and attachment options for when creating or updating an entry + * + * @param mixed $entry The entry object (can be null) + * @return array editor and attachment options + */ +function blog_get_editor_options(mixed $entry = null): array { + global $CFG; + + if (is_null($entry)) { + $entry = new stdClass(); + $entry->id = null; + } + + $sitecontext = context_system::instance(); + + $summaryoptions = ['maxfiles' => 99, 'maxbytes' => $CFG->maxbytes, 'trusttext' => true, 'context' => $sitecontext, + 'subdirs' => file_area_contains_subdirs($sitecontext, 'blog', 'post', $entry->id)]; + $attachmentoptions = ['subdirs' => false, 'maxfiles' => 99, 'maxbytes' => $CFG->maxbytes]; + + return [$summaryoptions, $attachmentoptions]; +} diff --git a/blog/locallib.php b/blog/locallib.php index 979b17ceeed..d6ad0880f48 100644 --- a/blog/locallib.php +++ b/blog/locallib.php @@ -135,12 +135,13 @@ class blog_entry implements renderable { * Constructor. If given an id, will fetch the corresponding record from the DB. * * @param mixed $idorparams A blog entry id if INT, or data for a new entry if array + * @throws moodle_exception */ public function __construct($id=null, $params=null, $form=null) { global $DB, $PAGE, $CFG; if (!empty($id)) { - $object = $DB->get_record('post', array('id' => $id)); + $object = $DB->get_record('post', array('id' => $id), '*', MUST_EXIST); foreach ($object as $var => $val) { $this->$var = $val; } diff --git a/blog/tests/external/external_test.php b/blog/tests/external/external_test.php index c1dc8f13b46..04feb2e44a4 100644 --- a/blog/tests/external/external_test.php +++ b/blog/tests/external/external_test.php @@ -119,6 +119,7 @@ class external_test extends \advanced_testcase { $this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']); $this->assertEquals($this->postid, $result['entries'][0]['id']); + $this->assertFalse($result['entries'][0]['canedit']); } /** @@ -266,6 +267,7 @@ class external_test extends \advanced_testcase { $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result); $this->assertCount(1, $result['entries']); $this->assertEquals($this->postid, $result['entries'][0]['id']); + $this->assertTrue($result['entries'][0]['canedit']); } /** @@ -283,6 +285,7 @@ class external_test extends \advanced_testcase { $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result); $this->assertCount(1, $result['entries']); $this->assertEquals($this->postid, $result['entries'][0]['id']); + $this->assertTrue($result['entries'][0]['canedit']); } /** diff --git a/blog/upgrade.txt b/blog/upgrade.txt index 644ea08ae2a..af73a733f2f 100644 --- a/blog/upgrade.txt +++ b/blog/upgrade.txt @@ -1,6 +1,9 @@ This files describes API changes in /blog/* , information provided here is intended especially for developers. +=== 4.4 === + * The blog_entry class constructor now throws an exception if the indicated entry id does not exist. + === 3.7 === * External function get_entries now returns an additional field "tags" returning the post tags. diff --git a/lang/en/blog.php b/lang/en/blog.php index c2aa9f0db79..e4b2594bc2e 100644 --- a/lang/en/blog.php +++ b/lang/en/blog.php @@ -213,7 +213,6 @@ $string['viewsiteentries'] = 'View all entries'; $string['viewuserentries'] = 'View all entries by {$a}'; $string['worldblogs'] = 'The world can read entries set to be world-accessible'; $string['wrongexternalid'] = 'Wrong external blog ID'; -$string['wrongpostid'] = 'Wrong blog post id'; $string['page-blog-edit'] = 'Blog editing pages'; $string['page-blog-index'] = 'Blog listing pages'; $string['page-blog-x'] = 'All blog pages';