Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emailfrom #63

Open
wants to merge 23 commits into
base: MOODLE_400_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:

addons:
firefox: "47.0.1"
postgresql: "9.4"
postgresql: "9.5"
apt:
packages:
- openjdk-8-jre-headless
Expand Down
2 changes: 2 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<FIELD NAME="remindercount" TYPE="int" LENGTH="3" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="how many times to send the email"/>
<FIELD NAME="suppresstarget" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="ID of module that, if completed, should prevent an email being sent out."/>
<FIELD NAME="emaildelay" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="604800" SEQUENCE="false" COMMENT="Time setting related to email"/>
<FIELD NAME="emailfrom" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="ID of user to use as sender, 0 for support user, 1 for default teacher."/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it makes me nervous to see xmldb files changed that don't include a version bump, it makes me wonder if this is hand-rolled or if you have used xmldb editor - can you please make sure you have used xmldb editor to add these fields to the install.xml file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Dan,

I used XMLDB editor but my dev site is not connected to the github repo, so I had to copy and paste the updated file from there to here. I don't have a lot of experience with the XMLDB editor so I'm not sure how to do it in a better way, hope that clears things up?

<FIELD NAME="instantmessage" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Notification type, 0 for email, 1 for instant message"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
15 changes: 15 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,20 @@ function xmldb_reengagement_upgrade($oldversion=0) {
upgrade_mod_savepoint(true, 2017102001, 'reengagement');
}

// Add emailfrom and instantmessage fields.
if ($oldversion < 2020022202) {

$table = new xmldb_table('reengagement');
$field = new xmldb_field('emailfrom', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('instantmessage', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
upgrade_mod_savepoint(true, 2020022202, 'reengagement');
}

return true;
}
8 changes: 8 additions & 0 deletions lang/en/reengagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
$string['emailcontentmanagerdefaultvalue'] = 'This is a reminder notification from course %courseshortname%, regarding user %userfirstname% %userlastname%.';
$string['emaildelay'] = 'Notification delay';
$string['emaildelay_help'] = 'When module is set to notify users "after delay", this setting controls how long the delay is.';
$string['emailfrom'] = 'Send notification from';
$string['emailfrom_help'] = 'The user to send the message from, pending permissions check. Default teacher is the first teacher in the course, will send from support user if no teachers are found.';
$string['emailfromsupport'] = 'Support User';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moodle uses sentance case.

Support User -> Support user.
Default Teacher -> Default teacher

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

$string['emailfromteacher'] = 'Default Teacher';
$string['emailrecipient'] = 'Notify recipient(s)';
$string['emailrecipient_help'] = 'When a notification needs to be sent out to prompt a user\'s re-engagement with the course, this setting controls if a notification is sent to the user, their manager(s), or both.';
$string['emailsubject'] = 'Notification subject (User)';
Expand Down Expand Up @@ -88,6 +92,10 @@
$string['nochangenoaccess'] = 'No change (user has not accessed course)';
$string['noemailattimex'] = 'Message scheduled for {$a} will not be sent because you have completed the target activity';
$string['nosuppresstarget'] = 'No target activity selected';
$string['notificationemail'] = 'Email';
$string['notificationim'] = 'Instant message';
$string['notificationtype'] = 'Notification type';
$string['notificationtype_help'] = 'Send notifications as an email or as a Moodle instant message. Third party notifications always send as emails.';
$string['oncompletion'] = 'On reengagement completion';
$string['receiveemailattimex'] = 'Message will be sent on {$a}.';
$string['receiveemailattimexunless'] = 'Message will be sent on {$a} unless you complete target activity.';
Expand Down
79 changes: 63 additions & 16 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
define('REENGAGEMENT_RECIPIENT_MANAGER', 1);
define('REENGAGEMENT_RECIPIENT_BOTH', 2);

define('REENGAGEMENT_EMAILFROM_SUPPORT', 0);
define('REENGAGEMENT_EMAILFROM_TEACHER', 1);

define('REENGAGEMENT_NOTIFICATION_EMAIL', 0);
define('REENGAGEMENT_NOTIFICATION_IM', 1);

/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
Expand Down Expand Up @@ -546,22 +552,29 @@ function reengagement_email_user($reengagement, $inprogress) {
* @param object $reengagement database record
*/
function reengagement_send_notification($userto, $subject, $messageplain, $messagehtml, $reengagement) {
$eventdata = new \core\message\message();
$eventdata->courseid = $reengagement->courseid;
$eventdata->modulename = 'reengagement';
$eventdata->userfrom = core_user::get_support_user();
$eventdata->userto = $userto;
$eventdata->subject = $subject;
$eventdata->fullmessage = $messageplain;
$eventdata->fullmessageformat = FORMAT_HTML;
$eventdata->fullmessagehtml = $messagehtml;
$eventdata->smallmessage = $subject;

// Required for messaging framework
$eventdata->name = 'mod_reengagement';
$eventdata->component = 'mod_reengagement';

return message_send($eventdata);
$emailfrom = reengagement_get_emailfrom($reengagement);
// Check instant message setting and verify we're sending to a real user, not third party.
if ($reengagement->instantmessage == REENGAGEMENT_NOTIFICATION_IM && $userto->id > 0) {
return message_post_message($emailfrom, $userto, $messagehtml, FORMAT_HTML);
} else {
$eventdata = new \core\message\message();
$eventdata->courseid = $reengagement->courseid;
$eventdata->modulename = 'reengagement';
$eventdata->userfrom = $emailfrom;
$eventdata->userto = $userto;
$eventdata->subject = $subject;
$eventdata->fullmessage = $messageplain;
$eventdata->fullmessageformat = FORMAT_HTML;
$eventdata->fullmessagehtml = $messagehtml;
$eventdata->smallmessage = $subject;
$eventdata->replyto = $emailfrom->email;

// Required for messaging framework
$eventdata->name = 'mod_reengagement';
$eventdata->component = 'mod_reengagement';

return message_send($eventdata);
}
}


Expand All @@ -578,6 +591,8 @@ function reengagement_template_variables($reengagement, $inprogress, $user) {

require_once($CFG->dirroot.'/user/profile/lib.php');

$emailfrom = reengagement_get_emailfrom($reengagement);

$templatevars = array(
'/%courseshortname%/' => $reengagement->courseshortname,
'/%coursefullname%/' => $reengagement->coursefullname,
Expand All @@ -588,6 +603,10 @@ function reengagement_template_variables($reengagement, $inprogress, $user) {
'/%usercity%/' => $user->city,
'/%userinstitution%/' => $user->institution,
'/%userdepartment%/' => $user->department,
'/%fromfirstname%/' => $emailfrom->firstname,
'/%fromlastname%/' => $emailfrom->lastname,
'/%fromid%/' => $emailfrom->id,
'/%fromemail%/' => $emailfrom->email,
);
// Add the users course groups as a template item.
$groups = $DB->get_records_sql_menu("SELECT g.id, g.name
Expand Down Expand Up @@ -980,3 +999,31 @@ function reengagement_checkstart($course, $cm, $reengagement) {
}
return $output;
}

/**
* Check that emailfrom user has capability to add reengagments,
* otherwise return support user
*
* @param object $reengagement
* @return object $user
*/
function reengagement_get_emailfrom($reengagement) {
$userid = $reengagement->emailfrom;
if ($userid > 0) {
$context = context_course::instance($reengagement->course);
if ($userid == 1) { // Default teacher, get first teacher in course.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use something like is siteadmin() style code - userid ===1 might not always be the siteadmin.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the variable REENGAGEMENT_EMAILFROM_TEACHER, but I take your point that the user with id 1 may not be the siteadmin, so I'm changing the value for default teacher to -1, 0 is still the support user, and then any integer above 0 will be the actual user.

global $DB;
$params = array('roleid' => 3, 'contextid' => $context->id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hard-coded roleid - this should be a site-level setting for re-engagement that allows the admin to select a role that is used for teaching staff or better still, use a has_capability check instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact - it would be better to use the get_course_contacts() style handling here with the $CFG->coursecontact settings instead of hard-coding this to a specific role.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed this to get_enrolled_users() with a permissions check for 'mod/reengagement:addinstance'

$userid = $DB->get_field('role_assignments', 'userid', $params);
}
$user = $userid ? core_user::get_user($userid) : null;

// Check selected teacher still has capability.
if ($user && has_capability('mod/reengagement:addinstance', $context, $user) ) {
return $user;
}
}

// If no default teacher, or selected teacher now lacks capability, return support user.
return core_user::get_support_user();
}
25 changes: 24 additions & 1 deletion mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class mod_reengagement_mod_form extends moodleform_mod {
*/
public function definition() {

global $COURSE, $CFG;
global $COURSE, $CFG, $DB;
$mform =& $this->_form;
// Make sure completion and restriction is enabled.
if (empty($CFG->enablecompletion) || empty($CFG->enableavailability)) {
Expand Down Expand Up @@ -127,10 +127,32 @@ public function definition() {
$mform->addHelpButton('remindercount', 'remindercount', 'reengagement');
$mform->hideif('remindercount', 'emailuser', 'neq', REENGAGEMENT_EMAILUSER_TIME);

// Add emailfrom selector.
$emailfromoptions = array();
$emailfromoptions[REENGAGEMENT_EMAILFROM_SUPPORT] = get_string('emailfromsupport', 'reengagement');
$emailfromoptions[REENGAGEMENT_EMAILFROM_TEACHER] = get_string('emailfromteacher', 'reengagement');
$coursecontext = context_course::instance($COURSE->id);
$emailfromusers = get_enrolled_users($coursecontext, 'mod/reengagement:addinstance');
foreach ($emailfromusers as $emailfromuser) {
$emailfromoptions[$emailfromuser->id] = get_string('fullnamedisplay', '', $emailfromuser);
}
$mform->addElement('select', 'emailfrom', get_string('emailfrom', 'reengagement'), $emailfromoptions);
$mform->hideif('emailfrom', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
$mform->addHelpButton('emailfrom', 'emailfrom', 'reengagement');

// Add notification type selector.
$instantmessageoptions = array();
$instantmessageoptions[REENGAGEMENT_NOTIFICATION_EMAIL] = get_string('notificationemail', 'reengagement');
$instantmessageoptions[REENGAGEMENT_NOTIFICATION_IM] = get_string('notificationim', 'reengagement');
$mform->addElement('select', 'instantmessage', get_string('notificationtype', 'reengagement'), $instantmessageoptions);
$mform->hideif('instantmessage', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
$mform->addHelpButton('instantmessage', 'notificationtype', 'reengagement');

$mform->addElement('text', 'emailsubject', get_string('emailsubject', 'reengagement'), array('size' => '64'));
$mform->setType('emailsubject', PARAM_TEXT);
$mform->addRule('emailsubject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->hideif('emailsubject', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
$mform->hideif('emailsubject', 'instantmessage', 'eq', REENGAGEMENT_NOTIFICATION_IM);
$mform->addHelpButton('emailsubject', 'emailsubject', 'reengagement');
$mform->addElement('editor', 'emailcontent', get_string('emailcontent', 'reengagement'), null, null);
$mform->setDefault('emailcontent', get_string('emailcontentdefaultvalue', 'reengagement'));
Expand All @@ -143,6 +165,7 @@ public function definition() {
$mform->setType('emailsubjectmanager', PARAM_TEXT);
$mform->addRule('emailsubjectmanager', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->hideif('emailsubjectmanager', 'emailuser', 'eq', REENGAGEMENT_EMAILUSER_NEVER);
$mform->hideif('emailsubjectmanager', 'instantmessage', 'eq', REENGAGEMENT_NOTIFICATION_IM);
$mform->addHelpButton('emailsubjectmanager', 'emailsubjectmanager', 'reengagement');
$mform->addElement('editor', 'emailcontentmanager', get_string('emailcontentmanager', 'reengagement'), null, null);
$mform->setDefault('emailcontentmanager', get_string('emailcontentmanagerdefaultvalue', 'reengagement'));
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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

$plugin->version = 2019091000; // The current module version.
$plugin->version = 2020022202; // The current module version.
$plugin->requires = 2018111800; // Requires 3.6
$plugin->component = 'mod_reengagement';
$plugin->release = '3.6.3';
Expand Down