MDL-65660 mod_forum: Allow guest users to view post buttons and links
For a self enrolment enabled course allow guest users to see the add discussion and reply buttons
This commit is contained in:
@@ -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' => [
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,18 @@
|
||||
</div>
|
||||
</div>
|
||||
{{/forum.capabilities.create}}
|
||||
{{^forum.capabilities.create}}
|
||||
{{#forum.capabilities.selfenrol}}
|
||||
<div class="p-t-1 p-b-1">
|
||||
<a class="btn btn-primary" href="{{forum.urls.create}}">
|
||||
{{$discussion_create_text}}
|
||||
{{#str}}addanewdiscussion, forum{{/str}}
|
||||
{{/discussion_create_text}}
|
||||
</a>
|
||||
</div>
|
||||
{{/forum.capabilities.selfenrol}}
|
||||
{{/forum.capabilities.create}}
|
||||
|
||||
|
||||
{{#state.hasdiscussions}}
|
||||
{{$discussion_top_pagination}}
|
||||
|
||||
@@ -258,6 +258,21 @@
|
||||
</a>
|
||||
{{/replyoutput}}
|
||||
{{/reply}}
|
||||
{{^reply}}
|
||||
{{#selfenrol}}
|
||||
{{$replyoutput}}
|
||||
<a
|
||||
href="{{{urls.reply}}}"
|
||||
class="btn btn-link"
|
||||
data-post-id="{{id}}"
|
||||
data-can-reply-privately="{{canreplyprivately}}"
|
||||
title="{{#str}} reply, mod_forum {{/str}}"
|
||||
>
|
||||
{{#str}} reply, mod_forum {{/str}}
|
||||
</a>
|
||||
{{/replyoutput}}
|
||||
{{/selfenrol}}
|
||||
{{/reply}}
|
||||
{{#export}}
|
||||
<a
|
||||
data-region="post-action"
|
||||
|
||||
@@ -104,6 +104,7 @@ class mod_forum_exporters_post_testcase extends advanced_testcase {
|
||||
$canexport = true;
|
||||
$cancontrolreadstatus = true;
|
||||
$canreplyprivately = true;
|
||||
$canenrol = true;
|
||||
$capabilitymanager = new test_capability_manager(
|
||||
$canview,
|
||||
$canedit,
|
||||
@@ -112,7 +113,8 @@ class mod_forum_exporters_post_testcase extends advanced_testcase {
|
||||
$canreply,
|
||||
$canexport,
|
||||
$cancontrolreadstatus,
|
||||
$canreplyprivately
|
||||
$canreplyprivately,
|
||||
$canenrol
|
||||
);
|
||||
$managerfactory = \mod_forum\local\container::get_manager_factory();
|
||||
$entityfactory = \mod_forum\local\container::get_entity_factory();
|
||||
@@ -157,6 +159,7 @@ class mod_forum_exporters_post_testcase extends advanced_testcase {
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user