forked from certificationy/certificationy-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartCommand.php
More file actions
180 lines (150 loc) · 6.26 KB
/
Copy pathStartCommand.php
File metadata and controls
180 lines (150 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
* This file is part of the Certificationy CLI application.
*
* (c) Vincent Composieux <vincent.composieux@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Certificationy\Cli\Command;
use Certificationy\Certification\Loader;
use Certificationy\Certification\Set;
use Symfony\Component\Console\Command\Command;
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 Symfony\Component\Console\Question\ChoiceQuestion;
/**
* Class StartCommand
*
* This is the command to start a new questions set
*
* @author Vincent Composieux <vincent.composieux@gmail.com>
*/
class StartCommand extends Command
{
/**
* @var integer
*/
const WORDWRAP_NUMBER = 80;
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('start')
->setDescription('Starts a new question set')
->addOption('number', null, InputOption::VALUE_OPTIONAL, 'How many questions do you want?', 20)
->addOption('list', 'l', InputOption::VALUE_NONE, 'List categories')
->addOption("training", null, InputOption::VALUE_NONE, "Training mode: the solution is displayed after each question")
->addOption('show-multiple-choice', null, InputOption::VALUE_OPTIONAL, 'Should we tell you when the question is multiple choice?', true)
->addArgument('categories', InputArgument::IS_ARRAY, 'Which categories do you want (separate multiple with a space)', array())
->addOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom config', null)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = $this->path($input->getOption('config'));
if ($input->getOption('list')) {
$output->writeln(Loader::getCategories($config));
return ;
}
$categories = $input->getArgument('categories');
$number = $input->getOption('number');
$set = Loader::init($number, $categories, $config);
if ($set->getQuestions()) {
$output->writeln(
sprintf('Starting a new set of <info>%s</info> questions (available questions: <info>%s</info>)', count($set->getQuestions()), Loader::count(array(), $this->path()))
);
$this->askQuestions($set, $input, $output);
$this->displayResults($set, $output);
} else {
$output->writeln('<error>✗</error> No questions can be found.');
}
}
/**
* Ask questions
*
* @param Set $set A Certificationy questions Set instance
* @param InputInterface $input A Symfony Console input instance
* @param OutputInterface $output A Symfony Console output instance
*/
protected function askQuestions(Set $set, InputInterface $input, OutputInterface $output)
{
$questionHelper = $this->getHelper('question');
$showMultipleChoice = $input->getOption('show-multiple-choice');
$questionCount = 1;
foreach($set->getQuestions() as $i => $question) {
$choiceQuestion = new ChoiceQuestion(
sprintf(
'Question <comment>#%d</comment> [<info>%s</info>] %s'.
($showMultipleChoice === true ? "\n" . 'This question <comment>'.($question->isMultipleChoice() === true ? 'IS' : 'IS NOT')."</comment> multiple choice." : ""),
$questionCount++, $question->getCategory(), $question->getQuestion()
),
$question->getAnswersLabels()
);
$multiSelect = $showMultipleChoice === true ? $question->isMultipleChoice() : true;
$choiceQuestion->setMultiselect($multiSelect);
$choiceQuestion->setErrorMessage('Answer %s is invalid.');
$answer = $questionHelper->ask($input, $output, $choiceQuestion);
$answers = true === $multiSelect ? $answer : array($answer);
$answer = true === $multiSelect ? implode(', ', $answer) : $answer;
$set->setAnswer($i, $answers);
if($input->getOption("training"))
{
$uniqueSet = new Set(array($i => $question));
$uniqueSet->setAnswer($i, $answers);
$this->displayResults($uniqueSet, $output);
}
$output->writeln('<comment>✎ Your answer</comment>: ' . $answer . "\n");
}
}
/**
* Returns results
*
* @param Set $set A Certificationy questions Set instance
* @param OutputInterface $output A Symfony Console output instance
*/
protected function displayResults(Set $set, OutputInterface $output)
{
$results = array();
$questionCount = 1;
foreach($set->getQuestions() as $key => $question) {
$isCorrect = $set->isCorrect($key);
$label = wordwrap($question->getQuestion(), self::WORDWRAP_NUMBER, "\n");
$results[] = array(
sprintf('<comment>#%d</comment> %s', $questionCount++, $label),
wordwrap(implode(', ', $question->getCorrectAnswersValues()), self::WORDWRAP_NUMBER, "\n"),
$isCorrect ? '<info>✔</info>' : '<error>✗</error>'
);
}
if ($results) {
$tableHelper = $this->getHelper('table');
$tableHelper
->setHeaders(array('Question', 'Correct answer', 'Result'))
->setRows($results)
;
$tableHelper->render($output);
$output->writeln(
sprintf('<comment>Results</comment>: <error>errors: %s</error> - <info>correct: %s</info>', $set->getErrorsNumber(), $set->getValidNumber())
);
}
}
/**
* Returns configuration file path
*
* @param null|string $config
*
* @return String $path The configuration filepath
*/
protected function path($config = null)
{
return $config ? $config : dirname(__DIR__).DIRECTORY_SEPARATOR.('config.yml');
}
}