public function execute()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/csvlib.class.php';
     require_once $CFG->libdir . '/moodlelib.php';
     $filename = $this->expandedOptions['path'];
     if ($filename[0] != '/') {
         $filename = $this->cwd . DIRECTORY_SEPARATOR . $filename;
     }
     $categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
     $data = array();
     foreach ($categories as $category) {
         if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
             foreach ($fields as $field) {
                 $field->categoryname = $category->name;
                 $field->categorysortorder = $category->sortorder;
                 $data[] = $field;
             }
         }
     }
     // End of $categories foreach.
     $header = array('id', 'shortname', 'name', 'datatype', 'description', 'descriptionformat', 'categoryid', 'sortorder', 'required', 'locked', 'visible', 'forceunique', 'signup', 'defaultdata', 'defaultdataformat', 'param1', 'param2', 'param3', 'param4', 'param5', 'categoryname', 'categorysortorder');
     $csvexport = new \csv_export_writer();
     $csvexport->add_data($header);
     foreach ($data as $row) {
         $arrayrow = (array) $row;
         $csvexport->add_data($arrayrow);
     }
     try {
         file_put_contents($filename, $csvexport->print_csv_data(true));
         echo "Userfields exported to: " . $filename . "\n";
     } catch (Exception $e) {
         cli_error("Unable to save file. Check if file {$filename} is writable");
     }
 }
Example #2
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);
    }
Example #3
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 #4
0
 public function execute()
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/user/profile/lib.php';
     require_once $CFG->libdir . '/csvlib.class.php';
     $username = $this->arguments[0];
     $filename = $this->expandedOptions['name'];
     $user = get_user_by_name($username);
     if (!$user) {
         cli_error("User not found.");
     } else {
         $userid = $user->id;
     }
     $fields = array('id' => 'id', 'username' => 'username', 'email' => 'email', 'firstname' => 'firstname', 'lastname' => 'lastname', 'idnumber' => 'idnumber', 'institution' => 'institution', 'department' => 'department', 'phone1' => 'phone1', 'phone2' => 'phone2', 'city' => 'city', 'url' => 'url', 'icq' => 'icq', 'skype' => 'skype', 'aim' => 'aim', 'yahoo' => 'yahoo', 'msn' => 'msn', 'country' => 'country');
     if ($extrafields = $DB->get_records('user_info_field')) {
         foreach ($extrafields as $n => $v) {
             $fields['profile_field_' . $v->shortname] = 'profile_field_' . $v->shortname;
         }
     }
     $csvexport = new \csv_export_writer();
     $csvexport->set_filename($filename);
     $csvexport->add_data($fields);
     $row = array();
     profile_load_data($user);
     $userprofiledata = array();
     foreach ($fields as $field => $unused) {
         if (is_array($user->{$field})) {
             $userprofiledata[] = reset($user->{$field});
         } else {
             $userprofiledata[] = $user->{$field};
         }
     }
     $csvexport->add_data($userprofiledata);
     file_put_contents($filename, $csvexport->print_csv_data(true));
     echo "User " . $user->username . " successfully downloaded\n";
 }
Example #5
0
 /**
  * This will convert an array of values into a deliminated string.
  * Like the above function, this is for convenience.
  *
  * @param array $records     An array of information to be converted.
  * @param string $delimiter  The name of the delimiter. Supported types(comma, tab, semicolon, colon, cfg)
  * @param string $enclosure  How speical fields are enclosed.
  * @param bool $return       If true will return a string with the csv data.
  * @return string            csv data.
  */
 public static function print_array(array &$records, $delimiter = 'comma', $enclosure = '"', $return = false)
 {
     $csvdata = new csv_export_writer($delimiter, $enclosure);
     foreach ($records as $row) {
         $csvdata->add_data($row);
     }
     $data = $csvdata->print_csv_data($return);
     if ($return) {
         return $data;
     }
 }
 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 #7
0
function evapares_get_all_data($cmid, $evaparesname)
{
    global $DB;
    $csv = new csv_export_writer("semicolon", '"');
    $filename = "evapares_alldata_" . $evaparesname . "_" . date("d-m-Y", time());
    $header = array("N" => "N", "Grupo" => "Grupo", "Alumno evaluado" => "Alumno evaluado", "Email" => "Email", "Alumno evaluador" => "Alumno evaluador", "Evaluación" => "Evaluación", "Tiempo de respuesta" => "Tiempo de respuesta", "Nota" => "Nota", "Stop" => "Stop", "Start" => "Start", "Continue" => "Continue", "P1" => "P1", "P2" => "P2", "P3" => "P3", "P4" => "P4", "P5" => "P5", "P6" => "P6", "P7" => "P7", "P8" => "P8", "P9" => "P9", "P10" => "P10", "P11" => "P11", "P12" => "P12");
    $csv->add_data($header);
    $sqlfulldata = "SELECT ee.id, \r\n\t\t\tg.name AS gname, \r\n\t\t\tee.alu_evaluado_id AS evaluado,\r\n\t\t\tCONCAT(u.firstname, ' ', u.lastname) AS username,\r\n\t\t\tu.email,\r\n\t\t\tee.alu_evalua_id AS evaluator,\r\n\t\t\tee.iterations_id AS itid,\r\n\t\t\tei.evaluation_name as evaluacion,\r\n\t\t\tei.n_iteration AS inum,\r\n\t\t\tee.enddate,\r\n\t\t\tee.nota,\r\n\t\t\tee.ssc_stop AS stop, \r\n\t\t\tee.ssc_start AS start,\r\n\t\t\tee.ssc_continue AS cont\r\n\t\t\tFROM {user} AS u\r\n\t\t\tINNER JOIN {groups_members} AS gm ON (u.id = gm.userid)\r\n\t\t\tINNER JOIN {groups} AS g ON (gm.groupid = g.id)\r\n\t\t\tINNER JOIN {course} AS c ON (g.courseid = c.id)\r\n\t\t\tINNER JOIN {course_modules} AS cm ON (c.id = cm.course AND cm.id = ?)\r\n\t\t\tINNER JOIN {evapares_iterations} AS ei ON (cm.id = ei.evapares_id)\r\n\t\t\tINNER JOIN {evapares_evaluations} AS ee ON (ei.id = ee.iterations_id AND ee.alu_evaluado_id = u.id)\r\n\t\t\tORDER BY g.name, ei.id, ee.alu_evaluado_id";
    $evaluations = $DB->get_recordset_sql($sqlfulldata, array($cmid));
    $count = 1;
    foreach ($evaluations as $evaluation) {
        $row = array();
        $row[] = $count;
        $row[] = $evaluation->gname;
        $row[] = $evaluation->username;
        $row[] = $evaluation->email;
        $row[] = "Anónimo";
        $row[] = $evaluation->evaluacion;
        if ($evaluation->enddate != NULL) {
            $row[] = date("H:i - d/m/Y", $evaluation->enddate);
        } else {
            $row[] = "No realizado";
        }
        $row[] = $evaluation->nota;
        $row[] = (string) $evaluation->stop;
        $row[] = (string) $evaluation->start;
        $row[] = (string) $evaluation->cont;
        $sqlanswers = "SELECT eha.id, \r\n\t\t\t\teq.n_of_question AS nquestion, \r\n\t\t\t\tea.number, \r\n\t\t\t\tea.text\r\n\t\t\t\tFROM {evapares_eval_has_answ} AS eha\r\n\t\t\t\tINNER JOIN {evapares_answers} AS ea ON (ea.id = eha.answers_id)\r\n\t\t\t\tINNER JOIN {evapares_questions} AS eq ON (ea.question_id AND eq.id)\r\n\t\t\t\tWHERE eha.evaluations_id = ?\r\n\t\t\t\tGROUP BY eha.id \r\n\t\t\t\tORDER BY eq.n_of_question";
        // Have answers
        if ($answers = $DB->get_records_sql($sqlanswers, array($evaluation->id))) {
            foreach ($answers as $answer) {
                $row[] = $answer->number;
            }
        } else {
            for ($answer = 1; $answer <= 12; $answer++) {
                $row[] = "0";
            }
        }
        $csv->add_data($row);
        $count++;
    }
    $evaluations->close();
    $csv->set_filename($filename);
    $csv->download_file();
    echo $csv->print_csv_data();
}
Example #8
0
    function export_csv($report, $filename = '') {
        global $DB, $CFG;
        require_once($CFG->libdir . '/csvlib.class.php');

        $table = $report->table;
        $matrix = array();
        $filename = '' ? $filename = 'report.csv' : $filename . '.csv';

        if (!empty($table->head)) {
            $countcols = count($table->head);
            $keys = array_keys($table->head);
            $lastkey = end($keys);
            foreach ($table->head as $key => $heading) {
                $matrix[0][$key] = str_replace("\n", ' ', htmlspecialchars_decode(strip_tags(nl2br($heading))));
            }
        }

        if (!empty($table->data)) {
            foreach ($table->data as $rkey => $row) {
                foreach ($row as $key => $item) {
                    $matrix[$rkey + 1][$key] = str_replace("\n", ' ', htmlspecialchars_decode(strip_tags(nl2br($item))));
                }
            }
        }

        $csvexport = new csv_export_writer();
        $csvexport->set_filename($filename);

        foreach ($matrix as $ri => $col) {
            $csvexport->add_data($col);
        }
        if ($filename) {
            $fp = fopen($filename, "w");
            fwrite($fp, $csvexport->print_csv_data(true));
            fclose($fp);
        } else {
            $csvexport->download_file();
            exit;
        }
    }