splitting

git-svn-id: file:///home/shish/svn/shimmie2/trunk@539 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2007-10-21 22:20:31 +00:00
parent 7b621d6a8e
commit 7b289c3354
4 changed files with 32 additions and 83 deletions

View file

@ -1,60 +1,55 @@
<?php
/**
* Name: Image Scores
* Name: Image Scores (Numeric)
* Author: Shish <webmaster@shishnet.org>
* Link: http://trac.shishnet.org/shimmie2/
* License: GPLv2
* Description: Allow users to score images
*/
class ScoreSetEvent extends Event {
class NumericScoreSetEvent extends Event {
var $image_id, $user, $score;
public function ScoreSetEvent($image_id, $user, $score) {
public function NumericScoreSetEvent($image_id, $user, $score) {
$this->image_id = $image_id;
$this->user = $user;
$this->score = $score;
}
}
class Score extends Extension {
class NumericScore extends Extension {
var $theme;
public function receive_event($event) {
if(is_null($this->theme)) $this->theme = get_theme_object("score", "ScoreTheme");
if(is_null($this->theme)) $this->theme = get_theme_object("numeric_score", "NumericScoreTheme");
if(is_a($event, 'InitExtEvent')) {
global $config;
if($config->get_int("ext_score_version", 0) < 2) {
if($config->get_int("ext_numeric_score_version", 0) < 1) {
$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() &&
if(is_a($event, 'PageRequestEvent') && $event->page_name == "numeric_score" &&
$event->get_arg(0) == "vote" && $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));
if($i_score >= -1 || $i_score <= 1) {
send_event(new NumericScoreSetEvent($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')) {
if(is_a($event, 'NumericScoreSetEvent')) {
$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);
$this->theme->display_voter($event->page, $event->image->id, $event->image->numeric_score);
}
if(is_a($event, 'SetupBuildingEvent')) {
@ -69,11 +64,11 @@ class Score extends Extension {
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)");
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)");
$database->Execute("
CREATE TABLE images_score_votes (
CREATE TABLE numeric_score_votes (
image_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
score INTEGER NOT NULL,
@ -81,11 +76,7 @@ class Score extends Extension {
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);
$config->set_int("ext_numeric_score_version", 1);
}
}
@ -93,10 +84,10 @@ class Score extends Extension {
global $database;
// TODO: update if already voted
$database->Execute(
"INSERT INTO images_score_votes(image_id, user_id, score) VALUES(?, ?, ?)",
"INSERT INTO numeric_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=?",
"UPDATE images SET numeric_score=(SELECT SUM(score) FROM numeric_score_votes WHERE image_id=?) WHERE id=?",
array($image_id, $image_id));
}
}

View file

@ -1,31 +1,6 @@
<?php
class ScoreTheme extends Themelet {
public function display_scorer($page, $image_id, $score) {
$i_image_id = int_escape($image_id);
$words = array();
$words[-2] = "Delete";
$words[-1] = "Bad";
$words[ 0] = "Ok";
$words[ 1] = "Good";
$words[ 2] = "Favourite";
$s_score = $words[$score];
$html = "
Current score is \"$s_score\"
<br><form action='".make_link("score/set")."' method='POST'>
<input type='hidden' name='image_id' value='$i_image_id' />
<input type='radio' name='score' value='-2' id='-2'><label for='-2'>Delete</label>
<input type='radio' name='score' value='-1' id='-1'><label for='-1'>Bad</label>
<input type='radio' name='score' value='0' id='0' ><label for='0' >Ok</label>
<input type='radio' name='score' value='1' id='1' ><label for='1' >Good</label>
<input type='radio' name='score' value='2' id='2' ><label for='2' >Favourite</label>
<input type='submit' value='Vote' />
</form>
";
$page->add_block(new Block(null, $html, "main", 7));
}
class NumericScoreTheme extends Themelet {
public function display_voter($page, $image_id, $score) {
$i_image_id = int_escape($image_id);
$i_score = int_escape($score) / 2;
@ -33,12 +8,12 @@ class ScoreTheme extends Themelet {
$html = "
Current score is $i_score
<br>
<form action='".make_link("score/set")."' method='POST'>
<form action='".make_link("numeric_score/vote")."' method='POST'>
<input type='hidden' name='image_id' value='$i_image_id' />
<input type='hidden' name='score' value='-2'>
<input type='submit' value='Vote Down' />
</form>
<form action='".make_link("score/set")."' method='POST'>
<form action='".make_link("numeric_score/vote")."' method='POST'>
<input type='hidden' name='image_id' value='$i_image_id' />
<input type='hidden' name='score' value='2'>
<input type='submit' value='Vote Up' />

View file

@ -34,8 +34,8 @@ class Score extends Extension {
$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() &&
if(is_a($event, 'PageRequestEvent') && $event->page_name == "score_text" &&
$event->get_arg(0) == "vote" && $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']);
@ -53,7 +53,6 @@ class Score extends Extension {
}
if(is_a($event, 'DisplayingImageEvent')) {
// TODO: scorer vs voter
$this->theme->display_scorer($event->page, $event->image->id, $event->image->vote_score);
}
@ -87,16 +86,21 @@ class Score extends Extension {
$database->Execute("ALTER TABLE images CHANGE score vote_score INTEGER NOT NULL DEFAULT 0");
$config->set_int("ext_score_version", 2);
}
if($config->get_int("ext_score_version") < 3) {
$database->Execute("ALTER TABLE images CHANGE vote_score score_text INTEGER NOT NULL DEFAULT 0");
$database->Execute("RENAME TABLE images_score_votes TO votes_text");
$config->set_int("ext_score_version", 3);
}
}
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(?, ?, ?)",
"INSERT INTO votes_text(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=?",
"UPDATE images SET score_text=(SELECT AVG(score) FROM votes_text WHERE image_id=?) WHERE id=?",
array($image_id, $image_id));
}
}

View file

@ -13,7 +13,7 @@ class ScoreTheme extends Themelet {
$s_score = $words[$score];
$html = "
Current score is \"$s_score\"
<br><form action='".make_link("score/set")."' method='POST'>
<br><form action='".make_link("score_text/vote")."' method='POST'>
<input type='hidden' name='image_id' value='$i_image_id' />
<input type='radio' name='score' value='-2' id='-2'><label for='-2'>Delete</label>
<input type='radio' name='score' value='-1' id='-1'><label for='-1'>Bad</label>
@ -25,27 +25,6 @@ class ScoreTheme extends Themelet {
";
$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
<br>
<form action='".make_link("score/set")."' method='POST'>
<input type='hidden' name='image_id' value='$i_image_id' />
<input type='hidden' name='score' value='-2'>
<input type='submit' value='Vote Down' />
</form>
<form action='".make_link("score/set")."' method='POST'>
<input type='hidden' name='image_id' value='$i_image_id' />
<input type='hidden' name='score' value='2'>
<input type='submit' value='Vote Up' />
</form>
";
$page->add_block(new Block(null, $html, "main", 7));
}
}
?>