#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Pyromaths
# Un programme en Python qui permet de créer des fiches d'exercices types de
# mathématiques niveau collège ainsi que leur corrigé en LaTeX.
# Copyright (C) 2006 -- Jérôme Ortais (jerome.ortais@pyromaths.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
from lxml import etree
from re import findall
from os import chdir, chmod, popen
from os.path import join
from sys import platform
from stat import S_IEXEC
class quizs_html:
def __init__(self, dir_name, data_dir):
self.quizs = []
self.respondes = []
self.n_questions = int(0)
chdir(data_dir)
self.mimetex_path = join("mimetex", "mimetex.cgi")
if platform.find("win")!= -1:
self.mimetex_path = join("mimetex", "mimetex.exe")
chmod(self.mimetex_path, S_IEXEC)
self.dir_name = dir_name
def html_question(self, type_moodle, name_quiz, exo, cor):
# xml_type -> matching(emparejar), essay(ensayo), numerical, multichoice,
# shortanswer, cloze, truefalse, description(sin pregunta)
self.n_questions += 1
numero = etree.Element("div", {"class":"numero"})
numero.text = str(self.n_questions)
pregunta = etree.Element("div", {"class":"pregunta"})
p = etree.SubElement(pregunta, "p")
etree.SubElement(p, "b").text = (u"%s"%name_quiz)
form = etree.SubElement(pregunta, "form", name="q%s"%self.n_questions)
# Extrae código entre $$, genera con mimeTeX y lo convierte tipo html
exo_img = self.create_img(exo)
if type_moodle == "cloze":
exo_img = self.html_question_cloze(exo_img)
#Interpreta mal los "&", así que los convierte y más adelante los desconvierte
exo_img = exo_img.replace('&','&')
exo_img_etree = etree.XML(exo_img)
form.append(exo_img_etree)
pregunta_div = etree.SubElement(pregunta, "div", align="right")
etree.SubElement(pregunta_div, "span", id="resp%s"%self.n_questions).text = u" "
etree.SubElement(pregunta_div, "input", type="button", value=_(u"Vérifier"),
id="b%s"%self.n_questions, onclick="Engine_form(%s)"%self.n_questions)
elif type_moodle == "numerical" or type_moodle == "shortanswer":
exo_img = exo_img.replace("\n", "
\n")
exo_img = self.html_question_numerical_shortanswer(exo_img, cor)
#Interpreta mal los "&", así que los convierte y más adelante los desconvierte
exo_img = exo_img.replace('&','&')
exo_img_etree = etree.XML(exo_img)
form.append(exo_img_etree)
pregunta_div = etree.SubElement(pregunta, "div", align="right")
etree.SubElement(pregunta_div, "span", id="resp%s"%self.n_questions).text = u" "
etree.SubElement(pregunta_div, "input", type="button", value=_(u"Vérifier"),
id="b%s"%self.n_questions, onclick="Engine_form(%s)"%self.n_questions)
elif type_moodle == "multichoice":
exo_img = exo_img.replace("\n", "
\n")
exo_img = self.html_question_multichoice(exo_img, cor)
#Interpreta mal los "&", así que los convierte y más adelante los desconvierte
exo_img = exo_img.replace('&','&')
exo_img_etree = etree.XML(exo_img)
form.append(exo_img_etree)
pregunta_div = etree.SubElement(pregunta, "div", align="right")
etree.SubElement(pregunta_div, "span", id="resp%s"%self.n_questions).text = u" "
etree.SubElement(pregunta_div, "input", type="button", value=_(u"Vérifier"),
id="b%s"%self.n_questions, onclick="Engine_checkbox(%s)"%self.n_questions)
elif type_moodle == "matching":
exo_img = exo_img.replace("\n", "
\n")
exo_img = self.html_question_matching(exo_img, cor)
#Interpreta mal los "&", así que los convierte y más adelante los desconvierte
exo_img = exo_img.replace('&','&')
exo_img_etree = etree.XML(exo_img)
form.append(exo_img_etree)
pregunta_div = etree.SubElement(pregunta, "div", align="right")
etree.SubElement(pregunta_div, "span", id="resp%s"%self.n_questions).text = u" "
etree.SubElement(pregunta_div, "input", type="button", value=_(u"Vérifier"),
id="b%s"%self.n_questions, onclick="Engine_form(%s)"%self.n_questions)
elif type_moodle == "truefalse":
exo_img = exo_img.replace("\n", "
\n")
exo_img = self.html_question_multichoice(exo_img, cor, type_check="radio")
#Interpreta mal los "&", así que los convierte y más adelante los desconvierte
exo_img = exo_img.replace('&','&')
exo_img_etree = etree.XML(exo_img)
form.append(exo_img_etree)
pregunta_div = etree.SubElement(pregunta, "div", align="right")
etree.SubElement(pregunta_div, "span", id="resp%s"%self.n_questions).text = u" "
etree.SubElement(pregunta_div, "input", type="button", value=_(u"Vérifier"),
id="b%s"%self.n_questions, onclick="Engine_checkbox(%s)"%self.n_questions)
#tipo type_moodle == "essay" o type_moodle == "description" no hay ninguno:
quiz_txt = etree.tounicode(numero, pretty_print=True)
quiz_txt += etree.tounicode(pregunta, pretty_print=True)
#Corrige tabulación de salto de línea
if self.n_questions == 1: quiz_txt = " " + quiz_txt
quiz_txt = quiz_txt.replace("\n", "\n ")
#Añade separación en las tablas (diferente si tiene borde)
if quiz_txt.find("border") > -1:
quiz_txt = quiz_txt.replace('cellpadding=\"0\"', 'cellpadding=\"5\"')
else:
quiz_txt = quiz_txt.replace('cellpadding=\"0\"', 'cellpadding=\"20\"')
#Corrige conversión al añadirse como texto & en un etree (hacerlo 2 veces)
quiz_txt = quiz_txt.replace('&', '&').replace('&', '&')
#Corrige en "multichoice", pues se añade < y > como texto
quiz_txt = quiz_txt.replace(u'<img', '')
#Corrige en windows usa "\" y para las web es mejor "/"
quiz_txt = quiz_txt.replace(u'img\\', u'img/')
self.quizs.append(quiz_txt)
def create_img(self, text, subnom = ""):
# Crea las flechas de avanzar y retroceder
self.compileToFile("\Longrightarrow", "next.gif")
self.compileToFile("\Longleftarrow", "prev.gif")
# Busca los texto a convertir en imagen
to_img = findall("\$\$[^\$]*\$\$", text)
for i in to_img:
img_nom = str(self.n_questions)+"_"+str(to_img.index(i))+subnom+".gif"
self.compileToFile(i[2:len(i)-2], img_nom)
#img_txt = etree.Element("img", src=join("img", img_nom))
img_txt = etree.Element("img", align="middle", src=join("img", img_nom))
#img_txt = etree.Element("img", align="top", src=join("img", img_nom))
text = text.replace(i, etree.tounicode(img_txt))
if subnom == "":
return u"