qa-layer-voters-flaggers.php 5.57 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-layer-voters-flaggers.php
	Description: Theme layer class for viewing voters and flaggers


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
23 24 25 26
class qa_html_theme_layer extends qa_html_theme_base
{
	private $qa_voters_flaggers_queue=array();
	private $qa_voters_flaggers_cache=array();
Scott committed
27 28


Scott committed
29
//	Utility functions for this layer
Scott committed
30

Scott committed
31 32 33 34
	private function queue_post_voters_flaggers($post)
	{
		if (!qa_user_post_permit_error('permit_view_voters_flaggers', $post)) {
			$postids=array(@$post['postid'], @$post['opostid']); // opostid can be relevant for flags
Scott committed
35

Scott committed
36 37 38
			foreach ($postids as $postid)
				if (isset($postid) && !isset($this->qa_voters_flaggers_cache[$postid]))
					$this->qa_voters_flaggers_queue[$postid]=true;
Scott committed
39
		}
Scott committed
40
	}
Scott committed
41

Scott committed
42 43 44 45 46 47 48
	private function queue_raw_posts_voters_flaggers($posts)
	{
		if (is_array($posts))
			foreach ($posts as $post)
				if (isset($post['raw']))
					$this->queue_post_voters_flaggers($post['raw']);
	}
Scott committed
49

Scott committed
50 51 52 53
	private function retrieve_queued_voters_flaggers()
	{
		if (count($this->qa_voters_flaggers_queue)) {
			require_once QA_INCLUDE_DIR.'db/votes.php';
Scott committed
54

Scott committed
55
			$postids=array_keys($this->qa_voters_flaggers_queue);
Scott committed
56

Scott committed
57 58
			foreach ($postids as $postid)
				$this->qa_voters_flaggers_cache[$postid]=array();
Scott committed
59

Scott committed
60
			$newvotersflaggers=qa_db_uservoteflag_posts_get($postids);
Scott committed
61

Scott committed
62 63
			if (QA_FINAL_EXTERNAL_USERS) {
				$keyuserids=array();
Scott committed
64
				foreach ($newvotersflaggers as $voterflagger)
Scott committed
65
					$keyuserids[$voterflagger['userid']]=true;
Scott committed
66

Scott committed
67 68 69
				$useridhandles=qa_get_public_from_userids(array_keys($keyuserids));
				foreach ($newvotersflaggers as $index => $voterflagger)
					$newvotersflaggers[$index]['handle']=@$useridhandles[$voterflagger['userid']];
Scott committed
70
			}
Scott committed
71 72 73 74 75

			foreach ($newvotersflaggers as $voterflagger)
				$this->qa_voters_flaggers_cache[$voterflagger['postid']][]=$voterflagger;

			$this->qa_voters_flaggers_queue=array();
Scott committed
76
		}
Scott committed
77
	}
Scott committed
78

Scott committed
79 80
	private function get_post_voters_flaggers($post, $postid)
	{
Scott committed
81
		require_once QA_INCLUDE_DIR.'util/sort.php';
Scott committed
82

Scott committed
83 84 85 86
		if (!isset($this->qa_voters_flaggers_cache[$postid])) {
			$this->queue_post_voters_flaggers($post);
			$this->retrieve_queued_voters_flaggers();
		}
Scott committed
87

Scott committed
88
		$votersflaggers=@$this->qa_voters_flaggers_cache[$postid];
Scott committed
89

Scott committed
90 91
		if (isset($votersflaggers))
			qa_sort_by($votersflaggers, 'handle');
Scott committed
92

Scott committed
93 94
		return $votersflaggers;
	}
Scott committed
95 96


Scott committed
97
//	Collect up all required postids for the entire page to save DB queries - common case where whole page output
Scott committed
98

Scott committed
99 100 101 102 103
	public function main()
	{
		foreach ($this->content as $key => $part) {
			if (strpos($key, 'q_list')===0)
				$this->queue_raw_posts_voters_flaggers(@$part['qs']);
Scott committed
104

Scott committed
105 106 107
			elseif (strpos($key, 'q_view')===0) {
				$this->queue_post_voters_flaggers($part['raw']);
				$this->queue_raw_posts_voters_flaggers($part['c_list']['cs']);
Scott committed
108

Scott committed
109 110 111
			} elseif (strpos($key, 'a_list')===0) {
				if (!empty($part)) {
					$this->queue_raw_posts_voters_flaggers($part['as']);
Scott committed
112

Scott committed
113 114
					foreach ($part['as'] as $a_item)
						$this->queue_raw_posts_voters_flaggers(@$a_item['c_list']['cs']);
Scott committed
115 116 117 118
				}
			}
		}

Scott committed
119 120
		qa_html_theme_base::main();
	}
Scott committed
121 122


Scott committed
123
//	Other functions which also collect up required postids for lists to save DB queries - helps with widget output and Ajax calls
Scott committed
124

Scott committed
125 126 127
	public function q_list_items($q_items)
	{
		$this->queue_raw_posts_voters_flaggers($q_items);
Scott committed
128

Scott committed
129 130
		qa_html_theme_base::q_list_items($q_items);
	}
Scott committed
131

Scott committed
132 133 134
	public function a_list_items($a_items)
	{
		$this->queue_raw_posts_voters_flaggers($a_items);
Scott committed
135

Scott committed
136 137
		qa_html_theme_base::a_list_items($a_items);
	}
Scott committed
138

Scott committed
139 140 141
	public function c_list_items($c_items)
	{
		$this->queue_raw_posts_voters_flaggers($c_items);
Scott committed
142

Scott committed
143 144
		qa_html_theme_base::c_list_items($c_items);
	}
Scott committed
145 146


Scott committed
147
//	Actual output of the voters and flaggers
Scott committed
148

Scott committed
149 150 151
	public function vote_count($post)
	{
		$votersflaggers=$this->get_post_voters_flaggers($post['raw'], @$post['vote_opostid'] ? $post['raw']['opostid'] : $post['raw']['postid']);
Scott committed
152

Scott committed
153
		$tooltip='';
Scott committed
154

Scott committed
155 156 157
		if (isset($votersflaggers)) {
			$uphandles='';
			$downhandles='';
Scott committed
158

Scott committed
159 160 161
			foreach ($votersflaggers as $voterflagger) {
				if ($voterflagger['vote']>0)
					$uphandles.=(strlen($uphandles) ? ', ' : '').qa_html($voterflagger['handle']);
Scott committed
162

Scott committed
163 164
				if ($voterflagger['vote']<0)
					$downhandles.=(strlen($downhandles) ? ', ' : '').qa_html($voterflagger['handle']);
Scott committed
165

Scott committed
166 167
				$tooltip=trim((strlen($uphandles) ? ('&uarr; '.$uphandles) : '')."\n\n".(strlen($downhandles) ? ('&darr; '.$downhandles) : ''));
			}
Scott committed
168 169
		}

Scott committed
170
		$post['vote_count_tags']=@$post['vote_count_tags'].' title="'.$tooltip.'"';
Scott committed
171

Scott committed
172 173
		qa_html_theme_base::vote_count($post);
	}
Scott committed
174

Scott committed
175 176 177 178 179
	public function post_meta_flags($post, $class)
	{
		$postid=@$post['raw']['opostid'];
		if (!isset($postid))
			$postid=@$post['raw']['postid'];
Scott committed
180

Scott committed
181
		$tooltip='';
Scott committed
182

Scott committed
183 184
		if (isset($postid)) {
			$votersflaggers=$this->get_post_voters_flaggers($post, $postid);
Scott committed
185

Scott committed
186 187 188 189
			if (isset($votersflaggers))
				foreach ($votersflaggers as $voterflagger)
					if ($voterflagger['flag']>0)
						$tooltip.=(strlen($tooltip) ? ', ' : '').qa_html($voterflagger['handle']);
Scott committed
190 191
		}

Scott committed
192 193
		if (strlen($tooltip))
			$this->output('<span title="&#9873; '.$tooltip.'">');
Scott committed
194

Scott committed
195
		qa_html_theme_base::post_meta_flags($post, $class);
Scott committed
196

Scott committed
197 198 199 200
		if (strlen($tooltip))
			$this->output('</span>');
	}
}