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