本文整理匯總了PHP中mod_forum\subscriptions::subscribe_user_to_discussion方法的典型用法代碼示例。如果您正苦於以下問題:PHP subscriptions::subscribe_user_to_discussion方法的具體用法?PHP subscriptions::subscribe_user_to_discussion怎麽用?PHP subscriptions::subscribe_user_to_discussion使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mod_forum\subscriptions
的用法示例。
在下文中一共展示了subscriptions::subscribe_user_to_discussion方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: test_forum_discussion_subscription_forum_unsubscribed_discussion_subscribed_after_post
/**
* Test that a user unsubscribed from a forum who has subscribed to a discussion, only receives posts made after
* they subscribed to the discussion.
*/
public function test_forum_discussion_subscription_forum_unsubscribed_discussion_subscribed_after_post()
{
$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);
$expectedmessages = array();
// Create a user enrolled in the course as a student.
list($author) = $this->helper_create_users($course, 1);
// Post a discussion to the forum.
list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
$this->helper_update_post_time($post, -90);
$expectedmessages[] = array('id' => $post->id, 'subject' => $post->subject, 'count' => 0);
// Then subscribe the user to the discussion.
$this->assertTrue(\mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion));
$this->helper_update_subscription_time($author, $discussion, -60);
// Then post a reply to the first discussion.
$reply = $this->helper_post_to_discussion($forum, $discussion, $author);
$this->helper_update_post_time($reply, -30);
$expectedmessages[] = array('id' => $reply->id, 'subject' => $reply->subject, 'count' => 1);
$expectedcount = 1;
// Run cron and check that the expected number of users received the notification.
$messages = $this->helper_run_cron_check_counts($expectedmessages, $expectedcount);
}
示例2: forum_post_subscription
/**
* Given a new post, subscribes or unsubscribes as appropriate.
* Returns some text which describes what happened.
*
* @param object $fromform The submitted form
* @param stdClass $forum The forum record
* @param stdClass $discussion The forum discussion record
* @return string
*/
function forum_post_subscription($fromform, $forum, $discussion)
{
global $USER;
if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
return "";
} else {
if (\mod_forum\subscriptions::subscription_disabled($forum)) {
$subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum);
if ($subscribed && !has_capability('moodle/course:manageactivities', context_course::instance($forum->course), $USER->id)) {
// This user should not be subscribed to the forum.
\mod_forum\subscriptions::unsubscribe_user($USER->id, $forum);
}
return "";
}
}
$info = new stdClass();
$info->name = fullname($USER);
$info->forum = format_string($forum->name);
if ($fromform->discussionsubscribe) {
if ($result = \mod_forum\subscriptions::subscribe_user_to_discussion($USER->id, $discussion)) {
return html_writer::tag('p', get_string('nowsubscribed', 'forum', $info));
}
} else {
if ($result = \mod_forum\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion)) {
return html_writer::tag('p', get_string('nownotsubscribed', 'forum', $info));
}
}
return '';
}
示例3: test_forum_subscription_page_context_valid
/**
* 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();
//.........這裏部分代碼省略.........
示例4: test_forum_subscribe_toggle_as_other_repeat_subscriptions
/**
* 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)));
}
示例5: array
// Ensure they are subscribed to the discussion still.
$subscriptionchanges[$userid] = $subscriptiontime;
}
}
}
$DB->set_field('forum_discussions', 'forum', $forumto->id, array('id' => $discussion->id));
$DB->set_field('forum_read', 'forumid', $forumto->id, array('discussionid' => $discussion->id));
// Delete the existing per-discussion subscriptions and replace them with the newly calculated ones.
$DB->delete_records('forum_discussion_subs', array('discussion' => $discussion->id));
$newdiscussion = clone $discussion;
$newdiscussion->forum = $forumto->id;
foreach ($subscriptionchanges as $userid => $preference) {
if ($preference != \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED) {
// Users must have viewdiscussion to a discussion.
if (has_capability('mod/forum:viewdiscussion', $destinationctx, $userid)) {
\mod_forum\subscriptions::subscribe_user_to_discussion($userid, $newdiscussion, $destinationctx);
}
} else {
\mod_forum\subscriptions::unsubscribe_user_from_discussion($userid, $newdiscussion, $destinationctx);
}
}
$params = array('context' => $destinationctx, 'objectid' => $discussion->id, 'other' => array('fromforumid' => $forum->id, 'toforumid' => $forumto->id));
$event = \mod_forum\event\discussion_moved::create($params);
$event->add_record_snapshot('forum_discussions', $discussion);
$event->add_record_snapshot('forum', $forum);
$event->add_record_snapshot('forum', $forumto);
$event->trigger();
// Delete the RSS files for the 2 forums to force regeneration of the feeds
require_once $CFG->dirroot . '/mod/forum/rsslib.php';
forum_rss_delete_file($forum);
forum_rss_delete_file($forumto);
示例6: optional_param
$includetext = optional_param('includetext', false, PARAM_BOOL);
$forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
$discussion = $DB->get_record('forum_discussions', array('id' => $discussionid, 'forum' => $forumid), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
$context = context_module::instance($cm->id);
require_sesskey();
require_login($course, false, $cm);
require_capability('mod/forum:viewdiscussion', $context);
$return = new stdClass();
if (is_guest($context, $USER)) {
// is_guest should be used here as this also checks whether the user is a guest in the current course.
// Guests and visitors cannot subscribe - only enrolled users.
throw new moodle_exception('noguestsubscribe', 'mod_forum');
}
if (!\mod_forum\subscriptions::is_subscribable($forum)) {
// Nothing to do. We won't actually output any content here though.
echo json_encode($return);
die;
}
if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum, $discussion->id, $cm)) {
// The user is subscribed, unsubscribe them.
\mod_forum\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion, $context);
} else {
// The user is unsubscribed, subscribe them.
\mod_forum\subscriptions::subscribe_user_to_discussion($USER->id, $discussion, $context);
}
// Now return the updated subscription icon.
$return->icon = forum_get_discussion_subscription_icon($forum, $discussion->id, null, $includetext);
echo json_encode($return);
die;
示例7: test_automatic_with_subscribed_discussion_in_unsubscribed_forum
public function test_automatic_with_subscribed_discussion_in_unsubscribed_forum()
{
$this->resetAfterTest(true);
// Create a course, with a forum.
$course = $this->getDataGenerator()->create_course();
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
$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 'author' user from the discussion.
\mod_forum\subscriptions::unsubscribe_user($author->id, $forum);
// Then re-subscribe them to the discussion.
\mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion);
// We expect two users to receive this post.
$expected = 2;
// Run cron and check that the expected number of users received the notification.
$messages = $this->helper_run_cron_check_count($post, $expected);
$seenauthor = false;
$seenrecipient = false;
foreach ($messages as $message) {
// They should both be from our user.
$this->assertEquals($author->id, $message->useridfrom);
if ($message->useridto == $author->id) {
$seenauthor = true;
} else {
if ($message->useridto = $recipient->id) {
$seenrecipient = true;
}
}
}
// Check we only saw one user.
$this->assertTrue($seenauthor);
$this->assertTrue($seenrecipient);
}