File: review.php

package info (click to toggle)
opendb 0.81p18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,716 kB
  • ctags: 6,787
  • sloc: php: 50,213; sql: 3,098; sh: 272; makefile: 54; xml: 48
file content (452 lines) | stat: -rw-r--r-- 12,303 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/* 	OpenDb - Open Media Lending Database
	Copyright (C) 2001,2002 by Jason Pell

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


//
// This is the script for the comments table.
//
include_once("./functions/database.php");
include_once("./functions/logging.php");
include_once("./functions/datetime.php");
include_once("./functions/utils.php");
include_once("./functions/item.php");
include_once("./functions/item_type_group.php");

//
// Return TRUE if title has at least one comment.
//
function is_item_reviewed($item_id)
{
	global $CONFIG_VARS;
	
	$query = "SELECT count('x') as count ".
			"FROM review r ";
	
	if($CONFIG_VARS['item_review.include_other_title_reviews']===TRUE)
	{
		$item_r = fetch_item_r($item_id);
		$query .= ", item i ".
				  "WHERE r.item_id = i.id AND (i.id = $item_id OR i.title = '".addslashes($item_r['title'])."')";
		
		if($CONFIG_VARS['item_review.other_title_reviews_restrict_to_item_type_group']!==FALSE)
		{	
			// first of all we need to get the groups this item belongs to, then we need
			// to get the list of all other s_item_type's that are in those groups.	  
			$item_type_group_r = fetch_item_type_groups_for_item_type_r($item_r['s_item_type'], 'Y');// only system_ind='Y' groups here.
			if(is_array($item_type_group_r))
			{
				$item_type_r = fetch_item_types_for_group_r($item_type_group_r[0]); // only use first one.
				if(is_array($item_type_r))
				{
					$query .= " AND i.s_item_type IN (".format_sql_in_clause($item_type_r).")";
				}
			}
		}
	}
	else
	{	
		$query .= "WHERE r.item_id = $item_id";
	}
	
	$query .= " LIMIT 0,1";
	
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		if ($found!==FALSE && $found['count']>0)
			return TRUE;
	}
	
	//else
	return FALSE;
}

function is_exists_review($sequence_number)
{
	$query = "SELECT 'x' FROM review WHERE sequence_number = '$sequence_number'";

	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		mysql_free_result($result);
		return TRUE;
	}

	//else
	return FALSE;
}

function is_review_author($sequence_number, $author_id)
{
	$query = "SELECT author_id FROM review ".
			"WHERE sequence_number = $sequence_number";
	
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		if ($found && $found['author_id'] == $author_id)
			return TRUE;
	}
	
	//else
	return FALSE;
}

/**
	Returns a count of items stored in the database, or false if none found.
*/
function fetch_review_atdate_cnt($update_on)
{
	$query = "SELECT count(r.item_id) as count FROM review r WHERE r.update_on >= '$update_on'";
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		if ($found!== FALSE)
			return $found['count'];
	}

	//else
	return FALSE;
}

/**
	Returns a count of items stored in the database, or false if none found.
	If $s_item_type is specified, only reviews for items of the given s_item_type are counted
*/
function fetch_review_cnt($s_item_type = NULL)
{
	$query = "SELECT count(r.item_id) as count FROM review r";
	if($s_item_type)
		$query .= ", item i WHERE r.item_id = i.id AND i.s_item_type='$s_item_type'";

	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		if ($found!== FALSE)
			return $found['count'];
	}

	//else
	return FALSE;
}

//
// This will return all reviews for a $item_id.  This may match multiple $item_id records
// if more than one item of the same title exists, which is quite possible.
//
function fetch_review_rs($item_id)
{
	global $CONFIG_VARS;
	
	$query = "SELECT r.sequence_number, i.id as item_id, i.title, i.s_item_type, r.author_id, r.comment, r.rating, UNIX_TIMESTAMP(r.update_on) as update_on".
				" FROM review r, item i ".
				" WHERE r.item_id = i.id AND ";
				
	if($CONFIG_VARS['item_review.include_other_title_reviews']===TRUE)
	{
		$item_r = fetch_item_r($item_id);
		$query .= "(i.id = $item_id OR i.title = '".addslashes($item_r['title'])."')";
		
		if($CONFIG_VARS['item_review.other_title_reviews_restrict_to_item_type_group']!==FALSE)
		{		  
			// first of all we need to get the groups this item belongs to, then we need
			// to get the list of all other s_item_type's that are in those groups.	  
			$item_type_group_r = fetch_item_type_groups_for_item_type_r($item_r['s_item_type'], 'Y');// only system_ind='Y' groups here.
			if(is_array($item_type_group_r))
			{
				$item_type_r = fetch_item_types_for_group_r($item_type_group_r[0]); // only use first one.
				if(is_array($item_type_r))
				{
					$query .= " AND i.s_item_type IN (".format_sql_in_clause($item_type_r).")";
				}
			}
		}
	}
	else
	{
		$query .= "i.id = $item_id";
	}
	
	$query .= " ORDER BY r.update_on DESC";

	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
		return $result;
	else
		return FALSE;
}

function fetch_review_r($sequence_number)
{
	$query = "SELECT r.sequence_number, i.id as item_id, i.title, i.s_item_type, r.author_id, r.comment, r.rating, UNIX_TIMESTAMP(r.update_on) as update_on".
				" FROM review r, item i".
				" WHERE r.item_id = i.id AND r.sequence_number = $sequence_number";

	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		return $found;
	}
	else
		return FALSE;
}

/**
* @param $author_id
*/
function fetch_author_review_cnt($author_id)
{
	$query = "SELECT count('X') as count FROM review WHERE author_id = '$author_id'";

	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		if ($found!== FALSE)
			return $found['count'];
	}

	//else
	return FALSE;
}

/*
* Checks whether any reviews created by specified user.
* 
* @param $exclude_user_items - If TRUE, we should not count 
* 								reviews, for items the user owns
*/
function is_user_author($user_id, $exclude_user_items=FALSE)
{
	if($exclude_user_items!==TRUE)
		$query = "SELECT 'X' as count FROM review WHERE author_id = '$user_id'";
	else
	{
		$query = "SELECT 'X' ".
				"FROM review r, item_instance ii ".
				"WHERE r.item_id = ii.item_id AND ".
				"r.author_id = '$user_id' AND ".
				"ii.owner_id <> '$user_id'";
	}
	
	$result = run_opendb_query($query);
	if ($result && mysql_num_rows($result)>0)
	{
		mysql_free_result($result);
		return TRUE;
	}
	else
		return FALSE;
}

/*
 * Returns average rating for title.
 */
function fetch_review_rating($item_id = NULL)
{
	global $CONFIG_VARS;
	
	if($item_id)
	{
		$query = "SELECT rating FROM review r ";
		if($CONFIG_VARS['item_review.include_other_title_reviews']===TRUE)
		{
			$item_r = fetch_item_r($item_id);
			$query .= ", item i ".
					  "WHERE r.item_id = i.id AND (i.id = $item_id OR i.title = '".addslashes($item_r['title'])."')";
					  
			if($CONFIG_VARS['item_review.other_title_reviews_restrict_to_item_type_group']!==FALSE)
			{		  
				// first of all we need to get the groups this item belongs to, then we need
				// to get the list of all other s_item_type's that are in those groups.	  
				$item_type_group_r = fetch_item_type_groups_for_item_type_r($item_r['s_item_type'], 'Y');// only system_ind='Y' groups here.
				if(is_array($item_type_group_r))
				{
					$item_type_r = fetch_item_types_for_group_r($item_type_group_r[0]); // only use first one.
					if(is_array($item_type_r))
					{
						$query .= " AND i.s_item_type IN (".format_sql_in_clause($item_type_r).")";
					}
				}
			}
		}
		else
		{	
			$query .= "WHERE r.item_id = $item_id";
		}
	}
	else
	{
		$query = "SELECT rating FROM review";
	}

	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		// Initialise.
		$total = 0;
		$number = 0;

		while($review_r = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			$total = $total + $review_r['rating'];
			$number++;
		}
		mysql_free_result($result);

		if ($number==0)
			return 0;
		else
			return $total / $number;
	}
	//else
	return FALSE;
}

//
// Insert a review
//
function insert_review($item_id, $author_id, $comment, $rating)
{
	global $HTTP_SESSION_VARS;
	
	// Ensure no html can be used!
	$comment = addslashes(replace_newlines(trim($comment)));

	$query="INSERT INTO review (item_id,author_id,comment,rating)".
			"VALUES ('$item_id','$author_id','$comment','$rating')";
	$insert = run_opendb_query($query);
	if ($insert && mysql_affected_rows() > 0)
	{
		opendb_log("Inserted review (author_id=$author_id, item_id=$item_id, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to insert review (author_id=$author_id, item_id=$item_id, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

/*
*/
function update_review($sequence_number, $comment, $rating)
{
	global $HTTP_SESSION_VARS;
	
	// Ensure no html can be used!
	$comment = addslashes(replace_newlines(trim($comment)));
	
	$query = "UPDATE review ".
			"SET comment = '$comment',".
			" rating = '$rating' ".
			"WHERE sequence_number = $sequence_number";
	
	$update = run_opendb_query($query);
	// We should not treat updates that were not actually updated because value did not change as failures.
	$rows_affected = mysql_affected_rows();
	if ($rows_affected !== -1)
	{
		if($rows_affected>0)
		{
			opendb_log("Updated review (sequence_number=$sequence_number, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		}
		return TRUE;
	}
	else
	{
		opendb_log("Failed to update review (sequence_number=$sequence_number, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

/*
* Delete a single review
*/
function delete_review($sequence_number)
{
	global $HTTP_SESSION_VARS;
	
	$query = "DELETE FROM review WHERE sequence_number = '$sequence_number'";
	$delete = run_opendb_query($query);
	
	if ($delete && mysql_affected_rows() > 0)
	{
		opendb_log("Deleted review (sequence_number=$sequence_number, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to delete review (sequence_number=$sequence_number, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

/*
* Delete all reviews for an item_id
*/
function delete_reviews($item_id)
{
	global $HTTP_SESSION_VARS;
	
	$query = "DELETE FROM review WHERE item_id = '$item_id'";
	$delete = run_opendb_query($query);
	// doesn't matter if no items deleted, as long as operation was successful.
	if ($delete)
	{
		opendb_log("Deleted reviews (item_id=$item_id, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to delete reviews (item_id=$item_id, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}

function delete_author_reviews($author_id)
{
	global $HTTP_SESSION_VARS;
	
	$query = "DELETE FROM review WHERE author_id = '$author_id'";
	$delete = run_opendb_query($query);
	// doesn't matter if no items deleted, as long as operation was successful.
	if($delete)
	{
		opendb_log("Deleted Author reviews (author_id=$author_id, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to delete author reviews (author_id=$author_id, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
}
?>