UsersList.php 6.96 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 22 23 24 25
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';

26
class UsersList extends \Q2A\Controllers\BaseController
Scott committed
27
{
28 29 30 31
	/**
	 * Display top users page (ordered by points)
	 * @return array $qa_content
	 */
Scott committed
32 33
	public function top()
	{
34 35 36 37 38 39 40 41 42 43
		// callables to fetch user data
		$fetchUsers = function($start, $pageSize) {
			return array(
				qa_opt('cache_userpointscount'),
				qa_db_select_with_pending(qa_db_top_users_selectspec($start, $pageSize))
			);
		};
		$userScore = function($user) {
			return qa_html(qa_format_number($user['points'], 0, true));
		};
Scott committed
44

45
		$qa_content = $this->rankedUsersContent($fetchUsers, $userScore);
Scott committed
46

47 48 49
		$qa_content['title'] = empty($qa_content['ranking']['items'])
			? qa_lang_html('main/no_active_users')
			: qa_lang_html('main/highest_users');
Scott committed
50

51
		$qa_content['ranking']['sort'] = 'points';
Scott committed
52 53 54 55

		return $qa_content;
	}

56 57 58 59
	/**
	 * Display newest users page
	 * @return array $qa_content
	 */
Scott committed
60 61
	public function newest()
	{
62
		// check we're not using single-sign on integration
Scott committed
63 64 65
		if (QA_FINAL_EXTERNAL_USERS)
			qa_fatal_error('User accounts are handled by external code');

66
		// check we have permission to view this page (moderator or above)
Scott committed
67 68 69 70 71 72
		if (qa_user_permit_error('permit_view_new_users_page')) {
			$qa_content = qa_content_prepare();
			$qa_content['error'] = qa_lang_html('users/no_permission');
			return $qa_content;
		}

73 74 75 76 77 78 79 80 81 82 83
		// callables to fetch user data
		$fetchUsers = function($start, $pageSize) {
			return array(
				qa_opt('cache_userpointscount'),
				qa_db_select_with_pending(qa_db_newest_users_selectspec($start, $pageSize))
			);
		};
		$userDate = function($user) {
			$when = qa_when_to_html($user['created'], 7);
			return $when['data'];
		};
Scott committed
84

85
		$qa_content = $this->rankedUsersContent($fetchUsers, $userDate);
Scott committed
86

87 88 89
		$qa_content['title'] = empty($qa_content['ranking']['items'])
			? qa_lang_html('main/no_active_users')
			: qa_lang_html('main/newest_users');
Scott committed
90

91
		$qa_content['ranking']['sort'] = 'date';
Scott committed
92 93 94 95

		return $qa_content;
	}

96 97 98 99
	/**
	 * Display special users page (admins, moderators, etc)
	 * @return array $qa_content
	 */
Scott committed
100 101
	public function special()
	{
102
		// check we're not using single-sign on integration
Scott committed
103 104 105
		if (QA_FINAL_EXTERNAL_USERS)
			qa_fatal_error('User accounts are handled by external code');

106
		// check we have permission to view this page (moderator or above)
Scott committed
107 108 109 110 111 112
		if (qa_user_permit_error('permit_view_special_users_page')) {
			$qa_content = qa_content_prepare();
			$qa_content['error'] = qa_lang_html('users/no_permission');
			return $qa_content;
		}

113 114 115 116 117 118 119 120 121
		// callables to fetch user data
		$fetchUsers = function($start, $pageSize) {
			// 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);
		};
		$userLevel = function($user) {
			return qa_html(qa_user_level_string($user['level']));
		};
Scott committed
122

123
		$qa_content = $this->rankedUsersContent($fetchUsers, $userLevel);
Scott committed
124 125 126

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

127
		$qa_content['ranking']['sort'] = 'level';
Scott committed
128 129 130 131

		return $qa_content;
	}

132 133 134 135
	/**
	 * Display blocked users page
	 * @return array $qa_content
	 */
Scott committed
136 137
	public function blocked()
	{
138
		// check we're not using single-sign on integration
Scott committed
139 140 141
		if (QA_FINAL_EXTERNAL_USERS)
			qa_fatal_error('User accounts are handled by external code');

142
		// check we have permission to view this page (moderator or above)
Scott committed
143 144 145 146 147 148
		if (qa_get_logged_in_level() < QA_USER_LEVEL_MODERATOR) {
			$qa_content = qa_content_prepare();
			$qa_content['error'] = qa_lang_html('users/no_permission');
			return $qa_content;
		}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		// callables to fetch user data
		$fetchUsers = function($start, $pageSize) {
			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);
		};
		$userLevel = function($user) {
			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
172

173 174 175 176 177 178 179 180 181
	/**
	 * 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
182

183 184 185
		$request = qa_request();
		$start = qa_get_start();
		$pageSize = qa_opt('page_size_users');
Scott committed
186

187
		list($totalUsers, $users) = $fnUsersAndCount($start, $pageSize);
Scott committed
188

189 190
		// get userids and handles of retrieved users
		$usersHtml = qa_userids_handles_html($users);
Scott committed
191

192
		// prepare content for theme
Scott committed
193

194
		$content = qa_content_prepare();
Scott committed
195

196
		$content['ranking'] = array(
Scott committed
197
			'items' => array(),
198
			'rows' => ceil($pageSize / qa_opt('columns_users')),
Scott committed
199
			'type' => 'users',
200
			// 'sort' => '',
Scott committed
201 202 203
		);

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

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

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

221 222 223
		// set the canonical url based on possible pagination
		$params = $start > 0 ? array('start' => $start) : null;
		$content['canonical'] = qa_path_html($request, $params, qa_opt('site_url'));
Scott committed
224

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

227
		return $content;
Scott committed
228 229
	}
}