2007-10-02 21:06:33 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2007-10-21 22:20:31 +00:00
|
|
|
* Name: Image Scores (Numeric)
|
2007-10-02 21:06:33 +00:00
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
2012-02-08 02:52:11 +00:00
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
2007-10-02 21:06:33 +00:00
|
|
|
* License: GPLv2
|
|
|
|
* Description: Allow users to score images
|
2009-01-16 08:18:41 +00:00
|
|
|
* Documentation:
|
|
|
|
* Each registered user may vote an image +1 or -1, the
|
|
|
|
* image's score is the sum of all votes.
|
2007-10-02 21:06:33 +00:00
|
|
|
*/
|
|
|
|
|
2007-10-21 22:20:31 +00:00
|
|
|
class NumericScoreSetEvent extends Event {
|
2007-10-18 02:29:39 +00:00
|
|
|
var $image_id, $user, $score;
|
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function NumericScoreSetEvent(/*int*/ $image_id, User $user, /*int*/ $score) {
|
2007-10-18 02:29:39 +00:00
|
|
|
$this->image_id = $image_id;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->score = $score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class NumericScore extends Extension {
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onInitExt(InitExtEvent $event) {
|
|
|
|
global $config;
|
|
|
|
if($config->get_int("ext_numeric_score_version", 0) < 1) {
|
|
|
|
$this->install();
|
|
|
|
}
|
|
|
|
}
|
2007-10-18 01:21:55 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event) {
|
|
|
|
global $user, $page;
|
|
|
|
if(!$user->is_anonymous()) {
|
2013-12-31 08:22:58 +00:00
|
|
|
$this->theme->get_voter($event->image);
|
2012-02-08 11:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-27 18:16:46 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onUserPageBuilding(UserPageBuildingEvent $event) {
|
2012-02-12 12:18:12 +00:00
|
|
|
global $page, $user;
|
2012-03-31 12:19:11 +00:00
|
|
|
if($user->can("edit_other_vote")) {
|
2013-12-31 08:22:58 +00:00
|
|
|
$this->theme->get_nuller($event->display_user);
|
2012-02-12 12:18:12 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
|
|
|
global $config, $database, $user, $page;
|
2007-10-18 01:21:55 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
if($event->page_matches("numeric_score_votes")) {
|
|
|
|
$image_id = int_escape($event->get_arg(0));
|
|
|
|
$x = $database->get_all(
|
|
|
|
"SELECT users.name as username, user_id, score
|
|
|
|
FROM numeric_score_votes
|
|
|
|
JOIN users ON numeric_score_votes.user_id=users.id
|
|
|
|
WHERE image_id=?",
|
|
|
|
array($image_id));
|
2013-07-06 09:42:06 +00:00
|
|
|
$html = "<table style='width: 100%;'>";
|
2012-02-08 11:24:25 +00:00
|
|
|
foreach($x as $vote) {
|
|
|
|
$html .= "<tr><td>";
|
2013-04-07 11:30:38 +00:00
|
|
|
$html .= "<a href='".make_link("user/{$vote['username']}")."'>{$vote['username']}</a>";
|
2013-07-06 09:42:06 +00:00
|
|
|
$html .= "</td><td width='10'>";
|
2012-02-08 11:24:25 +00:00
|
|
|
$html .= $vote['score'];
|
|
|
|
$html .= "</td></tr>";
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
die($html);
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
if($event->page_matches("numeric_score_vote") && $user->check_auth_token()) {
|
2008-04-08 16:53:48 +00:00
|
|
|
if(!$user->is_anonymous()) {
|
2012-02-08 11:24:25 +00:00
|
|
|
$image_id = int_escape($_POST['image_id']);
|
|
|
|
$char = $_POST['vote'];
|
|
|
|
$score = null;
|
|
|
|
if($char == "up") $score = 1;
|
|
|
|
else if($char == "null") $score = 0;
|
|
|
|
else if($char == "down") $score = -1;
|
|
|
|
if(!is_null($score) && $image_id>0) send_event(new NumericScoreSetEvent($image_id, $user, $score));
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("post/view/$image_id"));
|
2008-07-21 15:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
if($event->page_matches("numeric_score/remove_votes_on") && $user->check_auth_token()) {
|
2012-03-31 11:28:34 +00:00
|
|
|
if($user->can("edit_other_vote")) {
|
2012-02-08 11:24:25 +00:00
|
|
|
$image_id = int_escape($_POST['image_id']);
|
|
|
|
$database->execute(
|
|
|
|
"DELETE FROM numeric_score_votes WHERE image_id=?",
|
|
|
|
array($image_id));
|
|
|
|
$database->execute(
|
|
|
|
"UPDATE images SET numeric_score=0 WHERE id=?",
|
|
|
|
array($image_id));
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("post/view/$image_id"));
|
|
|
|
}
|
2011-03-28 21:31:45 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
if($event->page_matches("numeric_score/remove_votes_by") && $user->check_auth_token()) {
|
2012-03-31 11:28:34 +00:00
|
|
|
if($user->can("edit_other_vote")) {
|
2012-03-25 01:41:33 +00:00
|
|
|
$this->delete_votes_by(int_escape($_POST['user_id']));
|
2012-02-08 11:24:25 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link());
|
2011-03-23 10:44:52 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
}
|
|
|
|
if($event->page_matches("popular_by_day") || $event->page_matches("popular_by_month") || $event->page_matches("popular_by_year")) {
|
2012-04-01 16:47:39 +00:00
|
|
|
$t_images = $config->get_int("index_images");
|
2012-02-08 11:24:25 +00:00
|
|
|
|
|
|
|
//TODO: Add Popular_by_week.
|
|
|
|
|
|
|
|
//year
|
|
|
|
if(empty($_GET['year'])){
|
|
|
|
$year = date("Y");
|
|
|
|
}else{
|
|
|
|
$year = $_GET['year'];
|
2007-10-21 22:43:35 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
//month
|
|
|
|
if(empty($_GET['month']) || int_escape($_GET['month']) > 12){
|
|
|
|
$month = date("m");
|
|
|
|
}else{
|
|
|
|
$month = $_GET['month'];
|
2011-03-23 11:04:22 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
//day
|
|
|
|
if(empty($_GET['day']) || int_escape($_GET['day']) > 31){
|
|
|
|
$day = date("d");
|
|
|
|
}else{
|
|
|
|
$day = $_GET['day'];
|
2011-03-23 11:04:22 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
$totaldate = $year."/".$month."/".$day;
|
2012-01-24 20:11:16 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
$sql =
|
|
|
|
"SELECT * FROM images
|
|
|
|
WHERE EXTRACT(YEAR FROM posted) = :year
|
|
|
|
";
|
2012-01-24 20:11:16 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
$agrs = array("limit" => $t_images, "year" => $year);
|
2012-01-26 05:39:04 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
if($event->page_matches("popular_by_day")){
|
|
|
|
$sql .=
|
|
|
|
"AND EXTRACT(MONTH FROM posted) = :month
|
|
|
|
AND EXTRACT(DAY FROM posted) = :day
|
|
|
|
AND NOT numeric_score=0
|
2012-01-27 00:03:26 +00:00
|
|
|
";
|
2012-02-08 11:24:25 +00:00
|
|
|
//array_push doesn't seem to like using double arrows
|
|
|
|
//this requires us to instead create two arrays and merge
|
|
|
|
$sgra = array("month" => $month, "day" => $day);
|
|
|
|
$args = array_merge($agrs, $sgra);
|
2012-01-27 00:03:26 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
$dte = array($totaldate, date("F jS, Y", (strtotime($totaldate))), "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m\\&\\d\\a\\y\\=d", "day");
|
|
|
|
}
|
|
|
|
if($event->page_matches("popular_by_month")){
|
|
|
|
$sql .=
|
|
|
|
"AND EXTRACT(MONTH FROM posted) = :month
|
|
|
|
AND NOT numeric_score=0
|
|
|
|
";
|
|
|
|
$sgra = array("month" => $month);
|
|
|
|
$args = array_merge($agrs, $sgra);
|
2012-01-27 00:03:26 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
$title = date("F Y", (strtotime($totaldate)));
|
|
|
|
$dte = array($totaldate, $title, "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m", "month");
|
|
|
|
}
|
|
|
|
if($event->page_matches("popular_by_year")){
|
|
|
|
$sql .= "AND NOT numeric_score=0";
|
|
|
|
$dte = array($totaldate, $year, "\y\e\a\\r\=Y", "year");
|
|
|
|
$args = $agrs;
|
|
|
|
}
|
|
|
|
$sql .= " ORDER BY numeric_score DESC LIMIT :limit OFFSET 0";
|
2012-01-24 20:11:16 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
//filter images by year/score != 0 > limit to max images on one page > order from highest to lowest score
|
|
|
|
$result = $database->get_all($sql, $args);
|
2012-01-24 20:11:16 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
$images = array();
|
|
|
|
foreach($result as $singleResult) {
|
|
|
|
$images[] = Image::by_id($singleResult["id"]);
|
2012-01-24 20:11:16 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
$this->theme->view_popular($images, $dte);
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onNumericScoreSet(NumericScoreSetEvent $event) {
|
|
|
|
global $user;
|
2013-08-29 23:19:38 +00:00
|
|
|
log_debug("numeric_score", "Rated Image #{$event->image_id} as {$event->score}", true, array("image_id"=>$event->image_id));
|
2012-02-08 11:24:25 +00:00
|
|
|
$this->add_vote($event->image_id, $user->id, $event->score);
|
|
|
|
}
|
2008-04-08 16:53:48 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onImageDeletion(ImageDeletionEvent $event) {
|
|
|
|
global $database;
|
|
|
|
$database->execute("DELETE FROM numeric_score_votes WHERE image_id=:id", array("id" => $event->image->id));
|
|
|
|
}
|
2007-11-04 08:16:41 +00:00
|
|
|
|
2012-03-25 01:41:33 +00:00
|
|
|
public function onUserDeletion(UserDeletionEvent $event) {
|
|
|
|
$this->delete_votes_by($event->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete_votes_by(/*int*/ $user_id) {
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
$image_ids = $database->get_col("SELECT image_id FROM numeric_score_votes WHERE user_id=?", array($user_id));
|
|
|
|
|
2012-04-05 14:20:04 +00:00
|
|
|
if(count($image_ids) == 0) return;
|
|
|
|
|
2012-03-25 01:41:33 +00:00
|
|
|
$database->execute(
|
|
|
|
"DELETE FROM numeric_score_votes WHERE user_id=? AND image_id IN (".implode(",", $image_ids).")",
|
|
|
|
array($user_id));
|
2012-03-29 18:18:00 +00:00
|
|
|
$database->execute("
|
|
|
|
UPDATE images
|
|
|
|
SET numeric_score=COALESCE(
|
|
|
|
(
|
|
|
|
SELECT SUM(score)
|
|
|
|
FROM numeric_score_votes
|
|
|
|
WHERE image_id=images.id
|
|
|
|
),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
WHERE images.id IN (".implode(",", $image_ids).")");
|
2012-03-25 01:41:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
// FIXME: on user deletion
|
|
|
|
// FIXME: on user vote nuke
|
2008-04-08 17:16:29 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onParseLinkTemplate(ParseLinkTemplateEvent $event) {
|
|
|
|
$event->replace('$score', $event->image->numeric_score);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSearchTermParse(SearchTermParseEvent $event) {
|
|
|
|
$matches = array();
|
2014-01-13 09:13:56 +00:00
|
|
|
if(preg_match("/^score([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(-?\d+)$/", $event->term, $matches)) {
|
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
2012-02-08 11:24:25 +00:00
|
|
|
$score = $matches[2];
|
|
|
|
$event->add_querylet(new Querylet("numeric_score $cmp $score"));
|
|
|
|
}
|
2014-01-13 09:13:56 +00:00
|
|
|
if(preg_match("/^upvoted_by[=|:](.*)$/", $event->term, $matches)) {
|
2012-02-08 11:24:25 +00:00
|
|
|
$duser = User::by_name($matches[1]);
|
|
|
|
if(is_null($duser)) {
|
|
|
|
throw new SearchTermParseException(
|
|
|
|
"Can't find the user named ".html_escape($matches[1]));
|
2010-03-01 00:14:32 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
$event->add_querylet(new Querylet(
|
|
|
|
"images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)",
|
|
|
|
array("ns_user_id"=>$duser->id)));
|
|
|
|
}
|
2014-01-13 09:13:56 +00:00
|
|
|
if(preg_match("/^downvoted_by[=|:](.*)$/", $event->term, $matches)) {
|
2012-02-08 11:24:25 +00:00
|
|
|
$duser = User::by_name($matches[1]);
|
|
|
|
if(is_null($duser)) {
|
|
|
|
throw new SearchTermParseException(
|
|
|
|
"Can't find the user named ".html_escape($matches[1]));
|
2010-03-01 00:14:32 +00:00
|
|
|
}
|
2012-02-08 11:24:25 +00:00
|
|
|
$event->add_querylet(new Querylet(
|
|
|
|
"images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)",
|
|
|
|
array("ns_user_id"=>$duser->id)));
|
|
|
|
}
|
2014-01-13 09:13:56 +00:00
|
|
|
if(preg_match("/^upvoted_by_id[=|:](\d+)$/", $event->term, $matches)) {
|
2012-02-08 11:24:25 +00:00
|
|
|
$iid = int_escape($matches[1]);
|
|
|
|
$event->add_querylet(new Querylet(
|
|
|
|
"images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)",
|
|
|
|
array("ns_user_id"=>$iid)));
|
|
|
|
}
|
2014-01-13 09:13:56 +00:00
|
|
|
if(preg_match("/^downvoted_by_id[=|:](\d+)$/", $event->term, $matches)) {
|
2012-02-08 11:24:25 +00:00
|
|
|
$iid = int_escape($matches[1]);
|
|
|
|
$event->add_querylet(new Querylet(
|
|
|
|
"images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)",
|
|
|
|
array("ns_user_id"=>$iid)));
|
2008-04-08 17:16:29 +00:00
|
|
|
}
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
2007-10-18 01:21:55 +00:00
|
|
|
|
|
|
|
private function install() {
|
2007-10-02 21:06:33 +00:00
|
|
|
global $database;
|
|
|
|
global $config;
|
|
|
|
|
2007-10-21 22:20:31 +00:00
|
|
|
if($config->get_int("ext_numeric_score_version") < 1) {
|
2011-02-22 22:24:00 +00:00
|
|
|
$database->execute("ALTER TABLE images ADD COLUMN numeric_score INTEGER NOT NULL DEFAULT 0");
|
|
|
|
$database->execute("CREATE INDEX images__numeric_score ON images(numeric_score)");
|
2009-01-22 12:05:55 +00:00
|
|
|
$database->create_table("numeric_score_votes", "
|
2009-01-22 13:03:51 +00:00
|
|
|
image_id INTEGER NOT NULL,
|
|
|
|
user_id INTEGER NOT NULL,
|
2009-01-22 12:05:55 +00:00
|
|
|
score INTEGER NOT NULL,
|
|
|
|
UNIQUE(image_id, user_id),
|
2009-01-22 13:03:51 +00:00
|
|
|
INDEX(image_id),
|
|
|
|
FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE,
|
|
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
2007-10-18 01:21:55 +00:00
|
|
|
");
|
2007-10-21 22:20:31 +00:00
|
|
|
$config->set_int("ext_numeric_score_version", 1);
|
2007-10-21 17:18:31 +00:00
|
|
|
}
|
2009-04-22 03:29:14 +00:00
|
|
|
if($config->get_int("ext_numeric_score_version") < 2) {
|
2011-02-22 22:24:00 +00:00
|
|
|
$database->execute("CREATE INDEX numeric_score_votes__user_votes ON numeric_score_votes(user_id, score)");
|
2009-04-22 03:29:14 +00:00
|
|
|
$config->set_int("ext_numeric_score_version", 2);
|
|
|
|
}
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
private function add_vote(/*int*/ $image_id, /*int*/ $user_id, /*int*/ $score) {
|
2007-10-02 21:06:33 +00:00
|
|
|
global $database;
|
2011-02-22 22:24:00 +00:00
|
|
|
$database->execute(
|
|
|
|
"DELETE FROM numeric_score_votes WHERE image_id=:imageid AND user_id=:userid",
|
|
|
|
array("imageid" => $image_id, "userid" => $user_id));
|
2010-05-28 10:58:26 +00:00
|
|
|
if($score != 0) {
|
2011-02-22 22:24:00 +00:00
|
|
|
$database->execute(
|
|
|
|
"INSERT INTO numeric_score_votes(image_id, user_id, score) VALUES(:imageid, :userid, :score)",
|
|
|
|
array("imageid" => $image_id, "userid" => $user_id, "score" => $score));
|
2010-05-28 10:58:26 +00:00
|
|
|
}
|
2007-10-18 01:21:55 +00:00
|
|
|
$database->Execute(
|
2012-03-30 16:48:03 +00:00
|
|
|
"UPDATE images SET numeric_score=(
|
|
|
|
COALESCE(
|
|
|
|
(SELECT SUM(score) FROM numeric_score_votes WHERE image_id=:imageid),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
) WHERE id=:id",
|
2011-02-22 22:24:00 +00:00
|
|
|
array("imageid" => $image_id, "id" => $image_id));
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|