Skip to content

Commit 8d4638d

Browse files
committed
add table for digestmail to moodleoverflow db, add task to send the mail
1 parent 92d0ba3 commit 8d4638d

File tree

7 files changed

+163
-8
lines changed

7 files changed

+163
-8
lines changed

classes/task/send_daily_mail.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Task schedule configuration for the plugintype_pluginname plugin.
19+
*
20+
* @package mod_moodleoverflow
21+
* @copyright 2023, Tamaro Walter
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
namespace mod_moodleoverflow\task;
25+
defined('MOODLE_INTERNAL') || die();
26+
/**
27+
* This task sends a daily mail of unread posts
28+
*/
29+
class send_daily_mail extends \core\task\scheduled_task {
30+
31+
/**
32+
* Return the task's name as shown in admin screens.
33+
*
34+
* @return string
35+
*/
36+
public function get_name() {
37+
return get_string('tasksenddailymail', 'mod_moodleoverflow');
38+
}
39+
40+
/**
41+
* Execute the task.
42+
*/
43+
public function execute() {
44+
global $DB;
45+
// Call your own api
46+
$users = $DB->get_records_sql('SELECT DISTINCT userid FROM {moodleoverflow_mail_info}');
47+
foreach($users as $user) {
48+
$userdata = $DB->get_records('moodleoverflow_mail_info', array('userid' => $user->userid), 'courseid, forumid'); //order by courseid
49+
$mail = array();
50+
foreach($userdata as $row) {
51+
$currentcourse = $DB->get_record('course', array('id' => $row->courseid), 'fullname');
52+
$currentforum = $DB->get_record('moodleoverflow', array('id' => $row->forumid), 'name');
53+
$discussion = $DB->get_record('moodleoverflow_discussions', array('id' => $row->forumdiscussionid), 'name');
54+
$unreadposts = $row->numberofposts;
55+
$string = get_string('digestunreadpost', 'mod_moodleoverflow', array('currentcourse' => $currentcourse->fullname,
56+
'currentforum' => $currentforum->name,
57+
'discussion' => $discussion->name,
58+
'unreadposts' => $unreadposts));
59+
array_push($mail,$string);
60+
}
61+
$message = implode('<br>', $mail);
62+
mtrace($message);
63+
//send message to user
64+
$userto = $DB->get_record('user', array('id' => $user->userid));
65+
$from = \core_user::get_noreply_user();
66+
$subject = get_string('tasksenddailymail', 'mod_moodleoverflow');
67+
68+
email_to_user($userto,$from, $subject,$message);
69+
}
70+
}
71+
}

db/install.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,22 @@
175175
<KEY NAME="moodleoverflowid" TYPE="foreign" FIELDS="moodleoverflowid" REFTABLE="moodleoverflow" REFFIELDS="id"/>
176176
</KEYS>
177177
</TABLE>
178+
<TABLE NAME="moodleoverflow_mail_info" COMMENT="represent the content of the digest mail">
179+
<FIELDS>
180+
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
181+
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true"/>
182+
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true"/>
183+
<FIELD NAME="forumid" TYPE="int" LENGTH="10" NOTNULL="true"/>
184+
<FIELD NAME="forumdiscussionid" TYPE="int" LENGTH="10" NOTNULL="true"/>
185+
<FIELD NAME="numberofposts" TYPE="int" LENGTH="10" NOTNULL="true"/>
186+
</FIELDS>
187+
<KEYS>
188+
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
189+
<KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id"/>
190+
<KEY NAME="courseid" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id"/>
191+
<KEY NAME="forumid" TYPE="foreign" FIELDS="forumid" REFTABLE="moodleoverflow" REFFIELDS="id"/>
192+
<KEY NAME="forumdiscussionid" TYPE="foreign" FIELDS="forumdiscussionid" REFTABLE="moodleoverflow_discussions" REFFIELDS="id"/>
193+
</KEYS>
194+
</TABLE>
178195
</TABLES>
179196
</XMLDB>

db/tasks.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@
4848
'dayofweek' => '*'
4949
),
5050

51+
// Clean old read records.
52+
array(
53+
'classname' => 'mod_moodleoverflow\task\send_daily_mail',
54+
'blocking' => 0,
55+
'minute' => '0',
56+
'hour' => '17',
57+
'day' => '*',
58+
'month' => '*',
59+
'dayofweek' => '*'
60+
)
5161
);

db/upgrade.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,31 @@ function xmldb_moodleoverflow_upgrade($oldversion) {
246246
upgrade_mod_savepoint(true, 2022110700, 'moodleoverflow');
247247
}
248248

249+
if($oldversion < 2023022400) {
250+
//Table for information of digest mail.
251+
$table = new xmldb_table('moodleoverflow_mail_info');
252+
253+
// Adding fields to table moodleoverflow_mail_info.
254+
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
255+
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
256+
$table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
257+
$table->add_field('forumid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
258+
$table->add_field('forumdiscussionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
259+
$table->add_field('numberofposts', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
260+
261+
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
262+
$table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
263+
$table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
264+
$table->add_key('forumid', XMLDB_KEY_FOREIGN, array('forumid'), 'moodleoverflow', array('id'));
265+
$table->add_key('forumdiscussionid', XMLDB_KEY_FOREIGN, array('forumdiscussionid'), 'moodleoverflow_discussions', array('id'));
266+
267+
// Conditionally launch create table for moodleoverflow_mail_info.
268+
if (!$dbman->table_exists($table)) {
269+
$dbman->create_table($table);
270+
}
271+
272+
// Moodleoverflow savepoint reached.
273+
upgrade_mod_savepoint(true, 2023022400, 'moodleoverflow');
274+
}
249275
return true;
250276
}

lang/en/moodleoverflow.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
$string['crontask'] = 'Moodleoverflow maintenance jobs';
211211
$string['taskcleanreadrecords'] = 'Moodleoverflow maintenance job to clean old read records';
212212
$string['tasksendmails'] = 'Moodleoverflow maintenance job to send mails';
213+
$string['tasksenddailymail'] = 'Moodleoverflow job to send a daily mail of unread post';
213214
$string['nopermissiontosubscribe'] = 'You do not have the permission to view subscribers';
214215
$string['subscribeenrolledonly'] = 'Sorry, only enrolled users are allowed to subscribe to post notifications.';
215216
$string['everyonecannowchoose'] = 'Everyone can now choose to be subscribed';
@@ -453,3 +454,8 @@
453454
$string['your_post_was_rejected'] = 'Your post was rejected.';
454455
$string['your_post_was_rejected_with_reason'] = 'Your post was rejected with the following reason:';
455456
$string['original_post'] = 'Original post';
457+
458+
459+
// Daily mail message.
460+
$string['digestunreadpost'] = 'Course: {$a->currentcourse} -> {$a->currentforum}, Topic: {$a->discussion} has {$a->unreadposts} unread posts.';
461+

lib.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,7 @@ function moodleoverflow_send_mails() {
745745

746746
// Tracing information.
747747
mtrace('Processing user ' . $userto->id);
748-
749-
// Check if user wants a resume.
750-
$usermailsetting = $DB->get_record('user', array('id' => $userto->id), $fields = 'maildigest');
751-
if($usermailsetting->maildigest != 0) {
752-
mtrace('mailsetting ' . $usermailsetting->maildigest);
753-
continue;
754-
}
748+
mtrace('Mail setting of user: ' . $userto->maildigest);
755749
// Initiate the user caches to save memory.
756750
$userto = clone($userto);
757751
$userto->ciewfullnames = array();
@@ -771,12 +765,43 @@ function moodleoverflow_send_mails() {
771765
// Loop through all posts of this users.
772766
foreach ($posts as $postid => $post) {
773767

768+
769+
774770
// Initiate variables for the post.
775771
$discussion = $discussions[$post->discussion];
776772
$moodleoverflow = $moodleoverflows[$discussion->moodleoverflow];
777773
$course = $courses[$moodleoverflow->course];
778774
$cm =& $coursemodules[$moodleoverflow->id];
779775

776+
/**
777+
* Check if user wants a resume
778+
* in this case: make a new dataset in "moodleoverflow_mail_info" to save the posts data
779+
* Dataset from moodleoverflow_mail_info will be send later in a mail
780+
*/
781+
$usermailsetting = $userto->maildigest;
782+
if($usermailsetting != 0) {
783+
$dataobject = new stdClass();
784+
$dataobject->userid = $userto->id;
785+
$dataobject->courseid = $course->id;
786+
$dataobject->forumid = $moodleoverflow->id;
787+
$dataobject->forumdiscussionid = $discussion->id;
788+
$record = $DB->get_record('moodleoverflow_mail_info', array( 'userid' => $dataobject->userid,
789+
'courseid' => $dataobject->courseid,
790+
'forumid' => $dataobject->forumid,
791+
'forumdiscussionid' => $dataobject->forumdiscussionid), 'numberofposts, id');
792+
if(is_object($record)) {
793+
$dataset = $record;
794+
$dataobject->numberofposts = $dataset->numberofposts + 1;
795+
$dataobject->id = $dataset->id;
796+
$DB->update_record('moodleoverflow_mail_info', $dataobject);
797+
}
798+
else {
799+
$dataobject->numberofposts = 1;
800+
$DB->insert_record('moodleoverflow_mail_info', $dataobject);
801+
}
802+
continue;
803+
}
804+
780805
// Check whether the user is subscribed.
781806
if (!isset($subscribedusers[$moodleoverflow->id][$userto->id])) {
782807
continue;

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
defined('MOODLE_INTERNAL') || die();
2929

3030
$plugin->component = 'mod_moodleoverflow';
31-
$plugin->version = 2022120600;
31+
$plugin->version = 2023022401;
3232
$plugin->release = 'v4.1-r1';
3333
$plugin->requires = 2020061500; // Requires Moodle 3.9+.
3434
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)