Ejemplo n.º 1
0
 public static function getReviewTitle(Review $review)
 {
     $format = " %s this course";
     if ($review->getListId() == UserCourse::LIST_TYPE_CURRENT) {
         $title = ' is taking this course right now';
     } else {
         $title = sprintf(" <strong>%s</strong> this course", strtolower($review->getProgress()));
     }
     $title .= $review->getHours() > 0 ? sprintf(", spending <strong>%s hours</strong> a week on it", $review->getHours()) : '';
     $title .= $review->getDifficultyId() ? sprintf(" and found the course difficulty to be <strong>%s</strong>", strtolower($review->getDifficulty())) : '';
     $title .= '.';
     return $title;
 }
Ejemplo n.º 2
0
 public function getReviewFeedbackDetails(\ClassCentral\SiteBundle\Entity\Review $review)
 {
     $rsm = new ResultSetMapping();
     $em = $this->container->get('doctrine')->getManager();
     $rsm = new ResultSetMapping();
     $rsm->addScalarResult('positive', 'positive');
     $rsm->addScalarResult('negative', 'negative');
     $rsm->addScalarResult('total', 'total');
     $q = $em->createNativeQuery("SELECT\n                SUM(if(helpful =0,1,0)) as negative,\n                SUM(if (helpful =1,1,0)) as positive,\n                count(*) as total FROM reviews_feedback\n                WHERE review_id = " . $review->getId(), $rsm);
     $result = $q->getResult();
     return $result[0];
 }
Ejemplo n.º 3
0
 /**
  * Creates/Updates a review
  * @param $courseId
  * @param $reviewData
  */
 public function saveReview($courseId, \ClassCentral\SiteBundle\Entity\User $user, $reviewData, $isAdmin = false)
 {
     $em = $this->em;
     $newReview = false;
     $course = $em->getRepository('ClassCentralSiteBundle:Course')->find($courseId);
     if (!$course) {
         return $this->getAjaxResponse(false, 'Course not found');
     }
     // Get the review object if it exists
     $review = null;
     if (isset($reviewData['reviewId']) && is_numeric($reviewData['reviewId'])) {
         // Its an edit. Get the review
         // Get the review
         $review = $em->getRepository('ClassCentralSiteBundle:Review')->find($reviewData['reviewId']);
         if (!$review) {
             return $this->getAjaxResponse(false, 'Review does not exist');
         }
         // Check if the user has access to edit the review
         // Either the user is an admin or the person who created the review
         $admin = $this->container->get('security.context')->isGranted('ROLE_ADMIN');
         if (!$admin && $user->getId() != $review->getUser()->getId()) {
             return $this->getAjaxResponse(false, 'User does not have access to edit the review');
         }
     } else {
         $newReview = true;
         $review = $em->getRepository('ClassCentralSiteBundle:Review')->findOneBy(array('user' => $user, 'course' => $course));
         // Admins can create multiple reviews - for adding external reviews
         if ($review && !$isAdmin) {
             return $this->getAjaxResponse(false, 'Review already exists');
         }
         $review = new \ClassCentral\SiteBundle\Entity\Review();
         $review->setUser($user);
         $review->setCourse($course);
     }
     // Get the offering
     if (isset($reviewData['offeringId']) && $reviewData['offeringId'] != -1) {
         $offering = $em->getRepository('ClassCentralSiteBundle:Offering')->find($reviewData['offeringId']);
         $review->setOffering($offering);
     }
     // check if the rating valid
     if (!isset($reviewData['rating']) && !is_numeric($reviewData['rating'])) {
         return $this->getAjaxResponse(false, 'Rating is required and expected to be a number');
     }
     // Check if the rating is in range
     if (!($reviewData['rating'] >= 1 && $reviewData['rating'] <= 5)) {
         return $this->getAjaxResponse(false, 'Rating should be between 1 to 5');
     }
     // If review exists its length should be atleast 20 words
     if (!empty($reviewData['reviewText']) && str_word_count($reviewData['reviewText']) < 20) {
         return $this->getAjaxResponse(false, 'Review should be at least 20 words long');
     }
     $review->setRating($reviewData['rating']);
     $review->setReview($reviewData['reviewText']);
     // Progress is required
     if (!isset($reviewData['progress'])) {
         return $this->getAjaxResponse(false, 'Progress is required');
     }
     // Progress
     if (isset($reviewData['progress']) && array_key_exists($reviewData['progress'], UserCourse::$progress)) {
         $review->setListId($reviewData['progress']);
         // Add/update the course to users library
         if (!$isAdmin) {
             // Do not add this t
             $userService = $this->container->get('user_service');
             $uc = $userService->addCourse($user, $course, $reviewData['progress']);
         }
     }
     // Difficulty
     if (isset($reviewData['difficulty']) && array_key_exists($reviewData['difficulty'], \ClassCentral\SiteBundle\Entity\Review::$difficulty)) {
         $review->setDifficultyId($reviewData['difficulty']);
     }
     // Level
     if (isset($reviewData['level']) && array_key_exists($reviewData['level'], \ClassCentral\SiteBundle\Entity\Review::$levels)) {
         $review->setLevelId($reviewData['level']);
     }
     // Effort
     if (isset($reviewData['effort']) && is_numeric($reviewData['effort']) && $reviewData['effort'] > 0) {
         $review->setHours($reviewData['effort']);
     }
     if ($isAdmin) {
         // Status
         if (isset($reviewData['status']) && array_key_exists($reviewData['status'], \ClassCentral\SiteBundle\Entity\Review::$statuses)) {
             $review->setStatus($reviewData['status']);
         }
         // External reviewer name
         if (isset($reviewData['externalReviewerName'])) {
             $review->setReviewerName($reviewData['externalReviewerName']);
         }
         // External review link
         if (isset($reviewData['externalReviewLink']) && filter_var($reviewData['externalReviewLink'], FILTER_VALIDATE_URL)) {
             $review->setExternalLink($reviewData['externalReviewLink']);
         }
     }
     $user->addReview($review);
     $em->persist($review);
     $em->flush();
     $this->clearCache($courseId);
     // If its an actual user and not an anonymous user update the session information
     if ($user->getEmail() != \ClassCentral\SiteBundle\Entity\User::REVIEW_USER_EMAIL) {
         //Update the users review history in session
         $userSession = $this->container->get('user_session');
         $userSession->saveUserInformationInSession();
         $showNotification = true;
         if (isset($reviewData['showNotification'])) {
             $showNotification = $reviewData['showNotification'];
         }
         if ($showNotification) {
             if ($newReview) {
                 $userSession->notifyUser(UserSession::FLASH_TYPE_SUCCESS, 'Review created', sprintf("Review for <i>%s</i> created successfully", $review->getCourse()->getName()));
             } else {
                 $userSession->notifyUser(UserSession::FLASH_TYPE_SUCCESS, 'Review updated', sprintf("Your review for <i>%s</i> has been updated successfully", $review->getCourse()->getName()));
             }
         }
     }
     // Send a message in Slack
     if ($newReview) {
         $message = ReviewUtility::getRatingStars($review->getRating()) . "\nReview {$review->getId()} created for Course *" . $review->getCourse()->getName() . "*" . "\n *{$review->getUser()->getDisplayName()}*" . ReviewUtility::getReviewTitle($review);
         if ($review->getReview()) {
             $message .= "\n\n" . $review->getReview();
         }
         $message .= "\n" . $this->container->getParameter('baseurl') . $this->container->get('router')->generate('review_edit', array('reviewId' => $review->getId()));
         $message = str_replace('<strong>', '_', $message);
         $message = str_replace('</strong>', '_', $message);
         $this->container->get('slack_client')->to('#cc-activity-user')->from($review->getUser()->getDisplayName())->withIcon($this->container->get('user_service')->getProfilePic($review->getUser()->getId()))->send($message);
     }
     return $review;
 }