def setUp(self): self.user = User.objects.create_user( username='******', email='jacob@_', password='******' ) # have to login or Django self.client.session storage won't work self.client.login(username='******', password='******') self.course = Course( title='Great Course', description='the bestest', addedBy=self.user ) self.course.save() self.unit = Unit(title='My Courselet', addedBy=self.user) self.unit.save() self.lesson = Lesson( title='Big Deal', text='very interesting info', addedBy=self.user ) self.lesson.save_root() self.unitLesson = UnitLesson.create_from_lesson( self.lesson, self.unit, order='APPEND' ) self.ulQ = create_question_unit(self.user) self.ulQ2 = create_question_unit( self.user, 'Pretest', 'Scary Question', 'Tell me something.' ) self.json_mixin = JSONBlobMixin() self.fsmDict = dict(name='test', title='try this') self.nodeDict = dict( START=dict(title='start here', path='ct:home', funcName='fsm.fsm_plugin.testme.START'), MID=dict(title='in the middle', path='ct:about', doLogging=True), END=dict(title='end here', path='ct:home') ) self.edgeDict = ( dict(name='next', fromNode='START', toNode='END', title='go go go'), dict(name='select_Lesson', fromNode='MID', toNode='MID', title='go go go'), )
def setUp(self): self.user = User.objects.create_user( username='******', email='jacob@_', password='******' ) # have to login or Django self.client.session storage won't work self.client.login(username='******', password='******') self.course = Course( title='Great Course', description='the bestest', addedBy=self.user ) self.course.save() self.unit = Unit(title='My Courselet', addedBy=self.user) self.unit.save() self.lesson = Lesson( title='Big Deal', text='very interesting info', addedBy=self.user ) self.lesson.save_root() self.unitLesson = UnitLesson.create_from_lesson( self.lesson, self.unit, order='APPEND' ) self.ulQ = create_question_unit(self.user) self.ulQ2 = create_question_unit( self.user, 'Pretest', 'Scary Question', 'Tell me something.' ) self.json_mixin = JSONBlobMixin() self.fsmDict = dict(name='test', title='try this') self.nodeDict = dict( START=dict(title='start here', path='ct:home', funcName='fsm.fsm_plugin.testme.START'), MID=dict(title='in the middle', path='ct:about', doLogging=True), END=dict(title='end here', path='ct:home') ) self.edgeDict = ( dict(name='next', fromNode='START', toNode='END', title='go go go'), dict(name='select_Lesson', fromNode='MID', toNode='MID', title='go go go'), )
def save(self, unit, user, ul, commit=True): should_create_ul = not self.instance.id self.instance.text = self.cleaned_data['answer'] self.instance.title = 'Answer' self.instance.addedBy = user self.instance.kind = Lesson.ANSWER self.instance.save_root() if should_create_ul: ul = UnitLesson.create_from_lesson(self.instance, unit, kind=UnitLesson.ANSWERS, parent=ul) lesson = super(CreateEditUnitAnswerForm, self).save(commit) return lesson
def save(self, unit, user, ul, commit=True): should_create_ul = not self.instance.id self.instance.text = self.cleaned_data['answer'] self.instance.title = 'Answer' self.instance.addedBy = user self.instance.kind = Lesson.ANSWER self.instance.save_root() if should_create_ul: ul = UnitLesson.create_from_lesson(self.instance, unit, kind=UnitLesson.ANSWERS, parent=ul) lesson = super(CreateEditUnitAnswerForm, self).save(commit) return lesson
def form_valid(self, form): courslet = self.get_courslet() unit = courslet.unit self.object = Lesson(title=form.cleaned_data['title'], text='', kind=Lesson.ORCT_QUESTION, addedBy=self.request.user) self.object.save() self.object.treeID = self.object.pk self.object.save() # create UnitLesson with blank answer for this unit unit_lesson = UnitLesson.create_from_lesson(self.object, unit, order='APPEND', addAnswer=False) self.object.unit_lesson = unit_lesson cache.delete(memoize.cache_key('get_units_by_courselet', courslet)) return redirect(self.get_success_url())
def setUp(self): self.user = User.objects.create_user(username="******", email="jacob@_", password="******") # have to login or Django self.client.session storage won't work self.client.login(username="******", password="******") self.course = Course(title="Great Course", description="the bestest", addedBy=self.user) self.course.save() self.unit = Unit(title="My Courselet", addedBy=self.user) self.unit.save() self.lesson = Lesson(title="Big Deal", text="very interesting info", addedBy=self.user) self.lesson.save_root() self.unitLesson = UnitLesson.create_from_lesson(self.lesson, self.unit, order="APPEND") self.ulQ = create_question_unit(self.user) self.ulQ2 = create_question_unit(self.user, "Pretest", "Scary Question", "Tell me something.") self.json_mixin = JSONBlobMixin() self.fsmDict = dict(name="test", title="try this") self.nodeDict = dict( START=dict(title="start here", path="ct:home", funcName="fsm.fsm_plugin.testme.START"), MID=dict(title="in the middle", path="ct:about", doLogging=True), END=dict(title="end here", path="ct:home"), ) self.edgeDict = ( dict(name="next", fromNode="START", toNode="END", title="go go go"), dict(name="select_Lesson", fromNode="MID", toNode="MID", title="go go go"), )
def perform_update(self, serializer): chat_id = self.request.data.get('chat_id') message = self.get_object() chat = Chat.objects.get(id=chat_id, user=self.request.user) activity = chat.state and chat.state.activity is_in_node = lambda node: message.chat.state.fsmNode.name == node # Check if message is not in current chat if not message.chat or message.chat != chat: return # Chat add unit lesson if is_chat_add_lesson(message): message.chat = chat text = self.request.data.get('text') option = self.request.data.get('option') course_unit = message.chat.enroll_code.courseUnit unit = course_unit.unit if message.input_type == 'options' and is_in_node('HAS_UNIT_ANSWER'): message = self.next_handler.next_point( current=message.content, chat=chat, message=message, request=self.request ) chat.next_point = message chat.save() serializer.save(chat=chat, timestamp=timezone.now()) if is_in_node('GET_UNIT_NAME_TITLE'): if course_unit and unit: if not message.content_id: lesson = Lesson.objects.create(title=text, addedBy=self.request.user, kind=Lesson.ORCT_QUESTION, text='') lesson.treeID = lesson.id lesson.save() ul = UnitLesson.create_from_lesson( lesson=lesson, unit=unit, kind=UnitLesson.COMPONENT, order='APPEND', ) chat.state.unitLesson = ul chat.state.save() else: ul = message.content if not message.timestamp: serializer.save( content_id=ul.id, timestamp=timezone.now(), chat=chat, text=text, contenttype='unitlesson' ) else: serializer.save() if is_in_node('GET_UNIT_QUESTION'): ul = message.content ul.lesson.text = text ul.lesson.save() if not message.timestamp: serializer.save( content_id=ul.id, timestamp=timezone.now(), chat=chat, contenttype='unitlesson', text=text ) else: serializer.save() if is_in_node('GET_UNIT_ANSWER'): # create answer ul = message.content if not message.timestamp: answer = Lesson.objects.create( title='Answer', text=text, addedBy=self.request.user, kind=Lesson.ANSWER, ) answer.save_root() unit_lesson_answer = UnitLesson.create_from_lesson( unit=ul.unit, lesson=answer, parent=ul, kind=UnitLesson.ANSWERS ) # chat.next_point = message chat.save() serializer.save(content_id=ul.id, timestamp=timezone.now(), chat=chat, contenttype='unitlesson', text=text) else: serializer.save() if is_in_node('GET_HAS_UNIT_ANSWER'): yes_no_map = { 'yes': True, 'no': False } ul = message.content has_answer = yes_no_map.get(self.request.data.get('option')) if has_answer is None: raise ValueError("Recieved not valid response from user") ul.lesson.kind = Lesson.ORCT_QUESTION if has_answer else Lesson.BASE_EXPLANATION ul.lesson.save() message.text = self.request.data.get('option') message.save() if message.input_type == 'text' and not is_chat_add_lesson(message): message.chat = chat text = self.request.data.get('text') if message.lesson_to_answer.sub_kind == Lesson.EQUATION: text = text.strip("$") text = '.. math:: ' + text resp = StudentResponse(text=text) # convert base64 attachment string to django File data_attachment = self.request.data.get('attachment') if data_attachment and data_attachment.startswith('data:image'): format, image_string = data_attachment.split(';base64,') extension = format.split('/')[-1].split('+')[0] name = '{}.{}'.format('canvas', extension) resp.attachment = ContentFile(base64.b64decode(image_string), name=name) if not message.content_id: resp.lesson = message.lesson_to_answer.lesson resp.unitLesson = message.lesson_to_answer resp.course = message.chat.enroll_code.courseUnit.course resp.author = self.request.user resp.activity = activity resp.is_test = chat.is_test resp.is_preview = chat.enroll_code.isPreview resp.sub_kind = resp.lesson.sub_kind else: resp = message.content resp.text = text resp.is_trial = chat.is_trial resp.save() if not message.timestamp: message.content_id = resp.id chat.next_point = message chat.last_modify_timestamp = timezone.now() chat.save() serializer.save(content_id=resp.id, timestamp=timezone.now(), chat=chat) else: serializer.save() message_is_response = message.contenttype == 'response' lesson_has_sub_kind = message.lesson_to_answer and message.lesson_to_answer.sub_kind content_is_not_additional = not message.content and not message.is_additional mc_selfeval = None if message_is_response and lesson_has_sub_kind and content_is_not_additional: resp_text = '' if message.lesson_to_answer.sub_kind == Lesson.MULTIPLE_CHOICES: selected_items = self.request.data.get('selected') try: selected = selected_items[str(message.id)]['choices'] except KeyError: # here request.data is like {"option":1,"chat_id":9,"selected":{"116":{"choices":[0]}}} selected_msg_ids = self.request.data.get( 'selected' ).keys() # selected_messages == tuple with keys of this dict {"116":{"choices":[0]}} - it will be ("116",) msg_ids = Message.objects.filter(id__in=selected_msg_ids, chat=chat).values_list('id', flat=True) correct_ids = set(msg_ids).intersection( set(int(i) for i in selected_items.keys()) ) selected_choices = [] for i in correct_ids: selected_choices.append(selected_items[str(i)]['choices']) selected = chain(*selected_choices) resp_text = '[selected_choices] ' + ' '.join(str(i) for i in selected) correct_choices = set([_[0] for _ in message.lesson_to_answer.lesson.get_correct_choices()]) selected_choices = set([_ for _ in chain(*selected_choices)]) if not (correct_choices - selected_choices or correct_choices ^ selected_choices): mc_selfeval = StudentResponse.CORRECT elif selected_choices & correct_choices: mc_selfeval = StudentResponse.CLOSE else: mc_selfeval = StudentResponse.DIFFERENT resp = StudentResponse(text=resp_text) # tes, preview flags resp.is_test = chat.is_test resp.selfeval = mc_selfeval or None resp.is_preview = chat.enroll_code.isPreview resp.is_trial = chat.is_trial resp.kind = message.lesson_to_answer.kind resp.sub_kind = message.lesson_to_answer.sub_kind resp.lesson = message.lesson_to_answer.lesson resp.unitLesson = message.lesson_to_answer resp.course = message.chat.enroll_code.courseUnit.course resp.author = self.request.user resp.activity = activity resp.save() if not message.timestamp: serializer.save(content_id=resp.id, timestamp=timezone.now(), chat=chat, response_to_check=resp) else: serializer.save() return if message.input_type == 'options' and message.kind != 'button': if ( message.contenttype == 'uniterror' and 'selected' in self.request.data ): # user selected error model message.chat = chat try: selected = self.request.data.get( 'selected' )[str(message.id)]['errorModel'] except KeyError: selected = [] uniterror = message.content uniterror.save_response(user=self.request.user, response_list=selected) if not message.chat.is_live: get_additional_messages(uniterror.response, chat) chat.next_point = self.next_handler.next_point( current=message.content, chat=chat, message=message, request=self.request ) chat.last_modify_timestamp = timezone.now() chat.save() serializer.save(chat=chat) elif message.contenttype == 'NoneType' and message.kind == 'abort': # user selected abort model message.chat = chat try: selected = self.request.data.get( 'selected' )[str(message.id)]['errorModel'] except KeyError: selected = [] if not message.chat.is_live and selected: get_help_messages(chat) chat.next_point = self.next_handler.next_point( current=message.content, chat=chat, message=message, request=self.request ) chat.last_modify_timestamp = timezone.now() chat.save() serializer.save(chat=chat) elif message.content_id and not message.student_error: # confidence and selfeval message.chat = chat opt_data = self.request.data.get('option') resp = message.content if chat.state and chat.state.fsmNode.node_name_is_one_of('GET_CONFIDENCE'): resp.confidence = opt_data text = resp.get_confidence_display() else: resp.selfeval = opt_data text = resp.get_selfeval_display() # FIX if response was correct - user will not go to `else` section and response status should be set if resp.selfeval == StudentResponse.CORRECT: resp.status = DONE_STATUS message.text = text resp.save() chat.next_point = message chat.last_modify_timestamp = timezone.now() chat.save() serializer.save(content_id=resp.id, chat=chat, text=text) else: # message.chat = chat selfeval = self.request.data.get('option') resp = message.student_error resp.status = selfeval resp.save() # pass status to main response resp.response.status = selfeval resp.response.save() chat.next_point = message chat.last_modify_timestamp = timezone.now() chat.save() message.text = selfeval message.save() serializer.save(text=selfeval, chat=chat) if message.kind == 'button' and not (message.content_id and message.content and message.content.sub_kind): chat.last_modify_timestamp = timezone.now() chat.next_point = self.next_handler.next_point( current=message.content, chat=chat, message=message, request=self.request, ) chat.save()
def lesson_answer_canvas(unit, lesson_question_canvas): return UnitLesson.create_from_lesson(unit=unit, lesson=lesson_question_canvas, addAnswer=True)
def lesson_answer(unit, lesson_question): return UnitLesson.create_from_lesson(unit=unit, lesson=lesson_question, addAnswer=True, order=1)
def lesson_answer_canvas(unit, lesson_question_canvas): return UnitLesson.create_from_lesson(unit=unit, lesson=lesson_question_canvas, addAnswer=True)
def lesson_answer(unit, lesson_question): return UnitLesson.create_from_lesson(unit=unit, lesson=lesson_question, addAnswer=True, order=1)