diff --git a/knowledge.py b/knowledge.py index d2ec4cc..0874b57 100644 --- a/knowledge.py +++ b/knowledge.py @@ -129,8 +129,10 @@ class StudentKnowledge(object): if grade > 0.999: self.correct_answers += 1 - self.next_question() - action = 'new_question' + if self.next_question() is None: + action = 'finished_topic' + else: + action = 'new_question' else: self.wrong_answers += 1 @@ -140,13 +142,15 @@ class StudentKnowledge(object): if self.current_question['tries'] <= 0: logger.debug("Appending new instance of this question to the end") self.questions.append(self.factory[q['ref']].generate()) - self.next_question() - action = 'new_question' + if self.next_question() is None: + action = 'finished_topic' + else: + action = 'new_question' # FIXME show comments else: - action = 'wrong' + action = 'try_again' - # returns answered and corrected question (not new one) + # returns corrected question (not new one) which might include comments return q, action diff --git a/learnapp.py b/learnapp.py index 7165d7c..a84afe1 100644 --- a/learnapp.py +++ b/learnapp.py @@ -58,8 +58,8 @@ class LearnApp(object): session.close() # ------------------------------------------------------------------------ - def __init__(self, config_files, prefix): - self.db_setup() # setup database and check students + def __init__(self, config_files, prefix, db): + self.db_setup(db) # setup database and check students self.online = dict() # online students self.deps = nx.DiGraph(prefix=prefix) @@ -152,7 +152,7 @@ class LearnApp(object): topic_id=topic)) logger.debug(f'Saved "{q["ref"]}" into database') - if knowledge.get_current_question() is None: + if action == 'finished_topic': # finished topic, save into database logger.info(f'User "{uid}" finished "{topic}"') level = knowledge.get_topic_level(topic) @@ -176,9 +176,8 @@ class LearnApp(object): s.add(a) logger.debug(f'Saved topic "{topic}" into database') - action = 'finished_topic' # FIXME - return action + return q, action # ------------------------------------------------------------------------ @@ -208,7 +207,7 @@ class LearnApp(object): # ------------------------------------------------------------------------ # setup and check database # ------------------------------------------------------------------------ - def db_setup(self, db='students.db'): + def db_setup(self, db): logger.info(f'Checking database "{db}":') engine = create_engine(f'sqlite:///{db}', echo=False) self.Session = sessionmaker(bind=engine) diff --git a/questions.py b/questions.py index f68a1cb..35db7a1 100644 --- a/questions.py +++ b/questions.py @@ -42,10 +42,6 @@ class Question(dict): 'files': {}, }) - # FIXME unused. do childs need do override this? - # def updateAnswer(answer=None): - # self['answer'] = answer - def correct(self): self['comments'] = '' self['grade'] = 0.0 diff --git a/serve.py b/serve.py index 7db8537..e076e19 100755 --- a/serve.py +++ b/serve.py @@ -248,16 +248,17 @@ class QuestionHandler(BaseHandler): answer = answer[0] # check answer in another thread (nonblocking) - action = await self.learn.check_answer(user, answer) + # and get corrected question + q, action = await self.learn.check_answer(user, answer) # get next question (same, new or None) question = self.learn.get_current_question(user) - if action == 'wrong': + if action == 'try_again': comments_html = self.render_string('comments.html', comments=question['comments'], md=md_to_html) self.write({ - 'method': action, + 'method': 'try_again', # FIXME js 'params': { 'progress': self.learn.get_student_progress(user), 'comments': tornado.escape.to_unicode(comments_html), # FIXME @@ -265,14 +266,20 @@ class QuestionHandler(BaseHandler): } }) - elif action == 'finished_topic': # right answer, finished topic - finished_topic_html = self.render_string('finished_topic.html') - self.write({ - 'method': 'finished_topic', - 'params': { - 'question': tornado.escape.to_unicode(finished_topic_html) - } - }) + # if action == 'wrong': + # comments_html = self.render_string('comments.html', + # comments=question['comments'], md=md_to_html) + # template = self.templates[question['type']] + # question_html = self.render_string(template, question=question, md=md_to_html) + # self.write({ + # 'method': 'wrong', # FIXME js + # 'params': { + # 'question': tornado.escape.to_unicode(question_html), + # 'progress': self.learn.get_student_progress(user), + # 'comments': tornado.escape.to_unicode(comments_html), # FIXME + # 'tries': question['tries'], + # } + # }) elif action == 'new_question': # get next question in the topic template = self.templates[question['type']] @@ -287,6 +294,15 @@ class QuestionHandler(BaseHandler): } }) + elif action == 'finished_topic': # right answer, finished topic + finished_topic_html = self.render_string('finished_topic.html') + self.write({ + 'method': 'finished_topic', + 'params': { + 'question': tornado.escape.to_unicode(finished_topic_html) + } + }) + else: logger.error(f'Unknown action {action}') @@ -316,6 +332,8 @@ def main(): help='Path prefix under which the topic directories can be found, e.g. ~/topics') argparser.add_argument('--port', type=int, default=8443, help='Port to be used by the HTTPS server, e.g. 8443') + argparser.add_argument('--db', type=str, default='students.db', + help='SQLite3 database file, e.g. students.db') argparser.add_argument('--debug', action='store_true', help='Enable debug messages') arg = argparser.parse_args() @@ -336,7 +354,7 @@ def main(): # --- start application logging.info('Starting App') try: - learnapp = LearnApp(arg.conffile, prefix=arg.prefix) + learnapp = LearnApp(arg.conffile, prefix=arg.prefix, db=arg.db) except Exception as e: logging.critical('Failed to start backend application') raise e diff --git a/static/js/topic.js b/static/js/topic.js index 767eed9..b0aaad6 100644 --- a/static/js/topic.js +++ b/static/js/topic.js @@ -7,31 +7,34 @@ $.fn.extend({ } }); + +function new_question(question, tries, progress) { + $("#question_div").html(question); + $("#comments").html(""); + $("#tries").html(tries); + $('#topic_progress').css('width', (100*progress)+'%').attr('aria-valuenow', 100*progress); + MathJax.Hub.Queue(["Typeset",MathJax.Hub,"question_div"]); + $('#question_div').animateCSS('bounceInDown'); + + // enable shift+enter to submit and tab to spaces conversion + $("input:text, input:radio, input:checkbox").keydown(function (e) { + if (e.keyCode == 13) { + e.preventDefault(); + if (e.shiftKey) postQuestion(); + return false; + }}); +} + // updates question according to the response given by the server function updateQuestion(response){ switch (response["method"]) { case "new_question": - $("#question_div").html(response["params"]["question"]); - $("#comments").html(""); - $("#tries").html(response["params"]["tries"]); - - $('#topic_progress').css('width', (100*response["params"]["progress"])+'%').attr('aria-valuenow', 100*response["params"]["progress"]); - - MathJax.Hub.Queue(["Typeset",MathJax.Hub,"question_div"]); - - // enable shift+enter to submit and tab to spaces conversion - $("input:text, input:radio, input:checkbox").keydown(function (e) { - if (e.keyCode == 13) { - e.preventDefault(); - if (e.shiftKey) postQuestion(); - return false; - }}); - - $('#question_div').animateCSS('bounceInDown'); + params = response["params"]; + new_question(params["question"], params["tries"], params["progress"]); break; - case "wrong": + case "try_again": $('#topic_progress').css('width', (100*response["params"]["progress"])+'%').attr('aria-valuenow', 100*response["params"]["progress"]); $('#question_div').animateCSS('shake'); $('#comments').html(response['params']['comments']); @@ -39,6 +42,20 @@ function updateQuestion(response){ MathJax.Hub.Queue(["Typeset",MathJax.Hub,"#comments"]); break; + // case "wrong": + // $('#topic_progress').css('width', (100*response["params"]["progress"])+'%').attr('aria-valuenow', 100*response["params"]["progress"]); + // $('#question_div').animateCSS('shake'); + // $('#comments').html(response['params']['comments']); + // $("#tries").html(response["params"]["tries"]); + // MathJax.Hub.Queue(["Typeset",MathJax.Hub,"#comments"]); + + // // setTimeout(function(){ + // new_question(response["params"]["question"], response["params"]["tries"], response["params"]["progress"]); + // // }, 5000); + // break; + + + case "finished_topic": $('#submit').css("visibility", "hidden"); $("#content").html(response["params"]["question"]); @@ -62,7 +79,7 @@ function getQuestion() { // Send answer and receive a response. // The response can be a new_question or a shake if the answer is wrong, which -// is then passed to updateQuestion() +// is then passed to updateQuestion() function postQuestion() { if (typeof editor === 'object') editor.save(); -- libgit2 0.21.2