MDL-26965 add support for uploading of users to cohorts via csv
authorPetr Skoda <commits@skodak.org>
Sat, 9 Jul 2011 14:32:52 +0000 (16:32 +0200)
committerPetr Skoda <commits@skodak.org>
Sat, 9 Jul 2011 14:32:52 +0000 (16:32 +0200)
admin/uploaduser.php
admin/uploaduserlib.php
lang/en/cohort.php

index c2f4d52..1a52a0c 100644 (file)
@@ -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) {
index 5fde2ed..31ab7ec 100644 (file)
@@ -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;
 
index a7786d0..44fabbd 100644 (file)
@@ -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}"';