Skip to content

Commit

Permalink
MDL-65978 blog: Preparatory API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Feb 29, 2024
1 parent e567c21 commit 98daf57
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 11 deletions.
6 changes: 6 additions & 0 deletions admin/tool/mobile/classes/api.php
Expand Up @@ -393,6 +393,12 @@ public static function get_config($section) {
$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();
Expand Down
5 changes: 5 additions & 0 deletions admin/tool/mobile/tests/externallib_test.php
Expand Up @@ -270,6 +270,10 @@ public function test_get_config(): void {
$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']);

Expand All @@ -282,6 +286,7 @@ public function test_get_config(): void {

$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']);

Expand Down
9 changes: 9 additions & 0 deletions blog/classes/external/post_exporter.php
Expand Up @@ -178,10 +178,18 @@ protected static function define_other_properties() {
'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);
Expand All @@ -192,6 +200,7 @@ protected function get_other_values(renderer_base $output) {
} 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;
}
Expand Down
14 changes: 5 additions & 9 deletions blog/edit.php
Expand Up @@ -54,9 +54,7 @@
$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;
Expand Down Expand Up @@ -125,7 +123,7 @@
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.
Expand Down Expand Up @@ -190,9 +188,7 @@
}
}

$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',
Expand Down Expand Up @@ -232,7 +228,7 @@

case 'edit':
if (empty($entry->id)) {
throw new \moodle_exception('wrongentryid', 'blog');
throw new \moodle_exception('wrongentryid');
}

$entry->edit($data, $blogeditform, $summaryoptions, $attachmentoptions);
Expand Down Expand Up @@ -272,7 +268,7 @@

case 'edit':
if (empty($entry->id)) {
throw new \moodle_exception('wrongentryid', 'blog');
throw new \moodle_exception('wrongentryid');
}
$strformheading = get_string('updateentrywithid', 'blog');

Expand Down
23 changes: 23 additions & 0 deletions blog/lib.php
Expand Up @@ -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];
}
3 changes: 2 additions & 1 deletion blog/locallib.php
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions blog/tests/external/external_test.php
Expand Up @@ -119,6 +119,7 @@ public function test_get_public_entries_global_level_by_non_logged_users() {
$this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);

$this->assertEquals($this->postid, $result['entries'][0]['id']);
$this->assertFalse($result['entries'][0]['canedit']);
}

/**
Expand Down Expand Up @@ -266,6 +267,7 @@ public function test_get_draft_entries_site_level_by_admin_users() {
$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']);
}

/**
Expand All @@ -283,6 +285,7 @@ public function test_get_draft_entries_user_level_by_author_users() {
$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']);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions 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.

Expand Down
1 change: 0 additions & 1 deletion lang/en/blog.php
Expand Up @@ -213,7 +213,6 @@
$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';

0 comments on commit 98daf57

Please sign in to comment.