| Conditions | 36 |
| Paths | > 20000 |
| Total Lines | 272 |
| Code Lines | 185 |
| 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 |
||
| 112 | function manageForm($default, $select_from_user_list = null, $sent_to = '', $tpl = null) |
||
| 113 | { |
||
| 114 | $group_id = isset($_REQUEST['group_id']) ? (int) $_REQUEST['group_id'] : null; |
||
| 115 | $message_id = isset($_GET['message_id']) ? (int) $_GET['message_id'] : null; |
||
| 116 | |||
| 117 | $onlyTeachers = false; |
||
| 118 | if (api_get_configuration_value('send_only_messages_to_teachers') && api_is_student()) { |
||
| 119 | $onlyTeachers = true; |
||
| 120 | } |
||
| 121 | |||
| 122 | if (isset($_SESSION['form_values'])) { |
||
| 123 | $default = $_SESSION['form_values']; |
||
| 124 | } |
||
| 125 | |||
| 126 | $form = new FormValidator( |
||
| 127 | 'compose_message', |
||
| 128 | null, |
||
| 129 | api_get_self(), |
||
| 130 | null, |
||
| 131 | ['enctype' => 'multipart/form-data'] |
||
| 132 | ); |
||
| 133 | |||
| 134 | if (empty($group_id)) { |
||
| 135 | if (isset($select_from_user_list)) { |
||
| 136 | $form->addText( |
||
| 137 | 'id_text_name', |
||
| 138 | get_lang('SendMessageTo'), |
||
| 139 | true, |
||
| 140 | [ |
||
| 141 | 'id' => 'id_text_name', |
||
| 142 | 'onkeyup' => 'send_request_and_search()', |
||
| 143 | 'autocomplete' => 'off', |
||
| 144 | ] |
||
| 145 | ); |
||
| 146 | $form->addRule('id_text_name', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 147 | $form->addElement( |
||
| 148 | 'html', |
||
| 149 | '<div id="id_div_search" style="padding:0px" class="message-select-box" > </div>' |
||
| 150 | ); |
||
| 151 | $form->addElement('hidden', 'user_list', 0, ['id' => 'user_list']); |
||
| 152 | } else { |
||
| 153 | if (!empty($sent_to)) { |
||
| 154 | $form->addLabel(get_lang('SendMessageTo'), $sent_to); |
||
| 155 | } |
||
| 156 | if (empty($default['users'])) { |
||
| 157 | if ($onlyTeachers) { |
||
| 158 | $courses = CourseManager::get_courses_list_by_user_id(api_get_user_id()); |
||
| 159 | $teachers = []; |
||
| 160 | foreach ($courses as $course) { |
||
| 161 | $courseTeachers = CourseManager::getTeachersFromCourse($course['real_id']); |
||
| 162 | if ($courseTeachers) { |
||
| 163 | foreach ($courseTeachers as $teacher) { |
||
| 164 | $teachers[$teacher['id']] = $teacher['fullname']; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | if (!empty($teachers)) { |
||
| 169 | asort($teachers); |
||
| 170 | } |
||
| 171 | $form->addSelect( |
||
| 172 | 'users', |
||
| 173 | get_lang('SendMessageTo'), |
||
| 174 | $teachers, |
||
| 175 | [ |
||
| 176 | 'multiple' => 'multiple', |
||
| 177 | ] |
||
| 178 | ); |
||
| 179 | } else { |
||
| 180 | $form->addElement( |
||
| 181 | 'select_ajax', |
||
| 182 | 'users', |
||
| 183 | get_lang('SendMessageTo'), |
||
| 184 | [], |
||
| 185 | [ |
||
| 186 | 'multiple' => 'multiple', |
||
| 187 | 'url' => api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=find_users', |
||
| 188 | ] |
||
| 189 | ); |
||
| 190 | $form->addRule('users', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 191 | } |
||
| 192 | } else { |
||
| 193 | $form->addElement('hidden', 'hidden_user', $default['users'][0], ['id' => 'hidden_user']); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } else { |
||
| 197 | $userGroup = new UserGroup(); |
||
| 198 | $group_info = $userGroup->get($group_id); |
||
| 199 | |||
| 200 | $form->addElement('label', get_lang('ToGroup'), api_xml_http_response_encode($group_info['name'])); |
||
| 201 | $form->addElement('hidden', 'group_id', $group_id); |
||
| 202 | $form->addElement('hidden', 'parent_id', $message_id); |
||
| 203 | } |
||
| 204 | |||
| 205 | $form->addText('title', get_lang('Subject'), true); |
||
| 206 | $form->addHtmlEditor( |
||
| 207 | 'content', |
||
| 208 | get_lang('Message'), |
||
| 209 | false, |
||
| 210 | true, |
||
| 211 | ['ToolbarSet' => 'Messages'] |
||
| 212 | ); |
||
| 213 | |||
| 214 | if (isset($_GET['re_id'])) { |
||
| 215 | $message_reply_info = MessageManager::get_message_by_id($_GET['re_id']); |
||
| 216 | $default['title'] = get_lang('MailSubjectReplyShort').' '.Security::remove_XSS($message_reply_info['title']); |
||
| 217 | $form->addHidden('re_id', (int) $_GET['re_id']); |
||
| 218 | $form->addHidden('save_form', 'save_form'); |
||
| 219 | |||
| 220 | // Adding reply mail |
||
| 221 | $user_reply_info = api_get_user_info($message_reply_info['user_sender_id']); |
||
| 222 | $default['content'] = '<p><br/></p>'.sprintf( |
||
| 223 | get_lang('XWroteY'), |
||
| 224 | $user_reply_info['complete_name'], |
||
| 225 | Security::filter_terms($message_reply_info['content']) |
||
| 226 | ); |
||
| 227 | } |
||
| 228 | |||
| 229 | if (isset($_GET['forward_id']) && MessageManager::isUserOwner(api_get_user_id(), (int) $_GET['forward_id'])) { |
||
| 230 | $forwardId = (int) $_GET['forward_id']; |
||
| 231 | $message_reply_info = MessageManager::get_message_by_id($forwardId); |
||
| 232 | $attachments = MessageManager::getAttachmentLinkList($forwardId, MessageManager::MESSAGE_TYPE_INBOX); |
||
| 233 | if (!empty($attachments)) { |
||
| 234 | $fileListToString = !empty($attachments) ? implode('<br />', $attachments) : ''; |
||
| 235 | $form->addLabel('', $fileListToString); |
||
| 236 | } |
||
| 237 | $default['title'] = '['.get_lang('MailSubjectForwardShort').": ".Security::remove_XSS($message_reply_info['title']).']'; |
||
| 238 | $form->addHidden('forward_id', $forwardId); |
||
| 239 | $form->addHidden('save_form', 'save_form'); |
||
| 240 | $receiverInfo = api_get_user_info($message_reply_info['user_receiver_id']); |
||
| 241 | |||
| 242 | $forwardMessage = '---------- '.get_lang('ForwardedMessage').' ---------'.'<br />'; |
||
| 243 | $forwardMessage .= get_lang('Date').': '.api_get_local_time($message_reply_info['send_date']).'<br />'; |
||
| 244 | $forwardMessage .= get_lang('Subject').': '.Security::remove_XSS($message_reply_info['title']).'<br />'; |
||
| 245 | $forwardMessage .= get_lang('To').': '.$receiverInfo['complete_name'].' - '.$receiverInfo['email'].' <br />'; |
||
| 246 | $default['content'] = '<p><br/></p>'.$forwardMessage.'<br />'.Security::filter_terms($message_reply_info['content']); |
||
| 247 | } |
||
| 248 | |||
| 249 | $extrafield = new ExtraField('message'); |
||
| 250 | $extraHtml = $extrafield->addElements($form); |
||
| 251 | |||
| 252 | if (empty($group_id)) { |
||
| 253 | $form->addLabel( |
||
| 254 | '', |
||
| 255 | '<div id="file_uploads"><div id="filepath_1"> |
||
| 256 | <div id="filepaths" class="form-horizontal"> |
||
| 257 | <div id="paths-file" class="form-group"> |
||
| 258 | <label class="col-sm-4">'.get_lang('FilesAttachment').'</label> |
||
| 259 | <input class="col-sm-8" type="file" name="attach_1"/> |
||
| 260 | </div> |
||
| 261 | </div> |
||
| 262 | <div id="paths-description" class="form-group"> |
||
| 263 | <label class="col-sm-4">'.get_lang('Description').'</label> |
||
| 264 | <div class="col-sm-8"> |
||
| 265 | <input id="file-descrtiption" class="form-control" type="text" name="legend[]" /> |
||
| 266 | </div> |
||
| 267 | </div> |
||
| 268 | </div> |
||
| 269 | </div>' |
||
| 270 | ); |
||
| 271 | |||
| 272 | $form->addLabel( |
||
| 273 | '', |
||
| 274 | '<span id="link-more-attach"> |
||
| 275 | <a class="btn btn-default" href="javascript://" onclick="return add_image_form()">'. |
||
| 276 | get_lang('AddOneMoreFile').'</a></span> ('. |
||
| 277 | sprintf( |
||
| 278 | get_lang('MaximunFileSizeX'), |
||
| 279 | getIniMaxFileSizeInBytes(true, true) |
||
| 280 | ).')' |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 284 | $form->addLabel( |
||
| 285 | '', |
||
| 286 | '<iframe |
||
| 287 | frameborder="0" height="200" width="100%" scrolling="no" |
||
| 288 | src="'.api_get_path(WEB_CODE_PATH).'messages/record_audio.php"></iframe>' |
||
| 289 | ); |
||
| 290 | |||
| 291 | $form->addButtonSend(get_lang('SendMessage'), 'compose'); |
||
| 292 | $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>'); |
||
| 293 | |||
| 294 | if (!empty($group_id) && !empty($message_id)) { |
||
| 295 | $message_info = MessageManager::get_message_by_id($message_id); |
||
| 296 | $default['title'] = get_lang('MailSubjectReplyShort')." ".$message_info['title']; |
||
| 297 | } |
||
| 298 | $form->setDefaults($default); |
||
| 299 | $html = ''; |
||
| 300 | if ($form->validate()) { |
||
| 301 | $check = Security::check_token('post'); |
||
| 302 | $disabled = api_get_configuration_value('disable_token_in_new_message'); |
||
| 303 | if ($disabled) { |
||
| 304 | $check = true; |
||
| 305 | } |
||
| 306 | |||
| 307 | if ($check) { |
||
| 308 | if (isset($_SESSION['form_values'])) { |
||
| 309 | unset($_SESSION['form_values']); |
||
| 310 | } |
||
| 311 | |||
| 312 | $user_list = $default['users']; |
||
| 313 | $file_comments = $_POST['legend']; |
||
| 314 | $title = $default['title']; |
||
| 315 | $content = $default['content']; |
||
| 316 | $group_id = isset($default['group_id']) ? $default['group_id'] : null; |
||
| 317 | $parent_id = isset($default['parent_id']) ? $default['parent_id'] : null; |
||
| 318 | $forwardId = isset($_POST['forward_id']) ? $_POST['forward_id'] : false; |
||
| 319 | |||
| 320 | if (is_array($user_list) && count($user_list) > 0) { |
||
| 321 | $extraParams = []; |
||
| 322 | |||
| 323 | foreach ($form->exportValues() as $key => $value) { |
||
| 324 | if (!str_contains($key, 'extra_')) { |
||
| 325 | continue; |
||
| 326 | } |
||
| 327 | |||
| 328 | $extraParams[$key] = $value; |
||
| 329 | } |
||
| 330 | |||
| 331 | // All is well, send the message |
||
| 332 | foreach ($user_list as $userId) { |
||
| 333 | $res = MessageManager::send_message( |
||
| 334 | $userId, |
||
| 335 | $title, |
||
| 336 | $content, |
||
| 337 | $_FILES, |
||
| 338 | $file_comments, |
||
| 339 | $group_id, |
||
| 340 | $parent_id, |
||
| 341 | 0, |
||
| 342 | 0, |
||
| 343 | null, |
||
| 344 | false, |
||
| 345 | $forwardId, |
||
| 346 | [], |
||
| 347 | true, |
||
| 348 | false, |
||
| 349 | 0, |
||
| 350 | $extraParams |
||
| 351 | ); |
||
| 352 | |||
| 353 | if ($res) { |
||
| 354 | $userInfo = api_get_user_info($userId); |
||
| 355 | Display::addFlash(Display::return_message( |
||
| 356 | get_lang('MessageSentTo')." <b>".$userInfo['complete_name_with_username']."</b>", |
||
| 357 | 'confirmation', |
||
| 358 | false |
||
| 359 | )); |
||
| 360 | } else { |
||
| 361 | $_SESSION['form_values'] = $default; |
||
| 362 | header('Location: '.api_request_uri()); |
||
| 363 | exit; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | MessageManager::cleanAudioMessage(); |
||
| 367 | } else { |
||
| 368 | Display::addFlash(Display::return_message('ErrorSendingMessage', 'error')); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | Security::clear_token(); |
||
| 372 | header('Location: '.api_get_path(WEB_CODE_PATH).'messages/inbox.php'); |
||
| 373 | exit; |
||
| 374 | } else { |
||
| 375 | $token = Security::get_token(); |
||
| 376 | $form->addElement('hidden', 'sec_token'); |
||
| 377 | $form->setConstants(['sec_token' => $token]); |
||
| 378 | $html .= $form->returnForm(); |
||
| 379 | } |
||
| 380 | |||
| 381 | $html .= '<script>$(function () { '.$extraHtml['jquery_ready_content'].' });</script>'; |
||
| 382 | |||
| 383 | return $html; |
||
| 384 | } |
||
| 511 |