| Conditions | 28 |
| Paths | 100 |
| Total Lines | 165 |
| Code Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 113 | function aiken_import_exercise($file) |
||
| 114 | { |
||
| 115 | $archive_path = api_get_path(SYS_ARCHIVE_PATH).'aiken/'; |
||
| 116 | $baseWorkDir = $archive_path; |
||
| 117 | |||
| 118 | if (!is_dir($baseWorkDir)) { |
||
| 119 | mkdir($baseWorkDir, api_get_permissions_for_new_directories(), true); |
||
| 120 | } |
||
| 121 | |||
| 122 | $uploadPath = 'aiken_'.api_get_unique_id().'/'; |
||
| 123 | |||
| 124 | // set some default values for the new exercise |
||
| 125 | $exercise_info = []; |
||
| 126 | $exercise_info['name'] = preg_replace('/.(zip|txt)$/i', '', $file); |
||
| 127 | $exercise_info['question'] = []; |
||
| 128 | |||
| 129 | // if file is not a .zip, then we cancel all |
||
| 130 | if (!preg_match('/.(zip|txt)$/i', $file)) { |
||
| 131 | return 'YouMustUploadAZipOrTxtFile'; |
||
| 132 | } |
||
| 133 | |||
| 134 | // unzip the uploaded file in a tmp directory |
||
| 135 | if (preg_match('/.(zip|txt)$/i', $file)) { |
||
| 136 | if (!get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)) { |
||
| 137 | return 'ThereWasAProblemWithYourFile'; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // find the different manifests for each question and parse them |
||
| 142 | $exerciseHandle = opendir($baseWorkDir.$uploadPath); |
||
| 143 | $file_found = false; |
||
| 144 | $operation = false; |
||
| 145 | $result = false; |
||
| 146 | |||
| 147 | // Parse every subdirectory to search txt question files |
||
| 148 | while (false !== ($file = readdir($exerciseHandle))) { |
||
| 149 | if (is_dir($baseWorkDir.'/'.$uploadPath.$file) && $file != "." && $file != "..") { |
||
| 150 | //find each manifest for each question repository found |
||
| 151 | $questionHandle = opendir($baseWorkDir.'/'.$uploadPath.$file); |
||
| 152 | while (false !== ($questionFile = readdir($questionHandle))) { |
||
| 153 | if (preg_match('/.txt$/i', $questionFile)) { |
||
| 154 | $result = aiken_parse_file( |
||
| 155 | $exercise_info, |
||
| 156 | $baseWorkDir, |
||
| 157 | $file, |
||
| 158 | $questionFile |
||
| 159 | ); |
||
| 160 | $file_found = true; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } elseif (preg_match('/.txt$/i', $file)) { |
||
| 164 | $result = aiken_parse_file($exercise_info, $baseWorkDir.$uploadPath, '', $file); |
||
| 165 | $file_found = true; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | if (!$file_found) { |
||
| 170 | $result = 'NoTxtFileFoundInTheZip'; |
||
| 171 | } |
||
| 172 | |||
| 173 | if ($result !== true) { |
||
| 174 | return $result; |
||
| 175 | } |
||
| 176 | |||
| 177 | // 1. Create exercise |
||
| 178 | $exercise = new Exercise(); |
||
| 179 | $exercise->exercise = $exercise_info['name']; |
||
| 180 | $exercise->save(); |
||
| 181 | $last_exercise_id = $exercise->selectId(); |
||
| 182 | $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 183 | $tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 184 | if (!empty($last_exercise_id)) { |
||
| 185 | // For each question found... |
||
| 186 | $courseId = api_get_course_int_id(); |
||
| 187 | foreach ($exercise_info['question'] as $key => $question_array) { |
||
| 188 | // 2.create question |
||
| 189 | $question = new Aiken2Question(); |
||
| 190 | $question->type = $question_array['type']; |
||
| 191 | $question->setAnswer(); |
||
| 192 | $question->updateTitle($question_array['title']); |
||
| 193 | |||
| 194 | if (isset($question_array['description'])) { |
||
| 195 | $question->updateDescription($question_array['description']); |
||
| 196 | } |
||
| 197 | $type = $question->selectType(); |
||
| 198 | $question->type = constant($type); |
||
| 199 | $question->save($exercise); |
||
| 200 | $last_question_id = $question->selectId(); |
||
| 201 | //3. Create answer |
||
| 202 | // 3. Create answer |
||
| 203 | $answer = new Answer($last_question_id, $courseId, $exercise, false); |
||
| 204 | $answer->new_nbrAnswers = count($question_array['answer']); |
||
| 205 | $max_score = 0; |
||
| 206 | |||
| 207 | $scoreFromFile = 0; |
||
| 208 | if (isset($question_array['score']) && !empty($question_array['score'])) { |
||
| 209 | $scoreFromFile = $question_array['score']; |
||
| 210 | } |
||
| 211 | |||
| 212 | foreach ($question_array['answer'] as $key => $answers) { |
||
| 213 | $key++; |
||
| 214 | $answer->new_answer[$key] = $answers['value']; |
||
| 215 | $answer->new_position[$key] = $key; |
||
| 216 | $answer->new_comment[$key] = ''; |
||
| 217 | // Correct answers ... |
||
| 218 | if (in_array($key, $question_array['correct_answers'])) { |
||
| 219 | $answer->new_correct[$key] = 1; |
||
| 220 | if (isset($question_array['feedback'])) { |
||
| 221 | $answer->new_comment[$key] = $question_array['feedback']; |
||
| 222 | } |
||
| 223 | } else { |
||
| 224 | $answer->new_correct[$key] = 0; |
||
| 225 | } |
||
| 226 | |||
| 227 | if (isset($question_array['weighting'][$key - 1])) { |
||
| 228 | $answer->new_weighting[$key] = $question_array['weighting'][$key - 1]; |
||
| 229 | $max_score += $question_array['weighting'][$key - 1]; |
||
| 230 | } |
||
| 231 | |||
| 232 | if (!empty($scoreFromFile) && $answer->new_correct[$key]) { |
||
| 233 | $answer->new_weighting[$key] = $scoreFromFile; |
||
| 234 | } |
||
| 235 | |||
| 236 | $params = [ |
||
| 237 | 'c_id' => $courseId, |
||
| 238 | 'question_id' => $last_question_id, |
||
| 239 | 'answer' => $answer->new_answer[$key], |
||
| 240 | 'correct' => $answer->new_correct[$key], |
||
| 241 | 'comment' => $answer->new_comment[$key], |
||
| 242 | 'ponderation' => isset($answer->new_weighting[$key]) ? $answer->new_weighting[$key] : '', |
||
| 243 | 'position' => $answer->new_position[$key], |
||
| 244 | 'hotspot_coordinates' => '', |
||
| 245 | 'hotspot_type' => '', |
||
| 246 | ]; |
||
| 247 | |||
| 248 | $answerId = Database::insert($tableAnswer, $params); |
||
| 249 | if ($answerId) { |
||
| 250 | $params = [ |
||
| 251 | 'id_auto' => $answerId, |
||
| 252 | 'id' => $answerId, |
||
| 253 | ]; |
||
| 254 | Database::update($tableAnswer, $params, ['iid = ?' => [$answerId]]); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | if (!empty($scoreFromFile)) { |
||
| 259 | $max_score = $scoreFromFile; |
||
| 260 | } |
||
| 261 | |||
| 262 | // Now that we know the question score, set it! |
||
| 263 | |||
| 264 | $params = ['ponderation' => $max_score]; |
||
| 265 | Database::update( |
||
| 266 | $tableQuestion, |
||
| 267 | $params, |
||
| 268 | ['iid = ?' => [$last_question_id]] |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | // Delete the temp dir where the exercise was unzipped |
||
| 273 | my_delete($baseWorkDir.$uploadPath); |
||
| 274 | $operation = $last_exercise_id; |
||
| 275 | } |
||
| 276 | |||
| 277 | return $operation; |
||
| 278 | } |
||
| 411 |