Steps To Reproduce | Here is a sample of the python script used to generate the question via remotecontrol ...
class C_Question:
"""
Classe C_Question minimale
"""
listFields = []
sid = 0
qid = 0
def __init__(self, sid, gid, type, code, help, intitule, other='N', default="", mandatory="Y"):
"""
Constructeur de la classe
:param sid: id du questionnaire
:param gid: id du groupe de question
:param type: type de question
:param code: code de la question dans LS
:param help: texte d'aide
:param intitule: intiulé de la question
"""
print("\t Création question ",code)
self.sid = sid
self.qid = 1
self.gid = gid
self.parent_qid = 0
self.type = type
self.help = help
self.code = code
self.other = other
self.default=default
self.mandatory=mandatory
self.intitule = intitule
self.fields_list = ['qid', 'sid', 'gid', 'type', 'title',
'question', 'preg', 'help', 'other',
'mandatory', 'encrypted', 'question_order', 'language',
'parent_qid', 'scale_id', 'same_default',
'relevance','defaultvalues', 'modulename']
self.intitule = intitule
self.questions = None
self.attributes=None
self.subquestions = None
self.answers = None
self.rows = None
self.document = ET.Element('document')
ET.SubElement(self.document, 'LimeSurveyDocType').text = 'Question'
ET.SubElement(self.document, 'DBVersion').text = '263'
lang = ET.SubElement(self.document, 'languages')
ET.SubElement(lang, 'language').text = 'fr'
self.questions = ET.SubElement(self.document, 'questions')
self.questions.append(self.createFields())
self.addQuestionsRows(self.code, self.help, self.intitule)
def setFieldsList(self, list):
self.field_list = list
def createFields(self):
"""
Créé un élément XML fields à partir d'une liste de champs
Cet item fields contient tous les fieldname fournis dans la liste
:param list_fields: listfields = Tableau des différents champs
field à ajouter
:type list_fields: Array
:return: Retourne un élément de type fields
:rtype: ET.Element
"""
fields = ET.Element('fields')
for field in self.fields_list:
ET.SubElement(fields, 'fieldname').text = field
return fields
def addQuestionsRows(self, title, help, intitule):
rows = ET.Element('rows')
row = ET.SubElement(rows, 'row')
ET.SubElement(row, 'qid').text = ET.CDATA(str(self.qid))
ET.SubElement(row, 'sid').text = ET.CDATA(self.sid)
ET.SubElement(row, 'gid').text = ET.CDATA(self.gid)
ET.SubElement(row, 'type').text = ET.CDATA(self.type)
ET.SubElement(row, 'title').text = ET.CDATA(title)
ET.SubElement(row, 'question').text = ET.CDATA(intitule)
ET.SubElement(row, 'preg')
ET.SubElement(row, 'help').text = ET.CDATA(help)
ET.SubElement(row, 'other').text = ET.CDATA(self.other)
ET.SubElement(row, 'mandatory').text = ET.CDATA(self.mandatory)
ET.SubElement(row, 'encrypted').text = ET.CDATA('N')
ET.SubElement(row, 'question_order').text = ET.CDATA('1')
ET.SubElement(row, 'language').text = ET.CDATA('fr')
ET.SubElement(row, 'parent_qid').text = ET.CDATA('0')
ET.SubElement(row, 'scale_id').text = ET.CDATA('0')
ET.SubElement(row, 'same_default').text = ET.CDATA('0')
ET.SubElement(row, 'relevance').text = ET.CDATA('1')
ET.SubElement(row, 'defaultvalues').text = ET.CDATA(self.default)
ET.SubElement(row, 'modulename')
self.rows = rows
self.questions.append(rows)
def post(self, mandatory='Y'):
root = ET.ElementTree(self.document)
chaine = ET.tostring(self.document)
encoded = base64.b64encode(chaine)
res = send('import_question', [token, sid, gid, encoded.decode('utf-8'), 'lsq', mandatory])
self.qid = res.json()['result']
class C_Question_Liste(C_Question):
"""
Classe C_Question par défaut de type ! (menu déroulant) ou L (Bouton radio)
"""
def __init__(self, sid, gid, typeQ, code, help, intitule, answersList, other='N', default="", mandatory="Y"):
super().__init__(sid, gid, typeQ, code, help, intitule, other, default, mandatory)
self.answers = None
self.answersList = answersList
self.fields_list = ['qid', 'code', 'answer',
'assessment_value', 'sortorder', 'language', 'scale_id']
self.addAnswersRows(self.qid)
def addAnswersRows(self, qid):
subQuestions = ET.SubElement(self.document, 'answers')
subQuestions.append(self.createFields())
rows = ET.Element('rows')
intqid = 1
for elt in self.answersList:
intqid = intqid+1
row = ET.SubElement(rows, 'row')
ET.SubElement(row, 'qid').text = ET.CDATA(str(self.qid))
ET.SubElement(row, 'code').text = ET.CDATA("A"+str(intqid-1))
ET.SubElement(row, 'answer').text = ET.CDATA(elt)
ET.SubElement(row, 'assessment_value').text = ET.CDATA('0')
ET.SubElement(row, 'sortorder').text = ET.CDATA(str(intqid-1))
ET.SubElement(row, 'language').text = ET.CDATA('fr')
ET.SubElement(row, 'scale_id').text = ET.CDATA('0')
self.answers = rows
subQuestions.append(self.answers)
############################################################################
#
Programme principal
#
############################################################################
token = init_session_key()
boutonRadio=C_Question_Liste(str(sid), str(gid), 'L',
'qBouton',
'Quel est le type de votre structure d\'appartenance ?',
'Type de structure',
['UFR, institut ou école', 'Laboratoire',
'Service'])
boutonRadio.post() |
---|