From 92b59a56d19b2e9ea733a33d45af93842b195880 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sat, 9 Jul 2011 16:32:52 +0200 Subject: [PATCH] MDL-26965 add support for uploading of users to cohorts via csv --- admin/uploaduser.php | 44 +++++++++++++++++++++++++++++++++++++++++ admin/uploaduserlib.php | 2 +- lang/en/cohort.php | 3 +++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/admin/uploaduser.php b/admin/uploaduser.php index c2f4d520f8c..1a52a0cabf6 100644 --- a/admin/uploaduser.php +++ b/admin/uploaduser.php @@ -28,6 +28,7 @@ require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/csvlib.class.php'); require_once($CFG->dirroot.'/user/profile/lib.php'); require_once($CFG->dirroot.'/group/lib.php'); +require_once($CFG->dirroot.'/cohort/lib.php'); require_once('uploaduserlib.php'); require_once('uploaduser_form.php'); @@ -172,6 +173,7 @@ if ($formdata = $mform2->is_cancelled()) { // caches $ccache = array(); // course cache - do not fetch all courses here, we will not probably use them all anyway! + $cohorts = array(); $rolecache = uu_allowed_roles_cache(); // roles lookup cache $manualcache = array(); // cache of used manual enrol plugins in each course $supportedauths = uu_supported_auths(); // officially supported plugins that are enabled @@ -699,6 +701,48 @@ if ($formdata = $mform2->is_cancelled()) { } } + + // add to cohort first, it might trigger enrolments indirectly - do NOT create cohorts here! + foreach ($filecolumns as $column) { + if (!preg_match('/^cohort\d+$/', $column)) { + continue; + } + + if (!empty($user->$column)) { + $addcohort = $user->$column; + if (!isset($cohorts[$addcohort])) { + if (is_number($addcohort)) { + // only non-numeric idnumbers! + $cohort = $DB->get_record('cohort', array('id'=>$addcohort)); + } else { + $cohort = $DB->get_record('cohort', array('idnumber'=>$addcohort)); + } + + if (empty($cohort)) { + $cohorts[$addcohort] = get_string('unknowncohort', 'core_cohort', s($addcohort)); + } else if (!empty($cohort->component)) { + // cohorts synchronised with external sources must not be modified! + $cohorts[$addcohort] = get_string('external', 'core_cohort'); + } else { + $cohorts[$addcohort] = $cohort; + } + } + + if (is_object($cohorts[$addcohort])) { + $cohort = $cohorts[$addcohort]; + if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))) { + cohort_add_member($cohort->id, $user->id); + // we might add special column later, for now let's abuse enrolments + $upt->track('enrolments', get_string('useradded', 'core_cohort', s($cohort->name))); + } + } else { + // error message + $upt->track('enrolments', $cohorts[$addcohort], 'error'); + } + } + } + + // find course enrolments, groups, roles/types and enrol periods // this is again a special case, we always do this for any updated or created users foreach ($filecolumns as $column) { diff --git a/admin/uploaduserlib.php b/admin/uploaduserlib.php index 5fde2edd185..31ab7eccf52 100644 --- a/admin/uploaduserlib.php +++ b/admin/uploaduserlib.php @@ -197,7 +197,7 @@ function uu_validate_user_upload_columns(csv_import_reader $cir, $stdfields, $pr // hack: somebody wrote uppercase in csv file, but the system knows only lowercase profile field $newfield = $lcfield; - } else if (preg_match('/^(course|group|type|role|enrolperiod)\d+$/', $lcfield)) { + } else if (preg_match('/^(cohort|course|group|type|role|enrolperiod)\d+$/', $lcfield)) { // special fields for enrolments $newfield = $lcfield; diff --git a/lang/en/cohort.php b/lang/en/cohort.php index a7786d05050..44fabbd7e4e 100644 --- a/lang/en/cohort.php +++ b/lang/en/cohort.php @@ -43,6 +43,7 @@ $string['delconfirm'] = 'Do you really want to delete cohort \'{$a}\'?'; $string['description'] = 'Description'; $string['duplicateidnumber'] = 'Cohort with the same ID number already exists'; $string['editcohort'] = 'Edit cohort'; +$string['external'] = 'External cohort'; $string['idnumber'] = 'Cohort ID'; $string['memberscount'] = 'Cohort size'; $string['name'] = 'Name'; @@ -50,3 +51,5 @@ $string['nocomponent'] = 'Created manually'; $string['potusers'] = 'Potential users'; $string['potusersmatching'] = 'Potential matching users'; $string['selectfromcohort'] = 'Select members from cohort'; +$string['unknowncohort'] = 'Unknown cohort ({$a})!'; +$string['useradded'] = 'User added to cohort "{$a}"';