<commit_before><?php
/**
 * @copyright 2009 City of Bloomington, Indiana
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
 * @author <NAME> <<EMAIL>>
 */
class Term extends ActiveRecord
{
	private $id;
	private $seat_id;
	private $person_id;
	private $term_start;
	private $term_end;

	private $seat;
	private $person;

	/**
	 * This will load all fields in the table as properties of this class.
	 * You may want to replace this with, or add your own extra, custom loading
	 *
	 * @param int $id
	 */
	public function __construct($id=null)
	{
		if ($id) {
			$PDO = Database::getConnection();
			$query = $PDO->prepare('select * from terms where id=?');
			$query->execute(array($id));

			$result = $query->fetchAll(PDO::FETCH_ASSOC);
			if (!count($result)) {
				throw new Exception('terms/unknownTerm');
			}
			foreach ($result[0] as $field=>$value) {
				if ($value) {
					switch ($field) {
						case 'term_start':
						case 'term_end':
							if ($value && $value!='0000-00-00') {
								$this->$field = strtotime($value);
							}
							break;

						default:
							$this->$field = $value;
					}
				}
			}
		}
		else {
			// This is where the code goes to generate a new, empty instance.
			// Set any default values for properties that need it here
			$this->term_start = time();
		}
	}

	/**
	 * Throws an exception if anything's wrong
	 * @throws Exception $e
	 */
	public function validate()
	{
		// Check for required fields here.  Throw an exception if anything is missing.
		if (!$this->seat_id || !$this->person_id) {
			throw new Exception('missingRequiredFields');
		}

		if (!$this->term_start) {
			$this->term_start = time();
		}

		// Make sure the end date falls after the start date
		if ($this->term_end && $this->term_end < $this->term_start) {
			throw new Exception('terms/invalidEndDate');
		}

		// Make sure this term does not exceed the maxCurrentTerms for the seat
		if ( $this->term_start <= time() && (!$this->term_end || $this->term_end >= time()) ) {
			// The term we're adding is current, make sure there's room
			$count = count($this->getSeat()->getCurrentTerms());
			if (!$this->id) {
				$count++;
			}
			if ($count > $this->getSeat()->getMaxCurrentTerms()) {
				throw new Exception('seats/maxCurrentTermsFilled');
			}
		}

		// Make sure this person is not serving overlapping terms for the same committee
		$pdo = Database::getConnection();
		$sql = "select id from terms
				where (term_start<=? and ?>=term_end)
				or (?<=term_end and ?>=term_end)";
		$query = $pdo->prepare($sql);
		$query->execute(array($this->term_start,$this->term_end,$this->term_start,$this->term_end));
		$result = $query->fetchAll(PDO::FETCH_ASSOC);<commit_msg>Terms now make sure that the person is not serving two terms at the same time for the same committee<commit_after><?php
/**
 * @copyright 2009 City of Bloomington, Indiana
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
 * @author <NAME> <<EMAIL>>
 */
class Term extends ActiveRecord
{
	private $id;
	private $seat_id;
	private $person_id;
	private $term_start;
	private $term_end;

	private $seat;
	private $person;

	/**
	 * This will load all fields in the table as properties of this class.
	 * You may want to replace this with, or add your own extra, custom loading
	 *
	 * @param int $id
	 */
	public function __construct($id=null)
	{
		if ($id) {
			$PDO = Database::getConnection();
			$query = $PDO->prepare('select * from terms where id=?');
			$query->execute(array($id));

			$result = $query->fetchAll(PDO::FETCH_ASSOC);
			if (!count($result)) {
				throw new Exception('terms/unknownTerm');
			}
			foreach ($result[0] as $field=>$value) {
				if ($value) {
					switch ($field) {
						case 'term_start':
						case 'term_end':
							if ($value && $value!='0000-00-00') {
								$this->$field = strtotime($value);
							}
							break;

						default:
							$this->$field = $value;
					}
				}
			}
		}
		else {
			// This is where the code goes to generate a new, empty instance.
			// Set any default values for properties that need it here
			$this->term_start = time();
		}
	}

	/**
	 * Throws an exception if anything's wrong
	 * @throws Exception $e
	 */
	public function validate()
	{
		// Check for required fields here.  Throw an exception if anything is missing.
		if (!$this->seat_id || !$this->person_id) {
			throw new Exception('missingRequiredFields');
		}

		if (!$this->term_start) {
			$this->term_start = time();
		}

		// Make sure the end date falls after the start date
		if ($this->term_end && $this->term_end < $this->term_start) {
			throw new Exception('terms/invalidEndDate');
		}

		// Make sure this term does not exceed the maxCurrentTerms for the seat
		if ( $this->term_start <= time() && (!$this->term_end || $this->term_end >= time()) ) {
			// The term we're adding is current, make sure there's room
			$count = count($this->getSeat()->getCurrentTerms());
			if (!$this->id) {
				$count++;
			}
			if ($count > $this->getSeat()->getMaxCurrentTerms()) {
				throw new Exception('seats/maxCurrentTermsFilled');
			}
		}

		// Make sure this person is not serving overlapping terms for the same committee
		$pdo = Database::getConnection();
		$sql = "select id from terms
				where id!=?
				and ((term_start<=? and ?>=term_end)
					or (?<=term_end and ?>=term_end))";
		$query = $pdo->prepare($sql);
		$query->execute(array($this->id,
							  $this->term_start,$this->term_end,
							  $this->term_start,$this->term_end));
		$result = $query->fetchAll(PDO::FETCH_ASSOC);
{'fraction_non_alphanumeric': 0.1557906059519541, 'fraction_numerical': 0.004661168877733955, 'mean_word_length': 3.252286585365854, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6110126', 'n_tokens_mistral': 2149, 'n_tokens_neox': 1796, 'n_words': 528}