Total Complexity | 51 |
Total Lines | 432 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CatForm 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 CatForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class CatForm extends FormValidator |
||
11 | { |
||
12 | public const TYPE_ADD = 1; |
||
13 | public const TYPE_EDIT = 2; |
||
14 | public const TYPE_MOVE = 3; |
||
15 | public const TYPE_SELECT_COURSE = 4; |
||
16 | |||
17 | /** @var Category */ |
||
18 | private $category_object; |
||
19 | |||
20 | /** |
||
21 | * CatForm constructor. |
||
22 | * Builds a form containing form items based on a given parameter. |
||
23 | * |
||
24 | * @param string $form_type 1=add, 2=edit,3=move,4=browse |
||
25 | * @param string $category_object |
||
26 | * @param string $form_name |
||
27 | * @param string $method |
||
28 | * @param null $action |
||
|
|||
29 | */ |
||
30 | public function __construct( |
||
31 | $form_type, |
||
32 | $category_object, |
||
33 | $form_name, |
||
34 | $method = 'post', |
||
35 | $action = null |
||
36 | ) { |
||
37 | parent::__construct($form_name, $method, $action); |
||
38 | $this->form_type = $form_type; |
||
39 | if (isset($category_object)) { |
||
40 | $this->category_object = $category_object; |
||
41 | } |
||
42 | |||
43 | switch ($this->form_type) { |
||
44 | case self::TYPE_EDIT: |
||
45 | $this->build_editing_form(); |
||
46 | break; |
||
47 | case self::TYPE_ADD: |
||
48 | $this->build_add_form(); |
||
49 | break; |
||
50 | case self::TYPE_MOVE: |
||
51 | $this->build_move_form(); |
||
52 | break; |
||
53 | case self::TYPE_SELECT_COURSE: |
||
54 | $this->build_select_course_form(); |
||
55 | break; |
||
56 | } |
||
57 | |||
58 | $this->setDefaults(); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * This function will build a move form that will allow the user to move a category to |
||
63 | * a another. |
||
64 | */ |
||
65 | protected function build_move_form() |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * This function builds an 'add category form, if parent id is 0, it will only |
||
94 | * show courses. |
||
95 | */ |
||
96 | protected function build_add_form() |
||
97 | { |
||
98 | // check if we are a root category |
||
99 | // if so, you can only choose between courses |
||
100 | if ('0' == $this->category_object->get_parent_id()) { |
||
101 | $this->setDefaults( |
||
102 | [ |
||
103 | 'select_course' => $this->category_object->get_course_code(), |
||
104 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
105 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
106 | ] |
||
107 | ); |
||
108 | } else { |
||
109 | $this->setDefaults( |
||
110 | [ |
||
111 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
112 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
113 | ] |
||
114 | ); |
||
115 | $this->addElement( |
||
116 | 'hidden', |
||
117 | 'course_code', |
||
118 | $this->category_object->get_course_code() |
||
119 | ); |
||
120 | $this->addElement( |
||
121 | 'hidden', |
||
122 | 'course_id', |
||
123 | $this->category_object->getCourseId() |
||
124 | ); |
||
125 | } |
||
126 | $this->build_basic_form(); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Builds an form to edit a category. |
||
131 | */ |
||
132 | protected function build_editing_form() |
||
133 | { |
||
134 | $skills = $this->category_object->getSkillsForSelect(); |
||
135 | |||
136 | $course_code = api_get_course_id(); |
||
137 | $session_id = api_get_session_id(); |
||
138 | |||
139 | $test_cats = Category::load( |
||
140 | null, |
||
141 | null, |
||
142 | api_get_course_int_id(), |
||
143 | null, |
||
144 | null, |
||
145 | $session_id, |
||
146 | null |
||
147 | ); |
||
148 | |||
149 | $links = null; |
||
150 | if (isset($test_cats[0])) { |
||
151 | $links = $test_cats[0]->get_links(); |
||
152 | } |
||
153 | $grade_model_id = $this->category_object->get_grade_model_id(); |
||
154 | |||
155 | if (empty($links)) { |
||
156 | $grade_model_id = 0; |
||
157 | } |
||
158 | |||
159 | $category_name = $this->category_object->get_name(); |
||
160 | |||
161 | // The main course category: |
||
162 | if (!empty($this->category_object) && 0 == $this->category_object->get_parent_id()) { |
||
163 | if (empty($category_name)) { |
||
164 | $category_name = $course_code; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | $this->setDefaults( |
||
169 | [ |
||
170 | 'name' => $category_name, |
||
171 | 'description' => $this->category_object->get_description(), |
||
172 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
173 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
174 | 'grade_model_id' => $grade_model_id, |
||
175 | 'skills' => $skills, |
||
176 | 'weight' => $this->category_object->get_weight(), |
||
177 | 'visible' => $this->category_object->is_visible(), |
||
178 | 'certif_min_score' => $this->category_object->getCertificateMinScore(), |
||
179 | 'generate_certificates' => $this->category_object->getGenerateCertificates(), |
||
180 | 'is_requirement' => $this->category_object->getIsRequirement(), |
||
181 | ] |
||
182 | ); |
||
183 | |||
184 | $this->addElement('hidden', 'hid_id', $this->category_object->get_id()); |
||
185 | $this->addElement( |
||
186 | 'hidden', |
||
187 | 'course_code', |
||
188 | $this->category_object->get_course_code() |
||
189 | ); |
||
190 | $this->addElement( |
||
191 | 'hidden', |
||
192 | 'course_id', |
||
193 | $this->category_object->getCourseId() |
||
194 | ); |
||
195 | $this->build_basic_form(); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * This function builds an 'select course' form in the add category process, |
||
200 | * if parent id is 0, it will only show courses. |
||
201 | */ |
||
202 | protected function build_select_course_form() |
||
203 | { |
||
204 | $select = $this->addSelect( |
||
205 | 'select_course', |
||
206 | [get_lang('Pick a course'), 'test'], |
||
207 | null |
||
208 | ); |
||
209 | $courses = Category::get_all_courses(api_get_user_id()); |
||
210 | //only return courses that are not yet created by the teacher |
||
211 | foreach ($courses as $row) { |
||
212 | $select->addOption($row[1], $row[0]); |
||
213 | } |
||
214 | $this->setDefaults([ |
||
215 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
216 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
217 | ]); |
||
218 | $this->addElement('hidden', 'hid_user_id'); |
||
219 | $this->addElement('hidden', 'hid_parent_id'); |
||
220 | $this->addButtonSave(get_lang('Validate')); |
||
221 | } |
||
222 | |||
223 | private function build_basic_form() |
||
442 | ] |
||
443 | ); |
||
444 | } |
||
445 | } |
||
446 |