public static function updateReview() { // Process updating review $reviews = ReviewsDB::getReviewsBy('reviewId', $_SESSION['arguments']); if (empty($reviews)) { HomeView::show(); header('Location: /' . $_SESSION['base']); } elseif ($_SERVER["REQUEST_METHOD"] == "GET") { $_SESSION['reviews'] = $reviews; ReviewView::showUpdate(); } else { $parms = $reviews[0]->getParameters(); $parms['score'] = array_key_exists('score', $_POST) ? $_POST['score'] : $reviews[0]->getScore(); $parms['review'] = array_key_exists('review', $_POST) ? $_POST['review'] : $reviews[0]->getReview(); $newReview = new Review($parms); $newReview->setReviewId($reviews[0]->getReviewId()); $review = ReviewsDB::updateReview($newReview); if ($review->getErrorCount() != 0) { $_SESSION['reviews'] = array($newReview); ReviewView::showUpdate(); } else { HomeView::show(); header('Location: /' . $_SESSION['base']); } } }
public function testGetReviewByReviewerName() { $myDb = DBMaker::create('ptest'); Database::clearDB(); $db = Database::getDB('ptest', 'C:\\xampp\\myConfig.ini'); $reviews = ReviewsDB::getReviewsBy('reviewerName', 'Alice'); $this->assertEquals(count($reviews), 3, 'Alice should have three reviews'); foreach ($reviews as $review) { $this->assertTrue(is_a($review, "Review"), 'The returned values should be Review objects'); $this->assertTrue(empty($review->getErrors()), "The returned reviews should have no errors"); } }
public static function show() { $arguments = array_key_exists('arguments', $_SESSION) ? $_SESSION['arguments'] : 0; $user = $_SESSION['user']; if (!is_null($user)) { $_SESSION['user'] = $user; $_SESSION['userSubmissions'] = SubmissionsDB::getSubmissionsBy('submitterName', $user->getUserName()); $_SESSION['userReviews'] = ReviewsDB::getReviewsBy('reviewerName', $user->getUserName()); UserView::show(); } else { HomeView::show(); } }
public static function updateReview($review) { // Update a review try { $db = Database::getDB(); if (is_null($review) || $review->getErrorCount() > 0) { return $review; } $checkReview = ReviewsDB::getReviewsBy('reviewId', $review->getReviewId()); if (empty($checkReview)) { $review->setError('reviewId', 'REVIEW_DOES_NOT_EXIST'); } elseif ($checkReview[0]->getSubmissionId() != $review->getSubmissionId()) { $review->setError('reviewId', 'REVIEW_HAS_WRONG_SUBMISSION_ID'); } elseif ($checkReview[0]->getreviewerName() != $review->getReviewerName()) { $review->setError('reviewId', 'REVIEWER_NAME_DOES_NOT_MATCH'); } if ($review->getErrorCount() > 0) { return $review; } $query = "UPDATE Reviews SET review = :review, score = :score\r\n\t \t\t\t WHERE reviewId = :reviewId"; $statement = $db->prepare($query); $statement->bindValue(":review", $review->getReview()); $statement->bindValue(":score", $review->getScore()); $statement->bindValue(":reviewId", $review->getReviewId()); $statement->execute(); $statement->closeCursor(); } catch (Exception $e) { // Not permanent error handling $review->setError('reviewId', 'REVIEW_COULD_NOT_BE_UPDATED'); } return $review; }
Database::clearDB(); $db = Database::getDB('ptest', 'C:\\xampp\\myConfig.ini'); $beforeCount = count(ReviewsDB::getReviewsBy()); $reviews = ReviewsDB::getReviewsBy('reviewId', 1); $currentReview = $reviews[0]; echo "Current review: {$currentReview}<br>"; $parms = $currentReview->getParameters(); $parms['review'] = 'new review text'; $newReview = new Review($parms); $newReview->setReviewId($currentReview->getReviewId()); $updatedReview = ReviewsDB::updateReview($newReview); echo "Updated review: {$updatedReview}<br>"; $afterCount = count(ReviewsDB::getReviewsBy()); echo "<br>Count before update = {$beforeCount}<br>"; echo "Count after = {$afterCount}<br>"; ?> <h2>It should get a review by reviewer name</h2> <?php DBMaker::create('ptest'); Database::clearDB(); $db = Database::getDB('ptest', 'C:\\xampp\\myConfig.ini'); $reviews = ReviewsDB::getReviewsBy('reviewerName', 'Alice'); echo "<br>Number of reviews by Alice is " . count($reviews); foreach ($reviews as $review) { echo "<br>Review: {$review}<br>"; } ?> </body> </html>