app/soc/logic/models/survey.py
changeset 2431 800a020c9bcf
child 2438 0ecf0e48a4f2
equal deleted inserted replaced
2430:c9a5bf5ed3c5 2431:800a020c9bcf
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Survey (Model) query functions.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Daniel Diniz" <ajaksu@gmail.com>',
       
    22   '"James Levy" <jamesalexanderlevy@gmail.com>',
       
    23   '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    24   ]
       
    25 
       
    26 
       
    27 import logging
       
    28 
       
    29 from google.appengine.ext import db
       
    30 
       
    31 from soc.cache import sidebar
       
    32 from soc.logic.models import linkable as linkable_logic
       
    33 from soc.logic.models.news_feed import logic as newsfeed_logic
       
    34 from soc.logic.models.user import logic as user_logic
       
    35 from soc.logic.models import work
       
    36 from soc.models.program import Program
       
    37 from soc.models import student_project
       
    38 from soc.models.survey import Survey
       
    39 from soc.models.survey import SurveyContent
       
    40 from soc.models.survey_record import SurveyRecord
       
    41 from soc.models.survey_record_group import SurveyRecordGroup
       
    42 from soc.models.work import Work
       
    43 
       
    44 #TODO(James): Ensure this facilitates variable # of surveys 
       
    45 GRADES = {'pass': True, 'fail': False}
       
    46 PROJECT_STATUSES = {
       
    47 'accepted': {True: 'mid_term_passed', False: 'mid_term_failed'},
       
    48 'mid_term_passed': {True: 'passed', False: 'final_failed'}
       
    49 }
       
    50 
       
    51 class Logic(work.Logic):
       
    52   """Logic methods for the Survey model.
       
    53   """
       
    54 
       
    55   def __init__(self, model=Survey, base_model=Work,
       
    56                scope_logic=linkable_logic):
       
    57     """Defines the name, key_name and model for this entity.
       
    58     """
       
    59 
       
    60     super(Logic, self).__init__(model=model, base_model=base_model,
       
    61                                 scope_logic=scope_logic)
       
    62 
       
    63   def createSurvey(self, survey_fields, schema, survey_content=False):
       
    64     """Create a new survey from prototype.
       
    65 
       
    66     params:
       
    67       survey_fields = dict of survey field items (see SurveyContent model)
       
    68       schema = metadata about survey fields (SurveyContent.schema)
       
    69       survey_content = existing SurveyContent entity
       
    70     """
       
    71 
       
    72     if not survey_content:
       
    73       survey_content = SurveyContent()
       
    74     else:
       
    75       # wipe clean existing dynamic properties if they exist
       
    76       for prop in survey_content.dynamic_properties():
       
    77         delattr(survey_content, prop)
       
    78 
       
    79     for name, value in survey_fields.items():
       
    80       setattr(survey_content, name, value)
       
    81 
       
    82     survey_content.schema = str(schema)
       
    83 
       
    84     db.put(survey_content)
       
    85 
       
    86     return survey_content
       
    87 
       
    88   def updateSurveyRecord(self, user, survey, survey_record, fields):
       
    89     """ Create a new survey record, or get an existing one.
       
    90 
       
    91     params:
       
    92       user = user taking survey
       
    93       survey = survey entity
       
    94       survey_record = existing record, if one exists
       
    95       fields = submitted responses to survey fields
       
    96     """
       
    97     if survey_record:
       
    98       create = False
       
    99       for prop in survey_record.dynamic_properties():
       
   100         delattr(survey_record, prop)
       
   101     else:
       
   102       create = True
       
   103       survey_record = SurveyRecord(user=user, survey=survey)
       
   104 
       
   105     schema = eval(survey.survey_content.schema)
       
   106 
       
   107     for name, value in fields.items():
       
   108       if name == 'project':
       
   109         project = student_project.StudentProject.get(value)
       
   110         survey_record.project = project
       
   111       elif name == 'grade':
       
   112         survey_record.grade = GRADES[value]
       
   113       else:
       
   114         pick_multi = name in schema and schema[name]['type'] == 'pick_multi'
       
   115         if pick_multi and hasattr(fields, 'getlist'): # it's a multidict
       
   116           setattr(survey_record, name, ','.join(fields.getlist(name)))
       
   117         else:
       
   118           setattr(survey_record, name, value)
       
   119 
       
   120     # if creating evaluation record, set SurveyRecordGroup
       
   121     db.put(survey_record)
       
   122     if 'evaluation' in survey.taking_access and create:
       
   123       if not project: return False
       
   124       role = self.getUserRole(user, survey, project)
       
   125       survey_record_group = self.setSurveyRecordGroup(survey,
       
   126       survey_record, project)
       
   127       if survey_record_group:  db.put(survey_record_group)
       
   128 
       
   129     return survey_record
       
   130 
       
   131   def setSurveyRecordGroup(self, survey, survey_record, project):
       
   132     """First looks for an existing SurveyRecordGroup, using the
       
   133     project and its current status as a filter.
       
   134 
       
   135     IOW SurveyRecordGroup cannot consist of surveys taken with
       
   136     two different statuses.
       
   137 
       
   138     This means that a student cannot take a survey after the mentor
       
   139     has taken the accompanying survey and the project has since
       
   140     changed. (Assuming we want this strict behavior)
       
   141 
       
   142     params:
       
   143       survey = survey entity
       
   144       survey_record = saved response to survey
       
   145       project = student project for survey taker
       
   146     """
       
   147 
       
   148     group_query = SurveyRecordGroup.all(
       
   149     ).filter("project = ", project
       
   150     ).filter("initial_status = ", project.status
       
   151     )
       
   152 
       
   153     if survey.taking_access == 'mentor evaluation':
       
   154       survey_record_group = group_query.filter(
       
   155       "mentor = ", None ).get()
       
   156     elif survey.taking_access == 'student evaluation':
       
   157       survey_record_group = group_query.filter(
       
   158       "student = ", None ).get()
       
   159 
       
   160     if not survey_record_group:
       
   161       #create Survey Record Group if it doesn't already exist
       
   162       survey_record_group = SurveyRecordGroup(
       
   163       project=project,
       
   164       initial_status = project.status
       
   165       )
       
   166 
       
   167     if survey.taking_access == 'mentor evaluation':
       
   168       survey_record_group.mentor_record = survey_record
       
   169     elif survey.taking_access == 'student evaluation':
       
   170       survey_record_group.student_record = survey_record
       
   171 
       
   172     return survey_record_group
       
   173 
       
   174   def getUserRole(self, user, survey, project):
       
   175     """Gets the role of a user for a project, used for SurveyRecordGroup.
       
   176 
       
   177     params:
       
   178       user: user taking survey
       
   179       survey: survey entity
       
   180       project: student project for this user
       
   181     """
       
   182 
       
   183     if survey.taking_access == 'mentor evaluation':
       
   184       mentors = self.getMentorforProject(user, project)
       
   185 
       
   186       if len(mentors) < 1 or len(mentors) > 1:
       
   187         logging.warning('Unable to determine mentor for \
       
   188         user %s. Results returned: %s ' % (
       
   189         user.key().name(), str(mentors)) )
       
   190         return False
       
   191 
       
   192       this_mentor = mentors[0]
       
   193 
       
   194     if survey.taking_access == 'student evaluation':
       
   195       students = self.getStudentforProject(user, project)
       
   196 
       
   197       if len(students) < 1 or len(students) > 1:
       
   198         logging.warning('Unable to determine student for \
       
   199         user %s. Results returned: %s ' % (
       
   200         user.key().name(), str(students)) )
       
   201         return False
       
   202 
       
   203       this_student = students[0]
       
   204 
       
   205   def getStudentforProject(self, user, project):
       
   206     """Get student projects for a given User.
       
   207 
       
   208     params:
       
   209       user = survey taking user
       
   210       project = survey taker's student project
       
   211     """
       
   212     from soc.logic.models.student import logic as student_logic
       
   213     import soc.models.student
       
   214 
       
   215     # TODO this should be done per Student or Program
       
   216     # TODO filter for accepted, midterm_passed, etc?
       
   217     user_students = student_logic.getForFields({'user': user}) 
       
   218     if not user_students: return []
       
   219     return set([project.student for project in sum(
       
   220     (list(s.student_projects.run())
       
   221     for s in user_students), []) if project.key() == project.key()])
       
   222 
       
   223   def getMentorforProject(self, user, project):
       
   224     """Get Student Projects that are being mentored by the given User.
       
   225 
       
   226     params:
       
   227       user = survey taking user
       
   228       project = survey taker's student project
       
   229     """
       
   230 
       
   231     from soc.logic.models.mentor import logic as mentor_logic
       
   232     import soc.models.mentor
       
   233 
       
   234     # TODO filter for accepted, midterm_passed, etc?
       
   235     # TODO this should be done a program basis not user
       
   236 
       
   237     user_mentors = mentor_logic.getForFields({'user': user}) 
       
   238 
       
   239     if not user_mentors:
       
   240       return []
       
   241 
       
   242     return set([project.mentor for project in sum(
       
   243             (list(mentor.student_projects.run())
       
   244              for mentor in user_mentors), [])
       
   245         if project.key() == project.key()])
       
   246 
       
   247   def activateGrades(self, survey):
       
   248     """Activates the grades on a Grading Survey.
       
   249 
       
   250     TODO(James) Fix this Docstring
       
   251 
       
   252     params:
       
   253       survey = survey entity
       
   254     """
       
   255     if survey.taking_access != "mentor evaluation":
       
   256       logging.error("Cannot grade survey %s with taking access %s"
       
   257       % (survey.key().name(), survey.taking_access))
       
   258       return False
       
   259 
       
   260     program = survey.scope
       
   261 
       
   262     for project in program.student_projects.fetch(1000):
       
   263       this_record_group = SurveyRecordGroup.all().filter(
       
   264       "project = ", project).filter(
       
   265       "initial_status = ", project.status).get()
       
   266 
       
   267       if not this_record_group:
       
   268          logging.warning('neither mentor nor student has \
       
   269          taken the survey for project %s' % project.key().name() )
       
   270          continue
       
   271 
       
   272       if not this_record_group.mentor_record:
       
   273         # student has taken survey, but not mentor
       
   274         logging.warning('not continuing without mentor record...')
       
   275         continue
       
   276 
       
   277       status_options = PROJECT_STATUSES.get(project.status)
       
   278 
       
   279       if not status_options:
       
   280         logging.warning('unable to find status options for project \
       
   281         status %s' % project.status)
       
   282         continue
       
   283 
       
   284       new_project_grade = this_record_group.mentor_record.grade
       
   285       new_project_status = status_options.get(new_project_grade)
       
   286 
       
   287       if getattr(this_record_group, 'final_status'):
       
   288          logging.warning('project %s record group should not \
       
   289          yet have a final status %s' % (
       
   290          project.key().name(), this_record_group.final_status ) )
       
   291          continue
       
   292 
       
   293       # assign the new status to the project and s
       
   294       project.status = new_project_status
       
   295       this_record_group.final_status = new_project_status
       
   296 
       
   297   def getKeyNameFromPath(self, path):
       
   298     """Gets survey key name from a request path.
       
   299 
       
   300     params:
       
   301       path = path of the current request
       
   302     """
       
   303 
       
   304     # TODO determine if kwargs in the request contains this information
       
   305     return '/'.join(path.split('/')[-4:]).split('?')[0]
       
   306 
       
   307   def getProjects(self, survey, user):
       
   308     """Get projects linking user to a program.
       
   309 
       
   310     Serves as access handler (since no projects == no access).
       
   311     And retrieves projects to choose from (if mentors have >1 projects).
       
   312 
       
   313     params:
       
   314       survey = survey entity
       
   315       user = survey taking user
       
   316     """
       
   317     this_program = survey.scope
       
   318 
       
   319     if 'mentor' in survey.taking_access:
       
   320       these_projects = self.getMentorProjects(user, this_program)
       
   321 
       
   322     if 'student' in survey.taking_access:
       
   323       these_projects = self.getStudentProjects(user, this_program)
       
   324 
       
   325     logging.info(these_projects)
       
   326 
       
   327     if len(these_projects) == 0:
       
   328       return False
       
   329 
       
   330     return these_projects
       
   331 
       
   332   def getDebugUser(self, survey, this_program):
       
   333     """Debugging method impersonates other roles.
       
   334 
       
   335     Tests taking survey, saving response, and grading.
       
   336 
       
   337     params:
       
   338       survey = survey entity
       
   339       this_program = program scope of survey
       
   340     """
       
   341 
       
   342     if 'mentor' in survey.taking_access:
       
   343       from soc.models.mentor import Mentor
       
   344       role = Mentor.get_by_key_name(
       
   345       this_program.key().name() + "/org_1/test")
       
   346 
       
   347     if 'student' in survey.taking_access:
       
   348       from soc.models.student import Student
       
   349       role = Student.get_by_key_name(
       
   350       this_program.key().name() + "/test")
       
   351 
       
   352     if role: return role.user
       
   353 
       
   354   def getKeyValuesFromEntity(self, entity):
       
   355     """See base.Logic.getKeyNameValues.
       
   356     """
       
   357 
       
   358     return [entity.prefix, entity.scope_path, entity.link_id]
       
   359 
       
   360   def getKeyValuesFromFields(self, fields):
       
   361     """See base.Logic.getKeyValuesFromFields.
       
   362     """
       
   363 
       
   364     return [fields['prefix'], fields['scope_path'], fields['link_id']]
       
   365 
       
   366   def getKeyFieldNames(self):
       
   367     """See base.Logic.getKeyFieldNames.
       
   368     """
       
   369 
       
   370     return ['prefix', 'scope_path', 'link_id']
       
   371 
       
   372   def getScope(self, entity):
       
   373     """Gets Scope for entity.
       
   374 
       
   375     params:
       
   376       entity = Survey entity
       
   377     """
       
   378 
       
   379     if getattr(entity, 'scope', None):
       
   380       return entity.scope
       
   381 
       
   382     import soc.models.program
       
   383     import soc.models.organization
       
   384     import soc.models.user
       
   385     import soc.models.site
       
   386 
       
   387     # use prefix to generate dict key
       
   388     scope_types = {"program": soc.models.program.Program,
       
   389     "org": soc.models.organization.Organization,
       
   390     "user": soc.models.user.User,
       
   391     "site": soc.models.site.Site}
       
   392 
       
   393     # determine the type of the scope
       
   394     scope_type = scope_types.get(entity.prefix)
       
   395 
       
   396     if not scope_type:
       
   397       # no matching scope type found
       
   398       raise AttributeError('No Matching Scope type found for %s' % entity.prefix)
       
   399 
       
   400     # set the scope and update the entity
       
   401     entity.scope = scope_type.get_by_key_name(entity.scope_path)
       
   402     entity.put()
       
   403 
       
   404     # return the scope
       
   405     return entity.scope
       
   406 
       
   407 logic = Logic()
       
   408 
       
   409 
       
   410 class ResultsLogic(work.Logic):
       
   411   """Logic methods for listing results for Surveys.
       
   412   """
       
   413 
       
   414   def __init__(self, model=SurveyRecord,
       
   415                base_model=None, scope_logic=None):
       
   416     """Defines the name, key_name and model for this entity.
       
   417     """
       
   418 
       
   419     super(ResultsLogic, self).__init__(model=model, base_model=base_model,
       
   420                                 scope_logic=scope_logic)
       
   421 
       
   422 
       
   423 results_logic = ResultsLogic()