| Conditions | 25 |
| Paths | 8101 |
| Total Lines | 197 |
| Code Lines | 130 |
| 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 |
||
| 134 | public function edit($id, $description_type) |
||
| 135 | { |
||
| 136 | $course_description = new CourseDescription(); |
||
| 137 | $session_id = api_get_session_id(); |
||
| 138 | $course_description->set_session_id($session_id); |
||
| 139 | $data = []; |
||
| 140 | $affected_rows = null; |
||
| 141 | if ('POST' === strtoupper($_SERVER['REQUEST_METHOD'])) { |
||
| 142 | if (!empty($_POST['title']) && !empty($_POST['contentDescription'])) { |
||
| 143 | $title = $_POST['title']; |
||
| 144 | $content = $_POST['contentDescription']; |
||
| 145 | $description_type = $_POST['description_type']; |
||
| 146 | $id = $_POST['id']; |
||
| 147 | |||
| 148 | if (empty($id)) { |
||
| 149 | $description = $course_description->get_data_by_description_type($description_type); |
||
| 150 | if (count($description) > 0) { |
||
| 151 | $id = $description['iid']; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | $progress = isset($_POST['progress']) ? $_POST['progress'] : 0; |
||
| 156 | |||
| 157 | $searchEnabled = api_get_setting('search.search_enabled'); |
||
| 158 | $enableSearch = $searchEnabled === 'true' && !empty($_POST['enable_search']); |
||
| 159 | |||
| 160 | $repo = Container::getCourseDescriptionRepository(); |
||
| 161 | |||
| 162 | /** @var CCourseDescription|null $courseDescription */ |
||
| 163 | $courseDescription = $repo->find($id); |
||
| 164 | |||
| 165 | if ($courseDescription) { |
||
| 166 | if ($searchEnabled === 'true') { |
||
| 167 | // If checkbox is unchecked => skip search index for this request. |
||
| 168 | $courseDescription->setSkipSearchIndex(!$enableSearch); |
||
| 169 | } |
||
| 170 | |||
| 171 | $courseDescription |
||
| 172 | ->setTitle($title) |
||
| 173 | ->setProgress((int) $progress) |
||
| 174 | ->setContent($content) |
||
| 175 | ; |
||
| 176 | |||
| 177 | $repo->update($courseDescription); |
||
| 178 | } else { |
||
| 179 | $course_description->set_description_type($description_type); |
||
| 180 | $course_description->set_title($title); |
||
| 181 | $course_description->set_progress($progress); |
||
| 182 | $course_description->set_content($content); |
||
| 183 | |||
| 184 | // Pass the flag down to insert (see next section). |
||
| 185 | $course_description->insert($enableSearch); |
||
| 186 | } |
||
| 187 | |||
| 188 | Display::addFlash( |
||
| 189 | Display::return_message( |
||
| 190 | get_lang('The description has been updated') |
||
| 191 | ) |
||
| 192 | ); |
||
| 193 | |||
| 194 | $url = api_get_path(WEB_CODE_PATH).'course_description/index.php?'.api_get_cidreq(); |
||
| 195 | api_location($url); |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | $default_description_titles = $course_description->get_default_description_title(); |
||
| 199 | $default_description_title_editable = $course_description->get_default_description_title_editable(); |
||
| 200 | $default_description_icon = $course_description->get_default_description_icon(); |
||
| 201 | $question = $course_description->get_default_question(); |
||
| 202 | $information = $course_description->get_default_information(); |
||
| 203 | $description_type = $description_type; |
||
| 204 | if (empty($id)) { |
||
| 205 | // If the ID was not provided, find the first matching description item given the item type |
||
| 206 | $description = $course_description->get_data_by_description_type($description_type); |
||
| 207 | if (count($description) > 0) { |
||
| 208 | $id = $description['iid']; |
||
| 209 | } |
||
| 210 | // If no corresponding description is found, edit a new one |
||
| 211 | } |
||
| 212 | if (!empty($id)) { |
||
| 213 | if (isset($_GET['id_session'])) { |
||
| 214 | $session_id = intval($_GET['id_session']); |
||
| 215 | } |
||
| 216 | $course_description_data = $course_description->get_data_by_id( |
||
| 217 | $id, |
||
| 218 | null, |
||
| 219 | $session_id |
||
| 220 | ); |
||
| 221 | $description_type = $course_description_data['description_type']; |
||
| 222 | $description_title = $course_description_data['description_title']; |
||
| 223 | $description_content = $course_description_data['description_content']; |
||
| 224 | $progress = $course_description_data['progress']; |
||
| 225 | $descriptions = $course_description->get_data_by_description_type( |
||
| 226 | $description_type, |
||
| 227 | null, |
||
| 228 | $session_id |
||
| 229 | ); |
||
| 230 | } |
||
| 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 ('true' === api_get_setting('editor.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 | |||
| 295 | if ('true' === api_get_setting('search.search_enabled')) { |
||
| 296 | $form->addCheckBox( |
||
| 297 | 'enable_search', |
||
| 298 | null, |
||
| 299 | get_lang('Index this description in global search') |
||
| 300 | ); |
||
| 301 | |||
| 302 | // Default: checked |
||
| 303 | $default['enable_search'] = 1; |
||
| 304 | } |
||
| 305 | |||
| 306 | $form->addButtonCreate(get_lang('Save')); |
||
| 307 | |||
| 308 | $actions = self::getToolbar(); |
||
| 309 | // Set some default values |
||
| 310 | if (!empty($description_title)) { |
||
| 311 | $default['title'] = Security::remove_XSS($description_title); |
||
| 312 | } |
||
| 313 | if (!empty($description_content)) { |
||
| 314 | $default['contentDescription'] = Security::remove_XSS($description_content, COURSEMANAGERLOWSECURITY); |
||
| 315 | } |
||
| 316 | $default['description_type'] = $description_type; |
||
| 317 | |||
| 318 | $form->setDefaults($default); |
||
| 319 | |||
| 320 | if (isset($question[$description_type])) { |
||
| 321 | $message = '<strong>'.get_lang('Help').'</strong><br />'; |
||
| 322 | $message .= $question[$description_type]; |
||
| 323 | Display::addFlash(Display::return_message($message, 'normal', false)); |
||
| 324 | } |
||
| 325 | $tpl = new Template(get_lang('Description')); |
||
| 326 | //$tpl->assign('is_allowed_to_edit', $is_allowed_to_edit); |
||
| 327 | $tpl->assign('actions', $actions); |
||
| 328 | $tpl->assign('session_id', $session_id); |
||
| 329 | $tpl->assign('content', $form->returnForm()); |
||
| 330 | $tpl->display_one_col_template(); |
||
| 331 | } |
||
| 447 |