Upload users - with cohorts

Upload users - with cohorts

by András Gazdag -
Number of replies: 15

Hi,

I ofen use "Upload users" option to register users on my Moodle site. I've upgraded to Moodle 2.0 and I want to use cohorts to manage enrolments.

There is any option to automatically put users in specific cohorts during user upload?
(like the course1 - group1 column method)

Average of ratings: -
In reply to András Gazdag

Svar: Upload users - with cohorts

by Steinar Smith -

I have the same problem with some 250 user and 6 different cohorts. I will try and find a workaround nd post it here if i succeed.

Steinar

In reply to András Gazdag

Re: Upload users - with cohorts

by Graham Paull -

Did you ever get this sorted ?...

In reply to Graham Paull

Re: Upload users - with cohorts

by Andrew Maughan -

I worked this out this afternoon.  With the following code modifications you can automatically register users to cohorts.

Edit admin/uploaduserlib.php at line 200:

} else if (preg_match('/^(course|group|type|role|enrolperiod)\d+$/', $lcfield)) {

to

} else if (preg_match('/^(course|group|type|role|enrolperiod|cohort)\d+$/', $lcfield)) {

Edit admin/uploaduser.php

After line 32 add:
require_once($CFG->dirroot.'/cohort/lib.php');

Edit admin/uploaduser.php at approximately line 700 add code below

// find cohort enrolments
foreach ($filecolumns as $column) {
  if(!preg_match('/^cohort\d+$/', $column)) {
    continue;
  }
  $i = substr($column, 6);
                                        
  if(empty($user->{'cohort'.$i})) {
    continue;
  }
                                        
  if (!empty($user->{'cohort'.$i})) {
    $cShortname = $user->{'cohort'.$i};
    // check if assigned to a cohort
    if($DB->record_exists('cohort', array('idnumber'=>$cShortname))) {
      // Get cohertId
      $cohortData = $DB->get_record('cohort',array('idnumber'=>$cShortname));
      cohort_add_member($cohortData->id, $user->id);
    }
  }
}

A sample CSV file would be:

firstname, lastname, username, cohort1, cohort2, cohort3
Joe, Bloggs, jbloggs, x, y, z

cohort1 etc must be the cohort id

In reply to Andrew Maughan

Re: Upload users - with cohorts

by Guillermo Madero -

This should definetely be integrated into the moodle code! Would a tracker need to be opened for this to happen, or what procedure should be followed?

In reply to Guillermo Madero

Re: Upload users - with cohorts

by Isabelle Langeveld -

I thought it was already integrated in 2.2? See http://tracker.moodle.org/browse/MDL-26965

But, I haven't found out how it works yet.

I get 'cohort1' is not a valid field name.

In reply to Isabelle Langeveld

Re: Upload users - with cohorts

by Gisele Brugger -
In reply to Gisele Brugger

Re: Upload users - with cohorts

by Isabelle Langeveld -

Thanks for your links. I skipped at least 1 step and that was creating the enrolment methods in the courses. But still I get the message: "cohort1" is not a valid field name when I try to upload the csv. I add the csv as an attachment and 2 screenshots of creating the cohorts and the enrollment methods. Maybe you can see where I go wrong.

What I've also done is:

  • enabled enrolment plugins: category enrolment en flat file csv
  • in Define roles allowed a student to be added to a categorie

What I hoped to create is a bulk upload file which would allow me to upload users, put them in the right cohort and enrol this cohort in a whole category of courses. But, maybe this is not possible?   

In reply to Isabelle Langeveld

Re: Upload users - with cohorts

by Gisele Brugger -

Yes, I found where the error is. in your file . csv line 3

username password firstname lastname email cohort1
444168                     Chantal        Bor                    van den                                                chades@hotmail.com   2011-1A

444168 Chantal Bor van den chades@hotmail.com 2011-1A

the collun cohort1= chades@hotmail.com   and this cohort dont exist

see collun email= van den

enable debbuging and  you will see the error messages

 

sucess

Average of ratings: Useful (1)
In reply to Gisele Brugger

Re: Upload users - with cohorts

by Isabelle Langeveld -

Mmm, yes it looks distorted in your post, but if I open it in Texteditor it looks like this:

username;password;firstname;lastname;email;cohort1 441193;;Brenda;van Hoogenvest;brendavh_1995@hotmail.com;2011-1A 444168;;Chantal;Bor, van den;chades@hotmail.com;2011-1A 448565;;Dijon;Rothuis;dylanvanbuuren6@hotmail.com;2011-1B 444539;;Dylan;van Buuren;evertjan_bogerd@hotmail.com;2011-1B

would that be right?

is it not a problem to leave out the password column? 

In reply to Isabelle Langeveld

Re: Upload users - with cohorts

by Gisele Brugger -
username password firstname lastname email cohort1  
441193   Brenda van Hoogenvest brendavh_1995@hotmail.com 2011-1A  
444168   Chantal Bor van den chades@hotmail.com 2011-1A
448565   Dijon Rothuis dylanvanbuuren6@hotmail.com 2011-1B  
444539   Dylan van Buuren evertjan_bogerd@hotmail.com 2011-1B  
             

see.. line 3 is incorrect. collumn have a address mail

problem to leave out the password column?  NO.. no problem..

Average of ratings: Useful (1)
In reply to Gisele Brugger

Re: Upload users - with cohorts

by Isabelle Langeveld -

Thank you. I'll improve the csv.

But, it might also help to upgrade tot 2.2! I found out I'm still on 2.1.2 (gggrrrr)

In reply to Isabelle Langeveld

Re: Upload users - with cohorts

by Hartmut Otto -

Dear Isabelle,

as I don't know wether your problem is solved I post my thoughts:

Line 3 has the following error: in the field "firstname" you put a comma to divide the real firstname " Bor" from "van den".

According to http://docs.moodle.org/22/en/Upload_users the comma is the problem:
"Commas within a field must be encoded as &#44 - the script will decode these back to commas." 

Using the comma in the way you did it will force the system do read your file as Gisele presented.

Hope this helps

Hartmut

In reply to Hartmut Otto

Re: Upload users - with cohorts

by barbara nance -

Hi

Like Isabelle I am having problems uploading cohorts. I have done created my cohort etc however when uploading the csv file I get a

404 - File or directory not found. warning which I know I normally get if something is incorrect on the file.

Now if I don't include the cohort1columme it uploads OK, so really am at a loss as to what I am doing which is causing the problem.

I hope some-one can help.

Cheers Barbara

In reply to barbara nance

Re: Upload users - with cohorts

by Mary Cooch -
Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

Can you confirm the cohort has been created beforehand on Moodle?

In reply to Mary Cooch

Re: Upload users - with cohorts

by barbara nance -

Hi Mary

Yes I created my cohort (cohort ID 2012-TLS) in advance.

Cheers

Barbara