<?php

function redirect_to($page)
{
	header("Location: {$page}");
	exit;
}
function make_connection(){
	global $connection;
	
	$dbhost="localhost";
	$dbuser="root";
	$dbpass="secret";
	$dbname="utopia";

		 // $dbhost = 'localhost'; //Define database server host
		 // $dbuser= 'vitcabsh'; //Define database username
		 // $dbpass= 'lDTy9176sg'; //Define database password
		 // $dbname = 'vitcabsh_vitbookshare'; //Define database name
	$connection=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
	if(mysqli_connect_errno()){
		die("Database connection failed:"
			);
	}
}
function sql_escape($input){
	global $connection;
	return mysqli_real_escape_string($connection,$input);
}
function confirm_query($result){
	if(!$result){
		echo "DataBase Query failed.";
	}
}
function prepare_block($blockno){
	return $blockno;
}
function register_user($name,$regno,$phone,$password,$block)
{
	global $connection;
	$name=sql_escape($name);
	$regno=sql_escape($regno);
	$phone=sql_escape($phone);
	$password=sql_escape($password);
	$password=password_hash($password,PASSWORD_DEFAULT);
	$query="INSERT INTO user ";
	$query.="VALUES('{$name}','{$regno}',{$phone},'{$password}','{$block}',' ');";
	$result=mysqli_query($connection,$query);
	if(!$result)
	{
		return 0;
	}
	return 1;
}
function register_user_fb($fbid,$name,$regno,$email,$phone,$block,$roomno)
{
	global $connection;
	$fbid=sql_escape($fbid);
	$name=sql_escape($name);
	$regno=sql_escape($regno);
	$email=sql_escape($email);
	$phone=sql_escape($phone);
	$block=sql_escape($block);
	$roomno=sql_escape($roomno);
	$query="INSERT INTO user(fbid,name,regno,email,phone,block,roomno) ";
	$query.="VALUES('{$fbid}','{$name}','{$regno}','{$email}',{$phone},'{$block}','{$roomno}');";
	$result=mysqli_query($connection,$query);
	if(!$result)
	{
		return 0;
	}
	return 1;
}
function update_user($name,$regno,$email,$phone,$block,$roomno)
{
	global $connection;
	$name=sql_escape($name);
	$regno=sql_escape($regno);
	$email=sql_escape($email);
	$phone=sql_escape($phone);
	$block=sql_escape($block);
	$roomno=sql_escape($roomno);
	$query="UPDATE user SET name='$name',email='$email',phone=$phone,block=$block,roomno='$roomno' WHERE regno='$regno'";
	$result=mysqli_query($connection,$query);
	if(!$result)
	{
		return 0;
	}
	return 1;
}
function attempt_login($regno,$password)
{
	global $connection;
	$query="SELECT password FROM user WHERE regno='{$regno}' limit 1;";
	$result=mysqli_query($connection,$query);
	if(!$result)
	{
		return 0;
	}
	$row=mysqli_fetch_assoc($result);
	$password_db=$row['password'];
	if(password_verify($password,$password_db))
		return 1;
	else
		return 0;
}
function check_fb_login($fbid){
	global $connection;
	$query="SELECT regno FROM user WHERE fbid='{$fbid}';";
	$result=mysqli_query($connection,$query);
	if(mysqli_num_rows($result)>=1)
		return mysqli_fetch_assoc($result);
	else
		return false;
}
function is_loggedin(){
	if(isset($_SESSION['login']) && !empty($_SESSION['login']) && get_user())
		return true;
	else
		return false;
}
function get_user()
{
	global $connection;
	if((!isset($_SESSION['login'])) || empty($_SESSION['login']))
		return null;
	$regno=$_SESSION['login'];
	$query="SELECT * FROM user WHERE regno='{$regno}' LIMIT 1;";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	$row=mysqli_fetch_assoc($result);
	if(!$row)
		return null;
	return $row;
}
function get_all_users()
{
	global $connection;
	$query="SELECT * FROM user;";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	return $result;
}
function get_user_by_id($id)
{
	global $connection;
	$query="SELECT * FROM user WHERE regno='{$id}';";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	$row=mysqli_fetch_assoc($result);
	return $row;
}
function get_all_books()
{
	global $connection;
	$query="SELECT * FROM book order by course,price;";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	return $result;
}
function get_all_grievances()
{
	global $connection;
	$query="SELECT * FROM grievance order by upvotes DESC;";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	return $result;
}
function get_all_news()
{
	global $connection;
	$query="SELECT * FROM news order by upvotes DESC;";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	return $result;
}

function get_all_recommendations()
{
	global $connection;
	$query="SELECT * FROM recommendation order by upvotes DESC;";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	return $result;
}
function get_all_books_uid($user_id)
{
	global $connection;
	$query="SELECT * FROM book WHERE owner_id='{$user_id}';";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	return $result;
}
function get_all_book_request($book_id,$user_id)
{
	global $connection;
	$query="SELECT * FROM requests WHERE book_id=$book_id and to_id='$user_id';";
	$result=mysqli_query($connection,$query);
	return $result;
}
function get_book_by_id($id)
{
	global $connection;
	$query="SELECT * FROM book WHERE id=$id;";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	$row=mysqli_fetch_assoc($result);
	return $row;
}
function get_my_books($regno)
{
	global $connection;
	$query="SELECT * FROM book WHERE owner_id='{$regno}';";
	$result=mysqli_query($connection,$query);
	if(!$result)
		return null;
	return $result;
}
function print_a_book($row,$user_id)
{
	global $connection;
	$owner_id=$row['owner_id'];
	$id=$row['id'];
	$name=htmlspecialchars($row['name']);
	$author=htmlspecialchars($row['author']);
	$course=htmlspecialchars($row['course']);
	$price=htmlspecialchars($row['price']);
	$imageuploaded=$row['imageuploaded'];
	$dispmyname=$row['dispmyname'];
	$dispmyemail=$row['dispmyemail'];
	$dispmyphone=$row['dispmyphone'];
	$dispmyhostel=$row['dispmyhostel'];
	$symbol="&#8377;";
	$query2="SELECT * FROM user WHERE regno='$owner_id';";
	$result2=mysqli_query($connection,$query2);
	$row2=mysqli_fetch_assoc($result2);
	$owner_name=$row2['name'];
	$owner_email=$row2['regno'];
	$owner_phone=$row2['phone'];
	$owner_block=$row2['block'];
	$owner_roomno=$row2['roomno'];
	if($dispmyname==0)
		$owner_name="Annonymous";
	if($dispmyemail==0)
		$owner_email="";
	if($dispmyphone==0)
		$owner_phone="";
	if($dispmyhostel==0){
		$owner_block="";
		$owner_roomno="";
	}
	$query3="SELECT * FROM requests WHERE book_id='$id' and from_id='$user_id' LIMIT 1;";
	$result3=mysqli_query($connection,$query3);
	$row3=mysqli_fetch_assoc($result3);
	$requestsend=1;
	if(empty($row3))
		$requestsend=0;
	$khudkabook=0;
	if($row['owner_id']==$user_id)
		$khudkabook=1;
	if(empty($owner_block))
	{
		$owner_block="";
		$owner_roomno="";
	}
	if(empty($owner_roomno))
		$owner_roomno="";
	if($price==0||empty($price))
	{
		$symbol="FREE";
		$price='';
	}
	$health=$row['health'];
	if($imageuploaded==1)
		$pathtoimage="book_images/{$id}.png";
	else
		$pathtoimage="book_images/default_image.png";
	$toprint="

	<section class='book card-panel card hoverable modal-trigger' href='#modal{$id}'>
		<div class='row'>
			<h5 class='center booktitle'>{$name}</h5>
			<div class='col s4'>
				<img src='{$pathtoimage}' class='bookimg responsive-img'>
				<div class='bookauthor center'>by <i>{$author}</i></div>
			</div>
			<div class='col s8'>
				<div class='bookcourse'><b>{$course}</b></div>
				<div class='bookhealth'>{$health}</div>
			</div>
			<div class='right bookprice'>{$symbol}{$price}</div>
			<div class='clicktoview'><a href=''>view owner</a></div>
		</div>
	</section>";
	//now the modal
	$toprint.="
	<div id='modal{$id}' class='modal'>
		<div class='modal-content center'>
			<h4 class='center booktitle'>{$name} </h4>
			<p class='ownedby'>Owned by -</p>
			<div class='row'>
				<i class='fa fa-user fa-2x hide-on-small-only' aria-hidden='true'></i>
				<i class='fa fa-user fa-1x hide-on-med-and-up' aria-hidden='true'></i>
				<span class='bookperson'>&nbsp;{$owner_name}</span>
			</div>
			<div class='row'>";
				if(!empty($owner_block))
				{
					$toprint.="
					<i class='fa fa-map-marker fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-map-marker fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookblock'>&nbsp;{$owner_block}</span>";
				}
				$toprint.="
			</div>
			<div class='row'>
				";

				if(!empty($owner_email))
				{
					$toprint.="
					<i class='fa fa-envelope fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-envelope fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookemail'>&nbsp;{$owner_email}</span>
					&nbsp;&nbsp;";
				}
				if(!empty($owner_phone))
				{
					$toprint.="
					<i class='fa fa-phone fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-phone fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookphone'>&nbsp;{$owner_phone}</span>";
				}
				$toprint.="
			</div>
		</div>
		<div class='modal-footer'>

			<a style='color:#a3a3a3' class='modal-action modal-close waves-effect waves-green btn-flat'>Close</a>
			";
			if($requestsend==1)
				$toprint.="
			<button class='modal-action modal-close waves-effect waves-green btn disabled'>request send</button>";
			elseif($khudkabook==1)	
				$toprint.="
			<button class='modal-action modal-close waves-effect waves-green btn disabled'>your book</button>";
			else
				$toprint.="
			<form action='searchbook.php' method='POST'>
				<input type='text' class='hide' value='$owner_id' name='owner_id'>
				<input type='number' class='hide' value='$id' name='book_id'>
				<button type='submit' name='sendrequestsubmit' class='modal-action modal-close waves-effect waves-green btn'>send request</button>
			</form>";
			if($imageuploaded==1)
				$toprint.="<br/>
			<a href='{$pathtoimage}' target='_blank' class='viewimglink'>view image</a>";
			$toprint.="
		</div>
	</div>

	";
	echo $toprint;

}
function print_a_grievance($row,$user_id)
{
	global $connection;
	$owner_id=$row['owner_id'];
	$id=$row['id'];
	$name=htmlspecialchars($row['name']);
	$authority=htmlspecialchars($row['authority']);
	$imageuploaded=$row['imageuploaded'];
	$dispmyname=$row['dispmyname'];
	$dispmyemail=$row['dispmyemail'];
	$dispmyphone=$row['dispmyphone'];
	$upvotes=$row['upvotes'];
	$dispmyhostel=$row['dispmyhostel'];
	$symbol="&#8377;";
	$query2="SELECT * FROM user WHERE regno='$owner_id';";
	$result2=mysqli_query($connection,$query2);
	$row2=mysqli_fetch_assoc($result2);
	$owner_name=$row2['name'];
	$owner_email=$row2['regno'];
	$owner_phone=$row2['phone'];
	$owner_block=$row2['block'];
	$owner_roomno=$row2['roomno'];
	if($dispmyname==0)
		$owner_name="Annonymous";
	if($dispmyemail==0)
		$owner_email="";
	if($dispmyphone==0)
		$owner_phone="";
	if($dispmyhostel==0){
		$owner_block="";
		$owner_roomno="";
	}
	
	$khudkabook=0;
	if($row['owner_id']==$user_id)
		$khudkabook=1;
	if(empty($owner_block))
	{
		$owner_block="";
		$owner_roomno="";
	}
	if(empty($owner_roomno))
		$owner_roomno="";
	
	$health=$row['health'];
	$location=$row['location'];
	if($imageuploaded==1)
		$pathtoimage="grievances_images/{$id}.png";
	else
		$pathtoimage="grievances_images/default_image.png";
	$toprint="

	<section class='book card-panel card hoverable modal-trigger' href='#modal{$id}'>
		<div class='row'>
			<h5 class='center booktitle'>{$name}</h5>
			<div class='col s4'>
				<img src='{$pathtoimage}' class='bookimg responsive-img'>
			</div>
			<div class='col s8'>
				<div class='bookcourse'><b>{$authority}</b></div>
				<div class='bookhealth'>{$health}</div>
			</div>
			<div class='right bookprice'><i class='fa fa-map-marker fa-1x' aria-hidden='true'></i> {$location}</div>
			<div class='clicktoview'><a href=''>view poster</a></div>
			<span class='upvote-btn hoverable z-depth-2'><span class='fas fa-arrow-up'></span> Upvote {$upvotes}</span>
		</div>
	</section>";
	//now the modal
	$toprint.="
	<div id='modal{$id}' class='modal'>
		<div class='modal-content center'>
			<h4 class='center booktitle'>{$name} </h4>
			<p class='ownedby'>Posted by -</p>
			<div class='row'>
				<i class='fa fa-user fa-2x hide-on-small-only' aria-hidden='true'></i>
				<i class='fa fa-user fa-1x hide-on-med-and-up' aria-hidden='true'></i>
				<span class='bookperson'>&nbsp;{$owner_name}</span>
			</div>
			<div class='row'>";
				if(!empty($owner_block))
				{
					$toprint.="
					<i class='fa fa-map-marker fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-map-marker fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookblock'>&nbsp;{$owner_block}</span>";
				}
				$toprint.="
			</div>
			<div class='row'>
				";

				if(!empty($owner_email))
				{
					$toprint.="
					<i class='fa fa-envelope fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-envelope fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookemail'>&nbsp;{$owner_email}</span>
					&nbsp;&nbsp;";
				}
				if(!empty($owner_phone))
				{
					$toprint.="
					<i class='fa fa-phone fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-phone fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookphone'>&nbsp;{$owner_phone}</span>";
				}
				$toprint.="
			</div>
		</div>
		<div class='modal-footer'>

			<a style='color:#a3a3a3' class='modal-action modal-close waves-effect waves-green btn-flat'>Close</a>
			";
			if($imageuploaded==1)
				$toprint.="<br/>
			<a href='{$pathtoimage}' target='_blank' class='viewimglink'>view image</a>";
			$toprint.="
		</div>
	</div>

	";
	echo $toprint;

}
function print_a_news($row,$user_id)
{
	global $connection;
	$owner_id=$row['owner_id'];
	$id=$row['id'];
	$name=htmlspecialchars($row['name']);
	$imageuploaded=$row['imageuploaded'];
	$dispmyname=$row['dispmyname'];
	$dispmyemail=$row['dispmyemail'];
	$upvotes=$row['upvotes'];
	$dispmyphone=$row['dispmyphone'];
	$dispmyhostel=$row['dispmyhostel'];
	$symbol="&#8377;";
	$query2="SELECT * FROM user WHERE regno='$owner_id';";
	$result2=mysqli_query($connection,$query2);
	$row2=mysqli_fetch_assoc($result2);
	$owner_name=$row2['name'];
	$owner_email=$row2['regno'];
	$owner_phone=$row2['phone'];
	$owner_block=$row2['block'];
	$owner_roomno=$row2['roomno'];
	if($dispmyname==0)
		$owner_name="Annonymous";
	if($dispmyemail==0)
		$owner_email="";
	if($dispmyphone==0)
		$owner_phone="";
	if($dispmyhostel==0){
		$owner_block="";
		$owner_roomno="";
	}
	
	$khudkabook=0;
	if($row['owner_id']==$user_id)
		$khudkabook=1;
	if(empty($owner_block))
	{
		$owner_block="";
		$owner_roomno="";
	}
	if(empty($owner_roomno))
		$owner_roomno="";
	
	$health=$row['health'];
	$location=$row['location'];
	if($imageuploaded==1)
		$pathtoimage="news_images/{$id}.png";
	else
		$pathtoimage="news_images/default_image.png";
	$toprint="

	<section class='book card-panel card hoverable modal-trigger' href='#modal{$id}'>
		<div class='row'>
			<h5 class='center booktitle'>{$name}</h5>
			<div class='col s4'>
				<img src='{$pathtoimage}' class='bookimg responsive-img'>
			</div>
			<div class='col s8'>
				<div class='bookhealth'>{$health}</div>
			</div>
			<div class='right bookprice'><i class='fa fa-map-marker fa-1x' aria-hidden='true'></i> {$location}</div>
			<div class='clicktoview'><a href=''>view poster</a></div>
			<span class='upvote-btn hoverable z-depth-2'><span class='fas fa-arrow-up'></span> Upvote {$upvotes}</span>
		</div>
	</section>";
	//now the modal
	$toprint.="
	<div id='modal{$id}' class='modal'>
		<div class='modal-content center'>
			<h4 class='center booktitle'>{$name} </h4>
			<p class='ownedby'>Posted by -</p>
			<div class='row'>
				<i class='fa fa-user fa-2x hide-on-small-only' aria-hidden='true'></i>
				<i class='fa fa-user fa-1x hide-on-med-and-up' aria-hidden='true'></i>
				<span class='bookperson'>&nbsp;{$owner_name}</span>
			</div>
			<div class='row'>";
				if(!empty($owner_block))
				{
					$toprint.="
					<i class='fa fa-map-marker fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-map-marker fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookblock'>&nbsp;{$owner_block}</span>";
				}
				$toprint.="
			</div>
			<div class='row'>
				";

				if(!empty($owner_email))
				{
					$toprint.="
					<i class='fa fa-envelope fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-envelope fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookemail'>&nbsp;{$owner_email}</span>
					&nbsp;&nbsp;";
				}
				if(!empty($owner_phone))
				{
					$toprint.="
					<i class='fa fa-phone fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-phone fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookphone'>&nbsp;{$owner_phone}</span>";
				}
				$toprint.="
			</div>
		</div>
		<div class='modal-footer'>

			<a style='color:#a3a3a3' class='modal-action modal-close waves-effect waves-green btn-flat'>Close</a>
			";
			if($imageuploaded==1)
				$toprint.="<br/>
			<a href='{$pathtoimage}' target='_blank' class='viewimglink'>view image</a>";
			$toprint.="
		</div>
	</div>

	";
	echo $toprint;

}
function print_a_recommendation($row,$user_id)
{
	global $connection;
	$owner_id=$row['owner_id'];
	$id=$row['id'];
	$name=htmlspecialchars($row['name']);
	$authority=htmlspecialchars($row['authority']);
	$stars=htmlspecialchars($row['stars']);
	$imageuploaded=$row['imageuploaded'];
	$dispmyname=$row['dispmyname'];
	$dispmyemail=$row['dispmyemail'];
	$upvotes=$row['upvotes'];
	$dispmyphone=$row['dispmyphone'];
	$dispmyhostel=$row['dispmyhostel'];
	$symbol="&#8377;";
	$query2="SELECT * FROM user WHERE regno='$owner_id';";
	$result2=mysqli_query($connection,$query2);
	$row2=mysqli_fetch_assoc($result2);
	$owner_name=$row2['name'];
	$owner_email=$row2['regno'];
	$owner_phone=$row2['phone'];
	$owner_block=$row2['block'];
	$owner_roomno=$row2['roomno'];
	if($dispmyname==0)
		$owner_name="Annonymous";
	if($dispmyemail==0)
		$owner_email="";
	if($dispmyphone==0)
		$owner_phone="";
	if($dispmyhostel==0){
		$owner_block="";
		$owner_roomno="";
	}
	
	$khudkabook=0;
	if($row['owner_id']==$user_id)
		$khudkabook=1;
	if(empty($owner_block))
	{
		$owner_block="";
		$owner_roomno="";
	}
	if(empty($owner_roomno))
		$owner_roomno="";
	
	$health=$row['health'];
	$location=$row['location'];
	if($imageuploaded==1)
		$pathtoimage="recommendations_images/{$id}.png";
	else
		$pathtoimage="recommendations_images/default_image.png";
	$toprint="

	<section class='book card-panel card hoverable modal-trigger' href='#modal{$id}'>
		<div class='row'>
			<h5 class='center booktitle'>{$name}</h5>
			<div class='col s4'>
				<img src='{$pathtoimage}' class='bookimg responsive-img'>
			</div>
			<div class='col s8'>
				<div class='bookcourse'><b>{$authority}</b></div>
				<div class='bookhealth'>{$health}</div>
				<b>Rating: {$stars}</b>
			</div>
			<div class='right bookprice'><i class='fa fa-map-marker fa-1x' aria-hidden='true'></i> {$location}</div>
			<div class='clicktoview'><a href=''>view poster</a></div>
			<span class='upvote-btn hoverable z-depth-2'><span class='fas fa-arrow-up'></span> Upvote {$upvotes}</span>
		</div>
	</section>";
	//now the modal
	$toprint.="
	<div id='modal{$id}' class='modal'>
		<div class='modal-content center'>
			<h4 class='center booktitle'>{$name} </h4>
			<p class='ownedby'>Posted by -</p>
			<div class='row'>
				<i class='fa fa-user fa-2x hide-on-small-only' aria-hidden='true'></i>
				<i class='fa fa-user fa-1x hide-on-med-and-up' aria-hidden='true'></i>
				<span class='bookperson'>&nbsp;{$owner_name}</span>
			</div>
			<div class='row'>";
				if(!empty($owner_block))
				{
					$toprint.="
					<i class='fa fa-map-marker fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-map-marker fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookblock'>&nbsp;{$owner_block}</span>";
				}
				$toprint.="
			</div>
			<div class='row'>
				";

				if(!empty($owner_email))
				{
					$toprint.="
					<i class='fa fa-envelope fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-envelope fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookemail'>&nbsp;{$owner_email}</span>
					&nbsp;&nbsp;";
				}
				if(!empty($owner_phone))
				{
					$toprint.="
					<i class='fa fa-phone fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-phone fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookphone'>&nbsp;{$owner_phone}</span>";
				}
				$toprint.="
			</div>
		</div>
		<div class='modal-footer'>

			<a style='color:#a3a3a3' class='modal-action modal-close waves-effect waves-green btn-flat'>Close</a>
			";
			if($imageuploaded==1)
				$toprint.="<br/>
			<a href='{$pathtoimage}' target='_blank' class='viewimglink'>view image</a>";
			$toprint.="
		</div>
	</div>

	";
	echo $toprint;

}
function print_my_book($row)
{
	$id=$row['id'];
	$name=htmlspecialchars($row['name']);
	$author=htmlspecialchars($row['author']);
	$course=htmlspecialchars($row['course']);
	$price=htmlspecialchars($row['price']);
	$symbol="&#8377;";
	if($price==0||empty($price))
	{
		$symbol="FREE";
		$price='';
	}
	$health=$row['health'];
	$imageuploaded=$row['imageuploaded'];
	$pathtoimage='';
	if($imageuploaded==1)
		$pathtoimage="book_images/{$id}.png";
	else
		$pathtoimage="book_images/default_image.png";
	$toprint="
	<a href='editthisbook.php?id=$id' class='acolornone'>
		<section class='book card card-panel hoverable'>
			<div class='row'>
				<h5 class='center booktitle'>{$name}</h5>
				<div class='col s4'>
					<img src='{$pathtoimage}' class='bookimg responsive-img'>
					<div class='bookauthor center'>by <i>{$author}</i></div>
				</div>
				<div class='col s8'>
					<div class='bookcourse'><b>{$course}</b></div>
					<div class='bookhealth'>{$health}</div>
				</div>
				<div class='right bookprice'>{$symbol}{$price}</div>
				<div class='clicktoview'>click to edit..</div>
				
			</div>
		</section>
	</a>
	";
	return $toprint;

}
function add_request($rq_book_id,$user_id,$rq_owner_id){
	global $connection;
	$query="INSERT INTO requests(from_id,to_id,book_id) values('$user_id','$rq_owner_id',$rq_book_id);";
	$result=mysqli_query($connection,$query);
	return $result;
}

function get_all_requests($user_id){
	global $connection;
	$query="SELECT * FROM requests WHERE to_id='$user_id' order by id desc;";
	$result=mysqli_query($connection,$query);
	return $result;
}
function print_book_requests($row,$book_requests)
{
	$book_id=$row['id'];
	$book_name=$row['name'];
	$book_author=$row['author'];
	$book_course=$row['course'];
	$book_price=$row['price'];
	$book_health=$row['health'];
	$book_imageuploaded=$row['imageuploaded'];
	$norequests=0;
	if($book_requests)
		$norequests=mysqli_num_rows($book_requests);
	$pathtoimage='';
	if($book_imageuploaded==1)
		$pathtoimage="book_images/{$book_id}.png";
	else
		$pathtoimage="book_images/default_image.png";
	$toprint="
	<section class='book card card-panel hoverable modal-trigger' href='#modal{$book_id}'>
		<div class='row'>
			<h5 class='center booktitle'>{$book_name}</h5>
			<div class='col s4'>
				<img src='{$pathtoimage}' class='bookimg responsive-img'>
				<div class='bookauthor center'>by <i>{$book_author}</i></div>
			</div>
			<div class='col s8'>
				<div class='bookcourse'><b>{$book_course}</b></div>
				<div class='bookhealth'>{$book_health}</div>
			</div>";
			if(empty($book_price))
				$toprint.="
			<div class='right bookprice'>FREE</div>";
			else
				$toprint.="
			<div class='right bookprice'>&#8377;{$book_price}</div>";
			$toprint.="
			<div class='badge-bs left norequests'>{$norequests} Requests</div>
			<div class='clicktoview'><a href=''>view requests</a></div>
			
		</div>
	</section>
	";
	$toprint.="
	<div id='modal{$book_id}' class='modal'>
		<div class='modal-content center'>
			<h4 class='center booktitle'>{$book_name}</h4>";
			if($norequests!=0)
				$toprint.="
			<p class='ownedby'>Requests from:</p>";
			$cur_re_no=0;
			while($re_row=mysqli_fetch_assoc($book_requests))
			{
				$cur_re_no++;
				$rq_owner_id=$re_row['from_id'];
				$request_from_row=get_user_by_id($rq_owner_id);
				$rq_owner_name=$request_from_row['name'];
				$rq_owner_email=$request_from_row['regno'];
				$rq_owner_phone=$request_from_row['phone'];
				$rq_owner_block=$request_from_row['block'];
				$rq_owner_roomno=$request_from_row['roomno'];
				$toprint.="
				<div class='row'>
					<span class='left'>{$cur_re_no}. </span>
					<i class='fa fa-user fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-user fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookperson'>&nbsp;{$rq_owner_name}</span>
				</div>
				<div class='row'>
					<i class='fa fa-map-marker fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-map-marker fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					";
					if($rq_owner_block==99||$rq_owner_block=='99')
						$toprint.="
					<span class='bookblock'>dayscholar</span>";
					else
					{
						$rq_owner_block=prepare_block($rq_owner_block);
						$toprint.="
						<span class='bookblock'>{$rq_owner_block}</span>
						<span class='bookroomno'>&nbsp;{$rq_owner_roomno}</span>";
					}
					$toprint.="	
				</div>
				<div class='row'>

					<i class='fa fa-envelope fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-envelope fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookemail'>&nbsp;{$rq_owner_email}</span>
					&nbsp;&nbsp;
					<i class='fa fa-phone fa-2x hide-on-small-only' aria-hidden='true'></i>
					<i class='fa fa-phone fa-1x hide-on-med-and-up' aria-hidden='true'></i>
					<span class='bookphone'>&nbsp;{$rq_owner_phone}</span>
				</div><hr>
				";
			}
			if($cur_re_no==0){
				$toprint.="no requests";
			}
			$toprint.=
			"
		</div>
	</div>
	";
	return $toprint;
}

function get_all_transactions($user_id){
	global $connection;
	$query="SELECT b.name,u.name,t.sold_borrowed,t.cost,t.due_return_date,t.act_return_date as bname,uname,status,cost,due_return_date,act_return_date FROM transactions t,book b,user u WHERE t.book_id=b.id and b.owner_id='{$user_id}' and t.buyer_id=u.regno;";
	$query="SELECT b.name as bname,u.name as uname,t.sold_borrowed as status,t.cost as cost,t.due_return_date as due_return_date,t.act_return_date as act_return_date FROM transactions t,book b,user u WHERE t.book_id=b.id and b.owner_id='{$user_id}' and t.buyer_id=u.regno;";
	$result=mysqli_query($connection,$query);
	return $result;
}
function insert_transaction($book_id,$buyer_id,$sold_borrowed,$cost,$due_return_date,$act_return_date)
{
	global $connection;
	if($sold_borrowed==2)
	{
		if(empty($due_return_date) or empty($act_return_date))
			return false;
	}
	if(empty($due_return_date) or empty($act_return_date))
		$query="INSERT INTO transactions(book_id,buyer_id,sold_borrowed,cost,due_return_date,act_return_date) values({$book_id},'{$buyer_id}',{$sold_borrowed},{$cost},null,null);";
	else
		$query="INSERT INTO transactions(book_id,buyer_id,sold_borrowed,cost,due_return_date,act_return_date) values({$book_id},'{$buyer_id}',{$sold_borrowed},{$cost},'{$due_return_date}','{$act_return_date}');";
	$result=mysqli_query($connection,$query);
	return $result;

}
function user_male_percent(){
	global $connection;
	$query="SELECT count(regno)/(select count(regno) from user)*100 as user_male_percent FROM user WHERE block<=17;";
	$result=mysqli_query($connection,$query);
	$row=mysqli_fetch_assoc($result);
	return $row['user_male_percent'];
}
function user_female_percent(){
	global $connection;
	$query="SELECT count(regno)/(select count(regno) from user)*100 as user_female_percent FROM user WHERE block>17;";
	$result=mysqli_query($connection,$query);
	$row=mysqli_fetch_assoc($result);
	return $row['user_female_percent'];
}
function no_books_borrowed(){
	global $connection;
	$query="SELECT count(id)/(select count(id) from transactions)*100 as no_books_borrowed FROM transactions WHERE sold_borrowed=2;";
	$result=mysqli_query($connection,$query);
	$row=mysqli_fetch_assoc($result);
	return $row['no_books_borrowed'];
}
function no_books_sold(){
	global $connection;
	$query="SELECT count(id)/(select count(id) from transactions)*100 as no_books_sold FROM transactions WHERE sold_borrowed=1;";
	$result=mysqli_query($connection,$query);
	$row=mysqli_fetch_assoc($result);
	return $row['no_books_sold'];
}
function overdue_percent(){
	global $connection;
	$query=" SELECT count(id)/(SELECT count(id) FROM transactions WHERE sold_borrowed=2)*100 as overdue_percent FROM transactions WHERE sold_borrowed=2 and due_return_date<act_return_date;";
	$result=mysqli_query($connection,$query);
	$row=mysqli_fetch_assoc($result);
	return $row['overdue_percent'];
}
function timely_return_percent(){
	global $connection;
	$query="SELECT count(id)/(SELECT count(id) FROM transactions WHERE sold_borrowed=2)*100 as timely_return_percent FROM transactions WHERE sold_borrowed=2 and due_return_date>=act_return_date;";
	$result=mysqli_query($connection,$query);
	$row=mysqli_fetch_assoc($result);
	return $row['timely_return_percent'];
}
function money_received_by_female_user(){
	global $connection;
	$query="SELECT sum(cost)/(select sum(price) FROM book,user WHERE book.owner_id=regno and block>17)*100 as money_received_by_female_user FROM  transactions,book,user WHERE user.regno=owner_id and book_id=book.id and block>17;";
	$result=mysqli_query($connection,$query);
	$row=mysqli_fetch_assoc($result);
	return $row['money_received_by_female_user'];
}
function money_received_by_male_user(){
	global $connection;
	$query="SELECT sum(cost)/(select sum(price) FROM book,user WHERE book.owner_id=regno and block<=17)*100 as money_received_by_male_user FROM  transactions,book,user WHERE user.regno=owner_id and book_id=book.id and block<=17;";
	$result=mysqli_query($connection,$query);
	$row=mysqli_fetch_assoc($result);
	return $row['money_received_by_male_user'];
}
function max_books(){
	global $connection;
	$query="SELECT u.name as uname,owner_id,count(id) as count_rid from book b,user u WHERE b.owner_id=u.regno group by owner_id order by count(id) desc;";
	$result=mysqli_query($connection,$query);
	return $result;
}
function max_requests(){
	global $connection;
	$query="SELECT u.name as uname,r.from_id,count(r.id) as count_rid FROM requests r,user u WHERE r.from_id=u.regno GROUP BY r.from_id ORDER BY count(r.id) desc;";
	$result=mysqli_query($connection,$query);
	return $result;
}
function insert_review($fbid,$user_id,$user_name,$message){
	$query="INSERT INTO reviews(fbid,regno,name,message) values('{$fbid}','{$user_id}','{$user_name}','{$message}');";
	global $connection;
	$result=mysqli_query($connection,$query);
	if($result)
		return true;
	else
		return false;
}
function get_all_reviews(){
	$query="SELECT * FROM reviews;";
	global $connection;
	$result=mysqli_query($connection,$query);
	return $result;
}
function delete_review($review_id){
	global $connection;
	$query="DELETE FROM reviews WHERE id='{$review_id}';";
	$result=mysqli_query($connection,$query);
	if($result)
		return true;
	else
		return false;
}
?>