def get_sorted_questions(course_id, topic_id, user_id=None): """ Return a list of questions, sorted by position """ def cmp_question_position(a, b): """Order questions by the absolute value of their positions since we use -'ve to indicate hidden. """ return cmp(abs(a['position']), abs(b['position'])) questionlist = General.get_q_list(topic_id, user_id, numdone=False) if questionlist: # Filter out the questions without a positive position unless # the user has prevew permission. canpreview = check_perm(user_id, course_id, "questionpreview") if not canpreview: questionlist = [question for question in questionlist if question['position'] > 0] else: # At the moment we use -'ve positions to indicate that a question # is hidden but when displaying them we want to maintain the sort # order. for question in questionlist: # Usually questions with position 0 are broken or # uninteresting so put them at the bottom. if question['position'] == 0: question['position'] = -10000 questionlist.sort(cmp_question_position) else: questionlist = [] return questionlist
def get_q_list(topic_id): """ Return a list of questions, sorted by position. """ # TODO: Duplicated in General.get_q_list ? def cmp_question_position(a, b): """Order questions by the absolute value of their positions since we use -'ve to indicate hidden. """ return cmp(abs(a['position']), abs(b['position'])) questionlist = General.get_q_list(topic_id, None, False) if questionlist: # At the moment we use -'ve positions to indicate that a question is # hidden but when displaying them we want to maintain the sort order. for question in questionlist: # Usually questions with position 0 are broken or uninteresting # so put them at the bottom. if question['position'] == 0: question['position'] = -10000 questionlist.sort(cmp_question_position) else: questionlist = [] return questionlist
def get_sorted_questions(course_id, topic_id, user_id=None): """ Return a list of questions, sorted by position """ def cmp_question_position(a, b): """Order questions by the absolute value of their positions since we use -'ve to indicate hidden. """ return cmp(abs(a['position']), abs(b['position'])) questionlist = General.get_q_list(topic_id, user_id, numdone=False) if questionlist: # Filter out the questions without a positive position unless # the user has prevew permission. canpreview = check_perm(user_id, course_id, "questionpreview") if not canpreview: questionlist = [question for question in questionlist if question['position'] > 0] else: # At the moment we use -'ve positions to indicate that a question # is hidden but when displaying them we want to maintain the sort # order. for question in questionlist: # Usually questions with position 0 are broken or # uninteresting so put them at the bottom. if question['position'] == 0: question['position'] = -10000 questionlist.sort(cmp_question_position) else: questionlist = [] return questionlist
def get_next_prev(qt_id, topic_id): """ Find the "next" and "previous" qtemplates, by topic, position. """ if not topic_id: return None, None # This is very inefficient, but with the way questions are stored, # I didn't see a better way. Could maybe be revisited some time? questionlist = General.get_q_list(topic_id, numdone=False) if questionlist: # Filter out the questions without a positive position questionlist = [question for question in questionlist if question['position'] > 0] else: questionlist = [] # We need to step through the list finding the "next and previous" id's nextid = None foundprev = None previd = None foundcurrent = None for i in questionlist: if foundcurrent: nextid = int(i['qtid']) break if int(i['qtid']) == int(qt_id): foundprev = True foundcurrent = True if not foundprev: previd = int(i['qtid']) # previd and nextid should now contain the correct values # or None, if they are not valid (current_qtid is the first or # last question) return previd, nextid
def get_q_list(topic): """ Return a list of questions, sorted by position. """ # TODO: Duplicated in General.get_q_list ? def cmp_question_position(a, b): """Order questions by the absolute value of their positions since we use -'ve to indicate hidden. """ return cmp(abs(a['position']), abs(b['position'])) questionlist = General.get_q_list(topic, None, False) if questionlist: # At the moment we use -'ve positions to indicate that a question is # hidden but when displaying them we want to maintain the sort order. for question in questionlist: # Usually questions with position 0 are broken or uninteresting # so put them at the bottom. if question['position'] == 0: question['position'] = -10000 questionlist.sort(cmp_question_position) else: questionlist = [] return questionlist
def get_sorted_qlist_wstats(course_id, topic_id, user_id=None): """ Return a list of questions, sorted by position. With some statistics (may be expensive to calculate). """ def cmp_question_position(a, b): """Order questions by the absolute value of their positions since we use -'ve to indicate hidden. """ return cmp(abs(a['position']), abs(b['position'])) questionlist = General.get_q_list(topic_id, user_id, numdone=False) if not questionlist: return [] # Filter out the questions without a positive position unless # the user has prevew permission. questions = [question for question in questionlist if question['position'] > 0] questions.sort(cmp_question_position) for question in questions: try: question['maxscore'] = DB.get_qt_maxscore(question['qtid']) except KeyError: question['maxscore'] = 0 stats_1 = DB.get_student_q_practice_stats(user_id, question['qtid'], 3) if stats_1: # Last practices # Date of last practice question['age'] = stats_1[(len(stats_1) - 1)]['age'] question['ageseconds'] = stats_1[(len(stats_1) - 1)]['ageseconds'] # Fetch last three scores and rate them as good, average or poor for attempt in stats_1: if question['maxscore'] > 0: attempt['pscore'] = "%d%%" % ((attempt['score'] / question['maxscore']) * 100,) attempt['rating'] = 2 # average if attempt['score'] == question['maxscore']: attempt['rating'] = 3 # good if attempt['score'] == 0: attempt['rating'] = 1 # poor else: # don't have maxscore so don't make score a percentage attempt['pscore'] = "%2.1f " % (attempt['score'],) if attempt['score'] == 0: attempt['rating'] = 1 question['stats'] = stats_1 else: question['stats'] = None stats_2 = DB.get_q_stats_class(course_id, question['qtid']) if not stats_2: # no stats, make some up stats_2 = {'num': 0, 'max': 0, 'min': 0, 'avg': 0} percentage = 0 else: if stats_2['max'] == 0: percentage = 0 else: percentage = int(stats_2['avg'] / stats_2['max'] * 100) question['classpercent'] = str(percentage) + "%" user_stats = DB.get_prac_stats_user_qt(user_id, question['qtid']) if not user_stats: indivpercentage = 0 else: if stats_2['max'] == 0: indivpercentage = 0 else: indivpercentage = int(user_stats['avg'] / stats_2['max'] * 100) question['indivpercent'] = str(indivpercentage) + "%" return questions
def get_sorted_qlist_wstats(course_id, topic_id, user_id=None): """ Return a list of questions, sorted by position. With some statistics (may be expensive to calculate). """ def cmp_question_position(a, b): """Order questions by the absolute value of their positions since we use -'ve to indicate hidden. """ return cmp(abs(a['position']), abs(b['position'])) questionlist = General.get_q_list(topic_id, user_id, numdone=False) if not questionlist: return [] # Filter out the questions without a positive position unless # the user has prevew permission. questions = [question for question in questionlist if question['position'] > 0] questions.sort(cmp_question_position) for question in questions: try: question['maxscore'] = DB.get_qt_maxscore(question['qtid']) except KeyError: question['maxscore'] = 0 stats_1 = DB.get_student_q_practice_stats(user_id, question['qtid'], 3) if stats_1: # Last practices # Date of last practice question['age'] = stats_1[(len(stats_1) - 1)]['age'] question['ageseconds'] = stats_1[(len(stats_1) - 1)]['ageseconds'] # Fetch last three scores and rate them as good, average or poor for attempt in stats_1: if question['maxscore'] > 0: attempt['pscore'] = "%d%%" % ((attempt['score'] / question['maxscore']) * 100,) attempt['rating'] = 2 # average if attempt['score'] == question['maxscore']: attempt['rating'] = 3 # good if attempt['score'] == 0: attempt['rating'] = 1 # poor else: # don't have maxscore so don't make score a percentage attempt['pscore'] = "%2.1f " % (attempt['score'],) if attempt['score'] == 0: attempt['rating'] = 1 question['stats'] = stats_1 else: question['stats'] = None stats_2 = DB.get_q_stats_class(course_id, question['qtid']) if not stats_2: # no stats, make some up stats_2 = {'num': 0, 'max': 0, 'min': 0, 'avg': 0} percentage = 0 else: if stats_2['max'] == 0: percentage = 0 else: percentage = int(stats_2['avg'] / stats_2['max'] * 100) question['classpercent'] = str(percentage) + "%" user_stats = DB.get_prac_stats_user_qt(user_id, question['qtid']) if not user_stats: indivpercentage = 0 else: if stats_2['max'] == 0: indivpercentage = 0 else: indivpercentage = int(user_stats['avg'] / stats_2['max'] * 100) question['indivpercent'] = str(indivpercentage) + "%" return questions