RateAction.php 30.6 KB
Newer Older
Cameron Chunn's avatar
Cameron Chunn committed
1 2
<?php

pcj's avatar
pcj committed
3 4
use MediaWiki\MediaWikiServices;

Cameron Chunn's avatar
Cameron Chunn committed
5 6 7
class RateAction extends FormlessAction {


Alexia's avatar
Alexia committed
8 9 10 11 12 13 14
	/**
	 * Return the name of the action
	 * @return string
	 */
	public function getName() {
		return 'rate';
	}
Cameron Chunn's avatar
Cameron Chunn committed
15

Cameron Chunn's avatar
Cameron Chunn committed
16

Alexia's avatar
Alexia committed
17
	/**
18
	 * Handle viewing of action.
Cameron Chunn's avatar
Cameron Chunn committed
19 20 21
	 * @param  string $action
	 * @param  Article $article
	 * @return bool
Cameron Chunn's avatar
Cameron Chunn committed
22
	 */
23
	function onView() {
Cameron Chunn's avatar
Cameron Chunn committed
24
		global $wgOut, $wgUser, $wgRequest,$wgPvXRateBuildNamespace;
Cameron Chunn's avatar
Cameron Chunn committed
25

Alexia's avatar
Alexia committed
26 27 28
		$wgOut->addModules('ext.pvxrate');
		$action = $wgRequest->getText('rating');
		$article = $this->page;
Cameron Chunn's avatar
Cameron Chunn committed
29

30

Alexia's avatar
Alexia committed
31 32 33 34
		$buildNamespace = defined($wgPvXRateBuildNamespace);
		if (!$buildNamespace) {
			wfWarn('The PvXRateBuildNamespace defined in PvX Rate\'s extension.json file ('.$wgPvXRateBuildNamespace.') is not a valid namespace.',2);
		} else {
35 36
			$buildNamespace = constant($wgPvXRateBuildNamespace);
		}
Cameron Chunn's avatar
Cameron Chunn committed
37

Alex's avatar
Alex committed
38 39 40 41 42 43 44 45 46 47 48 49
		// There are two scenarios where we do not show all existing votes:
		// (1) Page does not exist
		// (2) Page is in the wrong namespace

		// Check whether associated page exists
		if ($article->getID() == 0) {
			$perm_msg = '=== Page does not exist ===
__NOEDITSECTION__
The target page does not exist yet.

&larr; <i>Back to [{{FULLURL:{{SUBJECTPAGENAME}}}} {{SUBJECTPAGENAME}}]</i>';
			$wgOut->addWikiText($perm_msg);
Cameron Chunn's avatar
Cameron Chunn committed
50 51 52
			return false;
		}

Alex's avatar
Alex committed
53 54 55 56 57 58 59 60 61
		// Check whether page is in the wrong namespace
		if ( ($this->getTitle()->getNamespace() == NS_SPECIAL)
		  || ($this->getTitle()->getNamespace() !== $buildNamespace) ) {
			$perm_msg = '=== Incorrect namespace ===
__NOEDITSECTION__
Pages in this namespace cannot be voted upon.

&larr; <i>Back to [{{FULLURL:{{SUBJECTPAGENAME}}}} {{SUBJECTPAGENAME}}]</i>';
			$wgOut->addWikiText($perm_msg);
Cameron Chunn's avatar
Cameron Chunn committed
62 63 64
			return false;
		}

Alex's avatar
Alex committed
65
		// For all other scenarios, we display all existing votes.
Cameron Chunn's avatar
Cameron Chunn committed
66

Alex's avatar
Alex committed
67
		// Check if user has the correct permissions (autoconfirmed, not blocked)
Cameron Chunn's avatar
Cameron Chunn committed
68
		if ($this->ratePermissions()) {
Cameron Chunn's avatar
Cameron Chunn committed
69

Alex's avatar
Alex committed
70 71
			// Page title
			$wgOut->setPageTitle('Build rating');
Cameron Chunn's avatar
Cameron Chunn committed
72

Alex's avatar
Alex committed
73 74 75 76 77
			// rating=delete|edit|rollback
			$posted_action = $wgRequest->getText('rating');
			$posted_build  = $wgRequest->getText('build');
			$posted_update = $wgRequest->getText('ratingid');
			$rate_input = $this->rateGet($article);
Cameron Chunn's avatar
Cameron Chunn committed
78

Alex's avatar
Alex committed
79
			$is_admin = $this->getUser()->isAllowed('vote_rollback');
Cameron Chunn's avatar
Cameron Chunn committed
80

Alex's avatar
Alex committed
81 82 83
			$show_own  = true;
			$show_all  = true;
			$show_rate = true;
Cameron Chunn's avatar
Cameron Chunn committed
84

Alex's avatar
Alex committed
85 86
			if (($posted_action == 'edit') && ($this->rateCheckRights($article, $posted_build))) {
				$wgOut->addHtml('<h2> Rate this build </h2>');
87
				$wgOut->addHtml($this->rateForm($this->rateRead(null, null, $posted_build)));
Alex's avatar
Alex committed
88 89 90 91
				$show_own = false;
			}

			if (($posted_action == 'rollback') && ($is_admin) && ($posted_build)) {
92
				$wgOut->addHtml($this->rateRollback($this->rateRead(null, null, $posted_build)));
Alex's avatar
Alex committed
93 94 95
			}

			if (($posted_action == 'restore') && ($is_admin) && ($posted_build)) {
96
				$wgOut->addHtml($this->rateRestore($this->rateRead(null, null, $posted_build)));
Alex's avatar
Alex committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
			}

			if (($posted_action == 'delete') && ($this->rateCheckRights($article, $posted_build))) {
				$this->rateDelete($posted_build, $article->getID(), $this->getUser()->getID());
				$wgOut->addHtml('<h2> Your rating </h2>');
				$wgOut->addWikiText('Your rating was deleted from our database.');
				$show_own  = false;
				$show_rate = false;
			}

			// Check if a form was submitted, if so let's store it.
			if ($wgRequest->wasPosted()) {
				if ($rate_input['error']) {
					// ERROR! Something went wrong.
					$wgOut->addHtml($rate_input['error_msg']);
				} else {
					if (($posted_action == 'update') && ($this->rateCheckRights($article, $posted_update))) {
						$rate_input['rollback'] = 0;
						$rate_input['admin_id'] = 0;
						$wgOut->addHtml($this->rateUpdate($rate_input));
					} elseif ($posted_action == 'rollback') {
						$wgOut->addHtml($this->rateUpdate($rate_input));
					} elseif ($posted_action == 'restore') {
						$wgOut->addHtml($this->rateUpdate($rate_input));
Cameron Chunn's avatar
Cameron Chunn committed
121
					} else {
Alex's avatar
Alex committed
122 123 124 125
						// Response was good, lets process it.
						$rate_input['rollback'] = 0;
						$rate_input['admin_id'] = 0;
						$rate_input['reason'] = '';
126
						if (!$this->rateRead($article->getID(), $this->getUser()->getID(), null)) {
Alex's avatar
Alex committed
127
							$this->rateSave($rate_input);
Cameron Chunn's avatar
Cameron Chunn committed
128 129 130 131
						}
					}
				}
			}
Alex's avatar
Alex committed
132 133 134

			// Let's print our standard output: show everything, not read only mode.
			$this->ratePrintAll($article, $show_own, $show_rate, $show_all, false);
Cameron Chunn's avatar
Cameron Chunn committed
135 136 137
			return false;
		}

Alex's avatar
Alex committed
138 139 140 141 142
		// User does not have permission to rate: show your own rating, don't show the form, show current ratings, readonly
		else {
			$this->ratePrintAll($article, true, false, true, true);
			return false;
		}
Cameron Chunn's avatar
Cameron Chunn committed
143 144 145
	}


Cameron Chunn's avatar
Cameron Chunn committed
146
	/**
Alex's avatar
Alex committed
147
	 * Check permissions, and add WikiText if permissions fail.
Cameron Chunn's avatar
Cameron Chunn committed
148
	 * @return boolean
Cameron Chunn's avatar
Cameron Chunn committed
149 150
	 */
	public function ratePermissions() {
Cameron Chunn's avatar
Cameron Chunn committed
151
		global $wgUser, $wgOut, $perm_msg;
Alex's avatar
Alex committed
152

pcj's avatar
pcj committed
153
		$configfactory = MediaWikiServices::getInstance()->getConfigFactory();
Alexia's avatar
Alexia committed
154
		$config = $configfactory->makeConfig('main');
155
		$editsReq = $config->get('PvXRateEditsRequired');
Cameron Chunn's avatar
Cameron Chunn committed
156

Alex's avatar
Alex committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170
		// Check if user allowed to rate this build, then display a message if not. Requirements:
		// * Logged in
		// * Not a blocked user
		// * Email authenticated
		// * Minimum of X edits

		if ($this->getUser()->isBlocked()) {
			$perm_msg = '=== Read-only mode: You are currently blocked. ===
__NOEDITSECTION__
Blocked users cannot vote. You will need to wait until your current block expires.';
			$wgOut->addWikiText($perm_msg);
			return false;

		} elseif ($this->getUser()->isAnon()) {
Cameron Chunn's avatar
Cameron Chunn committed
171
			$perm_msg = '=== Read-only mode: You are currently not logged in. ===
Alex's avatar
Alex committed
172 173 174
__NOEDITSECTION__
For security reasons you need to fulfill the following requirements in order to submit a vote:
* You need to log in. Visit [[Special:UserLogin]] to log in or create a new account.
Cameron Chunn's avatar
Cameron Chunn committed
175
* You need to authenticate your e-mail address.
pcj's avatar
pcj committed
176
* You need to make at least ' . $editsReq . ' edits to the wiki.';
Cameron Chunn's avatar
Cameron Chunn committed
177 178
			$wgOut->addWikiText($perm_msg);
			return false;
Alex's avatar
Alex committed
179

Cameron Chunn's avatar
Cameron Chunn committed
180
		} elseif (!$this->getUser()->mEmailAuthenticated) {
Cameron Chunn's avatar
Cameron Chunn committed
181
			$perm_msg = '=== Read-only mode: Your e-mail address is not authenticated. ===
Alex's avatar
Alex committed
182 183
__NOEDITSECTION__
For security reasons you need to fulfill the following requirements in order to submit a vote:
Cameron Chunn's avatar
Cameron Chunn committed
184 185
* You need to log in.
* You need to authenticate your e-mail address. Please edit/add your e-mail address using [[Special:Preferences]] and a confirmation e-mail will be sent to that address. Follow the instructions in the e-mail to confirm that the account is actually yours.
pcj's avatar
pcj committed
186
* You need to make at least ' . $editsReq . ' edits to the wiki.';
Cameron Chunn's avatar
Cameron Chunn committed
187 188
			$wgOut->addWikiText($perm_msg);
			return false;
Alex's avatar
Alex committed
189

pcj's avatar
pcj committed
190
		} elseif ($this->getUser()->getEditCount() < $editsReq) {
Robert Nix's avatar
Robert Nix committed
191
			$perm_msg = '=== Read-only mode: You made only ' . $this->getUser()->getEditCount() . ' edits so far. ===
Alex's avatar
Alex committed
192 193
__NOEDITSECTION__
For security reasons you need to fulfill the following requirements in order to submit a vote:
Cameron Chunn's avatar
Cameron Chunn committed
194 195
* You need to log in.
* You need to authenticate your e-mail address.
pcj's avatar
pcj committed
196
* You need to make at least ' . $editsReq . ' contributions to the wiki. A contribution is any edit to any page. A good way to get your first few contributions is adding some information about yourself to [[Special:Mypage|your userpage]].';
Cameron Chunn's avatar
Cameron Chunn committed
197 198
			$wgOut->addWikiText($perm_msg);
			return false;
Alex's avatar
Alex committed
199 200 201 202 203 204
		} elseif (!$this->getUser()->isAllowed('ratebuild')) {
			$perm_msg = '=== Permissions error. ===
__NOEDITSECTION__
Whilst your account meets all of the basic requirements for the rating permission (logged in, not blocked, email authenticated, edit count threshold met), miraculously your user account lacks the "ratebuild" rights. This may indicate a bug with the PvXRate extension. Please report this bug to your site administrator.';
			$wgOut->addWikiText($perm_msg);
			return false;
Cameron Chunn's avatar
Cameron Chunn committed
205
		}
Alex's avatar
Alex committed
206
		// Default to true otherwise, i.e. user has permission to rate builds.
Cameron Chunn's avatar
Cameron Chunn committed
207 208 209
		return true;
	}

Cameron Chunn's avatar
Cameron Chunn committed
210
	/**
Cameron Chunn's avatar
Cameron Chunn committed
211 212 213 214 215 216
	 * Format a link for use elsewhere
	 * @param  Article $article reference to article
	 * @param  string $name link title
	 * @param  string $action sets the ?rating= request variable.
	 * @param  string $id the build id. sets ?build=
	 * @return string
Cameron Chunn's avatar
Cameron Chunn committed
217 218
	 */
	public function rateLink(&$article, $name, $action, $id) {
Cameron Chunn's avatar
Cameron Chunn committed
219
		global $wgUser;
Cameron Chunn's avatar
Cameron Chunn committed
220
		return '<div class="aedit">[&nbsp;' . Linker::link($article->getTitle(), $name, [], ['action'=>'rate','rating'=>$action,'build'=>$id]) . '&nbsp;]&nbsp;</div>';
Cameron Chunn's avatar
Cameron Chunn committed
221 222
	}

Cameron Chunn's avatar
Cameron Chunn committed
223
	/**
Cameron Chunn's avatar
Cameron Chunn committed
224 225 226 227
	 * Builds output for a specific rating
	 * @param  array  $rate_results array containing the rating records
	 * @param  string $link links (edit, delete) to include
	 * @return string
Cameron Chunn's avatar
Cameron Chunn committed
228
	 */
Cameron Chunn's avatar
Cameron Chunn committed
229
	public function ratePrint(array $rate_results, $link) {
Cameron Chunn's avatar
Cameron Chunn committed
230
		global $wgUser, $wgParser, $wgOut, $wgScriptPath;
Cameron Chunn's avatar
Cameron Chunn committed
231 232 233 234 235 236 237 238 239 240

		$user_name = User::newFromId($rate_results['user_id'])->getName();

		$parserOptions = ParserOptions::newFromUser($wgUser);
		$parserOptions->setEditSection(false);
		$parserOptions->setTidy(true);
		$wgParser->mShowToc = false;

		$number_max = 5;
		$number_cat = 3;
Alex's avatar
Alex committed
241
		$bar_width = 168; // width in pixels of bar
Cameron Chunn's avatar
Cameron Chunn committed
242

Alex's avatar
Alex committed
243 244 245 246
		$cate = array(
			1 => 0.8,
			2 => 0.2,
			3 => 0.0
Cameron Chunn's avatar
Cameron Chunn committed
247
		);
Alex's avatar
Alex committed
248
		$rate = array(
Cameron Chunn's avatar
Cameron Chunn committed
249 250 251 252 253 254
			1 => $rate_results['rating'][0],
			2 => $rate_results['rating'][1],
			3 => $rate_results['rating'][2]
		);
		$cur_score = ($rate[1] * $cate[1] + $rate[2] * $cate[2] + $rate[3] * $cate[3]);
		$sze_table = array(
Alex's avatar
Alex committed
255 256 257
			1 => (($rate[1] / $number_max) * $bar_width),
			2 => (($rate[2] / $number_max) * $bar_width),
			3 => (($rate[3] / $number_max) * $bar_width)
Cameron Chunn's avatar
Cameron Chunn committed
258 259 260
		);
		$max_score = (($number_max / 100) * $cate[1]) + (($number_max / 100) * $cate[2]) + (($number_max / 100) * $cate[3]);

Alex's avatar
Alex committed
261
		$overall_rating_bar_width = ($cur_score / $number_max) * $bar_width;
Cameron Chunn's avatar
Cameron Chunn committed
262

Alexia's avatar
Alexia committed
263
		$parsedComment = $wgParser->parse($rate_results['comment'], $this->getTitle(), $parserOptions)->mText;
Cameron Chunn's avatar
Cameron Chunn committed
264 265 266

		if ($rate_results['rollback']) {
			$comment = '<b>Removed: </b><s>' . $parsedComment . '</s><br> <b>Reason: </b>' . $rate_results['reason'] . '<br><b>Removed by: </b> ' . User::newFromId($rate_results['admin_id'])->getName();
Alexia's avatar
Alexia committed
267
			$tduser	 = $user_name;
Cameron Chunn's avatar
Cameron Chunn committed
268 269
		} else {
			$comment = $parsedComment;
Alexia's avatar
Alexia committed
270
			$tduser	 = $user_name;
Cameron Chunn's avatar
Cameron Chunn committed
271 272 273 274 275 276 277 278 279 280 281 282
		}

		$timestamp = strtotime($rate_results['timestamp']);
		$timestr   = date('H:i, d M Y', $timestamp) . ' (EST)'; # GMT on test box, EST on main server

		$tduser = $wgParser->parse('[[User:' . $user_name . '|' . $user_name . ']]', $this->getTitle(), $parserOptions)->mText;
		if ($rate[3] > 0) {
			$inno_out = 'X';
		} else {
			$inno_out = 'O';
		}
		$out = '
Alex's avatar
Alex committed
283 284 285 286 287 288
<div class="rating">
	<table border="1" cellpadding="0" cellspacing="3">
		<tr>
			<td class="tdrating">
				<div class="r1" style="width:' . $overall_rating_bar_width . 'px;"><span>Overall</span></div>
			</td>
Alexia's avatar
Alexia committed
289 290
			<td class="tdresult">' . sprintf('%3.1f', round($cur_score * 10) / 10) . '</td>
			<td class="tdcomment" rowspan="4">
Alex's avatar
Alex committed
291 292 293 294 295 296 297 298
				<table class="tablecomment" border="0" style="border:0px;">
					<tr>
						<td class="tduser">' . $tduser . '</td>
						<td class="tdedit"><div align="right"> Last edit: ' . $timestr . '&nbsp;</div>' . $link . '</td>
					</tr>
					<tr>
						<td colspan="2">' . $comment . '</td>
					</tr>
Alexia's avatar
Alexia committed
299
				</table>
Alex's avatar
Alex committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
			</td>
		</tr>
		<tr>
			<td class="tdrating">
				<div class="r2" style="width:' . $sze_table[1] . 'px;"><span>Effectiveness</span></div>
			</td>
			<td class="tdresult">' . $rate[1] . '</td>
		</tr>
		<tr>
			<td class="tdrating">
				<div class="r3" style="width:' . $sze_table[2] . 'px;"><span>Universality</span></div>
			</td>
			<td class="tdresult">' . $rate[2] . '</td>
		</tr>
		<tr>
			<td class="tdrating">
				<div class="r4" style="width:' . $sze_table[3] . 'px;"><span>Innovation</span></div>
			</td>
			<td class="tdresult">' . $inno_out . '</td>
		</tr>
	</table>
</div><br>';
Cameron Chunn's avatar
Cameron Chunn committed
322 323 324 325

		return $out;
	}

Alexia's avatar
Alexia committed
326 327 328 329 330 331 332 333 334
	/**
	 * print ratings for a specific build
	 * @param  Article $article reference to Article
	 * @param  boolean $show_own Show users own rating or not
	 * @param  boolean $show_form Show rating form
	 * @param  boolean $show_all Show all current ratings
	 * @param  boolean $read_only if true, just call ratePrint
	 * @return void, prints to screen
	 */
Cameron Chunn's avatar
Cameron Chunn committed
335 336 337
	public function ratePrintAll($article, $show_own, $show_form, $show_all, $read_only) {
		global $wgOut, $wgUser;

Alex's avatar
Alex committed
338 339 340 341 342 343 344 345 346
		$out_rmv_count = 0;
		$out_own_count = 0;
		$out_all_count = 0;

		$out_rmv = '';
		$out_own = '';
		$out_all = '';

		// Get all the ratings for this build
347
		$current = $this->rateRead($article->getID(), null, null);
Cameron Chunn's avatar
Cameron Chunn committed
348

Alex's avatar
Alex committed
349
		// Check if there are any current ratings in the database.
Cameron Chunn's avatar
Cameron Chunn committed
350 351 352 353
		if ($current) {
			// Getting the info from database.
			foreach ($current as $array) {
				if ($read_only) {
Cameron Chunn's avatar
Cameron Chunn committed
354
					$out_all .= $this->ratePrint($array, '');
Alex's avatar
Alex committed
355
					$out_all_count++;
Cameron Chunn's avatar
Cameron Chunn committed
356 357
				} else {
					if ($array['rollback']) {
Alex's avatar
Alex committed
358
						// Rating has been removed, and is owned by the current logged in user
Cameron Chunn's avatar
Cameron Chunn committed
359 360 361
						if ($array['user_id'] == $this->getUser()->getID()) {
							$link = $this->rateLink($article, 'delete', 'delete', $array['rate_id']);
							$link .= $this->rateLink($article, 'edit', 'edit', $array['rate_id']);
Alex's avatar
Alex committed
362 363
							$out_rmv = $this->ratePrint($array, $link);
							$out_rmv_count++;
Cameron Chunn's avatar
Cameron Chunn committed
364
							$show_form = false;
Alex's avatar
Alex committed
365 366 367
						}
						// Rating has been removed, and the current logged in user has permissions to restore the rating (admin)
						elseif ($this->getUser()->isAllowed('vote_rollback')) {
Cameron Chunn's avatar
Cameron Chunn committed
368 369
							$link = $this->rateLink($article, 'restore', 'restore', $array['rate_id']);
							$out_rmv .= $this->ratePrint($array, $link);
Alex's avatar
Alex committed
370 371 372 373
							$out_rmv_count++;
						}
						// Rating has been removed, and its somebody elses
						else {
Cameron Chunn's avatar
Cameron Chunn committed
374
							$out_rmv .= $this->ratePrint($array, '');
Alex's avatar
Alex committed
375
							$out_rmv_count++;
Cameron Chunn's avatar
Cameron Chunn committed
376 377
						}
					} else {
Alex's avatar
Alex committed
378
						// Rating is current, and is owned by the current logged in user
Cameron Chunn's avatar
Cameron Chunn committed
379 380 381
						if ($array['user_id'] == $this->getUser()->getID()) {
							$link = $this->rateLink($article, 'delete', 'delete', $array['rate_id']);
							$link .= $this->rateLink($article, 'edit', 'edit', $array['rate_id']);
Alex's avatar
Alex committed
382 383
							$out_own = $this->ratePrint($array, $link);
							$out_own_count++;
Cameron Chunn's avatar
Cameron Chunn committed
384
							$show_form = false;
Alex's avatar
Alex committed
385 386 387
						}
						// Rating is current, and the current logged in user has permissions to remove the rating (admin)
						elseif ($this->getUser()->isAllowed('vote_rollback')) {
Cameron Chunn's avatar
Cameron Chunn committed
388 389
							$link = $this->rateLink($article, 'remove', 'rollback', $array['rate_id']);
							$out_all .= $this->ratePrint($array, $link);
Alex's avatar
Alex committed
390 391 392 393
							$out_all_count++;
						}
						// Rating is current, and its somebody elses
						else {
Cameron Chunn's avatar
Cameron Chunn committed
394
							$out_all .= $this->ratePrint($array, '');
Alex's avatar
Alex committed
395
							$out_all_count++;
Cameron Chunn's avatar
Cameron Chunn committed
396 397 398 399 400
						}
					}
				}
			}

Alex's avatar
Alex committed
401 402
			// Existing overall voting results
			$out = $this->ratePrintResults($article->getID());
Cameron Chunn's avatar
Cameron Chunn committed
403

Alex's avatar
Alex committed
404 405 406 407
			// If you have already voted
			if (($out_own_count > 0) && ($show_own)) {
				$out .= '<h2> Your Rating </h2>';
				$out .= $out_own;
Cameron Chunn's avatar
Cameron Chunn committed
408 409
			}

Alex's avatar
Alex committed
410 411 412 413
			// If there are other ratings which are valid
			if (($out_all_count > 0) && ($show_all)) {
				$out .= '<h2> Current Ratings </h2>';
				$out .= $out_all;
Cameron Chunn's avatar
Cameron Chunn committed
414 415
			}

Alex's avatar
Alex committed
416 417
			// If there are other ratings which are not valid
			if (($out_rmv_count > 0) && ($show_all)) {
Cameron Chunn's avatar
Cameron Chunn committed
418 419 420 421 422
				$out .= ('<h2> Removed Ratings </h2>');
				$out .= $out_rmv;
			}
		}

Alex's avatar
Alex committed
423 424
		// If there are no ratings in the database, and you have permission to rate, and haven't voted already
		if ($show_form) {
Cameron Chunn's avatar
Cameron Chunn committed
425 426 427 428 429 430
			$wgOut->addHtml('<h2> Rate this build </h2>');
			$wgOut->addHtml($this->rateForm(array(
				false
			)));
		}

Alex's avatar
Alex committed
431 432 433 434 435 436
		// If there were ratings in the database, append them to the page.
		if (isset($out)) {
			$wgOut->addHtml($out);
		} else {
			// Possible to land on this branch if you're not yet authenticated, on a page with zero votes
		}
Cameron Chunn's avatar
Cameron Chunn committed
437 438
	}

Cameron Chunn's avatar
Cameron Chunn committed
439
	/**
Cameron Chunn's avatar
Cameron Chunn committed
440 441 442
	 * Gets retults and returns them for a specific page. Used inside ratePrintAll
	 * @param  int $page_id
	 * @return string
Cameron Chunn's avatar
Cameron Chunn committed
443 444
	 */
	public function ratePrintResults($page_id) {
Cameron Chunn's avatar
Cameron Chunn committed
445 446 447 448 449 450 451 452

		# names of criteria
		$rate_names = array(
			1 => 'Effectiveness',
			2 => 'Universality',
			3 => 'Innovation'
		);

Alex's avatar
Alex committed
453 454 455 456 457 458 459
		# tooltip text for the above criteria
		$rate_descr = array(
			1 => 'This criterion describes how effective the build does what it was designed for. That is, how much damage does a spiker build deal, a healer build heal or a protector build prevent? How good is the chance to get through the specified area with a running build or to reach and defeat the specified foes with a farming build?',
			2 => 'This criterion describes how flexible the build is when used in a situation slightly different from what the build was designed for. This includes the ability to change strategy in case a foe shows unexpected actions, in case an ally does not perform as expected, or when used in a different location than originally intended.',
			3 => 'This criterion describes how new the idea behind this build is. Does it use a new approach for dealing with a known task or even act as a precursor for dealing with a previously unconsidered task? To what extend is it expected to become a prototype for a new class of builds?'
		);

Cameron Chunn's avatar
Cameron Chunn committed
460 461
		# weighting of criteria
		$cate = array(
Alex's avatar
Alex committed
462 463 464
			1 => 0.8,
			2 => 0.2,
			3 => 0.0
Cameron Chunn's avatar
Cameron Chunn committed
465 466
		);

Cameron Chunn's avatar
Cameron Chunn committed
467
		$dbr = wfGetDB(DB_SLAVE);
Cameron Chunn's avatar
Cameron Chunn committed
468 469 470

		# determine overall rating (equal weighting of all voters, not counting rolled back votes)
		$res = $dbr->query("SELECT count(rating1) AS count, sum( rating1 ) AS r1, sum( rating2 ) AS r2, sum( rating3 ) AS r3
Alexia's avatar
Alexia committed
471
						FROM rating WHERE rollback != 1 AND page_id = " . $page_id);
Cameron Chunn's avatar
Cameron Chunn committed
472 473 474 475
		$row = $dbr->fetchObject($res);

		# put into local variables
		$count = $row->count;
Alex's avatar
Alex committed
476 477 478
		$r1 = $row->r1;
		$r2 = $row->r2;
		$r3 = $row->r3;
Cameron Chunn's avatar
Cameron Chunn committed
479 480 481 482

		# overall rating
		if ($count) {
			$final = array(
Alex's avatar
Alex committed
483 484 485 486
				0 => ($r1 * $cate[1] + $r2 * $cate[2] + $r3 * $cate[3]) / $count,
				$r1 / $count,
				$r2 / $count,
				$r3 / $count
Cameron Chunn's avatar
Cameron Chunn committed
487 488 489 490 491 492 493 494 495 496
			);
		} else {
			$final = array(
				0 => 0,
				0,
				0,
				0
			);
		}

Alex's avatar
Alex committed
497
		# fill histogram
Cameron Chunn's avatar
Cameron Chunn committed
498 499 500 501 502
		# $rating[x][y] is number of 'y' ratings on criterion 'x'
		$rating = array();
		for ($y = 1; $y <= 3; $y++) { # y=1..3 counts criteria
			for ($i = 0; $i <= 5; $i++) { # i=0..5 counts rating
				$rating[$y][$i] = $dbr->fetchObject($dbr->query("SELECT count(rating" . $y . ") AS count FROM rating
Alexia's avatar
Alexia committed
503
									 WHERE rating" . $y . " = " . $i . " AND rollback != 1 AND page_id = " . $page_id))->count;
Cameron Chunn's avatar
Cameron Chunn committed
504 505 506
			}
		}

Alex's avatar
Alex committed
507 508
		# overall rating output - Bars increase in horizontal direction
		$out = ('<h2> Rating totals: ' . $count . ' votes</h2>');
Cameron Chunn's avatar
Cameron Chunn committed
509
		$out .= '<table border="0" cellpadding="0" cellspacing="0"><tr>';
Alex's avatar
Alex committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
		$out .= '
<td>
	<div class="sum">
		<table border="0" cellpadding="0" cellspacing="3">
			<tr>
				<td class="tdrating">
					<div class="r1" style="width:' . round($final[0] * 168 / 5) . 'px;"><span>Overall</span></div>
				</td>
				<td class="tdresult">' . sprintf('%4.2f', round($final[0] * 100) / 100) . '</td>
			</tr>
			<tr>
				<td class="tdrating">
					<div class="r2" style="width:' . round($final[1] * 168 / 5) . 'px;"><span>Effectiveness</span></div>
				</td>
				<td class="tdresult">' . sprintf('%4.2f', round($final[1] * 100) / 100) . '</td>
			</tr>
			<tr>
				<td class="tdrating">
					<div class="r3" style="width:' . round($final[2] * 168 / 5) . 'px;"><span>Universality</span></div>
				</td>
				<td class="tdresult">' . sprintf('%4.2f', round($final[2] * 100) / 100) . '</td>
			</tr>
			<tr>
				<td class="tdrating">
					<div class="r4" style="width:' . round($final[3] * 168 / 5) . 'px;"><span>Innovation</span></div>
				</td>
				<td class="tdresult">' . sprintf('%4.0f', round($final[3] * 20)) . '%</td>
			</tr>
		</table>
	</div>
</td>';
Cameron Chunn's avatar
Cameron Chunn committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554

		# histograms
		# $rating[c][q] is number of 'q' ratings on criterion 'c'
		for ($c = 1; $c <= 2; $c++) {

			# normalize histo
			for ($q = 0; $q <= 5; $q++) {
				if ($count) {
					$histo[$c][$q] = round($rating[$c][$q] / $count * 77);
				} else {
					$histo[$c][$q] = 0;
				}
			}

Alex's avatar
Alex committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
			# plot - Bars increase in vertical direction
			$out .= '
<td>
	<div class="result">
		<table border="1" cellpadding="0" cellspacing="3">
			<tr>
				<td colspan="6" class="tdresult"><span title="' . $rate_descr[$c] . '">' . $rate_names[$c] . '</span></td>
			</tr>
			<tr>
				<td class="tdrating">
					<div style="height:' . ($histo[$c][0]) . 'px;"></div>
				</td>
				<td class="tdrating"><div class="v' . ($c+1) . '" style="height:' . ($histo[$c][1]) . 'px;" title="' . $rating[$c][1] . 'votes "></div></td>
				<td class="tdrating"><div class="v' . ($c+1) . '" style="height:' . ($histo[$c][2]) . 'px;" title="' . $rating[$c][1] . 'votes "></div></td>
				<td class="tdrating"><div class="v' . ($c+1) . '" style="height:' . ($histo[$c][3]) . 'px;" title="' . $rating[$c][1] . 'votes "></div></td>
				<td class="tdrating"><div class="v' . ($c+1) . '" style="height:' . ($histo[$c][4]) . 'px;" title="' . $rating[$c][1] . 'votes "></div></td>
				<td class="tdrating"><div class="v' . ($c+1) . '" style="height:' . ($histo[$c][5]) . 'px;" title="' . $rating[$c][1] . 'votes "></div></td>
			</tr>
			<tr>
				<td class="tdresult">0</td>
				<td class="tdresult">1</td>
				<td class="tdresult">2</td>
				<td class="tdresult">3</td>
				<td class="tdresult">4</td>
				<td class="tdresult">5</td>
			</tr>
		</table>
	</div>
</td>';
Cameron Chunn's avatar
Cameron Chunn committed
584 585 586 587 588 589 590
		}

		$out .= '</tr></table><br>';
		return $out;

	}

Alexia's avatar
Alexia committed
591 592 593 594 595 596
	/**
	 * Check Users rights
	 * @param  Article $article
	 * @param  int $build_id
	 * @return boolean
	 */
Cameron Chunn's avatar
Cameron Chunn committed
597
	public function rateCheckRights($article, $build_id) {
Cameron Chunn's avatar
Cameron Chunn committed
598 599
		global $wgUser;

600
		$rating_posted = $this->rateRead(null, null, $build_id);
Cameron Chunn's avatar
Cameron Chunn committed
601
		if (($rating_posted[0]['page_id']) && ($rating_posted[0]['user_id'])) {
602
			$rating_control = $this->rateRead($rating_posted[0]['page_id'], $rating_posted[0]['user_id'], null);
Cameron Chunn's avatar
Cameron Chunn committed
603 604
		}

Cameron Chunn's avatar
Cameron Chunn committed
605
		if (($rating_control[0]['page_id'] == $article->getID()) && ($rating_control[0]['user_id'] == $this->getUser()->getID())) {
Cameron Chunn's avatar
Cameron Chunn committed
606 607 608 609 610 611
			return true;
		} else {
			return false;
		}
	}

Cameron Chunn's avatar
Cameron Chunn committed
612
	/**
Cameron Chunn's avatar
Cameron Chunn committed
613 614 615
	 * Generates the rate form
	 * @param  array  $rate_value Values to populate form with
	 * @return string
Cameron Chunn's avatar
Cameron Chunn committed
616
	 */
Cameron Chunn's avatar
Cameron Chunn committed
617 618 619 620 621 622 623 624 625
	public function rateForm(array $rate_value) {
		global $wgUser;
		$rate_names = array(
			1 => 'Effectiveness',
			2 => 'Universality',
			3 => 'Innovation'
		);

		$rate_descr = array(
Alex's avatar
Alex committed
626 627 628
			1 => 'This criterion describes how effective the build does what it was designed for. That is, how much damage does a spiker build deal, a healer build heal or a protector build prevent? How good is the chance to get through the specified area with a running build or to reach and defeat the specified foes with a farming build?',
			2 => 'This criterion describes how flexible the build is when used in a situation slightly different from what the build was designed for. This includes the ability to change strategy in case a foe shows unexpected actions, in case an ally does not perform as expected, or when used in a different location than originally intended.',
			3 => 'This criterion describes how new the idea behind this build is. Does it use a new approach for dealing with a known task or even act as a precursor for dealing with a previously unconsidered task? To what extend is it expected to become a prototype for a new class of builds?'
Cameron Chunn's avatar
Cameron Chunn committed
629 630 631 632
		);

		// ---------- Loading form with values.
		if ($rate_value[0]) {
Alexia's avatar
Alexia committed
633
			$input	 = $rate_value[0]['rating'];
Cameron Chunn's avatar
Cameron Chunn committed
634
			$comment = $rate_value[0]['comment'];
Alexia's avatar
Alexia committed
635 636 637
			$submit	 = 'Save';
			$update	 = $rate_value[0]['rate_id'];
			$action	 = '&rating=update';
Cameron Chunn's avatar
Cameron Chunn committed
638
		} else {
Alexia's avatar
Alexia committed
639
			$input	 = false;
Cameron Chunn's avatar
Cameron Chunn committed
640
			$comment = '';
Alexia's avatar
Alexia committed
641 642 643
			$submit	 = 'Rate';
			$update	 = 0;
			$action	 = '';
Cameron Chunn's avatar
Cameron Chunn committed
644 645 646
		}

		// ---------- HEAD
Alex's avatar
Alex committed
647
		$out = '<div class="ratingform"><form method="post" action="' . $this->getTitle()->getFullURL('action=rate' . $action) . '"><table class="rating_table">';
Cameron Chunn's avatar
Cameron Chunn committed
648

Alex's avatar
Alex committed
649
		// ---------- Printing form.
Cameron Chunn's avatar
Cameron Chunn committed
650
		foreach ($rate_names as $key => $value) {
Alex's avatar
Alex committed
651
			$out .= ('<tr><td><span class="rating_cat" title="' . $rate_descr[$key] . '">' . $rate_names[$key] . '</span></td><td>');
Cameron Chunn's avatar
Cameron Chunn committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
			if ($value == 'Innovation') {
				if (($input) && ($input[$key - 1])) {
					$checked = ' checked ';
				} else {
					$checked = '';
				}
				$out .= ('<div class="rating_input">' . '<input name="p' . $key . '" type="checkbox" value="5"' . $checked . '>' . '</div>');
				$out .= ('</td></tr>');
			} else {
				for ($i = 0; $i <= 5; $i++) {
					if (($input) && ($input[$key - 1] == $i)) {
						$checked = ' checked ';
					} else {
						$checked = '';
					}
					$out .= ('<div class="rating_input"' . ' onMouseOver="swapColor(\'p' . $key . $i . '\',\'#f' . (8 - ($i + 3)) . 'f' . (8 - ($i + 3)) . 'f' . (8 - ($i + 3)) . '\');"' . ' onMouseOut="swapColor(\'p' . $key . $i . '\',\'#ffffff\');" id="p' . $key . $i . '">' . '<input name="p' . $key . '" type="radio" value="' . $i . '"' . $checked . '>' . '<span>' . $i . '</span></div>');
				}
			}
			$out .= ('</td></tr>');
		}

		$out .= ('<tr valign="top"><td><span class="rating_cat">Comments</span></td>' . '<td><textarea class="rating_text" name="comment" cols="10" rows="5">' . $comment . '</textarea>');
		$out .= ('<input name="ratingid" type="hidden" value="' . $update . '" />');
		$out .= ('</table><input class="button" type="submit" value="' . $submit . '" /></form></div><br>');
		return $out;
	}

Alexia's avatar
Alexia committed
679 680 681 682 683
	/**
	 * Generate rating rollback form
	 * @param  array  $rate_value values to populate form with
	 * @return string
	 */
Cameron Chunn's avatar
Cameron Chunn committed
684
	public function rateRollback(array $rate_value) {
Cameron Chunn's avatar
Cameron Chunn committed
685 686 687 688

		$submit = 'Remove';
		$update = $rate_value[0]['rate_id'];
		$action = '&rating=rollback';
Alex's avatar
Alex committed
689
		$default_comment = '';
Cameron Chunn's avatar
Cameron Chunn committed
690 691

		$out = ('<div class="ratingform"><form method="post" action="' . $this->getTitle()->getFullURL('action=rate' . $action) . '"><table class="rating_table">');
Alex's avatar
Alex committed
692
		$out .= ('<tr valign="top"><td><span class="rating_cat">Reason</span></td><td><textarea class="rating_text" name="reason" cols="10" rows="5">' . $default_comment . '</textarea>');
Cameron Chunn's avatar
Cameron Chunn committed
693 694 695 696 697
		$out .= ('<input name="rollback" type="hidden" value=1 /><input name="ratingid" type="hidden" value="' . $update . '" />');
		$out .= ('</table><input class="button" type="submit" value="' . $submit . '" /></form></div><br>');
		return $out;
	}

Alexia's avatar
Alexia committed
698 699 700 701 702
	/**
	 * Generate rating restore form
	 * @param  array  $rate_value values to populate form with
	 * @return string
	 */
Cameron Chunn's avatar
Cameron Chunn committed
703
	public function rateRestore(array $rate_value) {
Cameron Chunn's avatar
Cameron Chunn committed
704 705 706 707

		$submit = 'Restore';
		$update = $rate_value[0]['rate_id'];
		$action = '&rating=restore';
Alex's avatar
Alex committed
708
		$default_comment = '';
Cameron Chunn's avatar
Cameron Chunn committed
709 710

		$out = ('<div class="ratingform"><form method="post" action="' . $this->getTitle()->getFullURL('action=rate' . $action) . '"><table class="rating_table">');
Alex's avatar
Alex committed
711
		$out .= ('<tr valign="top"><td><span class="rating_cat">Reason</span></td><td><textarea class="rating_text" name="reason" cols="10" rows="5">' . $default_comment . '</textarea>');
Cameron Chunn's avatar
Cameron Chunn committed
712 713 714 715 716
		$out .= ('<input name="restore" type="hidden" value=1 /><input name="ratingid" type="hidden" value="' . $update . '" />');
		$out .= ('</table><input class="button" type="submit" value="' . $submit . '" /></form></div><br>');
		return $out;
	}

Cameron Chunn's avatar
Cameron Chunn committed
717
	/**
Cameron Chunn's avatar
Cameron Chunn committed
718
	 * Get rating data array, can be used with other functions.
719
	 * @param  Article $article
Cameron Chunn's avatar
Cameron Chunn committed
720
	 * @return array
Cameron Chunn's avatar
Cameron Chunn committed
721 722
	 */
	public function rateGet(Article &$article) {
Cameron Chunn's avatar
Cameron Chunn committed
723 724 725
		global $wgUser, $wgRequest;
		$rate_input = array(
			'page_id' => $article->getID(),
Cameron Chunn's avatar
Cameron Chunn committed
726
			'user_id' => $this->getUser()->getID(),
Cameron Chunn's avatar
Cameron Chunn committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
			'comment' => $wgRequest->getText('comment'),
			'rollback' => $wgRequest->getText('rollback'),
			'restore' => $wgRequest->getText('restore'),
			'reason' => $wgRequest->getText('reason'),
			'rating' => array(
				$wgRequest->getText('p1'),
				$wgRequest->getText('p2'),
				$wgRequest->getText('p3')
			),
			'rate_id' => $wgRequest->getText('ratingid'),
			'error' => false,
			'error_msg' => ''
		);

		if ((($rate_input['rollback'] == 1) || ($rate_input['restore'] == 1)) && (strlen($rate_input['reason']) > 0)) {
Cameron Chunn's avatar
Cameron Chunn committed
742 743
			if ($this->getUser()->isAllowed('vote_rollback')) {
				$rate_input['admin_id'] = $this->getUser()->getID();
Cameron Chunn's avatar
Cameron Chunn committed
744
			} else {
Alexia's avatar
Alexia committed
745
				$rate_input['error']	 = true;
Cameron Chunn's avatar
Cameron Chunn committed
746 747 748 749 750 751 752 753 754
				$rate_input['error_msg'] = 'You are not admin.';
			}
			return $rate_input;
			;
		} else {
			# If innovation was not sent, set it to zero
			if (!is_numeric($rate_input['rating'][2])) {
				$rate_input['rating'][2] = 0;
			}
Alex's avatar
Alex committed
755
			# Check if rating was numeric, > than 0 and < than 6.
Cameron Chunn's avatar
Cameron Chunn committed
756
			if ((!is_numeric($rate_input['rating'][0])) || ($rate_input['rating'][0] > 5) || ($rate_input['rating'][0] < 0) || (!is_numeric($rate_input['rating'][1])) || ($rate_input['rating'][1] > 5) || ($rate_input['rating'][1] < 0)) {
Alexia's avatar
Alexia committed
757
				$rate_input['error']	 = true;
Cameron Chunn's avatar
Cameron Chunn committed
758 759
				$rate_input['error_msg'] = 'Please try again.';
			} elseif (strlen($rate_input['comment']) < 12) {
Alexia's avatar
Alexia committed
760
				$rate_input['error']	 = true;
Cameron Chunn's avatar
Cameron Chunn committed
761 762 763 764 765 766
				$rate_input['error_msg'] = 'Comment is too short..';
			}
			return $rate_input;
		}
	}

Cameron Chunn's avatar
Cameron Chunn committed
767
	/**
768 769
	 * Update rating in database.
	 * @param  array  $input
Cameron Chunn's avatar
Cameron Chunn committed
770
	 * @return void
Cameron Chunn's avatar
Cameron Chunn committed
771 772
	 */
	public function rateUpdate(array $input) {
Cameron Chunn's avatar
Cameron Chunn committed
773
		$dbw = wfGetDB(DB_MASTER);
Alexia's avatar
Alexia committed
774
		$dbw->startAtomic(__METHOD__);
Cameron Chunn's avatar
Cameron Chunn committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
		if ($input['rollback'] || $input['restore']) {
			$dbw->update('rating', array(
				'rollback' => $input['rollback'],
				'reason' => $input['reason'],
				'admin_id' => $input['admin_id']
			), array(
				'rate_id' => $input['rate_id']
			));
		} else {
			$dbw->update('rating', array(
				'comment' => $input['comment'],
				'rating1' => $input['rating'][0],
				'rating2' => $input['rating'][1],
				'rating3' => $input['rating'][2],
				'rollback' => $input['rollback'],
				'reason' => $input['reason'],
				'admin_id' => $input['admin_id']
			), array(
				'rate_id' => $input['rate_id']
			));
		}
Alexia's avatar
Alexia committed
796
		$dbw->endAtomic(__METHOD__);
Cameron Chunn's avatar
Cameron Chunn committed
797 798 799
		return;
	}

Alexia's avatar
Alexia committed
800 801 802 803 804 805 806
	/**
	 * delete rating from database
	 * @param  int $rate_id
	 * @param  int $page_id
	 * @param  int $user_id
	 * @return true
	 */
Cameron Chunn's avatar
Cameron Chunn committed
807
	public function rateDelete($rate_id, $page_id, $user_id) {
Cameron Chunn's avatar
Cameron Chunn committed
808
		//echo $rate_id . $page_id . $user_id;
Cameron Chunn's avatar
Cameron Chunn committed
809
		$dbw = wfGetDB(DB_MASTER);
Alexia's avatar
Alexia committed
810
		$dbw->startAtomic(__METHOD__);
Cameron Chunn's avatar
Cameron Chunn committed
811 812 813 814 815
		$dbw->delete('rating', array(
			'rate_id' => $rate_id,
			'page_id' => $page_id,
			'user_id' => $user_id
		));
Alexia's avatar
Alexia committed
816
		$dbw->endAtomic(__METHOD__);
Cameron Chunn's avatar
Cameron Chunn committed
817 818 819
		return true;
	}

Cameron Chunn's avatar
Cameron Chunn committed
820
	/**
821 822 823
	 * Save rating to database
	 * @param  array  $input
	 * @return true
Cameron Chunn's avatar
Cameron Chunn committed
824 825
	 */
	public function rateSave(array $input) {
Cameron Chunn's avatar
Cameron Chunn committed
826
		$dbw = wfGetDB(DB_MASTER);
Alexia's avatar
Alexia committed
827
		$dbw->startAtomic(__METHOD__);
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
		$dbw->insert(
			'rating',
			[
				'page_id' => $input['page_id'],
				'user_id' => $input['user_id'],
				'comment' => $input['comment'],
				'rollback' => $input['rollback'],
				'admin_id' => 0,
				'reason' => $input['reason'],
				'rating1' => $input['rating'][0],
				'rating2' => $input['rating'][1],
				'rating3' => $input['rating'][2],
				'ip_address' => ''
			],
			__METHOD__
		);
Alexia's avatar
Alexia committed
844
		$dbw->endAtomic(__METHOD__);
Cameron Chunn's avatar
Cameron Chunn committed
845 846 847
		return true;
	}

Cameron Chunn's avatar
Cameron Chunn committed
848 849
	/**
	 * Read ratings from db
850 851 852
	 * @param  int|null $page_id
	 * @param  int|null $user_id
	 * @param  int|null $rate_id
Cameron Chunn's avatar
Cameron Chunn committed
853 854
	 * @return array|false
	 */
855
	public function rateRead(?int $page_id, ?int $user_id, ?int $rate_id) {
856
		$dbr = wfGetDB(DB_SLAVE);
Cameron Chunn's avatar
Cameron Chunn committed
857

858 859 860 861 862
		$where = [];
		$limit = 1000;
		if ($rate_id > 1) {
			$where['rate_id'] = $rate_id;
			$limit = 1;
Cameron Chunn's avatar
Cameron Chunn committed
863
		} else {
864
			$where['page_id'] = $page_id;
Cameron Chunn's avatar
Cameron Chunn committed
865
			if ($user_id) {
866 867
				$where['user_id'] = $user_id;
				$limit = 1;
Cameron Chunn's avatar
Cameron Chunn committed
868 869
			}
		}
Alexia's avatar
Typ.  
Alexia committed
870
		$res = $dbr->select(
871 872 873 874 875 876
			['rating'],
			['*'],
			$where,
			__METHOD__,
			['LIMIT' => $limit]
		);
Cameron Chunn's avatar
Cameron Chunn committed
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

		$count = $dbr->numRows($res);
		if ($count > 0) {
			# Make list
			$i = 0;
			while ($row = $dbr->fetchObject($res)) {
				$rate_out[$i] = array(
					'page_id' => $row->page_id,
					'user_id' => $row->user_id,
					'comment' => $row->comment,
					'rollback' => $row->rollback,
					'admin_id' => $row->admin_id,
					'reason' => $row->reason,
					'rating' => array(
						$row->rating1,
						$row->rating2,
						$row->rating3
					),
					'rate_id' => $row->rate_id,
					'timestamp' => $row->timestamp
				);
				$i++;
			}
		} else {
			return false;
		}
		return $rate_out;
	}

}