Karma::action_applaud()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Deals with the giving and taking of a users karma
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 *
13
 * @version 2.0 dev
14
 *
15
 */
16
17
namespace ElkArte\Controller;
18
19
use ElkArte\AbstractController;
20
use ElkArte\Exceptions\Exception;
21
use ElkArte\Hooks;
22
23
/**
24
 * Can give good or bad karma so watch out!
25
 *
26
 * @package Karma
27
 */
28
class Karma extends AbstractController
29
{
30
	/**
31
	 * Pre Dispatch, called before other methods.  Loads integration hooks.
32
	 */
33
	public function pre_dispatch()
34
	{
35
		Hooks::instance()->loadIntegrationsSettings();
36
	}
37
38
	/**
39
	 * Default entry point, in case action methods are not directly
40
	 * called. Simply forward to applaud.
41
	 *
42
	 * @see \ElkArte\AbstractController::action_index()
43
	 */
44
	public function action_index()
45
	{
46
		// Applauds for us :P
47
		$this->action_applaud();
48
	}
49
50
	/**
51
	 * Modify a user's karma.
52
	 *
53
	 * What it does:
54
	 *
55
	 * - It redirects back to the referrer afterward, whether by javascript or the passed parameters.
56
	 * - Requires the karma_edit permission, and that the user isn't a guest.
57
	 * - It depends on the karmaMode, karmaWaitTime, and karmaTimeRestrictAdmins settings.
58
	 * - It is accessed via ?action=karma, sa=smite or sa=applaud.
59
	 */
60
	public function action_applaud()
61
	{
62
		$id_target = $this->_req->getQuery('uid', 'intval', 0);
63
64
		// Start off with no change in karma.
65
		$action = $this->_prepare_karma($id_target);
66
67
		// Applaud (if you can) and return
68
		$this->_give_karma($this->user->id, $id_target, $action, 1);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
69
		$this->_redirect_karma();
70
	}
71
72
	/**
73
	 * Makes sure that a user can perform the karma action
74
	 *
75
	 * @param int $id_target
76
	 *
77
	 * @return int
78
	 * @throws Exception feature_disabled
79
	 */
80
	private function _prepare_karma($id_target)
81
	{
82
		global $modSettings;
83
84
		// If the mod is disabled, show an error.
85
		if (empty($modSettings['karmaMode']))
86
		{
87
			throw new Exception('feature_disabled', true);
88
		}
89
90
		// If you're a guest or can't do this, blow you off...
91
		is_not_guest();
92
		isAllowedTo('karma_edit');
93
94
		checkSession('get');
95
96
		// We hold karma here.
97
		require_once(SUBSDIR . '/Karma.subs.php');
98
99
		// If you don't have enough posts, tough luck.
100
		// @todo Should this be dropped in favor of post group permissions?
101
		// Should this apply to the member you are smiting/applauding?
102
		if ($this->user->is_admin === false && $this->user->posts < $modSettings['karmaMinPosts'])
103
		{
104
			throw new Exception('not_enough_posts_karma', true, array($modSettings['karmaMinPosts']));
105
		}
106
107
		// And you can't modify your own, punk! (use the profile if you need to.)
108
		if (empty($id_target) || $id_target == $this->user->id)
109
		{
110
			throw new Exception('cant_change_own_karma', false);
111
		}
112
113
		// Delete any older items from the log so we can get the go ahead or not
114
		clearKarma($modSettings['karmaWaitTime']);
115
  if (!empty($modSettings['karmaTimeRestrictAdmins']) || !allowedTo('moderate_forum')) {
116
      return lastActionOn($this->user->id, $id_target);
117
  }
118
119
		return 0;
120
	}
121
122
	/**
123
	 * Function to move the karma needle up or down for a given user
124
	 *
125
	 * @param int $id_executor who is performing the action
126
	 * @param int $id_target who is getting the action
127
	 * @param int $action applaud or smite
128
	 * @param int $dir
129
	 *
130
	 * @throws Exception karma_wait_time
131
	 */
132
	private function _give_karma($id_executor, $id_target, $action, $dir)
133
	{
134
		global $modSettings, $txt;
135
136
		// They haven't, not before now, anyhow.
137
		if (empty($action) || empty($modSettings['karmaWaitTime']))
138
		{
139
			addKarma($id_executor, $id_target, $dir);
140
		}
141
		else
142
		{
143
			// If you are gonna try to repeat.... don't allow it.
144
			if ($action === $dir)
145
			{
146
				throw new Exception('karma_wait_time', false, array($modSettings['karmaWaitTime'], ($modSettings['karmaWaitTime'] == 1 ? strtolower($txt['hour']) : $txt['hours'])));
147
			}
148
149
			updateKarma($id_executor, $id_target, $dir);
150
		}
151
	}
152
153
	/**
154
	 * Done with the action, return to where we need to be, or make it up if we
155
	 * can't figure it out.
156
	 */
157
	private function _redirect_karma()
158
	{
159
		global $context, $topic;
160
161
		// Figure out where to go back to.... the topic?
162
		if (!empty($topic))
163
		{
164
			redirectexit('topic=' . $topic . '.' . $this->_req->start . '#msg' . $this->_req->get('m', 'intval'));
0 ignored issues
show
Bug Best Practice introduced by
The property start does not exist on ElkArte\Helper\HttpReq. Since you implemented __get, consider adding a @property annotation.
Loading history...
165
		}
166
		// Hrm... maybe a personal message?
167
		elseif (isset($_REQUEST['f']))
168
		{
169
			redirectexit('action=pm;f=' . $_REQUEST['f'] . ';start=' . $this->_req->start . (isset($_REQUEST['l']) ? ';l=' . $this->_req->get('l', 'intval') : '') . (isset($_REQUEST['pm']) ? '#' . $this->_req->get('pm', 'intval') : ''));
170
		}
171
		// JavaScript as a last resort.
172
		else
173
		{
174
			echo '<!DOCTYPE html>
175
<html ', $context['right_to_left'] ? 'dir="rtl"' : '', '>
176
	<head>
177
		<title>...</title>
178
		<script>
179
			history.go(-1);
180
		</script>
181
	</head>
182
	<body>&laquo;</body>
183
</html>';
184
185
			obExit(false);
186
		}
187
	}
188
189
	/**
190
	 * Smite a user.
191
	 */
192
	public function action_smite()
193
	{
194
		global $modSettings;
195
196
		// Sometimes the community needs to chill
197
		if (!empty($modSettings['karmaDisableSmite']))
198
		{
199
			$this->_redirect_karma();
200
		}
201
202
		// The user ID _must_ be a number, no matter what.
203
		$id_target = $this->_req->getQuery('uid', 'intval', 0);
204
205
		// Start off with no change in karma.
206
		$action = $this->_prepare_karma($id_target);
207
208
		// Give em a wack and run away
209
		$this->_give_karma($this->user->id, $this->_req->query->uid, $action, -1);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
210
		$this->_redirect_karma();
211
	}
212
}
213