<?php 
/*
 * Copyright 2010 Nathan Gelderloos, Ethan Zonca, Nathan Phillip Brink
 *
 * This file is part of SlatePermutate.
 *
 * SlatePermutate is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (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';
include_once 'inc' . DIRECTORY_SEPARATOR . 'class.mobilepage.php';
require_once('inc' . DIRECTORY_SEPARATOR . 'schedule_store.inc');

/*
 * Help constrol whether or not the school selection dialogue should
 * be shown or whether or not $_SESSION['school_chosen'] should be set
 * TRUE. These things should generally be false when loading a saved
 * schedule.
 */
$creating_new_schedule = TRUE;

$schedule_store = FALSE;
$sch = FALSE;
$errors_fix = FALSE;
$inputPage_options = array('school_semester_constant' => FALSE);

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

    /*
     * Allow a user to change the school and semester of a
     * saved_schedule he's trying to revive if he really wants to.
     */
    if (!empty($_GET['school']))
      $school = school_load_guess(FALSE);
    else
      $school = $sch->school_get();

    if (!empty($_GET['semester']))
      $semester = school_semester_guess($school, FALSE);
    else
      $semester = $sch->semester_get();

    if (!empty($sch))
      {
	$creating_new_schedule = FALSE;
	$inputPage_options += array('school' => $school,
				    'semester' => $semester);
      }
    else
      $parent_schedule_id = NULL;

    /*
     * Code outside of this block should _not_ assume $school and/or
     * $semester are defined. But it'd be more expensive to unset()
     * them here than to just overwrite them later...
     */
  }
elseif (!empty($_REQUEST['e']))
  {
    /*
     * Read an errorful schedule out of $_POST, this $_POST is created
     * by process.php when the originally sinful user produces bad
     * data.
     */
    $errors_fix = TRUE;

    if (!empty($_POST['postData']))
      $postData = $_POST['postData'];

    if (!empty($postData['parent_schedule_id']))
      $parent_schedule_id = (int)$postData['parent_schedule_id'];

    if (!empty($postData['school']))
      {
	$school = school_load($postData['school']);
	if (!empty($school))
	  $inputPage_options['school'] = $school;
      }

    if (!empty($school) && !empty($postData['semester']))
      {
	$semesters = school_semesters($school);
	if (!empty($semesters[$postData['semester']]))
	  $inputPage_options['semester'] = $semester;
      }

    $creating_new_schedule = FALSE;
  }

/*
 * We cannot initialize the page object nor guess the school before
 * figuring loading a saved schedule because we'll default to that
 * saved_schedule's school/semester.
 */
$scripts = array('jQueryMobile', 'jQueryUI-NoStyles', 'mScheduleInput');
$inputPage = mobilepage::page_create('Scheduler', $scripts, $inputPage_options);
$school = $inputPage->get_school();
$semester = $inputPage->semester_get();

$hc = ' 
';

$inputPage->headcode_add('scheduleInput', $hc, TRUE);

$inputPage->head();

/*
 * Force a student to choose a school or declare he's a generic
 * student before displaying the input form. To do this, we need
 * another variable in $_SESSION: $_SESSION['school_chosen'].
 */
if (!empty($_REQUEST['school']) && !empty($_SESSION['school']) && !strcmp($_REQUEST['school'], $_SESSION['school']))
  $_SESSION['school_chosen'] = TRUE;
if (!empty($_REQUEST['selectschool'])
    || empty($school) || $school['id'] == 'default' && empty($_SESSION['school_chosen']))
  {
    $next_page = 'input.php?';
    if (isset($_GET['s']))
      $next_page .= 's=' . (int)$_GET['s'] . '&';
    if (isset($_GET['semester']))
      $next_page .= 'semester=' . htmlentities($$_GET['semester']) . '&';
?>
<h2>School Selection</h2>
<p>
  Choose the school you attend from the list below. <strong>If you cannot
  find your school</strong>, you may proceed using
  the <a href="<?php echo htmlentities($next_page); ?>school=default">generic
  settings</a>.
</p>
<?php
    $inputPage->showSchools($next_page);
    $inputPage->foot();
    exit;
  }

if (!empty($_REQUEST['selectsemester']))
  {
?>
<h2>Semester Selection</h2>
<p>
  Choose the semester for which you wish you make a schedule from the
  list below. If any semester is missing, please <a
  href="feedback.php?feedback=My+school+is+missing+the+&lt;semester+name&gt;+semester.">let us know</a>.
</p>
<?php
  $next_page = 'input.php?';
  if (isset($_GET['s']))
    $next_page .= 's=' . (int)$_GET['s'] . '&';
  if (!empty($_GET['school']))
    $next_page .= 'school=' . $_GET['school'] . '&';

  $inputPage->showSemesters($next_page);
  $inputPage->foot();
  exit;
  }

$inputPage->showSavedScheds($_SESSION);
?>

<?php if (!empty($input_warning_banner)): ?>
<div class="warning">
  <?php echo $input_warning_banner; ?>
</div>
<?php endif; ?>

<p>
  Welcome to SP<?php $inputPage->addressStudent(', ', '', FALSE); ?>!
  <?php if (school_has_auto($inputPage->get_school())): ?>
  To get started, enter in a course identifier (e.g., <em>
 <!--'-->
  <?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"
    title="My <?php echo $semester['name']; ?> Schedule"
    name="postData[name]"
    <?php
      if ($sch)
        echo 'value="' . htmlentities($sch->getName(), ENT_QUOTES) . '"';
      elseif ($errors_fix)
        echo 'value="' . htmlentities($_POST['postData']['name'], ENT_QUOTES) . '"';
    ?> />
  <?php if (!empty($parent_schedule_id)): ?>
  <input type="hidden" name="postData[parent_schedule_id]" value="<?php echo htmlentities($parent_schedule_id, ENT_QUOTES); ?>" />
  <input type="hidden" name="postData[school]" value="<?php echo htmlentities($school['id']); ?>" />
  <input type="hidden" name="postData[semester]" value="<?php echo htmlentities($semester['id']); ?>" />
  <?php endif; ?>
</p>

<table id="courseTable">
	<tr><td><input class="courseInput" type="text"><a class="check" href="#" data-role="button" data-icon="check" data-iconpos="notext">OK</a></td></tr>
</table>

<div class="paddingtop">
  <input data-role="button" data-theme="b" type="submit" value="Find a schedule" />
</div>

</form>

<?php
$inputPage->foot();

function input_course_js(Course $course, $whitespace = '  ')
{
  $title = $course->title_get();
  if (empty($title))
    $title = '';
  $js = $whitespace . 'class_last = add_class_n(' . json_encode($course->getName()) . ', '
    . json_encode($title) . ');' . PHP_EOL;

  foreach ($course as $course_slot)
    foreach ($course_slot as $section)
      {
	$meetings = $section->getMeetings();
      foreach ($meetings as $meeting)
	{
	  $js .= $whitespace . 'add_section_n(class_last, ' . json_encode($section->getLetter()) . ', '
	    . json_encode($section->getSynonym()) . ', '
	    . json_encode($meeting->getStartTime()) . ', '
	    . json_encode($meeting->getEndTime()) . ', '
	    . json_encode(array('m' => $meeting->getDay(0), 't' => $meeting->getDay(1), 'w' => $meeting->getDay(2), 'h' => $meeting->getDay(3), 'f' => $meeting->getDay(4),
				's' => $meeting->getDay(5))) . ', '
	    . json_encode($meeting->instructor_get()) . ', '
	    . json_encode($meeting->getLocation()) . ', '
	    . json_encode($meeting->type_get()) . ', '
	    . json_encode($course_slot->id_get()) . ');' . PHP_EOL;
	}
    }

  return $js;
}
