UNPKG

webclass

Version:

A package for web class checking

1,126 lines (987 loc) 42.5 kB
'use strict'; const fs = require('fs'); const path = require('path'); const LineByLine = require('readlinesyn'); module.exports = class { constructor(){ this._myFileParser = new LineByLine(); this._datalist = []; this._allStudListFilePath = "./studall.txt"; this._jsonFileSource = "./stud.json"; this._jsondata = {}; this._htmlFilePath = "./score.html"; this._classID = 1; this._editable = false; // Class Plan this._jiaoanTexFilePath = "./jiaoan.tex", // Exam this._linespace = 1.2; this._testFilePath; this._hasAnswer = false; this._hasComment = false; this._examhtmlheadlist = [ "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">", "<html> ", "<head> ", " <meta charset=\"utf-8\"> <style> span { color: black; } </style> ", " <title> 课堂训练</title> ", " <meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\"> ", " <meta http-equiv=\"description\" content=\"this is my page\"> ", " <meta http-equiv=\"content-type\" content=\"text/html; \"> ", " <meta charset=\"UTF-8\"> ", " <meta charset=\"GB2312\"> ", " <script type=\"text/javascript\" src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script> ", " <script type=\"text/x-mathjax-config\">MathJax.Hub.Config({ tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']]}});</script><script type=\"text/javascript\" src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script> ", "</head> ", "<body> " ]; this._examhtmlendlist = [ " <script type=\"text/javascript\"> ", " function getUserAnswer(radioGroupName){ ", " var rg = document.getElementsByName(radioGroupName); ", " var result = -1; ", " for(var i=0;i<rg.length;i++){ ", " if(rg[i].checked ==true) ", " result = i; ", " } ", " userAnswer.push(result); ", " } ", " </script> ", " <input name=\"ok\" type=\"submit\" value=\"提交\" onclick='showResult()'/> ", "</body> ", "</html> " ]; this._latexcnarticlehead = [ "\\documentclass[UTF8]{article} ", "\\RequirePackage{fix-cm}", "\\usepackage{tikz} ", "\\usetikzlibrary{arrows,", " patterns,", " shapes.geometric,", " decorations.pathmorphing,", " backgrounds,", " positioning,", " fit,", " petri,", " matrix,", " automata,chains}", "\\setcounter{tocdepth}{7}", "\\graphicspath{{./figures/}} ", ]; this._latexPackageList = [ "{comment}", "{ulem}", "[colorlinks,linkcolor=red,anchorcolor=blue,citecolor=black]{hyperref}", "{graphicx}", "{comment}", "{subfigure}", "{amsmath}", "{multirow}", "{pgfplots}", "{setspace}", "{enumitem}", "{lastpage}", "{fancyhdr}", "{geometry}", "{amsthm}", "{CJKutf8}" ]; this._latexNewcommandList = [ "{\\myTitleA}[1]{ {\\noindent \\Large {\\CJKfamily{li} #1} } \\vspace{0.5cm}}", "{\\normalCentralTitle}[1]{ \\begin{center} {\\CJKfamily{hei} #1} \\end{center}\\vspace{0.5cm}}", "{\\myTitleB}[1] {\\noindent \\hspace{0.5cm} {\\CJKfamily{hei} #1} \\\\ \\hfill}", "{\\content}[1]{ \\begin{tabular}{c p{10.5cm}} \\hspace{0.5cm} & #1 \\\\ \\end{tabular}}", "{\\spacingContent}[1]{\\begin{spacing}{1.5} #1 \\end{spacing} \\vspace{0.5cm}}" ]; } get editable(){ return this._editable; } seteditable(value){ var self = this; self._editable = value; } get classid(){ return this._classID; } set classid(value){ this._classID = value; } // Checking whether an js object is valid checkingObject( targetObj ) { var result = true; if( typeof targetObj == 'undefined' ) { console.log("checkingObject(): undefined Object"); result = false; } if( targetObj === "" ) { console.log("checkingObject(): Object is empty"); result = false; } return result; } // Deep copy // https://www.cnblogs.com/lzpong/p/6973003.html clone(obj) { var self = this; // Handle the 3 simple types, and null or undefined or function if (null == obj || "object" != typeof obj) return obj; // Handle Date if (obj instanceof Date) { var copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array or Object if (obj instanceof Array | obj instanceof Object) { var copy = (obj instanceof Array)?[]:{}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = self.clone(obj[attr]); } return copy; } throw new Error("Unable to clone obj! Its type isn't supported."); } // writing data list to a file writedatafile( filepath ) { var self = this; var fd; try { fd = fs.openSync( filepath, 'w'); fs.appendFileSync( fd, self._datalist.join("\n"), {"encoding":'utf8'}); } catch (err) { /* 处理错误 */ } //fs.fsyncSync(fd) if (fd !== undefined) fs.closeSync(fd); } updating(){ // Updating source file fs.writeFileSync( this._jsonFileSource, JSON.stringify( this._jsondata, null, " "), {"encoding":'utf8'}); } init( filePath ) { var self = this; if( filePath ) { // do nothing } else{ console.log("init(): Invalid file path"); //return; } if( typeof filePath == 'undefined' ) { console.log("init(): undefined file path"); } if( typeof filePath === "" ) { console.log("init(): File path is empty"); } self._allStudListFilePath = filePath; try{ var tmp = fs.statSync( self._allStudListFilePath ).isFile(); if( !Boolean(tmp) ){ console.error("init(): Invalid data file path."); return; } }catch( exception ) { console.log( exception ); } self._myFileParser.open( self._allStudListFilePath ); var theline = self._myFileParser.next(); while( !self._myFileParser.isEOF() ) { theline = self._myFileParser.next(); var l = theline.split("\t"); //console.log( 'READ LINE: ' + l[2] ); var id = l[0]; var classes = {}; self._jsondata[id] = {}; self._jsondata[id]["name"] = l[1]; self._jsondata[id]["pinyin"] = l[2]; self._jsondata[id]["major"] = l[4]; self._jsondata[id]["year"] = l[3]; self._jsondata[id]["class"] = l[6]; self._jsondata[id]["check"] = classes; self._jsondata[id]["homework"] = classes; } self._myFileParser.close(); self.updating(); } openjsonfile( jsonFile ) { var self = this; try{ if( !(fs.existsSync(jsonFile)) ){ console.error("openjsonfile(): Invalid json file path."); // If not exist, create the file. fs.writeFileSync( jsonFile, "{}" , {"encoding":'utf8', "flag":"w+"}); } }catch( exception ) { console.log( exception ); } self._jsonFileSource = jsonFile; var jsonfiledata = fs.readFileSync(self._jsonFileSource, "utf8"); self._jsondata = JSON.parse(jsonfiledata); return; } // // Checking Component // checking( dataFilePath ) { var self = this; if( dataFilePath ) { // do nothing } else{ console.log("checking(): Invalid file path"); return; } if( typeof dataFilePath == 'undefined' ) { console.log("checking(): undefined file path"); return; } if( dataFilePath === "" ) { console.log("checking(): file path is empty"); return; } var fileData = fs.readFileSync(self._jsonFileSource, "utf8"); self._jsondata = JSON.parse(fileData); for(var stud in self._jsondata ){ self._jsondata[stud]["check"][String(self._classID)] = { "score": 0, "comments":"" }; //= 0; } self._myFileParser.open( dataFilePath ); var theline; while( !self._myFileParser.isEOF() ) { theline = self._myFileParser.next(); //console.log( 'READ LINE: ' + theline ); for(var stud in self._jsondata ){ var result = theline.indexOf( stud ); if( parseInt(result) != -1 ){ // console.log( stud ); var n = self._jsondata[stud]["check"][String(self._classID)]["score"] ; if( typeof(n) !== "undefined" ){ self._jsondata[stud]["check"][String(self._classID)] = { "score": parseInt( n ) + 1, "comments":"" };// = parseInt( n ) + 1; }else { self._jsondata[stud]["check"][String(self._classID)] //= 1; = { "score": 1, "comments":"" }; } } } } self._myFileParser.close(); } stat( jsonFilePath ) { var self = this; var validJSONFile = true; if( jsonFilePath ) { // do nothing } else{ validJSONFile = false; } if( typeof jsonFilePath == 'undefined' ) { validJSONFile = false; } if( jsonFilePath === "" ) { validJSONFile = false; } try{ if( Boolean(validJSONFile) ) { self._jsonFileSource = jsonFilePath; var fileData = fs.readFileSync(self._jsonFileSource, "utf8"); self._jsondata = JSON.parse(fileData); } }catch( exception ) { console.log( exception ); } var ok = 0; var mis = 0; for( var stud in self._jsondata ){ var n = self._jsondata[stud]["check"][String(self._classID)]["score"] ; if( parseInt( n ) > 0 ){ ok++; }else { mis++; console.log( stud + ", " + self._jsondata[stud]["name"] + + ", " + self._jsondata[stud]["pinyin"] + ", " + self._jsondata[stud]["year"] + ", " + self._jsondata[stud]["major"] + ", " + ", " + self._jsondata[stud]["class"]); } } console.log( "实到 " + ok ); console.log( "缺勤 " + mis ); } // iterating the dir iteratDir( dir ) { var results = [] var workDir = __dirname; var list = fs.readdirSync(dir) list.forEach(function(file) { file = dir + '/' + file var stat = fs.statSync(file) if (stat && stat.isDirectory()) results = results.concat( file ); else results.push(file) }) return results } addHomework( id ){ var self = this; var fileData = fs.readFileSync(self._jsonFileSource, "utf8"); self._jsondata = JSON.parse(fileData); for(var stud in self._jsondata ){ try{ var n = self._jsondata[stud]["homework"][String(id)]; if( typeof(n) !== "undefined" ){ // self._jsondata[stud]["homework"][String(id)] = 60; }else { self._jsondata[stud]["homework"][String(id)]= { "score": 0, "comments":"" }; } }catch( exp ){ console.log( exp ); } } } homework( ite ) { var self = this; var fileData = fs.readFileSync(self._jsonFileSource, "utf8"); self._jsondata = JSON.parse(fileData); var curDir = process.cwd(); var hwDir = path.join(curDir, String(ite)); let state = fs.statSync( hwDir ); if( !state.isDirectory() ){ console.log("Invalid directory:", hwDir); return; } var r = self.iteratDir(hwDir); // console.log(r); var validHWNumber = 0; for( var it in r ){ var theline = r[it]; for(var stud in self._jsondata ){ var result = theline.indexOf( stud ); if( parseInt(result) != -1 ){ // console.log( stud ); var n = self._jsondata[stud]["homework"][String(ite)] ; if( typeof(n) !== "undefined" ){ self._jsondata[stud]["homework"][String(ite)] = { "score": 60, "comments":"" }; validHWNumber++; }else { self._jsondata[stud]["homework"][String(ite)] = { "score": 0, "comments":"" }; } } } } console.log(" Checking homework directory:", hwDir ); console.log(" Valid homework number:", validHWNumber ); } homeworkStat( ite ){ var self = this; var handinNum = 0; var voidNum = 0; var unHandinList = []; var fileData = fs.readFileSync(self._jsonFileSource, "utf8"); self._jsondata = JSON.parse(fileData); for(var stud in self._jsondata ){ var n = self._jsondata[stud]["homework"][String(ite)] ; if( typeof(n) !== "undefined" ){ var score = self._jsondata[stud]["homework"][String(ite)]["score"]; if(parseInt(score) > 0 ){ handinNum++; }else{ voidNum++; unHandinList.push( stud ); } } } console.log(" Hand in stud number:", handinNum ); console.log(" NOT Hand in stud number:", voidNum ); console.log(unHandinList); } htmlhead( ) { var self = this; self._datalist = []; self._datalist.push("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); self._datalist.push("<html>"); self._datalist.push(" <head>"); self._datalist.push(" <title> </title> "); self._datalist.push(" <meta charset=\"utf-8\"> "); self._datalist.push(" </head>"); self._datalist.push(" <body>"); } htmlend( ) { var self = this; self._datalist.push(" </body>"); self._datalist.push("</html>"); self.writedatafile(self._htmlFilePath); } setHwScore( id, type, hwid, score ){ var self = this; try{ self._jsondata[id][type][hwid]["score"] = score; } catch(ep){ console.log( "setHwScore:", id, type, hwid, score); console.log( ep ); } } writescoretohtml( type ) { var self = this; var fileData = fs.readFileSync(self._jsonFileSource, "utf8"); self._jsondata = JSON.parse(fileData); self._datalist.push(" <table border=\"0\" id=\"score\">"); self._datalist.push(" <tr>"); self._datalist.push(" <th contentEditable=\"true\"> ID </th>"); self._datalist.push(" <th contentEditable=\"true\"> MAME </th>"); for( var id in self._jsondata ){ var stud = self._jsondata[id]; var len = 1; for( var hwid in self._jsondata[id][type] ) { if(parseInt(hwid) > len ) { len = parseInt(hwid); } } for( var theID = 1; theID <= len; theID++ ) { self._datalist.push(" <th contentEditable=\"true\">" + theID + "</th>"); } break; } self._datalist.push(" </tr>"); for( var id in self._jsondata ){ var stud = self._jsondata[id]; self._datalist.push(" <tr>"); self._datalist.push(" <td>" + id + "</td>"); self._datalist.push(" <td>" + self._jsondata[id]["name"] + "</td>"); var scoreList = []; var len = 1; for( var hwid in self._jsondata[id][type] ) { var theScore = self._jsondata[id][type][hwid]["score"]; scoreList[parseInt(hwid)] = theScore; if(parseInt(hwid) > len ) { len = parseInt(hwid); } } for( var theID = 0; theID < len; theID++ ) { var value = scoreList[parseInt(theID)+1]; if( typeof value == 'undefined' ) { value = 0; } // console.log( "self._editable ", self._editable ); if( self._editable ){ self._datalist.push(" <td contentEditable=\"true\">" + value + "</td>"); }else{ self._datalist.push(" <td>" + value + "</td>"); } } self._datalist.push(" </tr>"); } self._datalist.push(" </table>"); if( self._editable ){ self._datalist.push(" <p><input type=\"button\" name=\"btn\" value=\"Table Content\" onclick=\"showTableContent('score')\"></p>"); self._datalist.push(" <p><div id=\"result\"></div></p>"); self._datalist.push(" <script type=\"text/javascript\">"); self._datalist.push(" function getTableContent(id){"); self._datalist.push(" var mytable = document.getElementById(id);"); self._datalist.push(" var data = [];"); self._datalist.push(" for(var i=0,rows=mytable.rows.length; i<rows; i++){"); self._datalist.push(" for(var j=0,cells=mytable.rows[i].cells.length; j<cells; j++){"); self._datalist.push(" if(!data[i]){"); self._datalist.push(" data[i] = new Array();"); self._datalist.push(" }"); self._datalist.push(" data[i][j] = mytable.rows[i].cells[j].innerHTML;"); self._datalist.push(" }"); self._datalist.push(" }"); self._datalist.push(" return data;"); self._datalist.push(" }"); self._datalist.push(" function showTableContent(id){"); self._datalist.push(" var data = getTableContent(id);"); self._datalist.push(" var tmp = \"\";"); self._datalist.push(" for(i=0,rows=data.length; i<rows; i++){"); self._datalist.push(" var id = data[i][0];"); self._datalist.push(" for(j=2,cells=data[i].length; j<cells; j++){"); //self._datalist.push(" tmp += data[i][j] + \",\";"); self._datalist.push(" var n = j - 1; "); self._datalist.push(" var score = data[i][j]; "); self._datalist.push(" if( parseInt( score ) == 0 ) continue; "); self._datalist.push(" tmp += \"web.setHwScore(\\\"\"+id+\"\\\",\\\"homework\\\", \\\"\" + n + \"\\\",\" + score + \" );\";"); self._datalist.push(" tmp += \"<br>\";"); self._datalist.push(" }"); self._datalist.push(" tmp += \"<br>\";"); self._datalist.push(" }"); self._datalist.push(" document.getElementById(\"result\").innerHTML = tmp;"); self._datalist.push(" }"); self._datalist.push(" </script>"); } } // class plan jiaoanhead( name, semester, teacher ) { var self = this; self._datalist = self.clone( self._latexcnarticlehead); self.latexfilepackage(self._testFilePath); self.latexfilenewcommand( self._latexNewcommandList ); self._datalist.push("\n "); self._datalist.push("\\begin{document} "); self._datalist.push("\\begin{CJK}{UTF8}{song}"); self._datalist.push("\\begin{titlepage} "); self._datalist.push(" \\centering "); self._datalist.push(" \\includegraphics[width=0.4\\textwidth]{jgxy.png} \\\\ "); self._datalist.push(" \\includegraphics[width=0.8\\textwidth]{jg-title.png} "); self._datalist.push(" \\par "); self._datalist.push("\\vspace{1cm} "); self._datalist.push("{\\huge\\itshape 教~~学~~简~~案 \\par} "); self._datalist.push("\\vspace{5cm} "); self._datalist.push("\\begin{Large} "); self._datalist.push("\\begin{tabular}{r l} "); self._datalist.push(" 课 程:& " + name + " \\\\ "); self._datalist.push(" 姓 名:& " + teacher + " \\\\ "); self._datalist.push(" 教学单位:& \\\\ "); self._datalist.push(" 职 称:& \\\\ "); self._datalist.push(" 学 期:& " + semester + " \\\\ "); self._datalist.push("\\end{tabular} "); self._datalist.push("\\end{Large} "); self._datalist.push("\\vfill "); self._datalist.push(" {\\large \\today\\par} "); self._datalist.push("\\end{titlepage}"); } jiaoanend( ref ) { var self = this; self._datalist.push("\\newpage "); self._datalist.push("\\myTitleA{参考资料} "); self._datalist.push("\\spacingContent{ " + ref + " }"); self._datalist.push("\\end{CJK} "); self._datalist.push("\\end{document}"); } isObject = function(a) { return (!!a) && (a.constructor === Object); }; writeJiaoAnSection( theJson ){ var self = this; if( undefined == theJson ){ return; } if( Array.isArray(theJson) ){ for(var theKey in theJson) { // figure object var theEntry = theJson[theKey]; if( self.isObject( theEntry ) ) { var width = theEntry["figure"]["width"]; var path = theEntry["figure"]["path"]; if( self.checkingObject(path) ) { self._datalist.push(" \\spacingContent{"); self._datalist.push("\\begin{figure}[h] "); self._datalist.push(" \\centering "); self._datalist.push(" \\includegraphics[width="+width+"\\textwidth]{" + path + "} "); //self._datalist.push("\\caption{ 题" + index + "} "); //self._datalist.push(" \\label{fig: } "); self._datalist.push("\\end{figure} "); self._datalist.push(" } "); self._datalist.push(" "); } } else{ self._datalist.push(" \\spacingContent{"); self._datalist.push( theJson[theKey] + ""); self._datalist.push(" } "); self._datalist.push(" "); } } }else{ self._datalist.push(" \\spacingContent{"); self._datalist.push( theJson + ""); self._datalist.push(" } "); self._datalist.push(" "); } } jiaoan( jsonFile ) { var self = this; try{ var tmp = fs.statSync( jsonFile ).isFile(); if( !Boolean(tmp) ){ console.error("init(): Invalid json file path."); return; } }catch( exception ) { console.log( exception ); } var jiaoanData = fs.readFileSync(jsonFile, "utf8"); var jiaoanList = JSON.parse(jiaoanData); var name = jiaoanList["name"]; var teacher = jiaoanList["teacher"]; var semester = jiaoanList["semester"]; var target = jiaoanList["target"]; var mainMethods = jiaoanList["mainMethod"]; var teachTarget = jiaoanList["teachTarget"]; var targetCharacters = jiaoanList["targetCharacters"]; var ref = jiaoanList["reference"]; self.jiaoanhead( name, semester, teacher ); var classes = jiaoanList["classes"]; for( var ite in classes ) { var date = jiaoanList["classes"][ite]["date"]; var teachTarget = jiaoanList["classes"][ite]["teachTarget"]; var hardPoints = jiaoanList["classes"][ite]["hardPoints"]; var topic = jiaoanList["classes"][ite]["topic"]; var introduction = jiaoanList["classes"][ite]["introduction"]; var content_1 = jiaoanList["classes"][ite]["content_1"]; var content_2 = jiaoanList["classes"][ite]["content_2"]; var summary = jiaoanList["classes"][ite]["summary"]; var homework = jiaoanList["classes"][ite]["homework"]; self._datalist.push(" "); self._datalist.push("%%--------------------------- "); self._datalist.push( "%% 第 " + ite + " 次课"); self._datalist.push( "%%--------------------------- "); self._datalist.push( "\\clearpage "); self._datalist.push("\\begin{center}"); self._datalist.push(" {\\Large " + name + "简案} "); self._datalist.push("\\end{center}"); self._datalist.push(" "); self._datalist.push(" \\vspace{0.5cm} \n "); self._datalist.push("\\myTitleA{授课时间} "); self._datalist.push(" \\content{" + date + "} "); self._datalist.push(" "); self._datalist.push("\\myTitleA{授课对象} "); self._datalist.push(" \\content{" + target + " } "); self._datalist.push(" "); self._datalist.push("\\myTitleB{授课对象特点} "); self._datalist.push(" \\content{"); self._datalist.push( targetCharacters + ""); self._datalist.push(" } "); self._datalist.push(" "); self._datalist.push(" \\vspace{0.5cm}"); self._datalist.push(" \\myTitleA{授课主题} "); self.writeJiaoAnSection( topic ); self._datalist.push(" \\myTitleA{教学目的与要求} "); self.writeJiaoAnSection( teachTarget ); self._datalist.push(" \\myTitleA{教学重、难点} "); self._datalist.push(" \\spacingContent{"); self._datalist.push(hardPoints + ""); self._datalist.push(" } "); self._datalist.push(" "); self._datalist.push(" \\myTitleA{主要教学方法} "); self.writeJiaoAnSection( mainMethods ); self._datalist.push(" \\myTitleA{教学内容的组织与设计} "); self._datalist.push(" "); if(content_2 != '' ){ self._datalist.push(" \\normalCentralTitle{第一堂课}"); self._datalist.push(" "); } if( introduction != '' ) { self._datalist.push(" \\myTitleB{引入新课} "); self.writeJiaoAnSection( introduction ); } self._datalist.push(" \\myTitleB{ 新课讲授 } "); self.writeJiaoAnSection( content_1 ); if(content_2 != '' ){ self._datalist.push(" \\normalCentralTitle{第二堂课}"); self._datalist.push(" "); self._datalist.push(" \\myTitleB{新课讲授} "); self.writeJiaoAnSection( content_2 ); } if( summary != '' ) { self._datalist.push(" \\myTitleB{课堂小结} "); self.writeJiaoAnSection( summary ); } if( homework != '' ) { self._datalist.push(" \\myTitleA{作业布置} "); self.writeJiaoAnSection( homework ); } } self.jiaoanend(ref); self.writedatafile(self._jiaoanTexFilePath); } latexfilepackage( texfilepath ) { var self = this; for( var ite in self._latexPackageList ) self._datalist.push( "\\usepackage"+ self._latexPackageList[ite] ); } latexfilenewcommand( texfilepath ) { var self = this; for( var ite in self._latexNewcommandList ) self._datalist.push( "\\newcommand"+ self._latexNewcommandList[ite] ); } // Latex file head examTexHead( texFilePath ) { var self = this; self._testFilePath = texFilePath; self._datalist = self.clone( self._latexcnarticlehead); var examName = self._jsondata["exam_name"]; var examDesp = self._jsondata["exam_des"]; var teacher = self._jsondata["teacher"]; var year = self._jsondata["year"]; var term = self._jsondata["term"]; var category = self._jsondata["category"]; var open = self._jsondata["open"]; var time_length = self._jsondata["time_length"]; self.latexfilepackage(self._testFilePath); self._datalist.push("\\geometry{left=1cm,right=3cm,top=1cm,bottom=2cm} "); self._datalist.push("\n "); self._datalist.push("\\begin{document}"); self._datalist.push("\\begin{CJK}{UTF8}{song}\n \n"); self._datalist.push("\\fancyhead{} "); self._datalist.push("\\fancyfoot[C] {" + category + "卷 第~\\thepage~页~共~\\pageref{LastPage}~页} "); self._datalist.push("\\renewcommand{\\headrulewidth}{0 pt} "); self._datalist.push("\\pagestyle{fancy} "); self._datalist.push("\\begin{minipage}{0.2\\linewidth} "); self._datalist.push("\\centerline{\\includegraphics[width= 1.2\\textwidth]{jgxy.png}} "); self._datalist.push("\\end{minipage} "); self._datalist.push("\\hfill "); self._datalist.push("\\begin{minipage}{.85\\linewidth} "); self._datalist.push("\\centerline{\\includegraphics[width= 0.9\\textwidth]{test-title.png}} "); self._datalist.push("\\begin{center} "); self._datalist.push("\\textbf{ "); self._datalist.push("课程《\\underline{" + examName+ "}》"); if(( category == "A" ) || ( category == "a" )){ self._datalist.push("【$\\surd$ A卷 \\qedsymbol B卷】"); }else{ self._datalist.push("【\\qedsymbol A卷 $\\surd$ B卷】"); } self._datalist.push("任课教师 \\underline{ " + teacher + " } \\\\ "); self._datalist.push("\\underline{" + year+ "}学年 第\\underline{~~" + term + "~~}学期 "); self._datalist.push("考试时长: \\underline{~"+time_length+"~}分钟 "); if(( open == "Y" ) || ( open == "y" )){ self._datalist.push("【\\qedsymbol \\textbf{闭卷} $\\surd$ 开卷】 "); }else{ self._datalist.push("【$\\surd$ \\textbf{闭卷} \\qedsymbol 开卷】 "); } self._datalist.push("} "); self._datalist.push("\\end{center} "); self._datalist.push("\\end{minipage} "); self._datalist.push( "\\noindent\\rule[0.25\\baselineskip]{1.1\\textwidth}{2pt}"); self._datalist.push( " "); self._datalist.push( "\\begin{small} "); self._datalist.push( "说明: "); self._datalist.push( " "); self._datalist.push( "\\hfill "); self._datalist.push( "\\begin{minipage}{.95\\linewidth} "); self._datalist.push( "\\begin{enumerate} "); self._datalist.push( " \\item 请填写课程名称、任课教师姓名、考试时长和考试方式,并注明是A卷还是B卷; "); self._datalist.push( " \\item 请在该试卷上集中出题,标清题号,学生答题纸将统一另备。 "); self._datalist.push( "\\end{enumerate} "); self._datalist.push( "\\end{minipage} "); self._datalist.push( "\\end{small} "); self._datalist.push( " "); self._datalist.push( "\\noindent\\rule[0.25\\baselineskip]{1.1\\textwidth}{2pt}"); self._datalist.push( " "); } // adding an item addJSONEntry( itemID, theItem ) { var self = this; var result = self.checkingObject( self._jsondata); if(! Boolean( result )) { console.error("addJSONEntry(): no exam data object"); return; } result = self.checkingObject( theItem ); if(! Boolean( result )) { console.error("addJSONEntry(): no item object"); return; } self._jsondata[itemID] = theItem; } // write latex test file examTexEnd( ) { var self = this; self._datalist.push("\\clearpage "); self._datalist.push("\\end{CJK} "); self._datalist.push("\\end{document} "); self.writedatafile(self._testFilePath); } // adding an item addExam( itemType, itemID, theItem ) { var self = this; var result = self.checkingObject( self._jsondata); if(! Boolean( result )) { console.error("addExam(): no exam data object"); return; } result = self.checkingObject( theItem ); if(! Boolean( result )) { console.error("addItem(): no item object"); return; } if(!Boolean(self.checkingObject( self._jsondata[itemType]))) { console.error("addItem(): no item " + itemType); self._jsondata[itemType] = {}; } self._jsondata[itemType][itemID] = theItem; } writeExamSubject( type ) { var self = this; var name = self._jsondata[ "exam_type" ][type]["name"]; var des = self._jsondata[ "exam_type" ][type]["des"]; self._datalist.push("\\section{" + name + "}"); self._datalist.push( des + "\n \\vspace{0.5cm} "); var index = 0; for(var item in self._jsondata[ type ] ) { index++; // self._datalist.push("\\subsection{ }"); self._datalist.push("\\begin{spacing}{" + self._linespace + "} "); self._datalist.push("\\noindent " + index + ") "+ self._jsondata[type][item]["subject"] + " "); // figure object var figure = self._jsondata[type][item]["figure"]; if( self.checkingObject(figure) ) { var width = self._jsondata[type][item]["figure"]["width"]; var path = self._jsondata[type][item]["figure"]["path"]; if( self.checkingObject(path) ) { self._datalist.push("\\begin{figure}[h] "); self._datalist.push(" \\centering "); self._datalist.push(" \\includegraphics[width="+width+"\\textwidth]{" + path + "} "); self._datalist.push("\\caption{ 题" + index + "} "); self._datalist.push(" \\label{fig:" +type+ "-" + index + "} "); self._datalist.push("\\end{figure} "); } } var choice = self._jsondata[type][item]["choices"]; if( self.checkingObject(choice) ) { self._datalist.push("\\begin{enumerate}[label=(\\Alph*)] "); for(var it in self._jsondata[type][item]["choices"] ) { self._datalist.push(" \\item " + self._jsondata[type][item]["choices"][it] + " "); } self._datalist.push("\\end{enumerate} "); } self._datalist.push("\\end{spacing} "); if(Boolean(self._hasAnswer)) { self._datalist.push("解:"); self._datalist.push(self._jsondata[type][item]["answer"] + " "); } if(Boolean(self._hasComment)) { self._datalist.push(""); self._datalist.push(self._jsondata[type][item]["comment"] + " "); } self._datalist.push(" \n"); } } // // // classexamfile( examFilePath ) { var self = this; self.openjsonfile(examFilePath); var type = "multiple_choice"; var itemIndex = 1; for(var item in self._jsondata[ type ] ) { self._examhtmlheadlist.push( " <br> "); self._examhtmlheadlist.push( " <table width=\"800\"> "); self._examhtmlheadlist.push( " <tr colspan=\"4\"> "+ itemIndex + ") " + self._jsondata[type][item]["subject"] + " </tr> "); var index = 1; var itemList = ["A", "B", "C", "D"]; for(var it in self._jsondata[type][item]["choices"] ) { self._examhtmlheadlist.push( " <tr> "); self._examhtmlheadlist.push( " <td><label> "); self._examhtmlheadlist.push( " <input type=\"radio\" name=\"" + item + "\" value=\"单选\" id=\"rg1_value"+index+"\" /> "); self._examhtmlheadlist.push( " " + itemList[index-1] + ") " + self._jsondata[type][item]["choices"][it] + " </label></td> "); self._examhtmlheadlist.push( " </tr> "); index++; } self._examhtmlheadlist.push( " </table> "); itemIndex++; } self._examhtmlheadlist.push( " <script type=\"text/javascript\"> "); self._examhtmlheadlist.push( " var result = 0; "); self._examhtmlheadlist.push( " var perScore = 5; "); self._examhtmlheadlist.push( " var userAnswer = []; "); var answerstr = " var trueAnswer = ["; for(var item in self._jsondata[ type ] ) { var answer = self._jsondata[type][item]["answer"]; if(answer == 'A' ){ answerstr += "0,"; } if(answer == 'B' ){ answerstr += "1,"; } if(answer == 'C' ){ answerstr += "2,"; } if(answer == 'D' ){ answerstr += "3,"; } if(answer == 'E' ){ answerstr += "4,"; } } answerstr += "0]; "; self._examhtmlheadlist.push( answerstr); self._examhtmlheadlist.push( " function showResult(){ "); self._examhtmlheadlist.push( " userAnswer = []; "); self._examhtmlheadlist.push( " result = 0; "); for(var item in self._jsondata[ type ] ) { self._examhtmlheadlist.push( " getUserAnswer(" + item + "); "); } self._examhtmlheadlist.push( " if(userAnswer){ "); self._examhtmlheadlist.push( " for(var i = 0;i< userAnswer.length;i++){ "); self._examhtmlheadlist.push( " if(userAnswer[i] == trueAnswer[i]) "); self._examhtmlheadlist.push( " result += perScore; "); self._examhtmlheadlist.push( " } "); self._examhtmlheadlist.push( " } "); // showing the score self._examhtmlheadlist.push( " alert(\"得分: \" + result); "); self._examhtmlheadlist.push( " } "); self._examhtmlheadlist.push( " </script> "); self._datalist = self._examhtmlheadlist.concat(self._examhtmlendlist); self.writedatafile(self._testFilePath); } }