From b7cb6ffc3f81bb8c6d37d187bb41e5e7118ab09e Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 25 Feb 2016 15:52:32 +0100 Subject: Add a screen to view own solutions --- js/codeq/comms.js | 7 +++++++ js/codeq/problem_list.js | 54 ++++++++++++++++++++++++++++++++++++------------ js/codeq/solutions.js | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 js/codeq/solutions.js (limited to 'js') diff --git a/js/codeq/comms.js b/js/codeq/comms.js index d3f9252..c3f86f5 100644 --- a/js/codeq/comms.js +++ b/js/codeq/comms.js @@ -469,6 +469,13 @@ along with this program. If not, see . */ }); }, + getSolutions: function commsGetSolutions (problem_ids) { + return this.send({ + 'action': 'get_solutions', + 'problem_ids': problem_ids + }); + }, + loadProblem: function commsLoadProblem (problem_id) { return this.send({ 'action': 'load_problem', diff --git a/js/codeq/problem_list.js b/js/codeq/problem_list.js index 97bac25..e5ca36e 100644 --- a/js/codeq/problem_list.js +++ b/js/codeq/problem_list.js @@ -214,10 +214,10 @@ along with this program. If not, see . */ var li = languageIdentifier, // a shorthand rawTranslations = data.translations || {}, groups = data.groups || [], - Ngroups = groups.length, html = [], problemReferences = [], - group, problems, Nproblems, problem, i, j, nrefs, + groupReferences = [], groupProblemIDs, + group, problems, problem, i, j, nrefs, baseTabIndex = 200; // tabindex attribute of the first problem link var langDict = convertTranslations(rawTranslations, 'name', 'description'), // this will be the resulting dictionary: multi-level keys that lead up to the lang-dict groupDict, problemDict; @@ -227,26 +227,32 @@ along with this program. If not, see . */ // title: HTML structure for "name" and "desc" html.push('


'); html.push('

'); + // content: problem directory - for (i = 0; i < Ngroups; i++) { + for (i = 0; i < groups.length; i++) { group = groups[i] || {}; + problems = group.problems || []; + groupDict = convertTranslations(group.translations, 'name', 'description'); // the group-level translations, added will codeq.template.processDictionary(groupDict.description, [languageIdentifier, group.identifier]); + // group content - html.push('

'); + html.push('

'); html.push('
'); html.push('
    '); + // problem content - problems = group.problems || []; - Nproblems = problems.length; - for (j = 0; j < Nproblems; j++) { + groupProblemIDs = []; + for (j = 0; j < problems.length; j++) { nrefs = problemReferences.length; problem = problems[j] || {}; problemDict = convertTranslations(problem.translations, 'name'); html.push('
  • '); problemReferences.push({'g': group.identifier || 'nogroup', 'p': problem.identifier || 'noproblem', 'id': problem.id}); + groupProblemIDs.push(problem.id); } + groupReferences.push(groupProblemIDs); html.push('
'); } @@ -254,6 +260,7 @@ along with this program. If not, see . */ 'language': languageIdentifier, // 'prolog', 'python', ... 'html': html.join(''), // the DOM structure (without textual content), as HTML text 'refs': problemReferences, // array of problem info {g: group, p: problem, id: problem_id}, referenced from DOM elements + 'groups': groupReferences, // array of group info [problem_id1, problem_id2, …] 'commonDef': { 'hint': processHints(rawTranslations), // hint translations: keyword -> lang -> value 'hint_type': data.hint_type || {} @@ -286,18 +293,39 @@ along with this program. If not, see . */ }, /** - * Show attempted/solved status in the link for every problem. + * Update mutable data in the problem list screen. */ - showAttempts = function (languageData, attempts) { - var i; + updateDom = function (languageData, attempts) { + var problems = languageData.refs, + groups = languageData.groups, + jqGroup = jqScreen.find('h2.group-title'), + i; + + // show attempted/solved status in the link for each problem for (i = 0; i < languageData.refs.length; i++) { var ref = languageData.refs[i]; if (ref.id in attempts) { jqScreen.find('a.problem-'+i) - .removeClass('solved attempted') - .addClass(attempts[ref.id] ? 'solved' : 'attempted'); + .removeClass('solved attempted') + .addClass(attempts[ref.id] ? 'solved' : 'attempted'); } } + + // add links to view all solutions for each group + jqGroup.find('a.view-solutions').remove(); + jqGroup.each(function () { + var group = +$(this).attr('class').split(' ')[1].split('-')[1], + problem_ids = groups[group].filter(function (pid) { return pid in attempts }), + link; + if (problem_ids.length > 0) { + var link = $('') + .on('click', function (e) { + e.preventDefault(); + codeq.globalStateMachine.transition('solutions', problem_ids); + }); + $(this).append(' ', link); + } + }); }, // ================================================================================ @@ -418,7 +446,7 @@ along with this program. If not, see . */ createDom(data); } currentLanguage = language; - showAttempts(data, attemptData.data); + updateDom(data, attemptData.data); })).done(); jqScreen.css('display', ''); diff --git a/js/codeq/solutions.js b/js/codeq/solutions.js new file mode 100644 index 0000000..399c934 --- /dev/null +++ b/js/codeq/solutions.js @@ -0,0 +1,49 @@ +/* CodeQ: an online programming tutor. + Copyright (C) 2016 UL FRI + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU Affero General Public License as published by the Free +Software Foundation, either version 3 of the License, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more +details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . */ + +(function(){ + "use strict"; + var jqScreen = $('#screen_solutions'), + jqSolutions = jqScreen.find('.solutions'), + jqBtnGoBack = jqScreen.find('.btnGoBack'); + + // Register with the state machine + codeq.globalStateMachine.register('solutions', { + 'jqScreen': jqScreen, + 'isModal': true, + + 'enter': function (problem_ids) { + jqBtnGoBack.on('click', function () { + history.back(); + }); + codeq.comms.getSolutions(problem_ids) + .then(function (solutions) { + var i; + solutions = solutions.data; + for (i = 0; i < solutions.length; i++) { + jqSolutions.append('
' + solutions[i].trim() + '
'); + } + }) + .done(); + jqScreen.css('display', ''); + }, + 'exit' : function(){ + jqBtnGoBack.off('click'); + jqSolutions.empty(); + jqScreen.css('display', 'none'); + } + }); +})(); -- cgit v1.2.1