本文整理汇总了PHP中csv_export_writer::add_data方法的典型用法代码示例。如果您正苦于以下问题:PHP csv_export_writer::add_data方法的具体用法?PHP csv_export_writer::add_data怎么用?PHP csv_export_writer::add_data使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类csv_export_writer
的用法示例。
在下文中一共展示了csv_export_writer::add_data方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
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");
}
}
示例2: foreach
function report_download_csv($fields, $data, $reportname = 'reportincsv')
{
$filename = clean_filename($reportname);
$csvexport = new csv_export_writer();
$csvexport->set_filename($filename);
$csvexport->add_data($fields);
foreach ($data as $eachrow) {
$csvexport->add_data($eachrow);
}
$csvexport->download_file();
}
示例3: user_download_csv
function user_download_csv($fields) {
global $CFG;
require_once($CFG->libdir . '/csvlib.class.php');
$filename = clean_filename('Users sample');
$csvexport = new csv_export_writer();
$csvexport->set_filename($filename);
$csvexport->add_data($fields);
$userprofiledata = array('lp_shortname','testuser');
$csvexport->add_data($userprofiledata);
$csvexport->download_file();
die;
}
示例4: user_download_csv
function user_download_csv($fields) {
global $CFG;
require_once($CFG->libdir . '/csvlib.class.php');
$filename = clean_filename(get_string('course', 'local_cobaltcourses'));
$csvexport = new csv_export_writer();
$csvexport->set_filename($filename);
$csvexport->add_data($fields);
$userprofiledata = array();
$csvexport->add_data($userprofiledata);
$csvexport->download_file();
die;
}
示例5: user_download_csv
function user_download_csv($fields) {
global $CFG;
require_once($CFG->libdir . '/csvlib.class.php');
$filename = clean_filename('Departments');
$csvexport = new csv_export_writer();
$csvexport->set_filename($filename);
$csvexport->add_data($fields);
$userprofiledata = array();
$csvexport->add_data($userprofiledata);
$csvexport->download_file();
die;
}
示例6: export_report
/** Configurable Reports
* A Moodle block for creating customizable reports
* @package blocks
* @author: Juan leyva <http://www.twitter.com/jleyvadelgado>
* @date: 2009
*/
function export_report($report)
{
global $DB, $CFG;
require_once $CFG->libdir . '/csvlib.class.php';
$table = $report->table;
$matrix = array();
$filename = 'report';
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);
}
$csvexport->download_file();
exit;
}
示例7: print_grades
public function print_grades()
{
global $CFG;
$export_tracking = $this->track_exports();
$strgrades = get_string('grades');
$profilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields);
$shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id)));
$downloadfilename = clean_filename("{$shortname} {$strgrades}");
$csvexport = new csv_export_writer($this->separator);
$csvexport->set_filename($downloadfilename);
// Print names of all the fields
$exporttitle = array();
foreach ($profilefields as $field) {
$exporttitle[] = $field->fullname;
}
if (!$this->onlyactive) {
$exporttitle[] = get_string("suspended");
}
// Add a feedback column.
foreach ($this->columns as $grade_item) {
$exporttitle[] = $this->format_column_name($grade_item);
if ($this->export_feedback) {
$exporttitle[] = $this->format_column_name($grade_item, true);
}
}
$csvexport->add_data($exporttitle);
// Print all the lines of data.
$geub = new grade_export_update_buffer();
$gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
$gui->require_active_enrolment($this->onlyactive);
$gui->allow_user_custom_fields($this->usercustomfields);
$gui->init();
while ($userdata = $gui->next_user()) {
$exportdata = array();
$user = $userdata->user;
foreach ($profilefields as $field) {
$fieldvalue = grade_helper::get_user_field_value($user, $field);
$exportdata[] = $fieldvalue;
}
if (!$this->onlyactive) {
$issuspended = $user->suspendedenrolment ? get_string('yes') : '';
$exportdata[] = $issuspended;
}
foreach ($userdata->grades as $itemid => $grade) {
if ($export_tracking) {
$status = $geub->track($grade);
}
$exportdata[] = $this->format_grade($grade);
if ($this->export_feedback) {
$exportdata[] = $this->format_feedback($userdata->feedbacks[$itemid]);
}
}
$csvexport->add_data($exportdata);
}
$gui->close();
$geub->close();
$csvexport->download_file();
exit;
}
示例8: test_csv_functions
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);
}
示例9: execute
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";
}
示例10: test_csv_functions
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);
}
示例11: foreach
}
// Add activity headers
foreach ($criteria as $criterion) {
// Handle activity completion differently
if ($criterion->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
// Load activity
$mod = $criterion->get_mod_instance();
$row[] = $mod->name;
$row[] = $mod->name . ' - ' . get_string('completiondate', 'report_completion');
} else {
// Handle all other criteria
$row[] = strip_tags($criterion->get_title_detailed());
}
}
$row[] = get_string('coursecomplete', 'completion');
$export->add_data($row);
}
///
/// Display a row for each user
///
foreach ($progress as $user) {
// User name
if ($csv) {
$row = array();
$row[] = $user->id;
$row[] = fullname($user);
foreach ($extrafields as $field) {
$row[] = $user->{$field};
}
} else {
print PHP_EOL . '<tr id="user-' . $user->id . '">';
示例12: ratings_csv_for_ratingallocate
/**
* Shows table containing information about the users' ratings
* and their distribution over the choices (allocations).
*
* @return HTML code
*/
public function ratings_csv_for_ratingallocate(ratingallocate $ratingallocate, csv_export_writer $csvexport)
{
$exporttitle[0] = 'userid';
$exporttitle[1] = 'username';
$exporttitle[2] = 'firstname';
$exporttitle[3] = 'lastname';
$offsetchoices = count($exporttitle);
$columnids = array();
$rateable_choices = $ratingallocate->get_rateable_choices();
foreach ($rateable_choices as $choice) {
$columnids[$choice->id] = $choice->id + $offsetchoices;
$exporttitle[$choice->id + $offsetchoices] = $choice->id . '|' . $choice->title;
}
// Sort headings by (choice-)id to align them with exported data (created below).
ksort($exporttitle);
$maxcolumnid = key(array_slice($exporttitle, -1, 1, true));
$exporttitle[$maxcolumnid + 1] = "allocation";
$columnids["allocation"] = $maxcolumnid + 1;
// add the header to the data
$csvexport->add_data($exporttitle);
$ratingscells = array();
foreach ($ratingallocate->get_raters_in_course() as $user) {
if (!array_key_exists($user->id, $ratingscells)) {
$ratingscells[$user->id] = array();
}
$ratingscells[$user->id][0] = $user->id;
$ratingscells[$user->id][1] = $user->username;
$ratingscells[$user->id][2] = fullname($user);
$ratingscells[$user->id][3] = $user->lastname;
foreach ($columnids as $choice => $choicecolumn) {
$ratingscells[$user->id][$choicecolumn] = '';
}
// Sort ratings by choiceid to align them with the group names in the table
ksort($ratingscells[$user->id]);
}
$ratings = $ratingallocate->get_ratings_for_rateable_choices();
// get rating titles
$titles = $this->get_options_titles(array_map(function ($rating) {
return $rating->rating;
}, $ratings), $ratingallocate);
foreach ($ratings as $rating) {
$choicecolumnindex = $columnids[$rating->choiceid];
$ratingscells[$rating->userid][$choicecolumnindex] = $titles[$rating->rating];
}
$memberships = $ratingallocate->get_allocations();
$allocationcolumnindex = $columnids["allocation"];
foreach ($memberships as $membership) {
$choice = $rateable_choices[$membership->choiceid];
$ratingscells[$membership->userid][$allocationcolumnindex] = $choice->id . '|' . $choice->title;
}
foreach ($ratingscells as $userline) {
$csvexport->add_data($userline);
}
}
示例13: print_array
/**
* 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;
}
}
示例14: print_grades
public function print_grades()
{
global $CFG;
$exporttracking = $this->track_exports();
$course = $this->course;
$strgrades = get_string('grades');
$profilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields);
$courseid = $course->id;
$shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id)));
$downloadfilename = clean_filename("{$shortname} {$strgrades}");
$csvexport = new csv_export_writer($this->separator);
$csvexport->filename = clean_filename("{$downloadfilename}.csv");
// Print names of all the fields.
$exporttitle = array();
$shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id)));
$exporttitle[] = $shortname . "";
$csvexport->add_data($exporttitle);
$exporttitle = array();
$exporttitle[] = "TERM";
$exporttitle[] = "Class Number";
$exporttitle[] = "ID";
$exporttitle[] = "Final Grade";
$exporttitle[] = "" . get_string("firstname");
$exporttitle[] = "" . get_string("lastname");
$exporttitle[] = "" . get_string("idnumber");
$exporttitle[] = "" . get_string("institution");
$exporttitle[] = "" . get_string("department");
$exporttitle[] = "" . get_string("email");
foreach ($this->columns as $gradeitem) {
$exporttitle[] = trim($gradeitem->get_name());
}
$csvexport->add_data($exporttitle);
$sseat = $this->primcomp($courseid);
// Print all the lines of data.
$geub = new grade_export_update_buffer();
$gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
$gui->require_active_enrolment($this->onlyactive);
$gui->allow_user_custom_fields($this->usercustomfields);
$gui->init();
while ($userdata = $gui->next_user()) {
$exportdata = array();
$user = $userdata->user;
if (!isset($sseat[$user->username])) {
continue;
}
foreach ($userdata->grades as $itemid => $grade) {
if ($this->finalitemid == $itemid) {
$finalletter = $this->format_grade($userdata->grades[$itemid]);
if ($this->displaytype == 1) {
$userdata->grades[$itemid]->finalgrade = $finalletter;
}
}
}
$coarr = explode('.', $sseat[$user->username]);
$exportdata[] = $coarr[0];
$exportdata[] = $coarr[1];
$exportdata[] = $user->username;
$exportdata[] = $finalletter;
$exportdata[] = $user->firstname;
$exportdata[] = $user->lastname;
$exportdata[] = $user->idnumber;
$exportdata[] = $user->institution;
$exportdata[] = $user->department;
$exportdata[] = $user->email;
foreach ($userdata->grades as $itemid => $grade) {
$gradestr = $userdata->grades[$itemid]->finalgrade;
$exportdata[] = $gradestr;
}
$csvexport->add_data($exportdata);
}
$gui->close();
$geub->close();
$csvexport->download_file();
exit;
}
示例15: save_users_to_csv
/**
* Save requested users to a comma separated values (CSV) file.
*
* @return csv_export_writer The csv file object.
*/
protected function save_users_to_csv()
{
global $DB;
$csv = new csv_export_writer($this->delimiter);
$csv->set_filename('users');
$maxrolesnumber = 0;
// Getting the maximum number of roles a user can have.
foreach ($this->contents as $key => $user) {
$rolesnumber = count($user->roles);
if ($rolesnumber > $maxrolesnumber) {
$maxrolesnumber = $rolesnumber;
}
}
// Saving field names.
$userfields = $this->fields;
if ($this->useoverrides) {
foreach ($this->overrides as $field => $value) {
if (!array_search($field, $userfields)) {
$userfields[] = $field;
}
}
}
$row = $userfields;
if ($maxrolesnumber > 0) {
for ($i = 1; $i <= $maxrolesnumber; $i++) {
$coursename = 'course' . $i;
$rolename = 'role' . $i;
$row[] = $coursename;
$row[] = $rolename;
}
}
$csv->add_data($row);
$columnsnumber = count($row);
foreach ($this->contents as $key => $user) {
if (!empty($user->roles)) {
$row = array();
foreach ($userfields as $key => $field) {
$row[] = $user->{$field};
}
foreach ($user->roles as $key => $rolesarray) {
foreach ($rolesarray as $role => $course) {
$row[] = $course;
$row[] = $role;
}
}
// Adding blank columns until we have the same number of columns.
$no = count($row);
while ($no < $columnsnumber) {
$row[] = '';
$no++;
}
$csv->add_data($row);
}
}
return $csv;
}