Automatic user creation and updation through a csv file.

Automatic user creation and updation through a csv file.

by Adi Gupta -
Number of replies: 8

Hi All,

I have to create  and update user by csv file in moodle 2.7 not by uploaded the file its done by automatic process.

Exampleblush have hardcoded path of csv file like $CFG->dataroot/import_csv/user.csv how can i parse the file and and create and update user in moodle also I want to check error validation and its also able to handle custom profile field as well. 

Please help me ...its urgent Thanks in Advance.

Average of ratings: Useful (1)
In reply to Adi Gupta

Re: Automatic user creation and updation through a csv file.

by Perry Way -

Hello Praveen,

You could use the php function fopen() to open the CSV file, then you could process the CSV line by line by calling fgetcsv() which will give you an array for each record. Then when at the end of the file you should close the file with fclose().

As for creating users, you could use the Moodle functions for that. I don't know how embellished you want to get with your validation routines so I will outline a few things I think might be useful for you.

In my case I have to verify if the user exists or not and we use email address as the primary key basically so I retrieve the user from Moodle's database using the global $DB object:

$user = $DB->get_record('user', array('email'=>$values["Email"]));

Then if the user does not exist, I need to create it, so I just make a new object and fill the values in, like so..

$user = new object();
$user->password = "whatever password you want";  
$user->idnumber = $values["StudentID"];
$user->firstname = $values["FirstName"];
$user->lastname = $values["LastName"];
$user->username = $values["Username"];
$user->phone1 = $values["HomePhone"];
$user->phone2 = $values["MobilePhone"];
$user->email = $values["Email"];

Then I make a call to create that user:

$new_userid = user_create_user($user, false, false); 

Then I want to make sure they change their password after first login:

set_user_preference('auth_forcepasswordchange', 1, $new_user);

In order to access the manual enrollment methods you need to make a call to retrieve or get the enrollment plugin. This is how I do it:

$plugin = enrol_get_plugin('manual');

Then I want to enroll the user into their proper course, so I use the enrollment plugin to call the enrol_user() method:

$plugin->enrol_user($course_instance, $user->id, $role->id);

$course_instance is gotten in this kind of fashion. I am leaving out the checking whether $course = true in this example because it is code that is mixed in with some other proprietary stuff which I don't want to reveal, but you want to make sure that $course is not null. We use the course's Id Number:

$course = $DB->get_record('course', array('idnumber'=>$values["CourseIdNumber"]));
$course_instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'), '*', MUST_EXIST);


Average of ratings: Useful (1)
In reply to Perry Way

Re: Automatic user creation and updation through a csv file.

by Adi Gupta -

Thanks for your reply I have already done it.

many many thanks

In reply to Perry Way

Re: Automatic user creation and updation through a csv file.

by Adi Gupta -

Hi Perry,

How I can set the cron in local plugin lib file so that cron run every day   ,weekly ,monthly ,yearly not with task api.


Please help 

THANKS IN ADVANCE.

In reply to Adi Gupta

Re: Automatic user creation and updation through a csv file.

by Perry Way -

Hi Jay,

Well, I have written some modules and built the stubs for all the functionality including the cron aspects but I decided in my case not to use the built-in cron capabilities since only one of my modules needs this kind of thing and I needed to first call a Windows console to generate my data files so I decided not to implement using cron. But what I can tell you is that in the module makeup, in the root folder of the module is a file you make call lib.php and in that lib.php file if you create a function with the proper naming, i.e. <module name>_cron(), that it will be called by Moodle's cron mechanism once you have that module (plugin) installed properly. 

In reply to Adi Gupta

Re: Automatic user creation and updation through a csv file.

by Perry Way -

Also, some extra information can be found here:

https://docs.moodle.org/dev/version.php

Your module's version.php file has a property associated to cron capabilities. If the cron property is set to 0, then cron will be disabled. If it's set to a value higher than 0 it will be how many seconds minimum for the cron to occur. That applies to modules for Moodle prior to 2.7 which I think is what you were asking for.

In reply to Perry Way

Re: Moodle in English: Re: Automatic user creation and updation through a csv file.

by Adi Gupta -
Hi Perry ,



I have set cron =300 in version file  that means every 5 minutes later cron

will run  by default but i want to add more filter like if i choose value

from form that daily at 4:30 pm cron will run and the same for

weekly,monthly,yearly how can i filter in lib file.

Please reply .
In reply to Adi Gupta

Re: Moodle in English: Re: Automatic user creation and updation through a csv file.

by Perry Way -

Hmm, well if I wanted to do something like that, I would make a support table in the database to store when each execution of the cron occurred. When the cron method runs, it runs in a global sense, not in the context of a specific instance of a module so you'll need to invent some way to store the information you need in order to evaluate whether or not it's the right time to execute whatever it is you want to put in the method.

I think this is one reason why the scheduled task API was created, so as to provide a generic way to schedule some tasks more creatively than just simply every n minutes. I think for your needs though you'll need to create your own mechanism, and I think an extra table might be the better idea that way you have a history of each execution from which to determine if it's time or not to execute again.

Average of ratings: Useful (1)
In reply to Perry Way

Re: Moodle in English: Re: Automatic user creation and updation through a csv file.

by Adi Gupta -

Thanks man,

Now I want to run cron daily 6 am and 6 pm how i can achieve this ?

Plz reply .