Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 14 | class ReadingComprehension extends UniqueAnswer |
||
| 15 | { |
||
| 16 | public static $typePicture = 'reading-comprehension.png'; |
||
| 17 | public static $explanationLangVar = 'ReadingComprehension'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Defines the different speeds of scrolling for the reading window, |
||
| 21 | * in words per minute. If 300 words text in 50w/m, then the moving |
||
| 22 | * window will progress from top to bottom in 6 minutes |
||
| 23 | * @var array $speeds |
||
| 24 | */ |
||
| 25 | public static $speeds = [ |
||
| 26 | 1 => 50, |
||
| 27 | 2 => 100, |
||
| 28 | 3 => 175, |
||
| 29 | 4 => 300, |
||
| 30 | 5 => 600 |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The number of words in the question description (which serves as the |
||
| 35 | * text to read) |
||
| 36 | * @var int $wordsCount |
||
| 37 | */ |
||
| 38 | public $wordsCount = 0; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Number of words expected to show per refresh |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | public $expectedWordsPerRefresh = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Refresh delay in seconds |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | public $refreshTime = 3; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor |
||
| 54 | */ |
||
| 55 | public function __construct() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $wordsCount |
||
| 64 | * @param $turns |
||
| 65 | * @param $text |
||
| 66 | */ |
||
| 67 | private function displayReading($wordsCount, $turns, $text) |
||
| 80 | |||
| 81 | public function processText($text) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns total count of words of the text to read |
||
| 127 | * @return int |
||
| 128 | */ |
||
| 129 | public function getWordsCount() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | public function createForm(&$form) |
||
| 140 | { |
||
| 141 | // Categories |
||
| 142 | $tabCat = TestCategory::getCategoriesIdAndName(); |
||
| 143 | $form->addSelect('questionCategory', get_lang('Category'), $tabCat); |
||
| 144 | // Advanced parameters |
||
| 145 | $levels = self::get_default_levels(); |
||
| 146 | $form->addSelect('questionLevel', get_lang('Difficulty'), $levels); |
||
| 147 | $form->addElement('hidden', 'answerType', READING_COMPREHENSION); |
||
| 148 | $form->addTextarea('questionDescription', get_lang('Text'), ['rows' => 20]); |
||
| 149 | // question name |
||
| 150 | View Code Duplication | if (api_get_configuration_value('save_titles_as_html')) { |
|
| 151 | $editorConfig = ['ToolbarSet' => 'Minimal']; |
||
| 152 | $form->addHtmlEditor( |
||
| 153 | 'questionName', |
||
| 154 | get_lang('Question'), |
||
| 155 | false, |
||
| 156 | false, |
||
| 157 | $editorConfig, |
||
| 158 | true |
||
| 159 | ); |
||
| 160 | } else { |
||
| 161 | $form->addText('questionName', get_lang('Question'), false); |
||
| 162 | } |
||
| 163 | |||
| 164 | // hidden values |
||
| 165 | $my_id = isset($_REQUEST['myid']) ? intval($_REQUEST['myid']) : null; |
||
| 166 | $form->addElement('hidden', 'myid', $my_id); |
||
| 167 | $form->addRule('questionName', get_lang('GiveQuestion'), 'required'); |
||
| 168 | |||
| 169 | $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null; |
||
| 170 | |||
| 171 | // default values |
||
| 172 | $defaults = array(); |
||
| 173 | $defaults['questionName'] = $this->question; |
||
| 174 | $defaults['questionDescription'] = $this->description; |
||
| 175 | $defaults['questionLevel'] = $this->level; |
||
| 176 | $defaults['questionCategory'] = $this->category; |
||
| 177 | |||
| 178 | // Came from he question pool |
||
| 179 | if (isset($_GET['fromExercise'])) { |
||
| 180 | $form->setDefaults($defaults); |
||
| 181 | } |
||
| 182 | |||
| 183 | View Code Duplication | if (!empty($_REQUEST['myid'])) { |
|
| 184 | $form->setDefaults($defaults); |
||
| 185 | } else { |
||
| 186 | if ($isContent == 1) { |
||
| 187 | $form->setDefaults($defaults); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @inheritdoc |
||
| 194 | */ |
||
| 195 | public static function get_default_levels() |
||
| 206 | } |
||
| 207 |