Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Add option to force creation of manual enrollment #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
4 changes: 4 additions & 0 deletions import_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions lang/en/local_userenrols.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
19 changes: 17 additions & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down