Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #CO-229 #CO-230 #CO-316 fix: {Reraising PR on release-6.0.0} #303

Merged
merged 7 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/routes/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ async function printDocx(req, res) {
const id = req.query.id;
const format = req.query.format;
const version = req.query.version;
const config={
id : req.query.id,
allQuestions: req.query.allQuestions || false
}

if (version === "1.0") {
buildDOCX_1_WithCallback(id, function (binary, error, errorMsg, filename) {
buildDOCX_1_WithCallback(config, function (binary, error, errorMsg, filename) {
const id = config.id;
var date = new Date();
if (!error) {
if (format === "json") {
Expand Down Expand Up @@ -53,7 +58,8 @@ async function printDocx(req, res) {
}
});
} else {
buildDOCXWithCallback(id, function (binary, error, errorMsg, filename) {
buildDOCXWithCallback(config, function (binary, error, errorMsg, filename) {
const id = config.id
var date = new Date();
if (!error) {
if (format === "json") {
Expand Down
6 changes: 4 additions & 2 deletions src/service/print/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const buildCSVWithCallback = async (id, callback) => {
let error = false;
let errorMsg = "";
let totalMarks = 0;
getQuestionSet(id)
const config = {
id: id
}
getQuestionSet(config)
.then(async (data) => {
if (data.error) {
callback(null, data.error, data.errorMsg, null);
Expand Down Expand Up @@ -395,7 +398,6 @@ async function renderMCQ(
questionOptions.forEach(( quesOpt , i)=>{
data[`Option${i+1}`] = quesOpt[0]
})

return data;
}

Expand Down
152 changes: 45 additions & 107 deletions src/service/print/docxHelper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
var cheerio = require("cheerio");
var cheerioTableparser = require("cheerio-tableparser");
const sizeOf = require("image-size");
const { compact } = require("lodash");
const ProgramServiceHelper = require("../../helpers/programHelper");
const programServiceHelper = new ProgramServiceHelper();
const optionLabelling = {
english: ['i', 'ii', 'iii', 'iv'],
hindi: ['क', 'ख', 'ग', 'घ'],
}
const defaultLanguage = 'english'

var {
docDefinition,
Expand Down Expand Up @@ -237,6 +241,7 @@ async function getStack(htmlString) {
}

async function renderMCQ(question, questionCounter, marks) {
const printLanguage = (question.medium && question.medium.length) ? question.medium[0].toLowerCase() : defaultLanguage;
const questionOptions = [];
let questionTitle;
for (const [index, qo] of question.editorState.options.entries()) {
Expand Down Expand Up @@ -264,134 +269,60 @@ async function renderMCQ(question, questionCounter, marks) {

let questionOpt = [];
let imageProperties = [];
if (questionOptions[0] !== undefined) {
if (
questionOptions[0][0] !== undefined &&
typeof questionOptions[0][0] === "object"
) {
if (questionOptions[0][0].text) {
questionOpt.push(["I.", questionOptions[0][0].text[1]]);
imageProperties.push({
width: 0,
height: 0,
});
} else {
questionOpt.push(["I.", questionOptions[0][0].image]);
imageProperties.push({
width: questionOptions[0][0].width,
height: questionOptions[0][0].height,
});
}
} else {
questionOpt.push(["I.", questionOptions[0][0]]);
imageProperties.push({
width: 0,
height: 0,
});
}
}

if (questionOptions[1] !== undefined) {
if (
questionOptions[1][0] !== undefined &&
typeof questionOptions[1][0] === "object"
) {
if (questionOptions[1][0].text) {
questionOpt.push(["II.", questionOptions[1][0].text[1]]);
imageProperties.push({
width: 0,
height: 0,
});
} else {
questionOpt.push(["II.", questionOptions[1][0].image]);
imageProperties.push({
width: questionOptions[1][0].width,
height: questionOptions[1][0].height,
});
}
} else {
questionOpt.push(["II.", questionOptions[1][0]]);
imageProperties.push({
width: 0,
height: 0,
});
}
for (let i = 0; i < questionOptions.length; i++) {
setOptions(questionOptions, questionOpt, imageProperties, printLanguage, i)
}

if (questionOptions[2] !== undefined) {
if (
questionOptions[2][0] !== undefined &&
typeof questionOptions[2][0] === "object"
) {
if (questionOptions[2][0].text) {
questionOpt.push(["III.", questionOptions[2][0].text[1]]);
imageProperties.push({
width: 0,
height: 0,
});
} else {
questionOpt.push(["III.", questionOptions[2][0].image]);
imageProperties.push({
width: questionOptions[2][0].width,
height: questionOptions[2][0].height,
});
}
} else {
questionOpt.push(["III.", questionOptions[2][0]]);
imageProperties.push({
width: 0,
height: 0,
});
}
let data = {
QuestionIndex: questionCounter,
Questions: questionTitle,
Marks: marks,
Language: detectLanguage(questionTitle[0]),
type: "MCQ",
height1: imageProperties[0] ? imageProperties[0].height : undefined,
width1: imageProperties[0] ? imageProperties[0].width : undefined,
height2: imageProperties[1] ? imageProperties[1].height : undefined,
width2: imageProperties[1] ? imageProperties[1].width : undefined,
height3: imageProperties[2] ? imageProperties[2].height : undefined,
width3: imageProperties[2] ? imageProperties[2].width : undefined,
height4: imageProperties[3] ? imageProperties[3].height : undefined,
width4: imageProperties[3] ? imageProperties[3].width : undefined,
};

for (let index = 0; index < questionOptions.length; index++) {
data[`Option${index + 1}`] = questionOpt[index];
}
return data;
}

if (questionOptions[3] !== undefined) {
function setOptions(questionOptions, questionOpt, imageProperties, printLanguage, i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope you enjoyed writing the code.. looks fine.

@vaivk369 @shubhambansaltarento

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes i did, Thank you for guidance.

if (questionOptions[i] !== undefined) {
if (
questionOptions[3][0] !== undefined &&
typeof questionOptions[3][0] === "object"
questionOptions[i][0] !== undefined &&
typeof questionOptions[i][0] === "object"
) {
if (questionOptions[3][0].text) {
questionOpt.push(["IV.", questionOptions[3][0].text[1]]);
if (questionOptions[i][0].text) {
questionOpt.push([`${getLanguageKey(printLanguage, i)+")"}`, questionOptions[i][0].text[1]]);
imageProperties.push({
width: 0,
height: 0,
});
} else {
questionOpt.push(["IV.", questionOptions[3][0].image]);
questionOpt.push([`${getLanguageKey(printLanguage, i)+")"}`, questionOptions[i][0].image]);
imageProperties.push({
width: questionOptions[3][0].width,
height: questionOptions[3][0].height,
width: questionOptions[i][0].width,
height: questionOptions[i][0].height,
});
}
} else {
questionOpt.push(["IV.", questionOptions[3][0]]);
questionOpt.push([`${getLanguageKey(printLanguage, i)+")"}`, questionOptions[i][0]]);
imageProperties.push({
width: 0,
height: 0,
});
}
}
let data = {
QuestionIndex: questionCounter,
Questions: questionTitle,
Option1: questionOpt[0],
Option2: questionOpt[1],
Option3: questionOpt[2],
Option4: questionOpt[3],
Marks: marks,
Language: detectLanguage(questionTitle[0]),
type: "MCQ",
height1: imageProperties[0] ? imageProperties[0].height : undefined,
width1: imageProperties[0] ? imageProperties[0].width : undefined,
height2: imageProperties[1] ? imageProperties[1].height : undefined,
width2: imageProperties[1] ? imageProperties[1].width : undefined,
height3: imageProperties[2] ? imageProperties[2].height : undefined,
width3: imageProperties[2] ? imageProperties[2].width : undefined,
height4: imageProperties[3] ? imageProperties[3].height : undefined,
width4: imageProperties[3] ? imageProperties[3].width : undefined,
};

return data;
}

async function renderQuestion(question, questionCounter, marks, Type) {
Expand Down Expand Up @@ -498,6 +429,13 @@ async function renderMTF(question, questionCounter, marks, Type) {

return mtfData;
}
function getLanguageKey(lang, index) {
if (optionLabelling.hasOwnProperty(lang) && optionLabelling[lang].length > index) {
return optionLabelling[lang][index];
}

return optionLabelling[defaultLanguage][index];
}

module.exports = {
renderComprehension,
Expand Down
47 changes: 30 additions & 17 deletions src/service/print/getdocxdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ const {
convertInchesToTwip,
} = docx;
const _ = require("lodash");
const basicDetails={
english : require('./lang/english.json'),
hindi : require('./lang/hindi.json')
}

function create(data, paperData) {
const defaultLanguage = 'english';
const language = (paperData && paperData.language) ? paperData.language.toLowerCase(): defaultLanguage;

const doc = new Document({
sections: [
{
properties: {},
children: [
headers(
"Student Name:..............................................................",
"Roll Number:............................."
getLanguageKey(language, 'studentName') + ":..................................................",
getLanguageKey(language, 'rollNo') + ":............................."
),
new Paragraph({
children: [], // Just newline without text
Expand All @@ -49,7 +56,7 @@ function create(data, paperData) {
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: `Grade: ${paperData.className}`,
text: getLanguageKey(language, 'class')+ `:${paperData.className}`,
bold: true,
}),
],
Expand All @@ -61,15 +68,15 @@ function create(data, paperData) {
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: `Subject: ${paperData.subject}`,
text: getLanguageKey(language, 'subject')+ `:${paperData.subject}`,
bold: true,
}),
],
}),
new Paragraph({
children: [], // Just newline without text
}),
headers("Time:............", "Marks:............"),
headers(getLanguageKey(language, 'time')+":" + `${paperData.maxTime}`, getLanguageKey(language, 'marks')+":"+`${paperData.maxScore}`),
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
Expand All @@ -81,7 +88,7 @@ function create(data, paperData) {
],
}),

instructionHead(paperData.instructions),
instructionHead(paperData.instructions, language),
instructions(paperData.instructions),
new Paragraph({
alignment: AlignmentType.CENTER,
Expand Down Expand Up @@ -226,9 +233,9 @@ function create(data, paperData) {
count++;
});
arr.push(
new Paragraph({
children: [], // Just newline without text
})
// new Paragraph({
// children: [], // Just newline without text
// })
);
arr.push(optionsTabel(testimage));
arr.push(
Expand Down Expand Up @@ -287,13 +294,13 @@ function create(data, paperData) {
return doc;
}

function instructionHead(data) {
function instructionHead(data, language) {
const arr = [];

if (!_.isUndefined(data)) {
arr.push(
new TextRun({
text: `Instructions:`,
text: (getLanguageKey(language, 'instructionHead')+":"),
bold: true,
})
);
Expand Down Expand Up @@ -691,9 +698,9 @@ function displayNumber(data) {
type: WidthType.DXA,
},
margins: {
top: convertInchesToTwip(0.0693701),
bottom: convertInchesToTwip(0.0693701),
left: convertInchesToTwip(0.3493701),
top: convertInchesToTwip(0.0093701),
bottom: convertInchesToTwip(0.0093701),
left: convertInchesToTwip(0.0693701),
right: convertInchesToTwip(0.0693701),
},
verticalAlign: VerticalAlign.CENTER,
Expand All @@ -711,9 +718,9 @@ function displayNumber(data) {
type: WidthType.DXA,
},
margins: {
top: convertInchesToTwip(0.0693701),
bottom: convertInchesToTwip(0.0693701),
left: convertInchesToTwip(0.3493701),
top: convertInchesToTwip(0.0093701),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the right way of fixing the margins. It should be common across all sides. Any specific reason why the margins are different for the document.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While printing paper having MCQ questions, the line height between line 1 of options and line 2 of options was a bit higher. It was done to reduce the line height space to consume less physical paper.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not convinced.. Can you share the snapshot of the UI.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without altering Margin:
image
With altering Margin:
image

bottom: convertInchesToTwip(0.0093701),
left: convertInchesToTwip(0.0693701),
right: convertInchesToTwip(0.0693701),
},
verticalAlign: VerticalAlign.CENTER,
Expand Down Expand Up @@ -992,6 +999,12 @@ function optionsTabel(testimage) {
});
}

function getLanguageKey(lang, key) {
return basicDetails[lang] && basicDetails[lang][key] ?
basicDetails[lang][key] :
basicDetails[defaultLanguage][key]
}

module.exports = {
create,
};
9 changes: 9 additions & 0 deletions src/service/print/lang/english.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"studentName" : "Student Name",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These files should be taken from blob rather than adding to the code repo. Any new language should not require any code code, it will be configuration driven.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where must i save these language files and how we will be using them. If there is any reference, please share.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vaivk369 , please help me with this if you can.

"rollNo": "Roll Number",
"subject": "Subject",
"class": "Class",
"time": "Time",
"marks": "Marks",
"instructionHead": "Instructions"
}
Loading