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 |
||
| 21 | class UserManager |
||
| 22 | { |
||
| 23 | // This constants are deprecated use the constants located in ExtraField |
||
| 24 | const USER_FIELD_TYPE_TEXT = 1; |
||
| 25 | const USER_FIELD_TYPE_TEXTAREA = 2; |
||
| 26 | const USER_FIELD_TYPE_RADIO = 3; |
||
| 27 | const USER_FIELD_TYPE_SELECT = 4; |
||
| 28 | const USER_FIELD_TYPE_SELECT_MULTIPLE = 5; |
||
| 29 | const USER_FIELD_TYPE_DATE = 6; |
||
| 30 | const USER_FIELD_TYPE_DATETIME = 7; |
||
| 31 | const USER_FIELD_TYPE_DOUBLE_SELECT = 8; |
||
| 32 | const USER_FIELD_TYPE_DIVIDER = 9; |
||
| 33 | const USER_FIELD_TYPE_TAG = 10; |
||
| 34 | const USER_FIELD_TYPE_TIMEZONE = 11; |
||
| 35 | const USER_FIELD_TYPE_SOCIAL_PROFILE = 12; |
||
| 36 | const USER_FIELD_TYPE_FILE = 13; |
||
| 37 | const USER_FIELD_TYPE_MOBILE_PHONE_NUMBER = 14; |
||
| 38 | |||
| 39 | private static $encryptionMethod; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The default constructor only instanciates an empty user object |
||
| 43 | * @assert () === null |
||
| 44 | */ |
||
| 45 | public function __construct() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Repository is use to query the DB, selects, etc |
||
| 52 | * @return Chamilo\UserBundle\Entity\Repository\UserRepository |
||
| 53 | */ |
||
| 54 | public static function getRepository() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Create/update/delete methods are available in the UserManager |
||
| 61 | * (based in the Sonata\UserBundle\Entity\UserManager) |
||
| 62 | * |
||
| 63 | * @return Chamilo\UserBundle\Entity\Manager\UserManager |
||
| 64 | */ |
||
| 65 | public static function getManager() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $encryptionMethod |
||
| 82 | */ |
||
| 83 | public static function setPasswordEncryption($encryptionMethod) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return bool|mixed |
||
| 90 | */ |
||
| 91 | public static function getPasswordEncryption() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return EncoderFactory |
||
| 103 | */ |
||
| 104 | private static function getEncoderFactory() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param User $user |
||
| 130 | * |
||
| 131 | * @return \Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface |
||
| 132 | */ |
||
| 133 | private static function getEncoder(User $user) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Validates the password |
||
| 142 | * @param string $password |
||
| 143 | * @param User $user |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public static function isPasswordValid($password, User $user) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $raw |
||
| 162 | * @param User $user |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public static function encryptPassword($raw, User $user) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param int $userId |
||
| 180 | * @param string $password |
||
| 181 | * |
||
| 182 | */ |
||
| 183 | public static function updatePassword($userId, $password) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Creates a new user for the platform |
||
| 195 | * @author Hugues Peeters <[email protected]>, |
||
| 196 | * @author Roan Embrechts <[email protected]> |
||
| 197 | * @param string Firstname |
||
| 198 | * @param string Lastname |
||
| 199 | * @param int Status (1 for course tutor, 5 for student, 6 for anonymous) |
||
| 200 | * @param string e-mail address |
||
| 201 | * @param string Login |
||
| 202 | * @param string Password |
||
| 203 | * @param string Any official code (optional) |
||
| 204 | * @param string User language (optional) |
||
| 205 | * @param string Phone number (optional) |
||
| 206 | * @param string Picture URI (optional) |
||
| 207 | * @param string Authentication source (optional, defaults to 'platform', dependind on constant) |
||
| 208 | * @param string Account expiration date (optional, defaults to null) |
||
| 209 | * @param int Whether the account is enabled or disabled by default |
||
| 210 | * @param int The department of HR in which the user is registered (optional, defaults to 0) |
||
| 211 | * @param array Extra fields |
||
| 212 | * @param string Encrypt method used if password is given encrypted. Set to an empty string by default |
||
| 213 | * @param bool $send_mail |
||
| 214 | * @param bool $isAdmin |
||
| 215 | * |
||
| 216 | * @return mixed new user id - if the new user creation succeeds, false otherwise |
||
| 217 | * @desc The function tries to retrieve user id from the session. |
||
| 218 | * If it exists, the current user id is the creator id. If a problem arises, |
||
| 219 | * it stores the error message in global $api_failureList |
||
| 220 | * @assert ('Sam','Gamegie',5,'[email protected]','jo','jo') > 1 |
||
| 221 | * @assert ('Pippin','Took',null,null,'jo','jo') === false |
||
| 222 | */ |
||
| 223 | public static function create_user( |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Can user be deleted? This function checks whether there's a course |
||
| 467 | * in which the given user is the |
||
| 468 | * only course administrator. If that is the case, the user can't be |
||
| 469 | * deleted because the course would remain without a course admin. |
||
| 470 | * @param int $user_id The user id |
||
| 471 | * @return boolean true if user can be deleted |
||
| 472 | * @assert (null) === false |
||
| 473 | * @assert (-1) === false |
||
| 474 | * @assert ('abc') === false |
||
| 475 | */ |
||
| 476 | public static function can_delete_user($user_id) |
||
| 477 | { |
||
| 478 | $deny = api_get_configuration_value('deny_delete_users'); |
||
| 479 | |||
| 480 | if ($deny) { |
||
| 481 | return false; |
||
| 482 | } |
||
| 483 | |||
| 484 | $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 485 | if ($user_id != strval(intval($user_id))) { |
||
| 486 | return false; |
||
| 487 | } |
||
| 488 | if ($user_id === false) { |
||
| 489 | return false; |
||
| 490 | } |
||
| 491 | $sql = "SELECT * FROM $table_course_user |
||
| 492 | WHERE status = 1 AND user_id = ".$user_id; |
||
| 493 | $res = Database::query($sql); |
||
| 494 | while ($course = Database::fetch_object($res)) { |
||
| 495 | $sql = "SELECT id FROM $table_course_user |
||
| 496 | WHERE status=1 AND c_id = " . intval($course->c_id); |
||
| 497 | $res2 = Database::query($sql); |
||
| 498 | if (Database::num_rows($res2) == 1) { |
||
| 499 | |||
| 500 | return false; |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | return true; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Delete a user from the platform, and all its belongings. This is a |
||
| 509 | * very dangerous function that should only be accessible by |
||
| 510 | * super-admins. Other roles should only be able to disable a user, |
||
| 511 | * which removes access to the platform but doesn't delete anything. |
||
| 512 | * @param int The ID of th user to be deleted |
||
| 513 | * @return boolean true if user is successfully deleted, false otherwise |
||
| 514 | * @assert (null) === false |
||
| 515 | * @assert ('abc') === false |
||
| 516 | */ |
||
| 517 | public static function delete_user($user_id) |
||
| 518 | { |
||
| 519 | if ($user_id != strval(intval($user_id))) { |
||
| 520 | return false; |
||
| 521 | } |
||
| 522 | |||
| 523 | if ($user_id === false) { |
||
| 524 | return false; |
||
| 525 | } |
||
| 526 | |||
| 527 | if (!self::can_delete_user($user_id)) { |
||
| 528 | return false; |
||
| 529 | } |
||
| 530 | |||
| 531 | $table_user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 532 | $usergroup_rel_user = Database :: get_main_table(TABLE_USERGROUP_REL_USER); |
||
| 533 | $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 534 | $table_course = Database :: get_main_table(TABLE_MAIN_COURSE); |
||
| 535 | $table_session = Database :: get_main_table(TABLE_MAIN_SESSION); |
||
| 536 | $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN); |
||
| 537 | $table_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 538 | $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 539 | $table_group = Database :: get_course_table(TABLE_GROUP_USER); |
||
| 540 | $table_work = Database :: get_course_table(TABLE_STUDENT_PUBLICATION); |
||
| 541 | |||
| 542 | // Unsubscribe the user from all groups in all his courses |
||
| 543 | $sql = "SELECT c.id FROM $table_course c, $table_course_user cu |
||
| 544 | WHERE |
||
| 545 | cu.user_id = '".$user_id."' AND |
||
| 546 | relation_type<>".COURSE_RELATION_TYPE_RRHH." AND |
||
| 547 | c.id = cu.c_id"; |
||
| 548 | |||
| 549 | $res = Database::query($sql); |
||
| 550 | while ($course = Database::fetch_object($res)) { |
||
| 551 | $sql = "DELETE FROM $table_group |
||
| 552 | WHERE c_id = {$course->id} AND user_id = $user_id"; |
||
| 553 | Database::query($sql); |
||
| 554 | } |
||
| 555 | |||
| 556 | // Unsubscribe user from usergroup_rel_user |
||
| 557 | $sql = "DELETE FROM $usergroup_rel_user WHERE user_id = '".$user_id."'"; |
||
| 558 | Database::query($sql); |
||
| 559 | |||
| 560 | // Unsubscribe user from all courses |
||
| 561 | $sql = "DELETE FROM $table_course_user WHERE user_id = '".$user_id."'"; |
||
| 562 | Database::query($sql); |
||
| 563 | |||
| 564 | // Unsubscribe user from all courses in sessions |
||
| 565 | $sql = "DELETE FROM $table_session_course_user WHERE user_id = '".$user_id."'"; |
||
| 566 | Database::query($sql); |
||
| 567 | |||
| 568 | // If the user was added as a id_coach then set the current admin as coach see BT# |
||
| 569 | $currentUserId = api_get_user_id(); |
||
| 570 | $sql = "UPDATE $table_session SET id_coach = $currentUserId |
||
| 571 | WHERE id_coach = '".$user_id."'"; |
||
| 572 | Database::query($sql); |
||
| 573 | |||
| 574 | $sql = "UPDATE $table_session SET id_coach = $currentUserId |
||
| 575 | WHERE session_admin_id = '".$user_id."'"; |
||
| 576 | Database::query($sql); |
||
| 577 | |||
| 578 | // Unsubscribe user from all sessions |
||
| 579 | $sql = "DELETE FROM $table_session_user |
||
| 580 | WHERE user_id = '".$user_id."'"; |
||
| 581 | Database::query($sql); |
||
| 582 | |||
| 583 | // Delete user picture |
||
| 584 | /* TODO: Logic about api_get_setting('split_users_upload_directory') == 'true' |
||
| 585 | a user has 4 different sized photos to be deleted. */ |
||
| 586 | $user_info = api_get_user_info($user_id); |
||
| 587 | |||
| 588 | if (strlen($user_info['picture_uri']) > 0) { |
||
| 589 | $path = self::getUserPathById($user_id, 'system'); |
||
| 590 | $img_path = $path.$user_info['picture_uri']; |
||
| 591 | if (file_exists($img_path)) { |
||
| 592 | unlink($img_path); |
||
| 593 | } |
||
| 594 | } |
||
| 595 | |||
| 596 | // Delete the personal course categories |
||
| 597 | $course_cat_table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); |
||
| 598 | $sql = "DELETE FROM $course_cat_table WHERE user_id = '".$user_id."'"; |
||
| 599 | Database::query($sql); |
||
| 600 | |||
| 601 | // Delete user from the admin table |
||
| 602 | $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'"; |
||
| 603 | Database::query($sql); |
||
| 604 | |||
| 605 | // Delete the personal agenda-items from this user |
||
| 606 | $agenda_table = Database :: get_main_table(TABLE_PERSONAL_AGENDA); |
||
| 607 | $sql = "DELETE FROM $agenda_table WHERE user = '".$user_id."'"; |
||
| 608 | Database::query($sql); |
||
| 609 | |||
| 610 | $gradebook_results_table = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
||
| 611 | $sql = 'DELETE FROM '.$gradebook_results_table.' WHERE user_id = '.$user_id; |
||
| 612 | Database::query($sql); |
||
| 613 | |||
| 614 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 615 | $extraFieldValue->deleteValuesByItem($user_id); |
||
| 616 | |||
| 617 | UrlManager::deleteUserFromAllUrls($user_id); |
||
| 618 | |||
| 619 | if (api_get_setting('allow_social_tool') == 'true') { |
||
| 620 | $userGroup = new UserGroup(); |
||
| 621 | //Delete user from portal groups |
||
| 622 | $group_list = $userGroup->get_groups_by_user($user_id); |
||
| 623 | if (!empty($group_list)) { |
||
| 624 | foreach ($group_list as $group_id => $data) { |
||
| 625 | $userGroup->delete_user_rel_group($user_id, $group_id); |
||
| 626 | } |
||
| 627 | } |
||
| 628 | |||
| 629 | // Delete user from friend lists |
||
| 630 | SocialManager::remove_user_rel_user($user_id, true); |
||
| 631 | } |
||
| 632 | |||
| 633 | // Removing survey invitation |
||
| 634 | SurveyManager::delete_all_survey_invitations_by_user($user_id); |
||
| 635 | |||
| 636 | // Delete students works |
||
| 637 | $sql = "DELETE FROM $table_work WHERE user_id = $user_id AND c_id <> 0"; |
||
| 638 | Database::query($sql); |
||
| 639 | |||
| 640 | $sql = "UPDATE c_item_property SET to_user_id = NULL |
||
| 641 | WHERE to_user_id = '".$user_id."'"; |
||
| 642 | Database::query($sql); |
||
| 643 | |||
| 644 | $sql = "UPDATE c_item_property SET insert_user_id = NULL |
||
| 645 | WHERE insert_user_id = '".$user_id."'"; |
||
| 646 | Database::query($sql); |
||
| 647 | |||
| 648 | $sql = "UPDATE c_item_property SET lastedit_user_id = NULL |
||
| 649 | WHERE lastedit_user_id = '".$user_id."'"; |
||
| 650 | Database::query($sql); |
||
| 651 | |||
| 652 | |||
| 653 | |||
| 654 | |||
| 655 | // Delete user from database |
||
| 656 | $sql = "DELETE FROM $table_user WHERE id = '".$user_id."'"; |
||
| 657 | Database::query($sql); |
||
| 658 | |||
| 659 | // Add event to system log |
||
| 660 | $user_id_manager = api_get_user_id(); |
||
| 661 | |||
| 662 | Event::addEvent( |
||
| 663 | LOG_USER_DELETE, |
||
| 664 | LOG_USER_ID, |
||
| 665 | $user_id, |
||
| 666 | api_get_utc_datetime(), |
||
| 667 | $user_id_manager |
||
| 668 | ); |
||
| 669 | |||
| 670 | Event::addEvent( |
||
| 671 | LOG_USER_DELETE, |
||
| 672 | LOG_USER_OBJECT, |
||
| 673 | $user_info, |
||
| 674 | api_get_utc_datetime(), |
||
| 675 | $user_id_manager |
||
| 676 | ); |
||
| 677 | |||
| 678 | return true; |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Deletes users completely. Can be called either as: |
||
| 683 | * - UserManager :: delete_users(1, 2, 3); or |
||
| 684 | * - UserManager :: delete_users(array(1, 2, 3)); |
||
| 685 | * @param array|int $ids |
||
| 686 | * @return boolean True if at least one user was successfuly deleted. False otherwise. |
||
| 687 | * @author Laurent Opprecht |
||
| 688 | * @uses UserManager::delete_user() to actually delete each user |
||
| 689 | * @assert (null) === false |
||
| 690 | * @assert (-1) === false |
||
| 691 | * @assert (array(-1)) === false |
||
| 692 | */ |
||
| 693 | static function delete_users($ids = array()) |
||
| 694 | { |
||
| 695 | $result = false; |
||
| 696 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 697 | if (!is_array($ids) or count($ids) == 0) { return false; } |
||
| 698 | $ids = array_map('intval', $ids); |
||
| 699 | foreach ($ids as $id) { |
||
| 700 | if (empty($id) or $id < 1) { continue; } |
||
| 701 | $deleted = self::delete_user($id); |
||
| 702 | $result = $deleted || $result; |
||
| 703 | } |
||
| 704 | |||
| 705 | return $result; |
||
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Disable users. Can be called either as: |
||
| 710 | * - UserManager :: deactivate_users(1, 2, 3); |
||
| 711 | * - UserManager :: deactivate_users(array(1, 2, 3)); |
||
| 712 | * @param array|int $ids |
||
| 713 | * @return boolean |
||
| 714 | * @author Laurent Opprecht |
||
| 715 | * @assert (null) === false |
||
| 716 | * @assert (array(-1)) === false |
||
| 717 | */ |
||
| 718 | View Code Duplication | static function deactivate_users($ids = array()) |
|
| 719 | { |
||
| 720 | if (empty($ids)) { |
||
| 721 | return false; |
||
| 722 | } |
||
| 723 | |||
| 724 | $table_user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 725 | |||
| 726 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 727 | $ids = array_map('intval', $ids); |
||
| 728 | $ids = implode(',', $ids); |
||
| 729 | |||
| 730 | $sql = "UPDATE $table_user SET active = 0 WHERE id IN ($ids)"; |
||
| 731 | $r = Database::query($sql); |
||
| 732 | if ($r !== false) { |
||
| 733 | Event::addEvent(LOG_USER_DISABLE, LOG_USER_ID, $ids); |
||
| 734 | } |
||
| 735 | return $r; |
||
| 736 | } |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Enable users. Can be called either as: |
||
| 740 | * - UserManager :: activate_users(1, 2, 3); |
||
| 741 | * - UserManager :: activate_users(array(1, 2, 3)); |
||
| 742 | * @param array|int IDs of the users to enable |
||
| 743 | * @return boolean |
||
| 744 | * @author Laurent Opprecht |
||
| 745 | * @assert (null) === false |
||
| 746 | * @assert (array(-1)) === false |
||
| 747 | */ |
||
| 748 | View Code Duplication | static function activate_users($ids = array()) |
|
| 749 | { |
||
| 750 | if (empty($ids)) { |
||
| 751 | return false; |
||
| 752 | } |
||
| 753 | |||
| 754 | $table_user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 755 | |||
| 756 | $ids = is_array($ids) ? $ids : func_get_args(); |
||
| 757 | $ids = array_map('intval', $ids); |
||
| 758 | $ids = implode(',', $ids); |
||
| 759 | |||
| 760 | $sql = "UPDATE $table_user SET active = 1 WHERE id IN ($ids)"; |
||
| 761 | $r = Database::query($sql); |
||
| 762 | if ($r !== false) { |
||
| 763 | Event::addEvent(LOG_USER_ENABLE,LOG_USER_ID,$ids); |
||
| 764 | } |
||
| 765 | return $r; |
||
| 766 | } |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Update user information with new openid |
||
| 770 | * @param int $user_id |
||
| 771 | * @param string $openid |
||
| 772 | * @return boolean true if the user information was updated |
||
| 773 | * @assert (false,'') === false |
||
| 774 | * @assert (-1,'') === false |
||
| 775 | */ |
||
| 776 | public static function update_openid($user_id, $openid) |
||
| 777 | { |
||
| 778 | $table_user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 779 | if ($user_id != strval(intval($user_id))) |
||
| 780 | return false; |
||
| 781 | if ($user_id === false) |
||
| 782 | return false; |
||
| 783 | $sql = "UPDATE $table_user SET |
||
| 784 | openid='".Database::escape_string($openid)."'"; |
||
| 785 | $sql .= " WHERE id= $user_id"; |
||
| 786 | return Database::query($sql); |
||
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Update user information with all the parameters passed to this function |
||
| 791 | * @param int The ID of the user to be updated |
||
| 792 | * @param string The user's firstname |
||
| 793 | * @param string The user's lastname |
||
| 794 | * @param string The user's username (login) |
||
| 795 | * @param string The user's password |
||
| 796 | * @param string The authentication source (default: "platform") |
||
| 797 | * @param string The user's e-mail address |
||
| 798 | * @param int The user's status |
||
| 799 | * @param string The user's official code (usually just an internal institutional code) |
||
| 800 | * @param string The user's phone number |
||
| 801 | * @param string The user's picture URL (internal to the Chamilo directory) |
||
| 802 | * @param int The user ID of the person who registered this user (optional, defaults to null) |
||
| 803 | * @param int The department of HR in which the user is registered (optional, defaults to 0) |
||
| 804 | * @param array A series of additional fields to add to this user as extra fields (optional, defaults to null) |
||
| 805 | * @return boolean|integer False on error, or the user ID if the user information was updated |
||
| 806 | * @assert (false, false, false, false, false, false, false, false, false, false, false, false, false) === false |
||
| 807 | */ |
||
| 808 | public static function update_user( |
||
| 809 | $user_id, |
||
| 810 | $firstname, |
||
| 811 | $lastname, |
||
| 812 | $username, |
||
| 813 | $password = null, |
||
| 814 | $auth_source = null, |
||
| 815 | $email, |
||
| 816 | $status, |
||
| 817 | $official_code, |
||
| 818 | $phone, |
||
| 819 | $picture_uri, |
||
| 820 | $expiration_date, |
||
| 821 | $active, |
||
| 822 | $creator_id = null, |
||
| 823 | $hr_dept_id = 0, |
||
| 824 | $extra = null, |
||
| 825 | $language = 'english', |
||
| 826 | $encrypt_method = '', |
||
| 827 | $send_email = false, |
||
| 828 | $reset_password = 0 |
||
| 829 | ) { |
||
| 830 | $hook = HookUpdateUser::create(); |
||
| 831 | if (!empty($hook)) { |
||
| 832 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE); |
||
| 833 | } |
||
| 834 | global $_configuration; |
||
| 835 | $original_password = $password; |
||
| 836 | |||
| 837 | if (empty($user_id)) { |
||
| 838 | return false; |
||
| 839 | } |
||
| 840 | $user_info = api_get_user_info($user_id, false, true); |
||
| 841 | |||
| 842 | if ($reset_password == 0) { |
||
| 843 | $password = null; |
||
| 844 | $auth_source = $user_info['auth_source']; |
||
| 845 | } elseif ($reset_password == 1) { |
||
| 846 | $original_password = $password = api_generate_password(); |
||
| 847 | $auth_source = PLATFORM_AUTH_SOURCE; |
||
| 848 | } elseif ($reset_password == 2) { |
||
| 849 | $password = $password; |
||
| 850 | $auth_source = PLATFORM_AUTH_SOURCE; |
||
| 851 | } elseif ($reset_password == 3) { |
||
| 852 | $password = $password; |
||
| 853 | $auth_source = $auth_source; |
||
| 854 | } |
||
| 855 | |||
| 856 | if ($user_id != strval(intval($user_id))) { |
||
| 857 | return false; |
||
| 858 | } |
||
| 859 | |||
| 860 | if ($user_id === false) { |
||
| 861 | return false; |
||
| 862 | } |
||
| 863 | |||
| 864 | //Checking the user language |
||
| 865 | $languages = api_get_languages(); |
||
| 866 | if (!in_array($language, $languages['folder'])) { |
||
| 867 | $language = api_get_setting('platformLanguage'); |
||
| 868 | } |
||
| 869 | |||
| 870 | $change_active = 0; |
||
| 871 | if ($user_info['active'] != $active) { |
||
| 872 | $change_active = 1; |
||
| 873 | } |
||
| 874 | |||
| 875 | $userManager = self::getManager(); |
||
| 876 | |||
| 877 | /** @var Chamilo\UserBundle\Entity\User $user */ |
||
| 878 | $user = self::getRepository()->find($user_id); |
||
| 879 | |||
| 880 | if (empty($user)) { |
||
| 881 | return false; |
||
| 882 | } |
||
| 883 | |||
| 884 | if (!empty($expiration_date)) { |
||
| 885 | $expiration_date = api_get_utc_datetime($expiration_date); |
||
| 886 | $expiration_date = new \DateTime( |
||
| 887 | $expiration_date, |
||
| 888 | new DateTimeZone('UTC') |
||
| 889 | ); |
||
| 890 | } |
||
| 891 | |||
| 892 | $user |
||
| 893 | ->setLastname($lastname) |
||
| 894 | ->setFirstname($firstname) |
||
| 895 | ->setUsername($username) |
||
| 896 | ->setStatus($status) |
||
| 897 | ->setAuthSource($auth_source) |
||
| 898 | ->setLanguage($language) |
||
| 899 | ->setEmail($email) |
||
| 900 | ->setOfficialCode($official_code) |
||
| 901 | ->setPhone($phone) |
||
| 902 | ->setPictureUri($picture_uri) |
||
| 903 | ->setExpirationDate($expiration_date) |
||
| 904 | ->setActive($active) |
||
| 905 | ->setHrDeptId($hr_dept_id) |
||
| 906 | ; |
||
| 907 | |||
| 908 | if (!is_null($password)) { |
||
| 909 | $user->setPlainPassword($password); |
||
| 910 | } |
||
| 911 | |||
| 912 | $userManager->updateUser($user, true); |
||
| 913 | |||
| 914 | if ($change_active == 1) { |
||
| 915 | if ($active == 1) { |
||
| 916 | $event_title = LOG_USER_ENABLE; |
||
| 917 | } else { |
||
| 918 | $event_title = LOG_USER_DISABLE; |
||
| 919 | } |
||
| 920 | Event::addEvent($event_title, LOG_USER_ID, $user_id); |
||
| 921 | } |
||
| 922 | |||
| 923 | View Code Duplication | if (is_array($extra) && count($extra) > 0) { |
|
| 924 | $res = true; |
||
| 925 | foreach ($extra as $fname => $fvalue) { |
||
| 926 | $res = $res && self::update_extra_field_value( |
||
| 927 | $user_id, |
||
| 928 | $fname, |
||
| 929 | $fvalue |
||
| 930 | ); |
||
| 931 | } |
||
| 932 | } |
||
| 933 | |||
| 934 | if (!empty($email) && $send_email) { |
||
| 935 | $recipient_name = api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS); |
||
| 936 | $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName'); |
||
| 937 | $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS); |
||
| 938 | $email_admin = api_get_setting('emailAdministrator'); |
||
| 939 | |||
| 940 | if (api_is_multiple_url_enabled()) { |
||
| 941 | $access_url_id = api_get_current_access_url_id(); |
||
| 942 | if ($access_url_id != -1) { |
||
| 943 | $url = api_get_access_url($access_url_id); |
||
| 944 | $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ".$username.(($reset_password > 0) ? "\n".get_lang('Pass')." : ".stripslashes($original_password) : "")."\n\n".get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".$url['url']."\n\n".get_lang('Problem')."\n\n".get_lang('SignatureFormula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator'); |
||
| 945 | } |
||
| 946 | } else { |
||
| 947 | $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ".$username.(($reset_password > 0) ? "\n".get_lang('Pass')." : ".stripslashes($original_password) : "")."\n\n".get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".$_configuration['root_web']."\n\n".get_lang('Problem')."\n\n".get_lang('SignatureFormula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator'); |
||
| 948 | } |
||
| 949 | api_mail_html( |
||
| 950 | $recipient_name, |
||
| 951 | $email, |
||
| 952 | $emailsubject, |
||
| 953 | $emailbody, |
||
| 954 | $sender_name, |
||
| 955 | $email_admin |
||
| 956 | ); |
||
| 957 | } |
||
| 958 | |||
| 959 | if (!empty($hook)) { |
||
| 960 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_POST); |
||
| 961 | } |
||
| 962 | |||
| 963 | return $user->getId(); |
||
| 964 | } |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Disables or enables a user |
||
| 968 | * @param int user_id |
||
| 969 | * @param int Enable or disable |
||
| 970 | * @return void |
||
| 971 | * @assert (-1,0) === false |
||
| 972 | * @assert (1,1) === true |
||
| 973 | */ |
||
| 974 | private static function change_active_state($user_id, $active) |
||
| 975 | { |
||
| 976 | if (strval(intval($user_id)) != $user_id) { |
||
| 977 | return false; |
||
| 978 | } |
||
| 979 | if ($user_id < 1) { |
||
| 980 | return false; |
||
| 981 | } |
||
| 982 | $user_id = intval($user_id); |
||
| 983 | $table_user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 984 | $sql = "UPDATE $table_user SET active = '$active' WHERE id = $user_id"; |
||
| 985 | $r = Database::query($sql); |
||
| 986 | $ev = LOG_USER_DISABLE; |
||
| 987 | if ($active == 1) { |
||
| 988 | $ev = LOG_USER_ENABLE; |
||
| 989 | } |
||
| 990 | if ($r !== false) { |
||
| 991 | Event::addEvent($ev, LOG_USER_ID, $user_id); |
||
| 992 | } |
||
| 993 | |||
| 994 | return $r; |
||
| 995 | } |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Disables a user |
||
| 999 | * @param int User id |
||
| 1000 | * @return bool |
||
| 1001 | * @uses UserManager::change_active_state() to actually disable the user |
||
| 1002 | * @assert (0) === false |
||
| 1003 | */ |
||
| 1004 | public static function disable($user_id) |
||
| 1005 | { |
||
| 1006 | if (empty($user_id)) { |
||
| 1007 | return false; |
||
| 1008 | } |
||
| 1009 | self::change_active_state($user_id, 0); |
||
| 1010 | return true; |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Enable a user |
||
| 1015 | * @param int User id |
||
| 1016 | * @return bool |
||
| 1017 | * @uses UserManager::change_active_state() to actually disable the user |
||
| 1018 | * @assert (0) === false |
||
| 1019 | */ |
||
| 1020 | public static function enable($user_id) |
||
| 1021 | { |
||
| 1022 | if (empty($user_id)) { |
||
| 1023 | return false; |
||
| 1024 | } |
||
| 1025 | self::change_active_state($user_id, 1); |
||
| 1026 | return true; |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Returns the user's id based on the original id and field name in |
||
| 1031 | * the extra fields. Returns 0 if no user was found. This function is |
||
| 1032 | * mostly useful in the context of a web services-based sinchronization |
||
| 1033 | * @param string Original user id |
||
| 1034 | * @param string Original field name |
||
| 1035 | * @return int User id |
||
| 1036 | * @assert ('0','---') === 0 |
||
| 1037 | */ |
||
| 1038 | View Code Duplication | public static function get_user_id_from_original_id($original_user_id_value, $original_user_id_name) |
|
| 1039 | { |
||
| 1040 | $t_uf = Database::get_main_table(TABLE_EXTRA_FIELD); |
||
| 1041 | $t_ufv = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES); |
||
| 1042 | $extraFieldType = EntityExtraField::USER_FIELD_TYPE; |
||
| 1043 | $sql = "SELECT item_id as user_id |
||
| 1044 | FROM $t_uf uf |
||
| 1045 | INNER JOIN $t_ufv ufv |
||
| 1046 | ON ufv.field_id=uf.id |
||
| 1047 | WHERE |
||
| 1048 | variable='$original_user_id_name' AND |
||
| 1049 | value='$original_user_id_value' AND |
||
| 1050 | extra_field_type = $extraFieldType |
||
| 1051 | "; |
||
| 1052 | $res = Database::query($sql); |
||
| 1053 | $row = Database::fetch_object($res); |
||
| 1054 | if ($row) { |
||
| 1055 | return $row->user_id; |
||
| 1056 | } else { |
||
| 1057 | return 0; |
||
| 1058 | } |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | /** |
||
| 1062 | * Check if a username is available |
||
| 1063 | * @param string the wanted username |
||
| 1064 | * @return boolean true if the wanted username is available |
||
| 1065 | * @assert ('') === false |
||
| 1066 | * @assert ('xyzxyzxyz') === true |
||
| 1067 | */ |
||
| 1068 | public static function is_username_available($username) |
||
| 1069 | { |
||
| 1070 | if (empty($username)) { |
||
| 1071 | return false; |
||
| 1072 | } |
||
| 1073 | $table_user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 1074 | $sql = "SELECT username FROM $table_user |
||
| 1075 | WHERE username = '".Database::escape_string($username)."'"; |
||
| 1076 | $res = Database::query($sql); |
||
| 1077 | return Database::num_rows($res) == 0; |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Creates a username using person's names, i.e. creates jmontoya from Julio Montoya. |
||
| 1082 | * @param string $firstname The first name of the user. |
||
| 1083 | * @param string $lastname The last name of the user. |
||
| 1084 | * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then. |
||
| 1085 | * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default. |
||
| 1086 | * @return string Suggests a username that contains only ASCII-letters and digits, without check for uniqueness within the system. |
||
| 1087 | * @author Julio Montoya Armas |
||
| 1088 | * @author Ivan Tcholakov, 2009 - rework about internationalization. |
||
| 1089 | * @assert ('','') === false |
||
| 1090 | * @assert ('a','b') === 'ab' |
||
| 1091 | */ |
||
| 1092 | public static function create_username($firstname, $lastname, $language = null, $encoding = null) |
||
| 1093 | { |
||
| 1094 | if (empty($firstname) && empty($lastname)) { |
||
| 1095 | return false; |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | $firstname = api_substr(preg_replace(USERNAME_PURIFIER, '', $firstname), 0, 1); // The first letter only. |
||
| 1099 | //Looking for a space in the lastname |
||
| 1100 | $pos = api_strpos($lastname, ' '); |
||
| 1101 | if ($pos !== false) { |
||
| 1102 | $lastname = api_substr($lastname, 0, $pos); |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | $lastname = preg_replace(USERNAME_PURIFIER, '', $lastname); |
||
| 1106 | $username = $firstname.$lastname; |
||
| 1107 | if (empty($username)) { |
||
| 1108 | $username = 'user'; |
||
| 1109 | } |
||
| 1110 | |||
| 1111 | $username = URLify::transliterate($username); |
||
| 1112 | |||
| 1113 | return strtolower(substr($username, 0, USERNAME_MAX_LENGTH - 3)); |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Creates a unique username, using: |
||
| 1118 | * 1. the first name and the last name of a user; |
||
| 1119 | * 2. an already created username but not checked for uniqueness yet. |
||
| 1120 | * @param string $firstname The first name of a given user. If the second parameter $lastname is NULL, then this |
||
| 1121 | * parameter is treated as username which is to be checked for uniqueness and to be modified when it is necessary. |
||
| 1122 | * @param string $lastname The last name of the user. |
||
| 1123 | * @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then. |
||
| 1124 | * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default. |
||
| 1125 | * @return string Returns a username that contains only ASCII-letters and digits, and that is unique within the system. |
||
| 1126 | * Note: When the method is called several times with same parameters, its results look like the following sequence: ivan, ivan2, ivan3, ivan4, ... |
||
| 1127 | * @author Ivan Tcholakov, 2009 |
||
| 1128 | */ |
||
| 1129 | public static function create_unique_username($firstname, $lastname = null, $language = null, $encoding = null) |
||
| 1130 | { |
||
| 1131 | if (is_null($lastname)) { |
||
| 1132 | // In this case the actual input parameter $firstname should contain ASCII-letters and digits only. |
||
| 1133 | // For making this method tolerant of mistakes, let us transliterate and purify the suggested input username anyway. |
||
| 1134 | // So, instead of the sentence $username = $firstname; we place the following: |
||
| 1135 | $username = strtolower(preg_replace(USERNAME_PURIFIER, '', $firstname)); |
||
| 1136 | } else { |
||
| 1137 | $username = self::create_username($firstname, $lastname, $language, $encoding); |
||
| 1138 | } |
||
| 1139 | if (!self::is_username_available($username)) { |
||
| 1140 | $i = 2; |
||
| 1141 | $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string) $i)).$i; |
||
| 1142 | while (!self::is_username_available($temp_username)) { |
||
| 1143 | $i++; |
||
| 1144 | $temp_username = substr($username, 0, USERNAME_MAX_LENGTH - strlen((string) $i)).$i; |
||
| 1145 | } |
||
| 1146 | $username = $temp_username; |
||
| 1147 | } |
||
| 1148 | |||
| 1149 | $username = URLify::transliterate($username); |
||
| 1150 | |||
| 1151 | return $username; |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * Modifies a given username accordingly to the specification for valid characters and length. |
||
| 1156 | * @param $username string The input username. |
||
| 1157 | * @param bool $strict (optional) When this flag is TRUE, the result is guaranteed for full compliance, otherwise compliance may be partial. The default value is FALSE. |
||
| 1158 | * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default. |
||
| 1159 | * @return string The resulting purified username. |
||
| 1160 | */ |
||
| 1161 | public static function purify_username($username, $strict = false, $encoding = null) |
||
| 1162 | { |
||
| 1163 | if ($strict) { |
||
| 1164 | // 1. Conversion of unacceptable letters (latinian letters with accents for example) into ASCII letters in order they not to be totally removed. |
||
| 1165 | // 2. Applying the strict purifier. |
||
| 1166 | // 3. Length limitation. |
||
| 1167 | $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); |
||
| 1168 | $return = URLify::transliterate($return); |
||
| 1169 | return $return; |
||
| 1170 | } |
||
| 1171 | // 1. Applying the shallow purifier. |
||
| 1172 | // 2. Length limitation. |
||
| 1173 | return substr(preg_replace(USERNAME_PURIFIER_SHALLOW, '', $username), 0, USERNAME_MAX_LENGTH); |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Checks whether the user id exists in the database |
||
| 1178 | * |
||
| 1179 | * @param int User id |
||
| 1180 | * @return bool True if user id was found, false otherwise |
||
| 1181 | */ |
||
| 1182 | View Code Duplication | public static function is_user_id_valid($userId) |
|
| 1183 | { |
||
| 1184 | $resultData = Database::select( |
||
| 1185 | 'COUNT(1) AS count', |
||
| 1186 | Database::get_main_table(TABLE_MAIN_USER), |
||
| 1187 | [ |
||
| 1188 | 'where' => ['id = ?' => intval($userId)] |
||
| 1189 | ], |
||
| 1190 | 'first' |
||
| 1191 | ); |
||
| 1192 | |||
| 1193 | if ($resultData === false) { |
||
| 1194 | return false; |
||
| 1195 | } |
||
| 1196 | |||
| 1197 | return $resultData['count'] > 0; |
||
| 1198 | } |
||
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Checks whether a given username matches to the specification strictly. The empty username is assumed here as invalid. |
||
| 1202 | * Mostly this function is to be used in the user interface built-in validation routines for providing feedback while usernames are enterd manually. |
||
| 1203 | * @param string $username The input username. |
||
| 1204 | * @param string $encoding (optional) The character encoding for the input names. If it is omitted, the platform character set will be used by default. |
||
| 1205 | * @return bool Returns TRUE if the username is valid, FALSE otherwise. |
||
| 1206 | */ |
||
| 1207 | public static function is_username_valid($username, $encoding = null) |
||
| 1208 | { |
||
| 1209 | return !empty($username) && $username == self::purify_username($username, true); |
||
| 1210 | } |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Checks whether a username is empty. If the username contains whitespace characters, such as spaces, tabulators, newlines, etc., |
||
| 1214 | * it is assumed as empty too. This function is safe for validation unpurified data (during importing). |
||
| 1215 | * @param string $username The given username. |
||
| 1216 | * @return bool Returns TRUE if length of the username exceeds the limit, FALSE otherwise. |
||
| 1217 | */ |
||
| 1218 | public static function is_username_empty($username) |
||
| 1219 | { |
||
| 1220 | return (strlen(self::purify_username($username, false)) == 0); |
||
| 1221 | } |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Checks whether a username is too long or not. |
||
| 1225 | * @param string $username The given username, it should contain only ASCII-letters and digits. |
||
| 1226 | * @return bool Returns TRUE if length of the username exceeds the limit, FALSE otherwise. |
||
| 1227 | */ |
||
| 1228 | public static function is_username_too_long($username) |
||
| 1229 | { |
||
| 1230 | return (strlen($username) > USERNAME_MAX_LENGTH); |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Get the users by ID |
||
| 1235 | * @param array $ids student ids |
||
| 1236 | * @param string $active |
||
| 1237 | * @param string $order |
||
| 1238 | * @param string $limit |
||
| 1239 | * @return array $result student information |
||
| 1240 | */ |
||
| 1241 | public static function get_user_list_by_ids($ids = array(), $active = null, $order = null, $limit = null) |
||
| 1242 | { |
||
| 1243 | if (empty($ids)) { |
||
| 1244 | return array(); |
||
| 1245 | } |
||
| 1246 | |||
| 1247 | $ids = is_array($ids) ? $ids : array($ids); |
||
| 1248 | $ids = array_map('intval', $ids); |
||
| 1249 | $ids = implode(',', $ids); |
||
| 1250 | |||
| 1251 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 1252 | $sql = "SELECT * FROM $tbl_user WHERE id IN ($ids)"; |
||
| 1253 | if (!is_null($active)) { |
||
| 1254 | $sql .= ' AND active='.($active ? '1' : '0'); |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | if (!is_null($order)) { |
||
| 1258 | $order = Database::escape_string($order); |
||
| 1259 | $sql .= ' ORDER BY ' . $order; |
||
| 1260 | } |
||
| 1261 | |||
| 1262 | if (!is_null($limit)) { |
||
| 1263 | $limit = Database::escape_string($limit); |
||
| 1264 | $sql .= ' LIMIT ' . $limit; |
||
| 1265 | } |
||
| 1266 | |||
| 1267 | $rs = Database::query($sql); |
||
| 1268 | $result = array(); |
||
| 1269 | while ($row = Database::fetch_array($rs)) { |
||
| 1270 | $result[] = $row; |
||
| 1271 | } |
||
| 1272 | return $result; |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | /** |
||
| 1276 | * Get a list of users of which the given conditions match with an = 'cond' |
||
| 1277 | * @param array $conditions a list of condition (exemple : status=>STUDENT) |
||
| 1278 | * @param array $order_by a list of fields on which sort |
||
| 1279 | * @return array An array with all users of the platform. |
||
| 1280 | * @todo optional course code parameter, optional sorting parameters... |
||
| 1281 | * @todo security filter order by |
||
| 1282 | */ |
||
| 1283 | public static function get_user_list($conditions = array(), $order_by = array(), $limit_from = false, $limit_to = false) |
||
| 1284 | { |
||
| 1285 | $user_table = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 1286 | $return_array = array(); |
||
| 1287 | $sql_query = "SELECT * FROM $user_table"; |
||
| 1288 | if (count($conditions) > 0) { |
||
| 1289 | $sql_query .= ' WHERE '; |
||
| 1290 | foreach ($conditions as $field => $value) { |
||
| 1291 | $field = Database::escape_string($field); |
||
| 1292 | $value = Database::escape_string($value); |
||
| 1293 | $sql_query .= "$field = '$value'"; |
||
| 1294 | } |
||
| 1295 | } |
||
| 1296 | View Code Duplication | if (count($order_by) > 0) { |
|
| 1297 | $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by), null, false); |
||
| 1298 | } |
||
| 1299 | |||
| 1300 | if (is_numeric($limit_from) && is_numeric($limit_from)) { |
||
| 1301 | $limit_from = intval($limit_from); |
||
| 1302 | $limit_to = intval($limit_to); |
||
| 1303 | $sql_query .= " LIMIT $limit_from, $limit_to"; |
||
| 1304 | } |
||
| 1305 | $sql_result = Database::query($sql_query); |
||
| 1306 | while ($result = Database::fetch_array($sql_result)) { |
||
| 1307 | $return_array[] = $result; |
||
| 1308 | } |
||
| 1309 | return $return_array; |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * Get a list of users of which the given conditions match with a LIKE '%cond%' |
||
| 1314 | * @param array $conditions a list of condition (exemple : status=>STUDENT) |
||
| 1315 | * @param array $order_by a list of fields on which sort |
||
| 1316 | * @return array An array with all users of the platform. |
||
| 1317 | * @todo optional course code parameter, optional sorting parameters... |
||
| 1318 | * @todo security filter order_by |
||
| 1319 | */ |
||
| 1320 | public static function get_user_list_like($conditions = array(), $order_by = array(), $simple_like = false, $condition = 'AND') |
||
| 1321 | { |
||
| 1322 | $user_table = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 1323 | $return_array = array(); |
||
| 1324 | $sql_query = "SELECT * FROM $user_table"; |
||
| 1325 | if (count($conditions) > 0) { |
||
| 1326 | $sql_query .= ' WHERE '; |
||
| 1327 | $temp_conditions = array(); |
||
| 1328 | foreach ($conditions as $field => $value) { |
||
| 1329 | $field = Database::escape_string($field); |
||
| 1330 | $value = Database::escape_string($value); |
||
| 1331 | View Code Duplication | if ($simple_like) { |
|
| 1332 | $temp_conditions[] = $field." LIKE '$value%'"; |
||
| 1333 | } else { |
||
| 1334 | $temp_conditions[] = $field.' LIKE \'%'.$value.'%\''; |
||
| 1335 | } |
||
| 1336 | } |
||
| 1337 | if (!empty($temp_conditions)) { |
||
| 1338 | $sql_query .= implode(' '.$condition.' ', $temp_conditions); |
||
| 1339 | } |
||
| 1340 | } |
||
| 1341 | View Code Duplication | if (count($order_by) > 0) { |
|
| 1342 | $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by), null, false); |
||
| 1343 | } |
||
| 1344 | $sql_result = Database::query($sql_query); |
||
| 1345 | while ($result = Database::fetch_array($sql_result)) { |
||
| 1346 | $return_array[] = $result; |
||
| 1347 | } |
||
| 1348 | return $return_array; |
||
| 1349 | } |
||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * Get user picture URL or path from user ID (returns an array). |
||
| 1353 | * The return format is a complete path, enabling recovery of the directory |
||
| 1354 | * with dirname() or the file with basename(). This also works for the |
||
| 1355 | * functions dealing with the user's productions, as they are located in |
||
| 1356 | * the same directory. |
||
| 1357 | * @param integer $id User ID |
||
| 1358 | * @param string $type Type of path to return (can be 'system', 'web') |
||
| 1359 | * @param array $userInfo user information to avoid query the DB |
||
| 1360 | * returns the /main/img/unknown.jpg image set it at true |
||
| 1361 | * |
||
| 1362 | * @return array Array of 2 elements: 'dir' and 'file' which contain |
||
| 1363 | * the dir and file as the name implies if image does not exist it will |
||
| 1364 | * return the unknow image if anonymous parameter is true if not it returns an empty array |
||
| 1365 | */ |
||
| 1366 | public static function get_user_picture_path_by_id($id, $type = 'web', $userInfo = []) |
||
| 1367 | { |
||
| 1368 | switch ($type) { |
||
| 1369 | case 'system': // Base: absolute system path. |
||
| 1370 | $base = api_get_path(SYS_CODE_PATH); |
||
| 1371 | break; |
||
| 1372 | case 'web': // Base: absolute web path. |
||
| 1373 | default: |
||
| 1374 | $base = api_get_path(WEB_CODE_PATH); |
||
| 1375 | break; |
||
| 1376 | } |
||
| 1377 | |||
| 1378 | $anonymousPath = array( |
||
| 1379 | 'dir' => $base.'img/', |
||
| 1380 | 'file' => 'unknown.jpg', |
||
| 1381 | 'email' => '', |
||
| 1382 | ); |
||
| 1383 | |||
| 1384 | if (empty($id) || empty($type)) { |
||
| 1385 | return $anonymousPath; |
||
| 1386 | } |
||
| 1387 | |||
| 1388 | $id = intval($id); |
||
| 1389 | View Code Duplication | if (empty($userInfo)) { |
|
| 1390 | $user_table = Database:: get_main_table(TABLE_MAIN_USER); |
||
| 1391 | $sql = "SELECT email, picture_uri FROM $user_table |
||
| 1392 | WHERE id=".$id; |
||
| 1393 | $res = Database::query($sql); |
||
| 1394 | |||
| 1395 | if (!Database::num_rows($res)) { |
||
| 1396 | return $anonymousPath; |
||
| 1397 | } |
||
| 1398 | $user = Database::fetch_array($res); |
||
| 1399 | } else { |
||
| 1400 | $user = $userInfo; |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | $pictureFilename = trim($user['picture_uri']); |
||
| 1404 | |||
| 1405 | $dir = self::getUserPathById($id, $type); |
||
| 1406 | |||
| 1407 | return array( |
||
| 1408 | 'dir' => $dir, |
||
| 1409 | 'file' => $pictureFilename, |
||
| 1410 | 'email' => $user['email'], |
||
| 1411 | ); |
||
| 1412 | } |
||
| 1413 | |||
| 1414 | /** |
||
| 1415 | * Get user path from user ID (returns an array). |
||
| 1416 | * The return format is a complete path to a folder ending with "/" |
||
| 1417 | * In case the first level of subdirectory of users/ does not exist, the |
||
| 1418 | * function will attempt to create it. Probably not the right place to do it |
||
| 1419 | * but at least it avoids headaches in many other places. |
||
| 1420 | * @param integer $id User ID |
||
| 1421 | * @param string $type Type of path to return (can be 'system', 'web', 'rel', 'last') |
||
| 1422 | * @return string User folder path (i.e. /var/www/chamilo/app/upload/users/1/1/) |
||
| 1423 | */ |
||
| 1424 | public static function getUserPathById($id, $type) |
||
| 1425 | { |
||
| 1426 | $id = intval($id); |
||
| 1427 | if (!$id) { |
||
| 1428 | return null; |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | $userPath = "users/$id/"; |
||
| 1432 | if (api_get_setting('split_users_upload_directory') === 'true') { |
||
| 1433 | $userPath = 'users/'.substr((string) $id, 0, 1).'/'.$id.'/'; |
||
| 1434 | // In exceptional cases, on some portals, the intermediate base user |
||
| 1435 | // directory might not have been created. Make sure it is before |
||
| 1436 | // going further. |
||
| 1437 | $rootPath = api_get_path(SYS_UPLOAD_PATH) . 'users/' . substr((string) $id, 0, 1); |
||
| 1438 | if (!is_dir($rootPath)) { |
||
| 1439 | $perm = api_get_permissions_for_new_directories(); |
||
| 1440 | try { |
||
| 1441 | mkdir($rootPath, $perm); |
||
| 1442 | } catch (Exception $e) { |
||
| 1443 | // |
||
| 1444 | } |
||
| 1445 | } |
||
| 1446 | } |
||
| 1447 | switch ($type) { |
||
| 1448 | case 'system': // Base: absolute system path. |
||
| 1449 | $userPath = api_get_path(SYS_UPLOAD_PATH).$userPath; |
||
| 1450 | break; |
||
| 1451 | case 'web': // Base: absolute web path. |
||
| 1452 | $userPath = api_get_path(WEB_UPLOAD_PATH).$userPath; |
||
| 1453 | break; |
||
| 1454 | case 'rel': // Relative to the document root (e.g. app/upload/users/1/13/) |
||
| 1455 | $userPath = api_get_path(REL_UPLOAD_PATH).$userPath; |
||
| 1456 | break; |
||
| 1457 | case 'last': // Only the last part starting with users/ |
||
| 1458 | break; |
||
| 1459 | } |
||
| 1460 | |||
| 1461 | return $userPath; |
||
| 1462 | } |
||
| 1463 | |||
| 1464 | /** |
||
| 1465 | * Gets the current user image |
||
| 1466 | * @param string $user_id |
||
| 1467 | * @param int $size it can be USER_IMAGE_SIZE_SMALL, |
||
| 1468 | * USER_IMAGE_SIZE_MEDIUM, USER_IMAGE_SIZE_BIG or USER_IMAGE_SIZE_ORIGINAL |
||
| 1469 | * @param bool $addRandomId |
||
| 1470 | * @param array $userInfo to avoid query the DB |
||
| 1471 | * |
||
| 1472 | * @return string |
||
| 1473 | */ |
||
| 1474 | public static function getUserPicture( |
||
| 1557 | |||
| 1558 | /** |
||
| 1559 | * Creates new user photos in various sizes of a user, or deletes user photos. |
||
| 1560 | * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php |
||
| 1561 | * @param int $user_id The user internal identification number. |
||
| 1562 | * @param string $file The common file name for the newly created photos. |
||
| 1563 | * It will be checked and modified for compatibility with the file system. |
||
| 1564 | * If full name is provided, path component is ignored. |
||
| 1565 | * If an empty name is provided, then old user photos are deleted only, |
||
| 1566 | * @see UserManager::delete_user_picture() as the prefered way for deletion. |
||
| 1567 | * @param string $source_file The full system name of the image from which user photos will be created. |
||
| 1568 | * @param string $cropParameters Optional string that contents "x,y,width,height" of a cropped image format |
||
| 1569 | * @return string/bool Returns the resulting common file name of created images which usually should be stored in database. |
||
| 1570 | * When deletion is requested returns empty string. In case of internal error or negative validation returns FALSE. |
||
| 1571 | */ |
||
| 1572 | public static function update_user_picture($user_id, $file = null, $source_file = null, $cropParameters) |
||
| 1663 | |||
| 1664 | /** |
||
| 1665 | * Update User extra field file type into {user_folder}/{$extra_field} |
||
| 1666 | * @param int $user_id The user internal identification number |
||
| 1667 | * @param string $extra_field The $extra_field The extra field name |
||
| 1668 | * @param null $file The filename |
||
| 1669 | * @param null $source_file The temporal filename |
||
| 1670 | * @return bool|null return filename if success, but false |
||
| 1671 | */ |
||
| 1672 | public static function update_user_extra_file($user_id, $extra_field = '', $file = null, $source_file = null) |
||
| 1708 | |||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * Deletes user photos. |
||
| 1712 | * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php |
||
| 1713 | * @param int $user_id The user internal identitfication number. |
||
| 1714 | * @return string/bool Returns empty string on success, FALSE on error. |
||
| 1715 | */ |
||
| 1716 | public static function delete_user_picture($user_id) |
||
| 1720 | |||
| 1721 | /** |
||
| 1722 | * Returns an XHTML formatted list of productions for a user, or FALSE if he |
||
| 1723 | * doesn't have any. |
||
| 1724 | * |
||
| 1725 | * If there has been a request to remove a production, the function will return |
||
| 1726 | * without building the list unless forced to do so by the optional second |
||
| 1727 | * parameter. This increases performance by avoiding to read through the |
||
| 1728 | * productions on the filesystem before the removal request has been carried |
||
| 1729 | * out because they'll have to be re-read afterwards anyway. |
||
| 1730 | * |
||
| 1731 | * @param int $user_id User id |
||
| 1732 | * @param $force Optional parameter to force building after a removal request |
||
| 1733 | * |
||
| 1734 | * @return A string containing the XHTML code to dipslay the production list, or FALSE |
||
| 1735 | */ |
||
| 1736 | public static function build_production_list($user_id, $force = false, $showdelete = false) |
||
| 1766 | |||
| 1767 | /** |
||
| 1768 | * Returns an array with the user's productions. |
||
| 1769 | * |
||
| 1770 | * @param $user_id User id |
||
| 1771 | * @return array An array containing the user's productions |
||
| 1772 | */ |
||
| 1773 | public static function get_user_productions($user_id) |
||
| 1801 | |||
| 1802 | /** |
||
| 1803 | * Remove a user production. |
||
| 1804 | * |
||
| 1805 | * @param int $user_id User id |
||
| 1806 | * @param string $production The production to remove |
||
| 1807 | */ |
||
| 1808 | public static function remove_user_production($user_id, $production) |
||
| 1818 | |||
| 1819 | /** |
||
| 1820 | * Update an extra field value for a given user |
||
| 1821 | * @param integer $userId User ID |
||
| 1822 | * @param string $variable Field variable name |
||
| 1823 | * @param string $value Field value |
||
| 1824 | * |
||
| 1825 | * @return boolean true if field updated, false otherwise |
||
| 1826 | */ |
||
| 1827 | View Code Duplication | public static function update_extra_field_value($userId, $variable, $value = '') |
|
| 1837 | |||
| 1838 | /** |
||
| 1839 | * Get an array of extra fields with field details (type, default value and options) |
||
| 1840 | * @param integer Offset (from which row) |
||
| 1841 | * @param integer Number of items |
||
| 1842 | * @param integer Column on which sorting is made |
||
| 1843 | * @param string Sorting direction |
||
| 1844 | * @param boolean Optional. Whether we get all the fields or just the visible ones |
||
| 1845 | * @param int Optional. Whether we get all the fields with field_filter 1 or 0 or everything |
||
| 1846 | * @return array Extra fields details (e.g. $list[2]['type'], $list[4]['options'][2]['title'] |
||
| 1847 | */ |
||
| 1848 | public static function get_extra_fields( |
||
| 1922 | |||
| 1923 | /** |
||
| 1924 | * Build a list of extra file already uploaded in $user_folder/{$extra_field}/ |
||
| 1925 | * @param $user_id |
||
| 1926 | * @param $extra_field |
||
| 1927 | * @param bool $force |
||
| 1928 | * @param bool $showdelete |
||
| 1929 | * @return bool|string |
||
| 1930 | */ |
||
| 1931 | public static function build_user_extra_file_list($user_id, $extra_field, $force = false, $showdelete = false) |
||
| 1932 | { |
||
| 1933 | if (!$force && !empty($_POST['remove_'.$extra_field])) { |
||
| 1934 | return true; // postpone reading from the filesystem |
||
| 1935 | } |
||
| 1936 | |||
| 1937 | $extra_files = self::get_user_extra_files($user_id, $extra_field); |
||
| 1938 | if (empty($extra_files)) { |
||
| 1939 | return false; |
||
| 1940 | } |
||
| 1941 | |||
| 1942 | $path_info = self::get_user_picture_path_by_id($user_id, 'web'); |
||
| 1943 | $path = $path_info['dir']; |
||
| 1944 | $del_image = Display::returnIconPath('delete.png'); |
||
| 1945 | |||
| 1946 | $del_text = get_lang('Delete'); |
||
| 1962 | |||
| 1963 | /** |
||
| 1964 | * Get valid filenames in $user_folder/{$extra_field}/ |
||
| 1965 | * @param $user_id |
||
| 1966 | * @param $extra_field |
||
| 1967 | * @param bool $full_path |
||
| 1968 | * @return array |
||
| 1969 | */ |
||
| 1970 | public static function get_user_extra_files($user_id, $extra_field, $full_path = false) |
||
| 2000 | |||
| 2001 | /** |
||
| 2002 | * Remove an {$extra_file} from the user folder $user_folder/{$extra_field}/ |
||
| 2003 | * @param $user_id |
||
| 2004 | * @param $extra_field |
||
| 2005 | * @param $extra_file |
||
| 2006 | * @return bool |
||
| 2007 | */ |
||
| 2008 | public static function remove_user_extra_file($user_id, $extra_field, $extra_file) |
||
| 2023 | |||
| 2024 | /** |
||
| 2025 | * Creates a new extra field |
||
| 2026 | * @param string $variable Field's internal variable name |
||
| 2027 | * @param int $fieldType Field's type |
||
| 2028 | * @param string $displayText Field's language var name |
||
| 2029 | * @param string $default Field's default value |
||
| 2030 | * @return int |
||
| 2031 | */ |
||
| 2032 | View Code Duplication | public static function create_extra_field($variable, $fieldType, $displayText, $default) |
|
| 2044 | |||
| 2045 | /** |
||
| 2046 | * Check if a field is available |
||
| 2047 | * @param string th$variable |
||
| 2048 | * @return boolean |
||
| 2049 | */ |
||
| 2050 | public static function is_extra_field_available($variable) |
||
| 2056 | |||
| 2057 | /** |
||
| 2058 | * Gets user extra fields data |
||
| 2059 | * @param integer User ID |
||
| 2060 | * @param boolean Whether to prefix the fields indexes with "extra_" (might be used by formvalidator) |
||
| 2061 | * @param boolean Whether to return invisible fields as well |
||
| 2062 | * @param boolean Whether to split multiple-selection fields or not |
||
| 2063 | * @return array Array of fields => value for the given user |
||
| 2064 | */ |
||
| 2065 | public static function get_extra_user_data( |
||
| 2152 | |||
| 2153 | /** Get extra user data by field |
||
| 2154 | * @param int user ID |
||
| 2155 | * @param string the internal variable name of the field |
||
| 2156 | * @return array with extra data info of a user i.e array('field_variable'=>'value'); |
||
| 2157 | */ |
||
| 2158 | public static function get_extra_user_data_by_field( |
||
| 2217 | |||
| 2218 | /** |
||
| 2219 | * Get the extra field information for a certain field (the options as well) |
||
| 2220 | * @param int $variable The name of the field we want to know everything about |
||
| 2221 | * @return array Array containing all the information about the extra profile field |
||
| 2222 | * (first level of array contains field details, then 'options' sub-array contains options details, |
||
| 2223 | * as returned by the database) |
||
| 2224 | * @author Julio Montoya |
||
| 2225 | * @since v1.8.6 |
||
| 2226 | */ |
||
| 2227 | public static function get_extra_field_information_by_name($variable) |
||
| 2233 | |||
| 2234 | /** |
||
| 2235 | * @param string $type |
||
| 2236 | * |
||
| 2237 | * @return array |
||
| 2238 | */ |
||
| 2239 | public static function get_all_extra_field_by_type($type) |
||
| 2245 | |||
| 2246 | /** |
||
| 2247 | * Get all the extra field information of a certain field (also the options) |
||
| 2248 | * |
||
| 2249 | * @param int $field_name the name of the field we want to know everything of |
||
| 2250 | * @return array $return containing all th information about the extra profile field |
||
| 2251 | * @author Julio Montoya |
||
| 2252 | * @deprecated |
||
| 2253 | * @since v1.8.6 |
||
| 2254 | */ |
||
| 2255 | public static function get_extra_field_information($fieldId) |
||
| 2261 | |||
| 2262 | /** Get extra user data by value |
||
| 2263 | * @param string the internal variable name of the field |
||
| 2264 | * @param string the internal value of the field |
||
| 2265 | * @return array with extra data info of a user i.e array('field_variable'=>'value'); |
||
| 2266 | */ |
||
| 2267 | public static function get_extra_user_data_by_value($field_variable, $field_value, $all_visibility = true) |
||
| 2288 | |||
| 2289 | /** |
||
| 2290 | * Get extra user data by field variable |
||
| 2291 | * @param string field variable |
||
| 2292 | * @return array data |
||
| 2293 | */ |
||
| 2294 | public static function get_extra_user_data_by_field_variable($field_variable) |
||
| 2311 | |||
| 2312 | /** |
||
| 2313 | * Gives a list of [session_category][session_id] for the current user. |
||
| 2314 | * @param integer $user_id |
||
| 2315 | * @param boolean whether to fill the first element or not (to give space for courses out of categories) |
||
| 2316 | * @param boolean optional true if limit time from session is over, false otherwise |
||
| 2317 | * @param boolean $ignoreTimeLimit ignore time start/end |
||
| 2318 | * @return array list of statuses [session_category][session_id] |
||
| 2319 | * |
||
| 2320 | * @todo ensure multiple access urls are managed correctly |
||
| 2321 | */ |
||
| 2322 | public static function get_sessions_by_category( |
||
| 2471 | |||
| 2472 | /** |
||
| 2473 | * Gives a list of [session_id-course_code] => [status] for the current user. |
||
| 2474 | * @param integer $user_id |
||
| 2475 | * @return array list of statuses (session_id-course_code => status) |
||
| 2476 | */ |
||
| 2477 | public static function get_personal_session_course_list($user_id) |
||
| 2683 | |||
| 2684 | /** |
||
| 2685 | * Gives a list of courses for the given user in the given session |
||
| 2686 | * @param integer $user_id |
||
| 2687 | * @param integer $session_id |
||
| 2688 | * @return array list of statuses (session_id-course_code => status) |
||
| 2689 | */ |
||
| 2690 | public static function get_courses_list_by_session($user_id, $session_id) |
||
| 2807 | |||
| 2808 | /** |
||
| 2809 | * Get user id from a username |
||
| 2810 | * @param string Username |
||
| 2811 | * @return int User ID (or false if not found) |
||
| 2812 | */ |
||
| 2813 | public static function get_user_id_from_username($username) |
||
| 2832 | |||
| 2833 | /** |
||
| 2834 | * Get the users files upload from his share_folder |
||
| 2835 | * @param string User ID |
||
| 2836 | * @param string course directory |
||
| 2837 | * @param string resourcetype: images, all |
||
| 2838 | * @return int User ID (or false if not found) |
||
| 2839 | */ |
||
| 2840 | public static function get_user_upload_files_by_course($user_id, $course, $resourcetype = 'all') |
||
| 2881 | |||
| 2882 | /** |
||
| 2883 | * Gets the API key (or keys) and return them into an array |
||
| 2884 | * @param int Optional user id (defaults to the result of api_get_user_id()) |
||
| 2885 | * @return array Non-indexed array containing the list of API keys for this user, or FALSE on error |
||
| 2886 | */ |
||
| 2887 | public static function get_api_keys($user_id = null, $api_service = 'dokeos') |
||
| 2914 | |||
| 2915 | /** |
||
| 2916 | * Adds a new API key to the users' account |
||
| 2917 | * @param int Optional user ID (defaults to the results of api_get_user_id()) |
||
| 2918 | * @return boolean True on success, false on failure |
||
| 2919 | */ |
||
| 2920 | public static function add_api_key($user_id = null, $api_service = 'dokeos') |
||
| 2942 | |||
| 2943 | /** |
||
| 2944 | * Deletes an API key from the user's account |
||
| 2945 | * @param int API key's internal ID |
||
| 2946 | * @return boolean True on success, false on failure |
||
| 2947 | */ |
||
| 2948 | public static function delete_api_key($key_id) |
||
| 2968 | |||
| 2969 | /** |
||
| 2970 | * Regenerate an API key from the user's account |
||
| 2971 | * @param int user ID (defaults to the results of api_get_user_id()) |
||
| 2972 | * @param string API key's internal ID |
||
| 2973 | * @return int num |
||
| 2974 | */ |
||
| 2975 | public static function update_api_key($user_id, $api_service) |
||
| 2998 | |||
| 2999 | /** |
||
| 3000 | * @param int user ID (defaults to the results of api_get_user_id()) |
||
| 3001 | * @param string API key's internal ID |
||
| 3002 | * @return int row ID, or return false if not found |
||
| 3003 | */ |
||
| 3004 | public static function get_api_key_id($user_id, $api_service) |
||
| 3022 | |||
| 3023 | /** |
||
| 3024 | * Checks if a user_id is platform admin |
||
| 3025 | * @param int user ID |
||
| 3026 | * @return boolean True if is admin, false otherwise |
||
| 3027 | * @see main_api.lib.php::api_is_platform_admin() for a context-based check |
||
| 3028 | */ |
||
| 3029 | public static function is_admin($user_id) |
||
| 3039 | |||
| 3040 | /** |
||
| 3041 | * Get the total count of users |
||
| 3042 | * @param int Status of users to be counted |
||
| 3043 | * @param int Access URL ID (optional) |
||
| 3044 | * @return mixed Number of users or false on error |
||
| 3045 | */ |
||
| 3046 | public static function get_number_of_users($status = 0, $access_url_id = null) |
||
| 3066 | |||
| 3067 | /** |
||
| 3068 | * @author Isaac flores <[email protected]> |
||
| 3069 | * @param string The email administrator |
||
| 3070 | * @param integer The user id |
||
| 3071 | * @param string The message title |
||
| 3072 | * @param string The content message |
||
| 3073 | */ |
||
| 3074 | public static function send_message_in_outbox($email_administrator, $user_id, $title, $content) |
||
| 3098 | |||
| 3099 | /* |
||
| 3100 | * |
||
| 3101 | * USER TAGS |
||
| 3102 | * |
||
| 3103 | * Intructions to create a new user tag by Julio Montoya <[email protected]> |
||
| 3104 | * |
||
| 3105 | * 1. Create a new extra field in main/admin/user_fields.php with the "TAG" field type make it available and visible. Called it "books" for example. |
||
| 3106 | * 2. Go to profile main/auth/profile.php There you will see a special input (facebook style) that will show suggestions of tags. |
||
| 3107 | * 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 |
||
| 3108 | * 4. Tags are independent this means that tags can't be shared between tags + book + hobbies. |
||
| 3109 | * 5. Test and enjoy. |
||
| 3110 | * |
||
| 3111 | */ |
||
| 3112 | |||
| 3113 | /** |
||
| 3114 | * Gets the tags of a specific field_id |
||
| 3115 | * |
||
| 3116 | * @param int field_id |
||
| 3117 | * @param string how we are going to result value in array or in a string (json) |
||
| 3118 | * @return mixed |
||
| 3119 | * @since Nov 2009 |
||
| 3120 | * @version 1.8.6.2 |
||
| 3121 | */ |
||
| 3122 | public static function get_tags($tag, $field_id, $return_format = 'json', $limit = 10) |
||
| 3145 | |||
| 3146 | /** |
||
| 3147 | * @param int $field_id |
||
| 3148 | * @param int $limit |
||
| 3149 | * @return array |
||
| 3150 | */ |
||
| 3151 | public static function get_top_tags($field_id, $limit = 100) |
||
| 3175 | |||
| 3176 | /** |
||
| 3177 | * Get user's tags |
||
| 3178 | * @param int field_id |
||
| 3179 | * @param int user_id |
||
| 3180 | * @return array |
||
| 3181 | */ |
||
| 3182 | public static function get_user_tags($user_id, $field_id) |
||
| 3207 | |||
| 3208 | /** |
||
| 3209 | * Get user's tags |
||
| 3210 | * @param int user_id |
||
| 3211 | * @param int field_id |
||
| 3212 | * @param bool show links or not |
||
| 3213 | * @return array |
||
| 3214 | */ |
||
| 3215 | public static function get_user_tags_to_string($user_id, $field_id, $show_links = true) |
||
| 3253 | |||
| 3254 | /** |
||
| 3255 | * Get the tag id |
||
| 3256 | * @param int tag |
||
| 3257 | * @param int field_id |
||
| 3258 | * @return int returns 0 if fails otherwise the tag id |
||
| 3259 | */ |
||
| 3260 | public static function get_tag_id($tag, $field_id) |
||
| 3276 | |||
| 3277 | /** |
||
| 3278 | * Get the tag id |
||
| 3279 | * @param int tag |
||
| 3280 | * @param int field_id |
||
| 3281 | * @return int 0 if fails otherwise the tag id |
||
| 3282 | */ |
||
| 3283 | View Code Duplication | public static function get_tag_id_from_id($tag_id, $field_id) |
|
| 3298 | |||
| 3299 | /** |
||
| 3300 | * Adds a user-tag value |
||
| 3301 | * @param mixed tag |
||
| 3302 | * @param int The user id |
||
| 3303 | * @param int field id of the tag |
||
| 3304 | * @return bool |
||
| 3305 | */ |
||
| 3306 | public static function add_tag($tag, $user_id, $field_id) |
||
| 3362 | |||
| 3363 | /** |
||
| 3364 | * Deletes an user tag |
||
| 3365 | * @param int user id |
||
| 3366 | * @param int field id |
||
| 3367 | * |
||
| 3368 | */ |
||
| 3369 | public static function delete_user_tags($user_id, $field_id) |
||
| 3387 | |||
| 3388 | /** |
||
| 3389 | * Process the tag list comes from the UserManager::update_extra_field_value() function |
||
| 3390 | * @param array the tag list that will be added |
||
| 3391 | * @param int user id |
||
| 3392 | * @param int field id |
||
| 3393 | * @return bool |
||
| 3394 | */ |
||
| 3395 | public static function process_tags($tags, $user_id, $field_id) |
||
| 3408 | |||
| 3409 | /** |
||
| 3410 | * Returns a list of all administrators |
||
| 3411 | * @author jmontoya |
||
| 3412 | * @return array |
||
| 3413 | */ |
||
| 3414 | public static function get_all_administrators() |
||
| 3444 | |||
| 3445 | /** |
||
| 3446 | * Search an user (tags, first name, last name and email ) |
||
| 3447 | * @param string $tag |
||
| 3448 | * @param int $field_id field id of the tag |
||
| 3449 | * @param int $from where to start in the query |
||
| 3450 | * @param int $number_of_items |
||
| 3451 | * @param bool $getCount get count or not |
||
| 3452 | * @return array |
||
| 3453 | */ |
||
| 3454 | public static function get_all_user_tags( |
||
| 3539 | |||
| 3540 | /** |
||
| 3541 | * Get extra filtrable user fields (only type select) |
||
| 3542 | * @return array |
||
| 3543 | */ |
||
| 3544 | public static function get_extra_filtrable_fields() |
||
| 3566 | |||
| 3567 | /** |
||
| 3568 | * Get extra where clauses for finding users based on extra filtrable user fields (type select) |
||
| 3569 | * @return string With AND clauses based on user's ID which have the values to search in extra user fields |
||
| 3570 | */ |
||
| 3571 | public static function get_search_form_where_extra_fields() |
||
| 3613 | |||
| 3614 | /** |
||
| 3615 | * Show the search form |
||
| 3616 | * @param string $query the value of the search box |
||
| 3617 | * @return string HTML form |
||
| 3618 | */ |
||
| 3619 | public static function get_search_form($query) |
||
| 3684 | |||
| 3685 | /** |
||
| 3686 | * Shows the user menu |
||
| 3687 | */ |
||
| 3688 | public static function show_menu() |
||
| 3699 | |||
| 3700 | /** |
||
| 3701 | * Allow to register contact to social network |
||
| 3702 | * @param int $friend_id user friend id |
||
| 3703 | * @param int $my_user_id user id |
||
| 3704 | * @param int $relation_type relation between users see constants definition |
||
| 3705 | */ |
||
| 3706 | public static function relate_users($friend_id, $my_user_id, $relation_type) |
||
| 3754 | |||
| 3755 | /** |
||
| 3756 | * Deletes a contact |
||
| 3757 | * @param int user friend id |
||
| 3758 | * @param bool true will delete ALL friends relationship from $friend_id |
||
| 3759 | * @author isaac flores paz <[email protected]> |
||
| 3760 | * @author Julio Montoya <[email protected]> Cleaning code |
||
| 3761 | */ |
||
| 3762 | public static function remove_user_rel_user($friend_id, $real_removed = false, $with_status_condition = '') |
||
| 3806 | |||
| 3807 | /** |
||
| 3808 | * @param int $userId |
||
| 3809 | * @return array |
||
| 3810 | */ |
||
| 3811 | public static function getDrhListFromUser($userId) |
||
| 3839 | |||
| 3840 | /** |
||
| 3841 | * get users followed by human resource manager |
||
| 3842 | * @param int $userId |
||
| 3843 | * @param int $userStatus (STUDENT, COURSEMANAGER, etc) |
||
| 3844 | * @param bool $getOnlyUserId |
||
| 3845 | * @param bool $getSql |
||
| 3846 | * @param bool $getCount |
||
| 3847 | * @param int $from |
||
| 3848 | * @param int $numberItems |
||
| 3849 | * @param int $column |
||
| 3850 | * @param string $direction |
||
| 3851 | * @param int $active |
||
| 3852 | * @param string $lastConnectionDate |
||
| 3853 | * @return array users |
||
| 3854 | */ |
||
| 3855 | View Code Duplication | public static function get_users_followed_by_drh( |
|
| 3883 | |||
| 3884 | /** |
||
| 3885 | * Get users followed by human resource manager |
||
| 3886 | * @param int $userId |
||
| 3887 | * @param int $userStatus Filter users by status (STUDENT, COURSEMANAGER, etc) |
||
| 3888 | * @param bool $getOnlyUserId |
||
| 3889 | * @param bool $getSql |
||
| 3890 | * @param bool $getCount |
||
| 3891 | * @param int $from |
||
| 3892 | * @param int $numberItems |
||
| 3893 | * @param int $column |
||
| 3894 | * @param string $direction |
||
| 3895 | * @param int $active |
||
| 3896 | * @param string $lastConnectionDate |
||
| 3897 | * @param int $status the function is called by who? COURSEMANAGER, DRH? |
||
| 3898 | * @param string $keyword |
||
| 3899 | * |
||
| 3900 | * @return array user list |
||
| 3901 | */ |
||
| 3902 | public static function getUsersFollowedByUser( |
||
| 4101 | |||
| 4102 | /** |
||
| 4103 | * Subscribes users to human resource manager (Dashboard feature) |
||
| 4104 | * @param int hr dept id |
||
| 4105 | * @param array Users id |
||
| 4106 | * @param int affected rows |
||
| 4107 | * */ |
||
| 4108 | public static function suscribe_users_to_hr_manager($hr_dept_id, $users_id) |
||
| 4112 | |||
| 4113 | /** |
||
| 4114 | * Add subscribed users to a user by relation type |
||
| 4115 | * @param int $userId The user id |
||
| 4116 | * @param array $subscribedUsersId The id of suscribed users |
||
| 4117 | * @param action $relationType The relation type |
||
| 4118 | */ |
||
| 4119 | public static function subscribeUsersToUser($userId, $subscribedUsersId, $relationType) |
||
| 4169 | |||
| 4170 | /** |
||
| 4171 | * This function check if an user is followed by human resources manager |
||
| 4172 | * @param int User id |
||
| 4173 | * @param int Human resources manager |
||
| 4174 | * @return bool |
||
| 4175 | */ |
||
| 4176 | public static function is_user_followed_by_drh($user_id, $hr_dept_id) |
||
| 4195 | |||
| 4196 | /** |
||
| 4197 | * get user id of teacher or session administrator |
||
| 4198 | * @param array $courseInfo |
||
| 4199 | * |
||
| 4200 | * @return int The user id |
||
| 4201 | */ |
||
| 4202 | public static function get_user_id_of_course_admin_or_session_admin($courseInfo) |
||
| 4241 | |||
| 4242 | /** |
||
| 4243 | * Determines if a user is a gradebook certified |
||
| 4244 | * @param int The category id of gradebook |
||
| 4245 | * @param int The user id |
||
| 4246 | * @return boolean |
||
| 4247 | */ |
||
| 4248 | public static function is_user_certified($cat_id, $user_id) |
||
| 4263 | |||
| 4264 | /** |
||
| 4265 | * Gets the info about a gradebook certificate for a user by course |
||
| 4266 | * @param string The course code |
||
| 4267 | * @param int The user id |
||
| 4268 | * @return array if there is not information return false |
||
| 4269 | */ |
||
| 4270 | public static function get_info_gradebook_certificate($course_code, $user_id) |
||
| 4304 | |||
| 4305 | /** |
||
| 4306 | * Gets the user path of user certificated |
||
| 4307 | * @param int The user id |
||
| 4308 | * @return array containing path_certificate and cat_id |
||
| 4309 | */ |
||
| 4310 | public static function get_user_path_certificate($user_id) |
||
| 4336 | |||
| 4337 | /** |
||
| 4338 | * This function check if the user is a coach inside session course |
||
| 4339 | * @param int User id |
||
| 4340 | * @param int $courseId |
||
| 4341 | * @param int Session id |
||
| 4342 | * @return bool True if the user is a coach |
||
| 4343 | * |
||
| 4344 | */ |
||
| 4345 | public static function is_session_course_coach($user_id, $courseId, $session_id) |
||
| 4367 | |||
| 4368 | /** |
||
| 4369 | * This function returns an icon path that represents the favicon of the website of which the url given. |
||
| 4370 | * Defaults to the current Chamilo favicon |
||
| 4371 | * @param string URL of website where to look for favicon.ico |
||
| 4372 | * @param string Optional second URL of website where to look for favicon.ico |
||
| 4373 | * @return string Path of icon to load |
||
| 4374 | */ |
||
| 4375 | public static function get_favicon_from_url($url1, $url2 = null) |
||
| 4392 | |||
| 4393 | /** |
||
| 4394 | * |
||
| 4395 | * @param int student id |
||
| 4396 | * @param int years |
||
| 4397 | * @param bool show warning_message |
||
| 4398 | * @param bool return_timestamp |
||
| 4399 | */ |
||
| 4400 | public static function delete_inactive_student($student_id, $years = 2, $warning_message = false, $return_timestamp = false) |
||
| 4436 | |||
| 4437 | /** |
||
| 4438 | * @param FormValidator $form |
||
| 4439 | * @param $extra_data |
||
| 4440 | * @param $form_name |
||
| 4441 | * @param bool $admin_permissions |
||
| 4442 | * @param null $user_id |
||
| 4443 | * @deprecated |
||
| 4444 | * @return array |
||
| 4445 | */ |
||
| 4446 | static function set_extra_fields_in_form( |
||
| 4729 | |||
| 4730 | /** |
||
| 4731 | * @return array |
||
| 4732 | */ |
||
| 4733 | static function get_user_field_types() |
||
| 4753 | |||
| 4754 | /** |
||
| 4755 | * @param int $userId |
||
| 4756 | */ |
||
| 4757 | View Code Duplication | static function add_user_as_admin($userId) |
|
| 4767 | |||
| 4768 | /** |
||
| 4769 | * @param int $userId |
||
| 4770 | */ |
||
| 4771 | View Code Duplication | public static function remove_user_admin($userId) |
|
| 4780 | |||
| 4781 | /** |
||
| 4782 | * @param string $from |
||
| 4783 | * @param string $to |
||
| 4784 | */ |
||
| 4785 | public static function update_all_user_languages($from, $to) |
||
| 4797 | |||
| 4798 | /** |
||
| 4799 | * Subscribe users to student boss |
||
| 4800 | * @param int $bossId The boss id |
||
| 4801 | * @param array $usersId The users array |
||
| 4802 | * @return int Affected rows |
||
| 4803 | */ |
||
| 4804 | public static function subscribeUsersToBoss($bossId, $usersId) |
||
| 4808 | |||
| 4809 | /** |
||
| 4810 | * Get users followed by student boss |
||
| 4811 | * @param int $userId |
||
| 4812 | * @param int $userStatus (STUDENT, COURSEMANAGER, etc) |
||
| 4813 | * @param bool $getOnlyUserId |
||
| 4814 | * @param bool $getSql |
||
| 4815 | * @param bool $getCount |
||
| 4816 | * @param int $from |
||
| 4817 | * @param int $numberItems |
||
| 4818 | * @param int $column |
||
| 4819 | * @param string $direction |
||
| 4820 | * @param int $active |
||
| 4821 | * @param string $lastConnectionDate |
||
| 4822 | * @return array users |
||
| 4823 | */ |
||
| 4824 | View Code Duplication | public static function getUsersFollowedByStudentBoss( |
|
| 4842 | |||
| 4843 | /** |
||
| 4844 | * Get the teacher (users with COURSEMANGER status) list |
||
| 4845 | * @return array The list |
||
| 4846 | */ |
||
| 4847 | public static function getTeachersList() |
||
| 4863 | |||
| 4864 | /** |
||
| 4865 | * @return array |
||
| 4866 | */ |
||
| 4867 | View Code Duplication | public static function getOfficialCodeGrouped() |
|
| 4883 | |||
| 4884 | /** |
||
| 4885 | * @param string $officialCode |
||
| 4886 | * @return array |
||
| 4887 | */ |
||
| 4888 | public static function getUsersByOfficialCode($officialCode) |
||
| 4905 | |||
| 4906 | /** |
||
| 4907 | * Calc the expended time (in seconds) by a user in a course |
||
| 4908 | * @param int $userId The user id |
||
| 4909 | * @param int $courseId The course id |
||
| 4910 | * @param int $sessionId Optional. The session id |
||
| 4911 | * @param string $from Optional. From date |
||
| 4912 | * @param string $until Optional. Until date |
||
| 4913 | * @return int The time |
||
| 4914 | */ |
||
| 4915 | public static function getTimeSpentInCourses($userId, $courseId, $sessionId = 0, $from = '', $until = '') |
||
| 4947 | |||
| 4948 | /** |
||
| 4949 | * Get the boss user ID from a followed user id |
||
| 4950 | * @param $userId |
||
| 4951 | * @return bool |
||
| 4952 | */ |
||
| 4953 | public static function getStudentBoss($userId) |
||
| 4978 | |||
| 4979 | /** |
||
| 4980 | * Get either a Gravatar URL or complete image tag for a specified email address. |
||
| 4981 | * |
||
| 4982 | * @param string $email The email address |
||
| 4983 | * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
||
| 4984 | * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
||
| 4985 | * @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
||
| 4986 | * @param boole $img True to return a complete IMG tag False for just the URL |
||
| 4987 | * @param array $atts Optional, additional key/value attributes to include in the IMG tag |
||
| 4988 | * @return String containing either just a URL or a complete image tag |
||
| 4989 | * @source http://gravatar.com/site/implement/images/php/ |
||
| 4990 | */ |
||
| 4991 | private static function getGravatar( |
||
| 5013 | |||
| 5014 | |||
| 5015 | |||
| 5016 | /** |
||
| 5017 | * Displays the name of the user and makes the link to the user profile |
||
| 5018 | * @param array $userInfo |
||
| 5019 | * |
||
| 5020 | * @return string |
||
| 5021 | */ |
||
| 5022 | public static function getUserProfileLink($userInfo) |
||
| 5030 | |||
| 5031 | /** |
||
| 5032 | * Displays the name of the user and makes the link to the user profile |
||
| 5033 | * |
||
| 5034 | * @param $userInfo |
||
| 5035 | * |
||
| 5036 | * @return string |
||
| 5037 | */ |
||
| 5038 | public static function getUserProfileLinkWithPicture($userInfo) |
||
| 5042 | |||
| 5043 | /** |
||
| 5044 | * Get users whose name matches $firstname and $lastname |
||
| 5045 | * @param string $firstname Firstname to search |
||
| 5046 | * @param string $lastname Lastname to search |
||
| 5047 | * @return array The user list |
||
| 5048 | */ |
||
| 5049 | View Code Duplication | public static function getUserByName($firstname, $lastname) |
|
| 5073 | |||
| 5074 | /** |
||
| 5075 | * @param int $optionSelected |
||
| 5076 | * @return string |
||
| 5077 | */ |
||
| 5078 | public static function getUserSubscriptionTab($optionSelected = 1) |
||
| 5116 | |||
| 5117 | } |
||
| 5118 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: