Example #1
0
 /**
  * Close the grade importer file and optionally delete any temp files
  *
  * @param bool $delete
  */
 public function close($delete)
 {
     $this->csvreader->close();
     if ($delete) {
         $this->csvreader->cleanup();
     }
 }
Example #2
0
/**
 * Validation callback function - verified the column line of csv file.
 * Converts standard column names to lowercase.
 * @param csv_import_reader $cir
 * @param array $stdfields standard user fields
 * @param array $profilefields custom profile fields
 * @param moodle_url $returnurl return url in case of any error
 * @return array list of fields
 */
function uu_validate_user_upload_columns(csv_import_reader $cir, $stdfields, $profilefields, moodle_url $returnurl)
{
    $columns = $cir->get_columns();
    if (empty($columns)) {
        $cir->close();
        $cir->cleanup();
        print_error('cannotreadtmpfile', 'error', $returnurl);
    }
    if (count($columns) < 2) {
        $cir->close();
        $cir->cleanup();
        print_error('csvfewcolumns', 'error', $returnurl);
    }
    // test columns
    $processed = array();
    foreach ($columns as $key => $unused) {
        $field = $columns[$key];
        $lcfield = textlib::strtolower($field);
        if (in_array($field, $stdfields) or in_array($lcfield, $stdfields)) {
            // standard fields are only lowercase
            $newfield = $lcfield;
        } else {
            if (in_array($field, $profilefields)) {
                // exact profile field name match - these are case sensitive
                $newfield = $field;
            } else {
                if (in_array($lcfield, $profilefields)) {
                    // hack: somebody wrote uppercase in csv file, but the system knows only lowercase profile field
                    $newfield = $lcfield;
                } else {
                    if (preg_match('/^(cohort|course|group|type|role|enrolperiod)\\d+$/', $lcfield)) {
                        // special fields for enrolments
                        $newfield = $lcfield;
                    } else {
                        $cir->close();
                        $cir->cleanup();
                        print_error('invalidfieldname', 'error', $returnurl, $field);
                    }
                }
            }
        }
        if (in_array($newfield, $processed)) {
            $cir->close();
            $cir->cleanup();
            print_error('duplicatefieldname', 'error', $returnurl, $newfield);
        }
        $processed[$key] = $newfield;
    }
    return $processed;
}
Example #3
0
                    }
                    // for now, only for "latlong" and "url" fields, but that should better be looked up from
                    // $CFG->dirroot . '/mod/data/field/' . $field->type . '/field.class.php'
                    // once there is stored how many contents the field can have.
                    if (preg_match("/^(latlong|url)\$/", $field->type)) {
                        $values = explode(" ", $value, 2);
                        $content->content = $values[0];
                        $content->content1 = $values[1];
                    } else {
                        $content->content = $value;
                    }
                    $oldcontent = $DB->get_record('data_content', array('fieldid' => $field->id, 'recordid' => $recordid));
                    $content->id = $oldcontent->id;
                    $DB->update_record('data_content', $content);
                }
                $recordsadded++;
                print get_string('added', 'moodle', $recordsadded) . ". " . get_string('entry', 'data') . " (ID {$recordid})<br />\n";
            }
        }
        $cir->close();
        $cir->cleanup(true);
    }
}
if ($recordsadded > 0) {
    echo $OUTPUT->notification($recordsadded . ' ' . get_string('recordssaved', 'data'), '');
} else {
    echo $OUTPUT->notification(get_string('recordsnotsaved', 'data'), 'notifysuccess');
}
echo $OUTPUT->continue_button('import.php?d=' . $data->id);
/// Finish the page
echo $OUTPUT->footer();
Example #4
0
 /**
  * Validation callback function - verified the column line of csv file.
  * Converts standard column names to lowercase.
  * @param csv_import_reader $cir
  * @param array $fields standard user fields
  * @param moodle_url $returnurl return url in case of any error
  * @return array list of fields
  */
 static function validate_columns(csv_import_reader $cir, $fields, moodle_url $returnurl)
 {
     $columns = $cir->get_columns();
     $ignoredfields = $fields['ignored'];
     $stdfields = array_merge($fields['required'], $fields['optional']);
     if (empty($columns)) {
         $cir->close();
         $cir->cleanup();
         print_error('cannotreadtmpfile', 'error', $returnurl);
     }
     if (count($columns) < 2) {
         $cir->close();
         $cir->cleanup();
         print_error('csvfewcolumns', 'error', $returnurl);
     }
     // test columns
     $processed = array();
     foreach ($columns as $key => $unused) {
         $field = $columns[$key];
         $lcfield = core_text::strtolower($field);
         if (in_array($field, $stdfields) or in_array($lcfield, $stdfields)) {
             // standard fields are only lowercase
             $newfield = $lcfield;
         } else {
             if (in_array($field, $ignoredfields) or in_array($lcfield, $ignoredfields)) {
                 continue;
             } else {
                 $cir->close();
                 $cir->cleanup();
                 print_error('invalidfieldname', 'error', $returnurl, $field);
             }
         }
         if (in_array($newfield, $processed)) {
             $cir->close();
             $cir->cleanup();
         }
         $processed[$key] = $newfield;
     }
     foreach ($fields['required'] as $field) {
         $lcfield = core_text::strtolower($field);
         if (!in_array($lcfield, $processed)) {
             $cir->close();
             $cir->cleanup(true);
             print_error('fieldrequired', 'error', $returnurl, $lcfield);
         }
     }
     return $processed;
 }
 public function test_csv_functions()
 {
     global $CFG;
     $csvexport = new csv_export_writer();
     $csvexport->set_filename('unittest');
     foreach ($this->testdata as $data) {
         $csvexport->add_data($data);
     }
     $csvoutput = $csvexport->print_csv_data(true);
     $this->assertEquals($csvoutput, $this->teststring);
     $test_data = csv_export_writer::print_array($this->testdata, 'comma', '"', true);
     $this->assertEquals($test_data, $this->teststring);
     // Testing that the content is imported correctly.
     $iid = csv_import_reader::get_new_iid('lib');
     $csvimport = new csv_import_reader($iid, 'lib');
     $contentcount = $csvimport->load_csv_content($this->teststring, 'utf-8', 'comma');
     $csvimport->init();
     $dataset = array();
     $dataset[] = $csvimport->get_columns();
     while ($record = $csvimport->next()) {
         $dataset[] = $record;
     }
     $csvimport->cleanup();
     $csvimport->close();
     $this->assertEquals($dataset, $this->testdata);
     // Testing for the wrong count of columns.
     $errortext = get_string('csvweirdcolumns', 'error');
     $iid = csv_import_reader::get_new_iid('lib');
     $csvimport = new csv_import_reader($iid, 'lib');
     $contentcount = $csvimport->load_csv_content($this->teststring2, 'utf-8', 'comma');
     $importerror = $csvimport->get_error();
     $csvimport->cleanup();
     $csvimport->close();
     $this->assertEquals($importerror, $errortext);
     // Testing for empty content
     $errortext = get_string('csvemptyfile', 'error');
     $iid = csv_import_reader::get_new_iid('lib');
     $csvimport = new csv_import_reader($iid, 'lib');
     $contentcount = $csvimport->load_csv_content($this->teststring3, 'utf-8', 'comma');
     $importerror = $csvimport->get_error();
     $csvimport->cleanup();
     $csvimport->close();
     $this->assertEquals($importerror, $errortext);
     // Testing for a tab separated file.
     // The tab separated file has a trailing tab and extra blank lines at the end of the file.
     $filename = $CFG->dirroot . '/lib/tests/fixtures/tabfile.csv';
     $fp = fopen($filename, 'r');
     $tabdata = fread($fp, filesize($filename));
     fclose($fp);
     $iid = csv_import_reader::get_new_iid('tab');
     $csvimport = new csv_import_reader($iid, 'tab');
     $contentcount = $csvimport->load_csv_content($tabdata, 'utf-8', 'tab');
     // This should import four rows including the headings.
     $this->assertEquals($contentcount, 4);
     // Testing for empty lines.
     $iid = csv_import_reader::get_new_iid('blanklines');
     $csvimport = new csv_import_reader($iid, 'blanklines');
     $contentcount = $csvimport->load_csv_content($this->teststring4, 'utf-8', 'comma');
     // Five lines including the headings should be imported.
     $this->assertEquals($contentcount, 5);
 }
Example #6
0
 /**
  * @param array  $options associative delimiter,enclosure,encoding,updateexisting,settings
  */
 public function process_csv($data, $csvcontent, $options = null)
 {
     global $CFG, $DB;
     require_once "{$CFG->libdir}/csvlib.class.php";
     @set_time_limit(0);
     raise_memory_limit(MEMORY_EXTRA);
     $iid = \csv_import_reader::get_new_iid('moddataform');
     $cir = new \csv_import_reader($iid, 'moddataform');
     $delimiter = !empty($options['delimiter']) ? $options['delimiter'] : $this->_delimiter;
     $enclosure = !empty($options['enclosure']) ? $options['enclosure'] : $this->_enclosure;
     $encoding = !empty($options['encoding']) ? $options['encoding'] : $this->_encoding;
     $fieldsettings = !empty($options['settings']) ? $options['settings'] : array();
     $readcount = $cir->load_csv_content($csvcontent, $encoding, $delimiter);
     if (empty($readcount)) {
         $data->errors[] = $cir->get_error();
         return $data;
     }
     // Csv column headers.
     if (!($fieldnames = $cir->get_columns())) {
         $data->errors[] = $cir->get_error();
         return $data;
     }
     // Are we updating existing entries?
     $existingkeys = array();
     $keyname = null;
     if ($updateexisting = !empty($options['updateexisting'])) {
         if (isset($fieldnames['entryid'])) {
             $keyname = 'entryid';
         } else {
             $keyname = reset($fieldnames);
             if ($field = $this->df->field_manager->get_field_by_name($keyname)) {
                 $params = array('fieldid' => $field->id);
                 $existingkeys = $DB->get_records_menu('dataform_contents', $params, '', 'entryid,content');
             }
         }
     }
     // Are we adding the imported entries to every participant?
     $addperparticipant = (!empty($options['addperparticipant']) and $users = $this->df->grade_manager->get_gradebook_users());
     $i = 0;
     $cir->init();
     while ($csvrecord = $cir->next()) {
         $csvrecord = array_combine($fieldnames, $csvrecord);
         // Add the entry for every participant.
         if ($addperparticipant) {
             foreach ($users as $userid => $unused) {
                 // Set the entry id.
                 $i++;
                 $entryid = -$i;
                 $data->eids[$entryid] = $entryid;
                 $data->{"entry_{$entryid}_userid"} = $userid;
                 // Iterate the fields and collate their entry content.
                 foreach ($fieldsettings as $fieldid => $importsettings) {
                     $field = $this->df->field_manager->get_field_by_id($fieldid);
                     $data = $field->prepare_import_content($data, $importsettings, $csvrecord, $entryid);
                 }
             }
             continue;
         }
         // Get the entry id.
         $entryid = 0;
         if ($updateexisting and $keyname) {
             if ($keyname == 'entryid') {
                 if (!empty($csvrecord['entryid'])) {
                     $entryid = $csvrecord['entryid'];
                 }
             } else {
                 if ($existingkeys and !empty($csvrecord[$keyname])) {
                     $entryid = array_search($csvrecord[$keyname], $existingkeys);
                 }
             }
         }
         if (!$entryid) {
             $i++;
             $entryid = -$i;
         }
         $data->eids[$entryid] = $entryid;
         // Iterate the fields and collate their entry content.
         foreach ($fieldsettings as $fieldid => $importsettings) {
             $field = $this->df->field_manager->get_field_by_id($fieldid);
             $data = $field->prepare_import_content($data, $importsettings, $csvrecord, $entryid);
         }
     }
     $cir->cleanup(true);
     $cir->close();
     return $data;
 }
Example #7
0
    public function test_csv_functions() {
        $csvexport = new csv_export_writer();
        $csvexport->set_filename('unittest');
        foreach ($this->testdata as $data) {
            $csvexport->add_data($data);
        }
        $csvoutput = $csvexport->print_csv_data(true);
        $this->assertEquals($csvoutput, $this->teststring);

        $test_data = csv_export_writer::print_array($this->testdata, 'comma', '"', true);
        $this->assertEquals($test_data, $this->teststring);

        // Testing that the content is imported correctly.
        $iid = csv_import_reader::get_new_iid('lib');
        $csvimport = new csv_import_reader($iid, 'lib');
        $contentcount = $csvimport->load_csv_content($this->teststring, 'utf-8', 'comma');
        $csvimport->init();
        $dataset = array();
        $dataset[] = $csvimport->get_columns();
        while ($record = $csvimport->next()) {
            $dataset[] = $record;
        }
        $csvimport->cleanup();
        $csvimport->close();
        $this->assertEquals($dataset, $this->testdata);

        // Testing for the wrong count of columns.
        $errortext = get_string('csvweirdcolumns', 'error');
        $iid = csv_import_reader::get_new_iid('lib');
        $csvimport = new csv_import_reader($iid, 'lib');
        $contentcount = $csvimport->load_csv_content($this->teststring2, 'utf-8', 'comma');
        $importerror = $csvimport->get_error();
        $csvimport->cleanup();
        $csvimport->close();
        $this->assertEquals($importerror, $errortext);

        // Testing for empty content
        $errortext = get_string('csvemptyfile', 'error');

        $iid = csv_import_reader::get_new_iid('lib');
        $csvimport = new csv_import_reader($iid, 'lib');
        $contentcount = $csvimport->load_csv_content($this->teststring3, 'utf-8', 'comma');
        $importerror = $csvimport->get_error();
        $csvimport->cleanup();
        $csvimport->close();
        $this->assertEquals($importerror, $errortext);
    }
Example #8
0
/**
 * Validation callback function - verified the column line of csv file.
 * Converts column names to lowercase too.
 * @param csv_import_reader $cir
 * @param array standard user fields
 * @param array custom profile fields
 * @param moodle_url $returnurl return url in case of any error
 * @return array list of fields
 */
function uu_validate_user_upload_columns(csv_import_reader $cir, $stdfields, $frofilefields, moodle_url $returnurl)
{
    $columns = $cir->get_columns();
    if (empty($columns)) {
        $cir->close();
        $cir->cleanup();
        print_error('cannotreadtmpfile', 'error', $returnurl);
    }
    if (count($columns) < 2) {
        $cir->close();
        $cir->cleanup();
        print_error('csvfewcolumns', 'error', $returnurl);
    }
    // test columns
    $processed = array();
    foreach ($columns as $key => $unused) {
        $field = strtolower($columns[$key]);
        // no unicode expected here, ignore case
        if (!in_array($field, $stdfields) && !in_array($field, $frofilefields) && !preg_match('/^course\\d+$/', $field) && !preg_match('/^group\\d+$/', $field) && !preg_match('/^type\\d+$/', $field) && !preg_match('/^role\\d+$/', $field) && !preg_match('/^enrolperiod\\d+$/', $field)) {
            print_error('invalidfieldname', 'error', $returnurl, $field);
        }
        if (in_array($field, $processed)) {
            $cir->close();
            $cir->cleanup();
            print_error('duplicatefieldname', 'error', $returnurl, $field);
        }
        $processed[$key] = $field;
    }
    return $processed;
}
/**
 * Validation callback function - verified the column line of csv file.
 * Converts standard column names to lowercase.
 * @param csv_import_reader $cir
 * @param array $stdfields standard coursecategory fields
 * @param moodle_url $returnurl return url in case of any error
 * @return array list of fields
 */
function cc_validate_coursecategory_upload_columns(csv_import_reader $cir, $stdfields, moodle_url $returnurl)
{
    $columns = $cir->get_columns();
    if (empty($columns)) {
        $cir->close();
        $cir->cleanup();
        print_error('cannotreadtmpfile', 'error', $returnurl);
    }
    if (count($columns) < 2) {
        $cir->close();
        $cir->cleanup();
        print_error('csvfewcolumns', 'error', $returnurl);
    }
    // test columns
    $processed = array();
    foreach ($columns as $key => $unused) {
        $field = $columns[$key];
        $lcfield = core_text::strtolower($field);
        if (in_array($field, $stdfields) or in_array($lcfield, $stdfields)) {
            // standard fields are only lowercase
            $newfield = $lcfield;
        } else {
            $cir->close();
            $cir->cleanup();
            print_error('invalidfieldname', 'error', $returnurl, $field);
        }
        if (in_array($newfield, $processed)) {
            $cir->close();
            $cir->cleanup();
            print_error('duplicatefieldname', 'error', $returnurl, $newfield);
        }
        $processed[$key] = $newfield;
    }
    return $processed;
}
         $table->data[] = $line;
     }
     echo get_string('confirm_upload_help', 'block_upload_group');
     echo html_writer::table($table);
     // The confirm form.
     $data = array('id' => $course->id, 'iid' => $iid);
     $confirmform = new block_upload_group_confirm_form(null, $data);
     $confirmform->display();
     echo $OUTPUT->footer();
     break;
 case 'process_group_data':
     $iid = required_param('iid', PARAM_INT);
     $reader = new csv_import_reader($iid, 'upload_group');
     $form = new block_upload_group_confirm_form();
     if ($form->is_cancelled()) {
         $reader->cleanup();
         redirect($CFG->wwwroot . '/course/view.php?id=' . $course->id);
     }
     $formdata = $form->get_data();
     $selflib = new block_upload_group_lib();
     echo $OUTPUT->header();
     try {
         $result = $selflib->process_uploaded_groups($course, $reader, $formdata->role);
     } catch (Exception $e) {
         print_error('e_process_group', 'block_upload_group', $returnurl, array('msg' => $e->getMessage()));
     }
     // Output the result.
     echo $selflib->format_result($result);
     echo $OUTPUT->footer();
     break;
 default: