| Total Complexity | 42 |
| Total Lines | 256 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Positioning 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 Positioning, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Positioning extends Plugin |
||
| 6 | { |
||
| 7 | public $isCoursePlugin = true; |
||
| 8 | public $table; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Class constructor. |
||
| 12 | */ |
||
| 13 | protected function __construct() |
||
| 14 | { |
||
| 15 | parent::__construct( |
||
| 16 | '1.0', |
||
| 17 | 'Julio Montoya', |
||
| 18 | [ |
||
| 19 | 'tool_enable' => 'boolean', |
||
| 20 | 'block_course_if_initial_exercise_not_attempted' => 'boolean', |
||
| 21 | 'average_percentage_to_unlock_final_exercise' => 'text', |
||
| 22 | ] |
||
| 23 | ); |
||
| 24 | |||
| 25 | $this->table = Database::get_main_table('plugin_positioning_exercise'); |
||
| 26 | } |
||
| 27 | |||
| 28 | public static function create() |
||
| 33 | } |
||
| 34 | |||
| 35 | public function install() |
||
| 36 | { |
||
| 37 | $table = $this->table; |
||
| 38 | |||
| 39 | $sql = 'CREATE TABLE IF NOT EXISTS '.$table.' ( |
||
| 40 | id INT unsigned NOT NULL auto_increment PRIMARY KEY, |
||
| 41 | exercise_id INT unsigned NOT NULL, |
||
| 42 | c_id INT unsigned NOT NULL, |
||
| 43 | session_id INT unsigned DEFAULT NULL, |
||
| 44 | is_initial TINYINT(1) NOT NULL, |
||
| 45 | is_final TINYINT(1) NOT NULL |
||
| 46 | )'; |
||
| 47 | Database::query($sql); |
||
| 48 | |||
| 49 | // Installing course settings |
||
| 50 | $this->install_course_fields_in_all_courses(true); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function uninstall() |
||
| 57 | } |
||
| 58 | |||
| 59 | public function isInitialExercise($exerciseId, $courseId, $sessionId) |
||
| 60 | { |
||
| 61 | $data = $this->getPositionData($exerciseId, $courseId, $sessionId); |
||
| 62 | if ($data && isset($data['is_initial']) && 1 === (int) $data['is_initial']) { |
||
| 63 | return true; |
||
| 64 | } |
||
| 65 | |||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function getPositionData($exerciseId, $courseId, $sessionId) |
||
| 88 | } |
||
| 89 | |||
| 90 | public function isFinalExercise($exerciseId, $courseId, $sessionId) |
||
| 91 | { |
||
| 92 | $data = $this->getPositionData($exerciseId, $courseId, $sessionId); |
||
| 93 | if ($data && isset($data['is_final']) && 1 === (int) $data['is_final']) { |
||
| 94 | return true; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | public function setInitialExercise($exerciseId, $courseId, $sessionId) |
||
| 101 | } |
||
| 102 | |||
| 103 | public function setFinalExercise($exerciseId, $courseId, $sessionId) |
||
| 104 | { |
||
| 105 | $this->setOption('is_final', $exerciseId, $courseId, $sessionId); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function blockFinalExercise($userId, $exerciseId, $courseId, $sessionId) |
||
| 156 | } |
||
| 157 | |||
| 158 | public function shouldBlockUser(int $userId, int $courseId, ?int $sessionId): bool |
||
| 159 | { |
||
| 160 | if ($this->get('block_course_if_initial_exercise_not_attempted') !== 'true') { |
||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | $initialData = $this->getInitialExercise($courseId, $sessionId); |
||
| 165 | |||
| 166 | if (empty($initialData['exercise_id'])) { |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | |||
| 170 | $results = \Event::getExerciseResultsByUser( |
||
| 171 | $userId, |
||
| 172 | (int) $initialData['exercise_id'], |
||
| 173 | $courseId, |
||
| 174 | $sessionId |
||
| 175 | ); |
||
| 176 | |||
| 177 | return empty($results); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getInitialExercise($courseId, $sessionId) |
||
| 183 | } |
||
| 184 | |||
| 185 | public function getFinalExercise($courseId, $sessionId) |
||
| 186 | { |
||
| 187 | return $this->getCourseExercise($courseId, $sessionId, false, true); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function get_name() |
||
| 193 | } |
||
| 194 | |||
| 195 | private function setOption($field, $exerciseId, $courseId, $sessionId) |
||
| 196 | { |
||
| 197 | if (!in_array($field, ['is_initial', 'is_final'], true)) { |
||
| 198 | return false; |
||
| 199 | } |
||
| 200 | |||
| 201 | $data = $this->getPositionData($exerciseId, $courseId, $sessionId); |
||
| 202 | $disableField = 'is_initial' === $field ? 'is_final' : 'is_initial'; |
||
| 203 | if ($data && isset($data['id'])) { |
||
| 204 | $id = $data['id']; |
||
| 205 | $sql = "UPDATE $this->table SET |
||
| 206 | $field = 1, |
||
| 207 | $disableField = 0 |
||
| 208 | WHERE id = $id"; |
||
| 209 | Database::query($sql); |
||
| 210 | |||
| 211 | $sql = "DELETE FROM $this->table |
||
| 212 | WHERE $field = 1 AND c_id = $courseId AND session_id = $sessionId AND id <> $id"; |
||
| 213 | Database::query($sql); |
||
| 214 | } else { |
||
| 215 | $params = [ |
||
| 216 | 'exercise_id' => $exerciseId, |
||
| 217 | 'c_id' => $courseId, |
||
| 218 | 'session_id' => $sessionId, |
||
| 219 | $field => 1, |
||
| 220 | $disableField => 0, |
||
| 221 | ]; |
||
| 222 | $id = Database::insert($this->table, $params); |
||
| 223 | |||
| 224 | $sql = "DELETE FROM $this->table |
||
| 225 | WHERE $field = 1 AND c_id = $courseId AND session_id = $sessionId AND id <> $id"; |
||
| 226 | Database::query($sql); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | private function getCourseExercise($courseId, $sessionId, $isInitial, $isFinal) |
||
| 261 | } |
||
| 262 | } |
||
| 263 |