Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like UserManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class UserManager |
||
| 24 | { |
||
| 25 | // This constants are deprecated use the constants located in ExtraField |
||
| 26 | const USER_FIELD_TYPE_TEXT = 1; |
||
| 27 | const USER_FIELD_TYPE_TEXTAREA = 2; |
||
| 28 | const USER_FIELD_TYPE_RADIO = 3; |
||
| 29 | const USER_FIELD_TYPE_SELECT = 4; |
||
| 30 | const USER_FIELD_TYPE_SELECT_MULTIPLE = 5; |
||
| 31 | const USER_FIELD_TYPE_DATE = 6; |
||
| 32 | const USER_FIELD_TYPE_DATETIME = 7; |
||
| 33 | const USER_FIELD_TYPE_DOUBLE_SELECT = 8; |
||
| 34 | const USER_FIELD_TYPE_DIVIDER = 9; |
||
| 35 | const USER_FIELD_TYPE_TAG = 10; |
||
| 36 | const USER_FIELD_TYPE_TIMEZONE = 11; |
||
| 37 | const USER_FIELD_TYPE_SOCIAL_PROFILE = 12; |
||
| 38 | const USER_FIELD_TYPE_FILE = 13; |
||
| 39 | const USER_FIELD_TYPE_MOBILE_PHONE_NUMBER = 14; |
||
| 40 | |||
| 41 | private static $encryptionMethod; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * @assert () === null |
||
| 46 | */ |
||
| 47 | public function __construct() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Repository is use to query the DB, selects, etc |
||
| 53 | * @return Chamilo\UserBundle\Entity\Repository\UserRepository |
||
| 54 | */ |
||
| 55 | public static function getRepository() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Create/update/delete methods are available in the UserManager |
||
| 62 | * (based in the Sonata\UserBundle\Entity\UserManager) |
||
| 63 | * |
||
| 64 | * @return Chamilo\UserBundle\Entity\Manager\UserManager |
||
| 65 | */ |
||
| 66 | public static function getManager() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $encryptionMethod |
||
| 73 | */ |
||
| 74 | public static function setPasswordEncryption($encryptionMethod) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return bool|mixed |
||
| 81 | */ |
||
| 82 | public static function getPasswordEncryption() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return EncoderFactory |
||
| 94 | */ |
||
| 95 | private static function getEncoderFactory() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param User $user |
||
| 109 | * |
||
| 110 | * @return \Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface |
||
| 111 | */ |
||
| 112 | private static function getEncoder(User $user) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Validates the password |
||
| 121 | * |
||
| 122 | * @param $encoded |
||
| 123 | * @param $raw |
||
| 124 | * @param $salt |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public static function isPasswordValid($encoded, $raw, $salt) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $raw |
||
| 137 | * @param User $user |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public static function encryptPassword($raw, User $user) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param int $userId |
||
| 155 | * @param string $password |
||
| 156 | * |
||
| 157 | */ |
||
| 158 | public static function updatePassword($userId, $password) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Creates a new user for the platform |
||
| 170 | * @author Hugues Peeters <[email protected]>, |
||
| 171 | * @author Roan Embrechts <[email protected]> |
||
| 172 | * @param string $firstName |
||
| 173 | * @param string $lastName |
||
| 174 | * @param int $status (1 for course tutor, 5 for student, 6 for anonymous) |
||
| 175 | * @param string $email |
||
| 176 | * @param string $loginName |
||
| 177 | * @param string $password |
||
| 178 | * @param string $official_code Any official code (optional) |
||
| 179 | * @param string $language User language (optional) |
||
| 180 | * @param string $phone Phone number (optional) |
||
| 181 | * @param string $picture_uri Picture URI (optional) |
||
| 182 | * @param string $authSource Authentication source (optional, defaults to 'platform', dependind on constant) |
||
| 183 | * @param string $expirationDate Account expiration date (optional, defaults to null) |
||
| 184 | * @param int $active Whether the account is enabled or disabled by default |
||
| 185 | * @param int $hr_dept_id The department of HR in which the user is registered (optional, defaults to 0) |
||
| 186 | * @param array $extra Extra fields |
||
| 187 | * @param string $encrypt_method Encrypt method used if password is given encrypted. Set to an empty string by default |
||
| 188 | * @param bool $send_mail |
||
| 189 | * @param bool $isAdmin |
||
| 190 | * @param string $address |
||
| 191 | * @param bool $sendEmailToAllAdmins |
||
| 192 | * @param FormValidator $form |
||
| 193 | * |
||
| 194 | * @return mixed new user id - if the new user creation succeeds, false otherwise |
||
| 195 | * @desc The function tries to retrieve user id from the session. |
||
| 196 | * If it exists, the current user id is the creator id. If a problem arises, |
||
| 197 | * @assert ('Sam','Gamegie',5,'[email protected]','jo','jo') > 1 |
||
| 198 | * @assert ('Pippin','Took',null,null,'jo','jo') === false |
||
| 199 | */ |
||
| 200 | public static function create_user( |
||
| 201 | $firstName, |
||
| 202 | $lastName, |
||
| 203 | $status, |
||
| 204 | $email, |
||
| 205 | $loginName, |
||
| 206 | $password, |
||
| 207 | $official_code = '', |
||
| 208 | $language = '', |
||
| 209 | $phone = '', |
||
| 210 | $picture_uri = '', |
||
| 211 | $authSource = PLATFORM_AUTH_SOURCE, |
||
| 212 | $expirationDate = null, |
||
| 213 | $active = 1, |
||
| 214 | $hr_dept_id = 0, |
||
| 215 | $extra = [], |
||
| 216 | $encrypt_method = '', |
||
| 217 | $send_mail = false, |
||
| 218 | $isAdmin = false, |
||
| 219 | $address = '', |
||
| 220 | $sendEmailToAllAdmins = false, |
||
| 221 | $form = null |
||
| 222 | ) { |
||
| 223 | $currentUserId = api_get_user_id(); |
||
| 224 | $hook = HookCreateUser::create(); |
||
| 225 | if (!empty($hook)) { |
||
| 226 | $hook->notifyCreateUser(HOOK_EVENT_TYPE_PRE); |
||
| 227 | } |
||
| 228 | |||
| 229 | // First check wether the login already exists |
||
| 230 | if (!self::is_username_available($loginName)) { |
||
| 231 | Display::addFlash( |
||
| 232 | Display::return_message(get_lang('LoginAlreadyTaken')) |
||
| 233 | ); |
||
| 234 | |||
| 235 | return false; |
||
| 236 | } |
||
| 237 | |||
| 238 | global $_configuration; |
||
| 239 | $original_password = $password; |
||
| 240 | |||
| 241 | $access_url_id = 1; |
||
| 242 | if (api_get_multiple_access_url()) { |
||
| 243 | $access_url_id = api_get_current_access_url_id(); |
||
| 244 | } |
||
| 245 | |||
| 246 | View Code Duplication | if (isset($_configuration[$access_url_id]) && |
|
| 247 | is_array($_configuration[$access_url_id]) && |
||
| 248 | isset($_configuration[$access_url_id]['hosting_limit_users']) && |
||
| 249 | $_configuration[$access_url_id]['hosting_limit_users'] > 0) { |
||
| 250 | $num = self::get_number_of_users(); |
||
| 251 | if ($num >= $_configuration[$access_url_id]['hosting_limit_users']) { |
||
| 252 | api_warn_hosting_contact('hosting_limit_users'); |
||
| 253 | Display::addFlash(Display::return_message(get_lang('PortalUsersLimitReached'), 'warning')); |
||
| 254 | |||
| 255 | return false; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | View Code Duplication | if ($status === 1 && |
|
| 260 | isset($_configuration[$access_url_id]) && |
||
| 261 | is_array($_configuration[$access_url_id]) && |
||
| 262 | isset($_configuration[$access_url_id]['hosting_limit_teachers']) && |
||
| 263 | $_configuration[$access_url_id]['hosting_limit_teachers'] > 0 |
||
| 264 | ) { |
||
| 265 | $num = self::get_number_of_users(1); |
||
| 266 | if ($num >= $_configuration[$access_url_id]['hosting_limit_teachers']) { |
||
| 267 | Display::addFlash(Display::return_message(get_lang('PortalTeachersLimitReached'), 'warning')); |
||
| 268 | api_warn_hosting_contact('hosting_limit_teachers'); |
||
| 269 | |||
| 270 | return false; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | View Code Duplication | if (empty($password)) { |
|
| 275 | if ($authSource === PLATFORM_AUTH_SOURCE) { |
||
| 276 | Display::addFlash( |
||
| 277 | Display::return_message( |
||
| 278 | get_lang('ThisFieldIsRequired').': '.get_lang( |
||
| 279 | 'Password' |
||
| 280 | ), |
||
| 281 | 'warning' |
||
| 282 | ) |
||
| 283 | ); |
||
| 284 | |||
| 285 | return false; |
||
| 286 | } |
||
| 287 | |||
| 288 | // We use the authSource as password. |
||
| 289 | // The real validation will be by processed by the auth |
||
| 290 | // source not Chamilo |
||
| 291 | //$password = $authSource; |
||
| 292 | } |
||
| 293 | |||
| 294 | // database table definition |
||
| 295 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 296 | |||
| 297 | // Checking the user language |
||
| 298 | $languages = api_get_languages(); |
||
| 299 | $language = strtolower($language); |
||
| 300 | |||
| 301 | View Code Duplication | if (isset($languages['folder'])) { |
|
| 302 | if (!in_array($language, $languages['folder'])) { |
||
| 303 | $language = api_get_setting('platformLanguage'); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | if (!empty($currentUserId)) { |
||
| 308 | $creator_id = $currentUserId; |
||
| 309 | } else { |
||
| 310 | $creator_id = 0; |
||
| 311 | } |
||
| 312 | |||
| 313 | $currentDate = api_get_utc_datetime(); |
||
| 314 | $now = new DateTime(); |
||
| 315 | |||
| 316 | if (empty($expirationDate) || $expirationDate == '0000-00-00 00:00:00') { |
||
| 317 | // Default expiration date |
||
| 318 | // if there is a default duration of a valid account then |
||
| 319 | // we have to change the expiration_date accordingly |
||
| 320 | // Accept 0000-00-00 00:00:00 as a null value to avoid issues with |
||
| 321 | // third party code using this method with the previous (pre-1.10) |
||
| 322 | // value of 0000... |
||
| 323 | if (api_get_setting('account_valid_duration') != '') { |
||
| 324 | $expirationDate = new DateTime($currentDate); |
||
| 325 | $days = intval(api_get_setting('account_valid_duration')); |
||
| 326 | $expirationDate->modify('+'.$days.' day'); |
||
| 327 | } |
||
| 328 | } else { |
||
| 329 | $expirationDate = api_get_utc_datetime($expirationDate); |
||
| 330 | $expirationDate = new \DateTime($expirationDate, new DateTimeZone('UTC')); |
||
| 331 | } |
||
| 332 | |||
| 333 | $em = Database::getManager(); |
||
| 334 | $userManager = self::getManager(); |
||
| 335 | |||
| 336 | /** @var User $user */ |
||
| 337 | $user = $userManager->createUser(); |
||
| 338 | |||
| 339 | $user |
||
|
|
|||
| 340 | ->setLastname($lastName) |
||
| 341 | ->setFirstname($firstName) |
||
| 342 | ->setUsername($loginName) |
||
| 343 | ->setStatus($status) |
||
| 344 | ->setPlainPassword($password) |
||
| 345 | ->setEmail($email) |
||
| 346 | ->setOfficialCode($official_code) |
||
| 347 | ->setPictureUri($picture_uri) |
||
| 348 | ->setCreatorId($creator_id) |
||
| 349 | ->setAuthSource($authSource) |
||
| 350 | ->setPhone($phone) |
||
| 351 | ->setAddress($address) |
||
| 352 | ->setLanguage($language) |
||
| 353 | ->setRegistrationDate($now) |
||
| 354 | ->setHrDeptId($hr_dept_id) |
||
| 355 | ->setActive($active) |
||
| 356 | ->setEnabled($active) |
||
| 357 | ; |
||
| 358 | |||
| 359 | if (!empty($expirationDate)) { |
||
| 360 | $user->setExpirationDate($expirationDate); |
||
| 361 | } |
||
| 362 | |||
| 363 | $url = $em->getRepository('ChamiloCoreBundle:AccessUrl')->find(api_get_current_access_url_id()); |
||
| 364 | |||
| 365 | $accessRelUser = new AccessUrlRelUser(); |
||
| 366 | $accessRelUser->setUser($user); |
||
| 367 | $accessRelUser->setPortal($url); |
||
| 368 | $user->setPortal($accessRelUser); |
||
| 369 | |||
| 370 | // Default group is student |
||
| 371 | $group = 'student'; |
||
| 372 | |||
| 373 | switch ($status) { |
||
| 374 | case STUDENT: |
||
| 375 | $group = 'student'; |
||
| 376 | break; |
||
| 377 | case COURSEMANAGER: |
||
| 378 | $group = 'teacher'; |
||
| 379 | break; |
||
| 380 | case DRH: |
||
| 381 | $group = 'drh'; |
||
| 382 | break; |
||
| 383 | case SESSIONADMIN: |
||
| 384 | $group = 'session_manager'; |
||
| 385 | break; |
||
| 386 | /*case QUESTION: |
||
| 387 | $group = 'question_manager'; |
||
| 388 | break;*/ |
||
| 389 | case STUDENT_BOSS: |
||
| 390 | $group = 'student_boss'; |
||
| 391 | break; |
||
| 392 | case INVITEE: |
||
| 393 | $group = 'invitee'; |
||
| 394 | break; |
||
| 395 | } |
||
| 396 | |||
| 397 | if ($isAdmin) { |
||
| 398 | $group = 'admin'; |
||
| 399 | } |
||
| 400 | |||
| 401 | $criteria = ['code' => $group]; |
||
| 402 | $group = $em->getRepository('ChamiloUserBundle:Group')->findOneBy($criteria); |
||
| 403 | $user->setGroups(array($group)); |
||
| 404 | |||
| 405 | $userManager->updateUser($user, true); |
||
| 406 | $userId = $user->getId(); |
||
| 407 | |||
| 408 | if (!empty($userId)) { |
||
| 409 | $sql = "UPDATE $table_user SET user_id = $userId WHERE id = $userId"; |
||
| 410 | Database::query($sql); |
||
| 411 | |||
| 412 | if ($isAdmin) { |
||
| 413 | UserManager::add_user_as_admin($user); |
||
| 414 | } |
||
| 415 | |||
| 416 | $extra['item_id'] = $userId; |
||
| 417 | |||
| 418 | if (is_array($extra) && count($extra) > 0) { |
||
| 419 | $courseFieldValue = new ExtraFieldValue('user'); |
||
| 420 | $courseFieldValue->saveFieldValues($extra); |
||
| 421 | } else { |
||
| 422 | // Create notify settings by default |
||
| 423 | self::update_extra_field_value($userId, 'mail_notify_invitation', '1'); |
||
| 424 | self::update_extra_field_value($userId, 'mail_notify_message', '1'); |
||
| 425 | self::update_extra_field_value($userId, 'mail_notify_group_message', '1'); |
||
| 426 | } |
||
| 427 | |||
| 428 | self::update_extra_field_value($userId, 'already_logged_in', 'false'); |
||
| 429 | |||
| 430 | if (!empty($email) && $send_mail) { |
||
| 431 | $recipient_name = api_get_person_name( |
||
| 432 | $firstName, |
||
| 433 | $lastName, |
||
| 434 | null, |
||
| 435 | PERSON_NAME_EMAIL_ADDRESS |
||
| 436 | ); |
||
| 437 | $tplSubject = new Template( |
||
| 438 | null, |
||
| 439 | false, |
||
| 440 | false, |
||
| 441 | false, |
||
| 442 | false, |
||
| 443 | false |
||
| 444 | ); |
||
| 445 | $layoutSubject = $tplSubject->get_template( |
||
| 446 | 'mail/subject_registration_platform.tpl' |
||
| 447 | ); |
||
| 448 | $emailSubject = $tplSubject->fetch($layoutSubject); |
||
| 449 | $sender_name = api_get_person_name( |
||
| 450 | api_get_setting('administratorName'), |
||
| 451 | api_get_setting('administratorSurname'), |
||
| 452 | null, |
||
| 453 | PERSON_NAME_EMAIL_ADDRESS |
||
| 454 | ); |
||
| 455 | $email_admin = api_get_setting('emailAdministrator'); |
||
| 456 | |||
| 457 | $url = api_get_path(WEB_PATH); |
||
| 458 | View Code Duplication | if (api_is_multiple_url_enabled()) { |
|
| 459 | $access_url_id = api_get_current_access_url_id(); |
||
| 460 | if ($access_url_id != -1) { |
||
| 461 | $urlInfo = api_get_access_url($access_url_id); |
||
| 462 | if ($urlInfo) { |
||
| 463 | $url = $urlInfo['url']; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | $tplContent = new Template(null, false, false, false, false, false); |
||
| 469 | // variables for the default template |
||
| 470 | $tplContent->assign('complete_name', stripslashes(api_get_person_name($firstName, $lastName))); |
||
| 471 | $tplContent->assign('login_name', $loginName); |
||
| 472 | $tplContent->assign('original_password', stripslashes($original_password)); |
||
| 473 | $tplContent->assign('mailWebPath', $url); |
||
| 474 | $tplContent->assign('new_user', $user); |
||
| 475 | |||
| 476 | $layoutContent = $tplContent->get_template('mail/content_registration_platform.tpl'); |
||
| 477 | $emailBody = $tplContent->fetch($layoutContent); |
||
| 478 | /* MANAGE EVENT WITH MAIL */ |
||
| 479 | if (EventsMail::check_if_using_class('user_registration')) { |
||
| 480 | $values["about_user"] = $userId; |
||
| 481 | $values["password"] = $original_password; |
||
| 482 | $values["send_to"] = array($userId); |
||
| 483 | $values["prior_lang"] = null; |
||
| 484 | EventsDispatcher::events('user_registration', $values); |
||
| 485 | } else { |
||
| 486 | $phoneNumber = isset($extra['mobile_phone_number']) ? $extra['mobile_phone_number'] : null; |
||
| 487 | |||
| 488 | $additionalParameters = array( |
||
| 489 | 'smsType' => SmsPlugin::WELCOME_LOGIN_PASSWORD, |
||
| 490 | 'userId' => $userId, |
||
| 491 | 'mobilePhoneNumber' => $phoneNumber, |
||
| 492 | 'password' => $original_password |
||
| 493 | ); |
||
| 494 | |||
| 495 | api_mail_html( |
||
| 496 | $recipient_name, |
||
| 497 | $email, |
||
| 498 | $emailSubject, |
||
| 499 | $emailBody, |
||
| 500 | $sender_name, |
||
| 501 | $email_admin, |
||
| 502 | null, |
||
| 503 | null, |
||
| 504 | null, |
||
| 505 | $additionalParameters |
||
| 506 | ); |
||
| 507 | } |
||
| 508 | |||
| 509 | if ($sendEmailToAllAdmins) { |
||
| 510 | $adminList = self::get_all_administrators(); |
||
| 511 | |||
| 512 | $tplContent = new Template(null, false, false, false, false, false); |
||
| 513 | // variables for the default template |
||
| 514 | $tplContent->assign('complete_name', stripslashes(api_get_person_name($firstName, $lastName))); |
||
| 515 | $tplContent->assign('user_added', $user); |
||
| 516 | |||
| 517 | $renderer = FormValidator::getDefaultRenderer(); |
||
| 518 | |||
| 519 | // Form template |
||
| 520 | $elementTemplate = ' {label}: {element} <br />'; |
||
| 521 | $renderer->setElementTemplate($elementTemplate); |
||
| 522 | /** @var FormValidator $form */ |
||
| 523 | $form->freeze(null, $elementTemplate); |
||
| 524 | $form->removeElement('submit'); |
||
| 525 | $formData = $form->returnForm(); |
||
| 526 | $url = api_get_path(WEB_CODE_PATH).'admin/user_information.php?user_id='.$user->getId(); |
||
| 527 | $tplContent->assign('link', Display::url($url, $url)); |
||
| 528 | $tplContent->assign('form', $formData); |
||
| 529 | |||
| 530 | $layoutContent = $tplContent->get_template('mail/content_registration_platform_to_admin.tpl'); |
||
| 531 | $emailBody = $tplContent->fetch($layoutContent); |
||
| 532 | $subject = get_lang('UserAdded'); |
||
| 533 | |||
| 534 | foreach ($adminList as $adminId => $data) { |
||
| 535 | MessageManager::send_message_simple($adminId, $subject, $emailBody); |
||
| 536 | } |
||
| 537 | |||
| 538 | } |
||
| 539 | /* ENDS MANAGE EVENT WITH MAIL */ |
||
| 540 | } |
||
| 541 | |||
| 542 | if (!empty($hook)) { |
||
| 543 | $hook->setEventData(array( |
||
| 544 | 'return' => $userId, |
||
| 545 | 'originalPassword' => $original_password |
||
| 546 | )); |
||
| 547 | $hook->notifyCreateUser(HOOK_EVENT_TYPE_POST); |
||
| 548 | } |
||
| 549 | Event::addEvent(LOG_USER_CREATE, LOG_USER_ID, $userId); |
||
| 550 | } else { |
||
| 551 | Display::addFlash(Display::return_message(get_lang('ErrorContactPlatformAdmin'))); |
||
| 552 | |||
| 553 | return false; |
||
| 554 | } |
||
| 555 | |||
| 556 | return $userId; |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Can user be deleted? This function checks whether there's a course |
||
| 561 | * in which the given user is the |
||
| 562 | * only course administrator. If that is the case, the user can't be |
||
| 563 | * deleted because the course would remain without a course admin. |
||
| 564 | * @param int $user_id The user id |
||
| 565 | * @return boolean true if user can be deleted |
||
| 566 | * @assert (null) === false |
||
| 567 | * @assert (-1) === false |
||
| 568 | * @assert ('abc') === false |
||
| 569 | */ |
||
| 570 | public static function can_delete_user($user_id) |
||
| 571 | { |
||
| 572 | $deny = api_get_configuration_value('deny_delete_users'); |
||
| 573 | |||
| 574 | if ($deny) { |
||
| 575 | return false; |
||
| 576 | } |
||
| 577 | |||
| 578 | $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 579 | if ($user_id != strval(intval($user_id))) { |
||
| 580 | return false; |
||
| 581 | } |
||
| 582 | if ($user_id === false) { |
||
| 583 | return false; |
||
| 584 | } |
||
| 585 | $sql = "SELECT * FROM $table_course_user |
||
| 586 | WHERE status = 1 AND user_id = ".$user_id; |
||
| 587 | $res = Database::query($sql); |
||
| 588 | while ($course = Database::fetch_object($res)) { |
||
| 589 | $sql = "SELECT id FROM $table_course_user |
||
| 590 | WHERE status=1 AND c_id = ".intval($course->c_id); |
||
| 591 | $res2 = Database::query($sql); |
||
| 592 | if (Database::num_rows($res2) == 1) { |
||
| 593 | return false; |
||
| 594 | } |
||
| 595 | } |
||
| 596 | |||
| 597 | return true; |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Delete a user from the platform, and all its belongings. This is a |
||
| 602 | * very dangerous function that should only be accessible by |
||
| 603 | * super-admins. Other roles should only be able to disable a user, |
||
| 604 | * which removes access to the platform but doesn't delete anything. |
||
| 605 | * @param int The ID of th user to be deleted |
||
| 606 | * @return boolean true if user is successfully deleted, false otherwise |
||
| 607 | * @assert (null) === false |
||
| 608 | * @assert ('abc') === false |
||
| 609 | */ |
||
| 610 | public static function delete_user($user_id) |
||
| 611 | { |
||
| 612 | if ($user_id != strval(intval($user_id))) { |
||
| 613 | return false; |
||
| 614 | } |
||
| 615 | |||
| 616 | if ($user_id === false) { |
||
| 617 | return false; |
||
| 618 | } |
||
| 619 | |||
| 620 | if (!self::can_delete_user($user_id)) { |
||
| 621 | return false; |
||
| 622 | } |
||
| 623 | |||
| 624 | $user = self::getManager()->find($user_id); |
||
| 625 | |||
| 626 | if (empty($user)) { |
||
| 627 | return false; |
||
| 628 | } |
||
| 629 | |||
| 630 | $usergroup_rel_user = Database :: get_main_table(TABLE_USERGROUP_REL_USER); |
||
| 631 | $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 632 | $table_course = Database :: get_main_table(TABLE_MAIN_COURSE); |
||
| 633 | $table_session = Database :: get_main_table(TABLE_MAIN_SESSION); |
||
| 634 | $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN); |
||
| 635 | $table_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 636 | $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 637 | $table_group = Database :: get_course_table(TABLE_GROUP_USER); |
||
| 638 | $table_work = Database :: get_course_table(TABLE_STUDENT_PUBLICATION); |
||
| 639 | |||
| 640 | // Unsubscribe the user from all groups in all his courses |
||
| 641 | $sql = "SELECT c.id |
||
| 642 | FROM $table_course c |
||
| 643 | INNER JOIN $table_course_user cu |
||
| 644 | ON (c.id = cu.c_id) |
||
| 645 | WHERE |
||
| 646 | cu.user_id = '".$user_id."' AND |
||
| 647 | relation_type<>".COURSE_RELATION_TYPE_RRHH." |
||
| 648 | "; |
||
| 649 | |||
| 650 | $res = Database::query($sql); |
||
| 651 | while ($course = Database::fetch_object($res)) { |
||
| 652 | $sql = "DELETE FROM $table_group |
||
| 653 | WHERE c_id = {$course->id} AND user_id = $user_id"; |
||
| 654 | Database::query($sql); |
||
| 655 | } |
||
| 656 | |||
| 657 | // Unsubscribe user from usergroup_rel_user |
||
| 658 | $sql = "DELETE FROM $usergroup_rel_user WHERE user_id = '".$user_id."'"; |
||
| 659 | Database::query($sql); |
||
| 660 | |||
| 661 | // Unsubscribe user from all courses |
||
| 662 | $sql = "DELETE FROM $table_course_user WHERE user_id = '".$user_id."'"; |
||
| 663 | Database::query($sql); |
||
| 664 | |||
| 665 | // Unsubscribe user from all courses in sessions |
||
| 666 | $sql = "DELETE FROM $table_session_course_user WHERE user_id = '".$user_id."'"; |
||
| 667 | Database::query($sql); |
||
| 668 | |||
| 669 | // If the user was added as a id_coach then set the current admin as coach see BT# |
||
| 670 | $currentUserId = api_get_user_id(); |
||
| 671 | $sql = "UPDATE $table_session SET id_coach = $currentUserId |
||
| 672 | WHERE id_coach = '".$user_id."'"; |
||
| 673 | Database::query($sql); |
||
| 674 | |||
| 675 | $sql = "UPDATE $table_session SET id_coach = $currentUserId |
||
| 676 | WHERE session_admin_id = '".$user_id."'"; |
||
| 677 | Database::query($sql); |
||
| 678 | |||
| 679 | // Unsubscribe user from all sessions |
||
| 680 | $sql = "DELETE FROM $table_session_user |
||
| 681 | WHERE user_id = '".$user_id."'"; |
||
| 682 | Database::query($sql); |
||
| 683 | |||
| 684 | // Delete user picture |
||
| 685 | /* TODO: Logic about api_get_setting('split_users_upload_directory') == 'true' |
||
| 686 | a user has 4 different sized photos to be deleted. */ |
||
| 687 | $user_info = api_get_user_info($user_id); |
||
| 688 | |||
| 689 | if (strlen($user_info['picture_uri']) > 0) { |
||
| 690 | $path = self::getUserPathById($user_id, 'system'); |
||
| 691 | $img_path = $path.$user_info['picture_uri']; |
||
| 692 | if (file_exists($img_path)) { |
||
| 693 | unlink($img_path); |
||
| 694 | } |
||
| 695 | } |
||
| 696 | |||
| 697 | // Delete the personal course categories |
||
| 698 | $course_cat_table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); |
||
| 699 | $sql = "DELETE FROM $course_cat_table WHERE user_id = '".$user_id."'"; |
||
| 700 | Database::query($sql); |
||
| 701 | |||
| 702 | // Delete user from the admin table |
||
| 703 | $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'"; |
||
| 704 | Database::query($sql); |
||
| 705 | |||
| 706 | // Delete the personal agenda-items from this user |
||
| 707 | $agenda_table = Database::get_main_table(TABLE_PERSONAL_AGENDA); |
||
| 708 | $sql = "DELETE FROM $agenda_table WHERE user = '".$user_id."'"; |
||
| 709 | Database::query($sql); |
||
| 710 | |||
| 711 | $gradebook_results_table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
||
| 712 | $sql = 'DELETE FROM '.$gradebook_results_table.' WHERE user_id = '.$user_id; |
||
| 713 | Database::query($sql); |
||
| 714 | |||
| 715 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 716 | $extraFieldValue->deleteValuesByItem($user_id); |
||
| 717 | |||
| 718 | UrlManager::deleteUserFromAllUrls($user_id); |
||
| 719 | |||
| 720 | if (api_get_setting('allow_social_tool') == 'true') { |
||
| 721 | $userGroup = new UserGroup(); |
||
| 722 | //Delete user from portal groups |
||
| 723 | $group_list = $userGroup->get_groups_by_user($user_id); |
||
| 724 | if (!empty($group_list)) { |
||
| 725 | foreach ($group_list as $group_id => $data) { |
||
| 726 | $userGroup->delete_user_rel_group($user_id, $group_id); |
||
| 727 | } |
||
| 728 | } |
||
| 729 | |||
| 730 | // Delete user from friend lists |
||
| 731 | SocialManager::remove_user_rel_user($user_id, true); |
||
| 732 | } |
||
| 733 | |||
| 734 | // Removing survey invitation |
||
| 735 | SurveyManager::delete_all_survey_invitations_by_user($user_id); |
||
| 736 | |||
| 737 | // Delete students works |
||
| 738 | $sql = "DELETE FROM $table_work WHERE user_id = $user_id AND c_id <> 0"; |
||
| 739 | Database::query($sql); |
||
| 740 | |||
| 741 | $sql = "UPDATE c_item_property SET to_user_id = NULL |
||
| 742 | WHERE to_user_id = '".$user_id."'"; |
||
| 743 | Database::query($sql); |
||
| 744 | |||
| 745 | $sql = "UPDATE c_item_property SET insert_user_id = NULL |
||
| 746 | WHERE insert_user_id = '".$user_id."'"; |
||
| 747 | Database::query($sql); |
||
| 748 | |||
| 749 | $sql = "UPDATE c_item_property SET lastedit_user_id = NULL |
||
| 750 | WHERE lastedit_user_id = '".$user_id."'"; |
||
| 751 | Database::query($sql); |
||
| 752 | |||
| 753 | // Skills |
||
| 754 | $table = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER); |
||
| 755 | $sql = "DELETE FROM $table WHERE user_id = $user_id"; |
||
| 756 | Database::query($sql); |
||
| 757 | |||
| 758 | $connection = Database::getManager()->getConnection(); |
||
| 759 | $tableExists = $connection->getSchemaManager()->tablesExist(['plugin_bbb_room']); |
||
| 760 | if ($tableExists) { |
||
| 761 | // Delete user from database |
||
| 762 | $sql = "DELETE FROM plugin_bbb_room WHERE participant_id = $user_id"; |
||
| 763 | Database::query($sql); |
||
| 764 | } |
||
| 765 | |||
| 766 | // Delete user/ticket relationships :( |
||
| 767 | $tableExists = $connection->getSchemaManager()->tablesExist(['ticket_ticket']); |
||
| 768 | if ($tableExists) { |
||
| 769 | TicketManager::deleteUserFromTicketSystem($user_id); |
||
| 770 | } |
||
| 771 | |||
| 772 | $tableExists = $connection->getSchemaManager()->tablesExist(['c_lp_category_user']); |
||
| 773 | if ($tableExists) { |
||
| 774 | $sql = "DELETE FROM c_lp_category_user WHERE user_id = $user_id"; |
||
| 775 | Database::query($sql); |
||
| 776 | } |
||
| 777 | |||
| 778 | // Delete user from database |
||
| 779 | /*$sql = "DELETE FROM $table_user WHERE id = '".$user_id."'"; |
||
| 780 | Database::query($sql);*/ |
||
| 781 | self::getManager()->delete($user); |
||
| 782 | |||
| 783 | // Add event to system log |
||
| 784 | $user_id_manager = api_get_user_id(); |
||
| 785 | |||
| 786 | Event::addEvent( |
||
| 787 | LOG_USER_DELETE, |
||
| 788 | LOG_USER_ID, |
||
| 789 | $user_id, |
||
| 790 | api_get_utc_datetime(), |
||
| 791 | $user_id_manager |
||
| 792 | ); |
||
| 793 | |||
| 794 | Event::addEvent( |
||
| 795 | LOG_USER_DELETE, |
||
| 796 | LOG_USER_OBJECT, |
||
| 797 | $user_info, |
||
| 798 | api_get_utc_datetime(), |
||
| 799 | $user_id_manager |
||
| 800 | ); |
||
| 801 | $cacheAvailable = api_get_configuration_value('apc'); |
||
| 802 | View Code Duplication | if ($cacheAvailable === true) { |
|
| 803 | $apcVar = api_get_configuration_value('apc_prefix').'userinfo_'.$user_id; |
||
| 804 | if (apcu_exists($apcVar)) { |
||
| 805 | apcu_delete($apcVar); |
||
| 806 | } |
||
| 807 | } |
||
| 808 | |||
| 809 | return true; |
||
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Deletes users completely. Can be called either as: |
||
| 814 | * - UserManager::delete_users(1, 2, 3); or |
||
| 815 | * - UserManager::delete_users(array(1, 2, 3)); |
||
| 816 | * @param array|int $ids |
||
| 817 | * @return boolean True if at least one user was successfuly deleted. False otherwise. |
||
| 818 | * @author Laurent Opprecht |
||
| 819 | * @uses UserManager::delete_user() to actually delete each user |
||
| 820 | * @assert (null) === false |
||
| 821 | * @assert (-1) === false |
||
| 822 | * @assert (array(-1)) === false |
||
| 823 | */ |
||
| 824 | public static function delete_users($ids = array()) |
||
| 825 | { |
||
| 826 | $result = false; |
||
| 827 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 828 | if (!is_array($ids) || count($ids) == 0) { |
||
| 829 | return false; |
||
| 830 | } |
||
| 831 | $ids = array_map('intval', $ids); |
||
| 832 | foreach ($ids as $id) { |
||
| 833 | if (empty($id) || $id < 1) { |
||
| 834 | continue; |
||
| 835 | } |
||
| 836 | $deleted = self::delete_user($id); |
||
| 837 | $result = $deleted || $result; |
||
| 838 | } |
||
| 839 | |||
| 840 | return $result; |
||
| 841 | } |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Disable users. Can be called either as: |
||
| 845 | * - UserManager::deactivate_users(1, 2, 3); |
||
| 846 | * - UserManager::deactivate_users(array(1, 2, 3)); |
||
| 847 | * @param array|int $ids |
||
| 848 | * @return boolean |
||
| 849 | * @author Laurent Opprecht |
||
| 850 | * @assert (null) === false |
||
| 851 | * @assert (array(-1)) === false |
||
| 852 | */ |
||
| 853 | View Code Duplication | public static function deactivate_users($ids = array()) |
|
| 854 | { |
||
| 855 | if (empty($ids)) { |
||
| 856 | return false; |
||
| 857 | } |
||
| 858 | |||
| 859 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 860 | |||
| 861 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 862 | $ids = array_map('intval', $ids); |
||
| 863 | $ids = implode(',', $ids); |
||
| 864 | |||
| 865 | $sql = "UPDATE $table_user SET active = 0 WHERE id IN ($ids)"; |
||
| 866 | $r = Database::query($sql); |
||
| 867 | if ($r !== false) { |
||
| 868 | Event::addEvent(LOG_USER_DISABLE, LOG_USER_ID, $ids); |
||
| 869 | } |
||
| 870 | return $r; |
||
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Enable users. Can be called either as: |
||
| 875 | * - UserManager::activate_users(1, 2, 3); |
||
| 876 | * - UserManager::activate_users(array(1, 2, 3)); |
||
| 877 | * @param array|int IDs of the users to enable |
||
| 878 | * @return boolean |
||
| 879 | * @author Laurent Opprecht |
||
| 880 | * @assert (null) === false |
||
| 881 | * @assert (array(-1)) === false |
||
| 882 | */ |
||
| 883 | View Code Duplication | public static function activate_users($ids = array()) |
|
| 884 | { |
||
| 885 | if (empty($ids)) { |
||
| 886 | return false; |
||
| 887 | } |
||
| 888 | |||
| 889 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 890 | |||
| 891 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 892 | $ids = array_map('intval', $ids); |
||
| 893 | $ids = implode(',', $ids); |
||
| 894 | |||
| 895 | $sql = "UPDATE $table_user SET active = 1 WHERE id IN ($ids)"; |
||
| 896 | $r = Database::query($sql); |
||
| 897 | if ($r !== false) { |
||
| 898 | Event::addEvent(LOG_USER_ENABLE, LOG_USER_ID, $ids); |
||
| 899 | } |
||
| 900 | return $r; |
||
| 901 | } |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Update user information with new openid |
||
| 905 | * @param int $user_id |
||
| 906 | * @param string $openid |
||
| 907 | * @return boolean true if the user information was updated |
||
| 908 | * @assert (false,'') === false |
||
| 909 | * @assert (-1,'') === false |
||
| 910 | */ |
||
| 911 | public static function update_openid($user_id, $openid) |
||
| 912 | { |
||
| 913 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 914 | if ($user_id != strval(intval($user_id))) { |
||
| 915 | return false; |
||
| 916 | } |
||
| 917 | if ($user_id === false) { |
||
| 918 | return false; |
||
| 919 | } |
||
| 920 | $sql = "UPDATE $table_user SET |
||
| 921 | openid='".Database::escape_string($openid)."'"; |
||
| 922 | $sql .= " WHERE id= $user_id"; |
||
| 923 | |||
| 924 | return Database::query($sql); |
||
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Update user information with all the parameters passed to this function |
||
| 929 | * @param int The ID of the user to be updated |
||
| 930 | * @param string The user's firstname |
||
| 931 | * @param string The user's lastname |
||
| 932 | * @param string The user's username (login) |
||
| 933 | * @param string The user's password |
||
| 934 | * @param string The authentication source (default: "platform") |
||
| 935 | * @param string The user's e-mail address |
||
| 936 | * @param int The user's status |
||
| 937 | * @param string The user's official code (usually just an internal institutional code) |
||
| 938 | * @param string The user's phone number |
||
| 939 | * @param string The user's picture URL (internal to the Chamilo directory) |
||
| 940 | * @param int The user ID of the person who registered this user (optional, defaults to null) |
||
| 941 | * @param int The department of HR in which the user is registered (optional, defaults to 0) |
||
| 942 | * @param array A series of additional fields to add to this user as extra fields (optional, defaults to null) |
||
| 943 | * @return boolean|integer False on error, or the user ID if the user information was updated |
||
| 944 | * @assert (false, false, false, false, false, false, false, false, false, false, false, false, false) === false |
||
| 945 | */ |
||
| 946 | public static function update_user( |
||
| 947 | $user_id, |
||
| 948 | $firstname, |
||
| 949 | $lastname, |
||
| 950 | $username, |
||
| 951 | $password = null, |
||
| 952 | $auth_source = null, |
||
| 953 | $email, |
||
| 954 | $status, |
||
| 955 | $official_code, |
||
| 956 | $phone, |
||
| 957 | $picture_uri, |
||
| 958 | $expiration_date, |
||
| 959 | $active, |
||
| 960 | $creator_id = null, |
||
| 961 | $hr_dept_id = 0, |
||
| 962 | $extra = null, |
||
| 963 | $language = 'english', |
||
| 964 | $encrypt_method = '', |
||
| 965 | $send_email = false, |
||
| 966 | $reset_password = 0, |
||
| 967 | $address = null |
||
| 968 | ) { |
||
| 969 | $hook = HookUpdateUser::create(); |
||
| 970 | if (!empty($hook)) { |
||
| 971 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE); |
||
| 972 | } |
||
| 973 | $original_password = $password; |
||
| 974 | |||
| 975 | if ($user_id != strval(intval($user_id))) { |
||
| 976 | return false; |
||
| 977 | } |
||
| 978 | |||
| 979 | if (empty($user_id)) { |
||
| 980 | return false; |
||
| 981 | } |
||
| 982 | |||
| 983 | $userManager = self::getManager(); |
||
| 984 | /** @var Chamilo\UserBundle\Entity\User $user */ |
||
| 985 | $user = self::getRepository()->find($user_id); |
||
| 986 | |||
| 987 | if (empty($user)) { |
||
| 988 | return false; |
||
| 989 | } |
||
| 990 | |||
| 991 | if ($reset_password == 0) { |
||
| 992 | $password = null; |
||
| 993 | $auth_source = $user->getAuthSource(); |
||
| 994 | } elseif ($reset_password == 1) { |
||
| 995 | $original_password = $password = api_generate_password(); |
||
| 996 | $auth_source = PLATFORM_AUTH_SOURCE; |
||
| 997 | } elseif ($reset_password == 2) { |
||
| 998 | $password = $password; |
||
| 999 | $auth_source = PLATFORM_AUTH_SOURCE; |
||
| 1000 | } elseif ($reset_password == 3) { |
||
| 1001 | $password = $password; |
||
| 1002 | $auth_source = $auth_source; |
||
| 1003 | } |
||
| 1004 | |||
| 1005 | // Checking the user language |
||
| 1006 | $languages = api_get_platform_isocodes(); |
||
| 1007 | if (!in_array($language, $languages)) { |
||
| 1008 | $language = api_get_setting('platformLanguage'); |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | $change_active = 0; |
||
| 1012 | $isUserActive = $user->getActive(); |
||
| 1013 | if ($isUserActive != $active) { |
||
| 1014 | $change_active = 1; |
||
| 1015 | } |
||
| 1016 | |||
| 1017 | $originalUsername = $user->getUsername(); |
||
| 1018 | |||
| 1019 | // If username is different from original then check if it exists. |
||
| 1020 | if ($originalUsername !== $username) { |
||
| 1021 | $available = self::is_username_available($username); |
||
| 1022 | if ($available === false) { |
||
| 1023 | return false; |
||
| 1024 | } |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | if (!empty($expiration_date)) { |
||
| 1028 | $expiration_date = api_get_utc_datetime($expiration_date); |
||
| 1029 | $expiration_date = new \DateTime( |
||
| 1030 | $expiration_date, |
||
| 1031 | new DateTimeZone('UTC') |
||
| 1032 | ); |
||
| 1033 | } |
||
| 1034 | |||
| 1035 | $user |
||
| 1036 | ->setLastname($lastname) |
||
| 1037 | ->setFirstname($firstname) |
||
| 1038 | ->setUsername($username) |
||
| 1039 | ->setStatus($status) |
||
| 1040 | ->setAuthSource($auth_source) |
||
| 1041 | ->setLanguage($language) |
||
| 1042 | ->setEmail($email) |
||
| 1043 | ->setOfficialCode($official_code) |
||
| 1044 | ->setPhone($phone) |
||
| 1045 | ->setAddress($address) |
||
| 1046 | ->setPictureUri($picture_uri) |
||
| 1047 | ->setExpirationDate($expiration_date) |
||
| 1048 | ->setActive($active) |
||
| 1049 | ->setEnabled($active) |
||
| 1050 | ->setHrDeptId($hr_dept_id) |
||
| 1051 | ; |
||
| 1052 | |||
| 1053 | if (!is_null($password)) { |
||
| 1054 | $user->setPlainPassword($password); |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | $userManager->updateUser($user, true); |
||
| 1058 | |||
| 1059 | if ($change_active == 1) { |
||
| 1060 | if ($active == 1) { |
||
| 1061 | $event_title = LOG_USER_ENABLE; |
||
| 1062 | } else { |
||
| 1063 | $event_title = LOG_USER_DISABLE; |
||
| 1064 | } |
||
| 1065 | Event::addEvent($event_title, LOG_USER_ID, $user_id); |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | if (is_array($extra) && count($extra) > 0) { |
||
| 1069 | $res = true; |
||
| 1070 | foreach ($extra as $fname => $fvalue) { |
||
| 1071 | $res = $res && self::update_extra_field_value( |
||
| 1072 | $user_id, |
||
| 1073 | $fname, |
||
| 1074 | $fvalue |
||
| 1075 | ); |
||
| 1076 | } |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | if (!empty($email) && $send_email) { |
||
| 1080 | $recipient_name = api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS); |
||
| 1081 | $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName'); |
||
| 1082 | $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS); |
||
| 1083 | $email_admin = api_get_setting('emailAdministrator'); |
||
| 1084 | |||
| 1085 | if (api_is_multiple_url_enabled()) { |
||
| 1086 | $access_url_id = api_get_current_access_url_id(); |
||
| 1087 | if ($access_url_id != -1) { |
||
| 1088 | $url = api_get_access_url($access_url_id); |
||
| 1089 | $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n". |
||
| 1090 | get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n". |
||
| 1091 | get_lang('Username')." : ".$username.(($reset_password > 0) ? "\n". |
||
| 1092 | get_lang('Pass')." : ".stripslashes($original_password) : "")."\n\n". |
||
| 1093 | get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".$url['url']."\n\n". |
||
| 1094 | get_lang('Problem')."\n\n". |
||
| 1095 | get_lang('SignatureFormula').",\n\n". |
||
| 1096 | api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n". |
||
| 1097 | get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n". |
||
| 1098 | get_lang('Email')." : ".api_get_setting('emailAdministrator'); |
||
| 1099 | } |
||
| 1100 | } else { |
||
| 1101 | $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n". |
||
| 1102 | get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n". |
||
| 1103 | get_lang('Username')." : ".$username.(($reset_password > 0) ? "\n". |
||
| 1104 | get_lang('Pass')." : ".stripslashes($original_password) : "")."\n\n". |
||
| 1105 | get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".api_get_path(WEB_PATH)."\n\n". |
||
| 1106 | get_lang('Problem')."\n\n". |
||
| 1107 | get_lang('SignatureFormula').",\n\n". |
||
| 1108 | api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n". |
||
| 1109 | get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n". |
||
| 1110 | get_lang('Email')." : ".api_get_setting('emailAdministrator'); |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | $emailbody = nl2br($emailbody); |
||
| 1114 | api_mail_html( |
||
| 1115 | $recipient_name, |
||
| 1116 | $email, |
||
| 1117 | $emailsubject, |
||
| 1118 | $emailbody, |
||
| 1119 | $sender_name, |
||
| 1120 | $email_admin |
||
| 1121 | ); |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | if (!empty($hook)) { |
||
| 1125 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST); |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | $cacheAvailable = api_get_configuration_value('apc'); |
||
| 1129 | View Code Duplication | if ($cacheAvailable === true) { |
|
| 1130 | $apcVar = api_get_configuration_value('apc_prefix').'userinfo_'.$user_id; |
||
| 1131 | if (apcu_exists($apcVar)) { |
||
| 1132 | apcu_delete($apcVar); |
||
| 1133 | } |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | return $user->getId(); |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Disables or enables a user |
||
| 1141 | * @param int $user_id |
||
| 1142 | * @param int $active Enable or disable |
||
| 1143 | * @return void |
||
| 1144 | * @assert (-1,0) === false |
||
| 1145 | * @assert (1,1) === true |
||
| 1146 | */ |
||
| 1147 | private static function change_active_state($user_id, $active) |
||
| 1148 | { |
||
| 1149 | if (strval(intval($user_id)) != $user_id) { |
||
| 1150 | return false; |
||
| 1151 | } |
||
| 1152 | if ($user_id < 1) { |
||
| 1153 | return false; |
||
| 1154 | } |
||
| 1155 | $user_id = intval($user_id); |
||
| 1156 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 1157 | $sql = "UPDATE $table_user SET active = '$active' WHERE id = $user_id"; |
||
| 1158 | $r = Database::query($sql); |
||
| 1159 | $ev = LOG_USER_DISABLE; |
||
| 1160 | if ($active == 1) { |
||
| 1161 | $ev = LOG_USER_ENABLE; |
||
| 1162 | } |
||
| 1163 | if ($r !== false) { |
||
| 1164 | Event::addEvent($ev, LOG_USER_ID, $user_id); |
||
| 1165 | } |
||
| 1166 | |||
| 1167 | return $r; |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Disables a user |
||
| 1172 | * @param int User id |
||
| 1173 | * @return bool |
||
| 1174 | * @uses UserManager::change_active_state() to actually disable the user |
||
| 1175 | * @assert (0) === false |
||
| 1176 | */ |
||
| 1177 | public static function disable($user_id) |
||
| 1178 | { |
||
| 1179 | if (empty($user_id)) { |
||
| 1180 | return false; |
||
| 1181 | } |
||
| 1182 | self::change_active_state($user_id, 0); |
||
| 1183 | return true; |
||
| 1184 | } |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Enable a user |
||
| 1188 | * @param int User id |
||
| 1189 | * @return bool |
||
| 1190 | * @uses UserManager::change_active_state() to actually disable the user |
||
| 1191 | * @assert (0) === false |
||
| 1192 | */ |
||
| 1193 | public static function enable($user_id) |
||
| 1194 | { |
||
| 1195 | if (empty($user_id)) { |
||
| 1196 | return false; |
||
| 1197 | } |
||
| 1198 | self::change_active_state($user_id, 1); |
||
| 1199 | return true; |
||
| 1200 | } |
||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Returns the user's id based on the original id and field name in |
||
| 1204 | * the extra fields. Returns 0 if no user was found. This function is |
||
| 1205 | * mostly useful in the context of a web services-based sinchronization |
||
| 1206 | * @param string Original user id |
||
| 1207 | * @param string Original field name |
||
| 1208 | * @return int User id |
||
| 1209 | * @assert ('0','---') === 0 |
||
| 1210 | */ |
||
| 1211 | View Code Duplication | public static function get_user_id_from_original_id($original_user_id_value, $original_user_id_name) |
|
| 1212 | { |
||
| 1213 | $t_uf = Database::get_main_table(TABLE_EXTRA_FIELD); |
||
| 1214 | $t_ufv = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES); |
||
| 1215 | $extraFieldType = EntityExtraField::USER_FIELD_TYPE; |
||
| 1216 | $sql = "SELECT item_id as user_id |
||
| 1217 | FROM $t_uf uf |
||
| 1218 | INNER JOIN $t_ufv ufv |
||
| 1219 | ON ufv.field_id=uf.id |
||
| 1220 | WHERE |
||
| 1221 | variable='$original_user_id_name' AND |
||
| 1222 | value='$original_user_id_value' AND |
||
| 1223 | extra_field_type = $extraFieldType |
||
| 1224 | "; |
||
| 1225 | $res = Database::query($sql); |
||
| 1226 | $row = Database::fetch_object($res); |
||
| 1227 | if ($row) { |
||
| 1228 | return $row->user_id; |
||
| 1229 | } else { |
||
| 1230 | return 0; |
||
| 1231 | } |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Check if a username is available |
||
| 1236 | * @param string $username the wanted username |
||
| 1237 | * @return boolean true if the wanted username is available |
||
| 1238 | * @assert ('') === false |
||
| 1239 | * @assert ('xyzxyzxyz') === true |
||
| 1240 | */ |
||
| 1241 | View Code Duplication | public static function is_username_available($username) |
|
| 1242 | { |
||
| 1243 | if (empty($username)) { |
||
| 1244 | return false; |
||
| 1245 | } |
||
| 1246 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 1247 | $sql = "SELECT username FROM $table_user |
||
| 1248 | WHERE username = '".Database::escape_string($username)."'"; |
||
| 1249 | $res = Database::query($sql); |
||
| 1250 | |||
| 1251 | return Database::num_rows($res) == 0; |
||
| 1252 | } |
||
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Creates a username using person's names, i.e. creates jmontoya from Julio Montoya. |
||
| 1256 | * @param string $firstname The first name of the user. |
||
| 1257 | * @param string $lastname The last name of the user. |
||
| 1258 | * @return string Suggests a username that contains only ASCII-letters and digits, |
||
| 1259 | * without check for uniqueness within the system. |
||
| 1260 | * @author Julio Montoya Armas |
||
| 1261 | * @author Ivan Tcholakov, 2009 - rework about internationalization. |
||
| 1262 | * @assert ('','') === false |
||
| 1263 | * @assert ('a','b') === 'ab' |
||
| 1264 | */ |
||
| 1265 | public static function create_username($firstname, $lastname) |
||
| 1266 | { |
||
| 1267 | if (empty($firstname) && empty($lastname)) { |
||
| 1268 | return false; |
||
| 1269 | } |
||
| 1270 | |||
| 1271 | $firstname = api_substr(preg_replace(USERNAME_PURIFIER, '', $firstname), 0, 1); // The first letter only. |
||
| 1272 | //Looking for a space in the lastname |
||
| 1273 | $pos = api_strpos($lastname, ' '); |
||
| 1274 | if ($pos !== false) { |
||
| 1275 | $lastname = api_substr($lastname, 0, $pos); |
||
| 1276 | } |
||
| 1277 | |||
| 1278 | $lastname = preg_replace(USERNAME_PURIFIER, '', $lastname); |
||
| 1279 | $username = $firstname.$lastname; |
||
| 1280 | if (empty($username)) { |
||
| 1281 | $username = 'user'; |
||
| 1282 | } |
||
| 1283 | |||
| 1284 | $username = URLify::transliterate($username); |
||
| 1285 | |||
| 1286 | return strtolower(substr($username, 0, USERNAME_MAX_LENGTH - 3)); |
||
| 1287 | } |
||
| 1288 | |||
| 1289 | /** |
||
| 1290 | * Creates a unique username, using: |
||
| 1291 | * 1. the first name and the last name of a user; |
||
| 1292 | * 2. an already created username but not checked for uniqueness yet. |
||
| 1293 | * @param string $firstname The first name of a given user. If the second parameter $lastname is NULL, then this |
||
| 1294 | * parameter is treated as username which is to be checked for uniqueness and to be modified when it is necessary. |
||
| 1295 | * @param string $lastname The last name of the user. |
||
| 1296 | * @return string Returns a username that contains only ASCII-letters and digits, and that is unique within the system. |
||
| 1297 | * Note: When the method is called several times with same parameters, its results look like the following sequence: ivan, ivan2, ivan3, ivan4, ... |
||
| 1298 | * @author Ivan Tcholakov, 2009 |
||
| 1299 | */ |
||
| 1300 | public static function create_unique_username($firstname, $lastname = null) |
||
| 1301 | { |
||
| 1302 | if (is_null($lastname)) { |
||
| 1303 | // In this case the actual input parameter $firstname should contain ASCII-letters and digits only. |
||
| 1304 | // For making this method tolerant of mistakes, let us transliterate and purify the suggested input username anyway. |
||
| 1305 | // So, instead of the sentence $username = $firstname; we place the following: |
||
| 1306 | $username = strtolower(preg_replace(USERNAME_PURIFIER, '', $firstname)); |
||
| 1307 | } else { |
||
| 1308 | $username = self::create_username($firstname, $lastname); |
||
| 1309 | } |
||
| 1310 | if (!self::is_username_available($username)) { |
||
| 1311 | $i = 2; |
||
| 1312 | $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string) $i)).$i; |
||
| 1313 | while (!self::is_username_available($temp_username)) { |
||
| 1314 | $i++; |
||
| 1315 | $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string) $i)).$i; |
||
| 1316 | } |
||
| 1317 | $username = $temp_username; |
||
| 1318 | } |
||
| 1319 | |||
| 1320 | $username = URLify::transliterate($username); |
||
| 1321 | |||
| 1322 | return $username; |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Modifies a given username accordingly to the specification for valid characters and length. |
||
| 1327 | * @param $username string The input username. |
||
| 1328 | * @param bool $strict (optional) When this flag is TRUE, the result is guaranteed for full compliance, |
||
| 1329 | * otherwise compliance may be partial. The default value is FALSE. |
||
| 1330 | * @return string The resulting purified username. |
||
| 1331 | */ |
||
| 1332 | public static function purify_username($username, $strict = false) |
||
| 1333 | { |
||
| 1334 | if ($strict) { |
||
| 1335 | // 1. Conversion of unacceptable letters (latinian letters with accents for example) |
||
| 1336 | // into ASCII letters in order they not to be totally removed. |
||
| 1337 | // 2. Applying the strict purifier. |
||
| 1338 | // 3. Length limitation. |
||
| 1339 | $return = api_get_setting('login_is_email') === 'true' ? substr(preg_replace(USERNAME_PURIFIER_MAIL, '', $username), 0, USERNAME_MAX_LENGTH) : substr(preg_replace(USERNAME_PURIFIER, '', $username), 0, USERNAME_MAX_LENGTH); |
||
| 1340 | $return = URLify::transliterate($return); |
||
| 1341 | |||
| 1342 | return $return; |
||
| 1343 | } |
||
| 1344 | |||
| 1345 | // 1. Applying the shallow purifier. |
||
| 1346 | // 2. Length limitation. |
||
| 1347 | return substr(preg_replace(USERNAME_PURIFIER_SHALLOW, '', $username), 0, USERNAME_MAX_LENGTH); |
||
| 1348 | } |
||
| 1349 | |||
| 1350 | /** |
||
| 1351 | * Checks whether the user id exists in the database |
||
| 1352 | * |
||
| 1353 | * @param int User id |
||
| 1354 | * @return bool True if user id was found, false otherwise |
||
| 1355 | */ |
||
| 1356 | View Code Duplication | public static function is_user_id_valid($userId) |
|
| 1357 | { |
||
| 1358 | $resultData = Database::select( |
||
| 1359 | 'COUNT(1) AS count', |
||
| 1360 | Database::get_main_table(TABLE_MAIN_USER), |
||
| 1361 | [ |
||
| 1362 | 'where' => ['id = ?' => intval($userId)] |
||
| 1363 | ], |
||
| 1364 | 'first' |
||
| 1365 | ); |
||
| 1366 | |||
| 1367 | if ($resultData === false) { |
||
| 1368 | return false; |
||
| 1369 | } |
||
| 1370 | |||
| 1371 | return $resultData['count'] > 0; |
||
| 1372 | } |
||
| 1373 | |||
| 1374 | /** |
||
| 1375 | * Checks whether a given username matches to the specification strictly. The empty username is assumed here as invalid. |
||
| 1376 | * Mostly this function is to be used in the user interface built-in validation routines for providing feedback while usernames are enterd manually. |
||
| 1377 | * @param string $username The input username. |
||
| 1378 | * @return bool Returns TRUE if the username is valid, FALSE otherwise. |
||
| 1379 | */ |
||
| 1380 | public static function is_username_valid($username) |
||
| 1381 | { |
||
| 1382 | return !empty($username) && $username == self::purify_username($username, true); |
||
| 1383 | } |
||
| 1384 | |||
| 1385 | /** |
||
| 1386 | * Checks whether a username is empty. If the username contains whitespace characters, such as spaces, tabulators, newlines, etc., |
||
| 1387 | * it is assumed as empty too. This function is safe for validation unpurified data (during importing). |
||
| 1388 | * @param string $username The given username. |
||
| 1389 | * @return bool Returns TRUE if length of the username exceeds the limit, FALSE otherwise. |
||
| 1390 | */ |
||
| 1391 | public static function is_username_empty($username) |
||
| 1392 | { |
||
| 1393 | return (strlen(self::purify_username($username, false)) == 0); |
||
| 1394 | } |
||
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Checks whether a username is too long or not. |
||
| 1398 | * @param string $username The given username, it should contain only ASCII-letters and digits. |
||
| 1399 | * @return bool Returns TRUE if length of the username exceeds the limit, FALSE otherwise. |
||
| 1400 | */ |
||
| 1401 | public static function is_username_too_long($username) |
||
| 1402 | { |
||
| 1403 | return (strlen($username) > USERNAME_MAX_LENGTH); |
||
| 1404 | } |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Get the users by ID |
||
| 1408 | * @param array $ids student ids |
||
| 1409 | * @param string $active |
||
| 1410 | * @param string $order |
||
| 1411 | * @param string $limit |
||
| 1412 | * @return array $result student information |
||
| 1413 | */ |
||
| 1414 | public static function get_user_list_by_ids($ids = array(), $active = null, $order = null, $limit = null) |
||
| 1415 | { |
||
| 1416 | if (empty($ids)) { |
||
| 1417 | return array(); |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | $ids = is_array($ids) ? $ids : array($ids); |
||
| 1421 | $ids = array_map('intval', $ids); |
||
| 1422 | $ids = implode(',', $ids); |
||
| 1423 | |||
| 1424 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 1425 | $sql = "SELECT * FROM $tbl_user WHERE id IN ($ids)"; |
||
| 1426 | if (!is_null($active)) { |
||
| 1427 | $sql .= ' AND active='.($active ? '1' : '0'); |
||
| 1428 | } |
||
| 1429 | |||
| 1430 | if (!is_null($order)) { |
||
| 1431 | $order = Database::escape_string($order); |
||
| 1432 | $sql .= ' ORDER BY '.$order; |
||
| 1433 | } |
||
| 1434 | |||
| 1435 | if (!is_null($limit)) { |
||
| 1436 | $limit = Database::escape_string($limit); |
||
| 1437 | $sql .= ' LIMIT '.$limit; |
||
| 1438 | } |
||
| 1439 | |||
| 1440 | $rs = Database::query($sql); |
||
| 1441 | $result = array(); |
||
| 1442 | while ($row = Database::fetch_array($rs)) { |
||
| 1443 | $result[] = $row; |
||
| 1444 | } |
||
| 1445 | |||
| 1446 | return $result; |
||
| 1447 | } |
||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * Get a list of users of which the given conditions match with an = 'cond' |
||
| 1451 | * @param array $conditions a list of condition (example : status=>STUDENT) |
||
| 1452 | * @param array $order_by a list of fields on which sort |
||
| 1453 | * @return array An array with all users of the platform. |
||
| 1454 | * @todo optional course code parameter, optional sorting parameters... |
||
| 1455 | * @todo security filter order by |
||
| 1456 | */ |
||
| 1457 | public static function get_user_list( |
||
| 1498 | |||
| 1499 | /** |
||
| 1500 | * Get a list of users of which the given conditions match with a LIKE '%cond%' |
||
| 1501 | * @param array $conditions a list of condition (exemple : status=>STUDENT) |
||
| 1502 | * @param array $order_by a list of fields on which sort |
||
| 1503 | * @return array An array with all users of the platform. |
||
| 1504 | * @todo optional course code parameter, optional sorting parameters... |
||
| 1505 | * @todo security filter order_by |
||
| 1506 | */ |
||
| 1507 | public static function get_user_list_like( |
||
| 1558 | |||
| 1559 | /** |
||
| 1560 | * Get user picture URL or path from user ID (returns an array). |
||
| 1561 | * The return format is a complete path, enabling recovery of the directory |
||
| 1562 | * with dirname() or the file with basename(). This also works for the |
||
| 1563 | * functions dealing with the user's productions, as they are located in |
||
| 1564 | * the same directory. |
||
| 1565 | * @param integer $id User ID |
||
| 1566 | * @param string $type Type of path to return (can be 'system', 'web') |
||
| 1567 | * @param array $userInfo user information to avoid query the DB |
||
| 1568 | * returns the /main/img/unknown.jpg image set it at true |
||
| 1569 | * |
||
| 1570 | * @return array Array of 2 elements: 'dir' and 'file' which contain |
||
| 1571 | * the dir and file as the name implies if image does not exist it will |
||
| 1572 | * return the unknow image if anonymous parameter is true if not it returns an empty array |
||
| 1573 | */ |
||
| 1574 | View Code Duplication | public static function get_user_picture_path_by_id($id, $type = 'web', $userInfo = []) |
|
| 1624 | |||
| 1625 | /** |
||
| 1626 | * *** READ BEFORE REVIEW THIS FUNCTION *** |
||
| 1627 | * This function is a exact copy from get_user_picture_path_by_id() and it was create it to avoid |
||
| 1628 | * a recursive calls for get_user_picture_path_by_id() in another functions when you update a user picture |
||
| 1629 | * in same script, so you can find this function usage in update_user_picture() function. |
||
| 1630 | * |
||
| 1631 | * @param integer $id User ID |
||
| 1632 | * @param string $type Type of path to return (can be 'system', 'web') |
||
| 1633 | * @param array $userInfo user information to avoid query the DB |
||
| 1634 | * returns the /main/img/unknown.jpg image set it at true |
||
| 1635 | * |
||
| 1636 | * @return array Array of 2 elements: 'dir' and 'file' which contain |
||
| 1637 | * the dir and file as the name implies if image does not exist it will |
||
| 1638 | * return the unknown image if anonymous parameter is true if not it returns an empty array |
||
| 1639 | */ |
||
| 1640 | View Code Duplication | public static function getUserPicturePathById($id, $type = 'web', $userInfo = []) |
|
| 1691 | |||
| 1692 | /** |
||
| 1693 | * Get user path from user ID (returns an array). |
||
| 1694 | * The return format is a complete path to a folder ending with "/" |
||
| 1695 | * In case the first level of subdirectory of users/ does not exist, the |
||
| 1696 | * function will attempt to create it. Probably not the right place to do it |
||
| 1697 | * but at least it avoids headaches in many other places. |
||
| 1698 | * @param integer $id User ID |
||
| 1699 | * @param string $type Type of path to return (can be 'system', 'web', 'last') |
||
| 1700 | * @return string User folder path (i.e. /var/www/chamilo/app/upload/users/1/1/) |
||
| 1701 | */ |
||
| 1702 | public static function getUserPathById($id, $type) |
||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Gets the current user image |
||
| 1742 | * @param string $user_id |
||
| 1743 | * @param int $size it can be USER_IMAGE_SIZE_SMALL, |
||
| 1744 | * USER_IMAGE_SIZE_MEDIUM, USER_IMAGE_SIZE_BIG or USER_IMAGE_SIZE_ORIGINAL |
||
| 1745 | * @param bool $addRandomId |
||
| 1746 | * @param array $userInfo to avoid query the DB |
||
| 1747 | * |
||
| 1748 | * @return string |
||
| 1749 | */ |
||
| 1750 | public static function getUserPicture( |
||
| 1839 | |||
| 1840 | /** |
||
| 1841 | * Creates new user photos in various sizes of a user, or deletes user photos. |
||
| 1842 | * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php |
||
| 1843 | * @param int $user_id The user internal identification number. |
||
| 1844 | * @param string $file The common file name for the newly created photos. |
||
| 1845 | * It will be checked and modified for compatibility with the file system. |
||
| 1846 | * If full name is provided, path component is ignored. |
||
| 1847 | * If an empty name is provided, then old user photos are deleted only, |
||
| 1848 | * @see UserManager::delete_user_picture() as the prefered way for deletion. |
||
| 1849 | * @param string $source_file The full system name of the image from which user photos will be created. |
||
| 1850 | * @param string $cropParameters Optional string that contents "x,y,width,height" of a cropped image format |
||
| 1851 | * @return mixed Returns the resulting common file name of created images which usually should be stored in database. |
||
| 1852 | * When deletion is requested returns empty string. In case of internal error or negative validation returns FALSE. |
||
| 1853 | */ |
||
| 1854 | public static function update_user_picture($user_id, $file = null, $source_file = null, $cropParameters = '') |
||
| 1951 | |||
| 1952 | /** |
||
| 1953 | * Update User extra field file type into {user_folder}/{$extra_field} |
||
| 1954 | * @param int $user_id The user internal identification number |
||
| 1955 | * @param string $extra_field The $extra_field The extra field name |
||
| 1956 | * @param null $file The filename |
||
| 1957 | * @param null $source_file The temporal filename |
||
| 1958 | * @return bool|null return filename if success, but false |
||
| 1959 | */ |
||
| 1960 | public static function update_user_extra_file($user_id, $extra_field = '', $file = null, $source_file = null) |
||
| 1996 | |||
| 1997 | |||
| 1998 | /** |
||
| 1999 | * Deletes user photos. |
||
| 2000 | * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php |
||
| 2001 | * @param int $user_id The user internal identification number. |
||
| 2002 | * @return mixed Returns empty string on success, FALSE on error. |
||
| 2003 | */ |
||
| 2004 | public static function delete_user_picture($user_id) |
||
| 2008 | |||
| 2009 | /** |
||
| 2010 | * Returns an XHTML formatted list of productions for a user, or FALSE if he |
||
| 2011 | * doesn't have any. |
||
| 2012 | * |
||
| 2013 | * If there has been a request to remove a production, the function will return |
||
| 2014 | * without building the list unless forced to do so by the optional second |
||
| 2015 | * parameter. This increases performance by avoiding to read through the |
||
| 2016 | * productions on the filesystem before the removal request has been carried |
||
| 2017 | * out because they'll have to be re-read afterwards anyway. |
||
| 2018 | * |
||
| 2019 | * @param int $user_id User id |
||
| 2020 | * @param bool $force Optional parameter to force building after a removal request |
||
| 2021 | * @param bool $showDelete |
||
| 2022 | * |
||
| 2023 | * @return string A string containing the XHTML code to display the production list, or FALSE |
||
| 2024 | */ |
||
| 2025 | public static function build_production_list($user_id, $force = false, $showDelete = false) |
||
| 2057 | |||
| 2058 | /** |
||
| 2059 | * Returns an array with the user's productions. |
||
| 2060 | * |
||
| 2061 | * @param $user_id User id |
||
| 2062 | * @return array An array containing the user's productions |
||
| 2063 | */ |
||
| 2064 | public static function get_user_productions($user_id) |
||
| 2091 | |||
| 2092 | /** |
||
| 2093 | * Remove a user production. |
||
| 2094 | * |
||
| 2095 | * @param int $user_id User id |
||
| 2096 | * @param string $production The production to remove |
||
| 2097 | */ |
||
| 2098 | public static function remove_user_production($user_id, $production) |
||
| 2108 | |||
| 2109 | /** |
||
| 2110 | * Update an extra field value for a given user |
||
| 2111 | * @param integer $userId User ID |
||
| 2112 | * @param string $variable Field variable name |
||
| 2113 | * @param string $value Field value |
||
| 2114 | * |
||
| 2115 | * @return boolean true if field updated, false otherwise |
||
| 2116 | */ |
||
| 2117 | View Code Duplication | public static function update_extra_field_value($userId, $variable, $value = '') |
|
| 2128 | |||
| 2129 | /** |
||
| 2130 | * Get an array of extra fields with field details (type, default value and options) |
||
| 2131 | * @param integer Offset (from which row) |
||
| 2132 | * @param integer Number of items |
||
| 2133 | * @param integer Column on which sorting is made |
||
| 2134 | * @param string Sorting direction |
||
| 2135 | * @param boolean Optional. Whether we get all the fields or just the visible ones |
||
| 2136 | * @param int Optional. Whether we get all the fields with field_filter 1 or 0 or everything |
||
| 2137 | * @return array Extra fields details (e.g. $list[2]['type'], $list[4]['options'][2]['title'] |
||
| 2138 | */ |
||
| 2139 | public static function get_extra_fields( |
||
| 2213 | |||
| 2214 | /** |
||
| 2215 | * Build a list of extra file already uploaded in $user_folder/{$extra_field}/ |
||
| 2216 | * @param $user_id |
||
| 2217 | * @param $extra_field |
||
| 2218 | * @param bool $force |
||
| 2219 | * @param bool $showDelete |
||
| 2220 | * @return bool|string |
||
| 2221 | */ |
||
| 2222 | public static function build_user_extra_file_list($user_id, $extra_field, $force = false, $showDelete = false) |
||
| 2253 | |||
| 2254 | /** |
||
| 2255 | * Get valid filenames in $user_folder/{$extra_field}/ |
||
| 2256 | * @param $user_id |
||
| 2257 | * @param $extra_field |
||
| 2258 | * @param bool $full_path |
||
| 2259 | * @return array |
||
| 2260 | */ |
||
| 2261 | public static function get_user_extra_files($user_id, $extra_field, $full_path = false) |
||
| 2291 | |||
| 2292 | /** |
||
| 2293 | * Remove an {$extra_file} from the user folder $user_folder/{$extra_field}/ |
||
| 2294 | * @param $user_id |
||
| 2295 | * @param $extra_field |
||
| 2296 | * @param $extra_file |
||
| 2297 | * @return bool |
||
| 2298 | */ |
||
| 2299 | public static function remove_user_extra_file($user_id, $extra_field, $extra_file) |
||
| 2314 | |||
| 2315 | /** |
||
| 2316 | * Creates a new extra field |
||
| 2317 | * @param string $variable Field's internal variable name |
||
| 2318 | * @param int $fieldType Field's type |
||
| 2319 | * @param string $displayText Field's language var name |
||
| 2320 | * @param string $default Field's default value |
||
| 2321 | * @return int |
||
| 2322 | */ |
||
| 2323 | View Code Duplication | public static function create_extra_field($variable, $fieldType, $displayText, $default) |
|
| 2335 | |||
| 2336 | /** |
||
| 2337 | * Check if a field is available |
||
| 2338 | * @param string $variable |
||
| 2339 | * @return boolean |
||
| 2340 | */ |
||
| 2341 | public static function is_extra_field_available($variable) |
||
| 2348 | |||
| 2349 | /** |
||
| 2350 | * Gets user extra fields data |
||
| 2351 | * @param integer User ID |
||
| 2352 | * @param boolean Whether to prefix the fields indexes with "extra_" (might be used by formvalidator) |
||
| 2353 | * @param boolean Whether to return invisible fields as well |
||
| 2354 | * @param boolean Whether to split multiple-selection fields or not |
||
| 2355 | * @return array Array of fields => value for the given user |
||
| 2356 | */ |
||
| 2357 | public static function get_extra_user_data( |
||
| 2444 | |||
| 2445 | /** Get extra user data by field |
||
| 2446 | * @param int user ID |
||
| 2447 | * @param string the internal variable name of the field |
||
| 2448 | * @return array with extra data info of a user i.e array('field_variable'=>'value'); |
||
| 2449 | */ |
||
| 2450 | public static function get_extra_user_data_by_field( |
||
| 2509 | |||
| 2510 | /** |
||
| 2511 | * Get the extra field information for a certain field (the options as well) |
||
| 2512 | * @param int $variable The name of the field we want to know everything about |
||
| 2513 | * @return array Array containing all the information about the extra profile field |
||
| 2514 | * (first level of array contains field details, then 'options' sub-array contains options details, |
||
| 2515 | * as returned by the database) |
||
| 2516 | * @author Julio Montoya |
||
| 2517 | * @since v1.8.6 |
||
| 2518 | */ |
||
| 2519 | public static function get_extra_field_information_by_name($variable) |
||
| 2525 | |||
| 2526 | /** |
||
| 2527 | * Get the extra field information for user tag (the options as well) |
||
| 2528 | * @param int $variable The name of the field we want to know everything about |
||
| 2529 | * @return array Array containing all the information about the extra profile field |
||
| 2530 | * (first level of array contains field details, then 'options' sub-array contains options details, |
||
| 2531 | * as returned by the database) |
||
| 2532 | * @author José Loguercio |
||
| 2533 | * @since v1.11.0 |
||
| 2534 | */ |
||
| 2535 | public static function get_extra_field_tags_information_by_name($variable) |
||
| 2541 | |||
| 2542 | /** |
||
| 2543 | * @param string $type |
||
| 2544 | * |
||
| 2545 | * @return array |
||
| 2546 | */ |
||
| 2547 | public static function get_all_extra_field_by_type($type) |
||
| 2553 | |||
| 2554 | /** |
||
| 2555 | * Get all the extra field information of a certain field (also the options) |
||
| 2556 | * |
||
| 2557 | * @param int $fieldId the ID of the field we want to know everything of |
||
| 2558 | * @return array $return containing all th information about the extra profile field |
||
| 2559 | * @author Julio Montoya |
||
| 2560 | * @deprecated |
||
| 2561 | * @since v1.8.6 |
||
| 2562 | */ |
||
| 2563 | public static function get_extra_field_information($fieldId) |
||
| 2569 | |||
| 2570 | /** |
||
| 2571 | * Get extra user data by value |
||
| 2572 | * @param string $variable the internal variable name of the field |
||
| 2573 | * @param string $value the internal value of the field |
||
| 2574 | * @param bool $all_visibility |
||
| 2575 | * |
||
| 2576 | * @return array with extra data info of a user i.e array('field_variable'=>'value'); |
||
| 2577 | */ |
||
| 2578 | public static function get_extra_user_data_by_value($variable, $value, $all_visibility = true) |
||
| 2606 | |||
| 2607 | /** |
||
| 2608 | * Get extra user data by tags value |
||
| 2609 | * |
||
| 2610 | * @param int $fieldId the ID of the field we want to know everything of |
||
| 2611 | * @param string $tag the tag name for search |
||
| 2612 | * @return array with extra data info of a user |
||
| 2613 | * @author José Loguercio |
||
| 2614 | * @since v1.11.0 |
||
| 2615 | */ |
||
| 2616 | public static function get_extra_user_data_by_tags($fieldId, $tag) |
||
| 2626 | |||
| 2627 | /** |
||
| 2628 | * Get extra user data by field variable |
||
| 2629 | * @param string $variable field variable |
||
| 2630 | * @return array data |
||
| 2631 | */ |
||
| 2632 | public static function get_extra_user_data_by_field_variable($variable) |
||
| 2649 | |||
| 2650 | /** |
||
| 2651 | * Get extra user data tags by field variable |
||
| 2652 | * |
||
| 2653 | * @param string $variable field variable |
||
| 2654 | * @return array |
||
| 2655 | */ |
||
| 2656 | public static function get_extra_user_data_for_tags($variable) |
||
| 2662 | |||
| 2663 | /** |
||
| 2664 | * Gives a list of [session_category][session_id] for the current user. |
||
| 2665 | * @param integer $user_id |
||
| 2666 | * @param boolean $is_time_over whether to fill the first element or not (to give space for courses out of categories) |
||
| 2667 | * @param boolean $ignore_visibility_for_admins optional true if limit time from session is over, false otherwise |
||
| 2668 | * @param boolean $ignoreTimeLimit ignore time start/end |
||
| 2669 | * @return array list of statuses [session_category][session_id] |
||
| 2670 | * |
||
| 2671 | * @todo ensure multiple access urls are managed correctly |
||
| 2672 | */ |
||
| 2673 | public static function get_sessions_by_category( |
||
| 2674 | $user_id, |
||
| 2675 | $is_time_over = true, |
||
| 2676 | $ignore_visibility_for_admins = false, |
||
| 2677 | $ignoreTimeLimit = false |
||
| 2678 | ) { |
||
| 2679 | if ($user_id != strval(intval($user_id))) { |
||
| 2680 | return array(); |
||
| 2681 | } |
||
| 2682 | |||
| 2683 | // Get the list of sessions per user |
||
| 2849 | |||
| 2850 | /** |
||
| 2851 | * Gives a list of [session_id-course_code] => [status] for the current user. |
||
| 2852 | * @param integer $user_id |
||
| 2853 | * @param int $sessionLimit |
||
| 2854 | * @return array list of statuses (session_id-course_code => status) |
||
| 2855 | */ |
||
| 2856 | public static function get_personal_session_course_list($user_id, $sessionLimit = null) |
||
| 3068 | |||
| 3069 | /** |
||
| 3070 | * Gives a list of courses for the given user in the given session |
||
| 3071 | * @param integer $user_id |
||
| 3072 | * @param integer $session_id |
||
| 3073 | * @return array list of statuses (session_id-course_code => status) |
||
| 3074 | */ |
||
| 3075 | public static function get_courses_list_by_session($user_id, $session_id) |
||
| 3196 | |||
| 3197 | /** |
||
| 3198 | * Get user id from a username |
||
| 3199 | * @param string $username |
||
| 3200 | * @return int User ID (or false if not found) |
||
| 3201 | */ |
||
| 3202 | public static function get_user_id_from_username($username) |
||
| 3224 | |||
| 3225 | /** |
||
| 3226 | * Get the users files upload from his share_folder |
||
| 3227 | * @param string $user_id User ID |
||
| 3228 | * @param string $course course directory |
||
| 3229 | * @param string $resourcetype resourcetype: images, all |
||
| 3230 | * @return int User ID (or false if not found) |
||
| 3231 | */ |
||
| 3232 | public static function get_user_upload_files_by_course($user_id, $course, $resourcetype = 'all') |
||
| 3277 | |||
| 3278 | /** |
||
| 3279 | * Gets the API key (or keys) and return them into an array |
||
| 3280 | * @param int Optional user id (defaults to the result of api_get_user_id()) |
||
| 3281 | * @return array Non-indexed array containing the list of API keys for this user, or FALSE on error |
||
| 3282 | */ |
||
| 3283 | public static function get_api_keys($user_id = null, $api_service = 'dokeos') |
||
| 3310 | |||
| 3311 | /** |
||
| 3312 | * Adds a new API key to the users' account |
||
| 3313 | * @param int Optional user ID (defaults to the results of api_get_user_id()) |
||
| 3314 | * @return boolean True on success, false on failure |
||
| 3315 | */ |
||
| 3316 | public static function add_api_key($user_id = null, $api_service = 'dokeos') |
||
| 3338 | |||
| 3339 | /** |
||
| 3340 | * Deletes an API key from the user's account |
||
| 3341 | * @param int API key's internal ID |
||
| 3342 | * @return boolean True on success, false on failure |
||
| 3343 | */ |
||
| 3344 | public static function delete_api_key($key_id) |
||
| 3364 | |||
| 3365 | /** |
||
| 3366 | * Regenerate an API key from the user's account |
||
| 3367 | * @param int user ID (defaults to the results of api_get_user_id()) |
||
| 3368 | * @param string API key's internal ID |
||
| 3369 | * @return int num |
||
| 3370 | */ |
||
| 3371 | public static function update_api_key($user_id, $api_service) |
||
| 3394 | |||
| 3395 | /** |
||
| 3396 | * @param int user ID (defaults to the results of api_get_user_id()) |
||
| 3397 | * @param string API key's internal ID |
||
| 3398 | * @return int row ID, or return false if not found |
||
| 3399 | */ |
||
| 3400 | public static function get_api_key_id($user_id, $api_service) |
||
| 3418 | |||
| 3419 | /** |
||
| 3420 | * Checks if a user_id is platform admin |
||
| 3421 | * @param int user ID |
||
| 3422 | * @return boolean True if is admin, false otherwise |
||
| 3423 | * @see main_api.lib.php::api_is_platform_admin() for a context-based check |
||
| 3424 | */ |
||
| 3425 | View Code Duplication | public static function is_admin($user_id) |
|
| 3436 | |||
| 3437 | /** |
||
| 3438 | * Get the total count of users |
||
| 3439 | * @param int Status of users to be counted |
||
| 3440 | * @param int Access URL ID (optional) |
||
| 3441 | * @return mixed Number of users or false on error |
||
| 3442 | */ |
||
| 3443 | public static function get_number_of_users($status = 0, $access_url_id = 1) |
||
| 3462 | |||
| 3463 | /** |
||
| 3464 | * @author Isaac flores <[email protected]> |
||
| 3465 | * @param string The email administrator |
||
| 3466 | * @param integer The user id |
||
| 3467 | * @param string The message title |
||
| 3468 | * @param string The content message |
||
| 3469 | */ |
||
| 3470 | public static function send_message_in_outbox($email_administrator, $user_id, $title, $content) |
||
| 3494 | |||
| 3495 | /** |
||
| 3496 | * |
||
| 3497 | * Gets the tags of a specific field_id |
||
| 3498 | * USER TAGS |
||
| 3499 | * |
||
| 3500 | * Instructions to create a new user tag by Julio Montoya <[email protected]> |
||
| 3501 | * |
||
| 3502 | * 1. Create a new extra field in main/admin/user_fields.php with the "TAG" field type make it available and visible. |
||
| 3503 | * Called it "books" for example. |
||
| 3504 | * 2. Go to profile main/auth/profile.php There you will see a special input (facebook style) that will show suggestions of tags. |
||
| 3505 | * 3. All the tags are registered in the user_tag table and the relationship between user and tags is in the user_rel_tag table |
||
| 3506 | * 4. Tags are independent this means that tags can't be shared between tags + book + hobbies. |
||
| 3507 | * 5. Test and enjoy. |
||
| 3508 | * |
||
| 3509 | * @param string $tag |
||
| 3510 | * @param int $field_id field_id |
||
| 3511 | * @param string $return_format how we are going to result value in array or in a string (json) |
||
| 3512 | * @param $limit |
||
| 3513 | * |
||
| 3514 | * @return mixed |
||
| 3515 | * |
||
| 3516 | */ |
||
| 3517 | public static function get_tags($tag, $field_id, $return_format = 'json', $limit = 10) |
||
| 3541 | |||
| 3542 | /** |
||
| 3543 | * @param int $field_id |
||
| 3544 | * @param int $limit |
||
| 3545 | * |
||
| 3546 | * @return array |
||
| 3547 | */ |
||
| 3548 | public static function get_top_tags($field_id, $limit = 100) |
||
| 3572 | |||
| 3573 | /** |
||
| 3574 | * Get user's tags |
||
| 3575 | * @param int $user_id |
||
| 3576 | * @param int $field_id |
||
| 3577 | * |
||
| 3578 | * @return array |
||
| 3579 | */ |
||
| 3580 | public static function get_user_tags($user_id, $field_id) |
||
| 3605 | |||
| 3606 | /** |
||
| 3607 | * Get user's tags |
||
| 3608 | * @param int $user_id |
||
| 3609 | * @param int $field_id |
||
| 3610 | * @param bool $show_links show links or not |
||
| 3611 | * |
||
| 3612 | * @return array |
||
| 3613 | */ |
||
| 3614 | public static function get_user_tags_to_string($user_id, $field_id, $show_links = true) |
||
| 3655 | |||
| 3656 | /** |
||
| 3657 | * Get the tag id |
||
| 3658 | * @param int $tag |
||
| 3659 | * @param int $field_id |
||
| 3660 | * @return int returns 0 if fails otherwise the tag id |
||
| 3661 | */ |
||
| 3662 | public static function get_tag_id($tag, $field_id) |
||
| 3680 | |||
| 3681 | /** |
||
| 3682 | * Get the tag id |
||
| 3683 | * @param int $tag_id |
||
| 3684 | * @param int $field_id |
||
| 3685 | * |
||
| 3686 | * @return int 0 if fails otherwise the tag id |
||
| 3687 | */ |
||
| 3688 | View Code Duplication | public static function get_tag_id_from_id($tag_id, $field_id) |
|
| 3703 | |||
| 3704 | /** |
||
| 3705 | * Adds a user-tag value |
||
| 3706 | * @param mixed $tag |
||
| 3707 | * @param int $user_id |
||
| 3708 | * @param int $field_id field id of the tag |
||
| 3709 | * @return bool |
||
| 3710 | */ |
||
| 3711 | public static function add_tag($tag, $user_id, $field_id) |
||
| 3765 | |||
| 3766 | /** |
||
| 3767 | * Deletes an user tag |
||
| 3768 | * @param int $user_id |
||
| 3769 | * @param int $field_id |
||
| 3770 | * |
||
| 3771 | */ |
||
| 3772 | public static function delete_user_tags($user_id, $field_id) |
||
| 3790 | |||
| 3791 | /** |
||
| 3792 | * Process the tag list comes from the UserManager::update_extra_field_value() function |
||
| 3793 | * @param array $tags the tag list that will be added |
||
| 3794 | * @param int $user_id |
||
| 3795 | * @param int $field_id |
||
| 3796 | * |
||
| 3797 | * @return bool |
||
| 3798 | */ |
||
| 3799 | public static function process_tags($tags, $user_id, $field_id) |
||
| 3812 | |||
| 3813 | /** |
||
| 3814 | * Returns a list of all administrators |
||
| 3815 | * |
||
| 3816 | * @return array |
||
| 3817 | */ |
||
| 3818 | public static function get_all_administrators() |
||
| 3848 | |||
| 3849 | /** |
||
| 3850 | * Search an user (tags, first name, last name and email ) |
||
| 3851 | * @param string $tag |
||
| 3852 | * @param int $field_id field id of the tag |
||
| 3853 | * @param int $from where to start in the query |
||
| 3854 | * @param int $number_of_items |
||
| 3855 | * @param bool $getCount get count or not |
||
| 3856 | * @return array |
||
| 3857 | */ |
||
| 3858 | public static function get_all_user_tags( |
||
| 3942 | |||
| 3943 | /** |
||
| 3944 | * Get extra filtrable user fields (only type select) |
||
| 3945 | * @return array |
||
| 3946 | */ |
||
| 3947 | public static function get_extra_filtrable_fields() |
||
| 3969 | |||
| 3970 | /** |
||
| 3971 | * Get extra where clauses for finding users based on extra filtrable user fields (type select) |
||
| 3972 | * @return string With AND clauses based on user's ID which have the values to search in extra user fields |
||
| 3973 | */ |
||
| 3974 | public static function get_search_form_where_extra_fields() |
||
| 4016 | |||
| 4017 | /** |
||
| 4018 | * Show the search form |
||
| 4019 | * @param string $query the value of the search box |
||
| 4020 | * @return string HTML form |
||
| 4021 | */ |
||
| 4022 | public static function get_search_form($query, $defaultParams = []) |
||
| 4093 | |||
| 4094 | /** |
||
| 4095 | * Shows the user menu |
||
| 4096 | */ |
||
| 4097 | public static function show_menu() |
||
| 4108 | |||
| 4109 | /** |
||
| 4110 | * Allow to register contact to social network |
||
| 4111 | * @param int $friend_id user friend id |
||
| 4112 | * @param int $my_user_id user id |
||
| 4113 | * @param int $relation_type relation between users see constants definition |
||
| 4114 | */ |
||
| 4115 | public static function relate_users($friend_id, $my_user_id, $relation_type) |
||
| 4163 | |||
| 4164 | /** |
||
| 4165 | * Deletes a contact |
||
| 4166 | * @param int user friend id |
||
| 4167 | * @param bool true will delete ALL friends relationship from $friend_id |
||
| 4168 | * @author isaac flores paz <[email protected]> |
||
| 4169 | * @author Julio Montoya <[email protected]> Cleaning code |
||
| 4170 | */ |
||
| 4171 | public static function remove_user_rel_user($friend_id, $real_removed = false, $with_status_condition = '') |
||
| 4215 | |||
| 4216 | /** |
||
| 4217 | * @param int $userId |
||
| 4218 | * @return array |
||
| 4219 | */ |
||
| 4220 | public static function getDrhListFromUser($userId) |
||
| 4248 | |||
| 4249 | /** |
||
| 4250 | * get users followed by human resource manager |
||
| 4251 | * @param int $userId |
||
| 4252 | * @param int $userStatus (STUDENT, COURSEMANAGER, etc) |
||
| 4253 | * @param bool $getOnlyUserId |
||
| 4254 | * @param bool $getSql |
||
| 4255 | * @param bool $getCount |
||
| 4256 | * @param int $from |
||
| 4257 | * @param int $numberItems |
||
| 4258 | * @param int $column |
||
| 4259 | * @param string $direction |
||
| 4260 | * @param int $active |
||
| 4261 | * @param string $lastConnectionDate |
||
| 4262 | * @return array users |
||
| 4263 | */ |
||
| 4264 | View Code Duplication | public static function get_users_followed_by_drh( |
|
| 4292 | |||
| 4293 | /** |
||
| 4294 | * Get users followed by human resource manager |
||
| 4295 | * @param int $userId |
||
| 4296 | * @param int $userStatus Filter users by status (STUDENT, COURSEMANAGER, etc) |
||
| 4297 | * @param bool $getOnlyUserId |
||
| 4298 | * @param bool $getSql |
||
| 4299 | * @param bool $getCount |
||
| 4300 | * @param int $from |
||
| 4301 | * @param int $numberItems |
||
| 4302 | * @param int $column |
||
| 4303 | * @param string $direction |
||
| 4304 | * @param int $active |
||
| 4305 | * @param string $lastConnectionDate |
||
| 4306 | * @param int $status the function is called by who? COURSEMANAGER, DRH? |
||
| 4307 | * @param string $keyword |
||
| 4308 | * |
||
| 4309 | * @return array user list |
||
| 4310 | */ |
||
| 4311 | public static function getUsersFollowedByUser( |
||
| 4514 | |||
| 4515 | /** |
||
| 4516 | * Subscribes users to human resource manager (Dashboard feature) |
||
| 4517 | * @param int $hr_dept_id |
||
| 4518 | * @param array $users_id |
||
| 4519 | * @param int affected rows |
||
| 4520 | * */ |
||
| 4521 | public static function subscribeUsersToHRManager($hr_dept_id, $users_id) |
||
| 4525 | |||
| 4526 | /** |
||
| 4527 | * Add subscribed users to a user by relation type |
||
| 4528 | * @param int $userId The user id |
||
| 4529 | * @param array $subscribedUsersId The id of suscribed users |
||
| 4530 | * @param string $relationType The relation type |
||
| 4531 | * @param bool $deleteUsersBeforeInsert |
||
| 4532 | */ |
||
| 4533 | public static function subscribeUsersToUser($userId, $subscribedUsersId, $relationType, $deleteUsersBeforeInsert = false) |
||
| 4591 | |||
| 4592 | /** |
||
| 4593 | * This function check if an user is followed by human resources manager |
||
| 4594 | * @param int $user_id |
||
| 4595 | * @param int $hr_dept_id Human resources manager |
||
| 4596 | * @return bool |
||
| 4597 | */ |
||
| 4598 | public static function is_user_followed_by_drh($user_id, $hr_dept_id) |
||
| 4617 | |||
| 4618 | /** |
||
| 4619 | * get user id of teacher or session administrator |
||
| 4620 | * @param array $courseInfo |
||
| 4621 | * |
||
| 4622 | * @return int The user id |
||
| 4623 | */ |
||
| 4624 | public static function get_user_id_of_course_admin_or_session_admin($courseInfo) |
||
| 4663 | |||
| 4664 | /** |
||
| 4665 | * Determines if a user is a gradebook certified |
||
| 4666 | * @param int $cat_id The category id of gradebook |
||
| 4667 | * @param int $user_id The user id |
||
| 4668 | * @return boolean |
||
| 4669 | */ |
||
| 4670 | View Code Duplication | public static function is_user_certified($cat_id, $user_id) |
|
| 4685 | |||
| 4686 | /** |
||
| 4687 | * Gets the info about a gradebook certificate for a user by course |
||
| 4688 | * @param int $courseId course id |
||
| 4689 | * @param int $user_id The user id |
||
| 4690 | * @return array if there is not information return false |
||
| 4691 | */ |
||
| 4692 | public static function getInfoGradeBookCertificate($courseId, $user_id) |
||
| 4732 | |||
| 4733 | /** |
||
| 4734 | * Gets the user path of user certificated |
||
| 4735 | * @param int The user id |
||
| 4736 | * @return array containing path_certificate and cat_id |
||
| 4737 | */ |
||
| 4738 | public static function get_user_path_certificate($user_id) |
||
| 4765 | |||
| 4766 | /** |
||
| 4767 | * This function check if the user is a coach inside session course |
||
| 4768 | * @param int $user_id User id |
||
| 4769 | * @param int $courseId |
||
| 4770 | * @param int $session_id |
||
| 4771 | * @return bool True if the user is a coach |
||
| 4772 | * |
||
| 4773 | */ |
||
| 4774 | View Code Duplication | public static function is_session_course_coach($user_id, $courseId, $session_id) |
|
| 4796 | |||
| 4797 | /** |
||
| 4798 | * This function returns an icon path that represents the favicon of the website of which the url given. |
||
| 4799 | * Defaults to the current Chamilo favicon |
||
| 4800 | * @param string $url1 URL of website where to look for favicon.ico |
||
| 4801 | * @param string $url2 Optional second URL of website where to look for favicon.ico |
||
| 4802 | * @return string Path of icon to load |
||
| 4803 | */ |
||
| 4804 | public static function get_favicon_from_url($url1, $url2 = null) |
||
| 4821 | |||
| 4822 | /** |
||
| 4823 | * |
||
| 4824 | * @param int student id |
||
| 4825 | * @param int years |
||
| 4826 | * @param bool show warning_message |
||
| 4827 | * @param bool return_timestamp |
||
| 4828 | */ |
||
| 4829 | public static function delete_inactive_student($student_id, $years = 2, $warning_message = false, $return_timestamp = false) |
||
| 4865 | |||
| 4866 | /** |
||
| 4867 | * @return array |
||
| 4868 | */ |
||
| 4869 | public static function get_user_field_types() |
||
| 4889 | |||
| 4890 | /** |
||
| 4891 | * @param User $user |
||
| 4892 | */ |
||
| 4893 | public static function add_user_as_admin(User $user) |
||
| 4908 | |||
| 4909 | /** |
||
| 4910 | * @param int $userId |
||
| 4911 | */ |
||
| 4912 | View Code Duplication | public static function remove_user_admin($userId) |
|
| 4921 | |||
| 4922 | /** |
||
| 4923 | * @param string $from |
||
| 4924 | * @param string $to |
||
| 4925 | */ |
||
| 4926 | public static function update_all_user_languages($from, $to) |
||
| 4938 | |||
| 4939 | /** |
||
| 4940 | * Subscribe boss to students |
||
| 4941 | * |
||
| 4942 | * @param int $bossId The boss id |
||
| 4943 | * @param array $usersId The users array |
||
| 4944 | * @return int Affected rows |
||
| 4945 | */ |
||
| 4946 | public static function subscribeBossToUsers($bossId, $usersId) |
||
| 4950 | |||
| 4951 | /** |
||
| 4952 | * Subscribe boss to students |
||
| 4953 | * |
||
| 4954 | * @param int $studentId |
||
| 4955 | * @param array $bossList |
||
| 4956 | * @return int Affected rows |
||
| 4957 | */ |
||
| 4958 | public static function subscribeUserToBossList($studentId, $bossList) |
||
| 4976 | |||
| 4977 | /** |
||
| 4978 | * Get users followed by student boss |
||
| 4979 | * @param int $userId |
||
| 4980 | * @param int $userStatus (STUDENT, COURSEMANAGER, etc) |
||
| 4981 | * @param bool $getOnlyUserId |
||
| 4982 | * @param bool $getSql |
||
| 4983 | * @param bool $getCount |
||
| 4984 | * @param int $from |
||
| 4985 | * @param int $numberItems |
||
| 4986 | * @param int $column |
||
| 4987 | * @param string $direction |
||
| 4988 | * @param int $active |
||
| 4989 | * @param string $lastConnectionDate |
||
| 4990 | * @return array users |
||
| 4991 | */ |
||
| 4992 | View Code Duplication | public static function getUsersFollowedByStudentBoss( |
|
| 5020 | |||
| 5021 | /** |
||
| 5022 | * Get the teacher (users with COURSEMANGER status) list |
||
| 5023 | * @return array The list |
||
| 5024 | */ |
||
| 5025 | public static function getTeachersList() |
||
| 5041 | |||
| 5042 | /** |
||
| 5043 | * @return array |
||
| 5044 | */ |
||
| 5045 | View Code Duplication | public static function getOfficialCodeGrouped() |
|
| 5061 | |||
| 5062 | /** |
||
| 5063 | * @param string $officialCode |
||
| 5064 | * @return array |
||
| 5065 | */ |
||
| 5066 | public static function getUsersByOfficialCode($officialCode) |
||
| 5083 | |||
| 5084 | /** |
||
| 5085 | * Calc the expended time (in seconds) by a user in a course |
||
| 5086 | * @param int $userId The user id |
||
| 5087 | * @param int $courseId The course id |
||
| 5088 | * @param int $sessionId Optional. The session id |
||
| 5089 | * @param string $from Optional. From date |
||
| 5090 | * @param string $until Optional. Until date |
||
| 5091 | * @return int The time |
||
| 5092 | */ |
||
| 5093 | public static function getTimeSpentInCourses($userId, $courseId, $sessionId = 0, $from = '', $until = '') |
||
| 5125 | |||
| 5126 | /** |
||
| 5127 | * Get the boss user ID from a followed user id |
||
| 5128 | * @param $userId |
||
| 5129 | * @return bool |
||
| 5130 | */ |
||
| 5131 | View Code Duplication | public static function getFirstStudentBoss($userId) |
|
| 5156 | |||
| 5157 | /** |
||
| 5158 | * Get the boss user ID from a followed user id |
||
| 5159 | * @param $userId |
||
| 5160 | * @return bool |
||
| 5161 | */ |
||
| 5162 | View Code Duplication | public static function getStudentBossList($userId) |
|
| 5186 | |||
| 5187 | /** |
||
| 5188 | * @param int $bossId |
||
| 5189 | * @param int $studentId |
||
| 5190 | * |
||
| 5191 | * @return bool |
||
| 5192 | */ |
||
| 5193 | public static function userIsBossOfStudent($bossId, $studentId) |
||
| 5206 | |||
| 5207 | /** |
||
| 5208 | * Get either a Gravatar URL or complete image tag for a specified email address. |
||
| 5209 | * |
||
| 5210 | * @param string $email The email address |
||
| 5211 | * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
||
| 5212 | * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
||
| 5213 | * @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
||
| 5214 | * @param boole $img True to return a complete IMG tag False for just the URL |
||
| 5215 | * @param array $atts Optional, additional key/value attributes to include in the IMG tag |
||
| 5216 | * @return String containing either just a URL or a complete image tag |
||
| 5217 | * @source http://gravatar.com/site/implement/images/php/ |
||
| 5218 | */ |
||
| 5219 | private static function getGravatar( |
||
| 5241 | |||
| 5242 | /** |
||
| 5243 | * Displays the name of the user and makes the link to the user profile |
||
| 5244 | * @param array $userInfo |
||
| 5245 | * |
||
| 5246 | * @return string |
||
| 5247 | */ |
||
| 5248 | public static function getUserProfileLink($userInfo) |
||
| 5259 | |||
| 5260 | /** |
||
| 5261 | * Displays the name of the user and makes the link to the user profile |
||
| 5262 | * |
||
| 5263 | * @param $userInfo |
||
| 5264 | * |
||
| 5265 | * @return string |
||
| 5266 | */ |
||
| 5267 | public static function getUserProfileLinkWithPicture($userInfo) |
||
| 5271 | |||
| 5272 | /** |
||
| 5273 | * Get users whose name matches $firstname and $lastname |
||
| 5274 | * @param string $firstname Firstname to search |
||
| 5275 | * @param string $lastname Lastname to search |
||
| 5276 | * @return array The user list |
||
| 5277 | */ |
||
| 5278 | View Code Duplication | public static function getUserByName($firstname, $lastname) |
|
| 5302 | |||
| 5303 | /** |
||
| 5304 | * @param int $optionSelected |
||
| 5305 | * @return string |
||
| 5306 | */ |
||
| 5307 | public static function getUserSubscriptionTab($optionSelected = 1) |
||
| 5345 | |||
| 5346 | |||
| 5347 | /** |
||
| 5348 | * @param int $user_id |
||
| 5349 | * @return bool |
||
| 5350 | */ |
||
| 5351 | View Code Duplication | public static function user_is_online($user_id) |
|
| 5380 | |||
| 5381 | /** |
||
| 5382 | * @param int $time_limit seconds |
||
| 5383 | * @param bool $friends show friends (true) or all users (false) |
||
| 5384 | * @return bool |
||
| 5385 | */ |
||
| 5386 | public static function whoIsOnlineCount( |
||
| 5459 | |||
| 5460 | /** |
||
| 5461 | * Gives a list of people online now (and in the last $valid minutes) |
||
| 5462 | * |
||
| 5463 | * @param int $from |
||
| 5464 | * @param int $number_of_items |
||
| 5465 | * @param string $column |
||
| 5466 | * @param string $direction |
||
| 5467 | * @param int $time_limit in seconds |
||
| 5468 | * @param bool $friends show friends (true) or all users (false) |
||
| 5469 | * @return array|bool |
||
| 5470 | */ |
||
| 5471 | public static function whoIsOnline( |
||
| 5579 | |||
| 5580 | /** |
||
| 5581 | * @param int $user_id |
||
| 5582 | * @param bool $is_time_over |
||
| 5583 | * @param bool $get_count |
||
| 5584 | * @param bool $reverse_order |
||
| 5585 | * @param int $start |
||
| 5586 | * @param null $maxPerPage |
||
| 5587 | * @param null $categoryFilter |
||
| 5588 | * @return array |
||
| 5589 | */ |
||
| 5590 | public static function getCategories( |
||
| 5679 | |||
| 5680 | |||
| 5681 | } |
||
| 5682 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: