Changeset - 318a910b91ad
[Not reviewed]
default
0 1 0
Nathan Brink (binki) - 15 years ago 2011-03-18 15:36:47
ohnobinki@ohnopublishing.net
Now instead of automatically guessing that a user want to register for the next semester, assume they want to register for a semester whose middle is half a year into the future.
1 file changed with 28 insertions and 6 deletions:
0 comments (0 inline, 0 general)
inc/school.inc
Show inline comments
 
@@ -236,181 +236,203 @@ function school_list_html($highlight = N
 
/**
 
 * \brief
 
 *   Get a school-specific information page.
 
 *
 
 * Each school may define a function called
 
 * <school_id>_instructions_html(). This is the wrapper which retrieves a
 
 * specific school's info HTML. It is recommended that instructions
 
 * about using the school's registration system in conjunction with
 
 * slate_permutate be placed in the instructions_html.
 
 *
 
 * \param $school
 
 *   A school handle obtained from school_load() or
 
 *   school_load_guess().
 
 * \return
 
 *   An HTML fragment of the school's information or NULL if the
 
 *   school either doesn't have any such information or if the school
 
 *   handle is invalid.
 
 */
 
function school_instructions_html($school)
 
{
 
  global $school_default_school;
 

	
 
  if (empty($school) || empty($school['id']))
 
    /*
 
     * Invalid param deserves a NULL :-p. Really, this invalid param
 
     * handling shouldn't be needed...
 
     */
 
    return NULL;
 

	
 
  $school_instructions_html = $school['id'] . '_instructions_html';
 

	
 
  if (!function_exists($school_instructions_html))
 
    {
 
      /* load the default school's _instructions_html() function */
 
      if ($school_default_school === NULL)
 
	$school_default_school = school_load('default');
 
      /* ``hacky'', but preferable to recursion: */
 
      $school_instructions_html = 'default' . '_instructions_html';
 

	
 
      /* be 503-safe */
 
      if (!function_exists($school_instructions_html))
 
	return NULL;
 
    }
 

	
 
  return $school_instructions_html();
 
}
 

	
 
/**
 
 * \brief
 
 *   Return information about available semesters.
 
 *
 
 * \param $school
 
 *   The school.
 
 * \return
 
 *   An array with keys being semester IDs ordered by weights with
 
 *   lowest first and keys of 'id' (the semester's ID), 'name' (the
 
 *   friendly name), and 'weight' (lower numbers mean these semesters
 
 *   should be earlier, may be positive or negative). 'time_start',
 
 *   'time_end' are unix timestamps estimating the begin and end point
 
 *   of each semester.
 
 */
 
function school_semesters(array $school)
 
{
 
  if (empty($school['crawled']))
 
    return array();
 
  return $school['semesters'];
 
}
 

	
 
/**
 
 * \brief
 
 *   Return the semester which either the user has selected or which
 
 *   makes the most sense.
 
 *
 
 * \param $school
 
 *   The school for which a semester should be guessed.
 
 * \return
 
 *   An array with the keys 'id', 'name', and 'weight' corresponding
 
 *   to the same keys in the arrays returned by school_semesters() or
 
 *   NULL if no semester can be found.
 
 */
 
function school_semester_guess(array $school)
 
{
 
  $semesters = school_semesters($school);
 

	
 
  if (!empty($_REQUEST['semester'])
 
      && isset($semesters[$_REQUEST['semester']]))
 
    {
 
      $semester = $semesters[$_REQUEST['semester']];
 
      $_SESSION['semester'] = $semester['id'];
 
      return $semester;
 
    }
 

	
 
  if (!empty($_SESSION['semester'])
 
      && isset($semesters[$_SESSION['semester']]))
 
      return $semesters[$_SESSION['semester']];
 

	
 
  $time = time();
 
  $next_semester = FALSE;
 
  /*
 
   * The following is the most _common_ scenario:
 
   *
 
   * A student is looking ahead in the last half of March (3) to
 
   * register for a semester starting in September (9) and ending in
 
   * December. Thus, looking 6 months into the future may put us right
 
   * in the middle of the desired semester, also considering that
 
   * during the summer (6) one is looking to register for a fall
 
   * semester which ends in December (12).
 
   */
 
  $time_target = time() + 60*60*24*365.25 * 0.5;
 

	
 
  $semester = NULL;
 
  /* guessed semester */
 
  $best_semester = NULL;
 
  /*
 
   * The absolute value of the difference between the $time_target and
 
   * the middle of the guessed semester. Smaller is better.
 
   */
 
  $best_score = -1;
 
  foreach ($semesters as $semester)
 
    {
 
      if ($next_semester)
 
	return $semester;
 
      if ($semester['time_start'] < $time)
 
	$next_semester = TRUE;
 
      $my_score = abs(($semester['time_end'] + $semester['time_start']) / 2 - $time_target);
 
      if ($best_score == -1 || $my_score < $best_score)
 
	{
 
	  $best_semester = $semester;
 
	  $best_score = $my_score;
 
	}
 
      error_log($semester['name'] . ': ' . $my_score);
 
    }
 
  if (!empty($best_semester))
 
    return $best_semester;
 
  return $semester;
 
}
 

	
 
/**
 
 * \brief
 
 *   Return an array of default classes for a particular school.
 
 *
 
 * \param $school
 
 *   The school's handle.
 
 */
 
function school_default_courses($school)
 
{
 
  $school_default_courses = $school['id'] . '_default_courses';
 
  if (function_exists($school_default_courses))
 
    {
 
      require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'class.course.inc');
 
      return $school_default_courses();
 
    }
 

	
 
  return array();
 
}
 

	
 
/**
 
 * \brief
 
 *   Return an example course id for the school.
 
 *
 
 * Each school may specify an example course ID by placing a key
 
 * called 'example_course_id' into the array returned by its
 
 * <school_id>_info() function. See school_load().
 
 *
 
 * \param $school
 
 *   The school's handle.
 
 * \return
 
 *   A string containing a representative example of a course ID for
 
 *   the given school.
 
 */
 
function school_example_course_id(array $school)
 
{
 
  return $school['example_course_id'];
 
}
 

	
 
/**
 
 * \brief
 
 *   Determine if a school has crawler data stored.
 
 *
 
 * \param $school
 
 *   The which should be checked.
 
 */
 
function school_has_auto(array $school)
 
{
 
  return isset($school['crawled']) && $school['crawled'];
 
}
 

	
 
/**
 
 * \brief
 
 *   Used to load the school cache.
 
 *
 
 * \return
 
 *   The cache array or NULL if the cache couldn't be loaded.
 
 */
 
function _school_cache_load()
 
{
 
  static $cache = NULL;
 

	
 
  if ($cache != NULL)
 
    return $cache;
 

	
 
  $cache_file_name = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'
 
    . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'schools';
 
  $cache_serialized = @file_get_contents($cache_file_name);
 
  if (!empty($cache_serialized))
 
    $cache = unserialize($cache_serialized);
 

	
 
  return $cache;
 
}
0 comments (0 inline, 0 general)