/**
  * Returns an array with fields required to render
  * create/edit review form
  * @param Course $course
  */
 private function getReviewFormData(Course $course)
 {
     $em = $this->getDoctrine()->getManager();
     $offerings = $em->getRepository('ClassCentralSiteBundle:Offering')->findAllByCourseIds(array($course->getId()));
     $offeringTypesOrder = array('ongoing', 'selfpaced', 'past');
     $offeringCount = 0;
     $offering = null;
     // If there is only one offering this will keep track of it
     // offering count
     foreach ($offerings as $type => $ot) {
         if (in_array($type, $offeringTypesOrder)) {
             foreach ($ot as $o) {
                 $offering = $o;
                 $offeringCount++;
             }
         }
     }
     return array('progress' => UserCourse::$progress, 'difficulty' => Review::$difficulty, 'course' => $course, 'levels' => Review::$levels, 'offerings' => $offerings, 'offeringTypes' => Offering::$types, 'offeringCount' => $offeringCount, 'offering' => $offering, 'offeringTypesOrder' => $offeringTypesOrder, 'reviewStatuses' => Review::$statuses, 'isAdmin' => $this->get('security.context')->isGranted('ROLE_ADMIN'));
 }
Example #2
0
 /**
  * Saves the course
  * @param Course $c
  * @param array $tags
  */
 public function saveCourseTags(Course $c, array $tags)
 {
     $newTag = false;
     $tagsToBeRemoved = array();
     // Get existing tags for this course
     $ct = array();
     foreach ($c->getTags() as $cTag) {
         $tName = $cTag->getName();
         $ct[] = $tName;
         if (!in_array($tName, $tags)) {
             $tagsToBeRemoved[] = $cTag;
         }
     }
     foreach ($tagsToBeRemoved as $tr) {
         $c->removeTag($tr);
     }
     // Create new tags if necessary
     foreach ($tags as $tag) {
         if (in_array($tag, $ct)) {
             // nothing to do here
             continue;
         }
         // Check if the tag exists
         $t = $this->em->getRepository('ClassCentralSiteBundle:Tag')->findOneBy(array('name' => $tag));
         if (!$t) {
             $newTag = true;
             $t = new \ClassCentral\SiteBundle\Entity\Tag();
             $t->setName($tag);
             $this->em->persist($t);
         }
         $c->addTag($t);
     }
     $this->em->persist($c);
     $this->em->flush();
     // Flush the tag cache if a new tag was created
     if ($newTag) {
         $this->cache->deleteCache(self::ALL_TAGS_CACHE_KEY);
     }
 }
Example #3
0
 public function migrate()
 {
     $this->output->writeln("Getting Started with migration version 1");
     // Get all offerings
     $em = $this->container->get('Doctrine')->getManager();
     $offerings = $em->getRepository('ClassCentralSiteBundle:Offering')->findAll();
     foreach ($offerings as $offering) {
         $name = $offering->getName();
         $initiative = null;
         if ($offering->getInitiative()) {
             $initiative = $offering->getInitiative();
         }
         $stream = $offering->getCourse()->getStream();
         // Check if the course name and initiative exist
         $initiative_id = $initiative ? $initiative->getID() : null;
         $course = $em->getRepository('ClassCentralSiteBundle:Course')->findOneBy(array('name' => $name, 'initiative' => $initiative_id));
         if (!$course) {
             // Course does not exist. Create the course
             //$this->output->writeln("NOT FOUND");
             $course = new Course();
             $course->setName($name);
             $course->setInitiative($initiative);
             $course->setStream($stream);
             $em->persist($course);
             $em->flush();
             $this->output->writeln("Course '" . $course->getName() . "' created with  " . $course->getId());
         } else {
             $this->output->writeln("Course '" . $course->getName() . " already exists with   " . $course->getId());
         }
         // Update the course id in offering
         $offering->setCourse($course);
         $em->persist($offering);
         $em->flush();
     }
     // Delete all the courses with course id 100
     $em->createQuery("DELETE FROM ClassCentralSiteBundle:Course c WHERE c.initiative=100")->execute();
     // Delete initative
     $em->createQuery("DELETE FROM ClassCentralSiteBundle:Initiative i WHERE i.id = 100")->execute();
 }
Example #4
0
 /**
  * Build a doctrine Course Entity out of a csv row
  * @param $row
  * @return Course
  */
 public function getCourseEntity($row)
 {
     $course = new Course();
     $course->setName($row[0]);
     $course->setDescription($row[1]);
     $course->setVideoIntro(str_replace('http', 'https', $row[4]));
     $course->setUrl($row[5]);
     $course->setShortName($this->getCourseId($row[5]));
     $course->setInitiative($this->initiative);
     // Set the language to arabic
     $langMap = $this->dbHelper->getLanguageMap();
     $course->setLanguage($langMap['Arabic']);
     // Set the default stream as humanities
     $defaultStream = $this->dbHelper->getStreamBySlug('humanities');
     $course->setStream($defaultStream);
     // Calculate the length of the course
     $start = new \DateTime($row[2]);
     $end = new \DateTime($row[3]);
     $length = ceil($start->diff($end)->days / 7);
     $course->setLength($length);
     return $course;
 }
 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());
 }
Example #6
0
 /**
  * Summarizes reviews for all courses
  * @param Course $course
  * @return int
  */
 public function summarizeReviewsForACourse(Course $course)
 {
     $numSummarized = 0;
     foreach ($course->getReviews() as $review) {
         $response = $this->summarizeReview($review);
         if ($response != self::REVIEW_SUMMARY_FAILED && $response != self::REVIEW_ALREADY_SUMMARIZED_OR_EMPTY_TEXT) {
             $numSummarized++;
         }
     }
     return $numSummarized;
 }
Example #7
0
 private function uploadImageIfNecessary($imageUrl, Course $course)
 {
     $kuber = $this->container->get('kuber');
     $uniqueKey = basename($imageUrl);
     if ($kuber->hasFileChanged(Kuber::KUBER_ENTITY_COURSE, Kuber::KUBER_TYPE_COURSE_IMAGE, $course->getId(), $uniqueKey)) {
         // Upload the file
         $filePath = '/tmp/course_' . $uniqueKey;
         file_put_contents($filePath, file_get_contents($imageUrl));
         $kuber->upload($filePath, Kuber::KUBER_ENTITY_COURSE, Kuber::KUBER_TYPE_COURSE_IMAGE, $course->getId(), null, $uniqueKey);
     }
 }
 /**
  * Gets the count of number of times the course has been
  * added to the users list
  */
 public function getListedCount(Course $course)
 {
     $query = $this->getEntityManager()->createQueryBuilder();
     $query->add('select', 'count(uc.id) as listed')->add('from', 'ClassCentralSiteBundle:UserCourse uc')->join('uc.course', 'c')->andWhere('c.id = :id')->setParameter('id', $course->getId());
     $listed = $query->getQuery()->getSingleScalarResult();
     return $listed;
 }
Example #9
0
 public function sendNewCourseToSlack(Course $course, Initiative $initiative)
 {
     try {
         $providerInfo = PageHeaderFactory::get($initiative);
         $coursePageUrl = $this->scraper->getContainer()->getParameter('baseurl') . $this->scraper->getContainer()->get('router')->generate('ClassCentralSiteBundle_mooc', array('id' => $course->getId(), 'slug' => $course->getSlug()));
         $logo = $this->scraper->getContainer()->getParameter('rackspace_cdn_base_url') . $providerInfo->getImageUrl();
         $message = "[New Course] *{$course->getName()}*\n" . $coursePageUrl;
         $this->scraper->getContainer()->get('slack_client')->to('#cc-activity-data')->from($initiative->getName())->withIcon($logo)->send($message);
     } catch (\Exception $e) {
     }
 }
Example #10
0
 private function getOnDemandCourse($data = array())
 {
     $dbLanguageMap = $this->dbHelper->getLanguageMap();
     $course = new Course();
     $course->setShortName(substr('coursera_' . $data['elements'][0]['slug'], 0, 49));
     $course->setInitiative($this->initiative);
     $course->setName($data['elements'][0]['name']);
     $course->setDescription($data['elements'][0]['description']);
     $course->setLongDescription(nl2br($data['elements'][0]['description']));
     $course->setStream($this->dbHelper->getStreamBySlug('cs'));
     // Default to Computer Science
     $course->setUrl('https://www.coursera.org/learn/' . $data['elements'][0]['slug']);
     $lang = self::$languageMap[$data['elements']['0']['primaryLanguageCodes'][0]];
     if (isset($dbLanguageMap[$lang])) {
         $course->setLanguage($dbLanguageMap[$lang]);
     } else {
         $this->out("Language not found " . $data['elements']['0']['primaryLanguageCodes'][0]);
     }
     $course->setCertificate(false);
     $course->setVerifiedCertificate($data['elements'][0]['isVerificationEnabled']);
     // Add the university
     foreach ($data['linked']['partners.v1'] as $university) {
         $ins = new Institution();
         $ins->setName($university['name']);
         $ins->setIsUniversity(true);
         $ins->setSlug($university['shortName']);
         $course->addInstitution($this->dbHelper->createInstitutionIfNotExists($ins));
     }
     foreach ($data['linked']['instructors.v1'] as $courseraInstructor) {
         if (!empty($courseraInstructor['fullName'])) {
             $insName = $courseraInstructor['fullName'];
         } else {
             $insName = $courseraInstructor['firstName'] . ' ' . $courseraInstructor['lastName'];
         }
         $course->addInstructor($this->dbHelper->createInstructorIfNotExists($insName));
     }
     // Get Course Details like Syllabus and length
     $courseDetails = json_decode(file_get_contents(sprintf(self::ONDEMAND_OPENCOURSE_API, $data['elements'][0]['slug'])), true);
     if (!empty($courseDetails)) {
         $syllabus = '';
         foreach ($courseDetails['courseMaterial']['elements'] as $item) {
             $syllabus .= "<b>{$item['name']}</b><br/>{$item['description']}<br/><br/>";
         }
         $course->setSyllabus($syllabus);
     }
     // Calculate the length of the course
     $schedule = json_decode(file_get_contents(sprintf(self::ONDEMAND_COURSE_SCHEDULE, $data['elements'][0]['id'])), true);
     if (!empty($schedule)) {
         $length = 0;
         foreach ($schedule['elements'][0]['defaultSchedule']['periods'] as $period) {
             $length += $period['numberOfWeeks'];
         }
         if ($length > 0) {
             $course->setLength($length);
         }
     }
     return $course;
 }
 public function emailFormat(Course $course)
 {
     $router = $this->container->get('router');
     $url = 'https://www.class-central.com' . $router->generate('ClassCentralSiteBundle_mooc', array('id' => $course->getId(), 'slug' => $course->getSlug()));
     return sprintf("<li><a href='%s'>%s</a></li>", $url, $course->getName());
 }
Example #12
0
 public function scrape()
 {
     $em = $this->getManager();
     // Array of offerings created or updated
     $offerings = array();
     $this->out("Scraping " . $this->initiative->getName());
     // Step 1: Getting a list of course URLs
     $this->out("Getting a list of course pages");
     $urls = $this->getListOfCoursePages();
     $urlsCount = count($urls);
     // Step 2: Go through the page and create/update offering
     $this->out("Number of courses found: {$urlsCount}");
     $this->out("Gathering details about each course");
     $courseDetails = array();
     foreach ($urls as $url) {
         if (!$url) {
             continue;
         }
         $courseDetail = array();
         $this->domParser->load(file_get_contents(self::BASE_URL . $url));
         // Ignore self paced
         if (!$this->domParser->find('h2.offering_dates_date', 0)) {
             continue;
         }
         // Get Name and shortName
         $nameString = $this->domParser->find('h1.page-title', 0)->plaintext;
         $openBracketPosition = strpos($nameString, '(');
         $closeBracketPosition = strpos($nameString, ')');
         $courseDetail['name'] = substr($nameString, 0, $openBracketPosition - 1);
         $courseDetail['shortName'] = substr($nameString, $openBracketPosition + 1, $closeBracketPosition - $openBracketPosition - 1);
         if ($courseDetail['name'] == 'Introduction to Nursing in Healthcar') {
             $courseDetail['name'] = 'Introduction to Nursing in Healthcare';
             $courseDetail['shortName'] = 'IntroNur';
         }
         // Get the video id from the url
         // eg. www.youtube.com/embed/Bw8HkjGQb3U?wmode=opaque&rel=0&showinfo=0
         $youtubeIdPosition = 31;
         $video = 'http://' . $this->domParser->find('iframe.media-youtube-player', 0)->src;
         $questionMarkPosition = strpos($video, '?');
         $courseDetail['video'] = 'http://www.youtube.com/watch?v=' . substr($video, $youtubeIdPosition, $questionMarkPosition - $youtubeIdPosition);
         $instructors = trim($this->domParser->find('div[id=subject-teacher-tagline]', 0)->plaintext);
         // Remove the 'by'
         $instructors = substr($instructors, 3);
         $courseDetail['instructors'] = explode(' & ', $instructors);
         $courseDetail['desc'] = $this->domParser->find('div.offering_body', 0)->plaintext;
         $courseDetail['start_date'] = $this->domParser->find('h2.offering_dates_date', 0)->plaintext;
         $courseDetail['end_date'] = $this->domParser->find('h2.offering_dates_date', 1)->plaintext;
         $courseDetail['url'] = $url;
         print_r($courseDetail);
         $courseDetails[] = $courseDetail;
         $this->domParser->clear();
     }
     $this->out(count($courseDetails) . ' course pages found');
     // Default stream
     $stream = $this->dbHelper->getStreamBySlug('business');
     $this->out("Default stream is " . $stream->getName());
     foreach ($courseDetails as $courseDetail) {
         /**
          * Taking a shortcut here. Check if a course is created or not. If it isn't create the
          * course,offering, etc. Updates are ignored
          * TODO: Not take a shortcut
          */
         // Build a course object
         $course = new Course();
         $courseShortName = 'open2study_' . $courseDetail['shortName'];
         $course->setShortName($courseShortName);
         $course->setInitiative($this->initiative);
         $course->setName($courseDetail['name']);
         $course->setDescription($courseDetail['desc']);
         $course->setStream($stream);
         // Default to Business
         $course->setVideoIntro($courseDetail['video']);
         $course->setUrl(self::BASE_URL . $courseDetail['url']);
         $dbCourse = $this->dbHelper->getCourseByShortName($courseShortName);
         if (!$dbCourse) {
             if ($this->doCreate()) {
                 // New course
                 $this->out("NEW COURSE - " . $course->getName());
                 if ($this->doModify()) {
                     foreach ($courseDetail['instructors'] as $instructor) {
                         $course->addInstructor($this->dbHelper->createInstructorIfNotExists($instructor));
                     }
                     $em->persist($course);
                     $em->flush();
                 }
             }
         } else {
             $course = $dbCourse;
         }
         // Check if offering exists
         $shortName = $this->getOfferingShortName($courseDetail);
         $offering = $this->dbHelper->getOfferingByShortName($shortName);
         if ($offering) {
             continue;
         }
         // Check if create offering is oon
         if (!$this->doCreate()) {
             $offerings[] = $offering;
             // Add it to the offerings table
             continue;
         }
         $offering = new Offering();
         $offering->setCourse($course);
         $offering->setStartDate(\DateTime::createFromFormat("d/m/Y", $courseDetail['start_date']));
         $offering->setEndDate(\DateTime::createFromFormat("d/m/Y", $courseDetail['end_date']));
         $offering->setStatus(Offering::START_DATES_KNOWN);
         $offering->setLength(4);
         $offering->setShortName($shortName);
         $offering->setUrl(self::BASE_URL . $courseDetail['url']);
         $offering->setVideoIntro($courseDetail['video']);
         $offering->setSearchDesc($courseDetail['desc']);
         $offering->setCreated(new \DateTime());
         if ($this->doModify()) {
             try {
                 $em->persist($offering);
                 $em->flush();
                 $this->out("OFFERING {$courseDetail['name']} created");
             } catch (\Exception $e) {
                 $this->out("OFFERING {$courseDetail['name']} creation FAILED");
             }
         }
         $offerings[] = $offering;
     }
     return $offerings;
 }
Example #13
0
 /**
  * Retrives the userCourse
  * There can be only one course added per user. So ignoring the list id
  * @param \ClassCentral\SiteBundle\Entity\User $user
  * @param Course $course
  * @param $listId
  */
 private function getUserCourseId(\ClassCentral\SiteBundle\Entity\User $user, Course $course, $listId)
 {
     $em = $this->container->get('doctrine')->getManager();
     $rsm = new ResultSetMapping();
     $rsm->addScalarResult('id', 'id');
     $query = $em->createNativeQuery("SELECT id FROM users_courses WHERE user_id = ? AND course_id = ?", $rsm);
     $query->setParameter('1', $user->getId());
     $query->setParameter('2', $course->getId());
     //$query->setParameter('3', $listId);
     $result = $query->getResult();
     if (empty($result)) {
         return null;
     } else {
         return $result[0]["id"];
     }
 }
Example #14
0
 private function updateShortName(Course $course, $sn)
 {
     $em = $this->container->get('Doctrine')->getManager();
     $course->setShortName($sn);
     $em->persist($course);
 }
Example #15
0
 private function getOnDemandCourse($data = array())
 {
     $dbLanguageMap = $this->dbHelper->getLanguageMap();
     $course = new Course();
     $course->setShortName(substr('coursera_' . $data['elements'][0]['slug'], 0, 49));
     $course->setInitiative($this->initiative);
     $course->setName($data['elements'][0]['name']);
     $course->setDescription($data['elements'][0]['description']);
     $course->setLongDescription(nl2br($data['elements'][0]['description']));
     $course->setStream($this->dbHelper->getStreamBySlug('cs'));
     // Default to Computer Science
     $course->setUrl('https://www.coursera.org/learn/' . $data['elements'][0]['slug']);
     $lang = self::$languageMap[$data['elements']['0']['primaryLanguageCodes'][0]];
     if (isset($dbLanguageMap[$lang])) {
         $course->setLanguage($dbLanguageMap[$lang]);
     } else {
         $this->out("Language not found " . $data['elements']['0']['primaryLanguageCodes'][0]);
     }
     $course->setCertificate(false);
     $course->setVerifiedCertificate($data['elements'][0]['isVerificationEnabled']);
     // Add the university
     foreach ($data['linked']['partners.v1'] as $university) {
         $ins = new Institution();
         $ins->setName($university['name']);
         $ins->setIsUniversity(true);
         $ins->setSlug($university['shortName']);
         $course->addInstitution($this->dbHelper->createInstitutionIfNotExists($ins));
     }
     foreach ($data['linked']['instructors.v1'] as $courseraInstructor) {
         if (!empty($courseraInstructor['fullName'])) {
             $insName = $courseraInstructor['fullName'];
         } else {
             $insName = $courseraInstructor['firstName'] . ' ' . $courseraInstructor['lastName'];
         }
         $course->addInstructor($this->dbHelper->createInstructorIfNotExists($insName));
     }
     return $course;
 }
 /**
  * Returns an image url that can be used as the opengraph image tag
  * @param Course $c
  */
 public static function getImageUrl(Course $c)
 {
     $imageUrl = null;
     // Do institutions first
     foreach ($c->getInstitutions() as $ins) {
         if ($ins->getImageUrl()) {
             $imageUrl = $ins->getImageDir() . '/' . $ins->getImageUrl();
             return $imageUrl;
         }
     }
     // Provider
     $provider = $c->getInitiative();
     if ($provider && $provider->getImageUrl()) {
         $imageUrl = $provider->getImageDir() . '/' . $provider->getImageUrl();
     }
     return $imageUrl;
 }