Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // input validation
     $errors = $this->validateInput($input);
     if (!empty($errors)) {
         foreach ($errors as $error) {
             $output->writeln("<error>{$error}</error>");
         }
         return;
     }
     $daysInAdvance = $input->getOption('days');
     $sender = $input->getArgument('sender');
     $baseUrl = rtrim($input->getArgument('base_url'), '/');
     $subject = $input->getOption('subject');
     $isDryRun = $input->getOption('dry-run');
     // get all applicable offerings.
     $offerings = $this->offeringManager->getOfferingsForTeachingReminders($daysInAdvance);
     if ($offerings->isEmpty()) {
         $output->writeln('<info>No offerings with pending teaching reminders found.</info>');
         return;
     }
     // mail out a reminder per instructor per offering.
     $templateCache = [];
     $iterator = $offerings->getIterator();
     $i = 0;
     /** @var OfferingInterface $offering */
     foreach ($iterator as $offering) {
         $school = $offering->getSession()->getCourse()->getSchool();
         if (!array_key_exists($school->getId(), $templateCache)) {
             $template = $this->getTemplatePath($school);
             $templateCache[$school->getId()] = $template;
         }
         $template = $templateCache[$school->getId()];
         $instructors = $offering->getAllInstructors()->toArray();
         /** @var UserInterface $instructor */
         foreach ($instructors as $instructor) {
             $i++;
             $messageBody = $this->templatingEngine->render($template, ['base_url' => $baseUrl, 'instructor' => $instructor, 'offering' => $offering]);
             $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($sender)->setTo($instructor->getEmail())->setCharset('UTF-8')->setContentType('text/plain')->setBody($messageBody)->setMaxLineLength(998);
             if ($isDryRun) {
                 $output->writeln($message->getHeaders()->toString());
                 $output->writeln($message->getBody());
             } else {
                 $this->mailer->send($message);
             }
         }
     }
     $output->writeln("<info>Sent {$i} teaching reminders.</info>");
 }
 /**
  * @covers Ilios\CliBundle\Command\SendTeachingRemindersCommand::execute
  */
 public function testExecuteDryRun()
 {
     $sender = '*****@*****.**';
     $baseUrl = 'https://ilios.bar.edu';
     $this->commandTester->execute(['sender' => $sender, 'base_url' => $baseUrl, '--dry-run' => true]);
     /** @var OfferingInterface $offering */
     $offering = $this->fakeOfferingManager->getOfferingsForTeachingReminders(7)->toArray()[0];
     $output = $this->commandTester->getDisplay();
     /** @var UserInterface $instructor */
     foreach ($offering->getAllInstructors()->toArray() as $instructor) {
         $this->assertContains("To: {$instructor->getEmail()}", $output);
         $this->assertContains("Dear {$instructor->getFirstName()} {$instructor->getLastName()}", $output);
     }
     $this->assertContains("From: {$sender}", $output);
     $subject = SendTeachingRemindersCommand::DEFAULT_MESSAGE_SUBJECT;
     $this->assertContains("Subject: {$subject}", $output);
     $this->assertContains("upcoming {$offering->getSession()->getSessionType()->getTitle()}", $output);
     $this->assertContains("School of {$offering->getSession()->getCourse()->getSchool()->getTitle()}", $output);
     $this->assertContains("Course:   {$offering->getSession()->getCourse()->getTitle()}", $output);
     $this->assertContains("Session:  {$offering->getSession()->getTitle()}", $output);
     $this->assertContains('Date:     ' . $offering->getStartDate()->format('D M d, Y'), $output);
     $this->assertContains('Time:     ' . $offering->getStartDate()->format('h:i a') . ' - ' . $offering->getEndDate()->format('h:i a'), $output);
     $this->assertContains("Location: {$offering->getRoom()}", $output);
     $this->assertContains("Coordinator at {$offering->getSession()->getCourse()->getSchool()->getIliosAdministratorEmail()}.", $output);
     /** @var LearnerGroupInterface $learnerGroup */
     foreach ($offering->getLearnerGroups()->toArray() as $learnerGroup) {
         $this->assertContains("- {$learnerGroup->getTitle()}", $output);
     }
     /** @var UserInterface $learner */
     foreach ($offering->getLearners()->toArray() as $learner) {
         $this->assertContains("- {$learner->getFirstName()} {$learner->getLastName()}", $output);
     }
     /** @var ObjectiveInterface $objective */
     foreach ($offering->getSession()->getObjectives() as $objective) {
         $this->assertContains("- {$objective->getTitle()}", $output);
     }
     /** @var ObjectiveInterface $objective */
     foreach ($offering->getSession()->getCourse()->getObjectives() as $objective) {
         $this->assertContains("- {$objective->getTitle()}", $output);
     }
     $this->assertContains("{$baseUrl}/courses/{$offering->getSession()->getCourse()->getId()}", $output);
     $totalMailsSent = count($offering->getAllInstructors()->toArray());
     $this->assertContains("Sent {$totalMailsSent} teaching reminders.", $output);
 }