Skip to content

Commit

Permalink
Merge branch 'MDL-65660-master' of git://github.com/peterRd/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jun 3, 2019
2 parents 944df31 + 92ba55e commit cbf768b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
1 change: 1 addition & 0 deletions mod/forum/classes/local/exporters/forum.php
Expand Up @@ -119,6 +119,7 @@ protected function get_other_values(renderer_base $output) {
'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' => [
Expand Down
11 changes: 9 additions & 2 deletions mod/forum/classes/local/exporters/post.php
Expand Up @@ -147,6 +147,11 @@ protected static function define_other_properties() {
'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,
Expand Down Expand Up @@ -361,6 +366,7 @@ protected function get_other_values(renderer_base $output) {
$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'];
Expand All @@ -370,7 +376,7 @@ protected function get_other_values(renderer_base $output) {
$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;
Expand Down Expand Up @@ -432,7 +438,8 @@ protected function get_other_values(renderer_base $output) {
'reply' => $canreply,
'export' => $canexport,
'controlreadstatus' => $cancontrolreadstatus,
'canreplyprivately' => $canreplyprivately
'canreplyprivately' => $canreplyprivately,
'selfenrol' => $canselfenrol
],
'urls' => [
'view' => $viewurl ? $viewurl->out(false) : null,
Expand Down
26 changes: 26 additions & 0 deletions mod/forum/classes/local/managers/capability.php
Expand Up @@ -607,4 +607,30 @@ public function can_manage_forum(stdClass $user) {
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;
}
}
12 changes: 12 additions & 0 deletions mod/forum/templates/discussion_list.mustache
Expand Up @@ -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}}
Expand Down
15 changes: 15 additions & 0 deletions mod/forum/templates/forum_discussion_post.mustache
Expand Up @@ -277,6 +277,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"
Expand Down
20 changes: 18 additions & 2 deletions mod/forum/tests/exporters_post_test.php
Expand Up @@ -104,6 +104,7 @@ public function test_export_post() {
$canexport = true;
$cancontrolreadstatus = true;
$canreplyprivately = true;
$canenrol = true;
$capabilitymanager = new test_capability_manager(
$canview,
$canedit,
Expand All @@ -112,7 +113,8 @@ public function test_export_post() {
$canreply,
$canexport,
$cancontrolreadstatus,
$canreplyprivately
$canreplyprivately,
$canenrol
);
$managerfactory = \mod_forum\local\container::get_manager_factory();
$entityfactory = \mod_forum\local\container::get_entity_factory();
Expand Down Expand Up @@ -157,6 +159,7 @@ public function test_export_post() {
$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']);
Expand Down Expand Up @@ -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.
Expand All @@ -436,7 +441,8 @@ public function __construct(
bool $reply = true,
bool $export = true,
bool $controlreadstatus = true,
bool $canreplyprivatelytopost = true
bool $canreplyprivatelytopost = true,
bool $canenrol = true
) {
$this->view = $view;
$this->edit = $edit;
Expand All @@ -446,6 +452,7 @@ public function __construct(
$this->export = $export;
$this->controlreadstatus = $controlreadstatus;
$this->canreplyprivatelytopost = $canreplyprivatelytopost;
$this->canenrol = $canenrol;
}

/**
Expand Down Expand Up @@ -538,4 +545,13 @@ public function can_manually_control_post_read_status(stdClass $user) : bool {
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;
}
}
6 changes: 4 additions & 2 deletions mod/forum/tests/externallib_test.php
Expand Up @@ -693,7 +693,8 @@ public function test_mod_forum_get_discussion_posts() {
'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),
Expand Down Expand Up @@ -747,7 +748,8 @@ public function test_mod_forum_get_discussion_posts() {
'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),
Expand Down

0 comments on commit cbf768b

Please sign in to comment.