Newer
Older
Handbook / calendar / generate_lecturedates.php
  1. <?php
  2.  
  3. /*
  4. This script generates a LaTeX include file that contains macros specifying
  5. the dates for every lecture in a teaching period. It doesn't generate dates
  6. for multiple papers; rather, you should generate a separate include file for
  7. each paper individually. The main reason for this approach is that paper
  8. codes include digits, which you can't include in LaTeX macro names. We
  9. therefore generate generic macros (\DateLectureOne, \DateLectureTwo, etc.),
  10. that don't include any paper information. The configuration for a specific
  11. paper is read from a separate file.
  12. The output of the script should be placed at the root of the tree for the
  13. target paper, alongside the paper_init.tex file.
  14. */
  15.  
  16. // LaTeX macro names can't include numbers, so we have to convert them to words.
  17. require 'Numbers/Words.php';
  18.  
  19. /*
  20. Configuration requires the following variables to be specified in the file
  21. lecturedates_config.php:
  22. $year
  23. The year for which dates are being generated. This is only used for
  24. documentation purposes.
  25. $period_name
  26. A description of the teaching period; usually one of "summer school",
  27. "first semester", "second semester" or "full year". This is only used
  28. for documentation purposes.
  29. $paper
  30. The paper code, e.g., "INFO 214". This is only used for documentation
  31. purposes.
  32. $start_dates
  33. A list of starting dates for contiguous teaching weeks, e.g., the six
  34. weeks up to mid-semester break, and the seven weeks after. We pretty
  35. much ignore breaks, etc., otherwise; all we want to know is what the
  36. date(s) are for the lecture(s) in each week. It also means that we
  37. don't have to do anything clever for things like full year papers that
  38. span more than one semester.
  39. Example (INFO 405 2012, full year with one break in each semester):
  40. $start_dates => array(
  41. new DateTime( "2012-02-27" ),
  42. new DateTime( "2012-04-02" ),
  43. new DateTime( "2012-07-09" ),
  44. new DateTime( "2012-09-03" ),
  45. );
  46. $period_lengths
  47. A list of integers representing the length in weeks of each period
  48. specifed in $start_dates. Both $start_dates and $period_lengths must
  49. therefore have the same length!
  50. Example (INFO 405 2012):
  51. $period_lengths => array( 6, 7, 7, 6 );
  52. $class_offsets:
  53. For each class in a week, specify the offset interval from the start
  54. of week (Monday). Thus, a class on Monday will be 'P0D', Wednesday
  55. 'P2D', Friday 'P4D', etc. Include as many entries in the list as there
  56. are classes per week (e.g., two lectures => two entries).
  57. Example (INFO 405 2012, one lecture per week, on Monday):
  58. $class_offsets => array(
  59. new DateInterval( 'P0D' ),
  60. );
  61. */
  62. require 'lecturedates_config.php';
  63.  
  64. $numwords = new Numbers_Words();
  65.  
  66. // A handy date interval: seven days to the start of next week.
  67. $plus_seven_days = new DateInterval( 'P7D' );
  68.  
  69. $week_num = 1;
  70.  
  71. printf( "%% Lecture dates for %s, %s, %s.\n", $paper, $period_name, $year );
  72.  
  73. for ( $period = 0; $period < count( $start_dates ); $period++ )
  74. {
  75. $period_start = $start_dates[$period];
  76. $week_start = clone( $start_dates[$period] );
  77.  
  78. for ( $week = 0; $week < $period_lengths[$period]; $week++ )
  79. {
  80. $lecture_num = 1;
  81. foreach ( $class_offsets as $class )
  82. {
  83. $class_date = clone( $week_start );
  84. $class_date->add( $class );
  85. // Note that the toWords() method generates hyphens in the result,
  86. // so we need to strip these out for LaTeX to be happy. Larger
  87. // numbers may well include spaces and commas, but it's pretty
  88. // unlikely that we'll ever get that high!
  89. printf( "\\newcommand{\\DateWeek%sLecture%s}{%s}\n",
  90. ucfirst( str_replace( '-', '', $numwords->toWords( $week_num ) ) ),
  91. ucfirst( str_replace( '-', '', $numwords->toWords( $lecture_num ) ) ),
  92. $class_date->format( 'j F' )
  93. );
  94. $lecture_num++;
  95. } // for every class in week
  96. $week_start->add( $plus_seven_days );
  97. $week_num++;
  98. } // for every week in period
  99. } // for every period