def responses(self, section, question): assert ISurveySection.providedBy(section), "Not a survey section" assert IQuestion.providedBy(question), "Not a question" results = [] for response in section.responses.values(): results.append(response.get(question.cluster, "")) return results
def manage(self): survey_manage.need() manage_css.need() post = self.request.POST if 'cancel' in self.request.POST: self.flash_messages.add(_("Canceled")) url = self.request.resource_url(self.context) return HTTPFound(location = url) if 'save' in post: self.process_question_ids() self.flash_messages.add(_("Saved")) url = self.request.resource_url(self.context, 'manage') return HTTPFound(location = url) response = {} picked_questions = set() survey_sections = [] for section in self.context.values(): if not ISurveySection.providedBy(section): continue picked_questions.update(section.question_ids) survey_sections.append(section) response['survey_sections'] = survey_sections if not survey_sections: msg = _(u"no_sections_added_notice", default = u"You need to add a Survey section " "and then use this view to manage the questions.") self.flash_messages.add(msg, auto_destruct = False) #Load all question objects that haven't been picked response['available_questions'] = self.get_questions(exclude = picked_questions) return response
def get_picked_choice(request, section, question, participant_uid, default = None, lang = None): """ Only works on questions with single choice questions right now. :param section: SurveySection instance :param question: Question instance :param participant_uid: Respondent (survey participant) id. :param default: Return this if not found. :return: Choice object or default. """ if not ISurveySection.providedBy(section): raise TypeError("section must be a SurveySection object") #pragma: no coverage if not IQuestion.providedBy(question): raise TypeError("question must be a Question object") #pragma: no coverage if lang is None: lang = request.locale_name question_widget = get_question_widget(request, question) if question_widget.allow_choices == True and question_widget.multichoice == False: choice_id = section.responses.get(participant_uid, {}).get(question.cluster) if choice_id: query = "type_name == 'Choice' and cluster == '%s'" % choice_id for docid in request.root.catalog.query(query + " and language == '%s'" % lang)[1]: #Generator with one item in this case for obj in request.resolve_docids(docid, perm = None): return obj for docid in request.root.catalog.query(query)[1]: #Generator that may have more items, but we're interested in any of them for obj in request.resolve_docids(docid, perm = None): return obj return default
def referenced_section_widget(node, kw): context = kw['context'] values = [('', _("<Select>"))] survey = find_interface(context, ISurvey) for obj in survey.values(): if ISurveySection.providedBy(obj): values.append((obj.uid, obj.title)) return deform.widget.SelectWidget(values = values)
def process_question_ids(self): sect_id_questions = self.request.POST.dict_of_lists() for section in self.context.values(): if ISurveySection.providedBy(section): sect_id_questions.setdefault(section.__name__, []) for (sect_id, question_ids) in sect_id_questions.items(): if sect_id in self.context: #Might be other things than section ids within the post self.context[sect_id].question_ids = question_ids
def responses(self, section, question): assert ISurveySection.providedBy(section), "Not a survey section" assert IQuestion.providedBy(question), "Not a question" results = {} for response in section.responses.values(): val = response.get(question.cluster, "") if val in results: results[val] += 1 else: results[val] = 1 return results
def clone_survey(self, parent_path, name): new_survey = deepcopy(self.context) #new_survey.title = title new_survey.uid = unicode(uuid4()) #Clear tokens new_survey.tokens.clear() #Reset uid and clear responses for obj in new_survey.values(): obj.uid = unicode(uuid4()) if ISurveySection.providedBy(obj): obj.responses.clear() #Attach and reindex contained parent = find_resource(self.root, parent_path) parent[name] = new_survey for obj in new_survey.values(): objectEventNotify(ObjectAddedEvent(obj, new_survey, obj.__name__)) return new_survey
def get_participants_data(self): """Returns the participants with statistics on the survey """ participants = [] for (email, uid) in self.tokens.items(): participant = {} participant['uid'] = uid participant['email'] = email response = 0 questions = 0 sections = [x for x in self.values() if ISurveySection.providedBy(x)] for section in sections: response += len(section.responses.get(uid, {})) questions += len(section.question_ids) if response != 0: participant['finished'] = Decimal(response) / Decimal(questions) * 100 else: participant['finished'] = 0 participants.append(participant) return participants
def get_sections(self): for obj in self.context.values(): if ISurveySection.providedBy(obj): yield obj