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>
|
|
|
|
* Link: http://trac.shishnet.org/shimmie2/
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: Allow users to score images
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-21 22:20:31 +00:00
|
|
|
class NumericScore extends Extension {
|
2007-10-18 01:21:55 +00:00
|
|
|
var $theme;
|
|
|
|
|
2007-10-02 21:06:33 +00:00
|
|
|
public function receive_event($event) {
|
2007-10-21 22:20:31 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object("numeric_score", "NumericScoreTheme");
|
2007-10-18 01:21:55 +00:00
|
|
|
|
2007-10-02 21:06:33 +00:00
|
|
|
if(is_a($event, 'InitExtEvent')) {
|
|
|
|
global $config;
|
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();
|
|
|
|
}
|
2007-10-21 22:43:35 +00:00
|
|
|
$config->set_default_bool("numeric_score_anon", true);
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2007-10-21 22:20:31 +00:00
|
|
|
if(is_a($event, 'PageRequestEvent') && $event->page_name == "numeric_score" &&
|
2007-10-21 23:04:48 +00:00
|
|
|
$event->get_arg(0) == "vote" &&
|
2007-10-18 01:21:55 +00:00
|
|
|
isset($_POST['score']) && isset($_POST['image_id'])) {
|
|
|
|
$i_score = int_escape($_POST['score']);
|
|
|
|
$i_image_id = int_escape($_POST['image_id']);
|
|
|
|
|
2007-10-21 22:20:31 +00:00
|
|
|
if($i_score >= -1 || $i_score <= 1) {
|
2007-10-21 22:35:47 +00:00
|
|
|
send_event(new NumericScoreSetEvent($i_image_id, $event->user, $i_score));
|
2007-10-18 01:21:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$event->page->set_mode("redirect");
|
|
|
|
$event->page->set_redirect(make_link("post/view/$i_image_id"));
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
2007-10-18 02:29:39 +00:00
|
|
|
|
2007-10-21 22:20:31 +00:00
|
|
|
if(is_a($event, 'NumericScoreSetEvent')) {
|
2007-10-21 22:43:35 +00:00
|
|
|
if(!$event->user->is_anonymous() || $config->get_bool("numeric_score_anon")) {
|
|
|
|
$this->add_vote($event->image_id, $event->user->id, $event->score);
|
|
|
|
}
|
2007-10-18 02:29:39 +00:00
|
|
|
}
|
2007-10-02 21:06:33 +00:00
|
|
|
|
2007-10-18 01:21:55 +00:00
|
|
|
if(is_a($event, 'DisplayingImageEvent')) {
|
2007-10-21 22:43:35 +00:00
|
|
|
global $user;
|
2007-10-22 03:17:08 +00:00
|
|
|
global $config;
|
2007-10-21 22:43:35 +00:00
|
|
|
if(!$user->is_anonymous() || $config->get_bool("numeric_score_anon")) {
|
|
|
|
$this->theme->display_voter($event->page, $event->image->id, $event->image->numeric_score);
|
|
|
|
}
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
2007-10-18 01:21:55 +00:00
|
|
|
|
2007-10-21 23:12:49 +00:00
|
|
|
if(is_a($event, 'ImageDeletionEvent')) {
|
|
|
|
global $database;
|
|
|
|
$database->execute("DELETE FROM numeric_score_votes WHERE image_id=?", array($event->image->id));
|
|
|
|
}
|
|
|
|
|
2007-10-18 01:21:55 +00:00
|
|
|
if(is_a($event, 'SetupBuildingEvent')) {
|
2007-10-21 22:43:35 +00:00
|
|
|
$sb = new SetupBlock("Numeric Score");
|
|
|
|
$sb->add_bool_option("numeric_score_anon", "Allow anonymous votes: ");
|
|
|
|
$event->panel->add_block($sb);
|
2007-10-02 21:06:33 +00:00
|
|
|
}
|
2007-11-04 08:16:41 +00:00
|
|
|
|
|
|
|
if(is_a($event, 'ParseLinkTemplateEvent')) {
|
|
|
|
$event->replace('$score', $event->image->numeric_score);
|
|
|
|
}
|
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)");
|
2007-10-18 01:21:55 +00:00
|
|
|
$database->Execute("
|
2007-10-21 22:20:31 +00:00
|
|
|
CREATE TABLE numeric_score_votes (
|
2007-10-18 01:21:55 +00:00
|
|
|
image_id INTEGER NOT NULL,
|
|
|
|
user_id INTEGER NOT NULL,
|
|
|
|
score INTEGER NOT NULL,
|
|
|
|
UNIQUE(image_id, user_id),
|
|
|
|
INDEX(image_id)
|
|
|
|
)
|
|
|
|
");
|
2007-10-21 22:20:31 +00:00
|
|
|
$config->set_int("ext_numeric_score_version", 1);
|
2007-10-21 17:18:31 +00:00
|
|
|
}
|
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(
|
2007-10-21 22:59:47 +00:00
|
|
|
"REPLACE 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
|
|
|
?>
|