From 5ea392de646db3396bf5f68c5618329bd1f68d58 Mon Sep 17 00:00:00 2001 From: meirzamoodle Date: Sat, 14 Oct 2023 16:00:10 +0700 Subject: [PATCH] MDL-78300 block: Determine if users can comment based on context --- blocks/comments/lib.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/blocks/comments/lib.php b/blocks/comments/lib.php index 454d1cde56d..66f418f32f0 100644 --- a/blocks/comments/lib.php +++ b/blocks/comments/lib.php @@ -59,7 +59,26 @@ function block_comments_comment_validate($comment_param) { * @return array */ function block_comments_comment_permissions($args) { - return array('post'=>true, 'view'=>true); + global $DB, $USER; + // By default, anyone can post and view comments. + $canpost = $canview = true; + // Check if it's the user context and not the owner's profile. + if ($args->context->contextlevel == CONTEXT_USER && $USER->id != $args->context->instanceid) { + // Check whether the context owner has a comment block in the user's profile. + $sqlparam = [ + 'blockname' => 'comments', + 'parentcontextid' => $args->context->id, + 'pagetypepattern' => 'user-profile', + ]; + // If the comment block is not present at the target user's profile, + // then the logged-in user cannot post or view comments. + $canpost = $canview = $DB->record_exists_select( + 'block_instances', + 'blockname = :blockname AND parentcontextid = :parentcontextid AND pagetypepattern = :pagetypepattern', + $sqlparam, + ); + } + return ['post' => $canpost, 'view' => $canview]; } /**