| Conditions | 12 |
| Paths | 17 |
| Total Lines | 117 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 129 | public function edit($id, $description_type) |
||
| 130 | { |
||
| 131 | $course_description = new CourseDescription(); |
||
| 132 | $session_id = api_get_session_id(); |
||
| 133 | $course_description->set_session_id($session_id); |
||
| 134 | $data = []; |
||
| 135 | $data['id'] = $id; |
||
| 136 | $affected_rows = null; |
||
| 137 | if ("POST" == strtoupper($_SERVER['REQUEST_METHOD'])) { |
||
| 138 | if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) { |
||
| 139 | if (1) { |
||
| 140 | $title = $_POST['title']; |
||
| 141 | $content = $_POST['contentDescription']; |
||
| 142 | $description_type = $_POST['description_type']; |
||
| 143 | $id = $_POST['id']; |
||
| 144 | if (empty($id)) { |
||
| 145 | // If the ID was not provided, find the first matching description item given the item type |
||
| 146 | $description = $course_description->get_data_by_description_type( |
||
| 147 | $description_type |
||
| 148 | ); |
||
| 149 | if (count($description) > 0) { |
||
| 150 | $id = $description['iid']; |
||
| 151 | } |
||
| 152 | // If no corresponding description is found, edit a new one |
||
| 153 | } |
||
| 154 | $progress = isset($_POST['progress']) ? $_POST['progress'] : ''; |
||
| 155 | $repo = Container::getCourseDescriptionRepository(); |
||
| 156 | |||
| 157 | /** @var CCourseDescription $courseDescription */ |
||
| 158 | $courseDescription = $repo->find($id); |
||
| 159 | $courseDescription |
||
| 160 | ->setTitle($title) |
||
| 161 | ->setProgress($progress) |
||
| 162 | ->setContent($content) |
||
| 163 | ; |
||
| 164 | $repo->getEntityManager()->persist($courseDescription); |
||
| 165 | $repo->getEntityManager()->flush(); |
||
| 166 | |||
| 167 | /*$course_description->set_description_type($description_type); |
||
| 168 | $course_description->set_title($title); |
||
| 169 | $course_description->set_content($content); |
||
| 170 | $course_description->set_progress($progress); |
||
| 171 | $thematic_advance = $course_description->get_data_by_id($id); |
||
| 172 | |||
| 173 | if (!empty($thematic_advance)) { |
||
| 174 | $course_description->set_id($id); |
||
| 175 | $course_description->update(); |
||
| 176 | } else { |
||
| 177 | $course_description->insert(); |
||
| 178 | }*/ |
||
| 179 | |||
| 180 | Display::addFlash( |
||
| 181 | Display::return_message( |
||
| 182 | get_lang('The description has been updated') |
||
| 183 | ) |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | $this->listing(false); |
||
| 187 | } else { |
||
| 188 | $data['error'] = 1; |
||
| 189 | $data['default_description_titles'] = $course_description->get_default_description_title(); |
||
| 190 | $data['default_description_title_editable'] = $course_description->get_default_description_title_editable(); |
||
| 191 | $data['default_description_icon'] = $course_description->get_default_description_icon(); |
||
| 192 | $data['question'] = $course_description->get_default_question(); |
||
| 193 | $data['information'] = $course_description->get_default_information(); |
||
| 194 | $data['description_title'] = $_POST['title']; |
||
| 195 | $data['description_content'] = $_POST['contentDescription']; |
||
| 196 | $data['description_type'] = $_POST['description_type']; |
||
| 197 | $data['progress'] = $_POST['progress']; |
||
| 198 | $data['descriptions'] = $course_description->get_data_by_id($_POST['id']); |
||
| 199 | // render to the view |
||
| 200 | $this->view->set_data($data); |
||
| 201 | $this->view->set_layout('layout'); |
||
| 202 | $this->view->set_template('edit'); |
||
| 203 | $this->view->render(); |
||
| 204 | } |
||
| 205 | } else { |
||
| 206 | $data['default_description_titles'] = $course_description->get_default_description_title(); |
||
| 207 | $data['default_description_title_editable'] = $course_description->get_default_description_title_editable(); |
||
| 208 | $data['default_description_icon'] = $course_description->get_default_description_icon(); |
||
| 209 | $data['question'] = $course_description->get_default_question(); |
||
| 210 | $data['information'] = $course_description->get_default_information(); |
||
| 211 | |||
| 212 | $data['description_type'] = $description_type; |
||
| 213 | if (empty($id)) { |
||
| 214 | // If the ID was not provided, find the first matching description item given the item type |
||
| 215 | $description = $course_description->get_data_by_description_type($description_type); |
||
| 216 | if (count($description) > 0) { |
||
| 217 | $id = $description['id']; |
||
| 218 | } |
||
| 219 | // If no corresponding description is found, edit a new one |
||
| 220 | } |
||
| 221 | if (!empty($id)) { |
||
| 222 | if (isset($_GET['id_session'])) { |
||
| 223 | $session_id = intval($_GET['id_session']); |
||
| 224 | } |
||
| 225 | $course_description_data = $course_description->get_data_by_id( |
||
| 226 | $id, |
||
| 227 | null, |
||
| 228 | $session_id |
||
| 229 | ); |
||
| 230 | $data['description_type'] = $course_description_data['description_type']; |
||
| 231 | $data['description_title'] = $course_description_data['description_title']; |
||
| 232 | $data['description_content'] = $course_description_data['description_content']; |
||
| 233 | $data['progress'] = $course_description_data['progress']; |
||
| 234 | $data['descriptions'] = $course_description->get_data_by_description_type( |
||
| 235 | $description_type, |
||
| 236 | null, |
||
| 237 | $session_id |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | // render to the view |
||
| 242 | $this->view->set_data($data); |
||
| 243 | $this->view->set_layout('layout'); |
||
| 244 | $this->view->set_template('edit'); |
||
| 245 | $this->view->render(); |
||
| 246 | } |
||
| 331 |