Location: PHPKode > projects > PDF Annotation Engine > annotation/db/course.php
<?php

/**
 * 
 * Copyright (C) 2007  IVLOS
 * 
 * This file is part of PDF Annotation Engine.
 * 
 * PDF Annotation Engine is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * PDF Annotation Engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PDF Annotation Engine.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * PDF Annotation Engine was originaly made by Infi, The Netherlands.
 * 
 * If you have any questions or suggestions, mail us at hide@address.com
 * 
 **/

class course extends crud_db {
	// static methods
	
	function from_group_id($group_id) {
		$group = group::from_id($group_id);
		
		return course::from_id($group->course_id);
	}
	
	function check_owner() {
		return ($this->owner_id == user::id());
	}	
		
	function search($query, $filter=array()) {
		global $db;
		
		$query = str_replace('  ', ' ', trim($query));
		$values = explode(' ', $query);
		
		$key = 'name';
		
		$sql = "SELECT * FROM course";
		$data = array();
		
		if(count($values) > 0) {
			$first = true;
			
			foreach($values as $value) {
				if($first) {
					$sql .= " WHERE (`$key` LIKE ?";
					$first = false;
				} else {
					$sql .= " OR `$key` LIKE ?";
				}
				
				$sql .= " OR `$key` LIKE ?";
				$sql .= " OR `$key` LIKE ?";
				$sql .= " OR `$key` LIKE ?";
				
				$data[] = "$value %";
				$data[] = "%$value%";
				$data[] = "%$value";
				$data[] = "$value";
			}
			
			$sql .= ")";
		} else {
			$sql .= " WHERE 1";
		}
		
		if(count($filter) > 0) {
			foreach($filter as $key => $value) {
				$sql .= " AND `$key` = ?";
				$data[] = $value;
			}
		}
		
		$result = $db->getAll($sql, $data, DB_FETCHMODE_ASSOC);
		
		return crud_db::from_result($result, 'course');
	}
	
	// regular methods
	
	function delete() {
		$groups = $this->get_groups();
		
		foreach($groups as $group) {
			$group->delete();
		}
		
		return parent::delete(__CLASS__);
	}
	
	function get_groups() {
		return group::select(array('course_id' => $this->id));
	}

	function is_open()
	{
		return ($this->status == 'open');
	}
	
	function check_guest() {
		return ($this->guest_ok == 'true');
	}
	
	function check_user($user_id) {
		$groups = $this->get_groups();
		
		foreach($groups as $group) {
			if($group->contains($user_id)) {
				return true;
			}
		}
		
		return false;
	}
	
	// inherited static methods
	
	function from_row($row) {
		return parent::from_row($row, __CLASS__);
	}
	
	function from_values($values) {
		return parent::from_values($values, __CLASS__);
	}
	
	function array_from_id($id) {
		return parent::array_from_id($id, __CLASS__);
	}
	
	function from_id($id) {
		return parent::from_id($id, __CLASS__);
	}

	function select($filter=array()) {
		return parent::select($filter, __CLASS__);
	}
	
	function assoc_list($field, $filter=array()) {
		return parent::assoc_list($field, $filter, __CLASS__);
	}
	
	// inherited regular methods

	function update() {
		return parent::update(__CLASS__);
	}
	
	function insert() {
		return parent::insert(__CLASS__);
	}
}
Return current item: PDF Annotation Engine