Skip to content

Commit 8605f56

Browse files
committed
WIP - update unread posts after marking posts as read
1 parent c2a2bfa commit 8605f56

File tree

6 files changed

+78
-25
lines changed

6 files changed

+78
-25
lines changed

amd/build/readtracking.min.js

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

amd/build/readtracking.min.js.map

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

amd/src/readtracking.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Ajax from 'core/ajax';
3030
*/
3131
export function init(itemid) {
3232
const element = document.getElementById(itemid);
33-
element.addEventListener('click', function() {
33+
element.addEventListener('click', async function() {
3434
const userid = parseInt(element.dataset.userid);
3535
const instanceid = parseInt(element.dataset.instanceid);
3636
const domain = element.dataset.domain;
@@ -42,6 +42,8 @@ export function init(itemid) {
4242
userid: userid
4343
},
4444
};
45-
return Ajax.call([data]);
45+
const result = await Ajax.call([data])[0];
46+
// Update the red bubble icon with the new amount of unread posts.
47+
return result;
4648
});
4749
}

classes/readtracking.php

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -458,23 +458,11 @@ public static function get_untracked_moodleoverflows($userid, $courseid) {
458458
*/
459459
public static function moodleoverflow_count_unread_posts_moodleoverflow($cm) {
460460
global $DB, $USER;
461-
462-
$moodleoverflow = $DB->get_record_sql("SELECT m.*, tm.id as hasdisabledtracking " .
463-
"FROM {moodleoverflow} m " .
464-
"LEFT JOIN {moodleoverflow_tracking} tm ON m.id = tm.moodleoverflowid AND tm.userid = :userid " .
465-
"WHERE m.id = :moodleoverflowid", ['userid' => $USER->id, 'moodleoverflowid' => $cm->instance]);
466-
467461
// Return if tracking is off, or ((optional or forced, but forced disallowed by admin) and user has disabled tracking).
468-
if (
469-
$moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OFF || (
470-
($moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OPTIONAL || (
471-
$moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_FORCED &&
472-
!get_config('moodleoverflow', 'allowforcedreadtracking')
473-
)
474-
) && $moodleoverflow->hasdisabledtracking)
475-
) {
462+
if (self::check_tracking_off($cm->instance, $USER->id)) {
476463
return 0;
477464
}
465+
478466
// Get the current timestamp and the cutoffdate.
479467
$now = round(time(), -2);
480468
$cutoffdate = $now - (get_config('moodleoverflow', 'oldpostdays') * 24 * 60 * 60);
@@ -490,4 +478,63 @@ public static function moodleoverflow_count_unread_posts_moodleoverflow($cm) {
490478
// Return the number of unread posts per moodleoverflow.
491479
return $DB->get_field_sql($sql, $params);
492480
}
481+
482+
/**
483+
* Get amound of unread posts in a discussion
484+
* @param int $discussionid
485+
* @param int $userid
486+
* @return int
487+
*/
488+
public static function moodleoverflow_count_unread_posts_discussion(int $discussionid, int $userid = 0) {
489+
global $DB, $USER;
490+
if ($userid == 0) {
491+
$userid = $USER->id;
492+
}
493+
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $discussionid]);
494+
495+
if (self::check_tracking_off($discussion->moodleoverflow, $userid)) {
496+
return 0;
497+
}
498+
499+
// Get the current timestamp and the cutoffdate.
500+
$now = round(time(), -2);
501+
$cutoffdate = $now - (get_config('moodleoverflow', 'oldpostdays') * 24 * 60 * 60);
502+
503+
// Define a sql-query.
504+
$params = [$USER->id, $discussion->id, $cutoffdate];
505+
$sql = "SELECT COUNT(p.id)
506+
FROM {moodleoverflow_posts} p
507+
JOIN {moodleoverflow_discussions} d ON p.discussion = d.id
508+
LEFT JOIN {moodleoverflow_read} r ON (r.postid = p.id AND r.userid = ?)
509+
WHERE d.id = ? AND p.modified >= ? AND r.id IS NULL";
510+
511+
// Return the number of unread posts per discussion.
512+
return $DB->get_field_sql($sql, $params);
513+
}
514+
515+
/**
516+
* Helper function, checks if:
517+
* tracking is off, or ((optional or forced, but forced disallowed by admin) and user has disabled tracking).
518+
* @param int $moodleoverflowid Object from DB.
519+
* @param int $userid
520+
* @return bool
521+
*/
522+
private static function check_tracking_off(int $moodleoverflowid, int $userid): bool {
523+
global $DB;
524+
$moodleoverflow = $DB->get_record_sql("SELECT m.*, tm.id as hasdisabledtracking " .
525+
"FROM {moodleoverflow} m " .
526+
"LEFT JOIN {moodleoverflow_tracking} tm ON m.id = tm.moodleoverflowid AND tm.userid = :userid " .
527+
"WHERE m.id = :moodleoverflowid", ['userid' => $userid, 'moodleoverflowid' => $moodleoverflowid]);
528+
529+
return (
530+
$moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OFF || (
531+
$moodleoverflow->hasdisabledtracking && (
532+
$moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_OPTIONAL || (
533+
$moodleoverflow->trackingtype == MOODLEOVERFLOW_TRACKING_FORCED &&
534+
!get_config('moodleoverflow', 'allowforcedreadtracking')
535+
)
536+
)
537+
)
538+
);
539+
}
493540
}

externallib.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,27 +412,31 @@ public static function mark_post_read_parameters(): external_function_parameters
412412
* @return external_value
413413
*/
414414
public static function mark_post_read_returns(): external_value {
415-
return new external_value(PARAM_BOOL, 'true if successful');
415+
return new external_value(PARAM_INT, 'Amount of unread posts after calling the function');
416416
}
417417

418418
/**
419419
* Marks all posts of a discussion/moodleoverflow as read
420420
* @param int $instanceid id of the discussion/moodleoverflow.
421421
* @param string $domain Can be "moodleoverflow" or "discussion"
422422
* @param int $userid
423-
* @return bool
423+
* @return int Return how many unread posts the user has in the discussion/moodleoverflow. JS uses it to update the unread info.
424+
* (It should always be 0, otherwise an error ocurred. This is important for behat testing).
424425
* @throws coding_exception|dml_exception
425426
*/
426-
public static function mark_post_read(int $instanceid, string $domain, int $userid): bool {
427+
public static function mark_post_read(int $instanceid, string $domain, int $userid): int {
427428
global $DB;
428429
if ($domain == 'moodleoverflow') {
429430
$cm = get_coursemodule_from_instance('moodleoverflow', $instanceid);
430-
return readtracking::moodleoverflow_mark_moodleoverflow_read($cm, $userid);
431+
readtracking::moodleoverflow_mark_moodleoverflow_read($cm, $userid);
432+
return readtracking::moodleoverflow_count_unread_posts_moodleoverflow($cm);
431433
} else {
432434
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $instanceid]);
433435
$cm = get_coursemodule_from_instance('moodleoverflow', $discussion->moodleoverflow, $discussion->course);
434-
$modcontext = context_module::instance($cm->id);
435-
return readtracking::moodleoverflow_mark_discussion_read($instanceid, $modcontext, $userid);
436+
readtracking::moodleoverflow_mark_discussion_read($instanceid, context_module::instance($cm->id), $userid);
437+
return readtracking::moodleoverflow_count_unread_posts_discussion($instanceid);
436438
}
439+
440+
437441
}
438442
}

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 = 2025112706;
29+
$plugin->version = 2025112707;
3030
$plugin->requires = 2024100700.00; // Require Moodle 4.5.
3131
$plugin->supported = [405, 501];
3232
$plugin->component = 'mod_moodleoverflow';

0 commit comments

Comments
 (0)