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