예제 #1
0
 def validate_num_preguntas(self, field):
     """ Function to verify that there are enough questions of the subject and type given to generate the exam """
     asignatura = Asignaturas.objects(asignatura=self.asignatura.data).first()
     tipo = self.tipo_examen.data
     num_preguntas = self.num_preguntas.data
     preguntas = Preguntas.user_objects(asignatura=asignatura.get_id(), tipo=tipo).count()
     if preguntas < num_preguntas:
         raise validators.ValidationError(u'No existen suficientes preguntas de la asignatura para el tipo indicado.')
예제 #2
0
 def validate_num_preguntas(self, field):
     """ Function to verify that there are enough questions of the subject and type given to generate the exam """
     asignatura = Asignaturas.objects(
         asignatura=self.asignatura.data).first()
     tipo = self.tipo_examen.data
     num_preguntas = self.num_preguntas.data
     preguntas = Preguntas.user_objects(asignatura=asignatura.get_id(),
                                        tipo=tipo).count()
     if preguntas < num_preguntas:
         raise validators.ValidationError(
             u'No existen suficientes preguntas de la asignatura para el tipo indicado.'
         )
예제 #3
0
def genera_examen_view():
    """
    Function for generating random tests
    """    
    form = GeneraExamenForm(request.form)   

    asig=login.current_user.get_asignaturas()
    if not asig:
        msg_error = u"El usuario no tiene actualmente asignada ninguna asignatura, por lo que no \
        es posible generar exámenes de forma automática."
        return render_template('error/error_msg.html', error=msg_error)
    form.asignatura.choices = [(g.asignatura, g.asignatura) for g in asig]
    
    # the form  has already completed    
    if request.method == 'POST' and form.validate():        
        asignatura = Asignaturas.objects(asignatura=form.asignatura.data).first()
        tipo = form.tipo_examen.data
        
        lista_preguntas = Preguntas.user_objects(asignatura= asignatura.get_id(), tipo=tipo)
        num_preguntas = form.num_preguntas.data
        nombre = form.nombre.data
        
        examen = Examenes(nombre=nombre, asignatura=asignatura, publico=form.publico.data, usuario=login.current_user.get_id())

        # Random mode    
        if form.modo.data==0:
            # the necessary questions randomly selected from the list of questions            
            lista_preguntas = random.sample(lista_preguntas, num_preguntas)

        # mode = "questions by chapter"
        else:
            lista = []
            lista_preguntas = []
            lista_temas = Temas.user_objects(asignatura = asignatura.get_id())
            num_temas = len(lista_temas)
            
            # A list of questions is created for each chapter and randomly rearranges
            # and are included as well in another list
            for tema in lista_temas:
                preguntas = Preguntas.user_objects(asignatura= asignatura.get_id(), tipo=tipo, tema=tema)
                preguntas = random.sample(preguntas, len(preguntas))
                lista.append(preguntas)
            random.shuffle(lista)
            
            # Questions are selected from the random list until the total number needed
            indice = 0  
            ind = 0
            while num_preguntas > 0:
                if len(lista[indice]) > 0:
                    lista_preguntas.insert(ind, lista[indice].pop())
                    ind = ind + 1

                    num_preguntas = num_preguntas -1
            
                if indice < num_temas-1:
                    indice = indice + 1
                else:
                    indice = 0
                                        
        examen.preguntas = lista_preguntas
        examen.save(clean=False)

        return render_template('exams/gen_exa_ok.html', asignatura=asignatura, nombre=nombre, preguntas=lista_preguntas, tipo=tipo)
    
    # the form is displayed to complete
    return render_template('exams/gen_exa.html', form=form)