@@ -98,10 +98,11 @@ class subscriptions {
98
98
*
99
99
* @return bool
100
100
*/
101
- public static function is_subscribed ($ userid , $ moodleoverflow , $ discussionid = null ) {
101
+ public static function is_subscribed ($ userid , $ moodleoverflow , $ context , $ discussionid = null ) {
102
102
103
103
// 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 )) {
105
106
return true ;
106
107
}
107
108
@@ -354,10 +355,11 @@ public static function subscription_disabled($moodleoverflow) {
354
355
* Checks wheter the specified moodleoverflow can be subscribed to.
355
356
*
356
357
* @param object $moodleoverflow The moodleoverflow ID
358
+ * @param \context_module $context The module context.
357
359
*
358
360
* @return boolean
359
361
*/
360
- public static function is_subscribable ($ moodleoverflow ) {
362
+ public static function is_subscribable ($ moodleoverflow, $ context ) {
361
363
362
364
// Check if the user is an authenticated user.
363
365
$ authenticated = (isloggedin () AND !isguestuser ());
@@ -366,7 +368,8 @@ public static function is_subscribable($moodleoverflow) {
366
368
$ disabled = self ::subscription_disabled ($ moodleoverflow );
367
369
368
370
// 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 );
370
373
371
374
// Return the result.
372
375
return ($ authenticated AND !$ forced AND !$ disabled );
@@ -462,7 +465,7 @@ public static function get_potential_subscribers($context, $fields, $sort = '')
462
465
global $ DB ;
463
466
464
467
// Only enrolled users can subscribe.
465
- list ($ esql , $ params ) = get_enrolled_sql ($ context );
468
+ list ($ esql , $ params ) = get_enrolled_sql ($ context, ' mod/moodleoverflow:allowforcesubscribe ' );
466
469
467
470
// Default ordering of the list.
468
471
if (!$ sort ) {
@@ -539,12 +542,8 @@ public static function get_subscribed_users($moodleoverflow, $context, $fields =
539
542
u.picture, u.timezone, u.theme, u.lang, u.trackforums, u.mnethostid " ;
540
543
}
541
544
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 = [];
548
547
} else {
549
548
550
549
// Only enrolled users can subscribe to a moodleoverflow.
@@ -586,6 +585,12 @@ public static function get_subscribed_users($moodleoverflow, $context, $fields =
586
585
587
586
// Fetch the data.
588
587
$ 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
+ }
589
594
}
590
595
591
596
// 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
645
650
global $ DB ;
646
651
647
652
// Check if the user is already subscribed.
648
- if (self ::is_subscribed ($ userid , $ moodleoverflow )) {
653
+ if (self ::is_subscribed ($ userid , $ moodleoverflow, $ context )) {
649
654
return true ;
650
655
}
651
656
@@ -912,7 +917,7 @@ public static function unsubscribe_user_from_discussion($userid, $discussion, $c
912
917
* Generate and return the subscribe or unsubscribe link for a moodleoverflow.
913
918
*
914
919
* @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.
916
921
* @param array $messages text used for the link in its various states
917
922
* (subscribed, unsubscribed, forcesubscribed or cantsubscribe).
918
923
* Any strings not passed in are taken from the $defaultmessages array
@@ -935,7 +940,7 @@ public static function moodleoverflow_get_subscribe_link($moodleoverflow, $conte
935
940
$ messages = $ messages + $ defaultmessages ;
936
941
937
942
// 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 ) ;
939
944
$ isdisabled = self ::subscription_disabled ($ moodleoverflow );
940
945
941
946
// Return messages depending on the subscription state.
@@ -951,7 +956,7 @@ public static function moodleoverflow_get_subscribe_link($moodleoverflow, $conte
951
956
}
952
957
953
958
// Check whether the user is subscribed.
954
- $ issubscribed = self ::is_subscribed ($ USER ->id , $ moodleoverflow );
959
+ $ issubscribed = self ::is_subscribed ($ USER ->id , $ moodleoverflow, $ context );
955
960
956
961
// Define the text of the link depending on the subscription state.
957
962
if ($ issubscribed ) {
@@ -982,7 +987,7 @@ public static function moodleoverflow_get_subscribe_link($moodleoverflow, $conte
982
987
* @param object $fromform The submitted form
983
988
* @param \stdClass $moodleoverflow The moodleoverflow record
984
989
* @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
986
991
*
987
992
* @return bool
988
993
*/
@@ -994,15 +999,15 @@ public static function moodleoverflow_post_subscription($fromform, $moodleoverfl
994
999
$ disabled = self ::subscription_disabled ($ moodleoverflow );
995
1000
996
1001
// Do not continue if the user is already forced to be subscribed.
997
- if ($ force ) {
1002
+ if ($ force && has_capability ( ' mod/moodleoverflow:allowforcesubscribe ' , $ modulecontext ) ) {
998
1003
return false ;
999
1004
}
1000
1005
1001
1006
// Do not continue if subscriptions are disabled.
1002
1007
if ($ disabled ) {
1003
1008
1004
1009
// If the user is subscribed, unsubscribe him.
1005
- $ subscribed = self ::is_subscribed ($ USER ->id , $ moodleoverflow );
1010
+ $ subscribed = self ::is_subscribed ($ USER ->id , $ moodleoverflow, $ modulecontext );
1006
1011
$ coursecontext = \context_course::instance ($ moodleoverflow ->course );
1007
1012
$ canmanage = has_capability ('moodle/course:manageactivities ' , $ coursecontext , $ USER ->id );
1008
1013
if ($ subscribed AND !$ canmanage ) {
@@ -1023,18 +1028,19 @@ public static function moodleoverflow_post_subscription($fromform, $moodleoverfl
1023
1028
* Return the markup for the discussion subscription toggling icon.
1024
1029
*
1025
1030
* @param object $moodleoverflow The forum moodleoverflow.
1031
+ * @param \context $context
1026
1032
* @param int $discussionid The discussion to create an icon for.
1027
1033
*
1028
1034
* @return string The generated markup.
1029
1035
*/
1030
- public static function get_discussion_subscription_icon ($ moodleoverflow , $ discussionid ) {
1036
+ public static function get_discussion_subscription_icon ($ moodleoverflow , $ context , $ discussionid ) {
1031
1037
global $ OUTPUT , $ PAGE , $ USER ;
1032
1038
1033
1039
// Set the url to return to.
1034
1040
$ returnurl = $ PAGE ->url ->out ();
1035
1041
1036
1042
// 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 );
1038
1044
1039
1045
// Create a link to subscribe or unsubscribe to the discussion.
1040
1046
$ array = array (
0 commit comments