2007-10-02 21:06:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
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>
|
|
|
|
* 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;
|
|
|
|
|
2007-10-21 22:20:31 +00:00
|
|
|
public function NumericScoreSetEvent($image_id, $user, $score) {
|
2007-10-18 02:29:39 +00:00
|
|
|
$this->image_id = $image_id;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->score = $score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
class NumericScore implements Extension {
|
2007-10-18 01:21:55 +00:00
|
|
|
var $theme;
|
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
public function receive_event(Event $event) {
|
2009-05-11 14:04:33 +00:00
|
|
|
global $config, $database, $page, $user;
|
2008-09-06 16:59:02 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
2007-10-18 01:21:55 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof InitExtEvent) {
|
2007-10-21 22:20:31 +00:00
|
|
|
if($config->get_int("ext_numeric_score_version", 0) < 1) {
|
2007-10-02 21:06:33 +00:00
|
|
|
$this->install();
|
|
|
|
}
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof DisplayingImageEvent) {
|
2008-04-08 16:53:48 +00:00
|
|
|
if(!$user->is_anonymous()) {
|
2008-07-21 15:16:48 +00:00
|
|
|
$html = $this->theme->get_voter_html($event->image);
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->add_block(new Block("Image Score", $html, "left", 20));
|
2008-07-21 15:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-09-06 17:48:03 +00:00
|
|
|
if(($event instanceof PageRequestEvent) && $event->page_matches("numeric_score_vote")) {
|
2009-05-11 14:04:33 +00:00
|
|
|
if(!$user->is_anonymous()) {
|
2008-07-21 15:16:48 +00:00
|
|
|
$image_id = int_escape($_POST['image_id']);
|
|
|
|
$char = $_POST['vote'];
|
|
|
|
$score = 0;
|
|
|
|
if($char == "up") $score = 1;
|
|
|
|
else if($char == "down") $score = -1;
|
2009-05-11 14:04:33 +00:00
|
|
|
if($score != 0) send_event(new NumericScoreSetEvent($image_id, $user, $score));
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("post/view/$image_id"));
|
2007-10-21 22:43:35 +00:00
|
|
|
}
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof NumericScoreSetEvent) {
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->add_vote($event->image_id, $user->id, $event->score);
|
2008-04-08 16:53:48 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof ImageDeletionEvent) {
|
2007-10-21 23:12:49 +00:00
|
|
|
$database->execute("DELETE FROM numeric_score_votes WHERE image_id=?", array($event->image->id));
|
|
|
|
}
|
2007-11-04 08:16:41 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof ParseLinkTemplateEvent) {
|
2007-11-04 08:16:41 +00:00
|
|
|
$event->replace('$score', $event->image->numeric_score);
|
|
|
|
}
|
2008-04-08 17:16:29 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof SearchTermParseEvent) {
|
2008-04-08 17:16:29 +00:00
|
|
|
$matches = array();
|
2009-01-22 07:04:29 +00:00
|
|
|
if(preg_match("/^score(<|<=|=|>=|>)(\d+)$/", $event->term, $matches)) {
|
2008-04-08 17:16:29 +00:00
|
|
|
$cmp = $matches[1];
|
|
|
|
$score = $matches[2];
|
2008-05-19 15:59:58 +00:00
|
|
|
$event->set_querylet(new Querylet("numeric_score $cmp $score"));
|
2008-04-08 17:16:29 +00:00
|
|
|
}
|
2009-04-22 03:29:14 +00:00
|
|
|
if(preg_match("/^favou?rite$/", $event->term, $matches)) {
|
|
|
|
$event->set_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=1)", array($user->id)));
|
|
|
|
}
|
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) {
|
|
|
|
$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) {
|
2009-05-11 14:13:49 +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
|
|
|
}
|
|
|
|
|
2007-10-18 01:21:55 +00:00
|
|
|
private function add_vote($image_id, $user_id, $score) {
|
2007-10-02 21:06:33 +00:00
|
|
|
global $database;
|
2007-10-18 01:21:55 +00:00
|
|
|
$database->Execute(
|
2008-07-24 15:07:39 +00:00
|
|
|
"DELETE FROM numeric_score_votes WHERE image_id=? AND user_id=?",
|
|
|
|
array($image_id, $user_id));
|
|
|
|
$database->Execute(
|
|
|
|
"INSERT INTO numeric_score_votes(image_id, user_id, score) VALUES(?, ?, ?)",
|
2007-10-18 01:21:55 +00:00
|
|
|
array($image_id, $user_id, $score));
|
|
|
|
$database->Execute(
|
2007-10-21 22:20:31 +00:00
|
|
|
"UPDATE images SET numeric_score=(SELECT SUM(score) FROM numeric_score_votes WHERE image_id=?) WHERE id=?",
|
2007-10-18 01:21:55 +00:00
|
|
|
array($image_id, $image_id));
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 22:29:54 +00:00
|
|
|
add_event_listener(new NumericScore());
|
2007-10-02 21:06:33 +00:00
|
|
|
?>
|