Newer
Older
# -*- coding: utf-8 -*-
from __future__ import division
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
ifaint
committed
from django.utils.translation import ugettext_lazy as _ , string_concat
from django.utils.functional import lazy
from django.shortcuts import ( # noqa
render, get_object_or_404, redirect)
from django.contrib import messages
from django.core.exceptions import (
PermissionDenied, SuspiciousOperation,
ObjectDoesNotExist)
from django.db import transaction
from django.utils.safestring import mark_safe
mark_safe_lazy = lazy(mark_safe, six.text_type)
from relate.utils import StyledForm, local_now, as_local_time
from course.constants import (
flow_permission,
FLOW_SESSION_EXPIRATION_MODE_CHOICES,
is_expiration_mode_allowed,
flow_rule_kind,
grade_aggregation_strategy,
GRADE_AGGREGATION_STRATEGY_CHOICES
)
FlowSession, FlowPageData, FlowPageVisit,
FlowPageVisitGrade,
get_feedback_for_grade,
GradeChange, update_bulk_feedback)
from course.utils import (
FlowContext, FlowPageContext, PageOrdinalOutOfRange,
instantiate_flow_page_with_ctx,
course_view, render_course_page,
get_session_start_rule,
get_session_access_rule,
get_session_grading_rule,
FlowSessionGradingRule)
from course.views import get_now_or_fake_time
# {{{ grade page visit
def grade_page_visit(visit, visit_grade_model=FlowPageVisitGrade,
grade_data=None, graded_at_git_commit_sha=None):
if not visit.is_submitted_answer:
raise RuntimeError(_("cannot grade ungraded answer"))
flow_session = visit.flow_session
course = flow_session.course
page_data = visit.page_data
most_recent_grade = visit.get_most_recent_grade()
if most_recent_grade is not None and grade_data is None:
grade_data = most_recent_grade.grade_data
from course.content import (
get_course_repo,
get_course_commit_sha,
get_flow_desc,
get_flow_page_desc,
instantiate_flow_page)
repo = get_course_repo(course)
course_commit_sha = get_course_commit_sha(
course, flow_session.participation)
flow_desc = get_flow_desc(repo, course,
page_desc = get_flow_page_desc(
flow_session.flow_id,
flow_desc,
page_data.group_id, page_data.page_id)
page = instantiate_flow_page(
location="flow '%s', group, '%s', page '%s'"
% (flow_session.flow_id, page_data.group_id, page_data.page_id),
repo=repo, page_desc=page_desc,
assert page.expects_answer()
if not page.is_answer_gradable():
return
from course.page import PageContext
grading_page_context = PageContext(
course=course,
repo=repo,
flow_session=flow_session)
answer_feedback = page.grade(
grading_page_context, visit.page_data.data,
visit.answer, grade_data=grade_data)
grade = visit_grade_model()
grade.visit = visit
grade.grade_data = grade_data
grade.max_points = page.max_points(visit.page_data)
grade.graded_at_git_commit_sha = graded_at_git_commit_sha
bulk_feedback_json = None
if answer_feedback is not None:
grade.correctness = answer_feedback.correctness
grade.feedback, bulk_feedback_json = answer_feedback.as_json()
grade.save()
update_bulk_feedback(page_data, grade, bulk_feedback_json)
# }}}
# {{{ start flow
def start_flow(repo, course, participation, flow_id, flow_desc,
access_rules_tag, now_datetime):
from course.content import get_course_commit_sha
course_commit_sha = get_course_commit_sha(course, participation)
session = FlowSession(
course=course,
participation=participation,
active_git_commit_sha=course_commit_sha.decode(),
flow_id=flow_id,
in_progress=True,
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
access_rules_tag=access_rules_tag)
session.save()
# Create flow grading opportunity. This makes the flow
# show up in the grade book.
from course.utils import get_flow_rules
from course.models import get_flow_grading_opportunity
for grading_rule in get_flow_rules(
flow_desc, flow_rule_kind.grading, participation,
flow_id, now_datetime, consider_exceptions=False):
identifier = getattr(grading_rule, "grade_identifier", None)
if identifier is not None:
get_flow_grading_opportunity(
course, flow_id, flow_desc,
FlowSessionGradingRule(
grade_identifier=identifier,
grade_aggregation_strategy=getattr(
grading_rule, "grade_aggregation_strategy"),
))
# will implicitly modify and save the session if there are changes
from course.content import adjust_flow_session_page_data
adjust_flow_session_page_data(repo, session,
course.identifier, flow_desc, course_commit_sha)
return session
# }}}
# {{{ finish flow
def get_flow_session_graded_answers_qset(flow_session):
from django.db.models import Q
qset = (FlowPageVisit.objects
.filter(flow_session=flow_session)
.filter(Q(answer__isnull=False) | Q(is_synthetic=True)))
if not flow_session.in_progress:
Loading
Loading full blame...