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,139 +854,149 @@ 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;
// Merge these two settings to one value for the single selection element. // Main fields.
if ($instance->notifyall and $instance->expirynotify) { if (has_capability('enrol/self:config', $context)) {
$instance->expirynotify = 2; // Merge these two settings to one value for the single selection element.
} if ($instance->notifyall && $instance->expirynotify) {
unset($instance->notifyall); $instance->expirynotify = 2;
}
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');
$options = $this->get_status_options(); $options = $this->get_status_options();
$mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options); $mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options);
$mform->addHelpButton('status', 'status', 'enrol_self'); $mform->addHelpButton('status', 'status', 'enrol_self');
$options = $this->get_newenrols_options(); $options = $this->get_newenrols_options();
$mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_self'), $options); $mform->addElement('select', 'customint6', get_string('newenrols', 'enrol_self'), $options);
$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');
$options = $this->get_groupkey_options(); $options = $this->get_groupkey_options();
$mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options); $mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options);
$mform->addHelpButton('customint1', 'groupkey', 'enrol_self'); $mform->addHelpButton('customint1', 'groupkey', 'enrol_self');
$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');
$options = $this->get_expirynotify_options(); $options = $this->get_expirynotify_options();
$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');
$options = $this->get_longtimenosee_options(); $options = $this->get_longtimenosee_options();
$mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options); $mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options);
$mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self'); $mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self');
$mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self')); $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self'));
$mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self'); $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self');
$mform->setType('customint3', PARAM_INT); $mform->setType('customint3', PARAM_INT);
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) {
// Current cohort was not found because current user can not see it. Still keep it. // Current cohort was not found because current user can not see it. Still keep it.
$allcohorts[$instance->customint5] = $c; $allcohorts[$instance->customint5] = $c;
}
}
foreach ($allcohorts as $c) {
$cohorts[$c->id] = format_string($c->name, true, ['context' => context::instance_by_id($c->contextid)]);
if ($c->idnumber) {
$cohorts[$c->id] .= ' ['.s($c->idnumber).']';
}
}
if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) {
// Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol.
$cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5);
}
if (count($cohorts) > 1) {
$mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_self'), $cohorts);
$mform->addHelpButton('customint5', 'cohortonly', 'enrol_self');
} else {
$mform->addElement('hidden', 'customint5');
$mform->setType('customint5', PARAM_INT);
$mform->setConstant('customint5', 0);
} }
} }
foreach ($allcohorts as $c) {
$cohorts[$c->id] = format_string($c->name, true, array('context' => context::instance_by_id($c->contextid))); // Course welcome message.
if ($c->idnumber) { if (has_any_capability(['enrol/self:config', 'moodle/course:editcoursewelcomemessage'], $context)) {
$cohorts[$c->id] .= ' ['.s($c->idnumber).']'; $mform->addElement('select', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'),
} enrol_send_welcome_email_options());
} $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self');
if ($instance->customint5 && !isset($allcohorts[$instance->customint5])) {
// Somebody deleted a cohort, better keep the wrong value so that random ppl can not enrol. $options = [
$cohorts[$instance->customint5] = get_string('unknowncohort', 'cohort', $instance->customint5); 'cols' => '60',
} 'rows' => '8',
if (count($cohorts) > 1) { ];
$mform->addElement('select', 'customint5', get_string('cohortonly', 'enrol_self'), $cohorts); $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'core_enrol'), $options);
$mform->addHelpButton('customint5', 'cohortonly', 'enrol_self'); $mform->setDefault('customtext1', get_string('customwelcomemessageplaceholder', 'core_enrol'));
} else { $mform->hideIf(
$mform->addElement('hidden', 'customint5'); elementname: 'customtext1',
$mform->setType('customint5', PARAM_INT); dependenton: 'customint4',
$mform->setConstant('customint5', 0); condition: 'eq',
value: ENROL_DO_NOT_SEND_EMAIL,
);
// Static form elements cannot be hidden by hideIf() so we need to add a dummy group.
// See: https://tracker.moodle.org/browse/MDL-66251.
$group[] = $mform->createElement(
'static',
'customwelcomemessage_extra_help',
null,
get_string(
identifier: 'customwelcomemessage_help',
component: 'core_enrol',
),
);
$mform->addGroup($group, 'group_customwelcomemessage_extra_help', '', ' ', false);
$mform->hideIf(
elementname: 'group_customwelcomemessage_extra_help',
dependenton: 'customint4',
condition: 'eq',
value: ENROL_DO_NOT_SEND_EMAIL,
);
} }
$mform->addElement('select', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self'), // Enrolment changes warning.
enrol_send_welcome_email_options()); if (has_capability('enrol/self:config', $context) && enrol_accessing_via_instance($instance)) {
$mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self');
$options = array('cols' => '60', 'rows' => '8');
$mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'core_enrol'), $options);
$mform->setDefault('customtext1', get_string('customwelcomemessageplaceholder', 'core_enrol'));
$mform->hideIf(
elementname: 'customtext1',
dependenton: 'customint4',
condition: 'eq',
value: ENROL_DO_NOT_SEND_EMAIL,
);
// Static form elements cannot be hidden by hideIf() so we need to add a dummy group.
// See: https://tracker.moodle.org/browse/MDL-66251.
$group[] = $mform->createElement(
'static',
'customwelcomemessage_extra_help',
null,
get_string(
identifier: 'customwelcomemessage_help',
component: 'core_enrol',
),
);
$mform->addGroup($group, 'group_customwelcomemessage_extra_help', '', ' ', false);
$mform->hideIf(
elementname: 'group_customwelcomemessage_extra_help',
dependenton: 'customint4',
condition: 'eq',
value: ENROL_DO_NOT_SEND_EMAIL,
);
if (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