-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserActions.php
123 lines (107 loc) · 3.75 KB
/
UserActions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of UserActions
*
* @author Brad
*/
require_once 'MySQLBooksConf.php';
class UserActions {
protected $link;
public function __construct() {
$this->link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PW);
if (!$this->link){
die('Could not connect');
}
mysql_select_db(MYSQL_DB);
}
public function checkoutBook($book, $user, $admin){
$query = sprintf("SELECT book_key, quantity FROM books WHERE isbn=%d",
mysql_real_escape_string($book->getISBN()));
$result = mysql_query($query);
if (!$result){
return 0;
}
$row = mysql_fetch_row($result);
$query = sprintf("SELECT COUNT(*) FROM checked_out WHERE book=%d AND returned=0 GROUP BY book",
mysql_real_escape_string($row[0]));
$result2 = mysql_query($query);
if (!$result2){
return 0;
}
if (mysql_num_rows($result2) != 0){
$row2 = mysql_fetch_row($result2);
if ($row[1] == $row2[0]){ //all checked out
return 0;
}
}
$query = sprintf("INSERT INTO checked_out (username, book, c_admin) VALUES('%s', %d, '%s')",
mysql_real_escape_string($user->getUserName()),
mysql_real_escape_string($row[0]),
mysql_real_escape_string($admin->getUserName()));
$result = mysql_query($query);
if (!$result){
return 0;
}
return 1;
}
public function returnBook($book, $user, $admin){
$query = sprintf("SELECT book_key FROM checked_out INNER JOIN books ON book_key=book WHERE isbn=%d and returned=0 AND username='%s'",
mysql_real_escape_string($book->getISBN()),
mysql_real_escape_string($user->getUserName()));
$result = mysql_query($query);
if (!$result){
return 0;
}
if (mysql_num_rows($result) == 0){ //no book checked out
return 0;
}
$row = mysql_fetch_array($result);
$query = sprintf("UPDATE checked_out SET returned=1, r_admin='%s' WHERE book=%d AND returned=0 AND username='%s'",
mysql_real_escape_string($admin->getUserName()),
mysql_real_escape_string($row[0]),
mysql_real_escape_string($user->getUserName()));
$result = mysql_query($query);
if (!$result){
return 0;
}
return 1;
}
public function getCheckedOutUsers(){
$query = "SELECT username, name FROM checked_out INNER JOIN books ON book_key=book WHERE returned=0";
$result = mysql_query($query);
if (!$result){
return 0;
}
$users_books = array();
while ($row = mysql_fetch_array($result)){
array_push($users_books, $row);
}
if (count($users_books)){
return $users_books;
}else{
return 0;
}
}
public function getCheckedOut(){
$query = "SELECT username, name FROM checked_out INNER JOIN books ON book=book_key WHERE returned=0";
$result = mysql_query($query);
if (!$result){
return 0;
}
$user_book = array();
while ($row = mysql_fetch_array($result)){
array_push($user_book, $row[0]);
array_push($user_book, $row[1]);
}
if (count($user_book)){
return $user_book;
}else{
return 0;
}
}
}
?>