Upload User with a predefined CSV rather than chosing the file vial filepicker

Upload User with a predefined CSV rather than chosing the file vial filepicker

by sas ss -
Number of replies: 8
hello, I am trying to make moodle communicate with an external system, I am running moodle 2.9, I know there are enrolment and authentication plugins from external database, but I would like to use the flat file upload for users Maybe the external connection/bridge failed for some reason), what I would like to know if there is a way to set the filepath and location rather tnan using the filepicker? this way the external system will dump a csv file and Moodle cron job will directly read/update from this csv without my intervention or input to select the file from filepicker and click upload....


Thank you

Average of ratings: -
In reply to sas ss

Re: Upload User with a predefined CSV rather than chosing the file vial filepicker

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

Seems like you would need something like /admin/tool/uploadcourse/cli/uploadcourse.php but for the tool_uploaduser. That would be a nice contribution to Moodle!

In reply to David Mudrák

Re: Upload User with a predefined CSV rather than chosing the file vial filepicker

by sas ss -
hello, thank you for your quick reply, seems like uploadcourse also uses the filepicker, the same way as upload user; is there a script that uses a predefined csv filepath to patch it?
In reply to sas ss

Re: Upload User with a predefined CSV rather than chosing the file vial filepicker

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The uploadcourse.php CLI script accepts a --file parameter, which is the CSV file it will process.  You could set up a cron job which calls it on a regular basis to process a file in a particular location, then removes it afterwards.

My metacourse link and tutor link blocks (which should really be admin tools, but were created before admin tools were a thing) have a slightly different way of doing things - You can upload a CSV file through a form, or set a path in the block's settings to to a CSV file which will be processed by the block's own cron function.

Either approach would work here, the CLI script approach used by tool_uploadcourse is more flexible, but requires more server config outside of Moodle itself compared using the plugin's own cron functions.

Average of ratings: Useful (1)
In reply to Mark Johnson

Re: Upload User with a predefined CSV rather than chosing the file vial filepicker

by Daniel Neis Araujo -
Picture of Core developers Picture of Plugin developers Picture of Translators

Hello,


just adding my two cents,

if you are using Linux, you may use incrontab (http://linux.die.net/man/5/incrontab),

to only run the script when a file is created of modified on a specified monitored directory,

using inotify events, instead of running a cron every minute, so you don't wast resources.

In reply to Daniel Neis Araujo

Re: Upload User with a predefined CSV rather than chosing the file vial filepicker

by sas ss -

Thank you for your help, and please excuse me, I didn't get it at first, plus I am a newbie; so what I need to do is create a cli folder in upload user, save the uploadcorse.php in it with a different name "autouploaduser.php" add the following 

require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');
require_once($CFG->dirroot.'/group/lib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
require_once('locallib.php');

and change the following on line 156 

$defaults = array();

$defaults['category'] = $options['category'];

$defaults['startdate'] = time() + 3600 * 24;

$defaults['newsitems'] = $courseconfig->newsitems;.....


to 

$defaults = array();

$defaults['username'] = $courseconfig->username;

$defaults['password'] = $courseconfig->password;

$defaults['firstname'] = $courseconfig->firstname;

$defaults['lastname'] = $courseconfig->lastname;

$defaults['email'] = $CFG->email;

$defaults['idnumber'] = $courseconfig->idnumber;


An amateur yet I need to ask this question how does this script would know what table-row it should write in? shall I change anything else to specify the table-row to indicate to mdl_user rather than mdl_course?

Thanks a million

In reply to sas ss

Re: Upload User with a predefined CSV rather than chosing the file vial filepicker

by sas ss -

So this is what I've done so far
removed     'category' => coursecat::get_default()->id, since I didn't know with what to replace it,

line35: $courseconfig with $userconfig and moodlecourse with moodleuser

script attached below, when I run it it gives me that its still looking for course rather than user since the error is "missing value for mandatory fields: fullname, category"

any suggestions?



In reply to sas ss

Re: Upload User with a predefined CSV rather than chosing the file vial filepicker

by Brian Pool -

I think I developed what you need.  I had done this some time ago for version 1.9 but just redid it for 3.1.

You simply drop the attached file in the admin/tool/uploaduser directory.  There are then 2 variables inside the script that you need to set.

$customfileLocation = "$CFG->dataroot/moodleimport/userimport.csv";
$defaultdomain = "@yourdomain.com";

The first is where your csv file sits.  Mine is a mount inside the moodledata folder that the csv is dropped into automatically from our student management system (DASL) each night.  The file conforms to the same format as the manual userupload in Moodle and the file is simply an edited version of that (from 3.1).

The second variable is for uploads without email fields.  It will automatically make all new user emails username@yourdomain.com so that the upload functions without emails.  This is the same as putting %u@yourdomain.com in the manual upload.

I then make this a nightly CRON job on my system.

I have been using this system for years.  Automatically makes and updates existing users, adds them to correct course, and into the correct groups.  Makes everything hands off once the courses are in Moodle.

Hope that helps!

I would add this as a upload but the upload feature does not allow me to upload something without it being a full mod, which this is obviously not, since it is a single file.


Brian


Average of ratings: Useful (2)
In reply to Brian Pool

Re: Upload User with a predefined CSV rather than chosing the file vial filepicker

by Luis de Vasconcelos -

Thanks Brian.

What columns do you need to have in the userimport.csv file?

Can you post and example .csv file?