From 20ba9213950544c6a7f1dd04af160bf674561e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= Date: Wed, 30 Sep 2015 16:32:07 +0200 Subject: Implemented structural translation of hints and plans. --- js/codeq/problem.js | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'js/codeq/problem.js') diff --git a/js/codeq/problem.js b/js/codeq/problem.js index d07e5fa..f6331b3 100644 --- a/js/codeq/problem.js +++ b/js/codeq/problem.js @@ -83,25 +83,51 @@ if (!translation || !(translation instanceof Array)) return false; return translation.length > 0; }, + /** + * Structurally converts the plan definition into something that the + * hint module can work with. + * The input is the translations object from problem.json in the form + * {'sl': { ..., 'plan': [hint1_sl, hint2_sl, ...]}, 'en': { ..., 'plan': [hint1_en, hint2_en, ...]}, ...} + * The output is a list of hints in the plan, translations are in each hint: + * [{'sl': hint1_sl, 'en': hint1_en, ...}, {'sl': hint2_sl, 'en': hint2_en}, ...] + */ processPlans = function (rawTranslations) { // find the default plan translation var defaultPlan = chooseDefaultTranslation(rawTranslations, 'plan', defaultPlanCondition) || [], - allPlans = {}, // the result - i, lang, tr; - // create all translations for plan + result = [], + i, lang, tr, plan, j, fragment; + if (defaultPlan.length == 0) return result; // no plan + // copy all translations from plan for (i = langs.length - 1; i >= 0; i--) { lang = langs[i]; tr = rawTranslations[lang]; // set up plan if (tr && defaultPlanCondition(tr.plan)) { - allPlans[lang] = tr.plan; + plan = tr.plan; } else { // there's no plan in the current language, copy the default plan - allPlans[lang] = defaultPlan; + plan = defaultPlan; + } + if (!(plan instanceof Array)) plan = [plan]; + for (j = 0; j < plan.length; j++) { + if (j < result.length) fragment = result[j]; + else { + fragment = {}; + result.push(fragment); + } + fragment[lang] = plan[j]; } } - return allPlans; + // ensure each plan element has all translations + for (j = result.length - 1; j >= 0; j--) { + fragment = result[j]; + for (i = langs.length - 1; i >= 0; i--) { + lang = langs[i]; + if (!fragment[lang]) fragment[lang] = 'Missing plan for language ' + lang + ' at index ' + j; + } + } + return result; }, // ================================================================================ -- cgit v1.2.1 From 8995341d43efe16f307b55f200821944cfecf4c8 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Wed, 30 Sep 2015 16:47:55 +0200 Subject: Correctly access solution in user problem data --- js/codeq/problem.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'js/codeq/problem.js') diff --git a/js/codeq/problem.js b/js/codeq/problem.js index f6331b3..e3de85a 100644 --- a/js/codeq/problem.js +++ b/js/codeq/problem.js @@ -266,7 +266,7 @@ .spread(function (userProblemData, generalProblemData) { if (userProblemData.code !== 0) throw new Error('Failed to obtain user problem data, code: ' + userProblemData.code + ', message: ' + userProblemData.message); if (!generalProblemData) throw new Error('General problem data is not defined'); - codeq.globalStateMachine.transition(language, generalProblemData, data.commonDef, userProblemData.solution); + codeq.globalStateMachine.transition(language, generalProblemData, data.commonDef, userProblemData.data.solution); }) ) .fail(function (reason) { @@ -382,4 +382,4 @@ $('#navigation-problem').css('display', 'none').removeClass("active"); } }); -})(); \ No newline at end of file +})(); -- cgit v1.2.1