Beispiel #1
0
def get_sharing_information_for_editor(request, levelID):
    """ Returns a information about who the level can be and is shared with """
    level = get_object_or_404(Level, id=levelID)
    valid_recipients = {}

    if permissions.can_share_level(request.user, level):
        userprofile = request.user.userprofile
        valid_recipients = {}

        # Note: independent users can't share levels so no need to check
        if hasattr(userprofile, 'student'):
            student = userprofile.student

            # First get all the student's classmates
            class_ = student.class_field
            classmates = Student.objects.filter(class_field=class_).exclude(id=student.id)
            valid_recipients['classmates'] = [
                {'id': classmate.user.user.id,
                 'name': app_tags.make_into_username(classmate.user.user),
                 'shared': level.shared_with.filter(id=classmate.user.user.id).exists()}
                for classmate in classmates]

            # Then add their teacher as well
            teacher = class_.teacher
            valid_recipients['teacher'] = {
                'id': teacher.user.user.id,
                'name': app_tags.make_into_username(teacher.user.user),
                'shared': level.shared_with.filter(id=teacher.user.user.id).exists()}

        elif hasattr(userprofile, 'teacher'):
            teacher = userprofile.teacher

            # First get all the students they teach
            valid_recipients['classes'] = []
            classes_taught = Class.objects.filter(teacher=teacher)
            for class_ in classes_taught:
                students = Student.objects.filter(class_field=class_)
                valid_recipients['classes'].append({
                    'name': class_.name, 'id': class_.id,
                    'students': [{
                        'id': student.user.user.id,
                        'name': app_tags.make_into_username(student.user.user),
                        'shared': level.shared_with.filter(id=student.user.user.id).exists()}
                        for student in students]})

            if not teacher.school:
                valid_recipients['teachers'] = []
            else:
                fellow_teachers = Teacher.objects.filter(school=teacher.school)
                valid_recipients['teachers'] = [{
                    'id': fellow_teacher.user.user.id,
                    'name': app_tags.make_into_username(fellow_teacher.user.user),
                    'shared': level.shared_with.filter(id=fellow_teacher.user.user.id).exists()}
                    for fellow_teacher in fellow_teachers if teacher != fellow_teacher]

    return HttpResponse(json.dumps(valid_recipients), content_type='application/javascript')
def get_sharing_information_for_editor(request, levelID):
    """ Returns a information about who the level can be and is shared with """
    level = get_object_or_404(Level, id=levelID)
    valid_recipients = {}

    if permissions.can_share_level(request.user, level):
        userprofile = request.user.userprofile
        valid_recipients = {}

        # Note: independent users can't share levels so no need to check
        if hasattr(userprofile, 'student'):
            student = userprofile.student

            # First get all the student's classmates
            class_ = student.class_field
            classmates = Student.objects.filter(class_field=class_).exclude(id=student.id)
            valid_recipients['classmates'] = [
                {'id': classmate.user.user.id,
                 'name': app_tags.make_into_username(classmate.user.user),
                 'shared': level.shared_with.filter(id=classmate.user.user.id).exists()}
                for classmate in classmates]

            # Then add their teacher as well
            teacher = class_.teacher
            valid_recipients['teacher'] = {
                'id': teacher.user.user.id,
                'name': app_tags.make_into_username(teacher.user.user),
                'shared': level.shared_with.filter(id=teacher.user.user.id).exists()}

        elif hasattr(userprofile, 'teacher'):
            teacher = userprofile.teacher

            # First get all the students they teach
            valid_recipients['classes'] = []
            classes_taught = Class.objects.filter(teacher=teacher)
            for class_ in classes_taught:
                students = Student.objects.filter(class_field=class_)
                valid_recipients['classes'].append({
                    'name': class_.name, 'id': class_.id,
                    'students': [{
                        'id': student.user.user.id,
                        'name': app_tags.make_into_username(student.user.user),
                        'shared': level.shared_with.filter(id=student.user.user.id).exists()}
                        for student in students]})

            # Then add all the teachers at the same organisation
            fellow_teachers = Teacher.objects.filter(school=teacher.school)
            valid_recipients['teachers'] = [{
                'id': fellow_teacher.user.user.id,
                'name': app_tags.make_into_username(fellow_teacher.user.user),
                'shared': level.shared_with.filter(id=fellow_teacher.user.user.id).exists()}
                for fellow_teacher in fellow_teachers if teacher != fellow_teacher]

    return HttpResponse(json.dumps(valid_recipients), content_type='application/javascript')
def level_moderation(request):
    """ Renders a page with students' scores.

    **Context**

    ``RequestContext``
    ``form``
        Form used to choose a class and level to show. Instance of `forms.ScoreboardForm.`
    ``studentData``
        List of lists containing all the data to be stored in the scoreboard table.
    ``thead``
        List of Strings representing the headers of the scoreboard table.

    **Template:**

    :template:`game/level_moderation.html`
    """

    # Not showing this part to outsiders.
    if not permissions.can_see_level_moderation(request.user):
        return renderError(request, messages.noPermissionLevelModerationTitle(),
                           messages.noPermissionLevelModerationPage())

    teacher = request.user.userprofile.teacher
    classes_taught = Class.objects.filter(teacher=teacher)

    if len(classes_taught) <= 0:
        return renderError(request, messages.noPermissionLevelModerationTitle(),
                           messages.noDataToShowLevelModeration())

    form = LevelModerationForm(request.POST or None, classes=classes_taught)

    student_id = None
    student_dict = None
    level_data = None
    table_headers = None

    if request.method == 'POST':
        if form.is_valid():
            student_id = form.data.get('students')
            class_id = form.data.get('classes')

            if not class_id:
                raise Http404

            # check user has permission to look at this class!
            cl = get_object_or_404(Class, id=class_id)
            if not permissions.can_see_class(request.user, cl):
                return renderError(request,
                                   messages.noPermissionLevelModerationTitle(),
                                   messages.noPermissionLevelModerationClass())

            students = Student.objects.filter(class_field=cl)
            student_dict = {student.id: student.user.user.first_name for student in students}

            if student_id:
                # check student is in class
                student = get_object_or_404(Student, id=student_id)
                if student.class_field != cl:
                    return renderError(request,
                                       messages.noPermissionLevelModerationTitle(),
                                       messages.noPermissionLevelModerationStudent())

                owners = [student.user]

            else:
                owners = [student.user for student in students]

            table_headers = [ugettext('Student'), ugettext('Level name'), ugettext('Shared with'), ugettext('Play'),
                             ugettext('Delete')]
            level_data = []

            for owner in owners:
                for level in Level.objects.filter(owner=owner):
                    users_shared_with = [user for user in level.shared_with.all()
                                         if permissions.can_share_level_with(user, owner.user)
                                         and user != owner.user]

                    if not users_shared_with:
                        shared_str = "-"
                    else:
                        shared_str = ", ".join(app_tags.make_into_username(user) for user in users_shared_with)

                    level_data.append({'student': app_tags.make_into_username(owner.user),
                                       'id': level.id,
                                       'name': level.name,
                                       'shared_with': shared_str})

    context = RequestContext(request, {
        'student_id': student_id,
        'students': student_dict,
        'form': form,
        'levelData': level_data,
        'thead': table_headers,
    })
    return render(request, 'game/level_moderation.html', context_instance=context)
Beispiel #4
0
    def get(self, request, **kwargs):
        """
        Gets a level's information of who it is and can be shared with. How it works:
        - if the requester is a student, get all their classmates and their teacher.
        - if the requester is a teacher, get all their students and their colleagues.
        :param request: GET request made by user.
        :param kwargs: In this case, houses the level ID.
        :return: A HttpResponse with the data in JSON format.
        """
        levelID = kwargs["levelID"]
        level = get_object_or_404(Level, id=levelID)

        self.check_object_permissions(request, level)

        userprofile = request.user.userprofile
        valid_recipients = {}

        # Note: independent users can't share levels so no need to check
        if hasattr(userprofile, "student"):
            student = userprofile.student

            # First get all the student's classmates
            class_ = student.class_field
            classmates = Student.objects.filter(class_field=class_).exclude(
                id=student.id)
            valid_recipients["classmates"] = [{
                "id":
                classmate.user.user.id,
                "name":
                app_tags.make_into_username(classmate.user.user),
                "shared":
                level.shared_with.filter(id=classmate.user.user.id).exists(),
            } for classmate in classmates]

            # Then add their teacher as well
            teacher = class_.teacher
            valid_recipients["teacher"] = {
                "id":
                teacher.user.user.id,
                "name":
                app_tags.make_into_username(teacher.user.user),
                "shared":
                level.shared_with.filter(id=teacher.user.user.id).exists(),
            }

        elif hasattr(userprofile, "teacher"):
            teacher = userprofile.teacher

            # First get all the students they teach
            valid_recipients["classes"] = []
            classes_taught = Class.objects.filter(teacher=teacher)
            for class_ in classes_taught:
                students = Student.objects.filter(class_field=class_)
                valid_recipients["classes"].append({
                    "name":
                    class_.name,
                    "id":
                    class_.id,
                    "students": [{
                        "id":
                        student.user.user.id,
                        "name":
                        app_tags.make_into_username(student.user.user),
                        "shared":
                        level.shared_with.filter(
                            id=student.user.user.id).exists(),
                    } for student in students],
                })

            if not teacher.school:
                valid_recipients["teachers"] = []
            else:
                fellow_teachers = Teacher.objects.filter(school=teacher.school)
                valid_recipients["teachers"] = [{
                    "id":
                    fellow_teacher.user.user.id,
                    "name":
                    app_tags.make_into_username(fellow_teacher.user.user),
                    "shared":
                    level.shared_with.filter(
                        id=fellow_teacher.user.user.id).exists(),
                } for fellow_teacher in fellow_teachers
                                                if teacher != fellow_teacher]

        return HttpResponse(json.dumps(valid_recipients),
                            content_type="application/javascript")
Beispiel #5
0
def level_data_for(level):
    return {
        "name": level.name,
        "owner": app_tags.make_into_username(level.owner.user),
        "id": level.id,
    }
Beispiel #6
0
def level_data_for(level):
    return {
        'name': level.name,
        'owner': app_tags.make_into_username(level.owner.user),
        'id': level.id
    }
def level_data_for(level):
    return {'name': level.name,
            'owner': app_tags.make_into_username(level.owner.user),
            'id': level.id}
def level_data_for(level):
    return {
        "name": level.name,
        "owner": app_tags.make_into_username(level.owner.user),
        "id": level.id,
    }