GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
1
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
nigel.stanger
/
Handbook
Browse code
• Split calendar script configuration out into a separate (untracked) script.
• Changed includes to requires.
master
1 parent
d6b768e
commit
1374941c725cb122c956800deb68a9fbcc31843e
Nigel Stanger
authored
on 12 Dec 2013
Patch
Showing
3 changed files
calendar/.gitignore
calendar/generate_calendar.php
calendar/generate_lecturedates.php
Ignore Space
Show notes
View
calendar/.gitignore
lecturedates_config.php calendar_config.php
lecturedates_config.php
Ignore Space
Show notes
View
calendar/generate_calendar.php
<?php /* 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. require '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_weeks: A list of breaks that occur during the teaching period. Each is specified by the start (break_start) and end (break_end) week. If a break is only one week long, then break_start and break_end should be equal. If there are no breaks in the teaching period, include a single break specification with both break_start and break_end set to zero. The list should ideally be in ascending order, but the script doesn't assume this and sorts the list anyway. Weeks are numbered by absolute calendar week number (i.e., week 1 is the first calendar week of the year). The date of the first Monday of the year is stored in $first_monday below (everything is Monday-indexed). Example: $first_monday = new DateTime( "2012-12-31" ); $periods = array( 'SummerSchool' => array( 'first_week' => 2, 'last_week' => 7, 'break_weeks' => array( array( 'break_starts' => 0, 'break_ends' => 0, ), ), ), 'SemesterOne' => array( 'first_week' => 9, 'last_week' => 22, 'break_weeks' => array( array( 'break_starts' => 14, 'break_ends' => 14, ), ), ), 'SemesterTwo' => array( 'first_week' => 28, 'last_week' => 41, 'break_weeks' => array( array( 'break_starts' => 35, 'break_ends' => 35, ), ), ), 'FullYear' => array( 'first_week' => 9, 'last_week' => 41, 'break_weeks' => array( array( 'break_starts' => 23, 'break_ends' => 27, ), array( 'break_starts' => 15, 'break_ends' => 15, ), array( 'break_starts' => 35, 'break_ends' => 35, ), ), ), ); */ require 'calendar_config.php'; // 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( $first_monday ); // Jump forward the correct number of weeks to the start of the teaching period. $week_start->add( new DateInterval( sprintf( "P%dD", ( $period_data['first_week'] - 1 ) * 7 ) ) ); $week_number = 0; $num_breaks = 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 = $period_data['first_week']; $week <= $period_data['last_week']; $week++ ) { $week_end = clone( $week_start ); $week_end->add( $plus_four_days ); // Handle break weeks. if ( ( $week >= $break_week['break_starts'] ) && ( $week <= $break_week['break_ends'] ) ) { if ( $week == $break_week['break_starts'] ) { $num_breaks++; // Work out the end date of the break. $break_end = clone( $week_start ); // Note: +4 days to get to Friday. $break_end->add( new DateInterval( sprintf( "P%dD", ( ( $break_week['break_ends'] - $break_week['break_starts'] ) * 7 ) + 4 ) ) ); // Output macro for inclusion in paper calendar. printf( "\\newcommand{\\%sBreak%s}{%s--%s}\n", $period_name, str_replace( "-", "", ucfirst( $numwords->toWords( $num_breaks ) ) ), // If the week start and end dates are in the same month, we only // need to output the month name once. ( $week_start->format( 'n' ) == $break_end->format( 'n' ) ) ? $week_start->format( 'j' ) : $week_start->format( 'j M' ), $break_end->format( 'j M' ) ); } // if current week == first week of break if ( $week == $break_week['break_ends'] ) { // Grab the next break week off the front of the list. $break_week = array_shift( $period_data['break_weeks'] ); } // if current week == last week of break } // if current week is a break week else { $week_number++; // If the week start and end dates are in the same month, we only // need to output the month name once. if ( $week_start->format( 'n' ) == $week_end->format( 'n' ) ) { // Output macro for inclusion in paper calendar. printf( "\\newcommand{\\%sWeek%s}{%s--%s \\\\ %s}\n", $period_name, str_replace( "-", "", 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, str_replace( "-", "", 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 ?>
<?php /* 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_weeks: A list of breaks that occur during the teaching period. Each is specified by the start (break_start) and end (break_end) week. If a break is only one week long, then break_start and break_end should be equal. If there are no breaks in the teaching period, include a single break specification with both break_start and break_end set to zero. The list should ideally be in ascending order, but the script doesn't assume this and sorts the list anyway. Weeks are numbered by absolute calendar week number (i.e., week 1 is the first calendar week of the year). The date of the first Monday of the year is stored in $first_monday below (everything is Monday-indexed). */ $first_monday = new DateTime( "2012-12-31" ); $periods = array( 'SummerSchool' => array( 'first_week' => 2, 'last_week' => 7, 'break_weeks' => array( array( 'break_starts' => 0, 'break_ends' => 0, ), ), ), 'SemesterOne' => array( 'first_week' => 9, 'last_week' => 22, 'break_weeks' => array( array( 'break_starts' => 14, 'break_ends' => 14, ), ), ), 'SemesterTwo' => array( 'first_week' => 28, 'last_week' => 41, 'break_weeks' => array( array( 'break_starts' => 35, 'break_ends' => 35, ), ), ), 'FullYear' => array( 'first_week' => 9, 'last_week' => 41, 'break_weeks' => array( array( 'break_starts' => 23, 'break_ends' => 27, ), array( 'break_starts' => 15, 'break_ends' => 15, ), array( 'break_starts' => 35, 'break_ends' => 35, ), ), ), ); // 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( $first_monday ); // Jump forward the correct number of weeks to the start of the teaching period. $week_start->add( new DateInterval( sprintf( "P%dD", ( $period_data['first_week'] - 1 ) * 7 ) ) ); $week_number = 0; $num_breaks = 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 = $period_data['first_week']; $week <= $period_data['last_week']; $week++ ) { $week_end = clone( $week_start ); $week_end->add( $plus_four_days ); // Handle break weeks. if ( ( $week >= $break_week['break_starts'] ) && ( $week <= $break_week['break_ends'] ) ) { if ( $week == $break_week['break_starts'] ) { $num_breaks++; // Work out the end date of the break. $break_end = clone( $week_start ); // Note: +4 days to get to Friday. $break_end->add( new DateInterval( sprintf( "P%dD", ( ( $break_week['break_ends'] - $break_week['break_starts'] ) * 7 ) + 4 ) ) ); // Output macro for inclusion in paper calendar. printf( "\\newcommand{\\%sBreak%s}{%s--%s}\n", $period_name, str_replace( "-", "", ucfirst( $numwords->toWords( $num_breaks ) ) ), // If the week start and end dates are in the same month, we only // need to output the month name once. ( $week_start->format( 'n' ) == $break_end->format( 'n' ) ) ? $week_start->format( 'j' ) : $week_start->format( 'j M' ), $break_end->format( 'j M' ) ); } // if current week == first week of break if ( $week == $break_week['break_ends'] ) { // Grab the next break week off the front of the list. $break_week = array_shift( $period_data['break_weeks'] ); } // if current week == last week of break } // if current week is a break week else { $week_number++; // If the week start and end dates are in the same month, we only // need to output the month name once. if ( $week_start->format( 'n' ) == $week_end->format( 'n' ) ) { // Output macro for inclusion in paper calendar. printf( "\\newcommand{\\%sWeek%s}{%s--%s \\\\ %s}\n", $period_name, str_replace( "-", "", 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, str_replace( "-", "", 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 ?>
Ignore Space
Show notes
View
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
<?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. include( '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' ), ); */ include( '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
Show line notes below