/** * Observer for role_assigned event. * * @param \core\event\role_assigned $event * @return void */ public static function role_assigned(\core\event\role_assigned $event) { global $CFG, $DB; $context = context::instance_by_id($event->contextid, MUST_EXIST); // If contextlevel is course then only subscribe user. Role assignment // at course level means user is enroled in course and can subscribe to anonymous forum. if ($context->contextlevel != CONTEXT_COURSE) { return; } // Anonymous Forum lib required for the constant used below. require_once $CFG->dirroot . '/mod/anonforum/lib.php'; $userid = $event->relateduserid; $sql = "SELECT f.id, cm.id AS cmid\n FROM {anonforum} f\n JOIN {course_modules} cm ON (cm.instance = f.id)\n JOIN {modules} m ON (m.id = cm.module)\n LEFT JOIN {anonforum_subscriptions} fs ON (fs.anonforum = f.id AND fs.userid = :userid)\n WHERE f.course = :courseid\n AND f.forcesubscribe = :initial\n AND m.name = 'anonforum'\n AND fs.id IS NULL"; $params = array('courseid' => $context->instanceid, 'userid' => $userid, 'initial' => ANONFORUM_INITIALSUBSCRIBE); $anonforums = $DB->get_records_sql($sql, $params); foreach ($anonforums as $anonforum) { // If user doesn't have allowforcesubscribe capability then don't subscribe. if (has_capability('mod/anonforum:allowforcesubscribe', context_module::instance($anonforum->cmid), $userid)) { anonforum_subscribe($userid, $anonforum->id); } } }
} // Can proceed now, the user is not guest and is enrolled foreach ($modinfo->get_instances_of('anonforum') as $anonforumid => $cm) { $anonforum = $anonforums[$anonforumid]; $modcontext = context_module::instance($cm->id); $cansub = false; if (has_capability('mod/anonforum:viewdiscussion', $modcontext)) { $cansub = true; } if ($cansub && $cm->visible == 0 && !has_capability('mod/anonforum:managesubscriptions', $modcontext)) { $cansub = false; } if (!anonforum_is_forcesubscribed($anonforum)) { $subscribed = anonforum_is_subscribed($USER->id, $anonforum); if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $anonforum->forcesubscribe != ANONFORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) { anonforum_subscribe($USER->id, $anonforumid); } else { if (!$subscribe && $subscribed) { anonforum_unsubscribe($USER->id, $anonforumid); } } } } $returnto = anonforum_go_back_to("index.php?id={$course->id}"); $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id))); if ($subscribe) { redirect($returnto, get_string('nowallsubscribed', 'anonforum', $shortname), 1); } else { redirect($returnto, get_string('nowallunsubscribed', 'anonforum', $shortname), 1); } }
/** * This function gets run whenever user is enrolled into course * * @deprecated deprecating this function as we will be using \mod_anonforum\observer::role_assigned() * @param stdClass $cp * @return void */ function anonforum_user_enrolled($cp) { global $DB; // NOTE: this has to be as fast as possible - we do not want to slow down enrolments! // Originally there used to be 'mod/anonymous forum:initialsubscriptions' which was // introduced because we did not have enrolment information in earlier versions... $sql = "SELECT f.id\n FROM {anonforum} f\n LEFT JOIN {anonforum_subscriptions} fs ON (fs.anonforum = f.id AND fs.userid = :userid)\n WHERE f.course = :courseid AND f.forcesubscribe = :initial AND fs.id IS NULL"; $params = array('courseid' => $cp->courseid, 'userid' => $cp->userid, 'initial' => ANONFORUM_INITIALSUBSCRIBE); $anonforums = $DB->get_records_sql($sql, $params); foreach ($anonforums as $anonforum) { anonforum_subscribe($cp->userid, $anonforum->id); } }
redirect($returnto, get_string("nownotsubscribed", "anonforum", $info), 1); } else { print_error('cannotunsubscribe', 'anonforum', $_SERVER["HTTP_REFERER"]); } } else { // subscribe if ($anonforum->forcesubscribe == ANONFORUM_DISALLOWSUBSCRIBE && !has_capability('mod/anonforum:managesubscriptions', $context)) { print_error('disallowsubscribe', 'anonforum', $_SERVER["HTTP_REFERER"]); } if (!has_capability('mod/anonforum:viewdiscussion', $context)) { print_error('noviewdiscussionspermission', 'anonforum', $_SERVER["HTTP_REFERER"]); } if (is_null($sesskey)) { // we came here via link in email $PAGE->set_title($course->shortname); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); echo $OUTPUT->confirm(get_string('confirmsubscribe', 'anonforum', format_string($anonforum->name)), new moodle_url($PAGE->url, array('sesskey' => sesskey())), new moodle_url('/mod/anonforum/view.php', array('f' => $id))); echo $OUTPUT->footer(); exit; } require_sesskey(); anonforum_subscribe($user->id, $anonforum->id); if (empty($anonforum->anonymous)) { $params = array('context' => $context, 'courseid' => $course->id, 'anonymous' => 1, 'other' => array('forumid' => $anonforum->id, '$cm->id' => $cm->id, 'action' => 'subscribe')); $event = mod_anonforum\event\subscription_created::create($params); $event->add_record_snapshot('anonforum', $anonforum); $event->trigger(); } redirect($returnto, get_string("nowsubscribed", "anonforum", $info), 1); }
$options = array('anonforumid' => $anonforum->id, 'currentgroup' => $currentgroup, 'context' => $context); $existingselector = new anonforum_existing_subscriber_selector('existingsubscribers', $options); $subscriberselector = new anonforum_potential_subscriber_selector('potentialsubscribers', $options); $subscriberselector->set_existing_subscribers($existingselector->find_users('')); if (data_submitted()) { require_sesskey(); $subscribe = (bool) optional_param('subscribe', false, PARAM_RAW); $unsubscribe = (bool) optional_param('unsubscribe', false, PARAM_RAW); // It has to be one or the other, not both or neither. if (!($subscribe xor $unsubscribe)) { print_error('invalidaction'); } if ($subscribe) { $users = $subscriberselector->get_selected_users(); foreach ($users as $user) { if (!anonforum_subscribe($user->id, $id)) { print_error('cannotaddsubscriber', 'anonforum', '', $user->id); } } } else { if ($unsubscribe) { $users = $existingselector->get_selected_users(); foreach ($users as $user) { if (!anonforum_unsubscribe($user->id, $id)) { print_error('cannotremovesubscriber', 'anonforum', '', $user->id); } } } } $subscriberselector->invalidate_selected_users(); $existingselector->invalidate_selected_users();