Skip to content

Commit

Permalink
MDL-19670 mod_forum: Allow user to post a copy to all accessible groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Cooper committed Mar 12, 2015
1 parent f267b8b commit 2587313
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 50 deletions.
8 changes: 8 additions & 0 deletions mod/forum/classes/post_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ function definition() {

$contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
if ($contextcheck) {
if (has_capability('mod/forum:canposttomygroups', $modulecontext)
&& !isset($post->edit)) {
$mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
$mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
$mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
}

foreach ($groupdata as $grouptemp) {
$groupinfo[$grouptemp->id] = $grouptemp->name;
}
Expand All @@ -196,6 +203,7 @@ function definition() {
} else {
$submit_string = get_string('posttoforum', 'forum');
}

$this->add_action_buttons(true, $submit_string);

$mform->addElement('hidden', 'course');
Expand Down
10 changes: 10 additions & 0 deletions mod/forum/db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,15 @@
'frontpage' => CAP_ALLOW
)
),
'mod/forum:canposttomygroups' => array(

'captype' => 'write',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
);

3 changes: 3 additions & 0 deletions mod/forum/lang/en/forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
$string['forumauthorhidden'] = 'Author (hidden)';
$string['forumblockingalmosttoomanyposts'] = 'You are approaching the posting threshold. You have posted {$a->numposts} times in the last {$a->blockperiod} and the limit is {$a->blockafter} posts.';
$string['forumbodyhidden'] = 'This post cannot be viewed by you, probably because you have not posted in the discussion, the maximum editing time hasn\'t passed yet, the discussion has not started or the discussion has expired.';
$string['forum:canposttomygroups'] = 'Can post to all groups you have access to';
$string['forum:createattachment'] = 'Create attachments';
$string['forum:deleteanypost'] = 'Delete any posts (anytime)';
$string['forum:deleteownpost'] = 'Delete own posts (within deadline)';
Expand Down Expand Up @@ -371,6 +372,8 @@
$string['page-mod-forum-discuss'] = 'Forum module discussion thread page';
$string['parent'] = 'Show parent';
$string['parentofthispost'] = 'Parent of this post';
$string['posttomygroups'] = 'Post a copy to all groups';
$string['posttomygroups_help'] = 'Posts a copy of this message to all groups you have access to. Participants in groups you do not have access to will not see this post';
$string['prevdiscussiona'] = 'Previous discussion: {$a}';
$string['pluginadministration'] = 'Forum administration';
$string['pluginname'] = 'Forum';
Expand Down
110 changes: 61 additions & 49 deletions mod/forum/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,24 +849,10 @@
exit;

} else { // Adding a new discussion.
// Before we add this we must check that the user will not exceed the blocking threshold.
forum_check_blocking_threshold($thresholdwarning);

if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
print_error('cannotcreatediscussion', 'forum');
}
// If the user has access all groups capability let them choose the group.
if ($contextcheck) {
$fromform->groupid = $fromform->groupinfo;
}
if (empty($fromform->groupid)) {
$fromform->groupid = -1;
}

$fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;

$discussion = $fromform;
$discussion->name = $fromform->subject;
$discussion->name = $fromform->subject;

$newstopic = false;
if ($forum->type == 'news' && !$fromform->parent) {
Expand All @@ -875,50 +861,76 @@
$discussion->timestart = $fromform->timestart;
$discussion->timeend = $fromform->timeend;

$message = '';
if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {

$params = array(
'context' => $modcontext,
'objectid' => $discussion->id,
'other' => array(
'forumid' => $forum->id,
)
);
$event = \mod_forum\event\discussion_created::create($params);
$event->add_record_snapshot('forum_discussions', $discussion);
$event->trigger();
$allowedgroups = array();
$groupstopostto = array();

$timemessage = 2;
if (!empty($message)) { // if we're printing stuff about the file upload
$timemessage = 4;
// If we are posting a copy to all groups the user has access to.
if (isset($fromform->posttomygroups)) {
$allowedgroups = groups_get_activity_allowed_groups($cm);
$groupstopostto = array_keys($allowedgroups);
} else {
if ($contextcheck) {
$fromform->groupid = $fromform->groupinfo;
}

if ($fromform->mailnow) {
$message .= get_string("postmailnow", "forum");
$timemessage = 4;
} else {
$message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
$message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
if (empty($fromform->groupid)) {
$fromform->groupid = -1;
}
$groupstopostto = array($fromform->groupid);
}

if ($subscribemessage = forum_post_subscription($fromform, $forum, $discussion)) {
$timemessage = 6;
}
// Before we post this we must check that the user will not exceed the blocking threshold.
forum_check_blocking_threshold($thresholdwarning);

// Update completion status
$completion=new completion_info($course);
if($completion->is_enabled($cm) &&
($forum->completiondiscussions || $forum->completionposts)) {
$completion->update_state($cm,COMPLETION_COMPLETE);
foreach ($groupstopostto as $group) {
if (!forum_user_can_post_discussion($forum, $group, -1, $cm, $modcontext)) {
print_error('cannotcreatediscussion', 'forum');
}

redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
$discussion->groupid = $group;
$message = '';
if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {

} else {
print_error("couldnotadd", "forum", $errordestination);
$params = array(
'context' => $modcontext,
'objectid' => $discussion->id,
'other' => array(
'forumid' => $forum->id,
)
);
$event = \mod_forum\event\discussion_created::create($params);
$event->add_record_snapshot('forum_discussions', $discussion);
$event->trigger();

$timemessage = 2;
if (!empty($message)) { // If we're printing stuff about the file upload.
$timemessage = 4;
}

if ($fromform->mailnow) {
$message .= get_string("postmailnow", "forum");
$timemessage = 4;
} else {
$message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
$message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
}

if ($subscribemessage = forum_post_subscription($fromform, $forum, $discussion)) {
$timemessage = 6;
}
} else {
print_error("couldnotadd", "forum", $errordestination);
}
}

// Update completion status.
$completion = new completion_info($course);
if ($completion->is_enabled($cm) &&
($forum->completiondiscussions || $forum->completionposts)) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}

redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);

exit;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2014111000; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2015031200; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2014110400; // Requires this Moodle version
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)

0 comments on commit 2587313

Please sign in to comment.