From 28399d4557e69ddf21996eb9b6edb1721a13b87b Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Tue, 18 Jan 2022 14:08:21 +0800 Subject: [PATCH] Add option to force creation of manual enrollment --- import.php | 4 +++- import_form.php | 4 ++++ lang/en/local_userenrols.php | 2 ++ lib.php | 19 +++++++++++++++++-- version.php | 2 +- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/import.php b/import.php index 79a0f06..d9a9a78 100644 --- a/import.php +++ b/import.php @@ -148,13 +148,15 @@ ? 0 : intval($formdata->{local_userenrols_plugin::FORMID_GROUP_ID}); $group_create = empty($formdata->{local_userenrols_plugin::FORMID_GROUP_CREATE}) ? 0 : intval($formdata->{local_userenrols_plugin::FORMID_GROUP_CREATE}); + $force = empty($formdata->{local_userenrols_plugin::FORMID_ENROLL_FORCE_CREATE}) + ? 0 : intval($formdata->{local_userenrols_plugin::FORMID_ENROLL_FORCE_CREATE}); // Leave the file in the user's draft area since we // will not plan to keep it after processing $area_files = get_file_storage()->get_area_files($user_context->id, 'user', 'draft', $formdata->{local_userenrols_plugin::FORMID_FILES}, null, false); $result = local_userenrols_plugin::import_file($COURSE, $manual_enrol_instance, $user_id_field, $role_id, $canmanagegroups ? (boolean)$group_assign : false, - $group_id, (boolean)$group_create, array_shift($area_files)); + $group_id, (boolean)$group_create, array_shift($area_files), (boolean)$force); // Clean up the file area get_file_storage()->delete_area_files($user_context->id, 'user', 'draft', $formdata->{local_userenrols_plugin::FORMID_FILES}); diff --git a/import_form.php b/import_form.php index ab9178e..4d22b80 100644 --- a/import_form.php +++ b/import_form.php @@ -77,6 +77,10 @@ public function definition() $this->_form->addHelpButton(local_userenrols_plugin::FORMID_ROLE_ID, 'LBL_ROLE_ID', local_userenrols_plugin::PLUGIN_NAME); $this->_form->disabledIf(local_userenrols_plugin::FORMID_ROLE_ID, local_userenrols_plugin::FORMID_METACOURSE, 'eq', '1'); + // Force new manual enrolment creation + $this->_form->addElement('selectyesno', local_userenrols_plugin::FORMID_ENROLL_FORCE_CREATE, get_string('LBL_ENROLL_FORCE_CREATE', local_userenrols_plugin::PLUGIN_NAME)); + $this->_form->setDefault(local_userenrols_plugin::FORMID_ENROLL_FORCE_CREATE, 0); + $this->_form->addHelpButton(local_userenrols_plugin::FORMID_ENROLL_FORCE_CREATE, 'LBL_ENROLL_FORCE_CREATE', local_userenrols_plugin::PLUGIN_NAME); // Conditionally based on user capability if ($this->_customdata['data']->canmanagegroups) { diff --git a/lang/en/local_userenrols.php b/lang/en/local_userenrols.php index e2a2812..7744719 100644 --- a/lang/en/local_userenrols.php +++ b/lang/en/local_userenrols.php @@ -40,6 +40,8 @@ $string['LBL_IMPORT'] = 'Import'; $string['LBL_IDENTITY_OPTIONS'] = 'User Identity'; $string['LBL_ENROLL_OPTIONS'] = 'Enrollment Options'; + $string['LBL_ENROLL_FORCE_CREATE'] = 'Force creation of new manual enrollment'; + $string['LBL_ENROLL_FORCE_CREATE_help'] = 'If set to yes, a new manual enrollment will be created regardless of whether or not the user has an existing role in the course.'; $string['LBL_GROUP_OPTIONS'] = 'Group Options'; $string['LBL_FILE_OPTIONS'] = 'Import File'; $string['LBL_FILE_OPTIONS_help'] = 'Upload or pick from a repository a delimited data file with user and optional group information. File should have either a .txt or .csv extension.'; diff --git a/lib.php b/lib.php index 8e97d20..e023a6a 100644 --- a/lib.php +++ b/lib.php @@ -106,6 +106,11 @@ class local_userenrols_plugin */ const FORMID_ROLE_ID = 'role_id'; + /** + * @const string Form id for enroll_force_create. + */ + const FORMID_ENROLL_FORCE_CREATE = 'enrol_force_create'; + /** * @const string Form id for user_id (key field to match). */ @@ -196,7 +201,17 @@ public static function get_user_id_field_options() * * @uses $DB */ - public static function import_file(stdClass $course, stdClass $enrol_instance, $ident_field, $role_id, $group_assign, $group_id, $group_create, stored_file $import_file) + public static function import_file( + stdClass $course, + stdClass $enrol_instance, + $ident_field, + $role_id, + $group_assign, + $group_id, + $group_create, + stored_file $import_file, + bool $forcemanualenrollment = false + ) { global $DB; @@ -310,7 +325,7 @@ public static function import_file(stdClass $course, stdClass $enrol_instance, $ // If a user has a role in this course, then we leave it alone and move on // to the group assignment if there is one. If they have no role, then we // should go ahead and add one, as long as it is not a metacourse. - if (!$roles && $role_id > 0) { + if ((!$roles && $role_id > 0) || $forcemanualenrollment) { if ($metacourse) { $result .= sprintf(get_string('ERR_ENROLL_META', self::PLUGIN_NAME), $line_num, $ident_value); } else { diff --git a/version.php b/version.php index daacec5..02744a9 100644 --- a/version.php +++ b/version.php @@ -32,7 +32,7 @@ $plugin = new stdClass(); - $plugin->version = 2018052010; + $plugin->version = 2022012000; $plugin->requires = 2017111300; $plugin->release = "0.0.10_34 (Build 2021053100)"; $plugin->component = 'local_userenrols';