PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/dynamictable/public/SchoolCalendarService.php

http://pyjamas.googlecode.com/
PHP | 155 lines | 118 code | 37 blank | 0 comment | 16 complexity | d5e884963d9f0629b1864ceb572296e7 MD5 | raw file
  1<?
  2include_once("phpolait/phpolait.php");
  3
  4class SchoolCalendarService {
  5	function getPeople($start_row, $max_rows) {
  6		global $calendar_generator;
  7		
  8		return $calendar_generator->getPeople($start_row, $max_rows);
  9	}
 10}
 11
 12function timeslot_compare($a, $b) {
 13	if ($a["dayOfWeek"] < $b["dayOfWeek"]) return -1;
 14	else if ($a["dayOfWeek"] > $b["dayOfWeek"]) return 1;
 15	else {
 16		if ($a["startMinutes"] < $b["startMinutes"]) return -1;
 17		else if ($a["startMinutes"] > $b["startMinutes"]) return 1;
 18	}
 19
 20	return 0;
 21}
 22
 23class SchoolCalendarGenerator {
 24	function SchoolCalendarGenerator() {
 25		$this->people = array();
 26		mt_srand(3);
 27		$this->CLASS_LENGTH_MINS = 50;
 28		$this->MAX_SCHED_ENTRIES = 5;
 29		$this->MIN_SCHED_ENTRIES = 1;
 30		$this->MAX_PEOPLE = 100;
 31		$this->STUDENTS_PER_PROF = 5;
 32		
 33		$this->FIRST_NAMES = array("Inman", "Sally", "Omar", "Teddy", "Jimmy", 
 34			"Cathy", "Barney", "Fred", "Eddie", "Carlos");
 35		$this->LAST_NAMES = array("Smith", "Jones", "Epps", "Gibbs", "Webber",
 36			"Blum", "Mendez", "Crutcher", "Needler", "Wilson", "Chase", "Edelstein");
 37		$this->SUBJECTS = array("Chemistry", "Phrenology", "Geometry", 
 38			"Underwater Basket Weaving", "Basketball", "Computer Science", "Statistics",
 39			"Materials Engineering", "English Literature", "Geology");
 40
 41		$this->NO_PEOPLE = array();
 42		
 43		$this->generateRandomPeople();
 44	}
 45	
 46	function getPeople($startIndex, $maxCount) {
 47		$peopleCount = count($this->people);
 48		
 49		$start = $startIndex;
 50		if ($start >= $peopleCount) {
 51			return $this->NO_PEOPLE;
 52		}
 53
 54		$end = min($startIndex + $maxCount, $peopleCount);
 55		if ($start == $end) {
 56			return $this->NO_PEOPLE;
 57		}
 58
 59		$resultCount = $end - $start;
 60		$results = array();
 61		for ($from = $start, $to = 0; $to < $resultCount; ++$from, ++$to) {
 62			$results[$to] = $this->people[$from];
 63		}
 64		return $results;
 65	}
 66
 67	function generateRandomPeople() {
 68		for ($i = 0; $i < $this->MAX_PEOPLE; ++$i) {
 69			$this->people[]=$this->generateRandomPerson();
 70		}
 71	}
 72			
 73	function generateRandomPerson() {
 74		if (mt_rand(0, $this->STUDENTS_PER_PROF) == 1) {
 75			return $this->generateRandomProfessor();
 76		} else {
 77			return $this->generateRandomStudent();
 78		}
 79	}
 80
 81	function generateRandomProfessor() {
 82		$prof = array();
 83		
 84		$firstName = $this->pickRandomString($this->FIRST_NAMES);
 85		$lastName = $this->pickRandomString($this->LAST_NAMES);
 86		
 87		$prof["__jsonclass__"]=array("Professor.Professor");
 88		$prof["name"]="Dr. " . $firstName . " " . $lastName;
 89		
 90		$subject = $this->pickRandomString($this->SUBJECTS);
 91		$prof["description"]="Professor of " . $subject;
 92		
 93		$prof["teachingSchedule"]=$this->generateRandomSchedule();
 94
 95		return $prof;
 96	}
 97
 98	function generateRandomSchedule() {
 99		$schedule = array();
100		$schedule["__jsonclass__"]=array("Schedule.Schedule");
101		
102		$range = $this->MAX_SCHED_ENTRIES - $this->MIN_SCHED_ENTRIES + 1;
103		$howMany = $this->MIN_SCHED_ENTRIES + mt_rand(0, $range);
104		
105		$timeSlots = array();
106
107		for ($i = 0; $i < $howMany; ++$i) {
108			$startHrs = 8 + mt_rand(0, 9);
109			$startMins = 15 * mt_rand(0, 4);
110			$dayOfWeek = 1 + mt_rand(0, 5);
111			
112			$absStartMins = 60 * $startHrs + $startMins;
113			$absStopMins = $absStartMins + $this->CLASS_LENGTH_MINS;
114			
115			$timeSlots[$i] = array(
116				"__jsonclass__"=>array("TimeSlot.TimeSlot"),
117				"dayOfWeek"=>$dayOfWeek,
118				"startMinutes"=>$absStartMins,
119				"endMinutes"=>$absStopMins
120				);
121		}
122
123		usort($timeSlots, "timeslot_compare");
124		
125		$schedule["timeSlots"] = $timeSlots;
126
127		return $schedule;
128	}
129
130	function generateRandomStudent() {
131		$student = array();
132		$student["__jsonclass__"]=array("Student.Student");
133		
134		$firstName = $this->pickRandomString($this->FIRST_NAMES);
135		$lastName = $this->pickRandomString($this->LAST_NAMES);
136		$student["name"]=$firstName . " " . $lastName;
137		
138		$subject = $this->pickRandomString($this->SUBJECTS);
139		$student["description"]="Majoring in " . $subject;
140		
141		$student["classSchedule"]=$this->generateRandomSchedule();
142		
143		return $student;
144	  }
145
146	function pickRandomString($a) {
147		return $a[array_rand($a)];	
148	}
149}
150
151$calendar_generator = new SchoolCalendarGenerator();
152$service = new SchoolCalendarService();
153
154$server = new JSONRpcServer($service);
155?>