From 7b621d6a8e0f05314c913a4beb7ca7c6e0123ca0 Mon Sep 17 00:00:00 2001 From: shish Date: Sun, 21 Oct 2007 22:06:34 +0000 Subject: [PATCH] two score extensions git-svn-id: file:///home/shish/svn/shimmie2/trunk@538 7f39781d-f577-437e-ae19-be835c7a54ca --- contrib/event_log/main.php | 3 +- contrib/{score => score_numeric}/main.php | 0 contrib/{score => score_numeric}/theme.php | 0 contrib/score_text/main.php | 104 +++++++++++++++++++++ contrib/score_text/theme.php | 51 ++++++++++ contrib/wiki/main.php | 11 ++- 6 files changed, 164 insertions(+), 5 deletions(-) rename contrib/{score => score_numeric}/main.php (100%) rename contrib/{score => score_numeric}/theme.php (100%) create mode 100644 contrib/score_text/main.php create mode 100644 contrib/score_text/theme.php diff --git a/contrib/event_log/main.php b/contrib/event_log/main.php index f8d7aa53..f9b79cd8 100644 --- a/contrib/event_log/main.php +++ b/contrib/event_log/main.php @@ -86,7 +86,8 @@ class EventLog extends Extension { $this->add_to_log($user, 'Source Set', "Source for image #{$event->image_id} set to '{$event->source}'"); } if(is_a($event, 'TagSetEvent')) { - $this->add_to_log($user, 'Tags Set', "Tags for image #{$event->image_id} set to '{$event->tags}'"); + $tags = implode($event->tags, ", "); + $this->add_to_log($user, 'Tags Set', "Tags for image #{$event->image_id} set to '$tags'"); } } diff --git a/contrib/score/main.php b/contrib/score_numeric/main.php similarity index 100% rename from contrib/score/main.php rename to contrib/score_numeric/main.php diff --git a/contrib/score/theme.php b/contrib/score_numeric/theme.php similarity index 100% rename from contrib/score/theme.php rename to contrib/score_numeric/theme.php diff --git a/contrib/score_text/main.php b/contrib/score_text/main.php new file mode 100644 index 00000000..62ad0cd9 --- /dev/null +++ b/contrib/score_text/main.php @@ -0,0 +1,104 @@ + + * Link: http://trac.shishnet.org/shimmie2/ + * License: GPLv2 + * Description: Allow users to score images + */ + +class ScoreSetEvent extends Event { + var $image_id, $user, $score; + + public function ScoreSetEvent($image_id, $user, $score) { + $this->image_id = $image_id; + $this->user = $user; + $this->score = $score; + } +} + +class Score extends Extension { + var $theme; + + public function receive_event($event) { + if(is_null($this->theme)) $this->theme = get_theme_object("score", "ScoreTheme"); + + if(is_a($event, 'InitExtEvent')) { + global $config; + if($config->get_int("ext_score_version", 0) < 2) { + $this->install(); + } + + global $config; + $config->set_default_string("ext_rating_anon_privs", 'sq'); + $config->set_default_string("ext_rating_user_privs", 'sq'); + } + + if(is_a($event, 'PageRequestEvent') && $event->page_name == "score" && + $event->get_arg(0) == "set" && $event->user->is_admin() && + isset($_POST['score']) && isset($_POST['image_id'])) { + $i_score = int_escape($_POST['score']); + $i_image_id = int_escape($_POST['image_id']); + + if($i_score >= -2 || $i_score <= 2) { + send_event(new ScoreSetEvent($i_image_id, $event->user->id, $i_score)); + } + + $event->page->set_mode("redirect"); + $event->page->set_redirect(make_link("post/view/$i_image_id")); + } + + if(is_a($event, 'ScoreSetEvent')) { + $this->add_vote($event->image_id, $event->user->id, $event->score); + } + + if(is_a($event, 'DisplayingImageEvent')) { + // TODO: scorer vs voter + $this->theme->display_scorer($event->page, $event->image->id, $event->image->vote_score); + } + + if(is_a($event, 'SetupBuildingEvent')) { + /* + TODO: disable anon voting + TODO: switch between average and sum modes + */ + } + } + + private function install() { + global $database; + global $config; + + if($config->get_int("ext_score_version") < 1) { + $database->Execute("ALTER TABLE images ADD COLUMN score INTEGER NOT NULL DEFAULT 0"); + $database->Execute("CREATE INDEX images__score ON images(score)"); + $database->Execute(" + CREATE TABLE images_score_votes ( + image_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + score INTEGER NOT NULL, + UNIQUE(image_id, user_id), + INDEX(image_id) + ) + "); + $config->set_int("ext_score_version", 1); + } + if($config->get_int("ext_score_version") < 2) { + $database->Execute("ALTER TABLE images CHANGE score vote_score INTEGER NOT NULL DEFAULT 0"); + $config->set_int("ext_score_version", 2); + } + } + + private function add_vote($image_id, $user_id, $score) { + global $database; + // TODO: update if already voted + $database->Execute( + "INSERT INTO images_score_votes(image_id, user_id, score) VALUES(?, ?, ?)", + array($image_id, $user_id, $score)); + $database->Execute( + "UPDATE images SET vote_score=(SELECT AVG(score) FROM images_score_votes WHERE image_id=?) WHERE id=?", + array($image_id, $image_id)); + } +} +add_event_listener(new Score()); +?> diff --git a/contrib/score_text/theme.php b/contrib/score_text/theme.php new file mode 100644 index 00000000..64d070ff --- /dev/null +++ b/contrib/score_text/theme.php @@ -0,0 +1,51 @@ +
+ + + + + + + +
+ "; + $page->add_block(new Block(null, $html, "main", 7)); + } + + public function display_voter($page, $image_id, $score) { + $i_image_id = int_escape($image_id); + $i_score = int_escape($score) / 2; + + $html = " + Current score is $i_score +
+
+ + + +
+
+ + + +
+ "; + $page->add_block(new Block(null, $html, "main", 7)); + } +} + +?> diff --git a/contrib/wiki/main.php b/contrib/wiki/main.php index fe25b270..b1e062c9 100644 --- a/contrib/wiki/main.php +++ b/contrib/wiki/main.php @@ -11,11 +11,11 @@ // WikiUpdateEvent {{{ class WikiUpdateEvent extends Event { var $user; - var $page; + var $wikipage; - public function WikiUpdateEvent($user, $page) { + public function WikiUpdateEvent($user, $wikipage) { $this->user = $user; - $this->page = $page; + $this->wikipage = $wikipage; } } // }}} @@ -81,6 +81,7 @@ class Wiki extends Extension { global $user; if($this->can_edit($user, $this->get_page($title))) { + // send_event(new WikiUpdateEvent($event->user, new WikiPage(...)) if($user->is_admin()) { $this->set_page($title, $rev, $body, $lock); } @@ -121,7 +122,7 @@ class Wiki extends Extension { } if(is_a($event, 'WikiUpdateEvent')) { - $this->update_wiki_page($event->user, $event->page); + $this->set_page($event->wikipage); } if(is_a($event, 'SetupBuildingEvent')) { @@ -177,6 +178,8 @@ class Wiki extends Extension { ORDER BY revision DESC", array($title)); return ($row ? new WikiPage($row) : null); } + + // TODO: accept a WikiPage object private function set_page($title, $rev, $body, $locked) { global $database; global $user;