123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- import { numberToChinese } from "../plugins/utils";
- import {
- getModel as createFillQuestion,
- getFullModel as getFillQuesitonElements,
- } from "../elements/fill-question/model";
- import {
- getModel as createFillLine,
- getFullModel as getFillLineElements,
- } from "../elements/fill-line/model";
- import {
- getModel as createExplain,
- getFullModel as getExplainElements,
- } from "../elements/explain/model";
- const COMMON_QUESTION_TYPES = [
- "SINGLE_ANSWER_QUESTION",
- "MULTIPLE_ANSWER_QUESTION",
- "BOOL_ANSWER_QUESTION",
- "FILL_BLANK_QUESTION",
- "TEXT_ANSWER_QUESTION",
- ];
- // TODO: READING_COMPREHENSION,LISTENING_QUESTION,CLOZE,PARAGRAPH_MATCHING,BANKED_CLOZE
- const structTypeToCardElementType = {
- SINGLE_ANSWER_QUESTION: "FILL_QUESTION",
- MULTIPLE_ANSWER_QUESTION: "FILL_QUESTION",
- BOOL_ANSWER_QUESTION: "FILL_QUESTION",
- FILL_BLANK_QUESTION: "FILL_LINE",
- TEXT_ANSWER_QUESTION: "EXPLAIN",
- };
- const elementModel = {
- FILL_QUESTION: (presetData) => {
- const model = createFillQuestion(presetData);
- return getFillQuesitonElements(model, { pageSize: "A3", columnNumber: 2 });
- },
- FILL_LINE: (presetData) => {
- const model = createFillLine(presetData);
- return getFillLineElements(model, presetData.questions);
- },
- EXPLAIN: (presetData) => {
- const model = createExplain(presetData);
- return getExplainElements(model);
- },
- };
- function parseCommonPaperStruct(paperJson) {
- let structList = [];
- paperJson.paperDetails.forEach((detail) => {
- const commonQuestions = detail.paperDetailUnits.filter((q) =>
- COMMON_QUESTION_TYPES.includes(q.questionType)
- );
- if (commonQuestions.length) {
- const commonStructList = getCommonQuestionStructList(
- detail,
- commonQuestions,
- true,
- 0
- );
- structList.push(...commonStructList);
- }
- const nestedQuestions = detail.paperDetailUnits.filter(
- (q) => !COMMON_QUESTION_TYPES.includes(q.questionType)
- );
- if (nestedQuestions.length) {
- const nestedStructList = getNestedQuestionStructList(
- detail,
- nestedQuestions
- );
- structList.push(...nestedStructList);
- }
- });
- structList.sort((a, b) => a.sortNo - b.sortNo);
- console.log(structList);
- return structList;
- }
- function getCommonQuestionStructList(
- detail,
- questions,
- isCommon,
- nestedQNo = 0
- ) {
- let structList = [];
- let structs = {};
- const qTypeNum = isCommon ? 1 : 2;
- questions.forEach((question) => {
- if (!structs[question.questionType]) structs[question.questionType] = [];
- structs[question.questionType].push(question);
- });
- Object.keys(structs).forEach((structType) => {
- const typeNo = COMMON_QUESTION_TYPES.indexOf(structType);
- const questions = parseCommonTypeQuestions(structType, structs[structType]);
- structList.push({
- detailName: detail.name,
- detailNo: detail.number,
- structType,
- isCommon,
- nestedQNo,
- sortNo:
- typeNo * 10000 + detail.number * 1000 + qTypeNum * 100 + nestedQNo,
- questions,
- });
- });
- return structList;
- }
- function getNestedQuestionStructList(detail, questions) {
- let structList = [];
- questions.forEach((question) => {
- const qStructList = getCommonQuestionStructList(
- detail,
- question.subQuestions,
- false,
- question.number
- );
- structList.push(...qStructList);
- });
- return structList;
- }
- function parseCommonTypeQuestions(structType, dataList) {
- const choiceQs = [
- "SINGLE_ANSWER_QUESTION",
- "MULTIPLE_ANSWER_QUESTION",
- "BOOL_ANSWER_QUESTION",
- ];
- let questions = [];
- if (choiceQs.includes(structType)) {
- questions = dataList.map((item) => {
- return {
- questionNo: item.number,
- optionCount:
- structType === "BOOL_ANSWER_QUESTION"
- ? 2
- : item.question.quesOptions.length,
- };
- });
- } else if (structType === "FILL_BLANK_QUESTION") {
- questions = dataList.map((item) => {
- return {
- questionNo: item.number,
- fillCount: getFillBackClozeCount(item.question.quesBody),
- };
- });
- } else {
- questions = dataList.map((item) => item.number);
- }
- return questions;
- }
- function getFillBackClozeCount(questionBodyContent) {
- // TODO:
- let questionBody = null;
- let num = 0;
- try {
- questionBody = JSON.parse(questionBodyContent);
- questionBody.sections.forEach((section) => {
- section.blocks.forEach((block) => {
- if (block.type === "cloze") num++;
- });
- });
- } catch (error) {
- num = 1;
- }
- return num;
- }
- function buildCardElements(structList) {
- let cardElements = [];
- structList.forEach((struct) => {
- const detailChineseNumber = numberToChinese(struct.detailNo);
- let topicName = struct.isCommon
- ? `${detailChineseNumber}、${struct.detailName}`
- : `${detailChineseNumber}(${struct.nestedQNo})`;
- const modelType = structTypeToCardElementType[struct.structType];
- if (!struct.isCommon && struct.structType === "TEXT_ANSWER_QUESTION") {
- struct.questions.forEach((qno) => {
- const tname = `${topicName}、${qno}`;
- const elements = elementModel[modelType]({
- questions: [""],
- topicName: tname,
- });
- cardElements.push(...elements);
- });
- return;
- }
- let presetData = {
- questions: struct.questions,
- topicName,
- };
- if (struct.structType === "MULTIPLE_ANSWER_QUESTION")
- presetData.isMultiply = true;
- if (struct.structType === "BOOL_ANSWER_QUESTION")
- presetData.isBoolean = true;
- const elements = elementModel[modelType](presetData);
- cardElements.push(...elements);
- });
- return cardElements;
- }
- export function buildPaperCard(paperJson) {
- const structList = parseCommonPaperStruct(paperJson);
- const cardElements = buildCardElements(structList);
- console.log(cardElements);
- return cardElements;
- }
- // const paperStruct = {
- // paperName: "",
- // structList: [
- // {
- // detailName: "",
- // detailNo: 1,
- // structType: 1,
- // questions: [
- // { questionNo: "1", optionCount: 4 },
- // { questionNo: "2", optionCount: 4 },
- // ],
- // },
- // {
- // detailName: "",
- // detailNo: 1,
- // structType: 2,
- // questions: [
- // { questionNo: "3", optionCount: 4 },
- // { questionNo: "4", optionCount: 4 },
- // ],
- // },
- // {
- // detailName: "",
- // detailNo: 1,
- // structType: 3,
- // questions: [
- // { questionNo: "5", optionCount: 2 },
- // { questionNo: "6", optionCount: 2 },
- // ],
- // },
- // {
- // detailName: "",
- // detailNo: 1,
- // structType: 4,
- // questions: [
- // { questionNo: "7", fillCount: 1 },
- // { questionNo: "8", fillCount: 2 },
- // ],
- // },
- // {
- // detailName: "",
- // detailNo: 1,
- // structType: 5,
- // questions: ["11", "12"],
- // },
- // ],
- // };
|