Newer
Older
Handbook / calendar / generate_calendar.php
nstanger on 12 Jun 2012 4 KB - Fixed typo in comments.
<?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. A good spot would
	be somewhere in the TeX search path.
*/

// 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:	A list of week numbers in which breaks from teaching occur.
					The list should ideally be in ascending order, but the
					script doesn't assume this and sorts the list anyway. The
					week number of a break is relative to num_weeks, so if a
					break week falls after the sixth then that week is week
					number 7. The numbering of teaching weeks will skip the
					break weeks regardless. If there are no breaks in the
					period, then set this to an empty array.
					
					Note that we aren't clever about coalescing contiguous
					weeks. For example, if weeks 7 and 8 are indicated as break
					weeks, a separate macro will be output for each week, not
					one covering both weeks. It's probably technically feasible,
					but not really worth the extra work :).
*/
$periods = array(
	'SummerSchool' => array(
		'start_date'  => new DateTime( "2012-01-09" ),
		'num_weeks'   => 6,
		'break_weeks' => array(),
	),
	'SemesterOne' => array(
		'start_date'  => new DateTime( "2012-02-27" ),
		'num_weeks'   => 14,
		'break_weeks' => array( 7 ),
	),
	'SemesterTwo' => array(
		'start_date'  => new DateTime( "2012-07-09" ),
		'num_weeks'   => 14,
		'break_weeks' => array( 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'] );
	
	// We need to adjust the teaching week number according to the number
	// of break weeks that we've processed. $break_adjustment therefore
	// effectively stores the count of break weeks processed so far.
	$break_adjustment = 0;
	
	// Grab the first break week off the front of the list.
	sort( $period_data['break_weeks'] );
	$break_week = array_shift( $period_data['break_weeks'] );
	
	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 == $break_week )
		{
			// Increment this first before doing anything else, because we use
			// it to generate a macro name for this break, and we want the
			// macro numbering to start from one, not zero.
			$break_adjustment++;
			
			// Output macro for inclusion in paper calendar.
			printf(	"\\newcommand{\\%sBreak%s}{%s--%s}\n",
				$period_name,
				ucfirst( $numwords->toWords( $break_adjustment ) ),
				( $same_month ) ? $week_start->format( 'j' ) : $week_start->format( 'j M' ),
				$week_end->format( 'j M' )
			);
			
			// Grab the next break week off the front of the list.
			$break_week = array_shift( $period_data['break_weeks'] );
		} // if break week
		else
		{
			$week_number = $week - $break_adjustment;
			
			if( $same_month )
			{
				// Output macro for inclusion in paper calendar.
				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
			{
				// Output macro for inclusion in paper calendar.
				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

?>