Esempio n. 1
0
def missedLectureCheck(user):
	day = datetime.date.today().weekday()
	hour = datetime.datetime.now().hour
	minute = datetime.datetime.now().minute
	if minute > 45:
		hour = hour + 1

	userQuery = User.query(User.userid == user.user_id())
	#gets the current user
	for userEntity in userQuery:
		#loops through the weeks that have occurred so far
		for i in range(1, getCurrentWeek()):
			#loops through the users lectures
			for lecture in userEntity.lectures:
				#sets the default value to not checked
				checked = False
				#loops through the lectures in history
				for attlecture in userEntity.history:
					#checks for a match
					if attlecture.week == i and attlecture.day == lecture.day and attlecture.time == lecture.time:
							#if one is found, there is an entry in the history so don't need to worry
							checked = True
				if not checked:
					#else we need to add that the user missed that lecture to the history
					missedLecture = Lecture(module=lecture.module, title=lecture.title, location=lecture.location, day=lecture.day, time=lecture.time, duration=lecture.duration)
					missedLecture.attended = False
					missedLecture.week = i
					userEntity.history.append(missedLecture)
					userEntity.streak = 0

		#does the same check for the current week
		for lecture in userEntity.lectures:
			#only applies to lectures that have already occured
			if lecture.day < day or (lecture.day == day and lecture.time+lecture.duration <= hour):
				#sets the default value to False
				checked = False
				for attlecture in userEntity.history:
					if attlecture.week == getCurrentWeek() and attlecture.day == lecture.day and attlecture.time == lecture.time:
						checked = True
				if not checked:
					missedLecture = Lecture(module=lecture.module, title=lecture.title, location=lecture.location, day=lecture.day, time=lecture.time, duration=lecture.duration)
					missedLecture.attended = False
					missedLecture.week = getCurrentWeek()
					userEntity.history.append(missedLecture)
					userEntity.streak = 0

		userEntity.put()
Esempio n. 2
0
	def post(self):
		user = users.get_current_user()
		if user:
			missedLectureCheck(user)
			#logging.info(self.request.body)
			data = json.loads(self.request.body)
			latitude = data["lat"]
			longitude = data["lon"]

			day = datetime.date.today().weekday()
			hour = datetime.datetime.now().hour
			minute = datetime.datetime.now().minute
			if minute > 45:
				hour = hour + 1

			thisLecture = None
			thisUser = None
			poly = []			

			userQuery = User.query(User.userid == user.user_id())
			for userEntity in userQuery:
				thisUser = userEntity
				for lecture in userEntity.lectures:
					#checks for a lecture that matches the current day and time
					if(lecture.day == day and (lecture.time <= hour and lecture.time + lecture.duration > hour)):
						thisLecture = lecture
						locations = lecture.location.split(";");
						for location in locations:
							#need to make multiple polys for each lecture, for each possible location
							buildingQuery = Building.query(Building.number == location)
							for building in buildingQuery:
								buildingCoords = []
								for coordinate in building.coordinates:
									c = (coordinate.lon, coordinate.lat)
									buildingCoords.append(c)
								poly.append(buildingCoords)

			noLecture = False
			checkedIn = False

			#checks if there is no current lecture
			if thisLecture is None:
				noLecture = True
				self.response.out.write(json.dumps({"valid":3}))
			else:
				#checks if the user has already checked in to this lecture
				for pastLecture in thisUser.history:
					if pastLecture.week == getCurrentWeek() and pastLecture.time == thisLecture.time and pastLecture.day == thisLecture.day:
						checkedIn = True
				if checkedIn:
					self.response.out.write(json.dumps({"valid":4}))	
			if not checkedIn and not noLecture:
				inBuilding = False
				for coords in poly:
					#checks user is at the correct location(s) for the lecture
					if not inBuilding and point_in_poly(longitude, latitude, coords):
						inBuilding = True

						attendedLecture = Lecture(module=thisLecture.module, title=thisLecture.title, location=thisLecture.location, day=thisLecture.day, time=thisLecture.time, duration=thisLecture.duration)

						attendedLecture.attended = True
						attendedLecture.week = getCurrentWeek()

						checkin = CheckIn(student=thisUser, lecture=attendedLecture)
						checkin.put()

						thisUser.history.append(attendedLecture)

						completedChalls = challengecheck(thisUser, attendedLecture, checkin)

						pointsEarned = 0
						
						challIcons = []
						challTitles = []
						challDescs = []
						challPoints = []

						for challenge in completedChalls:
							pointsEarned += challenge.points
							challTitles.append(challenge.title)
							challIcons.append(challenge.badge.iconName)
							challDescs.append(challenge.description)
							challPoints.append(challenge.points)

						thisUser.score = thisUser.score + 10 + thisUser.streak + pointsEarned
						thisUser.streak = thisUser.streak + 1
						thisUser.count = thisUser.count + 1

						thisUser.put()
						
						self.response.out.write(json.dumps({"valid":1, "score":thisUser.score, "count":thisUser.count, "streak":thisUser.streak, "icons":challIcons, "titles":challTitles, "points":challPoints, "descriptions":challDescs}))
				if not inBuilding: 
					self.response.out.write(json.dumps({"valid":2}))	
		else:
			self.redirect(users.create_login_url(self.request.uri))