public function test_subscription_modes() { global $DB; $this->resetAfterTest(true); // Create a course, with a twf. $course = $this->getDataGenerator()->create_course(); $options = array('course' => $course->id); $twf = $this->getDataGenerator()->create_module('twf', $options); \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_FORCESUBSCRIBE); $twf = $DB->get_record('twf', array('id' => $twf->id)); $this->assertEquals(FORUM_FORCESUBSCRIBE, \mod_twf\subscriptions::get_subscription_mode($twf)); $this->assertTrue(\mod_twf\subscriptions::is_forcesubscribed($twf)); $this->assertFalse(\mod_twf\subscriptions::is_subscribable($twf)); $this->assertFalse(\mod_twf\subscriptions::subscription_disabled($twf)); \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_DISALLOWSUBSCRIBE); $twf = $DB->get_record('twf', array('id' => $twf->id)); $this->assertEquals(FORUM_DISALLOWSUBSCRIBE, \mod_twf\subscriptions::get_subscription_mode($twf)); $this->assertTrue(\mod_twf\subscriptions::subscription_disabled($twf)); $this->assertFalse(\mod_twf\subscriptions::is_subscribable($twf)); $this->assertFalse(\mod_twf\subscriptions::is_forcesubscribed($twf)); \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_INITIALSUBSCRIBE); $twf = $DB->get_record('twf', array('id' => $twf->id)); $this->assertEquals(FORUM_INITIALSUBSCRIBE, \mod_twf\subscriptions::get_subscription_mode($twf)); $this->assertTrue(\mod_twf\subscriptions::is_subscribable($twf)); $this->assertFalse(\mod_twf\subscriptions::subscription_disabled($twf)); $this->assertFalse(\mod_twf\subscriptions::is_forcesubscribed($twf)); \mod_twf\subscriptions::set_subscription_mode($twf->id, FORUM_CHOOSESUBSCRIBE); $twf = $DB->get_record('twf', array('id' => $twf->id)); $this->assertEquals(FORUM_CHOOSESUBSCRIBE, \mod_twf\subscriptions::get_subscription_mode($twf)); $this->assertTrue(\mod_twf\subscriptions::is_subscribable($twf)); $this->assertFalse(\mod_twf\subscriptions::subscription_disabled($twf)); $this->assertFalse(\mod_twf\subscriptions::is_forcesubscribed($twf)); }
/** * 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); }
if (\mod_twf\subscriptions::unsubscribe_user($user->id, $twf, $context, true)) { redirect($returnto, get_string("nownotsubscribed", "twf", $info), 1); } else { print_error('cannotunsubscribe', 'twf', get_local_referer(false)); } } else { if (\mod_twf\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion, $context)) { $info->discussion = $discussion->name; redirect($returnto, get_string("discussionnownotsubscribed", "twf", $info), 1); } else { print_error('cannotunsubscribe', 'twf', get_local_referer(false)); } } } else { // subscribe if (\mod_twf\subscriptions::subscription_disabled($twf) && !has_capability('mod/twf:managesubscriptions', $context)) { print_error('disallowsubscribe', 'twf', get_local_referer(false)); } if (!has_capability('mod/twf:viewdiscussion', $context)) { print_error('noviewdiscussionspermission', 'twf', get_local_referer(false)); } if (is_null($sesskey)) { // We came here via link in email. $PAGE->set_title($course->shortname); $PAGE->set_heading($course->fullname); echo $OUTPUT->header(); $viewurl = new moodle_url('/mod/twf/view.php', array('f' => $id)); if ($discussionid) { $a = new stdClass(); $a->twf = format_string($twf->name); $a->discussion = format_string($discussion->name);
/** * Helper to determine whether the specified twf can be subscribed to. * * @param \stdClass $twf The record of the twf to test * @return bool */ public static function is_subscribable($twf) { return !\mod_twf\subscriptions::is_forcesubscribed($twf) && !\mod_twf\subscriptions::subscription_disabled($twf); }
$formheading = ''; if (!empty($parent)) { $heading = get_string("yourreply", "twf"); $formheading = get_string('reply', 'twf'); } else { if ($twf->type == 'qanda') { $heading = get_string('yournewquestion', 'twf'); } else { $heading = get_string('yournewtopic', 'twf'); } } $postid = empty($post->id) ? null : $post->id; $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 {
/** * 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', '')); } }