Score extension
git-svn-id: file:///home/shish/svn/shimmie2/trunk@527 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
		
							parent
							
								
									a7ea9bb397
								
							
						
					
					
						commit
						46ba77fa67
					
				| @ -8,124 +8,77 @@ | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| class Score extends Extension { | class Score extends Extension { | ||||||
| // event handler {{{
 | 	var $theme; | ||||||
|  | 
 | ||||||
| 	public function receive_event($event) { | 	public function receive_event($event) { | ||||||
|  | 		if(is_null($this->theme)) $this->theme = get_theme_object("score", "ScoreTheme"); | ||||||
|  | 
 | ||||||
| 		if(is_a($event, 'InitExtEvent')) { | 		if(is_a($event, 'InitExtEvent')) { | ||||||
| 			global $config; | 			global $config; | ||||||
| 			if($config->get_int("ext_ratings_version") < 1) { | 			if($config->get_int("ext_score_version", 0) < 1) { | ||||||
| 				$this->install(); | 				$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" && | ||||||
| 		if(is_a($event, 'ImageDeletionEvent')) { | 				$event->get_arg(0) == "set" && $event->user->is_admin() && | ||||||
| 			$this->delete_comments($event->image->id); | 				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) { | ||||||
|  | 				$this->add_vote($i_image_id, $event->user->id, $i_score); | ||||||
| 			} | 			} | ||||||
| 		if(is_a($event, 'CommentDeletionEvent')) { | 			 | ||||||
| 			$this->delete_comment($event->comment_id); | 			$event->page->set_mode("redirect"); | ||||||
|  | 			$event->page->set_redirect(make_link("post/view/$i_image_id")); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if(is_a($event, 'DisplayingImageEvent')) { | ||||||
|  | 			$this->theme->display_rater($event->page, $event->image->id, $event->image->score); | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		if(is_a($event, 'SetupBuildingEvent')) { | 		if(is_a($event, 'SetupBuildingEvent')) { | ||||||
| 			$sb = new SetupBlock("Comment Options"); | 			/* | ||||||
| 			$sb->add_label("Allow anonymous comments "); | 			TODO: disable anon voting | ||||||
| 			$sb->add_bool_option("comment_anon"); |  | ||||||
| 			$sb->add_label("<br>Limit to "); |  | ||||||
| 			$sb->add_int_option("comment_limit", 1, 60); |  | ||||||
| 			$sb->add_label(" comments per "); |  | ||||||
| 			$sb->add_int_option("comment_window", 1, 60); |  | ||||||
| 			$sb->add_label(" minutes"); |  | ||||||
| 			$sb->add_label("<br>Show "); |  | ||||||
| 			$sb->add_int_option("comment_count", 0, 100); |  | ||||||
| 			$sb->add_label(" recent comments on the index"); |  | ||||||
| 			$event->panel->add_block($sb); |  | ||||||
| 		} |  | ||||||
| 			*/ | 			*/ | ||||||
| 		} | 		} | ||||||
| 
 |  | ||||||
| 	private function can_comment() { |  | ||||||
| 		global $config, $user; |  | ||||||
| 		return $config->get_bool("rate_anon") || ($user->id != $config->get_int("anon_id")); |  | ||||||
| 	} | 	} | ||||||
| // }}} 
 | 
 | ||||||
| // installer {{{
 | 	private function install() { | ||||||
| 	protected function install() { |  | ||||||
| 		global $database; | 		global $database; | ||||||
| 		global $config; | 		global $config; | ||||||
| 		$database->Execute("CREATE TABLE `image_voters` (
 |  | ||||||
| 			`image_id` int(11) NOT NULL, |  | ||||||
| 			`user_id` int(11) NOT NULL, |  | ||||||
| 			`vote` tinyint(4) NOT NULL, |  | ||||||
| 			`voted` datetime NOT NULL, |  | ||||||
| 			PRIMARY KEY  (`image_id`,`user_id`) |  | ||||||
| 		)");
 |  | ||||||
| 		$config->set_int("ext_ratings_version", 1); |  | ||||||
| 	} |  | ||||||
| // }}}
 |  | ||||||
| // page building {{{
 |  | ||||||
| 
 | 
 | ||||||
| 	private function build_image_rating($image_id) { | 		if($config->get_int("ext_score_version") < 1) { | ||||||
| 		global $config; | 			$database->Execute("ALTER TABLE images ADD COLUMN score INTEGER NOT NULL DEFAULT 0"); | ||||||
| 		$i_image_id = int_escape($image_id); | 			$database->Execute("CREATE INDEX images__score ON images(score)"); | ||||||
| 		return $this->query_to_html(" | 			$database->Execute(" | ||||||
| 				SELECT | 				CREATE TABLE images_score_votes ( | ||||||
| 				users.id as user_id, users.name as user_name, | 					image_id INTEGER NOT NULL, | ||||||
| 				comments.comment as comment, comments.id as comment_id, | 					user_id INTEGER NOT NULL, | ||||||
| 				comments.image_id as image_id, comments.owner_ip as poster_ip | 					score INTEGER NOT NULL, | ||||||
| 				FROM comments | 					UNIQUE(image_id, user_id), | ||||||
| 				LEFT JOIN users ON comments.owner_id=users.id | 					INDEX(image_id) | ||||||
| 				WHERE comments.image_id=? | 				) | ||||||
| 				ORDER BY comments.id ASC | 			");
 | ||||||
| 				LIMIT ? | 			$config->set_int("ext_score_version", 1); | ||||||
| 		", array($i_image_id, $config->get_int('recent_count')));
 | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private function build_rater($image_id) { | 	private function add_vote($image_id, $user_id, $score) { | ||||||
| 		if($this->can_comment()) { |  | ||||||
| 			$i_image_id = int_escape($image_id); |  | ||||||
| 			return " |  | ||||||
| 				<form action='".make_link("rating/vote_up")."' method='POST'> |  | ||||||
| 				<input type='hidden' name='image_id' value='$i_image_id' /> |  | ||||||
| 				<input type='submit' value='Vote Up' /> |  | ||||||
| 				</form> |  | ||||||
| 				<form action='".make_link("rating/vote_down")."' method='POST'> |  | ||||||
| 				<input type='hidden' name='image_id' value='$i_image_id' /> |  | ||||||
| 				<input type='submit' value='Vote Down' /> |  | ||||||
| 				</form> |  | ||||||
| 			";
 |  | ||||||
| 		} |  | ||||||
| 		else { |  | ||||||
| 			return "You need to create an account before you can rate"; |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| // }}}
 |  | ||||||
| // add / remove / edit comments {{{
 |  | ||||||
| 	private function add_rating($image_id, $rating) { |  | ||||||
| 		global $user; |  | ||||||
| 		global $database; | 		global $database; | ||||||
| 		global $config; | 		// TODO: update if already voted
 | ||||||
| 		global $page; |  | ||||||
| 
 |  | ||||||
| 		$page->set_title("Error"); |  | ||||||
| 		$page->set_heading("Error"); |  | ||||||
| 		if(!$config->get_bool('rating_anon') && $user->is_anonymous()) { |  | ||||||
| 			$page->add_main_block(new Block("Permission Denied", "Anonymous rating has been disabled")); |  | ||||||
| 		} |  | ||||||
| 		else { |  | ||||||
| 			$i_rating = int_escape($rating); |  | ||||||
| 		$database->Execute( | 		$database->Execute( | ||||||
| 					"INSERT INTO image_ratings(image_id, user_id, rating, rated) ". | 			"INSERT INTO images_score_votes(image_id, user_id, score) VALUES(?, ?, ?)", | ||||||
| 					"VALUES(?, ?, ?, now())", | 			array($image_id, $user_id, $score)); | ||||||
| 					array($image_id, $user->id, $i_rating)); | 		$database->Execute( | ||||||
| 			$page->set_mode("redirect"); | 			"UPDATE images SET score=(SELECT AVG(score) FROM images_score_votes WHERE image_id=?) WHERE id=?", | ||||||
| 			$page->set_redirect(make_link("post/view/".int_escape($image_id))); | 			array($image_id, $image_id)); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 |  | ||||||
| 	private function delete_ratings($image_id) { |  | ||||||
| 		global $database; |  | ||||||
| 		$database->Execute("DELETE FROM image_voters WHERE image_id=?", array($image_id)); |  | ||||||
| 	} |  | ||||||
| // }}}
 |  | ||||||
| } |  | ||||||
| add_event_listener(new Score()); | add_event_listener(new Score()); | ||||||
| ?>
 | ?>
 | ||||||
|  | |||||||
							
								
								
									
										21
									
								
								contrib/score/theme.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								contrib/score/theme.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | class ScoreTheme extends Themelet { | ||||||
|  | 	public function display_rater($page, $image_id, $score) { | ||||||
|  | 		$i_image_id = int_escape($image_id); | ||||||
|  | 		$html = " | ||||||
|  | 			<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'>Awesome</label> | ||||||
|  | 				<input type='submit' value='Vote' /> | ||||||
|  | 			</form> | ||||||
|  | 		";
 | ||||||
|  | 		$page->add_block(new Block(null, $html, "main", 7)); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | ?>
 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user