jhuesser/bzu-jodel-clone

View on GitHub
functions/votes.php

Summary

Maintainability
C
1 day
Test Coverage
<?php
 
/**
*
* @return integer $jodel2vote The ID of the post to vote
* @return string $how2vote "Up" or "down"
*
* @author Jonas Hüsser
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @since 0.3
*/
Function `voteJodel` has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
Method `voteJodel` has 63 lines of code (exceeds 25 allowed). Consider refactoring.
The function voteJodel() has an NPath complexity of 435. The configured NPath complexity threshold is 200.
The function voteJodel() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
function voteJodel($jodel2vote, $how2vote){
global $apiroot, $baseurl, $config;
//Get the post to upvote and users who voted this post
$callurl = $apiroot . "jodels?transform=1&filter=jodelID,eq," . $jodel2vote;
$jodeljson = getCall($callurl);
$callurl = $apiroot . "jodelvotes?transform=1&filter=jodelIDFK,eq," . $jodel2vote;
$votejson = getCall($callurl);
$votes = json_decode($votejson,true);
//Check if ID of the user already voted this post
foreach($votes['jodelvotes'] as $vote){
if($vote['userIDFK'] == $_SESSION['userid']){
$voted = true;
}
}
//If user hasn't voted for this post yet
if(!$voted){
$jodel = json_decode($jodeljson, true);
//Get current votes, score and author of the post, add 1 to vote and score
foreach($jodel['jodels'] as $post){
$votes = $post['votes_cnt'];
$score = $post['score'];
$author = $post['jodlerIDFK'];
if ($how2vote == "up"){
$votes++;
$score = $score + $config->postmeta['get_upvote'];
} elseif ($how2vote == "down"){
$votes--;
$score = $score - $config->postmeta['get_downvote']; }
}
//Update votes & score of post in DB
$postfields = "{\n \n \"votes_cnt\": \"$votes\",\n \"score\": \"$score\"\n}";
$callurl = $apiroot . "jodels/" . $jodel2vote;
$voted = putCall($callurl,$postfields);
$userid = $_SESSION['userid'];
//Wirte to DB, that this user now voted on this post
$postfields = "{\n \n \"userIDFK\": \"$userid\",\n \"jodelIDFK\": \"$jodel2vote\"\n}";
$callurl = $apiroot . "jodelvotes";
Avoid unused local variables such as '$uservoted'.
$uservoted = postCall($callurl,$postfields);
 
//Get current karma of post author
$callurl = $apiroot . "jodlers?transform=1&filter=jodlerID,eq," . $author;
$authorkarmajson = getCall($callurl);
$authorkarma = json_decode($authorkarmajson, true);
foreach($authorkarma['jodlers'] as $user){
$karmaFromAuthor = $user['karma'];
}
 
//Get current karma of voter
$callurl = $apiroot . "jodlers?transform=1&filter=jodlerID,eq," . $userid;
$voterkarmajson = getCall($callurl);
$voterkarma = json_decode($voterkarmajson, true);
foreach($voterkarma['jodlers'] as $user){
$voterKarma = $user['karma'];
}
 
//incerase karma of the author, update it in DB
if ($how2vote == "up"){
$karmaFromAuthor = $karmaFromAuthor + $config->karma_calc['get_upvote'];
} elseif ($how2vote == "down"){
$karmaFromAuthor = $karmaFromAuthor - $config->karma_calc['get_downvote'];
}
$postfields = "{\n \n \"karma\": \"$karmaFromAuthor\"\n}";
$callurl = $apiroot . "jodlers/" . $author;
Avoid unused local variables such as '$karmaupdated'.
$karmaupdated = putCall($callurl, $postfields);
 
//incerase the karma of the voter (current user) and update it in DB
if ($how2vote == "up"){
$voterKarma = $voterKarma + $config->karma_calc['do_upvote'];
} elseif ($how2vote == "down"){
$voterKarma = $voterKarma - $config->karma_calc['do_downvote'];
}
$postfields = "{\n \n \"karma\": \"$voterKarma\"\n}";
$callurl = $apiroot . "jodlers/" . $userid;
$karmaupdated = putCall($callurl, $postfields);
 
} else {
//user has already voted on this post
$_SESSION['errorMsg'] = "Already voted";
}
//redirect again to jodels.php to show clean URL in browser
header('Location: ' . $baseurl . 'jodels.php#' . $jodel2vote);
 
 
}
 
 
/**
*
* @return integer $comment2vote The ID of the comment to vote
* @return string $how2vote "Up" or "down"
*
* @author Jonas Hüsser
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @since 0.3
*/
Function `voteComment` has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
Method `voteComment` has 65 lines of code (exceeds 25 allowed). Consider refactoring.
The function voteComment() has an NPath complexity of 435. The configured NPath complexity threshold is 200.
The function voteComment() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
function voteComment( $comment2vote, $how2vote){
global $apiroot, $baseurl, $config;
$userid = $_SESSION['userid'];
$apiroot = $config->apiUrl;
$commentsjson = getCall($apiroot . "comments?transform=1&filter=commentID,eq," . $comment2vote);
$votejson = getCall($apiroot . "commentvotes?transform=1&filter=commentIDFK,eq," . $comment2vote);
$votes = json_decode($votejson,true);
foreach($votes['commentvotes'] as $vote){
if($vote['jodlerIDFK'] == $userid){
$voted = true;
}
}
if(!$voted){
$comment = json_decode($commentsjson, true);
foreach($comment['comments'] as $post){
$votes = $post['votes_cnt'];
$score = $post['score'];
$author = $post['jodlerIDFK'];
$postID = $post['jodelIDFK'];
if ($how2vote == "up"){
$votes++;
$score = $score + $config->postmeta['get_upvote'];
} elseif($how2vote == "down"){
$votes--;
$score = $score - $config->postmeta['get_downvote'];
}
}
//Update votes & score of post in DB
$postfields = "{\n \n \"votes_cnt\": \"$votes\",\n \"score\": \"$score\"\n}";
$callurl = $apiroot . "comments/" . $comment2vote;
$voted = putCall($callurl,$postfields);
$userid = $_SESSION['userid'];
//Wirte to DB, that this user now voted on this post
$postfields = "{\n \n \"jodlerIDFK\": \"$userid\",\n \"commentIDFK\": \"$comment2vote\"\n}";
$callurl = $apiroot . "commentvotes";
Avoid unused local variables such as '$uservoted'.
$uservoted = postCall($callurl,$postfields);
 
//Get current karma of post author
$callurl = $apiroot . "jodlers?transform=1&filter=jodlerID,eq," . $author;
$authorkarmajson = getCall($callurl);
$authorkarma = json_decode($authorkarmajson, true);
foreach($authorkarma['jodlers'] as $user){
$karmaFromAuthor = $user['karma'];
}
 
//Get current karma of voter
$callurl = $apiroot . "jodlers?transform=1&filter=jodlerID,eq," . $userid;
$voterkarmajson = getCall($callurl);
$voterkarma = json_decode($voterkarmajson, true);
foreach($voterkarma['jodlers'] as $user){
$voterKarma = $user['karma'];
}
 
//incerase karma of the author, update it in DB
if ($how2vote == "up"){
$karmaFromAuthor = $karmaFromAuthor + $config->karma_calc['get_upvote'];
} elseif ($how2vote == "down"){
$karmaFromAuthor = $karmaFromAuthor - $config->karma_calc['get_downvote'];
}
$postfields = "{\n \n \"karma\": \"$karmaFromAuthor\"\n}";
$callurl = $apiroot . "jodlers/" . $author;
Avoid unused local variables such as '$karmaupdated'.
$karmaupdated = putCall($callurl, $postfields);
 
//incerase the karma of the voter (current user) and update it in DB
if ($how2vote == "up"){
$voterKarma = $voterKarma + $config->karma_calc['do_upvote'];
} elseif ($how2vote == "down"){
$voterKarma = $voterKarma - $config->karma_calc['do_downvote'];
}
$postfields = "{\n \n \"karma\": \"$voterKarma\"\n}";
$callurl = $apiroot . "jodlers/" . $userid;
$karmaupdated = putCall($callurl, $postfields);
 
} else {
//user has already voted on this post
$_SESSION['errorMsg'] = "Already voted";
}
header('Location: ' . $baseurl . 'comments.php?showcomment=' . $postID . '#' . $comment2vote);
 
 
}