function ajax_termdates()
 {
     OutputModes('ajax');
     if (!CheckPermissions('public')) {
         return;
     }
     $error = false;
     $years = array();
     if (isset($_GET['years'])) {
         $year_ranges = split(',', $_GET['years']);
         foreach ($year_ranges as $year_range) {
             if ($year_range === '') {
                 continue;
             }
             $ends = split('-', $year_range);
             if (count($ends) > 2) {
                 $this->main_frame->Error(array('class' => 'error', 'text' => 'Invalid year range: ' . $year_range));
                 $error = true;
             } else {
                 if (count($ends) == 1) {
                     $ends = array($year_range, $year_range);
                 }
                 $range_error = false;
                 foreach ($ends as &$end) {
                     if (is_numeric($end) && $end >= 1970 && $end < 2037) {
                         $end = (int) $end;
                     } else {
                         $this->main_frame->Error(array('class' => 'error', 'text' => 'Invalid year: ' . $end));
                         $range_error = true;
                     }
                 }
                 if (!$range_error) {
                     for ($year = $ends[0]; $year <= $ends[1]; ++$year) {
                         $years[] = $year;
                     }
                 } else {
                     $error = true;
                 }
             }
         }
     }
     if (!$error) {
         $root = array('_tag' => 'calendar', 'termdates' => array());
         foreach ($years as $year) {
             $acyear = array('_tag' => 'academicyear', '_attr' => array('year' => $year));
             for ($term = 0; $term < 6; ++$term) {
                 $start_ts = Academic_time::StartOfAcademicTerm($year, $term);
                 $monday_ts = Academic_time::MondayWeek1OfAcademicTerm($year, $term);
                 $start = new Academic_time($start_ts);
                 $days = Academic_time::LengthOfAcademicTerm($year, $term);
                 $end = $start->Adjust($days . 'days');
                 $monday = new Academic_time($monday_ts);
                 $acterm = array('_tag' => 'term', '_attr' => array('id' => "term_{$year}_{$term}", 'term' => $term), 'type' => $start->AcademicTermTypeName(), 'name' => $start->AcademicTermName(), 'unique' => $start->AcademicTermNameUnique(), 'start' => $start->Format('Y-m-d'), 'end' => $end->Format('Y-m-d'), 'days' => $days, 'mondayweek1' => $monday->Format('Y-m-d'), 'weeks' => $start->AcademicTermWeeks());
                 $acyear[] = $acterm;
             }
             $root['termdates'][] = $acyear;
         }
         $this->main_frame->SetXml($root);
     }
     $this->main_frame->Load();
 }
 /**
  * @brief Perform tests on the academic calendar functions.
  * @return The number of errors detected.
  *
  * Runs through every day in the academic calendar checking that the term
  * number and week number functions are correct and that days are
  * consecutive when calculated using Academic_calendar::Academic.
  */
 function PerformTests()
 {
     // Store the previous date so can see how many days have elapsed
     $prev_date = 0;
     $errors = 0;
     // Go through academic years
     for ($year = 2004; $year < 2012; ++$year) {
         // Go through all 6 academic terms
         for ($term_counter = 0; $term_counter < 6; ++$term_counter) {
             // Go through every week in the term
             $term_days = Academic_time::LengthOfAcademicTerm($year, $term_counter);
             $term_day = 0;
             $dow_counter = Academic_time::DayOfStartOfAcademicTerm($year, $term_counter) + 1;
             for ($week_counter = 1; $term_day < $term_days; ++$week_counter) {
                 // Go through every day in the week
                 for ($dow_counter = $dow_counter; $dow_counter <= 7 && $term_day < $term_days; ++$dow_counter) {
                     // Create a date object from academic year/term/week/day_of_week
                     $actime = $this->Academic($year, $term_counter, $week_counter, $dow_counter);
                     // Create a date object from academic year/term/day_of_term
                     $actime2 = $this->AcademicDayOfTerm($year, $term_counter, $term_day);
                     // Detect any inconsistencies
                     $error_detected = FALSE;
                     // Do Academic and AcademicDayOfTerm give the same result?
                     if ($actime != $actime2) {
                         echo '!!Academic and AcademicDayOfTerm give different results!!<br/>';
                         ++$errors;
                         $error_detected = TRUE;
                     }
                     // Has more than one day elapsed?
                     if (0 !== $prev_date && 1 !== Academic_time::DaysBetweenTimestamps($prev_date->Timestamp(), $actime->Timestamp())) {
                         echo '!!days not consecutive!!<br/>';
                         ++$errors;
                         $error_detected = TRUE;
                     }
                     // Is the calculated academic year consistent?
                     if ($year != $actime->AcademicYear()) {
                         echo '!!year doesn\'t match!!<br/>';
                         ++$errors;
                         $error_detected = TRUE;
                     }
                     // Is the calculated academic term consistent?
                     if ($term_counter != $actime->AcademicTerm()) {
                         echo '!!term doesn\'t match!!<br/>';
                         ++$errors;
                         $error_detected = TRUE;
                     }
                     // Is the calculated academic week consistent?
                     if ($week_counter != $actime->AcademicWeek()) {
                         echo '!!week doesn\'t match!!<br/>';
                         ++$errors;
                         $error_detected = TRUE;
                     }
                     if ($error_detected) {
                         // an error has been detected so print date information
                         if (0 !== $prev_date) {
                             // starting with previous date
                             echo '&nbsp;&nbsp;prev date: ' . $prev_date->Format(DATE_RFC822) . '<br/>';
                             //*/
                             // elapsed days
                             echo '&nbsp;&nbsp;days between: ' . Academic_time::DaysBetweenTimestamps($prev_date->Timestamp(), $actime->Timestamp()) . '<br/>';
                             //*/;
                         }
                         // academic date input
                         echo '&nbsp;&nbsp;Year: ' . $year . ', Term: ' . $term_counter . ', Week: ' . $week_counter . ', Day of week: ' . $dow_counter . ', Day of term: ' . $term_day . '<br/>';
                         //*/
                         // standard date output
                         echo '&nbsp;&nbsp;date1: ' . $actime->Format(DATE_RFC822) . '<br/>';
                         //*/
                         echo '&nbsp;&nbsp;date2: ' . $actime2->Format(DATE_RFC822) . '<br/>';
                         //*/
                         // gregorian & academic date output
                         echo '&nbsp;&nbsp;date: ' . $actime->DayOfMonth() . '/' . $actime->Month() . '/' . $actime->Year() . ': ' . $actime->AcademicWeek() . ',' . $actime->AcademicTermName() . ',' . $actime->AcademicYearName() . '<br/>';
                         //*/
                         // data from inside DaysBetweenTimestamps function
                         // probably no longer required now its fixed
                         echo '&nbsp;&nbsp;&nbsp;Day of year of first: ' . (int) date('z', $prev_date->Timestamp()) . '<br/>';
                         echo '&nbsp;&nbsp;&nbsp;Day of year of second: ' . (int) date('z', $actime->Timestamp()) . '<br/>';
                         $difference = (int) date('z', $actime->Timestamp()) - (int) date('z', $prev_date->Timestamp());
                         if ($difference < 0) {
                             echo '&nbsp;&nbsp;&nbsp;Days of year of first: ' . (365 + (int) date('L', $prev_date->Timestamp())) . '<br/>';
                         }
                         // New line to seperate error dates
                         echo '<br/>';
                     }
                     $prev_date = $actime;
                     ++$term_day;
                 }
                 $dow_counter = 1;
             }
         }
     }
     return $errors;
 }