diff --git a/mod/forum/classes/local/exporters/forum.php b/mod/forum/classes/local/exporters/forum.php index 40d4dd66045..ad396159228 100644 --- a/mod/forum/classes/local/exporters/forum.php +++ b/mod/forum/classes/local/exporters/forum.php @@ -119,6 +119,7 @@ class forum extends exporter { 'capabilities' => [ 'viewdiscussions' => $capabilitymanager->can_view_discussions($user), 'create' => $capabilitymanager->can_create_discussions($user, $currentgroup), + 'selfenrol' => $capabilitymanager->can_self_enrol($user), 'subscribe' => $capabilitymanager->can_subscribe_to_forum($user), ], 'urls' => [ diff --git a/mod/forum/classes/local/exporters/post.php b/mod/forum/classes/local/exporters/post.php index 4528c602d8e..f0f8d52a4c6 100644 --- a/mod/forum/classes/local/exporters/post.php +++ b/mod/forum/classes/local/exporters/post.php @@ -146,6 +146,11 @@ class post extends exporter { 'null' => NULL_ALLOWED, 'description' => 'Whether the user can reply to the post', ], + 'selfenrol' => [ + 'type' => PARAM_BOOL, + 'null' => NULL_ALLOWED, + 'description' => 'Whether the user can self enrol into the course', + ], 'export' => [ 'type' => PARAM_BOOL, 'null' => NULL_ALLOWED, @@ -360,6 +365,7 @@ class post extends exporter { $canreply = $capabilitymanager->can_reply_to_post($user, $discussion, $post); $canexport = $capabilitymanager->can_export_post($user, $post); $cancontrolreadstatus = $capabilitymanager->can_manually_control_post_read_status($user); + $canselfenrol = $capabilitymanager->can_self_enrol($user); $canreplyprivately = $capabilitymanager->can_reply_privately_to_post($user, $post); $urlfactory = $this->related['urlfactory']; @@ -369,7 +375,7 @@ class post extends exporter { $editurl = $canedit ? $urlfactory->get_edit_post_url_from_post($forum, $post) : null; $deleteurl = $candelete ? $urlfactory->get_delete_post_url_from_post($post) : null; $spliturl = $cansplit ? $urlfactory->get_split_discussion_at_post_url_from_post($post) : null; - $replyurl = $canreply ? $urlfactory->get_reply_to_post_url_from_post($post) : null; + $replyurl = $canreply || $canselfenrol ? $urlfactory->get_reply_to_post_url_from_post($post) : null; $exporturl = $canexport ? $urlfactory->get_export_post_url_from_post($post) : null; $markasreadurl = $cancontrolreadstatus ? $urlfactory->get_mark_post_as_read_url_from_post($post) : null; $markasunreadurl = $cancontrolreadstatus ? $urlfactory->get_mark_post_as_unread_url_from_post($post) : null; @@ -424,7 +430,8 @@ class post extends exporter { 'reply' => $canreply, 'export' => $canexport, 'controlreadstatus' => $cancontrolreadstatus, - 'canreplyprivately' => $canreplyprivately + 'canreplyprivately' => $canreplyprivately, + 'selfenrol' => $canselfenrol ], 'urls' => [ 'view' => $viewurl ? $viewurl->out(false) : null, diff --git a/mod/forum/classes/local/managers/capability.php b/mod/forum/classes/local/managers/capability.php index 76ba7c127dc..ece049e94f0 100644 --- a/mod/forum/classes/local/managers/capability.php +++ b/mod/forum/classes/local/managers/capability.php @@ -607,4 +607,30 @@ class capability { public function can_manage_tags(stdClass $user) : bool { return has_capability('moodle/tag:manage', context_system::instance(), $user); } + + /** + * Checks whether the user can self enrol into the course. + * Mimics the checks on the add button in deprecatedlib/forum_print_latest_discussions + * + * @param stdClass $user + * @return bool + */ + public function can_self_enrol(stdClass $user) : bool { + $canstart = false; + + if ($this->forum->get_type() != 'news') { + if (isguestuser($user) or !isloggedin()) { + $canstart = true; + } + + if (!is_enrolled($this->context) and !is_viewing($this->context)) { + // Allow guests and not-logged-in to see the button - they are prompted to log in after clicking the link, + // Normal users with temporary guest access see this button too, they are asked to enrol instead, + // Do not show the button to users with suspended enrolments here. + $canstart = enrol_selfenrol_available($this->forum->get_course_id()); + } + } + + return $canstart; + } } diff --git a/mod/forum/templates/discussion_list.mustache b/mod/forum/templates/discussion_list.mustache index 60e3704d270..1224277132a 100644 --- a/mod/forum/templates/discussion_list.mustache +++ b/mod/forum/templates/discussion_list.mustache @@ -51,6 +51,18 @@ {{/forum.capabilities.create}} + {{^forum.capabilities.create}} + {{#forum.capabilities.selfenrol}} +
+ + {{$discussion_create_text}} + {{#str}}addanewdiscussion, forum{{/str}} + {{/discussion_create_text}} + +
+ {{/forum.capabilities.selfenrol}} + {{/forum.capabilities.create}} + {{#state.hasdiscussions}} {{$discussion_top_pagination}} diff --git a/mod/forum/templates/forum_discussion_post.mustache b/mod/forum/templates/forum_discussion_post.mustache index e5a8e1d1c48..564d1b55287 100644 --- a/mod/forum/templates/forum_discussion_post.mustache +++ b/mod/forum/templates/forum_discussion_post.mustache @@ -258,6 +258,21 @@ {{/replyoutput}} {{/reply}} + {{^reply}} + {{#selfenrol}} + {{$replyoutput}} + + {{#str}} reply, mod_forum {{/str}} + + {{/replyoutput}} + {{/selfenrol}} + {{/reply}} {{#export}} assertEquals($cansplit, $exportedpost->capabilities['split']); $this->assertEquals($canreply, $exportedpost->capabilities['reply']); $this->assertEquals($canexport, $exportedpost->capabilities['export']); + $this->assertEquals($canenrol, $exportedpost->capabilities['selfenrol']); $this->assertEquals($cancontrolreadstatus, $exportedpost->capabilities['controlreadstatus']); $this->assertNotEmpty($exportedpost->urls['view']); $this->assertNotEmpty($exportedpost->urls['viewisolated']); @@ -416,6 +419,8 @@ class test_capability_manager extends capability_manager { private $controlreadstatus; /** @var bool $controlreadstatus Value for can_reply_privately_to_post */ private $canreplyprivatelytopost; + /** @var bool $canenrol Value for can_self_enrol */ + private $canenrol; /** * Constructor. @@ -436,7 +441,8 @@ class test_capability_manager extends capability_manager { bool $reply = true, bool $export = true, bool $controlreadstatus = true, - bool $canreplyprivatelytopost = true + bool $canreplyprivatelytopost = true, + bool $canenrol = true ) { $this->view = $view; $this->edit = $edit; @@ -446,6 +452,7 @@ class test_capability_manager extends capability_manager { $this->export = $export; $this->controlreadstatus = $controlreadstatus; $this->canreplyprivatelytopost = $canreplyprivatelytopost; + $this->canenrol = $canenrol; } /** @@ -538,4 +545,13 @@ class test_capability_manager extends capability_manager { public function can_reply_privately_to_post(stdClass $user, post_entity $post) : bool { return $this->canreplyprivatelytopost; } + + /** + * Override can_self_enrol + * @param stdClass $user + * @return bool + */ + public function can_self_enrol(stdClass $user) : bool { + return $this->canenrol; + } } diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 5e903acbee5..390cfbba553 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -692,7 +692,8 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'reply' => 1, 'export' => 0, 'controlreadstatus' => 0, - 'canreplyprivately' => 0 + 'canreplyprivately' => 0, + 'selfenrol' => 0 ], 'urls' => [ 'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply2->discussion, $discussion1reply2->id), @@ -745,7 +746,8 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'reply' => 1, 'export' => 0, 'controlreadstatus' => 0, - 'canreplyprivately' => 0 + 'canreplyprivately' => 0, + 'selfenrol' => 0 ], 'urls' => [ 'view' => $urlfactory->get_view_post_url_from_post_id($discussion1reply1->discussion, $discussion1reply1->id),