<commit_before><?php
/**
 * Created by PhpStorm.
 * User: <NAME>
 * Date: 11.09.14
 * Time: 11:58
 */

namespace Model;

class UserModel extends Model {

	public function __construct() {
		parent::__construct();
	}

	/**
	 * Get one distinct record of a table by its id
	 * @param $id
	 * @return mixed
	 */
	public function getOne($id) {
		$db = $this->getDb();
		$sql = 'SELECT id, name, email, role , created, modified FROM user WHERE id = {id};';
		$fields = array(
			'id'=> $id
		);
		$sql = prepare($sql, $fields);
		$result = $db->query($sql);
		if (isset($result[0])) {
			$result[0]['roleName'] = $this->getRoleName($result[0]['role']);
			return $result[0];
		} else {
			return null;
		}
	}

	/**
	 * gets all records of a table
	 * @return mixed
	 */
	public function getAll() {
		$db = $this->getDb();
		$sql =
		$sql = 'SELECT
					id,
					name,
					email,
					role
				FROM user;';
		$result = $db->query($sql);
		foreach ($result as $row){
			$row['roleName'] = $this->getRoleName($row['role']);
			$newResult[] = $row;
		}

		return $newResult;
	}
	protected function getRoleName($role){
		switch ($role){
			case 1:
				return 'Administrator';
			case 2:
				return 'Author';
			case 3:
				return 'Guest';
		}
	}
	public function getOneByEmail($email){
		$db = $this->getDb();
		$sql = 'SELECT id, name, email FROM user WHERE email = {email};';
		$fields = array(
			'email'=> $email
		);
		$sql = prepare($sql, $fields);
		$result = $db->query($sql);
		return $result[0];
	}

	public function create($fields){
		$db = $this->getDb();
		$fields['password'] = password_hash($fields['password'],PASSWORD_DEFAULT);
		$sql = 'INSERT INTO user (name, email, role, password)
				VALUES ({name}, {email}, {role}, {password});';
		$sql = prepare($sql, $fields, true);
		return $db->exec($sql);
	}

	public function update($fields) {
		$db = $this->getDb();
				$sql = 'UPDATE user SET
				name = {name},
				email = {email},
				role = {role}
				password = {password}
				WHERE id = {id};';
		$fields['password'] = password_hash($fields['password'],PASSWORD_DEFAULT);
		$sql = prepare($sql,$fields);
		return $db->exec($sql);
	}

	public function delete($id) {
		//
		$db = $this->getDb();
		$sql = 'DELETE FROM user WHERE id = {id};';
		$fields = array(
			'id' => $id
		);
		$sql = prepare($sql,$fields);
		return $db->exec($sql);
	}

	/**
	 * Checks if the password is equal to the saved password hash
	 *
	 * @param $id
	 * @param $password
	 * @return bool Returns true if the password equals the hash
	 */
	public function checkPassword($id, $password){
		$db = $this->getDb();
		$sql = 'SELECT password FROM user WHERE id = {id};';
		$fields = array(
			'id' => $id
		);
		$sql = prepare($sql, $fields);
		$result = $db->query($sql);
		return password_verify($password,$result);
	}
	public function setPassword($id,$password){
		$db = $this->getDb();
		$password = password_hash($password,PASSWORD_DEFAULT);
		$sql = $sql = 'UPDATE user SET
				password = {password}
				WHERE id = {id};';
		$fields = array(
			'id' => $id,
			'password' => <PASSWORD>
		);
		$sql = prepare($sql,$fields);
		return $db->exec($sql);
	}
	public function validate($data){<commit_msg>Implemented modified and created fields implemented hashing end comparing of passwords<commit_after><?php
/**
 * Created by PhpStorm.
 * User: <NAME>
 * Date: 11.09.14
 * Time: 11:58
 */

namespace Model;

class UserModel extends Model {

	public function __construct() {
		parent::__construct();
	}

	/**
	 * Get one distinct record of a table by its id
	 * @param $id
	 * @return mixed
	 */
	public function getOne($id) {
		$db = $this->getDb();
		$sql = 'SELECT id, name, email, role , created, modified FROM user WHERE id = {id};';
		$fields = array(
			'id'=> $id
		);
		$sql = $db->prepare($sql, $fields);
		$result = $db->query($sql);
		if (isset($result[0])) {
			$result[0]['roleName'] = $this->getRoleName($result[0]['role']);
			return $result[0];
		} else {
			return null;
		}
	}

	/**
	 * gets all records of a table
	 * @return mixed
	 */
	public function getAll() {
		$db = $this->getDb();
		$sql =
		$sql = 'SELECT
					id,
					name,
					email,
					role
				FROM user;';
		$result = $db->query($sql);
		foreach ($result as $row){
			$row['roleName'] = $this->getRoleName($row['role']);
			$newResult[] = $row;
		}

		return $newResult;
	}
	protected function getRoleName($role){
		switch ($role){
			case 1:
				return 'Administrator';
			case 2:
				return 'Author';
			case 3:
				return 'Guest';
		}
	}
	public function getOneByEmail($email){
		$db = $this->getDb();
		$sql = 'SELECT id, name, email, password FROM user WHERE email = {email};';
		$fields = array(
			'email'=> $email
		);
		$sql = $db->prepare($sql, $fields);
		$result = $db->query($sql);
		if (isset($result[0])) {
			return $result[0];
		} else {
			return null;
		}
	}

	public function create($fields){
		$db = $this->getDb();


		$hash = password_hash($fields['password'],PASSWORD_DEFAULT);
		$fields['password'] = $<PASSWORD>;
		$sql = 'INSERT INTO user (name, email, role, password, created)
				VALUES ({name}, {email}, {role}, {password}, {created});';
		$sql = $db->prepare($sql, $fields, true);
		return $db->exec($sql);
	}

	public function update($fields) {
		$db = $this->getDb();
				$sql = 'UPDATE user SET
				password = {password}
				modified = {modified},
				WHERE id = {id};';
		$hash = password_hash($fields['password'],PASSWORD_DEFAULT);
		$fields['password'] = $hash;
		$sql = $db->prepare($sql,$fields);
		return $db->exec($sql);
	}

	public function delete($id) {
		//
		$db = $this->getDb();
		$sql = 'DELETE FROM user WHERE id = {id};';
		$fields = array(
			'id' => $id
		);
		$sql = $db->prepare($sql,$fields);
		return $db->exec($sql);
	}

	/**
	 * Checks if the password is equal to the saved password hash
	 *
	 * @param $id
	 * @param $password
	 * @return bool Returns true if the password equals the hash
	 */
	public function checkPassword($id, $password){
		$db = $this->getDb();
		$sql = 'SELECT password FROM user WHERE id = {id};';
		$fields = array(
			'id' => $id
		);
		$sql = $db->prepare($sql, $fields);
		$result = $db->query($sql);
		return password_verify($password,$result[0]['password']);
	}
	public function setPassword($id,$password){
		$db = $this->getDb();
		$password = password_hash($password,PASSWORD_DEFAULT);
		$sql = $sql = 'UPDATE user SET
				password = {password}
				modified = {modified}
				WHERE id = {id};';
		$fields = array(
			'id' => $id,
			'password' => $password
		);
		$sql = $db->prepare($sql,$fields);
		return $db->exec($sql);
	}
	public function validate($data){
{'fraction_non_alphanumeric': 0.19193597100573845, 'fraction_numerical': 0.005738447598912715, 'mean_word_length': 3.424181696726787, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 62, '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': '7283916', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2279, 'n_words': 617}