Ejemplo n.º 1
0
 /**
  * Creates a course if it does not exist
  * @param $name Name of the course
  * @param Initiative $initiative
  * @param Institution $ins
  * @return Course
  */
 public function createCourseIfNotExists($name, Initiative $initiative, Institution $ins = null, Stream $stream)
 {
     // Check if course exists
     $em = $this->scraper->getManager();
     $courseRepository = $em->getRepository('ClassCentralSiteBundle:Course');
     $course = $courseRepository->findOneBy(array('name' => $name, 'initiative' => $initiative->getId()));
     // Course exists
     if ($course) {
         return $course;
     }
     $course = new Course();
     $course->setName($name);
     $course->setInitiative($initiative);
     if ($ins) {
         $course->addInstitution($ins);
     }
     $course->setStream($stream);
     // Check if course is to be created
     if ($this->scraper->doModify() && $this->scraper->doCreate()) {
         $em->persist($course);
         $em->flush();
         $this->scraper->out("COURSE {$name} created for initiative " . $initiative->getName());
     }
     return $course;
 }
Ejemplo n.º 2
0
 /**
  * Retrieves all the data required for a particular provider
  * @param $slug
  * @param Request $request
  */
 public function byProvider($slug, Request $request)
 {
     $cache = $this->container->get('cache');
     $data = $cache->get('provider_' . $slug . $request->server->get('QUERY_STRING'), function ($slug, $request) {
         $finder = $this->container->get('course_finder');
         $em = $this->container->get('doctrine')->getManager();
         if ($slug == 'others') {
             $provider = new Initiative();
             $provider->setName('Others');
             $provider->setCode('others');
         } elseif ($slug == 'independent') {
             $provider = new Initiative();
             $provider->setName('Independent');
             $provider->setCode('independent');
         } else {
             $provider = $em->getRepository('ClassCentralSiteBundle:Initiative')->findOneBy(array('code' => $slug));
             if (!$provider) {
                 throw new \Exception("Provider {$slug} not found");
             }
         }
         extract($this->getInfoFromParams($request->query->all()));
         $courses = $finder->byProvider($slug, $filters, $sort, $pageNo);
         extract($this->getFacets($courses));
         $pageInfo = PageHeaderFactory::get($provider);
         $breadcrumbs = array(Breadcrumb::getBreadCrumb('Providers', $this->container->get('router')->generate('providers')));
         $breadcrumbs[] = Breadcrumb::getBreadCrumb($provider->getName());
         return compact('provider', 'allSubjects', 'allLanguages', 'allSessions', 'courses', 'sortField', 'sortClass', 'pageNo', 'pageInfo', 'breadcrumbs');
     }, array($slug, $request));
     return $data;
 }
 private static function getFromInitiative(Initiative $entity)
 {
     $info = new PageHeaderInfo("Initiative");
     $info->setName($entity->getName());
     $info->setUrl($entity->getUrl());
     $info->setDescription($entity->getDescription());
     if ($entity->getImageUrl()) {
         $info->setImageUrl($entity->getImageDir() . '/' . $entity->getImageUrl());
     }
     return $info;
 }
 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;
 }
 /**
  * Builds a list of providers with counts
  * @param ContainerInterface $container
  * @return mixed
  */
 public function getProvidersList(ContainerInterface $container)
 {
     $cache = $container->get('cache');
     $data = $cache->get('providers_with_count', function ($container) {
         $esCourses = $container->get('es_courses');
         $counts = $esCourses->getCounts();
         $em = $container->get('doctrine')->getManager();
         arsort($counts['providers']);
         $providers = array();
         foreach ($counts['providers'] as $code => $count) {
             if ($code == 'independent') {
                 $entity = new Initiative();
                 $entity->setCode($code);
                 $entity->setName('Independent');
             } else {
                 $entity = $em->getRepository('ClassCentralSiteBundle:Initiative')->findOneBy(array('code' => $code));
             }
             $provider = array();
             $provider['id'] = $entity->getId();
             $provider['count'] = $count;
             $provider['code'] = $code;
             $provider['name'] = $entity->getName();
             $providers[$code] = $provider;
         }
         return compact('providers');
     }, array($container));
     return $data;
 }
Ejemplo n.º 6
0
 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 getBody()
 {
     $body = array();
     $credentialService = $this->container->get('credential');
     $c = $this->entity;
     // Alias for entity
     $formatter = $c->getFormatter();
     $body['name'] = $c->getName();
     $body['id'] = $c->getId();
     $body['slug'] = $c->getSlug();
     $body['oneLiner'] = $c->getOneLiner();
     $body['subTitle'] = $c->getSubTitle();
     $body['price'] = $c->getPrice();
     $body['pricePeriod'] = $c->getPricePeriod();
     $body['displayPrice'] = $formatter->getPrice();
     $body['durationMin'] = $c->getDurationMin();
     $body['durationMax'] = $c->getDurationMax();
     $body['displayDuration'] = $formatter->getDuration();
     $body['workloadMin'] = $c->getWorkloadMin();
     $body['workloadMax'] = $c->getDurationMax();
     $body['workloadType'] = $c->getWorkloadType();
     $body['displayWorkload'] = $formatter->getWorkload();
     $body['url'] = $c->getUrl();
     $body['description'] = $c->getDescription();
     $body['status'] = $c->getStatus();
     $body['image'] = $credentialService->getImage($c);
     $body['cardImage'] = $credentialService->getCardImage($c);
     $body['subjectSlug'] = null;
     $body['subject'] = null;
     if ($c->getSubject()) {
         $body['subjectSlug'] = $c->getSubject();
         $body['subject'] = Credential::$SUBJECTS[$c->getSubject()];
     }
     $orgs = array();
     // Array of names of organizations who are involved in creating the credential
     // Provider
     $body['provider'] = array();
     if ($c->getInitiative()) {
         $provider = $c->getInitiative();
         $body['certificateName'] = $formatter->getCertificateName();
         $body['certificateSlug'] = $formatter->getCertificateSlug();
         $bulletOrg = "{$formatter->getCertificateName()} via ";
         $orgs[] = $provider->getName();
         // Populate the organization list
     } else {
         // create an independent provider
         $provider = new Initiative();
         $provider->setName('Independent');
         $provider->setCode('independent');
     }
     $pDoc = new ProviderDocumentType($provider, $this->container);
     $body['provider'] = $pDoc->getBody();
     // Institutions
     $body['institutions'] = array();
     $institutions = array();
     foreach ($c->getInstitutions() as $ins) {
         $iDoc = new InstitutionDocumentType($ins, $this->container);
         $body['institutions'][] = $iDoc->getBody();
         $orgs[] = $ins->getName();
         // Populate the organization list
     }
     // Get the ratings
     $rating = $credentialService->calculateAverageRating($this->entity);
     $body['rating'] = $rating['rating'];
     $body['formattedRating'] = ReviewUtility::formatRating($rating['rating']);
     // Rounds the rating to the nearest 0.5
     $body['numRatings'] = $rating['numRatings'];
     $courses = array();
     foreach ($this->entity->getCourses() as $course) {
         $cDoc = new CourseDocumentType($course, $this->container);
         $courses[] = $cDoc->getBody();
     }
     $body['courses'] = $courses;
     $body['isSponsered'] = $c->isSponsored();
     // Build the bullet points in the array
     $bulletPoints = array();
     $bulletPoints[] = $bulletOrg . UniversalHelper::commaSeparateList($orgs);
     // Bullet Price and Duration
     $bulletPriceAndDuration = $formatter->getPrice();
     $displayDuration = $formatter->getDuration();
     if ($displayDuration) {
         $bulletPriceAndDuration .= ' for ' . $displayDuration;
     }
     $bulletPoints[] = $bulletPriceAndDuration;
     // Bullet effort
     $effort = $formatter->getWorkload();
     if ($effort) {
         $bulletPoints[] = $effort . ' of effort';
     }
     if ($provider->getName() == 'Coursera') {
         $bulletPoints[] = count($body['courses']) - 1 . ' courses + capstone project ';
     } elseif ($provider->getName() == 'HBX') {
         $bulletPoints[] = '3 courses and a final exam. Application Required';
     }
     if ($formatter->getEnrollment()) {
         $bulletPoints[] = $formatter->getEnrollment();
     }
     $body['bulletPoints'] = $bulletPoints;
     return $body;
 }
 /**
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $indexer = $this->getContainer()->get('es_indexer');
     $indexer->setContainer($this->getContainer());
     $em = $this->getContainer()->get('doctrine')->getManager();
     $cache = $this->getContainer()->get('cache');
     /****
      * Index courses
      */
     $offset = 0;
     $limit = 100;
     $count = 0;
     do {
         unset($courses);
         $courses = $this->getContainer()->get('doctrine')->getManager()->getRepository('ClassCentralSiteBundle:Course')->findBy(array(), array(), 100, $offset);
         foreach ($courses as $course) {
             $indexer->index($course);
             $count++;
         }
         $output->writeLn("{$count} courses indexed");
         $offset += $limit;
     } while ($courses);
     /****
      * Index Credentials
      */
     $credentials = $this->getContainer()->get('doctrine')->getManager()->getRepository('ClassCentralCredentialBundle:Credential')->findAll();
     foreach ($credentials as $credential) {
         $indexer->index($credential);
     }
     $output->writeln("All Credentials indexed");
     /***
      * Index languages
      */
     $langController = new LanguageController();
     $languages = $cache->get('language_list_count', array($langController, 'getLanguagesList'), array($this->getContainer()));
     foreach ($languages as $language) {
         $indexer->index($language);
     }
     $output->writeln("All Languages indexed");
     /***
      * Index universities/institutions
      */
     $insController = new InstitutionController();
     // Get institutions
     $data = $insController->getInstitutions($this->getContainer(), false);
     $institutions = $data['institutions'];
     // Get Universities
     $data = $insController->getInstitutions($this->getContainer(), true);
     $universities = $data['institutions'];
     $all = array_merge($institutions, $universities);
     foreach ($all as $ins) {
         if ($ins['count'] > 0) {
             $i = $em->getRepository('ClassCentralSiteBundle:Institution')->findOneBy(array('slug' => $ins['slug']));
             $i->setCount($ins['count']);
             $indexer->index($i);
         }
     }
     $output->writeln("All Institutions indexed");
     /****
      * Index providers
      */
     $providerController = new InitiativeController();
     $data = $providerController->getProvidersList($this->getContainer());
     foreach ($data['providers'] as $provider) {
         if ($provider['count'] > 0) {
             if ($provider['code'] == 'independent') {
                 $p = new Initiative();
                 $p->setCode('independent');
                 $p->setName('Independent');
             } else {
                 $p = $em->getRepository('ClassCentralSiteBundle:Initiative')->findOneBy(array('code' => $provider['code']));
             }
             $p->setCount($provider['count']);
             $indexer->index($p);
         }
     }
     $output->writeln("All Providers indexed");
     /****
      * Index subjects
      */
     $subjectRepository = $em->getRepository('ClassCentralSiteBundle:Stream');
     $subjects = $cache->get('stream_list_count', array(new StreamController(), 'getSubjectsList'), array($this->getContainer()));
     foreach ($subjects['parent'] as $subject) {
         $indexer->index($subjectRepository->find($subject['id']));
     }
     foreach ($subjects['children'] as $childSubjects) {
         foreach ($childSubjects as $subject) {
             $indexer->index($subjectRepository->find($subject['id']));
         }
     }
     $output->writeln("All subjects indexed");
 }