def practice_do_question(topic_id, qt_id): """ Show them a question and allow them to fill in some answers """ user_id = session['user_id'] try: course_id = Topics.get_course_id(topic_id) except KeyError: course_id = None abort(404) try: course = Courses2.get_course(course_id) except KeyError: course = None abort(404) topictitle = "UNKNOWN" try: topictitle = Topics.get_name(topic_id) except KeyError: abort(404) try: qtemplate = DB.get_qtemplate(qt_id) except KeyError: qtemplate = None abort(404) questions = Practice.get_sorted_questions(course_id, topic_id, user_id) q_title = qtemplate['title'] q_pos = DB.get_qtemplate_topic_pos(qt_id, topic_id) blocked = Practice.is_q_blocked(user_id, course_id, topic_id, qt_id) if blocked: return render_template( "practicequestionblocked.html", mesg=blocked, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) try: q_id = Practice.get_practice_q(qt_id, user_id) except (ValueError, TypeError), err: log(ERROR, "ERROR 1001 (%s,%s) %s" % (qt_id, user_id, err)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos="?", )
def practice_mark_question(topic_id, question_id): """ Mark the submitted question answersjust wa """ user_id = session['user_id'] course_id = Topics.get_course_id(topic_id) if not course_id: abort(404) course = Courses2.get_course(course_id) if not course: abort(404) topictitle = "UNKNOWN" try: topictitle = Topics.get_name(topic_id) except KeyError: abort(404) qt_id = DB.get_q_parent(question_id) q_title = DB.get_qt_name(qt_id) questions = Practice.get_sorted_questions(course_id, topic_id, user_id) q_pos = DB.get_qtemplate_topic_pos(qt_id, topic_id) blocked = Practice.is_q_blocked(user_id, course_id, topic_id, qt_id) if blocked: return render_template( "practicequestionblocked.html", mesg=blocked, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) marking = Practice.mark_q(user_id, topic_id, question_id, request) prev_id, next_id = Practice.get_next_prev(qt_id, topic_id) return render_template( "practicemarkquestion.html", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, q_id=question_id, marking=marking, next_id=next_id, prev_id=prev_id )
def practice_mark_question(topic_id, question_id): """ Mark the submitted question answersjust wa """ user_id = session['user_id'] course_id = Topics.get_course_id(topic_id) if not course_id: abort(404) course = Courses2.get_course(course_id) if not course: abort(404) topictitle = "UNKNOWN" try: topictitle = Topics.get_name(topic_id) except KeyError: abort(404) qt_id = DB.get_q_parent(question_id) q_title = DB.get_qt_name(qt_id) questions = Practice.get_sorted_questions(course_id, topic_id, user_id) q_pos = DB.get_qtemplate_topic_pos(qt_id, topic_id) blocked = Practice.is_q_blocked(user_id, course_id, topic_id, qt_id) if blocked: return render_template( "practicequestionblocked.html", mesg=blocked, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) marking = Practice.mark_q(user_id, topic_id, question_id, request) prev_id, next_id = Practice.get_next_prev(qt_id, topic_id) # TODO: need next_pos and prev_pos return render_template( "practicemarkquestion.html", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, q_id=question_id, marking=marking, next_id=next_id, prev_id=prev_id )
def do_topic_page_commands(request, topic_id, user_id): """We've been asked to perform some operations on the Topic page. Expecting form fields: selected_QTID position_QTID name_QTID where QTID is a question template id. May receive many. new_position new_name new_type select_cmd = 'copy' | 'move' select_target = TOPICID of target topic """ form = request.form mesg = [] # Make a list of all the commands to run cmdlist = [] for command in request.form.keys(): (cmd, data) = command.split('_', 2) value = form[command] if not value == "none": cmdlist += [{'cmd': cmd, 'data': data, 'value': value}] # Now run them: # Titles first for command in [cmd for cmd in cmdlist if cmd['cmd'] == 'name']: qid = int(command['data']) title = command['value'] DB.update_qt_title(qid, title) # Then positions for command in [cmd for cmd in cmdlist if cmd['cmd'] == 'position']: qtid = int(command['data']) try: position = int(command['value']) except ValueError: position = 0 DB.update_qt_pos(qtid, topic_id, position) # Then commands on selected questions target_cmd = form.get('target_cmd', None) if target_cmd: qtids = [int(cmd['data']) for cmd in cmdlist if cmd['cmd'] == 'select'] try: target_topic = int(form.get('target_topic', 0)) except ValueError: target_topic = None if target_cmd == 'move': if target_topic: for qtid in qtids: qt_title = DB.get_qt_name(qtid) topic_title = Topics.get_name(target_topic) flash("Moving %s to %s" % (qt_title, topic_title)) DB.move_qt_to_topic(qtid, target_topic) Topics.flush_num_qs(topic_id) Topics.flush_num_qs(target_topic) if target_cmd == 'copy': if target_topic: for qtid in qtids: qt_title = DB.get_qt_name(qtid) topic_title = Topics.get_name(target_topic) flash("Copying %s to %s" % (qt_title, topic_title)) newid = DB.copy_qt_all(qtid) DB.add_qt_to_topic(newid, target_topic) Topics.flush_num_qs(target_topic) if target_cmd == 'hide': for qtid in qtids: position = DB.get_qtemplate_topic_pos(qtid, topic_id) if position > 0: # If visible, make it hidden DB.update_qt_pos(qtid, topic_id, -position) title = DB.get_qt_name(qtid) flash("Made '%s' Hidden" % title) Topics.flush_num_qs(topic_id) if target_cmd == 'show': for qtid in qtids: position = DB.get_qtemplate_topic_pos(qtid, topic_id) if position == 0: # If hidden, make it visible newpos = DB.get_qt_max_pos_in_topic(topic_id) DB.update_qt_pos(qtid, topic_id, newpos + 1) Topics.flush_num_qs(topic_id) title = DB.get_qt_name(qtid) flash("Made '%s' Visible" % title) if position < 0: # If hidden, make it visible DB.update_qt_pos(qtid, topic_id, -position) Topics.flush_num_qs(topic_id) title = DB.get_qt_name(qtid) flash("Made '%s' Visible" % title) if target_cmd == "export": if len(qtids) < 1: flash("No questions selected to export") else: data = External.qts_to_zip(qtids, fname="oa_export", suffix="oaq") if not data: abort(401) sio = StringIO.StringIO(data) return 2, send_file(sio, "application/oasisqe", as_attachment=True, attachment_filename="oa_export.zip") # Then new questions new_title = form.get('new_title', None) if new_title: if not (new_title == "[New Question]" or new_title == ""): new_position = form.get('new_position', 0) try: new_position = int(new_position) except ValueError: new_position = 0 new_qtype = form.get('new_qtype', 'raw') try: new_maxscore = float(form.get('new_maxscore', 0)) except ValueError: new_maxscore = 0 newid = DB.create_qt(user_id, new_title, "No Description", 1, new_maxscore, 0) if newid: mesg.append("Created new question, id %s" % newid) DB.update_qt_pos(newid, topic_id, new_position) DB.create_qt_att(newid, "qtemplate.html", "application/oasis-html", "empty", 1) DB.create_qt_att(newid, "qtemplate.html", "application/oasis-html", "empty", 1) if new_qtype == "oqe": mesg.append("Creating new question, id %s as OQE" % newid) DB.create_qt_att(newid, "_editor.oqe", "application/oasis-oqe", "", 1) if new_qtype == "raw": mesg.append("Creating new question, id %s as RAW (%s)" % (newid, new_qtype)) DB.create_qt_att(newid, "datfile.txt", "application/oasis-dat", "0", 1) else: mesg.append("Error creating new question, id %s" % newid) L.error("Unable to create new question (%s) (%s)" % (new_title, new_position)) Topics.flush_num_qs(topic_id) return 1, {'mesg': mesg}
def practice_do_question_id(topic_id, qt_id): """ Show them a question and allow them to fill in some answers """ user_id = session['user_id'] try: course_id = Topics.get_course_id(topic_id) except KeyError: course_id = None abort(404) try: course = Courses2.get_course(course_id) except KeyError: course = None abort(404) topictitle = "UNKNOWN" try: topictitle = Topics.get_name(topic_id) except KeyError: abort(404) qtemplate = DB.get_qtemplate(qt_id) questions = Practice.get_sorted_questions(course_id, topic_id, user_id) q_title = qtemplate['title'] q_pos = DB.get_qtemplate_topic_pos(qt_id, topic_id) blocked = Practice.is_q_blocked(user_id, course_id, topic_id, qt_id) if blocked: return render_template( "practicequestionblocked.html", mesg=blocked, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) try: q_id = Practice.get_practice_q(qt_id, user_id) except (ValueError, TypeError) as err: L.error("ERROR 1001 (%s,%s) %s" % (qt_id, user_id, err)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) if not q_id > 0: L.error("ERROR 1002 (%s,%s) Question not generated" % (qt_id, user_id)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) q_body = General.render_q_html(q_id) q_body = q_body.replace(r"\240", u" ") # TODO: why is this here? return render_template( "practicedoquestion.html", q_body=q_body, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, q_id=q_id, )
def practice_do_question(topic_id, position): """ Show them a question and allow them to fill in some answers """ user_id = session['user_id'] try: course_id = Topics.get_course_id(topic_id) except KeyError: course_id = None abort(404) try: course = Courses2.get_course(course_id) except KeyError: course = None abort(404) topictitle = "UNKNOWN" try: topictitle = Topics.get_name(topic_id) except KeyError: abort(404) try: choices = DB.get_qtemplates_in_topic_position(topic_id, position) except KeyError: choices = None abort(404) if len(choices) == 1: qt_id = choices[0] elif len(choices) > 1: L.debug("Practice choosing random from: %s" % repr(choices)) qt_id = random.choice(choices) else: L.warn("Access to non existent practice topic %s question %s" % (topic_id, position)) return render_template( "practicequestionerror.html", mesg="Error accessing question.", topic_id=topic_id, course=course, q_pos=position ) qtemplate = DB.get_qtemplate(qt_id) questions = Practice.get_sorted_questions(course_id, topic_id, user_id) q_title = qtemplate['title'] q_pos = DB.get_qtemplate_topic_pos(qt_id, topic_id) blocked = Practice.is_q_blocked(user_id, course_id, topic_id, qt_id) if blocked: return render_template( "practicequestionblocked.html", mesg=blocked, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) try: q_id = Practice.get_practice_q(qt_id, user_id) except (ValueError, TypeError) as err: L.error("ERROR 1001 (%s,%s) %s" % (qt_id, user_id, err)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) if not q_id > 0: L.error("ERROR 1002 (%s,%s) Question not generated" % (qt_id, user_id)) return render_template( "practicequestionerror.html", mesg="Error generating question.", topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, ) q_body = General.render_q_html(q_id) q_body = q_body.replace(r"\240", u" ") # TODO: why is this here? return render_template( "practicedoquestion.html", q_body=q_body, topictitle=topictitle, topic_id=topic_id, qt_id=qt_id, course=course, q_title=q_title, questions=questions, q_pos=q_pos, q_id=q_id, )
def do_topic_page_commands(request, topic_id, user_id): """We've been asked to perform some operations on the Topic page. Expecting form fields: selected_QTID position_QTID name_QTID where QTID is a question template id. May receive many. new_position new_name new_type select_cmd = 'copy' | 'move' select_target = TOPICID of target topic """ form = request.form mesg = [] # Make a list of all the commands to run cmdlist = [] for command in request.form.keys(): (cmd, data) = command.split('_', 2) value = form[command] if not value == "none": cmdlist += [{'cmd': cmd, 'data': data, 'value': value}] # Now run them: # Titles first for command in [cmd for cmd in cmdlist if cmd['cmd'] == 'name']: qid = int(command['data']) title = command['value'] DB.update_qt_title(qid, title) # Then positions for command in [cmd for cmd in cmdlist if cmd['cmd'] == 'position']: qtid = int(command['data']) try: position = int(command['value']) except ValueError: position = 0 DB.update_qt_pos(qtid, topic_id, position) # Then commands on selected questions target_cmd = form.get('target_cmd', None) if target_cmd: qtids = [int(cmd['data']) for cmd in cmdlist if cmd['cmd'] == 'select'] try: target_topic = int(form.get('target_topic', 0)) except ValueError: target_topic = None if target_cmd == 'move': if target_topic: for qtid in qtids: qt_title = DB.get_qt_name(qtid) topic_title = Topics.get_name(target_topic) flash("Moving %s to %s" % (qt_title, topic_title)) DB.move_qt_to_topic(qtid, target_topic) Topics.flush_num_qs(topic_id) Topics.flush_num_qs(target_topic) if target_cmd == 'copy': if target_topic: for qtid in qtids: qt_title = DB.get_qt_name(qtid) topic_title = Topics.get_name(target_topic) flash("Copying %s to %s" % (qt_title, topic_title)) newid = DB.copy_qt_all(qtid) DB.add_qt_to_topic(newid, target_topic) Topics.flush_num_qs(target_topic) if target_cmd == 'hide': for qtid in qtids: position = DB.get_qtemplate_topic_pos(qtid, topic_id) if position > 0: # If visible, make it hidden DB.update_qt_pos(qtid, topic_id, -position) title = DB.get_qt_name(qtid) flash("Made '%s' Hidden" % title) Topics.flush_num_qs(topic_id) if target_cmd == 'show': for qtid in qtids: position = DB.get_qtemplate_topic_pos(qtid, topic_id) if position == 0: # If hidden, make it visible newpos = DB.get_qt_max_pos_in_topic(topic_id) DB.update_qt_pos(qtid, topic_id, newpos + 1) Topics.flush_num_qs(topic_id) title = DB.get_qt_name(qtid) flash("Made '%s' Visible" % title) if position < 0: # If hidden, make it visible DB.update_qt_pos(qtid, topic_id, -position) Topics.flush_num_qs(topic_id) title = DB.get_qt_name(qtid) flash("Made '%s' Visible" % title) if target_cmd == "export": if len(qtids) < 1: flash("No questions selected to export") else: data = External.qts_to_zip(qtids) if not data: abort(401) sio = StringIO.StringIO(data) return 2, send_file(sio, "application/oasisqe", as_attachment=True, attachment_filename="oa_export.zip") # Then new questions new_title = form.get('new_title', None) if new_title: if not (new_title == "[New Question]" or new_title == ""): new_position = form.get('new_position', 0) try: new_position = int(new_position) except ValueError: new_position = 0 new_qtype = form.get('new_qtype', 'raw') try: new_max_score = float(form.get('new_maxscore', 0)) except ValueError: new_max_score = 0 newid = DB.create_qt(user_id, new_title, "No Description", 1, new_max_score, 0) if newid: mesg.append("Created new question, id %s" % newid) DB.update_qt_pos(newid, topic_id, new_position) DB.create_qt_att(newid, "qtemplate.html", "application/oasis-html", "empty", 1) DB.create_qt_att(newid, "qtemplate.html", "application/oasis-html", "empty", 1) if new_qtype == "oqe": mesg.append("Creating new question, id %s as OQE" % newid) DB.create_qt_att(newid, "_editor.oqe", "application/oasis-oqe", "", 1) if new_qtype == "raw": mesg.append("Creating new question, id %s as RAW (%s)" % (newid, new_qtype)) DB.create_qt_att(newid, "datfile.txt", "application/oasis-dat", "0", 1) else: mesg.append("Error creating new question, id %s" % newid) L.error("Unable to create new question (%s) (%s)" % (new_title, new_position)) Topics.flush_num_qs(topic_id) return 1, {'mesg': mesg}