public function substantialReviewsAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $query = $em->createQueryBuilder();
     $created = new \DateTime();
     $created->modify('-2 month');
     $query->add('select', 'r')->add('from', 'ClassCentralSiteBundle:Review r')->andWhere('r.status < :status AND r.created > :created')->setParameter('status', Review::REVIEW_NOT_SHOWN_STATUS_LOWER_BOUND)->setParameter('created', $created->format('Y-m-d'));
     $allReviews = $query->getQuery()->getResult();
     $reviews = array();
     foreach ($allReviews as $review) {
         if ($review->getUser()->getId() == User::REVIEW_USER_ID) {
             continue;
         }
         if (strlen($review->getReview()) > 200 && $review->getRating() >= 4) {
             $reviews[] = ReviewUtility::getReviewArray($review);
         }
     }
     return $this->render('ClassCentralSiteBundle:Review:substantialReviews.html.twig', array('reviews' => $reviews));
 }
 public function getReviewsArray($courseId)
 {
     $course = $this->em->getRepository('ClassCentralSiteBundle:Course')->findOneById($courseId);
     $reviewEntities = $this->em->createQuery("\n               SELECT r,f, LENGTH (r.review) as reviewLength from ClassCentralSiteBundle:Review r JOIN r.course c LEFT JOIN r.fbSummary f WHERE c.id = {$courseId}\n                ORDER BY r.score DESC")->getResult();
     $r = array();
     $reviewCount = 0;
     $ratingCount = 0;
     $ratingsBreakdown = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0);
     foreach ($reviewEntities as $review) {
         $review = $review[0];
         if ($review->getStatus() < ReviewEntity::REVIEW_NOT_SHOWN_STATUS_LOWER_BOUND) {
             $ratingCount++;
             $ratingsBreakdown[$review->getRating()]++;
             if (!$review->getIsRating()) {
                 // Hide the review table entries that are purely rating
                 $r[] = ReviewUtility::getReviewArray($review);
                 $reviewCount++;
             }
         }
     }
     $reviews = array();
     $reviews['count'] = $ratingCount;
     $reviews['ratingCount'] = $ratingCount;
     $reviews['reviewCount'] = $reviewCount;
     $reviews['ratingsBreakdown'] = $ratingsBreakdown;
     $reviews['reviews'] = $r;
     return $reviews;
 }
 /**
  * Renders the users profile
  * @param $slug user id or username
  */
 public function profileAction(Request $request, $slug, $tab)
 {
     $tabs = array('transcript', 'interested', 'reviews', 'edit-profile');
     $em = $this->getDoctrine()->getManager();
     $cl = $this->get('course_listing');
     $userService = $this->get('user_service');
     $loggedInUser = $this->getUser();
     if (!in_array($tab, $tabs)) {
         // Invalid tab. Do a 301 redirect
         return $this->redirect($this->get('router')->generate('user_profile', array('slug' => $slug)), 301);
     }
     if (is_numeric($slug)) {
         $user_id = intval($slug);
         // Do not show profile pages for these user ids
         if ($user_id == User::SPECIAL_USER_ID || $user_id == User::REVIEW_USER_ID) {
             // User not found
             throw new \Exception("User {$slug} not found");
         }
         $user = $em->getRepository('ClassCentralSiteBundle:User')->find($user_id);
         if ($user->getHandle()) {
             // Redirect the user to the profile url
             $url = $this->get('router')->generate('user_profile_handle', array('slug' => $user->getHandle(), 'tab' => $tab == 'transcript' ? null : $tab));
             return $this->redirect($url, 301);
         }
     } else {
         $user = $em->getRepository('ClassCentralSiteBundle:User')->findOneBy(array('handle' => $slug));
     }
     if (!$user) {
         // User not found
         throw new \Exception("User {$slug} not found");
     }
     // if tab is edit-profile. Do some security checks
     if ($tab == 'edit-profile') {
         if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
             // Logged in user
             $loggedInUser = $this->get('security.context')->getToken()->getUser();
             if ($user->getId() != $loggedInUser->getId()) {
                 // Does not have access to the edit profile tab. Redirect to transcript
                 return $this->redirect($this->get('router')->generate('user_profile', array('slug' => $user->getId())), 301);
             }
         } else {
             // Does not have access to the edit profile tab. Redirect to transcript
             return $this->redirect($this->get('router')->generate('user_profile', array('slug' => $user->getId())), 301);
         }
     }
     // User might have a private prifle
     if ($user->getIsPrivate() && !$this->isCurrentUser($user)) {
         throw $this->createNotFoundException("Page does not exist");
     }
     // User might not have a profile
     $profile = $user->getProfile() ? $user->getProfile() : new Profile();
     // Get users course listing. This is the same function on My Courses page
     // and contains additional information related to pagination
     $clDetails = $cl->userLibrary($user, $request);
     $reviews = array();
     foreach ($user->getReviews() as $review) {
         $r = ReviewUtility::getReviewArray($review);
         $reviews[$r['course']['id']] = $r;
     }
     $reviewedCourses = array();
     if (!empty($clDetails['reviewedCourses']['hits']['hits'])) {
         foreach ($clDetails['reviewedCourses']['hits']['hits'] as $reviewedCourse) {
             $reviewedCourses[$reviewedCourse['_source']['id']] = $reviewedCourse['_source'];
         }
     }
     // Check if a change of email has been issued
     $changeEmail = null;
     $userPrefs = $user->getUserPreferencesByTypeMap();
     if ($this->isCurrentUser($user) && isset($userPrefs[UserPreference::USER_PROFILE_UPDATE_EMAIL])) {
         // An change of email request was issued. Check whether it is still valid
         $pref = $userPrefs[UserPreference::USER_PROFILE_UPDATE_EMAIL];
         $values = json_decode($pref->getValue(), true);
         $verifyTokenService = $this->get('verification_token');
         $tokenEntity = $verifyTokenService->get($values['token']);
         if ($tokenEntity) {
             // Email has been changed and the token is still valid
             $changeEmail = $values['email'];
         }
     }
     // Show a message if the profile is marked for deletion
     $deleteAccount = false;
     if ($this->isCurrentUser($user) && isset($userPrefs[UserPreference::USER_PROFILE_DELETE_ACCOUNT])) {
         $deleteAccount = true;
     }
     // Build an array for credential details.
     $credService = $this->get('es_credentials');
     $credReviews = $user->getCredentialReviews();
     $creds = array();
     foreach ($credReviews as $credReview) {
         $creds[] = array('cred' => $credService->findBySlug($credReview->getCredential()->getSlug()), 'review' => $credReview);
     }
     return $this->render('ClassCentralSiteBundle:Profile:profile.html.twig', array('user' => $user, 'profile' => $profile, 'listTypes' => UserCourse::$transcriptList, 'coursesByLists' => $clDetails['coursesByLists'], 'reviews' => $reviews, 'reviewedCourses' => $reviewedCourses, 'degrees' => Profile::$degrees, 'profilePic' => $userService->getProfilePic($user->getId()), 'changeEmail' => $changeEmail, 'deleteAccount' => $deleteAccount, 'tab' => $tab, 'userCreds' => $creds));
 }