-
Notifications
You must be signed in to change notification settings - Fork 73
/
exercise_forms.py
289 lines (236 loc) · 9.47 KB
/
exercise_forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import logging
from typing import Any, Dict, List
from django import forms
from django.utils.translation import gettext_lazy as _
from course.models import CourseModule, LearningObjectCategory
from exercise.models import LearningObject, CourseChapter, BaseExercise, \
LTIExercise, StaticExercise, ExerciseWithAttachment, RevealRule, \
LTI1p3Exercise
from lib.widgets import DateTimeLocalInput
from .course_forms import FieldsetModelForm
from exercise.exercisecollection_models import ExerciseCollection
logger = logging.getLogger("aplus.exercise")
COMMON_FIELDS = [
'status',
'audience',
'category',
'course_module',
'parent',
'order',
'url',
]
SERVICE_FIELDS = [
'service_url',
'name',
'description',
]
EXERCISE_FIELDS = [
'max_submissions',
'max_points',
'difficulty',
'points_to_pass',
'allow_assistant_viewing',
'allow_assistant_grading',
'min_group_size',
'max_group_size',
'model_answers',
'templates',
'grading_mode',
]
class LearningObjectMixin:
def init_fields(self, **kwargs):
self.lobject = kwargs.get('instance')
self.fields["category"].queryset = LearningObjectCategory.objects.filter(
course_instance=self.lobject.course_instance)
self.fields["course_module"].queryset = CourseModule.objects.filter(
course_instance=self.lobject.course_instance)
self.fields["parent"].queryset = LearningObject.objects\
.exclude(id=self.lobject.id)\
.filter(course_module=self.lobject.course_module)
self.fields['parent'].disabled = True
@property
def remote_service_head(self):
return True
def get_hierarchy_fieldset(self):
return { 'legend':_('HIERARCHY'), 'fields':self.get_fields('status',
'audience', 'category','course_module','parent','order','url') }
def get_content_fieldset(self, *add):
return { 'legend':_('CONTENT'), 'fields':self.get_fields('name',
'description', *add) }
class CourseChapterForm(LearningObjectMixin, FieldsetModelForm):
class Meta:
model = CourseChapter
fields = COMMON_FIELDS + SERVICE_FIELDS + [
'use_wide_column',
'generate_table_of_contents'
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.init_fields(**kwargs)
def get_fieldsets(self):
return [
self.get_hierarchy_fieldset(),
self.get_content_fieldset(
'use_wide_column', 'generate_table_of_contents'),
]
class RevealRuleForm(FieldsetModelForm):
# This form is only used internally by BaseExerciseForm.
class Meta:
model = RevealRule
fields = ['trigger', 'delay_minutes', 'time', 'currently_revealed', 'show_zero_points_immediately']
widgets = {'time': DateTimeLocalInput}
def __init__(self, *args: Any, is_submission_feedback: bool = False, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.fields['trigger'].widget.attrs['data-trigger'] = True
zero_feedback_triggers = [
RevealRule.TRIGGER.DEADLINE.value,
RevealRule.TRIGGER.DEADLINE_ALL.value,
RevealRule.TRIGGER.DEADLINE_OR_FULL_POINTS.value,
RevealRule.TRIGGER.TIME.value,
RevealRule.TRIGGER.MANUAL.value,
RevealRule.TRIGGER.COMPLETION.value,
] if is_submission_feedback else []
self.fields['show_zero_points_immediately'].widget.attrs['data-visible-triggers'] = zero_feedback_triggers
# Visibility rules for the form fields. Each of the following fields is
# only visible when one of their specified values is selected from the
# trigger dropdown. See edit_model.html.
self.fields['currently_revealed'].widget.attrs['data-visible-triggers'] = [
RevealRule.TRIGGER.MANUAL.value,
]
self.fields['time'].widget.attrs['data-visible-triggers'] = [
RevealRule.TRIGGER.TIME.value,
]
self.fields['delay_minutes'].widget.attrs['data-visible-triggers'] = [
RevealRule.TRIGGER.DEADLINE.value,
RevealRule.TRIGGER.DEADLINE_ALL.value,
RevealRule.TRIGGER.DEADLINE_OR_FULL_POINTS.value,
]
def clean(self) -> Dict[str, Any]:
result = super().clean()
errors = {}
trigger = self.cleaned_data.get('trigger')
if trigger == RevealRule.TRIGGER.TIME:
time = self.cleaned_data.get('time')
if time is None:
errors['time'] = _(
'ERROR_REQUIRED_WITH_SELECTED_TRIGGER'
)
if errors:
raise forms.ValidationError(errors)
return result
class BaseExerciseForm(LearningObjectMixin, FieldsetModelForm):
class Meta:
model = BaseExercise
fields = COMMON_FIELDS + SERVICE_FIELDS + EXERCISE_FIELDS
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.init_fields(**kwargs)
# This form contains two embedded RevealRuleForms.
self.submission_feedback_form = RevealRuleForm(
data=kwargs.get('data'),
instance=self.instance.active_submission_feedback_reveal_rule,
prefix='submission_feedback',
is_submission_feedback=True,
)
self.model_solutions_form = RevealRuleForm(
data=kwargs.get('data'),
instance=self.instance.active_model_solutions_reveal_rule,
prefix='model_solutions',
is_submission_feedback=False,
)
def get_fieldsets(self) -> List[Dict[str, Any]]:
return [
self.get_hierarchy_fieldset(),
self.get_content_fieldset('model_answers', 'templates'),
{ 'legend':_('GRADING'), 'fields':self.get_fields('max_submissions',
'max_points','points_to_pass', 'difficulty',
'allow_assistant_viewing','allow_assistant_grading','grading_mode') },
{ 'legend':_('GROUPS'), 'fields':self.get_fields('min_group_size',
'max_group_size') },
{ 'legend':_('REVEAL_SUBMISSION_FEEDBACK'), 'fields':self.submission_feedback_form },
{ 'legend':_('REVEAL_MODEL_SOLUTIONS'), 'fields':self.model_solutions_form },
]
def is_valid(self) -> bool:
return (
super().is_valid()
and self.submission_feedback_form.is_valid()
and self.model_solutions_form.is_valid()
)
def save(self, *args: Any, **kwargs: Any) -> Any:
# Save the reveal rules only if they have been changed.
# If they were not changed, we can keep using the default rule and
# there's no need to save a new RevealRule.
if self.submission_feedback_form.has_changed():
self.instance.submission_feedback_reveal_rule = (
self.submission_feedback_form.save(*args, **kwargs)
)
if self.model_solutions_form.has_changed():
self.instance.model_solutions_reveal_rule = (
self.model_solutions_form.save(*args, **kwargs)
)
return super().save(*args, **kwargs)
class LTIExerciseForm(BaseExerciseForm):
class Meta:
model = LTIExercise
fields = COMMON_FIELDS + SERVICE_FIELDS + EXERCISE_FIELDS + [
'lti_service',
'context_id',
'resource_link_id',
'resource_link_title',
'aplus_get_and_post',
'open_in_iframe',
]
@property
def remote_service_head(self):
return False
def get_content_fieldset(self, *add):
return super().get_content_fieldset('lti_service','context_id',
'resource_link_id','resource_link_title',
'aplus_get_and_post','open_in_iframe','service_url')
class LTI1p3ExerciseForm(BaseExerciseForm):
class Meta:
model = LTI1p3Exercise
fields = COMMON_FIELDS + SERVICE_FIELDS + EXERCISE_FIELDS + [
'lti_service',
'custom',
'open_in_iframe',
]
@property
def remote_service_head(self) -> bool:
return False
def get_content_fieldset(self, *add) -> Dict[str, Any]:
return super().get_content_fieldset('lti_service', 'custom', 'open_in_iframe')
class ExerciseWithAttachmentForm(BaseExerciseForm):
multipart = True
class Meta:
model = ExerciseWithAttachment
fields = COMMON_FIELDS + SERVICE_FIELDS + EXERCISE_FIELDS + [
'content',
'files_to_submit',
'attachment',
]
def get_content_fieldset(self, *add):
return super().get_content_fieldset(
'content', 'files_to_submit', 'attachment')
class StaticExerciseForm(BaseExerciseForm):
class Meta:
model = StaticExercise
fields = COMMON_FIELDS + EXERCISE_FIELDS + [
'name',
'description',
'exercise_page_content',
'submission_page_content',
]
@property
def remote_service_head(self):
return False
def get_content_fieldset(self, *add):
return super().get_content_fieldset(
'exercise_page_content', 'submission_page_content')
class ExerciseCollectionExerciseForm(BaseExerciseForm):
class Meta:
model = ExerciseCollection
fields = COMMON_FIELDS + EXERCISE_FIELDS + SERVICE_FIELDS + \
['target_category']
def get_content_fieldset(self, *add):
return super().get_content_fieldset('target_category')