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:
    
    $year
        The year for which dates are being generated. This can be used elswhere
        in the configuration.
    
    $period_config
        An array whose key is the teaching period code (e.g., "SS", "S1", "S2",
        "FY"). Each element is an array comprising the following sub-elements:
        
        "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.
        
        "period_lengths"
        A list of integers representing the length in weeks of each period
        specifed in $start_dates. Both the "start_dates" and "period_lengths"
        elements must therefore have the same length!
        
        Example (full year with one break in each semester):
        
            "full year" => array(
                "start_dates" => array(
                    new DateTime( "{$year}-02-27" ),
                    new DateTime( "{$year}-04-24" ),
                    new DateTime( "{$year}-07-10" ),
                    new DateTime( "{$year}-09-04" ),
                ),
                "period_lengths" => array(7, 6, 7, 6),
            ),
       
    $paper_config
        An array whose key is the paper code (e.g., "INFO214"). Each element
        is an array comprising the following sub-elements:
    
        "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 408 2017, one lecture per week, on Wednesday):
        
            "class_offsets" => array(
                new DateInterval( 'P2D' ),
            );
    
    A complete example:
    
    $year = 2017;

    $period_config = array(
        ...
        "FY" => array(
            "start_dates" => array(
                new DateTime( "{$year}-02-27" ),
                new DateTime( "{$year}-04-24" ),
                new DateTime( "{$year}-07-10" ),
                new DateTime( "{$year}-09-04" ),
            ),
            "period_lengths" => array(7, 6, 7, 6),
        ),
    );

    $paper_config = array(
        "COMP101" => array(
            "class_offsets" = array(
                new DateInterval( 'P0D' ),
                new DateInterval( 'P2D' ),
            ),
        ),
        ...
    );
*/
require 'lecturedates_config.php';

$numwords = new Numbers_Words();

// Process command line arguments.
// This only accepts paper codes in the usual form XXXX999, and will fail for
// unusual papers like "INFO5H". However, it's extremely unlikely that such
// papers will ever need to use this script.
($argc == 3 && preg_match('/[A-Z]{4}\d{3}/', $argv[1]) && preg_match('/S[S12]|FY/', $argv[2]))
    or die("Usage: php generate_lecturedates.php paper_code period_code\n");

$paper_code = $argv[1];
$period_code = $argv[2];

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

$week_num = 1;

printf( "%% Lecture dates for %s %s %s.\n", $paper_code, $period_code, $year );

$period_settings = $period_config[$period_code];
$paper_settings = $paper_config[$paper_code];

foreach ($period_settings['start_dates'] as $period => $period_start)
{
    $week_start = clone($period_start);
    
    for ($w = 0; $w < $period_settings['period_lengths'][$period]; $w++)
    {
        $lecture_num = 1;
        
        foreach ($paper_settings["class_offsets"] as $offset)
        {
            $class_date = clone($week_start);
            $class_date->add($offset);
            
            // 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