MDL-81506 enrol_self: Edit welcome message capability applied

This commit is contained in:
David Woloszyn 2024-06-20 14:51:56 +10:00
parent 49fa7785dd
commit 7b14538f21
2 changed files with 161 additions and 114 deletions

View file

@ -133,6 +133,32 @@ class enrol_self_plugin extends enrol_plugin {
return true; return true;
} }
/**
* Returns edit icons for the page with list of instances.
*
* @param stdClass $instance
* @return array
*/
public function get_action_icons(stdClass $instance): array {
global $OUTPUT;
$context = context_course::instance($instance->courseid);
$icons = [];
if (has_any_capability(['enrol/self:config', 'moodle/course:editcoursewelcomemessage'], $context)) {
$linkparams = [
'courseid' => $instance->courseid,
'id' => $instance->id,
'type' => $instance->enrol,
];
$editlink = new moodle_url('/enrol/editinstance.php', $linkparams);
$icon = new pix_icon('t/edit', get_string('edit'), 'core', ['class' => 'iconsmall']);
$icons[] = $OUTPUT->action_icon($editlink, $icon);
}
return $icons;
}
/** /**
* Self enrol user to course * Self enrol user to course
* *
@ -828,13 +854,15 @@ class enrol_self_plugin extends enrol_plugin {
public function edit_instance_form($instance, MoodleQuickForm $mform, $context) { public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
global $CFG, $DB; global $CFG, $DB;
// Main fields.
if (has_capability('enrol/self:config', $context)) {
// Merge these two settings to one value for the single selection element. // Merge these two settings to one value for the single selection element.
if ($instance->notifyall and $instance->expirynotify) { if ($instance->notifyall && $instance->expirynotify) {
$instance->expirynotify = 2; $instance->expirynotify = 2;
} }
unset($instance->notifyall); unset($instance->notifyall);
$nameattribs = array('size' => '20', 'maxlength' => '255'); $nameattribs = ['size' => '20', 'maxlength' => '255'];
$mform->addElement('text', 'name', get_string('custominstancename', 'enrol'), $nameattribs); $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'), $nameattribs);
$mform->setType('name', PARAM_TEXT); $mform->setType('name', PARAM_TEXT);
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server'); $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server');
@ -848,10 +876,10 @@ class enrol_self_plugin extends enrol_plugin {
$mform->addHelpButton('customint6', 'newenrols', 'enrol_self'); $mform->addHelpButton('customint6', 'newenrols', 'enrol_self');
$mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED); $mform->disabledIf('customint6', 'status', 'eq', ENROL_INSTANCE_DISABLED);
$passattribs = array('size' => '20', 'maxlength' => '50'); $passattribs = ['size' => '20', 'maxlength' => '50'];
$mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'), $passattribs); $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self'), $passattribs);
$mform->addHelpButton('password', 'password', 'enrol_self'); $mform->addHelpButton('password', 'password', 'enrol_self');
if (empty($instance->id) and $this->get_config('requirepassword')) { if (empty($instance->id) && $this->get_config('requirepassword')) {
$mform->addRule('password', get_string('required'), 'required', null, 'client'); $mform->addRule('password', get_string('required'), 'required', null, 'client');
} }
$mform->addRule('password', get_string('maximumchars', '', 50), 'maxlength', 50, 'server'); $mform->addRule('password', get_string('maximumchars', '', 50), 'maxlength', 50, 'server');
@ -863,7 +891,7 @@ class enrol_self_plugin extends enrol_plugin {
$roles = $this->extend_assignable_roles($context, $instance->roleid); $roles = $this->extend_assignable_roles($context, $instance->roleid);
$mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles); $mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles);
$options = array('optional' => true, 'defaultunit' => 86400); $options = ['optional' => true, 'defaultunit' => 86400];
$mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), $options); $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), $options);
$mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self'); $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self');
@ -871,17 +899,17 @@ class enrol_self_plugin extends enrol_plugin {
$mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options); $mform->addElement('select', 'expirynotify', get_string('expirynotify', 'core_enrol'), $options);
$mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol'); $mform->addHelpButton('expirynotify', 'expirynotify', 'core_enrol');
$options = array('optional' => false, 'defaultunit' => 86400); $options = ['optional' => false, 'defaultunit' => 86400];
$mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), $options); $mform->addElement('duration', 'expirythreshold', get_string('expirythreshold', 'core_enrol'), $options);
$mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol'); $mform->addHelpButton('expirythreshold', 'expirythreshold', 'core_enrol');
$mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0); $mform->disabledIf('expirythreshold', 'expirynotify', 'eq', 0);
$options = array('optional' => true); $options = ['optional' => true];
$mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), $options); $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), $options);
$mform->setDefault('enrolstartdate', 0); $mform->setDefault('enrolstartdate', 0);
$mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self'); $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self');
$options = array('optional' => true); $options = ['optional' => true];
$mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), $options); $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), $options);
$mform->setDefault('enrolenddate', 0); $mform->setDefault('enrolenddate', 0);
$mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_self'); $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_self');
@ -896,11 +924,11 @@ class enrol_self_plugin extends enrol_plugin {
require_once($CFG->dirroot.'/cohort/lib.php'); require_once($CFG->dirroot.'/cohort/lib.php');
$cohorts = array(0 => get_string('no')); $cohorts = [0 => get_string('no')];
$allcohorts = cohort_get_available_cohorts($context, 0, 0, 0); $allcohorts = cohort_get_available_cohorts($context, 0, 0, 0);
if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) { if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) {
$c = $DB->get_record('cohort', $c = $DB->get_record('cohort',
array('id' => $instance->customint5), ['id' => $instance->customint5],
'id, name, idnumber, contextid, visible', 'id, name, idnumber, contextid, visible',
IGNORE_MISSING); IGNORE_MISSING);
if ($c) { if ($c) {
@ -909,7 +937,7 @@ class enrol_self_plugin extends enrol_plugin {
} }
} }
foreach ($allcohorts as $c) { foreach ($allcohorts as $c) {
$cohorts[$c->id] = format_string($c->name, true, array('context' => context::instance_by_id($c->contextid))); $cohorts[$c->id] = format_string($c->name, true, ['context' => context::instance_by_id($c->contextid)]);
if ($c->idnumber) { if ($c->idnumber) {
$cohorts[$c->id] .= ' ['.s($c->idnumber).']'; $cohorts[$c->id] .= ' ['.s($c->idnumber).']';
} }
@ -926,12 +954,18 @@ class enrol_self_plugin extends enrol_plugin {
$mform->setType('customint5', PARAM_INT); $mform->setType('customint5', PARAM_INT);
$mform->setConstant('customint5', 0); $mform->setConstant('customint5', 0);
} }
}
// Course welcome message.
if (has_any_capability(['enrol/self:config', 'moodle/course:editcoursewelcomemessage'], $context)) {
$mform->addElement('select', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'), $mform->addElement('select', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'),
enrol_send_welcome_email_options()); enrol_send_welcome_email_options());
$mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self');
$options = array('cols' => '60', 'rows' => '8'); $options = [
'cols' => '60',
'rows' => '8',
];
$mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'core_enrol'), $options); $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'core_enrol'), $options);
$mform->setDefault('customtext1', get_string('customwelcomemessageplaceholder', 'core_enrol')); $mform->setDefault('customtext1', get_string('customwelcomemessageplaceholder', 'core_enrol'));
$mform->hideIf( $mform->hideIf(
@ -959,8 +993,10 @@ class enrol_self_plugin extends enrol_plugin {
condition: 'eq', condition: 'eq',
value: ENROL_DO_NOT_SEND_EMAIL, value: ENROL_DO_NOT_SEND_EMAIL,
); );
}
if (enrol_accessing_via_instance($instance)) { // Enrolment changes warning.
if (has_capability('enrol/self:config', $context) && enrol_accessing_via_instance($instance)) {
$warntext = get_string('instanceeditselfwarningtext', 'core_enrol'); $warntext = get_string('instanceeditselfwarningtext', 'core_enrol');
$mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warntext); $mform->addElement('static', 'selfwarn', get_string('instanceeditselfwarning', 'core_enrol'), $warntext);
} }

View file

@ -40,6 +40,17 @@ Feature: A course welcome message will be sent to the user when they auto-enrol
And I should not see "Custom welcome message" And I should not see "Custom welcome message"
And I should not see "Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders:" And I should not see "Accepted formats: Plain text or Moodle-auto format. HTML tags and multi-lang tags are also accepted, as well as the following placeholders:"
@javascript
Scenario: Teacher can edit the course welcome message
Given I am on the "C1" "Enrolled users" page logged in as teacher
And I set the field "Participants tertiary navigation" to "Enrolment methods"
When I click on "Edit" "link" in the "Self enrolment" "table_row"
Then I should see "Send course welcome message"
And I set the field "Custom welcome message" to "Hello {$a->fullname}, welcome to the course {$a->coursename}"
And I press "Save changes"
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And I should see "Hello {$a->fullname}, welcome to the course {$a->coursename}"
@javascript @javascript
Scenario: Student should not receive a welcome message if the setting is disabled Scenario: Student should not receive a welcome message if the setting is disabled
Given I am on the "C1" "Enrolled users" page logged in as manager Given I am on the "C1" "Enrolled users" page logged in as manager