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( |
||
| 224 | $firstName, |
||
| 225 | $lastName, |
||
| 226 | $status, |
||
| 227 | $email, |
||
| 228 | $loginName, |
||
| 229 | $password, |
||
| 230 | $official_code = '', |
||
| 231 | $language = '', |
||
| 232 | $phone = '', |
||
| 233 | $picture_uri = '', |
||
| 234 | $auth_source = PLATFORM_AUTH_SOURCE, |
||
| 235 | $expirationDate = null, |
||
| 236 | $active = 1, |
||
| 237 | $hr_dept_id = 0, |
||
| 238 | $extra = null, |
||
| 239 | $encrypt_method = '', |
||
| 240 | $send_mail = false, |
||
| 241 | $isAdmin = false |
||
| 242 | ) { |
||
| 243 | $currentUserId = api_get_user_id(); |
||
| 244 | $hook = HookCreateUser::create(); |
||
| 245 | if (!empty($hook)) { |
||
| 246 | $hook->notifyCreateUser(HOOK_EVENT_TYPE_PRE); |
||
| 247 | } |
||
| 248 | global $_configuration; |
||
| 249 | $original_password = $password; |
||
| 250 | $access_url_id = 1; |
||
| 251 | |||
| 252 | if (api_get_multiple_access_url()) { |
||
| 253 | $access_url_id = api_get_current_access_url_id(); |
||
| 254 | } |
||
| 255 | |||
| 256 | View Code Duplication | if (is_array($_configuration[$access_url_id]) && |
|
| 257 | isset($_configuration[$access_url_id]['hosting_limit_users']) && |
||
| 258 | $_configuration[$access_url_id]['hosting_limit_users'] > 0) { |
||
| 259 | $num = self::get_number_of_users(); |
||
| 260 | if ($num >= $_configuration[$access_url_id]['hosting_limit_users']) { |
||
| 261 | api_warn_hosting_contact('hosting_limit_users'); |
||
| 262 | Display::addFlash(Display::return_message(get_lang('PortalUsersLimitReached'), 'warning')); |
||
| 263 | |||
| 264 | return false; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | View Code Duplication | if ($status === 1 && |
|
| 269 | is_array($_configuration[$access_url_id]) && |
||
| 270 | isset($_configuration[$access_url_id]['hosting_limit_teachers']) && |
||
| 271 | $_configuration[$access_url_id]['hosting_limit_teachers'] > 0 |
||
| 272 | ) { |
||
| 273 | $num = self::get_number_of_users(1); |
||
| 274 | if ($num >= $_configuration[$access_url_id]['hosting_limit_teachers']) { |
||
| 275 | Display::addFlash(Display::return_message(get_lang('PortalTeachersLimitReached'), 'warning')); |
||
| 276 | api_warn_hosting_contact('hosting_limit_teachers'); |
||
| 277 | |||
| 278 | return false; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | View Code Duplication | if (empty($password)) { |
|
| 283 | Display::addFlash(Display::return_message(get_lang('ThisFieldIsRequired').': '.get_lang('Password') , 'warning')); |
||
| 284 | |||
| 285 | return false; |
||
| 286 | } |
||
| 287 | |||
| 288 | // database table definition |
||
| 289 | $table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 290 | |||
| 291 | //Checking the user language |
||
| 292 | $languages = api_get_languages(); |
||
| 293 | $language = strtolower($language); |
||
| 294 | if (!in_array($language, $languages['folder'])) { |
||
| 295 | $language = api_get_setting('platformLanguage'); |
||
| 296 | } |
||
| 297 | |||
| 298 | if (!empty($currentUserId)) { |
||
| 299 | $creator_id = $currentUserId; |
||
| 300 | } else { |
||
| 301 | $creator_id = 0; |
||
| 302 | } |
||
| 303 | |||
| 304 | // First check wether the login already exists |
||
| 305 | if (!self::is_username_available($loginName)) { |
||
| 306 | return api_set_failure('login-pass already taken'); |
||
| 307 | } |
||
| 308 | |||
| 309 | $currentDate = api_get_utc_datetime(); |
||
| 310 | $now = new DateTime($currentDate); |
||
| 311 | |||
| 312 | if (empty($expirationDate) || $expirationDate == '0000-00-00 00:00:00') { |
||
| 313 | // Default expiration date |
||
| 314 | // if there is a default duration of a valid account then |
||
| 315 | // we have to change the expiration_date accordingly |
||
| 316 | // Accept 0000-00-00 00:00:00 as a null value to avoid issues with |
||
| 317 | // third party code using this method with the previous (pre-1.10) |
||
| 318 | // value of 0000... |
||
| 319 | if (api_get_setting('account_valid_duration') != '') { |
||
| 320 | $expirationDate = new DateTime($currentDate); |
||
| 321 | $days = intval(api_get_setting('account_valid_duration')); |
||
| 322 | $expirationDate->modify('+'.$days.' day'); |
||
| 323 | } |
||
| 324 | } else { |
||
| 325 | $expirationDate = api_get_utc_datetime($expirationDate); |
||
| 326 | $expirationDate = new \DateTime($expirationDate, new DateTimeZone('UTC')); |
||
| 327 | } |
||
| 328 | |||
| 329 | $userManager = self::getManager(); |
||
| 330 | |||
| 331 | /** @var User $user */ |
||
| 332 | $user = $userManager->createUser(); |
||
| 333 | $user |
||
| 334 | ->setLastname($lastName) |
||
| 335 | ->setFirstname($firstName) |
||
| 336 | ->setUsername($loginName) |
||
| 337 | ->setStatus($status) |
||
| 338 | ->setPlainPassword($password) |
||
| 339 | ->setEmail($email) |
||
| 340 | ->setOfficialCode($official_code) |
||
| 341 | ->setPictureUri($picture_uri) |
||
| 342 | ->setCreatorId($creator_id) |
||
| 343 | ->setAuthSource($auth_source) |
||
| 344 | ->setPhone($phone) |
||
| 345 | ->setLanguage($language) |
||
| 346 | ->setRegistrationDate($now) |
||
| 347 | ->setHrDeptId($hr_dept_id) |
||
| 348 | ->setActive($active); |
||
| 349 | |||
| 350 | if (!empty($expirationDate)) { |
||
| 351 | $user->setExpirationDate($expirationDate); |
||
| 352 | } |
||
| 353 | |||
| 354 | $userManager->updateUser($user, true); |
||
| 355 | $userId = $user->getId(); |
||
| 356 | |||
| 357 | if (!empty($userId)) { |
||
| 358 | $return = $userId; |
||
| 359 | $sql = "UPDATE $table_user SET user_id = $return WHERE id = $return"; |
||
| 360 | Database::query($sql); |
||
| 361 | |||
| 362 | if ($isAdmin) { |
||
| 363 | UserManager::add_user_as_admin($userId); |
||
| 364 | } |
||
| 365 | |||
| 366 | if (api_get_multiple_access_url()) { |
||
| 367 | UrlManager::add_user_to_url($return, api_get_current_access_url_id()); |
||
| 368 | } else { |
||
| 369 | //we are adding by default the access_url_user table with access_url_id = 1 |
||
| 370 | UrlManager::add_user_to_url($return, 1); |
||
| 371 | } |
||
| 372 | |||
| 373 | if (!empty($email) && $send_mail) { |
||
| 374 | $recipient_name = api_get_person_name( |
||
| 375 | $firstName, |
||
| 376 | $lastName, |
||
| 377 | null, |
||
| 378 | PERSON_NAME_EMAIL_ADDRESS |
||
| 379 | ); |
||
| 380 | $tplSubject = new Template(null, false, false, false, false, false); |
||
| 381 | $layoutSubject = $tplSubject->get_template( |
||
| 382 | 'mail/subject_registration_platform.tpl' |
||
| 383 | ); |
||
| 384 | $emailSubject = $tplSubject->fetch($layoutSubject); |
||
| 385 | $sender_name = api_get_person_name( |
||
| 386 | api_get_setting('administratorName'), |
||
| 387 | api_get_setting('administratorSurname'), |
||
| 388 | null, |
||
| 389 | PERSON_NAME_EMAIL_ADDRESS |
||
| 390 | ); |
||
| 391 | $email_admin = api_get_setting('emailAdministrator'); |
||
| 392 | |||
| 393 | if (api_is_multiple_url_enabled()) { |
||
| 394 | $access_url_id = api_get_current_access_url_id(); |
||
| 395 | if ($access_url_id != -1) { |
||
| 396 | $url = api_get_access_url($access_url_id); |
||
| 397 | } |
||
| 398 | } else { |
||
| 399 | $url = api_get_path(WEB_PATH); |
||
| 400 | } |
||
| 401 | $tplContent = new Template(null, false, false, false, false, false); |
||
| 402 | // variables for the default template |
||
| 403 | $tplContent->assign('complete_name', stripslashes(api_get_person_name($firstName, $lastName))); |
||
| 404 | $tplContent->assign('login_name', $loginName); |
||
| 405 | $tplContent->assign('original_password', stripslashes($original_password)); |
||
| 406 | $tplContent->assign('mailWebPath', $url); |
||
| 407 | |||
| 408 | $layoutContent = $tplContent->get_template('mail/content_registration_platform.tpl'); |
||
| 409 | $emailBody = $tplContent->fetch($layoutContent); |
||
| 410 | /* MANAGE EVENT WITH MAIL */ |
||
| 411 | if (EventsMail::check_if_using_class('user_registration')) { |
||
| 412 | $values["about_user"] = $return; |
||
| 413 | $values["password"] = $original_password; |
||
| 414 | $values["send_to"] = array($return); |
||
| 415 | $values["prior_lang"] = null; |
||
| 416 | EventsDispatcher::events('user_registration', $values); |
||
| 417 | } else { |
||
| 418 | $phoneNumber = isset($extra['mobile_phone_number']) ? $extra['mobile_phone_number'] : null; |
||
| 419 | |||
| 420 | $additionalParameters = array( |
||
| 421 | 'smsType' => SmsPlugin::WELCOME_LOGIN_PASSWORD, |
||
| 422 | 'userId' => $return, |
||
| 423 | 'mobilePhoneNumber' => $phoneNumber, |
||
| 424 | 'password' => $original_password |
||
| 425 | ); |
||
| 426 | |||
| 427 | api_mail_html( |
||
| 428 | $recipient_name, |
||
| 429 | $email, |
||
| 430 | $emailSubject, |
||
| 431 | $emailBody, |
||
| 432 | $sender_name, |
||
| 433 | $email_admin, |
||
| 434 | null, |
||
| 435 | null, |
||
| 436 | null, |
||
| 437 | $additionalParameters |
||
| 438 | ); |
||
| 439 | } |
||
| 440 | /* ENDS MANAGE EVENT WITH MAIL */ |
||
| 441 | } |
||
| 442 | Event::addEvent(LOG_USER_CREATE, LOG_USER_ID, $return); |
||
| 443 | } else { |
||
| 444 | return api_set_failure('error inserting in Database'); |
||
| 445 | } |
||
| 446 | |||
| 447 | View Code Duplication | if (is_array($extra) && count($extra) > 0) { |
|
| 448 | $res = true; |
||
| 449 | foreach ($extra as $fname => $fvalue) { |
||
| 450 | $res = $res && self::update_extra_field_value($return, $fname, $fvalue); |
||
| 451 | } |
||
| 452 | } |
||
| 453 | self::update_extra_field_value($return, 'already_logged_in', 'false'); |
||
| 454 | |||
| 455 | if (!empty($hook)) { |
||
| 456 | $hook->setEventData(array( |
||
| 457 | 'return' => $return, |
||
| 458 | 'originalPassword' => $original_password |
||
| 459 | )); |
||
| 460 | $hook->notifyCreateUser(HOOK_EVENT_TYPE_POST); |
||
| 461 | } |
||
| 462 | |||
| 463 | return $return; |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Can user be deleted? This function checks whether there's a course |
||
| 468 | * in which the given user is the |
||
| 469 | * only course administrator. If that is the case, the user can't be |
||
| 470 | * deleted because the course would remain without a course admin. |
||
| 471 | * @param int $user_id The user id |
||
| 472 | * @return boolean true if user can be deleted |
||
| 473 | * @assert (null) === false |
||
| 474 | * @assert (-1) === false |
||
| 475 | * @assert ('abc') === false |
||
| 476 | */ |
||
| 477 | public static function can_delete_user($user_id) |
||
| 478 | { |
||
| 479 | $deny = api_get_configuration_value('deny_delete_users'); |
||
| 480 | |||
| 481 | if ($deny) { |
||
| 482 | return false; |
||
| 483 | } |
||
| 484 | |||
| 485 | $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 486 | if ($user_id != strval(intval($user_id))) { |
||
| 487 | return false; |
||
| 488 | } |
||
| 489 | if ($user_id === false) { |
||
| 490 | return false; |
||
| 491 | } |
||
| 492 | $sql = "SELECT * FROM $table_course_user |
||
| 493 | WHERE status = 1 AND user_id = ".$user_id; |
||
| 494 | $res = Database::query($sql); |
||
| 495 | while ($course = Database::fetch_object($res)) { |
||
| 496 | $sql = "SELECT id FROM $table_course_user |
||
| 497 | WHERE status=1 AND c_id = " . intval($course->c_id); |
||
| 498 | $res2 = Database::query($sql); |
||
| 499 | if (Database::num_rows($res2) == 1) { |
||
| 500 | |||
| 501 | return false; |
||
| 502 | } |
||
| 503 | } |
||
| 504 | |||
| 505 | return true; |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Delete a user from the platform, and all its belongings. This is a |
||
| 510 | * very dangerous function that should only be accessible by |
||
| 511 | * super-admins. Other roles should only be able to disable a user, |
||
| 512 | * which removes access to the platform but doesn't delete anything. |
||
| 513 | * @param int The ID of th user to be deleted |
||
| 514 | * @return boolean true if user is successfully deleted, false otherwise |
||
| 515 | * @assert (null) === false |
||
| 516 | * @assert ('abc') === false |
||
| 517 | */ |
||
| 518 | public static function delete_user($user_id) |
||
| 519 | { |
||
| 520 | if ($user_id != strval(intval($user_id))) { |
||
| 521 | return false; |
||
| 522 | } |
||
| 523 | |||
| 524 | if ($user_id === false) { |
||
| 525 | return false; |
||
| 526 | } |
||
| 527 | |||
| 528 | if (!self::can_delete_user($user_id)) { |
||
| 529 | return false; |
||
| 530 | } |
||
| 531 | |||
| 532 | $table_user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 533 | $usergroup_rel_user = Database :: get_main_table(TABLE_USERGROUP_REL_USER); |
||
| 534 | $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 535 | $table_course = Database :: get_main_table(TABLE_MAIN_COURSE); |
||
| 536 | $table_session = Database :: get_main_table(TABLE_MAIN_SESSION); |
||
| 537 | $table_admin = Database :: get_main_table(TABLE_MAIN_ADMIN); |
||
| 538 | $table_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 539 | $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 540 | $table_group = Database :: get_course_table(TABLE_GROUP_USER); |
||
| 541 | $table_work = Database :: get_course_table(TABLE_STUDENT_PUBLICATION); |
||
| 542 | |||
| 543 | // Unsubscribe the user from all groups in all his courses |
||
| 544 | $sql = "SELECT c.id FROM $table_course c, $table_course_user cu |
||
| 545 | WHERE |
||
| 546 | cu.user_id = '".$user_id."' AND |
||
| 547 | relation_type<>".COURSE_RELATION_TYPE_RRHH." AND |
||
| 548 | c.id = cu.c_id"; |
||
| 549 | |||
| 550 | $res = Database::query($sql); |
||
| 551 | while ($course = Database::fetch_object($res)) { |
||
| 552 | $sql = "DELETE FROM $table_group |
||
| 553 | WHERE c_id = {$course->id} AND user_id = $user_id"; |
||
| 554 | Database::query($sql); |
||
| 555 | } |
||
| 556 | |||
| 557 | // Unsubscribe user from usergroup_rel_user |
||
| 558 | $sql = "DELETE FROM $usergroup_rel_user WHERE user_id = '".$user_id."'"; |
||
| 559 | Database::query($sql); |
||
| 560 | |||
| 561 | // Unsubscribe user from all courses |
||
| 562 | $sql = "DELETE FROM $table_course_user WHERE user_id = '".$user_id."'"; |
||
| 563 | Database::query($sql); |
||
| 564 | |||
| 565 | // Unsubscribe user from all courses in sessions |
||
| 566 | $sql = "DELETE FROM $table_session_course_user WHERE user_id = '".$user_id."'"; |
||
| 567 | Database::query($sql); |
||
| 568 | |||
| 569 | // If the user was added as a id_coach then set the current admin as coach see BT# |
||
| 570 | $currentUserId = api_get_user_id(); |
||
| 571 | $sql = "UPDATE $table_session SET id_coach = $currentUserId |
||
| 572 | WHERE id_coach = '".$user_id."'"; |
||
| 573 | Database::query($sql); |
||
| 574 | |||
| 575 | $sql = "UPDATE $table_session SET id_coach = $currentUserId |
||
| 576 | WHERE session_admin_id = '".$user_id."'"; |
||
| 577 | Database::query($sql); |
||
| 578 | |||
| 579 | // Unsubscribe user from all sessions |
||
| 580 | $sql = "DELETE FROM $table_session_user |
||
| 581 | WHERE user_id = '".$user_id."'"; |
||
| 582 | Database::query($sql); |
||
| 583 | |||
| 584 | // Delete user picture |
||
| 585 | /* TODO: Logic about api_get_setting('split_users_upload_directory') == 'true' |
||
| 586 | a user has 4 different sized photos to be deleted. */ |
||
| 587 | $user_info = api_get_user_info($user_id); |
||
| 588 | |||
| 589 | if (strlen($user_info['picture_uri']) > 0) { |
||
| 590 | $path = self::getUserPathById($user_id, 'system'); |
||
| 591 | $img_path = $path.$user_info['picture_uri']; |
||
| 592 | if (file_exists($img_path)) { |
||
| 593 | unlink($img_path); |
||
| 594 | } |
||
| 595 | } |
||
| 596 | |||
| 597 | // Delete the personal course categories |
||
| 598 | $course_cat_table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY); |
||
| 599 | $sql = "DELETE FROM $course_cat_table WHERE user_id = '".$user_id."'"; |
||
| 600 | Database::query($sql); |
||
| 601 | |||
| 602 | // Delete user from the admin table |
||
| 603 | $sql = "DELETE FROM $table_admin WHERE user_id = '".$user_id."'"; |
||
| 604 | Database::query($sql); |
||
| 605 | |||
| 606 | // Delete the personal agenda-items from this user |
||
| 607 | $agenda_table = Database :: get_main_table(TABLE_PERSONAL_AGENDA); |
||
| 608 | $sql = "DELETE FROM $agenda_table WHERE user = '".$user_id."'"; |
||
| 609 | Database::query($sql); |
||
| 610 | |||
| 611 | $gradebook_results_table = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT); |
||
| 612 | $sql = 'DELETE FROM '.$gradebook_results_table.' WHERE user_id = '.$user_id; |
||
| 613 | Database::query($sql); |
||
| 614 | |||
| 615 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 616 | $extraFieldValue->deleteValuesByItem($user_id); |
||
| 617 | |||
| 618 | UrlManager::deleteUserFromAllUrls($user_id); |
||
| 619 | |||
| 620 | if (api_get_setting('allow_social_tool') == 'true') { |
||
| 621 | $userGroup = new UserGroup(); |
||
| 622 | //Delete user from portal groups |
||
| 623 | $group_list = $userGroup->get_groups_by_user($user_id); |
||
| 624 | if (!empty($group_list)) { |
||
| 625 | foreach ($group_list as $group_id => $data) { |
||
| 626 | $userGroup->delete_user_rel_group($user_id, $group_id); |
||
| 627 | } |
||
| 628 | } |
||
| 629 | |||
| 630 | // Delete user from friend lists |
||
| 631 | SocialManager::remove_user_rel_user($user_id, true); |
||
| 632 | } |
||
| 633 | |||
| 634 | // Removing survey invitation |
||
| 635 | SurveyManager::delete_all_survey_invitations_by_user($user_id); |
||
| 636 | |||
| 637 | // Delete students works |
||
| 638 | $sql = "DELETE FROM $table_work WHERE user_id = $user_id AND c_id <> 0"; |
||
| 639 | Database::query($sql); |
||
| 640 | |||
| 641 | $sql = "UPDATE c_item_property SET to_user_id = NULL |
||
| 642 | WHERE to_user_id = '".$user_id."'"; |
||
| 643 | Database::query($sql); |
||
| 644 | |||
| 645 | $sql = "UPDATE c_item_property SET insert_user_id = NULL |
||
| 646 | WHERE insert_user_id = '".$user_id."'"; |
||
| 647 | Database::query($sql); |
||
| 648 | |||
| 649 | $sql = "UPDATE c_item_property SET lastedit_user_id = NULL |
||
| 650 | WHERE lastedit_user_id = '".$user_id."'"; |
||
| 651 | Database::query($sql); |
||
| 652 | |||
| 653 | // Delete user from database |
||
| 654 | $sql = "DELETE FROM $table_user WHERE id = '".$user_id."'"; |
||
| 655 | Database::query($sql); |
||
| 656 | |||
| 657 | // Add event to system log |
||
| 658 | $user_id_manager = api_get_user_id(); |
||
| 659 | |||
| 660 | Event::addEvent( |
||
| 661 | LOG_USER_DELETE, |
||
| 662 | LOG_USER_ID, |
||
| 663 | $user_id, |
||
| 664 | api_get_utc_datetime(), |
||
| 665 | $user_id_manager |
||
| 666 | ); |
||
| 667 | |||
| 668 | Event::addEvent( |
||
| 669 | LOG_USER_DELETE, |
||
| 670 | LOG_USER_OBJECT, |
||
| 671 | $user_info, |
||
| 672 | api_get_utc_datetime(), |
||
| 673 | $user_id_manager |
||
| 674 | ); |
||
| 675 | |||
| 676 | return true; |
||
| 677 | } |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Deletes users completely. Can be called either as: |
||
| 681 | * - UserManager :: delete_users(1, 2, 3); or |
||
| 682 | * - UserManager :: delete_users(array(1, 2, 3)); |
||
| 683 | * @param array|int $ids |
||
| 684 | * @return boolean True if at least one user was successfuly deleted. False otherwise. |
||
| 685 | * @author Laurent Opprecht |
||
| 686 | * @uses UserManager::delete_user() to actually delete each user |
||
| 687 | * @assert (null) === false |
||
| 688 | * @assert (-1) === false |
||
| 689 | * @assert (array(-1)) === false |
||
| 690 | */ |
||
| 691 | public static function delete_users($ids = array()) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Disable users. Can be called either as: |
||
| 708 | * - UserManager :: deactivate_users(1, 2, 3); |
||
| 709 | * - UserManager :: deactivate_users(array(1, 2, 3)); |
||
| 710 | * @param array|int $ids |
||
| 711 | * @return boolean |
||
| 712 | * @author Laurent Opprecht |
||
| 713 | * @assert (null) === false |
||
| 714 | * @assert (array(-1)) === false |
||
| 715 | */ |
||
| 716 | View Code Duplication | public static function deactivate_users($ids = array()) |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Enable users. Can be called either as: |
||
| 738 | * - UserManager :: activate_users(1, 2, 3); |
||
| 739 | * - UserManager :: activate_users(array(1, 2, 3)); |
||
| 740 | * @param array|int IDs of the users to enable |
||
| 741 | * @return boolean |
||
| 742 | * @author Laurent Opprecht |
||
| 743 | * @assert (null) === false |
||
| 744 | * @assert (array(-1)) === false |
||
| 745 | */ |
||
| 746 | View Code Duplication | public static function activate_users($ids = array()) |
|
| 765 | |||
| 766 | /** |
||
| 767 | * Update user information with new openid |
||
| 768 | * @param int $user_id |
||
| 769 | * @param string $openid |
||
| 770 | * @return boolean true if the user information was updated |
||
| 771 | * @assert (false,'') === false |
||
| 772 | * @assert (-1,'') === false |
||
| 773 | */ |
||
| 774 | public static function update_openid($user_id, $openid) |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Update user information with all the parameters passed to this function |
||
| 789 | * @param int The ID of the user to be updated |
||
| 790 | * @param string The user's firstname |
||
| 791 | * @param string The user's lastname |
||
| 792 | * @param string The user's username (login) |
||
| 793 | * @param string The user's password |
||
| 794 | * @param string The authentication source (default: "platform") |
||
| 795 | * @param string The user's e-mail address |
||
| 796 | * @param int The user's status |
||
| 797 | * @param string The user's official code (usually just an internal institutional code) |
||
| 798 | * @param string The user's phone number |
||
| 799 | * @param string The user's picture URL (internal to the Chamilo directory) |
||
| 800 | * @param int The user ID of the person who registered this user (optional, defaults to null) |
||
| 801 | * @param int The department of HR in which the user is registered (optional, defaults to 0) |
||
| 802 | * @param array A series of additional fields to add to this user as extra fields (optional, defaults to null) |
||
| 803 | * @return boolean|integer False on error, or the user ID if the user information was updated |
||
| 804 | * @assert (false, false, false, false, false, false, false, false, false, false, false, false, false) === false |
||
| 805 | */ |
||
| 806 | public static function update_user( |
||
| 807 | $user_id, |
||
| 808 | $firstname, |
||
| 809 | $lastname, |
||
| 810 | $username, |
||
| 811 | $password = null, |
||
| 812 | $auth_source = null, |
||
| 813 | $email, |
||
| 814 | $status, |
||
| 815 | $official_code, |
||
| 816 | $phone, |
||
| 817 | $picture_uri, |
||
| 818 | $expiration_date, |
||
| 819 | $active, |
||
| 820 | $creator_id = null, |
||
| 821 | $hr_dept_id = 0, |
||
| 822 | $extra = null, |
||
| 823 | $language = 'english', |
||
| 824 | $encrypt_method = '', |
||
| 825 | $send_email = false, |
||
| 826 | $reset_password = 0 |
||
| 827 | ) { |
||
| 828 | $hook = HookUpdateUser::create(); |
||
| 829 | if (!empty($hook)) { |
||
| 830 | $hook->notifyUpdateUser(HOOK_EVENT_TYPE_PRE); |
||
| 831 | } |
||
| 832 | global $_configuration; |
||
| 833 | $original_password = $password; |
||
| 834 | |||
| 835 | if (empty($user_id)) { |
||
| 836 | return false; |
||
| 837 | } |
||
| 838 | $user_info = api_get_user_info($user_id, false, true); |
||
| 839 | |||
| 840 | if ($reset_password == 0) { |
||
| 841 | $password = null; |
||
| 842 | $auth_source = $user_info['auth_source']; |
||
| 843 | } elseif ($reset_password == 1) { |
||
| 844 | $original_password = $password = api_generate_password(); |
||
| 845 | $auth_source = PLATFORM_AUTH_SOURCE; |
||
| 846 | } elseif ($reset_password == 2) { |
||
| 847 | $password = $password; |
||
| 848 | $auth_source = PLATFORM_AUTH_SOURCE; |
||
| 849 | } elseif ($reset_password == 3) { |
||
| 850 | $password = $password; |
||
| 851 | $auth_source = $auth_source; |
||
| 852 | } |
||
| 853 | |||
| 854 | if ($user_id != strval(intval($user_id))) { |
||
| 855 | return false; |
||
| 856 | } |
||
| 857 | |||
| 858 | if ($user_id === false) { |
||
| 859 | return false; |
||
| 860 | } |
||
| 861 | |||
| 862 | //Checking the user language |
||
| 863 | $languages = api_get_languages(); |
||
| 864 | if (!in_array($language, $languages['folder'])) { |
||
| 865 | $language = api_get_setting('platformLanguage'); |
||
| 866 | } |
||
| 867 | |||
| 868 | $change_active = 0; |
||
| 869 | if ($user_info['active'] != $active) { |
||
| 870 | $change_active = 1; |
||
| 871 | } |
||
| 872 | |||
| 873 | $userManager = self::getManager(); |
||
| 874 | |||
| 875 | /** @var Chamilo\UserBundle\Entity\User $user */ |
||
| 876 | $user = self::getRepository()->find($user_id); |
||
| 877 | |||
| 878 | if (empty($user)) { |
||
| 879 | return false; |
||
| 880 | } |
||
| 881 | |||
| 882 | if (!empty($expiration_date)) { |
||
| 883 | $expiration_date = api_get_utc_datetime($expiration_date); |
||
| 884 | $expiration_date = new \DateTime( |
||
| 885 | $expiration_date, |
||
| 886 | new DateTimeZone('UTC') |
||
| 887 | ); |
||
| 888 | } |
||
| 889 | |||
| 890 | $user |
||
| 891 | ->setLastname($lastname) |
||
| 892 | ->setFirstname($firstname) |
||
| 893 | ->setUsername($username) |
||
| 894 | ->setStatus($status) |
||
| 895 | ->setAuthSource($auth_source) |
||
| 896 | ->setLanguage($language) |
||
| 897 | ->setEmail($email) |
||
| 898 | ->setOfficialCode($official_code) |
||
| 899 | ->setPhone($phone) |
||
| 900 | ->setPictureUri($picture_uri) |
||
| 901 | ->setExpirationDate($expiration_date) |
||
| 902 | ->setActive($active) |
||
| 903 | ->setHrDeptId($hr_dept_id) |
||
| 904 | ; |
||
| 905 | |||
| 906 | if (!is_null($password)) { |
||
| 907 | $user->setPlainPassword($password); |
||
| 908 | } |
||
| 909 | |||
| 910 | $userManager->updateUser($user, true); |
||
| 911 | |||
| 912 | if ($change_active == 1) { |
||
| 913 | if ($active == 1) { |
||
| 914 | $event_title = LOG_USER_ENABLE; |
||
| 915 | } else { |
||
| 916 | $event_title = LOG_USER_DISABLE; |
||
| 917 | } |
||
| 918 | Event::addEvent($event_title, LOG_USER_ID, $user_id); |
||
| 919 | } |
||
| 920 | |||
| 921 | View Code Duplication | if (is_array($extra) && count($extra) > 0) { |
|
| 922 | $res = true; |
||
| 923 | foreach ($extra as $fname => $fvalue) { |
||
| 924 | $res = $res && self::update_extra_field_value( |
||
| 925 | $user_id, |
||
| 926 | $fname, |
||
| 927 | $fvalue |
||
| 928 | ); |
||
| 929 | } |
||
| 930 | } |
||
| 931 | |||
| 932 | if (!empty($email) && $send_email) { |
||
| 933 | $recipient_name = api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS); |
||
| 934 | $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName'); |
||
| 935 | $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS); |
||
| 936 | $email_admin = api_get_setting('emailAdministrator'); |
||
| 937 | |||
| 938 | if (api_is_multiple_url_enabled()) { |
||
| 939 | $access_url_id = api_get_current_access_url_id(); |
||
| 940 | if ($access_url_id != -1) { |
||
| 941 | $url = api_get_access_url($access_url_id); |
||
| 942 | $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'); |
||
| 943 | } |
||
| 944 | } else { |
||
| 945 | $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'); |
||
| 946 | } |
||
| 947 | |||
| 948 | $emailbody = nl2br($emailbody); |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
|
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
|
| 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) |
||
| 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) |
||
| 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) |
||
| 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 | |||
| 1273 | return $result; |
||
| 1274 | } |
||
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Get a list of users of which the given conditions match with an = 'cond' |
||
| 1278 | * @param array $conditions a list of condition (exemple : status=>STUDENT) |
||
| 1279 | * @param array $order_by a list of fields on which sort |
||
| 1280 | * @return array An array with all users of the platform. |
||
| 1281 | * @todo optional course code parameter, optional sorting parameters... |
||
| 1282 | * @todo security filter order by |
||
| 1283 | */ |
||
| 1284 | public static function get_user_list( |
||
| 1285 | $conditions = array(), |
||
| 1286 | $order_by = array(), |
||
| 1287 | $limit_from = false, |
||
| 1288 | $limit_to = false |
||
| 1289 | ) { |
||
| 1290 | $user_table = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 1291 | $return_array = array(); |
||
| 1292 | $sql_query = "SELECT * FROM $user_table"; |
||
| 1293 | if (count($conditions) > 0) { |
||
| 1294 | $sql_query .= ' WHERE '; |
||
| 1295 | foreach ($conditions as $field => $value) { |
||
| 1296 | $field = Database::escape_string($field); |
||
| 1297 | $value = Database::escape_string($value); |
||
| 1298 | $sql_query .= "$field = '$value'"; |
||
| 1299 | } |
||
| 1300 | } |
||
| 1301 | View Code Duplication | if (count($order_by) > 0) { |
|
| 1302 | $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by), null, false); |
||
| 1303 | } |
||
| 1304 | |||
| 1305 | if (is_numeric($limit_from) && is_numeric($limit_from)) { |
||
| 1306 | $limit_from = intval($limit_from); |
||
| 1307 | $limit_to = intval($limit_to); |
||
| 1308 | $sql_query .= " LIMIT $limit_from, $limit_to"; |
||
| 1309 | } |
||
| 1310 | $sql_result = Database::query($sql_query); |
||
| 1311 | while ($result = Database::fetch_array($sql_result)) { |
||
| 1312 | $return_array[] = $result; |
||
| 1313 | } |
||
| 1314 | return $return_array; |
||
| 1315 | } |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Get a list of users of which the given conditions match with a LIKE '%cond%' |
||
| 1319 | * @param array $conditions a list of condition (exemple : status=>STUDENT) |
||
| 1320 | * @param array $order_by a list of fields on which sort |
||
| 1321 | * @return array An array with all users of the platform. |
||
| 1322 | * @todo optional course code parameter, optional sorting parameters... |
||
| 1323 | * @todo security filter order_by |
||
| 1324 | */ |
||
| 1325 | public static function get_user_list_like( |
||
| 1326 | $conditions = array(), |
||
| 1327 | $order_by = array(), |
||
| 1328 | $simple_like = false, |
||
| 1329 | $condition = 'AND' |
||
| 1330 | ) { |
||
| 1331 | $user_table = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 1332 | $return_array = array(); |
||
| 1333 | $sql_query = "SELECT * FROM $user_table"; |
||
| 1334 | if (count($conditions) > 0) { |
||
| 1335 | $sql_query .= ' WHERE '; |
||
| 1336 | $temp_conditions = array(); |
||
| 1337 | foreach ($conditions as $field => $value) { |
||
| 1338 | $field = Database::escape_string($field); |
||
| 1339 | $value = Database::escape_string($value); |
||
| 1340 | View Code Duplication | if ($simple_like) { |
|
| 1341 | $temp_conditions[] = $field." LIKE '$value%'"; |
||
| 1342 | } else { |
||
| 1343 | $temp_conditions[] = $field.' LIKE \'%'.$value.'%\''; |
||
| 1344 | } |
||
| 1345 | } |
||
| 1346 | if (!empty($temp_conditions)) { |
||
| 1347 | $sql_query .= implode(' '.$condition.' ', $temp_conditions); |
||
| 1348 | } |
||
| 1349 | } |
||
| 1350 | View Code Duplication | if (count($order_by) > 0) { |
|
| 1351 | $sql_query .= ' ORDER BY '.Database::escape_string(implode(',', $order_by), null, false); |
||
| 1352 | } |
||
| 1353 | $sql_result = Database::query($sql_query); |
||
| 1354 | while ($result = Database::fetch_array($sql_result)) { |
||
| 1355 | $return_array[] = $result; |
||
| 1356 | } |
||
| 1357 | return $return_array; |
||
| 1358 | } |
||
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Get user picture URL or path from user ID (returns an array). |
||
| 1362 | * The return format is a complete path, enabling recovery of the directory |
||
| 1363 | * with dirname() or the file with basename(). This also works for the |
||
| 1364 | * functions dealing with the user's productions, as they are located in |
||
| 1365 | * the same directory. |
||
| 1366 | * @param integer $id User ID |
||
| 1367 | * @param string $type Type of path to return (can be 'system', 'web') |
||
| 1368 | * @param array $userInfo user information to avoid query the DB |
||
| 1369 | * returns the /main/img/unknown.jpg image set it at true |
||
| 1370 | * |
||
| 1371 | * @return array Array of 2 elements: 'dir' and 'file' which contain |
||
| 1372 | * the dir and file as the name implies if image does not exist it will |
||
| 1373 | * return the unknow image if anonymous parameter is true if not it returns an empty array |
||
| 1374 | */ |
||
| 1375 | public static function get_user_picture_path_by_id($id, $type = 'web', $userInfo = []) |
||
| 1376 | { |
||
| 1377 | switch ($type) { |
||
| 1378 | case 'system': // Base: absolute system path. |
||
| 1379 | $base = api_get_path(SYS_CODE_PATH); |
||
| 1380 | break; |
||
| 1381 | case 'web': // Base: absolute web path. |
||
| 1382 | default: |
||
| 1383 | $base = api_get_path(WEB_CODE_PATH); |
||
| 1384 | break; |
||
| 1385 | } |
||
| 1386 | |||
| 1387 | $anonymousPath = array( |
||
| 1388 | 'dir' => $base.'img/', |
||
| 1389 | 'file' => 'unknown.jpg', |
||
| 1390 | 'email' => '', |
||
| 1391 | ); |
||
| 1392 | |||
| 1393 | if (empty($id) || empty($type)) { |
||
| 1394 | return $anonymousPath; |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | $id = intval($id); |
||
| 1398 | View Code Duplication | if (empty($userInfo)) { |
|
| 1399 | $user_table = Database:: get_main_table(TABLE_MAIN_USER); |
||
| 1400 | $sql = "SELECT email, picture_uri FROM $user_table |
||
| 1401 | WHERE id=".$id; |
||
| 1402 | $res = Database::query($sql); |
||
| 1403 | |||
| 1404 | if (!Database::num_rows($res)) { |
||
| 1405 | return $anonymousPath; |
||
| 1406 | } |
||
| 1407 | $user = Database::fetch_array($res); |
||
| 1408 | } else { |
||
| 1409 | $user = $userInfo; |
||
| 1410 | } |
||
| 1411 | |||
| 1412 | $pictureFilename = trim($user['picture_uri']); |
||
| 1413 | |||
| 1414 | $dir = self::getUserPathById($id, $type); |
||
| 1415 | |||
| 1416 | return array( |
||
| 1417 | 'dir' => $dir, |
||
| 1418 | 'file' => $pictureFilename, |
||
| 1419 | 'email' => $user['email'], |
||
| 1420 | ); |
||
| 1421 | } |
||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * Get user path from user ID (returns an array). |
||
| 1425 | * The return format is a complete path to a folder ending with "/" |
||
| 1426 | * In case the first level of subdirectory of users/ does not exist, the |
||
| 1427 | * function will attempt to create it. Probably not the right place to do it |
||
| 1428 | * but at least it avoids headaches in many other places. |
||
| 1429 | * @param integer $id User ID |
||
| 1430 | * @param string $type Type of path to return (can be 'system', 'web', 'rel', 'last') |
||
| 1431 | * @return string User folder path (i.e. /var/www/chamilo/app/upload/users/1/1/) |
||
| 1432 | */ |
||
| 1433 | public static function getUserPathById($id, $type) |
||
| 1434 | { |
||
| 1435 | $id = intval($id); |
||
| 1436 | if (!$id) { |
||
| 1437 | return null; |
||
| 1438 | } |
||
| 1439 | |||
| 1440 | $userPath = "users/$id/"; |
||
| 1441 | if (api_get_setting('split_users_upload_directory') === 'true') { |
||
| 1442 | $userPath = 'users/'.substr((string) $id, 0, 1).'/'.$id.'/'; |
||
| 1443 | // In exceptional cases, on some portals, the intermediate base user |
||
| 1444 | // directory might not have been created. Make sure it is before |
||
| 1445 | // going further. |
||
| 1446 | $rootPath = api_get_path(SYS_UPLOAD_PATH) . 'users/' . substr((string) $id, 0, 1); |
||
| 1447 | if (!is_dir($rootPath)) { |
||
| 1448 | $perm = api_get_permissions_for_new_directories(); |
||
| 1449 | try { |
||
| 1450 | mkdir($rootPath, $perm); |
||
| 1451 | } catch (Exception $e) { |
||
| 1452 | error_log($e->getMessage()); |
||
| 1453 | } |
||
| 1454 | } |
||
| 1455 | } |
||
| 1456 | switch ($type) { |
||
| 1457 | case 'system': // Base: absolute system path. |
||
| 1458 | $userPath = api_get_path(SYS_UPLOAD_PATH).$userPath; |
||
| 1459 | break; |
||
| 1460 | case 'web': // Base: absolute web path. |
||
| 1461 | $userPath = api_get_path(WEB_UPLOAD_PATH).$userPath; |
||
| 1462 | break; |
||
| 1463 | case 'rel': // Relative to the document root (e.g. app/upload/users/1/13/) |
||
| 1464 | $userPath = api_get_path(REL_UPLOAD_PATH).$userPath; |
||
| 1465 | break; |
||
| 1466 | case 'last': // Only the last part starting with users/ |
||
| 1467 | break; |
||
| 1468 | } |
||
| 1469 | |||
| 1470 | return $userPath; |
||
| 1471 | } |
||
| 1472 | |||
| 1473 | /** |
||
| 1474 | * Gets the current user image |
||
| 1475 | * @param string $user_id |
||
| 1476 | * @param int $size it can be USER_IMAGE_SIZE_SMALL, |
||
| 1477 | * USER_IMAGE_SIZE_MEDIUM, USER_IMAGE_SIZE_BIG or USER_IMAGE_SIZE_ORIGINAL |
||
| 1478 | * @param bool $addRandomId |
||
| 1479 | * @param array $userInfo to avoid query the DB |
||
| 1480 | * |
||
| 1481 | * @return string |
||
| 1482 | */ |
||
| 1483 | public static function getUserPicture( |
||
| 1484 | $user_id, |
||
| 1485 | $size = USER_IMAGE_SIZE_MEDIUM, |
||
| 1486 | $addRandomId = true, |
||
| 1487 | $userInfo = [] |
||
| 1488 | ) { |
||
| 1489 | $imageWebPath = self::get_user_picture_path_by_id($user_id, 'web', $userInfo); |
||
| 1490 | $pictureWebFile = $imageWebPath['file']; |
||
| 1491 | $pictureWebDir = $imageWebPath['dir']; |
||
| 1492 | |||
| 1493 | $pictureAnonymousSize = '128'; |
||
| 1494 | $gravatarSize = 22; |
||
| 1495 | $realSizeName = 'small_'; |
||
| 1496 | |||
| 1497 | switch ($size) { |
||
| 1498 | case USER_IMAGE_SIZE_SMALL: |
||
| 1499 | $pictureAnonymousSize = '22'; |
||
| 1500 | $realSizeName = 'small_'; |
||
| 1501 | $gravatarSize = 22; |
||
| 1502 | break; |
||
| 1503 | case USER_IMAGE_SIZE_MEDIUM: |
||
| 1504 | $pictureAnonymousSize = '64'; |
||
| 1505 | $realSizeName = 'medium_'; |
||
| 1506 | $gravatarSize = 50; |
||
| 1507 | break; |
||
| 1508 | case USER_IMAGE_SIZE_ORIGINAL: |
||
| 1509 | $pictureAnonymousSize = '128'; |
||
| 1510 | $realSizeName = ''; |
||
| 1511 | $gravatarSize = 108; |
||
| 1512 | break; |
||
| 1513 | case USER_IMAGE_SIZE_BIG: |
||
| 1514 | $pictureAnonymousSize = '128'; |
||
| 1515 | $realSizeName = 'big_'; |
||
| 1516 | $gravatarSize = 200; |
||
| 1517 | break; |
||
| 1518 | } |
||
| 1519 | |||
| 1520 | $gravatarEnabled = api_get_setting('gravatar_enabled'); |
||
| 1521 | $anonymousPath = Display::returnIconPath('unknown.png', $pictureAnonymousSize); |
||
| 1522 | |||
| 1523 | if ($pictureWebFile == 'unknown.jpg' || empty($pictureWebFile)) { |
||
| 1524 | |||
| 1525 | if ($gravatarEnabled === 'true') { |
||
| 1526 | $file = self::getGravatar( |
||
| 1527 | $imageWebPath['email'], |
||
| 1528 | $gravatarSize, |
||
| 1529 | api_get_setting('gravatar_type') |
||
| 1530 | ); |
||
| 1531 | |||
| 1532 | if ($addRandomId) { |
||
| 1533 | $file .= '&rand='.uniqid(); |
||
| 1534 | } |
||
| 1535 | |||
| 1536 | return $file; |
||
| 1537 | } |
||
| 1538 | |||
| 1539 | return $anonymousPath; |
||
| 1540 | } |
||
| 1541 | |||
| 1542 | $pictureSysPath = self::get_user_picture_path_by_id($user_id, 'system'); |
||
| 1543 | |||
| 1544 | $file = $pictureSysPath['dir'].$realSizeName.$pictureWebFile; |
||
| 1545 | $picture = ''; |
||
| 1546 | if (file_exists($file)) { |
||
| 1547 | $picture = $pictureWebDir.$realSizeName.$pictureWebFile; |
||
| 1548 | } else { |
||
| 1549 | $file = $pictureSysPath['dir'].$pictureWebFile; |
||
| 1550 | if (file_exists($file) && !is_dir($file)) { |
||
| 1551 | $picture = $pictureWebFile['dir'].$pictureWebFile; |
||
| 1552 | } |
||
| 1553 | } |
||
| 1554 | |||
| 1555 | if (empty($picture)) { |
||
| 1556 | return $anonymousPath; |
||
| 1557 | } |
||
| 1558 | |||
| 1559 | if ($addRandomId) { |
||
| 1560 | $picture .= '?rand='.uniqid(); |
||
| 1561 | } |
||
| 1562 | |||
| 1563 | return $picture; |
||
| 1564 | } |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Creates new user photos in various sizes of a user, or deletes user photos. |
||
| 1568 | * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php |
||
| 1569 | * @param int $user_id The user internal identification number. |
||
| 1570 | * @param string $file The common file name for the newly created photos. |
||
| 1571 | * It will be checked and modified for compatibility with the file system. |
||
| 1572 | * If full name is provided, path component is ignored. |
||
| 1573 | * If an empty name is provided, then old user photos are deleted only, |
||
| 1574 | * @see UserManager::delete_user_picture() as the prefered way for deletion. |
||
| 1575 | * @param string $source_file The full system name of the image from which user photos will be created. |
||
| 1576 | * @param string $cropParameters Optional string that contents "x,y,width,height" of a cropped image format |
||
| 1577 | * @return string/bool Returns the resulting common file name of created images which usually should be stored in database. |
||
| 1578 | * When deletion is requested returns empty string. In case of internal error or negative validation returns FALSE. |
||
| 1579 | */ |
||
| 1580 | public static function update_user_picture($user_id, $file = null, $source_file = null, $cropParameters) |
||
| 1671 | |||
| 1672 | /** |
||
| 1673 | * Update User extra field file type into {user_folder}/{$extra_field} |
||
| 1674 | * @param int $user_id The user internal identification number |
||
| 1675 | * @param string $extra_field The $extra_field The extra field name |
||
| 1676 | * @param null $file The filename |
||
| 1677 | * @param null $source_file The temporal filename |
||
| 1678 | * @return bool|null return filename if success, but false |
||
| 1679 | */ |
||
| 1680 | public static function update_user_extra_file($user_id, $extra_field = '', $file = null, $source_file = null) |
||
| 1716 | |||
| 1717 | |||
| 1718 | /** |
||
| 1719 | * Deletes user photos. |
||
| 1720 | * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php |
||
| 1721 | * @param int $user_id The user internal identitfication number. |
||
| 1722 | * @return string/bool Returns empty string on success, FALSE on error. |
||
| 1723 | */ |
||
| 1724 | public static function delete_user_picture($user_id) |
||
| 1728 | |||
| 1729 | /** |
||
| 1730 | * Returns an XHTML formatted list of productions for a user, or FALSE if he |
||
| 1731 | * doesn't have any. |
||
| 1732 | * |
||
| 1733 | * If there has been a request to remove a production, the function will return |
||
| 1734 | * without building the list unless forced to do so by the optional second |
||
| 1735 | * parameter. This increases performance by avoiding to read through the |
||
| 1736 | * productions on the filesystem before the removal request has been carried |
||
| 1737 | * out because they'll have to be re-read afterwards anyway. |
||
| 1738 | * |
||
| 1739 | * @param int $user_id User id |
||
| 1740 | * @param $force Optional parameter to force building after a removal request |
||
| 1741 | * |
||
| 1742 | * @return A string containing the XHTML code to dipslay the production list, or FALSE |
||
| 1743 | */ |
||
| 1744 | public static function build_production_list($user_id, $force = false, $showdelete = false) |
||
| 1745 | { |
||
| 1746 | if (!$force && !empty($_POST['remove_production'])) { |
||
| 1747 | return true; // postpone reading from the filesystem |
||
| 1748 | } |
||
| 1749 | $productions = self::get_user_productions($user_id); |
||
| 1750 | |||
| 1751 | if (empty($productions)) { |
||
| 1752 | return false; |
||
| 1753 | } |
||
| 1754 | |||
| 1755 | $production_path = self::get_user_picture_path_by_id($user_id, 'web'); |
||
| 1756 | $production_dir = $production_path['dir']; |
||
| 1757 | $del_image = Display::returnIconPath('delete.png'); |
||
| 1758 | $add_image = Display::returnIconPath('archive.png'); |
||
| 1759 | $del_text = get_lang('Delete'); |
||
| 1760 | $production_list = ''; |
||
| 1761 | if (count($productions) > 0) { |
||
| 1762 | $production_list = '<div class="files-production"><ul id="productions">'; |
||
| 1763 | foreach ($productions as $file) { |
||
| 1764 | $production_list .= '<li><img src="'.$add_image.'" /><a href="'.$production_dir.urlencode($file).'" target="_blank">'.htmlentities($file).'</a>'; |
||
| 1765 | View Code Duplication | if ($showdelete) { |
|
| 1766 | $production_list .= ' <input style="width:16px;" type="image" name="remove_production['.urlencode($file).']" src="'.$del_image.'" alt="'.$del_text.'" title="'.$del_text.' '.htmlentities($file).'" onclick="javascript: return confirmation(\''.htmlentities($file).'\');" /></li>'; |
||
| 1767 | } |
||
| 1768 | } |
||
| 1769 | $production_list .= '</ul></div>'; |
||
| 1770 | } |
||
| 1771 | |||
| 1772 | return $production_list; |
||
| 1773 | } |
||
| 1774 | |||
| 1775 | /** |
||
| 1776 | * Returns an array with the user's productions. |
||
| 1777 | * |
||
| 1778 | * @param $user_id User id |
||
| 1779 | * @return array An array containing the user's productions |
||
| 1780 | */ |
||
| 1781 | public static function get_user_productions($user_id) |
||
| 1782 | { |
||
| 1783 | $production_path = self::get_user_picture_path_by_id($user_id, 'system'); |
||
| 1784 | $production_repository = $production_path['dir']; |
||
| 1785 | $productions = array(); |
||
| 1786 | |||
| 1787 | if (is_dir($production_repository)) { |
||
| 1788 | $handle = opendir($production_repository); |
||
| 1789 | while ($file = readdir($handle)) { |
||
| 1790 | if ($file == '.' || |
||
| 1791 | $file == '..' || |
||
| 1792 | $file == '.htaccess' || |
||
| 1793 | is_dir($production_repository.$file) |
||
| 1794 | ) { |
||
| 1795 | // skip current/parent directory and .htaccess |
||
| 1796 | continue; |
||
| 1797 | } |
||
| 1798 | |||
| 1799 | if (preg_match('/('.$user_id.'|[0-9a-f]{13}|saved)_.+\.(png|jpg|jpeg|gif)$/i', $file)) { |
||
| 1800 | // User's photos should not be listed as productions. |
||
| 1801 | continue; |
||
| 1802 | } |
||
| 1803 | $productions[] = $file; |
||
| 1804 | } |
||
| 1805 | } |
||
| 1806 | |||
| 1807 | return $productions; |
||
| 1808 | } |
||
| 1809 | |||
| 1810 | /** |
||
| 1811 | * Remove a user production. |
||
| 1812 | * |
||
| 1813 | * @param int $user_id User id |
||
| 1814 | * @param string $production The production to remove |
||
| 1815 | */ |
||
| 1816 | public static function remove_user_production($user_id, $production) |
||
| 1826 | |||
| 1827 | /** |
||
| 1828 | * Update an extra field value for a given user |
||
| 1829 | * @param integer $userId User ID |
||
| 1830 | * @param string $variable Field variable name |
||
| 1831 | * @param string $value Field value |
||
| 1832 | * |
||
| 1833 | * @return boolean true if field updated, false otherwise |
||
| 1834 | */ |
||
| 1835 | View Code Duplication | public static function update_extra_field_value($userId, $variable, $value = '') |
|
| 1845 | |||
| 1846 | /** |
||
| 1847 | * Get an array of extra fields with field details (type, default value and options) |
||
| 1848 | * @param integer Offset (from which row) |
||
| 1849 | * @param integer Number of items |
||
| 1850 | * @param integer Column on which sorting is made |
||
| 1851 | * @param string Sorting direction |
||
| 1852 | * @param boolean Optional. Whether we get all the fields or just the visible ones |
||
| 1853 | * @param int Optional. Whether we get all the fields with field_filter 1 or 0 or everything |
||
| 1854 | * @return array Extra fields details (e.g. $list[2]['type'], $list[4]['options'][2]['title'] |
||
| 1855 | */ |
||
| 1856 | public static function get_extra_fields( |
||
| 1930 | |||
| 1931 | /** |
||
| 1932 | * Build a list of extra file already uploaded in $user_folder/{$extra_field}/ |
||
| 1933 | * @param $user_id |
||
| 1934 | * @param $extra_field |
||
| 1935 | * @param bool $force |
||
| 1936 | * @param bool $showdelete |
||
| 1937 | * @return bool|string |
||
| 1938 | */ |
||
| 1939 | public static function build_user_extra_file_list($user_id, $extra_field, $force = false, $showdelete = false) |
||
| 1940 | { |
||
| 1941 | if (!$force && !empty($_POST['remove_'.$extra_field])) { |
||
| 1942 | return true; // postpone reading from the filesystem |
||
| 1943 | } |
||
| 1944 | |||
| 1945 | $extra_files = self::get_user_extra_files($user_id, $extra_field); |
||
| 1946 | if (empty($extra_files)) { |
||
| 1947 | return false; |
||
| 1948 | } |
||
| 1949 | |||
| 1950 | $path_info = self::get_user_picture_path_by_id($user_id, 'web'); |
||
| 1951 | $path = $path_info['dir']; |
||
| 1952 | $del_image = Display::returnIconPath('delete.png'); |
||
| 1953 | |||
| 1954 | $del_text = get_lang('Delete'); |
||
| 1955 | $extra_file_list = ''; |
||
| 1956 | if (count($extra_files) > 0) { |
||
| 1957 | $extra_file_list = '<div class="files-production"><ul id="productions">'; |
||
| 1958 | foreach ($extra_files as $file) { |
||
| 1959 | $filename = substr($file,strlen($extra_field)+1); |
||
| 1960 | $extra_file_list .= '<li>'.Display::return_icon('archive.png').'<a href="'.$path.$extra_field.'/'.urlencode($filename).'" target="_blank">'.htmlentities($filename).'</a> '; |
||
| 1961 | View Code Duplication | if ($showdelete) { |
|
| 1962 | $extra_file_list .= '<input style="width:16px;" type="image" name="remove_extra_' . $extra_field . '['.urlencode($file).']" src="'.$del_image.'" alt="'.$del_text.'" title="'.$del_text.' '.htmlentities($filename).'" onclick="javascript: return confirmation(\''.htmlentities($filename).'\');" /></li>'; |
||
| 1963 | } |
||
| 1964 | } |
||
| 1965 | $extra_file_list .= '</ul></div>'; |
||
| 1966 | } |
||
| 1967 | |||
| 1968 | return $extra_file_list; |
||
| 1969 | } |
||
| 1970 | |||
| 1971 | /** |
||
| 1972 | * Get valid filenames in $user_folder/{$extra_field}/ |
||
| 1973 | * @param $user_id |
||
| 1974 | * @param $extra_field |
||
| 1975 | * @param bool $full_path |
||
| 1976 | * @return array |
||
| 1977 | */ |
||
| 1978 | public static function get_user_extra_files($user_id, $extra_field, $full_path = false) |
||
| 2008 | |||
| 2009 | /** |
||
| 2010 | * Remove an {$extra_file} from the user folder $user_folder/{$extra_field}/ |
||
| 2011 | * @param $user_id |
||
| 2012 | * @param $extra_field |
||
| 2013 | * @param $extra_file |
||
| 2014 | * @return bool |
||
| 2015 | */ |
||
| 2016 | public static function remove_user_extra_file($user_id, $extra_field, $extra_file) |
||
| 2031 | |||
| 2032 | /** |
||
| 2033 | * Creates a new extra field |
||
| 2034 | * @param string $variable Field's internal variable name |
||
| 2035 | * @param int $fieldType Field's type |
||
| 2036 | * @param string $displayText Field's language var name |
||
| 2037 | * @param string $default Field's default value |
||
| 2038 | * @return int |
||
| 2039 | */ |
||
| 2040 | View Code Duplication | public static function create_extra_field($variable, $fieldType, $displayText, $default) |
|
| 2052 | |||
| 2053 | /** |
||
| 2054 | * Check if a field is available |
||
| 2055 | * @param string th$variable |
||
| 2056 | * @return boolean |
||
| 2057 | */ |
||
| 2058 | public static function is_extra_field_available($variable) |
||
| 2059 | { |
||
| 2060 | $extraField = new ExtraField('user'); |
||
| 2065 | |||
| 2066 | /** |
||
| 2067 | * Gets user extra fields data |
||
| 2068 | * @param integer User ID |
||
| 2069 | * @param boolean Whether to prefix the fields indexes with "extra_" (might be used by formvalidator) |
||
| 2070 | * @param boolean Whether to return invisible fields as well |
||
| 2071 | * @param boolean Whether to split multiple-selection fields or not |
||
| 2072 | * @return array Array of fields => value for the given user |
||
| 2073 | */ |
||
| 2074 | public static function get_extra_user_data( |
||
| 2161 | |||
| 2162 | /** Get extra user data by field |
||
| 2163 | * @param int user ID |
||
| 2164 | * @param string the internal variable name of the field |
||
| 2165 | * @return array with extra data info of a user i.e array('field_variable'=>'value'); |
||
| 2166 | */ |
||
| 2167 | public static function get_extra_user_data_by_field( |
||
| 2226 | |||
| 2227 | /** |
||
| 2228 | * Get the extra field information for a certain field (the options as well) |
||
| 2229 | * @param int $variable The name of the field we want to know everything about |
||
| 2230 | * @return array Array containing all the information about the extra profile field |
||
| 2231 | * (first level of array contains field details, then 'options' sub-array contains options details, |
||
| 2232 | * as returned by the database) |
||
| 2233 | * @author Julio Montoya |
||
| 2234 | * @since v1.8.6 |
||
| 2235 | */ |
||
| 2236 | public static function get_extra_field_information_by_name($variable) |
||
| 2242 | |||
| 2243 | /** |
||
| 2244 | * @param string $type |
||
| 2245 | * |
||
| 2246 | * @return array |
||
| 2247 | */ |
||
| 2248 | public static function get_all_extra_field_by_type($type) |
||
| 2254 | |||
| 2255 | /** |
||
| 2256 | * Get all the extra field information of a certain field (also the options) |
||
| 2257 | * |
||
| 2258 | * @param int $field_name the name of the field we want to know everything of |
||
| 2259 | * @return array $return containing all th information about the extra profile field |
||
| 2260 | * @author Julio Montoya |
||
| 2261 | * @deprecated |
||
| 2262 | * @since v1.8.6 |
||
| 2263 | */ |
||
| 2264 | public static function get_extra_field_information($fieldId) |
||
| 2270 | |||
| 2271 | /** Get extra user data by value |
||
| 2272 | * @param string the internal variable name of the field |
||
| 2273 | * @param string the internal value of the field |
||
| 2274 | * @return array with extra data info of a user i.e array('field_variable'=>'value'); |
||
| 2275 | */ |
||
| 2276 | public static function get_extra_user_data_by_value($field_variable, $field_value, $all_visibility = true) |
||
| 2297 | |||
| 2298 | /** |
||
| 2299 | * Get extra user data by field variable |
||
| 2300 | * @param string field variable |
||
| 2301 | * @return array data |
||
| 2302 | */ |
||
| 2303 | public static function get_extra_user_data_by_field_variable($field_variable) |
||
| 2320 | |||
| 2321 | /** |
||
| 2322 | * Gives a list of [session_category][session_id] for the current user. |
||
| 2323 | * @param integer $user_id |
||
| 2324 | * @param boolean whether to fill the first element or not (to give space for courses out of categories) |
||
| 2325 | * @param boolean optional true if limit time from session is over, false otherwise |
||
| 2326 | * @param boolean $ignoreTimeLimit ignore time start/end |
||
| 2327 | * @return array list of statuses [session_category][session_id] |
||
| 2328 | * |
||
| 2329 | * @todo ensure multiple access urls are managed correctly |
||
| 2330 | */ |
||
| 2331 | public static function get_sessions_by_category( |
||
| 2332 | $user_id, |
||
| 2333 | $is_time_over = true, |
||
| 2334 | $ignore_visibility_for_admins = false, |
||
| 2335 | $ignoreTimeLimit = false |
||
| 2336 | ) { |
||
| 2337 | // Database Table Definitions |
||
| 2338 | $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION); |
||
| 2339 | $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 2340 | $tbl_session_category = Database :: get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2341 | |||
| 2342 | if ($user_id != strval(intval($user_id))) { |
||
| 2343 | return array(); |
||
| 2344 | } |
||
| 2345 | |||
| 2346 | // Get the list of sessions per user |
||
| 2347 | $now = api_get_utc_datetime(); |
||
| 2348 | |||
| 2349 | $sql = "SELECT DISTINCT |
||
| 2350 | session.id, |
||
| 2351 | session.name, |
||
| 2352 | session.access_start_date, |
||
| 2353 | session.access_end_date, |
||
| 2354 | session_category_id, |
||
| 2355 | session_category.name as session_category_name, |
||
| 2356 | session_category.date_start session_category_date_start, |
||
| 2357 | session_category.date_end session_category_date_end, |
||
| 2358 | coach_access_start_date, |
||
| 2359 | coach_access_end_date |
||
| 2360 | FROM $tbl_session as session |
||
| 2361 | LEFT JOIN $tbl_session_category session_category |
||
| 2362 | ON (session_category_id = session_category.id) |
||
| 2363 | LEFT JOIN $tbl_session_course_user as session_rel_course_user |
||
| 2364 | ON (session_rel_course_user.session_id = session.id) |
||
| 2365 | WHERE ( |
||
| 2366 | session_rel_course_user.user_id = $user_id OR |
||
| 2367 | session.id_coach = $user_id |
||
| 2368 | ) |
||
| 2369 | ORDER BY session_category_name, name"; |
||
| 2370 | |||
| 2371 | $result = Database::query($sql); |
||
| 2372 | $categories = array(); |
||
| 2373 | if (Database::num_rows($result) > 0) { |
||
| 2374 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 2375 | |||
| 2376 | // User portal filters: |
||
| 2377 | if ($ignoreTimeLimit == false) { |
||
| 2378 | if ($is_time_over) { |
||
| 2379 | // History |
||
| 2380 | if (empty($row['access_end_date']) || $row['access_end_date'] == '0000-00-00 00:00:00') { |
||
| 2381 | continue; |
||
| 2382 | } |
||
| 2383 | |||
| 2384 | if (isset($row['access_end_date'])) { |
||
| 2385 | if ($row['access_end_date'] > $now) { |
||
| 2386 | continue; |
||
| 2387 | } |
||
| 2388 | |||
| 2389 | } |
||
| 2390 | } else { |
||
| 2391 | // Current user portal |
||
| 2392 | if (api_is_allowed_to_create_course()) { |
||
| 2393 | // Teachers can access the session depending in the access_coach date |
||
| 2394 | } else { |
||
| 2395 | if (isset($row['access_end_date']) && |
||
| 2396 | ($row['access_end_date'] != '0000-00-00 00:00:00') && |
||
| 2397 | !empty($row['access_end_date']) |
||
| 2398 | ) { |
||
| 2399 | if ($row['access_end_date'] <= $now) { |
||
| 2400 | continue; |
||
| 2401 | } |
||
| 2402 | } |
||
| 2403 | } |
||
| 2404 | } |
||
| 2405 | } |
||
| 2406 | |||
| 2407 | $categories[$row['session_category_id']]['session_category'] = array( |
||
| 2408 | 'id' => $row['session_category_id'], |
||
| 2409 | 'name' => $row['session_category_name'], |
||
| 2410 | 'date_start' => $row['session_category_date_start'], |
||
| 2411 | 'date_end' => $row['session_category_date_end'] |
||
| 2412 | ); |
||
| 2413 | |||
| 2414 | $session_id = $row['id']; |
||
| 2415 | |||
| 2416 | $courseList = UserManager::get_courses_list_by_session( |
||
| 2417 | $user_id, |
||
| 2418 | $row['id'] |
||
| 2419 | ); |
||
| 2420 | |||
| 2421 | // Session visibility. |
||
| 2422 | $visibility = api_get_session_visibility( |
||
| 2423 | $session_id, |
||
| 2424 | null, |
||
| 2425 | $ignore_visibility_for_admins |
||
| 2426 | ); |
||
| 2427 | |||
| 2428 | |||
| 2429 | // Course Coach session visibility. |
||
| 2430 | $blockedCourseCount = 0; |
||
| 2431 | $closedVisibilityList = array( |
||
| 2432 | COURSE_VISIBILITY_CLOSED, |
||
| 2433 | COURSE_VISIBILITY_HIDDEN |
||
| 2434 | ); |
||
| 2435 | |||
| 2436 | foreach ($courseList as $course) { |
||
| 2437 | // Checking session visibility |
||
| 2438 | $visibility = api_get_session_visibility( |
||
| 2439 | $session_id, |
||
| 2440 | $course['real_id'], |
||
| 2441 | $ignore_visibility_for_admins |
||
| 2442 | ); |
||
| 2443 | |||
| 2444 | $courseIsVisible = !in_array($course['visibility'], $closedVisibilityList); |
||
| 2445 | if ($courseIsVisible == false || $visibility == SESSION_INVISIBLE) { |
||
| 2446 | $blockedCourseCount++; |
||
| 2447 | } |
||
| 2448 | } |
||
| 2449 | |||
| 2450 | // If all courses are blocked then no show in the list. |
||
| 2451 | if ($blockedCourseCount == count($courseList)) { |
||
| 2452 | $visibility = SESSION_INVISIBLE; |
||
| 2453 | } |
||
| 2454 | |||
| 2455 | switch ($visibility) { |
||
| 2456 | case SESSION_VISIBLE_READ_ONLY: |
||
| 2457 | case SESSION_VISIBLE: |
||
| 2458 | case SESSION_AVAILABLE: |
||
| 2459 | break; |
||
| 2460 | case SESSION_INVISIBLE: |
||
| 2461 | if ($ignore_visibility_for_admins == false) { |
||
| 2462 | continue(2); |
||
| 2463 | } |
||
| 2464 | } |
||
| 2465 | |||
| 2466 | $categories[$row['session_category_id']]['sessions'][$row['id']] = array( |
||
| 2467 | 'session_name' => $row['name'], |
||
| 2468 | 'session_id' => $row['id'], |
||
| 2469 | 'access_start_date' => api_get_local_time($row['access_start_date']), |
||
| 2470 | 'access_end_date' => api_get_local_time($row['access_end_date']), |
||
| 2471 | 'coach_access_start_date' => api_get_local_time($row['coach_access_start_date']), |
||
| 2472 | 'coach_access_end_date' => api_get_local_time($row['coach_access_end_date']), |
||
| 2473 | 'courses' => $courseList |
||
| 2474 | ); |
||
| 2475 | } |
||
| 2476 | } |
||
| 2477 | |||
| 2478 | return $categories; |
||
| 2479 | } |
||
| 2480 | |||
| 2481 | /** |
||
| 2482 | * Gives a list of [session_id-course_code] => [status] for the current user. |
||
| 2483 | * @param integer $user_id |
||
| 2484 | * @return array list of statuses (session_id-course_code => status) |
||
| 2485 | */ |
||
| 2486 | public static function get_personal_session_course_list($user_id) |
||
| 2692 | |||
| 2693 | /** |
||
| 2694 | * Gives a list of courses for the given user in the given session |
||
| 2695 | * @param integer $user_id |
||
| 2696 | * @param integer $session_id |
||
| 2697 | * @return array list of statuses (session_id-course_code => status) |
||
| 2698 | */ |
||
| 2699 | public static function get_courses_list_by_session($user_id, $session_id) |
||
| 2819 | |||
| 2820 | /** |
||
| 2821 | * Get user id from a username |
||
| 2822 | * @param string Username |
||
| 2823 | * @return int User ID (or false if not found) |
||
| 2824 | */ |
||
| 2825 | public static function get_user_id_from_username($username) |
||
| 2844 | |||
| 2845 | /** |
||
| 2846 | * Get the users files upload from his share_folder |
||
| 2847 | * @param string User ID |
||
| 2848 | * @param string course directory |
||
| 2849 | * @param string resourcetype: images, all |
||
| 2850 | * @return int User ID (or false if not found) |
||
| 2851 | */ |
||
| 2852 | public static function get_user_upload_files_by_course($user_id, $course, $resourcetype = 'all') |
||
| 2893 | |||
| 2894 | /** |
||
| 2895 | * Gets the API key (or keys) and return them into an array |
||
| 2896 | * @param int Optional user id (defaults to the result of api_get_user_id()) |
||
| 2897 | * @return array Non-indexed array containing the list of API keys for this user, or FALSE on error |
||
| 2898 | */ |
||
| 2899 | public static function get_api_keys($user_id = null, $api_service = 'dokeos') |
||
| 2926 | |||
| 2927 | /** |
||
| 2928 | * Adds a new API key to the users' account |
||
| 2929 | * @param int Optional user ID (defaults to the results of api_get_user_id()) |
||
| 2930 | * @return boolean True on success, false on failure |
||
| 2931 | */ |
||
| 2932 | public static function add_api_key($user_id = null, $api_service = 'dokeos') |
||
| 2954 | |||
| 2955 | /** |
||
| 2956 | * Deletes an API key from the user's account |
||
| 2957 | * @param int API key's internal ID |
||
| 2958 | * @return boolean True on success, false on failure |
||
| 2959 | */ |
||
| 2960 | public static function delete_api_key($key_id) |
||
| 2980 | |||
| 2981 | /** |
||
| 2982 | * Regenerate an API key from the user's account |
||
| 2983 | * @param int user ID (defaults to the results of api_get_user_id()) |
||
| 2984 | * @param string API key's internal ID |
||
| 2985 | * @return int num |
||
| 2986 | */ |
||
| 2987 | public static function update_api_key($user_id, $api_service) |
||
| 3010 | |||
| 3011 | /** |
||
| 3012 | * @param int user ID (defaults to the results of api_get_user_id()) |
||
| 3013 | * @param string API key's internal ID |
||
| 3014 | * @return int row ID, or return false if not found |
||
| 3015 | */ |
||
| 3016 | public static function get_api_key_id($user_id, $api_service) |
||
| 3034 | |||
| 3035 | /** |
||
| 3036 | * Checks if a user_id is platform admin |
||
| 3037 | * @param int user ID |
||
| 3038 | * @return boolean True if is admin, false otherwise |
||
| 3039 | * @see main_api.lib.php::api_is_platform_admin() for a context-based check |
||
| 3040 | */ |
||
| 3041 | public static function is_admin($user_id) |
||
| 3051 | |||
| 3052 | /** |
||
| 3053 | * Get the total count of users |
||
| 3054 | * @param int Status of users to be counted |
||
| 3055 | * @param int Access URL ID (optional) |
||
| 3056 | * @return mixed Number of users or false on error |
||
| 3057 | */ |
||
| 3058 | public static function get_number_of_users($status = 0, $access_url_id = null) |
||
| 3078 | |||
| 3079 | /** |
||
| 3080 | * @author Isaac flores <[email protected]> |
||
| 3081 | * @param string The email administrator |
||
| 3082 | * @param integer The user id |
||
| 3083 | * @param string The message title |
||
| 3084 | * @param string The content message |
||
| 3085 | */ |
||
| 3086 | public static function send_message_in_outbox($email_administrator, $user_id, $title, $content) |
||
| 3110 | |||
| 3111 | /* |
||
| 3112 | * |
||
| 3113 | * USER TAGS |
||
| 3114 | * |
||
| 3115 | * Intructions to create a new user tag by Julio Montoya <[email protected]> |
||
| 3116 | * |
||
| 3117 | * 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. |
||
| 3118 | * 2. Go to profile main/auth/profile.php There you will see a special input (facebook style) that will show suggestions of tags. |
||
| 3119 | * 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 |
||
| 3120 | * 4. Tags are independent this means that tags can't be shared between tags + book + hobbies. |
||
| 3121 | * 5. Test and enjoy. |
||
| 3122 | * |
||
| 3123 | */ |
||
| 3124 | |||
| 3125 | /** |
||
| 3126 | * Gets the tags of a specific field_id |
||
| 3127 | * |
||
| 3128 | * @param int field_id |
||
| 3129 | * @param string how we are going to result value in array or in a string (json) |
||
| 3130 | * @return mixed |
||
| 3131 | * @since Nov 2009 |
||
| 3132 | * @version 1.8.6.2 |
||
| 3133 | */ |
||
| 3134 | public static function get_tags($tag, $field_id, $return_format = 'json', $limit = 10) |
||
| 3157 | |||
| 3158 | /** |
||
| 3159 | * @param int $field_id |
||
| 3160 | * @param int $limit |
||
| 3161 | * @return array |
||
| 3162 | */ |
||
| 3163 | public static function get_top_tags($field_id, $limit = 100) |
||
| 3187 | |||
| 3188 | /** |
||
| 3189 | * Get user's tags |
||
| 3190 | * @param int field_id |
||
| 3191 | * @param int user_id |
||
| 3192 | * @return array |
||
| 3193 | */ |
||
| 3194 | public static function get_user_tags($user_id, $field_id) |
||
| 3219 | |||
| 3220 | /** |
||
| 3221 | * Get user's tags |
||
| 3222 | * @param int user_id |
||
| 3223 | * @param int field_id |
||
| 3224 | * @param bool show links or not |
||
| 3225 | * @return array |
||
| 3226 | */ |
||
| 3227 | public static function get_user_tags_to_string($user_id, $field_id, $show_links = true) |
||
| 3265 | |||
| 3266 | /** |
||
| 3267 | * Get the tag id |
||
| 3268 | * @param int tag |
||
| 3269 | * @param int field_id |
||
| 3270 | * @return int returns 0 if fails otherwise the tag id |
||
| 3271 | */ |
||
| 3272 | public static function get_tag_id($tag, $field_id) |
||
| 3288 | |||
| 3289 | /** |
||
| 3290 | * Get the tag id |
||
| 3291 | * @param int tag |
||
| 3292 | * @param int field_id |
||
| 3293 | * @return int 0 if fails otherwise the tag id |
||
| 3294 | */ |
||
| 3295 | View Code Duplication | public static function get_tag_id_from_id($tag_id, $field_id) |
|
| 3310 | |||
| 3311 | /** |
||
| 3312 | * Adds a user-tag value |
||
| 3313 | * @param mixed tag |
||
| 3314 | * @param int The user id |
||
| 3315 | * @param int field id of the tag |
||
| 3316 | * @return bool |
||
| 3317 | */ |
||
| 3318 | public static function add_tag($tag, $user_id, $field_id) |
||
| 3374 | |||
| 3375 | /** |
||
| 3376 | * Deletes an user tag |
||
| 3377 | * @param int user id |
||
| 3378 | * @param int field id |
||
| 3379 | * |
||
| 3380 | */ |
||
| 3381 | public static function delete_user_tags($user_id, $field_id) |
||
| 3399 | |||
| 3400 | /** |
||
| 3401 | * Process the tag list comes from the UserManager::update_extra_field_value() function |
||
| 3402 | * @param array the tag list that will be added |
||
| 3403 | * @param int user id |
||
| 3404 | * @param int field id |
||
| 3405 | * @return bool |
||
| 3406 | */ |
||
| 3407 | public static function process_tags($tags, $user_id, $field_id) |
||
| 3420 | |||
| 3421 | /** |
||
| 3422 | * Returns a list of all administrators |
||
| 3423 | * @author jmontoya |
||
| 3424 | * @return array |
||
| 3425 | */ |
||
| 3426 | public static function get_all_administrators() |
||
| 3456 | |||
| 3457 | /** |
||
| 3458 | * Search an user (tags, first name, last name and email ) |
||
| 3459 | * @param string $tag |
||
| 3460 | * @param int $field_id field id of the tag |
||
| 3461 | * @param int $from where to start in the query |
||
| 3462 | * @param int $number_of_items |
||
| 3463 | * @param bool $getCount get count or not |
||
| 3464 | * @return array |
||
| 3465 | */ |
||
| 3466 | public static function get_all_user_tags( |
||
| 3551 | |||
| 3552 | /** |
||
| 3553 | * Get extra filtrable user fields (only type select) |
||
| 3554 | * @return array |
||
| 3555 | */ |
||
| 3556 | public static function get_extra_filtrable_fields() |
||
| 3578 | |||
| 3579 | /** |
||
| 3580 | * Get extra where clauses for finding users based on extra filtrable user fields (type select) |
||
| 3581 | * @return string With AND clauses based on user's ID which have the values to search in extra user fields |
||
| 3582 | */ |
||
| 3583 | public static function get_search_form_where_extra_fields() |
||
| 3625 | |||
| 3626 | /** |
||
| 3627 | * Show the search form |
||
| 3628 | * @param string $query the value of the search box |
||
| 3629 | * @return string HTML form |
||
| 3630 | */ |
||
| 3631 | public static function get_search_form($query) |
||
| 3696 | |||
| 3697 | /** |
||
| 3698 | * Shows the user menu |
||
| 3699 | */ |
||
| 3700 | public static function show_menu() |
||
| 3711 | |||
| 3712 | /** |
||
| 3713 | * Allow to register contact to social network |
||
| 3714 | * @param int $friend_id user friend id |
||
| 3715 | * @param int $my_user_id user id |
||
| 3716 | * @param int $relation_type relation between users see constants definition |
||
| 3717 | */ |
||
| 3718 | public static function relate_users($friend_id, $my_user_id, $relation_type) |
||
| 3766 | |||
| 3767 | /** |
||
| 3768 | * Deletes a contact |
||
| 3769 | * @param int user friend id |
||
| 3770 | * @param bool true will delete ALL friends relationship from $friend_id |
||
| 3771 | * @author isaac flores paz <[email protected]> |
||
| 3772 | * @author Julio Montoya <[email protected]> Cleaning code |
||
| 3773 | */ |
||
| 3774 | public static function remove_user_rel_user($friend_id, $real_removed = false, $with_status_condition = '') |
||
| 3775 | { |
||
| 3776 | $tbl_my_friend = Database :: get_main_table(TABLE_MAIN_USER_REL_USER); |
||
| 3777 | $tbl_my_message = Database :: get_main_table(TABLE_MESSAGE); |
||
| 3778 | $friend_id = intval($friend_id); |
||
| 3779 | |||
| 3780 | if ($real_removed) { |
||
| 3781 | $extra_condition = ''; |
||
| 3782 | if ($with_status_condition != '') { |
||
| 3783 | $extra_condition = ' AND relation_type = '.intval($with_status_condition); |
||
| 3784 | } |
||
| 3785 | $sql = 'DELETE FROM '.$tbl_my_friend.' |
||
| 3786 | WHERE relation_type <> '.USER_RELATION_TYPE_RRHH.' AND friend_user_id='.$friend_id.' '.$extra_condition; |
||
| 3787 | Database::query($sql); |
||
| 3788 | $sql= 'DELETE FROM '.$tbl_my_friend.' |
||
| 3789 | WHERE relation_type <> '.USER_RELATION_TYPE_RRHH.' AND user_id='.$friend_id.' '.$extra_condition; |
||
| 3790 | Database::query($sql); |
||
| 3791 | } else { |
||
| 3792 | $user_id = api_get_user_id(); |
||
| 3793 | $sql = 'SELECT COUNT(*) as count FROM '.$tbl_my_friend.' |
||
| 3794 | WHERE |
||
| 3795 | user_id='.$user_id.' AND |
||
| 3796 | relation_type NOT IN('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.') AND |
||
| 3797 | friend_user_id='.$friend_id; |
||
| 3798 | $result = Database::query($sql); |
||
| 3799 | $row = Database :: fetch_array($result, 'ASSOC'); |
||
| 3800 | if ($row['count'] == 1) { |
||
| 3801 | //Delete user rel user |
||
| 3802 | $sql_i = 'UPDATE '.$tbl_my_friend.' SET relation_type='.USER_RELATION_TYPE_DELETED.' |
||
| 3803 | WHERE user_id='.$user_id.' AND friend_user_id='.$friend_id; |
||
| 3804 | $sql_j = 'UPDATE '.$tbl_my_message.' SET msg_status='.MESSAGE_STATUS_INVITATION_DENIED.' |
||
| 3805 | WHERE user_receiver_id='.$user_id.' AND user_sender_id='.$friend_id.' AND update_date="0000-00-00 00:00:00" '; |
||
| 3806 | //Delete user |
||
| 3807 | $sql_ij = 'UPDATE '.$tbl_my_friend.' SET relation_type='.USER_RELATION_TYPE_DELETED.' |
||
| 3808 | WHERE user_id='.$friend_id.' AND friend_user_id='.$user_id; |
||
| 3809 | $sql_ji = 'UPDATE '.$tbl_my_message.' SET msg_status='.MESSAGE_STATUS_INVITATION_DENIED.' |
||
| 3810 | WHERE user_receiver_id='.$friend_id.' AND user_sender_id='.$user_id.' AND update_date="0000-00-00 00:00:00" '; |
||
| 3811 | Database::query($sql_i); |
||
| 3812 | Database::query($sql_j); |
||
| 3813 | Database::query($sql_ij); |
||
| 3814 | Database::query($sql_ji); |
||
| 3815 | } |
||
| 3816 | } |
||
| 3817 | } |
||
| 3818 | |||
| 3819 | /** |
||
| 3820 | * @param int $userId |
||
| 3821 | * @return array |
||
| 3822 | */ |
||
| 3823 | public static function getDrhListFromUser($userId) |
||
| 3851 | |||
| 3852 | /** |
||
| 3853 | * get users followed by human resource manager |
||
| 3854 | * @param int $userId |
||
| 3855 | * @param int $userStatus (STUDENT, COURSEMANAGER, etc) |
||
| 3856 | * @param bool $getOnlyUserId |
||
| 3857 | * @param bool $getSql |
||
| 3858 | * @param bool $getCount |
||
| 3859 | * @param int $from |
||
| 3860 | * @param int $numberItems |
||
| 3861 | * @param int $column |
||
| 3862 | * @param string $direction |
||
| 3863 | * @param int $active |
||
| 3864 | * @param string $lastConnectionDate |
||
| 3865 | * @return array users |
||
| 3866 | */ |
||
| 3867 | View Code Duplication | public static function get_users_followed_by_drh( |
|
| 3895 | |||
| 3896 | /** |
||
| 3897 | * Get users followed by human resource manager |
||
| 3898 | * @param int $userId |
||
| 3899 | * @param int $userStatus Filter users by status (STUDENT, COURSEMANAGER, etc) |
||
| 3900 | * @param bool $getOnlyUserId |
||
| 3901 | * @param bool $getSql |
||
| 3902 | * @param bool $getCount |
||
| 3903 | * @param int $from |
||
| 3904 | * @param int $numberItems |
||
| 3905 | * @param int $column |
||
| 3906 | * @param string $direction |
||
| 3907 | * @param int $active |
||
| 3908 | * @param string $lastConnectionDate |
||
| 3909 | * @param int $status the function is called by who? COURSEMANAGER, DRH? |
||
| 3910 | * @param string $keyword |
||
| 3911 | * |
||
| 3912 | * @return array user list |
||
| 3913 | */ |
||
| 3914 | public static function getUsersFollowedByUser( |
||
| 4113 | |||
| 4114 | /** |
||
| 4115 | * Subscribes users to human resource manager (Dashboard feature) |
||
| 4116 | * @param int hr dept id |
||
| 4117 | * @param array Users id |
||
| 4118 | * @param int affected rows |
||
| 4119 | * */ |
||
| 4120 | public static function suscribe_users_to_hr_manager($hr_dept_id, $users_id) |
||
| 4124 | |||
| 4125 | /** |
||
| 4126 | * Add subscribed users to a user by relation type |
||
| 4127 | * @param int $userId The user id |
||
| 4128 | * @param array $subscribedUsersId The id of suscribed users |
||
| 4129 | * @param action $relationType The relation type |
||
| 4130 | */ |
||
| 4131 | public static function subscribeUsersToUser($userId, $subscribedUsersId, $relationType, $deleteUsersBeforeInsert = false) |
||
| 4189 | |||
| 4190 | /** |
||
| 4191 | * This function check if an user is followed by human resources manager |
||
| 4192 | * @param int User id |
||
| 4193 | * @param int Human resources manager |
||
| 4194 | * @return bool |
||
| 4195 | */ |
||
| 4196 | public static function is_user_followed_by_drh($user_id, $hr_dept_id) |
||
| 4215 | |||
| 4216 | /** |
||
| 4217 | * get user id of teacher or session administrator |
||
| 4218 | * @param array $courseInfo |
||
| 4219 | * |
||
| 4220 | * @return int The user id |
||
| 4221 | */ |
||
| 4222 | public static function get_user_id_of_course_admin_or_session_admin($courseInfo) |
||
| 4261 | |||
| 4262 | /** |
||
| 4263 | * Determines if a user is a gradebook certified |
||
| 4264 | * @param int The category id of gradebook |
||
| 4265 | * @param int The user id |
||
| 4266 | * @return boolean |
||
| 4267 | */ |
||
| 4268 | public static function is_user_certified($cat_id, $user_id) |
||
| 4283 | |||
| 4284 | /** |
||
| 4285 | * Gets the info about a gradebook certificate for a user by course |
||
| 4286 | * @param string The course code |
||
| 4287 | * @param int The user id |
||
| 4288 | * @return array if there is not information return false |
||
| 4289 | */ |
||
| 4290 | public static function get_info_gradebook_certificate($course_code, $user_id) |
||
| 4324 | |||
| 4325 | /** |
||
| 4326 | * Gets the user path of user certificated |
||
| 4327 | * @param int The user id |
||
| 4328 | * @return array containing path_certificate and cat_id |
||
| 4329 | */ |
||
| 4330 | public static function get_user_path_certificate($user_id) |
||
| 4356 | |||
| 4357 | /** |
||
| 4358 | * This function check if the user is a coach inside session course |
||
| 4359 | * @param int User id |
||
| 4360 | * @param int $courseId |
||
| 4361 | * @param int Session id |
||
| 4362 | * @return bool True if the user is a coach |
||
| 4363 | * |
||
| 4364 | */ |
||
| 4365 | public static function is_session_course_coach($user_id, $courseId, $session_id) |
||
| 4387 | |||
| 4388 | /** |
||
| 4389 | * This function returns an icon path that represents the favicon of the website of which the url given. |
||
| 4390 | * Defaults to the current Chamilo favicon |
||
| 4391 | * @param string URL of website where to look for favicon.ico |
||
| 4392 | * @param string Optional second URL of website where to look for favicon.ico |
||
| 4393 | * @return string Path of icon to load |
||
| 4394 | */ |
||
| 4395 | public static function get_favicon_from_url($url1, $url2 = null) |
||
| 4412 | |||
| 4413 | /** |
||
| 4414 | * |
||
| 4415 | * @param int student id |
||
| 4416 | * @param int years |
||
| 4417 | * @param bool show warning_message |
||
| 4418 | * @param bool return_timestamp |
||
| 4419 | */ |
||
| 4420 | public static function delete_inactive_student($student_id, $years = 2, $warning_message = false, $return_timestamp = false) |
||
| 4456 | |||
| 4457 | /** |
||
| 4458 | * @param FormValidator $form |
||
| 4459 | * @param $extra_data |
||
| 4460 | * @param $form_name |
||
| 4461 | * @param bool $admin_permissions |
||
| 4462 | * @param null $user_id |
||
| 4463 | * @deprecated |
||
| 4464 | * @return array |
||
| 4465 | */ |
||
| 4466 | static function set_extra_fields_in_form( |
||
| 4749 | |||
| 4750 | /** |
||
| 4751 | * @return array |
||
| 4752 | */ |
||
| 4753 | static function get_user_field_types() |
||
| 4773 | |||
| 4774 | /** |
||
| 4775 | * @param int $userId |
||
| 4776 | */ |
||
| 4777 | View Code Duplication | static function add_user_as_admin($userId) |
|
| 4787 | |||
| 4788 | /** |
||
| 4789 | * @param int $userId |
||
| 4790 | */ |
||
| 4791 | View Code Duplication | public static function remove_user_admin($userId) |
|
| 4800 | |||
| 4801 | /** |
||
| 4802 | * @param string $from |
||
| 4803 | * @param string $to |
||
| 4804 | */ |
||
| 4805 | public static function update_all_user_languages($from, $to) |
||
| 4817 | |||
| 4818 | /** |
||
| 4819 | * Subscribe boss to students |
||
| 4820 | * |
||
| 4821 | * @param int $bossId The boss id |
||
| 4822 | * @param array $usersId The users array |
||
| 4823 | * @return int Affected rows |
||
| 4824 | */ |
||
| 4825 | public static function subscribeBossToUsers($bossId, $usersId) |
||
| 4829 | |||
| 4830 | /** |
||
| 4831 | * Subscribe boss to students |
||
| 4832 | * |
||
| 4833 | * @param int $studentId |
||
| 4834 | * @param array $bossList |
||
| 4835 | * @return int Affected rows |
||
| 4836 | */ |
||
| 4837 | public static function subscribeUserToBossList($studentId, $bossList) |
||
| 4856 | |||
| 4857 | /** |
||
| 4858 | * Get users followed by student boss |
||
| 4859 | * @param int $userId |
||
| 4860 | * @param int $userStatus (STUDENT, COURSEMANAGER, etc) |
||
| 4861 | * @param bool $getOnlyUserId |
||
| 4862 | * @param bool $getSql |
||
| 4863 | * @param bool $getCount |
||
| 4864 | * @param int $from |
||
| 4865 | * @param int $numberItems |
||
| 4866 | * @param int $column |
||
| 4867 | * @param string $direction |
||
| 4868 | * @param int $active |
||
| 4869 | * @param string $lastConnectionDate |
||
| 4870 | * @return array users |
||
| 4871 | */ |
||
| 4872 | View Code Duplication | public static function getUsersFollowedByStudentBoss( |
|
| 4900 | |||
| 4901 | /** |
||
| 4902 | * Get the teacher (users with COURSEMANGER status) list |
||
| 4903 | * @return array The list |
||
| 4904 | */ |
||
| 4905 | public static function getTeachersList() |
||
| 4921 | |||
| 4922 | /** |
||
| 4923 | * @return array |
||
| 4924 | */ |
||
| 4925 | View Code Duplication | public static function getOfficialCodeGrouped() |
|
| 4941 | |||
| 4942 | /** |
||
| 4943 | * @param string $officialCode |
||
| 4944 | * @return array |
||
| 4945 | */ |
||
| 4946 | public static function getUsersByOfficialCode($officialCode) |
||
| 4963 | |||
| 4964 | /** |
||
| 4965 | * Calc the expended time (in seconds) by a user in a course |
||
| 4966 | * @param int $userId The user id |
||
| 4967 | * @param int $courseId The course id |
||
| 4968 | * @param int $sessionId Optional. The session id |
||
| 4969 | * @param string $from Optional. From date |
||
| 4970 | * @param string $until Optional. Until date |
||
| 4971 | * @return int The time |
||
| 4972 | */ |
||
| 4973 | public static function getTimeSpentInCourses($userId, $courseId, $sessionId = 0, $from = '', $until = '') |
||
| 5005 | |||
| 5006 | /** |
||
| 5007 | * Get the boss user ID from a followed user id |
||
| 5008 | * @param $userId |
||
| 5009 | * @return bool |
||
| 5010 | */ |
||
| 5011 | View Code Duplication | public static function getFirstStudentBoss($userId) |
|
| 5036 | |||
| 5037 | /** |
||
| 5038 | * Get the boss user ID from a followed user id |
||
| 5039 | * @param $userId |
||
| 5040 | * @return bool |
||
| 5041 | */ |
||
| 5042 | View Code Duplication | public static function getStudentBossList($userId) |
|
| 5066 | |||
| 5067 | /** |
||
| 5068 | * Get either a Gravatar URL or complete image tag for a specified email address. |
||
| 5069 | * |
||
| 5070 | * @param string $email The email address |
||
| 5071 | * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] |
||
| 5072 | * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] |
||
| 5073 | * @param string $r Maximum rating (inclusive) [ g | pg | r | x ] |
||
| 5074 | * @param boole $img True to return a complete IMG tag False for just the URL |
||
| 5075 | * @param array $atts Optional, additional key/value attributes to include in the IMG tag |
||
| 5076 | * @return String containing either just a URL or a complete image tag |
||
| 5077 | * @source http://gravatar.com/site/implement/images/php/ |
||
| 5078 | */ |
||
| 5079 | private static function getGravatar( |
||
| 5101 | |||
| 5102 | |||
| 5103 | |||
| 5104 | /** |
||
| 5105 | * Displays the name of the user and makes the link to the user profile |
||
| 5106 | * @param array $userInfo |
||
| 5107 | * |
||
| 5108 | * @return string |
||
| 5109 | */ |
||
| 5110 | public static function getUserProfileLink($userInfo) |
||
| 5118 | |||
| 5119 | /** |
||
| 5120 | * Displays the name of the user and makes the link to the user profile |
||
| 5121 | * |
||
| 5122 | * @param $userInfo |
||
| 5123 | * |
||
| 5124 | * @return string |
||
| 5125 | */ |
||
| 5126 | public static function getUserProfileLinkWithPicture($userInfo) |
||
| 5130 | |||
| 5131 | /** |
||
| 5132 | * Get users whose name matches $firstname and $lastname |
||
| 5133 | * @param string $firstname Firstname to search |
||
| 5134 | * @param string $lastname Lastname to search |
||
| 5135 | * @return array The user list |
||
| 5136 | */ |
||
| 5137 | View Code Duplication | public static function getUserByName($firstname, $lastname) |
|
| 5161 | |||
| 5162 | /** |
||
| 5163 | * @param int $optionSelected |
||
| 5164 | * @return string |
||
| 5165 | */ |
||
| 5166 | public static function getUserSubscriptionTab($optionSelected = 1) |
||
| 5204 | |||
| 5205 | } |
||
| 5206 |
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: