[英]How do i create a function to export data to a excel file PHP
Hi all i have a plugin that i built.大家好,我有一个我构建的插件。 that displays data in a table that looks like this:
在表格中显示数据,如下所示:
Now i need to export this into into a excel file in PHP without a library as this would require me to add the library to moodle and i'm not so sure how to do this.现在我需要在没有库的情况下将其导出到 PHP 中的 excel 文件中,因为这需要我将库添加到moodle,我不太确定如何执行此操作。
This is the code to create the table in html on the site.这是在站点上的 html 中创建表的代码。
<?php
require_once('../../config.php');
global $DB;
$courseid = required_param('courseid', PARAM_INT);
$syncid = optional_param('syncid', 0, PARAM_INT);
$context = context_system::instance();
$heading = get_string('pluginname', 'local_gradebook_sync');
$title = get_string('pluginname', 'local_gradebook_sync');
$url = new moodle_url('/local/gradebook_sync/sync.php', ['courseid' => $courseid]);
$PAGE->set_context($context);
$PAGE->set_heading($heading);
$PAGE->set_title($title);
$PAGE->set_url($url);
$gradebook_sync = $DB->get_record('local_gradebook_sync', ['id' => $syncid]);
$destination_course = get_course($gradebook_sync->destinationcourseid);
echo $OUTPUT->header();
echo $OUTPUT->heading($destination_course->fullname . ' (' . $destination_course->shortname . ')');
$table = new html_table();
$table->head = ['Section', 'Activity', 'Activity type', 'Status'];
$source_course_modules_sql = <<<SQL
SELECT
cm.id,
m.name AS 'module',
cm.instance AS 'instance',
cs.section,
cm.idnumber
FROM {course_modules} cm
JOIN {course_sections} cs ON cs.id = cm.section
JOIN {modules} m ON m.id = cm.module
WHERE
cm.course = :course
AND
m.name NOT IN ('book', 'resource', 'folder', 'imscp', 'label', 'page','url')
SQL;
$source_course_modules_variables = ['course' => $gradebook_sync->sourcecourseid];
$source_course_modules = $DB->get_records_sql($source_course_modules_sql, $source_course_modules_variables);
foreach($source_course_modules as $source_course_module) {
$source_course_module->activitytitle = $DB->get_record_sql('SELECT name FROM {' . $source_course_module->module . '} WHERE id = :id', ['id' => $source_course_module->instance])->name;
}
$destination_course_modules_sql = <<<SQL
SELECT
cm.id,
cs.section,
IFNULL(NULLIF(cs.name, ''), IF(cs.section = 0, 'General', CONCAT('Topic ', cs.section))) AS 'sectiontitle',
m.name AS 'module',
cm.instance AS 'instance',
cm.idnumber
FROM {course_modules} cm
JOIN {course_sections} cs ON cs.id = cm.section
JOIN {modules} m ON m.id = cm.module
WHERE
cm.course = :course
AND
m.name NOT IN ('book', 'resource', 'folder', 'imscp', 'label', 'page','url')
SQL;
$destination_course_modules_variables = ['course' => $gradebook_sync->destinationcourseid];
$destination_course_modules = $DB->get_records_sql($destination_course_modules_sql, $destination_course_modules_variables);
foreach($destination_course_modules as $destination_course_module) {
$destination_course_module->activitytitle = $DB->get_record_sql('SELECT name FROM {' . $destination_course_module->module . '} WHERE id = :id', ['id' => $destination_course_module->instance])->name;
switch($destination_course_module->module) {
case 'assign':
$destination_course_module->activitytype = 'Assignment';
break;
case 'assignment':
$destination_course_module->activitytype = 'Assignment 2.2';
break;
case 'book':
$destination_course_module->activitytype = 'Book';
break;
case 'chat':
$destination_course_module->activitytype = 'Chat';
break;
case 'checklist':
$destination_course_module->activitytype = 'Checklist';
break;
case 'choice':
$destination_course_module->activitytype = 'Choice';
break;
case 'data':
$destination_course_module->activitytype = 'Database';
break;
case 'feedback':
$destination_course_module->activitytype = 'Feedback';
break;
case 'folder':
$destination_course_module->activitytype = 'Folder';
break;
case 'forum':
$destination_course_module->activitytype = 'Forum';
break;
case 'glossary':
$destination_course_module->activitytype = 'Glossary';
break;
case 'h5pactivity':
$destination_course_module->activitytype = 'H5P';
break;
case 'hvp':
$destination_course_module->activitytype = 'H5P';
break;
case 'imscp':
$destination_course_module->activitytype = 'IMS content package';
break;
case 'label':
$destination_course_module->activitytype = 'Label';
break;
case 'lesson':
$destination_course_module->activitytype = 'Lesson';
break;
case 'lti':
$destination_course_module->activitytype = 'External tool';
break;
case 'page':
$destination_course_module->activitytype = 'Page';
break;
case 'questionnaire':
$destination_course_module->activitytype = 'Questionnaire';
break;
case 'quiz':
$destination_course_module->activitytype = 'Quiz';
break;
case 'resource':
$destination_course_module->activitytype = 'File';
break;
case 'scorm':
$destination_course_module->activitytype = 'SCORM package';
break;
case 'survey':
$destination_course_module->activitytype = 'Survey';
break;
case 'url':
$destination_course_module->activitytype = 'URL';
break;
case 'wiki':
$destination_course_module->activitytype = 'Wiki';
break;
case 'workshop':
$destination_course_module->activitytype = 'Workshop';
break;
default:
$destination_course_module->activitytype = 'Unknown';
}
$destination_course_module->status = 'INVALID';
foreach($source_course_modules AS $source_course_module) {
if((trim($source_course_module->activitytitle) == trim($destination_course_module->activitytitle)) && ($source_course_module->section == $destination_course_module->section) && ($source_course_module->module == $destination_course_module->module)) {
$destination_course_module->status = 'VALID';
}
}
$table_row = new html_table_row([$destination_course_module->sectiontitle, $destination_course_module->activitytitle, $destination_course_module->activitytype, $destination_course_module->status]);
$table->data[] = $table_row;
}
echo html_writer::table($table);
echo $OUTPUT->footer();
Can anyone help as i am really stumped.任何人都可以帮忙,因为我真的很难过。
Note: this is a Moodle plugin if that is helpful注意:如果有帮助,这是一个 Moodle 插件
Thanks in advance.提前致谢。
It should help you without installing any packages它应该可以帮助您而无需安装任何软件包
For Moodle, I'd recommend searching the code first.对于 Moodle,我建议先搜索代码。
There is already an excel class you can use.已经有一个 excel class 您可以使用。
For an example, see mod/survey/download.php
例如,请参阅
mod/survey/download.php
require_once("$CFG->libdir/excellib.class.php");
$workbook = new MoodleExcelWorkbook('-');
// Send HTTP headers
$downloadfilename = 'myfilename.xls';
$workbook->send($downloadfilename);
$myxls = $workbook->add_worksheet('myworksheet');
$headers = array("column1","column2","column3");
$row = 0;
$col = 0;
foreach ($headers as $header) {
$myxls->write_string($row, $col++, $header);
}
foreach ($grades as $grade) {
$row++;
$col = 0;
foreach ($grade as $name => $value) {
$myxls->write_string($row, $col++, $value);
}
}
$workbook->close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.