def survey_review_single(request, tl, program, instance): """ View a single survey response. """ try: prog = Program.by_prog_inst(program, instance) except Program.DoesNotExist: #raise Http404 raise ESPError(), "Can't find the program %s/%s" % (program, instance) user = ESPUser(request.user) survey_response = None ints = request.REQUEST.items() if len(ints) == 1: srs = SurveyResponse.objects.filter(id=ints[0][0]) if len(srs) == 1: survey_response = srs[0] if survey_response is None: raise ESPError( False ), 'Ideally this page should give you some way to pick an individual response. For now I guess you should go back to <a href="review">reviewing the whole survey</a>.' if tl == 'manage' and user.isAdmin(prog): answers = survey_response.answers.order_by('content_type', 'object_id', 'question') classes_only = False other_responses = None elif tl == 'teach': subject_ct = ContentType.objects.get(app_label="program", model="classsubject") section_ct = ContentType.objects.get(app_label="program", model="classsection") class_ids = [x["id"] for x in user.getTaughtClasses().values('id')] section_ids = [x["id"] for x in user.getTaughtSections().values('id')] answers = survey_response.answers.filter( content_type__in=[subject_ct, section_ct], object_id__in=(class_ids + section_ids)).order_by( 'content_type', 'object_id', 'question') classes_only = True other_responses = SurveyResponse.objects.filter( answers__content_type=subject_ct, answers__object_id__in=class_ids).order_by('id').distinct() else: raise ESPError( False ), 'You need to be a teacher or administrator of this program to review survey responses.' context = { 'user': user, 'program': prog, 'response': survey_response, 'answers': answers, 'classes_only': classes_only, 'other_responses': other_responses } return render_to_response('survey/review_single.html', request, context)
def availability(self, request, tl, one, two, module, extra, prog): # Renders the teacher availability page and handles submissions of said page. teacher = ESPUser(request.user) time_options = self.program.getTimeSlots() # Group contiguous blocks if Tag.getTag('availability_group_timeslots', default=True) == 'False': time_groups = [list(time_options)] else: time_groups = Event.group_contiguous(list(time_options)) blank = False if request.method == 'POST': # Process form post_vars = request.POST # Reset teacher's availability teacher.clearAvailableTimes(self.program) # Add in resources for the checked available times. timeslot_ids = map(int, post_vars.getlist('timeslots')) timeslots = Event.objects.filter( id__in=timeslot_ids).order_by('start') missing_tsids = set(timeslot_ids) - set(x.id for x in timeslots) if missing_tsids: raise ESPError( False ), 'Received requests for the following timeslots that don\'t exist: %s' % str( list(sorted(missing_tsids))) blank = (not (bool(len(timeslot_ids)))) if not blank: for timeslot in timeslots: teacher.addAvailableTime(self.program, timeslot) # Send an e-mail showing availability to the teacher (and the archive) ccc = ClassCreationController(self.program) ccc.send_availability_email(teacher) # Return to the main registration page return self.goToCore(tl) # Show new form available_slots = teacher.getAvailableTimes(self.program, True) # must set the ignore_classes=True parameter above, otherwise when a teacher tries to edit their # availability, it will show their scheduled times as unavailable. # Fetch the timeslots the teacher is scheduled in and grey them out. # If we found a timeslot that they are scheduled in but is not available, show a warning. taken_slots = [] user_sections = teacher.getTaughtSections(self.program) conflict_found = False for section in user_sections: for timeslot in section.get_meeting_times(): taken_slots.append(timeslot) if timeslot not in available_slots: conflict_found = True if not (len(available_slots) or blank): # I'm not sure whether or not we want the "or blank" # If they didn't enter anything, make everything checked by default. available_slots = self.program.getTimeSlots() # The following 2 lines mark the teacher as always available. This # is sometimes helpful, but not usually the desired behavior. # for a in available_slots: # teacher.addAvailableTime(self.program, a) context = { 'groups': [{ 'selections': [{ 'checked': (t in available_slots), 'taken': (t in taken_slots), 'slot': t } for t in group] } for group in time_groups] } context['num_groups'] = len(context['groups']) context['prog'] = self.program context['is_overbooked'] = ( not self.isCompleted() and (request.user.getTaughtTime(self.program) > timedelta(0))) context['submitted_blank'] = blank context['conflict_found'] = conflict_found return render_to_response(self.baseDir() + 'availability_form.html', request, (prog, tl), context)
def survey_view(request, tl, program, instance): try: prog = Program.by_prog_inst(program, instance) except Program.DoesNotExist: raise Http404 user = ESPUser(request.user) if (tl == 'teach' and not user.isTeacher()) or (tl == 'learn' and not user.isStudent()): raise ESPError( False ), 'You need to be a program participant (i.e. student or teacher, not parent or educator) to participate in this survey. Please contact the directors directly if you have additional feedback.' if request.GET.has_key('done'): return render_to_response('survey/completed_survey.html', request, {'prog': prog}) if tl == 'learn': event = "student_survey" else: event = "teacher_survey" if Record.user_completed(user, event, prog): raise ESPError( False ), "You've already filled out the survey. Thanks for responding!" surveys = prog.getSurveys().filter(category=tl).select_related() if request.REQUEST.has_key('survey_id'): try: s_id = int(request.REQUEST['survey_id']) surveys = surveys.filter( id=s_id ) # We want to filter, not get: ID could point to a survey that doesn't exist for this program, or at all except ValueError: pass if len(surveys) < 1: raise ESPError(False), "Sorry, no such survey exists for this program!" if len(surveys) > 1: return render_to_response( 'survey/choose_survey.html', request, { 'surveys': surveys, 'error': request.POST } ) # if request.POST, then we shouldn't have more than one survey any more... survey = surveys[0] if request.POST: response = SurveyResponse() response.survey = survey response.save() r = Record(user=user, event=event, program=prog, time=datetime.datetime.now()) r.save() response.set_answers(request.POST, save=True) return HttpResponseRedirect(request.path + "?done") else: questions = survey.questions.filter(per_class=False).order_by('seq') perclass_questions = survey.questions.filter(per_class=True) classes = sections = timeslots = [] if tl == 'learn': classes = user.getEnrolledClasses(prog, request) timeslots = prog.getTimeSlots().order_by('start') for ts in timeslots: # The order by string really means "title" ts.classsections = prog.sections().filter( meeting_times=ts).exclude( meeting_times__start__lt=ts.start).order_by( 'parent_class__title').distinct() for sec in ts.classsections: if user in sec.students(): sec.selected = True elif tl == 'teach': classes = user.getTaughtClasses(prog) sections = user.getTaughtSections(prog).order_by( 'parent_class__title') context = { 'survey': survey, 'questions': questions, 'perclass_questions': perclass_questions, 'program': prog, 'classes': classes, 'sections': sections, 'timeslots': timeslots, } return render_to_response('survey/survey.html', request, context)