paristemi
[ class tree: paristemi ] [ index: paristemi ] [ all elements ]

Source for file db_studies.php

Documentation is available at db_studies.php

  1. <?php
  2. /**
  3. * <b>Database Class : Studies</b><br />
  4. * Provides all database access methods for the studies/events section
  5. * @author Kristen O'Brien <kristen_paristemi-com>
  6. * @copyright Copyright 2004, Kristen O'Brien
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. * @link http://www.paristemi.com Paristemi Main Site
  9. * @package paristemi
  10. * @subpackage paristemi_database_layer
  11. * @since Build 0.5
  12. * @version Build 0.7
  13. * @filesource
  14. */
  15. /**
  16. * <b>Database Class : Studies</b><br />
  17. * Provides all database access methods for the studies/events section
  18. * @author Kristen O'Brien <kristen_paristemi-com>
  19. * @copyright Copyright 2004, Kristen O'Brien
  20. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  21. * @link http://www.paristemi.com Paristemi Main Site
  22. * @package paristemi
  23. * @subpackage paristemi_database_layer
  24. * @since Build 0.5
  25. * @version Build 0.7
  26. * @filesource
  27. */
  28. class DB_Studies {
  29. function DB_Studies() { }
  30. function &getWeekdayNumber($weekday)
  31. {
  32. switch($weekday) {
  33. case 'Sunday' : return 0;
  34. case 'Monday' : return 1;
  35. case 'Tuesday' : return 2;
  36. case 'Wednesday' : return 3;
  37. case 'Thursday' : return 4;
  38. case 'Friday' : return 5;
  39. case 'Saturday' : return 6;
  40. default : return null;
  41. }
  42. }
  43. // functions taken from calendar include so this can stand alone
  44. function &nextDay ($day_marker,$begin_week)
  45. {
  46. $total_days = getdate(mktime(0,0,0,$begin_week['mon']+1,1,$begin_week['year'])-1);
  47. if($total_days['mday'] < ($begin_week['mday']+$day_marker)) {
  48. if($today['mon']+1 > 12) {
  49. $next_day = getdate(mktime(0,0,0,1,($begin_week['mday']+day_marker) - $total_days['mday'], $begin_week['year']+1));
  50. }
  51. else {
  52. $next_day = getdate(mktime(0, 0, 0, ($begin_week['mon']+1), (($begin_week['mday']+$day_marker) - $total_days['mday']), $begin_week['year']));
  53. }
  54. }
  55. else {
  56. $next_day = getdate(mktime(0,0,0,$begin_week['mon'],($begin_week['mday']+($day_marker)),$begin_week['year']));
  57. }
  58. return $next_day;
  59. }
  60. function &beginWeek ($today)
  61. {
  62. $diff_begin = ($today['mday'] - $today['wday']);
  63. if($diff_begin < 1) {
  64. if($today['mon'] == 1) {
  65. $end_day = getdate(mktime(0,0,0,1,1,$today['year'])-1);
  66. $begin_week = getdate(mktime(0,0,0,12,$end_day['mday']+$diff_begin,$today['year']-1));
  67. }
  68. else {
  69. $end_day = getdate(mktime(0,0,0,$today['mon'],1,$today['year'])-1);
  70. $begin_week = getdate(mktime(0,0,0,$today['mon']-1,$end_day['mday']+$diff_begin,$today['year']));
  71. }
  72. }
  73. else {
  74. $begin_week = getdate(mktime(0, 0, 0, $today['mon'], $diff_begin, $today['year']));
  75. }
  76. return $begin_week;
  77. }
  78. // Given an ID return the study information
  79. // ID, Title, Timestamp, First, Last, Description, LocationName, LocationID
  80. function &GetStudyInfo(&$id) {
  81.  
  82. $result = mysql_query("select i.Title as InstanceTitle, i.ID as iID,
  83. i.Recursive, i.RecDay, S.MeetByMonthDays,
  84. unix_timestamp(i.Start) as iStart,
  85. unix_timestamp(i.RecEnd) as RecursiveEnd
  86. from Instance i, Series S
  87. where (i.SeriesID = $id OR i.SubSeriesID = $id)
  88. AND S.ID = $id
  89. group by i.Start;");
  90. $temp[] = array(); $meetdays="";
  91. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  92. $temp[] = new BibleStudyDate($id,$row['InstanceTitle'],$row['iID'],
  93. $row['iStart'],$row['Recursive'],$row['RecDay'],$row['RecursiveEnd']);
  94. $meetdays = $row['MeetByMonthDays'];
  95. }
  96. $today = time();
  97. $bestdate = null;
  98. $diffval = 999999999; //impossibly large value
  99. $stamp = "";
  100. foreach($temp as $value) {
  101. if($value->Recursive && $value->RecEnd >= $today) {
  102. $todaydate = getdate($today);
  103. $day= $todaydate['mday'];
  104. $mkr = $day; $addflg=false;$useday = 0;
  105. if($meetdays==0) { $addflg = true; }
  106. else {
  107. $laststamp=mktime(23,59,59,$todaydate['mon'],$todaydate['mday'],$todaydate['year']);
  108. $last = getdate($laststamp);
  109. $nextmonth = $last['mon']+1;
  110. $nextyear = $last['year'];
  111. if($nextmonth > 12) { $nextmonth = 1; $nextyear++; }
  112. $endstamp = (mktime(0,0,0,$nextmonth,1,$nextyear)-1);
  113. $endmonth = getdate($endstamp);
  114. $endrec = getdate($value->RecEnd);
  115. $meetdays = explode(',',$meetdays);
  116. while($mkr > 0) { $mkr = $mkr-7; }
  117. $continue=true;
  118. while($continue) {
  119. if($endrec['mon']==$endmonth['mon'] && $endrec['year']==$endrec['year']) {
  120. $endmday = $endrec['mday']; }
  121. else { $endmday = $endmonth['mday']; }
  122. $mkr = $endmday; $dayarr=array();
  123. while($mkr > 0) { $mkr = $mkr-7; }
  124. $i=$mkr; while($i<=$endmday) { $dayarr[]=$i;$i=$i+7; }
  125. foreach($meetdays as $meetingdays) {
  126. if($dayarr[$meetingdays] >= $day) {
  127. $addflg=true; $useday=$dayarr[$meetingdays];
  128. $usemon=$endmonth['mon']; $useyear=$endmonth['year']; break; } }
  129. $nextmonth = $nextmonth+2;
  130. $nextyear = $nextyear;
  131. if($nextmonth > 12) { $nextmonth = 1; $nextyear++; }
  132. $endstamp = (mktime(0,0,0,$nextmonth,1,$nextyear)-1);
  133. $endmonth = getdate($endstamp);
  134. if($value->RecEnd >= $endstamp){$continue=true;}
  135. elseif($endrec['mon'] >= $endmonth['mon']){
  136. if($endrec['year']==$endmonth['year']) {$continue=true;}
  137. else {$continue=false;}}
  138. elseif($endrec['year'] > $endmonth['year']){$continue=true;}
  139. else{$continue=false;}
  140. $day =1;
  141. }
  142. }
  143. if($addflg) {
  144. $start = getdate($value->iStart);
  145. $recday = $this->getWeekdayNumber($value->RecDay);
  146. $recdate = $this->nextDay($recday,$this->beginWeek($todaydate));
  147. $recstamp = mktime($start['hours'],$start['minutes'],$start['seconds'],
  148. $recdate['mon'],$recdate['mday'],$recdate['year']);
  149. if($useday==0) {
  150. if($recstamp < $today) {
  151. $todaydate = getdate($today+604800);
  152. $recdate = $this->nextDay($recday,$this->beginWeek($todaydate));
  153. $recstamp = mktime($start['hours'],$start['minutes'],$start['seconds'],
  154. $recdate['mon'],$recdate['mday'],$recdate['year']);
  155. }
  156. }
  157. else {
  158. $recstamp = mktime($start['hours'],$start['minutes'],$start['seconds'],
  159. $usemon,$useday,$useyear);
  160. }
  161. $diffval = ($recstamp-$today);
  162. $bestdate = $value;
  163. $stamp = $recstamp;
  164. }
  165. }
  166. else if($value->iStart > $today) {
  167. if((($value->iStart)-$today) < $diffval) {
  168. $diffval = (($value->iStart)-$today);
  169. $bestdate = $value;
  170. $stamp = $value->iStart;
  171. }
  172. }
  173. }
  174. $result2 = mysql_query("select s.ID, s.Title,
  175. time_format(s.Start, '%l:%i %p') as Start,
  176. time_format(s.Stop, '%l:%i %p') as Stop,
  177. s.Day, s.MeetByMonthDays, s.Description, s.Type,
  178. l.Name as LocationName, l.ID as LocationID
  179. from Series s, Location l
  180. where s.ID = $id and s.LocationID = l.ID
  181. group by s.Day, s.Start, s.Title
  182. limit 1;");
  183.  
  184. $row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
  185. //Process the row.
  186. $data = new BibleStudyDetailed($row2['ID'], $row2['Title'],
  187. $bestdate->iTitle, $bestdate->iID, $stamp,
  188. $row2['Start'], $row2['Stop'], $row2['Day'], $row2['MeetByMonthDays'],
  189. $row2['Description'], $row2['Type'], $row2['LocationName'],
  190. $row2['LocationID']);
  191.  
  192. return $data;
  193. }
  194.  
  195.  
  196. // Return a list of current studies
  197. // ID, Timestamp, Title
  198. function &GetAllStudies() {
  199. $data = Array();
  200.  
  201. $result = mysql_query("select s.ID, s.Title,
  202. time_format(s.Start, '%l:%i %p') as Start,
  203. time_format(s.Stop, '%l:%i %p') as Stop,
  204. s.Day, s.MeetByMonthDays, s.ShowItem, s.Type
  205. from Series s
  206. group by s.Day, s.Start, s.Title;");
  207. //Process each row.
  208. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  209. $data[] = new BibleStudy($row['ID'], $row['Title'],
  210. $row['Start'], $row['Stop'],$row['Day'], $row['MeetByMonthDays'],
  211. $row['Type']);
  212. }
  213.  
  214. return $data;
  215. }
  216.  
  217. // Return a list of current studies
  218. // ID, Timestamp, Title
  219. function &GetCurrentStudies() {
  220. $data = Array();
  221.  
  222. $result = mysql_query("select s.ID, s.Title,
  223. time_format(s.Start, '%l:%i %p') as Start,
  224. time_format(s.Stop, '%l:%i %p') as Stop,
  225. s.Day, s.MeetByMonthDays, s.ShowItem, s.Type
  226. from Series s
  227. where s.ShowItem > 0
  228. group by s.Day, s.Start, s.Title;");
  229. //Process each row.
  230. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  231. $data[] = new BibleStudy($row['ID'], $row['Title'],
  232. $row['Start'], $row['Stop'],$row['Day'], $row['MeetByMonthDays'],
  233. $row['Type']);
  234. }
  235.  
  236. return $data;
  237. }
  238. }
  239.  
  240. /**
  241. * <b>Database Container Class : Study</b><br />
  242. * Contains all of the important data for a study.
  243. * @author Kristen O'Brien <kristen_paristemi-com>
  244. * @copyright Copyright 2004, Kristen O'Brien
  245. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  246. * @link http://www.paristemi.com Paristemi Main Site
  247. * @package paristemi
  248. * @subpackage paristemi_database_layer
  249. * @since Build 0.5
  250. * @version Build 0.7
  251. * @filesource
  252. */
  253. class BibleStudy {
  254. var $ID;
  255. var $Title;
  256. var $Start;
  257. var $Stop;
  258. var $Day;
  259. var $MeetByMonth;
  260. var $Type;
  261.  
  262. function BibleStudy($id, $title, $start, $stop, $day, $meet, $type) {
  263. $this->ID = $id;
  264. $this->Title = $title;
  265. $this->Start = $start;
  266. $this->Stop = $stop;
  267. $this->Day = $day;
  268. $this->MeetByMonth = $meet;
  269. $this->Type = $type;
  270. }
  271. }
  272.  
  273. /**
  274. * <b>Database Container Class : Study</b><br />
  275. * Contains all of the date specific data for a study.
  276. * @author Kristen O'Brien <kristen_paristemi-com>
  277. * @copyright Copyright 2004, Kristen O'Brien
  278. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  279. * @link http://www.paristemi.com Paristemi Main Site
  280. * @package paristemi
  281. * @subpackage paristemi_database_layer
  282. * @since Build 0.5
  283. * @version Build 0.7
  284. * @filesource
  285. */
  286. class BibleStudyDate {
  287. var $ID;
  288. var $iTitle;
  289. var $iID;
  290. var $iStart;
  291. var $Recursive;
  292. var $RecDay;
  293. var $RecEnd;
  294. function BibleStudyDate($id, $ititle, $iid, $istart,$rec,$recday,$recend) {
  295. $this->iTitle = $ititle;
  296. $this->iID = $iid;
  297. $this->iStart = $istart;
  298. $this->Recursive = $rec;
  299. $this->RecDay = $recday;
  300. $this->RecEnd = $recend;
  301. }
  302. }
  303.  
  304. /**
  305. * <b>Database Container Class : Study Detailed</b><br />
  306. * Contains all of the specific data for a study.
  307. * @author Kristen O'Brien <kristen_paristemi-com>
  308. * @copyright Copyright 2004, Kristen O'Brien
  309. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  310. * @link http://www.paristemi.com Paristemi Main Site
  311. * @package paristemi
  312. * @subpackage paristemi_database_layer
  313. * @since Build 0.5
  314. * @version Build 0.7
  315. * @filesource
  316. */
  317. class BibleStudyDetailed {
  318. var $ID;
  319. var $Title;
  320. var $InstanceTitle;
  321. var $iID;
  322. var $iStart;
  323. var $Start;
  324. var $Stop;
  325. var $Day;
  326. var $MeetByMonth;
  327. var $Description;
  328. var $Type;
  329. var $LocationName;
  330. var $LocationID;
  331. function BibleStudyDetailed($id, $title, $instancetitle, $iid, $istart,
  332. $start, $stop, $day, $meetbymonth, $description, $type, $locationname, $locationid) {
  333. $this->ID = $id;
  334. $this->Title = $title;
  335. $this->InstanceTitle = $instancetitle;
  336. $this->iID = $iid;
  337. $this->iStart = $istart;
  338. $this->Start = $start;
  339. $this->Stop = $stop;
  340. $this->Day = $day;
  341. $this->MeetByMonth = $meetbymonth;
  342. $this->Description = $description;
  343. $this->Type = $type;
  344. $this->LocationName = $locationname;
  345. $this->LocationID = $locationid;
  346. }
  347. }
  348.  
  349. ?>

Documentation generated on Mon, 10 May 2004 12:09:28 -0700 by phpDocumentor 1.3.0RC3