Skip to content

Commit 1b7621f

Browse files
author
Laur0r
authored
Merge pull request #108 from learnweb/fix/subscription-cap
Fix/use allowforcesubscription capability
2 parents 3d6a349 + 3d1894c commit 1b7621f

13 files changed

+186
-97
lines changed

Diff for: amd/build/warnmodechange.min.js

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: amd/build/warnmodechange.min.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: amd/src/warnmodechange.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This file is part of Moodle - http://moodle.org/
2+
//
3+
// Moodle is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// Moodle is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15+
16+
/**
17+
* Warns on changing the subscription mode.
18+
*
19+
* @module mod_moodleoverflow/warnmodechange
20+
* @copyright 2022 Justus Dieckmann WWU
21+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22+
*/
23+
import {get_string as getString} from 'core/str';
24+
import Notification from 'core/notification';
25+
import Prefetch from 'core/prefetch';
26+
27+
/**
28+
* Init function.
29+
* @param {string} previousSetting
30+
*/
31+
export function init(previousSetting) {
32+
Prefetch.prefetchStrings('mod_moodleoverflow', ['switchtooptional', 'switchtoauto']);
33+
Prefetch.prefetchStrings('moodle', ['confirm', 'cancel']);
34+
const form = document.querySelector('form.mform');
35+
const select = document.getElementById('id_forcesubscribe');
36+
form.onsubmit = async(e) => {
37+
const value = select.selectedOptions[0].value;
38+
if (value == previousSetting || value == 1 || value == 3) {
39+
return;
40+
}
41+
e.preventDefault();
42+
await Notification.confirm(
43+
await getString('confirm'),
44+
await getString(value == 0 ? 'switchtooptional' : 'switchtoauto', 'mod_moodleoverflow'),
45+
await getString('confirm'),
46+
await getString('cancel'),
47+
() => {
48+
// Prevent this listener from preventing the event again.
49+
form.onsubmit = undefined;
50+
form.requestSubmit(e.submitter);
51+
}, undefined);
52+
};
53+
}

Diff for: classes/output/moodleoverflow_email.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ public function __set($name, $value) {
251251
public function get_unsubscribediscussionlink() {
252252

253253
// Check whether the moodleoverflow is subscribable.
254-
$subscribable = \mod_moodleoverflow\subscriptions::is_subscribable($this->moodleoverflow);
254+
$subscribable = \mod_moodleoverflow\subscriptions::is_subscribable($this->moodleoverflow,
255+
\context_module::instance($this->cm->id));
255256
if (!$subscribable) {
256257
return null;
257258
}
@@ -431,7 +432,8 @@ public function get_replylink() {
431432
* @return string
432433
*/
433434
public function get_unsubscribemoodleoverflowlink() {
434-
if (!\mod_moodleoverflow\subscriptions::is_subscribable($this->moodleoverflow)) {
435+
if (!\mod_moodleoverflow\subscriptions::is_subscribable($this->moodleoverflow,
436+
\context_module::instance($this->cm->id))) {
435437
return null;
436438
}
437439
$link = new \moodle_url(

Diff for: classes/subscriptions.php

+26-20
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ class subscriptions {
9898
*
9999
* @return bool
100100
*/
101-
public static function is_subscribed($userid, $moodleoverflow, $discussionid = null) {
101+
public static function is_subscribed($userid, $moodleoverflow, $context, $discussionid = null) {
102102

103103
// Is the user forced to be subscribed to the moodleoverflow?
104-
if (self::is_forcesubscribed($moodleoverflow)) {
104+
if (self::is_forcesubscribed($moodleoverflow) &&
105+
has_capability('mod/moodleoverflow:allowforcesubscribe', $context, $userid)) {
105106
return true;
106107
}
107108

@@ -354,10 +355,11 @@ public static function subscription_disabled($moodleoverflow) {
354355
* Checks wheter the specified moodleoverflow can be subscribed to.
355356
*
356357
* @param object $moodleoverflow The moodleoverflow ID
358+
* @param \context_module $context The module context.
357359
*
358360
* @return boolean
359361
*/
360-
public static function is_subscribable($moodleoverflow) {
362+
public static function is_subscribable($moodleoverflow, $context) {
361363

362364
// Check if the user is an authenticated user.
363365
$authenticated = (isloggedin() AND !isguestuser());
@@ -366,7 +368,8 @@ public static function is_subscribable($moodleoverflow) {
366368
$disabled = self::subscription_disabled($moodleoverflow);
367369

368370
// Check if the moodleoverflow forces the user to be subscribed.
369-
$forced = self::is_forcesubscribed($moodleoverflow);
371+
$forced = self::is_forcesubscribed($moodleoverflow) &&
372+
has_capability('mod/moodleoverflow:allowforcesubscribe', $context);
370373

371374
// Return the result.
372375
return ($authenticated AND !$forced AND !$disabled);
@@ -462,7 +465,7 @@ public static function get_potential_subscribers($context, $fields, $sort = '')
462465
global $DB;
463466

464467
// Only enrolled users can subscribe.
465-
list($esql, $params) = get_enrolled_sql($context);
468+
list($esql, $params) = get_enrolled_sql($context, 'mod/moodleoverflow:allowforcesubscribe');
466469

467470
// Default ordering of the list.
468471
if (!$sort) {
@@ -539,12 +542,8 @@ public static function get_subscribed_users($moodleoverflow, $context, $fields =
539542
u.picture, u.timezone, u.theme, u.lang, u.trackforums, u.mnethostid";
540543
}
541544

542-
// Check if the user is forced to e subscribed to a moodleoverflow.
543-
if (self::is_forcesubscribed($moodleoverflow)) {
544-
545-
// Find the list of potential subscribers.
546-
$results = self::get_potential_subscribers($context, $fields, 'u.email ASC');
547-
545+
if (self::subscription_disabled($moodleoverflow)) {
546+
$results = [];
548547
} else {
549548

550549
// Only enrolled users can subscribe to a moodleoverflow.
@@ -586,6 +585,12 @@ public static function get_subscribed_users($moodleoverflow, $context, $fields =
586585

587586
// Fetch the data.
588587
$results = $DB->get_records_sql($sql, $params);
588+
589+
if (self::is_forcesubscribed($moodleoverflow)) {
590+
foreach (self::get_potential_subscribers($context, $fields, 'u.email ASC') as $id => $user) {
591+
$results[$id] = $user;
592+
}
593+
}
589594
}
590595

591596
// Remove all guest users from the results. They should never be subscribed to a moodleoverflow.
@@ -645,7 +650,7 @@ public static function subscribe_user($userid, $moodleoverflow, $context, $userr
645650
global $DB;
646651

647652
// Check if the user is already subscribed.
648-
if (self::is_subscribed($userid, $moodleoverflow)) {
653+
if (self::is_subscribed($userid, $moodleoverflow, $context)) {
649654
return true;
650655
}
651656

@@ -912,7 +917,7 @@ public static function unsubscribe_user_from_discussion($userid, $discussion, $c
912917
* Generate and return the subscribe or unsubscribe link for a moodleoverflow.
913918
*
914919
* @param object $moodleoverflow the moodleoverflow. Fields used are $moodleoverflow->id and $moodleoverflow->forcesubscribe.
915-
* @param object $context the context object for this moodleoverflow.
920+
* @param \context $context the context object for this moodleoverflow.
916921
* @param array $messages text used for the link in its various states
917922
* (subscribed, unsubscribed, forcesubscribed or cantsubscribe).
918923
* Any strings not passed in are taken from the $defaultmessages array
@@ -935,7 +940,7 @@ public static function moodleoverflow_get_subscribe_link($moodleoverflow, $conte
935940
$messages = $messages + $defaultmessages;
936941

937942
// Check whether the user is forced to be subscribed to the moodleoverflow.
938-
$isforced = self::is_forcesubscribed($moodleoverflow);
943+
$isforced = self::is_forcesubscribed($moodleoverflow) && has_capability('mod/moodleoverflow:allowforcesubscribe', $context);
939944
$isdisabled = self::subscription_disabled($moodleoverflow);
940945

941946
// Return messages depending on the subscription state.
@@ -951,7 +956,7 @@ public static function moodleoverflow_get_subscribe_link($moodleoverflow, $conte
951956
}
952957

953958
// Check whether the user is subscribed.
954-
$issubscribed = self::is_subscribed($USER->id, $moodleoverflow);
959+
$issubscribed = self::is_subscribed($USER->id, $moodleoverflow, $context);
955960

956961
// Define the text of the link depending on the subscription state.
957962
if ($issubscribed) {
@@ -982,7 +987,7 @@ public static function moodleoverflow_get_subscribe_link($moodleoverflow, $conte
982987
* @param object $fromform The submitted form
983988
* @param \stdClass $moodleoverflow The moodleoverflow record
984989
* @param \stdClass $discussion The discussion record
985-
* @param \context_course $modulecontext The context of the module
990+
* @param \context_module $modulecontext The context of the module
986991
*
987992
* @return bool
988993
*/
@@ -994,15 +999,15 @@ public static function moodleoverflow_post_subscription($fromform, $moodleoverfl
994999
$disabled = self::subscription_disabled($moodleoverflow);
9951000

9961001
// Do not continue if the user is already forced to be subscribed.
997-
if ($force) {
1002+
if ($force && has_capability('mod/moodleoverflow:allowforcesubscribe', $modulecontext)) {
9981003
return false;
9991004
}
10001005

10011006
// Do not continue if subscriptions are disabled.
10021007
if ($disabled) {
10031008

10041009
// If the user is subscribed, unsubscribe him.
1005-
$subscribed = self::is_subscribed($USER->id, $moodleoverflow);
1010+
$subscribed = self::is_subscribed($USER->id, $moodleoverflow, $modulecontext);
10061011
$coursecontext = \context_course::instance($moodleoverflow->course);
10071012
$canmanage = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
10081013
if ($subscribed AND !$canmanage) {
@@ -1023,18 +1028,19 @@ public static function moodleoverflow_post_subscription($fromform, $moodleoverfl
10231028
* Return the markup for the discussion subscription toggling icon.
10241029
*
10251030
* @param object $moodleoverflow The forum moodleoverflow.
1031+
* @param \context $context
10261032
* @param int $discussionid The discussion to create an icon for.
10271033
*
10281034
* @return string The generated markup.
10291035
*/
1030-
public static function get_discussion_subscription_icon($moodleoverflow, $discussionid) {
1036+
public static function get_discussion_subscription_icon($moodleoverflow, $context, $discussionid) {
10311037
global $OUTPUT, $PAGE, $USER;
10321038

10331039
// Set the url to return to.
10341040
$returnurl = $PAGE->url->out();
10351041

10361042
// Check if the discussion is subscrived.
1037-
$status = self::is_subscribed($USER->id, $moodleoverflow, $discussionid);
1043+
$status = self::is_subscribed($USER->id, $moodleoverflow, $context, $discussionid);
10381044

10391045
// Create a link to subscribe or unsubscribe to the discussion.
10401046
$array = array(

Diff for: index.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@
139139
}
140140

141141
// Get information about the subscription state.
142-
$cansubscribe = \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow);
142+
$cansubscribe = \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext);
143143
$moodleoverflow->cansubscribe = $cansubscribe || has_capability('mod/moodleoverflow:managesubscriptions', $modulecontext);
144-
$moodleoverflow->issubscribed = \mod_moodleoverflow\subscriptions::is_subscribed($USER->id, $moodleoverflow, null);
144+
$moodleoverflow->issubscribed = \mod_moodleoverflow\subscriptions::is_subscribed($USER->id, $moodleoverflow, $modulecontext);
145145
$showsubscriptioncolumns = $showsubscriptioncolumns || $moodleoverflow->issubscribed || $moodleoverflow->cansubscribe;
146146

147147
// Add the moodleoverflow to the cache.
@@ -192,12 +192,13 @@
192192
}
193193

194194
// Check the subscription state.
195-
$forcesubscribed = \mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow);
195+
$forcesubscribed = \mod_moodleoverflow\subscriptions::is_forcesubscribed($moodleoverflow) &&
196+
has_capability('mod/moodleoverflow:allowforcesubscribe', $modulecontext);
196197
if (!$forcesubscribed) {
197198

198199
// Check the current state.
199-
$subscribed = \mod_moodleoverflow\subscriptions::is_subscribed($USER->id, $moodleoverflow, null);
200-
$subscribable = \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow);
200+
$subscribed = \mod_moodleoverflow\subscriptions::is_subscribed($USER->id, $moodleoverflow, $modulecontext);
201+
$subscribable = \mod_moodleoverflow\subscriptions::is_subscribable($moodleoverflow, $modulecontext);
201202

202203
// Check whether to subscribe or unsubscribe the user.
203204
if ($cap['manageactivities'] OR $subscribable AND $subscribe AND !$subscribed AND $cansub) {

Diff for: lang/en/moodleoverflow.php

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
$string['moodleoverflow:viewdiscussion'] = 'View discussion';
5454
$string['moodleoverflow:view'] = 'View discussionlist';
5555
$string['moodleoverflow:marksolved'] = 'Mark a post as solution';
56+
$string['moodleoverflow:reviewpost'] = 'Review (approve or reject) posts';
5657
$string['nowallsubscribed'] = 'All forums in {$a} are subscribed.';
5758
$string['nowallunsubscribed'] = 'All forums in {$a} are unsubscribed.';
5859

@@ -293,6 +294,8 @@
293294
$string['subscriptionforced'] = 'Forced subscription';
294295
$string['subscriptionauto'] = 'Auto subscription';
295296
$string['subscriptiondisabled'] = 'Subscription disabled';
297+
$string['switchtooptional'] = 'If you switch to the optional subscription, all currently subscribed users will be unsubscribed from this forum!';
298+
$string['switchtoauto'] = 'If you switch to the auto subscription, all enrolled users will be subscribed to this forum!';
296299
$string['trackingoff'] = 'Off';
297300
$string['trackingon'] = 'Forced';
298301
$string['trackingoptional'] = 'Optional';

0 commit comments

Comments
 (0)