diff --git a/lang/en/moodle.php b/lang/en/moodle.php
index 24e37efc311..7361225dd43 100644
--- a/lang/en/moodle.php
+++ b/lang/en/moodle.php
@@ -559,6 +559,13 @@ $string['emailconfirmsent'] = '
An email should have been sent to your address
It contains easy instructions to complete your registration.
If you continue to have difficulty, contact the site administrator.
';
$string['emaildigest'] = 'Email digest type';
+$string['emaildigest_help'] = 'This is the daily digest setting that forums will use by default.
+
+* No digest - you will receive one e-mail per forum post;
+* Digest - complete posts - you will receive one digest e-mail per day containing the complete contents of each forum post;
+* Digest - subjects only - you will receive one digest e-mail per day containing just the subject of each forum post.
+
+You can also choose a different setting for each forum if you wish.';
$string['emaildigestcomplete'] = 'Complete (daily email with full posts)';
$string['emaildigestoff'] = 'No digest (single email per forum post)';
$string['emaildigestsubjects'] = 'Subjects (daily email with subjects only)';
diff --git a/mod/forum/backup/moodle2/backup_forum_stepslib.php b/mod/forum/backup/moodle2/backup_forum_stepslib.php
index 2ad3580c159..1a47641126d 100644
--- a/mod/forum/backup/moodle2/backup_forum_stepslib.php
+++ b/mod/forum/backup/moodle2/backup_forum_stepslib.php
@@ -70,6 +70,11 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
$subscription = new backup_nested_element('subscription', array('id'), array(
'userid'));
+ $digests = new backup_nested_element('digests');
+
+ $digest = new backup_nested_element('digest', array('id'), array(
+ 'userid', 'maildigest'));
+
$readposts = new backup_nested_element('readposts');
$read = new backup_nested_element('read', array('id'), array(
@@ -89,6 +94,9 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
$forum->add_child($subscriptions);
$subscriptions->add_child($subscription);
+ $forum->add_child($digests);
+ $digests->add_child($digest);
+
$forum->add_child($readposts);
$readposts->add_child($read);
@@ -118,6 +126,8 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
$subscription->set_source_table('forum_subscriptions', array('forum' => backup::VAR_PARENTID));
+ $digest->set_source_table('forum_digests', array('forum' => backup::VAR_PARENTID));
+
$read->set_source_table('forum_read', array('forumid' => backup::VAR_PARENTID));
$track->set_source_table('forum_track_prefs', array('forumid' => backup::VAR_PARENTID));
@@ -143,6 +153,8 @@ class backup_forum_activity_structure_step extends backup_activity_structure_ste
$subscription->annotate_ids('user', 'userid');
+ $digest->annotate_ids('user', 'userid');
+
$read->annotate_ids('user', 'userid');
$track->annotate_ids('user', 'userid');
diff --git a/mod/forum/backup/moodle2/restore_forum_stepslib.php b/mod/forum/backup/moodle2/restore_forum_stepslib.php
index c5b23867f90..2dfac63cf30 100644
--- a/mod/forum/backup/moodle2/restore_forum_stepslib.php
+++ b/mod/forum/backup/moodle2/restore_forum_stepslib.php
@@ -42,6 +42,7 @@ class restore_forum_activity_structure_step extends restore_activity_structure_s
$paths[] = new restore_path_element('forum_post', '/activity/forum/discussions/discussion/posts/post');
$paths[] = new restore_path_element('forum_rating', '/activity/forum/discussions/discussion/posts/post/ratings/rating');
$paths[] = new restore_path_element('forum_subscription', '/activity/forum/subscriptions/subscription');
+ $paths[] = new restore_path_element('forum_digest', '/activity/forum/digests/digest');
$paths[] = new restore_path_element('forum_read', '/activity/forum/readposts/read');
$paths[] = new restore_path_element('forum_track', '/activity/forum/trackedprefs/track');
}
@@ -149,6 +150,18 @@ class restore_forum_activity_structure_step extends restore_activity_structure_s
$newitemid = $DB->insert_record('forum_subscriptions', $data);
}
+ protected function process_forum_digest($data) {
+ global $DB;
+
+ $data = (object)$data;
+ $oldid = $data->id;
+
+ $data->forum = $this->get_new_parentid('forum');
+ $data->userid = $this->get_mappingid('user', $data->userid);
+
+ $newitemid = $DB->insert_record('forum_digests', $data);
+ }
+
protected function process_forum_read($data) {
global $DB;
diff --git a/mod/forum/db/install.xml b/mod/forum/db/install.xml
index 17ab2292bef..76dcc037896 100644
--- a/mod/forum/db/install.xml
+++ b/mod/forum/db/install.xml
@@ -1,5 +1,5 @@
-
@@ -120,6 +120,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
\ No newline at end of file
+
diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php
old mode 100644
new mode 100755
index 7603626aa8a..703e93f6156
--- a/mod/forum/db/upgrade.php
+++ b/mod/forum/db/upgrade.php
@@ -84,9 +84,30 @@ function xmldb_forum_upgrade($oldversion) {
// Moodle v2.5.0 release upgrade line.
// Put any upgrade step following this.
+ if ($oldversion < 2013071000) {
+ // Define table forum_digests to be created.
+ $table = new xmldb_table('forum_digests');
+ // Adding fields to table forum_digests.
+ $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+ $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('forum', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+ $table->add_field('maildigest', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '-1');
+
+ // Adding keys to table forum_digests.
+ $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+ $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
+ $table->add_key('forum', XMLDB_KEY_FOREIGN, array('forum'), 'forum', array('id'));
+ $table->add_key('forumdigest', XMLDB_KEY_UNIQUE, array('forum', 'userid', 'maildigest'));
+
+ // Conditionally launch create table for forum_digests.
+ if (!$dbman->table_exists($table)) {
+ $dbman->create_table($table);
+ }
+
+ // Forum savepoint reached.
+ upgrade_mod_savepoint(true, 2013071000, 'forum');
+ }
return true;
}
-
-
diff --git a/mod/forum/index.php b/mod/forum/index.php
index fc9903de13e..93b1656c483 100644
--- a/mod/forum/index.php
+++ b/mod/forum/index.php
@@ -68,9 +68,21 @@ $strunsubscribe = get_string('unsubscribe', 'forum');
$stryes = get_string('yes');
$strno = get_string('no');
$strrss = get_string('rss');
+$stremaildigest = get_string('emaildigest');
$searchform = forum_search_form($course);
+// Retrieve the list of forum digest options for later.
+$digestoptions = forum_get_user_digest_options();
+$digestoptions_selector = new single_select(new moodle_url('/mod/forum/maildigest.php',
+ array(
+ 'backtoindex' => 1,
+ )),
+ 'maildigest',
+ $digestoptions,
+ null,
+ '');
+$digestoptions_selector->method = 'post';
// Start of the table for General Forums
@@ -94,6 +106,9 @@ $can_subscribe = is_enrolled($coursecontext);
if ($can_subscribe) {
$generaltable->head[] = $strsubscribed;
$generaltable->align[] = 'center';
+
+ $generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
+ $generaltable->align[] = 'center';
}
if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
@@ -111,7 +126,13 @@ $table = new html_table();
// some special ones are not. These get placed in the general forums
// category with the forums in section 0.
-$forums = $DB->get_records('forum', array('course' => $course->id));
+$forums = $DB->get_records_sql("
+ SELECT f.*,
+ d.maildigest
+ FROM {forum} f
+ LEFT JOIN {forum_digests} d ON d.forum = f.id AND d.userid = ?
+ WHERE f.course = ?
+ ", array($USER->id, $course->id));
$generalforums = array();
$learningforums = array();
@@ -252,6 +273,14 @@ if ($generalforums) {
} else {
$row[] = '-';
}
+
+ $digestoptions_selector->url->param('id', $forum->id);
+ if ($forum->maildigest === null) {
+ $digestoptions_selector->selected = -1;
+ } else {
+ $digestoptions_selector->selected = $forum->maildigest;
+ }
+ $row[] = $OUTPUT->render($digestoptions_selector);
}
//If this forum has RSS activated, calculate it
@@ -297,6 +326,9 @@ if ($usetracking) {
if ($can_subscribe) {
$learningtable->head[] = $strsubscribed;
$learningtable->align[] = 'center';
+
+ $learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_forum');
+ $learningtable->align[] = 'center';
}
if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
@@ -390,6 +422,14 @@ if ($course->id != SITEID) { // Only real courses have learning forums
} else {
$row[] = '-';
}
+
+ $digestoptions_selector->url->param('id', $forum->id);
+ if ($forum->maildigest === null) {
+ $digestoptions_selector->selected = -1;
+ } else {
+ $digestoptions_selector->selected = $forum->maildigest;
+ }
+ $row[] = $OUTPUT->render($digestoptions_selector);
}
//If this forum has RSS activated, calculate it
diff --git a/mod/forum/lang/en/forum.php b/mod/forum/lang/en/forum.php
index 79a7bc03377..990b10939be 100644
--- a/mod/forum/lang/en/forum.php
+++ b/mod/forum/lang/en/forum.php
@@ -113,7 +113,8 @@ $string['deletedpost'] = 'The post has been deleted';
$string['deletedposts'] = 'Those posts have been deleted';
$string['deletesure'] = 'Are you sure you want to delete this post?';
$string['deletesureplural'] = 'Are you sure you want to delete this post and all replies? ({$a} posts)';
-$string['digestmailheader'] = 'This is your daily digest of new posts from the {$a->sitename} forums. To change your forum email preferences, go to {$a->userprefs}.';
+$string['digestmailheader'] = 'This is your daily digest of new posts from the {$a->sitename} forums. To change your default forum email preferences, go to {$a->userprefs}.';
+$string['digestmailpost'] = 'Change your forum digest preferences';
$string['digestmailprefs'] = 'your user profile';
$string['digestmailsubject'] = '{$a}: forum digest';
$string['digestmailtime'] = 'Hour to send digest emails';
@@ -142,6 +143,23 @@ $string['edit'] = 'Edit';
$string['editedby'] = 'Edited by {$a->name} - original submission {$a->date}';
$string['editedpostupdated'] = '{$a}\'s post was updated';
$string['editing'] = 'Editing';
+$string['emaildigestcompleteshort'] = 'Complete posts';
+$string['emaildigestdefault'] = 'Default ({$a})';
+$string['emaildigestoffshort'] = 'No digest';
+$string['emaildigestsubjectsshort'] = 'Subjects only';
+$string['emaildigesttype'] = 'Email digest options';
+$string['emaildigesttype_help'] = 'The type of notification that you will receive for each forum.
+
+* Default - follow the digest setting found in your user profile. If you update your profile, then that change will be reflected here too;
+* No digest - you will receive one e-mail per forum post;
+* Digest - complete posts - you will receive one digest e-mail per day containing the complete contents of each forum post;
+* Digest - subjects only - you will receive one digest e-mail per day containing just the subject of each forum post.
+';
+$string['emaildigestupdated'] = 'The e-mail digest option was changed to \'{$a->maildigesttitle}\' for the forum \'{$a->forum}\'. {$a->maildigestdescription}';
+$string['emaildigestupdated_default'] = 'Your default profile setting of \'{$a->maildigesttitle}\' was used for the forum \'{$a->forum}\'. {$a->maildigestdescription}.';
+$string['emaildigest_0'] = 'You will receive one e-mail per forum post.';
+$string['emaildigest_1'] = 'You will receive one digest e-mail per day containing the complete contents of each forum post.';
+$string['emaildigest_2'] = 'You will receive one digest e-mail per day containing the subject of each forum post.';
$string['emptymessage'] = 'Something was wrong with your post. Perhaps you left it blank, or the attachment was too big. Your changes have NOT been saved.';
$string['erroremptymessage'] = 'Post message cannot be empty';
$string['erroremptysubject'] = 'Post subject cannot be empty.';
@@ -209,6 +227,7 @@ $string['introsocial'] = 'An open forum for chatting about anything you want to'
$string['introteacher'] = 'A forum for teacher-only notes and discussion';
$string['invalidaccess'] = 'This page was not accessed correctly';
$string['invaliddiscussionid'] = 'Discussion ID was incorrect or no longer exists';
+$string['invaliddigestsetting'] = 'An invalid mail digest setting was provided';
$string['invalidforcesubscribe'] = 'Invalid force subscription mode';
$string['invalidforumid'] = 'Forum ID was incorrect';
$string['invalidparentpostid'] = 'Parent post ID was incorrect';
@@ -347,6 +366,7 @@ $string['replyforum'] = 'Reply to forum';
$string['replytouser'] = 'Use email address in reply';
$string['resetforums'] = 'Delete posts from';
$string['resetforumsall'] = 'Delete all posts';
+$string['resetdigests'] = 'Delete all per-user forum digest preferences';
$string['resetsubscriptions'] = 'Delete all forum subscriptions';
$string['resettrackprefs'] = 'Delete all forum tracking preferences';
$string['rsssubscriberssdiscussions'] = 'RSS feed of discussions';
diff --git a/mod/forum/lib.php b/mod/forum/lib.php
index 0dac55b172f..8a5a46b449d 100644
--- a/mod/forum/lib.php
+++ b/mod/forum/lib.php
@@ -264,6 +264,10 @@ function forum_delete_instance($id) {
}
}
+ if (!$DB->delete_records('forum_digests', array('forum' => $forum->id))) {
+ $result = false;
+ }
+
if (!$DB->delete_records('forum_subscriptions', array('forum'=>$forum->id))) {
$result = false;
}
@@ -457,6 +461,17 @@ function forum_cron() {
$endtime = $timenow - $CFG->maxeditingtime;
$starttime = $endtime - 48 * 3600; // Two days earlier
+ // Get the list of forum subscriptions for per-user per-forum maildigest settings.
+ $digestsset = $DB->get_recordset('forum_digests', null, '', 'id, userid, forum, maildigest');
+ $digests = array();
+ foreach ($digestsset as $thisrow) {
+ if (!isset($digests[$thisrow->forum])) {
+ $digests[$thisrow->forum] = array();
+ }
+ $digests[$thisrow->forum][$thisrow->userid] = $thisrow->maildigest;
+ }
+ $digestsset->close();
+
if ($posts = forum_get_unmailed_posts($starttime, $endtime, $timenow)) {
// Mark them all now as being mailed. It's unlikely but possible there
// might be an error later so that a post is NOT actually mailed out,
@@ -666,7 +681,9 @@ function forum_cron() {
// OK so we need to send the email.
// Does the user want this post in a digest? If so postpone it for now.
- if ($userto->maildigest > 0) {
+ $maildigest = forum_get_user_maildigest_bulk($digests, $userto, $forum->id);
+
+ if ($maildigest > 0) {
// This user wants the mails to be in digest form
$queue = new stdClass();
$queue->userid = $userto->id;
@@ -991,7 +1008,8 @@ function forum_cron() {
$userfrom->customheaders = array ("Precedence: Bulk");
- if ($userto->maildigest == 2) {
+ $maildigest = forum_get_user_maildigest_bulk($digests, $userto, $forum->id);
+ if ($maildigest == 2) {
// Subjects and link only
$posttext .= "\n";
$posttext .= $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
@@ -1015,11 +1033,14 @@ function forum_cron() {
}
}
}
+ $footerlinks = array();
if ($canunsubscribe) {
- $posthtml .= "\n";
+ $footerlinks[] = "wwwroot/mod/forum/subscribe.php?id=$forum->id\">" . get_string("unsubscribe", "forum") . "";
} else {
- $posthtml .= "\n".get_string("everyoneissubscribed", "forum")."
";
+ $footerlinks[] = get_string("everyoneissubscribed", "forum");
}
+ $footerlinks[] = "" . get_string("digestmailpost", "forum") . '';
+ $posthtml .= "\n" . implode(' ', $footerlinks) . '
';
$posthtml .= '
';
}
$posthtml .= '