<commit_before><?php
require_once("constants.php");

/**
 * Class for accessing Student data
 * @author Andrew
 */
class Student
{	
	/**
	 * Creates a Student
	 * @param $sid The student's ID
	 * @param $name The student's name
	 * @param $grade The student's grade
	 * @return A new student object on success, false otherwise
	 */
	public static function create($sid, $name, $grade)
	{
		global $mysql;
		if ( Student::getByProperty(PROPERTY_SID, $sid) )
			return false;
		$sid = real_escape_string($sid);
		$name = real_escape_string($name);
		if ( ($grade = intval($grade)) == 0 )
			return false;
		$result = $mysql->query("INSERT INTO students (sid, name, grade) VALUES('".$sid."', '".$name."', ".$grade.")");
		if ( !$result )
			return false;
		return new Student($sid);
	}
	
	/**
	 * Removes a student, preserving tickets and history
	 * @param $sid The student's ID
	 * @return true on success, false on failure
	 */
	public static function remove($sid)
	{
		global $mysql;
		$sid = real_escape_string($sid);
		return $mysql->query("DELETE FROM `students` WHERE `sid` = '".$sid."'");
	}
	
	/**
	 * Removes a student and all associated data (history and tickets)
	 * @param $sid The student's ID
	 * @return true on success, false on failure
	 */
	public static function nuke($sid)
	{
		global $mysql;
		$sid = real_escape_string($sid);
		
		$studentTicketRemoval = $mysql->query("DELETE FROM `tickets` WHERE `student` = '".$sid."'");
		$helperTicketRemoval = $mysql->query("DELETE FROM `tickets` WHERE `helper` = '".$sid."'");
		$historyRemoval = $mysql->query("DELETE FROM `history` WHERE `student` = '".$sid."'");
		$studentRemoval = $mysql->query("DELETE FROM `students` WHERE `sid` = '".$sid."'");
		
		return $studentTicketRemoval && $helperTicketRemoval && $historyRemoval && $studentRemoval;
	}
	
	/**
	 * Search for Student objects where $query is in the value of $property. It does not look for an exact match.
	 * @param $property The property to search in
	 * @param $query The query string to look for in $property
	 * @param $dupCheck Do not return any matching items already in this array. Useful when searching multiple properties. Defaults to an empty array.
	 * @return The matched Student objects that do not already exist in the $dupCheck array
	 */
	public static function searchField($property, $query, $dupCheck = array())
	{
		global $mysql;
		$query = real_escape_string($query);
		$result = $mysql->query("SELECT * FROM students WHERE ".$property." LIKE '%".$query."%'");
		if ( !$result || mysqli_num_rows($result) == 0 )
			return array();
		$output = array();
		
		while ( $d = mysqli_fetch_array($result) )
		{
			if ( !empty($d) )
			{
				$dup = false;
				foreach ( $dupCheck as $row )
				{
					if ( $row['sid'] == $d['sid'] )
						$dup = true;
				}
				if ( !$dup )
					$output[] = new Student($d['sid']);
			}
		}
		return $output;
	}
	
	/**
	 * Wraper for searchField() that searches by id and name.
	 * @param $query The query to search for
	 * @return An array of the Student objects that match $query
	 */
	public static function search($query)
	{
		$output = array();
		$output = array_merge(Student::searchField(PROPERTY_ID, $query, $output), $output);<commit_msg>Fix student search for searching by student ID (Issue #5)<commit_after><?php
require_once("constants.php");

/**
 * Class for accessing Student data
 * @author Andrew
 */
class Student
{	
	/**
	 * Creates a Student
	 * @param $sid The student's ID
	 * @param $name The student's name
	 * @param $grade The student's grade
	 * @return A new student object on success, false otherwise
	 */
	public static function create($sid, $name, $grade)
	{
		global $mysql;
		if ( Student::getByProperty(PROPERTY_SID, $sid) )
			return false;
		$sid = real_escape_string($sid);
		$name = real_escape_string($name);
		if ( ($grade = intval($grade)) == 0 )
			return false;
		$result = $mysql->query("INSERT INTO students (sid, name, grade) VALUES('".$sid."', '".$name."', ".$grade.")");
		if ( !$result )
			return false;
		return new Student($sid);
	}
	
	/**
	 * Removes a student, preserving tickets and history
	 * @param $sid The student's ID
	 * @return true on success, false on failure
	 */
	public static function remove($sid)
	{
		global $mysql;
		$sid = real_escape_string($sid);
		return $mysql->query("DELETE FROM `students` WHERE `sid` = '".$sid."'");
	}
	
	/**
	 * Removes a student and all associated data (history and tickets)
	 * @param $sid The student's ID
	 * @return true on success, false on failure
	 */
	public static function nuke($sid)
	{
		global $mysql;
		$sid = real_escape_string($sid);
		
		$studentTicketRemoval = $mysql->query("DELETE FROM `tickets` WHERE `student` = '".$sid."'");
		$helperTicketRemoval = $mysql->query("DELETE FROM `tickets` WHERE `helper` = '".$sid."'");
		$historyRemoval = $mysql->query("DELETE FROM `history` WHERE `student` = '".$sid."'");
		$studentRemoval = $mysql->query("DELETE FROM `students` WHERE `sid` = '".$sid."'");
		
		return $studentTicketRemoval && $helperTicketRemoval && $historyRemoval && $studentRemoval;
	}
	
	/**
	 * Search for Student objects where $query is in the value of $property. It does not look for an exact match.
	 * @param $property The property to search in
	 * @param $query The query string to look for in $property
	 * @param $dupCheck Do not return any matching items already in this array. Useful when searching multiple properties. Defaults to an empty array.
	 * @return The matched Student objects that do not already exist in the $dupCheck array
	 */
	public static function searchField($property, $query, $dupCheck = array())
	{
		global $mysql;
		$query = real_escape_string($query);
		$result = $mysql->query("SELECT * FROM students WHERE ".$property." LIKE '%".$query."%'");
		if ( !$result || mysqli_num_rows($result) == 0 )
			return array();
		$output = array();
		
		while ( $d = mysqli_fetch_array($result) )
		{
			if ( !empty($d) )
			{
				$dup = false;
				foreach ( $dupCheck as $row )
				{
					if ( $row->getID() == $d['sid'] )
						$dup = true;
				}
				if ( !$dup )
					$output[] = new Student($d['sid']);
			}
		}
		return $output;
	}
	
	/**
	 * Wraper for searchField() that searches by id and name.
	 * @param $query The query to search for
	 * @return An array of the Student objects that match $query
	 */
	public static function search($query)
	{
		$output = array();
		$output = array_merge(Student::searchField(PROPERTY_SID, $query, $output), $output);
{'fraction_non_alphanumeric': 0.15312259059367772, 'fraction_numerical': 0.0007710100231303007, 'mean_word_length': 3.7034082668600434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '453268', 'n_tokens_mistral': 2250, 'n_tokens_neox': 1998, 'n_words': 678}