Skip to content

Commit

Permalink
MDL-26965 add support for uploading of users to cohorts via csv
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jul 9, 2011
1 parent f6f6138 commit 92b59a5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
44 changes: 44 additions & 0 deletions admin/uploaduser.php
Expand Up @@ -28,6 +28,7 @@
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');

Expand Down Expand Up @@ -172,6 +173,7 @@

// 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
Expand Down Expand Up @@ -699,6 +701,48 @@
}
}


// 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) {
Expand Down
2 changes: 1 addition & 1 deletion admin/uploaduserlib.php
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions lang/en/cohort.php
Expand Up @@ -43,10 +43,13 @@
$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';
$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}"';

0 comments on commit 92b59a5

Please sign in to comment.