diff --git a/knowledge.py b/knowledge.py index b6ad154..3c13d34 100644 --- a/knowledge.py +++ b/knowledge.py @@ -42,45 +42,63 @@ class StudentKnowledge(object): # select a topic to do and initialize questions # self.start_topic() + + # ------------------------------------------------------------------------ + # Unlock all topics whose dependencies are satisfied + # ------------------------------------------------------------------------ + def unlock_topics(self): + min_level = 0.01 # minimum level to unlock + for topic in self.topic_sequence: + if topic not in self.state: # if locked + pred = self.deps.predecessors(topic) + if all(d in self.state and self.state[d]['level'] > min_level for d in pred): # dependencies done + self.state[topic] = { + 'level': 0.0, + 'date': datetime.now() + } + logger.debug(f'unlocked {topic}') + + + # ------------------------------------------------------------------------ + # Recommends a topic to practice/learn from the state + # ------------------------------------------------------------------------ + def recommend_topic(self): + pass # FIXME + # ------------------------------------------------------------------------ # Start a new topic. If not provided, selects the first with level < 0.8 # If all levels > 0.8, will stay in the last one forever... # ------------------------------------------------------------------------ def start_topic(self, topic=''): # unlock topics whose dependencies are done - unlocked_topics = [] - for t in self.topic_sequence: - if t not in self.state: # is locked - deps = self.depgraph.predecessors(t) - if all(d in self.state and self.state[d]['level'] > 0.01 for d in deps): # dependencies done - unlocked_topics.append(t) + logger.debug('start_topic ' + topic) - for t in unlocked_topics: - self.state[t] = {'level': 0.0, 'date': datetime.now()} - logger.info(f'Unlocked "{t}"') + self.unlock_topics() # choose topic if not topic: for topic in self.topic_sequence: unlocked = topic in self.state needs_work = unlocked and self.state[topic]['level'] < 0.8 - factory = self.depgraph.node[topic]['factory'] + factory = self.deps.node[topic]['factory'] if factory and (not unlocked or needs_work): break # use given topic if possible else: unlocked = topic in self.state - factory = self.depgraph.node[topic]['factory'] + factory = self.deps.node[topic]['factory'] if not factory or not unlocked: logger.debug(f'Can\'t start topic "{topic}".') return + + self.current_topic = topic logger.info(f'Topic set to "{topic}"') # generate question instances for current topic - questionlist = self.depgraph.node[topic]['questions'] + questionlist = self.deps.node[topic]['questions'] self.questions = [factory[qref].generate() for qref in questionlist] self.current_question = self.questions.pop(0) # FIXME crashes if questions==[] self.current_question['start_time'] = datetime.now() @@ -110,7 +128,7 @@ class StudentKnowledge(object): else: self.current_question['start_time'] = datetime.now() else: - factory = self.depgraph.node[self.current_topic]['factory'] + factory = self.deps.node[self.current_topic]['factory'] self.questions.append(factory[q['ref']].generate()) return q diff --git a/learnapp.py b/learnapp.py index aefb407..be9e621 100644 --- a/learnapp.py +++ b/learnapp.py @@ -289,6 +289,6 @@ def build_dependency_graph(config_file): q['path'] = fullpath tnode['factory'][q['ref']] = QFactory(q) - logger.info(f' {len(tnode["questions"])} questions from "{tref}"') + logger.info(f'{len(tnode["questions"]):4} questions from {tref}') return g diff --git a/serve.py b/serve.py index bab673f..ce70ce9 100755 --- a/serve.py +++ b/serve.py @@ -33,7 +33,7 @@ class WebApplication(tornado.web.Application): (r'/change_password', ChangePasswordHandler), (r'/question', QuestionHandler), (r'/', LearnHandler), - (r'/topic', TopicHandler), + (r'/topic/(.+)', TopicHandler), (r'/(.+)', FileHandler), ] settings = { @@ -109,7 +109,7 @@ class ChangePasswordHandler(BaseHandler): self.write({'msg': notification}) # ---------------------------------------------------------------------------- -# main page / +# Main page: / # Shows a list of topics and proficiency (stars, locked). # ---------------------------------------------------------------------------- class LearnHandler(BaseHandler): @@ -122,12 +122,14 @@ class LearnHandler(BaseHandler): state=self.learn.get_student_state(uid) ) - +# ---------------------------------------------------------------------------- +# Start a given topic: /topic +# ---------------------------------------------------------------------------- class TopicHandler(BaseHandler): @tornado.web.authenticated - def get(self): + def get(self, topic): uid = self.current_user - topic = self.get_query_argument('topic', default='') + # topic = self.get_query_argument('topic', default='') self.learn.start_topic(uid, topic) self.render('topic.html', uid=uid, diff --git a/templates/maintopics.html b/templates/maintopics.html index 7d543d1..7c60ba9 100644 --- a/templates/maintopics.html +++ b/templates/maintopics.html @@ -42,28 +42,39 @@