Beispiel #1
0
 def __init__(self, user, attendance_container, member_container,
              task_container):
     self.associated_user = user
     self.view = ViewMentor()
     self.controller_task_container = ControllerTaskContainer(
         task_container)
     self.controller_attendance_container = ControllerAttendanceContainer(
         attendance_container)
     self.controller_member_container = ControllerMemberContainer(
         member_container)
     self.controller_user = ControllerUser()
Beispiel #2
0
class ControllerStudent(ControllerUser):

    def __init__(self, user, task_container):
        self.associated_user = user
        self.controller_task_container = ControllerTaskContainer(task_container)
        self.controller_user = ControllerUser()
        self.view = ViewStudent()

    def start(self):
        self.view.clear_screen()
        choices = [
                    " 1: View my data",
                    " 2: Submit task",
                    " 3: View Your grades",
                    " 0: Log out"]
        correct_choices = [str(x) for x in range(0, len(choices))]
        message = "\nPlease, type Your choice: "
        to_continue = True
        while to_continue:
            user_input = ''
            while user_input not in correct_choices:
                self.view.clear_screen()
                self.view.display_collection(choices)
                user_input = self.view.get_user_input(message)
                if user_input == '1':
                    self.execute_member_display(self.associated_user)
                elif user_input == "2":
                    self.submit_task()
                elif user_input == "3":
                    self.view_grades()
                elif user_input == "0":
                    to_continue = False

    def submit_task(self):
        self.view.clear_screen()
        student_id = self.controller_user.get_member_id(self.associated_user)
        if self.controller_task_container.change_task_delivery_status(student_id):
            self.view.freeze_until_key_pressed("\nTask has been submitted successfully!")
        else:
            self.view.freeze_until_key_pressed("\nNo tasks to submit!")

    def view_grades(self):
        user_id = self.controller_user.get_member_id(self.associated_user)
        user_tasks = self.controller_task_container.cherry_pick_tasks_by_user_id(user_id)
        if user_tasks:
            self.controller_task_container.get_all_tasks(user_tasks)
            self.view.freeze_until_key_pressed("These are Your grades.")
        else:
            self.view.freeze_until_key_pressed("\nNo tasks has been deployed yet. Nothing to display.")

    def get_my_group(self, student):
        return student.get_my_group()
Beispiel #3
0
 def __init__(self, user, task_container):
     self.associated_user = user
     self.controller_task_container = ControllerTaskContainer(task_container)
     self.controller_user = ControllerUser()
     self.view = ViewStudent()
Beispiel #4
0
 def __init__(self, container_task):
     self.container_task = container_task
     self.view_task_container = ViewControllerTaskContainer()
     self.ctrl_user = ControllerUser()
Beispiel #5
0
class ControllerTaskContainer():

    def __init__(self, container_task):
        self.container_task = container_task
        self.view_task_container = ViewControllerTaskContainer()
        self.ctrl_user = ControllerUser()

    def is_collection_empty(self, collection):
        if not collection:
            self.view_task_container.display_message("\nTask list is empty. Nothing to display.")
            return True
        return False

    def add_task_to_container(self, task):
        self.container_task.add_task(task)

    def del_task_from_container(self):
        all_tasks = self.container_task.get_all_tasks()
        if self.is_collection_empty(all_tasks):
            return
        task_id = self.get_task_id_by_genre()

        for index in range(len(all_tasks)-1, -1, -1):
            if all_tasks[index].get_task_id() == task_id:
                self.container_task.del_task(all_tasks[index])

    def get_tasks_by_genre(self):
        all_tasks = self.container_task.get_all_tasks()
        if not self.is_collection_empty(all_tasks):
            chosen_task_id = self.get_task_id_by_genre()

        for task in all_tasks:
            if task.get_task_id() == chosen_task_id:
                self.view_task_container.display_message(task.get_detailed_task_display())

    def get_all_tasks(self, all_tasks=None):
        if not all_tasks:
            all_tasks = self.container_task.get_all_tasks()

        if not self.is_collection_empty(all_tasks):
            for task in all_tasks:
                self.view_task_container.display_message(task.get_detailed_task_display())

    def sumbit_task_link(self, task):
        link = self.get_valid_input("\nPass tasks link: ")
        task.set_task_link(link)

    def get_task_link(self, task):
        return task.get_task_link()

    def change_task_delivery_status(self, student_id=None):
        all_tasks = self.container_task.get_all_tasks()
        if not self.is_collection_empty(all_tasks):
            all_student_tasks = self.cherry_pick_tasks_by_user_id(student_id)
            if not self.is_collection_empty(all_student_tasks):
                task = self.take_and_validate_task_choice_by_task_id(all_student_tasks)
                self.sumbit_task_link(task)
                task.change_delivery_status()
                return True

    def grade_task(self, task):
        if not task.get_delivery_status():
            self.view_task_container.display_message("\nTask has not been submitted. Grading aborted\n")
            return False
        self.view_task_container.display_message(self.get_task_link(task))

        possible_grades = ['-3', '0', '4', '7', '10', '12']

        invalid_input = True
        while invalid_input:
            self.view_task_container.display_message("{}:\n {}".format('\nPossible choices', ",".join(possible_grades)))
            grade = self.get_valid_input("\nPass tasks grade: ")
            if grade in possible_grades:
                task.set_grade(grade)
                invalid_input = False
                self.view_task_container.display_message('\nTask graded!')
            else:
                self.view_task_container.display_message('\nChoice not in possible choices\n')

    def rename_task(self):
        if self.container_task.get_all_tasks():
            chosen_task_id = self.get_task_id_by_genre()
            new_task_name = self.get_valid_input("\nPass new task name: ")

            all_tasks = self.container_task.get_all_tasks()
            for task in all_tasks:
                if task.get_task_id() == chosen_task_id:
                    task.rename_task(new_task_name)

    def create_and_deploy_task(self, target_group):
        task_id = self.get_max_task_id()

        max_task_name_length = 15
        invalid_input = True
        while invalid_input:
            task_name = self.get_valid_input("\nPass tasks name(max {} char): ".format(max_task_name_length))
            if len(task_name) <= max_task_name_length:
                invalid_input = False

        for student in target_group:
            student_id = self.ctrl_user.get_member_id(student)
            self.add_task_to_container(ModelTask(task_name, task_id, student_id))

    def get_valid_input(self, message):
        invalid_input = True
        while invalid_input:
            user_input = self.view_task_container.get_user_input(message)
            if (len(user_input.strip()) != 0) and ('\\' not in user_input):
                invalid_input = False

        return user_input

    def get_max_task_id(self):
        all_tasks = self.container_task.get_all_tasks()
        if self.is_collection_empty(all_tasks):
                return "0000"  # First tasks id
        all_id = []
        for task in all_tasks:
            all_id.append(int(task.get_task_id()))
        return "{:0>4}".format(max(all_id)+1)

    def get_task_id_by_genre(self):
        all_tasks = self.container_task.get_all_tasks()
        if not self.is_collection_empty(all_tasks):
            tasks_by_id = []
            for task in all_tasks:
                tasks_by_id.append(task.get_short_task_display())

            invalid_choice = True
            while invalid_choice:
                self.view_task_container.display_collection(sorted(list(set(tasks_by_id))))
                user_choice = self.get_valid_input("\nChoose task by id: ")
                for task in all_tasks:
                    if task.get_task_id() == user_choice:
                        invalid_choice = False
                        break

            return user_choice

    def take_and_validate_task_choice_by_task_id(self, all_tasks):
        invalid_choice = True
        chosen_task = None
        while invalid_choice:
            self.view_task_container.display_message('\n')
            for task in all_tasks:
                self.view_task_container.display_message(task.get_generic_task_display())
            user_choice = self.get_valid_input("\nChoose task by task id: ")
            for task in all_tasks:
                if task.get_task_id() == user_choice:
                    chosen_task = task
                    invalid_choice = False
                    break
        return chosen_task

    def take_and_validate_task_choice_by_user_id(self, all_tasks):
        invalid_choice = True
        chosen_task = None
        while invalid_choice:
            self.view_task_container.display_message('\n')
            for task in all_tasks:
                self.view_task_container.display_message(task.get_detailed_task_display())

            user_choice = self.get_valid_input("\nChoose task by user id: ")

            for task in all_tasks:
                if task.get_user_id() == user_choice:
                    chosen_task = task
                    invalid_choice = False
                    break
        return chosen_task

    def take_and_validate_particular_task_choice(self):
        all_tasks = self.container_task.get_all_tasks()
        if self.is_collection_empty(all_tasks):
            return

        chosen_task = None
        invalid_choice = True
        while invalid_choice:
            self.view_task_container.display_message('\n')
            for task in all_tasks:
                self.view_task_container.display_message(task.get_detailed_task_display())

            task_id_choice = self.view_task_container.get_user_input("\nChoose task by task id: ")
            user_id_choice = self.view_task_container.get_user_input("\nChoose task by user id: ")
            for task in all_tasks:
                if (task.get_task_id() == task_id_choice) & (task.get_user_id() == user_id_choice):
                    chosen_task = task
                    invalid_choice = False
                    break
        return chosen_task

    def cherry_pick_tasks_by_user_id(self, user_id):
        all_tasks = self.container_task.get_all_tasks()
        user_tasks = []
        for task in all_tasks:
            if task.get_user_id() == user_id:
                user_tasks.append(task)

        return user_tasks

    def cherry_pick_tasks_by_task_id(self, task_id):
        all_tasks = self.container_task.get_all_tasks()
        tasks = []
        for task in all_tasks:
            if task.get_task_id() == task_id:
                tasks.append(task)
        return tasks
Beispiel #6
0
 def initialize_user_controller(self):
     return ControllerUser(self.user_name)
Beispiel #7
0
class ControllerMentor(ControllerUser):
    def __init__(self, user, attendance_container, member_container,
                 task_container):
        self.associated_user = user
        self.view = ViewMentor()
        self.controller_task_container = ControllerTaskContainer(
            task_container)
        self.controller_attendance_container = ControllerAttendanceContainer(
            attendance_container)
        self.controller_member_container = ControllerMemberContainer(
            member_container)
        self.controller_user = ControllerUser()

    def start(self):
        self.view.clear_screen()
        choices = [
            " 1: View my data", " 2: View students list", " 3: Edit student",
            " 4: Add student", " 5: Remove student", " 6: Task menu",
            " 7: Grade today's attendance", " 8: Display attendance",
            " 9: Get random groups of two", "10: Get random groups of four",
            " 0: Log out"
        ]
        correct_choices = [str(x) for x in range(0, len(choices))]
        message = "\nPlease, type Your choice: "
        to_continue = True
        while to_continue:
            user_input = ''
            while user_input not in correct_choices:
                self.view.clear_screen()
                self.view.display_collection(choices)
                user_input = self.view.get_user_input(message)
                if user_input == "1":
                    self.execute_member_display(self.associated_user)
                elif user_input == "2":
                    self.get_students_list_to_display()
                elif user_input == "3":
                    self.edit_student_details()
                elif user_input == "4":
                    self.add_student()
                elif user_input == "5":
                    self.remove_student()
                elif user_input == "6":
                    self.tasks_menu()
                elif user_input == "7":
                    self.grade_attendance()
                elif user_input == "8":
                    self.get_attendance_to_display()
                elif user_input == "9":
                    self.get_random_groups()
                elif user_input == "10":
                    self.get_random_groups(4)
                elif user_input == "0":
                    to_continue = False

    def grade_task_menu(self):
        choices = [
            "1. Choose from all tasks and students", "2. Choose by student",
            "3. Choose by task", "0. Exit menu"
        ]

        message = "\nPlease, type Your choice: "
        to_continue = True
        while to_continue:
            self.view.clear_screen()
            self.view.display_collection(choices)
            user_choice = self.view.get_user_input(message)
            if user_choice == '0':
                to_continue = False

            elif user_choice == '1':
                chosen_task = self.controller_task_container.take_and_validate_particular_task_choice(
                )
                if chosen_task:
                    self.controller_task_container.grade_task(chosen_task)
                    self.view.freeze_until_key_pressed(
                        "\nPress any key to go back to tasks menu ")
                else:
                    self.view.freeze_until_key_pressed(
                        "\nTask list is empty. Nothing to display.")

            elif user_choice == '2':
                all_students = self.controller_member_container.get_members_by_role(
                    'student')
                self.controller_member_container.get_members_display(
                    all_students)
                student = self.controller_member_container.get_user()
                student_id = self.controller_member_container.get_member_id(
                    student)
                studen_tasks = self.controller_task_container.cherry_pick_tasks_by_user_id(
                    student_id)
                if studen_tasks:
                    chosen_task = self.controller_task_container.take_and_validate_task_choice_by_task_id(
                        studen_tasks)
                    self.controller_task_container.grade_task(chosen_task)
                    self.view.freeze_until_key_pressed(
                        "\nPress any key to go back to tasks menu ")
                else:
                    self.view.freeze_until_key_pressed(
                        "\nTask list is empty. Nothing to display.")

            elif user_choice == '3':
                task_id = self.controller_task_container.get_task_id_by_genre()
                if task_id:
                    tasks_of_chosen_genre = self.controller_task_container.cherry_pick_tasks_by_task_id(
                        task_id)
                    chosen_task = self.controller_task_container.take_and_validate_task_choice_by_user_id(
                        tasks_of_chosen_genre)
                    self.controller_task_container.grade_task(chosen_task)
                    self.view.freeze_until_key_pressed(
                        "\nPress any key to go back to tasks menu ")
                else:
                    self.view.freeze_until_key_pressed(
                        "\nTask list is empty. Nothing to display.")

    def grade_attendance(self):
        group_of_students = self.controller_member_container.get_students_by_group(
        )
        if not group_of_students:
            self.view.freeze_until_key_pressed(
                'We should recruit students first...')
        else:
            self.__update_attendances(group_of_students)
            today = str(datetime.date.today())
            choices_to_display = [
                '1: student is present', '2: student is absent',
                '3: student is late'
            ]
            user_choices_to_presence_value = {'1': 1.0, '2': 0.0, '3': 0.75}
            correct_choices = [
                str(x + 1) for x in range(0, len(choices_to_display))
            ]
            for student in group_of_students:
                self.view.clear_screen()
                self.view.display_message(
                    "\nPlease, type Your choice for student: {} {}\n".format(
                        student.first_name, student.last_name))
                user_choice = ''
                while user_choice not in correct_choices:
                    self.view.display_collection(choices_to_display)
                    user_choice = self.view.get_user_input(
                        '\nType Your choice ==> ')
                self.controller_attendance_container.set_student_presence_status_for_given_date(
                    student.uid, today,
                    user_choices_to_presence_value[user_choice])

    def get_students_list_to_display(self):
        self.get_members_list_to_display(
            self.controller_member_container.get_members_by_role("student"))

    def get_attendance_to_display(self):
        group_of_students = self.controller_member_container.get_students_by_group(
        )
        if not group_of_students:
            self.view.freeze_until_key_pressed(
                'We should recruit students first...')
        else:
            self.__update_attendances(group_of_students)
            presence_values_to_words = {
                '1.0': 'present',
                '0.75': 'late',
                '0.0': 'absent'
            }
            self.view.clear_screen()
            for student in group_of_students:
                self.view.display_message(
                    '\n- student: {} {}\n\t\t\t attendance percentage: {}%'.
                    format(
                        student.first_name, student.last_name,
                        self.controller_attendance_container.
                        get_student_attendance_percentage(student.uid)))
                attendence = self.controller_attendance_container.get_presences_for_given_student(
                    student.uid)
                for presence in attendence:
                    presence_value = str(attendence[presence])
                    self.view.display_message(
                        '\t\t\t in day: {} student was {}'.format(
                            presence,
                            presence_values_to_words[presence_value]))
            self.view.freeze_until_key_pressed()

    def __update_attendances(self, students):
        '''Check if all students in collection have own attendances, if not, add attendance for student.'''
        attendances = self.controller_attendance_container.get_all_students_attendance(
        )
        students_uid = [student.uid for student in students]
        attendances_uid = [
            attendance.student_uid for attendance in attendances
        ]
        for uid in students_uid:
            if uid not in attendances_uid:
                self.controller_attendance_container.create_student_attendance_and_add_to_container(
                    uid)

    def get_members_display(self, members):
        for person in members:
            self.view.display_message(
                self.controller_user.get_member_display(person))

    def create_mentor(self, first_name, last_name, password, my_group):
        uid = self.controller_member_container.get_new_ID()
        return ModelMentor(uid, first_name, last_name, password, my_group)

    def create_student(self, first_name, last_name, password, my_group):
        uid = self.controller_member_container.get_new_ID()
        return ModelStudent(uid, first_name, last_name, password, my_group)

    def edit_student_details(self):
        students = self.controller_member_container.get_members_by_role(
            'student')
        if not students:
            self.view.freeze_until_key_pressed(
                "There is no one to edit. Press any key.. ")
        else:
            if len(students) == 1:
                student_to_change = students[0]
            else:
                self.view.clear_screen()
                self.view.display_message(
                    "\n\nCongratulations, You have privilages to change student's details.\n"
                )
                student_to_change_is_chosen = False
                while not student_to_change_is_chosen:
                    self.get_members_display(students)
                    student_to_change = self.controller_member_container.get_user(
                    )
                    if student_to_change in [
                            user for user in self.controller_member_container.
                            get_members_by_role('student')
                    ]:
                        student_to_change_is_chosen = True
                        break
                    self.view.display_message(
                        "\n\nThis user is not a student!\n")
            student_detail_to_change_is_chosen = False
            while not student_detail_to_change_is_chosen:
                self.view.clear_screen()
                self.view.display_message(
                    "\n\nLet's change data of {}:".format(
                        student_to_change.get_fullname()))
                student_detail_to_change = self.view.get_user_input(
                    "\n\nChange: first name (1) last name (2) or password (3)? "
                )
                if student_detail_to_change == "1":
                    return self.controller_user.change_first_name(
                        student_to_change)
                elif student_detail_to_change == "2":
                    return self.controller_user.change_last_name(
                        student_to_change)
                elif student_detail_to_change == "3":
                    return self.controller_user.change_password(
                        student_to_change)
                self.view.display_message(
                    "\n\nRead instructions properly and try again.\n\n")

    def add_student(self):
        self.view.clear_screen()
        self.view.display_message("\n\nLet's recruit student..\n\n")
        user_inputs = []
        messages = [
            "Enter first name: ", "Enter last name: ", "Specify password: "******"Specify group: "
        ]
        for num, statement in enumerate(messages):
            if num in (0, 1):
                user_input = self.validate_input_name(statement)
            else:
                user_input = self.validate_input(statement)
            user_inputs.append(user_input)
        user = self.create_student(user_inputs[0], user_inputs[1],
                                   user_inputs[2], user_inputs[3])
        self.view.display_message("\n\nStudent recruited..\n\n")
        self.controller_member_container.add_member(user)
        self.view.freeze_until_key_pressed()

    def remove_student(self):
        students = self.controller_member_container.get_members_by_role(
            "student")
        if not students:
            self.view.freeze_until_key_pressed(
                "There is no one to release. Press any key.. ")
        else:
            self.view.display_message(
                "\n\nLet's get rid of student! It's always fun!! :D\n\n")
            self.controller_member_container.get_members_display(students)
            if len(students) == 1:
                student_to_release = students[0]
            else:
                student_to_release = self.controller_member_container.get_user(
                    'student')
            self.controller_member_container.delete_member(student_to_release)
            self.view.freeze_until_key_pressed(
                "Done! Press anything to continue. ")

    def get_random_groups(self, size=2):
        groups = self.get_random_student_group(size)
        if groups:
            self.view.display_nested_collection(groups)
            self.view.freeze_until_key_pressed("Groups selected")

    def get_random_student_group(self, size=2):
        students = [
            x for x in self.controller_member_container.get_all_members()
            if x.role == 'student'
        ]
        if not students:
            self.view.freeze_until_key_pressed(
                'We should recruit students first...')
        else:
            shuffle(students)
            groups_of_two = list(
                zip_longest([
                    "{} {} {}".format(member.uid, member.first_name,
                                      member.last_name)
                    for member in students if students.index(member) % 2 == 0
                ], [
                    "{} {} {}".format(member.uid, member.first_name,
                                      member.last_name)
                    for member in students if students.index(member) % 2 != 0
                ],
                            fillvalue="Should join other group"))
            if size == 2:
                return groups_of_two
            if size == 4:
                return [
                    list(chain.from_iterable(group_of_four))
                    for group_of_four in list(
                        zip_longest([
                            group for group in groups_of_two
                            if groups_of_two.index(group) % 2 == 0
                        ], [
                            group for group in groups_of_two
                            if groups_of_two.index(group) % 2 != 0
                        ],
                                    fillvalue=["Should join other groups"]))
                ]

    def tasks_menu(self):
        choices = [
            "1. View all tasks", "2. View tasks by genre",
            "3. View tasks by student", "4. Add & deploy task",
            "5. Delete task", "6. Rename task", "7. Grade task", "0. Exit menu"
        ]

        message = "\nPlease, type Your choice: "
        to_continue = True
        while to_continue:
            self.view.clear_screen()
            self.view.display_collection(choices)
            user_choice = self.view.get_user_input(message)
            if user_choice == '0':
                to_continue = False
            elif user_choice == '1':
                self.controller_task_container.get_all_tasks()
                self.view.freeze_until_key_pressed(
                    "Press any key to go back to tasks menu ")
            elif user_choice == '2':
                self.controller_task_container.get_tasks_by_genre()
                self.view.freeze_until_key_pressed(
                    "Press any key to go back to tasks menu ")
            elif user_choice == '3':
                all_students = self.controller_member_container.get_members_by_role(
                    'student')
                self.controller_member_container.get_members_display(
                    all_students)
                student = self.controller_member_container.get_user()
                student_id = self.controller_member_container.get_member_id(
                    student)
                student_tasks = self.controller_task_container.cherry_pick_tasks_by_user_id(
                    student_id)
                self.controller_task_container.get_all_tasks(student_tasks)
                self.view.freeze_until_key_pressed(
                    "Press any key to go back to tasks menu ")
            elif user_choice == '4':
                target_group = self.controller_member_container.get_students_by_group(
                )
                self.controller_task_container.create_and_deploy_task(
                    target_group)
                self.view.freeze_until_key_pressed(
                    "Task added and deployed!\nPress any key to go back to tasks menu "
                )
            elif user_choice == '5':
                self.controller_task_container.del_task_from_container()
                self.view.freeze_until_key_pressed(
                    "Task deleted!\nPress any key to go back to tasks menu ")
            elif user_choice == '6':
                self.controller_task_container.rename_task()
                self.view.freeze_until_key_pressed(
                    "Task renamed!\nPress any key to go back to tasks menu ")
            elif user_choice == '7':
                self.grade_task_menu()