favorites.php 5.55 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<?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
*/

	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)
	{
Scott committed
41
		require_once QA_INCLUDE_DIR.'db/favorites.php';
Scott committed
42 43
		require_once QA_INCLUDE_DIR.'app/limits.php';
		require_once QA_INCLUDE_DIR.'app/updates.php';
Scott committed
44 45 46 47 48 49 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

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

		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;
		}

		qa_report_event($action, $userid, $handle, $cookieid, $params);
	}


	function qa_favorite_q_list_view($questions, $usershtml)
/*
	Returns content to set in $qa_content['q_list'] for a user's favorite $questions. Pre-generated
	user HTML in $usershtml.
*/
	{
		$q_list = array(
			'qs' => array(),
		);

		if (count($questions) === 0)
			return $q_list;

		$q_list['form'] = array(
			'tags' => 'method="post" action="'.qa_self_html().'"',
			'hidden' => array(
				'code' => qa_get_form_security_code('vote'),
			),
		);

		$defaults = qa_post_html_defaults('Q');

		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));
		}

		return $q_list;
	}


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

Scott committed
125
		require_once QA_INCLUDE_DIR.'app/users.php';
Scott committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

		$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(number_format($user['points'])),
				'raw' => $user,
			);
		}

		return $ranking;
	}


	function qa_favorite_tags_view($tags)
/*
	Returns content to set in $qa_content['ranking_tagss'] for a user's favorite $tags.
*/
	{
		$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' => number_format($tag['tagcount']),
			);
		}

		return $ranking;
	}


	function qa_favorite_categories_view($categories)
/*
	Returns content to set in $qa_content['nav_list_categories'] for a user's favorite $categories.
*/
	{
		$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', number_format($category['qcount']));
			$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,
			);
		}

		return $nav_list_categories;
	}


/*
	Omit PHP closing tag to help avoid accidental output
Gideon Greenspan committed
202
*/