Unverified Commit ed8cd71c authored by Bharat Kashyap's avatar Bharat Kashyap Committed by GitHub
Browse files

Merge pull request #9 from navadhiti/feat/question-paper-csv-docx

Feat/question paper csv docx
Showing with 297 additions and 376 deletions
+297 -376
var express = require("express");
const { buildPDFWithCallback } = require("../service/print/pdf");
<<<<<<< HEAD
const { buildCSVWithCallback } = require("../service/print/csv");
const { buildCSVReportWithCallback } = require('../service/print/csvreport');
=======
const { buildDOCXWithCallback } = require('../service/print/docx')
>>>>>>> origin/release-4.2.0-HC
const requestMiddleware = require("../middlewares/request.middleware");
// const base64 = require('base64topdf');
const BASE_URL = "/program/v1";
......@@ -13,10 +10,8 @@ const BASE_URL = "/program/v1";
async function printDocx(req,res){
const id = req.query.id;
const format = req.query.format;
<<<<<<< HEAD
=======
// buildDOCXwithCallback(function (binary, error, errorMsg) {
buildDOCXWithCallback(id,function (binary, error, errorMsg) {
buildDOCXWithCallback(id,function (binary, error, errorMsg,filename) {
// console.log("Enttere dres")
var date = new Date();
if (!error) {
......@@ -41,13 +36,13 @@ async function printDocx(req,res){
res.send(resJSON);
} else {
res.setHeader('Content-Disposition', 'attachment; filename=MyDocument.docx');
res.setHeader('Content-Disposition', `attachment; filename=${filename}.docx`);
res.send(Buffer.from(binary, 'base64'));
}
} else {
res.status(404).send({
error: errorMsg,
});
});
}
});
}
......@@ -55,7 +50,6 @@ async function printDocx(req,res){
async function printPDF(req, res) {
const id = req.query.id;
const format = req.query.format;
>>>>>>> origin/release-4.2.0-HC
buildPDFWithCallback(id, function (binary, error, errorMsg) {
var date = new Date();
if (!error) {
......@@ -170,27 +164,29 @@ module.exports = function (app) {
.get(
requestMiddleware.gzipCompression(),
requestMiddleware.createAndValidateRequestBody,
// printDocx
printPDF
);
app
.route(BASE_URL + "/print/docx")
.route(BASE_URL + "/print/csv")
.get(
requestMiddleware.gzipCompression(),
requestMiddleware.createAndValidateRequestBody,
printDocx
printCSV
);
app
.route(BASE_URL + "/print/csv")
.route(BASE_URL + "/print/report/aggregate")
.get(
requestMiddleware.gzipCompression(),
requestMiddleware.createAndValidateRequestBody,
printCSV
);
app
.route(BASE_URL + "/print/report/aggregate")
.route(BASE_URL + "/print/docx")
.get(
requestMiddleware.gzipCompression(),
requestMiddleware.createAndValidateRequestBody,
printCSV
printDocx
// printPDF
);
};
};
\ No newline at end of file
const { getData } = require("./dataImporter");
const docx = require("docx");
const { Document, Packer, Paragraph, TextRun, AlignmentType, ImageRun ,Table, TableRow, TableCell, WidthType} = docx;
const {
Document,
Packer,
Paragraph,
TextRun,
AlignmentType,
ImageRun,
Table,
TableRow,
TableCell,
WidthType,
} = docx;
// const getDocx = require("./getDocx");
const getDocx = require("./getdocxdata");
const fs = require("fs");
......@@ -101,9 +112,11 @@ const buildDOCXWithCallback = async (id, callback) => {
];
const questionPaperContent = [];
const paperDetails = {"examName" : examName,
"className" : grade,
"subject" : subject}
const paperDetails = {
examName: examName,
className: grade,
subject: subject,
};
let questionCounter = 0;
for (const d of data.sectionData) {
......@@ -128,12 +141,7 @@ const buildDOCXWithCallback = async (id, callback) => {
break;
case "FTB":
questionContent = [
await renderQuestion(
question,
questionCounter,
marks,
'FTB'
),
await renderQuestion(question, questionCounter, marks, "FTB"),
];
break;
case "SA":
......@@ -148,25 +156,25 @@ const buildDOCXWithCallback = async (id, callback) => {
break;
case "VSA":
questionContent = [
await renderQuestion(
question,
questionCounter,
marks,
"VSA"
),
await renderQuestion(question, questionCounter, marks, "VSA"),
];
break;
// case "MTF":
// questionContent = await renderMTF(
// question,
// questionCounter,
// marks,
// "MTF"
// );
// break;
case "MTF":
questionContent = await renderMTF(
question,
questionCounter,
marks,
"MTF"
);
break;
case "COMPREHENSION":
questionContent = [
await renderComprehension(question, questionCounter, marks,"COMPREHENSION"),
await renderComprehension(
question,
questionCounter,
marks,
"COMPREHENSION"
),
];
break;
case "CuriosityQuestion":
......@@ -184,12 +192,14 @@ const buildDOCXWithCallback = async (id, callback) => {
questionPaperContent.push(questionContent);
}
}
// console.log("Contents:",questionPaperContent[1])
const doc = await getDocx.create(questionPaperContent,paperDetails);
const doc = await getDocx.create(questionPaperContent, paperDetails);
const b64string = await Packer.toBase64String(doc);
callback(b64string, null, null);
let filename = grade + "_" + subject + "_" + examName;
filename = filename.replace(/\s/g, "");
callback(b64string, null, null, filename);
}
})
.catch((e) => {
......@@ -260,7 +270,7 @@ function createImageElement(src, width) {
imageElement.image = src;
let img = Buffer.from(src.split(";base64,").pop(), "base64");
let dimensions = sizeOf(img);
console.log("Dimensions", dimensions)
console.log("Dimensions", dimensions);
let resizedWidth = dimensions.width * width;
let resizedHeight = dimensions.height * width;
imageElement.width = resizedWidth > 150 ? 150 : resizedWidth;
......@@ -335,7 +345,6 @@ async function getStack(htmlString, questionCounter) {
if (style) {
width = parseFloat(style.split(":").pop().slice(0, -2));
width = width / 100;
}
if (elem.children && elem.children.length) {
let { src } = elem.children[0].attribs;
......@@ -391,59 +400,79 @@ async function renderMCQ(question, questionCounter, marks) {
: [`${questionCounter}. ${cleanHTML(q)}`];
let questionOpt = [];
let imageProperties = [];
if (typeof questionOptions[0][1] === "object") {
// console.log("Que:",questionOptions[0][0])
questionOpt.push(
questionOptions[0][0] + (questionOptions[0][1].image)
);
// console.log("Que:",questionOptions)
questionOpt.push(questionOptions[0][0] + questionOptions[0][1].image);
imageProperties.push({
width: questionOptions[0][1].width,
height: questionOptions[0][1].height,
});
} else {
questionOpt.push(questionOptions[0][0]);
imageProperties.push({
width: 0,
height: 0,
});
}
if (typeof questionOptions[1][1] === "object") {
questionOpt.push(
questionOptions[1][0] + (questionOptions[1][1].image)
);
questionOpt.push(questionOptions[1][0] + questionOptions[1][1].image);
imageProperties.push({
width: questionOptions[1][1].width,
height: questionOptions[1][1].height,
});
} else {
questionOpt.push(questionOptions[1][0]);
imageProperties.push({
width: 0,
height: 0,
});
}
if (typeof questionOptions[2][1] === "object") {
questionOpt.push(
questionOptions[2][0] + (questionOptions[2][1].image)
);
questionOpt.push(questionOptions[2][0] + questionOptions[2][1].image);
imageProperties.push({
width: questionOptions[2][1].width,
height: questionOptions[2][1].height,
});
} else {
questionOpt.push(questionOptions[2][0]);
imageProperties.push({
width: 0,
height: 0,
});
}
if (typeof questionOptions[3][1] === "object") {
questionOpt.push(
questionOptions[3][0] + (questionOptions[3][1].image)
);
questionOpt.push(questionOptions[3][0] + questionOptions[3][1].image);
imageProperties.push({
width: questionOptions[3][1].width,
height: questionOptions[3][1].height,
});
} else {
questionOpt.push(questionOptions[3][0]);
imageProperties.push({
width: 0,
height: 0,
});
}
let data = {
Questions: questionTitle,
Option1: questionOpt,
Option2: questionOpt,
Option3: questionOpt,
Option4: questionOpt,
Option1: questionOpt[0],
Option2: questionOpt[1],
Option3: questionOpt[2],
Option4: questionOpt[3],
Marks: marks,
Language: detectLanguage(questionTitle[0]),
type:'MCQ'
type: "MCQ",
height: imageProperties[0].height,
width: imageProperties[0].width,
};
return data;
// return getMCQ(
// questionTitle,
// questionOptions,
// detectLanguage(questionTitle[0]),
// marks
// );
}
async function renderQuestion(question, questionCounter, marks, Type) {
let data;
if (
......@@ -459,18 +488,17 @@ async function renderQuestion(question, questionCounter, marks, Type) {
} else {
data = [`${questionCounter}. ${cleanHTML(question.editorState.question)}`];
}
// let question;
// console.log("data:",typeof data[0],data)
let quedata = {
Questions: data,
Marks: marks,
type: Type
type: Type,
};
// return callback(data, detectLanguage(data[0]), marks);
return quedata;
return quedata;
}
async function renderComprehension(question, questionCounter, marks,Type) {
async function renderComprehension(question, questionCounter, marks, Type) {
let data;
if (
(question.media && question.media.length) ||
......@@ -480,7 +508,7 @@ async function renderComprehension(question, questionCounter, marks,Type) {
question.editorState.question.search("ol") >= 0 ||
question.editorState.question.search("ul") >= 0 ||
question.editorState.question.match(/<p>/g).length > 1
) {
) {
data = await getStack(question.editorState.question, questionCounter);
} else {
data = [`${questionCounter}. ${cleanHTML(question.editorState.question)}`];
......@@ -488,9 +516,9 @@ async function renderComprehension(question, questionCounter, marks,Type) {
let quedata = {
Questions: data,
Marks: marks,
type: Type
type: Type,
};
return quedata
return quedata;
// return getComprehension(data, detectLanguage(data[0]), marks);
}
......@@ -500,7 +528,7 @@ function renderTF(question, questionCounter, marks) {
return getTF(questionTitle, detectLanguage(questionTitle[0]), marks);
}
async function renderMTF(question, questionCounter, marks,Type) {
async function renderMTF(question, questionCounter, marks, Type) {
$ = cheerio.load(question.editorState.question);
cheerioTableparser($);
var data = [];
......@@ -510,8 +538,8 @@ async function renderMTF(question, questionCounter, marks,Type) {
);
const heading = questionCounter + ". " + cleanHTML($("p").first().text());
console.log("MTF:",heading)
data.push(getFTB([heading], detectLanguage(heading), marks));
console.log("MTF:", heading);
data.push(heading, detectLanguage(heading), marks);
// console.log("MTF:",transposeColumns)
data.push(
getMTFHeader(
......@@ -532,208 +560,20 @@ async function renderMTF(question, questionCounter, marks,Type) {
if (r[1].search("img") >= 0) {
right = await getStack(r[1]);
} else right = [cleanHTML(r[1])];
rows.push(left,right)
}
return data.concat(rows);
}
async function createDoc(docData) {
let data = docData
let queNum = 0;
// console.log("Doc data:",data)
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: "Mathematics",
bold: true,
}),
],
}),
new Paragraph({
children: [], // Just newline without text
}),
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: "FUn",
bold: true,
}),
],
}),
...data
.map((question) => {
const arr = [];
if(question !== undefined)
arr.push(createContactInfo(question));
return arr;
})
.reduce((prev, curr) => prev.concat(curr), []),
],
},
],
});
// console.log("final doc:", doc)
return doc;
}
async function createContactInfo(data) {
console.log("Info", data)
return new Paragraph({
alignment: AlignmentType.LEFT,
children: [
new TextRun(`${data[0].Questions}`),
new Paragraph({
children: [], // Just newline without text
}),
// await createOptions(data[0])
],
});
}
async function imageData(image) {
let bufferImage;
if (image.includes("data:image/png;base64,")) {
return bufferImage = image.replace("data:image/png;base64,", "");
} else if (image.includes("data:image/jpg;base64")) {
return bufferImage = image.replace("data:image/jpg;base64,", "");
} else if (image.includes("data:image/jpeg;base64")) {
return bufferImage = image.replace("data:image/jpeg;base64,", "");
}
}
async function getBufferData(data){
let image = await imageData(data)
console.log("options", typeof image.substr(2))
return image.substr(2)
}
async function formatOptions(data){
// console.log("Format:",data)
let optionArr = []
let image;
let testimage = data
if(testimage){
if (testimage.Option1.includes("data:image/")) {
// image = await imageData(testimage.Option1)
optionArr.push(testimage.Option1)
}else{
optionArr.push(testimage.Option1)
}
if (testimage.Option2.includes("data:image/")) {
// image = await imageData(testimage.Option2)
optionArr.push(testimage.Option2)
}else{
optionArr.push(testimage.Option2)
}
if (testimage.Option3.includes("data:image/")) {
// image = await imageData(testimage.Option3)
optionArr.push(testimage.Option3)
}else{
optionArr.push(testimage.Option3)
}
if (testimage.Option4.includes("data:image/")) {
// image = await imageData(testimage.Option4)
optionArr.push(testimage.Option4)
}else{
optionArr.push(testimage.Option4)
}
}
return optionArr
}
async function displayOptions(option){
if (option.includes("data:image/")){
let image = await getBufferData(option)
return new Paragraph({
text:option.substr(0,1),
children: [
new ImageRun({
data: image ,
transformation: {
width: 150,
height: 150,
},
})
],
})
} else {
return new Paragraph({
alignment: AlignmentType.LEFT,
children: [
new TextRun({
text: option,
}),
],
})
}
rows.push({ left, right });
}
async function createOptions(data) {
let testimage = await formatOptions(data)
return new Table({
columnWidths: [4505, 4505],
rows: [
new TableRow({
children: [
new TableCell({
width: {
size: 4505,
type: WidthType.DXA,
},
children: [
displayOptions(testimage[0])
],
}),
new TableCell({
width: {
size: 4505,
type: WidthType.DXA,
},
children: [
displayOptions(testimage[1])
],
}),
],
}),
new TableRow({
children: [
new TableCell({
width: {
size: 4505,
type: WidthType.DXA,
},
children: [
displayOptions(testimage[1])
],
}),
new TableCell({
width: {
size: 4505,
type: WidthType.DXA,
},
children: [
displayOptions(testimage[3])
],
}),
],
}),
],
});
}
// return data.concat(rows);
let mtfData = [
{
Questions: rows,
Marks: marks,
type: Type,
heading: heading,
},
];
return mtfData;
}
module.exports = {
buildDOCXWithCallback,
};
\ No newline at end of file
};
......@@ -98,14 +98,14 @@ function create(data, paperData) {
arr.push(Marks(question));
question[0].Questions.map((item) => {
if (item.ol) {
let count = 0;
let count1 = 0;
item.ol.map((text) => {
count++;
count1++;
if (typeof text === "object") {
text = count + text.text;
text = count1 + text.text;
arr.push(
new Paragraph({
text: `${count}.${text}`,
text: `${count1}.${text}`,
})
);
} else {
......@@ -113,7 +113,7 @@ function create(data, paperData) {
new Paragraph({
children: [
new TextRun({
text: `${count}.${text}`,
text: `${count1}.${text}`,
}),
],
})
......@@ -121,7 +121,7 @@ function create(data, paperData) {
}
});
}
arr.push(createCOMPREHENSIONObject(item, count++));
arr.push(createCOMPREHENSIONObject(item, count++));
});
arr.push(
......@@ -133,7 +133,7 @@ function create(data, paperData) {
// console.log("CuriosityQuestion Dta:", question[0].Questions);
} else if (question[0].type === "SA") {
let count = 0;
arr.push(Marks(question));
arr.push(Marks(question));
question[0].Questions.map((item) => {
arr.push(createSAObject(item, count++));
});
......@@ -145,7 +145,7 @@ function create(data, paperData) {
);
} else if (question[0].type === "LA") {
// console.log("LA Dta:", page);
arr.push(Marks(question))
arr.push(Marks(question));
question[0].Questions.map((item) => {
arr.push(createSAObject(item));
});
......@@ -160,7 +160,7 @@ function create(data, paperData) {
arr.push(Marks(question));
question[0].Questions.map((item) => {
// console.log(item)
arr.push(createSAObject(item, count++));
});
arr.push(
......@@ -183,22 +183,38 @@ function create(data, paperData) {
children: [], // Just newline without text
})
);
}
// else if (question[0].type === "MCQ") {
// let testimage = formatOptions(question[0]);
// arr.push(Marks(question));
// arr.push(createMCQdata(question));
// arr.push(optionsTabel(testimage));
// arr.push(
// new Paragraph({
// children: [], // Just newline without text
// })
// );
// }
}
} else if (question[0].type === "MCQ") {
let testimage = formatOptions(question[0]);
arr.push(Marks(question));
let count = 0;
question[0].Questions.map((item) => {
arr.push(createSAObject(item, count));
});
arr.push(optionsTabel(testimage));
arr.push(
new Paragraph({
children: [], // Just newline without text
})
);
} else if (question[0].type === "MTF") {
arr.push(Marks(question));
let count = 0;
arr.push(createSAObject(question[0].heading, count));
arr.push(
new Paragraph({
children: [], // Just newline without text
})
);
arr.push(MTFTabel(question));
arr.push(
new Paragraph({
children: [], // Just newline without text
})
);
}
}
return arr;
})
.reduce((prev, curr) => prev.concat(curr), []),
......@@ -208,7 +224,70 @@ function create(data, paperData) {
});
return doc;
}
function MTFTabel(question) {
const arr = [];
const rowheading = new TableRow({
children: [
new TableCell({
borders: MTFborder,
width: {
size: 5505,
type: WidthType.DXA,
},
children: [
new Paragraph({
text: "Column1",
bold: true,
}),
],
}),
new TableCell({
borders: MTFborder,
width: {
size: 5505,
type: WidthType.DXA,
},
children: [
new Paragraph({
text: "Column2",
bold: true,
}),
],
}),
],
});
arr.push(rowheading);
question[0].Questions.map((item) => {
arr.push(
new TableRow({
children: [
new TableCell({
borders: MTFborder,
width: {
size: 5505,
type: WidthType.DXA,
},
children: [createSAObject(item.left[0], 0)],
}),
new TableCell({
borders: MTFborder,
width: {
size: 5505,
type: WidthType.DXA,
},
children: [createSAObject(item.right[0], 0)],
}),
],
})
);
});
// console.log("Arr:",arr)
//
return new Table({
columnWidths: [5505, 5505],
rows: arr,
});
}
function Marks(data) {
if (data[0].Marks !== undefined) {
return new Paragraph({
......@@ -227,15 +306,7 @@ function Marks(data) {
});
}
}
function createMCQdata(data) {
console.log("Data:", data[0].Questions[0]);
return new Paragraph({
alignment: AlignmentType.LEFT,
children: [
new TextRun({text : `${data[0].Questions[0]}`}),
],
});
}
function createFTBObject(data) {
const arr = [];
......@@ -259,7 +330,7 @@ function createFTBObject(data) {
}
function createFTB(data, count) {
// console.log("Data:",data, count)
// console.log("Data:", count)
if (count !== 0) {
return new Paragraph({
alignment: AlignmentType.LEFT,
......@@ -282,48 +353,6 @@ function createFTB(data, count) {
});
}
}
function createMCWObject(data, count) {
// console.log("Item:", data)
const arr = [];
if (data.text) {
data.text
.map((text) => {
if (typeof text === "object") {
arr.push(new TextRun(text));
} else {
arr.push(
new TextRun({
text: `${text}`,
})
);
}
// return arr
})
.reduce((prev, curr) => prev.concat(curr), []);
return new Paragraph({
alignment: AlignmentType.LEFT,
children: arr,
});
} else if (data.image) {
if (data.image.includes("data:image/")) {
let image = getBufferImg(data.image);
return new Paragraph({
children: [
new ImageRun({
data: image,
transformation: {
width: data.width,
height: data.height,
},
}),
],
});
}
} else {
return createFTB(data, count);
}
}
function createSAObject(data, count) {
// console.log("Item:", data)
......@@ -340,7 +369,6 @@ function createSAObject(data, count) {
})
);
}
// return arr
})
.reduce((prev, curr) => prev.concat(curr), []);
return new Paragraph({
......@@ -405,14 +433,41 @@ function createCOMPREHENSIONObject(data, count) {
],
});
}
}
else {
} else if (data.ol) {
// let count1 = 0;
// data.ol
// .map((text) => {
// count1++;
// if (typeof text === "object") {
// text = count1 + text.text;
// arr.push(
// new Paragraph({
// text: `${count1}.${text}`,
// })
// );
// } else {
// arr.push(
// new Paragraph({
// // children: [
// // new TextRun({
// text: `${count1}.${text}`,
// // }),
// // ],
// })
// );
// }
// })
// .reduce((prev, curr) => prev.concat(curr), []);
// console.log("ol",arr)
// return new Paragraph({
// alignment: AlignmentType.LEFT,
// children: arr,
// });
} else {
return createFTB(data, count);
}
}
function imageData(image) {
let bufferImage;
if (image.includes("data:image/png;base64,")) {
......@@ -432,6 +487,7 @@ function getBufferImg(data) {
return image;
}
function formatOptions(data) {
// console.log("Options:",data)
let optionArr = [];
let image;
let testimage = data;
......@@ -456,24 +512,25 @@ function formatOptions(data) {
} else {
optionArr.push(testimage.Option4);
}
optionArr.push(testimage.height);
optionArr.push(testimage.width);
}
// console.log("options :",optionArr)
return optionArr;
}
function displayOptions(option) {
function displayOptions(option, height, width) {
// console.log("image", option);
if (option.includes("data:image/")) {
let image = getBufferData(option);
// console.log("image", image);
return new Paragraph({
text: option.substr(0, 1),
children: [
new ImageRun({
data: image,
transformation: {
width: 150,
height: 150,
width: width,
height: height,
},
}),
],
......@@ -509,6 +566,25 @@ const border = {
size: 0,
},
};
const MTFborder = {
left: {
style: BorderStyle.SINGLE,
size: 2,
},
right: {
style: BorderStyle.SINGLE,
size: 2,
},
top: {
style: BorderStyle.SINGLE,
size: 2,
},
bottom: {
style: BorderStyle.SINGLE,
size: 2,
},
};
function headers() {
return new Table({
columnWidths: [6505, 6505],
......@@ -596,6 +672,7 @@ function headers() {
}
//
function optionsTabel(testimage) {
// console.log(testimage)
return new Table({
columnWidths: [5505, 5505],
rows: [
......@@ -607,7 +684,9 @@ function optionsTabel(testimage) {
size: 5505,
type: WidthType.DXA,
},
children: [displayOptions(testimage[0])],
children: [
displayOptions(testimage[0], testimage[4], testimage[5]),
],
}),
new TableCell({
borders: border,
......@@ -615,7 +694,9 @@ function optionsTabel(testimage) {
size: 5505,
type: WidthType.DXA,
},
children: [displayOptions(testimage[1])],
children: [
displayOptions(testimage[1], testimage[4], testimage[5]),
],
}),
],
}),
......@@ -627,7 +708,9 @@ function optionsTabel(testimage) {
size: 5505,
type: WidthType.DXA,
},
children: [displayOptions(testimage[2])],
children: [
displayOptions(testimage[2], testimage[4], testimage[5]),
],
}),
new TableCell({
borders: border,
......@@ -635,7 +718,9 @@ function optionsTabel(testimage) {
size: 5505,
type: WidthType.DXA,
},
children: [displayOptions(testimage[3])],
children: [
displayOptions(testimage[3], testimage[4], testimage[5]),
],
}),
],
}),
......@@ -646,4 +731,4 @@ function optionsTabel(testimage) {
module.exports = {
create,
// buildDOCXwithCallback,
};
\ No newline at end of file
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment