qa-layer-voters-flaggers.php 6.01 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-layer-voters-flaggers.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27
	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
*/

	class qa_html_theme_layer extends qa_html_theme_base {
Scott Vivian committed
28

29 30
		private $qa_voters_flaggers_queue=array();
		private $qa_voters_flaggers_cache=array();
Scott Vivian committed
31

Gideon Greenspan committed
32 33

	//	Utility functions for this layer
Scott Vivian committed
34

35
		private function queue_post_voters_flaggers($post)
Gideon Greenspan committed
36 37 38
		{
			if (!qa_user_post_permit_error('permit_view_voters_flaggers', $post)) {
				$postids=array(@$post['postid'], @$post['opostid']); // opostid can be relevant for flags
Scott Vivian committed
39

Gideon Greenspan committed
40 41 42 43 44
				foreach ($postids as $postid)
					if (isset($postid) && !isset($this->qa_voters_flaggers_cache[$postid]))
						$this->qa_voters_flaggers_queue[$postid]=true;
			}
		}
Scott Vivian committed
45

46
		private function queue_raw_posts_voters_flaggers($posts)
Gideon Greenspan committed
47 48 49 50 51 52
		{
			if (is_array($posts))
				foreach ($posts as $post)
					if (isset($post['raw']))
						$this->queue_post_voters_flaggers($post['raw']);
		}
Scott Vivian committed
53

54
		private function retrieve_queued_voters_flaggers()
Gideon Greenspan committed
55 56 57
		{
			if (count($this->qa_voters_flaggers_queue)) {
				require_once QA_INCLUDE_DIR.'qa-db-votes.php';
Scott Vivian committed
58

Gideon Greenspan committed
59
				$postids=array_keys($this->qa_voters_flaggers_queue);
Scott Vivian committed
60

Gideon Greenspan committed
61 62
				foreach ($postids as $postid)
					$this->qa_voters_flaggers_cache[$postid]=array();
Scott Vivian committed
63

Gideon Greenspan committed
64
				$newvotersflaggers=qa_db_uservoteflag_posts_get($postids);
Scott Vivian committed
65

Gideon Greenspan committed
66 67 68 69
				if (QA_FINAL_EXTERNAL_USERS) {
					$keyuserids=array();
					foreach ($newvotersflaggers as $voterflagger)
						$keyuserids[$voterflagger['userid']]=true;
Scott Vivian committed
70

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

Gideon Greenspan committed
76 77
				foreach ($newvotersflaggers as $voterflagger)
					$this->qa_voters_flaggers_cache[$voterflagger['postid']][]=$voterflagger;
Scott Vivian committed
78

Gideon Greenspan committed
79 80 81
				$this->qa_voters_flaggers_queue=array();
			}
		}
Scott Vivian committed
82

83
		private function get_post_voters_flaggers($post, $postid)
Gideon Greenspan committed
84 85
		{
			require_once QA_INCLUDE_DIR.'qa-util-sort.php';
Scott Vivian committed
86

Gideon Greenspan committed
87 88 89 90
			if (!isset($this->qa_voters_flaggers_cache[$postid])) {
				$this->queue_post_voters_flaggers($post);
				$this->retrieve_queued_voters_flaggers();
			}
Scott Vivian committed
91

Gideon Greenspan committed
92
			$votersflaggers=@$this->qa_voters_flaggers_cache[$postid];
Scott Vivian committed
93

Gideon Greenspan committed
94 95
			if (isset($votersflaggers))
				qa_sort_by($votersflaggers, 'handle');
Scott Vivian committed
96

Gideon Greenspan committed
97 98
			return $votersflaggers;
		}
Scott Vivian committed
99

Gideon Greenspan committed
100 101

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

103
		public function main()
Gideon Greenspan committed
104 105 106 107
		{
			foreach ($this->content as $key => $part) {
				if (strpos($key, 'q_list')===0)
					$this->queue_raw_posts_voters_flaggers(@$part['qs']);
Scott Vivian committed
108

Gideon Greenspan committed
109 110 111
				elseif (strpos($key, 'q_view')===0) {
					$this->queue_post_voters_flaggers($part['raw']);
					$this->queue_raw_posts_voters_flaggers($part['c_list']['cs']);
Scott Vivian committed
112 113

				} elseif (strpos($key, 'a_list')===0) {
Gideon Greenspan committed
114 115 116 117 118 119 120 121
					if (!empty($part)) {
						$this->queue_raw_posts_voters_flaggers($part['as']);

						foreach ($part['as'] as $a_item)
							$this->queue_raw_posts_voters_flaggers(@$a_item['c_list']['cs']);
					}
				}
			}
Scott Vivian committed
122

Gideon Greenspan committed
123 124
			qa_html_theme_base::main();
		}
Scott Vivian committed
125 126


Gideon Greenspan committed
127
	//	Other functions which also collect up required postids for lists to save DB queries - helps with widget output and Ajax calls
Scott Vivian committed
128

129
		public function q_list_items($q_items)
Gideon Greenspan committed
130 131
		{
			$this->queue_raw_posts_voters_flaggers($q_items);
Scott Vivian committed
132

Gideon Greenspan committed
133 134
			qa_html_theme_base::q_list_items($q_items);
		}
Scott Vivian committed
135

136
		public function a_list_items($a_items)
Gideon Greenspan committed
137 138
		{
			$this->queue_raw_posts_voters_flaggers($a_items);
Scott Vivian committed
139

Gideon Greenspan committed
140 141 142
			qa_html_theme_base::a_list_items($a_items);
		}

143
		public function c_list_items($c_items)
Gideon Greenspan committed
144 145
		{
			$this->queue_raw_posts_voters_flaggers($c_items);
Scott Vivian committed
146

Gideon Greenspan committed
147 148
			qa_html_theme_base::c_list_items($c_items);
		}
Scott Vivian committed
149 150


Gideon Greenspan committed
151
	//	Actual output of the voters and flaggers
Scott Vivian committed
152

153
		public function vote_count($post)
Gideon Greenspan committed
154
		{
Gideon Greenspan committed
155
			$votersflaggers=$this->get_post_voters_flaggers($post['raw'], @$post['vote_opostid'] ? $post['raw']['opostid'] : $post['raw']['postid']);
Scott Vivian committed
156

Gideon Greenspan committed
157
			$tooltip='';
Scott Vivian committed
158

Gideon Greenspan committed
159 160 161
			if (isset($votersflaggers)) {
				$uphandles='';
				$downhandles='';
Scott Vivian committed
162

Gideon Greenspan committed
163 164 165
				foreach ($votersflaggers as $voterflagger) {
					if ($voterflagger['vote']>0)
						$uphandles.=(strlen($uphandles) ? ', ' : '').qa_html($voterflagger['handle']);
Scott Vivian committed
166

Gideon Greenspan committed
167 168
					if ($voterflagger['vote']<0)
						$downhandles.=(strlen($downhandles) ? ', ' : '').qa_html($voterflagger['handle']);
Scott Vivian committed
169

Gideon Greenspan committed
170 171 172
					$tooltip=trim((strlen($uphandles) ? ('&uarr; '.$uphandles) : '')."\n\n".(strlen($downhandles) ? ('&darr; '.$downhandles) : ''));
				}
			}
Scott Vivian committed
173

Gideon Greenspan committed
174
			$post['vote_count_tags']=@$post['vote_count_tags'].' title="'.$tooltip.'"';
Scott Vivian committed
175

Gideon Greenspan committed
176 177
			qa_html_theme_base::vote_count($post);
		}
Scott Vivian committed
178 179


180
		public function post_meta_flags($post, $class)
Gideon Greenspan committed
181 182 183 184
		{
			$postid=@$post['raw']['opostid'];
			if (!isset($postid))
				$postid=@$post['raw']['postid'];
Scott Vivian committed
185

Gideon Greenspan committed
186
			$tooltip='';
Scott Vivian committed
187

Gideon Greenspan committed
188 189
			if (isset($postid)) {
				$votersflaggers=$this->get_post_voters_flaggers($post, $postid);
Scott Vivian committed
190

Gideon Greenspan committed
191 192 193 194 195
				if (isset($votersflaggers))
					foreach ($votersflaggers as $voterflagger)
						if ($voterflagger['flag']>0)
							$tooltip.=(strlen($tooltip) ? ', ' : '').qa_html($voterflagger['handle']);
			}
Scott Vivian committed
196

Gideon Greenspan committed
197
			if (strlen($tooltip))
Gideon Greenspan committed
198
				$this->output('<span title="&#9873; '.$tooltip.'">');
Scott Vivian committed
199

Gideon Greenspan committed
200
			qa_html_theme_base::post_meta_flags($post, $class);
Scott Vivian committed
201

Gideon Greenspan committed
202
			if (strlen($tooltip))
Gideon Greenspan committed
203
				$this->output('</span>');
Gideon Greenspan committed
204 205 206
		}

	}
Scott Vivian committed
207

Gideon Greenspan committed
208 209 210 211

/*
	Omit PHP closing tag to help avoid accidental output
*/