<?php

/*
 *	  CLASS:  ATAChapterList
 *    
*/

class ATAChapterList {
	private $list = array();
	
	function __construct() {
		$this->init();
	}
	
	private function init() {
		try {
			$list_cache = cache_get(QMS_ATACHAPTER_LIST);
			if ( isset($list_cache->data) && ($list_cache->data <> '') ) {
				$this->list = $list_cache->data;
			}
			else {
				$sql = "SELECT chapter_id, chapter, code FROM {qms_ata_chapters} ORDER BY chapter";	
				$results = db_query($sql);
		
				$this->list[0] = '- Select -';  // add default option

				foreach($results as $row) {
					$this->list[$row->chapter_id] = sprintf("%d - %s", $row->code, $row->chapter);
				}
			
				// cache the list values for the next time
				cache_set(QMS_ATACHAPTER_LIST, $this->list, 'cache', time() + QMS_CACHE_TIMEOUT);
			}
		}
		catch (Exception $e) {
			watchdog('sabreQMS', 'ATAChapterList::init() ' . $e->getMessage(), array(), WATCHDOG_ERROR);
		} 
	}
	
	public function get() {
		return $this->list;
	}
	
	public function getName($id) {
		if ( $id && isset($this->list[$id])  ) {
			return $this->list[$id];
		}
		return '';
	}
	
	public function reload() {
		_clear_qms_cache(QMS_ATACHAPTER_LIST); 
		_clear_qms_cache(QMS_DISCREPANCY_SEARCH_FORM); 
		
		unset($this->list);
		$this->init();
	}
	
} // end - class ATAChapterList

