Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enroloptions #3

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
95 changes: 85 additions & 10 deletions classes/bulkenrol_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,62 @@ protected function definition() {

$mform = $this->_form;

// Selector for database field to match list to.
$availablefieldsstring = get_config('local_bulkenrol', 'fieldoptions');
$availablefieldsarray = explode(',', $availablefieldsstring);
if (count($availablefieldsarray) < 1 or $availablefieldsarray[0] == '') {
print_error(get_string('error_no_options_available', 'local_bulkenrol'));
}

$selectoptions = [];
foreach ($availablefieldsarray as $fieldoption) {
$selectoptions[$fieldoption] = $this->get_fieldname($fieldoption);
}

// Format CSV, replace last , with 'or' and add spaces after remaining.
$fieldnamestring = implode(', ', $selectoptions);
$formattedfieldnamestring = $this->str_last_replace(', ', ' ' . get_string('or', 'local_bulkenrol') . ' ', $fieldnamestring);

// Infotext.
$msg = get_string('bulkenrol_form_intro', 'local_bulkenrol');
$msg = get_string('bulkenrol_form_intro', 'local_bulkenrol', $formattedfieldnamestring);
$mform->addElement('html', '<div id="intro">'.$msg.'</div>');

// Textarea for Emails.
$mform->addElement('textarea', 'usermails',
get_string('usermails', 'local_bulkenrol'), 'wrap="virtual" rows="10" cols="80"');
$mform->addRule('usermails', null, 'required');
$mform->addHelpButton('usermails', 'usermails', 'local_bulkenrol');
// Helptext.
if ($availablefieldsarray[0] == 'u_username') {
$helpstringidentifier = 'userlist_username';
} else if ($availablefieldsarray[0] == 'u_idnumber') {
$helpstringidentifier = 'userlist_idnumber';
} else {
$helpstringidentifier = 'userlist_email';
}

$singleoption = count($availablefieldsarray) == 1;
if (!$singleoption) {
$mform->addElement('select', 'dbfield', get_string('choose_field', 'local_bulkenrol'), $selectoptions);
$listfieldtitle = get_string('userlist', 'local_bulkenrol');
} else {
$field = $availablefieldsarray[0];
$mform->addElement('hidden', 'dbfield');
$mform->setType('dbfield', PARAM_TEXT);
$mform->setDefault('dbfield', $field);
$listfieldtitle = get_string('userlist_singleoption', 'local_bulkenrol', $this->get_fieldname($field));
}
// Textarea for uservalues.
$mform->addElement('textarea', 'uservalues',
$listfieldtitle, 'wrap="virtual" rows="10" cols="80"');
$mform->addRule('uservalues', null, 'required');
$mform->addHelpButton('uservalues', $helpstringidentifier, 'local_bulkenrol');

// Add form content if the user came back to check his input.
$localbulkenroleditlist = optional_param('editlist', 0, PARAM_ALPHANUMEXT);
if (!empty($localbulkenroleditlist)) {
$localbulkenroldata = $localbulkenroleditlist.'_data';
if (!empty($localbulkenroldata) && !empty($SESSION->local_bulkenrol_inputs) &&
array_key_exists($localbulkenroldata, $SESSION->local_bulkenrol_inputs)) {
$formdatatmp = $SESSION->local_bulkenrol_inputs[$localbulkenroldata];
$mform->setDefault('usermails', $formdatatmp);
$formdatatmp = $SESSION->local_bulkenrol_inputs[$localbulkenroldata]['users'];
$dbfield = $SESSION->local_bulkenrol_inputs[$localbulkenroldata]['dbfield'];
$mform->setDefault('uservalues', $formdatatmp);
$mform->setDefault('dbfield', $dbfield);
}
}

Expand All @@ -89,10 +127,47 @@ protected function definition() {
public function validation($data, $files) {
$retval = array();

if (empty($data['usermails'])) {
$retval['usermails'] = get_string('error_usermails_empty', 'local_bulkenrol');
if (empty($data['uservalues'])) {
$retval['uservalues'] = get_string('error_list_empty', 'local_bulkenrol');
}

return $retval;
}

/**
* Returns the name of a fieldoption without its table prefix
* @param string $fieldoption fieldname with type prefix
* @return string name of field without type prefix
* @throws \UnexpectedValueException Field is not prefixed by c_ or u_
* @throws \dml_exception Database connection error
*/
private function get_fieldname($fieldoption) {
global $DB;
$fieldinfo = explode("_", $fieldoption, 2);
switch ($fieldinfo[0]) {
case "u":
return $fieldinfo[1];
case "c":
return $DB->get_field('user_info_field', 'name', array("id" => intval($fieldinfo[1])));
default:
throw new \UnexpectedValueException("field is not from usertable (u_) or customfield (c_)");
}
}

/**
* Replaces the last occurence of the needle in a string.
* @param string $search needle to search for
* @param string $replace string replacement for needle
* @param string $subject string subject string to search
* @return string subject string with the last occurence of the needle replaced
*/
private function str_last_replace($search, $replace, $subject) {
$pos = strrpos($subject, $search);

if ($pos !== false) {
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}

return $subject;
}
}
21 changes: 14 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,32 @@
if (empty($localbulkenrolkey)) {
$form = new bulkenrol_form(null, array('courseid' => $id));
if ($formdata = $form->get_data()) {
$emails = $formdata->usermails;
$datafield = $formdata->dbfield;
$uservalues = $formdata->uservalues;
$courseid = $formdata->id;

$checkedmails = local_bulkenrol_check_user_mails($emails, $courseid);
$availablefieldsstring = get_config('local_bulkenrol', 'fieldoptions');
$availablefieldsarray = explode(',', $availablefieldsstring);
if (!in_array($datafield, $availablefieldsarray)) {
print_error('The provided datafield has not been approved by the administrator.', 'local_bulkenrol');
}

$checkedusers = local_bulkenrol_check_user_data($uservalues, $courseid, $datafield);

// Create local_bulkenrol array in Session.
if (!isset($SESSION->local_bulkenrol)) {
$SESSION->local_bulkenrol = array();
}
// Save data in Session.
$localbulkenrolkey = $courseid.'_'.time();
$SESSION->local_bulkenrol[$localbulkenrolkey] = $checkedmails;
$SESSION->local_bulkenrol[$localbulkenrolkey] = $checkedusers;

// Create local_bulkenrol_inputs array in session.
if (!isset($SESSION->local_bulkenrol_inputs)) {
$SESSION->local_bulkenrol_inputs = array();
}
$localbulkenroldata = $localbulkenrolkey.'_data';
$SESSION->local_bulkenrol_inputs[$localbulkenroldata] = $emails;
$SESSION->local_bulkenrol_inputs[$localbulkenroldata] = array('users' => $uservalues, 'dbfield' => $datafield);
} else if ($form->is_cancelled()) {
if (!empty($id)) {
redirect($CFG->wwwroot .'/course/view.php?id='.$id, '', 0);
Expand Down Expand Up @@ -130,13 +137,13 @@
}

// Show notification if there aren't any valid email addresses to enrol.
if (!empty($localbulkenroldata) && isset($localbulkenroldata->validemailfound) &&
empty($localbulkenroldata->validemailfound)) {
if (!empty($localbulkenroldata) && isset($localbulkenroldata->validusersfound) &&
empty($localbulkenroldata->validusersfound)) {
$a = new stdClass();
$url = new moodle_url('/local/bulkenrol/index.php', array('id' => $id, 'editlist' => $localbulkenrolkey));
$a->url = $url->out();
$notification = new \core\output\notification(
get_string('error_no_valid_email_in_list', 'local_bulkenrol', $a),
get_string('error_no_valid_data_in_list', 'local_bulkenrol', $a),
\core\output\notification::NOTIFY_WARNING);
$notification->set_show_closebutton(false);
echo $OUTPUT->render($notification);
Expand Down
38 changes: 25 additions & 13 deletions lang/en/local_bulkenrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,49 @@
defined('MOODLE_INTERNAL') || die();

$string['bulkenrol:enrolusers'] = 'Use user bulk enrolment';
$string['bulkenrol_form_intro'] = 'Here, you can bulk enrol users to your course. A user to be enrolled is identified by his e-mail address stored in his Moodle account.';
$string['bulkenrol_form_intro'] = 'Here, you can bulk enrol users to your course. A user to be enrolled is identified by their {$a}.';
$string['choose_field'] = 'Choose field to match list to';
$string['enrol_users_successful'] = 'User bulk enrolment successful';
$string['enrol_users'] = 'Enrol users';
$string['enrolinfo_headline'] = 'Enrolment details';
$string['enrolplugin'] = 'Enrolment plugin';
$string['enrolplugin_desc'] = 'The enrolment method to be used to bulk enrol the users. If the configured enrolment method is not active / added in the course when the users are bulk-enrolled, it is automatically added / activated.';
$string['error_enrol_users'] = 'There was a problem when enrolling the users to the course.';
$string['error_enrol_user'] = 'There was a problem when enrolling the user with e-mail <em>{$a->email}</em> to the course.';
$string['error_enrol_user'] = 'There was a problem when enrolling the user <em>{$a->data}</em> to the course.';
$string['error_exception_info'] = 'Exception information';
$string['error_getting_user_for_email'] = 'There was a problem when getting the user record for e-mail address <em>{$a}</em> from the database.';
$string['error_getting_user_for_data'] = 'There was a problem when getting the user record for <em>{$a}</em> from the database.';
$string['error_group_add_members'] = 'There was a problem when adding the users to the course group(s).';
$string['error_group_add_member'] = 'There was a problem when adding the user with e-mail <em>{$a->email}</em> to the course group <em>{$a->group}</em>.';
$string['error_invalid_email'] = 'Invalid e-mail address found in line {$a->row} (<em>{$a->email}</em>). This line will be ignored.';
$string['error_more_than_one_record_for_email'] = 'More than one existing Moodle user account with e-mail address <em>{$a}</em>em> found.<br /> This line will be ignored, none of the existing Moodle users will be enrolled.';
$string['error_no_email'] = 'No e-mail address found in line {$a->line} (<em>{$a->content}</em>). This line will be ignored.';
$string['error_no_valid_email_in_list'] = 'No valid e-mail address was found in the given list.<br />Please <a href=\'{$a->url}\'>go back and check your input</a>.';
$string['error_no_record_found_for_email'] = 'No existing Moodle user account with e-mail address <em>{$a}</em>.<br />This line will be ignored, there won\'t be a Moodle user account created on-the-fly.';
$string['error_usermails_empty'] = 'List of e-mail addresses is empty. Please add at least one e-mail address.';
$string['error_check_is_already_member'] = 'Error checking if the user (<em>{$a->email}</em>) is already a member of group (<em>{$a->groupname}</em>). {$a->error}';
$string['error_group_add_member'] = 'There was a problem when adding the user <em>{$a->data}</em> to the course group <em>{$a->group}</em>.';
$string['error_more_than_one_record_for_data'] = 'More than one existing Moodle user account with {$a->field} <em>{$a->identifier}</em>em> found.<br /> This line will be ignored, none of the existing Moodle users will be enrolled.';
$string['error_no_valid_data_in_list'] = 'No valid entrys were found in the given list.<br />Please <a href=\'{$a->url}\'>go back and check your input</a>.';
$string['groupinfos_headline'] = 'Groups included in the list';
$string['group_name_headline'] = 'Group name';
$string['group_status_create'] = 'Group will be created';
$string['group_status_exists'] = 'Group already exists';
$string['group_status_headline'] = 'Group status';
$string['hints'] = 'Hints';
$string['error_no_data'] = 'No data found (<em>{$a}</em>). This line will be ignored.';
$string['error_no_record_found_for_data'] = 'No existing Moodle user account <em>{$a}</em> was found.<br />This line will be ignored, there won\'t be a Moodle user account created on-the-fly.';
$string['error_no_options_available'] = 'Your administrator has disabled all options. Please contact your administrator';
$string['error_list_empty'] = 'List is empty. Please add at least one fieldvalue';
$string['error_check_is_already_member'] = 'Error checking if the user (<em>{$a->data}</em>) is already a member of group (<em>{$a->groupname}</em>). {$a->error}';
$string['fieldoptions'] = 'Fieldoptions';
$string['fieldoptions_desc'] = 'Fields, that teachers can use as identifier to enrol students by.';
$string['pluginname'] = 'User bulk enrolment';
$string['or'] = 'or';
$string['privacy:metadata'] = 'The user bulk enrolment plugin acts as a tool to enrol users into courses, but does not store any personal data.';
$string['role'] = 'Role';
$string['role_assigned'] = 'Assigned role';
$string['role_description'] = 'The role to be used to bulk enrol the users.';
$string['row'] = 'Row';
$string['usermails'] = 'List of e-mail addresses';
$string['usermails_help'] = 'To enrol an existing Moodle user into this course, add his e-mail address to this form, one user / e-mail address per line.<br /><br />Example:<br />[email protected]<br />[email protected]<br /><br />Optionally, you are able to create groups and add the enrolled users to the groups. All you have to do is to add a heading line with a hash sign and the group\'s name, separating the list of users.<br /><br />Example:<br /># Group 1<br />[email protected]<br />[email protected]<br /># Group 2<br />[email protected]<br />[email protected]';
$string['userlist'] = 'List of users identified by your chosen field';
$string['userlist_singleoption'] = 'List of users identified by their {$a}';
$string['userlist_email'] = 'data input';
$string['userlist_username'] = 'data input';
$string['userlist_idnumber'] = 'data input';
$string['userlist_email_help'] = 'To enrol an existing Moodle user into this course, choose a field to identify the user by and add the identifier to the list. <br /><br />Example for field "email" :<br />[email protected]<br />[email protected]<br /><br />Optionally, you are able to create groups and add the enrolled users to the groups. All you have to do is to add a heading line with a hash sign and the group\'s name, separating the list of users.<br /><br />Example:<br /># Group 1<br />[email protected]<br />[email protected]<br /># Group 2<br />[email protected]<br />[email protected]';
$string['userlist_username_help'] = 'To enrol an existing Moodle user into this course, choose a field to identify the user by and add the identifier to the list. <br /><br />Example for field "username" :<br />alice<br />bob<br /><br />Optionally, you are able to create groups and add the enrolled users to the groups. All you have to do is to add a heading line with a hash sign and the group\'s name, separating the list of users.<br /><br />Example:<br /># Group 1<br />alice<br />bob<br /># Group 2<br />carol<br />dave';
$string['userlist_idnumber_help'] = 'To enrol an existing Moodle user into this course, choose a field to identify the user by and add the identifier to the list. <br /><br />Example for field "idnumber" :<br />1001<br />1002<br /><br />Optionally, you are able to create groups and add the enrolled users to the groups. All you have to do is to add a heading line with a hash sign and the group\'s name, separating the list of users.<br /><br />Example:<br /># Group 1<br />1001<br />1002<br /># Group 2<br />1003<br />1004';
$string['users_to_enrol_in_course'] = 'Users to be enrolled into the course';
$string['user_enroled'] = 'User enrolment';
$string['user_enroled_yes'] = 'User will be enrolled';
Expand All @@ -67,3 +77,5 @@
$string['user_groups_already'] = 'User is already group member';
$string['parameter_empty'] = 'Parameter empty';
$string['type_enrol'] = 'Enrolment method';
$string['identifying_data'] = 'Data';

Loading