Skip to content

Commit

Permalink
Merge pull request #10 from amorim-cjs/Sprint-0
Browse files Browse the repository at this point in the history
Main UI updates
  • Loading branch information
amorim-cjs authored Dec 11, 2020
2 parents 01d056f + 77f88ec commit 640f190
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 42 deletions.
3 changes: 3 additions & 0 deletions source/src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
define('DB_USER', 'root');
define('DB_PWD', 'topsecret');

// error codes
define('USER_NOT_LOGGED_IN', '10');

?>
89 changes: 89 additions & 0 deletions source/src/scigen/controllers/papers.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,95 @@ private function set($data){
$this->view->issue = $data['issue'];
$this->view->page = $data['page'];
}

public function upvote($doi){
session_start();

if (!Session::get('loggedin')){
// Save current DOI
$_SESSION['doi'] = $_GET['doi'];

header("Location: ". BASE_URL . "login/index?message=" . urlencode("You must be loggedin to submit a report"));

return 'Error: ' . USER_NOT_LOGGED_IN;
}
elseif (isset($_GET['doi'])){
//$doi = $_GET['doi'];
$userId = Session::get('user_id');
$this->model->registerInterest($doi, $userId);

//header("Location: ". BASE_URL . "reviews/get?doi=" . urlencode($doi));
return true;
}
}

public function downvote($doi){
session_start();

if (!Session::get('loggedin')){
// Save current DOI
$_SESSION['doi'] = $_GET['doi'];

header("Location: ". BASE_URL . "login/index?message=" . urlencode("You must be loggedin to submit a report"));

return 'Error: ' . USER_NOT_LOGGED_IN;
}
elseif (isset($_GET['doi'])){
//$doi = $_GET['doi'];
$userId = Session::get('user_id');
$this->model->unregisterInterest($doi, $userId);

//header("Location: ". BASE_URL . "reviews/get?doi=" . urlencode($doi));
return false;
}
}

public function switchVote(){
session_start();

if (Session::get('loggedin') && isset($_GET['doi'])){
$userId = Session::get('user_id');
$doi = $_GET['doi'];

//var_dump($userId);
//$interests = $this->countvote($doi, $userId);

$interested = $this->model->isInterested($doi, $userId);//$interests[1];

if ($interested) $interested = $this->downvote($doi);
else $interested = $this->upvote($doi);

$interests = $this->countvote($doi, $userId);

$response = [ 'count' => $interests[0], 'interest' => $interests[1] ];
http_response_code(200);
header('Content-type: application/json');
//var_dump($interests);
echo json_encode($response);
}
else{
http_response_code(401);
echo "invalid request";
}

}

public function countvote($doi, $userId = NULL){
$interested = $this->model->fetchInterest($doi);
if (empty($interested)) return array(0, false);

$interestArray = explode(',', $interested);

$votes = count($interestArray) - 1;

if (!is_null($userId)){
$interestFlag = in_array($userId, $interestArray);
return array($votes, $interestFlag);
}

return array($votes, false);
}

}

?>
28 changes: 11 additions & 17 deletions source/src/scigen/controllers/reviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function register(){
elseif (isset($_POST['delete'])){
$this->model->deleteReview($_POST['review_id']);
header('Location: ' . BASE_URL . 'reviews/get?doi=' . $_POST['doi']);
return 204;
http_response_code(204);
}
elseif (isset($_POST['cancel'])){
header('Location: ' . BASE_URL . 'reviews/get?doi=' . $_POST['doi']);
Expand Down Expand Up @@ -80,6 +80,8 @@ public function confirm(){
}

public function get($doi0=NULL, $doi1=NULL){
session_start();

if (!is_null($doi0) && !is_null($doi1)) $_GET['doi'] = $doi0."/".$doi1;
if (isset($_GET['doi']) && !empty($_GET['doi'])){
$this->view->doi = $_GET['doi'];
Expand All @@ -88,6 +90,11 @@ public function get($doi0=NULL, $doi1=NULL){

$this->view->paper_data =
$this->model->getPaperInfo($_GET['doi']);

if (Session::get('loggedin')){
$this->view->upvoted = $this->isInterested($this->view->paper_data['interested'],$_SESSION['user_id']);
}

$this->view->render('reviews/get');
}
else{
Expand All @@ -96,22 +103,9 @@ public function get($doi0=NULL, $doi1=NULL){

}

public function upvote(){
session_start();

if (!Session::get('loggedin')){
// Save current DOI
$_SESSION['doi'] = $_POST['doi'];

header("Location: ". BASE_URL . "login/index?message=" . urlencode("You must be loggedin to submit a report"));

return;
}
elseif (isset($_SESSION['doi'])){
$doi = $_SESSION['doi'];
$userId = Session::get('user_id');
$this->model->registerInterest($doi, $userId);
}
private function isInterested($interestString, $userId){
$interestArray = explode(',', $interestString);
return in_array($userId, $interestArray);
}

}
Expand Down
64 changes: 64 additions & 0 deletions source/src/scigen/models/papers_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,69 @@ public function getPapers(){
->fetchAll(PDO::FETCH_ASSOC);
}

public function registerInterest($doi, $userId){
// Check current interest
$interestedFlag = $this->isInterested($doi, $userId);

if ($interestedFlag) return true;

//Register interest in DB
$sql = "UPDATE papers SET interested = concat(interested, :user_id, ',') WHERE DOI=:doi;";
$stmt = $this->db->prepare($sql);

$stmt->bindValue(":doi", "$doi");
$stmt->bindValue(":user_id", "$userId");

$stmt->execute();
}

public function unregisterInterest($doi, $userId){
// Get interested users' list
$interestedUsers = $this->fetchInterest($doi);
$interestArray = explode(',', $interestedUsers);

$newInterestedArray = array_diff($interestArray, array($userId));
$newInterested = implode(',', $newInterestedArray);

// Send to DB
$sql = "UPDATE papers SET interested = :interested WHERE DOI=:doi;";
$stmt = $this->db->prepare($sql);

$stmt->bindValue(":doi", "$doi");
$stmt->bindValue(":interested", "$newInterested");

$stmt->execute();

}

public function fetchInterest($doi){
$sql = "SELECT interested FROM papers WHERE DOI=:doi;";
$stmt = $this->db->prepare($sql);

$stmt->bindValue(":doi", "$doi");

$stmt->execute();

$interested = $stmt->fetchAll(PDO::FETCH_COLUMN);

return $interested[0];
}

public function isInterested($doi, $userId){
// Check current interest
$interestedUsers = $this->fetchInterest($doi);
$interestArray = explode(',', $interestedUsers);

$interestedFlag = in_array($userId, $interestArray);

return $interestedFlag;
}

private function countInterested($doi){
$interestedUsers = $this->fetchInterest($doi);
$interestArray = explode(',', $interestedUsers);

return count($interestArray) - 1;
}
}
?>
14 changes: 4 additions & 10 deletions source/src/scigen/models/reviews_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getReviews($doi){
public function getPaperInfo($doi){
$sql = "SELECT title, authors,
journal, year, volume, issue, page,
rep_success, rep_fail, tricky, possible, partial
rep_success, rep_fail, tricky, possible, partial, interested
FROM papers WHERE doi=:doi";

$stmt = $this->db->prepare($sql);
Expand All @@ -81,6 +81,9 @@ public function getPaperInfo($doi){
$stmt->execute();

$paper = $stmt->fetch(PDO::FETCH_ASSOC);
$interest = explode(',', $paper['interested']);
$paper['interest'] = count($interest) - 1;
//unset($paper['interested']);

return $paper;
}
Expand All @@ -95,15 +98,6 @@ public function deleteReview($revId){
$stmt->execute();
}

public function registerInterest($doi, $userId){
$sql = "UPDATE reviews SET interested = concat(interested, :user_id, ' ') WHERE DOI=:doi;";
$stmt = $this->db->prepare($sql);

$stmt->bindValue(":doi", "$doi");
$stmt->bindValue(":user_id", "$userId");

$stmt->execute();
}

}
?>
21 changes: 21 additions & 0 deletions source/src/scigen/views/layout/reviewHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
width:100%;
text-align:center;
}

form {
display: inline;
}
</style>

<script>
Expand Down Expand Up @@ -154,6 +158,23 @@ function leftCorner(){
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="<?=BASE_URL . 'assets/scripts/clamp.js'?>"></script>
<script>
function switchInterest(){
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200){
console.log(this.responseText);
var response = JSON.parse(this.responseText);
var bstr = '+ ' + response.count;
document.getElementById('vote-btn').className = response.interest ?
"w3-btn w3-theme-l5" : "w3-btn w3-theme";
document.getElementById('vote-btn').innerHTML = bstr;
}
};
xhr.open("GET", "/papers/switchVote?doi=" + "<?=urlencode($_GET['doi'])?>", true);
xhr.send();

}

function formatAuthors(){
var header = document.getElementById('paper-title');
const lines = screen.height < 450 ? 2 : 3;
Expand Down
Loading

0 comments on commit 640f190

Please sign in to comment.