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

/*
    This script generates a LaTeX include file that contains macros specifying
    the dates for every lecture in a teaching period. It doesn't generate dates
    for multiple papers; rather, you should generate a separate include file for
    each paper individually. The main reason for this approach is that paper
    codes include digits, which you can't include in LaTeX macro names. We
    therefore generate generic macros (\DateLectureOne, \DateLectureTwo, etc.),
    that don't include any paper information. The configuration for a specific
    paper is read from a separate file.
    
    The output of the script should be placed at the root of the tree for the
    target paper, alongside the paper_init.tex file.
*/

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

/*
    Configuration requires the following variables to be specified in the file
    lecturedates_config.php:
    
    $start_dates
        A list of starting dates for contiguous teaching weeks, e.g., the six
        weeks up to mid-semester break, and the seven weeks after. We pretty
        much ignore breaks, etc., otherwise; all we want to know is what the
        date(s) are for the lecture(s) in each week. It also means that we
        don't have to do anything clever for things like full year papers that
        span more than one semester.
        
        Example (INFO 405 2012, full year with one break in each semester):
        
            $start_dates => array(
                new DateTime( "2012-02-27" ),
                new DateTime( "2012-04-02" ),
                new DateTime( "2012-07-09" ),
                new DateTime( "2012-09-03" ),
            );
    
    $period_lengths
        A list of integers representing the length in weeks of each period
        specifed in $start_dates. Both $start_dates and $period_lengths must
        therefore have the same length!
        
        Example (INFO 405 2012):
        
            $period_lengths => array( 6, 7, 7, 6 );
    
    $class_offsets:
        For each class in a week, specify the offset interval from the start
        of week (Monday). Thus, a class on Monday will be 'P0D', Wednesday
        'P2D', Friday 'P4D', etc. Include as many entries in the list as there
        are classes per week (e.g., two lectures => two entries).
        
        Example (INFO 405 2012, one lecture per week, on Monday):
        
            $class_offsets => array(
                new DateInterval( 'P0D' ),
            );
*/
require 'lecturedates_config.php';

$numwords = new Numbers_Words();

// A handy date interval: seven days to the start of next week.
$plus_seven_days = new DateInterval( 'P7D' );

$week_num = 1;

for ( $period = 0; $period < count( $start_dates ); $period++ )
{
    $period_start = $start_dates[$period];
    $week_start = clone( $start_dates[$period] );

    for ( $week = 0; $week < $period_lengths[$period]; $week++ )
    {
        $lecture_num = 1;
        
        foreach ( $class_offsets as $class )
        {
            $class_date = clone( $week_start );
            $class_date->add( $class );
            
            // Note that the toWords() method generates hyphens in the result,
            // so we need to strip these out for LaTeX to be happy. Larger
            // numbers may well include spaces and commas, but it's pretty
            // unlikely that we'll ever get that high!
            printf( "\\newcommand{\\DateWeek%sLecture%s}{%s}\n",
                ucfirst( str_replace( '-', '', $numwords->toWords( $week_num ) ) ),
                ucfirst( str_replace( '-', '', $numwords->toWords( $lecture_num ) ) ),
                $class_date->format( 'j F' )
            );
            $lecture_num++;
        } // for every class in week
        
        $week_start->add( $plus_seven_days );
        $week_num++;
    } // for every week in period
} // for every period