/**
  * Set up message and mail sinks, and set up other requirements for the
  * cron to be tested here.
  */
 public function setUp()
 {
     global $CFG;
     $this->helper = new stdClass();
     // Messaging is not compatible with transactions...
     $this->preventResetByRollback();
     // Catch all messages
     $this->helper->messagesink = $this->redirectMessages();
     $this->helper->mailsink = $this->redirectEmails();
     // Confirm that we have an empty message sink so far.
     $messages = $this->helper->messagesink->get_messages();
     $this->assertEquals(0, count($messages));
     $messages = $this->helper->mailsink->get_messages();
     $this->assertEquals(0, count($messages));
     // Tell Moodle that we've not sent any digest messages out recently.
     $CFG->digestmailtimelast = 0;
     // And set the digest sending time to a negative number - this has
     // the effect of making it 11pm the previous day.
     $CFG->digestmailtime = -1;
     // Forcibly reduce the maxeditingtime to a one second to ensure that
     // messages are sent out.
     $CFG->maxeditingtime = 1;
     // We must clear the subscription caches. This has to be done both before each test, and after in case of other
     // tests using these functions.
     \mod_twf\subscriptions::reset_twf_cache();
     \mod_twf\subscriptions::reset_discussion_cache();
 }
Beispiel #2
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 twf.
     if ($context->contextlevel != CONTEXT_COURSE) {
         return;
     }
     // Forum lib required for the constant used below.
     require_once $CFG->dirroot . '/mod/twf/lib.php';
     $userid = $event->relateduserid;
     $sql = "SELECT f.id, f.course as course, cm.id AS cmid, f.forcesubscribe\n                  FROM {twf} f\n                  JOIN {course_modules} cm ON (cm.instance = f.id)\n                  JOIN {modules} m ON (m.id = cm.module)\n             LEFT JOIN {twf_subscriptions} fs ON (fs.twf = f.id AND fs.userid = :userid)\n                 WHERE f.course = :courseid\n                   AND f.forcesubscribe = :initial\n                   AND m.name = 'twf'\n                   AND fs.id IS NULL";
     $params = array('courseid' => $context->instanceid, 'userid' => $userid, 'initial' => FORUM_INITIALSUBSCRIBE);
     $twfs = $DB->get_records_sql($sql, $params);
     foreach ($twfs as $twf) {
         // If user doesn't have allowforcesubscribe capability then don't subscribe.
         $modcontext = context_module::instance($twf->cmid);
         if (has_capability('mod/twf:allowforcesubscribe', $modcontext, $userid)) {
             \mod_twf\subscriptions::subscribe_user($userid, $twf, $modcontext);
         }
     }
 }
Beispiel #3
0
$node->display = false;
if ($node && $post->id != $discussion->firstpost) {
    $node->add(format_string($post->subject), $PAGE->url);
}
$PAGE->set_title("{$course->shortname}: " . format_string($discussion->name));
$PAGE->set_heading($course->fullname);
$PAGE->set_button($searchform);
$renderer = $PAGE->get_renderer('mod_twf');
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($twf->name), 2);
echo $OUTPUT->heading(format_string($discussion->name), 3, 'discussionname');
// 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.
if (!is_guest($modcontext, $USER) && isloggedin() && has_capability('mod/twf:viewdiscussion', $modcontext)) {
    // Discussion subscription.
    if (\mod_twf\subscriptions::is_subscribable($twf)) {
        echo html_writer::div(twf_get_discussion_subscription_icon($twf, $post->discussion, null, true), 'discussionsubscription');
        echo twf_get_discussion_subscription_icon_preloaders();
    }
}
/// Check to see if groups are being used in this twf
/// If so, make sure the current person is allowed to see this discussion
/// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
$canreply = twf_user_can_post($twf, $discussion, $USER, $cm, $course, $modcontext);
if (!$canreply and $twf->type !== 'news') {
    if (isguestuser() or !isloggedin()) {
        $canreply = true;
    }
    if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
        // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
        // normal users with temporary guest access see this link too, they are asked to enrol instead
$includetext = optional_param('includetext', false, PARAM_BOOL);
$twf = $DB->get_record('twf', array('id' => $twfid), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $twf->course), '*', MUST_EXIST);
$discussion = $DB->get_record('twf_discussions', array('id' => $discussionid), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('twf', $twf->id, $course->id, false, MUST_EXIST);
$context = context_module::instance($cm->id);
require_sesskey();
require_login($course, false, $cm);
require_capability('mod/twf: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_twf');
}
if (!\mod_twf\subscriptions::is_subscribable($twf)) {
    // Nothing to do. We won't actually output any content here though.
    echo json_encode($return);
    die;
}
if (\mod_twf\subscriptions::is_subscribed($USER->id, $twf, $discussion->id, $cm)) {
    // The user is subscribed, unsubscribe them.
    \mod_twf\subscriptions::unsubscribe_user_from_discussion($USER->id, $discussion, $context);
} else {
    // The user is unsubscribed, subscribe them.
    \mod_twf\subscriptions::subscribe_user_to_discussion($USER->id, $discussion, $context);
}
// Now return the updated subscription icon.
$return->icon = twf_get_discussion_subscription_icon($twf, $discussion->id, null, $includetext);
echo json_encode($return);
die;
Beispiel #5
0
 /**
  * Test that a user unsubscribed from a twf who has subscribed to a discussion, only receives posts made after
  * they subscribed to the discussion.
  */
 public function test_twf_discussion_subscription_twf_unsubscribed_discussion_subscribed_after_post()
 {
     $this->resetAfterTest(true);
     // Create a course, with a twf.
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
     $twf = $this->getDataGenerator()->create_module('twf', $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 twf.
     list($discussion, $post) = $this->helper_post_to_twf($twf, $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_twf\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($twf, $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);
 }
echo $OUTPUT->header();
echo $OUTPUT->heading($strunsubscribeall);
if (data_submitted() and $confirm and confirm_sesskey()) {
    $twfs = \mod_twf\subscriptions::get_unsubscribable_twfs();
    foreach ($twfs as $twf) {
        \mod_twf\subscriptions::unsubscribe_user($USER->id, $twf, context_module::instance($twf->cm), true);
    }
    $DB->delete_records('twf_discussion_subs', array('userid' => $USER->id));
    $DB->set_field('user', 'autosubscribe', 0, array('id' => $USER->id));
    echo $OUTPUT->box(get_string('unsubscribealldone', 'twf'));
    echo $OUTPUT->continue_button($return);
    echo $OUTPUT->footer();
    die;
} else {
    $count = new stdClass();
    $count->twfs = count(\mod_twf\subscriptions::get_unsubscribable_twfs());
    $count->discussions = $DB->count_records('twf_discussion_subs', array('userid' => $USER->id));
    if ($count->twfs || $count->discussions) {
        if ($count->twfs && $count->discussions) {
            $msg = get_string('unsubscribeallconfirm', 'twf', $count);
        } else {
            if ($count->twfs) {
                $msg = get_string('unsubscribeallconfirmtwfs', 'twf', $count);
            } else {
                if ($count->discussions) {
                    $msg = get_string('unsubscribeallconfirmdiscussions', 'twf', $count);
                }
            }
        }
        echo $OUTPUT->confirm($msg, new moodle_url('unsubscribeall.php', array('confirm' => 1)), $return);
        echo $OUTPUT->footer();
 public function tearDown()
 {
     // We must clear the subscription caches. This has to be done both before each test, and after in case of other
     // tests using these functions.
     \mod_twf\subscriptions::reset_twf_cache();
 }
Beispiel #8
0
/**
 * Get the list of potential subscribers to a twf.
 *
 * @param object $twfcontext the twf context.
 * @param integer $groupid the id of a group, or 0 for all groups.
 * @param string $fields the list of fields to return for each user. As for get_users_by_capability.
 * @param string $sort sort order. As for get_users_by_capability.
 * @return array list of users.
 * @deprecated since Moodle 2.8 use \mod_twf\subscriptions::get_potential_subscribers() instead
 */
function twf_get_potential_subscribers($twfcontext, $groupid, $fields, $sort = '')
{
    debugging("twf_get_potential_subscribers() has been deprecated, please use \\mod_twf\\subscriptions::get_potential_subscribers() instead.", DEBUG_DEVELOPER);
    \mod_twf\subscriptions::get_potential_subscribers($twfcontext, $groupid, $fields, $sort);
}
Beispiel #9
0
$PAGE->navbar->add($strsubscribers);
$PAGE->set_title($strsubscribers);
$PAGE->set_heading($COURSE->fullname);
if (has_capability('mod/twf:managesubscriptions', $context) && \mod_twf\subscriptions::is_forcesubscribed($twf) === false) {
    if ($edit != -1) {
        $USER->subscriptionsediting = $edit;
    }
    $PAGE->set_button(twf_update_subscriptions_button($course->id, $id));
} else {
    unset($USER->subscriptionsediting);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('twf', 'twf') . ' ' . $strsubscribers);
if (empty($USER->subscriptionsediting)) {
    $subscribers = \mod_twf\subscriptions::fetch_subscribed_users($twf, $currentgroup, $context);
    if (\mod_twf\subscriptions::is_forcesubscribed($twf)) {
        $subscribers = mod_twf_filter_hidden_users($cm, $context, $subscribers);
    }
    echo $twfoutput->subscriber_overview($subscribers, $twf, $course);
} else {
    echo $twfoutput->subscriber_selection_form($existingselector, $subscriberselector);
}
echo $OUTPUT->footer();
/**
 * Filters a list of users for whether they can see a given activity.
 * If the course module is hidden (closed-eye icon), then only users who have
 * the permission to view hidden activities will appear in the output list.
 *
 * @todo MDL-48625 This filtering should be handled in core libraries instead.
 *
 * @param stdClass $cm the course module record of the activity.
Beispiel #10
0
$draftid_editor = file_get_submitted_draft_itemid('message');
$currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_twf', 'post', $postid, mod_twf_post_form::editor_options($modcontext, $postid), $post->message);
$manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
if (\mod_twf\subscriptions::subscription_disabled($twf) && !$manageactivities) {
    // User does not have permission to subscribe to this discussion at all.
    $discussionsubscribe = false;
} else {
    if (\mod_twf\subscriptions::is_forcesubscribed($twf)) {
        // User does not have permission to unsubscribe from this discussion at all.
        $discussionsubscribe = true;
    } else {
        if (isset($discussion) && \mod_twf\subscriptions::is_subscribed($USER->id, $twf, $discussion->id, $cm)) {
            // User is subscribed to the discussion - continue the subscription.
            $discussionsubscribe = true;
        } else {
            if (!isset($discussion) && \mod_twf\subscriptions::is_subscribed($USER->id, $twf, null, $cm)) {
                // Starting a new discussion, and the user is subscribed to the twf - subscribe to the discussion.
                $discussionsubscribe = true;
            } else {
                // User is not subscribed to either twf or discussion. Follow user preference.
                $discussionsubscribe = $USER->autosubscribe;
            }
        }
    }
}
$mform_post->set_data(array('attachments' => $draftitemid, 'general' => $heading, 'subject' => $post->subject, 'message' => array('text' => $currenttext, 'format' => empty($post->messageformat) ? editors_get_preferred_format() : $post->messageformat, 'itemid' => $draftid_editor), 'discussionsubscribe' => $discussionsubscribe, 'mailnow' => !empty($post->mailnow), 'userid' => $post->userid, 'parent' => $post->parent, 'discussion' => $post->discussion, 'course' => $course->id) + $page_params + (isset($post->format) ? array('format' => $post->format) : array()) + (isset($discussion->timestart) ? array('timestart' => $discussion->timestart) : array()) + (isset($discussion->timeend) ? array('timeend' => $discussion->timeend) : array()) + (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) + (isset($discussion->id) ? array('discussion' => $discussion->id) : array()));
if ($mform_post->is_cancelled()) {
    if (!isset($discussion->id) || $twf->type === 'qanda') {
        // Q and A twfs don't have a discussion page, so treat them like a new thread..
        redirect(new moodle_url('/mod/twf/view.php', array('f' => $twf->id)));
    } else {
Beispiel #11
0
 /**
  * Test that the correct context is used in the events when subscribing
  * users.
  */
 public function test_twf_subscription_page_context_valid()
 {
     global $CFG, $PAGE;
     require_once $CFG->dirroot . '/mod/twf/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);
     $twf = $this->getDataGenerator()->create_module('twf', $options);
     $quiz = $this->getDataGenerator()->create_module('quiz', $options);
     // Add a discussion.
     $record = array();
     $record['course'] = $course->id;
     $record['twf'] = $twf->id;
     $record['userid'] = $user->id;
     $discussion = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_discussion($record);
     // Add a post.
     $record = array();
     $record['discussion'] = $discussion->id;
     $record['userid'] = $user->id;
     $post = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_post($record);
     // Set up the default page event to use this twf.
     $PAGE = new moodle_page();
     $cm = get_coursemodule_from_instance('twf', $discussion->twf);
     $context = \context_module::instance($cm->id);
     $PAGE->set_context($context);
     $PAGE->set_cm($cm, $course, $twf);
     // Trigger and capturing the event.
     $sink = $this->redirectEvents();
     // Trigger the event by subscribing the user to the twf.
     \mod_twf\subscriptions::subscribe_user($user->id, $twf);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_twf\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the twf.
     \mod_twf\subscriptions::unsubscribe_user($user->id, $twf);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_twf\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_twf\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_twf\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_twf\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_twf\\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 twf.
     \mod_twf\subscriptions::subscribe_user($user->id, $twf);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_twf\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the twf.
     \mod_twf\subscriptions::unsubscribe_user($user->id, $twf);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_twf\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_twf\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_twf\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_twf\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_twf\\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 twf.
     \mod_twf\subscriptions::subscribe_user($user->id, $twf);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_twf\\event\\subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user to the twf.
     \mod_twf\subscriptions::unsubscribe_user($user->id, $twf);
     $events = $sink->get_events();
     $sink->clear();
     $this->assertCount(1, $events);
     $event = reset($events);
     // Checking that the event contains the expected values.
     $this->assertInstanceOf('\\mod_twf\\event\\subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by subscribing the user to the discussion.
     \mod_twf\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_twf\\event\\discussion_subscription_created', $event);
     $this->assertEquals($context, $event->get_context());
     // Trigger the event by unsubscribing the user from the discussion.
     \mod_twf\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_twf\\event\\discussion_subscription_deleted', $event);
     $this->assertEquals($context, $event->get_context());
 }
Beispiel #12
0
 /**
  * Test subscription using disallow subscription on create.
  */
 public function test_twf_disallow_subscribe_on_create()
 {
     global $CFG;
     $this->resetAfterTest();
     $usercount = 5;
     $course = $this->getDataGenerator()->create_course();
     $users = array();
     for ($i = 0; $i < $usercount; $i++) {
         $user = $this->getDataGenerator()->create_user();
         $users[] = $user;
         $this->getDataGenerator()->enrol_user($user->id, $course->id);
     }
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_DISALLOWSUBSCRIBE);
     // Subscription prevented.
     $twf = $this->getDataGenerator()->create_module('twf', $options);
     $result = \mod_twf\subscriptions::fetch_subscribed_users($twf);
     // No subscriptions by default.
     $this->assertEquals(0, count($result));
     foreach ($users as $user) {
         $this->assertFalse(\mod_twf\subscriptions::is_subscribed($user->id, $twf));
     }
 }
Beispiel #13
0
        $modcontext = context_module::instance($cm->id);
        $cansub = false;
        if (has_capability('mod/twf:viewdiscussion', $modcontext)) {
            $cansub = true;
        }
        if ($cansub && $cm->visible == 0 && !has_capability('mod/twf:managesubscriptions', $modcontext)) {
            $cansub = false;
        }
        if (!\mod_twf\subscriptions::is_forcesubscribed($twf)) {
            $subscribed = \mod_twf\subscriptions::is_subscribed($USER->id, $twf, null, $cm);
            $canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
            if (($canmanageactivities || \mod_twf\subscriptions::is_subscribable($twf)) && $subscribe && !$subscribed && $cansub) {
                \mod_twf\subscriptions::subscribe_user($USER->id, $twf, $modcontext, true);
            } else {
                if (!$subscribe && $subscribed) {
                    \mod_twf\subscriptions::unsubscribe_user($USER->id, $twf, $modcontext, true);
                }
            }
        }
    }
    $returnto = twf_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', 'twf', $shortname), 1);
    } else {
        redirect($returnto, get_string('nowallunsubscribed', 'twf', $shortname), 1);
    }
}
/// First, let's process the general twfs and build up a display
if ($generaltwfs) {
    foreach ($generaltwfs as $twf) {
Beispiel #14
0
 /**
  * Form definition
  *
  * @return void
  */
 function definition()
 {
     global $CFG, $OUTPUT;
     $mform =& $this->_form;
     //var_dump($this->_customdata);die;
     $course = $this->_customdata['course'];
     $cm = $this->_customdata['cm'];
     $coursecontext = $this->_customdata['coursecontext'];
     $modcontext = $this->_customdata['modcontext'];
     $twf = $this->_customdata['twf'];
     $post = $this->_customdata['post'];
     $subscribe = $this->_customdata['subscribe'];
     $edit = $this->_customdata['edit'];
     $thresholdwarning = $this->_customdata['thresholdwarning'];
     $syx_newdiscussion = $this->_customdata['syx_newdiscussion'];
     $syx_phase = $this->_customdata['syx_phase'];
     $syx_instance = $this->_customdata['syx_instance'];
     $syx_teamwork = $this->_customdata['syx_teamwork'];
     $mform->addElement('header', 'general', '');
     //fill in the data depending on page params later using set_data
     // If there is a warning message and we are not editing a post we need to handle the warning.
     if (!empty($thresholdwarning) && !$edit) {
         // Here we want to display a warning if they can still post but have reached the warning threshold.
         if ($thresholdwarning->canpost) {
             $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
             $mform->addElement('html', $OUTPUT->notification($message));
         }
     }
     $mform->addElement('text', 'subject', get_string('subject', 'twf'), 'size="48"');
     $mform->setType('subject', PARAM_TEXT);
     $mform->addRule('subject', get_string('required'), 'required', null, 'client');
     $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
     if ($syx_newdiscussion) {
         $mform->addElement('text', 'score', get_string('score', 'twf'));
         $mform->setType('score', PARAM_INT);
         $mform->addRule('score', get_string('required'), 'required', null, 'client');
         $mform->addHelpButton('score', 'score_rules', 'twf');
     }
     $mform->addElement('editor', 'message', get_string('message', 'twf'), null, self::editor_options($modcontext, empty($post->id) ? null : $post->id));
     $mform->setType('message', PARAM_RAW);
     $mform->addRule('message', get_string('required'), 'required', null, 'client');
     $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
     if (\mod_twf\subscriptions::is_forcesubscribed($twf)) {
         $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'twf'));
         $mform->freeze('discussionsubscribe');
         $mform->setDefaults('discussionsubscribe', 0);
         $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'twf');
     } else {
         if (\mod_twf\subscriptions::subscription_disabled($twf) && !$manageactivities) {
             $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'twf'));
             $mform->freeze('discussionsubscribe');
             $mform->setDefaults('discussionsubscribe', 0);
             $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'twf');
         } else {
             $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'twf'));
             $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'twf');
         }
     }
     if (!empty($twf->maxattachments) && $twf->maxbytes != 1 && has_capability('mod/twf:createattachment', $modcontext)) {
         //  1 = No attachments at all
         $mform->addElement('filemanager', 'attachments', get_string('attachment', 'twf'), null, self::attachment_options($twf));
         $mform->addHelpButton('attachments', 'attachment', 'twf');
     }
     if (empty($post->id) && $manageactivities) {
         $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'twf'));
     }
     if (!empty($CFG->twf_enabletimedposts) && !$post->parent && has_capability('mod/twf:viewhiddentimedposts', $coursecontext)) {
         // hack alert
         $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'twf'));
         $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'twf'), array('optional' => true));
         $mform->addHelpButton('timestart', 'displaystart', 'twf');
         $mform->addElement('date_selector', 'timeend', get_string('displayend', 'twf'), array('optional' => true));
         $mform->addHelpButton('timeend', 'displayend', 'twf');
     } else {
         $mform->addElement('hidden', 'timestart');
         $mform->setType('timestart', PARAM_INT);
         $mform->addElement('hidden', 'timeend');
         $mform->setType('timeend', PARAM_INT);
         $mform->setConstants(array('timestart' => 0, 'timeend' => 0));
     }
     if ($groupmode = groups_get_activity_groupmode($cm, $course)) {
         $groupdata = groups_get_activity_allowed_groups($cm);
         $groupinfo = array();
         foreach ($groupdata as $groupid => $group) {
             // Check whether this user can post in this group.
             // We must make this check because all groups are returned for a visible grouped activity.
             if (twf_user_can_post_discussion($twf, $groupid, null, $cm, $modcontext)) {
                 // Build the data for the groupinfo select.
                 $groupinfo[$groupid] = $group->name;
             } else {
                 unset($groupdata[$groupid]);
             }
         }
         $groupcount = count($groupinfo);
         // Check whether a user can post to all of their own groups.
         // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
         // 1) It only makes sense to allow this when a user is in more than one group.
         // Note: This check must come before we consider adding accessallgroups, because that is not a real group.
         $canposttoowngroups = empty($post->edit) && $groupcount > 1;
         // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
         $canposttoowngroups = $canposttoowngroups && empty($post->parent);
         // 3) You also need the canposttoowngroups capability.
         $canposttoowngroups = $canposttoowngroups && has_capability('mod/twf:canposttomygroups', $modcontext);
         if ($canposttoowngroups) {
             // This user is in multiple groups, and can post to all of their own groups.
             // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
             // user is a member of.
             $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'twf'));
             $mform->addHelpButton('posttomygroups', 'posttomygroups', 'twf');
             $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
         }
         // Check whether this user can post to all groups.
         // Posts to the 'All participants' group go to all groups, not to each group in a list.
         // It makes sense to allow this, even if there currently aren't any groups because there may be in the future.
         if (twf_user_can_post_discussion($twf, -1, null, $cm, $modcontext)) {
             // Note: We must reverse in this manner because array_unshift renumbers the array.
             $groupinfo = array_reverse($groupinfo, true);
             $groupinfo[-1] = get_string('allparticipants');
             $groupinfo = array_reverse($groupinfo, true);
             $groupcount++;
         }
         // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons.
         // 1) This is a new post (not an edit), and there are at least two groups to choose from.
         $canselectgroupfornew = empty($post->edit) && $groupcount > 1;
         // 2) This is editing of an existing post and the user is allowed to movediscussions.
         // We allow this because the post may have been moved from another twf where groups are not available.
         // We show this even if no groups are available as groups *may* have been available but now are not.
         $canselectgroupformove = $groupcount && !empty($post->edit) && has_capability('mod/twf:movediscussions', $modcontext);
         // Important: You can *only* change the group for a top level post. Never any reply.
         $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove);
         if ($canselectgroup) {
             $mform->addElement('select', 'groupinfo', get_string('group'), $groupinfo);
             $mform->setDefault('groupinfo', $post->groupid);
             $mform->setType('groupinfo', PARAM_INT);
         } else {
             if (empty($post->groupid)) {
                 $groupname = get_string('allparticipants');
             } else {
                 $groupname = format_string($groupdata[$post->groupid]->name);
             }
             $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
         }
     }
     //-------------------------------------------------------------------------------
     // buttons
     if (isset($post->edit)) {
         // hack alert
         $submit_string = get_string('savechanges');
     } else {
         $submit_string = get_string('posttotwf', 'twf');
     }
     $this->add_action_buttons(true, $submit_string);
     //var_dump($syx_teamwork);die;
     $mform->addElement('hidden', 'teamwork', $syx_teamwork);
     $mform->setType('teamwork', PARAM_INT);
     $mform->addElement('hidden', 'phase', $syx_phase);
     $mform->setType('phase', PARAM_INT);
     $mform->addElement('hidden', 'instace', $syx_instance);
     $mform->setType('instace', PARAM_INT);
     $mform->addElement('hidden', 'course');
     $mform->setType('course', PARAM_INT);
     $mform->addElement('hidden', 'twf');
     $mform->setType('twf', PARAM_INT);
     $mform->addElement('hidden', 'discussion');
     $mform->setType('discussion', PARAM_INT);
     $mform->addElement('hidden', 'parent');
     $mform->setType('parent', PARAM_INT);
     $mform->addElement('hidden', 'userid');
     $mform->setType('userid', PARAM_INT);
     $mform->addElement('hidden', 'groupid');
     $mform->setType('groupid', PARAM_INT);
     $mform->addElement('hidden', 'edit');
     $mform->setType('edit', PARAM_INT);
     $mform->addElement('hidden', 'reply');
     $mform->setType('reply', PARAM_INT);
 }
Beispiel #15
0
 /**
  * Returns a list of user objects who are subscribed to this twf.
  *
  * @param stdClass $twf The twf record.
  * @param int $groupid The group id if restricting subscriptions to a group of users, or 0 for all.
  * @param context_module $context the twf context, to save re-fetching it where possible.
  * @param string $fields requested user fields (with "u." table prefix).
  * @param boolean $includediscussionsubscriptions Whether to take discussion subscriptions and unsubscriptions into consideration.
  * @return array list of users.
  */
 public static function fetch_subscribed_users($twf, $groupid = 0, $context = null, $fields = null, $includediscussionsubscriptions = false)
 {
     global $CFG, $DB;
     if (empty($fields)) {
         $allnames = get_all_user_name_fields(true, 'u');
         $fields = "u.id,\n                      u.username,\n                      {$allnames},\n                      u.maildisplay,\n                      u.mailformat,\n                      u.maildigest,\n                      u.imagealt,\n                      u.email,\n                      u.emailstop,\n                      u.city,\n                      u.country,\n                      u.lastaccess,\n                      u.lastlogin,\n                      u.picture,\n                      u.timezone,\n                      u.theme,\n                      u.lang,\n                      u.tracktwfs,\n                      u.mnethostid";
     }
     // Retrieve the twf context if it wasn't specified.
     $context = twf_get_context($twf->id, $context);
     if (self::is_forcesubscribed($twf)) {
         $results = \mod_twf\subscriptions::get_potential_subscribers($context, $groupid, $fields, "u.email ASC");
     } else {
         // Only active enrolled users or everybody on the frontpage.
         list($esql, $params) = get_enrolled_sql($context, '', $groupid, true);
         $params['twfid'] = $twf->id;
         if ($includediscussionsubscriptions) {
             $params['stwfid'] = $twf->id;
             $params['dstwfid'] = $twf->id;
             $params['unsubscribed'] = self::FORUM_DISCUSSION_UNSUBSCRIBED;
             $sql = "SELECT {$fields}\n                        FROM (\n                            SELECT userid FROM {twf_subscriptions} s\n                            WHERE\n                                s.twf = :stwfid\n                                UNION\n                            SELECT userid FROM {twf_discussion_subs} ds\n                            WHERE\n                                ds.twf = :dstwfid AND ds.preference <> :unsubscribed\n                        ) subscriptions\n                        JOIN {user} u ON u.id = subscriptions.userid\n                        JOIN ({$esql}) je ON je.id = u.id\n                        ORDER BY u.email ASC";
         } else {
             $sql = "SELECT {$fields}\n                        FROM {user} u\n                        JOIN ({$esql}) je ON je.id = u.id\n                        JOIN {twf_subscriptions} s ON s.userid = u.id\n                        WHERE\n                          s.twf = :twfid\n                        ORDER BY u.email ASC";
         }
         $results = $DB->get_records_sql($sql, $params);
     }
     // Guest user should never be subscribed to a twf.
     unset($results[$CFG->siteguest]);
     // Apply the activity module availability resetrictions.
     $cm = get_coursemodule_from_instance('twf', $twf->id, $twf->course);
     $modinfo = get_fast_modinfo($twf->course);
     $info = new \core_availability\info_module($modinfo->get_cm($cm->id));
     $results = $info->filter_user_list($results);
     return $results;
 }
 /**
  * Test that providing a context_module instance to is_subscribed does not result in additional lookups to retrieve
  * the context_module.
  */
 public function test_is_subscribed_cm()
 {
     global $DB;
     $this->resetAfterTest(true);
     // Create a course, with a twf.
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE);
     $twf = $this->getDataGenerator()->create_module('twf', $options);
     // Create a user enrolled in the course as a student.
     list($user) = $this->helper_create_users($course, 1);
     // Retrieve the $cm now.
     $cm = get_fast_modinfo($twf->course)->instances['twf'][$twf->id];
     // Reset get_fast_modinfo.
     get_fast_modinfo(0, 0, true);
     // Call is_subscribed without passing the $cmid - this should result in a lookup and filling of some of the
     // caches. This provides us with consistent data to start from.
     $this->assertTrue(\mod_twf\subscriptions::is_subscribed($user->id, $twf));
     $this->assertTrue(\mod_twf\subscriptions::is_subscribed($user->id, $twf));
     // Make a note of the number of DB calls.
     $basecount = $DB->perf_get_reads();
     // Call is_subscribed - it should give return the correct result (False), and result in no additional queries.
     $this->assertTrue(\mod_twf\subscriptions::is_subscribed($user->id, $twf, null, $cm));
     // The capability check does require some queries, so we don't test it directly.
     // We don't assert here because this is dependant upon linked code which could change at any time.
     $suppliedcmcount = $DB->perf_get_reads() - $basecount;
     // Call is_subscribed without passing the $cmid now - this should result in a lookup.
     get_fast_modinfo(0, 0, true);
     $basecount = $DB->perf_get_reads();
     $this->assertTrue(\mod_twf\subscriptions::is_subscribed($user->id, $twf));
     $calculatedcmcount = $DB->perf_get_reads() - $basecount;
     // There should be more queries than when we performed the same check a moment ago.
     $this->assertGreaterThan($suppliedcmcount, $calculatedcmcount);
 }
Beispiel #17
0
/**
 * Adds module specific settings to the settings block
 *
 * @param settings_navigation $settings The settings navigation object
 * @param navigation_node $twfnode The node to add module settings to
 */
function twf_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $twfnode)
{
    global $USER, $PAGE, $CFG, $DB, $OUTPUT;
    $twfobject = $DB->get_record("twf", array("id" => $PAGE->cm->instance));
    if (empty($PAGE->cm->context)) {
        $PAGE->cm->context = context_module::instance($PAGE->cm->instance);
    }
    $params = $PAGE->url->params();
    if (!empty($params['d'])) {
        $discussionid = $params['d'];
    }
    // for some actions you need to be enrolled, beiing admin is not enough sometimes here
    $enrolled = is_enrolled($PAGE->cm->context, $USER, '', false);
    $activeenrolled = is_enrolled($PAGE->cm->context, $USER, '', true);
    $canmanage = has_capability('mod/twf:managesubscriptions', $PAGE->cm->context);
    $subscriptionmode = \mod_twf\subscriptions::get_subscription_mode($twfobject);
    $cansubscribe = $activeenrolled && !\mod_twf\subscriptions::is_forcesubscribed($twfobject) && (!\mod_twf\subscriptions::subscription_disabled($twfobject) || $canmanage);
    if ($canmanage) {
        $mode = $twfnode->add(get_string('subscriptionmode', 'twf'), null, navigation_node::TYPE_CONTAINER);
        $allowchoice = $mode->add(get_string('subscriptionoptional', 'twf'), new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'mode' => FORUM_CHOOSESUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING);
        $forceforever = $mode->add(get_string("subscriptionforced", "twf"), new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'mode' => FORUM_FORCESUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING);
        $forceinitially = $mode->add(get_string("subscriptionauto", "twf"), new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'mode' => FORUM_INITIALSUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING);
        $disallowchoice = $mode->add(get_string('subscriptiondisabled', 'twf'), new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'mode' => FORUM_DISALLOWSUBSCRIBE, 'sesskey' => sesskey())), navigation_node::TYPE_SETTING);
        switch ($subscriptionmode) {
            case FORUM_CHOOSESUBSCRIBE:
                // 0
                $allowchoice->action = null;
                $allowchoice->add_class('activesetting');
                break;
            case FORUM_FORCESUBSCRIBE:
                // 1
                $forceforever->action = null;
                $forceforever->add_class('activesetting');
                break;
            case FORUM_INITIALSUBSCRIBE:
                // 2
                $forceinitially->action = null;
                $forceinitially->add_class('activesetting');
                break;
            case FORUM_DISALLOWSUBSCRIBE:
                // 3
                $disallowchoice->action = null;
                $disallowchoice->add_class('activesetting');
                break;
        }
    } else {
        if ($activeenrolled) {
            switch ($subscriptionmode) {
                case FORUM_CHOOSESUBSCRIBE:
                    // 0
                    $notenode = $twfnode->add(get_string('subscriptionoptional', 'twf'));
                    break;
                case FORUM_FORCESUBSCRIBE:
                    // 1
                    $notenode = $twfnode->add(get_string('subscriptionforced', 'twf'));
                    break;
                case FORUM_INITIALSUBSCRIBE:
                    // 2
                    $notenode = $twfnode->add(get_string('subscriptionauto', 'twf'));
                    break;
                case FORUM_DISALLOWSUBSCRIBE:
                    // 3
                    $notenode = $twfnode->add(get_string('subscriptiondisabled', 'twf'));
                    break;
            }
        }
    }
    if ($cansubscribe) {
        if (\mod_twf\subscriptions::is_subscribed($USER->id, $twfobject, null, $PAGE->cm)) {
            $linktext = get_string('unsubscribe', 'twf');
        } else {
            $linktext = get_string('subscribe', 'twf');
        }
        $url = new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'sesskey' => sesskey()));
        $twfnode->add($linktext, $url, navigation_node::TYPE_SETTING);
        if (isset($discussionid)) {
            if (\mod_twf\subscriptions::is_subscribed($USER->id, $twfobject, $discussionid, $PAGE->cm)) {
                $linktext = get_string('unsubscribediscussion', 'twf');
            } else {
                $linktext = get_string('subscribediscussion', 'twf');
            }
            $url = new moodle_url('/mod/twf/subscribe.php', array('id' => $twfobject->id, 'sesskey' => sesskey(), 'd' => $discussionid, 'returnurl' => $PAGE->url->out()));
            $twfnode->add($linktext, $url, navigation_node::TYPE_SETTING);
        }
    }
    if (has_capability('mod/twf:viewsubscribers', $PAGE->cm->context)) {
        $url = new moodle_url('/mod/twf/subscribers.php', array('id' => $twfobject->id));
        $twfnode->add(get_string('showsubscribers', 'twf'), $url, navigation_node::TYPE_SETTING);
    }
    if ($enrolled && twf_tp_can_track_twfs($twfobject)) {
        // keep tracking info for users with suspended enrolments
        if ($twfobject->trackingtype == FORUM_TRACKING_OPTIONAL || !$CFG->twf_allowforcedreadtracking && $twfobject->trackingtype == FORUM_TRACKING_FORCED) {
            if (twf_tp_is_tracked($twfobject)) {
                $linktext = get_string('notracktwf', 'twf');
            } else {
                $linktext = get_string('tracktwf', 'twf');
            }
            $url = new moodle_url('/mod/twf/settracking.php', array('id' => $twfobject->id, 'sesskey' => sesskey()));
            $twfnode->add($linktext, $url, navigation_node::TYPE_SETTING);
        }
    }
    if (!isloggedin() && $PAGE->course->id == SITEID) {
        $userid = guest_user()->id;
    } else {
        $userid = $USER->id;
    }
    $hascourseaccess = $PAGE->course->id == SITEID || can_access_course($PAGE->course, $userid);
    $enablerssfeeds = !empty($CFG->enablerssfeeds) && !empty($CFG->twf_enablerssfeeds);
    if ($enablerssfeeds && $twfobject->rsstype && $twfobject->rssarticles && $hascourseaccess) {
        if (!function_exists('rss_get_url')) {
            require_once "{$CFG->libdir}/rsslib.php";
        }
        if ($twfobject->rsstype == 1) {
            $string = get_string('rsssubscriberssdiscussions', 'twf');
        } else {
            $string = get_string('rsssubscriberssposts', 'twf');
        }
        $url = new moodle_url(rss_get_url($PAGE->cm->context->id, $userid, "mod_twf", $twfobject->id));
        $twfnode->add($string, $url, settings_navigation::TYPE_SETTING, null, null, new pix_icon('i/rss', ''));
    }
}