aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Course
diff options
context:
space:
mode:
authorMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2023-05-16 09:17:37 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2023-05-16 09:17:37 +0000
commit1de91f34232717544892bfddd3314900edf91f3b (patch)
tree4ec7e15e07499ffe631135c2ae36a7da4da54588 /cli/Commands/Course
parent132e3454d06db698ca5c184050b4a4aadef9d574 (diff)
Add CLI scripts to get and set global configuration.
Closes #2588 Merge request studip/studip!1746
Diffstat (limited to 'cli/Commands/Course')
-rw-r--r--cli/Commands/Course/GetCourse.php69
1 files changed, 69 insertions, 0 deletions
diff --git a/cli/Commands/Course/GetCourse.php b/cli/Commands/Course/GetCourse.php
new file mode 100644
index 0000000..97a7772
--- /dev/null
+++ b/cli/Commands/Course/GetCourse.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace Studip\Cli\Commands\Course;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\Table;
+use Symfony\Component\Console\Helper\TableSeparator;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Course;
+
+class GetCourse extends Command
+{
+ protected static $defaultName = 'course:get';
+
+ protected function configure(): void
+ {
+ $this->setDescription('Get data of Stud.IP courses.');
+ $this->setHelp('This command will return the course data.');
+ $this->addOption(
+ 'field',
+ 'f',
+ InputOption::VALUE_OPTIONAL,
+ 'In which database field should be searched',
+ 'name'
+ );
+ $this->addArgument('needle', InputArgument::REQUIRED, 'Value to search for a course.');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $needle = $input->getArgument('needle');
+ $field = $input->getOption('field');
+
+ if ($field === 'id') {
+ $courses = array_filter([Course::find($needle)]);
+ } else {
+ $courses = Course::findBySQL($field . " LIKE :needle ORDER BY $field", [':needle' => "%$needle%"]);
+ }
+
+ if (empty($courses)) {
+ $output->writeln('<error>Could not find courses</error>');
+ return Command::FAILURE;
+ }
+ $data = [];
+
+ foreach ($courses as $i => $course) {
+ $data[] = [
+ $course->id,
+ $course->veranstaltungsnummer,
+ $course->getFullName(),
+ $course->getTextualSemester()
+ ];
+ if ($i + 1 < count($courses)) {
+ $data[] = [new TableSeparator(), new TableSeparator()];
+ }
+ }
+
+ $table = new Table($output);
+ $table->setHeaders(['Id', 'Course-number', 'Name', 'Semester'])
+ ->setRows($data)
+ ->setStyle('box')
+ ->render();
+
+ return Command::SUCCESS;
+ }
+}