Changeset - fe65a57a01fe
[Not reviewed]
default
0 3 0
Nathan Brink (binki) - 15 years ago 2011-03-18 19:47:26
ohnobinki@ohnopublishing.net
Store a schedule's parent_id and make awkwardly accessible via a ``Parent'' buttonlink. Fixes bug 91.
3 files changed with 84 insertions and 18 deletions:
0 comments (0 inline, 0 general)
inc/class.schedule.php
Show inline comments
 
@@ -31,67 +31,83 @@ include_once $incdir . 'class.course.inc
 
include_once $incdir . 'class.section.php';
 
include_once $incdir . 'class.page.php';
 

	
 
/*
 
 * Load a Classes -> Course converter class for the sake of the
 
 * Schedule::__wakeup() magic function.
 
 */
 
require_once $incdir . 'class.classes_convert.inc';
 

	
 
class Schedule
 
{
 
  /*
 
   * Variables for upgrading from saved schedules created when there
 
   * was a class called Classes.
 
   */
 
  private $classStorage;			// array of courses
 
  private $nclasses;				// Integer number of classes
 

	
 
  /* My member variables. */
 
  private $courses;
 
  private $nPermutations = 0;		// Integer number of real permutations
 
  private $possiblePermutations;	// Integer number of possible permutations
 
  private $scheduleName;			// String name of schedule
 
  private $storage;				// Integer array of valid schedules
 
  /* The <title /> of the page used when rendering this schedule */
 
  private $title;
 

	
 
  /**
 
   * \brief
 
   *   My global identification number. Not defined until the schedule
 
   *   is processed and first saved.
 
   */
 
  private $id;
 

	
 
  /*
 
   * The identifier of the schedule from which this schedule was
 
   * derived or NULL.
 
   */
 
  private $parent_id;
 

	
 
  /**
 
   * \brief
 
   *   Create a schedule with the given name.
 
   *
 
   * \param $name
 
   *   A string, the friendly name the user gave this schedule.
 
   * \param $parent
 
   *   An integer, the id of the schedule from which this schedule is
 
   *   derived. A schedule is considered to be derived of another of
 
   *   the user created this schedule by clicking ``Edit'' for the
 
   *   previous schedule. Or NULL if this schedule stands on its own.
 
   */
 
  function __construct($name)
 
  function __construct($name, $parent = NULL)
 
  {
 
    $this->courses = array();
 
    $this->scheduleName = $name;
 
    $this->storage = array();
 
    $this->title = "SlatePermutate - Scheduler";
 
    $this->parent_id = $parent;
 

	
 
    /* mark this as an upgraded Schedule class. See __wakeup() */
 
    $this->nclasses = -1;
 
  }
 

	
 
  //--------------------------------------------------
 
  // Mutators and Accessors
 
  //--------------------------------------------------
 
  public function getName()
 
  {
 
    return $this->scheduleName;
 
  }    
 

	
 
  //--------------------------------------------------
 
  // Adds a new class to the schedule.
 
  //--------------------------------------------------
 
  function addCourse($n)
 
  {
 
    $this->courses[] = new Course($n);
 
  }
 

	
 
  //--------------------------------------------------
 
  // Adds a section to the desired class.
 
  //--------------------------------------------------
 
@@ -185,52 +201,58 @@ class Schedule
 
		$cs[$position] = 0;
 

	
 
		$position++;
 
					
 
		// This is for the very last permutation. Even 
 
		// though the combination is not actually true
 
		// the larger while loop will end before any 
 
		// new combinations are performed.
 
		if($position == count($this->courses))
 
		  {
 
		    $valid = true;
 
		  } else {
 
		  $cs[$position]++;
 
		}
 
	      } else {
 
	      $valid = true;
 
	      $position = 0;
 
	    }
 
	  }
 
            
 
	$counter++;
 
      } while($counter < $this->possiblePermutations);
 
  }
 
    
 
  /**
 
   * \brief
 
   *   Prints out the possible permutations in tables.
 
   *
 
   * \param $schedule_store
 
   *   The schedule_store handle with which this schedule was loaded,
 
   *   used to query the parent schedule.
 
   */
 
  //--------------------------------------------------
 
  // Prints out the possible permutations in tables.
 
  //--------------------------------------------------
 
  function writeoutTables()
 
  function writeoutTables(array $schedule_store = NULL)
 
  {
 
    $filled = false;
 
    $time = array(700,730,800,830,900,930,1000,1030,1100,1130,1200,1230,1300,1330,1400,1430,1500,1530,1600,1630,1700,1730,1800,1830,1900,1930,2000,2030,2100,2130, 2200);
 

	
 
    define('SP_PERMUTATIONS_PER_PAGE', 256); /** @TODO: Define this in config.inc */
 

	
 
    $npages = ceil($this->nPermutations / SP_PERMUTATIONS_PER_PAGE);
 
    $page = 0;
 
    if (isset($_REQUEST['page']))
 
      $page = $_REQUEST['page'];
 
    /*
 
     * only display the ``this page doesn't exist'' 404 if there is at
 
     * least one permutation. Otherwise, we give an irrelevant 404 for
 
     * users with no permutations.
 
     */
 
    if ($this->nPermutations && $page >= $npages)
 
      Page::show_404('Unable to find page ' . $page . ', there are only ' . $this->nPermutations . ' non-conflicting permutations, for a total of ' . $npages . ' pages.');
 
    /* zero-based */
 
    $first_permutation = $page * SP_PERMUTATIONS_PER_PAGE;
 
    $last_permutation = min($this->nPermutations, $first_permutation + SP_PERMUTATIONS_PER_PAGE);
 

	
 
    $footcloser = '';
 

	
 
    if(isset($_REQUEST['print']) && $_REQUEST['print'] != ''){
 
@@ -243,77 +265,90 @@ class Schedule
 
    $outputPage->head();
 

	
 

	
 

	
 
    if(isset($_REQUEST['print'])) {
 
 
 
     echo '<script type="text/javascript">';
 
      echo 'jQuery(document).ready( function() {';
 
 
 
      /* If user entered items to print */
 
      if($_REQUEST['print'] != 'all'){
 
	echo 'jQuery(\'.section\').hide();';
 
	$items = explode(',', $_REQUEST['print']);
 
	foreach($items as $item){
 
	  echo 'jQuery(\'#tabs-'.$item.'\').show();';
 
	}
 
      }
 
      echo '}); '; /* Close document.ready for jQuery */
 
      echo 'window.print(); </script>';
 

	
 
      echo '<p><a href="'.$_SERVER['SCRIPT_NAME'].'?s=' . $this->id_get() . '">&laquo; Return to normal view</a> </p>';
 

	
 
    }
 
    else {
 
      echo '<script type="text/javascript">';
 
      echo '        <script type="text/javascript">';
 
      echo '  jQuery(document).ready( function() {';
 
      echo '    jQuery("#tabs").tabs();';
 
      echo '    jQuery.address.change(function(event){';
 
      echo '      jQuery("#tabs").tabs( "select" , window.location.hash );';
 
      echo '    });';
 
      echo '    jQuery("#tabs").bind("tabsselect", function(event, ui) {';
 
      echo '      window.location.hash = ui.tab.hash;';
 
      echo '    });';
 

	
 

	
 
      echo '    jQuery("#sharedialog").dialog({ modal: true, width: 550, resizable: false, draggable: false, autoOpen: false });';
 
      echo '    jQuery("#share").click( function() {
 
                  jQuery("#sharedialog").dialog("open");
 
                });';
 
      echo '    jQuery(\'#printItems\').click( function() {
 
		  window.location = "'.$_SERVER['SCRIPT_NAME'].'?s='.$this->id_get().'&amp;print=" + (jQuery(\'#tabs\').tabs(\'option\',\'selected\') + 1);
 
	        });
 
	        jQuery(\'#cancelItems\').click( function() {
 
		  jQuery(\'#selectItemsInput\').hide();
 
	        });';
 
      echo '  });
 
            </script>';
 
	        });'
 
	. '  });' . PHP_EOL
 
	. '        </script>' . PHP_EOL;
 

	
 
      echo '<div id="sharedialog" title="Share Schedule"><p class="indent"><img class="noborder" src="http://facebook.com/favicon.ico" /> <a target="_blank" href="http://www.facebook.com/sharer.php?u=' . urlencode(htmlentities($outputPage->gen_share_url($this->id_get()))) .'&amp;t=My%20Schedule">Share on Facebook</a></p><p class="indent">You can also share your schedule with the URL below:<br /><em class="centeredtext smallurl">' . htmlentities($outputPage->gen_share_url($this->id_get())) . '</em></p></div>' . "\n";
 
      echo '<p><a href="input.php?s='.$this->id.'" class="button">Edit</a> <span id="printItems"><a href="#" class="button">Print</a></span> <span id="share"><a href="#" class="button">Share</a></span>  <a class="button" href="input.php">Home</a></p>'. "\n";
 
      echo '<p class="centeredtext">Having problems? <a href="feedback.php">Let us know</a>.</p>' . "\n";
 
      echo '<p class="centeredtext graytext"><em>Keyboard Shortcut: Left and right arrow keys switch between schedules</em></p>' . "\n";
 
      echo '        <div id="sharedialog" title="Share Schedule">' . PHP_EOL
 
	. '          <p class="indent"><img class="noborder" src="http://facebook.com/favicon.ico" /> <a target="_blank" href="http://www.facebook.com/sharer.php?u=' . urlencode(htmlentities($outputPage->gen_share_url($this->id_get()))) .'&amp;t=My%20Schedule">Share on Facebook</a></p><p class="indent">You can also share your schedule with the URL below:<br /><em class="centeredtext smallurl">' . htmlentities($outputPage->gen_share_url($this->id_get())) . '</em></p>' . PHP_EOL
 
	. '        </div>' . PHP_EOL
 
	. '        <p>' . PHP_EOL
 
	. '          <a href="input.php?s='.$this->id.'" class="button">Edit</a>' . PHP_EOL
 
	. '          <span id="printItems"><a href="#" class="button">Print</a></span>' . PHP_EOL
 
	. '          <span id="share"><a href="#" class="button">Share</a></span>' . PHP_EOL;
 

	
 

	
 
      if ($schedule_store !== NULL
 
	  && $this->parent_get() !== NULL
 
	  && ($parent_schedule = schedule_store_retrieve($schedule_store, $this->parent_get())) !== NULL)
 
	echo '          <a class="button" href="' . htmlentities($parent_schedule->url()) . '" title="Parent schedule: ' . htmlentities($parent_schedule->getName()) . '">Parent</a>' . PHP_EOL;
 

	
 
      echo '          <a class="button" href="input.php">Home</a>' . PHP_EOL
 
	. '        </p>'. PHP_EOL
 
	. '        <p class="centeredtext">Having problems? <a href="feedback.php">Let us know</a>.</p>' . PHP_EOL
 
	. '        <p class="centeredtext graytext"><em>Keyboard Shortcut: Left and right arrow keys switch between schedules</em></p>' . PHP_EOL;
 
    }		
 

	
 
    echo "\n";
 

	
 
    if($this->nPermutations > 0)
 
      {
 
	/*
 
	 * Figure out if we have to deal with Saturday and then deal
 
	 * with it.
 
	 *
 
	 * Also, ensure that our $time array is big enough for all of
 
	 * these courses.
 
	 */
 
	$max_day_plusone = 5;
 
	$have_saturday = FALSE;
 

	
 
	$max_time = (int)max($time);
 
	$min_time = (int)min($time);
 
	$sort_time = FALSE;
 
	foreach ($this->courses as $course)
 
	  {
 
	    for ($si = 0; $si < $course->getnsections(); $si ++)
 
	      foreach ($course->getSection($si)->getMeetings() as $meeting)
 
		{
 
@@ -577,41 +612,54 @@ class Schedule
 
  function url($id = NULL, $page = 0)
 
  {
 
    global $clean_urls;
 

	
 
    $url = '';
 
    if (!$clean_urls)
 
      $url .= 'process.php?s=';
 

	
 
    if (!$id)
 
      $id = $this->id;
 
    $url .= (int)$id;
 
    if ($clean_urls)
 
      $url .= '?';
 
    else
 
      $url .= '&';
 

	
 
    if ($page)
 
      $url .= 'page=' . (int)$page . '&';
 

	
 
    return $url;
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Get the ID of the schedule from which this schedule was
 
   *   derived.
 
   */
 
  public function parent_get()
 
  {
 
    return $this->parent_id;
 
  }
 

	
 
  /**
 
   * \brief
 
   *   A magic function which tries to upgrade old serialized sections
 
   *   to the new format.
 
   */
 
  function __wakeup()
 
  {
 
    if ($this->nclasses == -1)
 
      /* this Schedule doesn't need to be upgraded from Classes to Course */
 
      return;
 

	
 
    $this->courses = array();
 
    foreach ($this->classStorage as $classes)
 
      {
 
	$this->courses[] = $classes->to_course();
 
      }
 
    $this->nclasses = -1;
 

	
 
    if (empty($this->parent_id))
 
      $this->parent_id = NULL;
 
  }
 
}
input.php
Show inline comments
 
@@ -10,53 +10,54 @@
 
 * (at your option) any later version.
 
 *
 
 * SlatePermutate is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with SlatePermutate.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
include_once 'inc' . DIRECTORY_SEPARATOR . 'class.schedule.php';
 
include_once 'inc' . DIRECTORY_SEPARATOR . 'class.course.inc';
 
include_once 'inc' . DIRECTORY_SEPARATOR . 'class.section.php';
 
include_once 'inc' . DIRECTORY_SEPARATOR . 'class.page.php';
 
require_once('inc' . DIRECTORY_SEPARATOR . 'schedule_store.inc');
 

	
 
$scripts = array('jQuery', 'jQueryUI', 'jValidate','qTip','schedInput');
 
$inputPage = page::page_create('Scheduler', $scripts, FALSE);
 

	
 
$schedule_store = FALSE;
 
$sch = FALSE;
 
$school = $inputPage->get_school();
 

	
 
$parent_schedule_id = NULL;
 
if (isset($_REQUEST['s']))
 
  {
 
    $schedule_store = schedule_store_init();
 
    $schedule_id = (int)$_REQUEST['s'];
 
    $sch = schedule_store_retrieve($schedule_store, $schedule_id);
 
    $parent_schedule_id = (int)$_REQUEST['s'];
 
    $sch = schedule_store_retrieve($schedule_store, $parent_schedule_id);
 
  }
 

	
 
$my_hc = 'var slate_permutate_example_course_id = \'' . str_replace('\'', '\\\'', school_example_course_id($inputPage->get_school())) . '\';
 

	
 
jQuery(document).ready(
 
  function()
 
  {
 
    var class_last = 0;
 

	
 
';
 
if ($sch)
 
{
 
  $nclasses = $sch->nclasses_get();
 
  for ($class_key = 0; $class_key < $nclasses; $class_key ++)
 
    {
 
      $my_hc .= input_class_js($sch->class_get($class_key), '    ');
 
    }
 
}
 
else
 
  {
 
    $default_courses = school_default_courses($school);
 
    foreach ($default_courses as $default_class)
 
      $my_hc .= input_class_js($default_class, '    ');
 
    $my_hc .= '    class_last = add_class();
 
@@ -114,49 +115,52 @@ if (!empty($_REQUEST['selectsemester']))
 
  $inputPage->showSemesters();
 
  $inputPage->foot();
 
  exit;
 
  }
 

	
 
$inputPage->showSavedScheds($_SESSION);
 
?>
 
<p>
 
  Welcome to SlatePermutate<?php $inputPage->addressStudent(', ', '', FALSE); ?>!
 
  <?php if (school_has_auto($inputPage->get_school())): ?>
 
  To get started, enter in some a course identifier (e.g., <em>
 
  <?php echo school_example_course_id($inputPage->get_school()); ?></em>)
 
  and click the autosuggestion to automatically load available sections
 
  for each class.
 
  <?php else: ?>
 
  To get started, enter a course number and add some sections to it.
 
  Then specify each section's letter/number and what times it meets,
 
  add more courses, and click &ldquo;Find a Schedule&rdquo;.
 
  <!--'-->
 
  <?php endif; ?>
 
</p>
 

	
 
<form method="post" action="process.php" id="scheduleForm">
 
<p class="nospace" style="border-left: 5px solid #999; padding-left: 5px!important; padding-top: 5px!important;"><label>Schedule Name</label><br />
 
  <input id="scheduleName" style="margin-bottom: 1em;" class="defText required" type="text" size="25" title="Spring 2011" name="postData[name]" <?php if ($sch) echo 'value="' . htmlentities($sch->getName(), ENT_QUOTES) . '"'; /*"*/ ?> />
 
  <input id="scheduleName" style="margin-bottom: 1em;" class="defText required" type="text" size="25" title="Spring 2011" name="postData[name]" <?php if ($sch) echo 'value="' . htmlentities($sch->getName(), ENT_QUOTES) . '"'; ?> />
 
  <?php if (!empty($parent_schedule_id)): ?>
 
  <input type="hidden" name="postData[parent_schedule_id]" value="<?php echo htmlentities($parent_schedule_id); ?>" />
 
  <?php endif; ?>
 
</p>
 

	
 
<table id="container">
 
  <tr>
 
    <td>
 
      <table id="jsrows">
 
	<!-- Allow CSS to apply to entire rows at a time. -->
 
	<colgroup>
 
	  <col />
 
	  <col />
 
	  <col />
 
	  <col />
 
	  <col />
 
	  <col />
 
	  <col />
 
	  <col />
 
	  <col />
 
	  <col />
 
	  <col class="saturday<?php if (school_has_auto($inputPage->get_school())) echo ' collapsed';?>" />
 
	  <col />
 
	  <col />
 
	</colgroup>
 
        <!-- Header -->
 
        <tr>
process.php
Show inline comments
 
@@ -72,79 +72,93 @@ function prettyTime($time){
 
	return substr($time,0,strlen($time)-2) . ":" . substr($time,strlen($time)-2, strlen($time));
 
}
 

	
 
/*
 
 * The below code relies on sessions being started already.
 
 */
 
page::session_start();
 

	
 
$DEBUG = FALSE;
 
if (isset($_GET['debug']))
 
  $DEBUG = $_GET['debug'];
 

	
 
$schedule_store = schedule_store_init();
 

	
 
if(!$DEBUG)
 
  {
 
    $s = FALSE;
 
    if (isset($_GET['s']))
 
      $s = $_GET['s'];
 

	
 
    if($s !== FALSE)
 
      {
 
	$savedSched = schedule_store_retrieve($schedule_store, $s);
 
	if ($savedSched)
 
	  $savedSched->writeoutTables();
 
	  $savedSched->writeoutTables($schedule_store);
 
	else
 
	  page::show_404('Unable to find a saved schedule with an ID of ' . $s . '.');
 
      }
 
    elseif(isset($_GET['del']))
 
      {
 
	/* Allow the user to delete schedules that he has stored in his session */
 
	if ($_SESSION['saved'][(int)$_GET['del']])
 
	  {
 
	    /* user owns this schedule ID */
 
	    schedule_store_delete($schedule_store, (int)$_GET['del']);
 
	    unset($_SESSION['saved'][(int)$_GET['del']]);
 
	  }
 

	
 
	page::redirect('input.php');
 
	exit;
 
      }
 
    elseif (!isset($_POST['postData']))
 
      {
 
	page::redirect('input.php');
 
	exit;
 
      }
 
    else
 
      {
 
	/*
 
	 * we probably have input from the user and should interpret
 
	 * it as a schedule to permutate. Then we should redirect the
 
	 * user to the canonical URL for that schedule.
 
	 */
 
		$allClasses = new Schedule($_POST['postData']['name']);
 
	
 
	$name = '';
 
	if (!empty($_POST['postData']['name']))
 
	  $name = $_POST['postData']['name'];
 

	
 
	$parent_schedule_id = NULL;
 
	if (!empty($_POST['postData']['parent_schedule_id']))
 
	  {
 
	    $parent_schedule_id = (int)$_POST['postData']['parent_schedule_id'];
 
	    $parent_schedule = schedule_store_retrieve($schedule_store, $parent_schedule_id);
 
	    /* Detect bad parent_schedule reference. */
 
	    if (empty($parent_schedule))
 
	      $parent_schedule_id = NULL;
 
	  }
 

	
 
	$allClasses = new Schedule($name, $parent_schedule_id);
 

	
 
		foreach($_POST['postData'] as $class)
 
		{
 
		  /*
 
		   * Only add classes if the user added at least one
 
		   * section to the class. We know that $class['name']
 
		   * is not a section, so count() needs to be > 1 and
 
		   * we need to skip over 'name' in our loop.
 
		   */
 
			if(is_array($class) && count($class) > 1)
 
			{
 
				$allClasses->addCourse($class['name']);
 
		
 
				foreach($class as $section)
 
				  /* Skip the section name, which isn't a section */
 
					if(is_array($section))
 
					  {
 
					    $allClasses->addSection($class['name'], $section['letter'], $section['start'], $section['end'], arrayToDays($section['days'], 'alpha'), $section['synonym'], $section['professor'], $section['location'], $section['type']);
 
					  }
 
			}
 
		}
 
		$allClasses->findPossibilities();
 
		if (!isset($_SESSION['saved']))
 
		  $_SESSION['saved'] = array();
 
		$schedule_id = schedule_store_store($schedule_store, $allClasses);
0 comments (0 inline, 0 general)