Skip to content

Commit 82c2f38

Browse files
committed
Add subscription toggle to overview
User can now see if they are subscribed to a moodleoverflow and change the subscription mode via Ajax directly
1 parent edb5be1 commit 82c2f38

File tree

8 files changed

+160
-6
lines changed

8 files changed

+160
-6
lines changed

amd/build/toggle_subscription.min.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/toggle_subscription.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/toggle_subscription.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
* JavaScript to change the subscription status of a user.
18+
* This module is used in the activity overview page where a user can change the subscription status of a moodleoverflow with
19+
* a toggle button.
20+
* @module mod_moodleoverflow/toggle_subscription
21+
* @copyright 2026 Tamaro Walter
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
import Ajax from 'core/ajax';
26+
27+
28+
/**
29+
* Init function
30+
* @param {string} itemid
31+
*/
32+
export function init(itemid) {
33+
// Get the right subscription toggle element.
34+
const element = document.getElementById(itemid);
35+
element.addEventListener('change', function() {
36+
const userid = parseInt(element.dataset.userid);
37+
const cmid = parseInt(element.dataset.cmid);
38+
const subscribed = element.dataset.subscribed === 'true';
39+
const data = {
40+
methodname: 'mod_moodleoverflow_change_subscription_mode',
41+
args: {
42+
userid: userid,
43+
subscribed: subscribed,
44+
cmid: cmid
45+
},
46+
};
47+
element.dataset.subscribed = Boolean(!subscribed);
48+
// Call the AJAX function.
49+
return Ajax.call([data]);
50+
}
51+
);
52+
}

classes/courseformat/overview.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
use cm_info;
21+
use context_module;
2122
use core\output\action_link;
2223
use core\output\local\properties\button;
2324
use core\output\local\properties\text_align;
@@ -26,6 +27,7 @@
2627
use core_courseformat\activityoverviewbase;
2728
use core_courseformat\local\overview\overviewitem;
2829
use mod_moodleoverflow\readtracking;
30+
use mod_moodleoverflow\subscriptions;
2931

3032
/**
3133
* Checklist overview integration (for Moodle 5.0+)
@@ -104,7 +106,43 @@ private function get_extra_unread_posts_overview(): ?overviewitem {
104106
* @return overviewitem|null
105107
*/
106108
private function get_extra_subscriptions_overview(): ?overviewitem {
107-
return null;
109+
global $USER, $DB, $PAGE;
110+
// Get important objects.
111+
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $this->cm->instance], '*', MUST_EXIST);
112+
$modulecontext = context_module::instance($this->cm->id);
113+
$itemid = 'moodleoverflow-subscription-toggle-' . $moodleoverflow->id;
114+
debugging('CM object: ' . print_r($this->cm, true), DEBUG_DEVELOPER);
115+
116+
// Check the subscription status of the user and if it's changable.
117+
$subscribed = subscriptions::is_subscribed($USER->id, $moodleoverflow, $modulecontext);
118+
$changable = subscriptions::is_subscribable($moodleoverflow, $modulecontext);
119+
120+
// Build the content.
121+
$renderer = $PAGE->get_renderer('core_reportbuilder');
122+
$content = $renderer->render_from_template(
123+
'core/toggle',
124+
[
125+
'id' => $itemid,
126+
'checked' => $subscribed,
127+
'disabled' => !$changable,
128+
'extraattributes' => [
129+
['name' => 'data-type', 'value' => 'moodleoverflow-subscription-toggle'],
130+
['name' => 'data-action', 'value' => 'toggle'],
131+
['name' => 'data-cmid', 'value' => $this->cm->id],
132+
['name' => 'data-userid', 'value' => $USER->id],
133+
['name' => 'data-subscribed', 'value' => $subscribed ? 'true' : 'false'],
134+
],
135+
],
136+
);
137+
138+
// Add js to change subscription.
139+
$PAGE->requires->js_call_amd('mod_moodleoverflow/toggle_subscription', 'init', [$itemid]);
140+
141+
return new overviewitem(
142+
name: get_string('subscribed', 'mod_moodleoverflow'),
143+
value: 'hallo',
144+
content: $content,
145+
);
108146
}
109147

110148
/**

classes/subscriptions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ public static function reset_moodleoverflow_cache() {
641641
* @param context_module $context The module context
642642
* @param bool $userrequest Whether the user requested this change themselves.
643643
*
644-
* @return bool|int Returns true if the user is already subscribed or the subscription id if successfully subscribed.
644+
* @return bool
645645
*/
646-
public static function subscribe_user($userid, $moodleoverflow, $context, $userrequest = false) {
646+
public static function subscribe_user($userid, $moodleoverflow, $context, $userrequest = false): bool {
647647
global $DB;
648648

649649
// Check if the user is already subscribed.
@@ -693,8 +693,8 @@ public static function subscribe_user($userid, $moodleoverflow, $context, $userr
693693
$event = event\subscription_created::create($params);
694694
$event->trigger();
695695

696-
// Return the subscription ID.
697-
return $result;
696+
// Return if the operation was successful. As insert_record returns true/false/id, a parse to a bool is made.
697+
return (bool) $result;
698698
}
699699

700700
/**

db/services.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,12 @@
5050
'type' => 'write',
5151
'ajax' => true,
5252
],
53+
'mod_moodleoverflow_change_subscription_mode' => [
54+
'classname' => 'mod_moodleoverflow_external',
55+
'methodname' => 'change_subscription_mode',
56+
'classpath' => 'mod/moodleoverflow/externallib.php',
57+
'description' => 'Rejects a post',
58+
'type' => 'write',
59+
'ajax' => true,
60+
],
5361
];

externallib.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use mod_moodleoverflow\anonymous;
2626
use mod_moodleoverflow\output\moodleoverflow_email;
2727
use mod_moodleoverflow\review;
28+
use mod_moodleoverflow\subscriptions;
2829

2930
defined('MOODLE_INTERNAL') || die;
3031

@@ -310,4 +311,47 @@ public static function review_reject_post($postid, $reason = null) {
310311

311312
return $url;
312313
}
314+
315+
/**
316+
* Returns description of method parameters for change_subscription_mode
317+
* @return external_function_parameters
318+
*/
319+
public static function change_subscription_mode_parameters(): external_function_parameters {
320+
return new external_function_parameters(
321+
[
322+
'userid' => new external_value(PARAM_INT, 'the user id'),
323+
'subscribed' => new external_value(PARAM_BOOL, 'current subscription status'),
324+
'cmid' => new external_value(PARAM_INT, 'course module id that is targeted'),
325+
]
326+
);
327+
}
328+
329+
/**
330+
* Return the result of the change_subscription_mode function
331+
* @return external_value
332+
*/
333+
public static function change_subscription_mode_returns(): external_value {
334+
return new external_value(PARAM_BOOL, 'true if successful');
335+
}
336+
337+
/**
338+
* Changes the subscription mode on a moodleoverflow
339+
* @param int $userid The user the setting will be changed for.
340+
* @param bool $subscribed current subscription status. True if user is subscribed, false it user is not subscribed.
341+
* @param int $cmid The moodleoverflow that is being targeted.
342+
* @return bool
343+
*/
344+
public static function change_subscription_mode(int $userid, bool $subscribed, int $cmid): bool {
345+
global $DB;
346+
// Get the moodleoverflow from the cmid.
347+
$cm = get_coursemodule_from_id('moodleoverflow', $cmid, 0, false, MUST_EXIST);
348+
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $cm->instance], '*', MUST_EXIST);
349+
$modulecontext = context_module::instance($cmid);
350+
351+
if ($subscribed) {
352+
return subscriptions::unsubscribe_user($userid, $moodleoverflow, $modulecontext, true);
353+
} else {
354+
return subscriptions::subscribe_user($userid, $moodleoverflow, $modulecontext, true);
355+
}
356+
}
313357
}

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
defined('MOODLE_INTERNAL') || die();
2828

29-
$plugin->version = 2025112702;
29+
$plugin->version = 2025112703;
3030
$plugin->requires = 2024100700.00; // Require Moodle 4.5.
3131
$plugin->supported = [405, 501];
3232
$plugin->component = 'mod_moodleoverflow';

0 commit comments

Comments
 (0)