| Conditions | 21 |
| Paths | 100 |
| Total Lines | 140 |
| Code Lines | 81 |
| Lines | 11 |
| Ratio | 7.86 % |
| 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 |
||
| 84 | function aiken_import_exercise($file) |
||
| 85 | { |
||
| 86 | global $exercise_info; |
||
| 87 | global $element_pile; |
||
| 88 | global $non_HTML_tag_to_avoid; |
||
| 89 | global $record_item_body; |
||
| 90 | // used to specify the question directory where files could be found in relation in any question |
||
| 91 | global $questionTempDir; |
||
| 92 | $archive_path = api_get_path(SYS_ARCHIVE_PATH) . 'aiken'; |
||
| 93 | $baseWorkDir = $archive_path; |
||
| 94 | |||
| 95 | if (!is_dir($baseWorkDir)) { |
||
| 96 | mkdir($baseWorkDir, api_get_permissions_for_new_directories(), true); |
||
| 97 | } |
||
| 98 | |||
| 99 | $uploadPath = '/'; |
||
| 100 | |||
| 101 | // set some default values for the new exercise |
||
| 102 | $exercise_info = array(); |
||
| 103 | $exercise_info['name'] = preg_replace('/.(zip|txt)$/i', '', $file); |
||
| 104 | $exercise_info['question'] = array(); |
||
| 105 | $element_pile = array(); |
||
| 106 | |||
| 107 | // create parser and array to retrieve info from manifest |
||
| 108 | $element_pile = array(); //pile to known the depth in which we are |
||
| 109 | |||
| 110 | // if file is not a .zip, then we cancel all |
||
| 111 | if (!preg_match('/.(zip|txt)$/i', $file)) { |
||
| 112 | //Display :: display_error_message(get_lang('YouMustUploadAZipOrTxtFile')); |
||
| 113 | |||
| 114 | return 'YouMustUploadAZipOrTxtFile'; |
||
| 115 | } |
||
| 116 | |||
| 117 | // unzip the uploaded file in a tmp directory |
||
| 118 | if (preg_match('/.(zip|txt)$/i', $file)) { |
||
| 119 | if (!get_and_unzip_uploaded_exercise($baseWorkDir, $uploadPath)) { |
||
| 120 | |||
| 121 | return 'ThereWasAProblemWithYourFile'; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // find the different manifests for each question and parse them |
||
| 126 | $exerciseHandle = opendir($baseWorkDir); |
||
| 127 | $file_found = false; |
||
| 128 | $operation = false; |
||
| 129 | $result = false; |
||
| 130 | |||
| 131 | // Parse every subdirectory to search txt question files |
||
| 132 | while (false !== ($file = readdir($exerciseHandle))) { |
||
| 133 | if (is_dir($baseWorkDir . '/' . $file) && $file != "." && $file != "..") { |
||
| 134 | //find each manifest for each question repository found |
||
| 135 | $questionHandle = opendir($baseWorkDir . '/' . $file); |
||
| 136 | View Code Duplication | while (false !== ($questionFile = readdir($questionHandle))) { |
|
| 137 | if (preg_match('/.txt$/i', $questionFile)) { |
||
| 138 | $result = aiken_parse_file( |
||
| 139 | $exercise_info, |
||
| 140 | $baseWorkDir, |
||
| 141 | $file, |
||
| 142 | $questionFile |
||
| 143 | ); |
||
| 144 | $file_found = true; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } elseif (preg_match('/.txt$/i', $file)) { |
||
| 148 | $result = aiken_parse_file($exercise_info, $baseWorkDir, '', $file); |
||
| 149 | $file_found = true; |
||
| 150 | } // else ignore file |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!$file_found) { |
||
| 154 | $result = 'NoTxtFileFoundInTheZip'; |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($result !== true ) { |
||
| 158 | return $result; |
||
| 159 | } |
||
| 160 | |||
| 161 | // Add exercise in tool |
||
| 162 | |||
| 163 | // 1.create exercise |
||
| 164 | $exercise = new Exercise(); |
||
| 165 | $exercise->exercise = $exercise_info['name']; |
||
| 166 | |||
| 167 | $exercise->save(); |
||
| 168 | $last_exercise_id = $exercise->selectId(); |
||
| 169 | if (!empty($last_exercise_id)) { |
||
| 170 | // For each question found... |
||
| 171 | foreach ($exercise_info['question'] as $key => $question_array) { |
||
| 172 | //2.create question |
||
| 173 | $question = new Aiken2Question(); |
||
| 174 | $question->type = $question_array['type']; |
||
| 175 | $question->setAnswer(); |
||
| 176 | $question->updateTitle($question_array['title']); |
||
| 177 | |||
| 178 | if (isset($question_array['description'])) { |
||
| 179 | $question->updateDescription($question_array['description']); |
||
| 180 | } |
||
| 181 | $type = $question->selectType(); |
||
| 182 | $question->type = constant($type); |
||
| 183 | $question->save($last_exercise_id); |
||
| 184 | $last_question_id = $question->selectId(); |
||
| 185 | //3. Create answer |
||
| 186 | $answer = new Answer($last_question_id); |
||
| 187 | |||
| 188 | $answer->new_nbrAnswers = count($question_array['answer']); |
||
| 189 | $max_score = 0; |
||
| 190 | |||
| 191 | foreach ($question_array['answer'] as $key => $answers) { |
||
| 192 | $key++; |
||
| 193 | $answer->new_answer[$key] = $answers['value']; |
||
| 194 | $answer->new_position[$key] = $key; |
||
| 195 | // Correct answers ... |
||
| 196 | if (in_array($key, $question_array['correct_answers'])) { |
||
| 197 | $answer->new_correct[$key] = 1; |
||
| 198 | if (isset($question_array['feedback'])) { |
||
| 199 | $answer->new_comment[$key] = $question_array['feedback']; |
||
| 200 | } |
||
| 201 | } else { |
||
| 202 | $answer->new_correct[$key] = 0; |
||
| 203 | } |
||
| 204 | |||
| 205 | if (isset($question_array['weighting'][$key - 1])) { |
||
| 206 | $answer->new_weighting[$key] = $question_array['weighting'][$key - 1]; |
||
| 207 | $max_score += $question_array['weighting'][$key - 1]; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | $answer->save(); |
||
| 212 | // Now that we know the question score, set it! |
||
| 213 | $question->updateWeighting($max_score); |
||
| 214 | $question->save(); |
||
| 215 | } |
||
| 216 | |||
| 217 | // Delete the temp dir where the exercise was unzipped |
||
| 218 | my_delete($baseWorkDir . $uploadPath); |
||
| 219 | $operation = $last_exercise_id; |
||
| 220 | } |
||
| 221 | |||
| 222 | return $operation; |
||
| 223 | } |
||
| 224 | |||
| 350 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.