Source for file ResultsDB.inc

Documentation is available at ResultsDB.inc

  1. <?php
  2. /**
  3. * Result Tracker Module Database API.
  4. * This file is part of CompInaBox.
  5. * @copyright Copyright 2001-2005. Eric D. Nielsen, All rights reserverd.
  6. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  7. * @author Eric D. Nielsen <nielsene@alum.mit.edu>
  8. *
  9. * @package Database
  10. */
  11.  
  12. /**
  13. * Result Tracker Module Database API.
  14. * This class presents a OOP interface to the common
  15. * queries needed by Result Tracker Module functionality.
  16. * @todo Further tease out SQL to the {@link PhraseBook}. The ResultsDBDB
  17. * class should evolve into the functional API -- it needs more
  18. * parameter checking and more consistency of return values to
  19. * be useful. The places where a SQL statement was wrapped up for use
  20. * here, but is only used by a single location in the code, might get
  21. * "demoted" to pure PhraseBook status, etc.
  22. * @package Database
  23. */
  24. class ResultsDB extends CIB_DB
  25. {
  26. /**
  27. * ResultsDB Constructor.
  28. * Simply call the parent constructor.
  29. * @todo Add a check after connecting to make sure its a central DB and
  30. * not a per-comp DB
  31. * @access public
  32. *
  33. * @param string $dbname what database do you want to connect to
  34. * @param string $user as what user
  35. * @param string $pass which what password
  36. */
  37. function ResultsDB($dbname, $user, $pass)
  38. {
  39. CIB_DB::CIB_DB($dbname, $user, $pass);
  40. }
  41.  
  42. /**
  43. * Return a list of possible id matches for a given name.
  44. * Given a name, attempts to find matching people. Assumes "firstname
  45. * lastname" order. If ony one components, tries both. If multiple
  46. * components (3 or more) tries splitting into first and last sections
  47. * at each space. Case insenitive.
  48. *
  49. * @access public
  50. * @param string $name the name to find IDs for
  51. * @return array a list of peopleIDs from the database
  52. * @todo Look at reconcilling with the current Prime/Partner matching code,
  53. * but remember these touch different schema databases.
  54. */
  55. function getPeopleByName($name)
  56. {
  57. $ids = array();
  58. $nameComponents = explode(" ",trim($name));
  59. $numComponents = count($nameComponents);
  60. if ($numComponents==2)
  61. {
  62. $query = "SELECT peopleid FROM people WHERE lower(firstname)=lower('{$nameComponents[0]}') AND lower(lastname)=lower('{$nameComponents[1]}');";
  63. $result = $this->query($query);
  64. $numrows = $result->numrows();
  65. for ($i=0;$i<$numrows;$i++)
  66. {
  67. list($ids[])=$result->getRowAt($i);
  68. }
  69. }
  70. else if ($numComponents>2)
  71. {
  72. $seperator=1;
  73. while ($seperator < $numComponents)
  74. {
  75. $firstname=""; $lastname="";
  76. for ($i=0;$i<$numComponents;$i++)
  77. if ($i<$seperator) {
  78. if ($firstname!="") $firstname.= " ";
  79. $firstname.=$nameComponents[$i];
  80. } else {
  81. if ($lastname!="") $lastname.= " ";
  82. $lastname.=$nameComponents[$i];
  83. }
  84. $query = "SELECT peopleid FROM people WHERE
  85. lower(firstname)=lower('$firstname') AND lower(lastname)=lower('$lastname');";
  86. $result = $this->query($query);
  87. $numrows = $result->numrows();
  88. for ($i=0;$i<$numrows;$i++)
  89. {
  90. list($tempID)=$result->getRowAt($i);
  91. $ids[]=$tempID;
  92. }
  93. $seperator++;
  94. }
  95. }
  96. else
  97. {
  98. $query = "SELECT peopleid FROM people WHERE lower(firstname)=lower('{$nameComponents[0]}') OR lower(lastname)=lower('{$nameComponents[0]}');";
  99. $result = $this->query($query);
  100. $numrows = $result->numrows();
  101. for ($i=0;$i<$numrows;$i++)
  102. {
  103. list($ids[])=$result->getRowAt($i);
  104. }
  105. }
  106. return $ids;
  107. }
  108.  
  109. /**
  110. * Returns a list of details about all requested IDs
  111. * Returns the name, hometown (if entered), current team memberships,
  112. * and any past team memberships known for each individuals in $personIDs
  113. * @access public
  114. * @param array $personIDs Array of peopleIDs from the DB
  115. * @return array an array of arrays, each item is formatted as:
  116. * - "FirstName"=>firstname,
  117. * - "LastName"=>lastname,
  118. * - "City"=>hometwon, only if present in DB
  119. * - "State"=>home state, only if present in DB
  120. * - "Teams"=>array of team abbreviations (strings) only if any
  121. * memberships found
  122. * - "DancedAs"=>array of team abbreviations (string) from result list, only
  123. * if any found
  124. * Each subitem is keyed by the the associated personID
  125. * @todo remove duplicate entries from DanceAs that also appear in Teams, to
  126. * show only historic values in DanceAs
  127. */
  128. function getIdentificationByID($personIDs)
  129. {
  130. if (!is_array($personIDs)) $personIDs = array($personIDs);
  131. $details = array();
  132. foreach ($personIDs as $pID)
  133. {
  134. $aDetail=array();
  135. $query = "SELECT firstname, lastname FROM people WHERE peopleid = $pID;";
  136. $result = $this->query($query);
  137. if ($result->numrows()>0) {
  138. list($aDetail["FirstName"],$aDetail["LastName"]) =
  139. $result->getRowAt(0);
  140. }
  141. $query = "SELECT city,state FROM people_addresses WHERE peopleid = $pID;";
  142. $result = $this->query($query);
  143. if ($result->numrows()>0) {
  144. list($aDetail["City"],$aDetail["State"]) =
  145. $result->getRowAt(0);
  146. }
  147. $query = "SELECT abbreviation FROM teams NATURAL JOIN team_membership WHERE peopleid = $pID;";
  148. $result = $this->query($query);
  149. $numteams = $result->numrows();
  150. $teams=array();
  151. for ($i=0;$i<$numteams;$i++)
  152. {
  153. list($teams[]) = $result->getRowAt($i);
  154. }
  155. $aDetail["Teams"]=$teams;
  156.  
  157. $query =<<<END_QUERY
  158. SELECT abbreviation FROM teams, result_couples
  159. WHERE (leader=$pID AND leader_team=teamid) OR (follower=$pID AND follower_team=teamid);
  160. END_QUERY; $result = $this->query($query);
  161. $numteams = $result->numrows();
  162. $dancedAs=array();
  163. for ($i=0;$i<$numteams;$i++)
  164. {
  165. list($dancedAs[]) = $result->getRowAt($i);
  166. }
  167. $aDetail["DancedAs"]=$dancedAs;
  168. $details[$pID]=$aDetail;
  169. }
  170. return $details;
  171. }
  172.  
  173. /**
  174. * Returns an exhaustive set of placement information.
  175. * Returns a complete (from the Result Tracker's knowledge base
  176. * perspective) set of results for the requested person. The returned
  177. * strcuture is normally fed into the display rountines which
  178. * tabulate points.
  179. *
  180. * @access public
  181. * @param int $pID a peopleid to look up results for
  182. * @return array A deeply nested array whos details are highly likely
  183. * to be refactored soon.
  184. * @todo Refactor results array into a new class
  185. */
  186. function getResultsFor($pID)
  187. {
  188. $placements=array();
  189. $query =<<<END_QUERY
  190. SELECT eventid, compname, compunix, getformatteddate as date,
  191. stylename, s.eventshortprefix, s.eventfullprefix,
  192. levelname, l.eventshortprefix, age_level,
  193. placement, num_cuts, leader, follower,
  194. size, rounds
  195. FROM comp_events_placements NATURAL JOIN result_couples NATURAL JOIN
  196. comp_events NATURAL JOIN
  197. comp_events_size NATURAL JOIN
  198. all_comps_view NATURAL JOIN
  199. comp_events_cuts JOIN
  200. comp_styles AS s USING (compunix, stylename) JOIN
  201. comp_levels AS l USING (compunix, levelname)
  202. WHERE (leader=$pID or follower=$pID) and classification='Amateur/Amateur'
  203. ORDER BY getfirstdate ASC;
  204. END_QUERY; $result = $this->query($query);
  205. $numPlacements = $result->numrows();
  206. for ($i=0;$i<$numPlacements;$i++)
  207. {
  208. list($eventID, $comp, $compunix, $compDate, $style, $sAbbrev, $sFull,
  209. $level, $lAbbrev,
  210. $age, $place, $cuts,
  211. $leader, $follower,
  212. $numCouples,$numRounds) = $result->getRowAt($i);
  213. if ($place==0) $place="";
  214. if (!in_array($age,array_keys($placements)))
  215. $placements[$age]=array();
  216. if (!in_array($level,array_keys($placements[$age])))
  217. $placements[$age][$level]=array();
  218. if (!in_array($style,array_keys($placements[$age][$level])))
  219. $placements[$age][$level][$style]=array();
  220. $partnerID = ($leader==$pID ? $follower : $leader);
  221. $query = "SELECT firstname, lastname FROM people WHERE peopleid=$partnerID;";
  222. $partnerResult = $this->query($query);
  223. list($pFirst, $pLast) = $partnerResult->getRowAt(0);
  224. $partnerName = "$pFirst $pLast";
  225. $query =<<< END_QUERY
  226. SELECT DISTINCT dancename, danceabbrev, d.danceorder
  227. FROM comp_events_dances AS d JOIN
  228. comp_style_dances AS s USING(dancename)
  229. WHERE eventid=$eventID AND stylename='$style'
  230. ORDER BY d.danceorder;
  231. END_QUERY; $danceResult = $this->query($query);
  232. $numDances = $danceResult->numrows();
  233. $dances="";
  234. $danceAbbrev="";
  235. for ($j=0;$j<$numDances;$j++)
  236. {
  237. list($dance,$dAbbrev)=$danceResult->getRowAt($j);
  238. if ($j!=0)
  239. $dances .= "/ ";
  240. $dances.=$dance;
  241. $danceAbbrev.=$dAbbrev;
  242. }
  243. for ($j=0;$j<$numDances;$j++)
  244. {
  245. list($dance)=$danceResult->getRowAt($j);
  246. if (!in_array($dance,array_keys($placements[$age][$level][$style])))
  247. $placements[$age][$level][$style][$dance]=array();
  248. $entry = array("Comp"=>$comp,
  249. "CompUnix"=>$compunix,
  250. "Date"=>$compDate,
  251. "Event"=>"$level $sFull $dances",
  252. "EventShort"=>"$lAbbrev-$sAbbrev-$danceAbbrev",
  253. "Partner"=>$partnerName,
  254. "PartnerID"=>$partnerID,
  255. "Place"=>$place,
  256. "CoupleCuts"=>$cuts,
  257. "EventSize"=>$numCouples,
  258. "EventRounds"=>$numRounds);
  259. $placements[$age][$level][$style][$dance][]=$entry;
  260. }
  261. }
  262. return $placements;
  263. }
  264.  
  265. /**
  266. * Returns an exhaustive set of entry information.
  267. * Returns a complete (from the Result Tracker's knowledge base
  268. * perspective) set of entriess for the requested person. The returned
  269. * strcuture is normally fed into the display rountines which
  270. * tabulate points.
  271. *
  272. * @access public
  273. * @param int $pID a peopleid to look up results for
  274. * @return array A deeply nested array whos details are highly likely
  275. * to be refactored soon.
  276. * @todo Refactor results array into a new class
  277. */
  278. function getAllEntriesFor($pID)
  279. {
  280. $placements=array();
  281. $query =<<<END_QUERY
  282. SELECT eventid, compname, compunix, getformatteddate as date,
  283. stylename, s.eventshortprefix, s.eventfullprefix,
  284. levelname, l.eventshortprefix, age_level,
  285. COALESCE(placement,0), num_cuts, leader, follower,
  286. size, rounds
  287. FROM comp_events_cuts NATURAL JOIN result_couples NATURAL JOIN
  288. comp_events NATURAL JOIN
  289. comp_events_size NATURAL JOIN
  290. all_comps_view LEFT JOIN
  291. comp_events_placements USING (eventid, coupleid) JOIN
  292. comp_styles AS s USING (compunix, stylename) JOIN
  293. comp_levels AS l USING (compunix, levelname)
  294. WHERE (leader=$pID or follower=$pID) and classification='Amateur/Amateur'
  295. ORDER BY getfirstdate ASC;
  296. END_QUERY; $result = $this->query($query);
  297. $numPlacements = $result->numrows();
  298. for ($i=0;$i<$numPlacements;$i++)
  299. {
  300. list($eventID, $comp, $compunix, $compDate, $style, $sAbbrev, $sFull,
  301. $level, $lAbbrev,
  302. $age, $place, $cuts,
  303. $leader, $follower,
  304. $numCouples,$numRounds) = $result->getRowAt($i);
  305. if ($place==0) $place="";
  306. if (!in_array($age,array_keys($placements)))
  307. $placements[$age]=array();
  308. if (!in_array($level,array_keys($placements[$age])))
  309. $placements[$age][$level]=array();
  310. if (!in_array($style,array_keys($placements[$age][$level])))
  311. $placements[$age][$level][$style]=array();
  312. $partnerID = ($leader==$pID ? $follower : $leader);
  313. $query = "SELECT firstname, lastname FROM people WHERE peopleid=$partnerID;";
  314. $partnerResult = $this->query($query);
  315. list($pFirst, $pLast) = $partnerResult->getRowAt(0);
  316. $partnerName = "$pFirst $pLast";
  317. $query =<<< END_QUERY
  318. SELECT DISTINCT dancename, danceabbrev, d.danceorder
  319. FROM comp_events_dances AS d JOIN
  320. comp_style_dances AS s USING(dancename)
  321. WHERE eventid=$eventID AND stylename='$style'
  322. ORDER BY d.danceorder;
  323. END_QUERY; $danceResult = $this->query($query);
  324. $numDances = $danceResult->numrows();
  325. $dances="";
  326. $danceAbbrev="";
  327. for ($j=0;$j<$numDances;$j++)
  328. {
  329. list($dance,$dAbbrev)=$danceResult->getRowAt($j);
  330. if ($j!=0)
  331. $dances .= "/ ";
  332. $dances.=$dance;
  333. $danceAbbrev.=$dAbbrev;
  334. }
  335. for ($j=0;$j<$numDances;$j++)
  336. {
  337. list($dance)=$danceResult->getRowAt($j);
  338. if (!in_array($dance,array_keys($placements[$age][$level][$style])))
  339. $placements[$age][$level][$style][$dance]=array();
  340. $entry = array("Comp"=>$comp,
  341. "CompUnix"=>$compunix,
  342. "Date"=>$compDate,
  343. "Event"=>"$level $sFull $dances",
  344. "EventShort"=>"$lAbbrev-$sAbbrev-$danceAbbrev",
  345. "Partner"=>$partnerName,
  346. "PartnerID"=>$partnerID,
  347. "Place"=>$place,
  348. "CoupleCuts"=>$cuts,
  349. "EventSize"=>$numCouples,
  350. "EventRounds"=>$numRounds);
  351. $placements[$age][$level][$style][$dance][]=$entry;
  352. }
  353. }
  354. return $placements;
  355. }
  356.  
  357. /**
  358. * Returns an person's name.
  359. * @access public
  360. * @param int $pid a peopleid to look up for
  361. * @return string "Firstname Lastname"
  362. * @todo Look into a CIB_Person Mapper (light weight compared to the
  363. * Ragu_Person)
  364. */
  365. function getPersonName($pid)
  366. {
  367. $query = "SELECT firstname, lastname FROM people WHERE peopleid=$pid;";
  368. $result = $this->query($query);
  369. list($fname,$lname) = $result->getRowAt(0);
  370. $name = "$fname $lname";
  371. return $name;
  372. }
  373.  
  374. /**
  375. * Returns an person's hometwon or "No adddress lists".
  376. * @access public
  377. * @param int $pid a peopleid to look up for
  378. * @return string "City, State"
  379. * @todo Look into a CIB_Person Mapper (light weight compared to the
  380. * Ragu_Person)
  381. */
  382. function getHomeTown($pid)
  383. {
  384. $query = "SELECT city, state FROM people_addresses WHERE peopleid=$pid;";
  385. $result = $this->query($query);
  386. if ($result->numrows())
  387. {
  388. list($city,$state) = $result->getRowAt(0);
  389. $addr = "$city, $state";
  390. }
  391. else
  392. $addr = "No address listed";
  393. return $addr;
  394. }
  395. /**
  396. * Returns an person's team memberships
  397. * @access public
  398. * @param int $pid a peopleid to look up for
  399. * @return string "Team[, Team2[, Team3...]]"
  400. * @todo Look into a CIB_Person Mapper (light weight compared to the
  401. * Ragu_Person)
  402. */
  403. function getAffiliation($pid)
  404. {
  405. $query = "SELECT name FROM team_membership NATURAL JOIN teams WHERE peopleid=$pid;";
  406. $result = $this->query($query);
  407. $numRows = $result->numrows();
  408. if ($numRows==0)
  409. $team = "No listed affiliation";
  410. else
  411. $team="";
  412. for ($i=0;$i<$numRows;$i++)
  413. {
  414. if ($i!=0)
  415. $team .= ", ";
  416. list($aTeam) = $result->getRowAt($i);
  417. $team .= $aTeam;
  418. }
  419. return $aTeam;
  420. }
  421. /**
  422. * Returns a list of all competitions a person has competed in.
  423. * @access public
  424. * @param int $personID a peopleid to look up for
  425. * @return array array of arrays, each numerically index subitem:
  426. * - "Comp"=>Full Competition Name
  427. * - "Unix"=>Unix Namee of Competition
  428. * - "Date"=>Formatted date(s) of the competition
  429. * The subitems are entered in oldest first order.
  430. */
  431. function getCompHistory($personID)
  432. {
  433. $query =<<<END_QUERY
  434. SELECT DISTINCT compname, compunix,
  435. getformatteddate AS compDate,
  436. getfirstdate AS sortDate
  437. FROM comp_events_cuts NATURAL JOIN result_couples NATURAL JOIN
  438. comp_events NATURAL JOIN all_comps_view
  439. WHERE leader=$personID or follower=$personID
  440. ORDER BY sortDate;
  441. END_QUERY; $result=$this->query($query);
  442. $numComps = $result->numrows();
  443. $comps=array();
  444. for ($i=0;$i<$numComps;$i++)
  445. {
  446. list($compname, $compunix, $compDate) = $result->getRowAt($i);
  447. $comps[]=array("Comp"=>$compname,
  448. "Unix"=>$compunix,
  449. "Date"=>$compDate);
  450. }
  451. return $comps;
  452. }
  453. }
  454. ?>

Documentation generated on Tue, 25 Apr 2006 13:09:38 -0400 by phpDocumentor 1.3.0RC3