From 454a769324898259a4b0c73c44fb73408efbf48c Mon Sep 17 00:00:00 2001 From: btc989 Date: Wed, 6 Apr 2022 08:55:07 -0600 Subject: [PATCH] Custom email feature added --- classes/output/email_certificate.php | 46 +++++++++++++++++++++++----- lang/en/customcert.php | 4 +++ settings.php | 4 +++ version.php | 2 +- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/classes/output/email_certificate.php b/classes/output/email_certificate.php index 3020caa1..40e17301 100644 --- a/classes/output/email_certificate.php +++ b/classes/output/email_certificate.php @@ -99,20 +99,52 @@ public function export_for_template(\renderer_base $renderer) { $info->courseshortname = $this->courseshortname; $info->coursefullname = $this->coursefullname; - if ($this->isstudent) { + $studentpattern = '/\$studentname/'; + $certificatepattern = '/\$certificatename/'; + $coursepattern = '/\$coursename/'; + + $studentmessage = get_config('customcert', 'emailstudentmessage'); + + if($studentmessage != "" && $this->isstudent){ + + //replace all variables in email message + $studentmessage = preg_replace($studentpattern, $this->userfullname, $studentmessage); + $studentmessage = preg_replace($certificatepattern, $info->certificatename, $studentmessage); + $studentmessage = preg_replace($coursepattern, $info->coursefullname, $studentmessage); + + $data->emailgreeting=""; + $data->emailbody = $studentmessage; + $data->emailbodyplaintext = $studentmessage; + $data->emailcertificatelink = new \moodle_url('/mod/customcert/view.php', array('id' => $this->cmid)); + $data->emailcertificatetext = get_string('emailstudentcertificatelinktext', 'customcert'); + }elseif ($studentmessage == "" && $this->isstudent) { $data->emailgreeting = get_string('emailstudentgreeting', 'customcert', $this->userfullname); $data->emailbody = get_string('emailstudentbody', 'customcert', $info); $data->emailbodyplaintext = get_string('emailstudentbodyplaintext', 'customcert', $info); $data->emailcertificatelink = new \moodle_url('/mod/customcert/view.php', array('id' => $this->cmid)); $data->emailcertificatetext = get_string('emailstudentcertificatelinktext', 'customcert'); } else { - $data->emailgreeting = get_string('emailnonstudentgreeting', 'customcert'); - $data->emailbody = get_string('emailnonstudentbody', 'customcert', $info); - $data->emailbodyplaintext = get_string('emailnonstudentbodyplaintext', 'customcert', $info); - $data->emailcertificatelink = new \moodle_url('/mod/customcert/view.php', array('id' => $this->cmid)); - $data->emailcertificatetext = get_string('emailnonstudentcertificatelinktext', 'customcert'); - } + $nonstudentmessage = get_config('customcert', 'emailnonstudentmessage'); + if($nonstudentmessage!=""){ + //replace all variables in email message + $nonstudentmessage = preg_replace($studentpattern, $this->userfullname, $nonstudentmessage); + $nonstudentmessage = preg_replace($certificatepattern, $info->certificatename, $nonstudentmessage); + $nonstudentmessage = preg_replace($coursepattern, $info->coursefullname, $nonstudentmessage); + + $data->emailgreeting = ""; + $data->emailbody = $nonstudentmessage; + $data->emailbodyplaintext = $nonstudentmessage; + $data->emailcertificatelink = new \moodle_url('/mod/customcert/view.php', array('id' => $this->cmid)); + $data->emailcertificatetext = get_string('emailnonstudentcertificatelinktext', 'customcert'); + }else{ + $data->emailgreeting = get_string('emailnonstudentgreeting', 'customcert'); + $data->emailbody = get_string('emailnonstudentbody', 'customcert', $info); + $data->emailbodyplaintext = get_string('emailnonstudentbodyplaintext', 'customcert', $info); + $data->emailcertificatelink = new \moodle_url('/mod/customcert/view.php', array('id' => $this->cmid)); + $data->emailcertificatetext = get_string('emailnonstudentcertificatelinktext', 'customcert'); + } + } return $data; } } diff --git a/lang/en/customcert.php b/lang/en/customcert.php index fbc5464a..9217ac74 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -88,8 +88,12 @@ $string['emailnonstudentsubject'] = '{$a->coursefullname}: {$a->certificatename}'; $string['emailstudentbody'] = 'Attached is your certificate \'{$a->certificatename}\' for the course \'{$a->coursefullname}\'.'; $string['emailstudentbodyplaintext'] = 'Attached is your certificate \'{$a->certificatename}\' for the course \'{$a->coursefullname}\'.'; +$string['emailnonstudentmessage'] = 'Template email sent to teachers and others listed'; +$string['emailnonstudentmessage_help'] = 'Create message to be emailed to teachers and others listed above. If left blank default email will be sent. For emails customized to student use $studentname, $certificate, $coursename variables, for where the student name, certificate name, and course name should be replaced.'; $string['emailstudentcertificatelinktext'] = 'View certificate'; $string['emailstudentgreeting'] = 'Dear {$a}'; +$string['emailstudentmessage'] = 'Template email sent to students'; +$string['emailstudentmessage_help'] = 'Create message to be emailed to students. If left blank default email will be sent. For emails customized to student use $studentname, $certificate, $coursename variables, for where the student name, certificate name, and course name should be replaced.'; $string['emailstudentsubject'] = '{$a->coursefullname}: {$a->certificatename}'; $string['emailstudents'] = 'Email students'; $string['emailstudents_help'] = 'If set this will email the students a copy of the certificate when it becomes available. Warning: Setting this to \'Yes\' before you have finished creating the certificate will email the student an incomplete certificate.'; diff --git a/settings.php b/settings.php index 6a648edc..ae161533 100644 --- a/settings.php +++ b/settings.php @@ -61,10 +61,14 @@ $settings->add(new admin_setting_configselect('customcert/emailstudents', get_string('emailstudents', 'customcert'), get_string('emailstudents_help', 'customcert'), 0, $yesnooptions)); +$settings->add(new admin_setting_configtextarea('customcert/emailstudentmessage', + get_string('emailstudentmessage', 'customcert'), get_string('emailstudentmessage_help', 'customcert'),"", PARAM_RAW, '60','8')); $settings->add(new admin_setting_configselect('customcert/emailteachers', get_string('emailteachers', 'customcert'), get_string('emailteachers_help', 'customcert'), 0, $yesnooptions)); $settings->add(new admin_setting_configtext('customcert/emailothers', get_string('emailothers', 'customcert'), get_string('emailothers_help', 'customcert'), '', PARAM_TEXT)); +$settings->add(new admin_setting_configtextarea('customcert/emailnonstudentmessage', + get_string('emailnonstudentmessage', 'customcert'), get_string('emailnonstudentmessage_help', 'customcert'),"", PARAM_RAW, '60','8')); $settings->add(new admin_setting_configselect('customcert/verifyany', get_string('verifycertificateanyone', 'customcert'), get_string('verifycertificateanyone_help', 'customcert'), 0, $yesnooptions)); diff --git a/version.php b/version.php index 12b71b57..b4044902 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2021051702; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2022040503; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2021051700; // Requires this Moodle version (3.11). $plugin->cron = 0; // Period for cron to check this module (secs). $plugin->component = 'mod_customcert';