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

/**
 * SASHA :: inc/lib/lib.assignments.php
 *
 * This contains all of the assignments functions.
 *
 * @package SASHA
 * @copyright (C) 2006-2010 Gordon P. Hemsley
 * @license docs/LICENSE BSD License
 * @version $Id: lib.assignments.php 87 2010-02-10 22:27:14Z gphemsley $
 */

/**
 * Assignments
 *
 * Child class for tasks related to Assignments
 *
 * @package SASHA
 */
class Assignments extends Base
{
	public function add_assignment()
	{
		global $Database, $User;

		$schedule_id = FALSE;

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

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

		if( $submit )
		{
			$schedule_id = (int) $_POST['schedule_id'];
			$assignment_name = ( exists( $_POST['assignment_name'] ) ) ? $_POST['assignment_name'] : FALSE;
			$description = ( exists( $_POST['description'] ) ) ? $_POST['description'] : NULL;
			$assigned_date = mktime( 0, 0, 0, (int) $_POST['assigned_date']['month'], (int) $_POST['assigned_date']['day'], (int) $_POST['assigned_date']['year'] );
			$due_date = mktime( 0, 0, 0, (int) $_POST['due_date']['month'], (int) $_POST['due_date']['day'], (int) $_POST['due_date']['year'] );

			if( !$schedule_id )
			{
				print_message( 'bad', 'Assignment must have a valid schedule attached to it. If there are none available, you must create one before adding an assignment.', 'Addition failed.' );
			}
			elseif( !$assignment_name )
			{
				print_message( 'bad', 'Assignment must have a valid, non-empty name.', 'Addition failed.' );
			}
			elseif( $due_date < $assigned_date )
			{
					print_message( 'bad', 'Due date cannot be before assigned date.', 'Addition failed.' );
			}
			else
			{
				$sql = "INSERT INTO assignments ( user_id, schedule_id, assignment_name, description, assigned_date, due_date )
					VALUES ( {$User->user_info['id']}, $schedule_id, '" . $Database->escape( $assignment_name ) . "', '" . $Database->escape( $description ) . "', $assigned_date, $due_date )";

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

				if( $result )
				{
					print_message( 'good', 'Assignment &quot;' . htmlentities( $assignment_name, ENT_QUOTES, 'UTF-8' ) . '&quot; (due ' . date( 'F j, Y', $due_date ) . ') added successfully.', 'Addition succeeded.' );
				}
			}
		}

		print "\t" . '<h2>Add Assignment</h2>' . "\n";

		$form_data = array(
			array(
				'type'	=>	'course',
				'name'	=>	'schedule_id',
				'label'	=>	'Course',
				'data'	=>	array( $schedule_id, $semester, $institution )
			),
			array(
				'type'	=>	'text',
				'name'	=>	'assignment_name',
				'label'	=>	'Assignment Name',
				'data'	=>	array(
					'size'		=>	75,
					'maxlength'	=>	255
				)
			),
			array(
				'type'	=>	'textarea',
				'name'	=>	'description',
				'label'	=>	'Description',
				'data'	=>	array(
					'rows'	=>	5,
					'cols'	=>	70
				)
			),
			array(
				'type'	=>	'date',
				'name'	=>	'assigned_date',
				'label'	=>	'Assigned Date',
				'data'	=>	array( FALSE )
			),
			array(
				'type'	=>	'date',
				'name'	=>	'due_date',
				'label'	=>	'Due Date',
				'data'	=>	array( FALSE )
			),
			array(
				'type'	=>	'submit',
				'name'	=>	'submit',
				'data'	=>	array(
					'value'		=>	'Submit'
				)
			)
		);

		Forms::create_form( 'add-assignment', ROOT . 'assignments.php?mode=add&amp;semester=' . $semester, $form_data );
	}

	public function edit_assignment( $assignment_id )
	{
		global $Database, $User;

		if( !$assignment_id )
		{
			print_message( 'bad', 'Please specify which assignment you want to edit.', 'Assignment ID not specified.' );

			return FALSE;
		}

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

		$submit = ( exists( $_POST['submit'] ) ) ? TRUE : FALSE;
		$schedule_id = FALSE;

		if( $submit )
		{
			$schedule_id = (int) $_POST['schedule_id'];
			$assignment_name = ( exists( $_POST['assignment_name'] ) ) ? $_POST['assignment_name'] : FALSE;
			$description = ( exists( $_POST['description'] ) ) ? $_POST['description'] : NULL;
			$assigned_date = mktime( 0, 0, 0, (int) $_POST['assigned_date']['month'], (int) $_POST['assigned_date']['day'], (int) $_POST['assigned_date']['year'] );
			$due_date = mktime( 0, 0, 0, (int) $_POST['due_date']['month'], (int) $_POST['due_date']['day'], (int) $_POST['due_date']['year'] );
			$status = (int) $_POST['status'];

			if( !$schedule_id )
			{
				print_message( 'bad', 'Assignment must have a valid schedule attached to it. If there are none available, you must create one before adding an assignment.', 'Update failed.' );
			}
			elseif( !$assignment_name )
			{
				print_message( 'bad', 'Assignment must have a valid, non-empty name.', 'Update failed.' );
			}
			elseif( $due_date < $assigned_date )
			{
					print_message( 'bad', 'Due date cannot be before assigned date.', 'Update failed.' );
			}
			else
			{
				$sql = "UPDATE assignments
					SET schedule_id = $schedule_id, assignment_name = '" . $Database->escape( $assignment_name ) . "', description = '" . $Database->escape( $description ) . "', assigned_date = $assigned_date, due_date = $due_date, status = $status
					WHERE assignment_id = $assignment_id";

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

				if( $result )
				{
					print_message( 'good', 'Assignment &quot;' . htmlentities( $assignment_name, ENT_QUOTES, 'UTF-8' ) . '&quot; (due ' . date( 'F j, Y', $due_date ) . ') updated successfully.', 'Update succeeded.' );

					$this->list_assignments();

					return;
				}
			}
		}

		$sql = "SELECT a.*, s.*, c.course_title
			FROM assignments a, schedules s
			LEFT JOIN ( institutions i, courses c )
				ON ( i.institution = c.institution
					AND s.institution = c.institution
					AND s.subject = c.subject
					AND s.course = c.course )
			WHERE a.schedule_id = s.schedule_id
				AND s.user_id = {$User->user_info['id']}
				AND s.semester = '$semester'
				AND a.assignment_id =  $assignment_id";

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

		print "\t" . '<h2>Edit Assignment</h2>' . "\n";

		$form_data = array(
			array(
				'type'	=>	'course',
				'name'	=>	'schedule_id',
				'label'	=>	'Course',
				'data'	=>	array( $assignment['schedule_id'], $assignment['semester'] )
			),
			array(
				'type'	=>	'text',
				'name'	=>	'assignment_name',
				'label'	=>	'Assignment Name',
				'data'	=>	array(
					'size'		=>	75,
					'maxlength'	=>	255,
					'value'		=>	$assignment['assignment_name']
				)
			),
			array(
				'type'	=>	'textarea',
				'name'	=>	'description',
				'label'	=>	'Description',
				'data'	=>	array(
					'rows'	=>	5,
					'cols'	=>	70,
					'value'	=>	$assignment['description']
				)
			),
			array(
				'type'	=>	'date',
				'name'	=>	'assigned_date',
				'label'	=>	'Assigned Date',
				'data'	=>	array( $assignment['assigned_date'] )
			),
			array(
				'type'	=>	'date',
				'name'	=>	'due_date',
				'label'	=>	'Due Date',
				'data'	=>	array( $assignment['due_date'] )
			),
			array(
				'type'	=>	'assignment_status',
				'name'	=>	'status',
				'label'	=>	'Status',
				'data'	=>	array( $assignment['status'] )
			),
			array(
				'type'	=>	'hidden',
				'name'	=>	'assignment_id',
				'data'	=>	array(
					'value'	=>	$assignment_id
				)
			),
			array(
				'type'	=>	'submit',
				'name'	=>	'submit',
				'data'	=>	array(
					'value'	=>	'Submit'
				)
			)
		);

		Forms::create_form( 'edit-assignment', ROOT . 'assignments.php?mode=edit&amp;semester=' . $semester . '&amp;assignment_id=' . $assignment_id, $form_data );
	}

	public function list_assignments()
	{
		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'	=>	'Assignment'
		);

		$columns[5] = array(
			'style'	=>	'width: 12.5%;'
		);
		$headers[5] = array(
			'content'	=>	'Assigned Date'
		);

		$columns[6] = array(
			'style'	=>	'width: 12.5%;'
		);
		$headers[6] = array(
			'content'	=>	'Due Date'
		);

		$columns[7] = array(
			'style'	=>	'width: 6.25%; border-left: 2px solid black;'
		);
		$headers[7] = array(
			'content'	=>	'Status'
		);

		$sql = "SELECT a.*, s.*, c.course_title
			FROM assignments a, schedules s
			LEFT JOIN ( institutions i, courses c )
				ON ( i.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 a.schedule_id = s.schedule_id
				AND s.semester = '$semester'
				$institution_where
			ORDER BY a.due_date DESC, a.assigned_date DESC, s.start_time ASC, s.end_time ASC, a.assignment_name ASC, a.assignment_id 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 = '';
			$border = '';
			$green = $row['status'];
			$red = 100 - $green;

			if( $row['bg_color'] )
			{
				$bg_color = 'background-color: #' . $row['bg_color'] . ';';
			}

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

			if( $row['due_date'] == mktime( 0, 0, 0 ) )
			{
				$border = 'border-top: 3px double black; border-bottom: 3px double black;';
			}

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

			$rows[] = array(
				'style'		=>	trim( implode( ' ', array( $bg_color, $text_color, $border ) ) ),
				'content'	=>	array(
					0	=>	array(
						'class'		=>	'edit',
						'content'	=>	'<a href="' . ROOT . 'assignments.php?mode=edit&amp;assignment_id=' . $row['assignment_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(
						'style'		=>	'text-align: left;',
						'content'	=>	'<strong>' . htmlentities( $row['assignment_name'], ENT_QUOTES, 'UTF-8' ) . '</strong><br />' . $this->convert_newlines( htmlentities( $row['description'], ENT_QUOTES, 'UTF-8' ) )
					),
					5	=>	array(
						'content'	=>	$this->format_date( $row['assigned_date'] )
					),
					6	=>	array(
						'content'	=>	$this->format_date( $row['due_date'] )
					),
					7	=>	array(
						'style'		=>	'background-color: rgb( ' . $red . '%, ' . $green . '%, 0% ); color: black;',
						'content'	=>	$this->format_assignment_status( $row['status'] )
					)
				)
			);
		}

		$Database->free_result( $result );

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

?>
Return current item: SASHA