def edit_question(self): self.response['category'] = self.request.matchdict['category'] Categories.by(self.response['category'], user=self.request.user, permission=ACL.EDIT, strict=True).first() # security question = self.request.matchdict['id'] self.response['option'] = question if question == 'new': if 'form.submit' in self.request.params or 'form.submit.next' in self.request.params: qs = QuestionSets(category_id=int(self.response['category'])) DBSession.add(qs) DBSession.flush() qs = QuestionSets.by(None, sort='id desc').first() id = qs.id self._transaction(qs, self.request.params) if 'form.submit.next' in self.request.params: return HTTPFound(location=self.request.application_url + self.request.path) return HTTPFound(location=self.request.application_url + self.request.path + '/../' + str(id)) else: qs = QuestionSets.by(question).first() q = Questions.by({'question_sets_id':qs.id}).all() wa = Answers.by({'question_sets_id':qs.id,'is_correct':False}, sort='position asc').all() ca = Answers.by({'question_sets_id':qs.id,'is_correct':True}).first() self.response['question_sets'] = qs self.response['questions'] = q self.response['wrong_answers'] = wa self.response['correct_answer'] = ca if 'form.submit' in self.request.params or 'form.submit.next' in self.request.params: self.notify('Changes saved!') self._transaction(qs, self.request.params) if 'form.submit.next' in self.request.params: return HTTPFound(location=self.request.application_url + self.request.path + '/../new') return HTTPFound(location=self.request.application_url + self.request.path) return self.template('/edit-question.pt', theme='AdminPanel')
def _generate_test(self): category = Categories.by(self.category_id).first() last_test = Tests.by({'category':category.name, 'alias':self.alias},sort='id desc').first() # Create New Test test = Tests(alias=self.alias, category=category.name, d2l_folder=category.d2l_folder, used_accessibility_view=self.used_accessibility_view) # Copy values at time of generation, this in case values change in the future, personal results aren't effected. test.wrong_answer_time_penalty = category.wrong_answer_time_penalty test.max_wrong_answer_allowed = category.max_wrong_answer_allowed test.question_time_allowed = category.question_time_allowed DBSession.add(test) DBSession.flush() # Get and Randomize Questions questionsets = QuestionSets.by({'category_id':self.category_id}).all() random.shuffle(questionsets) # randomize first to guarentee all questions questionsets = questionsets[0:category.playable_questions] # limit randomized # Setup Unfinished Questions for questionset in questionsets: question = self._get_question_variation(questionset, last_test) result = TestsResults(tests_id=test.id, question_sets_id=questionset.id, question=question, answer_choices=TestManager.get_answers(questionset.id) ) DBSession.add(result) # okay below DBSession.flush() user = Users.by({'alias':self.alias}).first() user.current_test = test.id transaction.commit()
def export_category(self): category_id = int(self.request.matchdict['category']) category = Result2Dict(Categories.by(category_id).first()) sa_qs = QuestionSets.by({'category_id':category_id}).all() question_sets = [] for qs in sa_qs: questions = [] answers = [] # Get QuestionSet Transform d_qs = Result2Dict(qs) # Get All Questions in QuestionSet and Transform sa_q = Questions.by({'question_sets_id':qs.id}).all() for q in sa_q: questions.append(Result2Dict(q)) d_qs['questions'] = questions # Get All Answers in QuestionSet and Transform sa_a = Answers.by({'question_sets_id':qs.id}).all() for a in sa_a: answers.append(Result2Dict(a)) d_qs['answers'] = answers question_sets.append(d_qs) return self._export_zip(json.dumps({'category' : category, 'question_sets' : question_sets}, sort_keys=True, indent=4, separators=(',', ': ') ))
def edit_questions(self): category = self.request.matchdict['category'] self.response['active_category'] = Categories.by(category, user=self.request.user, permission=ACL.EDIT, strict=True).first() question_sets = QuestionSets.by({'category_id':category}).all() self.response['question_sets'] = [] i = 1 for qs in question_sets: self.response['question_sets'].append({ 'rank' : i, 'id' : qs.id, 'answer_help' : qs.answer_help, 'questions' : Questions.by({'question_sets_id':qs.id}).all(), 'wrong_answers' : Answers.by({'question_sets_id':qs.id,'is_correct':False}).all(), 'correct_answer' : Answers.by({'question_sets_id':qs.id,'is_correct':True}).first(), }) i += 1 return self.template('/edit-questions.pt', theme='AdminPanel')
def get_question_scores(self, result, total_results, max_allowed_wrong, max_allowed_duration): percentage = TestManager.score_percentage(result.correctly_answered, result.wrong_attempts, max_allowed_wrong, total_results, result.duration, max_allowed_duration ) competitive = TestManager.score_competitive(result.correctly_answered, result.wrong_attempts, max_allowed_wrong, total_results, result.duration, max_allowed_duration ) answer_help = None try: try: # if questionset was removed, this will still allow people to view old tests. answer_help = QuestionSets.by(result.question_sets_id).first().answer_help except exc.SQLAlchemyError as e: pass # defensive except Exception as e: pass # defensive return { 'question' : str(result.question), 'question_textonly' : RemoveImages(result.question), 'correctly_answered' : result.correctly_answered, 'wrong_attempts' : result.wrong_attempts, 'duration' : result.duration, 'duration_passed_fmt' : Seconds2Str((max_allowed_duration-result.duration)), 'duration_remaining_fmt' : Seconds2Str(result.duration), 'percentage' : percentage, 'competitive' : competitive, 'answer_help': str(answer_help), 'attempted': result.attempted, 'answer_choices': str(result.get_answers()), 'had_wrong_attempts': (result.wrong_attempts!=0 and result.attempted), 'had_more_than_one_wrong': (result.wrong_attempts>1), 'had_no_time_remaining': (result.duration<1 and result.attempted), }
def result(self): from quizsmith.app.views.leaderboard import LeaderBoard category = self._secure_get_category(int(self.request.matchdict['id'])) self.response['question'] = None self.response['wrong_attempts'] = 0 self.response['duration'] = 0 test = Tests.by(self.request.user.current_test).first() result = TestsResults.by({'tests_id':self.request.user.current_test, 'attempted':True}, sort='id desc').first() results = TestsResults.by({'tests_id':self.request.user.current_test}).all() questionset = QuestionSets.by(result.question_sets_id).first() self.response['answer_help'] = questionset.answer_help self.response['result'] = Result2Dict(result,purge=['id','tests_id','question_sets_id']) final = self._test_calculator(test,results,result) self.response['percentage'] = final['percentage'] self.response['competitive'] = final['competitive'] self.response['ranking'] = LeaderBoard.get_ranking(test) transaction.commit() return self.response
def edit_category(self): category_id = self.request.matchdict['category'] self.response['category_id'] = category_id self.response['version'] = Addons.get_version('QuizSmith Core') self.response['d2l_on'] = Validate.bool(self.settings('d2l_on')) if 'form.submit' in self.request.params or 'form.submit.questions' in self.request.params: active = None if category_id == 'add': active = Categories(name='New Category') editors = [] for group in self.request.user.get_groups(): editors.append(str(group.id)) reviewers = [] for group in self.request.user.get_groups(): reviewers.append(str(group.id)) active.set_groups([], editors, reviewers) DBSession.add(active) DBSession.flush() category_id = str(active.id) else: active = Categories.by(category_id, sort='position asc', user=self.request.user, permission=ACL.EDIT).first() active.name = self.request.params.get('category.name','') active.category_intro = self.request.params.get('category.intro','') active.playable_questions = self.request.params.get('category.playable_questions',10) active.wrong_answer_time_penalty = self.request.params.get('category.wrong_answer_time_penalty',5) active.max_wrong_answer_allowed = self.request.params.get('category.max_wrong_answer_allowed',2) active.question_time_allowed = self.request.params.get('category.question_time_allowed',30) active.transition_in = self.request.params.get('category.transition_in','Random') active.transition_out = self.request.params.get('category.transition_out','Random') active.d2l_folder = self.request.params.get('category.d2l_folder','') assesment_data = [] for key,v in self.request.params.iteritems(): if key.startswith('assessment'): field_data = key.split('.') row = {} if not any(a['id'] == field_data[-1] for a in assesment_data): assesment_data.append(row) else: row = filter(lambda x: x['id'] == field_data[-1], assesment_data)[0] row['id'] = field_data[-1] if v.isdigit(): row[field_data[1]] = int(v) else: row[field_data[1]] = str(v) active.set_assessments(assesment_data) editors = [] if self.request.params.getall('category.editable'): editors = self.request.params.getall('category.editable') else: for g in active.groups: if g.edit: editors.append(str(g.groups_id)) reviewers = [] if self.request.params.getall('category.reviewable'): reviewers = self.request.params.getall('category.reviewable') else: for g in active.groups: if g.edit: editors.append(str(g.groups_id)) active.set_groups(self.request.params.getall('category.playable'), editors, reviewers) DBSession.flush() transaction.commit() self.notify('Changes saved!') if 'form.submit.questions' in self.request.params: return HTTPFound(location=self.request.application_url + '/edit/category/' + category_id + '/questions') return HTTPFound(location=self.request.application_url + '/edit/category/' + category_id) elif category_id == 'add': self.response['active_category'] = Categories(name='New Category') else: self.response['active_category'] = Categories.by(category_id, sort='position asc', user=self.request.user, permission=ACL.EDIT, strict=True).first() self.response['transitions_in'] = self.response['active_category'].transition_in self.response['transitions_out'] = self.response['active_category'].transition_out self.response['transitions'] = Transitions.all() self.response['questions'] = QuestionSets.by({'category_id':category_id}).count() self.response['all_edit_groups'] = Groups.by({'edit':True}).all() self.response['all_play_groups'] = Groups.by({'play':True}).all() self.response['all_review_groups'] = Groups.by({'review':True}).all() self.response['play_groups'] = [] self.response['edit_groups'] = [] self.response['review_groups'] = [] if self.response['active_category'].groups: for categorygroup in self.response['active_category'].groups: group = Groups.by(categorygroup.groups_id).first() if categorygroup.edit: self.response['edit_groups'].append(group.name) if categorygroup.play: self.response['play_groups'].append(group.name) if categorygroup.review: self.response['review_groups'].append(group.name) return self.template('/edit-category.pt', theme='AdminPanel')