Skip to content

Commit 5588f81

Browse files
author
marco
committed
update moodle-an-hochschulen#14 to Moodle 4.x
1 parent c2b9393 commit 5588f81

File tree

7 files changed

+410
-219
lines changed

7 files changed

+410
-219
lines changed

classes/bulkenrol_form.php

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,62 @@ protected function definition() {
5050

5151
$mform = $this->_form;
5252

53+
// Selector for database field to match list to.
54+
$availablefieldsstring = get_config('local_bulkenrol', 'fieldoptions');
55+
$availablefieldsarray = explode(',', $availablefieldsstring);
56+
if (count($availablefieldsarray) < 1 or $availablefieldsarray[0] == '') {
57+
print_error(get_string('error_no_options_available', 'local_bulkenrol'));
58+
}
59+
60+
$selectoptions = [];
61+
foreach ($availablefieldsarray as $fieldoption) {
62+
$selectoptions[$fieldoption] = $this->get_fieldname($fieldoption);
63+
}
64+
65+
// Format CSV, replace last , with 'or' and add spaces after remaining.
66+
$fieldnamestring = implode(', ', $selectoptions);
67+
$formattedfieldnamestring = $this->str_last_replace(', ', ' ' . get_string('or', 'local_bulkenrol') . ' ', $fieldnamestring);
68+
5369
// Infotext.
54-
$msg = get_string('bulkenrol_form_intro', 'local_bulkenrol');
70+
$msg = get_string('bulkenrol_form_intro', 'local_bulkenrol', $formattedfieldnamestring);
5571
$mform->addElement('html', '<div id="intro">'.$msg.'</div>');
5672

57-
// Textarea for Emails.
58-
$mform->addElement('textarea', 'usermails',
59-
get_string('usermails', 'local_bulkenrol'), 'wrap="virtual" rows="10" cols="80"');
60-
$mform->addRule('usermails', null, 'required');
61-
$mform->addHelpButton('usermails', 'usermails', 'local_bulkenrol');
73+
// Helptext.
74+
if ($availablefieldsarray[0] == 'u_username') {
75+
$helpstringidentifier = 'userlist_username';
76+
} else if ($availablefieldsarray[0] == 'u_idnumber') {
77+
$helpstringidentifier = 'userlist_idnumber';
78+
} else {
79+
$helpstringidentifier = 'userlist_email';
80+
}
81+
82+
$singleoption = count($availablefieldsarray) == 1;
83+
if (!$singleoption) {
84+
$mform->addElement('select', 'dbfield', get_string('choose_field', 'local_bulkenrol'), $selectoptions);
85+
$listfieldtitle = get_string('userlist', 'local_bulkenrol');
86+
} else {
87+
$field = $availablefieldsarray[0];
88+
$mform->addElement('hidden', 'dbfield');
89+
$mform->setType('dbfield', PARAM_TEXT);
90+
$mform->setDefault('dbfield', $field);
91+
$listfieldtitle = get_string('userlist_singleoption', 'local_bulkenrol', $this->get_fieldname($field));
92+
}
93+
// Textarea for uservalues.
94+
$mform->addElement('textarea', 'uservalues',
95+
$listfieldtitle, 'wrap="virtual" rows="10" cols="80"');
96+
$mform->addRule('uservalues', null, 'required');
97+
$mform->addHelpButton('uservalues', $helpstringidentifier, 'local_bulkenrol');
6298

6399
// Add form content if the user came back to check his input.
64100
$localbulkenroleditlist = optional_param('editlist', 0, PARAM_ALPHANUMEXT);
65101
if (!empty($localbulkenroleditlist)) {
66102
$localbulkenroldata = $localbulkenroleditlist.'_data';
67103
if (!empty($localbulkenroldata) && !empty($SESSION->local_bulkenrol_inputs) &&
68104
array_key_exists($localbulkenroldata, $SESSION->local_bulkenrol_inputs)) {
69-
$formdatatmp = $SESSION->local_bulkenrol_inputs[$localbulkenroldata];
70-
$mform->setDefault('usermails', $formdatatmp);
105+
$formdatatmp = $SESSION->local_bulkenrol_inputs[$localbulkenroldata]['users'];
106+
$dbfield = $SESSION->local_bulkenrol_inputs[$localbulkenroldata]['dbfield'];
107+
$mform->setDefault('uservalues', $formdatatmp);
108+
$mform->setDefault('dbfield', $dbfield);
71109
}
72110
}
73111

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

92-
if (empty($data['usermails'])) {
93-
$retval['usermails'] = get_string('error_usermails_empty', 'local_bulkenrol');
130+
if (empty($data['uservalues'])) {
131+
$retval['uservalues'] = get_string('error_list_empty', 'local_bulkenrol');
94132
}
95133

96134
return $retval;
97135
}
136+
137+
/**
138+
* Returns the name of a fieldoption without its table prefix
139+
* @param string $fieldoption fieldname with type prefix
140+
* @return string name of field without type prefix
141+
* @throws \UnexpectedValueException Field is not prefixed by c_ or u_
142+
* @throws \dml_exception Database connection error
143+
*/
144+
private function get_fieldname($fieldoption) {
145+
global $DB;
146+
$fieldinfo = explode("_", $fieldoption, 2);
147+
switch ($fieldinfo[0]) {
148+
case "u":
149+
return $fieldinfo[1];
150+
case "c":
151+
return $DB->get_field('user_info_field', 'name', array("id" => intval($fieldinfo[1])));
152+
default:
153+
throw new \UnexpectedValueException("field is not from usertable (u_) or customfield (c_)");
154+
}
155+
}
156+
157+
/**
158+
* Replaces the last occurence of the needle in a string.
159+
* @param string $search needle to search for
160+
* @param string $replace string replacement for needle
161+
* @param string $subject string subject string to search
162+
* @return string subject string with the last occurence of the needle replaced
163+
*/
164+
private function str_last_replace($search, $replace, $subject) {
165+
$pos = strrpos($subject, $search);
166+
167+
if ($pos !== false) {
168+
$subject = substr_replace($subject, $replace, $pos, strlen($search));
169+
}
170+
171+
return $subject;
172+
}
98173
}

index.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,32 @@
5959
if (empty($localbulkenrolkey)) {
6060
$form = new bulkenrol_form(null, array('courseid' => $id));
6161
if ($formdata = $form->get_data()) {
62-
$emails = $formdata->usermails;
62+
$datafield = $formdata->dbfield;
63+
$uservalues = $formdata->uservalues;
6364
$courseid = $formdata->id;
6465

65-
$checkedmails = local_bulkenrol_check_user_mails($emails, $courseid);
66+
$availablefieldsstring = get_config('local_bulkenrol', 'fieldoptions');
67+
$availablefieldsarray = explode(',', $availablefieldsstring);
68+
if (!in_array($datafield, $availablefieldsarray)) {
69+
print_error('The provided datafield has not been approved by the administrator.', 'local_bulkenrol');
70+
}
71+
72+
$checkedusers = local_bulkenrol_check_user_data($uservalues, $courseid, $datafield);
6673

6774
// Create local_bulkenrol array in Session.
6875
if (!isset($SESSION->local_bulkenrol)) {
6976
$SESSION->local_bulkenrol = array();
7077
}
7178
// Save data in Session.
7279
$localbulkenrolkey = $courseid.'_'.time();
73-
$SESSION->local_bulkenrol[$localbulkenrolkey] = $checkedmails;
80+
$SESSION->local_bulkenrol[$localbulkenrolkey] = $checkedusers;
7481

7582
// Create local_bulkenrol_inputs array in session.
7683
if (!isset($SESSION->local_bulkenrol_inputs)) {
7784
$SESSION->local_bulkenrol_inputs = array();
7885
}
7986
$localbulkenroldata = $localbulkenrolkey.'_data';
80-
$SESSION->local_bulkenrol_inputs[$localbulkenroldata] = $emails;
87+
$SESSION->local_bulkenrol_inputs[$localbulkenroldata] = array('users' => $uservalues, 'dbfield' => $datafield);
8188
} else if ($form->is_cancelled()) {
8289
if (!empty($id)) {
8390
redirect($CFG->wwwroot .'/course/view.php?id='.$id, '', 0);
@@ -130,13 +137,13 @@
130137
}
131138

132139
// Show notification if there aren't any valid email addresses to enrol.
133-
if (!empty($localbulkenroldata) && isset($localbulkenroldata->validemailfound) &&
134-
empty($localbulkenroldata->validemailfound)) {
140+
if (!empty($localbulkenroldata) && isset($localbulkenroldata->validusersfound) &&
141+
empty($localbulkenroldata->validusersfound)) {
135142
$a = new stdClass();
136143
$url = new moodle_url('/local/bulkenrol/index.php', array('id' => $id, 'editlist' => $localbulkenrolkey));
137144
$a->url = $url->out();
138145
$notification = new \core\output\notification(
139-
get_string('error_no_valid_email_in_list', 'local_bulkenrol', $a),
146+
get_string('error_no_valid_data_in_list', 'local_bulkenrol', $a),
140147
\core\output\notification::NOTIFY_WARNING);
141148
$notification->set_show_closebutton(false);
142149
echo $OUTPUT->render($notification);

lang/en/local_bulkenrol.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$string['bulkenrol:enrolusers'] = 'Use user bulk enrolment';
28-
$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.';
28+
$string['bulkenrol_form_intro'] = 'Here, you can bulk enrol users to your course. A user to be enrolled is identified by their {$a}.';
29+
$string['choose_field'] = 'Choose field to match list to';
2930
$string['enrol_users_successful'] = 'User bulk enrolment successful';
3031
$string['enrol_users'] = 'Enrol users';
3132
$string['enrolinfo_headline'] = 'Enrolment details';
3233
$string['enrolplugin'] = 'Enrolment plugin';
3334
$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.';
3435
$string['error_empty_line'] = 'Line {$a->line} is empty and will be ignored.';
3536
$string['error_enrol_users'] = 'There was a problem when enrolling the users to the course.';
36-
$string['error_enrol_user'] = 'There was a problem when enrolling the user with e-mail <em>{$a->email}</em> to the course.';
37+
$string['error_enrol_user'] = 'There was a problem when enrolling the user <em>{$a->data}</em> to the course.';
3738
$string['error_exception_info'] = 'Exception information';
38-
$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.';
39+
$string['error_getting_user_for_data'] = 'There was a problem when getting the user record for <em>{$a}</em> from the database.';
3940
$string['error_group_add_members'] = 'There was a problem when adding the users to the course group(s).';
4041
$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>.';
4142
$string['error_invalid_email'] = 'Invalid e-mail address found in line {$a->row} (<em>{$a->email}</em>). This line will be ignored.';
@@ -51,14 +52,28 @@
5152
$string['group_status_exists'] = 'Group already exists';
5253
$string['group_status_headline'] = 'Group status';
5354
$string['hints'] = 'Hints';
55+
$string['error_no_data'] = 'No data found (<em>{$a}</em>). This line will be ignored.';
56+
$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.';
57+
$string['error_no_options_available'] = 'Your administrator has disabled all options. Please contact your administrator';
58+
$string['error_list_empty'] = 'List is empty. Please add at least one fieldvalue';
59+
$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}';
60+
$string['fieldoptions'] = 'Fieldoptions';
61+
$string['fieldoptions_desc'] = 'Fields, that teachers can use as identifier to enrol students by.';
5462
$string['pluginname'] = 'User bulk enrolment';
63+
$string['or'] = 'or';
5564
$string['privacy:metadata'] = 'The user bulk enrolment plugin acts as a tool to enrol users into courses, but does not store any personal data.';
5665
$string['role'] = 'Role';
5766
$string['role_assigned'] = 'Assigned role';
5867
$string['role_description'] = 'The role to be used to bulk enrol the users.';
5968
$string['row'] = 'Row';
60-
$string['usermails'] = 'List of e-mail addresses';
61-
$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]';
69+
$string['userlist'] = 'List of users identified by your chosen field';
70+
$string['userlist_singleoption'] = 'List of users identified by their {$a}';
71+
$string['userlist_email'] = 'data input';
72+
$string['userlist_username'] = 'data input';
73+
$string['userlist_idnumber'] = 'data input';
74+
$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]';
75+
$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';
76+
$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';
6277
$string['users_to_enrol_in_course'] = 'Users to be enrolled into the course';
6378
$string['user_enroled'] = 'User enrolment';
6479
$string['user_enroled_yes'] = 'User will be enrolled';
@@ -68,3 +83,5 @@
6883
$string['user_groups_already'] = 'User is already group member';
6984
$string['parameter_empty'] = 'Parameter empty';
7085
$string['type_enrol'] = 'Enrolment method';
86+
$string['identifying_data'] = 'Data';
87+

0 commit comments

Comments
 (0)