id = intval($id); $this->type = Dba::escape($type); // Check for the users rating if ($rating == $this->get_user($GLOBALS['user']->id)) { $this->rating = $rating; } else { $this->get_average(); } return true; } // Constructor /** * get_user * Get the user's rating this is based off the currently logged * in user. It returns the value */ public function get_user($user_id) { $user_id = Dba::escape($user_id); $sql = "SELECT `score` FROM `rating` WHERE `user`='$user_id' AND `object_id`='$this->id' AND `object_type`='$this->type'"; $db_results = Dba::query($sql); $results = Dba::fetch_assoc($db_results); return $results['score']; } // get_user /** * get_average * Get the users average rating this is based off the floor'd average * of what everyone has rated this album as. This is shown if there * is no personal rating, and used for random play mojo. It sets * $this->average_rating and returns the value */ public function get_average() { $sql = "SELECT `score` FROM `rating` WHERE `object_id`='$this->id' AND `object_type`='$this->type'"; $db_results = Dba::query($sql); $i = 0; while ($r = Dba::fetch_assoc($db_results)) { $i++; $total += $r['score']; } // while we're pulling results if ($total > 0) { $average = floor($total/$i); $this->rating = $average; } elseif ($i >= '1' AND $total == '0') { $this->rating = '-1'; } else { $this->rating = '0'; } return $average; } // get_average /** * set_rating * This function sets a rating for the current $this object. * This uses the currently logged in user for the 'user' who is rating * the object. Returns true on success, false on failure */ public function set_rating($score) { $score = Dba::escape($score); /* Check if it exists */ $sql = "SELECT `id` FROM `rating` WHERE `object_id`='$this->id' AND `object_type`='$this->type' " . "AND `user`='" . Dba::escape($GLOBALS['user']->id) . "'"; $db_results = Dba::query($sql); if ($existing = Dba::fetch_assoc($db_results)) { $sql = "UPDATE `rating` SET `score`='$score' WHERE `id`='" . $existing['id'] . "'"; $db_results = Dba::query($sql); } else { $sql = "INSERT INTO `rating` (`object_id`,`object_type`,`score`,`user`) VALUES " . " ('$this->id','$this->type','$score','" . $GLOBALS['user']->id . "')"; $db_results = Dba::query($sql); } return true; } // set_rating /** * show * This takes an id and a type and displays the rating if ratings are enabled. */ public static function show ($object_id,$type) { // If there aren't ratings don't return anything if (!Config::get('ratings')) { return false; } $rating = new Rating($object_id,$type); require Config::get('prefix') . '/templates/show_object_rating.inc.php'; } // show /** * show_static * This is a static version of the ratings created by Andy90 */ public static function show_static ($object_id,$type) { // If there aren't ratings don't return anything if (!Config::get('ratings')) { return false; } $rating = new Rating($object_id,$type); require Config::get('prefix') . '/templates/show_static_object_rating.inc.php'; } // show_static } //end rating class ?>