Skip to content

Instantly share code, notes, and snippets.

@Glowin
Last active April 1, 2021 15:14
Show Gist options
  • Save Glowin/1378794 to your computer and use it in GitHub Desktop.
Save Glowin/1378794 to your computer and use it in GitHub Desktop.
TCExam在线考试系统CSV转换成xml
echo '<div class="tceformbox">'.K_NEWLINE;
echo '<h1>csv转换为xml</h1>'.K_NEWLINE;
echo '<form action="transf.php" method="post" enctype="multipart/form-data" id="form_ratingeditor">'.K_NEWLINE;
echo '<div class="row">'.K_NEWLINE;
echo '<span class="label">'.K_NEWLINE;
echo '<label for="file">csv文件:</label>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '<span class="formw">'.K_NEWLINE;
echo '<input type="file" name="file" id="file" /><br />'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '</div>'.K_NEWLINE;
echo '<div class="row">'.K_NEWLINE;
echo '<span class="label">'.K_NEWLINE;
echo '<label for="module_name">试题名称:</label>'.K_NEWLINE;
echo '</span>'.K_NEWLINE;
echo '<span class="formw">'.K_NEWLINE;
echo '<input type="text" name="module_name" id="module_name" />'.K_NEWLINE;
F_submit_button("update", "上传", "上传");
echo '</span>'.K_NEWLINE;
echo '</div>'.K_NEWLINE;
echo '<br />'.K_NEWLINE;
echo '<br />'.K_NEWLINE;
echo '</form>'.K_NEWLINE;
echo '</div><!-- svc转换为xml结束 -->'.K_NEWLINE;
<?php
require_once('../config/tce_config.php');
$pagelevel = K_AUTH_ADMIN_RATING;
require_once('../../shared/code/tce_authorization.php');
require_once('../../shared/code/tce_functions_form.php');
require_once('../../shared/code/tce_functions_tcecode.php');
require_once('../code/tce_functions_auth_sql.php');
require_once('../code/tce_functions_statistics.php');
if (isset($_POST['content'])) {
$content = $_POST['content'];
}
if (isset($_POST['module_name'])) {
$module_name = $_POST['module_name'];
} else { $module_name = '未命名试题';}
if($_FILES['file']['name']) {
require_once('../code/tce_functions_upload.php');
$filename = preg_replace('/[\s]/', '_', $_FILES['file']['name']);
$filename = preg_replace('/[^a-zA-Z0-9_\.\-]/', '', $filename);
$filepath = K_PATH_CACHE.$filename;
if (F_is_allowed_upload($filename)) {
move_uploaded_file($_FILES['file']['tmp_name'], $filepath);
}
// upload file
}
// send XML headers
header('Content-Description: XML File Transfer');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
// force download dialog
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
header('Content-Type: application/xml', false);
// use the Content-Disposition header to supply a recommended filename
header('Content-Disposition: attachment; filename='.$module_name.'-'.date('YmdHis').'.xml;');
header('Content-Transfer-Encoding: binary');
//输出结果
switch($menu_mode) {
case 'update': {
$path = K_PATH_CACHE.$filename;
$fp = fopen($path, "r");
//转换部分
echo '<?xml version="1.0" encoding="UTF-8" ?>'.K_NEWLINE;
echo '<tcexamquestions version="11.2.004">'.K_NEWLINE;
echo '<header lang="cn" date="'.date("Y-m-d h:i:s").'">'.K_NEWLINE;
echo '</header>'.K_NEWLINE;
echo '<body>'.K_NEWLINE;
echo '<module>'.K_NEWLINE;
echo '<name>'.$module_name.'</name>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
//单项选择题部分
echo '<subject>'.K_NEWLINE;
echo '<name>单选题</name>'.K_NEWLINE;
echo '<description>单选题</description>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
while (!feof($fp)) {
$text = fgets($fp);
//转换编码GBK到UTF—8
$text = iconv('GBK','UTF-8',$text);
$test_array = explode(",",$text);
$test_account = count($test_array);
if ( $test_array[2] == 1) {
echo '<question>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
echo '<type>single</type>'.K_NEWLINE;
echo '<difficulty>1</difficulty>'.K_NEWLINE;
echo '<position></position>'.K_NEWLINE;
echo '<timer>0</timer>'.K_NEWLINE;
echo '<fullscreen>false</fullscreen>'.K_NEWLINE;
echo '<inline_answers>false</inline_answers>'.K_NEWLINE;
echo '<auto_next>false</auto_next>'.K_NEWLINE;
echo '<description>'.$test_array[1].'</description>'.K_NEWLINE;
echo '<explanation></explanation>'.K_NEWLINE;
for ($i = 11; $i < $test_account; $i++ ) {
if ($test_array[$i] != '' && $test_array[$i] != "\r" && $test_array[$i] != "\n" && $test_array[$i] != "\r\n") {
echo '<answer>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
//判断正确答案
if ( $i == $test_array[4]) { $right_id = 'true';} else { $right_id = 'false';}
echo '<isright>'.$right_id.'</isright>'.K_NEWLINE;
echo '<position></position>'.K_NEWLINE;
echo '<keyboard_key></keyboard_key>'.K_NEWLINE;
echo '<description>'.$test_array[$i].'</description>'.K_NEWLINE;
echo '<explanation></explanation>'.K_NEWLINE;
echo '</answer>'.K_NEWLINE;
}
}
echo '</question>'.K_NEWLINE;
}
}
echo '</subject>'.K_NEWLINE;
echo ''.K_NEWLINE;
//多选题部分
echo '<subject>'.K_NEWLINE;
echo '<name>多选题</name>'.K_NEWLINE;
echo '<description>多选题</description>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
//echo feof($fp).K_NEWLINE;
$fp = fopen($path, "r");
while (!feof($fp)) {
$text = fgets($fp);
//转换编码GBK到UTF—8
$text = iconv('GBK','UTF-8',$text);
$test_array = explode(",",$text);
$test_account = count($test_array);
if ( $test_array[2] == 2) {
echo '<question>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
echo '<type>multiple</type>'.K_NEWLINE;
echo '<difficulty>1</difficulty>'.K_NEWLINE;
echo '<position></position>'.K_NEWLINE;
echo '<timer>0</timer>'.K_NEWLINE;
echo '<fullscreen>false</fullscreen>'.K_NEWLINE;
echo '<inline_answers>false</inline_answers>'.K_NEWLINE;
echo '<auto_next>false</auto_next>'.K_NEWLINE;
echo '<description>'.$test_array[1].'</description>'.K_NEWLINE;
echo '<explanation></explanation>'.K_NEWLINE;
for ($i = 11; $i < $test_account; $i++ ) {
if ($test_array[$i] != '' && $test_array[$i] != "\r" && $test_array[$i] != "\n" && $test_array[$i] != "\r\n") {
echo '<answer>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
//判断正确答案
$right_id_array = explode(",",$test_array[4]);
$right_accout = count($right_id_array);
$break_switch = 0;
for ( $j = 0; $j < $right_accout; $j++) {
if ($i == $right_id_array[$j]) { $right_id = 'true'; break;} else { $right_id = 'false';}
}
echo '<isright>'.$right_id.'</isright>'.K_NEWLINE;
echo '<position></position>'.K_NEWLINE;
echo '<keyboard_key></keyboard_key>'.K_NEWLINE;
echo '<description>'.$test_array[$i].'</description>'.K_NEWLINE;
echo '<explanation></explanation>'.K_NEWLINE;
echo '</answer>'.K_NEWLINE;
}
}
echo '</question>'.K_NEWLINE;
}
}
echo '</subject>'.K_NEWLINE;
//判断题部分
echo '<subject>'.K_NEWLINE;
echo '<name>判断题</name>'.K_NEWLINE;
echo '<description>判断题</description>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
//echo feof($fp).K_NEWLINE;
$fp = fopen($path, "r");
while (!feof($fp)) {
$text = fgets($fp);
//转换编码GBK到UTF—8
$text = iconv('GBK','UTF-8',$text);
$test_array = explode(",",$text);
$test_account = count($test_array);
if ( $test_array[2] == 3) {
echo '<question>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
echo '<type>single</type>'.K_NEWLINE;
echo '<difficulty>1</difficulty>'.K_NEWLINE;
echo '<position></position>'.K_NEWLINE;
echo '<timer>0</timer>'.K_NEWLINE;
echo '<fullscreen>false</fullscreen>'.K_NEWLINE;
echo '<inline_answers>false</inline_answers>'.K_NEWLINE;
echo '<auto_next>false</auto_next>'.K_NEWLINE;
echo '<description>'.$test_array[1].'</description>'.K_NEWLINE;
echo '<explanation></explanation>'.K_NEWLINE;
for ($i = 11; $i < 13; $i++ ) {
echo '<answer>'.K_NEWLINE;
echo '<enabled>true</enabled>'.K_NEWLINE;
//判断正确答案
if ( $i == $test_array[4]) { $right_id = 'true';} else { $right_id = 'false';}
echo '<isright>'.$right_id.'</isright>'.K_NEWLINE;
echo '<position></position>'.K_NEWLINE;
echo '<keyboard_key></keyboard_key>'.K_NEWLINE;
if ($test_array[11] == '') { $test_array[11] ='是';}
if ($test_array[12] != '非') { $test_array[12] ='非';}
echo '<description>'.$test_array[$i].'</description>'.K_NEWLINE;
echo '<explanation></explanation>'.K_NEWLINE;
echo '</answer>'.K_NEWLINE;
}
echo '</question>'.K_NEWLINE;
}
}
echo '</subject>'.K_NEWLINE;
echo '</module>',K_NEWLINE;
echo '</body>',K_NEWLINE;
echo '</tcexamquestions>',K_NEWLINE;
break;
}
default: {
break;
}
} //end of switch
//============================================================+
// END OF FILE
//============================================================+
@updsky
Copy link

updsky commented Mar 13, 2021

上传那里记得加上单引号

@Glowin
Copy link
Author

Glowin commented Apr 1, 2021

@updsky 感谢反馈,已经修改好了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment