favorites.php 5.8 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-app-favorites.php
	Description: Handles favoriting and unfavoriting (application level)


	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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	exit;
}


/**
 * Set an entity to be favorited or removed from favorites. Handles event reporting.
 *
 * @param int $userid ID of user assigned to the favorite
 * @param string $handle Username of user
 * @param string $cookieid Cookie ID of user
 * @param string $entitytype Entity type code (one of QA_ENTITY_* constants)
 * @param string $entityid ID of the entity being favorited (e.g. postid for questions)
 * @param bool $favorite Whether to add favorite (true) or remove favorite (false)
 */
function qa_user_favorite_set($userid, $handle, $cookieid, $entitytype, $entityid, $favorite)
{
	require_once QA_INCLUDE_DIR . 'db/favorites.php';
	require_once QA_INCLUDE_DIR . 'app/limits.php';
	require_once QA_INCLUDE_DIR . 'app/updates.php';

	// Make sure the user is not favoriting themselves
	if ($entitytype == QA_ENTITY_USER && $userid == $entityid) {
		return;
Scott committed
48 49
	}

Scott committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	if ($favorite)
		qa_db_favorite_create($userid, $entitytype, $entityid);
	else
		qa_db_favorite_delete($userid, $entitytype, $entityid);

	switch ($entitytype) {
		case QA_ENTITY_QUESTION:
			$action = $favorite ? 'q_favorite' : 'q_unfavorite';
			$params = array('postid' => $entityid);
			break;

		case QA_ENTITY_USER:
			$action = $favorite ? 'u_favorite' : 'u_unfavorite';
			$params = array('userid' => $entityid);
			break;

		case QA_ENTITY_TAG:
			$action = $favorite ? 'tag_favorite' : 'tag_unfavorite';
			$params = array('wordid' => $entityid);
			break;

		case QA_ENTITY_CATEGORY:
			$action = $favorite ? 'cat_favorite' : 'cat_unfavorite';
			$params = array('categoryid' => $entityid);
			break;

		default:
			qa_fatal_error('Favorite type not recognized');
			break;
Scott committed
79 80
	}

Scott committed
81 82
	qa_report_event($action, $userid, $handle, $cookieid, $params);
}
Scott committed
83 84


Scott committed
85 86 87 88 89 90 91 92 93 94 95 96
/**
 * Returns content to set in $qa_content['q_list'] for a user's favorite $questions. Pre-generated
 * user HTML in $usershtml.
 * @param $questions
 * @param $usershtml
 * @return array
 */
function qa_favorite_q_list_view($questions, $usershtml)
{
	$q_list = array(
		'qs' => array(),
	);
Scott committed
97

Scott committed
98
	if (count($questions) === 0)
Scott committed
99 100
		return $q_list;

Scott committed
101 102 103 104 105 106
	$q_list['form'] = array(
		'tags' => 'method="post" action="' . qa_self_html() . '"',
		'hidden' => array(
			'code' => qa_get_form_security_code('vote'),
		),
	);
Scott committed
107

Scott committed
108
	$defaults = qa_post_html_defaults('Q');
Scott committed
109

Scott committed
110 111 112 113
	foreach ($questions as $question) {
		$q_list['qs'][] = qa_post_html_fields($question, qa_get_logged_in_userid(), qa_cookie_get(),
			$usershtml, null, qa_post_html_options($question, $defaults));
	}
Scott committed
114

Scott committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	return $q_list;
}


/**
 * Returns content to set in $qa_content['ranking_users'] for a user's favorite $users. Pre-generated
 * user HTML in $usershtml.
 * @param $users
 * @param $usershtml
 * @return array|null
 */
function qa_favorite_users_view($users, $usershtml)
{
	if (QA_FINAL_EXTERNAL_USERS)
		return null;

	require_once QA_INCLUDE_DIR . 'app/users.php';
	require_once QA_INCLUDE_DIR . 'app/format.php';

	$ranking = array(
		'items' => array(),
		'rows' => ceil(count($users) / qa_opt('columns_users')),
		'type' => 'users',
	);

	foreach ($users as $user) {
		$avatarhtml = qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'],
			$user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], qa_opt('avatar_users_size'), true);

		$ranking['items'][] = array(
			'avatar' => $avatarhtml,
			'label' => $usershtml[$user['userid']],
			'score' => qa_html(qa_format_number($user['points'], 0, true)),
			'raw' => $user,
Scott committed
149 150 151
		);
	}

Scott committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	return $ranking;
}


/**
 * Returns content to set in $qa_content['ranking_tags'] for a user's favorite $tags.
 * @param $tags
 * @return array
 */
function qa_favorite_tags_view($tags)
{
	require_once QA_INCLUDE_DIR . 'app/format.php';

	$ranking = array(
		'items' => array(),
		'rows' => ceil(count($tags) / qa_opt('columns_tags')),
		'type' => 'tags',
	);

	foreach ($tags as $tag) {
		$ranking['items'][] = array(
			'label' => qa_tag_html($tag['word'], false, true),
			'count' => qa_html(qa_format_number($tag['tagcount'], 0, true)),
Scott committed
175 176 177
		);
	}

Scott committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	return $ranking;
}


/**
 * Returns content to set in $qa_content['nav_list_categories'] for a user's favorite $categories.
 * @param $categories
 * @return array
 */
function qa_favorite_categories_view($categories)
{
	require_once QA_INCLUDE_DIR . 'app/format.php';

	$nav_list_categories = array(
		'nav' => array(),
		'type' => 'browse-cat',
	);

	foreach ($categories as $category) {
		$cat_url = qa_path_html('questions/' . implode('/', array_reverse(explode('/', $category['backpath']))));
		$cat_anchor = $category['qcount'] == 1
			? qa_lang_html_sub('main/1_question', '1', '1')
			: qa_lang_html_sub('main/x_questions', qa_format_number($category['qcount'], 0, true));
		$cat_descr = strlen($category['content']) ? qa_html(' - ' . $category['content']) : '';

		$nav_list_categories['nav'][$category['categoryid']] = array(
			'label' => qa_html($category['title']),
			'state' => 'open',
			'favorited' => true,
			'note' => ' - <a href="' . $cat_url . '">' . $cat_anchor . '</a>' . $cat_descr,
Scott committed
208 209 210
		);
	}

Scott committed
211 212
	return $nav_list_categories;
}