diff --git a/inc/class.schedule.php b/inc/class.schedule.php --- a/inc/class.schedule.php +++ b/inc/class.schedule.php @@ -108,15 +108,28 @@ class Schedule $this->courses[] = new Course($n); } - //-------------------------------------------------- - // Adds a section to the desired class. - //-------------------------------------------------- + /** + * \brief + * Adds a section to this semester after finding the class. + * + * \return + * NULL on success, a string on error which is a message for the + * user and a valid XHTML fragment. + */ function addSection($course_name, $letter, $time_start, $time_end, $days, $synonym = NULL, $faculty = NULL, $location = NULL, $type = 'lecture') { if (empty($letter) && (empty($time_start) || !strcmp($time_start, 'none')) && (empty($time_end) || !strcmp($time_end, 'none')) && empty($days) && empty($synonym) && empty($faculty) && empty($location) && (empty($type) || !strcmp($type, 'lecture'))) return; + /* reject invalid times */ + if (!strcmp($time_start, 'none') || !strcmp($time_end, 'none') + || $time_start > $time_end) + { + return 'Invalid time specifications for ' . htmlentities($course_name) . '-' . htmlentities($letter) + . '. Start time: ' . htmlentities($time_start) . '. End time: ' . htmlentities($time_end) . '.'; + } + foreach ($this->courses as $course) if (!strcmp($course_name, $course->getName())) { diff --git a/input.php b/input.php --- a/input.php +++ b/input.php @@ -29,6 +29,7 @@ require_once('inc' . DIRECTORY_SEPARATOR $schedule_store = FALSE; $sch = FALSE; +$fix_errors = FALSE; $school = $inputPage->get_school(); $parent_schedule_id = NULL; @@ -38,6 +39,16 @@ if (isset($_REQUEST['s'])) $parent_schedule_id = (int)$_REQUEST['s']; $sch = schedule_store_retrieve($schedule_store, $parent_schedule_id); } +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; + $parent_schedule_id = (int)$_POST['postData']['parent_schedule_id']; + } $my_hc = 'var slate_permutate_example_course_id = \'' . str_replace('\'', '\\\'', school_example_course_id($inputPage->get_school())) . '\'; @@ -55,6 +66,29 @@ if ($sch) $my_hc .= input_class_js($sch->class_get($class_key), ' '); } } +elseif ($errors_fix) + { + foreach ($_POST['postData'] as $course) + if (is_array($course)) + { + if (empty($course['name'])) + $my_hc .= ' class_last = add_class();' . PHP_EOL; + else + $my_hc .= ' class_last = add_class_n(\'' . htmlentities($course['name'], ENT_QUOTES) . '\');' . PHP_EOL; + foreach ($course as $section) + if (is_array($section)) + $my_hc .= ' add_section_n(class_last, \'' . htmlentities($section['letter'], ENT_QUOTES) . '\', \'' + . htmlentities($section['synonym'], ENT_QUOTES) . '\', \'' . htmlentities($section['start'], ENT_QUOTES) . '\', \'' + . htmlentities($section['end'], ENT_QUOTES) . '\', ' + . json_encode(array('m' => !empty($section['days'][0]), 't' => !empty($section['days'][1]), 'w' => !empty($section['days'][2]), + 'h' => !empty($section['days'][3]), 'f' => !empty($section['days'][4]), + 's' => !empty($section['days'][5]))) + . ', \'' . htmlentities($section['professor'], ENT_QUOTES) . '\', \'' + . htmlentities($section['location'], ENT_QUOTES) . '\', \'' + . htmlentities($section['type'], ENT_QUOTES) . '\');' . PHP_EOL; + $my_hc .= PHP_EOL; + } + } else { $default_courses = school_default_courses($school); @@ -135,7 +169,20 @@ if (!empty($_REQUEST['selectsemester']))


- /> + /> diff --git a/process.php b/process.php --- a/process.php +++ b/process.php @@ -72,6 +72,29 @@ function prettyTime($time){ return substr($time,0,strlen($time)-2) . ":" . substr($time,strlen($time)-2, strlen($time)); } +/** + * \brief + * Convert a multidimensional array to a set of s. + * + * Currently just echos out the s as they are created. + * + * \param $array + * The array to make into a set of s. + * \param $base + * The string to prefix. Normally the name of the array variable. + * \param $blankness + * A string to insert at the beginning of each line before an for indentation's sake. + */ +function array_to_form($array, $base = '', $blankness = ' ') +{ + foreach ($array as $key => $val) + if (is_array($val)) + array_to_form($val, $base . '[' . $key . ']', $blankness); + else + echo $blankness . '' . PHP_EOL; +} + /* * The below code relies on sessions being started already. */ @@ -138,6 +161,7 @@ if(!$DEBUG) $allClasses = new Schedule($name, $parent_schedule_id); + $errors = array(); foreach($_POST['postData'] as $class) { /* @@ -154,10 +178,47 @@ if(!$DEBUG) /* 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']); + $error_string = $allClasses->addSection($class['name'], $section['letter'], $section['start'], $section['end'], arrayToDays($section['days'], 'alpha'), $section['synonym'], $section['professor'], $section['location'], $section['type']); + if ($error_string !== NULL) + $errors[] = $error_string; } } } + + /* + * Tell the user that his input is erroneous and + * require him to fix it. + */ + if (count($errors)) + { + $error_page = new Page('Process Schedule — Errors'); + + echo '

' . PHP_EOL + . ' You have the following errors in your input:' . PHP_EOL + . '

' . PHP_EOL + . ' ' . PHP_EOL + . '

Solving Errors

' . PHP_EOL + . ' ' . PHP_EOL; + + /* Regurgitate the postData into a */ + echo ' ' . PHP_EOL + . ' ' . PHP_EOL; + array_to_form($_POST['postData'], 'postData', ' '); + echo ' ' . PHP_EOL + . '
' . PHP_EOL; + + $error_page->foot(); + exit; + } + $allClasses->findPossibilities(); if (!isset($_SESSION['saved'])) $_SESSION['saved'] = array();