qa-layer-voters-flaggers.php 5.93 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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	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
22 23
class qa_html_theme_layer extends qa_html_theme_base
{
24 25
	private $qa_voters_flaggers_queue = array();
	private $qa_voters_flaggers_cache = array();
Scott committed
26 27


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

Scott committed
30 31 32
	public function main()
	{
		foreach ($this->content as $key => $part) {
33 34 35
			if (strpos($key, 'q_list') === 0) {
				if (isset($part['qs']))
					$this->queue_raw_posts_voters_flaggers($part['qs']);
Scott committed
36

37
			} elseif (strpos($key, 'q_view') === 0) {
Scott committed
38 39
				$this->queue_post_voters_flaggers($part['raw']);
				$this->queue_raw_posts_voters_flaggers($part['c_list']['cs']);
Scott committed
40

41
			} elseif (strpos($key, 'a_list') === 0) {
Scott committed
42 43
				if (!empty($part)) {
					$this->queue_raw_posts_voters_flaggers($part['as']);
Scott committed
44

45 46 47 48
					foreach ($part['as'] as $a_item) {
						if (isset($a_item['c_list']['cs']))
							$this->queue_raw_posts_voters_flaggers($a_item['c_list']['cs']);
					}
Scott committed
49 50 51 52
				}
			}
		}

53
		parent::main();
Scott committed
54
	}
Scott committed
55 56


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

Scott committed
59 60 61
	public function q_list_items($q_items)
	{
		$this->queue_raw_posts_voters_flaggers($q_items);
Scott committed
62

63
		parent::q_list_items($q_items);
Scott committed
64
	}
Scott committed
65

Scott committed
66 67 68
	public function a_list_items($a_items)
	{
		$this->queue_raw_posts_voters_flaggers($a_items);
Scott committed
69

70
		parent::a_list_items($a_items);
Scott committed
71
	}
Scott committed
72

Scott committed
73 74 75
	public function c_list_items($c_items)
	{
		$this->queue_raw_posts_voters_flaggers($c_items);
Scott committed
76

77
		parent::c_list_items($c_items);
Scott committed
78
	}
Scott committed
79 80


Scott committed
81
	// Actual output of the voters and flaggers
Scott committed
82

Scott committed
83 84
	public function vote_count($post)
	{
85 86
		$postid = isset($post['vote_opostid']) && $post['vote_opostid'] ? $post['raw']['opostid'] : $post['raw']['postid'];
		$votersflaggers = $this->get_post_voters_flaggers($post['raw'], $postid);
Scott committed
87

Scott committed
88
		if (isset($votersflaggers)) {
89 90
			$uphandles = array();
			$downhandles = array();
Scott committed
91

Scott committed
92
			foreach ($votersflaggers as $voterflagger) {
93 94 95 96 97 98 99 100
				if ($voterflagger['vote'] != 0) {
					$newflagger = qa_html($voterflagger['handle']);
					if ($voterflagger['vote'] > 0)
						$uphandles[] = $newflagger;
					else  // if ($voterflagger['vote'] < 0)
						$downhandles[] = $newflagger;
				}
			}
Scott committed
101

102 103 104 105
			$tooltip = trim(
				(empty($uphandles) ? '' : '&uarr; ' . implode(', ', $uphandles)) . "\n\n" .
				(empty($downhandles) ? '' : '&darr; ' . implode(', ', $downhandles))
			);
Scott committed
106

107
			$post['vote_count_tags'] = sprintf('%s title="%s"', isset($post['vote_count_tags']) ? $post['vote_count_tags'] : '', $tooltip);
Scott committed
108 109
		}

110
		parent::vote_count($post);
Scott committed
111
	}
Scott committed
112

Scott committed
113 114
	public function post_meta_flags($post, $class)
	{
115 116 117 118
		if (isset($post['raw']['opostid']))
			$postid = $post['raw']['opostid'];
		elseif (isset($post['raw']['postid']))
			$postid = $post['raw']['postid'];
Scott committed
119

120
		$flaggers = array();
Scott committed
121

Scott committed
122
		if (isset($postid)) {
123
			$votersflaggers = $this->get_post_voters_flaggers($post, $postid);
Scott committed
124

125 126
			if (isset($votersflaggers)) {
				foreach ($votersflaggers as $voterflagger) {
127
					if ($voterflagger['flag'] > 0)
128
						$flaggers[] = qa_html($voterflagger['handle']);
129 130
				}
			}
Scott committed
131 132
		}

133 134
		if (!empty($flaggers))
			$this->output('<span title="&#9873; ' . implode(', ', $flaggers) . '">');
Scott committed
135

136
		parent::post_meta_flags($post, $class);
Scott committed
137

138
		if (!empty($flaggers))
Scott committed
139 140
			$this->output('</span>');
	}
141 142


Scott committed
143
	// Utility functions for this layer
144

145
	private function queue_post_voters_flaggers($post)
146 147
	{
		if (!qa_user_post_permit_error('permit_view_voters_flaggers', $post)) {
148 149 150 151
			$postkeys = array('postid', 'opostid');
			foreach ($postkeys as $key) {
				if (isset($post[$key]) && !isset($this->qa_voters_flaggers_cache[$post[$key]]))
					$this->qa_voters_flaggers_queue[$post[$key]] = true;
152 153 154 155
			}
		}
	}

156
	private function queue_raw_posts_voters_flaggers($posts)
157 158 159 160 161 162 163 164 165
	{
		if (is_array($posts)) {
			foreach ($posts as $post) {
				if (isset($post['raw']))
					$this->queue_post_voters_flaggers($post['raw']);
			}
		}
	}

166
	private function retrieve_queued_voters_flaggers()
167 168
	{
		if (count($this->qa_voters_flaggers_queue)) {
169
			require_once QA_INCLUDE_DIR . 'db/votes.php';
170

171
			$postids = array_keys($this->qa_voters_flaggers_queue);
172

Scott committed
173
			foreach ($postids as $postid) {
174
				$this->qa_voters_flaggers_cache[$postid] = array();
Scott committed
175
			}
176

177
			$newvotersflaggers = qa_db_uservoteflag_posts_get($postids);
178 179

			if (QA_FINAL_EXTERNAL_USERS) {
180
				$keyuserids = array();
Scott committed
181
				foreach ($newvotersflaggers as $voterflagger) {
182
					$keyuserids[$voterflagger['userid']] = true;
Scott committed
183
				}
184

185
				$useridhandles = qa_get_public_from_userids(array_keys($keyuserids));
Scott committed
186
				foreach ($newvotersflaggers as $index => $voterflagger) {
187
					$newvotersflaggers[$index]['handle'] = isset($useridhandles[$voterflagger['userid']]) ? $useridhandles[$voterflagger['userid']] : null;
Scott committed
188
				}
189 190
			}

Scott committed
191
			foreach ($newvotersflaggers as $voterflagger) {
192
				$this->qa_voters_flaggers_cache[$voterflagger['postid']][] = $voterflagger;
Scott committed
193
			}
194

195
			$this->qa_voters_flaggers_queue = array();
196 197 198
		}
	}

199
	private function get_post_voters_flaggers($post, $postid)
200
	{
201
		require_once QA_INCLUDE_DIR . 'util/sort.php';
202 203 204 205 206 207

		if (!isset($this->qa_voters_flaggers_cache[$postid])) {
			$this->queue_post_voters_flaggers($post);
			$this->retrieve_queued_voters_flaggers();
		}

208
		$votersflaggers = isset($this->qa_voters_flaggers_cache[$postid]) ? $this->qa_voters_flaggers_cache[$postid] : null;
209 210 211 212 213 214

		if (isset($votersflaggers))
			qa_sort_by($votersflaggers, 'handle');

		return $votersflaggers;
	}
Scott committed
215
}