libdir .'/accesslib.php');
require_once($CFG->libdir .'/datalib.php');

$forummodulerow = get_record("modules", "name", "forum");

$rs = get_recordset_sql("
  SELECT s.id as id, s.userid as userid, cm.id as cmid
    FROM {$CFG->prefix}forum f JOIN {$CFG->prefix}forum_subscriptions s ON f.id = s.forum
         JOIN {$CFG->prefix}course_modules cm ON (f.course = cm.course AND f.id = cm.instance)
   WHERE f.forcesubscribe = 3
     AND cm.module = {$forummodulerow->id}");

if (!rs) {
    echo 'No user to process - Database is clean.';
    die();
}

echo 'Starting cleanup - will examine '. $rs->numrows() .' records.

'; $i = 0; while ($record = rs_fetch_next_record($rs)) { $i++; $context = get_context_instance(CONTEXT_MODULE, $record->cmid); if (!has_capability('moodle/course:manageactivities', $context, $record->userid)) { if ($doitforreal) { delete_records('forum_subscriptions', 'id', $record->id); echo $i .'. unsubscribing user #'. $record->userid .'
'; } else { echo $i .'. (test run) would unsubscribe user #'. $record->userid .'
'; } } else { echo ''. $i .'. skipping user #'. $record->userid .', who has permission to subscribe
'; } } echo "
Cleanup completed."; ?>