/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('<info>Starting former student synchronization process.</info>'); $filter = $input->getArgument('filter'); $formerStudents = $this->directory->findByLdapFilter($filter); if (!$formerStudents) { $output->writeln("<error>{$filter} returned no results.</error>"); return; } $output->writeln('<info>Found ' . count($formerStudents) . ' former students in the directory.</info>'); $formerStudentsCampusIds = array_map(function (array $arr) { return $arr['campusId']; }, $formerStudents); $notFormerStudents = $this->userManager->findUsersWhoAreNotFormerStudents($formerStudentsCampusIds); $usersToUpdate = $notFormerStudents->filter(function (UserInterface $user) { return !$user->isUserSyncIgnore(); }); if (!$usersToUpdate->count() > 0) { $output->writeln("<info>There are no students to update.</info>"); return; } $output->writeln('<info>There are ' . $usersToUpdate->count() . ' students in Ilios who will be marked as a Former Student.</info>'); $rows = $usersToUpdate->map(function (UserInterface $user) { return [$user->getCampusId(), $user->getFirstName(), $user->getLastName(), $user->getEmail()]; })->toArray(); $table = new Table($output); $table->setHeaders(array('Campus ID', 'First', 'Last', 'Email'))->setRows($rows); $table->render(); $helper = $this->getHelper('question'); $output->writeln(''); $question = new ConfirmationQuestion('<question>Do you wish to mark these users as Former Students? </question>' . "\n", true); if ($helper->ask($input, $output, $question)) { /* @var UserRoleInterface $formerStudentRole */ $formerStudentRole = $this->userRoleManager->findOneBy(array('title' => 'Former Student')); /* @var UserInterface $user */ foreach ($usersToUpdate as $user) { $formerStudentRole->addUser($user); $user->addRole($formerStudentRole); $this->userManager->update($user, false); } $this->userRoleManager->update($formerStudentRole); $output->writeln('<info>Former students updated successfully!</info>'); } else { $output->writeln('<comment>Update canceled,</comment>'); } }
/** * @param int $origCourseId * @return CourseInterface * @throws \Exception */ private function getOriginalCourse($origCourseId) { $origCourse = $this->courseManager->findOneBy(['id' => $origCourseId]); if (empty($origCourse)) { throw new \Exception('There are no courses with courseId ' . $origCourseId . '.'); } return $origCourse; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $filter = $input->getArgument('filter'); $schoolId = $input->getArgument('schoolId'); $school = $this->schoolManager->findOneBy(['id' => $schoolId]); if (!$school) { throw new \Exception("School with id {$schoolId} could not be found."); } $output->writeln("<info>Searching for new students to add to " . $school->getTitle() . ".</info>"); $students = $this->directory->findByLdapFilter($filter); if (!$students) { $output->writeln("<error>{$filter} returned no results.</error>"); return; } $output->writeln('<info>Found ' . count($students) . ' students in the directory.</info>'); $campusIds = $this->userManager->getAllCampusIds(); $newStudents = array_filter($students, function (array $arr) use($campusIds) { return !in_array($arr['campusId'], $campusIds); }); if (!count($newStudents) > 0) { $output->writeln("<info>There are no new students to add.</info>"); return; } $output->writeln('<info>There are ' . count($newStudents) . ' new students to be added to ' . $school->getTitle() . '.</info>'); $rows = array_map(function (array $arr) { return [$arr['campusId'], $arr['firstName'], $arr['lastName'], $arr['email']]; }, $newStudents); $table = new Table($output); $table->setHeaders(array('Campus ID', 'First', 'Last', 'Email'))->setRows($rows); $table->render(); $helper = $this->getHelper('question'); $output->writeln(''); $question = new ConfirmationQuestion('<question>Do you wish to add these students to ' . $school->getTitle() . '? </question>' . "\n", true); if ($helper->ask($input, $output, $question)) { $studentRole = $this->userRoleManager->findOneBy(array('title' => 'Student')); foreach ($newStudents as $userRecord) { if (empty($userRecord['email'])) { $output->writeln('<error>Unable to add student ' . var_export($userRecord, true) . ' they have no email address.</error>'); continue; } if (empty($userRecord['campusId'])) { $output->writeln('<error>Unable to add student ' . var_export($userRecord, true) . ' they have no campus ID.</error>'); continue; } if (empty($userRecord['username'])) { $output->writeln('<error>Unable to add student ' . var_export($userRecord, true) . ' they have no username.</error>'); continue; } $user = $this->userManager->create(); $user->setFirstName($userRecord['firstName']); $user->setLastName($userRecord['lastName']); $user->setEmail($userRecord['email']); $user->setCampusId($userRecord['campusId']); $user->setAddedViaIlios(true); $user->setEnabled(true); $user->setSchool($school); $user->setUserSyncIgnore(false); $user->addRole($studentRole); $this->userManager->update($user); $authentication = $this->authenticationManager->create(); $authentication->setUser($user); $authentication->setUsername($userRecord['username']); $this->authenticationManager->update($authentication, false); $studentRole->addUser($user); $this->userRoleManager->update($studentRole); $output->writeln('<info>Success! New student #' . $user->getId() . ' ' . $user->getFirstAndLastName() . ' created.</info>'); } } else { $output->writeln('<comment>Update canceled.</comment>'); } }
/** * Retrieves a curriculum inventory in a data structure that lends itself for an easy transformation into * XML-formatted report. * * @param CurriculumInventoryReportInterface $invReport The report object. * @return array An associated array, containing the inventory. * Data is keyed off by: * 'report' ... The inventory report entity. * 'institution' ... An object representing the curriculum inventory's owning institution * 'events' ... An array of events, keyed off by event id. Each event is represented as assoc. array. * 'expectations' ... An associative array of arrays, each sub-array containing a * list of a different type of "competency object" within the curriculum. * These types are program objectives, course objectives and session objectives. * The keys for these type-specific sub-arrays are: * 'program_objectives' * 'course_objectives' * 'session_objectives' * 'framework' ... The competency framework data set. * 'includes' ... Identifiers of the various competency objects referenced in the framework. * 'pcrs_ids' * 'program_objective_ids' * 'course_objective_ids' * 'session_objective_ids' * 'relations' ... Relations between the various competencies within the framework * 'program_objectives_to_pcrs' * 'course_objectives_to_program_objectives' * 'session_objectives_to_course_objectives' * 'sequence_block_references' ...relationships maps between sequence blocks and other curricular entities. * 'events' ... maps sequence blocks to events * 'competency_objects' .. maps sequence blocks to competency objects * * @throws \Exception */ public function getCurriculumInventory(CurriculumInventoryReportInterface $invReport) { // report validation $program = $invReport->getProgram(); if (!$program) { throw new \Exception('No program found for report with id ' . $invReport->getId() . '.'); } $school = $program->getSchool(); if (!$school) { throw new \Exception('No school found for program with id = ' . $program->getId() . '.'); } /** @var CurriculumInventoryInstitutionInterface $institution */ $institution = $this->institutionManager->findOneBy(['school' => $school->getId()]); if (!$institution) { throw new \Exception('No curriculum inventory institution found for school with id = ' . $school->getId() . '.'); } $events = $this->reportManager->getEvents($invReport); $keywords = $this->reportManager->getEventKeywords($invReport); $resourceTypes = $this->reportManager->getEventResourceTypes($invReport); $eventRefsForSeqBlocks = $this->reportManager->getEventReferencesForSequenceBlocks($invReport); $programObjectives = $this->reportManager->getProgramObjectives($invReport); $sessionObjectives = $this->reportManager->getSessionObjectives($invReport); $courseObjectives = $this->reportManager->getCourseObjectives($invReport); $compObjRefsForSeqBlocks = $this->reportManager->getCompetencyObjectReferencesForSequenceBlocks($invReport); $compRefsForEvents = $this->reportManager->getCompetencyObjectReferencesForEvents($invReport); // The various objective type are all "Competency Objects" in the context of reporting the curriculum inventory. // The are grouped in the "Expectations" section of the report, lump 'em together here. $expectations = []; $expectations['program_objectives'] = $programObjectives; $expectations['session_objectives'] = $sessionObjectives; $expectations['course_objectives'] = $courseObjectives; // Build out the competency framework information and added to $expectations. $pcrs = $this->reportManager->getPcrs($invReport); $pcrsIds = array_keys($pcrs); $programObjectiveIds = array_keys($programObjectives); $courseObjectiveIds = array_keys($courseObjectives); $sessionObjectiveIds = array_keys($sessionObjectives); $includes = ['pcrs_ids' => [], 'program_objective_ids' => [], 'course_objective_ids' => [], 'session_objective_ids' => []]; $relations = ['program_objectives_to_pcrs' => [], 'course_objectives_to_program_objectives' => [], 'session_objectives_to_course_objectives' => []]; $rel = $this->reportManager->getProgramObjectivesToPcrsRelations($programObjectiveIds, $pcrsIds); $relations['program_objectives_to_pcrs'] = $rel['relations']; $includes['pcrs_ids'] = $rel['pcrs_ids']; $includes['program_objective_ids'] = $rel['program_objective_ids']; $rel = $this->reportManager->getCourseObjectivesToProgramObjectivesRelations($courseObjectiveIds, $programObjectiveIds); $relations['course_objectives_to_program_objectives'] = $rel['relations']; $includes['program_objective_ids'] = array_values(array_unique(array_merge($includes['program_objective_ids'], $rel['program_objective_ids']))); $includes['course_objective_ids'] = $rel['course_objective_ids']; $rel = $this->reportManager->getSessionObjectivesToCourseObjectivesRelations($sessionObjectiveIds, $courseObjectiveIds); $relations['session_objectives_to_course_objectives'] = $rel['relations']; $includes['course_objective_ids'] = array_values(array_unique(array_merge($includes['course_objective_ids'], $rel['course_objective_ids']))); $includes['session_objective_ids'] = $rel['session_objective_ids']; $expectations['framework'] = ['includes' => $includes, 'relations' => $relations]; // // transmogrify inventory data for reporting and fill in the blanks // $events = $this->addKeywordsToEvents($events, $keywords); $events = $this->addResourceTypesToEvents($events, $resourceTypes); $events = $this->addCompetencyObjectReferencesToEvents($events, $compRefsForEvents); // // aggregate inventory into single return-array // $rhett = []; $rhett['report'] = $invReport; $rhett['expectations'] = $expectations; $rhett['institution'] = $institution; $rhett['events'] = $events; $rhett['sequence_block_references'] = ['events' => $eventRefsForSeqBlocks, 'competency_objects' => $compObjRefsForSeqBlocks]; return $rhett; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { // prevent this command to run on a non-empty user store. $existingUser = $this->userManager->findOneBy([]); if (!empty($existingUser)) { throw new \Exception('Sorry, at least one user record already exists. Cannot create a "first" user account.'); } $schools = $this->schoolManager->findBy([], ['title' => 'ASC']); // check if any school data is present before invoking the form helper // to prevent the form from breaking on missing school data further downstream. if (empty($schools)) { throw new \Exception('No schools found. Please load schools into this Ilios instance first.'); } $schoolId = $input->getOption('school'); if (!$schoolId) { $schoolTitles = []; /* @var SchoolInterface $school */ foreach ($schools as $school) { $schoolTitles[$school->getTitle()] = $school->getId(); } $helper = $this->getHelper('question'); $question = new ChoiceQuestion("What is this user's primary school?", array_keys($schoolTitles)); $question->setErrorMessage('School %s is invalid.'); $schoolTitle = $helper->ask($input, $output, $question); $schoolId = $schoolTitles[$schoolTitle]; } $school = $this->schoolManager->findOneBy(['id' => $schoolId]); if (!$school) { throw new \Exception("School with id {$schoolId} could not be found."); } $email = $input->getOption('email'); if (!$email) { $question = new Question("What is the user's Email Address? "); $question->setValidator(function ($answer) { if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) { throw new \RuntimeException("Email is not valid"); } return $answer; }); $email = $this->getHelper('question')->ask($input, $output, $question); } $user = $this->userManager->create(); $user->setFirstName(self::FIRST_NAME); $user->setMiddleName(date('Y-m-d_h.i.s')); $user->setLastName(self::LAST_NAME); $user->setEmail($email); $user->setAddedViaIlios(true); $user->setEnabled(true); $user->setUserSyncIgnore(false); $user->addRole($this->userRoleManager->findOneBy(['title' => 'Developer'])); $user->addRole($this->userRoleManager->findOneBy(['title' => 'Course Director'])); $user->setSchool($school); $this->userManager->update($user); $authentication = $this->authenticationManager->create(); $authentication->setUser($user); $user->setAuthentication($authentication); $encodedPassword = $this->passwordEncoder->encodePassword($user, self::PASSWORD); $authentication->setUsername(self::USERNAME); $authentication->setPasswordBcrypt($encodedPassword); $this->authenticationManager->update($authentication); $output->writeln('Success!'); $output->writeln('A user account has been created.'); $output->writeln(sprintf("You may now log in as '%s' with the password '%s'.", self::USERNAME, self::PASSWORD)); $output->writeln('Please change this password as soon as possible.'); }
/** * Retrieves an the corresponding entity for a given data array. * * @param array $data * @return mixed the entity */ protected function getEntity(array $data) { return $this->em->findOneBy(['id' => $data[0]]); }