Exemplo n.º 1
0
 /**
  * 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 forum.
     if ($context->contextlevel != CONTEXT_COURSE) {
         return;
     }
     // Forum lib required for the constant used below.
     require_once $CFG->dirroot . '/mod/hsuforum/lib.php';
     $userid = $event->relateduserid;
     $sql = "SELECT f.id, cm.id AS cmid\n                  FROM {hsuforum} f\n                  JOIN {course_modules} cm ON (cm.instance = f.id)\n                  JOIN {modules} m ON (m.id = cm.module)\n             LEFT JOIN {hsuforum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid)\n                 WHERE f.course = :courseid\n                   AND f.forcesubscribe = :initial\n                   AND m.name = 'hsuforum'\n                   AND fs.id IS NULL";
     $params = array('courseid' => $context->instanceid, 'userid' => $userid, 'initial' => HSUFORUM_INITIALSUBSCRIBE);
     $forums = $DB->get_records_sql($sql, $params);
     foreach ($forums as $forum) {
         // If user doesn't have allowforcesubscribe capability then don't subscribe.
         $modcontext = context_module::instance($forum->cmid);
         if (has_capability('mod/hsuforum:allowforcesubscribe', $modcontext, $userid)) {
             hsuforum_subscribe($userid, $forum->id, $modcontext);
         }
     }
 }
Exemplo n.º 2
0
        echo $OUTPUT->confirm(get_string('confirmunsubscribe', 'hsuforum', format_string($forum->name)), new moodle_url($PAGE->url, array('sesskey' => sesskey())), new moodle_url('/mod/hsuforum/view.php', array('f' => $id)));
        echo $OUTPUT->footer();
        exit;
    }
    require_sesskey();
    if (hsuforum_unsubscribe($user->id, $forum->id)) {
        redirect($returnto, get_string("nownotsubscribed", "hsuforum", $info), 1);
    } else {
        print_error('cannotunsubscribe', 'hsuforum', $_SERVER["HTTP_REFERER"]);
    }
} else {
    // subscribe
    if ($forum->forcesubscribe == HSUFORUM_DISALLOWSUBSCRIBE && !has_capability('mod/hsuforum:managesubscriptions', $context)) {
        print_error('disallowsubscribe', 'hsuforum', $_SERVER["HTTP_REFERER"]);
    }
    if (!has_capability('mod/hsuforum:viewdiscussion', $context)) {
        print_error('noviewdiscussionspermission', 'hsuforum', $_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', 'hsuforum', format_string($forum->name)), new moodle_url($PAGE->url, array('sesskey' => sesskey())), new moodle_url('/mod/hsuforum/view.php', array('f' => $id)));
        echo $OUTPUT->footer();
        exit;
    }
    require_sesskey();
    hsuforum_subscribe($user->id, $forum->id);
    redirect($returnto, get_string("nowsubscribed", "hsuforum", $info), 1);
}
Exemplo n.º 3
0
/// Do course wide subscribe/unsubscribe
if (!is_null($subscribe) and !isguestuser()) {
    foreach ($modinfo->get_instances_of('hsuforum') as $forumid => $cm) {
        $forum = $forums[$forumid];
        $modcontext = context_module::instance($cm->id);
        $cansub = false;
        if (has_capability('mod/hsuforum:viewdiscussion', $modcontext)) {
            $cansub = true;
        }
        if ($cansub && $cm->visible == 0 && !has_capability('mod/hsuforum:managesubscriptions', $modcontext)) {
            $cansub = false;
        }
        if (!hsuforum_is_forcesubscribed($forum)) {
            $subscribed = hsuforum_is_subscribed($USER->id, $forum);
            if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != HSUFORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
                hsuforum_subscribe($USER->id, $forumid, $modcontext);
            } else {
                if (!$subscribe && $subscribed) {
                    hsuforum_unsubscribe($USER->id, $forumid, $modcontext);
                }
            }
        }
    }
    $returnto = hsuforum_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', 'hsuforum', $shortname), 1);
    } else {
        redirect($returnto, get_string('nowallunsubscribed', 'hsuforum', $shortname), 1);
    }
}
$options = array('forumid' => $forum->id, 'currentgroup' => $currentgroup, 'context' => $context);
$existingselector = new hsuforum_existing_subscriber_selector('existingsubscribers', $options);
$subscriberselector = new hsuforum_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 (!hsuforum_subscribe($user->id, $id)) {
                print_error('cannotaddsubscriber', 'hsuforum', '', $user->id);
            }
        }
    } else {
        if ($unsubscribe) {
            $users = $existingselector->get_selected_users();
            foreach ($users as $user) {
                if (!hsuforum_unsubscribe($user->id, $id)) {
                    print_error('cannotremovesubscriber', 'hsuforum', '', $user->id);
                }
            }
        }
    }
    $subscriberselector->invalidate_selected_users();
    $existingselector->invalidate_selected_users();
Exemplo n.º 5
0
/**
 * This function gets run whenever user is enrolled into course
 *
 * @deprecated deprecating this function as we will be using \mod_hsuforum\observer::role_assigned()
 * @param stdClass $cp
 * @return void
 */
function hsuforum_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/hsuforum:initialsubscriptions' which was
    //       introduced because we did not have enrolment information in earlier versions...
    $sql = "SELECT f.id\n              FROM {hsuforum} f\n         LEFT JOIN {hsuforum_subscriptions} fs ON (fs.forum = 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' => HSUFORUM_INITIALSUBSCRIBE);
    $forums = $DB->get_records_sql($sql, $params);
    foreach ($forums as $forum) {
        hsuforum_subscribe($cp->userid, $forum->id);
    }
}