Newer
Older
Handbook / calendar / generate_calendar.php
<?php

/*
	File: $Id$
	
	This script generates a LaTeX include file that contains macros 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 run the script, saving the output
	in a suitable location (ideally only once for all papers, perhaps in
	$ALL_PAPERS_ROOT?).
*/

// LaTeX macro names can't include numbers, so we have to convert them to words.
include( "Numbers/Words.php" );

/*
	Teaching period configuration. It's easily extensible to a new teaching
	period simply by adding a new specification for that period. The key should
	be valid as a LaTeX macro name (i.e., no spaces, no digits, no punctuation).
	The components of each period specification are:
	
		start_date:	The date of the first day of the first week of the period.
		
		num_weeks:	The total number of weeks in the period, /including/ any
					breaks (e.g., 13 teaching weeks plus 1 week mid-semester
					break = 14 weeks).
		
		break_week:	The week number that the break (if any) falls in, relative
					to num_weeks, so if the break week falls after the sixth
					teaching week, then set this to 7. The numbering of teaching
					weeks will skip the break week regardless. If there is no
					break in the period, then set this to a number larger than
					num_weeks.
					
					Note that this currently only supports a single, one week
					break per period. Generalising the configuration to
					multiple/longer breaks probably isn't that difficult if
					needed (a list of week numbers instead of a single value
					should cater for all cases). Setting up the LaTeX macros
					and teaching week numbering could be tricky, though.
*/
$periods = array(
	'SummerSchool' => array(
		'start_date' => new DateTime( "2012-01-09" ),
		'num_weeks'  => 6,
		'break_week' => 9999,
	),
	"SemesterOne" => array(
		'start_date' => new DateTime( "2012-02-27" ),
		'num_weeks'  => 14,
		'break_week' => 7,
	),
	"SemesterTwo" => array(
		'start_date' => new DateTime( "2012-07-09" ),
		'num_weeks'  => 14,
		'break_week' => 8,
	),
);

// A couple of handy date intervals: four days to the end of the current week,
// and seven days to the start of next week.
$plus_four_days = new DateInterval( 'P4D' );
$plus_seven_days = new DateInterval( 'P7D' );

$numwords = new Numbers_Words();

foreach ( $periods as $period_name => $period_data )
{
	$week_start = clone( $period_data['start_date'] );
	
	for ( $week = 1; $week <= $period_data['num_weeks']; $week++ )
	{
		$week_end = clone( $week_start );
		$week_end->add( $plus_four_days );
		
		// If the week start and end dates are in the same month, we only
		// need to output the month name once.
		$same_month = $week_start->format( 'n' ) == $week_end->format( 'n' );
		
		// Handle break weeks.
		if ( $week == $period_data['break_week'] )
		{
			printf(	"\\newcommand{\\%sMidSemesterBreak}{%s--%s}\n",
				$period_name,
				( $same_month ) ? $week_start->format( 'j' ) : $week_start->format( 'j M' ),
				$week_end->format( 'j M' )
			);
		} // if break week
		else
		{
			// If we've passed the break week, then we need to subtract one from
			// the week count to get the correct teaching week number.
			$week_number = ( $week > $period_data['break_week'] ) ? $week - 1 : $week;
			
			if( $same_month )
			{
				printf(	"\\newcommand{\\%sWeek%s}{%s--%s \\\\ %s}\n",
					$period_name,
					ucfirst( $numwords->toWords( $week_number ) ),
					$week_start->format( 'j' ),
					$week_end->format( 'j' ),
					$week_end->format( 'M' )
				);
			} // if same month
			else
			{
				printf(	"\\newcommand{\\%sWeek%s}{%s \\\\ --%s}\n",
					$period_name,
					ucfirst( $numwords->toWords( $week_number ) ),
					$week_start->format( 'j M' ),
					$week_end->format( 'j M' )
				);
			} // else different months
		} // else normal teaching week
		
		$week_start->add( $plus_seven_days );
	} // for each week
} // foreach teaching period

?>