UsersList.php 6.45 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	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
*/

19
namespace Q2A\Controllers\User;
Scott committed
20

21
use Q2A\Auth\NoPermissionException;
22
use Q2A\Middleware\Auth\InternalUsersOnly;
23
use Q2A\Middleware\Auth\MinimumUserLevel;
24

25
class UsersList extends \Q2A\Controllers\BaseController
Scott committed
26
{
27 28
	public function __construct()
	{
29 30 31 32 33
		require_once QA_INCLUDE_DIR . 'db/users.php';
		require_once QA_INCLUDE_DIR . 'db/selects.php';
		require_once QA_INCLUDE_DIR . 'app/users.php';
		require_once QA_INCLUDE_DIR . 'app/format.php';

34 35 36
		parent::__construct();

		$this->addMiddleware(new InternalUsersOnly(), array('newest', 'special', 'blocked'));
37
		$this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_MODERATOR), array('blocked'));
38 39
	}

40 41 42 43
	/**
	 * Display top users page (ordered by points)
	 * @return array $qa_content
	 */
Scott committed
44 45
	public function top()
	{
46
		// callables to fetch user data
47
		$fetchUsers = function ($start, $pageSize) {
48 49 50 51 52
			return array(
				qa_opt('cache_userpointscount'),
				qa_db_select_with_pending(qa_db_top_users_selectspec($start, $pageSize))
			);
		};
53
		$userScore = function ($user) {
54 55
			return qa_html(qa_format_number($user['points'], 0, true));
		};
Scott committed
56

57
		$qa_content = $this->rankedUsersContent($fetchUsers, $userScore);
Scott committed
58

59 60 61
		$qa_content['title'] = empty($qa_content['ranking']['items'])
			? qa_lang_html('main/no_active_users')
			: qa_lang_html('main/highest_users');
Scott committed
62

63
		$qa_content['ranking']['sort'] = 'points';
Scott committed
64 65 66 67

		return $qa_content;
	}

68 69
	/**
	 * Display newest users page
70
	 *
71
	 * @return array $qa_content
72
	 * @throws NoPermissionException
73
	 */
Scott committed
74 75
	public function newest()
	{
76
		// check we have permission to view this page (moderator or above)
Scott committed
77
		if (qa_user_permit_error('permit_view_new_users_page')) {
78
			throw new NoPermissionException();
Scott committed
79 80
		}

81
		// callables to fetch user data
82
		$fetchUsers = function ($start, $pageSize) {
83 84 85 86 87
			return array(
				qa_opt('cache_userpointscount'),
				qa_db_select_with_pending(qa_db_newest_users_selectspec($start, $pageSize))
			);
		};
88
		$userDate = function ($user) {
89 90 91
			$when = qa_when_to_html($user['created'], 7);
			return $when['data'];
		};
Scott committed
92

93
		$qa_content = $this->rankedUsersContent($fetchUsers, $userDate);
Scott committed
94

95 96 97
		$qa_content['title'] = empty($qa_content['ranking']['items'])
			? qa_lang_html('main/no_active_users')
			: qa_lang_html('main/newest_users');
Scott committed
98

99
		$qa_content['ranking']['sort'] = 'date';
Scott committed
100 101 102 103

		return $qa_content;
	}

104 105
	/**
	 * Display special users page (admins, moderators, etc)
106
	 *
107
	 * @return array $qa_content
108
	 * @throws NoPermissionException
109
	 */
Scott committed
110 111
	public function special()
	{
112
		// check we have permission to view this page (moderator or above)
Scott committed
113
		if (qa_user_permit_error('permit_view_special_users_page')) {
114
			throw new NoPermissionException();
Scott committed
115 116
		}

117
		// callables to fetch user data
118
		$fetchUsers = function ($start, $pageSize) {
119 120 121 122
			// here we fetch *all* users to get the total instead of a separate query; there are unlikely to be many special users
			$users = qa_db_select_with_pending(qa_db_users_from_level_selectspec(QA_USER_LEVEL_EXPERT));
			return array(count($users), $users);
		};
123
		$userLevel = function ($user) {
124 125
			return qa_html(qa_user_level_string($user['level']));
		};
Scott committed
126

127
		$qa_content = $this->rankedUsersContent($fetchUsers, $userLevel);
Scott committed
128 129 130

		$qa_content['title'] = qa_lang_html('users/special_users');

131
		$qa_content['ranking']['sort'] = 'level';
Scott committed
132 133 134 135

		return $qa_content;
	}

136 137 138 139
	/**
	 * Display blocked users page
	 * @return array $qa_content
	 */
Scott committed
140 141
	public function blocked()
	{
142
		// callables to fetch user data
143
		$fetchUsers = function ($start, $pageSize) {
144 145 146 147 148 149 150
			list($totalUsers, $users) = qa_db_select_with_pending(
				qa_db_selectspec_count(qa_db_users_with_flag_selectspec(QA_USER_FLAGS_USER_BLOCKED)),
				qa_db_users_with_flag_selectspec(QA_USER_FLAGS_USER_BLOCKED, $start, $pageSize)
			);

			return array($totalUsers['count'], $users);
		};
151
		$userLevel = function ($user) {
152 153 154 155 156 157 158 159 160 161 162 163 164
			return qa_html(qa_user_level_string($user['level']));
		};

		$qa_content = $this->rankedUsersContent($fetchUsers, $userLevel);

		$qa_content['title'] = empty($qa_content['ranking']['items'])
			? qa_lang_html('users/no_blocked_users')
			: qa_lang_html('users/blocked_users');

		$qa_content['ranking']['sort'] = 'level';

		return $qa_content;
	}
Scott committed
165

166 167 168 169 170 171 172 173 174
	/**
	 * Fetch $qa_content array for a set of ranked users.
	 * @param  callable $fnUsersAndCount Function that returns the list of users for a page and the user total.
	 * @param  callable $fnUserScore Function that returns the "score" (points, date, etc) that will be displayed.
	 * @return array $qa_content
	 */
	private function rankedUsersContent($fnUsersAndCount, $fnUserScore)
	{
		// get the users to display on this page
Scott committed
175

176 177 178
		$request = qa_request();
		$start = qa_get_start();
		$pageSize = qa_opt('page_size_users');
Scott committed
179

180
		list($totalUsers, $users) = $fnUsersAndCount($start, $pageSize);
Scott committed
181

182 183
		// get userids and handles of retrieved users
		$usersHtml = qa_userids_handles_html($users);
Scott committed
184

185
		// prepare content for theme
Scott committed
186

187
		$content = qa_content_prepare();
Scott committed
188

189
		$content['ranking'] = array(
Scott committed
190
			'items' => array(),
191
			'rows' => ceil($pageSize / qa_opt('columns_users')),
Scott committed
192
			'type' => 'users',
193
			// 'sort' is handled by calling code
Scott committed
194 195 196
		);

		foreach ($users as $user) {
197 198 199
			if (QA_FINAL_EXTERNAL_USERS) {
				$avatarHtml = qa_get_external_avatar_html($user['userid'], qa_opt('avatar_users_size'), true);
			} else {
200 201 202 203 204 205 206 207 208 209
				$avatarHtml = qa_get_user_avatar_html(
					$user['flags'],
					$user['email'],
					$user['handle'],
					$user['avatarblobid'],
					$user['avatarwidth'],
					$user['avatarheight'],
					qa_opt('avatar_users_size'),
					true
				);
210 211 212 213 214 215
			}

			$content['ranking']['items'][] = array(
				'avatar' => $avatarHtml,
				'label' => $usersHtml[$user['userid']],
				'score' => $fnUserScore($user),
Scott committed
216 217 218 219
				'raw' => $user,
			);
		}

220
		$content['page_links'] = qa_html_page_links($request, $start, $pageSize, $totalUsers, qa_opt('pages_prev_next'));
Scott committed
221

222
		$content['canonical'] = qa_get_canonical();
Scott committed
223

224
		$content['navigation']['sub'] = qa_users_sub_navigation();
Scott committed
225

226
		return $content;
Scott committed
227 228
	}
}