Location: PHPKode > projects > SASHA > SASHA/inc/lib/lib.schedule.php
<?php

/**
 * SASHA :: inc/lib/lib.schedule.php
 *
 * This contains all of the schedule functions.
 *
 * @package SASHA
 * @copyright (C) 2006-2010 Gordon P. Hemsley
 * @license docs/LICENSE BSD License
 * @version $Id: lib.schedule.php 86 2010-02-06 04:51:45Z gphemsley $
 */

/**
 * Schedule
 *
 * Child class for tasks related to Schedule
 *
 * @package SASHA
 */
class Schedule extends Base
{
	public function __construct( $semester = FALSE, $institution = FALSE )
	{
		parent::__construct( $semester, $institution );

		$this->schedule_type = ( exists( $_POST['schedule_type'] ) ) ? (int) $_POST['schedule_type'] : 0;
		$this->schedule_days = ( exists( $_POST['days'] ) ) ? $_POST['days'] : array(
			'M'	=>	FALSE,
			'T'	=>	FALSE,
			'W'	=>	FALSE,
			'R'	=>	FALSE,
			'F'	=>	FALSE,
			'S'	=>	FALSE,
			'U'	=>	FALSE
		);
		$this->subject = ( exists( $_POST['subject'] ) ) ? (string) $_POST['subject'] : FALSE;
		$this->course = ( exists( $_POST['course'] ) ) ? (int) $_POST['course'] : FALSE;
		$this->section = ( exists( $_POST['section'] ) ) ? (string) $_POST['section'] : FALSE;
		$this->start_time = ( exists( $_POST['start_time'] ) ) ? $_POST['start_time'] : array(
			'hour'		=>	8,
			'minute'	=>	0,
			'meridiem'	=>	'AM'
		);
		$this->end_time = ( exists( $_POST['end_time'] ) ) ? $_POST['end_time'] : array(
			'hour'		=>	9,
			'minute'	=>	0,
			'meridiem'	=>	'AM'
		);

		$this->parent_schedule_id = ( exists( $_POST['parent_schedule_id'] ) ) ? (int) $_POST['parent_schedule_id'] : FALSE;
		$this->instructors = ( exists( $_POST['instructors'] ) ) ? $_POST['instructors'] : FALSE;
		$this->section_title = ( exists( $_POST['section_title'] ) ) ? (string) $_POST['section_title'] : FALSE;
	}

	public function add_schedule()
	{
		global $Database, $User;

		$institution = ( $this->institution ) ? $this->institution : $this->default_institution;
		$semester = ( $this->semester ) ? $this->semester : $this->default_semester;

		$start_time = $this->convert_hour( TRUE, $this->start_time['hour'], $this->start_time );
		$end_time = $this->convert_hour( TRUE, $this->end_time['hour'], $this->end_time );

		$this_step = ( exists( $_POST['step'] ) ) ? (int) $_POST['step'] : FALSE;
		$prev_step = ( $this_step ) ? (int) ( $this_step - 1 ) : FALSE;
		$next_step = ( $this_step ) ? (int) ( $this_step + 1 ) : FALSE;

		$next = ( exists( $_POST['next'] ) ) ? TRUE : FALSE;
		$finish = ( exists( $_POST['finish'] ) ) ? TRUE : FALSE;

		if( $finish )
		{
			$sql = "INSERT INTO schedules ( user_id, institution, semester, subject, course, section, section_title, schedule_type, days, start_time, end_time, instructors, parent_schedule_id )
				VALUES ( {$User->user_info['id']}, '$institution', '$semester', '" . $Database->escape( $this->subject ) . "', '{$this->course}', '" . $Database->escape( $this->section ) . "', '" . $Database->escape( $this->section_title ) . "', {$this->schedule_type}, b'" . $this->format_days( $this->schedule_days, 'array', 'binary' ) . "', " . $this->format_time( $this->start_time, FALSE, 'array', '24-hour' ) . ', ' . $this->format_time( $this->end_time, FALSE, 'array', '24-hour' ) . ", '" . $this->prepare_instructors( $this->instructors ) . "', {$this->parent_schedule_id} )";
			$sql = str_replace( array( " '',", " '' ", ' ,', '  ' ), array( ' NULL,', ' NULL ', ' NULL,', ' NULL ' ), $sql );

			$result = $Database->query( $sql );

			if( $result )
			{
				print_message( 'good', 'Schedule for ' . $this->format_course( $this->subject, $this->course, $institution, '&nbsp;' ) . ' ' . $this->section . ' at ' . $this->format_institution( $institution ) . ' (' . $this->format_instructors( $this->instructors, '; ' ) . ' &mdash; ' . $this->format_schedule_type( $this->schedule_type ) . ': ' . $this->format_days( $this->schedule_days, 'array', 'html' ) . ') added successfully.', 'Addition successful.' );
			}
			else
			{
				print_message( 'bad', 'All fields in Step 1 are required. Check to see if you left any blank.', 'Addition failed.' );
			}
		}
		elseif( $next )
		{
			switch( $this_step )
			{
				case 1:
					print "\t" . '<h2>Add Schedule</h2>' . "\n";
					print "\t" . '<h3>Step 2</h3>' . "\n";

					print "\t" . '<p style="text-align: left;"><strong>Institution:</strong>&nbsp;' . $this->format_institution( $institution ) . '<br />' . "\n";
					print "\t" . '<strong>Semester:</strong>&nbsp;' . $this->format_semester( $semester ) . '<br />' . "\n";
					print "\t" . '<strong>Course:</strong>&nbsp;' . $this->format_course( $this->subject, $this->course, $institution, '&nbsp;' ) . ' (' . $this->section . ')<br />' . "\n";
					print "\t" . '<strong>Course Title:</strong>&nbsp;' . htmlentities( $this->get_course_title( $institution, $this->subject, $this->course ) )  . '<br />' . "\n";
					print "\t" . '<strong>Schedule Type:</strong>&nbsp;' . $this->format_schedule_type( $this->schedule_type ) . '<br />' . "\n";
					print "\t" . '<strong>Days:</strong>&nbsp;' . $this->format_days( $this->schedule_days, 'array', 'html' ) . '<br />' . "\n";
					print "\t" . '<strong>Time:</strong>&nbsp;' . $this->format_time( $this->start_time, TRUE, 'array', 'html' ) . '&ndash;' . $this->format_time( $this->end_time, TRUE, 'array', 'html' ) . '</p>' . "\n";

					$form_data = array(
						array(
							'type'	=>	'course',
							'name'	=>	'parent_schedule_id',
							'label'	=>	'Parent Schedule',
							'data'	=>	array( $this->parent_schedule_id, $semester )
						),
						array(
							'type'	=>	'instructor',
							'name'	=>	'instructors',
							'label'	=>	'Instructors',
							'data'	=>	array( $this->instructors, $institution )
						),
						array(
							'type'	=>	'text',
							'name'	=>	'section_title',
							'label'	=>	'Section Title',
							'data'	=>	array(
								'size'		=>	75,
								'maxlength'	=>	255,
								'value'		=>	$this->section_title
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'step',
							'data'	=>	array(
								'value'	=>	2
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'institution',
							'data'	=>	array(
								'value'	=>	$institution
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'semester',
							'data'	=>	array(
								'value'	=>	$semester
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'schedule_type',
							'data'	=>	array(
								'value'	=>	$this->schedule_type
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'days[M]',
							'data'	=>	array(
								'value'	=>	( ( isset( $this->schedule_days['M'] ) ) ? (bool) $this->schedule_days['M'] : is_int( array_search( 'M', $this->schedule_days ) ) )
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'days[T]',
							'data'	=>	array(
								'value'	=>	( ( isset( $this->schedule_days['T'] ) ) ? (bool) $this->schedule_days['T'] : is_int( array_search( 'T', $this->schedule_days ) ) )
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'days[W]',
							'data'	=>	array(
								'value'	=>	( ( isset( $this->schedule_days['W'] ) ) ? (bool) $this->schedule_days['W'] : is_int( array_search( 'W', $this->schedule_days ) ) )
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'days[R]',
							'data'	=>	array(
								'value'	=>	( ( isset( $this->schedule_days['R'] ) ) ? (bool) $this->schedule_days['R'] : is_int( array_search( 'R', $this->schedule_days ) ) )
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'days[F]',
							'data'	=>	array(
								'value'	=>	( ( isset( $this->schedule_days['F'] ) ) ? (bool) $this->schedule_days['F'] : is_int( array_search( 'F', $this->schedule_days ) ) )
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'days[S]',
							'data'	=>	array(
								'value'	=>	( ( isset( $this->schedule_days['S'] ) ) ? (bool) $this->schedule_days['S'] : is_int( array_search( 'S', $this->schedule_days ) ) )
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'days[U]',
							'data'	=>	array(
								'value'	=>	( ( isset( $this->schedule_days['U'] ) ) ? (bool) $this->schedule_days['U'] : is_int( array_search( 'U', $this->schedule_days ) ) )
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'subject',
							'data'	=>	array(
								'value'	=>	$this->subject
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'course',
							'data'	=>	array(
								'value'	=>	$this->course
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'section',
							'data'	=>	array(
								'value'	=>	$this->section
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'start_time[hour]',
							'data'	=>	array(
								'value'	=>	$this->start_time['hour']
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'start_time[minute]',
							'data'	=>	array(
								'value'	=>	$this->start_time['minute']
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'start_time[meridiem]',
							'data'	=>	array(
								'value'	=>	$this->start_time['meridiem']
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'end_time[hour]',
							'data'	=>	array(
								'value'	=>	$this->end_time['hour']
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'end_time[minute]',
							'data'	=>	array(
								'value'	=>	$this->end_time['minute']
							)
						),
						array(
							'type'	=>	'hidden',
							'name'	=>	'end_time[meridiem]',
							'data'	=>	array(
								'value'	=>	$this->end_time['meridiem']
							)
						),
						array(
							'type'	=>	'submit',
							'name'	=>	'finish',
							'data'	=>	array(
								'value'	=>	'Finish'
							)
						)
					);

					Forms::create_form( 'add-schedule', ROOT . 'schedule.php?mode=add', $form_data );
				break;

				default:
					// Something went wrong.
					break 2;
				break;
			}

			return;
		}

		print "\t" . '<h2>Add Schedule</h2>' . "\n";
		print "\t\t" . '<h3>Step 1</h3>' . "\n";

		$form_data = array(
			array(
				'type'	=>	'institution',
				'name'	=>	'institution',
				'label'	=>	'Institution',
				'data'	=>	array( $institution )
			),
			array(
				'type'	=>	'semester',
				'name'	=>	'semester',
				'label'	=>	'Semester',
				'data'	=>	array( $semester )
			),
			array(
				'type'	=>	'schedule_type',
				'name'	=>	'schedule_type',
				'label'	=>	'Schedule Type',
				'data'	=>	array( $this->schedule_type )
			),
			array(
				'type'	=>	'days',
				'name'	=>	'days',
				'label'	=>	'Days',
				'data'	=>	array( $this->schedule_days )
			),
			array(
				'type'	=>	'text',
				'name'	=>	'subject',
				'label'	=>	'Subject',
				'data'	=>	array(
					'size'		=>	5,
					'maxlength'	=>	5,
					'value'		=>	$this->subject
				)
			),
			array(
				'type'	=>	'text',
				'name'	=>	'course',
				'label'	=>	'Course',
				'data'	=>	array(
					'size'		=>	4,
					'maxlength'	=>	4,
					'value'		=>	$this->course

				)
			),
			array(
				'type'	=>	'text',
				'name'	=>	'section',
				'label'	=>	'Section',
				'data'	=>	array(
					'size'		=>	5,
					'maxlength'	=>	5,
					'value'		=>	$this->section

				)
			),
			array(
				'type'	=>	'time',
				'name'	=>	'start_time',
				'label'	=>	'Start Time',
				'data'	=>	array( mktime( $start_time['hour'], $start_time['minute'], 0 ) )
			),
			array(
				'type'	=>	'time',
				'name'	=>	'end_time',
				'label'	=>	'End Time',
				'data'	=>	array( mktime( $end_time['hour'], $end_time['minute'], 0 ) )
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'parent_schedule_id',
				'data'	=>	array(
					'value'	=>	$this->parent_schedule_id
				)
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'instructors[0]',
				'data'	=>	array(
					'value'	=>	$this->instructors[0]
				)
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'instructors[1]',
				'data'	=>	array(
					'value'	=>	$this->instructors[1]
				)
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'instructors[2]',
				'data'	=>	array(
					'value'	=>	$this->instructors[2]
				)
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'instructors[3]',
				'data'	=>	array(
					'value'	=>	$this->instructors[3]
				)
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'instructors[4]',
				'data'	=>	array(
					'value'	=>	$this->instructors[4]
				)
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'instructors[5]',
				'data'	=>	array(
					'value'	=>	$this->instructors[5]
				)
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'section_title',
				'data'	=>	array(
					'value'	=>	$this->section_title
				)
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'step',
				'data'	=>	array(
					'value'	=>	1
				)
			),
			array(
				'type'	=>	'submit',
				'name'	=>	'next',
				'data'	=>	array(
					'value'	=>	'Next'
				)
			)
		);

		Forms::create_form( 'add-schedule', ROOT . 'schedule.php?mode=add', $form_data );
	}

	public function edit_schedule( $schedule_id )
	{
		global $Database;

		if( !$schedule_id )
		{
			print_message( 'bad', 'Please specify which schedule you want to edit.', 'Schedule ID not specified.' );

			return FALSE;
		}

		$institution = ( $this->institution ) ? $this->institution : $this->default_institution;
		$semester = ( $this->semester ) ? $this->semester : $this->default_semester;

		$institution = ( exists( $_POST['institution'] ) ) ? $_POST['institution'] : $institution;
		$semester = ( exists( $_POST['semester'] ) ) ? ( ( is_array( $_POST['semester'] ) ) ? $_POST['semester']['year'] . $_POST['semester']['season'] : $_POST['semester'] ) : $semester;

		$this_step = ( exists( $_POST['step'] ) ) ? (int) $_POST['step'] : 1;

		if( ( $this_step > 1 ) && ( empty( $this->subject ) || empty( $this->course ) || empty( $this->section ) ) )
		{
			print_message( 'bad', 'All fields in Step 1 are required. Do not leave any blank.', 'Addition failed.' );

			$this_step = 1;
		}

		$prev_step = ( $this_step ) ? (int) ( $this_step - 1 ) : 0;
		$next_step = ( $this_step ) ? (int) ( $this_step + 1 ) : 0;

		$sql = "SELECT c.*, s.*
			FROM schedules s
			LEFT JOIN ( courses c, institutions i )
				ON ( c.institution = i.institution
					AND s.institution = c.institution
					AND s.subject = c.subject
					AND s.course = c.course )
			WHERE s.schedule_id = $schedule_id
			ORDER BY i.name ASC, s.semester ASC, s.subject ASC, s.course ASC, s.section ASC, s.days DESC";

		$result = $Database->query( $sql );
		$schedule = $Database->fetch_assoc( $result );
		$Database->free_result( $result );

		$course = $this->format_course( $schedule['subject'], $schedule['course'], $institution, FALSE );

		switch( $this_step )
		{
			case 3:
				$sql = "UPDATE schedules
					SET institution = '$institution', semester = '$semester', subject = '" . $Database->escape( $this->subject ) . "', course = {$this->course}, section = '" . $Database->escape( $this->section ) . "', section_title = '" . $Database->escape( $this->section_title ) . "', schedule_type = {$this->schedule_type}, days = b'" . $this->format_days( $this->schedule_days, 'decimal', 'binary' ) . "', start_time = {$this->start_time}, end_time = {$this->end_time}, instructors = '" . $this->prepare_instructors( $this->instructors ) . "', parent_schedule_id = {$this->parent_schedule_id}
					WHERE schedule_id = $schedule_id";
				$sql = str_replace( array( "= '',", "= '' ", '= ,', '=  ', "= \n" ), array( '= NULL,', '= NULL ', '= NULL,', '= NULL ', "= NULL\n" ), $sql );

				$result = $Database->query( $sql );

				if( $result )
				{
					print_message( 'good', 'Schedule for ' . $this->format_course( $this->subject, $this->course, $institution, '&nbsp;' ) . ' ' . $this->section . ' at ' . $this->format_institution( $institution ) . ' (' . $this->format_instructors( $this->instructors, '; ' ) . ' &mdash; ' . $this->format_schedule_type( $this->schedule_type ) . ': ' . $this->format_days( $this->schedule_days, 'decimal', 'html' ) . ') updated successfully.', 'Update successful.' );

					$this->list_schedule();

					return;
				}
				else
				{
					print_message( 'bad', 'Check all fields and try again.', 'Addition failed.' );
				}

				$form_data = array();
			break;

			case 2:
				print "\t" . '<h2>Edit Schedule</h2>' . "\n";
				print "\t" . '<h3>Step 2</h3>' . "\n";

				print "\t" . '<p style="text-align: left;"><strong>Institution:</strong>&nbsp;' . $this->format_institution( $institution ) . '<br />' . "\n";
				print "\t" . '<strong>Semester:</strong>&nbsp;' . $this->format_semester( $semester ) . '<br />' . "\n";
				print "\t" . '<strong>Course:</strong>&nbsp;' . $this->format_course( $this->subject, $this->course, $institution, '&nbsp;' ) . ' (' . $this->section . ')<br />' . "\n";
				print "\t" . '<strong>Course Title:</strong>&nbsp;' . htmlentities( $this->get_course_title( $institution, $this->subject, $this->course ) )  . '<br />' . "\n";
				print "\t" . '<strong>Schedule Type:</strong>&nbsp;' . $this->format_schedule_type( $this->schedule_type ) . '<br />' . "\n";
				print "\t" . '<strong>Days:</strong>&nbsp;' . $this->format_days( $this->schedule_days, 'array', 'html' ) . '<br />' . "\n";
				print "\t" . '<strong>Time:</strong>&nbsp;' . $this->format_time( $this->start_time, TRUE, 'array', 'html' ) . '&ndash;' . $this->format_time( $this->end_time, TRUE, 'array', 'html' ) . '</p>' . "\n";

				$form_data = array(
					array(
						'type'	=>	'course',
						'name'	=>	'parent_schedule_id',
						'label'	=>	'Parent Schedule',
						'data'	=>	array( $this->parent_schedule_id, $semester )
					),
					array(
						'type'	=>	'instructor',
						'name'	=>	'instructors',
						'label'	=>	'Instructors',
						'data'	=>	array( $this->instructors, $institution )
					),
					array(
						'type'	=>	'text',
						'name'	=>	'section_title',
						'label'	=>	'Section Title',
						'data'	=>	array(
							'size'		=>	75,
							'maxlength'	=>	255,
							'value'		=>	$this->section_title
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'step',
						'data'	=>	array(
							'value'	=>	3
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'institution',
						'label'	=>	'Institution',
						'data'	=>	array(
							'value'		=>	$institution
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'semester',
						'label'	=>	'Semester',
						'data'	=>	array(
							'value'		=>	$semester
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'schedule_type',
						'label'	=>	'Schedule Type',
						'data'	=>	array(
							'value'		=>	(int) $this->schedule_type
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'days',
						'label'	=>	'Days',
						'data'	=>	array(
							'value'		=>	$this->format_days( $this->schedule_days, 'array', 'decimal' )
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'subject',
						'label'	=>	'Subject',
						'data'	=>	array(
							'value'		=>	$this->subject
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'course',
						'label'	=>	'Course',
						'data'	=>	array(
							'value'		=>	$this->course
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'section',
						'label'	=>	'Section',
						'data'	=>	array(
							'value'		=>	$this->section
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'start_time',
						'label'	=>	'Start Time',
						'data'	=>	array(
							'value'		=>	$this->format_time( $this->start_time, FALSE, 'array', '24-hour' )
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'end_time',
						'label'	=>	'End Time',
						'data'	=>	array(
							'value'		=>	$this->format_time( $this->end_time, FALSE, 'array', '24-hour' )
						)
					),
					array(
						'type'	=>	'submit',
						'name'	=>	'finish',
						'data'	=>	array(
							'value'	=>	'Finish'
						)
					)
				);
			break;

			case 1:
			default:
				print "\t" . '<h2>Edit Schedule</h2>' . "\n";
				print "\t" . '<h3>Step 1</h3>' . "\n";

				$form_data = array(
					array(
						'type'	=>	'institution',
						'name'	=>	'institution',
						'label'	=>	'Institution',
						'data'	=>	array( $schedule['institution'] )
					),
					array(
						'type'	=>	'semester',
						'name'	=>	'semester',
						'label'	=>	'Semester',
						'data'	=>	array( $schedule['semester'] )
					),
					array(
						'type'	=>	'schedule_type',
						'name'	=>	'schedule_type',
						'label'	=>	'Schedule Type',
						'data'	=>	array( (int) $schedule['schedule_type'] )
					),
					array(
						'type'	=>	'days',
						'name'	=>	'days',
						'label'	=>	'Days',
						'data'	=>	array( $schedule['days'] )
					),
					array(
						'type'	=>	'text',
						'name'	=>	'subject',
						'label'	=>	'Subject',
						'data'	=>	array(
							'size'		=>	5,
							'maxlength'	=>	5,
							'value'		=>	$course['subject']
						)
					),
					array(
						'type'	=>	'text',
						'name'	=>	'course',
						'label'	=>	'Course',
						'data'	=>	array(
							'size'		=>	4,
							'maxlength'	=>	4,
							'value'		=>	$course['course']
						)
					),
					array(
						'type'	=>	'text',
						'name'	=>	'section',
						'label'	=>	'Section',
						'data'	=>	array(
							'size'		=>	5,
							'maxlength'	=>	5,
							'value'		=>	$schedule['section']
						)
					),
					array(
						'type'	=>	'time',
						'name'	=>	'start_time',
						'label'	=>	'Start Time',
						'data'	=>	array( $schedule['start_time'] )
					),
					array(
						'type'	=>	'time',
						'name'	=>	'end_time',
						'label'	=>	'End Time',
						'data'	=>	array( $schedule['end_time'] )
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'step',
						'data'	=>	array(
							'value'	=>	2
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'parent_schedule_id',
						'label'	=>	'Parent Schedule',
						'data'	=>	array(
							'value'	=>	$schedule['parent_schedule_id']
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'instructors',
						'label'	=>	'Instructors',
						'data'	=>	array(
							'value'	=>	$schedule['instructors']
						)
					),
					array(
						'type'	=>	'hidden',
						'name'	=>	'section_title',
						'label'	=>	'Section Title',
						'data'	=>	array(
							'value'		=>	$schedule['section_title']
						)
					),
					array(
						'type'	=>	'submit',
						'name'	=>	'next',
						'data'	=>	array(
							'value'	=>	'Next'
						)
					)
				);
			break;
		}

		Forms::create_form( 'edit-schedule', ROOT . 'schedule.php?mode=edit&amp;schedule_id=' . $schedule_id, $form_data );
	}

	public function list_schedule()
	{
		global $Database, $User;

		$columns = $headers = $rows = array();

		$semester = ( $this->semester ) ? $this->semester : $this->default_semester;

		$institution_where = ( $this->institution ) ? "AND s.institution = '{$this->institution}'" : '';

		$columns[0] = array(
			'style'	=>	'width: 5%; border-right: 2px solid black;'
		);
		$headers[0] = array();

		if( !$institution_where )
		{
			$columns[1] = array(
				'style'	=>	'width: 7.5%;'
			);
			$headers[1] = array(
				'content'	=>	'Institution'
			);
		}

		$columns[2] = array(
			'style'	=>	'width: 10%;'
		);
		$headers[2] = array(
			'content'	=>	'Course'
		);

		$columns[3] = array(
			'style'	=>	'width: 5%;'
		);
		$headers[3] = array(
			'content'	=>	'Section'
		);

		$columns[4] = array();
		$headers[4] = array(
			'content'	=>	'Course Title'
		);

		$columns[5] = array(
			'style'	=>	'width: 10%;'
		);
		$headers[5] = array(
			'content'	=>	'Type'
		);

		$columns[6] = array(
			'style'	=>	'width: 15%;'
		);
		$headers[6] = array(
			'content'	=>	'Instructor(s)'
		);

		$columns[7] = array(
			'style'	=>	'width: 5%;'
		);
		$headers[7] = array(
			'content'	=>	'Days'
		);

		$columns[8] = array(
			'style'	=>	'width: 7.5%;'
		);
		$headers[8] = array(
			'content'	=>	'Start Time'
		);

		$columns[9] = array(
			'style'	=>	'width: 7.5%;'
		);
		$headers[9] = array(
			'content'	=>	'End Time'
		);

		$sql = "SELECT s.*, c.course_title
			FROM schedules s
			LEFT JOIN ( institutions i1 )
				ON ( s.institution = i1.institution )
			LEFT JOIN ( institutions i2, courses c )
				ON ( i2.institution = c.institution
					AND s.institution = c.institution
					AND s.subject = c.subject
					AND s.course = c.course )
			WHERE s.user_id = {$User->user_info['id']}
				AND s.semester = '$semester'
				$institution_where
			ORDER BY s.semester ASC, i1.name ASC, s.days DESC, s.start_time ASC, s.end_time ASC, s.subject ASC, s.course ASC, s.section ASC";

		$result = $Database->query( $sql );

		if( !$Database->has_result( $result ) )
		{
			$rows[] = array(
				'content'	=>	array(
					0	=>	array(
						'colspan'	=>	count( $headers ),
						'content'	=>	'<strong>No results returned.</strong>'
					)
				)
			);
		}

		while( $row = $Database->fetch_assoc( $result ) )
		{
			$bg_color = $text_color = 'inherit';

			if( $row['bg_color'] )
			{
				$bg_color = '#' . $row['bg_color'];
			}

			if( $row['text_color'] )
			{
				$text_color = '#' . $row['text_color'];
			}

			$edit_institution = ( $row['institution'] != $this->default_institution ) ? '&amp;institution=' . $row['institution'] : '';
			$edit_semester = ( $row['semester'] != $this->default_semester ) ? '&amp;semester=' . $row['semester'] : '';

			$title = ( $row['section_title'] ) ? $row['course_title'] . ":\n" . $row['section_title'] : $row['course_title'];

			$rows[] = array(
				'style'		=>	'background-color: ' . $bg_color . '; color: ' . $text_color . ';',
				'content'	=>	array(
					0	=>	array(
						'class'		=>	'edit',
						'content'	=>	'<a href="' . ROOT . 'schedule.php?mode=edit&amp;schedule_id=' . $row['schedule_id'] . $edit_institution . $edit_semester . '">edit</a>'
					),
					1	=>	( $institution_where ) ? array() : array(
						'content'	=>	$this->format_institution( $row['institution'] )
					),
					2	=>	array(
						'content'	=>	$this->format_course( $row['subject'], $row['course'], $row['institution'], '&nbsp;' )
					),
					3	=>	array(
						'content'	=>	$row['section']
					),
					4	=>	array(
						'content'	=>	$this->convert_newlines( htmlentities( $title, ENT_QUOTES, 'UTF-8' ) )
					),
					5	=>	array(
						'content'	=>	$this->format_schedule_type( $row['schedule_type'] )
					),
					6	=>	array(
						'content'	=>	$this->format_instructors( $row['instructors'], '<br />' )
					),
					7	=>	array(
						'content'	=>	$this->format_days( $row['days'], NULL, 'html' )
					),
					8	=>	array(
						'content'	=>	$this->format_time( $row['start_time'], TRUE, '24-hour', 'html' )
					),
					9	=>	array(
						'content'	=>	$this->format_time( $row['end_time'], TRUE, '24-hour', 'html' )
					)
				)
			);
		}

		$Database->free_result( $result );

		$this->print_list_table( ROOT . 'schedule.php', $columns, $headers, $rows );
	}
}

?>
Return current item: SASHA