Newer
Older
XML / generate_calendar_dates.php
  1. <?php
  2.  
  3. /*
  4. This script generates an XSLT include file that contains templates specifying the date ranges for every week of the three main teaching periods through the year (i.e., summer school, first semester, second semester). Simply set the configuration as outlined below and make to regenerate the templates.
  5. */
  6.  
  7.  
  8. /*
  9. Date of the Monday of the first academic week of the year. Teaching weeks start on Monday, so this is a convenient base point to start from. This will need to be updated annually.
  10. */
  11. $first_monday = new DateTime( "2016-01-04" );
  12.  
  13.  
  14. /*
  15. Teaching period configuration. This will need to be updated annually. It's easily extensible to a new teaching period simply by adding a new specification for that period. The key should be mixed-case alphanumeric (i.e., no whitespace, no punctuation or other special characters). The components of each period specification are:
  16. first_week: The academic week number of the first week of the period.
  17. last_week: The academic week number of the last week of the period.
  18. break_weeks: A list of breaks that occur during the teaching period.
  19. Each is specified by the start (break_start) and end
  20. (break_end) week. If a break is only one week long, then
  21. break_start and break_end should be equal. If there are no
  22. breaks in the teaching period, include a single break
  23. specification with both break_start and break_end set to zero.
  24. The list should ideally be in ascending order, but the
  25. script doesn't assume this and sorts the list anyway. Weeks
  26. are numbered by academic week number rather than week within
  27. the teaching period.
  28. */
  29. $periods = array(
  30. 'SS' => array(
  31. 'first_week' => 2,
  32. 'last_week' => 7,
  33. 'break_weeks' => array(
  34. array(
  35. 'break_starts' => 0,
  36. 'break_ends' => 0,
  37. ),
  38. ),
  39. ),
  40. 'S1' => array(
  41. 'first_week' => 9,
  42. 'last_week' => 22,
  43. 'break_weeks' => array(
  44. array(
  45. 'break_starts' => 13,
  46. 'break_ends' => 13,
  47. ),
  48. ),
  49. ),
  50. 'S2' => array(
  51. 'first_week' => 28,
  52. 'last_week' => 41,
  53. 'break_weeks' => array(
  54. array(
  55. 'break_starts' => 35,
  56. 'break_ends' => 35,
  57. ),
  58. ),
  59. ),
  60. 'FY' => array(
  61. 'first_week' => 9,
  62. 'last_week' => 41,
  63. 'break_weeks' => array(
  64. array(
  65. 'break_starts' => 13,
  66. 'break_ends' => 13,
  67. ),
  68. array(
  69. 'break_starts' => 23,
  70. 'break_ends' => 27,
  71. ),
  72. array(
  73. 'break_starts' => 35,
  74. 'break_ends' => 35,
  75. ),
  76. ),
  77. ),
  78. # This represents the whole year from start to finish.
  79. 'YY' => array(
  80. 'first_week' => 1,
  81. 'last_week' => 52,
  82. 'break_weeks' => array(),
  83. ),
  84. );
  85.  
  86.  
  87. // We need a condition to validate the period code in the templates.
  88. $period_condition = sprintf( "( @period = '%s' )", implode( "' ) or ( @period = '", array_keys( $periods ) ) );
  89.  
  90. // We also a list of valid period code values for the error string.
  91. $period_list = sprintf( '"%s"', implode( '", "', array_keys( $periods ) ) );
  92.  
  93. // The period and week variables are defined identically in both templates, so
  94. // let's define them just once here to ensure consistency.
  95. $shared_parameters = <<<EOT
  96. <xsl:with-param name="period">
  97. <xsl:choose>
  98. <xsl:when test="{$period_condition}">
  99. <xsl:value-of select="@period" />
  100. </xsl:when>
  101. <!-- This also covers the case of @period being undefined. -->
  102. <xsl:otherwise>
  103. <xsl:message terminate="yes">
  104. <xsl:text>Attribute "period" must be one of the values {$period_list}.</xsl:text>
  105. </xsl:message>
  106. </xsl:otherwise>
  107. </xsl:choose>
  108. </xsl:with-param>
  109. <xsl:with-param name="week">
  110. <xsl:value-of select="@week" />
  111. <xsl:if test="not( @week )">
  112. <xsl:message terminate="yes">
  113. <xsl:text>The "week" attribute is required.</xsl:text>
  114. </xsl:message>
  115. </xsl:if>
  116. </xsl:with-param>
  117. EOT;
  118.  
  119.  
  120. // Generate the file header and the wrapper templates.
  121. print<<<EOT
  122. <?xml version="1.0" encoding="utf-8"?>
  123.  
  124.  
  125. <!--
  126. DO NOT EDIT! Automatically generated by ../generate_calendar_dates.php!
  127. Elements and templates for generating teaching period dates. Apart from the wrapper templates, this essentially boils down to a giant parameterised lookup table, which is generated by a script.
  128. -->
  129.  
  130.  
  131. <stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  132.  
  133. <!--
  134. Generate a date for a given teaching week in a teaching period, in the specified format. Return the date of the Monday of the week, plus an optional signed offset.
  135. @period: The teaching period for which to generate the date. [required]
  136. @week: The number of the week in the specified teaching period. [required]
  137. @format: How to format the date.
  138. 'day+long-name' => [FNn] [D] [MNn] (e.g., Friday 10 August)
  139. 'day+short-name' => [FNn,*-3] [D] [MNn] (e.g., Fri 10 August)
  140. 'day' => [D] [MNn] (e.g., 10 August)
  141. 'day-short' => [D] [MNn,*-3] (e.g., 10 Aug)
  142. 'day+year' => [D] [MNn], [Y] (e.g., 10 August, 2013)
  143. 'day-short+year' => [D] [MNn,*-3], [Y] (e.g., 10 Aug, 2013)
  144. 'ISO' => [Y0001]-[M01]-[D01] (e.g., 2012-08-10)
  145. date picture string => Any valid XSLT date picture specification (see http://www.w3.org/TR/xslt20/#date-picture-string).
  146. @offset: A signed offset specified as an XSLT duration, e.g., "P1D" for plus one day, "-P4D" for minus four days.
  147. -->
  148. <template name="TeachingPeriodDate" match="TeachingPeriodDate">
  149. <common>
  150. <xsl:call-template name="format-teaching-date">
  151. {$shared_parameters}
  152.  
  153. <xsl:with-param name="format">
  154. <xsl:choose>
  155. <!-- Named date formats. -->
  156. <xsl:when test="@format = 'day+long-name'">
  157. <xsl:text>[FNn] [D] [MNn]</xsl:text>
  158. </xsl:when>
  159. <xsl:when test="@format = 'day+short-name'">
  160. <xsl:text>[FNn,*-3] [D] [MNn]</xsl:text>
  161. </xsl:when>
  162. <xsl:when test="@format = 'day'">
  163. <xsl:text>[D] [MNn]</xsl:text>
  164. </xsl:when>
  165. <xsl:when test="@format = 'day-short'">
  166. <xsl:text>[D] [MNn,*-3]</xsl:text>
  167. </xsl:when>
  168. <xsl:when test="@format = 'day+year'">
  169. <xsl:text>[D] [MNn], [Y]</xsl:text>
  170. </xsl:when>
  171. <xsl:when test="@format = 'day-short+year'">
  172. <xsl:text>[D] [MNn,*-3], [Y]</xsl:text>
  173. </xsl:when>
  174. <xsl:when test="@format = 'ISO'">
  175. <xsl:text>[Y0001]-[M01]-[D01]</xsl:text>
  176. </xsl:when>
  177. <xsl:otherwise>
  178. <xsl:value-of select="@format" />
  179. </xsl:otherwise>
  180. </xsl:choose>
  181. <xsl:if test="not( @format )">
  182. <xsl:text>[Y0001]-[M01]-[D01]</xsl:text>
  183. </xsl:if>
  184. </xsl:with-param>
  185. <xsl:with-param name="offset">
  186. <xsl:value-of select="@offset" />
  187. <xsl:if test="not( @offset )">
  188. <xsl:text>P0D</xsl:text>
  189. </xsl:if>
  190. </xsl:with-param>
  191. </xsl:call-template>
  192. </common>
  193. </template>
  194. <!--
  195. Generate a date range corresponding to a given teaching week in a teaching period (i.e., Monday to Friday).
  196. @period: The teaching period for which to generate the date. [required]
  197. @week: The number of the week in the specified teaching period. [required]
  198. @format: How to format the month name in the date range.
  199. 'long' => Output the full month name (e.g., August)
  200. 'short' => Output only the first three characters of the month name (e.g., Aug)
  201. @wrap: Whether to wrap the date range across two lines (usually for insertion into a narrow cell in a calendar table).
  202. 'true' [default]
  203. 'false'
  204. -->
  205. <template name="TeachingPeriodDateRange" match="TeachingPeriodDateRange">
  206. <common>
  207. <xsl:call-template name="format-teaching-date-range">
  208. {$shared_parameters}
  209.  
  210. <xsl:with-param name="month-format">
  211. <xsl:choose>
  212. <xsl:when test="@format = 'long'">
  213. <xsl:text>[MNn]</xsl:text>
  214. </xsl:when>
  215. <xsl:when test="@format = 'short'">
  216. <xsl:text>[MNn,*-3]</xsl:text>
  217. </xsl:when>
  218. <xsl:otherwise>
  219. <xsl:message terminate="yes">
  220. <xsl:text>Attribute "format" must be either "long" or "short", not "</xsl:text>
  221. <xsl:value-of select="@format" />
  222. <xsl:text>".</xsl:text>
  223. </xsl:message>
  224. </xsl:otherwise>
  225. </xsl:choose>
  226. </xsl:with-param>
  227. <!-- Convert @wrap into a boolean value. -->
  228. <xsl:with-param name="wrap">
  229. <xsl:value-of select="@wrap = 'yes'" />
  230. </xsl:with-param>
  231. </xsl:call-template>
  232. </common>
  233. </template>
  234.  
  235. EOT;
  236.  
  237. // Generate the XSLT templates that do all the work. These essentially end up being huge parameterised lookup tables.
  238. generate_template( $first_monday, $periods, 'format-teaching-date' );
  239.  
  240. generate_template( $first_monday, $periods, 'format-teaching-date-range' );
  241.  
  242.  
  243. // Footer boilerplate.
  244. print( "</stylesheet>\n" );
  245.  
  246. exit;
  247.  
  248.  
  249. ////////////////////////////////////////////////////////////////////////////////
  250.  
  251.  
  252. function generate_template( $first_monday, $periods, $template_name )
  253. {
  254. // A couple of handy date intervals: four days to the end of the current week,
  255. // and seven days to the start of next week.
  256. $plus_four_days = new DateInterval( 'P4D' );
  257. $plus_seven_days = new DateInterval( 'P7D' );
  258.  
  259.  
  260. // Initial boilerplate for the template.
  261. switch ( $template_name )
  262. {
  263. case 'format-teaching-date':
  264. print<<<EOT
  265. <!--
  266. Generate a date (default Monday) for the specified teaching period and week (or break), using the specified date format, with an optional offset from the Monday of the week. Most of the body of this template is automatically generated by the script ../generate_calendar_dates.php.
  267. See the attributes of the TeachingPeriodDate template above for descriptions of the parameters.
  268. -->
  269. <template name="format-teaching-date">
  270. <common>
  271. <xsl:param name="period" as="xs:string" />
  272. <xsl:param name="week" as="xs:string" />
  273. <xsl:param name="format" as="xs:string" />
  274. <xsl:param name="offset" as="xs:dayTimeDuration" />
  275. <xsl:choose>
  276.  
  277. EOT;
  278. break;
  279. case 'format-teaching-date-range':
  280. print<<<EOT
  281. <!--
  282. Generate a date range for the specified teaching period and week (or break), using the specified month format, with optional wrapping. Most of the body of this template is automatically generated by the script ../generate_calendar_dates.php.
  283. See the attributes of the TeachingPeriodDateRange template above for descriptions of the parameters. \$month-format is derived from @format.
  284. -->
  285. <template name="format-teaching-date-range">
  286. <common>
  287. <xsl:param name="period" as="xs:string" />
  288. <xsl:param name="week" as="xs:string" />
  289. <xsl:param name="month-format" as="xs:string" />
  290. <xsl:param name="wrap" as="xs:boolean" />
  291. <xsl:choose>
  292.  
  293. EOT;
  294. break;
  295. default: // WTF?!?
  296. print "Unrecognised template name \"$template_name\", terminating.\n";
  297. exit;
  298. break;
  299. } // switch template name
  300.  
  301. // Generate XSLT code for each teaching period.
  302. foreach ( $periods as $period_name => $period_data )
  303. {
  304. print<<<EOT
  305. <xsl:when test="\$period = '{$period_name}'">
  306. <xsl:choose>
  307.  
  308. EOT;
  309. $week_start = clone( $first_monday );
  310. // Jump forward the correct number of weeks to the start of the teaching period.
  311. $week_start->add( new DateInterval( sprintf( "P%dD", ( $period_data['first_week'] - 1 ) * 7 ) ) );
  312. $week_number = 0;
  313. $num_breaks = 0;
  314. // Grab the first break week off the front of the list.
  315. sort( $period_data['break_weeks'] );
  316. $break_week = array_shift( $period_data['break_weeks'] );
  317. // Generate XSLT code for each teaching week and breaks within the period.
  318. for ( $week = $period_data['first_week']; $week <= $period_data['last_week']; $week++ )
  319. {
  320. $week_end = clone( $week_start );
  321. $week_end->add( $plus_four_days );
  322. // Handle breaks.
  323. if ( ( $week >= $break_week['break_starts'] ) && ( $week <= $break_week['break_ends'] ) )
  324. {
  325. if ( $week == $break_week['break_starts'] )
  326. {
  327. $num_breaks++;
  328. // Work out the end date of the break.
  329. $break_end = clone( $week_start );
  330. // Note: +4 days to get to Friday.
  331. $break_end->add(
  332. new DateInterval(
  333. sprintf( "P%dD", ( ( $break_week['break_ends'] - $break_week['break_starts'] ) * 7 ) + 4 )
  334. )
  335. );
  336. switch ( $template_name )
  337. {
  338. case 'format-teaching-date':
  339. generate_date( "B{$num_breaks}", $week_start );
  340. break;
  341. case 'format-teaching-date-range':
  342. if ( $week_start->format( 'n' ) == $break_end->format( 'n' ) )
  343. {
  344. generate_same_month_date_range( "B{$num_breaks}", $week_start, $break_end );
  345. } // if break start and break end are in the same month
  346. else
  347. {
  348. generate_different_month_date_range( "B{$num_breaks}", $week_start, $break_end );
  349. } // else break start and break end are in different months
  350. break;
  351. default: // WTF?!?
  352. print "Unrecognised template name \"$template_name\", terminating.\n";
  353. exit;
  354. break;
  355. } // switch template name
  356. } // if current week == first week of break
  357. if ( $week == $break_week['break_ends'] )
  358. {
  359. // Grab the next break week off the front of the list.
  360. $break_week = array_shift( $period_data['break_weeks'] );
  361. } // if current week == last week of break
  362. } // if current week is a break week
  363. else
  364. {
  365. $week_number++;
  366. switch ( $template_name )
  367. {
  368. case 'format-teaching-date':
  369. generate_date( $week_number, $week_start );
  370. break;
  371. case 'format-teaching-date-range':
  372. if ( $week_start->format( 'n' ) == $week_end->format( 'n' ) )
  373. {
  374. generate_same_month_date_range( $week_number, $week_start, $week_end );
  375. } // if break start and break end are in the same month
  376. else
  377. {
  378. generate_different_month_date_range( $week_number, $week_start, $week_end );
  379. } // else break start and break end are in different months
  380. break;
  381. default: // WTF?!?
  382. print "Unrecognised template name \"$template_name\", terminating.\n";
  383. exit;
  384. break;
  385. } // switch template name
  386. } // else normal teaching week
  387. $week_start->add( $plus_seven_days );
  388. } // for each week
  389. // Terminating boilerplate for this teaching period.
  390. print<<<EOT
  391. <xsl:otherwise>
  392. <xsl:message terminate="yes">
  393. <xsl:text>Invalid {$period_name} week specification "</xsl:text>
  394. <xsl:value-of select="\$week" />
  395. <xsl:text>".</xsl:text>
  396. </xsl:message>
  397. </xsl:otherwise>
  398. </xsl:choose>
  399. </xsl:when>
  400.  
  401. EOT;
  402. } // foreach teaching period
  403.  
  404. // Terminating boilerplate for the template.
  405. print<<<EOT
  406. </xsl:choose>
  407. </common>
  408. </template>
  409.  
  410.  
  411. EOT;
  412. } // generate_template
  413.  
  414.  
  415. ////////////////////////////////////////////////////////////////////////////////
  416.  
  417.  
  418. function generate_date( $week_spec, $date )
  419. {
  420. print<<<EOT
  421. <xsl:when test="\$week = '{$week_spec}'">
  422. <xsl:value-of select="format-date( xs:date( '{$date->format( 'Y-m-d' )}' ) + \$offset, \$format )" />
  423. </xsl:when>
  424.  
  425. EOT;
  426. } // generate_same_month_date_range
  427.  
  428.  
  429. ////////////////////////////////////////////////////////////////////////////////
  430.  
  431.  
  432. function generate_same_month_date_range( $week_spec, $start_date, $end_date )
  433. {
  434. print<<<EOT
  435. <xsl:when test="\$week = '{$week_spec}'">
  436. <xsl:value-of select="format-date( xs:date( '{$start_date->format( 'Y-m-d' )}' ), '[D]' )" />
  437. <xsl:call-template name="endash" />
  438. <xsl:value-of select="format-date( xs:date( '{$end_date->format( 'Y-m-d' )}' ), '[D] ' )" />
  439. <xsl:if test="\$wrap">
  440. <xsl:call-template name="newline" />
  441. </xsl:if>
  442. <xsl:value-of select="format-date( xs:date( '{$end_date->format( 'Y-m-d' )}' ), \$month-format )" />
  443. </xsl:when>
  444.  
  445. EOT;
  446. } // generate_same_month_date_range
  447.  
  448.  
  449. function generate_different_month_date_range( $week_spec, $start_date, $end_date )
  450. {
  451. print<<<EOT
  452. <xsl:when test="\$week = '{$week_spec}'">
  453. <xsl:value-of select="format-date( xs:date( '{$start_date->format( 'Y-m-d' )}' ), concat( '[D] ', \$month-format ) )" />
  454. <xsl:if test="\$wrap">
  455. <xsl:call-template name="newline" />
  456. </xsl:if>
  457. <xsl:call-template name="endash" />
  458. <xsl:value-of select="format-date( xs:date( '{$end_date->format( 'Y-m-d' )}' ), concat( '[D] ', \$month-format ) )" />
  459. </xsl:when>
  460.  
  461. EOT;
  462. } // generate_different_month_date_range
  463.  
  464. ?>
  465.