app/soc/logic/models/survey.py
changeset 2480 0079e1038740
parent 2467 f46cff8d676b
child 2500 ecc16ffe174b
equal deleted inserted replaced
2479:b83c9b1ea0e1 2480:0079e1038740
    91 
    91 
    92     db.put(survey_content)
    92     db.put(survey_content)
    93 
    93 
    94     return survey_content
    94     return survey_content
    95 
    95 
    96   def updateSurveyRecord(self, user, survey, survey_record, fields):
       
    97     """ Create a new survey record, or get an existing one.
       
    98 
       
    99     params:
       
   100       user = user taking survey
       
   101       survey = survey entity
       
   102       survey_record = existing record, if one exists
       
   103       fields = submitted responses to survey fields
       
   104     """
       
   105     if survey_record:
       
   106       create = False
       
   107       for prop in survey_record.dynamic_properties():
       
   108         delattr(survey_record, prop)
       
   109     else:
       
   110       create = True
       
   111       survey_record = SurveyRecord(user=user, survey=survey)
       
   112 
       
   113     schema = eval(survey.survey_content.schema)
       
   114 
       
   115     for name, value in fields.items():
       
   116       if name == 'project':
       
   117         project = student_project.StudentProject.get(value)
       
   118         survey_record.project = project
       
   119       elif name == 'grade':
       
   120         survey_record.grade = GRADES[value]
       
   121       else:
       
   122         pick_multi = name in schema and schema[name]['type'] == 'pick_multi'
       
   123         if pick_multi and hasattr(fields, 'getlist'): # it's a multidict
       
   124           setattr(survey_record, name, ','.join(fields.getlist(name)))
       
   125         else:
       
   126           setattr(survey_record, name, value)
       
   127 
       
   128     # if creating evaluation record, set SurveyRecordGroup
       
   129     db.put(survey_record)
       
   130     if 'evaluation' in survey.taking_access and create:
       
   131       if not project: return False
       
   132       role = self.getUserRole(user, survey, project)
       
   133       survey_record_group = self.setSurveyRecordGroup(survey,
       
   134       survey_record, project)
       
   135       if survey_record_group:  db.put(survey_record_group)
       
   136 
       
   137     return survey_record
       
   138 
       
   139   def setSurveyRecordGroup(self, survey, survey_record, project):
       
   140     """First looks for an existing SurveyRecordGroup, using the
       
   141     project and its current status as a filter.
       
   142 
       
   143     IOW SurveyRecordGroup cannot consist of surveys taken with
       
   144     two different statuses.
       
   145 
       
   146     This means that a student cannot take a survey after the mentor
       
   147     has taken the accompanying survey and the project has since
       
   148     changed. (Assuming we want this strict behavior)
       
   149 
       
   150     params:
       
   151       survey = survey entity
       
   152       survey_record = saved response to survey
       
   153       project = student project for survey taker
       
   154     """
       
   155 
       
   156     group_query = SurveyRecordGroup.all(
       
   157     ).filter("project = ", project
       
   158     ).filter("initial_status = ", project.status
       
   159     )
       
   160 
       
   161     if survey.taking_access == 'mentor evaluation':
       
   162       survey_record_group = group_query.filter(
       
   163       "mentor = ", None ).get()
       
   164     elif survey.taking_access == 'student evaluation':
       
   165       survey_record_group = group_query.filter(
       
   166       "student = ", None ).get()
       
   167 
       
   168     if not survey_record_group:
       
   169       #create Survey Record Group if it doesn't already exist
       
   170       survey_record_group = SurveyRecordGroup(
       
   171       project=project,
       
   172       initial_status = project.status
       
   173       )
       
   174 
       
   175     if survey.taking_access == 'mentor evaluation':
       
   176       survey_record_group.mentor_record = survey_record
       
   177     elif survey.taking_access == 'student evaluation':
       
   178       survey_record_group.student_record = survey_record
       
   179 
       
   180     return survey_record_group
       
   181 
       
   182   def getSurveyForContent(self, survey_content):
    96   def getSurveyForContent(self, survey_content):
   183     """Returns the Survey belonging to the given SurveyContent.
    97     """Returns the Survey belonging to the given SurveyContent.
   184 
    98 
   185     params:
    99     params:
   186       survey_content: the SurveyContent to retrieve the Survey for.
   100       survey_content: the SurveyContent to retrieve the Survey for.