| Conditions | 22 |
| Paths | 4045 |
| Total Lines | 181 |
| Code Lines | 122 |
| 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 |
||
| 138 | public function edit($id, $description_type) |
||
| 139 | { |
||
| 140 | $course_description = new CourseDescription(); |
||
| 141 | $session_id = api_get_session_id(); |
||
| 142 | $course_description->set_session_id($session_id); |
||
| 143 | $data = []; |
||
| 144 | $data['id'] = $id; |
||
| 145 | $affected_rows = null; |
||
| 146 | if ('POST' === strtoupper($_SERVER['REQUEST_METHOD'])) { |
||
| 147 | if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) { |
||
| 148 | $title = $_POST['title']; |
||
| 149 | $content = $_POST['contentDescription']; |
||
| 150 | $description_type = $_POST['description_type']; |
||
| 151 | $id = $_POST['id']; |
||
| 152 | if (empty($id)) { |
||
| 153 | // If the ID was not provided, find the first matching description item given the item type |
||
| 154 | $description = $course_description->get_data_by_description_type( |
||
| 155 | $description_type |
||
| 156 | ); |
||
| 157 | if (count($description) > 0) { |
||
| 158 | $id = $description['iid']; |
||
| 159 | } |
||
| 160 | // If no corresponding description is found, edit a new one |
||
| 161 | } |
||
| 162 | $progress = isset($_POST['progress']) ? $_POST['progress'] : 0; |
||
| 163 | $repo = Container::getCourseDescriptionRepository(); |
||
| 164 | |||
| 165 | /** @var CCourseDescription $courseDescription */ |
||
| 166 | $courseDescription = $repo->find($id); |
||
| 167 | if ($courseDescription) { |
||
| 168 | $courseDescription |
||
| 169 | ->setTitle($title) |
||
| 170 | ->setProgress($progress) |
||
| 171 | ->setContent($content) |
||
| 172 | ; |
||
| 173 | $repo->update($courseDescription); |
||
| 174 | } else { |
||
| 175 | $course_description->set_description_type($description_type); |
||
| 176 | $course_description->set_title($title); |
||
| 177 | $course_description->set_progress($progress); |
||
| 178 | $course_description->set_content($content); |
||
| 179 | $course_description->insert(api_get_course_int_id()); |
||
| 180 | } |
||
| 181 | |||
| 182 | Display::addFlash( |
||
| 183 | Display::return_message( |
||
| 184 | get_lang('The description has been updated') |
||
| 185 | ) |
||
| 186 | ); |
||
| 187 | |||
| 188 | $url = api_get_path(WEB_CODE_PATH).'course_description/index.php?'.api_get_cidreq(); |
||
| 189 | api_location($url); |
||
| 190 | } |
||
| 191 | } else { |
||
| 192 | $default_description_titles = $course_description->get_default_description_title(); |
||
| 193 | $default_description_title_editable = $course_description->get_default_description_title_editable(); |
||
| 194 | $default_description_icon = $course_description->get_default_description_icon(); |
||
| 195 | $question = $course_description->get_default_question(); |
||
| 196 | $information = $course_description->get_default_information(); |
||
| 197 | $description_type = $description_type; |
||
| 198 | if (empty($id)) { |
||
| 199 | // If the ID was not provided, find the first matching description item given the item type |
||
| 200 | $description = $course_description->get_data_by_description_type($description_type); |
||
| 201 | if (count($description) > 0) { |
||
| 202 | $id = $description['id']; |
||
| 203 | } |
||
| 204 | // If no corresponding description is found, edit a new one |
||
| 205 | } |
||
| 206 | if (!empty($id)) { |
||
| 207 | if (isset($_GET['id_session'])) { |
||
| 208 | $session_id = intval($_GET['id_session']); |
||
| 209 | } |
||
| 210 | $course_description_data = $course_description->get_data_by_id( |
||
| 211 | $id, |
||
| 212 | null, |
||
| 213 | $session_id |
||
| 214 | ); |
||
| 215 | $description_type = $course_description_data['description_type']; |
||
| 216 | $description_title = $course_description_data['description_title']; |
||
| 217 | $description_content = $course_description_data['description_content']; |
||
| 218 | $progress = $course_description_data['progress']; |
||
| 219 | $descriptions = $course_description->get_data_by_description_type( |
||
| 220 | $description_type, |
||
| 221 | null, |
||
| 222 | $session_id |
||
| 223 | ); |
||
| 224 | } |
||
| 225 | |||
| 226 | // render to the view |
||
| 227 | /*$this->view->set_data($data); |
||
| 228 | $this->view->set_layout('layout'); |
||
| 229 | $this->view->set_template('edit'); |
||
| 230 | $this->view->render();*/ |
||
| 231 | |||
| 232 | if (empty($id)) { |
||
| 233 | $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : ''; |
||
| 234 | if (empty($id)) { |
||
| 235 | // If the ID was not provided, find the first matching description item given the item type |
||
| 236 | $course_description = new CourseDescription(); |
||
| 237 | $description = $course_description->get_data_by_description_type($description_type); |
||
| 238 | if (count($description) > 0) { |
||
| 239 | $id = $description['id']; |
||
| 240 | } |
||
| 241 | // If no corresponding description is found, edit a new one |
||
| 242 | unset($course_description); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | $original_id = $id; |
||
| 246 | // display categories |
||
| 247 | $categories = []; |
||
| 248 | foreach ($default_description_titles as $id => $title) { |
||
| 249 | $categories[$id] = $title; |
||
| 250 | } |
||
| 251 | $categories[ADD_BLOCK] = get_lang('Other'); |
||
| 252 | |||
| 253 | // default header title form |
||
| 254 | $description_type = intval($description_type); |
||
| 255 | $header = $default_description_titles[$description_type]; |
||
| 256 | if ($description_type >= ADD_BLOCK) { |
||
| 257 | $header = $default_description_titles[ADD_BLOCK]; |
||
| 258 | } |
||
| 259 | |||
| 260 | // display form |
||
| 261 | $form = new FormValidator( |
||
| 262 | 'course_description', |
||
| 263 | 'POST', |
||
| 264 | 'index.php?action=edit&id='.$original_id.'&description_type='.$description_type.'&'.api_get_cidreq() |
||
| 265 | ); |
||
| 266 | $form->addElement('header', $header); |
||
| 267 | $form->addElement('hidden', 'id', $original_id); |
||
| 268 | $form->addElement('hidden', 'description_type', $description_type); |
||
| 269 | //$form->addElement('hidden', 'sec_token', $token); |
||
| 270 | |||
| 271 | if (api_get_configuration_value('save_titles_as_html')) { |
||
| 272 | $form->addHtmlEditor( |
||
| 273 | 'title', |
||
| 274 | get_lang('Title'), |
||
| 275 | true, |
||
| 276 | false, |
||
| 277 | ['ToolbarSet' => 'TitleAsHtml'] |
||
| 278 | ); |
||
| 279 | } else { |
||
| 280 | $form->addText('title', get_lang('Title')); |
||
| 281 | $form->applyFilter('title', 'html_filter'); |
||
| 282 | } |
||
| 283 | $form->addHtmlEditor( |
||
| 284 | 'contentDescription', |
||
| 285 | get_lang('Content'), |
||
| 286 | true, |
||
| 287 | false, |
||
| 288 | [ |
||
| 289 | 'ToolbarSet' => 'Basic', |
||
| 290 | 'Width' => '100%', |
||
| 291 | 'Height' => '200', |
||
| 292 | ] |
||
| 293 | ); |
||
| 294 | $form->addButtonCreate(get_lang('Save')); |
||
| 295 | |||
| 296 | $actions = self::getToolbar(); |
||
| 297 | // Set some default values |
||
| 298 | if (!empty($description_title)) { |
||
| 299 | $default['title'] = Security::remove_XSS($description_title); |
||
| 300 | } |
||
| 301 | if (!empty($description_content)) { |
||
| 302 | $default['contentDescription'] = Security::remove_XSS($description_content, COURSEMANAGERLOWSECURITY); |
||
| 303 | } |
||
| 304 | $default['description_type'] = $description_type; |
||
| 305 | |||
| 306 | $form->setDefaults($default); |
||
| 307 | |||
| 308 | if (isset($question[$description_type])) { |
||
| 309 | $message = '<strong>'.get_lang('Help').'</strong><br />'; |
||
| 310 | $message .= $question[$description_type]; |
||
| 311 | Display::addFlash(Display::return_message($message, 'normal', false)); |
||
| 312 | } |
||
| 313 | $tpl = new Template(get_lang('Description')); |
||
| 314 | //$tpl->assign('is_allowed_to_edit', $is_allowed_to_edit); |
||
| 315 | $tpl->assign('actions', $actions); |
||
| 316 | $tpl->assign('session_id', $session_id); |
||
| 317 | $tpl->assign('content', $form->returnForm()); |
||
| 318 | $tpl->display_one_col_template(); |
||
| 319 | |||
| 420 |