| Total Complexity | 58 |
| Total Lines | 357 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 1 | Features | 0 |
Complex classes like RemedialCoursePlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RemedialCoursePlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class RemedialCoursePlugin extends Plugin |
||
| 9 | { |
||
| 10 | const SETTING_ENABLED = 'enabled'; |
||
| 11 | const EXTRAFIELD_REMEDIAL_VARIABLE = 'remedialcourselist'; |
||
| 12 | const EXTRAFIELD_ADVACED_VARIABLE = 'advancedcourselist'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * RemedialCoursePlugin constructor. |
||
| 16 | */ |
||
| 17 | protected function __construct() |
||
| 18 | { |
||
| 19 | $settings = [ |
||
| 20 | self::SETTING_ENABLED => 'boolean', |
||
| 21 | ]; |
||
| 22 | parent::__construct( |
||
| 23 | '1.0', |
||
| 24 | 'Carlos Alvarado', |
||
| 25 | $settings |
||
| 26 | ); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Create a new instance of RemedialCoursePlugin. |
||
| 31 | */ |
||
| 32 | public static function create(): RemedialCoursePlugin |
||
| 33 | { |
||
| 34 | static $result = null; |
||
| 35 | |||
| 36 | return $result ?: $result = new self(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Perform the plugin installation. |
||
| 41 | */ |
||
| 42 | public function install() |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Save the arrangement for remedialcourselist, it is adjusted internally so that the values |
||
| 50 | * match the necessary ones. |
||
| 51 | */ |
||
| 52 | public function saveRemedialField() |
||
| 53 | { |
||
| 54 | $extraField = new ExtraField('exercise'); |
||
| 55 | $remedialcourselist = $extraField->get_handler_field_info_by_field_variable(self::EXTRAFIELD_REMEDIAL_VARIABLE); |
||
| 56 | if (false === $remedialcourselist) { |
||
| 57 | $extraField->save([ |
||
| 58 | 'field_type' => ExtraField::FIELD_TYPE_SELECT_MULTIPLE, |
||
| 59 | 'variable' => self::EXTRAFIELD_REMEDIAL_VARIABLE, |
||
| 60 | 'display_text' => 'remedialCourseList', |
||
| 61 | 'default_value' => 1, |
||
| 62 | 'field_order' => 0, |
||
| 63 | 'visible_to_self' => 1, |
||
| 64 | 'visible_to_others' => 0, |
||
| 65 | 'changeable' => 1, |
||
| 66 | 'filter' => 0, |
||
| 67 | ]); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Save the arrangement for remedialadvancecourselist, it is adjusted internally so that the values |
||
| 73 | * match the necessary ones. |
||
| 74 | */ |
||
| 75 | public function saveAdvanceRemedialField() |
||
| 76 | { |
||
| 77 | $extraField = new ExtraField('exercise'); |
||
| 78 | $advancedcourselist = $extraField->get_handler_field_info_by_field_variable( |
||
| 79 | self::EXTRAFIELD_ADVACED_VARIABLE |
||
| 80 | ); |
||
| 81 | if (false === $advancedcourselist) { |
||
| 82 | $extraField->save([ |
||
| 83 | 'field_type' => ExtraField::FIELD_TYPE_SELECT_MULTIPLE, |
||
| 84 | 'variable' => self::EXTRAFIELD_ADVACED_VARIABLE, |
||
| 85 | 'display_text' => 'advancedCourseList', |
||
| 86 | 'default_value' => 1, |
||
| 87 | 'field_order' => 0, |
||
| 88 | 'visible_to_self' => 1, |
||
| 89 | 'visible_to_others' => 0, |
||
| 90 | 'changeable' => 1, |
||
| 91 | 'filter' => 0, |
||
| 92 | ]); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Set default_value to 0. |
||
| 98 | */ |
||
| 99 | public function uninstall() |
||
| 100 | { |
||
| 101 | } |
||
| 102 | |||
| 103 | public function get_name(): string |
||
| 104 | { |
||
| 105 | return 'remedial_course'; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getRemedialCourseList( |
||
| 109 | Exercise $objExercise, |
||
| 110 | int $userId = 0, |
||
| 111 | int $sessionId = 0, |
||
| 112 | bool $review = false |
||
| 113 | ): ?string { |
||
| 114 | if ('true' !== $this->get(self::SETTING_ENABLED)) { |
||
| 115 | return null; |
||
| 116 | } |
||
| 117 | |||
| 118 | $field = new ExtraField('exercise'); |
||
| 119 | $remedialField = $field->get_handler_field_info_by_field_variable(self::EXTRAFIELD_REMEDIAL_VARIABLE); |
||
| 120 | |||
| 121 | if (empty($remedialField)) { |
||
| 122 | return null; |
||
| 123 | } |
||
| 124 | |||
| 125 | $extraFieldValue = new ExtraFieldValue('exercise'); |
||
| 126 | $remedialExcerciseField = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 127 | $objExercise->iId, |
||
| 128 | self::EXTRAFIELD_REMEDIAL_VARIABLE |
||
| 129 | ); |
||
| 130 | $remedialCourseIds = explode(';', $remedialExcerciseField['value']); |
||
| 131 | |||
| 132 | if (empty($remedialExcerciseField['value']) || count($remedialCourseIds) == 0) { |
||
| 133 | return null; |
||
| 134 | } |
||
| 135 | |||
| 136 | $questionExcluded = [ |
||
| 137 | FREE_ANSWER, |
||
| 138 | ORAL_EXPRESSION, |
||
| 139 | ANNOTATION, |
||
| 140 | ]; |
||
| 141 | |||
| 142 | $userId = empty($userId) ? api_get_user_id() : $userId; |
||
| 143 | |||
| 144 | $exerciseStatInfo = Event::getExerciseResultsByUser( |
||
| 145 | $userId, |
||
| 146 | $objExercise->iId, |
||
| 147 | $objExercise->course_id, |
||
| 148 | $sessionId |
||
| 149 | ); |
||
| 150 | $bestAttempt = Event::get_best_attempt_exercise_results_per_user( |
||
| 151 | $userId, |
||
| 152 | $objExercise->iId, |
||
| 153 | $objExercise->course_id, |
||
| 154 | $sessionId |
||
| 155 | ); |
||
| 156 | |||
| 157 | foreach ($exerciseStatInfo as $attempt) { |
||
| 158 | if (!isset($bestAttempt['exe_result']) || $attempt['exe_result'] >= $bestAttempt['exe_result']) { |
||
| 159 | $bestAttempt = $attempt; |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!isset($attempt['question_list'])) { |
||
| 163 | continue; |
||
| 164 | } |
||
| 165 | |||
| 166 | foreach ($attempt['question_list'] as $questionId => $answer) { |
||
| 167 | $question = Question::read($questionId, api_get_course_info_by_id($attempt['c_id'])); |
||
| 168 | $questionOpen = in_array($question->type, $questionExcluded) && !$review; |
||
| 169 | |||
| 170 | if (!$questionOpen) { |
||
| 171 | continue; |
||
| 172 | } |
||
| 173 | |||
| 174 | $score = $attempt['exe_result']; |
||
| 175 | $comments = Event::get_comments($objExercise->iId, $questionId); |
||
| 176 | |||
| 177 | if (empty($comments) || $score == 0) { |
||
| 178 | return null; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | if (empty($bestAttempt)) { |
||
| 184 | return null; |
||
| 185 | } |
||
| 186 | |||
| 187 | $bestAttempt['exe_result'] = (int) $bestAttempt['exe_result']; |
||
| 188 | |||
| 189 | $isPassedPercentage = ExerciseLib::isPassPercentageAttemptPassed( |
||
| 190 | $objExercise, |
||
| 191 | $bestAttempt['exe_result'], |
||
| 192 | $bestAttempt['exe_weighting'] |
||
| 193 | ); |
||
| 194 | |||
| 195 | if ($isPassedPercentage) { |
||
| 196 | return null; |
||
| 197 | } |
||
| 198 | |||
| 199 | $hasAttempts = count($exerciseStatInfo) < $objExercise->selectAttempts(); |
||
| 200 | |||
| 201 | $doSubscriptionToRemedial = !$hasAttempts || $objExercise->isBlockedByPercentage($bestAttempt); |
||
| 202 | |||
| 203 | if (!$doSubscriptionToRemedial) { |
||
| 204 | return null; |
||
| 205 | } |
||
| 206 | |||
| 207 | $courses = []; |
||
| 208 | $isInASession = !empty($sessionId); |
||
| 209 | |||
| 210 | foreach ($remedialCourseIds as $courseId) { |
||
| 211 | $courseData = api_get_course_info_by_id($courseId); |
||
| 212 | |||
| 213 | if (empty($courseData)) { |
||
| 214 | continue; |
||
| 215 | } |
||
| 216 | |||
| 217 | if ($isInASession) { |
||
| 218 | $courseExistsInSession = SessionManager::sessionHasCourse($sessionId, $courseData['code']); |
||
| 219 | |||
| 220 | if ($courseExistsInSession) { |
||
| 221 | SessionManager::subscribe_users_to_session_course([$userId], $sessionId, $courseData['code']); |
||
| 222 | $courses[] = $courseData['title']; |
||
| 223 | } |
||
| 224 | } else { |
||
| 225 | $isSubscribed = CourseManager::is_user_subscribed_in_course($userId, $courseData['code']); |
||
| 226 | |||
| 227 | if (!$isSubscribed) { |
||
| 228 | CourseManager::subscribeUser($userId, $courseData['code']); |
||
| 229 | $courses[] = $courseData['title']; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | if (empty($courses)) { |
||
| 235 | return null; |
||
| 236 | } |
||
| 237 | |||
| 238 | return sprintf($this->get_lang('SubscriptionToXRemedialCourses'), implode(' - ', $courses)); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function getAdvacedCourseList(Exercise $objExercise, int $userId = 0, int $sessionId = 0): ?string |
||
| 365 | ); |
||
| 366 | } |
||
| 367 | } |
||
| 368 |