Exemplo n.º 1
0
    def handle_if_check(self, user_id):
        mode = self.get_argument('mode', None)
        if (mode is None) or ((mode != "check") and (mode != "submit")):
            return False
        self.log_debug("handling check")
        params = self.get_argument('params', None)
        if params is None:
            course = self.get_argument('course', None)
            problemset = self.get_argument('problemset', None)
            question = self.get_argument('question', None)
            answer = self.get_argument('answer', None)
        else:
            params = json.loads(params)
            course = params['course']
            problemset = params['problemset']
            question = params['question']
            answer = params['answer']

        record = (mode == "submit")

        status, score, used_attempts, max_score, max_attempts = \
            JBoxCourseHomework.check_answer(course, problemset, question, user_id, answer, record)
        response = {
            'code': 0,
            'data': {
                'status': int(status),
                'score': float(score),
                'attempts': int(used_attempts),
                'max_score': float(max_score),
                'max_attempts': int(max_attempts)
            }
        }
        self.write(response)
        return True
Exemplo n.º 2
0
    def handle_get_metadata(self, is_admin, courses_offered):
        mode = self.get_argument('mode', None)
        if (mode is None) or (mode != "metadata"):
            return False

        self.log_debug("handling answers")
        params = self.get_argument('params', None)
        params = json.loads(params)
        course_id = params['course']
        problemset_id = params['problemset']
        question_ids = params['questions'] if 'questions' in params else None
        send_answers = True

        if (not is_admin) and (course_id not in courses_offered):
            send_answers = False

        err = None
        course = JBoxDynConfig.get_course(Compute.get_install_id(), course_id)
        self.log_debug("got course %r", course)
        if problemset_id not in course['problemsets']:
            err = "Problem set %s not found!" % (problemset_id,)
        if question_ids is None:
            question_ids = course['questions'][problemset_id]

        if err is None:
            report = JBoxCourseHomework.get_problemset_metadata(course_id, problemset_id, question_ids, send_answers)
            code = 0
        else:
            report = err
            code = -1

        response = {'code': code, 'data': report}
        self.write(response)
        return True
Exemplo n.º 3
0
    def handle_if_report(self, user_id, is_admin, courses_offered):
        mode = self.get_argument('mode', None)
        if (mode is None) or ((mode != "report") and (mode != "myreport")):
            return False

        self.log_debug("handling report")
        params = self.get_argument('params', None)
        params = json.loads(params)
        course_id = params['course']
        problemset_id = params['problemset']
        question_ids = params['questions'] if 'questions' in params else None

        if mode == "myreport":
            student_id = user_id
        else:
            student_id = params['student'] if 'student' in params else None

        err = None
        if (not is_admin) and (course_id not in courses_offered):
            if student_id is None:
                student_id = user_id
            elif student_id != user_id:
                err = "Course %s not found!" % (course_id, )

        if err is None:
            course = JBoxDynConfig.get_course(Compute.get_install_id(),
                                              course_id)
            if problemset_id not in course['problemsets']:
                err = "Problem set %s not found!" % (problemset_id, )
            elif question_ids is None:
                question_ids = course['questions'][problemset_id]

        if err is None:
            report = JBoxCourseHomework.get_report(course_id,
                                                   problemset_id,
                                                   question_ids,
                                                   student_id=student_id)
            code = 0
        else:
            report = err
            code = -1

        response = {'code': code, 'data': report}
        self.write(response)
        return True
Exemplo n.º 4
0
    def handle_if_report(self, user_id, is_admin, courses_offered):
        mode = self.get_argument('mode', None)
        if (mode is None) or ((mode != "report") and (mode != "myreport")):
            return False

        self.log_debug("handling report")
        params = self.get_argument('params', None)
        params = json.loads(params)
        course_id = params['course']
        problemset_id = params['problemset']
        question_ids = params['questions'] if 'questions' in params else None

        if mode == "myreport":
            student_id = user_id
        else:
            student_id = params['student'] if 'student' in params else None

        err = None
        if (not is_admin) and (course_id not in courses_offered):
            if student_id is None:
                student_id = user_id
            elif student_id != user_id:
                err = "Course %s not found!" % (course_id,)

        if err is None:
            course = JBoxDynConfig.get_course(Compute.get_install_id(), course_id)
            if problemset_id not in course['problemsets']:
                err = "Problem set %s not found!" % (problemset_id,)
            elif question_ids is None:
                question_ids = course['questions'][problemset_id]

        if err is None:
            report = JBoxCourseHomework.get_report(course_id, problemset_id, question_ids, student_id=student_id)
            code = 0
        else:
            report = err
            code = -1

        response = {'code': code, 'data': report}
        self.write(response)
        return True
Exemplo n.º 5
0
    def upload_course(user_id, course):
        course_id = course['id']

        if (user_id is not None) and (user_id not in course['admins']):
            course['admins'].append(user_id)

        existing_course = JBoxDynConfig.get_course(Compute.get_install_id(), course_id)
        existing_admins = existing_course['admins'] if existing_course is not None else []
        existing_psets = existing_course['problemsets'] if existing_course is not None else []

        question_list = {}
        if (existing_course is not None) and ('questions' in existing_course):
            question_list = existing_course['questions']

        if (existing_course is not None) and (user_id is not None) and (user_id not in existing_admins):
            return -1

        for pset in course['problemsets']:
            pset_id = pset['id']
            if pset_id not in existing_psets:
                existing_psets.append(pset_id)
            question_ids = [q['id'] for q in pset['questions']]
            question_list[pset_id] = question_ids

        dt = datetime.datetime.now(pytz.utc)
        JBoxDynConfig.set_course(Compute.get_install_id(), course_id, {
            'admins': course['admins'],
            'problemsets': existing_psets,
            'questions': question_list,
            'create_time': JBoxUserV2.datetime_to_yyyymmdd(dt)
        })

        for problemset in course['problemsets']:
            problemset_id = problemset['id']
            questions = problemset['questions']
            for question in questions:
                question_id = question['id']
                answer = question['ans']
                score = question['score'] if 'score' in question else 0
                attempts = question['attempts'] if 'attempts' in question else 0
                # nscore = question['nscore'] if 'nscore' in question else 0
                try:
                    ans = JBoxCourseHomework(course_id, problemset_id, question_id, JBoxCourseHomework.ANSWER_KEY,
                                             answer=answer, state=JBoxCourseHomework.STATE_CORRECT, create=True)
                except:
                    ans = JBoxCourseHomework(course_id, problemset_id, question_id, JBoxCourseHomework.ANSWER_KEY)
                    ans.set_answer(answer, JBoxCourseHomework.STATE_CORRECT)
                ans.set_score(score)
                ans.set_attempts(attempts)
                ans.save()

        for uid in course['admins']:
            user = JBoxUserV2(uid)
            courses_offered = user.get_courses_offered()
            if course['id'] not in courses_offered:
                courses_offered.append(course['id'])
            user.set_courses_offered(courses_offered)
            user.set_role(JBoxUserV2.ROLE_OFFER_COURSES)
            user.save()

        for uid in existing_admins:
            if uid in course['admins']:
                continue
            user = JBoxUserV2(uid)
            courses_offered = user.get_courses_offered()
            if course['id'] in courses_offered:
                courses_offered.remove(course['id'])
            user.set_courses_offered(courses_offered)
            user.set_role(JBoxUserV2.ROLE_OFFER_COURSES)
            user.save()

        return 0