Error handling for parser
This commit is contained in:
Родитель
1e3b44f056
Коммит
70ce0d70ac
|
@ -56,30 +56,44 @@ export const UploadQuestionBanksComponent = (
|
|||
}
|
||||
var parserFactory = new AssessmentAppParserFactory(text.toString(), selectedOption.key);
|
||||
var parser = parserFactory.parser;
|
||||
parser.parse();
|
||||
var questionBanks: ParsedQuestionBank[] = parser.questionbanks;
|
||||
try{
|
||||
parser.parse();
|
||||
var questionBanks: ParsedQuestionBank[] = parser.questionbanks;
|
||||
|
||||
for (let qb_id in questionBanks){
|
||||
var questionBank:ParsedQuestionBank = questionBanks[qb_id];
|
||||
const bank = await repositoryContext.createNewQuestionBank({
|
||||
id: "",
|
||||
name: questionBank.questionBankTitle,
|
||||
description: "",
|
||||
lastModified: new Date(),
|
||||
questionIds: [],
|
||||
assessmentType: "",
|
||||
});
|
||||
|
||||
const questions:Question[] = questionBank.questions;
|
||||
for (let questionId in questions){
|
||||
await repositoryContext.saveNewQuestion(bank.id, questions[questionId])
|
||||
for (let qb_id in questionBanks){
|
||||
var questionBank:ParsedQuestionBank = questionBanks[qb_id];
|
||||
const bank = await repositoryContext.createNewQuestionBank({
|
||||
id: "",
|
||||
name: questionBank.questionBankTitle,
|
||||
description: "",
|
||||
lastModified: new Date(),
|
||||
questionIds: [],
|
||||
assessmentType: "",
|
||||
});
|
||||
|
||||
const questions:Question[] = questionBank.questions;
|
||||
for (let questionId in questions){
|
||||
await repositoryContext.saveNewQuestion(bank.id, questions[questionId])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
onFinish(true);
|
||||
setInProgress(false);
|
||||
}
|
||||
catch (err){
|
||||
if (err instanceof Error) {
|
||||
alert(err.message);
|
||||
}
|
||||
else{
|
||||
alert("Could not parse the file");
|
||||
}
|
||||
onFinish(false);
|
||||
setInProgress(false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
onFinish(true);
|
||||
setInProgress(false);
|
||||
|
||||
}
|
||||
reader.readAsText(selectedFile);
|
||||
};
|
||||
|
|
|
@ -10,67 +10,80 @@ export class GiftParser extends AssessmentAppParser{
|
|||
public parse(): void {
|
||||
var questionBankTitle:string = "Not defined yet";
|
||||
var questions:Question[] = [];
|
||||
|
||||
const quiz: GIFTQuestion[] = parse(this.raw)
|
||||
for (let question in quiz){
|
||||
var q: GIFTQuestion = quiz[question]
|
||||
console.log(q);
|
||||
if (q.type === "Category"){
|
||||
questionBankTitle = q.title;
|
||||
}
|
||||
else{
|
||||
var stem:TextFormat = q.stem;
|
||||
var answerTexts:string[] = [];
|
||||
var correctAnswers:string[] = []
|
||||
var questionType:string = '';
|
||||
var results:questionTypeSpecificParse = {options:[], correctAnswer:[]}
|
||||
|
||||
if (q.type === "MC"){
|
||||
questionType = "MCQ";
|
||||
results = this.getMCQ(q);
|
||||
|
||||
}
|
||||
else if(q.type === "TF"){
|
||||
questionType = "MCQ";
|
||||
results = this.getTF(q);
|
||||
|
||||
}
|
||||
else if (q.type === "Short"){
|
||||
questionType = "QA";
|
||||
results = this.getShort(q);
|
||||
}
|
||||
else if (q.type === "Numerical"){
|
||||
questionType = "QA";
|
||||
results = this.getNumerical(q);
|
||||
try{
|
||||
const quiz: GIFTQuestion[] = parse(this.raw);
|
||||
for (let question in quiz){
|
||||
var q: GIFTQuestion = quiz[question]
|
||||
console.log(q);
|
||||
if (q.type === "Category"){
|
||||
questionBankTitle = q.title;
|
||||
}
|
||||
else{
|
||||
continue;
|
||||
var stem:TextFormat = q.stem;
|
||||
var answerTexts:string[] = [];
|
||||
var correctAnswers:string[] = []
|
||||
var questionType:string = '';
|
||||
var results:questionTypeSpecificParse = {options:[], correctAnswer:[]}
|
||||
|
||||
if (q.type === "MC"){
|
||||
questionType = "MCQ";
|
||||
results = this.getMCQ(q);
|
||||
|
||||
}
|
||||
else if(q.type === "TF"){
|
||||
questionType = "MCQ";
|
||||
results = this.getTF(q);
|
||||
|
||||
}
|
||||
else if (q.type === "Short"){
|
||||
questionType = "QA";
|
||||
results = this.getShort(q);
|
||||
}
|
||||
else if (q.type === "Numerical"){
|
||||
questionType = "QA";
|
||||
results = this.getNumerical(q);
|
||||
}
|
||||
else{
|
||||
continue;
|
||||
}
|
||||
correctAnswers = results.correctAnswer;
|
||||
answerTexts = results.options;
|
||||
|
||||
const question: Question = {
|
||||
id: "",
|
||||
name: this.removeTags(stem.text),
|
||||
description: this.removeTags(stem.text),
|
||||
lastModified: new Date (),
|
||||
options: answerTexts,
|
||||
answer: correctAnswers,
|
||||
textType:stem.format,
|
||||
questionType: questionType,
|
||||
}
|
||||
questions.push(question);
|
||||
|
||||
|
||||
|
||||
}
|
||||
correctAnswers = results.correctAnswer;
|
||||
answerTexts = results.options;
|
||||
|
||||
const question: Question = {
|
||||
id: "",
|
||||
name: this.removeTags(stem.text),
|
||||
description: this.removeTags(stem.text),
|
||||
lastModified: new Date (),
|
||||
options: answerTexts,
|
||||
answer: correctAnswers,
|
||||
textType:stem.format,
|
||||
questionType: questionType,
|
||||
}
|
||||
questions.push(question);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
var qb: ParsedQuestionBank = {
|
||||
questionBankTitle: questionBankTitle,
|
||||
questions:questions
|
||||
};
|
||||
this.questionbanks.push(qb);
|
||||
}
|
||||
|
||||
var qb: ParsedQuestionBank = {
|
||||
questionBankTitle: questionBankTitle,
|
||||
questions:questions
|
||||
};
|
||||
this.questionbanks.push(qb);
|
||||
catch (err)
|
||||
{
|
||||
if (err instanceof Error){
|
||||
throw new Error(err.message);
|
||||
}
|
||||
else{
|
||||
throw new Error("Thie file could not be parsed by the GIFT parser");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -7,56 +7,62 @@ export class MicrosoftOSCParser extends AssessmentAppParser{
|
|||
|
||||
public parse(): void {
|
||||
|
||||
const rawData = JSON.parse(this.raw);
|
||||
try{
|
||||
const rawData = JSON.parse(this.raw);
|
||||
|
||||
for (let rawBank of rawData){
|
||||
|
||||
for (let rawQuestion of rawBank.quizzes) {
|
||||
var questions:Question[] = [];
|
||||
for (let question of rawQuestion.quiz){
|
||||
var answerTexts = Array();
|
||||
var correctAnswer = [];
|
||||
var counter = 0;
|
||||
for (let option of question.answerOptions){
|
||||
answerTexts.push(option.answerText)
|
||||
if (option.isCorrect == "true"){
|
||||
correctAnswer.push(counter.toString());
|
||||
for (let rawBank of rawData){
|
||||
|
||||
for (let rawQuestion of rawBank.quizzes) {
|
||||
var questions:Question[] = [];
|
||||
for (let question of rawQuestion.quiz){
|
||||
var answerTexts = Array();
|
||||
var correctAnswer = [];
|
||||
var counter = 0;
|
||||
for (let option of question.answerOptions){
|
||||
answerTexts.push(option.answerText)
|
||||
if (option.isCorrect == "true"){
|
||||
correctAnswer.push(counter.toString());
|
||||
}
|
||||
counter = counter + 1;
|
||||
|
||||
}
|
||||
counter = counter + 1;
|
||||
|
||||
const questionToSave: Question = {
|
||||
id: "",
|
||||
name: question.questionText,
|
||||
description: question.questionText,
|
||||
lastModified: new Date(),
|
||||
options: answerTexts,
|
||||
answer: correctAnswer,
|
||||
textType: "text",
|
||||
questionType: "MCQ"
|
||||
}
|
||||
questions.push(questionToSave);
|
||||
}
|
||||
|
||||
const questionToSave: Question = {
|
||||
id: "",
|
||||
name: question.questionText,
|
||||
description: question.questionText,
|
||||
lastModified: new Date(),
|
||||
options: answerTexts,
|
||||
answer: correctAnswer,
|
||||
textType: "text",
|
||||
questionType: "MCQ"
|
||||
}
|
||||
questions.push(questionToSave);
|
||||
}
|
||||
|
||||
|
||||
var qb: ParsedQuestionBank = {
|
||||
questionBankTitle: rawQuestion.title,
|
||||
questions:questions
|
||||
};
|
||||
this.questionbanks.push(qb);
|
||||
|
||||
|
||||
}
|
||||
|
||||
var qb: ParsedQuestionBank = {
|
||||
questionBankTitle: rawQuestion.title,
|
||||
questions:questions
|
||||
};
|
||||
this.questionbanks.push(qb);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
catch (err){
|
||||
if (err instanceof Error){
|
||||
throw new Error(err.message);
|
||||
}
|
||||
else{
|
||||
throw new Error("Invalid JSON format");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -5,32 +5,40 @@ import { Question } from "../Question";
|
|||
// Currently only supports MCQs and TFs
|
||||
export class OriginalAppParser extends AssessmentAppParser{
|
||||
public parse() {
|
||||
const rawData = JSON.parse(this.raw);
|
||||
for (let rawBank of rawData) {
|
||||
var questions:Question[] = [];
|
||||
for (let rawQuestion of rawBank.questions) {
|
||||
try {
|
||||
const rawData = JSON.parse(this.raw);
|
||||
for (let rawBank of rawData) {
|
||||
var questions:Question[] = [];
|
||||
for (let rawQuestion of rawBank.questions) {
|
||||
|
||||
const question: Question = {
|
||||
id: "",
|
||||
name: rawQuestion.name,
|
||||
description: rawQuestion.description,
|
||||
lastModified: new Date(),
|
||||
options: rawQuestion.options,
|
||||
answer: rawQuestion.answer,
|
||||
textType: rawQuestion.textType,
|
||||
questionType: rawQuestion.questionType
|
||||
const question: Question = {
|
||||
id: "",
|
||||
name: rawQuestion.name,
|
||||
description: rawQuestion.description,
|
||||
lastModified: new Date(),
|
||||
options: rawQuestion.options,
|
||||
answer: rawQuestion.answer,
|
||||
textType: rawQuestion.textType,
|
||||
questionType: rawQuestion.questionType
|
||||
}
|
||||
questions.push(question);
|
||||
}
|
||||
questions.push(question);
|
||||
}
|
||||
|
||||
var qb: ParsedQuestionBank = {
|
||||
questionBankTitle: rawBank.name,
|
||||
questions:questions
|
||||
};
|
||||
this.questionbanks.push(qb);
|
||||
var qb: ParsedQuestionBank = {
|
||||
questionBankTitle: rawBank.name,
|
||||
questions:questions
|
||||
};
|
||||
this.questionbanks.push(qb);
|
||||
}
|
||||
}
|
||||
catch (err){
|
||||
if (err instanceof Error){
|
||||
throw new Error(err.message);
|
||||
}
|
||||
else{
|
||||
throw new Error("Invalid JSON format")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}}
|
||||
|
||||
|
|
|
@ -16,69 +16,80 @@ export class QTIParser extends AssessmentAppParser{
|
|||
var questions:Question[] = [];
|
||||
|
||||
const xmlParser: XMLParser = new XMLParser(this.options);
|
||||
var parsedInput = xmlParser.parse(this.raw);
|
||||
var assessment = parsedInput['questestinterop']['assessment'];
|
||||
questionBankTitle = assessment['@_title'];
|
||||
var questionsSection = assessment['section']['item']
|
||||
|
||||
for (let questionId in questionsSection){
|
||||
var currQuestion = questionsSection[questionId];
|
||||
// Get question title
|
||||
var questionTitle = currQuestion['@_title'];
|
||||
|
||||
// Get question type
|
||||
var qMetaDataField = currQuestion['itemmetadata']['qtimetadata']['qtimetadatafield'];
|
||||
var metaData = qMetaDataField[0]; // 0 position contains question type
|
||||
var questionType = 'NA';
|
||||
|
||||
// Get question description
|
||||
var questionText = currQuestion['presentation']['material']['mattext']['#text'];
|
||||
|
||||
// Get text type
|
||||
var cleanedTextType:string = 'text'
|
||||
if (currQuestion['presentation']['material']['mattext']['@_texttype'] === "text/html"){
|
||||
cleanedTextType = "html";
|
||||
try{
|
||||
var parsedInput = xmlParser.parse(this.raw);
|
||||
var assessment = parsedInput['questestinterop']['assessment'];
|
||||
questionBankTitle = assessment['@_title'];
|
||||
var questionsSection = assessment['section']['item']
|
||||
|
||||
for (let questionId in questionsSection){
|
||||
var currQuestion = questionsSection[questionId];
|
||||
// Get question title
|
||||
var questionTitle = currQuestion['@_title'];
|
||||
|
||||
// Get question type
|
||||
var qMetaDataField = currQuestion['itemmetadata']['qtimetadata']['qtimetadatafield'];
|
||||
var metaData = qMetaDataField[0]; // 0 position contains question type
|
||||
var questionType = 'NA';
|
||||
|
||||
// Get question description
|
||||
var questionText = currQuestion['presentation']['material']['mattext']['#text'];
|
||||
|
||||
// Get text type
|
||||
var cleanedTextType:string = 'text'
|
||||
if (currQuestion['presentation']['material']['mattext']['@_texttype'] === "text/html"){
|
||||
cleanedTextType = "html";
|
||||
}
|
||||
// Get answer and options
|
||||
|
||||
var result:questionTypeSpecificParse = {options:[], correctAnswer:[]}
|
||||
if (metaData['fieldentry'] === 'multiple_choice_question' ){
|
||||
result = this.parseMCQ(currQuestion);
|
||||
questionType = "MCQ";
|
||||
}
|
||||
else if (metaData['fieldentry'] === 'true_false_question'){
|
||||
result = this.parseMCQ(currQuestion);
|
||||
questionType = "TF";
|
||||
}
|
||||
else if (metaData['fieldentry'] === 'multiple_answers_question'){
|
||||
result= this.parseMAQ(currQuestion);
|
||||
questionType = "MCQ";
|
||||
}
|
||||
else if (metaData['fieldentry'] === 'numerical_question'){
|
||||
result= this.parseQA(currQuestion);
|
||||
questionType = "QA"
|
||||
}
|
||||
else{
|
||||
continue;
|
||||
}
|
||||
|
||||
const question:Question = {
|
||||
id: "",
|
||||
name: questionTitle,
|
||||
description: questionText,
|
||||
lastModified: new Date(),
|
||||
options: result.options,
|
||||
answer: result.correctAnswer,
|
||||
textType: cleanedTextType,
|
||||
questionType: questionType
|
||||
}
|
||||
questions.push(question);
|
||||
}
|
||||
// Get answer and options
|
||||
var qb: ParsedQuestionBank = {
|
||||
questionBankTitle: questionBankTitle,
|
||||
questions:questions
|
||||
};
|
||||
this.questionbanks.push(qb);
|
||||
|
||||
var result:questionTypeSpecificParse = {options:[], correctAnswer:[]}
|
||||
if (metaData['fieldentry'] === 'multiple_choice_question' ){
|
||||
result = this.parseMCQ(currQuestion);
|
||||
questionType = "MCQ";
|
||||
}
|
||||
else if (metaData['fieldentry'] === 'true_false_question'){
|
||||
result = this.parseMCQ(currQuestion);
|
||||
questionType = "TF";
|
||||
}
|
||||
else if (metaData['fieldentry'] === 'multiple_answers_question'){
|
||||
result= this.parseMAQ(currQuestion);
|
||||
questionType = "MCQ";
|
||||
}
|
||||
else if (metaData['fieldentry'] === 'numerical_question'){
|
||||
result= this.parseQA(currQuestion);
|
||||
questionType = "QA"
|
||||
}
|
||||
catch (err){
|
||||
if (err instanceof Error){
|
||||
throw new Error(err.message);
|
||||
}
|
||||
else{
|
||||
continue;
|
||||
throw new Error("File was in invalid QTI format");
|
||||
}
|
||||
|
||||
const question:Question = {
|
||||
id: "",
|
||||
name: questionTitle,
|
||||
description: questionText,
|
||||
lastModified: new Date(),
|
||||
options: result.options,
|
||||
answer: result.correctAnswer,
|
||||
textType: cleanedTextType,
|
||||
questionType: questionType
|
||||
}
|
||||
questions.push(question);
|
||||
}
|
||||
var qb: ParsedQuestionBank = {
|
||||
questionBankTitle: questionBankTitle,
|
||||
questions:questions
|
||||
};
|
||||
this.questionbanks.push(qb);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче