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.... the csv import file looks like this, can use a predefined filepath?

if ($proffields = $DB->get_records('user_info_field')) {
    foreach ($proffields as $key => $proffield) {
        $profilefieldname = 'profile_field_'.$proffield->shortname;
        $PRF_FIELDS[] = $profilefieldname;
        // Re-index $proffields with key as shortname. This will be
        // used while checking if profile data is key and needs to be converted (eg. menu profile field)
        $proffields[$profilefieldname] = $proffield;
        unset($proffields[$key]);
    }
}

if (empty($iid)) {
    $mform1 = new admin_uploaduser_form1();

    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploaduser');
        $cir = new csv_import_reader($iid, 'uploaduser');

        $content = $mform1->get_file_content('userfile');

        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        $csvloaderror = $cir->get_error();
        unset($content);

        if (!is_null($csvloaderror)) {
            print_error('csvloaderror', '', $returnurl, $csvloaderror);
        }
        // test if columns ok
        $filecolumns = uu_validate_user_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        // continue to form2
Member Avatar for diafol

I can't see why not. Obviously you need to ensure that moodle can reach your location.

I'll run throught the lines:

$iid = csv_import_reader::get_new_iid('uploaduser');

This returns a unique timestamp $iid - which is also the filename of the csv file to be used in the "csvimport/uploaduser/$USER->id" directory).

$cir = new csv_import_reader($iid, 'uploaduser');

The $cir object is set up awaiting data.

$content = $mform1->get_file_content('userfile');

The content of the CSV file is ripped out by the get_file_content() method, which, depending on the source, a simple file_get_contents() operation on the uploaded file: $_FILES['userfile']['tmp_name'].

$readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
$csvloaderror = $cir->get_error();
unset($content);
if (!is_null($csvloaderror)) {
    print_error('csvloaderror', '', $returnurl, $csvloaderror);
}
// test if columns ok
$filecolumns = uu_validate_user_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);

So in order to load the uploaded content to $cir, we may need to pass static values as $formdata->encoding and $formdata->delimiter_name.

Possible values for $encoding: probably 'utf-8', others in similar format
Possible values for $delimiter_name: 'comma','semicolon','colon','tab'

So from what I can see, you can do this:

$customfileLocation = 'path/to/filename.php';
$customEncoding = 'utf-8';
$customDelimiterName = 'comma';

if (empty($iid)) {
    //$mform1 = new admin_uploaduser_form1();
    //if ($formdata = $mform1->get_data()) {
      if(file_exists($customfileLocation)) {
        $iid = csv_import_reader::get_new_iid('uploaduser');
        $cir = new csv_import_reader($iid, 'uploaduser');
        //$content = $mform1->get_file_content('userfile');
        $content = file_get_contents($customfileLocation);
        //$readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        $readcount = $cir->load_csv_content($content, $customEncoding, $customDelimiterName);

However, not sure what follows so may get tangled later on. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.