Skip to content
Snippets Groups Projects
Commit 18f1e3f4 authored by Brett Bieber's avatar Brett Bieber
Browse files

Remove validation library, no course data is included in the database

parent a1c3314f
No related branches found
No related tags found
No related merge requests found
...@@ -64,12 +64,7 @@ class UNL_Catalog_Search ...@@ -64,12 +64,7 @@ class UNL_Catalog_Search
public function listings($q) public function listings($q)
{ {
include_once 'UNL/Catalog/Validate.php';
if ($r = UNL_Catalog_Validate::subjectAndNumber($q)) {
$where = "subject_id LIKE '%{$r[0]['subject_id']}%' AND number LIKE '%{$r[0]['number']}%'";
} else {
$where = "subject_id LIKE '%$q%' OR number LIKE '%$q%' OR title LIKE '%$q%'"; $where = "subject_id LIKE '%$q%' OR number LIKE '%$q%' OR title LIKE '%$q%'";
}
$listings = $this->catalog->factory('course_listings'); $listings = $this->catalog->factory('course_listings');
$courses = $this->catalog->factory('courses'); $courses = $this->catalog->factory('courses');
$listings->joinAdd($courses); $listings->joinAdd($courses);
......
<?php
/**
* This file contains everything necessary for validating sections of the catalog.
*
*/
/**
* This class contains methods for validating specific pieces of information for
* the catalog. Use the methods below to check strings for validity when inputting
* data.
*/
class UNL_Catalog_Validate
{
/**
* Determines if the subject and number are in a recognizable format.
*
* @param string $text eg 'AHIS *854Y'
*
* @return array subject_id, number, gradonly
*/
public static function subjectAndNumber($text)
{
$text = strtoupper($text);
if (preg_match('/^([A-Z]{3,4})\s((\*)?(\d{3,4}[a-zA-Z]?)\/?)+$/',$text,$matches)) {
$subject_id = $matches[1];
$text = trim(str_replace($subject_id, '', $text));
$numbers = explode('/', $text);
$data = array();
foreach ($numbers as $number) {
$course['subject_id'] = $subject_id;
if (preg_match('/(\*)?(\d{3,4}[a-zA-Z]?)/', $number, $matches)) {
$course['number'] = $matches[2];
if ($matches[1]=='*') {
$course['gradonly'] = true;
} else {
$course['gradonly'] = false;
}
} else {
throw new Exception('Malformed course number.. should never reach this.');
}
$data[] = $course;
}
return $data;
} else {
return false;
}
}
/**
* Checks if a textual list of crosslistings is formatted correctly.
*
* <code>
* $res = UNL_Catalog_Validate::crosslistings('(ARCH 558/858)');
* </code>
*
* @param unknown_type $text
* @return unknown
*/
public static function crosslistings($text)
{
$text = trim($text, ' ()');
if (preg_match('/([A-Z]{3,4})\s([*]?[\d]{3,4}[A-Z]?)/', $text, $matches)) {
$xlist = array();
// Appears to be a correctly formatted string of crosslistings.
$crosslistings = explode(';', $text);
foreach ($crosslistings as $crosslist) {
$crosslist = trim(str_replace(', ',',',$crosslist));
$crosslist = explode(',',$crosslist);
$crosslist = array_reverse($crosslist);
$course_num = '';
foreach ($crosslist as $subject_and_course) {
if ($subject_and_course = UNL_Catalog_Validate::subjectAndNumber($subject_and_course)) {
$xlist = array_merge($xlist, $subject_and_course);
}
}
}
return $xlist;
} else {
return false;
}
}
}
?>
\ No newline at end of file
<?php
// Call UNL_Catalog_ValidateTest::main() if this source file is executed directly.
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'UNL_Catalog_ValidateTest::main');
}
require_once 'PHPUnit/Framework.php';
require_once 'Catalog/Validate.php';
/**
* Test class for UNL_Catalog_Validate.
* Generated by PHPUnit on 2007-09-12 at 10:42:22.
*/
class UNL_Catalog_ValidateTest extends PHPUnit_Framework_TestCase {
/**
* Runs the test methods of this class.
*
* @access public
* @static
*/
public static function main() {
require_once 'PHPUnit/TextUI/TestRunner.php';
$suite = new PHPUnit_Framework_TestSuite('UNL_Catalog_ValidateTest');
$result = PHPUnit_TextUI_TestRunner::run($suite);
}
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*
* @access protected
*/
protected function setUp() {
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*
* @access protected
*/
protected function tearDown() {
}
/**
* @todo Implement testSubjectAndNumber().
*/
public function testSubjectAndNumber() {
$r = UNL_Catalog_Validate::subjectAndNumber('CSCE 855');
$this->assertEquals('CSCE', $r[0]['subject_id']);
$this->assertEquals('855', $r[0]['number']);
$r = UNL_Catalog_Validate::subjectAndNumber('csce 855');
$this->assertEquals('CSCE', $r[0]['subject_id']);
$this->assertEquals('855', $r[0]['number']);
$r = UNL_Catalog_Validate::subjectAndNumber('CSCE *855');
$this->assertEquals('CSCE', $r[0]['subject_id']);
$this->assertEquals('855', $r[0]['number']);
$r = UNL_Catalog_Validate::subjectAndNumber('CSCE *855A');
$this->assertEquals('CSCE', $r[0]['subject_id']);
$this->assertEquals('855A', $r[0]['number']);
$r = UNL_Catalog_Validate::subjectAndNumber('LAW 1000');
$this->assertEquals('LAW', $r[0]['subject_id']);
$this->assertEquals('1000', $r[0]['number']);
$r = UNL_Catalog_Validate::subjectAndNumber('LAW *1000A');
$this->assertEquals('LAW', $r[0]['subject_id']);
$this->assertEquals('1000A', $r[0]['number']);
$this->assertEquals(true, $r[0]['gradonly']);
$r = UNL_Catalog_Validate::subjectAndNumber('CSCE');
$this->assertEquals(false, $r);
}
public function testSubjectAndNumber_MultipleNumbers() {
$r = UNL_Catalog_Validate::subjectAndNumber('ARCH 699/*899');
$this->assertEquals('ARCH', $r[0]['subject_id']);
$this->assertEquals('699', $r[0]['number']);
$this->assertFalse($r[0]['gradonly']);
$this->assertEquals('ARCH', $r[1]['subject_id']);
$this->assertEquals('899', $r[1]['number']);
$this->assertTrue($r[1]['gradonly']);
}
public function testCrosslistings()
{
$this->assertFalse(UNL_Catalog_Validate::crosslistings('LAW'));
$r = UNL_Catalog_Validate::crosslistings('(LAW *1000A)');
$this->assertEquals(array(array(
'subject_id'=> 'LAW',
'number' => '1000A',
'gradonly' => true)),$r);
$r = UNL_Catalog_Validate::crosslistings('(BIOS *817; NRES *807)');
$this->assertEquals(array(array(
'subject_id'=> 'BIOS',
'number' => '817',
'gradonly' => true),
array(
'subject_id'=> 'NRES',
'number' => '807',
'gradonly' => true)),$r);
$r = UNL_Catalog_Validate::crosslistings('(BIOC 834; BIOS *834; CHEM 834)');
$this->assertEquals(array(array(
'subject_id'=> 'BIOC',
'number' => '834',
'gradonly' => false),
array(
'subject_id'=> 'BIOS',
'number' => '834',
'gradonly' => true),
array(
'subject_id'=> 'CHEM',
'number' => '834',
'gradonly' => false)),$r);
}
public function testCrosslistings2()
{
$r = UNL_Catalog_Validate::crosslistings('(ARCH 558/858)');
$this->assertEquals(array(array(
'subject_id'=> 'ARCH',
'number' => '558',
'gradonly' => false),
array(
'subject_id'=> 'ARCH',
'number' => '858',
'gradonly' => false)),$r);
}
}
// Call UNL_Catalog_ValidateTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == 'UNL_Catalog_ValidateTest::main') {
UNL_Catalog_ValidateTest::main();
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment