Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ForumNG delete and email option #2120

  • Loading branch information...
commit 5b4d5a671fbc3b7767001e5adb5626743f6b40f5 1 parent 057f4d3
Bryan Waddington authored sammarshallou committed
View
136 deletepost.php
@@ -34,6 +34,12 @@
// Delete or undelete
$delete = optional_param('delete', 1, PARAM_INT);
+// Email author
+$email = optional_param('email', 0, PARAM_INT);
+
+// Were the posts expanded in non js display
+$expand = optional_param('expand', 0, PARAM_INT);
+
$pageparams = array('p'=>$postid);
if ($cloneid) {
$pageparams['clone'] = $cloneid;
@@ -44,6 +50,9 @@
if ($ajax) {
$pageparams['ajax'] = $ajax;
}
+if ($expand) {
+ $pageparams['expand'] = $expand;
+}
// Get post
$post = mod_forumng_post::get_from_id($postid, $cloneid);
@@ -73,7 +82,7 @@
}
// Is this the actual delete?
-if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+if ($_SERVER['REQUEST_METHOD'] == 'POST' && $email != 1) {
// Delete or undelete the post
if ($delete) {
$post->delete();
@@ -91,28 +100,125 @@
if (!$delete || has_capability('mod/forumng:editanypost', $forum->get_context())) {
$postid = '#p' . $post->get_id();
}
+
redirect('discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN) . $postid);
}
+if ($email) {
+ require_once('deletepost_form.php');
+
+ $urlparams = array('p' => $postid, 'delete' => $delete, 'email' => $email);
+ if ($cloneid) {
+ $urlparams['clone'] = $cloneid;
+ }
+
+ $url = new moodle_url("{$CFG->wwwroot}/mod/forumng/deletepost.php", $urlparams);
+ $mform = new mod_forumng_deletepost_form($url);
+
+ // set up the expand url param so it gets added to the redirects
+ $expandparam = ($expand)? $expandparam = '&expand='.$expand : '';
+
+ if ($mform->is_cancelled()) {
+ //form is cancelled, redirect back to the discussion
+ redirect('discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN) . $expandparam);
+
+ } else if ($mform->is_submitted()) {
+ // Delete the post
+ $post->delete();
+
+ // Get the form data and set up the email
+ $submitted = $mform->get_data();
+ $messagetext = $submitted->message['text'];
+ $copyself = (isset($submitted->copyself))? true : false;
+ $user = $post->get_user();
+ $from = $USER;
+ $subject = get_string('deletedforumpost', 'forumng');
+
+ // Always send HTML version
+ $user->mailformat = 1;
+ $messagehtml = $out->deletion_email(text_to_html($messagetext));
+
+ // send an email to the author of the post
+ if (!email_to_user($user, $from, $subject, '', $messagehtml)) {
+ print_error(get_string('emailerror', 'forumng'));
+ }
+
+ // send a copy so self
+ if ($copyself) {
+ $user = $USER;
+ $user->mailformat = 1; // Always send HTML version
+ $subject = strtoupper(get_string('copy')) . ' - '. $subject;
+ if (!email_to_user($user, $from, $subject, '', $messagehtml)) {
+ print_error(get_string('emailerror', 'forumng'));
+ }
+ }
+
+ // redirect back to the discussion.
+ redirect('discuss.php?' . $discussion->get_link_params(mod_forumng::PARAM_PLAIN) . $expandparam);
+ }
+}
+
// Confirm page. Work out navigation for header
print $out->header();
-// Show confirm option
-if ($delete) {
- $confirmstring = get_string('confirmdelete', 'forumng');
- if ($post->is_root_post()) {
- $confirmstring .= ' ' . get_string('confirmdelete_nodiscussion', 'forumng');
- }
+// Include forum JS
+$forum->print_js($cm->id, true);
+
+if ($email) {
+ // prepare the object for the get_string
+ $emailmessage = new stdClass;
+ $emailmessage->subject = $post->get_effective_subject(true);
+ $emailmessage->firstname = $USER->firstname;
+ $emailmessage->lastname = $USER->lastname;
+ $emailmessage->course = $COURSE->fullname;
+ $emailmessage->forum = $post->get_forum()->get_name();
+
+ $formdata = new stdClass;
+
+ // Use the plain
+ $messagetext = get_string('emailcontentplain', 'forumng', $emailmessage);
+
+ $formdata->message['text'] = $messagetext;
+ $formdata->expand = $expand;
+
+ $mform->set_data($formdata);
+ $mform->display();
+ // output the html for use when JS is enabled
+ echo $out->delete_form_html(get_string('emailcontenthtml', 'forumng', $emailmessage));
} else {
- $confirmstring = get_string('confirmundelete', 'forumng');
+ // Show confirm option
+ if ($delete) {
+ $confirmstring = get_string('confirmdelete', 'forumng');
+ if ($post->is_root_post()) {
+ $confirmstring .= ' ' . get_string('confirmdelete_nodiscussion', 'forumng');
+ }
+
+ print $out->confirm_three_button($confirmstring,
+ new single_button(new moodle_url('/mod/forumng/deletepost.php',
+ array('p'=>$post->get_id(), 'delete'=>$delete,
+ 'clone'=>$cloneid, 'email' => 1, 'expand'=>$expand)),
+ $delete ? get_string('deleteemailpostbutton', 'forumng') :
+ get_string('undelete', 'forumng'), 'post'),
+ new single_button(new moodle_url('/mod/forumng/deletepost.php',
+ array('p'=>$post->get_id(), 'delete'=>$delete,
+ 'clone'=>$cloneid, 'expand'=>$expand)),
+ $delete ? get_string('delete') : get_string('undelete', 'forumng'), 'post'),
+ new single_button(new moodle_url('/mod/forumng/discuss.php',
+ array('d'=>$discussion->get_id(), 'clone'=>$cloneid, 'expand'=>$expand)),
+ get_string('cancel'), 'get'));
+ } else {
+ $confirmstring = get_string('confirmundelete', 'forumng');
+ print $out->confirm($confirmstring,
+ new single_button(new moodle_url('/mod/forumng/deletepost.php',
+ array('p'=>$post->get_id(), 'delete'=>$delete,
+ 'clone'=>$cloneid, 'expand'=>$expand)),
+ $delete ? get_string('delete') : get_string('undelete', 'forumng'), 'post'),
+ new single_button(new moodle_url('/mod/forumng/discuss.php',
+ array('d'=>$discussion->get_id(), 'clone'=>$cloneid, 'expand'=>$expand)),
+ get_string('cancel'), 'get'));
+ }
+
}
-print $out->confirm($confirmstring,
- new single_button(new moodle_url('/mod/forumng/deletepost.php',
- array('p'=>$post->get_id(), 'delete'=>$delete, 'clone'=>$cloneid)),
- $delete ? get_string('delete') : get_string('undelete', 'forumng'), 'post'),
- new single_button(new moodle_url('/mod/forumng/discuss.php',
- array('d'=>$discussion->get_id(), 'clone'=>$cloneid)),
- get_string('cancel'), 'get'));
// Print post
print $post->display(true,
View
53 deletepost_form.php
@@ -0,0 +1,53 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+require_once($CFG->libdir.'/formslib.php');
+
+/**
+ * Form for sending an email to the author of a post when deleting
+ * @package mod
+ * @subpackage forumng
+ * @copyright 2012 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class mod_forumng_deletepost_form extends moodleform {
+
+ public function definition() {
+ $mform =& $this->_form;
+
+ $expand = $this->_customdata['expand'];
+
+ //header
+ $mform->addElement('header', 'general', get_string('deleteandemail', 'forumng'));
+
+ //Message box
+ $mform->addElement('editor', 'message',
+ get_string('emailmessage', 'forumng'), array('size'=>'64', 'id' => 'id_forumng_delete_msg'));
+ $mform->setType('message', PARAM_RAW);
+ $mform->addRule('message', null, 'required', null, 'client');
+
+ //Send a copy to self
+ $mform->addElement('checkbox', 'copyself', get_string('copytoself', 'forumng'));
+
+ //keep the expand status
+ $mform->addElement('hidden', 'expand', $expand);
+
+ // add some buttons
+ $this->add_action_buttons(true, get_string('sendanddelete', 'forumng'));
+
+ }
+
+}
View
27 lang/en/forumng.php
@@ -308,7 +308,32 @@
$string['historypage'] = 'History: {$a}';
$string['currentpost'] = 'Current version of post';
$string['olderversions'] = 'Older versions (most recent first)';
-$string['deletepostbutton'] = 'Delete post';
+$string['deleteemailpostbutton'] = 'Delete and email';
+$string['deleteandemail'] = 'Delete and email author';
+$string['emailmessage'] = 'Message';
+$string['emailcontentplain'] = 'This is a notification to advise you that your forum post with the '.
+'following details has been deleted by \'{$a->firstname} {$a->lastname}\':
+
+Subject: {$a->subject}
+Forum: {$a->forum}
+Module: {$a->course}';
+$string['emailcontenthtml'] = 'This is a notification to advise you that your forum post with the '.
+'following details has been deleted by \'{$a->firstname} {$a->lastname}\':<br />
+<br />
+Subject: {$a->subject}<br />
+Forum: {$a->forum}<br />
+Module: {$a->course}';
+$string['emailcontentnonjs'] = 'This is a notification to advise you that your forum post with the '.
+'following details has been deleted by \'{$a->firstname} {$a->lastname}\':
+
+Subject: {$a->subject}
+Forum: {$a->forum}
+Module: {$a->course}';
+$string['copytoself'] = 'Send a copy to yourself';
+$string['deletedforumpost'] = 'Your post has been deleted';
+$string['emailerror'] = 'There was an error sending the email';
+$string['sendanddelete'] = 'Send and delete';
+$string['deletepostbutton'] = 'Delete';
$string['undeletepostbutton'] = 'Undelete post';
$string['averagerating'] = 'Average rating: {$a->avg} (from {$a->num})';
$string['yourrating'] = 'Your rating:';
View
1  mod_forumng.php
@@ -3642,6 +3642,7 @@ public function print_js($cmid=0, $ajaxattachments=false) {
'jserr_attachments' => null,
'confirmdelete' => null,
'confirmundelete' => null,
+ 'deleteemailpostbutton' => null,
'deletepostbutton' => null,
'undeletepostbutton' => null,
'js_nratings' => null,
View
34 module.js
@@ -105,6 +105,10 @@ M.mod_forumng = {
this.print_page();
return;
}
+ if (document.getElementById('page-mod-forumng-deletepost')) {
+ this.init_deletepost();
+ return;
+ }
},
/**
@@ -198,6 +202,16 @@ M.mod_forumng = {
},
/**
+ * Initialises all JavaScript for the deletepost page.
+ */
+ init_deletepost : function() {
+ // if JS is enabled then we can copy the html version of the text to
+ // the textarea used by tinymce, otherwise plain text is used by default.
+ var messagehtml = this.Y.one('#delete-form-html').getContent();
+ this.Y.one('#id_forumng_delete_msg').set('innerHTML', messagehtml);
+ },
+
+ /**
* Initialises 'content' i.e. posts and related. Can be called on the whole page or on
* a single post.
* @param node Node to run on (e.g. document node or a post div)
@@ -1455,12 +1469,12 @@ M.mod_forumng = {
if (this.are_links_disabled(link)) {
return;
}
- this.confirm(
- undelete ? M.str.forumng.confirmundelete : M.str.forumng.confirmdelete,
- undelete ? M.str.forumng.undeletepostbutton : M.str.forumng.deletepostbutton,
- M.str.moodle.cancel, link.post,
- function() {
- var cfg = {
+ var deleteandemail = function() {
+ window.location = 'deletepost.php' + '?p=' + link.postid + M.mod_forumng.cloneparam +
+ '&delete=1' + '&ajax=1' + '&email=1'
+ };
+ var deleteonly = function() {
+ var cfg = {
method: 'POST',
data: 'p=' + link.postid + M.mod_forumng.cloneparam +
'&delete=' + (undelete ? 0 : 1) + '&ajax=1',
@@ -1482,7 +1496,13 @@ M.mod_forumng = {
link.get('parentNode').appendChild(link.loader);
var linkregion = M.mod_forumng.Y.DOM.region(M.mod_forumng.Y.Node.getDOMNode(link));
link.loader.setXY([linkregion.right + 3, linkregion.top]);
- });
+ };
+ this.confirm(
+ undelete ? M.str.forumng.confirmundelete : M.str.forumng.confirmdelete,
+ undelete ? M.str.forumng.undeletepostbutton : new Array(M.str.forumng.deleteemailpostbutton,M.str.forumng.deletepostbutton),
+ M.str.moodle.cancel, link.post,
+ undelete ? deleteonly : new Array(deleteandemail, deleteonly)
+ );
}, this);
},
View
62 renderer.php
@@ -1483,4 +1483,66 @@ public function render_search_form($querytext, $linkfields, $help='') {
$out .= html_writer::end_tag('form');
return $out;
}
+
+ /**
+ * Print a message along with three buttons buttoneone/buttontwo/Cancel
+ *
+ * If a string or moodle_url is given instead of a single_button, method defaults to post.
+ *
+ * @param string $message The question to ask the user.
+ * @param single_button $buttonone The single_button component representing the buttontwo response.
+ * @param single_button $buttontwo The single_button component representing the buttontwo response.
+ * @param single_button $cancel The single_button component representing the Cancel response.
+ * @return string HTML fragment
+ */
+ public function confirm_three_button($message, $buttonone, $buttontwo, $cancel) {
+ if (!($buttonone instanceof single_button)) {
+ throw new coding_exception('The buttonone param must be an instance of a single_button.');
+ }
+
+ if (!($buttontwo instanceof single_button)) {
+ throw new coding_exception('The buttontwo param must be an instance of a single_button.');
+ }
+
+ if (!($cancel instanceof single_button)) {
+ throw new coding_exception('The cancel param must be an instance of a single_button.');
+ }
+
+ $output = $this->box_start('generalbox', 'notice');
+ $output .= html_writer::tag('p', $message);
+ $buttons = $this->render($buttonone) . $this->render($buttontwo) . $this->render($cancel);
+ $output .= html_writer::tag('div', $buttons, array('class' => 'buttons'));
+ $output .= $this->box_end();
+ return $output;
+ }
+
+ /**
+ * Compiles the html message content for the rejection email.
+ *
+ * @param object $group The details of one group
+ * @param string $coursename
+ * @return string HTML
+ */
+ public function deletion_email($messagetext) {
+ $out = '';
+ $out .= html_writer::start_tag('html');
+ $out .= html_writer::start_tag('body');
+ $out .= $messagetext;
+ $out .= html_writer::end_tag('body');
+ $out .= html_writer::end_tag('html');
+
+ return $out;
+ }
+
+ /**
+ * Compiles the html message content for the rejection email.
+ *
+ * @param object $group The details of one group
+ * @param string $coursename
+ * @return string HTML
+ */
+ public function delete_form_html($messagehtml) {
+ return html_writer::tag('div', htmlentities($messagehtml, ENT_QUOTES),
+ array('id' => 'delete-form-html'));
+ }
}
View
5 styles.css
@@ -910,3 +910,8 @@ padding-top: 1em;
.forumng-manualmark .iconhelp {
vertical-align: -3px;
}
+
+
+#delete-form-html {
+ display: none;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.