diff --git a/aprendizations/learnapp.py b/aprendizations/learnapp.py index 6cd0565..76f0e99 100644 --- a/aprendizations/learnapp.py +++ b/aprendizations/learnapp.py @@ -378,3 +378,35 @@ class LearnApp(object): topic = self.online[uid]['state'].get_current_topic() prefix = self.deps.graph['prefix'] return path.join(prefix, topic, 'public') + + # ------------------------------------------------------------------------ + def get_rankings(self, uid): + logger.info(f'User {uid} get rankings') + with self.db_session() as s: + students = s.query(Student.id, Student.name).all() + student_topics = s.query(StudentTopic.student_id, + StudentTopic.topic_id, + StudentTopic.level, + StudentTopic.date).all() + total_topics = s.query(Topic).count() + + rankings = {s[0]: 0.0 for s in students} + now = datetime.now() + for uid, topic, level, date in student_topics: + date = datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f") + rankings[uid] += level**(now - date).days / total_topics + + rankings = [(uid, name, rankings[uid]) + for uid, name in students if uid != '0'] + return sorted(rankings, key=lambda x: x[2], reverse=True) + + def get_performance(self): + perf = {} + with self.db_session() as s: + students = s.query(Student.id).all() + for uid, in students: + ans = s.query(Answer).filter_by(student_id=uid) + total = ans.count() + right = ans.filter_by(grade=1.0).count() + perf[uid] = right / total if total > 0 else 0.0 + return perf diff --git a/aprendizations/serve.py b/aprendizations/serve.py index 1a72c00..b943703 100644 --- a/aprendizations/serve.py +++ b/aprendizations/serve.py @@ -49,6 +49,7 @@ class WebApplication(tornado.web.Application): (r'/logout', LogoutHandler), (r'/change_password', ChangePasswordHandler), (r'/question', QuestionHandler), # renders each question + (r'/rankings', RankingsHandler), # student rankings (r'/topic/(.+)', TopicHandler), # start a topic (r'/file/(.+)', FileHandler), # serve files (r'/', RootHandler), # show list of topics @@ -87,6 +88,19 @@ class BaseHandler(tornado.web.RequestHandler): return uid +class RankingsHandler(BaseHandler): + @tornado.web.authenticated + def get(self): + uid = self.current_user + rankings = self.learn.get_rankings(uid) + performance = self.learn.get_performance() + self.render('rankings.html', + uid=uid, + name=self.learn.get_student_name(uid), + rankings=rankings, + performance=performance) + + # ---------------------------------------------------------------------------- # /auth/login and /auth/logout # ---------------------------------------------------------------------------- diff --git a/aprendizations/templates/maintopics-table.html b/aprendizations/templates/maintopics-table.html index 6f03f75..0ff028a 100644 --- a/aprendizations/templates/maintopics-table.html +++ b/aprendizations/templates/maintopics-table.html @@ -37,6 +37,9 @@ +