public function execute(InputInterface $input, OutputInterface $output)
 {
     $now = new \DateTime();
     $output->writeln("<comment>New Sessions notifications scheduler started on {$now->format('Y-m-d H:i:s')}</comment>");
     $esCourses = $this->getContainer()->get('es_courses');
     $em = $this->getContainer()->get('doctrine')->getManager();
     $scheduler = $this->getContainer()->get('scheduler');
     $date = $input->getArgument('date');
     $dateParts = explode('-', $date);
     if (!checkdate($dateParts[1], $dateParts[2], $dateParts[0])) {
         $output->writeLn("<error>Invalid date or format. Correct format is Y-m-d</error>");
         return;
     }
     $justAnnouncedCourses = $this->getJustAnnounced();
     $users = MTHelper::getUsersToCoursesMap($em, $justAnnouncedCourses);
     $output->writeln("<comment>" . count($users) . ' users found </comment>');
     $scheduled = 0;
     foreach ($users as $uid => $courses) {
         // Only send notifications for interested courses
         if (!isset($courses[UserCourse::LIST_TYPE_INTERESTED])) {
             continue;
         }
         $id = $scheduler->schedule(new \DateTime($date), CourseNewSessionJob::JOB_TYPE_NEW_SESSION, 'ClassCentral\\MOOCTrackerBundle\\Job\\CourseNewSessionJob', $courses, $uid);
         if ($id) {
             $scheduled++;
         }
     }
     $output->writeln("<info>{$scheduled} jobs scheduled</info>");
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $now = new \DateTime();
     $output->writeln("<comment>Create reminder scheduler started on {$now->format('Y-m-d H:i:s')}</comment>");
     $esCourses = $this->getContainer()->get('es_courses');
     $em = $this->getContainer()->get('doctrine')->getManager();
     $scheduler = $this->getContainer()->get('scheduler');
     // Parse the input arguments
     $type = $input->getArgument('type');
     $date = $input->getArgument('date');
     // The date at which the job is to be run
     if ($type != CourseStartReminderJob::JOB_TYPE_1_DAY_BEFORE && $type != CourseStartReminderJob::JOB_TYPE_2_WEEKS_BEFORE) {
         // Invalid job type
         $output->writeln("<error>Invalid job type. Valid types are email_reminder_course_start_2weeks/email_reminder_course_start_1day</error>");
         return;
     }
     $dateParts = explode('-', $date);
     if (!checkdate($dateParts[1], $dateParts[2], $dateParts[0])) {
         $output->writeLn("<error>Invalid date or format. Correct format is Y-m-d</error>");
         return;
     }
     $dt = new \DateTime($date);
     if ($type == CourseStartReminderJob::JOB_TYPE_2_WEEKS_BEFORE) {
         // Find courses starting 2 weeks (14 days after the current date)
         $dt->add(new \DateInterval('P14D'));
     } elseif ($type == CourseStartReminderJob::JOB_TYPE_1_DAY_BEFORE) {
         // Find courses starting 1 day later
         $dt->add(new \DateInterval('P1D'));
     }
     $output->writeln("<comment>{$type} - {$dt->format('Y-m-d')}</comment>");
     $results = $esCourses->findByNextSessionStartDate($dt, $dt);
     $output->writeln("<comment>" . $results['results']['hits']['total'] . ' courses starting on ' . $dt->format('Y-m-d') . " <comment>");
     if ($results['results']['hits']['total'] == 0) {
         $output->writeln("<info>No courses starting on {$dt->format('Y-m-d')}</info>");
         return;
     }
     $courseIds = array();
     foreach ($results['results']['hits']['hits'] as $course) {
         $courseIds[] = $course['_id'];
     }
     $users = MTHelper::getUsersToCoursesMap($em, $courseIds);
     $output->writeln("<comment>" . count($users) . ' users found </comment>');
     $scheduled = 0;
     foreach ($users as $uid => $courses) {
         $id = $scheduler->schedule(new \DateTime($date), $type, 'ClassCentral\\MOOCTrackerBundle\\Job\\CourseStartReminderJob', $courses, $uid);
         if ($id) {
             $scheduled++;
         }
     }
     $output->writeln("<info>{$scheduled} jobs scheduled</info>");
 }