Skip to content

Commit

Permalink
Create separate controllers for add and undo review
Browse files Browse the repository at this point in the history
  • Loading branch information
MusikAnimal committed Jun 22, 2016
1 parent 3284aad commit d4d166a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 17 deletions.
5 changes: 2 additions & 3 deletions public_html/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@
setReviewState( id, 'open' );

$.ajax( {
url: 'review/add',
url: 'review/undo',
data: {
id: id,
undo: true
id: id
},
dataType: 'json'
} ).done( function ( ret ) {
Expand Down
24 changes: 16 additions & 8 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,25 @@ function () use ( $slim ) {
} );
$slim->group( '(/)review/',
function () use ( $slim ) {
$slim->get( 'add', function () use ( $slim ) {
// TODO: Ideally the authentication step should be taken care of by a middleware
// but I haven't found a way to make it work with AJAX requests so far
if ( $slim->authManager->isAuthenticated() ) {
$page = new Controllers\Review( $slim );
$page->setDao( $slim->plagiabotDao );
$page();
} else {
// TODO: Ideally the authentication step should be taken care of by a middleware
// but I haven't found a way to make it work with AJAX requests so far
$requireAuth = function () use ( $slim ) {
if ( !$slim->authManager->isAuthenticated() ) {
echo json_encode( array( 'error' => 'Unauthorized' ) );
$slim->stop();
}
};

$slim->get( 'add', $requireAuth, function () use ( $slim ) {
$page = new Controllers\AddReview( $slim );
$page->setDao( $slim->plagiabotDao );
$page();
} )->name( 'add_review' );
$slim->get( 'undo', $requireAuth, function () use ( $slim ) {
$page = new Controllers\UndoReview( $slim );
$page->setDao( $slim->plagiabotDao );
$page();
} )->name( 'undo_review' );
} );
$slim->group( '/oauth/',
function () use ( $slim ) {
Expand Down
8 changes: 2 additions & 6 deletions src/Controllers/Review.php → src/Controllers/AddReview.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

use Wikimedia\Slimapp\Controller;

class Review extends CopyPatrol {
class AddReview extends CopyPatrol {

/**
* @param \Slim\Slim $slim Slim application
Expand All @@ -42,11 +42,7 @@ protected function handleGet() {
$timestamp = gmdate( 'c' );
$val = $this->request->get( 'val' );

if ( $undo ) {
$ret = $this->dao->insertCopyvioAssessment( $id, NULL, NULL, NULL );
} else {
$ret = $this->dao->insertCopyvioAssessment( $id, $val, $user, $timestamp );
}
$ret = $this->dao->insertCopyvioAssessment( $id, $val, $user, $timestamp );

// Return JSON with username and review timestamp if review was successful
if ( $ret === true ) {
Expand Down
62 changes: 62 additions & 0 deletions src/Controllers/UndoReview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* This file is part of CopyPatrol application
* Copyright (C) 2016 Niharika Kohli and contributors
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Niharika Kohli <nkohli@wikimedia.org>
* @copyright © 2016 Niharika Kohli and contributors.
*/
namespace Plagiabot\Web\Controllers;

use Wikimedia\Slimapp\Controller;

class UndoReview extends CopyPatrol {

/**
* @param \Slim\Slim $slim Slim application
*/
public function __construct( \Slim\Slim $slim = null ) {
parent::__construct( $slim );
}


protected function handleGet() {
$id = $this->request->get( 'id' );
$undo = (bool)$this->request->get( 'undo' );
$userData = $this->authManager->getUserData();
$user = $userData ? $userData->getName() : NULL;
// Get current UTC time as ISO 8601 timestamp.
$timestamp = gmdate( 'c' );

$ret = $this->dao->insertCopyvioAssessment( $id, NULL, NULL, NULL );

// Return JSON with username and review timestamp if unreview was successful
if ( $ret === true ) {
echo json_encode(
array(
'user' => $user,
'userpage' => $this->getUserPage( $user ),
'timestamp' => $this->formatTimestamp( $timestamp ),
'status' => NULL
) );
} else {
echo json_encode(
array(
'error' => 'false'
) );
}
}
}

0 comments on commit d4d166a

Please sign in to comment.