예제 #1
0
    def add_q_stats(uid, qid):
        answered = (session.query(AnswerModel).filter_by(userID=uid, questionID=qid)).one()
        recorded = (session.query(UserHistoryModel).filter_by(userid=uid, source_id=qid)).first()
        if (recorded != None):
            boole = recorded.qanswered

        thing = UserHistoryModel.get_user_latest_data(uid)

        # in case the question was immediately answered
        if (answered != None) and (recorded == None):
            thing = UserHistoryModel.get_user_latest_data(uid)
            thing.asked = thing.asked + 1
            thing.answered = thing.answered + 1
            thing.qanswered = True
            session.add(UserHistoryModel(thing.userid, thing.trust, thing.answered, thing.asked))
            session.commit

        # in case the question was not answered at all
        elif (answered == None) and (recorded == None):
            thing = UserHistoryModel.get_user_latest_data(uid)
            thing.asked = thing.asked + 1
            #thing.answered = thing.answered + 0
            thing.qanswered = False
            session.add(UserHistoryModel(thing.userid, thing.trust, thing.answered, thing.asked))
            session.commit

        # in case the question was previously asked, but only answered after unlocking it
        elif (answered != None) and (recorded != None) and not (boole):
            thing = UserHistoryModel.get_user_latest_data(uid)
            #thing.asked = thing.asked + 0
            thing.answered = thing.answered + 1
            thing.qanswered = True
            session.add(UserHistoryModel(thing.userid, thing.trust, thing.answered, thing.asked))
            session.commit
예제 #2
0
    def get_active_questions(userid, courseid):
        anssub = session.query(AnswerModel).filter(AnswerModel.userID == userid).\
            subquery()

        # HACK: I can't figure out how to do timedelta stuff inside a filter,
        #       so that is done after pulling all data... Slow!

        # Need to use the old Alias.c.[columname] when using subquery!
        tmp = session.query(Question).\
                outerjoin(anssub, anssub.c.questionID == Question.id).\
                filter(Question._answerable == True).\
                filter(Question.course_id == courseid)

        #print tmp
        #print [(x.modified + timedelta(seconds=x.time), datetime.now()) for x in tmp]

        questions = []

        for x in tmp:
            if x.time == 0:
                questions.append(x)
            elif x.activate_time + timedelta(seconds=x.time) > datetime.now():
                questions.append(x)

        return questions
예제 #3
0
 def get_list_asked():
    """Retrieves questions asked by the user currently logged in."""
    if g.lti.is_instructor():
        # TODO: pagination, etc..... same goes for get_questions
        session.commit()
        return render_template('question_list.html',
                               questions=session.query(Question).filter_by(user_id=g.lti.get_user_id()  ) )
    else:
        session.commit()
        return render_template('question_list.html',
                               questions=session.query(Question).filter_by(user_id=g.lti.get_user_id()  ) )
예제 #4
0
    def get_filtered_offset(cls,limit,offset=0,orderby='id',**kws):
        count = session.query(cls).count()
        curpage = (offset / limit)+1
        if curpage == 0: curpage = 1
        maxpages = (count/limit) if count % limit == 0 else (count/limit)+1
        startpage = (curpage/5)*5 if curpage > 5 else 1
        pagecount = startpage + 5 if maxpages >= startpage+5 else maxpages

        if len(kws) > 0:
            return session.query(cls).filter_by(**kws).order_by(getattr(cls,orderby).desc()).offset(offset).limit(limit)
        return (session.query(cls).order_by(getattr(cls,orderby).desc()).offset(offset).limit(limit),
                curpage,
                maxpages,
                startpage,
                pagecount)
예제 #5
0
 def save(questionID, userID, answerText):
     try:
         ranking = ((session.query(UserModel).filter_by(userid=userID).one().trust - 1000.0) / 4) + 1000.0
     except exc.InvalidRequestError:
         ranking = 1000.0
     session.add(AnswerModel(questionID=questionID, userID=userID, text=answerText, edit=0, ranking=ranking))
     session.commit()
예제 #6
0
 def add_list(list):
     for a_id, u_id in list:
         if session.query(Schedule).filter(Schedule.answer_id == a_id,
                 Schedule.user_id == u_id).first() is None:
             session.add(Schedule(a_id, u_id))
     
     session.commit()
예제 #7
0
 def get_tag_ids(answerID):
     tlist = session.query(AnswerTag.tag_id).filter(
             AnswerTag.answer_id==answerID).all()
     map(list, tlist)
     endlist = []
     for x in tlist:
         endlist.append(x[0])
     return endlist 
예제 #8
0
 def test_4updateAnswer(self):
     AnswerModel.updateAnswer(1,'NEW')
     obj = None
     try:
         obj = session.query(AnswerModel).filter_by(id=1,text="NEW").one()
     except:
         pass
     self.assertTrue(obj is not None)
예제 #9
0
 def test_3savereview(self):
     AnswerModel.savereview(999,9999,'TEST',1)
     obj = None
     try:
         obj = session.query(AnswerModel).filter_by(questionID=999,userID=9999).one()
     except:
         pass
     self.assertTrue(obj is not None)
예제 #10
0
 def test_2save(self):
     AnswerModel.save(101,1234,'TEST')
     obj = None
     try:
         obj = session.query(AnswerModel).filter_by(questionID=101,userID=1234).one()
     except:
         pass
     self.assertTrue(obj is not None)
예제 #11
0
 def add_tag(name):
     if session.query(Tag.name).filter(Tag.name == name).first() is None:
         tag = Tag(name)
         session.add(tag)
         session.commit()
         return tag.id
     else:
         return False
예제 #12
0
    def get_answer(user_id):
        answer_id = session.query(Schedule.answer_id).filter(
            Schedule.user_id == user_id).first()

        if answer_id is not None:
            return AnswerModel.by_id(answer_id[0])
        
        return None
예제 #13
0
 def test_4remove_by_id(self):
     AnswerModel.remove_by_id(1)
     obj = None
     try:
         obj = session.query(AnswerModel).filter_by(id=1).one()
     except:
         pass
     self.assertTrue(obj is None)
예제 #14
0
    def get_answered_active_questions(userid, courseid):
        """
        Exactly the same as get_unanswered_questions except we want the answered
        ones
        """
        anssub = session.query(AnswerModel).filter(AnswerModel.userID == userid).\
            subquery()


        # Need to use the old Alias.c.[columname] when using subquery!
        tmp = session.query(Question).\
                outerjoin(annsub, anssub.c.questionID == Question.id).\
                filter(Question.available == True).\
                filter(Question.course_id == courseid).\
                filter(anssub.c.id != None).all()

        print tmp
        print [(x.modified + timedelta(seconds=x.time), datetime.now()) for x in tmp]
예제 #15
0
 def check_answer_exists(uID, qID):
     if engine.dialect.has_table(engine.connect(), "answer"):
         try:
             answer = session.query(AnswerModel).filter_by(questionID=qID, userID=uID, edit=0).one()
             return 1
         except exc.InvalidRequestError:
             return 0
     else:
         return 0
예제 #16
0
 def time_since_last(cls, user_id):
     created = session.query(cls.created).filter(
         cls.user_id == user_id).order_by(cls.id.desc()).first()
     
     if created is None:
         return None
         
     dt = datetime.now() - created[0]
     return dt.seconds + dt.days*86400
예제 #17
0
 def __init__(self):
     # create a few database items
     for model in session.query(Question):
         break
     else:
         session.add(Question("1","What am I?",True))
         session.add(Question("1","Who am I?",True))
         session.add(Question("1","Where am I?",True))
         session.commit()
예제 #18
0
 def setTrust(uID, trust):
     # set current trust (in usermodel)
     user = session.query(UserModel).filter_by(userid=uID).one()
     user.trust = trust
     # fetch last known history record
     old = UserHistoryModel.get_user_latest_data(uID)
     # create a new trust entry in userhistory
     history = UserHistoryModel(uID, trust, old.answered, old.asked)
     session.add(history)
     session.commit()
예제 #19
0
 def getAnswerID(uID, qID):
     if engine.dialect.has_table(engine.connect(), "answer"):
         try:
             answer = session.query(AnswerModel).filter_by(
                 questionID=qID, userID=uID).one()
             return answer.id
         except exc.InvalidRequestError:
             return 0
     else:
         return 1
예제 #20
0
    def get_list_to_answer():
     """Retrieves questions to be answered by the instructor (all questions )"""
     if g.lti.is_instructor():
         # TODO: pagination, etc..... same goes for get_questions
         session.commit()
         return render_template('answer_student_questions.html',
                                questions=session.query(Question).\
                                    filter(Question.course_id == g.lti.get_course_id() ).\
                                    filter(Question.course_id != g.lti.get_user_id() ))         #Filter questions by instructor

     #Only instructors can answer these questions
     else:
         return render_template('access_restricted.html')
예제 #21
0
 def get_filtered(cls, **kws):
     if len(kws) > 0:
         return session.query(cls).filter_by(**kws).all()
     return cls.get_all()
예제 #22
0
 def by_ids(cls, ids):
     if not ids:
         return []
     return session.query(cls).filter(cls.id.in_(ids)).all()
예제 #23
0
 def by_id(cls, id):
     try:
         return session.query(cls).filter(cls.id == id).one()
     except:
         return None
예제 #24
0
 def get_all(cls):
     return session.query(cls).filter().all()
예제 #25
0
	def add_answerrating(aid, rid):
		if session.query(AnswerRating).filter(
					AnswerRating.answer_id==aid,
					AnswerRating.rating_id==rid).first() is None:
				session.add(AnswerRating(aid, rid))	
예제 #26
0
 def by_course_id(cls, course_id):
     return session.query(cls).filter(cls.course_id == course_id).all()
예제 #27
0
 def update_answer(answerID, answerText):
     session.query(AnswerModel).filter_by(id=answerID).update(
         {"text": answerText}, synchronize_session=False)
예제 #28
0
 def get_answers_ordered_by_rank(question_id):
     return session.query(AnswerModel).filter(AnswerModel.questionID==question_id).order_by(AnswerModel.ranking.desc())
예제 #29
0
 def render(self):
     return render_template('index.html',questions=session.query(Question).filter().all())
예제 #30
0
	def get_all():
		return session.query(AnswerRating).filter().all()