Skip to content

Commit

Permalink
Sending enrollment email to participants on using enrollment method c…
Browse files Browse the repository at this point in the history
…ohort sync
  • Loading branch information
jboulen committed Apr 17, 2018
1 parent 1402bb2 commit b191fbe
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 0 deletions.
114 changes: 114 additions & 0 deletions enrol/cohort/lib.php
Expand Up @@ -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'));
}

/**
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
113 changes: 113 additions & 0 deletions enrol/manual/lib.php
Expand Up @@ -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'));
Expand Down Expand Up @@ -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;
}
}

1 comment on commit b191fbe

@marisolcastro
Copy link

Choose a reason for hiding this comment

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

How do I download these files

Please sign in to comment.