qa-layer-voters-flaggers.php 6.51 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
class qa_html_theme_layer extends qa_html_theme_base
{
25 26
	private $qa_voters_flaggers_queue = array();
	private $qa_voters_flaggers_cache = array();
Scott committed
27 28


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

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

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

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

46 47 48 49
					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
50 51 52 53
				}
			}
		}

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


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

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

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

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

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

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

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


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

Scott committed
84 85
	public function vote_count($post)
	{
86 87
		$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
88

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

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

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

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

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

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

121
		$flaggers = array();
Scott committed
122

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

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

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

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

139
		if (!empty($flaggers))
Scott committed
140 141
			$this->output('</span>');
	}
142 143 144 145 146 147 148 149 150 151 152


//	Utility functions for this layer

	/**
	 * @deprecated This function will become private in Q2A 1.8. It is specific to this plugin and
	 * should not be used by outside code.
	 */
	public function queue_post_voters_flaggers($post)
	{
		if (!qa_user_post_permit_error('permit_view_voters_flaggers', $post)) {
153 154 155 156
			$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;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
			}
		}
	}

	/**
	 * @deprecated This function will become private in Q2A 1.8. It is specific to this plugin and
	 * should not be used by outside code.
	 */
	public 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']);
			}
		}
	}

	/**
	 * @deprecated This function will become private in Q2A 1.8. It is specific to this plugin and
	 * should not be used by outside code.
	 */
	public function retrieve_queued_voters_flaggers()
	{
		if (count($this->qa_voters_flaggers_queue)) {
182
			require_once QA_INCLUDE_DIR . 'db/votes.php';
183

184
			$postids = array_keys($this->qa_voters_flaggers_queue);
185 186

			foreach ($postids as $postid)
187
				$this->qa_voters_flaggers_cache[$postid] = array();
188

189
			$newvotersflaggers = qa_db_uservoteflag_posts_get($postids);
190 191

			if (QA_FINAL_EXTERNAL_USERS) {
192
				$keyuserids = array();
193
				foreach ($newvotersflaggers as $voterflagger)
194
					$keyuserids[$voterflagger['userid']] = true;
195

196
				$useridhandles = qa_get_public_from_userids(array_keys($keyuserids));
197
				foreach ($newvotersflaggers as $index => $voterflagger)
198
					$newvotersflaggers[$index]['handle'] = isset($useridhandles[$voterflagger['userid']]) ? $useridhandles[$voterflagger['userid']] : null;
199 200 201
			}

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

204
			$this->qa_voters_flaggers_queue = array();
205 206 207 208 209 210 211 212 213
		}
	}

	/**
	 * @deprecated This function will become private in Q2A 1.8. It is specific to this plugin and
	 * should not be used by outside code.
	 */
	public function get_post_voters_flaggers($post, $postid)
	{
214
		require_once QA_INCLUDE_DIR . 'util/sort.php';
215 216 217 218 219 220

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

221
		$votersflaggers = isset($this->qa_voters_flaggers_cache[$postid]) ? $this->qa_voters_flaggers_cache[$postid] : null;
222 223 224 225 226 227

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

		return $votersflaggers;
	}
Scott committed
228
}