@@ -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 (
0 commit comments