Skip to content

Commit

Permalink
Add a custom welcome message
Browse files Browse the repository at this point in the history
When set, it will send an email to the user enrolled. Most of this code
is copied and slightly modified from enrol_self, but without the default
message.

Fixes MDLSITE-3926 and markward#10
  • Loading branch information
danpoltawski committed Jun 12, 2015
1 parent 9bc16b7 commit f6f4b3c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
4 changes: 3 additions & 1 deletion enrol/autoenrol/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
$instance->customint8 = $data->customint8;
$instance->customchar1 = $data->customchar1;
$instance->customchar2 = $data->customchar2;
$instance->customtext1 = $data->customtext1;

$DB->update_record('enrol', $instance);


Expand All @@ -108,7 +110,7 @@
$fields['customint1'] = $data->customint1;
$fields['customint3'] = $data->customint3;
}

$instance->customtext1 = $data->customtext1;
$plugin->add_instance($course, $fields);

}
Expand Down
12 changes: 12 additions & 0 deletions enrol/autoenrol/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function definition() {
$this->add_hidden_fields();
$this->add_general_section($instance, $plugin, $context);
$this->add_filtering_section();
$this->add_welcomemesage_setting();
$this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));

$this->set_data($instance);
Expand Down Expand Up @@ -143,6 +144,17 @@ protected function add_filtering_section() {
}

/**
* Adds a custom welcome message setting.
*/
protected function add_welcomemesage_setting() {
$this->_form->addElement('header', 'welcomemessageheader', get_string('welcomemessage', 'enrol_autoenrol'));
$this->_form->setExpanded('welcomemessageheader', false);
$this->_form->addElement('textarea', 'customtext1',
get_string('customwelcomemessage', 'enrol_autoenrol'), array('cols' => '60', 'rows' => '8'));
$this->_form->addHelpButton('customtext1', 'customwelcomemessage', 'enrol_autoenrol');
}

/**
*
*/
protected function add_hidden_fields() {
Expand Down
11 changes: 11 additions & 0 deletions enrol/autoenrol/lang/en/enrol_autoenrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,22 @@
$string['pluginname_desc'] = 'The automatic enrolment module allows an option for logged in users to be automatically granted entry to a course and enrolled. This is similar to allowing guest access but the students will be permanently enrolled and therefore able to participate in forum and activities within the area.';

$string['config'] = 'Configuration';
$string['customwelcomemessage'] = 'Custom welcome message';
$string['customwelcomemessage_help'] = 'A custom welcome message may be added as plain text or Moodle-auto format, including HTML tags and multi-lang tags.
The following placeholders may be included in the message:
* Course name {$a->coursename}
* Link to user\'s profile page {$a->profileurl}
* User email {$a->email}
* User fullname {$a->fullname}';
$string['general'] = 'General';
$string['filtering'] = 'User Filtering';

$string['warning'] = 'Caution!';
$string['warning_message'] = 'Adding this plugin to your course will allow any registered Moodle users access to your course. Only install this plugin if you want to allow open access to your course for users who have logged in.';
$string['welcomemessage'] = 'Welcome message';
$string['welcometocourse'] = 'Welcome to {$a}';

$string['role'] = 'Role';
$string['role_help'] = 'Power users can use this setting to change the permission level at which users are enrolled.';
Expand Down
62 changes: 62 additions & 0 deletions enrol/autoenrol/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function try_autoenrol(stdClass $instance) {
if ($instance->customint1 == 0 && $this->enrol_allowed($USER, $instance)) {
$this->enrol_user($instance, $USER->id, $instance->customint3, time(), 0);
$this->process_group($instance, $USER);
$this->email_welcome_message($instance, $USER);
return 9999999999;
}
return false;
Expand Down Expand Up @@ -472,4 +473,65 @@ private function get_group(stdClass $instance, $name, moodle_database $DB) {

return $group;
}

/**
* Send welcome email to specified user.
*
* @param stdClass $instance
* @param stdClass $user user record
* @return void
*/
protected function email_welcome_message($instance, $user) {
global $CFG, $DB;
if (empty(trim($instance->customtext1))) {
// No welcome message set, nothing to do.
return;
}

$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";

$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);
}

$subject = get_string('welcometocourse', 'enrol_autoenrol',
format_string($course->fullname, true, array('context' => $context)));

$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 = reset($rusers);
} else {
$contact = core_user::get_support_user();
}

// Directly emailing welcome message rather than using messaging.
email_to_user($user, $contact, $subject, $messagetext, $messagehtml);
}
}

4 comments on commit f6f4b3c

@jeremy-schweitzer
Copy link

Choose a reason for hiding this comment

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

Dan,

I see some back and forth with David about using this and your addition of pre-3.1 messaging. Are you still using this plugin? We have been using it with 3.0 and planning to upgrade to 3.1, but noticing an error message that seems to indicate some changes in how 3.1 expects messages to be implemented.

If you're not using it - has something else superseded it?

@danpoltawski
Copy link
Owner Author

@danpoltawski danpoltawski commented on f6f4b3c Sep 12, 2016

Choose a reason for hiding this comment

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

I think that this is still in use on learn.moodle.net (perhaps with some more fixes). @mudrd8mz might know more. We probably should find some time to send a formal pull request

@mudrd8mz
Copy link

Choose a reason for hiding this comment

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

We use rev 3b07f70 of this plugin with some recent fix added by Karen Holland that might be related to the problem mentioned by Jeremy. I asked Karen to check this discussion.

@jeremy-schweitzer
Copy link

Choose a reason for hiding this comment

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

I appreciate it. If there's a pull request I can at least use that to grab the code, even if the original developer seems to have stopped working on the plugin. It is very useful - especially for corporate environments where we don't have an SIS.

Please sign in to comment.