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