<?php
/**
* SASHA :: inc/lib/lib.tests.php
*
* This contains all of the tests functions.
*
* @package SASHA
* @copyright (C) 2006-2010 Gordon P. Hemsley
* @license docs/LICENSE BSD License
* @version $Id: lib.tests.php 87 2010-02-10 22:27:14Z gphemsley $
*/
/**
* Tests
*
* Child class for tasks related to Tests
*
* @package SASHA
*/
class Tests extends Base
{
public function add_test()
{
global $Database, $User;
$schedule_id = $test_type = FALSE;
$institution = ( $this->institution ) ? $this->institution : FALSE;
$semester = ( $this->semester ) ? $this->semester : $this->default_semester;
$submit = ( exists( $_POST['submit'] ) ) ? TRUE : FALSE;
if( $submit )
{
$start_date_time = $this->format_time( $_POST['start_date'], FALSE, 'array', 'hour' );
$end_date_time = $this->format_time( $_POST['end_date'], FALSE, 'array', 'hour' );
$schedule_id = (int) $_POST['schedule_id'];
$test_type = (int) $_POST['test_type'];
$test_name = ( exists( $_POST['test_name'] ) ) ? $_POST['test_name'] : FALSE;
$description = ( exists( $_POST['description'] ) ) ? $_POST['description'] : NULL;
$start_date = mktime( (int) $start_date_time['hour'], (int) $start_date_time['minute'], 0, (int) $_POST['start_date']['month'], (int) $_POST['start_date']['day'], (int) $_POST['start_date']['year'] );
$end_date = mktime( (int) $end_date_time['hour'], (int) $end_date_time['minute'], 0, (int) $_POST['end_date']['month'], (int) $_POST['end_date']['day'], (int) $_POST['end_date']['year'] );
if( $end_date <= $start_date )
{
print_message( 'bad', 'End date must come after start date.', 'Addition failed.' );
}
elseif( $test_name )
{
$sql = "INSERT INTO tests ( user_id, schedule_id, test_type, test_name, description, start_date, end_date )
VALUES ( {$User->user_info['id']}, $schedule_id, $test_type, '" . $Database->escape( $test_name ) . "', '" . $Database->escape( $description ) . "', $start_date, $end_date )";
$result = $Database->query( $sql );
if( $result )
{
print_message( 'good', 'Test "' . htmlentities( $test_name, ENT_QUOTES, 'UTF-8' ) . '" (on ' . date( 'F j, Y', $start_date ) . ') added successfully.', 'Addition successful.' );
}
}
else
{
print_message( 'bad', 'Test must have a valid, non-empty name.', 'Addition failed.' );
}
}
print "\t" . '<h2>Add Test</h2>' . "\n";
$form_data = array(
array(
'type' => 'course',
'name' => 'schedule_id',
'label' => 'Course',
'data' => array( $schedule_id, $semester, $institution )
),
array(
'type' => 'test_type',
'name' => 'test_type',
'label' => 'Test Type',
'data' => array( $test_type )
),
array(
'type' => 'text',
'name' => 'test_name',
'label' => 'Test Name',
'data' => array(
'size' => 75,
'maxlength' => 255
)
),
array(
'type' => 'textarea',
'name' => 'description',
'label' => 'Description',
'data' => array(
'rows' => 5,
'cols' => 70
)
),
array(
'type' => 'date_time',
'name' => 'start_date',
'label' => 'Start Date',
'data' => array( FALSE )
),
array(
'type' => 'date_time',
'name' => 'end_date',
'label' => 'End Date',
'data' => array( FALSE )
),
array(
'type' => 'submit',
'name' => 'submit',
'data' => array(
'value' => 'Submit'
)
)
);
Forms::create_form( 'add-test', ROOT . 'tests.php?mode=add', $form_data );
}
public function edit_test( $test_id )
{
global $Database, $User;
if( !$test_id )
{
print_message( 'bad', 'Please specify which test you want to edit.', 'Test ID not specified.' );
return FALSE;
}
$institution = ( $this->institution ) ? $this->institution : FALSE;
$semester = ( $this->semester ) ? $this->semester : $this->default_semester;
$submit = ( exists( $_POST['submit'] ) ) ? TRUE : FALSE;
if( $submit )
{
$start_date_time = $this->format_time( $_POST['start_date'], FALSE, 'array', 'hour' );
$end_date_time = $this->format_time( $_POST['end_date'], FALSE, 'array', 'hour' );
$schedule_id = (int) $_POST['schedule_id'];
$test_type = (int) $_POST['test_type'];
$test_name = ( exists( $_POST['test_name'] ) ) ? $_POST['test_name'] : FALSE;
$description = ( exists( $_POST['description'] ) ) ? $_POST['description'] : NULL;
$start_date = mktime( (int) $start_date_time['hour'], (int) $start_date_time['minute'], 0, (int) $_POST['start_date']['month'], (int) $_POST['start_date']['day'], (int) $_POST['start_date']['year'] );
$end_date = mktime( (int) $end_date_time['hour'], (int) $end_date_time['minute'], 0, (int) $_POST['end_date']['month'], (int) $_POST['end_date']['day'], (int) $_POST['end_date']['year'] );
$received_score = ( exists( $_POST['received_score'] ) ) ? (float) $_POST['received_score'] : NULL;
$possible_score = ( exists( $_POST['possible_score'] ) ) ? (int) $_POST['possible_score'] : NULL;
$score = ( $possible_score ) ? ( ( ( $received_score ) ? ', received_score = ' . $received_score : '' ) . ', possible_score = ' . $possible_score ) : '';
if( $end_date <= $start_date )
{
print_message( 'bad', 'End date must come after start date.', 'Update failed.' );
}
elseif( $test_name )
{
$sql = "UPDATE tests
SET schedule_id = $schedule_id, test_type = $test_type, test_name = '" . $Database->escape( $test_name ) . "', description = '" . $Database->escape( $description ) . "', start_date = $start_date, end_date = $end_date
$score
WHERE test_id = $test_id";
$result = $Database->query( $sql );
if( $result )
{
print_message( 'good', 'Test "' . htmlentities( $test_name, ENT_QUOTES, 'UTF-8' ) . '" (on ' . date( 'F j, Y', $start_date ) . ') updated successfully.', 'Update successful.' );
$this->list_tests();
return;
}
}
else
{
print_message( 'bad', 'Test must have a valid, non-empty name.', 'Update failed.' );
}
}
$sql = "SELECT t.*, s.*, c.course_title
FROM tests t, 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 t.schedule_id = s.schedule_id
AND s.user_id = {$User->user_info['id']}
AND s.semester = '$semester'
AND t.test_id = $test_id";
$result = $Database->query( $sql );
$test = $Database->fetch_assoc( $result );
$Database->free_result( $result );
print "\t" . '<h2>Edit Test</h2>' . "\n";
$form_data = array(
array(
'type' => 'course',
'name' => 'schedule_id',
'label' => 'Course',
'data' => array( (int) $test['schedule_id'], $test['semester'] )
),
array(
'type' => 'test_type',
'name' => 'test_type',
'label' => 'Test Type',
'data' => array( (int) $test['test_type'] )
),
array(
'type' => 'text',
'name' => 'test_name',
'label' => 'Test Name',
'data' => array(
'size' => 75,
'maxlength' => 255,
'value' => $test['test_name']
)
),
array(
'type' => 'textarea',
'name' => 'description',
'label' => 'Description',
'data' => array(
'rows' => 5,
'cols' => 70,
'value' => $test['description']
)
),
array(
'type' => 'date_time',
'name' => 'start_date',
'label' => 'Start Date',
'data' => array( (int) $test['start_date'] )
),
array(
'type' => 'date_time',
'name' => 'end_date',
'label' => 'End Date',
'data' => array( (int) $test['end_date'] )
),
array(
'type' => 'text',
'name' => 'received_score',
'label' => 'Received Score',
'data' => array(
'size' => 5,
'maxlength' => 255,
'value' => (float) $test['received_score']
)
),
array(
'type' => 'text',
'name' => 'possible_score',
'label' => 'Possible Score',
'data' => array(
'size' => 3,
'maxlength' => 4,
'value' => (int) $test['possible_score']
)
),
array(
'type' => 'hidden',
'name' => 'institution',
'data' => array(
'value' => $institution
)
),
array(
'type' => 'hidden',
'name' => 'semester',
'data' => array(
'value' => $semester
)
),
array(
'type' => 'hidden',
'name' => 'test_id',
'data' => array(
'value' => $test_id
)
),
array(
'type' => 'submit',
'name' => 'submit',
'data' => array(
'value' => 'Submit'
)
)
);
Forms::create_form( 'edit-test', ROOT . 'tests.php?mode=edit&test_id=' . $test_id, $form_data );
}
public function list_tests()
{
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' => 'Test'
);
$columns[5] = array(
'style' => 'width: 5%;'
);
$headers[5] = array(
'content' => 'Type'
);
$columns[6] = array(
'style' => 'width: 12.5%;'
);
$headers[6] = array(
'content' => 'Start Time'
);
$columns[7] = array(
'style' => 'width: 12.5%;'
);
$headers[7] = array(
'content' => 'End Time'
);
$columns[8] = array(
'style' => 'width: 5%; border-left: 2px solid black;'
);
$headers[8] = array(
'content' => 'Score'
);
$sql = "SELECT t.*, s.*, c.course_title
FROM tests t, 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 t.schedule_id = s.schedule_id
AND s.user_id = {$User->user_info['id']}
AND s.semester = '$semester'
$institution_where
ORDER BY t.start_date DESC, t.end_date ASC, s.start_time ASC, s.end_time ASC, t.test_name ASC, t.test_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['possible_score'] ) ? round( ( $row['received_score'] / $row['possible_score'] ) * 100, 3 ) : 0;
$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['start_date'] >= mktime( 0, 0, 0 ) ) && ( $row['start_date'] <= mktime( 24, 0, 0 ) ) )
{
$border = 'border-top: 3px double black; border-bottom: 3px double black;';
}
$edit_institution = ( $row['institution'] != $this->default_institution ) ? '&institution=' . $row['institution'] : '';
$edit_semester = ( $row['semester'] != $this->default_semester ) ? '&semester=' . $row['semester'] : '';
$rows[] = array(
'style' => trim( implode( ' ', array( $bg_color, $text_color, $border ) ) ),
'content' => array(
0 => array(
'class' => 'edit',
'content' => '<a href="' . ROOT . 'tests.php?mode=edit&test_id=' . $row['test_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'], ' ' )
),
3 => array(
'content' => $row['section']
),
4 => array(
'style' => 'text-align: left;',
'content' => '<strong>' . htmlentities( $row['test_name'], ENT_QUOTES, 'UTF-8' ) . '</strong><br />' . $this->convert_newlines( htmlentities( $row['description'], ENT_QUOTES, 'UTF-8' ) )
),
5 => array(
'content' => $this->format_test_type( $row['test_type'] )
),
6 => array(
'content' => $this->format_date( $row['start_date'], 'F j, Y\<\b\r\ \/\>g:i A' )
),
7 => array(
'content' => $this->format_date( $row['end_date'], 'F j, Y\<\b\r\ \/\>g:i A' )
),
8 => array(
'style' => 'background-color: rgb( ' . $red . '%, ' . $green . '%, 0% ); color: black;',
'content' => $green . '%'
)
)
);
}
$Database->free_result( $result );
$this->print_list_table( ROOT . 'tests.php', $columns, $headers, $rows );
}
}
?>