大家好,我有一个插件。它在一个表中显示数据,如下所示:
现在我需要在没有库的情况下将其导出到PHP中的excel文件中,因为这需要我将库添加到moodle中,我不太确定如何做到这一点。
这是在网站上用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();
有人能帮我吗,因为我真的被难住了。
注意:这是一个Moodle插件,如果这是有帮助的
提前谢谢。
对于Moodle,我建议先搜索代码。
已经有一个excel类可以使用。
有关示例,请参见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();