Esempio n. 1
0
    def get(self, request, *args, **kwargs):
        create_module_form = InstructorModuleForm
        user_role = UserRole.objects.get(email_id=request.user.email).role
        full_name = User.objects.get(email=request.user.email).full_name
        if user_role == 'INS':
            class_list = ClassTeacherTeaches.objects.filter(
                teacher_email_id=request.user.email)
        else:  # STU
            class_list = ClassStudentAttends.objects.filter(
                student_email=request.user.email)

        class_names = []
        number_of_students = []

        for module in class_list:
            class_names.append(Class.objects.get(classid=module.classid_id))

        for cobj in class_names:
            student_list = ClassStudentAttends.objects.filter(
                classid_id=cobj.classid)
            number_of_students.append(len(student_list))
            cobj.classid = encryptData(cobj.classid)

        class_names.sort(key=operator.attrgetter('class_name'))
        class_list = zip(class_names, number_of_students)

        return self.render_to_response(
            self.get_context_data(
                full_name=full_name,
                user_role=user_role,
                class_list=class_list,
                create_module_form=create_module_form,
            ))
Esempio n. 2
0
    def get(self, request, *args, **kwargs):
        cid = self.kwargs['class_id']
        classid = int(decryptData(cid))

        module_name = Class.objects.get(classid=classid).class_name
        create_module_form = InstructorTestForm
        tests = TestForClass.objects.filter(
            classid_id=classid).order_by('test_name')
        student_list = ClassStudentAttends.objects.filter(
            classid_id=classid).values('student_email')
        attempts = []
        can_take_test = []

        number_of_students = len(student_list)
        number_of_students_attempted = []

        for tobj in tests:
            curr_attempt = StudentAttemptsTest.objects.filter(
                tid_id=tobj.tid, student_email_id=request.user.email).count()
            attempts.append(curr_attempt)
            current_time = datetime.datetime.fromtimestamp(
                time.time()).strftime('%Y-%m-%d %H:%M:%S')
            end_time = datetime.datetime.strftime(tobj.end_time,
                                                  '%Y-%m-%d %H:%M:%S')

            if curr_attempt < tobj.max_attempt and current_time <= end_time:
                can_take_test.append(True)
            else:
                can_take_test.append(False)

            student_attempt_count = 0
            for student in student_list:
                email = student['student_email']
                curr_student_attempt = StudentAttemptsTest.objects.filter(
                    tid_id=tobj.tid, student_email_id=email).count()

                if curr_student_attempt > 0:
                    student_attempt_count += 1

            number_of_students_attempted.append(
                str(student_attempt_count) + ' / ' + str(number_of_students))

            tobj.tid = encryptData(tobj.tid)

        user_role = UserRole.objects.get(email_id=request.user.email).role
        full_name = User.objects.get(email=request.user.email).full_name
        test_list = zip(tests, can_take_test, number_of_students_attempted,
                        attempts)

        return self.render_to_response(
            self.get_context_data(
                full_name=full_name,
                user_role=user_role,
                classid=cid,
                test_list=test_list,
                module_name=module_name,
                create_module_form=create_module_form,
            ))
Esempio n. 3
0
    def get(self, request, *args, **kwargs):
        tid = self.kwargs['test_id']
        testid = int(decryptData(tid))

        test_attempt_list = list(
            reversed(
                StudentAttemptsTest.objects.filter(
                    tid_id=testid, student_email_id=request.user.email)))
        test_name = TestForClass.objects.get(tid=testid).test_name
        user_role = UserRole.objects.get(email_id=request.user.email).role
        full_name = User.objects.get(email=request.user.email).full_name
        marks = []
        is_full_marks = []
        table_name = []

        for tobj in test_attempt_list:
            with connection.cursor() as cursor:
                # Get table name
                student_test_name = student_attempt_table_format(
                    testid, request.user.email, tobj.attempt_no)
                instructor_test_name = test_name_table_format(
                    testid, test_name)

                # Get total marks from instructor table
                cursor.execute('SELECT SUM(marks) FROM ' +
                               instructor_test_name)
                total_marks = cursor.fetchone()[0]

                # Get total marks from student table
                cursor.execute('SELECT SUM(marks) FROM ' + student_test_name)
                student_marks = cursor.fetchone()[0]

                marks.append(str(student_marks) + ' / ' + str(total_marks))
                curr_name = encryptUrl(
                    str(instructor_test_name + '-' + student_test_name))
                table_name.append(curr_name)

                if student_marks == total_marks:
                    is_full_marks.append(True)
                else:
                    is_full_marks.append(False)

            tobj.tid_id = encryptData(tobj.tid_id)

        test_attempt_list = zip(test_attempt_list, marks, is_full_marks,
                                table_name)

        return self.render_to_response(
            self.get_context_data(full_name=full_name,
                                  user_role=user_role,
                                  testid=tid,
                                  test_name=test_name,
                                  test_attempt_list=test_attempt_list))
Esempio n. 4
0
    def post(self, request, *args, **kwargs):
        test_id = self.kwargs['test_id']
        tid = int(decryptData(test_id))
        class_id = encryptData(TestForClass.objects.get(tid=tid).classid_id)

        start_time = request.POST['start_time']
        end_time = request.POST['end_time']
        max_attempt = request.POST['max_attempt']
        data_tables = QuestionDataUsedByTest.objects.filter(tid_id=tid)

        current_time = datetime.datetime.fromtimestamp(
            time.time()).strftime('%Y-%m-%d %H:%M:%S')

        if end_time > current_time and start_time < end_time:
            try:
                connection = get_db_connection()
                with transaction.atomic():
                    test = TestForClass.objects.get(tid=tid)
                    queryset_test = TestForClass.objects.filter(tid=tid)
                    fields = ['start_time', 'end_time', 'max_attempt']
                    updatedValues = [start_time, end_time, max_attempt]

                    for field, updatedValue in zip(fields, updatedValues):
                        if getattr(test, field) != updatedValue:
                            queryset_test.update(**{field: updatedValue})

                    for table in data_tables:
                        result = request.POST.getlist(table.data_tbl_name)

                        with transaction.atomic():
                            is_visible = False
                            result = ''.join(result)
                            if result == 'on':
                                is_visible = True

                            QuestionDataUsedByTest.objects.filter(
                                tid_id=tid,
                                data_tbl_name=table.data_tbl_name).update(
                                    student_visibility=is_visible)

                    connection.commit()

            except ValueError as err:
                connection.close()
                raise err

            return HttpResponseRedirect("../../" + str(class_id) + "/test")

        else:
            raise ValueError(
                'Date cannot be in the past or start date cannot be before end date.'
            )
Esempio n. 5
0
    def get(self, request, *args, **kwargs):
        test_id = self.kwargs['test_id']
        tid = int(decryptData(test_id))
        class_id = encryptData(TestForClass.objects.get(tid=tid).classid_id)

        try:
            connection = get_db_connection()
            with transaction.atomic():
                test = TestForClass.objects.get(tid=tid)
                test.delete()
                connection.commit()

        except ValueError as err:
            connection.close()
            raise err

        return HttpResponseRedirect("../../" + str(class_id) + "/test")
Esempio n. 6
0
    def post(self, request, *args, **kwargs):
        submit_answer_form = self.form_class(request.POST)
        test_id = self.kwargs['test_id']
        tid = int(decryptData(test_id))
        class_id = encryptData(TestForClass.objects.get(tid=tid).classid_id)

        if submit_answer_form.is_valid():
            student_answer_list = request.POST.getlist('student_answer')
            student_email = request.user.email
            attempt_no = StudentAttemptsTest.objects.filter(
                tid_id=tid, student_email_id=student_email).count() + 1

            try:
                connection = get_db_connection()
                with transaction.atomic():
                    row, created = StudentAttemptsTest.objects.get_or_create(
                        attempt_no=attempt_no,
                        student_email_id=student_email,
                        tid_id=tid)
                    cursor = connection.cursor()
                    create_student_attempt_table(
                        cursor,
                        student_attempt_table_format(tid, student_email,
                                                     attempt_no), tid,
                        student_answer_list)
                    connection.commit()

            except ValueError as err:
                raise err
            finally:
                connection.close()

            return HttpResponseRedirect("../../" + str(class_id) + "/test")

        else:
            raise ValueError(submit_answer_form.errors)
Esempio n. 7
0
    def get(self, request, *args, **kwargs):
        tid = self.kwargs['test_id']
        testid = int(decryptData(tid))
        test_name = TestForClass.objects.get(tid=testid).test_name
        max_attempt = TestForClass.objects.get(tid=testid).max_attempt
        classid = TestForClass.objects.get(tid=testid).classid_id
        curr_full_name = User.objects.get(email=request.user.email).full_name
        teacher_list = ClassTeacherTeaches.objects.filter(
            classid_id=classid).values('teacher_email_id')
        student_list = ClassStudentAttends.objects.filter(
            classid_id=classid).values('student_email')
        user_name = []
        highest_marks = []
        is_highest_marks_full = []
        number_of_attempts = []
        attempt_list = []
        email_list = []

        for teacher in teacher_list:
            email_list.append(teacher['teacher_email_id'])

        for student in student_list:
            email_list.append(student['student_email'])

        user_id = list(range(0, len(email_list)))

        try:
            with connection.cursor() as cursor:
                instructor_test_name = test_name_table_format(
                    testid, test_name)
                cursor.execute('SELECT SUM(marks) FROM ' +
                               instructor_test_name)
                total_marks = cursor.fetchone()[0]

                for email in email_list:
                    user_exist = User.objects.filter(email=email).count() == 1

                    if user_exist:
                        user_name.append(
                            User.objects.get(email=email).full_name.upper)
                        test_attempt_list = list(
                            reversed(
                                StudentAttemptsTest.objects.filter(
                                    tid_id=testid, student_email_id=email)))
                        curr_number_of_attempts = len(test_attempt_list)
                        curr_highest_mark = 0

                        marks = []
                        is_full_marks = []
                        table_name = []

                        for tobj in test_attempt_list:
                            student_test_name = student_attempt_table_format(
                                testid, email, tobj.attempt_no)
                            cursor.execute('SELECT SUM(marks) FROM ' +
                                           student_test_name)
                            student_marks = cursor.fetchone()[0]

                            marks.append(
                                str(student_marks) + ' / ' + str(total_marks))

                            if student_marks == total_marks:
                                is_full_marks.append(True)
                            else:
                                is_full_marks.append(False)

                            # Check for highest mark for current student
                            if student_marks > curr_highest_mark:
                                curr_highest_mark = student_marks

                            curr_name = encryptUrl(
                                str(instructor_test_name + '-' +
                                    student_test_name))
                            table_name.append(curr_name)
                            tobj.tid_id = encryptData(tobj.tid_id)

                        if curr_highest_mark == total_marks:
                            is_highest_marks_full.append(True)
                        else:
                            is_highest_marks_full.append(False)

                        highest_marks.append(str(curr_highest_mark))
                        number_of_attempts.append(
                            str(curr_number_of_attempts) + ' / ' +
                            str(max_attempt))
                        test_attempt_list = zip(test_attempt_list, marks,
                                                table_name, is_full_marks)
                        attempt_list.append(test_attempt_list)

        except ValueError as err:
            raise err

        finally:
            connection.close()

        user_list = zip(user_id, user_name, attempt_list, highest_marks,
                        is_highest_marks_full, number_of_attempts)

        return self.render_to_response(
            self.get_context_data(testid=tid,
                                  test_name=test_name,
                                  full_name=curr_full_name,
                                  user_list=user_list,
                                  total_marks=total_marks))