示例#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/forum/lib.php';
     $userid = $event->relateduserid;
     $sql = "SELECT f.id, f.course as course, cm.id AS cmid, f.forcesubscribe\n                  FROM {forum} f\n                  JOIN {course_modules} cm ON (cm.instance = f.id)\n                  JOIN {modules} m ON (m.id = cm.module)\n             LEFT JOIN {forum_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 = 'forum'\n                   AND fs.id IS NULL";
     $params = array('courseid' => $context->instanceid, 'userid' => $userid, 'initial' => FORUM_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/forum:allowforcesubscribe', $modcontext, $userid)) {
             \mod_forum\subscriptions::subscribe_user($userid, $forum, $modcontext);
         }
     }
 }
示例#2
0
/**
 * Given an object containing all the necessary data,
 * (defined by the form in mod_form.php) this function
 * will update an existing instance with new data.
 *
 * @global object
 * @param object $forum forum instance (with magic quotes)
 * @return bool success
 */
function forum_update_instance($forum, $mform)
{
    global $DB, $OUTPUT, $USER;
    $forum->timemodified = time();
    $forum->id = $forum->instance;
    if (empty($forum->assessed)) {
        $forum->assessed = 0;
    }
    if (empty($forum->ratingtime) or empty($forum->assessed)) {
        $forum->assesstimestart = 0;
        $forum->assesstimefinish = 0;
    }
    $oldforum = $DB->get_record('forum', array('id' => $forum->id));
    // MDL-3942 - if the aggregation type or scale (i.e. max grade) changes then recalculate the grades for the entire forum
    // if  scale changes - do we need to recheck the ratings, if ratings higher than scale how do we want to respond?
    // for count and sum aggregation types the grade we check to make sure they do not exceed the scale (i.e. max score) when calculating the grade
    if ($oldforum->assessed != $forum->assessed or $oldforum->scale != $forum->scale) {
        forum_update_grades($forum);
        // recalculate grades for the forum
    }
    if ($forum->type == 'single') {
        // Update related discussion and post.
        $discussions = $DB->get_records('forum_discussions', array('forum' => $forum->id), 'timemodified ASC');
        if (!empty($discussions)) {
            if (count($discussions) > 1) {
                echo $OUTPUT->notification(get_string('warnformorepost', 'forum'));
            }
            $discussion = array_pop($discussions);
        } else {
            // try to recover by creating initial discussion - MDL-16262
            $discussion = new stdClass();
            $discussion->course = $forum->course;
            $discussion->forum = $forum->id;
            $discussion->name = $forum->name;
            $discussion->assessed = $forum->assessed;
            $discussion->message = $forum->intro;
            $discussion->messageformat = $forum->introformat;
            $discussion->messagetrust = true;
            $discussion->mailnow = false;
            $discussion->groupid = -1;
            $message = '';
            forum_add_discussion($discussion, null, $message);
            if (!($discussion = $DB->get_record('forum_discussions', array('forum' => $forum->id)))) {
                print_error('cannotadd', 'forum');
            }
        }
        if (!($post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost)))) {
            print_error('cannotfindfirstpost', 'forum');
        }
        $cm = get_coursemodule_from_instance('forum', $forum->id);
        $modcontext = context_module::instance($cm->id, MUST_EXIST);
        $post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost), '*', MUST_EXIST);
        $post->subject = $forum->name;
        $post->message = $forum->intro;
        $post->messageformat = $forum->introformat;
        $post->messagetrust = trusttext_trusted($modcontext);
        $post->modified = $forum->timemodified;
        $post->userid = $USER->id;
        // MDL-18599, so that current teacher can take ownership of activities.
        if ($mform and $draftid = file_get_submitted_draft_itemid('introeditor')) {
            // Ugly hack - we need to copy the files somehow.
            $options = array('subdirs' => true);
            // Use the same options as intro field!
            $post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_forum', 'post', $post->id, $options, $post->message);
        }
        $DB->update_record('forum_posts', $post);
        $discussion->name = $forum->name;
        $DB->update_record('forum_discussions', $discussion);
    }
    $DB->update_record('forum', $forum);
    $modcontext = context_module::instance($forum->coursemodule);
    if ($forum->forcesubscribe == FORUM_INITIALSUBSCRIBE && $oldforum->forcesubscribe != $forum->forcesubscribe) {
        $users = \mod_forum\subscriptions::get_potential_subscribers($modcontext, 0, 'u.id, u.email', '');
        foreach ($users as $user) {
            \mod_forum\subscriptions::subscribe_user($user->id, $forum, $modcontext);
        }
    }
    forum_grade_item_update($forum);
    return true;
}
示例#3
0
 public function test_optional_with_unsubscribed_discussion_in_subscribed_forum()
 {
     $this->resetAfterTest(true);
     // Create a course, with a forum.
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
     $forum = $this->getDataGenerator()->create_module('forum', $options);
     // Create two users enrolled in the course as students.
     list($author, $recipient) = $this->helper_create_users($course, 2);
     // Post a discussion to the forum.
     list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
     // Unsubscribe the 'recipient' user from the discussion.
     \mod_forum\subscriptions::subscribe_user($recipient->id, $forum);
     // Then unsubscribe them from the discussion.
     \mod_forum\subscriptions::unsubscribe_user_from_discussion($recipient->id, $discussion);
     // We don't expect any users to receive this post.
     $expected = 0;
     // Run cron and check that the expected number of users received the notification.
     $messages = $this->helper_run_cron_check_count($post, $expected);
 }
示例#4
0
    // Can proceed now, the user is not guest and is enrolled
    foreach ($modinfo->get_instances_of('forum') as $forumid => $cm) {
        $forum = $forums[$forumid];
        $modcontext = context_module::instance($cm->id);
        $cansub = false;
        if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
            $cansub = true;
        }
        if ($cansub && $cm->visible == 0 && !has_capability('mod/forum:managesubscriptions', $modcontext)) {
            $cansub = false;
        }
        if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) {
            $subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
            $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
            if (($canmanageactivities || \mod_forum\subscriptions::is_subscribable($forum)) && $subscribe && !$subscribed && $cansub) {
                \mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext, true);
            } else {
                if (!$subscribe && $subscribed) {
                    \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext, true);
                }
            }
        }
    }
    $returnto = forum_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', 'forum', $shortname), 1);
    } else {
        redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1);
    }
}
示例#5
0
 /**
  * Test that the correct context is used in the events when subscribing
  * users.
  */
 public function test_forum_subscription_page_context_valid()
 {
     global $CFG, $PAGE;
     require_once $CFG->dirroot . '/mod/forum/lib.php';
     // Setup test data.
     $course = $this->getDataGenerator()->create_course();
     $user = $this->getDataGenerator()->create_user();
     $this->getDataGenerator()->enrol_user($user->id, $course->id);
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
     $forum = $this->getDataGenerator()->create_module('forum', $options);
     $quiz = $this->getDataGenerator()->create_module('quiz', $options);
     // Add a discussion.
     $record = array();
     $record['course'] = $course->id;
     $record['forum'] = $forum->id;
     $record['userid'] = $user->id;
     $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
     // Add a post.
     $record = array();
     $record['discussion'] = $discussion->id;
     $record['userid'] = $user->id;
     $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
     // Set up the default page event to use this forum.
     $PAGE = new moodle_page();
     $cm = get_coursemodule_from_instance('forum', $discussion->forum);
     $context = \context_module::instance($cm->id);
     $PAGE->set_context($context);
     $PAGE->set_cm($cm, $course, $forum);
     // Trigger and capturing the event.
     $sink = $this->redirectEvents();
     // Trigger the event by subscribing the user to the forum.
     \mod_forum\subscriptions::subscribe_user($user->id, $forum);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the forum.
     \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Now try with the context for a different module (quiz).
     $PAGE = new moodle_page();
     $cm = get_coursemodule_from_instance('quiz', $quiz->id);
     $quizcontext = \context_module::instance($cm->id);
     $PAGE->set_context($quizcontext);
     $PAGE->set_cm($cm, $course, $quiz);
     // Trigger and capturing the event.
     $sink = $this->redirectEvents();
     // Trigger the event by subscribing the user to the forum.
     \mod_forum\subscriptions::subscribe_user($user->id, $forum);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the forum.
     \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Now try with the course context - the module context should still be used.
     $PAGE = new moodle_page();
     $coursecontext = \context_course::instance($course->id);
     $PAGE->set_context($coursecontext);
     // Trigger the event by subscribing the user to the forum.
     \mod_forum\subscriptions::subscribe_user($user->id, $forum);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the forum.
     \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_forum\\event\\discussion_subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
 }
示例#6
0
 /**
  * Test that after toggling the forum subscription as another user,
  * the discussion subscription functionality works as expected.
  */
 public function test_forum_subscribe_toggle_as_other_repeat_subscriptions()
 {
     global $DB;
     $this->resetAfterTest(true);
     // Create a course, with a forum.
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
     $forum = $this->getDataGenerator()->create_module('forum', $options);
     // Create a user enrolled in the course as a student.
     list($user) = $this->helper_create_users($course, 1);
     // Post a discussion to the forum.
     list($discussion, $post) = $this->helper_post_to_forum($forum, $user);
     // Confirm that the user is currently not subscribed to the forum.
     $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
     // Confirm that the user is unsubscribed from the discussion too.
     $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum, $discussion->id));
     // Confirm that we have no records in either of the subscription tables.
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $user->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id)));
     // Subscribing to the forum should create a record in the subscriptions table, but not the forum discussion
     // subscriptions table.
     \mod_forum\subscriptions::subscribe_user($user->id, $forum);
     $this->assertEquals(1, $DB->count_records('forum_subscriptions', array('userid' => $user->id, 'forum' => $forum->id)));
     $this->assertEquals(0, $DB->count_records('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id)));
     // Now unsubscribe from the discussion. This should return true.
     $this->assertTrue(\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion));
     // Attempting to unsubscribe again should return false because no change was made.
     $this->assertFalse(\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion));
     // Subscribing to the discussion again should return truthfully as the subscription preference was removed.
     $this->assertTrue(\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion));
     // Attempting to subscribe again should return false because no change was made.
     $this->assertFalse(\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion));
     // Now unsubscribe from the discussion. This should return true once more.
     $this->assertTrue(\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion));
     // And unsubscribing from the forum but not as a request from the user should maintain their preference.
     \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $user->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id)));
     // Subscribing to the discussion should return truthfully because a change was made.
     $this->assertTrue(\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion));
     $this->assertEquals(0, $DB->count_records('forum_subscriptions', array('userid' => $user->id, 'forum' => $forum->id)));
     $this->assertEquals(1, $DB->count_records('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id)));
 }
示例#7
0
$options = array('forumid' => $forum->id, 'currentgroup' => $currentgroup, 'context' => $context);
$existingselector = new mod_forum_existing_subscriber_selector('existingsubscribers', $options);
$subscriberselector = new mod_forum_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 (!\mod_forum\subscriptions::subscribe_user($user->id, $forum)) {
                print_error('cannotaddsubscriber', 'forum', '', $user->id);
            }
        }
    } else {
        if ($unsubscribe) {
            $users = $existingselector->get_selected_users();
            foreach ($users as $user) {
                if (!\mod_forum\subscriptions::unsubscribe_user($user->id, $forum)) {
                    print_error('cannotremovesubscriber', 'forum', '', $user->id);
                }
            }
        }
    }
    $subscriberselector->invalidate_selected_users();
    $existingselector->invalidate_selected_users();
示例#8
0
/**
 * Adds user to the subscriber list
 *
 * @param int $userid
 * @param int $forumid
 * @param context_module|null $context Module context, may be omitted if not known or if called for the current module set in page.
 * @param boolean $userrequest Whether the user requested this change themselves. This has an effect on whether
 * discussion subscriptions are removed too.
 * @deprecated since Moodle 2.8 use \mod_forum\subscriptions::subscribe_user() instead
 */
function forum_subscribe($userid, $forumid, $context = null, $userrequest = false)
{
    global $DB;
    debugging("forum_subscribe() has been deprecated, please use \\mod_forum\\subscriptions::subscribe_user() instead.", DEBUG_DEVELOPER);
    // Note: The new function does not take an integer form of forum.
    $forum = $DB->get_record('forum', array('id' => $forumid));
    \mod_forum\subscriptions::subscribe_user($userid, $forum, $context, $userrequest);
}
            print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
        }
    }
} else {
    // subscribe
    if (\mod_forum\subscriptions::subscription_disabled($forum) && !has_capability('mod/forum:managesubscriptions', $context)) {
        print_error('disallowsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
    }
    if (!has_capability('mod/forum:viewdiscussion', $context)) {
        print_error('noviewdiscussionspermission', 'forum', $_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', 'forum', format_string($forum->name)), new moodle_url($PAGE->url, array('sesskey' => sesskey())), new moodle_url('/mod/forum/view.php', array('f' => $id)));
        echo $OUTPUT->footer();
        exit;
    }
    require_sesskey();
    if ($discussionid == null) {
        \mod_forum\subscriptions::subscribe_user($user->id, $forum, $context, true);
        redirect($returnto, get_string("nowsubscribed", "forum", $info), 1);
    } else {
        $discussion = $DB->get_record('forum_discussions', array('id' => $discussionid));
        $info->discussion = $discussion->name;
        \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion, $context);
        redirect($returnto, get_string("discussionnowsubscribed", "forum", $info), 1);
    }
}