Newer
Older
Handbook / calendar / generate_calendar.php
  1. <?php
  2.  
  3. /*
  4. This script generates a LaTeX include file that contains macros specifying
  5. the date ranges for every week of the three main teaching periods through
  6. the year (i.e., summer school, first semester, second semester). Simply set
  7. the configuration as outlined below and run the script, saving the output
  8. in a suitable location, ideally only once for all papers. A good spot would
  9. be somewhere in the TeX search path.
  10. */
  11.  
  12. // LaTeX macro names can't include numbers, so we have to convert them to words.
  13. require 'Numbers/Words.php';
  14.  
  15. /*
  16. Teaching period configuration. It's easily extensible to a new teaching
  17. period simply by adding a new specification for that period. The key should
  18. be valid as a LaTeX macro name (i.e., no spaces, no digits, no punctuation).
  19. The components of each period specification are:
  20. start_date: The date of the first day of the first week of the period.
  21. num_weeks: The total number of weeks in the period, /including/ any
  22. breaks (e.g., 13 teaching weeks plus 1 week mid-semester
  23. break = 14 weeks).
  24. break_weeks: A list of breaks that occur during the teaching period.
  25. Each is specified by the start (break_start) and end
  26. (break_end) week. If a break is only one week long, then
  27. break_start and break_end should be equal. If there are no
  28. breaks in the teaching period, include a single break
  29. specification with both break_start and break_end set to zero.
  30. The list should ideally be in ascending order, but the
  31. script doesn't assume this and sorts the list anyway. Weeks
  32. are numbered by absolute calendar week number (i.e., week
  33. 1 is the first calendar week of the year). The date of the
  34. first Monday of the year is stored in $first_monday below
  35. (everything is Monday-indexed).
  36.  
  37. Example:
  38. $year = 2013;
  39. $first_monday = new DateTime( "2012-12-31" );
  40. $periods = array(
  41. 'SummerSchool' => array(
  42. 'first_week' => 2,
  43. 'last_week' => 7,
  44. 'break_weeks' => array(
  45. array(
  46. 'break_starts' => 0,
  47. 'break_ends' => 0,
  48. ),
  49. ),
  50. ),
  51. 'SemesterOne' => array(
  52. 'first_week' => 9,
  53. 'last_week' => 22,
  54. 'break_weeks' => array(
  55. array(
  56. 'break_starts' => 14,
  57. 'break_ends' => 14,
  58. ),
  59. ),
  60. ),
  61. 'SemesterTwo' => array(
  62. 'first_week' => 28,
  63. 'last_week' => 41,
  64. 'break_weeks' => array(
  65. array(
  66. 'break_starts' => 35,
  67. 'break_ends' => 35,
  68. ),
  69. ),
  70. ),
  71. 'FullYear' => array(
  72. 'first_week' => 9,
  73. 'last_week' => 41,
  74. 'break_weeks' => array(
  75. array(
  76. 'break_starts' => 23,
  77. 'break_ends' => 27,
  78. ),
  79. array(
  80. 'break_starts' => 15,
  81. 'break_ends' => 15,
  82. ),
  83. array(
  84. 'break_starts' => 35,
  85. 'break_ends' => 35,
  86. ),
  87. ),
  88. ),
  89. );
  90. */
  91. require 'calendar_config.php';
  92.  
  93.  
  94. // A couple of handy date intervals: four days to the end of the current week,
  95. // and seven days to the start of next week.
  96. $plus_four_days = new DateInterval( 'P4D' );
  97. $plus_seven_days = new DateInterval( 'P7D' );
  98.  
  99. $numwords = new Numbers_Words();
  100.  
  101. printf( "%% Teaching calendar dates for %s.\n", $year );
  102.  
  103. foreach ( $periods as $period_name => $period_data )
  104. {
  105. $week_start = clone( $first_monday );
  106. // Jump forward the correct number of weeks to the start of the teaching period.
  107. $week_start->add( new DateInterval( sprintf( "P%dD", ( $period_data['first_week'] - 1 ) * 7 ) ) );
  108. $week_number = 0;
  109. $num_breaks = 0;
  110. // Grab the first break week off the front of the list.
  111. sort( $period_data['break_weeks'] );
  112. $break_week = array_shift( $period_data['break_weeks'] );
  113. for ( $week = $period_data['first_week']; $week <= $period_data['last_week']; $week++ )
  114. {
  115. $week_end = clone( $week_start );
  116. $week_end->add( $plus_four_days );
  117. // Handle break weeks.
  118. if ( ( $week >= $break_week['break_starts'] ) && ( $week <= $break_week['break_ends'] ) )
  119. {
  120. if ( $week == $break_week['break_starts'] )
  121. {
  122. $num_breaks++;
  123. // Work out the end date of the break.
  124. $break_end = clone( $week_start );
  125. // Note: +4 days to get to Friday.
  126. $break_end->add(
  127. new DateInterval(
  128. sprintf( "P%dD", ( ( $break_week['break_ends'] - $break_week['break_starts'] ) * 7 ) + 4 )
  129. )
  130. );
  131. // Output macro for inclusion in paper calendar.
  132. printf( "\\newcommand{\\%sBreak%s}{%s--%s}\n",
  133. $period_name,
  134. str_replace( "-", "", ucfirst( $numwords->toWords( $num_breaks ) ) ),
  135. // If the week start and end dates are in the same month, we only
  136. // need to output the month name once.
  137. ( $week_start->format( 'n' ) == $break_end->format( 'n' ) ) ? $week_start->format( 'j' ) : $week_start->format( 'j M' ),
  138. $break_end->format( 'j M' )
  139. );
  140. } // if current week == first week of break
  141. if ( $week == $break_week['break_ends'] )
  142. {
  143. // Grab the next break week off the front of the list.
  144. $break_week = array_shift( $period_data['break_weeks'] );
  145. } // if current week == last week of break
  146. } // if current week is a break week
  147. else
  148. {
  149. $week_number++;
  150. // If the week start and end dates are in the same month, we only
  151. // need to output the month name once.
  152. if ( $week_start->format( 'n' ) == $week_end->format( 'n' ) )
  153. {
  154. // Output macro for inclusion in paper calendar.
  155. printf( "\\newcommand{\\%sWeek%s}{%s--%s \\\\ %s}\n",
  156. $period_name,
  157. str_replace( "-", "", ucfirst( $numwords->toWords( $week_number ) ) ),
  158. $week_start->format( 'j' ),
  159. $week_end->format( 'j' ),
  160. $week_end->format( 'M' )
  161. );
  162. } // if same month
  163. else
  164. {
  165. // Output macro for inclusion in paper calendar.
  166. printf( "\\newcommand{\\%sWeek%s}{%s \\\\ --%s}\n",
  167. $period_name,
  168. str_replace( "-", "", ucfirst( $numwords->toWords( $week_number ) ) ),
  169. $week_start->format( 'j M' ),
  170. $week_end->format( 'j M' )
  171. );
  172. } // else different months
  173. } // else normal teaching week
  174. $week_start->add( $plus_seven_days );
  175. } // for each week
  176. } // foreach teaching period
  177.  
  178. ?>