| Total Complexity | 49 |
| Total Lines | 416 |
| 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 |
||
| 11 | class CatForm extends FormValidator |
||
| 12 | { |
||
| 13 | const TYPE_ADD = 1; |
||
| 14 | const TYPE_EDIT = 2; |
||
| 15 | const TYPE_MOVE = 3; |
||
| 16 | const TYPE_SELECT_COURSE = 4; |
||
| 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 ($this->category_object->get_parent_id() == '0') { |
||
| 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 | } |
||
| 121 | $this->build_basic_form(); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Builds an form to edit a category. |
||
| 126 | */ |
||
| 127 | protected function build_editing_form() |
||
| 128 | { |
||
| 129 | $skills = $this->category_object->getSkillsForSelect(); |
||
| 130 | |||
| 131 | $course_code = api_get_course_id(); |
||
| 132 | $session_id = api_get_session_id(); |
||
| 133 | |||
| 134 | $test_cats = Category::load( |
||
| 135 | null, |
||
| 136 | null, |
||
| 137 | $course_code, |
||
| 138 | null, |
||
| 139 | null, |
||
| 140 | $session_id, |
||
| 141 | false |
||
| 142 | ); |
||
| 143 | |||
| 144 | $links = null; |
||
| 145 | if (isset($test_cats[0])) { |
||
| 146 | $links = $test_cats[0]->get_links(); |
||
| 147 | } |
||
| 148 | $grade_model_id = $this->category_object->get_grade_model_id(); |
||
| 149 | |||
| 150 | if (empty($links)) { |
||
| 151 | $grade_model_id = 0; |
||
| 152 | } |
||
| 153 | |||
| 154 | $category_name = $this->category_object->get_name(); |
||
| 155 | |||
| 156 | // The main course category: |
||
| 157 | if (isset($this->category_object) && $this->category_object->get_parent_id() == 0) { |
||
| 158 | if (empty($category_name)) { |
||
| 159 | $category_name = $course_code; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | $this->setDefaults( |
||
| 164 | [ |
||
| 165 | 'name' => $category_name, |
||
| 166 | 'description' => $this->category_object->get_description(), |
||
| 167 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
| 168 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
| 169 | 'grade_model_id' => $grade_model_id, |
||
| 170 | 'skills' => $skills, |
||
| 171 | 'weight' => $this->category_object->get_weight(), |
||
| 172 | 'visible' => $this->category_object->is_visible(), |
||
| 173 | 'certif_min_score' => $this->category_object->getCertificateMinScore(), |
||
| 174 | 'generate_certificates' => $this->category_object->getGenerateCertificates(), |
||
| 175 | 'is_requirement' => $this->category_object->getIsRequirement(), |
||
| 176 | ] |
||
| 177 | ); |
||
| 178 | |||
| 179 | $this->addElement('hidden', 'hid_id', $this->category_object->get_id()); |
||
| 180 | $this->addElement( |
||
| 181 | 'hidden', |
||
| 182 | 'course_code', |
||
| 183 | $this->category_object->get_course_code() |
||
| 184 | ); |
||
| 185 | $this->build_basic_form(); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * This function builds an 'select course' form in the add category process, |
||
| 190 | * if parent id is 0, it will only show courses. |
||
| 191 | */ |
||
| 192 | protected function build_select_course_form() |
||
| 193 | { |
||
| 194 | $select = $this->addElement( |
||
| 195 | 'select', |
||
| 196 | 'select_course', |
||
| 197 | [get_lang('PickACourse'), 'test'], |
||
| 198 | null |
||
| 199 | ); |
||
| 200 | $courses = Category::get_all_courses(api_get_user_id()); |
||
| 201 | //only return courses that are not yet created by the teacher |
||
| 202 | foreach ($courses as $row) { |
||
| 203 | $select->addoption($row[1], $row[0]); |
||
| 204 | } |
||
| 205 | $this->setDefaults([ |
||
| 206 | 'hid_user_id' => $this->category_object->get_user_id(), |
||
| 207 | 'hid_parent_id' => $this->category_object->get_parent_id(), |
||
| 208 | ]); |
||
| 209 | $this->addElement('hidden', 'hid_user_id'); |
||
| 210 | $this->addElement('hidden', 'hid_parent_id'); |
||
| 211 | $this->addElement('submit', null, get_lang('Ok')); |
||
| 212 | } |
||
| 213 | |||
| 214 | private function build_basic_form() |
||
| 427 | ] |
||
| 428 | ); |
||
| 429 | } |
||
| 430 | } |
||
| 431 |