public function getBody()
 {
     $o = $this->entity;
     // The offering
     $b = array();
     // The session
     // Offerings that are not valid should not be passed here
     if ($o->getStatus() == Offering::COURSE_NA) {
         throw new \Exception("The offering is not available any more");
     }
     $b['id'] = $o->getId();
     $b['url'] = $o->getUrl();
     $b['displayDate'] = $o->getDisplayDate();
     $b['startDate'] = $o->getStartDate()->format('Y-m-d');
     $b['startTimeStamp'] = $o->getStartTimestamp();
     $b['microdataDate'] = $o->getMicrodataDate();
     $b['status'] = $o->getStatus();
     $b['courseId'] = $o->getCourse()->getId();
     $b['slug'] = $o->getCourse()->getSlug();
     // get the state
     $b['states'] = CourseUtility::getStates($o);
     $b['state'] = CourseUtility::calculateState($o);
     // Boost the state depending on the date. This value is used for sorting
     $b['state'] += CourseUtility::calculateBoost($b['states'], $o->getStartDate());
     return $b;
 }
 /**
  * HTML format for the blog
  * @param Course $course
  */
 public function blogFormat(Course $course)
 {
     $router = $this->container->get('router');
     $rs = $this->container->get('review');
     $line1 = '';
     // Course name
     $line2 = '';
     // Institution name
     $line3 = '';
     // Next Session
     $ratings = $rs->getRatings($course->getId());
     $reviews = $rs->getReviews($course->getId());
     // LINE 1
     $url = 'https://www.class-central.com' . $router->generate('ClassCentralSiteBundle_mooc', array('id' => $course->getId(), 'slug' => $course->getSlug()));
     $name = $course->getName();
     $line1 = "<a href='{$url}'><b>{$name}</b></a>";
     // LINE 2
     if ($course->getInstitutions()->count() > 0) {
         $ins = $course->getInstitutions()->first();
         $insName = $ins->getName();
         $line2 = "{$insName}";
     }
     if ($course->getInitiative()) {
         $line2 .= ' via ' . $course->getInitiative()->getName();
     } else {
         $line2 .= ' via Independent';
     }
     $line2 = "<i>{$line2}</i>";
     // LINE 3
     $nextOffering = CourseUtility::getNextSession($course);
     if ($nextOffering) {
         $displayDate = $nextOffering->getDisplayDate();
         $directUrl = $nextOffering->getUrl();
         $states = CourseUtility::getStates($nextOffering);
         if (in_array('past', $states)) {
             $displayDate = 'TBA';
         }
         if (in_array('selfpaced', $states)) {
             $displayDate = 'Self Paced';
         }
         $ratingsLine = '';
         if ($ratings > 0) {
             $formattedRatings = ReviewUtility::getRatingStars($ratings);
             $numRatings = $reviews['ratingCount'];
             $post = $numRatings == 1 ? 'rating' : 'ratings';
             $ratingsLine = " | {$formattedRatings} (<a href='{$url}#course-all-reviews'>{$numRatings} {$post}</a>) ";
         }
         $lineDesc = '';
         if ($course->getOneliner()) {
             $lineDesc = $course->getOneliner() . "<br/>";
         } elseif ($course->getDescription()) {
             $lineDesc = $course->getDescription() . "<br/>";
         }
         $line3 = "<b> <a href='{$directUrl}' target='_blank'>Go To Class</a> {$ratingsLine} | Next Session : {$displayDate} </b><br/>";
     }
     return $line1 . '<br/>' . $line2 . '<br/>' . $lineDesc . $line3 . '<br/>';
 }
 /**
  * Takes a course entity and builds an array so that
  * it can be serialized and saved in a cache
  * @param Course $course
  */
 public function getCourseArray(Course $course)
 {
     $courseDetails = array();
     $courseDetails['id'] = $course->getId();
     $courseDetails['name'] = $course->getName();
     $courseDetails['videoIntro'] = $course->getVideoIntro();
     $courseDetails['videoEmbedUrl'] = $this->getVideoEmbedUrl($course->getVideoIntro());
     $courseDetails['length'] = $course->getLength();
     $courseDetails['slug'] = $course->getSlug();
     $courseDetails['url'] = $course->getUrl();
     $courseDetails['nextOffering'] = null;
     $courseDetails['imageUrl'] = CourseUtility::getImageUrl($course);
     $courseDetails['status'] = $course->getStatus();
     $courseDetails['certificate'] = $course->getCertificate();
     $courseDetails['verifiedCertificate'] = $course->getVerifiedCertificate();
     $courseDetails['workloadMin'] = $course->getWorkloadMin();
     $courseDetails['workloadMax'] = $course->getWorkloadMax();
     // Calculate the workload
     $workload = '';
     if ($course->getWorkloadMin() && $course->getWorkloadMax()) {
         if ($course->getWorkloadMin() == $course->getWorkloadMax()) {
             $workload = $course->getWorkloadMin() . ' hours/week';
         } else {
             $workload = $course->getWorkloadMin() . "-" . $course->getWorkloadMax() . ' hours/week';
         }
     }
     $courseDetails['workload'] = $workload;
     // Get the descriptions
     $desc = null;
     $shortDesc = $course->getDescription();
     // this text only. No html. Goes in the head description
     $longDesc = $course->getLongDescription();
     //html
     $syllabus = $course->getSyllabus();
     // html
     if (empty($longDesc)) {
         $desc = nl2br($shortDesc);
     } else {
         $desc = $longDesc;
     }
     if (strlen($shortDesc) > 500) {
         $shortDesc = substr($shortDesc, 0, 497) . '...';
     }
     $courseDetails['shortDesc'] = $shortDesc;
     $courseDetails['syllabus'] = $syllabus;
     $courseDetails['longDesc'] = $longDesc;
     $courseDetails['desc'] = $desc;
     $nextOffering = $course->getNextOffering();
     if ($nextOffering) {
         $courseDetails['nextOffering']['displayDate'] = $nextOffering->getDisplayDate();
         $courseDetails['nextOffering']['id'] = $nextOffering->getId();
         $courseDetails['nextOffering']['url'] = $nextOffering->getUrl();
         // Get the state of this session
         $courseDetails['state'] = null;
         $states = array_intersect(array('past', 'ongoing', 'selfpaced', 'upcoming'), CourseUtility::getStates($nextOffering));
         if (!empty($states)) {
             $courseDetails['nextOffering']['state'] = array_pop($states);
         }
     }
     $courseDetails['tags'] = array();
     foreach ($course->getTags() as $tag) {
         $name = $tag->getName();
         if (!empty($name)) {
             $courseDetails['tags'][] = $name;
         }
     }
     $courseDetails['listed'] = $this->getListedCount($course);
     // Stream
     $stream = $course->getStream();
     $courseDetails['stream']['name'] = $stream->getName();
     $courseDetails['stream']['slug'] = $stream->getSlug();
     $courseDetails['stream']['showInNav'] = $stream->getShowInNav();
     // Initiative
     $initiative = $course->getInitiative();
     $courseDetails['initiative']['name'] = '';
     if ($initiative != null) {
         $courseDetails['initiative']['name'] = $initiative->getName();
         $courseDetails['initiative']['url'] = $initiative->getUrl();
         $courseDetails['initiative']['tooltip'] = $initiative->getTooltip();
         $courseDetails['initiative']['code'] = strtolower($initiative->getCode());
     } else {
         $courseDetails['initiative']['name'] = 'Independent';
         $courseDetails['initiative']['code'] = 'independent';
         $courseDetails['initiative']['tooltip'] = 'Independent';
     }
     // Language
     $lang = array();
     if ($course->getLanguage()) {
         $l = $course->getLanguage();
         $lang['name'] = $l->getName();
         $lang['slug'] = $l->getSlug();
         $lang['code'] = $l->getCode();
     }
     $courseDetails['lang'] = $lang;
     // Institutions
     $courseDetails['institutions'] = array();
     foreach ($course->getInstitutions() as $institution) {
         $courseDetails['institutions'][] = array('name' => $institution->getName(), 'url' => $institution->getUrl(), 'slug' => $institution->getSlug(), 'isUniversity' => $institution->getIsUniversity());
     }
     // Instructors
     $courseDetails['instructors'] = array();
     foreach ($course->getInstructors() as $instructor) {
         $courseDetails['instructors'][] = $instructor->getName();
     }
     $courseDetails['instructorsSingleLineDisplay'] = $this->getInstructorsSingleLineDisplay($courseDetails['instructors']);
     // Check if the course has a duplicate course id
     if ($course->getDuplicateCourse()) {
         $duplicate = $course->getDuplicateCourse();
         $courseDetails['duplicate'] = array('id' => $duplicate->getId(), 'slug' => $duplicate->getSlug());
     }
     // Build an array for indepth review
     $indepthReview = array();
     if ($course->getIndepthReview()) {
         $ir = $course->getIndepthReview();
         $irUser = $ir->getUser();
         $indepthReview = array('summary' => $ir->getSummary(), 'rating' => $ir->getRating(), 'url' => $ir->getUrl(), 'user' => array('name' => $irUser->getDisplayName(), 'id' => $irUser->getId(), 'handle' => $irUser->getHandle()));
     }
     $courseDetails['indepthReview'] = $indepthReview;
     // Save interview data if exists
     $interview = array();
     if ($course->getInterview()) {
         $i = $course->getInterview();
         $interview = array('id' => $i->getId(), 'title' => $i->getTitle(), 'summary' => $i->getSummary(), 'instructorName' => $i->getInstructorName(), 'instructorPhoto' => $i->getInstructorPhoto(), 'url' => $i->getUrl());
     }
     $courseDetails['interview'] = $interview;
     // Credential details
     // Get the Credential
     $credential = array();
     if (!$course->getCredentials()->isEmpty()) {
         $cred = $course->getCredentials()->first();
         if ($cred->getStatus() < 100) {
             $credential['id'] = $cred->getId();
             $credential['name'] = $cred->getName();
             $credential['slug'] = $cred->getSlug();
             $credential['certificateName'] = '';
             $credential['certificateSlug'] = '';
             $formatter = $cred->getFormatter();
             $credential['certificateName'] = $formatter->getCertificateName();
             $credential['certificateSlug'] = $formatter->getCertificateSlug();
         }
     }
     $courseDetails['credential'] = $credential;
     return $courseDetails;
 }
 /**
  * Boost the weight of courses that are upcoming or recent
  * @param Course $course
  */
 private function getCourseWeight(Course $course)
 {
     $weight = 0;
     $ns = CourseUtility::getNextSession($course);
     if ($ns) {
         $states = CourseUtility::getStates($ns);
         $date = $ns->getStartDate();
         if (in_array('recent', $states)) {
             if (in_array('ongoing', $states)) {
                 return 23;
                 // this means it has already started. Push it slightly down
             }
             return 25;
         }
         if (in_array('upcoming', $states)) {
             $dt = new \DateTime();
             $dt->add(new \DateInterval('P30D'));
             if ($date < $dt) {
                 return 15;
             }
             // Upcoming but later than a month
             return 10;
         }
         if (in_array('selfpaced', $states)) {
             return 14;
         }
     }
     return $weight;
 }
 public function getBody()
 {
     $indexer = $this->container->get('es_indexer');
     $em = $this->container->get('doctrine')->getManager();
     $rs = $this->container->get('review');
     $body = array();
     $c = $this->entity;
     // Alias for entity
     $body['name'] = $c->getName();
     $body['id'] = $c->getId();
     $body['videoIntro'] = $c->getVideoIntro();
     $body['length'] = $c->getLength();
     $body['slug'] = $c->getSlug();
     $body['description'] = $c->getDescription();
     $body['longDescription'] = $c->getLongDescription();
     $body['syllabus'] = $c->getSyllabus();
     $body['searchDesc'] = $c->getSearchDesc();
     $body['status'] = $c->getStatus();
     $body['certificate'] = $c->getCertificate();
     $body['verifiedCertificate'] = $c->getVerifiedCertificate();
     $body['url'] = $c->getUrl();
     if ($c->getCreated()) {
         $body['created'] = $c->getCreated()->format(DATE_ISO8601);
     }
     // Tags
     $tags = array();
     foreach ($c->getTags() as $tag) {
         $tags[] = strtolower($tag->getName());
     }
     $body['tags'] = $tags;
     // Instructors
     $body['instructors'] = array();
     foreach ($c->getInstructors() as $instructor) {
         $body['instructors'][] = $instructor->getName();
     }
     // Language
     $body['language'] = array();
     $lang = $c->getLanguage();
     if ($lang) {
         $body['language']['name'] = $lang->getName();
         $body['language']['id'] = $lang->getId();
         $body['language']['slug'] = $lang->getSlug();
     } else {
         // Set the default to english
         $l = $em->getRepository('ClassCentralSiteBundle:Language')->findOneBy(array('slug' => 'english'));
         $body['language']['name'] = $l->getName();
         $body['language']['id'] = $l->getId();
         $body['language']['slug'] = $l->getSlug();
     }
     // Institutions
     $body['institutions'] = array();
     foreach ($c->getInstitutions() as $ins) {
         $iDoc = new InstitutionDocumentType($ins, $this->container);
         $body['institutions'][] = $iDoc->getBody();
     }
     // Provider
     $body['provider'] = array();
     if ($c->getInitiative()) {
         $provider = $c->getInitiative();
     } else {
         // create an independent provider
         $provider = new Initiative();
         $provider->setName('Independent');
         $provider->setCode('independent');
     }
     $pDoc = new ProviderDocumentType($provider, $this->container);
     $body['provider'] = $pDoc->getBody();
     // Get the next session
     $body['nextSession'] = array();
     $ns = CourseUtility::getNextSession($c);
     if ($ns) {
         $nsDoc = new SessionDocumentType($ns, $this->container);
         $body['nextSession'] = $nsDoc->getBody();
     }
     // Subject
     $subjects = array();
     $sub = $c->getStream();
     if ($sub->getParentStream()) {
         // Add the parent stream first
         $psDoc = new SubjectDocumentType($sub->getParentStream(), $this->container);
         $subjects[] = $psDoc->getBody();
     }
     $sDoc = new SubjectDocumentType($sub, $this->container);
     $subjects[] = $sDoc->getBody();
     $body['subjects'] = $subjects;
     // Sessions. Add sessions to the records
     $sessions = array();
     $body['sessions'] = array();
     foreach ($c->getOfferings() as $session) {
         // Ignore invalid session
         if ($session->getStatus() == Offering::COURSE_NA) {
             continue;
         }
         $sDoc = new SessionDocumentType($session, $this->container);
         $sessions[] = $sDoc->getBody();
     }
     $body['sessions'] = $sessions;
     $body['numSessions'] = count($sessions);
     $body['rating'] = $rs->getRatings($c->getId());
     $body['ratingSort'] = $rs->getBayesianAverageRating($c->getId());
     $rArray = $rs->getReviewsArray($c->getId());
     $body['reviewsCount'] = $rArray['count'];
     $body['ratingStars'] = ReviewUtility::getRatingStars($body['rating']);
     $body['formattedRating'] = ReviewUtility::formatRating($body['rating']);
     // Get Followed count
     $courseRepo = $this->container->get('doctrine')->getManager()->getRepository('ClassCentralSiteBundle:Course');
     $body['followed'] = intval($courseRepo->getListedCount($c));
     // Check if the course being offered is new
     // Definition of new - created 30 days ago
     $oneMonthAgo = new \DateTime();
     $oneMonthAgo->sub(new \DateInterval("P30D"));
     $newCourse = false;
     if ($c->getCreated() >= $oneMonthAgo) {
         $newCourse = true;
     }
     $body['new'] = intval($newCourse);
     $startingSoon = false;
     $oneMonthLater = new \DateTime();
     $oneMonthLater->add(new \DateInterval("P30D"));
     if ($ns && !in_array('selfpaced', $body['nextSession']['states']) && in_array('upcoming', $body['nextSession']['states'])) {
         if ($ns->getStartDate() < $oneMonthLater and $ns->getStatus() != Offering::START_DATES_UNKNOWN) {
             $startingSoon = true;
         }
     }
     $body['startingSoon'] = intval($startingSoon);
     // Get the Credential
     $credential = array();
     if (!$c->getCredentials()->isEmpty()) {
         $cred = $c->getCredentials()->first();
         if ($cred->getStatus() < 100) {
             $credential['id'] = $cred->getId();
             $credential['name'] = $cred->getName();
             $credential['slug'] = $cred->getSlug();
             $credential['certificateName'] = '';
             $credential['certificateSlug'] = '';
             $credFormatter = $cred->getFormatter();
             $credential['certificateName'] = $credFormatter->getCertificateName();
             $credential['certificateSlug'] = $credFormatter->getCertificateSlug();
         }
     }
     $body['credential'] = $credential;
     return $body;
 }
 public function getBody()
 {
     $indexer = $this->container->get('es_indexer');
     $em = $this->container->get('doctrine')->getManager();
     $rs = $this->container->get('review');
     $body = array();
     $c = $this->entity;
     // Alias for entity
     $body['name'] = $c->getName();
     $body['id'] = $c->getId();
     $body['videoIntro'] = $c->getVideoIntro();
     $body['length'] = $c->getLength();
     $body['slug'] = $c->getSlug();
     $body['description'] = $c->getDescription();
     $body['longDescription'] = $c->getLongDescription();
     $body['syllabus'] = $c->getSyllabus();
     $body['searchDesc'] = $c->getSearchDesc();
     $body['status'] = $c->getStatus();
     $body['certificate'] = $c->getCertificate();
     $body['verifiedCertificate'] = $c->getVerifiedCertificate();
     $body['url'] = $c->getUrl();
     // Tags
     $tags = array();
     foreach ($c->getTags() as $tag) {
         $tags[] = strtolower($tag->getName());
     }
     $body['tags'] = $tags;
     // Instructors
     $body['instructors'] = array();
     foreach ($c->getInstructors() as $instructor) {
         $body['instructors'][] = $instructor->getName();
     }
     // Language
     $body['language'] = array();
     $lang = $c->getLanguage();
     if ($lang) {
         $body['language']['name'] = $lang->getName();
         $body['language']['id'] = $lang->getId();
         $body['language']['slug'] = $lang->getSlug();
     } else {
         // Set the default to english
         $l = $em->getRepository('ClassCentralSiteBundle:Language')->findOneBy(array('slug' => 'english'));
         $body['language']['name'] = $l->getName();
         $body['language']['id'] = $l->getId();
         $body['language']['slug'] = $l->getSlug();
     }
     // Institutions
     $body['institutions'] = array();
     foreach ($c->getInstitutions() as $ins) {
         $iDoc = new InstitutionDocumentType($ins, $this->container);
         $body['institutions'][] = $iDoc->getBody();
     }
     // Provider
     $body['provider'] = array();
     if ($c->getInitiative()) {
         $provider = $c->getInitiative();
     } else {
         // create an independent provider
         $provider = new Initiative();
         $provider->setName('Independent');
         $provider->setCode('independent');
     }
     $pDoc = new ProviderDocumentType($provider, $this->container);
     $body['provider'] = $pDoc->getBody();
     // Get the next session
     $body['nextSession'] = array();
     $ns = CourseUtility::getNextSession($c);
     if ($ns) {
         $nsDoc = new SessionDocumentType($ns, $this->container);
         $body['nextSession'] = $nsDoc->getBody();
     }
     // Subject
     $subjects = array();
     $sub = $c->getStream();
     if ($sub->getParentStream()) {
         // Add the parent stream first
         $psDoc = new SubjectDocumentType($sub->getParentStream(), $this->container);
         $subjects[] = $psDoc->getBody();
     }
     $sDoc = new SubjectDocumentType($sub, $this->container);
     $subjects[] = $sDoc->getBody();
     $body['subjects'] = $subjects;
     // Sessions. Add sessions to the records
     $sessions = array();
     $body['sessions'] = array();
     foreach ($c->getOfferings() as $session) {
         // Ignore invalid session
         if ($session->getStatus() == Offering::COURSE_NA) {
             continue;
         }
         $sDoc = new SessionDocumentType($session, $this->container);
         $sessions[] = $sDoc->getBody();
     }
     $body['sessions'] = $sessions;
     $body['rating'] = $rs->getRatings($c->getId());
     $body['ratingSort'] = $rs->getBayesianAverageRating($c->getId());
     $rArray = $rs->getReviewsArray($c->getId());
     $body['reviewsCount'] = $rArray['count'];
     $body['ratingStars'] = ReviewUtility::getRatingStars($body['rating']);
     $body['formattedRating'] = ReviewUtility::formatRating($body['rating']);
     // Get the Credential
     $credential = array();
     if (!$c->getCredentials()->isEmpty()) {
         $cred = $c->getCredentials()->first();
         if ($cred->getStatus() < 100) {
             $credential['id'] = $cred->getId();
             $credential['name'] = $cred->getName();
             $credential['slug'] = $cred->getSlug();
             $credential['certificateName'] = '';
             $credential['certificateSlug'] = '';
             $credFormatter = $cred->getFormatter();
             $credential['certificateName'] = $credFormatter->getCertificateName();
             $credential['certificateSlug'] = $credFormatter->getCertificateSlug();
         }
     }
     $body['credential'] = $credential;
     return $body;
 }
 public function testGetNextSession()
 {
     $c = new Course();
     // Finished offerings
     $fo1 = $this->buildOffering(1, new \DateTime("2012-06-03"), Offering::START_DATES_KNOWN);
     $fo2 = $this->buildOffering(2, new \DateTime("2012-05-03"), Offering::START_DATES_KNOWN);
     $fo3 = $this->buildOffering(3, new \DateTime("2012-07-03"), Offering::START_DATES_KNOWN);
     // Ongoing session
     $oodt1 = new \DateTime();
     $oodt1->sub(new \DateInterval("P10D"));
     $oo1 = $this->buildOffering(4, $oodt1, Offering::START_DATES_KNOWN);
     // Self paced session
     $so1 = $this->buildOffering(5, new \DateTime("2012-07-03"), Offering::COURSE_OPEN);
     // Upcoming sessions
     $uo1 = $this->buildOffering(6, $this->getFutureDateUtility("P20D"), Offering::START_DATES_KNOWN);
     $uo2 = $this->buildOffering(7, $this->getFutureDateUtility("P10D"), Offering::START_DATES_KNOWN);
     $uo3 = $this->buildOffering(8, $this->getFutureDateUtility("P30D"), Offering::START_DATES_KNOWN);
     // Course with single finished offering
     $c->addOffering($fo1);
     $next = CourseUtility::getNextSession($c);
     $this->assertEquals($fo1->getId(), $next->getId());
     // Course with multiple finished offering
     $c->addOffering($fo2);
     $c->addOffering($fo3);
     $next = CourseUtility::getNextSession($c);
     $this->assertEquals($fo3->getId(), $next->getId());
     // Course with ongoing sessions
     $c->addOffering($oo1);
     $next = CourseUtility::getNextSession($c);
     $this->assertEquals($oo1->getId(), $next->getId());
     // Course with with self paced sessions
     $c->addOffering($so1);
     $next = CourseUtility::getNextSession($c);
     $this->assertEquals($so1->getId(), $next->getId());
     // Course with single upcoming session
     $c->addOffering($uo1);
     $next = CourseUtility::getNextSession($c);
     $this->assertEquals($uo1->getId(), $next->getId());
     // Course with multiple upcoming sessions
     $c->addOffering($uo2);
     $c->addOffering($uo3);
     $next = CourseUtility::getNextSession($c);
     $this->assertEquals($uo2->getId(), $next->getId());
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $month = $input->getArgument('month');
     $year = $input->getArgument('year');
     $em = $this->getContainer()->get('doctrine')->getManager();
     $network = NetworkFactory::get($input->getOption('network'), $output);
     $network->setContainer($this->getContainer());
     $isReddit = $input->getOption('network') == 'Reddit' || $input->getOption('cs') == 'Yes';
     $courseToLevelMap = RedditNetwork::getCourseToLevelMap();
     $offerings = $this->getContainer()->get('doctrine')->getManager()->getRepository('ClassCentralSiteBundle:Offering')->courseReport($month, $year);
     // Segreagate by Initiative
     $offeringsByInitiative = array();
     $offeringsByStream = array();
     $offeringsByLevel = array('beginner' => array(), 'intermediate' => array(), 'advanced' => array(), 'uncategorized' => array());
     foreach ($offerings as $offering) {
         if ($offering->getInitiative() == null) {
             $initiative = 'Others';
         } else {
             $initiative = $offering->getInitiative()->getName();
         }
         // Skip unapproved courses
         if ($offering->getCourse()->getStatus() != CourseStatus::AVAILABLE) {
             continue;
         }
         if ($offering->getStatus() == Offering::START_DATES_UNKNOWN) {
             continue;
         }
         // Skip self paced courses
         if ($offering->getStatus() == Offering::COURSE_OPEN) {
             continue;
         }
         $offeringsByInitiative[$initiative][] = $offering;
         $subject = $offering->getCourse()->getStream();
         if ($subject->getParentStream()) {
             $subject = $subject->getParentStream();
         }
         $offeringsByStream[$subject->getName()][] = $offering;
         if ($isReddit && ($subject->getName() == 'Computer Science' || $subject->getName() == 'Programming')) {
             $courseId = $offering->getCourse()->getId();
             if (isset($courseToLevelMap[$courseId])) {
                 $offeringsByLevel[$courseToLevelMap[$courseId]][] = $offering;
             } else {
                 $offeringsByLevel['uncategorized'][] = $offering;
             }
         }
     }
     // Segregate by Stream
     $network->setRouter($this->getContainer()->get('router'));
     $coursesByCount = array();
     if ($isReddit) {
         foreach ($offeringsByLevel as $level => $offerings) {
             $count = count($offerings);
             $network->outLevel(ucfirst($level), $count);
             $network->beforeOffering();
             foreach ($offerings as $offering) {
                 $network->outOffering($offering);
                 // Count the number of times its been added to my courses
                 $added = $em->getRepository('ClassCentralSiteBundle:UserCourse')->findBy(array('course' => $offering->getCourse()));
                 $timesOffered = 0;
                 foreach ($offering->getCourse()->getOfferings() as $o) {
                     $states = CourseUtility::getStates($o);
                     if (in_array('past', $states) || in_array('ongoing', $states)) {
                         $timesOffered++;
                     }
                 }
                 if ($timesOffered < 2) {
                     $timesAdded = count($added);
                     $coursesByCount[$offering->getCourse()->getName()] = $timesAdded;
                 }
             }
         }
     } else {
         foreach ($offeringsByStream as $stream => $offerings) {
             $subject = $offerings[0]->getCourse()->getStream();
             if ($subject->getParentStream()) {
                 $subject = $subject->getParentStream();
             }
             $count = count($offerings);
             $network->outInitiative($subject, $count);
             $network->beforeOffering();
             foreach ($offerings as $offering) {
                 $network->outOffering($offering);
                 // Count the number of times its been added to my courses
                 $added = $em->getRepository('ClassCentralSiteBundle:UserCourse')->findBy(array('course' => $offering->getCourse()));
                 $timesOffered = 0;
                 foreach ($offering->getCourse()->getOfferings() as $o) {
                     $states = CourseUtility::getStates($o);
                     if (in_array('past', $states) || in_array('ongoing', $states)) {
                         $timesOffered++;
                     }
                 }
                 if ($timesOffered < 1) {
                     $timesAdded = count($added);
                     $coursesByCount[$offering->getCourse()->getName()] = $timesAdded;
                 }
             }
         }
     }
     asort($coursesByCount);
     /*
     $formatter = $this->getContainer()->get('course_formatter');
     $repo = $this->getContainer()->get('doctrine')->getManager()->getRepository('ClassCentralSiteBundle:Course');
     $i= 0;
     
     foreach($coursesByCount as $courseId => $count)
     {
         $c = $repo->find($courseId );
         echo $formatter->blogFormat( $c ) . "\n";
         $i++;
         if($i == 10) break;
     }
     */
     print_r($coursesByCount);
 }
 public function tableRowFormat(Course $course)
 {
     $followColumn = '';
     $courseNameColumn = '';
     $startDateColumn = '';
     $ratingColumn = '';
     $router = $this->container->get('router');
     $rs = $this->container->get('review');
     //
     $courseUrl = 'https://www.class-central.com' . $router->generate('ClassCentralSiteBundle_mooc', array('id' => $course->getId(), 'slug' => $course->getSlug()));
     $courseName = $course->getName();
     // COLUMN 1 - FOLLOW
     $followUrl = $courseUrl . '?follow=true';
     $followColumn = "<td width='30px' style='vertical-align: top'><a href='{$followUrl}' style='color: red;font-size: 25px;text-decoration: none' target='_blank'>♥</a></td>";
     // COLUMN 2 - COURSE NAME
     $providerLine = '';
     if ($course->getInstitutions()->count() > 0) {
         $ins = $course->getInstitutions()->first();
         $providerLine = $ins->getName();
         $providerLine = "{$providerLine}";
     }
     if ($course->getInitiative()) {
         $providerLine .= ' via ' . $course->getInitiative()->getName();
     } else {
         $providerLine .= ' via Independent';
     }
     $providerLine = "<i>{$providerLine}</i>";
     $courseNameColumn = "<td><a href='{$courseUrl}'>{$courseName}</a><br/>{$providerLine}</td>";
     // COLUMN 3 - START DATE
     $nextOffering = CourseUtility::getNextSession($course);
     if (!$nextOffering) {
         return '';
     }
     $displayDate = $nextOffering->getDisplayDate();
     $states = CourseUtility::getStates($nextOffering);
     if (in_array('past', $states)) {
         $displayDate = 'TBA';
     }
     if (in_array('selfpaced', $states)) {
         $displayDate = 'Self Paced';
     }
     $startDateColumn = "<td>{$displayDate}</td>";
     // COLUMN 4 - RATING
     $ratings = $rs->getRatings($course->getId());
     $reviews = $rs->getReviews($course->getId());
     $ratingsLine = ReviewUtility::getRatingStars(0);
     // Default value
     if ($ratings > 0) {
         $formattedRatings = ReviewUtility::getRatingStars($ratings);
         $numRatings = $reviews['ratingCount'];
         $post = $numRatings == 1 ? 'rating' : 'ratings';
         $ratingsLine = "{$formattedRatings} (<a href='{$courseUrl}#reviews'>{$numRatings}</a>) ";
     }
     $ratingColumn = "<td>{$ratingsLine}</td>";
     return "<tr>" . $followColumn . $courseNameColumn . $ratingColumn . "</tr>";
 }
 public static function getStates(Offering $offering)
 {
     $state = CourseUtility::calculateState($offering);
     return self::getStatesFromState($state);
 }