Esempio n. 1
0
 /**
  * Load up the above text through the csv import.
  *
  * @param string $content Text to be imported into the gradebook.
  * @return array All text separated by commas now in an array.
  */
 protected function csv_load($content)
 {
     // Import the csv strings.
     $this->iid = csv_import_reader::get_new_iid('grade');
     $this->csvimport = new csv_import_reader($this->iid, 'grade');
     $this->csvimport->load_csv_content($content, 'utf8', 'comma');
     $this->columns = $this->csvimport->get_columns();
     $this->csvimport->init();
     while ($line = $this->csvimport->next()) {
         $testarray[] = $line;
     }
     return $testarray;
 }
Esempio n. 2
0
 public function execute()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/csvlib.class.php';
     require_once $CFG->libdir . '/moodlelib.php';
     $csvfilepath = $this->arguments[0];
     if ($csvfilepath[0] != '/') {
         $csvfilepath = $this->cwd . DIRECTORY_SEPARATOR . $csvfilepath;
     }
     $iid = \csv_import_reader::get_new_iid('userprofile');
     $type = 'userprofile';
     $csvreader = new \csv_import_reader($iid, $type);
     if (false === ($csvfile = file_get_contents($csvfilepath))) {
         cli_error('Unable to load csv file. ' . error_get_last()['message']);
     }
     if (!$csvreader->load_csv_content($csvfile, 'utf-8', 'comma')) {
         cli_error('Unalbe to parse csv file. ' . $csvreader->get_error());
     }
     if (!$csvreader->init()) {
         cli_error('Unable to initialise csv reading');
     }
     $columns = $csvreader->get_columns();
     $columnsids = array_flip($columns);
     while (false !== ($row = $csvreader->next())) {
         $category = $this->get_or_create_category($row[$columnsids['categoryname']], $row[$columnsids['categorysortorder']]);
         $userfield = new \stdClass();
         $userfield->shortname = $row[$columnsids['shortname']];
         $userfield->name = $row[$columnsids['name']];
         $userfield->datatype = $row[$columnsids['datatype']];
         $userfield->description = $row[$columnsids['description']];
         $userfield->descriptionformat = $row[$columnsids['descriptionformat']];
         $userfield->categoryid = $category->id;
         $userfield->sortorder = $row[$columnsids['sortorder']];
         $userfield->required = $row[$columnsids['required']];
         $userfield->locked = $row[$columnsids['locked']];
         $userfield->visible = $row[$columnsids['visible']];
         $userfield->forceunique = $row[$columnsids['forceunique']];
         $userfield->signup = $row[$columnsids['signup']];
         $userfield->defaultdata = $row[$columnsids['defaultdata']];
         $userfield->defaultdataformat = $row[$columnsids['defaultdataformat']];
         $userfield->param1 = $row[$columnsids['param1']];
         $userfield->param2 = $row[$columnsids['param2']];
         $userfield->param3 = $row[$columnsids['param3']];
         $userfield->param4 = $row[$columnsids['param4']];
         $userfield->param5 = $row[$columnsids['param5']];
         $this->get_or_create_userfield($userfield);
     }
 }
Esempio n. 3
0
 /**
  * Load CSV content for previewing.
  *
  * @param string $text The grade data being imported.
  * @param string $encoding The type of encoding the file uses.
  * @param string $separator The separator being used to define each field.
  * @param int $previewrows How many rows are being previewed.
  */
 public function load_csv_content($text, $encoding, $separator, $previewrows)
 {
     $this->raise_limits();
     $this->iid = csv_import_reader::get_new_iid('grade');
     $csvimport = new csv_import_reader($this->iid, 'grade');
     $csvimport->load_csv_content($text, $encoding, $separator);
     $this->error = $csvimport->get_error();
     // Get header (field names).
     $this->headers = $csvimport->get_columns();
     $this->trim_headers();
     $csvimport->init();
     $this->previewdata = array();
     for ($numlines = 0; $numlines <= $previewrows; $numlines++) {
         $lines = $csvimport->next();
         if ($lines) {
             $this->previewdata[] = $lines;
         }
     }
 }
 function validation($data, $files)
 {
     global $CFG;
     $errors = array();
     // Use csv importer from Moodle
     $iid = csv_import_reader::get_new_iid('emarking-predefined-comments');
     $reader = new csv_import_reader($iid, 'emarking-predefined-comments');
     $content = $data['comments'];
     $reader->load_csv_content($content, 'utf8', "tab");
     // Validate columns, minimum number and first two to be userid and attemptid
     if (count($reader->get_columns()) < 0) {
         $errors['comments'] = get_string('onecolumnrequired', 'mod_emarking');
     }
     $reader->init();
     $current = 0;
     while ($line = $reader->next()) {
         $current++;
     }
     if ($current < 1) {
         $errors['comments'] = get_string('twolinesrequired', 'mod_emarking');
     }
     return $errors;
 }
Esempio n. 5
0
$exams = $DB->get_records_sql($sql);
foreach ($exams as $exam) {
    $exam->examtype = strtolower($exam->examtype);
    $exam->lecturetype = strtolower($exam->lecturetype);
    $cname = $DB->get_field('local_clclasses', 'fullname', array('id' => $exam->csid));
    $cname = strtolower($cname);
    $PRF_FIELDS[$exam->csid . ':' . $exam->eid . ':' . $exam->lid] = $cname . ':' . $exam->examtype . ':' . $exam->lecturetype;
}
// print_object( $PRF_FIELDS);
//-------- if variable $iid equal to zero,it allows enter into the form-----------------------------------
if (empty($iid)) {
    $mform1 = new admin_grades_form1();
    if ($mform1->is_cancelled())
        redirect($returnurl);
    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploadgrades');
        $cir = new csv_import_reader($iid, 'uploadgrades'); //this class fromcsvlib.php(includes csv methods and clclasses)
        $content = $mform1->get_file_content('gradesfile');
        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else if ($readcount == 0) {
            print_error('csvemptyfile', 'error', $returnurl);
        }
        // test if columns ok(to validate the csv file content)
        $filecolumns = uu_validate_grades_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        // continue to form2
    } else {
        echo $OUTPUT->header();
        echo $OUTPUT->heading(get_string('managegradesubmission', 'local_gradesubmission'));
Esempio n. 6
0
echo html_writer::end_tag('div');
$mform->display();
*/
if (!$iid) {
    // If the import form has been submitted.
    if ($mform->is_cancelled()) {
        redirect($url);
    } else {
        if ($formdata = $mform->get_data()) {
            // Large files are likely to take their time and memory. Let PHP know
            // that we'll take longer, and that the process should be recycled soon
            // to free up memory.
            @set_time_limit(0);
            raise_memory_limit(MEMORY_EXTRA);
            $text = $mform->get_file_content('userfile');
            $iid = csv_import_reader::get_new_iid('rooms');
            $csvimport = new csv_import_reader($iid, 'rooms');
            $csvimport->load_csv_content($text, $formdata->encoding, $separator);
            // --- get header (field names) ---
            $header = $csvimport->get_columns();
            // Print a preview of the data.
            $numlines = 0;
            // 0 lines previewed so far.
            echo $OUTPUT->heading(get_string('importpreview', 'grades'));
            foreach ($header as $i => $h) {
                $h = trim($h);
                // Remove whitespace.
                $h = clean_param($h, PARAM_RAW);
                // Clean the header.
                $header[$i] = $h;
            }
Esempio n. 7
0
 $table->head = array(get_string('comment', 'mod_emarking'), get_string('creator', 'mod_emarking'), get_string('actions', 'mod_emarking'));
 foreach ($predefinedcomments as $predefinedcomment) {
     $deleteurlcomment = new moodle_url('', array('action' => 'delete', 'id' => $cm->id, 'commentid' => $predefinedcomment->id));
     $deleteiconcomment = new pix_icon('t/delete', get_string('delete'));
     $deleteactioncomment = $OUTPUT->action_icon($deleteurlcomment, $deleteiconcomment, new confirm_action(get_string('questiondeletecomment', 'mod_emarking')));
     $editurlcomment = new moodle_url('', array('action' => 'edit', 'id' => $cm->id, 'commentid' => $predefinedcomment->id));
     $editiconcomment = new pix_icon('i/edit', get_string('edit'));
     $editactioncomment = $OUTPUT->action_icon($editurlcomment, $editiconcomment);
     $creatorname = $DB->get_record('user', array('id' => $predefinedcomment->markerid));
     $table->data[] = array($predefinedcomment->text, $creatorname->username, $editactioncomment . $deleteactioncomment);
 }
 // Form display.
 $predefinedform = new emarking_import_excel_form(null, array('cmid' => $cm->id));
 if ($predefinedform->get_data()) {
     // Use csv importer from Moodle.
     $iid = csv_import_reader::get_new_iid('emarking-predefined-comments');
     $reader = new csv_import_reader($iid, 'emarking-predefined-comments');
     $content = $predefinedform->get_data()->comments;
     $reader->load_csv_content($content, 'utf8', "tab");
     $data = array();
     if (isset($predefinedform->get_data()->headers)) {
         $columns = $reader->get_columns()[0];
     } else {
         $columns = get_string("comment", "mod_emarking");
         $data[] = array($reader->get_columns()[0]);
     }
     $reader->init();
     $current = 0;
     while ($line = $reader->next()) {
         if (count($line) > 0) {
             $data[] = array($line[0]);
Esempio n. 8
0
 public function test_preview()
 {
     global $DB;
     $this->resetAfterTest(true);
     $content = array("shortname,fullname,summary", "c1,Course 1,Course 1 summary", "c2,Course 2,Course 2 summary");
     $content = implode("\n", $content);
     $iid = csv_import_reader::get_new_iid('uploadcourse');
     $cir = new csv_import_reader($iid, 'uploadcourse');
     $cir->load_csv_content($content, 'utf-8', 'comma');
     $cir->init();
     $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
     $defaults = array('category' => '1');
     $p = new tool_uploadcourse_processor($cir, $options, $defaults);
     // Nothing special to expect here, just make sure no exceptions are thrown.
     $p->preview();
 }
 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);
 }
 /**
  * return content for mass unenrolment page.
  */
 public function page_mass_unenrol()
 {
     global $CFG, $USER;
     require_once $CFG->libdir . '/csvlib.class.php';
     require_once $CFG->dirroot . '/local/mass_enroll/mass_unenroll_form.php';
     require_once $CFG->dirroot . '/local/mass_enroll/lib.php';
     $course = $this->page->course;
     $context = $this->page->context;
     $mform = new mass_unenroll_form(new moodle_url($CFG->wwwroot . '/local/mass_enroll/mass_unenroll.php'), array('course' => $course, 'context' => $context));
     $currenttab = 'mass_unenroll';
     $out = '';
     $strinscriptions = get_string('mass_unenroll', 'local_mass_enroll');
     if ($mform->is_cancelled()) {
         redirect(new moodle_url('/course/view.php', array('id' => $course->id)));
     } else {
         if ($data = $mform->get_data(false)) {
             $content = $mform->get_file_content('attachment');
             $iid = csv_import_reader::get_new_iid('uploaduser');
             $cir = new csv_import_reader($iid, 'uploaduser');
             $readcount = $cir->load_csv_content($content, $data->encoding, $data->delimiter_name);
             unset($content);
             $returnurl = $this->page->url;
             if ($readcount === false) {
                 print_error('csvloaderror', '', $returnurl);
             } else {
                 if ($readcount == 0) {
                     print_error('csvemptyfile', 'error', $returnurl);
                 }
             }
             $result = mass_unenroll($cir, $course, $context, $data);
             $cir->close();
             $cir->cleanup(false);
             // Only currently uploaded CSV file.
             if ($data->mailreport) {
                 $a = new stdClass();
                 $a->course = $course->fullname;
                 $a->report = $result;
                 email_to_user($USER, $USER, get_string('mail_unenrolment_subject', 'local_mass_enroll', $CFG->wwwroot), get_string('mail_unenrolment', 'local_mass_enroll', $a));
                 $result .= "\n" . get_string('email_sent', 'local_mass_enroll', $USER->email);
             }
             $out .= $this->header();
             $out .= $this->get_tabs($context, $currenttab, array('id' => $course->id));
             $out .= $this->heading($strinscriptions);
             $out .= $this->box(nl2br($result), 'center');
             $out .= $this->continue_button($this->page->url);
             // Back to this page.
             $out .= $this->footer($course);
             return $out;
         }
     }
     $out .= $this->header();
     $out .= $this->get_tabs($context, $currenttab, array('id' => $course->id));
     $out .= $this->heading_with_help($strinscriptions, 'mass_unenroll', 'local_mass_enroll', 'icon', get_string('mass_unenroll', 'local_mass_enroll'));
     $out .= $this->box(get_string('mass_unenroll_info', 'local_mass_enroll'), 'center');
     $out .= $mform->render();
     $out .= $this->footer($course);
     return $out;
 }
Esempio n. 11
0
 public function execute()
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . '/course/lib.php';
     require_once $CFG->libdir . '/gradelib.php';
     require_once $CFG->dirroot . '/grade/lib.php';
     require_once $CFG->dirroot . '/grade/import/lib.php';
     require_once $CFG->libdir . '/csvlib.class.php';
     $options = $this->expandedOptions;
     $USER = $this->user;
     $text = file_get_contents($this->arguments[0]);
     if (!$text) {
         cli_error("No data in file '{$this->arguments[0]}''");
     }
     if ($options['course-idnumber']) {
         $course = $DB->get_record('course', array('idnumber' => $this->arguments[1]), '*', MUST_EXIST);
     } else {
         $course = $DB->get_record('course', array('id' => $this->arguments[1]), '*', MUST_EXIST);
     }
     $iid = \csv_import_reader::get_new_iid('moosh-gradebook');
     $csvimport = new \csv_import_reader($iid, 'moosh-gradebook');
     $csvimport->load_csv_content($text, 'utf-8', 'comma');
     $header = $csvimport->get_columns();
     //use "Email address" or "ID number" for mapping users
     if ($options['map-users-by'] == 'idnumber') {
         $usermap = array_search('ID number', $header);
         if ($usermap === false) {
             cli_error("Didn't find column called 'ID number' for mapping users");
         }
     } elseif ($options['map-users-by'] == 'email') {
         $usermap = array_search('Email address', $header);
         if ($usermap === false) {
             cli_error("Didn't find column called 'Email address' for mapping users");
         }
     } else {
         cli_error(' Wrong map-users-by value');
     }
     $map = array();
     //Try to automatically map columns in CSV file onto activities with the same name
     $grade_items = \grade_item::fetch_all(array('courseid' => $course->id));
     foreach ($grade_items as $grade_item) {
         // Skip course type and category type.
         if ($grade_item->itemtype == 'course' || $grade_item->itemtype == 'category') {
             continue;
         }
         $displaystring = null;
         if (!empty($grade_item->itemmodule)) {
             $displaystring = get_string('modulename', $grade_item->itemmodule) . ': ' . $grade_item->get_name();
         } else {
             $displaystring = $grade_item->get_name();
         }
         //echo $displaystring . "\n";
         $pos = array_search($displaystring, $header);
         if ($pos !== false) {
             $map[$pos] = $grade_item->id;
             echo "CSV column '{$header[$pos]}' will be mapped to grade item '{$displaystring}'\n";
         } else {
             echo "No mapping for gradebook item '{$displaystring}'\n";
         }
     }
     //iterate over all CSV records
     $csvimport->init();
     $newgrades = array();
     while ($line = $csvimport->next()) {
         //first find user
         if ($options['map-users-by'] == 'idnumber') {
             if (!($user = $DB->get_record('user', array('idnumber' => $line[$usermap])))) {
                 cli_error("Couldn't find user with idnumber '{$line[$usermap]}'");
             }
         } elseif ($options['map-users-by'] == 'email') {
             if (!($user = $DB->get_record('user', array('email' => $line[$usermap])))) {
                 cli_error("Couldn't find user with email '{$line[$usermap]}'");
             }
         }
         echo "Processing user {$user->email} ({$user->id},{$user->idnumber})\n";
         foreach ($map as $k => $v) {
             $gradeitem = $grade_items[$v];
             $value = $line[$k];
             $newgrade = new \stdClass();
             $newgrade->itemid = $gradeitem->id;
             //handle scales
             if ($gradeitem->gradetype == GRADE_TYPE_SCALE) {
                 $scale = $gradeitem->load_scale();
                 $scales = explode(',', $scale->scale);
                 $scales = array_map('trim', $scales);
                 //hack - trim whitespace around scale options
                 array_unshift($scales, '-');
                 // scales start at key 1
                 $key = array_search($value, $scales);
                 if ($key === false) {
                     echo "\tThe correct scale value '{$value}' for item '{$gradeitem->get_name()}' could not be found.\n";
                 } else {
                     echo "\tMapped value '{$value}' to '{$key}' as scale is used for '{$gradeitem->get_name()}'\n";
                     $value = $key;
                 }
             } else {
                 if ($value === '' or $value == '-') {
                     $value = null;
                     // no grade
                 }
             }
             echo "\tGrade for '{$gradeitem->get_name()}', type {$gradeitem->gradetype} will be set to '{$value}'\n";
             $newgrade->finalgrade = $value;
             $newgrade->userid = $user->id;
             $newgrade->importer = $USER->id;
             $newgrades[] = $newgrade;
         }
     }
     if ($options['test']) {
         echo "Test mode - exiting without performing import.\n";
     }
     //we will use safer method of importing useing temporary table
     $importcode = get_new_importcode();
     foreach ($newgrades as $newgrade) {
         $newgrade->importcode = $importcode;
         $DB->insert_record('grade_import_values', $newgrade);
     }
     grade_import_commit($course->id, $importcode);
 }
Esempio n. 12
0
 /**
  * @param stored_file $file
  * @param string $encoding
  * @param string $delimiter
  * @param context $defaultcontext
  * @return array
  */
 protected function process_upload_file($file, $encoding, $delimiter, $defaultcontext)
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/csvlib.class.php';
     $cohorts = array(0 => array('errors' => array(), 'warnings' => array(), 'data' => array()));
     // Read and parse the CSV file using csv library.
     $content = $file->get_content();
     if (!$content) {
         $cohorts[0]['errors'][] = new lang_string('csvemptyfile', 'error');
         return $cohorts;
     }
     $uploadid = csv_import_reader::get_new_iid('uploadcohort');
     $cir = new csv_import_reader($uploadid, 'uploadcohort');
     $readcount = $cir->load_csv_content($content, $encoding, $delimiter);
     unset($content);
     if (!$readcount) {
         $cohorts[0]['errors'][] = get_string('csvloaderror', 'error', $cir->get_error());
         return $cohorts;
     }
     $columns = $cir->get_columns();
     // Check that columns include 'name' and warn about extra columns.
     $allowedcolumns = array('contextid', 'name', 'idnumber', 'description', 'descriptionformat', 'visible');
     $additionalcolumns = array('context', 'category', 'category_id', 'category_idnumber', 'category_path');
     $displaycolumns = array();
     $extracolumns = array();
     $columnsmapping = array();
     foreach ($columns as $i => $columnname) {
         $columnnamelower = preg_replace('/ /', '', core_text::strtolower($columnname));
         $columnsmapping[$i] = null;
         if (in_array($columnnamelower, $allowedcolumns)) {
             $displaycolumns[$columnnamelower] = $columnname;
             $columnsmapping[$i] = $columnnamelower;
         } else {
             if (in_array($columnnamelower, $additionalcolumns)) {
                 $columnsmapping[$i] = $columnnamelower;
             } else {
                 $extracolumns[] = $columnname;
             }
         }
     }
     if (!in_array('name', $columnsmapping)) {
         $cohorts[0]['errors'][] = new lang_string('namecolumnmissing', 'cohort');
         return $cohorts;
     }
     if ($extracolumns) {
         $cohorts[0]['warnings'][] = new lang_string('csvextracolumns', 'cohort', s(join(', ', $extracolumns)));
     }
     if (!isset($displaycolumns['contextid'])) {
         $displaycolumns['contextid'] = 'contextid';
     }
     $cohorts[0]['data'] = $displaycolumns;
     // Parse data rows.
     $cir->init();
     $rownum = 0;
     $idnumbers = array();
     $haserrors = false;
     $haswarnings = false;
     while ($row = $cir->next()) {
         $rownum++;
         $cohorts[$rownum] = array('errors' => array(), 'warnings' => array(), 'data' => array());
         $hash = array();
         foreach ($row as $i => $value) {
             if ($columnsmapping[$i]) {
                 $hash[$columnsmapping[$i]] = $value;
             }
         }
         $this->clean_cohort_data($hash);
         $warnings = $this->resolve_context($hash, $defaultcontext);
         $cohorts[$rownum]['warnings'] = array_merge($cohorts[$rownum]['warnings'], $warnings);
         if (!empty($hash['idnumber'])) {
             if (isset($idnumbers[$hash['idnumber']]) || $DB->record_exists('cohort', array('idnumber' => $hash['idnumber']))) {
                 $cohorts[$rownum]['errors'][] = new lang_string('duplicateidnumber', 'cohort');
             }
             $idnumbers[$hash['idnumber']] = true;
         }
         if (empty($hash['name'])) {
             $cohorts[$rownum]['errors'][] = new lang_string('namefieldempty', 'cohort');
         }
         $cohorts[$rownum]['data'] = array_intersect_key($hash, $cohorts[0]['data']);
         $haserrors = $haserrors || !empty($cohorts[$rownum]['errors']);
         $haswarnings = $haswarnings || !empty($cohorts[$rownum]['warnings']);
     }
     if ($haserrors) {
         $cohorts[0]['errors'][] = new lang_string('csvcontainserrors', 'cohort');
     }
     if ($haswarnings) {
         $cohorts[0]['warnings'][] = new lang_string('csvcontainswarnings', 'cohort');
     }
     return $cohorts;
 }
$strcoursecategorynotaddederror = get_string('coursecategorynotaddederror', 'tool_uploadcoursecategory');
$strcoursecategorydeleted = get_string('coursecategorydeleted', 'tool_uploadcoursecategory');
$strcoursecategorynotdeletederror = get_string('coursecategorynotdeletederror', 'tool_uploadcoursecategory');
$strcoursecategorynotdeletedmissing = get_string('coursecategorynotdeletedmissing', 'tool_uploadcoursecategory');
$strcoursecategorynotdeletedoff = get_string('coursecategorynotdeletedoff', 'tool_uploadcoursecategory');
$errorstr = get_string('error');
$returnurl = new moodle_url('/admin/tool/uploadcoursecategory/index.php');
$bulknurl = new moodle_url('/admin/tool/uploadcoursecategory/index.php');
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
// array of all valid fields for validation
$STD_FIELDS = array('name', 'description', 'idnumber', 'theme', 'visible', 'deleted', 'oldname');
if (empty($iid)) {
    $mform1 = new admin_uploadcoursecategory_form1();
    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploadcoursecategory');
        $cir = new csv_import_reader($iid, 'uploadcoursecategory');
        $content = $mform1->get_file_content('coursefile');
        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else {
            if ($readcount == 0) {
                print_error('csvemptyfile', 'error', $returnurl);
            }
        }
        // test if columns ok
        $filecolumns = cc_validate_coursecategory_upload_columns($cir, $STD_FIELDS, $returnurl);
        // continue to form2
    } else {
Esempio n. 14
0
$PAGE->set_url('/local/learningplan/upload.php');
$PAGE->set_pagelayout('admin');
$PAGE->set_heading($SITE->fullname);
$strheading = get_string('pluginname', 'local_learningplan');
$PAGE->set_title($strheading);
$PAGE->navbar->add(get_string('pluginname', 'local_learningplan'), new moodle_url('/local/learningplan/index.php'));
$PAGE->navbar->add(get_string('uploadlearningplans', 'local_learningplan'));
global $USER;
/* ---array of all valid fields for validation--- */
$STD_FIELDS = array('fullname', 'parentid', 'type', 'description');
$PRF_FIELDS = array();
/* ---if variable $iid equal to zero,it allows enter into the form--- */
if (empty($iid)) {
    $mform1 = new admin_learningplan_form1();
    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploadlearningplan');
        $cir = new csv_import_reader($iid, 'uploadlearningplan');
        /* ---this class fromcsvlib.php(includes csv methods and classes)--- */
        $content = $mform1->get_file_content('learningplanfile');
        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else if ($readcount == 0) {
            print_error('csvemptyfile', 'error', $returnurl);
        }
        /* ---test if columns ok(to validate the csv file content)--- */
        $filecolumns = uu_validate_learningplan_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        /* ---continue to form2--- */
    } else {
        echo $OUTPUT->header();
Esempio n. 15
0
require_once 'locallib.php';
require_login();
admin_externalpage_setup('keymanager_import');
require_capability('local/rcommon:importcredentials', context_system::instance());
@set_time_limit(3600);
// 1 hour should be enough
@raise_memory_limit('256M');
$iid = optional_param('iid', '', PARAM_INT);
$continue = optional_param('continue', false, PARAM_BOOL);
$fields = array('required' => array('isbn', 'credential'), 'optional' => array('username', 'userid'), 'ignored' => array('pack', 'packid'));
$errorstr = get_string('error');
$returnurl = new moodle_url('/local/rcommon/import.php');
if (empty($iid)) {
    $mform = new local_rcommon_import_credentials_form();
    if ($formdata = $mform->get_data()) {
        $iid = csv_import_reader::get_new_iid('import_marsupial');
        $cir = new csv_import_reader($iid, 'import_marsupial');
        $content = $mform->get_file_content('import_marsupial');
        $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);
        }
        $filecolumns = credentials::validate_columns($cir, $fields, $returnurl);
    } else {
        echo $OUTPUT->header();
        echo $OUTPUT->heading_with_help(get_string('keymanager_import_title', 'local_rcommon'), 'importcsv', 'local_rcommon');
        $mform->display();
        echo $OUTPUT->footer();
        die;
 /**
  * Inserts data from a csv file into the data module table specified.
  *
  * @param string $file comma seperated value file
  * @param string $tablename name of the table for the data to be inserted into.
  */
 function insert_data_from_csv($file, $tablename) {
     global $DB;
     $iid = csv_import_reader::get_new_iid('moddata');
     $csvdata = new csv_import_reader($iid, 'moddata');
     $fielddata = $csvdata->load_csv_content($file, 'utf8', 'comma');
     $columns = $csvdata->get_columns();
     $columncount = count($columns);
     $csvdata->init();
     $fieldinfo = array();
     for ($j = 0; $j < $fielddata; $j++) {
         $thing = $csvdata->next();
         $fieldinfo[$j] = new stdClass();
         for ($i = 0; $i < $columncount; $i++) {
             $fieldinfo[$j]->$columns[$i] = $thing[$i];
         }
         $DB->insert_record($tablename, $fieldinfo[$j], false);
     }
 }
Esempio n. 17
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;
 }
 /**
  * Imports the log views defined in the csv file, iterating each row. This is made under the transaction initiated in
  * import_data function.
  *
  * @param object $logsfile Course csv file.
  * @param object $formdata Submitted form data, needed to load the csv.
  * @param int $courseid Generated course id in this transaction.
  * @param \block_mycourse_recommendations\database_helper $db Database handler object, passed as argument to instance it
  * again.
  */
 public static function import_logs($logsfile, $formdata, $courseid, $db)
 {
     $iid = \csv_import_reader::get_new_iid('logsfile');
     $csvreader = new \csv_import_reader($iid, 'logsfile');
     $csvreader->load_csv_content($logsfile, $formdata->encoding, $formdata->delimiter_name);
     $csvreader->init();
     $fields = $csvreader->get_columns();
     while ($fields) {
         $userid = $fields[0];
         $resourcename = $fields[1];
         $resourcetype = $fields[2];
         $resourceid = $fields[3];
         $views = $fields[4];
         $timecreated = $fields[5];
         $db->insert_historic_logs($userid, $courseid, $resourcename, $resourcetype, $resourceid, $views, $timecreated);
         self::$lastinsertedlogs++;
         $fields = $csvreader->next();
     }
     $csvreader->close();
 }
Esempio n. 19
0
$strheading = get_string('pluginname', 'local_programs');
// $PAGE->set_title($strheading);
$PAGE->navbar->add(get_string('pluginname', 'local_programs'), new moodle_url('/local/programs/index.php'));
$PAGE->navbar->add(get_string('uploadprograms', 'local_programs'));
$myprogram = programs::getInstance();
global $USER;
// array of all valid fields for validation
$STD_FIELDS = array('fullname', 'shortname', 'schoolname', 'description', 'type', 'programlevel', 'duration');
$PRF_FIELDS = array();
//  if variable $iid equal to zero,it allows enter into the form
if (empty($iid)) {
    $mform1 = new admin_program_form1();
    if ($mform1->is_cancelled())
        redirect($returnurl);
    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploadprogram');
        $cir = new csv_import_reader($iid, 'uploadprogram'); //this class fromcsvlib.php(includes csv methods and clclasses)
        $content = $mform1->get_file_content('programfile');
        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else if ($readcount == 0) {
            print_error('csvemptyfile', 'error', $returnurl);
        }
        // test if columns ok(to validate the csv file content)
        $filecolumns = uu_validate_program_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        // continue to form2
    } else {
        echo $OUTPUT->header();
        echo $OUTPUT->heading(get_string('pluginname', 'local_programs'));  
Esempio n. 20
0
$PAGE->set_url('/local/collegestructure/upload.php');
$PAGE->set_pagelayout('admin');
$PAGE->set_heading($SITE->fullname);
$strheading = get_string('pluginname', 'local_collegestructure');
$PAGE->set_title($strheading);
$PAGE->navbar->add(get_string('pluginname', 'local_collegestructure'), new moodle_url('/local/collegestructure/index.php'));
$PAGE->navbar->add(get_string('uploadschools', 'local_collegestructure'));
global $USER;
/* ---array of all valid fields for validation--- */
$STD_FIELDS = array('fullname', 'parentid', 'type', 'description');
$PRF_FIELDS = array();
/* ---if variable $iid equal to zero,it allows enter into the form--- */
if (empty($iid)) {
    $mform1 = new admin_school_form1();
    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploadschool');
        $cir = new csv_import_reader($iid, 'uploadschool');
        /* ---this class fromcsvlib.php(includes csv methods and clclasses)--- */
        $content = $mform1->get_file_content('schoolfile');
        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else if ($readcount == 0) {
            print_error('csvemptyfile', 'error', $returnurl);
        }
        /* ---test if columns ok(to validate the csv file content)--- */
        $filecolumns = uu_validate_school_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        /* ---continue to form2--- */
    } else {
        echo $OUTPUT->header();
Esempio n. 21
0
 * @package    tool_uploadcourse
 * @copyright  2011 Piers Harding
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require __DIR__ . '/../../../config.php';
require_once $CFG->libdir . '/adminlib.php';
require_once $CFG->libdir . '/coursecatlib.php';
require_once $CFG->libdir . '/csvlib.class.php';
admin_externalpage_setup('tooluploadcourse');
$importid = optional_param('importid', '', PARAM_INT);
$previewrows = optional_param('previewrows', 10, PARAM_INT);
$returnurl = new moodle_url('/admin/tool/uploadcourse/index.php');
if (empty($importid)) {
    $mform1 = new tool_uploadcourse_step1_form();
    if ($form1data = $mform1->get_data()) {
        $importid = csv_import_reader::get_new_iid('uploadcourse');
        $cir = new csv_import_reader($importid, 'uploadcourse');
        $content = $mform1->get_file_content('coursefile');
        $readcount = $cir->load_csv_content($content, $form1data->encoding, $form1data->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvfileerror', 'tool_uploadcourse', $returnurl, $cir->get_error());
        } else {
            if ($readcount == 0) {
                print_error('csvemptyfile', 'error', $returnurl, $cir->get_error());
            }
        }
    } else {
        echo $OUTPUT->header();
        echo $OUTPUT->heading_with_help(get_string('uploadcourses', 'tool_uploadcourse'), 'uploadcourses', 'tool_uploadcourse');
        $mform1->display();
Esempio n. 22
0
 // Save uploaded file in Moodle filesystem and check
 $fs = get_file_storage();
 $fs->delete_area_files($context->id, 'mod_emarking', 'upload', $quiz->id);
 $file = $answersform->save_stored_file('answersfile', $context->id, 'mod_emarking', 'upload', $quiz->id, '/', emarking_clean_filename($answersform->get_new_filename('answersfile')));
 // Validate that file was correctly uploaded
 if (!$file) {
     print_error("Could not upload file");
 }
 // Check that the file is a zip
 if ($file->get_mimetype() !== 'text/csv') {
     echo $OUTPUT->error_text(get_string('fileisnotcsv', 'mod_emarking'));
     echo $OUTPUT->continue_button($urlquizzes);
     echo $OUTPUT->footer();
     die;
 }
 $iid = csv_import_reader::get_new_iid('emarking');
 $reader = new csv_import_reader($iid, 'emarking');
 $reader->load_csv_content($file->get_content(), 'utf8', $answersform->get_data()->delimiter_name);
 if (count($reader->get_columns()) < 3 || $reader->get_columns()[0] !== 'userid' || $reader->get_columns()[1] !== 'attemptid') {
     print_error('Invalid CSV file, it requires at least 3 columns. Starting with userid and attemptid.');
 }
 $validcolumns = 0;
 $columns = array();
 $columns[0] = 'userid';
 $columns[1] = 'attemptid';
 for ($i = 2; $i < count($reader->get_columns()); $i++) {
     if (preg_match('/^Question (\\d\\d\\d)$/', $reader->get_columns()[$i], $matches)) {
         $validcolumns++;
         $columns[$i] = $matches[1];
     } else {
         $columns[$i] = null;
Esempio n. 23
0
 /**
  * Display upload grades form
  *
  * @return string The response html
  */
 public function upload_grades()
 {
     global $CFG, $USER;
     require_capability('mod/assign:grade', $this->assignment->get_context());
     require_once $CFG->dirroot . '/mod/assign/feedback/offline/uploadgradesform.php';
     require_once $CFG->dirroot . '/mod/assign/feedback/offline/importgradesform.php';
     require_once $CFG->dirroot . '/mod/assign/feedback/offline/importgradeslib.php';
     require_once $CFG->libdir . '/csvlib.class.php';
     $mform = new assignfeedback_offline_upload_grades_form(null, array('context' => $this->assignment->get_context(), 'cm' => $this->assignment->get_course_module()->id));
     $o = '';
     $confirm = optional_param('confirm', 0, PARAM_BOOL);
     $renderer = $this->assignment->get_renderer();
     if ($mform->is_cancelled()) {
         redirect(new moodle_url('view.php', array('id' => $this->assignment->get_course_module()->id, 'action' => 'grading')));
         return;
     } else {
         if (($data = $mform->get_data()) && ($csvdata = $mform->get_file_content('gradesfile'))) {
             $importid = csv_import_reader::get_new_iid('assignfeedback_offline');
             $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
             // File exists and was valid.
             $ignoremodified = !empty($data->ignoremodified);
             $draftid = $data->gradesfile;
             // Preview import.
             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment' => $this->assignment, 'csvdata' => $csvdata, 'ignoremodified' => $ignoremodified, 'gradeimporter' => $gradeimporter, 'draftid' => $draftid));
             $o .= $renderer->render(new assign_header($this->assignment->get_instance(), $this->assignment->get_context(), false, $this->assignment->get_course_module()->id, get_string('confirmimport', 'assignfeedback_offline')));
             $o .= $renderer->render(new assign_form('confirmimport', $mform));
             $o .= $renderer->render_footer();
         } else {
             if ($confirm) {
                 $importid = optional_param('importid', 0, PARAM_INT);
                 $draftid = optional_param('draftid', 0, PARAM_INT);
                 $ignoremodified = optional_param('ignoremodified', 0, PARAM_BOOL);
                 $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
                 $mform = new assignfeedback_offline_import_grades_form(null, array('assignment' => $this->assignment, 'csvdata' => '', 'ignoremodified' => $ignoremodified, 'gradeimporter' => $gradeimporter, 'draftid' => $draftid));
                 if ($mform->is_cancelled()) {
                     redirect(new moodle_url('view.php', array('id' => $this->assignment->get_course_module()->id, 'action' => 'grading')));
                     return;
                 }
                 $o .= $this->process_import_grades($draftid, $importid, $ignoremodified);
             } else {
                 $o .= $renderer->render(new assign_header($this->assignment->get_instance(), $this->assignment->get_context(), false, $this->assignment->get_course_module()->id, get_string('uploadgrades', 'assignfeedback_offline')));
                 $o .= $renderer->render(new assign_form('batchuploadfiles', $mform));
                 $o .= $renderer->render_footer();
             }
         }
     }
     return $o;
 }
Esempio n. 24
0
$string = get_string('pluginname', 'local_admission') . ' : ' . get_string('uploadadmissions', 'local_admission');
$PAGE->set_title($string);
$PAGE->set_url('/local/admission/upload.php');
$PAGE->set_heading($SITE->fullname);
$strheading = get_string('pluginname', 'local_admission');
$PAGE->navbar->add(get_string('manage', 'local_admission'), new moodle_url('/local/admission/viewapplicant.php'));
$PAGE->navbar->add(get_string('uploadadmissions', 'local_admission'));
$returnurl = new moodle_url('/local/admission/upload.php');
$STD_FIELDS = array('firstname', 'middlename', 'lastname', 'gender', 'dob', 'birthcountry', 'birthplace', 'fathername', 'pob', 'region', 'town', 'current_home_no', 'current_country', 'phone', 'email', 'howlong', 'same', 'permenant_country', 'permenant_home_no', 'state', 'city', 'pincode', 'contactname', 'primary_school', 'primary_year', 'primary_score', 'primary_place', 'undergraduate_in', 'ugname', 'ug_year', 'ug_score', 'ug_place', 'graduate_in', 'graduate_name', 'graduate_year', 'graduate_score', 'graduate_place', 'examname', 'hallticketno', 'score', 'no_of_months', 'reason', 'description', 'schoolname', 'programname', 'typeofstudent', 'typeofapplication', 'previousstudent', 'serviceid', 'typeofprogram');

$PRF_FIELDS = array();
//-------- if variable $iid equal to zero,it allows enter into the form-----------------------------------
if (empty($iid)) {
    $mform1 = new admin_admission_form1();
    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploadadmission');
        $cir = new csv_import_reader($iid, 'uploadadmission'); //this class fromcsvlib.php(includes csv methods and clclasses)
        $content = $mform1->get_file_content('admissionfile');
        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else if ($readcount == 0) {
            print_error('csvemptyfile', 'error', $returnurl);
        }
        // test if columns ok(to validate the csv file content)
        $filecolumns = uu_validate_admission_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        // continue to form2
    } else {
        echo $OUTPUT->header();
        $instance = cobalt_admission::get_instance();
Esempio n. 25
0
    echo $OUTPUT->heading(get_string('uploadrecords', 'data'), 3);
    $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
    $formdata = new stdClass();
    $formdata->d = $data->id;
    $form->set_data($formdata);
    $form->display();
    echo $OUTPUT->box_end();
    echo $OUTPUT->footer();
    die;
} else {
    // Large files are likely to take their time and memory. Let PHP know
    // that we'll take longer, and that the process should be recycled soon
    // to free up memory.
    @set_time_limit(0);
    raise_memory_limit(MEMORY_EXTRA);
    $iid = csv_import_reader::get_new_iid('moddata');
    $cir = new csv_import_reader($iid, 'moddata');
    $readcount = $cir->load_csv_content($form->get_file_content('recordsfile'), $formdata->encoding, $formdata->fielddelimiter);
    if (empty($readcount)) {
        print_error('csvfailed', 'data', "{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
    } else {
        if (!($fieldnames = $cir->get_columns())) {
            print_error('cannotreadtmpfile', 'error');
        }
        // check the fieldnames are valid
        $fields = $DB->get_records('data_fields', array('dataid' => $data->id), '', 'name, id, type');
        $errorfield = '';
        foreach ($fieldnames as $name) {
            if (!isset($fields[$name])) {
                $errorfield .= "'{$name}' ";
            }
Esempio n. 26
0
$PAGE->set_url('/local/costcenter/upload.php');
$PAGE->set_pagelayout('admin');
$PAGE->set_heading($SITE->fullname);
$strheading = get_string('pluginname', 'local_costcenter');
$PAGE->set_title($strheading);
$PAGE->navbar->add(get_string('pluginname', 'local_costcenter'), new moodle_url('/local/costcenter/index.php'));
$PAGE->navbar->add(get_string('uploadcostcenters', 'local_costcenter'));
global $USER;
/* ---array of all valid fields for validation--- */
$STD_FIELDS = array('fullname', 'parentid', 'type', 'description');
$PRF_FIELDS = array();
/* ---if variable $iid equal to zero,it allows enter into the form--- */
if (empty($iid)) {
    $mform1 = new admin_costcenter_form1();
    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploadcostcenter');
        $cir = new csv_import_reader($iid, 'uploadcostcenter');
        /* ---this class fromcsvlib.php(includes csv methods and classes)--- */
        $content = $mform1->get_file_content('costcenterfile');
        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else if ($readcount == 0) {
            print_error('csvemptyfile', 'error', $returnurl);
        }
        /* ---test if columns ok(to validate the csv file content)--- */
        $filecolumns = uu_validate_costcenter_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        /* ---continue to form2--- */
    } else {
        echo $OUTPUT->header();
Esempio n. 27
0
// Set up the import form.
$mform = new grade_import_form(null, array('includeseparator' => true, 'verbosescales' => true));
// If the csv file hasn't been imported yet then look for a form submission or
// show the initial submission form.
if (!$iid) {
    // If the import form has been submitted.
    if ($formdata = $mform->get_data()) {
        // Large files are likely to take their time and memory. Let PHP know
        // that we'll take longer, and that the process should be recycled soon
        // to free up memory.
        core_php_time_limit::raise();
        raise_memory_limit(MEMORY_EXTRA);
        // Use current (non-conflicting) time stamp.
        $importcode = get_new_importcode();
        $text = $mform->get_file_content('userfile');
        $iid = csv_import_reader::get_new_iid('grade');
        $csvimport = new csv_import_reader($iid, 'grade');
        $csvimport->load_csv_content($text, $formdata->encoding, $separator);
        // --- get header (field names) ---
        $header = $csvimport->get_columns();
        // Print a preview of the data.
        $numlines = 0;
        // 0 lines previewed so far.
        echo $OUTPUT->heading(get_string('importpreview', 'grades'));
        foreach ($header as $i => $h) {
            $h = trim($h);
            // Remove whitespace.
            $h = clean_param($h, PARAM_RAW);
            // Clean the header.
            $header[$i] = $h;
        }
Esempio n. 28
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);
    }
Esempio n. 29
0
$bulknurl = new moodle_url('/admin/user/user_bulk.php');
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
// array of all valid fields for validation
$STD_FIELDS = array('id', 'firstname', 'lastname', 'username', 'email', 'city', 'country', 'lang', 'timezone', 'mailformat', 'maildisplay', 'maildigest', 'htmleditor', 'ajax', 'autosubscribe', 'institution', 'department', 'idnumber', 'skype', 'msn', 'aim', 'yahoo', 'icq', 'phone1', 'phone2', 'address', 'url', 'description', 'descriptionformat', 'password', 'auth', 'oldusername', 'deleted');
$PRF_FIELDS = array();
if ($prof_fields = $DB->get_records('user_info_field')) {
    foreach ($prof_fields as $prof_field) {
        $PRF_FIELDS[] = 'profile_field_' . $prof_field->shortname;
    }
}
unset($prof_fields);
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);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else {
            if ($readcount == 0) {
                print_error('csvemptyfile', 'error', $returnurl);
            }
        }
        // test if columns ok
        $filecolumns = uu_validate_user_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        // continue to form2
    } else {
Esempio n. 30
0
$strheading = get_string('manageclasses', 'local_clclasses') . ' : ' . get_string('uploadclasss', 'local_clclasses');
$PAGE->set_title($strheading);
$PAGE->navbar->add(get_string('pluginname', 'local_clclasses'), new moodle_url('/local/classs/index.php'));
$PAGE->navbar->add(get_string('uploadclasss', 'local_clclasses'));
global $USER, $DB;
/* ---array of all valid fields for validation--- */
$STD_FIELDS = array('schoolname', 'semestername', 'fullname', 'shortname', 'description', 'cobaltcourse', 'startdate', 'enddate', 'classlimit', 'lecturetype', 'department1', 'department2', 'instructor', 'starttime', 'endtime', 'classroom');

$PRF_FIELDS = array();
/* ---if variable $iid equal to zero,it allows enter into the form--- */
if (empty($iid)) {
    $mform1 = new admin_class_form1();
    if ($mform1->is_cancelled())
        redirect($returnurl);
    if ($formdata = $mform1->get_data()) {
        $iid = csv_import_reader::get_new_iid('uploadclass');
        $cir = new csv_import_reader($iid, 'uploadclass'); //this class fromcsvlib.php(includes csv methods and classes)
        $content = $mform1->get_file_content('classfile');
        $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name);
        unset($content);
        if ($readcount === false) {
            print_error('csvloaderror', '', $returnurl);
        } else if ($readcount == 0) {
            print_error('csvemptyfile', 'error', $returnurl);
        }
        /* ---test if columns ok(to validate the csv file content)--- */
        $filecolumns = uu_validate_class_upload_columns($cir, $STD_FIELDS, $PRF_FIELDS, $returnurl);
        /* ---continue to form2--- */
    } else {
        echo $OUTPUT->header();
        echo $OUTPUT->heading(get_string('manageclasses', 'local_clclasses'));