Example #1
0
def cadmin_top(course_id):
    """ Present top level course admin page """
    course = Courses2.get_course(course_id)
    if not course:
        abort(404)

    user_id = session['user_id']
    is_sysadmin = check_perm(user_id, -1, 'sysadmin')

    topics = Courses2.get_topics_list(course_id)
    exams = [
        Exams.get_exam_struct(exam_id, course_id)
        for exam_id in Courses.get_exams(course_id, prev_years=False)
    ]

    exams.sort(key=lambda y: y['start_epoch'], reverse=True)
    groups = Courses.get_groups(course_id)
    choosegroups = [
        group for group in Groups.all_groups() if group.id not in groups
    ]
    return render_template("courseadmin_top.html",
                           course=course,
                           topics=topics,
                           exams=exams,
                           choosegroups=choosegroups,
                           groups=groups,
                           is_sysadmin=is_sysadmin)
Example #2
0
def practice_choose_topic(course_id):
    """ Present a list of topics for them to choose from the given course """
    user_id = session['user_id']
    try:
        course = Courses.get_course(course_id)
    except KeyError:
        course = None
        abort(404)
    try:
        topics = Courses.get_topics_list(course_id)
    except KeyError:
        topics = []
        abort(404)

    members = None
    for topic in topics:
        if topic['visibility'] == 2:  # course only
            if not members:
                members = Courses.get_users(course_id)
            if user_id not in members:
                topics.remove(topic)
    return render_template(
        "practicecourse.html",
        courses=Setup.get_sorted_courselist(),
        canpreview=check_perm(user_id, course_id, "questionpreview"),
        topics=topics,
        course=course
    )
Example #3
0
def cadmin_top(course_id):
    """ Present top level course admin page """
    course = Courses2.get_course(course_id)
    if not course:
        abort(404)

    user_id = session['user_id']
    is_sysadmin = check_perm(user_id, -1, 'sysadmin')

    topics = Courses2.get_topics_list(course_id)
    exams = [Exams.get_exam_struct(exam_id, course_id)
             for exam_id in Courses.get_exams(course_id, prev_years=False)]

    exams.sort(key=lambda y: y['start_epoch'], reverse=True)
    groups = Courses.get_groups(course_id)
    choosegroups = [group
                    for group in Groups.all_groups()
                    if group.id not in groups]
    return render_template(
        "courseadmin_top.html",
        course=course,
        topics=topics,
        exams=exams,
        choosegroups=choosegroups,
        groups=groups,
        is_sysadmin=is_sysadmin
    )
Example #4
0
    def test_assess_create(self):
        """ Create an empty assessment"""

        course_id = Courses.create("TESTCOURSE5", "unit tests for assessment", 1, 1)
        Courses.create_config(course_id, "casual", 1)
        Courses.set_active(course_id, True)
        Courses.set_prac_vis(course_id, "none")
        Courses.set_assess_vis(course_id, "none")

        title = "Test Assessment 1"
        atype = 2  # assignment
        duration = 60
        code = "123456"
        instant = 1
        instructions = "These are the instructions"
        astart = datetime.datetime.utcnow()
        aend = astart + datetime.timedelta(hours=2)

        exam_id = Exams.create(course_id, 1, title, atype, duration, astart,
                               aend, instructions, code=code, instant=instant)
        self.assertGreater(exam_id, 0)

        topic1_id = Topics.create(course_id, "TESTASSESS1", 1, 1)
        self.assertGreater(topic1_id, 0)

        data = open(self.test_question_fname).read()
        numread = External.import_qts_from_zip(data, topic1_id)
        self.assertEqual(numread, 3)
Example #5
0
def get_exam_list_sorted(user_id, prev_years=False):
    """ Return a list of exams for the given user. """
    courses = Courses.get_all()
    exams = []
    for cid in courses:
        try:
            exams += [Exams.get_exam_struct(e, user_id)
                      for e in Courses.get_exams(cid, prev_years=prev_years)]
        except KeyError, err:
            L.error("Failed fetching exam list for user %s: %s" %
                    (user_id, err))
Example #6
0
def cadmin_course_add_group(course_id):
    """ We've been asked to add a group to the course.
    """
    group_id = int(request.form.get("addgroup", "0"))
    if not group_id:
        flash("No group selected")
        return redirect(url_for('cadmin_config', course_id=course_id))

    Courses.add_group(group_id, course_id)
    group = Groups.Group(group_id)
    flash("Group %s added" % (group.name, ))
    return redirect(url_for('cadmin_config', course_id=course_id))
Example #7
0
    def test_create_topic(self):
        """ Fetch a topic back and check it
        """

        course_id = Courses.create("TEST101", "Test topic position logic", 1, 1)

        self.assertDictContainsSubset(
            {course_id:
                 {'active': 1,
                  'assess_visibility': 'enrol',
                  'id': course_id,
                  'name': 'TEST101',
                  'owner': 1,
                  'practice_visibility': 'all',
                  'title': 'Test topic position logic',
                  'type': 1
                  }
             },
            Courses.get_courses_dict(),
        )

        topic1_id = Topics.create(course_id, "TESTTOPIC1", 1, 2)
        topic2_id = Topics.create(course_id, "TESTTOPIC2", 3, 3)

        self.assertGreater(topic1_id, 0)
        self.assertIsInstance(topic1_id, int)

        self.assertGreater(topic2_id, 0)
        self.assertIsInstance(topic2_id, int)

        self.assertNotEqual(topic1_id, topic2_id)

        topic1 = Topics.get_topic(topic1_id)
        topic2 = Topics.get_topic(topic2_id)

        self.assertEqual(topic1['id'], topic1_id)
        self.assertEqual(topic2['id'], topic2_id)
        self.assertEqual(topic1['title'], "TESTTOPIC1")
        self.assertEqual(topic2['title'], "TESTTOPIC2")
        self.assertEqual(topic1['visibility'], 1)
        self.assertEqual(topic2['visibility'], 3)

        self.assertEqual(Topics.get_name(topic1_id), topic1['title'])

        Topics.set_name(topic1_id, "NEWNAME1")
        self.assertEqual(Topics.get_name(topic1_id), "NEWNAME1")

        self.assertEqual(Topics.get_num_qs(topic1_id), 0)

        self.assertEqual(Topics.get_pos(topic1_id), 2)

        Topics.set_pos(topic1_id, 8)
        self.assertEqual(Topics.get_pos(topic1_id), 8)
Example #8
0
def cadmin_course_add_group(course_id):
    """ We've been asked to add a group to the course.
    """
    group_id = int(request.form.get("addgroup", "0"))
    if not group_id:
        flash("No group selected")
        return redirect(url_for('cadmin_config', course_id=course_id))

    Courses.add_group(group_id, course_id)
    group = Groups.Group(group_id)
    flash("Group %s added" % (group.name,))
    return redirect(url_for('cadmin_config', course_id=course_id))
Example #9
0
def cadmin_add_course_save():
    """ accept saved settings for a new course"""
    user_id = session['user_id']
    form = request.form
    if 'cancel_edit' in form:
        flash("Course creation cancelled")
        return redirect(url_for("setup_courses"))

    if 'save_changes' not in form:
        abort(400)

    if 'name' not in form:
        flash("You must give the course a name!")
        return redirect(url_for("cadmin_add_course"))

    if 'title' not in form:
        flash("You must give the course a title!")
        return redirect(url_for("cadmin_add_course"))

    name = form.get('name', '')
    title = form.get('title', '')
    coursetemplate = form.get('coursetemplate', 'casual')
    courserepeat = form.get('courserepeat', 1)

    course = {
        'name': name,
        'title': title,
        'coursetemplate': coursetemplate,
        'courserepeat': courserepeat
    }

    if len(name) < 1:
        flash("You must give the course a name!")
        return render_template("cadmin_add_course.html", course=course)

    existing = Courses.get_course_by_name(name)
    if existing:
        flash("There is already a course called %(name)s" % existing)
        return render_template("cadmin_add_course.html", course=course)
    if len(title) < 1:
        flash("You must give the course a title!")
        return render_template("cadmin_add_course.html", course=course)

    course_id = Courses.create(name, title, user_id, 1)
    if not course_id:
        flash("Error Adding Course!")
        return render_template("cadmin_add_course.html", course=course)

    Courses.create_config(course_id, coursetemplate, int(courserepeat))

    flash("Course %s added!" % name)
    return redirect(url_for("cadmin_top", course_id=course_id))
Example #10
0
def get_exam_list_sorted(user_id, prev_years=False):
    """ Return a list of exams for the given user. """
    courses = Courses.get_all()
    exams = []
    for cid in courses:
        try:
            exams += [
                Exams.get_exam_struct(e, user_id)
                for e in Courses.get_exams(cid, prev_years=prev_years)
            ]
        except KeyError, err:
            L.error("Failed fetching exam list for user %s: %s" %
                    (user_id, err))
Example #11
0
def get_sorted_courselist(with_stats=False, only_active=True):
    """Return a list of courses suitable for choosing one to edit
         [  ('example101', { coursedict }),  ('sorted302', { coursedict } )  ]
    """

    courses = Courses.get_courses_dict(only_active=only_active)

    inorder = []
    for cid, course in courses.iteritems():
        if with_stats:
            course['students'] = Courses.get_users(cid)
            course['size'] = len(course['students'])
        inorder.append((course['name'], course))
    inorder.sort()
    return inorder
Example #12
0
def cadmin_activate(course_id):
    """ Mark the course as active
    """
    course = None
    try:
        course = Courses2.get_course(course_id)
    except KeyError:
        abort(404)

    if not course:
        abort(404)

    Courses.set_active(course_id, True)
    flash("Course %s marked as active" % (course['name']))
    return redirect(url_for("cadmin_config", course_id=course_id))
Example #13
0
def admin_course(course_id):
    """ Present page to administer settings for a given course"""

    course = Courses2.get_course(course_id)
    course['size'] = len(Courses.get_users(course_id))
    groups = Courses.get_groups(course_id)
    choosegroups = [group
                    for g_id, group in Groups.enrolment_groups().iteritems()
                    if not g_id in groups]
    return render_template(
        "cadmin_course.html",
        course=course,
        groups=groups,
        choosegroups=choosegroups
    )
Example #14
0
def admin_course(course_id):
    """ Present page to administer settings for a given course"""

    course = Courses2.get_course(course_id)
    course['size'] = len(Courses.get_users(course_id))
    groups = Courses.get_groups(course_id)
    choosegroups = [group
                    for g_id, group in Groups.enrolment_groups().iteritems()
                    if g_id not in groups]
    return render_template(
        "cadmin_course.html",
        course=course,
        groups=groups,
        choosegroups=choosegroups
    )
Example #15
0
def cadmin_edittopics(course_id):
    """ Present a page to view and edit all topics, including hidden. """
    course = None
    try:
        course = Courses.get_course(course_id)
    except KeyError:
        abort(404)

    if not course:
        abort(404)

    topics = Courses.get_topics_list(course_id)
    return render_template("courseadmin_edittopics.html",
                           course=course,
                           topics=topics)
Example #16
0
def cadmin_activate(course_id):
    """ Mark the course as active
    """
    course = None
    try:
        course = Courses2.get_course(course_id)
    except KeyError:
        abort(404)

    if not course:
        abort(404)

    Courses.set_active(course_id, True)
    flash("Course %s marked as active" % (course['name']))
    return redirect(url_for("cadmin_config", course_id=course_id))
Example #17
0
    def test_create_qtemplate(self):
        """ Test qtemplates creation
        """

        qt1_id = DB.create_qt(1, "TESTQ1", "Test question 1", 0, 5.0, 1)
        qt2_id = DB.create_qt(1, "TESTQ2", "Test question 2", 0, 4.1, 2)

        self.assertIsInstance(qt1_id, int)
        self.assertIsInstance(qt2_id, int)

        qt1 = DB.get_qtemplate(qt1_id)
        qt2 = DB.get_qtemplate(qt2_id)

        self.assertEqual(qt1['title'], "TESTQ1")
        self.assertEqual(qt2['title'], "TESTQ2")
        self.assertEqual(qt1['description'], "Test question 1")
        self.assertEqual(qt2['description'], "Test question 2")

        course_id = Courses.create("TEST107", "Test create qtemplate", 1, 1)
        topic1_id = Topics.create(course_id, "TESTTOPIC9", 1, 2)

        qt3_id = DB.create_qt(1, "TESTQ3", "Test question 3", 0, 5.0, 1, topic1_id)

        self.assertIsInstance(qt3_id, int)

        qt3 = DB.get_qtemplate(qt3_id)
        self.assertEqual(qt3['title'], "TESTQ3")
        self.assertEqual(qt3['description'], "Test question 3")
        self.assertEqual(DB.get_topic_for_qtemplate(qt3_id), topic1_id)
Example #18
0
def cadmin_edit_exam(course_id, exam_id):
    """ Provide a form to edit an assessment """
    course = Courses.get_course(course_id)
    if not course:
        abort(404)

    exam = Exams.get_exam_struct(exam_id, course_id)
    if not exam:
        abort(404)

    if not int(exam['cid']) == int(course_id):
        flash("Assessment %s does not belong to this course." % int(exam_id))
        return redirect(url_for('cadmin_top', course_id=course_id))

    exam['start_date'] = int(date_from_py2js(exam['start']))
    exam['end_date'] = int(date_from_py2js(exam['end']))
    exam['start_hour'] = int(exam['start'].hour)
    exam['end_hour'] = int(exam['end'].hour)
    exam['start_minute'] = int(exam['start'].minute)
    exam['end_minute'] = int(exam['end'].minute)

    return render_template(
        "exam_edit.html",
        course=course,
        exam=exam
    )
Example #19
0
    def test_do_question(self):
        """ Do a question"""

        course_id = Courses.create("TEST102", "Test question logic", 1, 1)
        self.assertGreater(course_id, 0)
        topic1_id = Topics.create(course_id, "TESTQUESTIONS1", 1, 2)
        self.assertGreater(topic1_id, 0)

        qt1_id = DB.create_qt(1, "TESTQ9", "Test question 9", 0, 5.0, 1, topic_id=topic1_id)
        self.assertIsNotNone(qt1_id)

        ver = DB.get_qt_version(qt1_id)
        self.assertGreater(ver, 0)

        data = "2\n|1\n|2\n"
        qvars = [{'A1': "2"}, {'A1': "3"}]
        for row in range(0, len(qvars)):
            DB.add_qt_variation(qt1_id, row + 1, qvars[row], ver)
        DB.create_qt_att(qt1_id, "datfile.dat", "text/plain", data , ver)
        DB.create_qt_att(qt1_id, "qtemplate.html", "text/html", "What is <VAL A1>? <ANSWER 1>", ver)

        q_id = DB.get_q_by_qt_student(qt1_id, 1)
        self.assertFalse(q_id)  # Not generated yet

        q_id = General.gen_q(qt1_id, 1)
        self.assertGreater(q_id, 0)

        q_id = DB.get_q_by_qt_student(qt1_id, 1)
        self.assertTrue(qt1_id)  # Better be there now

        DB.update_qt_maxscore(qt1_id, 7.0)
        score = DB.get_qt_maxscore(qt1_id)
        self.assertEqual(score, 7.0)
        DB.set_q_viewtime(q_id)
        self.assertIsNotNone(DB.get_q_viewtime(q_id))
Example #20
0
def cadmin_assign_coord(course_id):
    """ Set someone as course coordinator
    """
    cur_user = session['user_id']
    course = Courses.get_course(course_id)
    if not course:
        abort(404)

    if "coord" not in request.form:
        abort(400)

    new_uname = sanitize_username(request.form['coord'])
    try:
        new_uid = Users2.uid_by_uname(new_uname)
    except KeyError:
        flash("User '%s' Not Found" % new_uname)
    else:
        if not new_uid:
            flash("User '%s' Not Found" % new_uname)
        else:
            L.info("courseadmin: user %s assigned as coordinator to course %s by %s" % (new_uid, course_id, cur_user))
            Permissions.add_perm(new_uid, course_id, 3)  # courseadmin
            Permissions.add_perm(new_uid, course_id, 4)  # coursecoord
            flash("%s can now control the course." % (new_uname,))

    return redirect(url_for('cadmin_config', course_id=course_id))
Example #21
0
def cadmin_group_detach(course_id, group_id):
    """ Mark the course as inactive
    """
    course = None
    try:
        course = Courses2.get_course(course_id)
    except KeyError:
        abort(404)

    if not course:
        abort(404)

    group = Groups.Group(g_id=group_id)
    Courses.del_group(group_id, course_id)
    flash("Group %s removed from course" % (group.name,))
    return redirect(url_for("cadmin_config", course_id=course_id))
Example #22
0
def cadmin_create_exam(course_id):
    """ Provide a form to create/edit a new assessment """
    course = Courses.get_course(course_id)
    if not course:
        abort(404)

    topics = CourseAdmin.get_create_exam_q_list(course_id)

    today = datetime.now()
    return render_template(
        "exam_edit.html",
        course=course,
        topics=topics,
        #  defaults
        exam={
            'id': 0,
            'start_date': int(date_from_py2js(today)+86400000),  # tomorrow
            'end_date': int(date_from_py2js(today)+90000000),  # tomorrow + hour
            'start_hour': int(today.hour),
            'end_hour': int(today.hour + 1),
            'start_minute': int(today.minute),
            'end_minute': int(today.minute),
            'duration': 60,
            'title': "Assessment",
            'archived': 1
        }
    )
Example #23
0
def practice_choose_topic(course_id):
    """ Present a list of topics for them to choose from the given course """
    user_id = session['user_id']
    try:
        course = Courses2.get_course(course_id)
    except KeyError:
        course = None
        abort(404)
    try:
        topics = Courses2.get_topics_list(course_id)
    except KeyError:
        topics = []
        abort(404)

    members = None
    for topic in topics:
        if topic['visibility'] == 2:  # course only
            if not members:
                members = Courses.get_users(course_id)
            if user_id not in members:
                topics.remove(topic)
    return render_template(
        "practicecourse.html",
        courses=Setup.get_sorted_courselist(),
        canpreview=check_perm(user_id, course_id, "questionpreview"),
        topics=topics,
        course=course
    )
Example #24
0
def cadmin_group_detach(course_id, group_id):
    """ Mark the course as inactive
    """
    course = None
    try:
        course = Courses2.get_course(course_id)
    except KeyError:
        abort(404)

    if not course:
        abort(404)

    group = Groups.Group(g_id=group_id)
    Courses.del_group(group_id, course_id)
    flash("Group %s removed from course" % (group.name, ))
    return redirect(url_for("cadmin_config", course_id=course_id))
Example #25
0
def cadmin_add_course_save():
    """ accept saved settings for a new course"""
    user_id = session["user_id"]
    form = request.form
    if "cancel_edit" in form:
        flash("Course creation cancelled")
        return redirect(url_for("setup_courses"))

    if not "save_changes" in form:
        abort(400)

    if not "name" in form:
        flash("You must give the course a name!")
        return redirect(url_for("cadmin_add_course"))

    if not "title" in form:
        flash("You must give the course a title!")
        return redirect(url_for("cadmin_add_course"))

    name = form.get("name", "")
    title = form.get("title", "")
    coursetemplate = form.get("coursetemplate", "casual")
    courserepeat = form.get("courserepeat", "eternal")

    course = {"name": name, "title": title, "coursetemplate": coursetemplate, "courserepeat": courserepeat}

    if len(name) < 1:
        flash("You must give the course a name!")
        return render_template("cadmin_add_course.html", course=course)

    existing = Courses.get_course_by_name(name)
    if existing:
        flash("There is already a course called %(name)s" % existing)
        return render_template("cadmin_add_course.html", course=course)
    if len(title) < 1:
        flash("You must give the course a title!")
        return render_template("cadmin_add_course.html", course=course)

    course_id = Courses.create(name, title, user_id, 1)
    if not course_id:
        flash("Error Adding Course!")
        return render_template("cadmin_add_course.html", course=course)

    Courses.create_config(course_id, coursetemplate, int(courserepeat))

    flash("Course %s added!" % name)
    return redirect(url_for("cadmin_top", course_id=course_id))
Example #26
0
def cadmin_edit_topic(course_id, topic_id):
    """ Present a page to view and edit a topic, including adding/editing
        questions and setting some parameters.
    """
    user_id = session['user_id']

    if not course_id:
        abort(404)

    course = Courses.get_course(course_id)
    topic = {
        'id': topic_id,
        'position': Topics.get_pos(topic_id),
        'name': Topics.get_name(topic_id)
    }
    questions = [question
                 for question in Topics.get_qts(topic_id).values()]
    for question in questions:
        question['embed_id'] = DB.get_qt_embedid(question['id'])
        if question['embed_id']:
            question['embed_url'] = "%s/embed/question/%s/question.html" % \
                                    (OaConfig.parentURL, question['embed_id'])
        else:
            question['embed_url'] = None
        question['editor'] = DB.get_qt_editor(question['id'])

    all_courses = [crse
                   for crse in Courses.get_course_list()
                   if satisfy_perms(user_id, int(crse['id']),
                                    ("questionedit", "courseadmin",
                                    "sysadmin"))]
    all_courses.sort(lambda f, s: cmp(f['name'], s['name']))

    all_course_topics = []
    for crse in all_courses:
        topics = Courses.get_topics_all(crse['id'], numq=False)
        if topics:
            all_course_topics.append({'course': crse['name'], 'topics': topics})

    questions.sort(key=lambda k: k['position'])
    return render_template(
        "courseadmin_edittopic.html",
        course=course,
        topic=topic,
        questions=questions,
        all_course_topics=all_course_topics
    )
Example #27
0
def reload_if_needed():
    """If the course table has changed, reload the info. """
    global COURSES_VERSION
    newversion = Courses.get_version()
    if newversion > COURSES_VERSION:
        COURSES_VERSION = newversion
        load_courses()
    return
Example #28
0
def exam_available_q_list(course_id):
    """ Return a list of questions that can be used to create an assessment
    """
    topics = Courses.get_topics_all(course_id, archived=0, numq=False)
    for num, topic in topics.iteritems():
        topic_id = topics[num]['id']
        topics[num]['questions'] = get_q_list(topic_id)
    return topics
Example #29
0
def reload_if_needed():
    """If the course table has changed, reload the info. """
    global COURSES_VERSION
    newversion = Courses.get_version()
    if newversion > COURSES_VERSION:
        COURSES_VERSION = newversion
        load_courses()
    return
Example #30
0
def exam_available_q_list(course):
    """ Return a list of questions that can be used to create an assessment
    """
    topics = Courses.get_topics_all(course, archived=0, numq=False)
    for num, topic in topics.iteritems():
        topic_id = topics[num]['id']
        topics[num]['questions'] = get_q_list(topic_id)
    return topics
Example #31
0
def practice_choose_question(topic_id):
    """ Present a list of questions for them to choose from the given topic """
    user_id = session['user_id']
    try:
        course_id = Topics.get_course_id(topic_id)
    except KeyError:
        course_id = None
        abort(404)
    topics = []
    try:
        topics = Courses2.get_topics_list(course_id)
    except KeyError:
        abort(404)
    try:
        course = Courses2.get_course(course_id)
    except KeyError:
        course = None
        abort(404)
    topictitle = Topics.get_name(topic_id)
    questions = Practice.get_sorted_questions(course_id, topic_id, user_id)

    thistopic = Topics.get_topic(topic_id)
    members = []
    if thistopic['visibility'] == 2:  # course only
        if not members:
            members = Courses.get_users(course_id)
            if not user_id in members:
                abort(404)

    for topic in topics:
        if topic['visibility'] == 2:  # course only
            if not members:
                members = Courses.get_users(course_id)
            if not user_id in members:
                topics.remove(topic)

    return render_template(
        "practicetopic.html",
        canpreview=check_perm(user_id, course_id, "questionpreview"),
        topics=topics,
        topic_id=topic_id,
        course=course,
        topictitle=topictitle,
        questions=questions
    )
Example #32
0
def practice_choose_question(topic_id):
    """ Present a list of questions for them to choose from the given topic """
    user_id = session['user_id']
    try:
        course_id = Topics.get_course_id(topic_id)
    except KeyError:
        course_id = None
        abort(404)
    topics = []
    try:
        topics = Courses2.get_topics_list(course_id)
    except KeyError:
        abort(404)
    try:
        course = Courses2.get_course(course_id)
    except KeyError:
        course = None
        abort(404)
    topictitle = Topics.get_name(topic_id)
    questions = Practice.get_sorted_questions(course_id, topic_id, user_id)

    thistopic = Topics.get_topic(topic_id)
    members = []
    if thistopic['visibility'] == 2:  # course only
        if not members:
            members = Courses.get_users(course_id)
            if user_id not in members:
                abort(404)

    for topic in topics:
        if topic['visibility'] == 2:  # course only
            if not members:
                members = Courses.get_users(course_id)
            if user_id not in members:
                topics.remove(topic)

    return render_template(
        "practicetopic.html",
        canpreview=check_perm(user_id, course_id, "questionpreview"),
        topics=topics,
        topic_id=topic_id,
        course=course,
        topictitle=topictitle,
        questions=questions
    )
Example #33
0
    def test_practice_topic_list(self):

        with self.app.test_client() as c:

            self.login(ADMIN_UNAME, self.adminpass, client=c)

            s = c.get('/practice/top', follow_redirects=True)
            self.assertEqual(s.status, "200 OK")
            self.assertIn("Choose A Course", s.data)

            self.assertNotIn("TESTCOURSE10", s.data)

            course_id = Courses.create("TESTCOURSE10", "unit tests", 1, 1)
            Courses.create_config(course_id, "casual", 1)
            Courses.set_active(course_id, True)
            Courses.set_prac_vis(course_id, "all")

            s = c.get('/practice/top', follow_redirects=True)
            self.assertEqual(s.status, "200 OK")
            self.assertIn("Choose A Course", s.data)

            self.assertIn("TESTCOURSE10", s.data)

            s = c.get('/practice/coursequestions/%s' % course_id)

            self.assertIn("<h2>TESTCOURSE10 (unit tests)</h2>", s.data)
            self.assertIn("Select a Topic", s.data)
Example #34
0
def get_create_exam_q_list(course):
    """ Return a list of questions that can be used to create an assessment
    """

    topics = Courses.get_topics_all(course, archived=0, numq=False)
    for num, topic in topics.iteritems():
        topic_id = topics[num]['id']
        topics[num]['questions'] = _get_q_list_sorted(topic_id)
    return topics
Example #35
0
def get_topics_list(course_id, archived=2):
    """ Return a list of all topics in the course.
    """

    reload_if_needed()
    if not "topics" in COURSES[course_id]:
        COURSES[course_id]['topics'] = Courses.get_topics_all(course_id, archived, True)
    topics = COURSES[course_id]['topics']
    tlist = [topics[tid] for tid in topics]
    return tlist
Example #36
0
    def test_import_questions(self):
        """ Import the questions made in export_questions"""
        course_id = Courses.create("TEST103", "Test import questions", 1, 1)
        self.assertGreater(course_id, 0)
        topic1_id = Topics.create(course_id, "TESTQUESTIONS1", 1, 2)
        self.assertGreater(topic1_id, 0)

        data = open(self.test_question_fname).read()
        numread = External.import_qts_from_zip(data, topic1_id)
        self.assertEqual(numread, 3)
Example #37
0
def practice_mark_question(topic_id, question_id):
    """ Mark the submitted question answers """
    user_id = session['user_id']

    course_id = Topics.get_course_id(topic_id)
    if not course_id:
        abort(404)

    course = Courses.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_practice_pos(qt_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_pos, next_pos = Practice.get_next_prev_pos(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_pos=next_pos,
        prev_pos=prev_pos
    )
Example #38
0
def get_topics_list(course_id, archived=2):
    """ Return a list of all topics in the course.
    """

    reload_if_needed()
    if "topics" not in COURSES[course_id]:
        COURSES[course_id]['topics'] = Courses.get_topics_all(
            course_id, archived, True)
    topics = COURSES[course_id]['topics']
    tlist = [topics[tid] for tid in topics]
    return tlist
Example #39
0
def cadmin_exam_viewmarked(course_id, exam_id, student_uid):
    """  Show a student's marked assessment results """

    course = Courses.get_course(course_id)
    try:
        exam = Exams.get_exam_struct(exam_id, course_id)
    except KeyError:
        exam = {}
        abort(404)
    results, examtotal = Assess.render_own_marked_exam(student_uid, exam_id)

    if examtotal is False:
        status = 0
    else:
        status = 1
    mark_time = Exams.get_mark_time(exam_id, student_uid)
    first_view = Exams.get_student_start_time(exam_id, student_uid)
    submit_time = Exams.get_submit_time(exam_id, student_uid)

    try:
        datemarked = General.human_date(mark_time)
    except AttributeError:
        datemarked = None
    try:
        datefirstview = General.human_date(first_view)
    except AttributeError:
        datefirstview = None
    try:
        datesubmit = General.human_date(submit_time)
    except AttributeError:
        datesubmit = None

    user = Users2.get_user(student_uid)

    if submit_time and first_view:
        taken = submit_time-first_view
        taken_mins = (taken.seconds/60)
    else:
        taken_mins = None

    return render_template(
        "cadmin_markedresult.html",
        course=course,
        exam=exam,
        results=results,
        examtotal=examtotal,
        datesubmit=datesubmit,
        datemarked=datemarked,
        datefirstview=datefirstview,
        taken=taken_mins,
        user=user,
        status=status
    )
Example #40
0
def cadmin_exam_results(course_id, exam_id):
    """ View the results of an assessment """
    course = Courses.get_course(course_id)
    if not course:
        abort(404)

    exam = Exams.get_exam_struct(exam_id, course_id)
    if not exam:
        abort(404)

    if not int(exam['cid']) == int(course_id):
        flash("Assessment %s does not belong to this course." % int(exam_id))
        return redirect(url_for('cadmin_top', course_id=course_id))

    exam['start_date'] = int(date_from_py2js(exam['start']))
    exam['end_date'] = int(date_from_py2js(exam['end']))
    exam['start_hour'] = int(exam['start'].hour)
    exam['end_hour'] = int(exam['end'].hour)
    exam['start_minute'] = int(exam['start'].minute)
    exam['end_minute'] = int(exam['end'].minute)

    groups = [Groups.Group(g_id=g_id)
              for g_id
              in Groups.active_by_course(course_id)]
    results = {}
    uids = set([])
    totals = {}
    for group in groups:
        results[group.id] = Exams.get_marks(group, exam_id)
        for user_id in results[group.id]:
            uids.add(user_id)
            if user_id not in totals:
                totals[user_id] = 0.0
            for qt, val in results[group.id][user_id].iteritems():
                totals[user_id] += val['score']

    questions = Exams.get_qts_list(exam_id)
    users = {}
    for uid in uids:
        users[uid] = Users2.get_user(uid)
    return render_template(
        "cadmin_examresults.html",
        course=course,
        exam=exam,
        results=results,
        groups=groups,
        users=users,
        questions=questions,
        when=datetime.now().strftime("%H:%m, %a %d %b %Y"),
        totals=totals
    )
Example #41
0
def do_topic_update(course, request):
    """Read the submitted form and make relevant changes to Topic information
    """
    categories = []
    topics = []

    form = request.form
    if form:
        for i in form.keys():
            parts = i.split('_')
            if len(parts) > 1:
                catid = parts[0]
                if catid not in categories:
                    categories = categories + [catid]

        for c in categories:
            topics = topics + [{'id': int(c),
                                'position': form['%s_position' % c],
                                'name': form['%s_name' % c],
                                'visibility': form['%s_visibility' % c]
                                }]
        for i in topics:
            if not i['id'] == 0:
                Topics.set_pos(i['id'], i['position'])
                Topics.set_name(int(i['id']), i['name'])
                Topics.set_vis(i['id'], i['visibility'])
                Courses.incr_version()
            else:
                if not i['name'] == "[Name of new topic]":
                    Topics.create(course['id'],
                                  i['name'],
                                  int(i['visibility']),
                                  i['position'])
                    Courses.incr_version()

        return True

    return False
Example #42
0
def setup_myprofile():
    """ Show an account summary for the current user account. """
    user_id = session['user_id']

    user = Users2.get_user(user_id)
    course_ids = Users2.get_courses(user_id)
    courses = []
    for course_id in course_ids:
        courses.append(Courses.get_course(course_id))
    return render_template(
        'setup_myprofile.html',
        user=user,
        courses=courses
    )
Example #43
0
def do_topic_update(course, request):
    """Read the submitted form and make relevant changes to Topic information
    """
    categories = []
    topics = []

    form = request.form
    if form:
        for i in form.keys():
            parts = i.split('_')
            if len(parts) > 1:
                catid = parts[0]
                if catid not in categories:
                    categories = categories + [catid]

        for c in categories:
            topics = topics + [{
                'id': int(c),
                'position': form['%s_position' % c],
                'name': form['%s_name' % c],
                'visibility': form['%s_visibility' % c]
            }]
        for i in topics:
            if not i['id'] == 0:
                Topics.set_pos(i['id'], i['position'])
                Topics.set_name(int(i['id']), i['name'])
                Topics.set_vis(i['id'], i['visibility'])
                Courses.incr_version()
            else:
                if not i['name'] == "[Name of new topic]":
                    Topics.create(course['id'], i['name'],
                                  int(i['visibility']), i['position'])
                    Courses.incr_version()

        return True

    return False
Example #44
0
def get_sorted_courselist(with_stats=False, only_active=True):
    """Return a list of courses suitable for choosing one to edit
         [  ('example101', { coursedict }),  ('sorted302', { coursedict } )  ]
    """

    courses = Courses2.get_course_dict(only_active=only_active)

    inorder = []
    for cid, course in courses.iteritems():
        if with_stats:
            course['students'] = Courses.get_users(cid)
            course['size'] = len(course['students'])
        inorder.append((course['name'], course))
    inorder.sort()
    return inorder
Example #45
0
def cadmin_view_topic(course_id, topic_id):
    """ Present a page to view a topic, including basic stats """
    user_id = session['user_id']

    if not course_id:
        abort(404)

    course = Courses2.get_course(course_id)
    topic = {
        'id': topic_id,
        'position': Topics.get_pos(topic_id),
        'name': Topics.get_name(topic_id)
    }
    questions = [question for question in Topics.get_qts(topic_id).values()]
    for question in questions:
        question['embed_id'] = DB.get_qt_embedid(question['id'])
        if question['embed_id']:
            question['embed_url'] = "%s/embed/question/%s/question.html" % \
                                    (OaConfig.parentURL, question['embed_id'])
        else:
            question['embed_url'] = None
        question['editor'] = DB.get_qt_editor(question['id'])

    all_courses = [
        crse for crse in Courses2.get_course_list()
        if satisfy_perms(user_id, int(crse['id']), ("questionedit",
                                                    "courseadmin", "sysadmin"))
    ]
    all_courses.sort(lambda f, s: cmp(f['name'], s['name']))

    all_course_topics = []
    for crse in all_courses:
        topics = Courses.get_topics_all(crse['id'], numq=False)
        if topics:
            all_course_topics.append({
                'course': crse['name'],
                'topics': topics
            })

    questions.sort(key=lambda k: k['position'])
    return render_template(
        "courseadmin_viewtopic.html",
        course=course,
        topic=topic,
        questions=questions,
        all_course_topics=all_course_topics,
    )
Example #46
0
def cadmin_config_submit(course_id):
    """ Allow some course configuration """
    course = Courses2.get_course(course_id)
    if not course:
        abort(404)

    form = request.form

    if "cancel" in form:
        flash("Cancelled edit")
        return redirect(url_for("cadmin_top", course_id=course_id))

    saved = False

    new_name = form.get('name', course['name'])

    existing = Courses.get_course_by_name(new_name)
    if not new_name == course['name']:
        if not (3 <= len(new_name) <= 20):
            flash("Course Name must be between 3 and 20 characters.")
        elif existing:
            flash("There is already a course called %(name)s" % existing)
        else:
            Courses.set_name(course['id'], new_name)
            saved = True

    new_title = form.get('title', course['title'])
    if not new_title == course['title']:
        if not (3 <= len(new_title) <= 100):
            flash("Course Title must be between 3 and 100 characters.")
        else:
            Courses.set_title(course['id'], new_title)
            saved = True

    practice_visibility = form.get('practice_visibility',
                                   course['practice_visibility'])
    if not (practice_visibility == course['practice_visibility']):
        saved = True
        Courses.set_prac_vis(course_id, practice_visibility)

    if saved:
        flash("Changes Saved")
    else:
        flash("No changes made.")
    return redirect(url_for("cadmin_config", course_id=course_id))
Example #47
0
def cadmin_config(course_id):
    """ Allow some course configuration """
    course = Courses2.get_course(course_id)
    if not course:
        abort(404)

    user_id = session['user_id']
    is_sysadmin = check_perm(user_id, -1, 'sysadmin')
    coords = [
        Users2.get_user(perm[0])
        for perm in Permissions.get_course_perms(course_id) if perm[1] == 3
    ]  # course_coord
    groups = Courses.get_groups(course_id)
    choosegroups = [
        group for group in Groups.all_groups() if group.id not in groups
    ]
    return render_template("courseadmin_config.html",
                           course=course,
                           coords=coords,
                           choosegroups=choosegroups,
                           groups=groups,
                           is_sysadmin=is_sysadmin)
Example #48
0
def admin_course_save(course_id):
    """ accept saved settings """
    form = request.form
    cancel_edit = form.get("cancel_edit", False)
    if cancel_edit:
        flash("Course edits cancelled")
        return redirect(url_for("admin_courses"))

    changed = False
    course = Courses2.get_course(course_id)
    groups = Courses.get_groups(course_id)

    for g_id, group in groups.iteritems():
        if form.get('delgroup_%s' % g_id):
            changed = True
            flash("Removing group %s" % group.name, "info")
            Courses.del_group(int(g_id), course_id)

    if 'course_name' in form:
        name = form['course_name']
        if not name == course['name']:
            changed = True
            Courses.set_name(course_id, name)

    if 'course_title' in form:
        title = form['course_title']
        if not title == course['title']:
            changed = True
            Courses.set_title(course_id, title)

    if 'course_active' in form:
        active = form['course_active']
        if active == '1' or active == 1:
            active = True
        else:
            active = False
        if not (active == course['active']):
            changed = True
            Courses.set_active(course_id, active)

    addbtn = form.get('group_addbtn')
    if addbtn:
        newgroup = form.get('addgroup', None)
        if newgroup:
            Courses.add_group(newgroup, course_id)
            changed = True
            group = Groups.Group(newgroup)
            flash("Group %s added." % group.name)

    if changed:
        Courses2.reload_if_needed()
        flash("Course changes saved!")
        return redirect(url_for("admin_course", course_id=course_id))

    course = Courses2.get_course(course_id)
    course['size'] = len(Courses.get_users(course_id))
    return redirect(url_for("admin_courses"))
Example #49
0
def get_topics(cid, archived=2):
    """ Return a dict of all topics in the course. """
    reload_if_needed()
    if "topics" not in COURSES[cid]:
        COURSES[cid]['topics'] = Courses.get_topics_all(cid, archived, True)
    return COURSES[cid]['topics']
Example #50
0
def load_courses():
    """Read the list of courses into memory """
    global COURSES
    L.info("Courses fetched from database.")
    COURSES = Courses.get_courses_dict()