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

/*
    This script generates a LaTeX include file that contains macros for generating
    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 each paper
    will have different lecture offsets in a week, and possibly also a different
    number of lectures each week. It's easier to keep track of this in paper-
    specific files rather than a single file for all papers. 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:
    
    $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. The variable $year (defined to be current
        year below) can be used here.
        
        "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:
    
    $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' ),
            ),
        ),
        ...
    );
*/
$year = date("Y");

require 'lecturedates_config.php';

// 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];

        print <<<EOT
\\newcount\\lecnum
\\newcount\\weeknum

\\newcommand{\\LectureDate}[1]{%
    \\lecnum #1\\relax%
    \\advance\\lecnum by -1\\relax%
    \\ifcase\\lecnum 
EOT;

$first_lecture = true;

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);
            
            if ($first_lecture) {
                $first_lecture = false;
            }
            else {
                print "        \\or ";
            }
            printf("%s%%\n", $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

print <<<EOT
        \\else ????%
    \\fi%
}

\\newcommand{\\LectureDateByWeek}[2]{%
    \\weeknum #1\\relax%
    \\advance\\weeknum by -1\\relax%
    \\multiply\\weeknum by 2\\relax%
    \\advance\\weeknum by #2\\relax%
    \\LectureDate{\\the\\weeknum}%
}

EOT;