From b191fbe9a9b3de403a4ae5d06b274a4e7c0de094 Mon Sep 17 00:00:00 2001 From: Julien Boulen Date: Tue, 17 Apr 2018 12:45:23 +0200 Subject: [PATCH] Sending enrollment email to participants on using enrollment method cohort sync --- enrol/cohort/lib.php | 114 +++++++++++++++++++++++++++++++++++++++++++ enrol/manual/lib.php | 113 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 227 insertions(+) diff --git a/enrol/cohort/lib.php b/enrol/cohort/lib.php index fdf9c5727a666..16e80c5488dc1 100644 --- a/enrol/cohort/lib.php +++ b/enrol/cohort/lib.php @@ -473,6 +473,11 @@ public function edit_instance_form($instance, MoodleQuickForm $mform, $coursecon $mform->setDefault('roleid', $this->get_config('roleid')); $groups = $this->get_group_options($coursecontext); $mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups); + + $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); + $mform->setDefault('customint4', $this->get_config('sendcoursewelcomemessage')); + $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); + $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), array('cols'=>'60', 'rows'=>'8')); } /** @@ -516,6 +521,115 @@ public function edit_instance_validation($data, $files, $instance, $context) { return $errors; } + + /** + * @param stdClass $instance + * @param int $userid + * @param int $roleid optional role id + * @param int $timestart 0 means unknown + * @param int $timeend 0 means forever + * @param int $status default to ENROL_USER_ACTIVE for new enrolments, no change by default in updates + * @param bool $recovergrades restore grade history + * @return void + */ + public function enrol_user(stdClass $instance, $userid, $roleid = NULL, $timestart = 0, $timeend = 0, $status = NULL, $recovergrades = null){ + global $DB; + + parent::enrol_user($instance, $userid, $roleid, $timestart, $timeend, $status, $recovergrades = null); + + $user = $DB->get_record('user', array('id' => $userid)); + // Send welcome message. + if ($instance->customint4) { + $this->email_welcome_message($instance, $user); + } + } + + /** + * @param stdClass $instance + * @param stdClass $user user record + * @return void + */ + protected function email_welcome_message($instance, $user) { + global $CFG, $DB; + + $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST); + $context = context_course::instance($course->id); + + $a = new stdClass(); + $a->coursename = format_string($course->fullname, true, array('context'=>$context)); + $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id"; + + if (trim($instance->customtext1) !== '') { + $message = $instance->customtext1; + $key = array('{$a->coursename}', '{$a->profileurl}', '{$a->fullname}', '{$a->email}'); + $value = array($a->coursename, $a->profileurl, fullname($user), $user->email); + $message = str_replace($key, $value, $message); + if (strpos($message, '<') === false) { + // Plain text only. + $messagetext = $message; + $messagehtml = text_to_html($messagetext, null, false, true); + } else { + // This is most probably the tag/newline soup known as FORMAT_MOODLE. + $messagehtml = format_text($message, FORMAT_MOODLE, array('context'=>$context, 'para'=>false, 'newlines'=>true, 'filter'=>true)); + $messagetext = html_to_text($messagehtml); + } + } else { + $messagetext = get_string('welcometocoursetext', 'enrol_self', $a); + $messagehtml = text_to_html($messagetext, null, false, true); + } + + $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname, true, array('context'=>$context))); + + $sendoption = ENROL_SEND_EMAIL_FROM_NOREPLY; + $contact = $this->get_welcome_email_contact($sendoption, $context); + + // Directly emailing welcome message rather than using messaging. + email_to_user($user, $contact, $subject, $messagetext, $messagehtml); + } + + /** + * @param int $sendoption send email from constant ENROL_SEND_EMAIL_FROM_* + * @param $context context where the user will be fetched + * @return mixed|stdClass the contact user object. + */ + public function get_welcome_email_contact($sendoption, $context) { + global $CFG; + + $contact = null; + // Send as the first user assigned as the course contact. + if ($sendoption == ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) { + $rusers = array(); + if (!empty($CFG->coursecontact)) { + $croles = explode(',', $CFG->coursecontact); + list($sort, $sortparams) = users_order_by_sql('u'); + // We only use the first user. + $i = 0; + do { + $rusers = get_role_users($croles[$i], $context, true, '', + 'r.sortorder ASC, ' . $sort, null, '', '', '', '', $sortparams); + $i++; + } while (empty($rusers) && !empty($croles[$i])); + } + if ($rusers) { + $contact = array_values($rusers)[0]; + } + } else if ($sendoption == ENROL_SEND_EMAIL_FROM_KEY_HOLDER) { + // Send as the first user with enrol/self:holdkey capability assigned in the course. + list($sort) = users_order_by_sql('u'); + $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', 'u.*', $sort); + if (!empty($keyholders)) { + $contact = array_values($keyholders)[0]; + } + } + + // If send welcome email option is set to no reply or if none of the previous options have + // returned a contact send welcome message as noreplyuser. + if ($sendoption == ENROL_SEND_EMAIL_FROM_NOREPLY || empty($contact)) { + $contact = core_user::get_noreply_user(); + } + + return $contact; + } } /** diff --git a/enrol/manual/lib.php b/enrol/manual/lib.php index 869dce005b562..3a4380aaf5b4e 100644 --- a/enrol/manual/lib.php +++ b/enrol/manual/lib.php @@ -677,6 +677,11 @@ public function edit_instance_form($instance, MoodleQuickForm $mform, $context) $mform->addElement('select', 'roleid', get_string('defaultrole', 'role'), $roles); $mform->setDefault('roleid', $this->get_config('roleid')); + $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); + $mform->setDefault('customint4', $this->get_config('sendcoursewelcomemessage')); + $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); + $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), array('cols'=>'60', 'rows'=>'8')); + $options = array('optional' => true, 'defaultunit' => 86400); $mform->addElement('duration', 'enrolperiod', get_string('defaultperiod', 'enrol_manual'), $options); $mform->setDefault('enrolperiod', $this->get_config('enrolperiod')); @@ -733,4 +738,112 @@ public function edit_instance_validation($data, $files, $instance, $context) { return $errors; } + /** + * @param stdClass $instance + * @param int $userid + * @param int $roleid optional role id + * @param int $timestart 0 means unknown + * @param int $timeend 0 means forever + * @param int $status default to ENROL_USER_ACTIVE for new enrolments, no change by default in updates + * @param bool $recovergrades restore grade history + * @return void + */ + public function enrol_user(stdClass $instance, $userid, $roleid = NULL, $timestart = 0, $timeend = 0, $status = NULL, $recovergrades = null){ + global $DB; + + parent::enrol_user($instance, $userid, $roleid, $timestart, $timeend, $status, $recovergrades = null); + + $user = $DB->get_record('user', array('id' => $userid)); + // Send welcome message. + if ($instance->customint4) { + $this->email_welcome_message($instance, $user); + } + } + + /** + * @param stdClass $instance + * @param stdClass $user user record + * @return void + */ + protected function email_welcome_message($instance, $user) { + global $CFG, $DB; + + $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST); + $context = context_course::instance($course->id); + + $a = new stdClass(); + $a->coursename = format_string($course->fullname, true, array('context'=>$context)); + $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id"; + + if (trim($instance->customtext1) !== '') { + $message = $instance->customtext1; + $key = array('{$a->coursename}', '{$a->profileurl}', '{$a->fullname}', '{$a->email}'); + $value = array($a->coursename, $a->profileurl, fullname($user), $user->email); + $message = str_replace($key, $value, $message); + if (strpos($message, '<') === false) { + // Plain text only. + $messagetext = $message; + $messagehtml = text_to_html($messagetext, null, false, true); + } else { + // This is most probably the tag/newline soup known as FORMAT_MOODLE. + $messagehtml = format_text($message, FORMAT_MOODLE, array('context'=>$context, 'para'=>false, 'newlines'=>true, 'filter'=>true)); + $messagetext = html_to_text($messagehtml); + } + } else { + $messagetext = get_string('welcometocoursetext', 'enrol_self', $a); + $messagehtml = text_to_html($messagetext, null, false, true); + } + + $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname, true, array('context'=>$context))); + + $sendoption = ENROL_SEND_EMAIL_FROM_NOREPLY; + $contact = $this->get_welcome_email_contact($sendoption, $context); + + // Directly emailing welcome message rather than using messaging. + email_to_user($user, $contact, $subject, $messagetext, $messagehtml); + } + + /** + * @param int $sendoption send email from constant ENROL_SEND_EMAIL_FROM_* + * @param $context context where the user will be fetched + * @return mixed|stdClass the contact user object. + */ + public function get_welcome_email_contact($sendoption, $context) { + global $CFG; + + $contact = null; + // Send as the first user assigned as the course contact. + if ($sendoption == ENROL_SEND_EMAIL_FROM_COURSE_CONTACT) { + $rusers = array(); + if (!empty($CFG->coursecontact)) { + $croles = explode(',', $CFG->coursecontact); + list($sort, $sortparams) = users_order_by_sql('u'); + // We only use the first user. + $i = 0; + do { + $rusers = get_role_users($croles[$i], $context, true, '', + 'r.sortorder ASC, ' . $sort, null, '', '', '', '', $sortparams); + $i++; + } while (empty($rusers) && !empty($croles[$i])); + } + if ($rusers) { + $contact = array_values($rusers)[0]; + } + } else if ($sendoption == ENROL_SEND_EMAIL_FROM_KEY_HOLDER) { + // Send as the first user with enrol/self:holdkey capability assigned in the course. + list($sort) = users_order_by_sql('u'); + $keyholders = get_users_by_capability($context, 'enrol/self:holdkey', 'u.*', $sort); + if (!empty($keyholders)) { + $contact = array_values($keyholders)[0]; + } + } + + // If send welcome email option is set to no reply or if none of the previous options have + // returned a contact send welcome message as noreplyuser. + if ($sendoption == ENROL_SEND_EMAIL_FROM_NOREPLY || empty($contact)) { + $contact = core_user::get_noreply_user(); + } + + return $contact; + } }