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 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')

        
        
        
        
        
        
        
        
        
        
        
        
Exemple #3
0
 def get_answers(cls,id):
     answers = []
     
     wrong = Answers.by({'question_sets_id':id,'is_correct':False}).all()
     random.shuffle(wrong) # randomize wrong
     wrong = wrong[:3] # get max of first 3 wrong in randomized list
     correct = Answers.by({'question_sets_id':id,'is_correct':True}).first()
     answers = wrong + [correct] # add correct to list
     random.shuffle(answers) # randomize corect in list
     
     # sort em'. if all positions set as same value, all stay randomized from before.  cool eh?
     answers = sorted(answers, key=lambda a: a.position)  
     answer_set = []
     for a in answers:
         answer_set.append({'content':a.answer,'id':a.id,'answered':0})
     return answer_set
 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 _transaction(self, question_set, fields):
         
        for key,v in fields.iteritems():
            if Validate.sanatize(v) != '':
               
                parts = key.split('_')
            
                if parts[0] == 'answerhelp':
                    question_set.answer_help = v

                if parts[0] == 'correctanswer' and not key.endswith('_index'):
                    if parts[1] == 'old':
                        a = Answers.by(parts[2]).first()
                        a.answer = v
                        a.position=fields[key + '_index']
                    else:
                        a = Answers(question_sets_id=question_set.id, answer=v, is_correct=True, position=fields[key + '_index'])
                        DBSession.add(a)
                
                if parts[0] == 'wronganswer' and not key.endswith('_index'):
                    if parts[1] == 'old':
                        a = Answers.by(parts[2]).first()
                        a.answer = v
                        a.position = fields[key + '_index']
                    else:
                        a = Answers(question_sets_id=question_set.id, answer=v, is_correct=False, position=fields[key + '_index'])
                        DBSession.add(a)
                        
                if parts[0] == 'question':
                    if parts[1] == 'old':
                        a = Questions.by(parts[2]).first()
                        a.question = v
                    else:
                        a = Questions(question=v, question_sets_id=question_set.id)
                        DBSession.add(a)
        
            DBSession.flush()
        transaction.commit()
        
        
        
        
        
        
        
        
        
        
Exemple #6
0
 def check_answer_byid(cls,id,answer):
     correct_answer = Answers.by({'question_sets_id':id, 'is_correct':True}).first()
     return (correct_answer.id == int(answer))