| Total Complexity | 118 |
| Total Lines | 1435 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UserRepository 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.
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 UserRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 70 | class UserRepository extends ResourceRepository implements UserLoaderInterface, PasswordUpgraderInterface |
||
| 71 | { |
||
| 72 | public function __construct(ManagerRegistry $registry) |
||
| 73 | { |
||
| 74 | parent::__construct($registry, User::class); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var UserPasswordEncoderInterface |
||
| 79 | */ |
||
| 80 | protected $encoder; |
||
| 81 | |||
| 82 | public function setEncoder(UserPasswordEncoderInterface $encoder) |
||
| 83 | { |
||
| 84 | $this->encoder = $encoder; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function loadUserByUsername($username): ?User |
||
| 88 | { |
||
| 89 | return $this->findBy(['username' => $username]); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function updateUser($user, $andFlush = true) |
||
| 93 | { |
||
| 94 | $this->updateCanonicalFields($user); |
||
| 95 | $this->updatePassword($user); |
||
| 96 | $this->getEntityManager()->persist($user); |
||
| 97 | if ($andFlush) { |
||
| 98 | $this->getEntityManager()->flush(); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | public function canonicalize($string) |
||
| 103 | { |
||
| 104 | $encoding = mb_detect_encoding($string); |
||
| 105 | |||
| 106 | return $encoding |
||
| 107 | ? mb_convert_case($string, MB_CASE_LOWER, $encoding) |
||
| 108 | : mb_convert_case($string, MB_CASE_LOWER); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function updateCanonicalFields(UserInterface $user) |
||
| 112 | { |
||
| 113 | $user->setUsernameCanonical($this->canonicalize($user->getUsername())); |
||
| 114 | $user->setEmailCanonical($this->canonicalize($user->getEmail())); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function updatePassword(UserInterface $user) |
||
| 118 | { |
||
| 119 | //UserPasswordEncoderInterface $passwordEncoder |
||
| 120 | if (0 !== strlen($password = $user->getPlainPassword())) { |
||
| 121 | $password = $this->encoder->encodePassword($user, $password); |
||
| 122 | $user->setPassword($password); |
||
| 123 | $user->eraseCredentials(); |
||
| 124 | // $encoder = $this->getEncoder($user); |
||
| 125 | //$user->setPassword($encoder->encodePassword($password, $user->getSalt())); |
||
| 126 | //$user->eraseCredentials(); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | public function upgradePassword(UserInterface $user, string $newEncodedPassword): void |
||
| 131 | { |
||
| 132 | // this code is only an example; the exact code will depend on |
||
| 133 | // your own application needs |
||
| 134 | $user->setPassword($newEncodedPassword); |
||
| 135 | $this->getEntityManager()->persist($user); |
||
| 136 | $this->getEntityManager()->flush(); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function getRootUser(): User |
||
| 140 | { |
||
| 141 | $qb = $this->getRepository()->createQueryBuilder('u'); |
||
| 142 | $qb |
||
| 143 | ->innerJoin( |
||
| 144 | 'u.resourceNode', |
||
| 145 | 'r' |
||
| 146 | ); |
||
| 147 | $qb->where('r.creator = u'); |
||
| 148 | $qb->andWhere('r.parent IS NULL'); |
||
| 149 | $qb->getFirstResult(); |
||
| 150 | |||
| 151 | $rootUser = $qb->getQuery()->getSingleResult(); |
||
| 152 | |||
| 153 | if (null === $rootUser) { |
||
| 154 | throw new UsernameNotFoundException('Root user not found'); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $rootUser; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function deleteUser(User $user) |
||
| 161 | { |
||
| 162 | $em = $this->getEntityManager(); |
||
| 163 | $type = $user->getResourceNode()->getResourceType(); |
||
| 164 | $rootUser = $this->getRootUser(); |
||
| 165 | |||
| 166 | // User children will be set to the root user. |
||
| 167 | $criteria = Criteria::create()->where(Criteria::expr()->eq('resourceType', $type)); |
||
| 168 | $userNodeCreatedList = $user->getResourceNodes()->matching($criteria); |
||
| 169 | /** @var ResourceNode $userCreated */ |
||
| 170 | foreach ($userNodeCreatedList as $userCreated) { |
||
| 171 | $userCreated->setCreator($rootUser); |
||
| 172 | } |
||
| 173 | |||
| 174 | $em->remove($user->getResourceNode()); |
||
| 175 | |||
| 176 | foreach ($user->getGroups() as $group) { |
||
| 177 | $user->removeGroup($group); |
||
| 178 | } |
||
| 179 | |||
| 180 | $em->remove($user); |
||
| 181 | $em->flush(); |
||
| 182 | } |
||
| 183 | |||
| 184 | public function addUserToResourceNode(int $userId, int $creatorId): ResourceNode |
||
| 185 | { |
||
| 186 | /** @var User $user */ |
||
| 187 | $user = $this->find($userId); |
||
| 188 | $creator = $this->find($creatorId); |
||
| 189 | |||
| 190 | $resourceNode = new ResourceNode(); |
||
| 191 | $resourceNode |
||
| 192 | ->setTitle($user->getUsername()) |
||
| 193 | ->setCreator($creator) |
||
| 194 | ->setResourceType($this->getResourceType()) |
||
| 195 | //->setParent($resourceNode) |
||
| 196 | ; |
||
| 197 | |||
| 198 | $user->setResourceNode($resourceNode); |
||
| 199 | |||
| 200 | $this->getEntityManager()->persist($resourceNode); |
||
| 201 | $this->getEntityManager()->persist($user); |
||
| 202 | |||
| 203 | return $resourceNode; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function findByUsername(string $username): ?User |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $role |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | public function findByRole($role) |
||
| 223 | { |
||
| 224 | $em = $this->repository->getEntityManager(); |
||
| 225 | $qb = $em->createQueryBuilder(); |
||
| 226 | |||
| 227 | $qb->select('u') |
||
| 228 | ->from($this->_entityName, 'u') |
||
| 229 | ->where('u.roles LIKE :roles') |
||
| 230 | ->setParameter('roles', '%"'.$role.'"%'); |
||
| 231 | |||
| 232 | return $qb->getQuery()->getResult(); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $keyword |
||
| 237 | */ |
||
| 238 | public function searchUserByKeyword($keyword) |
||
| 239 | { |
||
| 240 | $qb = $this->repository->createQueryBuilder('a'); |
||
| 241 | |||
| 242 | // Selecting user info |
||
| 243 | $qb->select('DISTINCT b'); |
||
| 244 | |||
| 245 | $qb->from('Chamilo\CoreBundle\Entity\User', 'b'); |
||
| 246 | |||
| 247 | // Selecting courses for users |
||
| 248 | //$qb->innerJoin('u.courses', 'c'); |
||
| 249 | |||
| 250 | //@todo check app settings |
||
| 251 | $qb->add('orderBy', 'b.firstname ASC'); |
||
| 252 | $qb->where('b.firstname LIKE :keyword OR b.lastname LIKE :keyword '); |
||
| 253 | $qb->setParameter('keyword', "%$keyword%"); |
||
| 254 | $query = $qb->getQuery(); |
||
| 255 | |||
| 256 | return $query->execute(); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get course user relationship based in the course_rel_user table. |
||
| 261 | * |
||
| 262 | * @return Course[] |
||
| 263 | */ |
||
| 264 | public function getCourses(User $user, AccessUrl $url, int $status, $keyword = '') |
||
| 265 | { |
||
| 266 | $qb = $this->repository->createQueryBuilder('user'); |
||
| 267 | |||
| 268 | $qb |
||
| 269 | ->select('DISTINCT course') |
||
| 270 | ->innerJoin( |
||
| 271 | CourseRelUser::class, |
||
| 272 | 'courseRelUser', |
||
| 273 | Join::WITH, |
||
| 274 | 'user = courseRelUser.user' |
||
| 275 | ) |
||
| 276 | ->innerJoin('courseRelUser.course', 'course') |
||
| 277 | ->innerJoin('course.urls', 'accessUrlRelCourse') |
||
| 278 | ->innerJoin('accessUrlRelCourse.url', 'url') |
||
| 279 | ->where('user = :user') |
||
| 280 | ->andWhere('url = :url') |
||
| 281 | ->andWhere('courseRelUser.status = :status') |
||
| 282 | ->setParameters(['user' => $user, 'url' => $url, 'status' => $status]) |
||
| 283 | ->addSelect('courseRelUser'); |
||
| 284 | |||
| 285 | if (!empty($keyword)) { |
||
| 286 | $qb |
||
| 287 | ->andWhere('course.title like = :keyword OR course.code like = :keyword') |
||
| 288 | ->setParameter('keyword', $keyword); |
||
| 289 | } |
||
| 290 | |||
| 291 | $qb->add('orderBy', 'course.title DESC'); |
||
| 292 | |||
| 293 | $query = $qb->getQuery(); |
||
| 294 | |||
| 295 | return $query->getResult(); |
||
| 296 | } |
||
| 297 | |||
| 298 | /* |
||
| 299 | public function getTeachers() |
||
| 300 | { |
||
| 301 | $queryBuilder = $this->repository->createQueryBuilder('u'); |
||
| 302 | |||
| 303 | // Selecting course info. |
||
| 304 | $queryBuilder |
||
| 305 | ->select('u') |
||
| 306 | ->where('u.groups.id = :groupId') |
||
| 307 | ->setParameter('groupId', 1); |
||
| 308 | |||
| 309 | $query = $queryBuilder->getQuery(); |
||
| 310 | |||
| 311 | return $query->execute(); |
||
| 312 | }*/ |
||
| 313 | |||
| 314 | /*public function getUsers($group) |
||
| 315 | { |
||
| 316 | $queryBuilder = $this->repository->createQueryBuilder('u'); |
||
| 317 | |||
| 318 | // Selecting course info. |
||
| 319 | $queryBuilder |
||
| 320 | ->select('u') |
||
| 321 | ->where('u.groups = :groupId') |
||
| 322 | ->setParameter('groupId', $group); |
||
| 323 | |||
| 324 | $query = $queryBuilder->getQuery(); |
||
| 325 | |||
| 326 | return $query->execute(); |
||
| 327 | }*/ |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get a filtered list of user by status and (optionally) access url. |
||
| 331 | * |
||
| 332 | * @todo not use status |
||
| 333 | * |
||
| 334 | * @param string $query The query to filter |
||
| 335 | * @param int $status The status |
||
| 336 | * @param int $accessUrlId The access URL ID |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | public function findByStatus($query, $status, $accessUrlId = null) |
||
| 341 | { |
||
| 342 | $accessUrlId = (int) $accessUrlId; |
||
| 343 | $queryBuilder = $this->repository->createQueryBuilder('u'); |
||
| 344 | |||
| 345 | if ($accessUrlId > 0) { |
||
| 346 | $queryBuilder->innerJoin( |
||
| 347 | 'ChamiloCoreBundle:AccessUrlRelUser', |
||
| 348 | 'auru', |
||
| 349 | Join::WITH, |
||
| 350 | 'u.id = auru.user' |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | |||
| 354 | $queryBuilder->where('u.status = :status') |
||
| 355 | ->andWhere('u.username LIKE :query OR u.firstname LIKE :query OR u.lastname LIKE :query') |
||
| 356 | ->setParameter('status', $status) |
||
| 357 | ->setParameter('query', "$query%"); |
||
| 358 | |||
| 359 | if ($accessUrlId > 0) { |
||
| 360 | $queryBuilder->andWhere('auru.url = :url') |
||
| 361 | ->setParameter(':url', $accessUrlId); |
||
| 362 | } |
||
| 363 | |||
| 364 | return $queryBuilder->getQuery()->getResult(); |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get the coaches for a course within a session. |
||
| 369 | * |
||
| 370 | * @param Session $session The session |
||
| 371 | * @param Course $course The course |
||
| 372 | * |
||
| 373 | * @return \Doctrine\ORM\QueryBuilder |
||
| 374 | */ |
||
| 375 | public function getCoachesForSessionCourse(Session $session, Course $course) |
||
| 376 | { |
||
| 377 | $queryBuilder = $this->repository->createQueryBuilder('u'); |
||
| 378 | |||
| 379 | $queryBuilder->select('u') |
||
| 380 | ->innerJoin( |
||
| 381 | 'ChamiloCoreBundle:SessionRelCourseRelUser', |
||
| 382 | 'scu', |
||
| 383 | Join::WITH, |
||
| 384 | 'scu.user = u' |
||
| 385 | ) |
||
| 386 | ->where( |
||
| 387 | $queryBuilder->expr()->andX( |
||
| 388 | $queryBuilder->expr()->eq('scu.session', $session->getId()), |
||
| 389 | $queryBuilder->expr()->eq('scu.course', $course->getId()), |
||
| 390 | $queryBuilder->expr()->eq('scu.status', SessionRelCourseRelUser::STATUS_COURSE_COACH) |
||
| 391 | ) |
||
| 392 | ); |
||
| 393 | |||
| 394 | return $queryBuilder->getQuery()->getResult(); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get course user relationship based in the course_rel_user table. |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | /*public function getCourses(User $user) |
||
| 403 | { |
||
| 404 | $queryBuilder = $this->createQueryBuilder('user'); |
||
| 405 | |||
| 406 | // Selecting course info. |
||
| 407 | $queryBuilder->select('c'); |
||
| 408 | |||
| 409 | // Loading User. |
||
| 410 | //$qb->from('Chamilo\CoreBundle\Entity\User', 'u'); |
||
| 411 | |||
| 412 | // Selecting course |
||
| 413 | $queryBuilder->innerJoin('Chamilo\CoreBundle\Entity\Course', 'c'); |
||
| 414 | |||
| 415 | //@todo check app settings |
||
| 416 | //$qb->add('orderBy', 'u.lastname ASC'); |
||
| 417 | |||
| 418 | $wherePart = $queryBuilder->expr()->andx(); |
||
| 419 | |||
| 420 | // Get only users subscribed to this course |
||
| 421 | $wherePart->add($queryBuilder->expr()->eq('user.userId', $user->getUserId())); |
||
| 422 | |||
| 423 | $queryBuilder->where($wherePart); |
||
| 424 | $query = $queryBuilder->getQuery(); |
||
| 425 | |||
| 426 | return $query->execute(); |
||
| 427 | } |
||
| 428 | |||
| 429 | public function getTeachers() |
||
| 430 | { |
||
| 431 | $queryBuilder = $this->createQueryBuilder('u'); |
||
| 432 | |||
| 433 | // Selecting course info. |
||
| 434 | $queryBuilder |
||
| 435 | ->select('u') |
||
| 436 | ->where('u.groups.id = :groupId') |
||
| 437 | ->setParameter('groupId', 1); |
||
| 438 | |||
| 439 | $query = $queryBuilder->getQuery(); |
||
| 440 | |||
| 441 | return $query->execute(); |
||
| 442 | }*/ |
||
| 443 | |||
| 444 | /*public function getUsers($group) |
||
| 445 | { |
||
| 446 | $queryBuilder = $this->createQueryBuilder('u'); |
||
| 447 | |||
| 448 | // Selecting course info. |
||
| 449 | $queryBuilder |
||
| 450 | ->select('u') |
||
| 451 | ->where('u.groups = :groupId') |
||
| 452 | ->setParameter('groupId', $group); |
||
| 453 | |||
| 454 | $query = $queryBuilder->getQuery(); |
||
| 455 | |||
| 456 | return $query->execute(); |
||
| 457 | }*/ |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get the sessions admins for a user. |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | public function getSessionAdmins(User $user) |
||
| 465 | { |
||
| 466 | $queryBuilder = $this->repository->createQueryBuilder('u'); |
||
| 467 | $queryBuilder |
||
| 468 | ->distinct() |
||
| 469 | ->innerJoin( |
||
| 470 | 'ChamiloCoreBundle:SessionRelUser', |
||
| 471 | 'su', |
||
| 472 | Join::WITH, |
||
| 473 | $queryBuilder->expr()->eq('u', 'su.user') |
||
| 474 | ) |
||
| 475 | ->innerJoin( |
||
| 476 | 'ChamiloCoreBundle:SessionRelCourseRelUser', |
||
| 477 | 'scu', |
||
| 478 | Join::WITH, |
||
| 479 | $queryBuilder->expr()->eq('su.session', 'scu.session') |
||
| 480 | ) |
||
| 481 | ->where( |
||
| 482 | $queryBuilder->expr()->eq('scu.user', $user->getId()) |
||
| 483 | ) |
||
| 484 | ->andWhere( |
||
| 485 | $queryBuilder->expr()->eq('su.relationType', SESSION_RELATION_TYPE_RRHH) |
||
| 486 | ) |
||
| 487 | ; |
||
| 488 | |||
| 489 | return $queryBuilder->getQuery()->getResult(); |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Get the student bosses for a user. |
||
| 494 | * |
||
| 495 | * @return array |
||
| 496 | */ |
||
| 497 | public function getStudentBosses(User $user) |
||
| 498 | { |
||
| 499 | $queryBuilder = $this->repository->createQueryBuilder('u'); |
||
| 500 | $queryBuilder |
||
| 501 | ->distinct() |
||
| 502 | ->innerJoin( |
||
| 503 | 'ChamiloCoreBundle:UserRelUser', |
||
| 504 | 'uu', |
||
| 505 | Join::WITH, |
||
| 506 | $queryBuilder->expr()->eq('u.id', 'uu.friendUserId') |
||
| 507 | ) |
||
| 508 | ->where( |
||
| 509 | $queryBuilder->expr()->eq('uu.relationType', USER_RELATION_TYPE_BOSS) |
||
| 510 | ) |
||
| 511 | ->andWhere( |
||
| 512 | $queryBuilder->expr()->eq('uu.userId', $user->getId()) |
||
| 513 | ); |
||
| 514 | |||
| 515 | return $queryBuilder->getQuery()->getResult(); |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get number of users in URL. |
||
| 520 | * |
||
| 521 | * @return int |
||
| 522 | */ |
||
| 523 | public function getCountUsersByUrl(AccessUrl $url) |
||
| 524 | { |
||
| 525 | return $this->repository->createQueryBuilder('a') |
||
| 526 | ->select('COUNT(a)') |
||
| 527 | ->innerJoin('a.portals', 'u') |
||
| 528 | ->where('u.portal = :u') |
||
| 529 | ->setParameters(['u' => $url]) |
||
| 530 | ->getQuery() |
||
| 531 | ->getSingleScalarResult(); |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Get number of users in URL. |
||
| 536 | * |
||
| 537 | * @return int |
||
| 538 | */ |
||
| 539 | public function getCountTeachersByUrl(AccessUrl $url) |
||
| 540 | { |
||
| 541 | $qb = $this->repository->createQueryBuilder('a'); |
||
| 542 | |||
| 543 | return $qb |
||
| 544 | ->select('COUNT(a)') |
||
| 545 | ->innerJoin('a.portals', 'u') |
||
| 546 | ->where('u.portal = :u') |
||
| 547 | ->andWhere($qb->expr()->in('a.roles', ['ROLE_TEACHER'])) |
||
| 548 | ->setParameters(['u' => $url]) |
||
| 549 | ->getQuery() |
||
| 550 | ->getSingleScalarResult() |
||
| 551 | ; |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Find potential users to send a message. |
||
| 556 | * |
||
| 557 | * @param int $currentUserId The current user ID |
||
| 558 | * @param string $searchFilter Optional. The search text to filter the user list |
||
| 559 | * @param int $limit Optional. Sets the maximum number of results to retrieve |
||
| 560 | */ |
||
| 561 | public function findUsersToSendMessage($currentUserId, $searchFilter = null, $limit = 10) |
||
| 562 | { |
||
| 563 | $allowSendMessageToAllUsers = api_get_setting('allow_send_message_to_all_platform_users'); |
||
| 564 | $accessUrlId = api_get_multiple_access_url() ? api_get_current_access_url_id() : 1; |
||
| 565 | |||
| 566 | if ('true' === api_get_setting('allow_social_tool') && |
||
| 567 | 'true' === api_get_setting('allow_message_tool') |
||
| 568 | ) { |
||
| 569 | // All users |
||
| 570 | if ('true' === $allowSendMessageToAllUsers || api_is_platform_admin()) { |
||
| 571 | $dql = "SELECT DISTINCT U |
||
| 572 | FROM ChamiloCoreBundle:User U |
||
| 573 | LEFT JOIN ChamiloCoreBundle:AccessUrlRelUser R |
||
| 574 | WITH U = R.user |
||
| 575 | WHERE |
||
| 576 | U.active = 1 AND |
||
| 577 | U.status != 6 AND |
||
| 578 | U.id != $currentUserId AND |
||
| 579 | R.url = $accessUrlId"; |
||
| 580 | } else { |
||
| 581 | $dql = 'SELECT DISTINCT U |
||
| 582 | FROM ChamiloCoreBundle:AccessUrlRelUser R, ChamiloCoreBundle:UserRelUser UF |
||
| 583 | INNER JOIN ChamiloCoreBundle:User AS U |
||
| 584 | WITH UF.friendUserId = U |
||
| 585 | WHERE |
||
| 586 | U.active = 1 AND |
||
| 587 | U.status != 6 AND |
||
| 588 | UF.relationType NOT IN('.USER_RELATION_TYPE_DELETED.', '.USER_RELATION_TYPE_RRHH.") AND |
||
| 589 | UF.userId = $currentUserId AND |
||
| 590 | UF.friendUserId != $currentUserId AND |
||
| 591 | U = R.user AND |
||
| 592 | R.url = $accessUrlId"; |
||
| 593 | } |
||
| 594 | } elseif ( |
||
| 595 | 'false' === api_get_setting('allow_social_tool') && |
||
| 596 | 'true' === api_get_setting('allow_message_tool') |
||
| 597 | ) { |
||
| 598 | if ('true' === $allowSendMessageToAllUsers) { |
||
| 599 | $dql = "SELECT DISTINCT U |
||
| 600 | FROM ChamiloCoreBundle:User U |
||
| 601 | LEFT JOIN ChamiloCoreBundle:AccessUrlRelUser R |
||
| 602 | WITH U = R.user |
||
| 603 | WHERE |
||
| 604 | U.active = 1 AND |
||
| 605 | U.status != 6 AND |
||
| 606 | U.id != $currentUserId AND |
||
| 607 | R.url = $accessUrlId"; |
||
| 608 | } else { |
||
| 609 | $time_limit = api_get_setting('time_limit_whosonline'); |
||
| 610 | $online_time = time() - $time_limit * 60; |
||
| 611 | $limit_date = api_get_utc_datetime($online_time); |
||
| 612 | $dql = "SELECT DISTINCT U |
||
| 613 | FROM ChamiloCoreBundle:User U |
||
| 614 | INNER JOIN ChamiloCoreBundle:TrackEOnline T |
||
| 615 | WITH U.id = T.loginUserId |
||
| 616 | WHERE |
||
| 617 | U.active = 1 AND |
||
| 618 | T.loginDate >= '".$limit_date."'"; |
||
| 619 | } |
||
| 620 | } |
||
| 621 | |||
| 622 | $parameters = []; |
||
| 623 | |||
| 624 | if (!empty($searchFilter)) { |
||
| 625 | $dql .= ' AND (U.firstname LIKE :search OR U.lastname LIKE :search OR U.email LIKE :search OR U.username LIKE :search)'; |
||
|
|
|||
| 626 | $parameters['search'] = "%$searchFilter%"; |
||
| 627 | } |
||
| 628 | |||
| 629 | return $this->getEntityManager() |
||
| 630 | ->createQuery($dql) |
||
| 631 | ->setMaxResults($limit) |
||
| 632 | ->setParameters($parameters) |
||
| 633 | ->getResult(); |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the list of HRM who have assigned this user. |
||
| 638 | * |
||
| 639 | * @param int $userId |
||
| 640 | * @param int $urlId |
||
| 641 | * |
||
| 642 | * @return array |
||
| 643 | */ |
||
| 644 | public function getAssignedHrmUserList($userId, $urlId) |
||
| 645 | { |
||
| 646 | $qb = $this->repository->createQueryBuilder('user'); |
||
| 647 | |||
| 648 | return $qb |
||
| 649 | ->select('uru') |
||
| 650 | ->innerJoin('ChamiloCoreBundle:UserRelUser', 'uru', Join::WITH, 'uru.user = user.id') |
||
| 651 | ->innerJoin('ChamiloCoreBundle:AccessUrlRelUser', 'auru', Join::WITH, 'auru.user = uru.friendUserId') |
||
| 652 | ->where( |
||
| 653 | $qb->expr()->eq('auru.url', $urlId) |
||
| 654 | ) |
||
| 655 | ->andWhere( |
||
| 656 | $qb->expr()->eq('uru.user', $userId) |
||
| 657 | ) |
||
| 658 | ->andWhere( |
||
| 659 | $qb->expr()->eq('uru.relationType', USER_RELATION_TYPE_RRHH) |
||
| 660 | ) |
||
| 661 | ->getQuery() |
||
| 662 | ->getResult(); |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Serialize the whole entity to an array. |
||
| 667 | * |
||
| 668 | * @param int $userId |
||
| 669 | * @param array $substitutionTerms Substitute terms for some elements |
||
| 670 | * |
||
| 671 | * @return string |
||
| 672 | */ |
||
| 673 | public function getPersonalDataToJson($userId, array $substitutionTerms) |
||
| 674 | { |
||
| 675 | $em = $this->getEntityManager(); |
||
| 676 | $dateFormat = \Datetime::ATOM; |
||
| 677 | |||
| 678 | /** @var User $user */ |
||
| 679 | $user = $this->find($userId); |
||
| 680 | |||
| 681 | $user->setPassword($substitutionTerms['password']); |
||
| 682 | $user->setSalt($substitutionTerms['salt']); |
||
| 683 | $noDataLabel = $substitutionTerms['empty']; |
||
| 684 | |||
| 685 | // Dummy content |
||
| 686 | $user->setDateOfBirth(null); |
||
| 687 | //$user->setBiography($noDataLabel); |
||
| 688 | /*$user->setFacebookData($noDataLabel); |
||
| 689 | $user->setFacebookName($noDataLabel); |
||
| 690 | $user->setFacebookUid($noDataLabel);*/ |
||
| 691 | //$user->setImageName($noDataLabel); |
||
| 692 | //$user->setTwoStepVerificationCode($noDataLabel); |
||
| 693 | //$user->setGender($noDataLabel); |
||
| 694 | /*$user->setGplusData($noDataLabel); |
||
| 695 | $user->setGplusName($noDataLabel); |
||
| 696 | $user->setGplusUid($noDataLabel);*/ |
||
| 697 | $user->setLocale($noDataLabel); |
||
| 698 | $user->setTimezone($noDataLabel); |
||
| 699 | /*$user->setTwitterData($noDataLabel); |
||
| 700 | $user->setTwitterName($noDataLabel); |
||
| 701 | $user->setTwitterUid($noDataLabel);*/ |
||
| 702 | $user->setWebsite($noDataLabel); |
||
| 703 | //$user->setToken($noDataLabel); |
||
| 704 | |||
| 705 | $courses = $user->getCourses(); |
||
| 706 | $list = []; |
||
| 707 | $chatFiles = []; |
||
| 708 | /** @var CourseRelUser $course */ |
||
| 709 | foreach ($courses as $course) { |
||
| 710 | $list[] = $course->getCourse()->getCode(); |
||
| 711 | } |
||
| 712 | |||
| 713 | $user->setCourses($list); |
||
| 714 | |||
| 715 | $classes = $user->getClasses(); |
||
| 716 | $list = []; |
||
| 717 | /** @var UsergroupRelUser $class */ |
||
| 718 | foreach ($classes as $class) { |
||
| 719 | $name = $class->getUsergroup()->getName(); |
||
| 720 | $list[$class->getUsergroup()->getGroupType()][] = $name.' - Status: '.$class->getRelationType(); |
||
| 721 | } |
||
| 722 | $user->setClasses($list); |
||
| 723 | |||
| 724 | $collection = $user->getSessionCourseSubscriptions(); |
||
| 725 | $list = []; |
||
| 726 | /** @var SessionRelCourseRelUser $item */ |
||
| 727 | foreach ($collection as $item) { |
||
| 728 | $list[$item->getSession()->getName()][] = $item->getCourse()->getCode(); |
||
| 729 | } |
||
| 730 | $user->setSessionCourseSubscriptions($list); |
||
| 731 | |||
| 732 | $documents = \DocumentManager::getAllDocumentsCreatedByUser($userId); |
||
| 733 | |||
| 734 | $friends = \SocialManager::get_friends($userId); |
||
| 735 | $friendList = []; |
||
| 736 | if (!empty($friends)) { |
||
| 737 | foreach ($friends as $friend) { |
||
| 738 | $friendList[] = $friend['user_info']['complete_name']; |
||
| 739 | } |
||
| 740 | } |
||
| 741 | |||
| 742 | $agenda = new \Agenda('personal'); |
||
| 743 | $events = $agenda->getEvents('', '', null, null, $userId, 'array'); |
||
| 744 | $eventList = []; |
||
| 745 | if (!empty($events)) { |
||
| 746 | foreach ($events as $event) { |
||
| 747 | $eventList[] = $event['title'].' '.$event['start_date_localtime'].' / '.$event['end_date_localtime']; |
||
| 748 | } |
||
| 749 | } |
||
| 750 | |||
| 751 | // GradebookCertificate |
||
| 752 | $criteria = [ |
||
| 753 | 'userId' => $userId, |
||
| 754 | ]; |
||
| 755 | $result = $em->getRepository(GradebookCertificate::class)->findBy($criteria); |
||
| 756 | $gradebookCertificate = []; |
||
| 757 | /** @var GradebookCertificate $item */ |
||
| 758 | foreach ($result as $item) { |
||
| 759 | $createdAt = $item->getCreatedAt() ? $item->getCreatedAt()->format($dateFormat) : ''; |
||
| 760 | $list = [ |
||
| 761 | 'Score: '.$item->getScoreCertificate(), |
||
| 762 | 'Path: '.$item->getPathCertificate(), |
||
| 763 | 'Created at: '.$createdAt, |
||
| 764 | ]; |
||
| 765 | $gradebookCertificate[] = implode(', ', $list); |
||
| 766 | } |
||
| 767 | |||
| 768 | // TrackEExercises |
||
| 769 | $criteria = [ |
||
| 770 | 'exeUserId' => $userId, |
||
| 771 | ]; |
||
| 772 | $result = $em->getRepository(TrackEExercises::class)->findBy($criteria); |
||
| 773 | $trackEExercises = []; |
||
| 774 | /** @var TrackEExercises $item */ |
||
| 775 | foreach ($result as $item) { |
||
| 776 | $date = $item->getExeDate() ? $item->getExeDate()->format($dateFormat) : ''; |
||
| 777 | $list = [ |
||
| 778 | 'IP: '.$item->getUserIp(), |
||
| 779 | 'Start: '.$date, |
||
| 780 | 'Status: '.$item->getStatus(), |
||
| 781 | // 'Result: '.$item->getExeResult(), |
||
| 782 | // 'Weighting: '.$item->getExeWeighting(), |
||
| 783 | ]; |
||
| 784 | $trackEExercises[] = implode(', ', $list); |
||
| 785 | } |
||
| 786 | |||
| 787 | // TrackEAttempt |
||
| 788 | $criteria = [ |
||
| 789 | 'userId' => $userId, |
||
| 790 | ]; |
||
| 791 | $result = $em->getRepository(TrackEAttempt::class)->findBy($criteria); |
||
| 792 | $trackEAttempt = []; |
||
| 793 | /** @var TrackEAttempt $item */ |
||
| 794 | foreach ($result as $item) { |
||
| 795 | $date = $item->getTms() ? $item->getTms()->format($dateFormat) : ''; |
||
| 796 | $list = [ |
||
| 797 | 'Attempt #'.$item->getExeId(), |
||
| 798 | 'Course # '.$item->getCourse()->getCode(), |
||
| 799 | //'Answer: '.$item->getAnswer(), |
||
| 800 | 'Session #'.$item->getSessionId(), |
||
| 801 | //'Marks: '.$item->getMarks(), |
||
| 802 | 'Position: '.$item->getPosition(), |
||
| 803 | 'Date: '.$date, |
||
| 804 | ]; |
||
| 805 | $trackEAttempt[] = implode(', ', $list); |
||
| 806 | } |
||
| 807 | |||
| 808 | // TrackECourseAccess |
||
| 809 | $criteria = [ |
||
| 810 | 'userId' => $userId, |
||
| 811 | ]; |
||
| 812 | $result = $em->getRepository(TrackECourseAccess::class)->findBy($criteria); |
||
| 813 | $trackECourseAccessList = []; |
||
| 814 | /** @var TrackECourseAccess $item */ |
||
| 815 | foreach ($result as $item) { |
||
| 816 | $startDate = $item->getLoginCourseDate() ? $item->getLoginCourseDate()->format($dateFormat) : ''; |
||
| 817 | $endDate = $item->getLogoutCourseDate() ? $item->getLogoutCourseDate()->format($dateFormat) : ''; |
||
| 818 | $list = [ |
||
| 819 | 'IP: '.$item->getUserIp(), |
||
| 820 | 'Start: '.$startDate, |
||
| 821 | 'End: '.$endDate, |
||
| 822 | ]; |
||
| 823 | $trackECourseAccessList[] = implode(', ', $list); |
||
| 824 | } |
||
| 825 | |||
| 826 | $checkEntities = [ |
||
| 827 | 'ChamiloCoreBundle:TrackELogin' => 'loginUserId', |
||
| 828 | 'ChamiloCoreBundle:TrackEAccess' => 'accessUserId', |
||
| 829 | 'ChamiloCoreBundle:TrackEOnline' => 'loginUserId', |
||
| 830 | 'ChamiloCoreBundle:TrackEDefault' => 'defaultUserId', |
||
| 831 | 'ChamiloCoreBundle:TrackELastaccess' => 'accessUserId', |
||
| 832 | 'ChamiloCoreBundle:TrackEUploads' => 'uploadUserId', |
||
| 833 | 'ChamiloCoreBundle:GradebookResult' => 'userId', |
||
| 834 | 'ChamiloCoreBundle:TrackEDownloads' => 'downUserId', |
||
| 835 | ]; |
||
| 836 | |||
| 837 | $maxResults = 1000; |
||
| 838 | $trackResults = []; |
||
| 839 | foreach ($checkEntities as $entity => $field) { |
||
| 840 | $qb = $em->createQueryBuilder(); |
||
| 841 | $qb->select($qb->expr()->count('l')) |
||
| 842 | ->from($entity, 'l') |
||
| 843 | ->where("l.$field = :login") |
||
| 844 | ->setParameter('login', $userId); |
||
| 845 | $query = $qb->getQuery(); |
||
| 846 | $count = $query->getSingleScalarResult(); |
||
| 847 | |||
| 848 | if ($count > $maxResults) { |
||
| 849 | $qb = $em->getRepository($entity)->createQueryBuilder('l'); |
||
| 850 | $qb |
||
| 851 | ->select('l') |
||
| 852 | ->where("l.$field = :login") |
||
| 853 | ->setParameter('login', $userId); |
||
| 854 | $qb |
||
| 855 | ->setFirstResult(0) |
||
| 856 | ->setMaxResults($maxResults) |
||
| 857 | ; |
||
| 858 | $result = $qb->getQuery()->getResult(); |
||
| 859 | } else { |
||
| 860 | $criteria = [ |
||
| 861 | $field => $userId, |
||
| 862 | ]; |
||
| 863 | $result = $em->getRepository($entity)->findBy($criteria); |
||
| 864 | } |
||
| 865 | $trackResults[$entity] = $result; |
||
| 866 | } |
||
| 867 | |||
| 868 | $trackELoginList = []; |
||
| 869 | /** @var TrackELogin $item */ |
||
| 870 | foreach ($trackResults['ChamiloCoreBundle:TrackELogin'] as $item) { |
||
| 871 | $startDate = $item->getLoginDate() ? $item->getLoginDate()->format($dateFormat) : ''; |
||
| 872 | $endDate = $item->getLogoutDate() ? $item->getLogoutDate()->format($dateFormat) : ''; |
||
| 873 | $list = [ |
||
| 874 | 'IP: '.$item->getUserIp(), |
||
| 875 | 'Start: '.$startDate, |
||
| 876 | 'End: '.$endDate, |
||
| 877 | ]; |
||
| 878 | $trackELoginList[] = implode(', ', $list); |
||
| 879 | } |
||
| 880 | |||
| 881 | // TrackEAccess |
||
| 882 | $trackEAccessList = []; |
||
| 883 | /** @var TrackEAccess $item */ |
||
| 884 | foreach ($trackResults['ChamiloCoreBundle:TrackEAccess'] as $item) { |
||
| 885 | $date = $item->getAccessDate() ? $item->getAccessDate()->format($dateFormat) : ''; |
||
| 886 | $list = [ |
||
| 887 | 'IP: '.$item->getUserIp(), |
||
| 888 | 'Tool: '.$item->getAccessTool(), |
||
| 889 | 'End: '.$date, |
||
| 890 | ]; |
||
| 891 | $trackEAccessList[] = implode(', ', $list); |
||
| 892 | } |
||
| 893 | |||
| 894 | // TrackEOnline |
||
| 895 | $trackEOnlineList = []; |
||
| 896 | /** @var TrackEOnline $item */ |
||
| 897 | foreach ($trackResults['ChamiloCoreBundle:TrackEOnline'] as $item) { |
||
| 898 | $date = $item->getLoginDate() ? $item->getLoginDate()->format($dateFormat) : ''; |
||
| 899 | $list = [ |
||
| 900 | 'IP: '.$item->getUserIp(), |
||
| 901 | 'Login date: '.$date, |
||
| 902 | 'Course # '.$item->getCId(), |
||
| 903 | 'Session # '.$item->getSessionId(), |
||
| 904 | ]; |
||
| 905 | $trackEOnlineList[] = implode(', ', $list); |
||
| 906 | } |
||
| 907 | |||
| 908 | // TrackEDefault |
||
| 909 | $trackEDefault = []; |
||
| 910 | /** @var TrackEDefault $item */ |
||
| 911 | foreach ($trackResults['ChamiloCoreBundle:TrackEDefault'] as $item) { |
||
| 912 | $date = $item->getDefaultDate() ? $item->getDefaultDate()->format($dateFormat) : ''; |
||
| 913 | $list = [ |
||
| 914 | 'Type: '.$item->getDefaultEventType(), |
||
| 915 | 'Value: '.$item->getDefaultValue(), |
||
| 916 | 'Value type: '.$item->getDefaultValueType(), |
||
| 917 | 'Date: '.$date, |
||
| 918 | 'Course #'.$item->getCId(), |
||
| 919 | 'Session # '.$item->getSessionId(), |
||
| 920 | ]; |
||
| 921 | $trackEDefault[] = implode(', ', $list); |
||
| 922 | } |
||
| 923 | |||
| 924 | // TrackELastaccess |
||
| 925 | $trackELastaccess = []; |
||
| 926 | /** @var TrackELastaccess $item */ |
||
| 927 | foreach ($trackResults['ChamiloCoreBundle:TrackELastaccess'] as $item) { |
||
| 928 | $date = $item->getAccessDate() ? $item->getAccessDate()->format($dateFormat) : ''; |
||
| 929 | $list = [ |
||
| 930 | 'Course #'.$item->getCId(), |
||
| 931 | 'Session # '.$item->getAccessSessionId(), |
||
| 932 | 'Tool: '.$item->getAccessTool(), |
||
| 933 | 'Access date: '.$date, |
||
| 934 | ]; |
||
| 935 | $trackELastaccess[] = implode(', ', $list); |
||
| 936 | } |
||
| 937 | |||
| 938 | // TrackEUploads |
||
| 939 | $trackEUploads = []; |
||
| 940 | /** @var TrackEUploads $item */ |
||
| 941 | foreach ($trackResults['ChamiloCoreBundle:TrackEUploads'] as $item) { |
||
| 942 | $date = $item->getUploadDate() ? $item->getUploadDate()->format($dateFormat) : ''; |
||
| 943 | $list = [ |
||
| 944 | 'Course #'.$item->getCId(), |
||
| 945 | 'Uploaded at: '.$date, |
||
| 946 | 'Upload id # '.$item->getUploadId(), |
||
| 947 | ]; |
||
| 948 | $trackEUploads[] = implode(', ', $list); |
||
| 949 | } |
||
| 950 | |||
| 951 | $gradebookResult = []; |
||
| 952 | /** @var GradebookResult $item */ |
||
| 953 | foreach ($trackResults['ChamiloCoreBundle:GradebookResult'] as $item) { |
||
| 954 | $date = $item->getCreatedAt() ? $item->getCreatedAt()->format($dateFormat) : ''; |
||
| 955 | $list = [ |
||
| 956 | 'Evaluation id# '.$item->getEvaluationId(), |
||
| 957 | //'Score: '.$item->getScore(), |
||
| 958 | 'Creation date: '.$date, |
||
| 959 | ]; |
||
| 960 | $gradebookResult[] = implode(', ', $list); |
||
| 961 | } |
||
| 962 | |||
| 963 | $trackEDownloads = []; |
||
| 964 | /** @var TrackEDownloads $item */ |
||
| 965 | foreach ($trackResults['ChamiloCoreBundle:TrackEDownloads'] as $item) { |
||
| 966 | $date = $item->getDownDate() ? $item->getDownDate()->format($dateFormat) : ''; |
||
| 967 | $list = [ |
||
| 968 | 'File: '.$item->getDownDocPath(), |
||
| 969 | 'Download at: '.$date, |
||
| 970 | ]; |
||
| 971 | $trackEDownloads[] = implode(', ', $list); |
||
| 972 | } |
||
| 973 | |||
| 974 | // UserCourseCategory |
||
| 975 | $criteria = [ |
||
| 976 | 'userId' => $userId, |
||
| 977 | ]; |
||
| 978 | $result = $em->getRepository(UserCourseCategory::class)->findBy($criteria); |
||
| 979 | $userCourseCategory = []; |
||
| 980 | /** @var UserCourseCategory $item */ |
||
| 981 | foreach ($result as $item) { |
||
| 982 | $list = [ |
||
| 983 | 'Title: '.$item->getTitle(), |
||
| 984 | ]; |
||
| 985 | $userCourseCategory[] = implode(', ', $list); |
||
| 986 | } |
||
| 987 | |||
| 988 | // Forum |
||
| 989 | $criteria = [ |
||
| 990 | 'posterId' => $userId, |
||
| 991 | ]; |
||
| 992 | $result = $em->getRepository(CForumPost::class)->findBy($criteria); |
||
| 993 | $cForumPostList = []; |
||
| 994 | /** @var CForumPost $item */ |
||
| 995 | foreach ($result as $item) { |
||
| 996 | $date = $item->getPostDate() ? $item->getPostDate()->format($dateFormat) : ''; |
||
| 997 | $list = [ |
||
| 998 | 'Title: '.$item->getPostTitle(), |
||
| 999 | 'Creation date: '.$date, |
||
| 1000 | ]; |
||
| 1001 | $cForumPostList[] = implode(', ', $list); |
||
| 1002 | } |
||
| 1003 | |||
| 1004 | // CForumThread |
||
| 1005 | $criteria = [ |
||
| 1006 | 'threadPosterId' => $userId, |
||
| 1007 | ]; |
||
| 1008 | $result = $em->getRepository(CForumThread::class)->findBy($criteria); |
||
| 1009 | $cForumThreadList = []; |
||
| 1010 | /** @var CForumThread $item */ |
||
| 1011 | foreach ($result as $item) { |
||
| 1012 | $date = $item->getThreadDate() ? $item->getThreadDate()->format($dateFormat) : ''; |
||
| 1013 | $list = [ |
||
| 1014 | 'Title: '.$item->getThreadTitle(), |
||
| 1015 | 'Creation date: '.$date, |
||
| 1016 | ]; |
||
| 1017 | $cForumThreadList[] = implode(', ', $list); |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | // CForumAttachment |
||
| 1021 | /*$criteria = [ |
||
| 1022 | 'threadPosterId' => $userId, |
||
| 1023 | ]; |
||
| 1024 | $result = $em->getRepository('ChamiloCourseBundle:CForumAttachment')->findBy($criteria); |
||
| 1025 | $cForumThreadList = []; |
||
| 1026 | * @var CForumThread $item |
||
| 1027 | foreach ($result as $item) { |
||
| 1028 | $list = [ |
||
| 1029 | 'Title: '.$item->getThreadTitle(), |
||
| 1030 | 'Creation date: '.$item->getThreadDate()->format($dateFormat), |
||
| 1031 | ]; |
||
| 1032 | $cForumThreadList[] = implode(', ', $list); |
||
| 1033 | }*/ |
||
| 1034 | |||
| 1035 | // cGroupRelUser |
||
| 1036 | $criteria = [ |
||
| 1037 | 'user' => $userId, |
||
| 1038 | ]; |
||
| 1039 | $result = $em->getRepository(CGroupRelUser::class)->findBy($criteria); |
||
| 1040 | $cGroupRelUser = []; |
||
| 1041 | /** @var CGroupRelUser $item */ |
||
| 1042 | foreach ($result as $item) { |
||
| 1043 | $list = [ |
||
| 1044 | 'Course # '.$item->getCId(), |
||
| 1045 | 'Group #'.$item->getGroup()->getIid(), |
||
| 1046 | 'Role: '.$item->getStatus(), |
||
| 1047 | ]; |
||
| 1048 | $cGroupRelUser[] = implode(', ', $list); |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | // CAttendanceSheet |
||
| 1052 | $criteria = [ |
||
| 1053 | 'userId' => $userId, |
||
| 1054 | ]; |
||
| 1055 | $result = $em->getRepository(CAttendanceSheet::class)->findBy($criteria); |
||
| 1056 | $cAttendanceSheetList = []; |
||
| 1057 | /** @var CAttendanceSheet $item */ |
||
| 1058 | foreach ($result as $item) { |
||
| 1059 | $list = [ |
||
| 1060 | 'Presence: '.$item->getPresence(), |
||
| 1061 | 'Calendar id: '.$item->getAttendanceCalendarId(), |
||
| 1062 | ]; |
||
| 1063 | $cAttendanceSheetList[] = implode(', ', $list); |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | // CBlogPost |
||
| 1067 | $criteria = [ |
||
| 1068 | 'authorId' => $userId, |
||
| 1069 | ]; |
||
| 1070 | $result = $em->getRepository(CBlogPost::class)->findBy($criteria); |
||
| 1071 | $cBlog = []; |
||
| 1072 | /** @var CBlogPost $item */ |
||
| 1073 | foreach ($result as $item) { |
||
| 1074 | $date = $item->getDateCreation() ? $item->getDateCreation()->format($dateFormat) : ''; |
||
| 1075 | $list = [ |
||
| 1076 | 'Title: '.$item->getTitle(), |
||
| 1077 | 'Date: '.$date, |
||
| 1078 | ]; |
||
| 1079 | $cBlog[] = implode(', ', $list); |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | // CAttendanceResult |
||
| 1083 | $criteria = [ |
||
| 1084 | 'userId' => $userId, |
||
| 1085 | ]; |
||
| 1086 | $result = $em->getRepository(CAttendanceResult::class)->findBy($criteria); |
||
| 1087 | $cAttendanceResult = []; |
||
| 1088 | /** @var CAttendanceResult $item */ |
||
| 1089 | foreach ($result as $item) { |
||
| 1090 | $list = [ |
||
| 1091 | 'Score : '.$item->getScore(), |
||
| 1092 | 'Calendar id: '.$item->getAttendanceId(), |
||
| 1093 | ]; |
||
| 1094 | $cAttendanceResult[] = implode(', ', $list); |
||
| 1095 | } |
||
| 1096 | |||
| 1097 | // Message |
||
| 1098 | $criteria = [ |
||
| 1099 | 'userSender' => $userId, |
||
| 1100 | ]; |
||
| 1101 | $result = $em->getRepository(Message::class)->findBy($criteria); |
||
| 1102 | $messageList = []; |
||
| 1103 | /** @var Message $item */ |
||
| 1104 | foreach ($result as $item) { |
||
| 1105 | $date = $item->getSendDate() ? $item->getSendDate()->format($dateFormat) : ''; |
||
| 1106 | $userName = ''; |
||
| 1107 | if ($item->getUserReceiver()) { |
||
| 1108 | $userName = $item->getUserReceiver()->getUsername(); |
||
| 1109 | } |
||
| 1110 | $list = [ |
||
| 1111 | 'Title: '.$item->getTitle(), |
||
| 1112 | 'Sent date: '.$date, |
||
| 1113 | 'To user: '.$userName, |
||
| 1114 | 'Status'.$item->getMsgStatus(), |
||
| 1115 | ]; |
||
| 1116 | $messageList[] = implode(', ', $list); |
||
| 1117 | } |
||
| 1118 | |||
| 1119 | // CSurveyAnswer |
||
| 1120 | $criteria = [ |
||
| 1121 | 'user' => $userId, |
||
| 1122 | ]; |
||
| 1123 | $result = $em->getRepository(CSurveyAnswer::class)->findBy($criteria); |
||
| 1124 | $cSurveyAnswer = []; |
||
| 1125 | /** @var CSurveyAnswer $item */ |
||
| 1126 | foreach ($result as $item) { |
||
| 1127 | $list = [ |
||
| 1128 | 'Answer # '.$item->getAnswerId(), |
||
| 1129 | 'Value: '.$item->getValue(), |
||
| 1130 | ]; |
||
| 1131 | $cSurveyAnswer[] = implode(', ', $list); |
||
| 1132 | } |
||
| 1133 | |||
| 1134 | // CDropboxFile |
||
| 1135 | $criteria = [ |
||
| 1136 | 'uploaderId' => $userId, |
||
| 1137 | ]; |
||
| 1138 | $result = $em->getRepository(CDropboxFile::class)->findBy($criteria); |
||
| 1139 | $cDropboxFile = []; |
||
| 1140 | /** @var CDropboxFile $item */ |
||
| 1141 | foreach ($result as $item) { |
||
| 1142 | $date = $item->getUploadDate() ? $item->getUploadDate()->format($dateFormat) : ''; |
||
| 1143 | $list = [ |
||
| 1144 | 'Title: '.$item->getTitle(), |
||
| 1145 | 'Uploaded date: '.$date, |
||
| 1146 | 'File: '.$item->getFilename(), |
||
| 1147 | ]; |
||
| 1148 | $cDropboxFile[] = implode(', ', $list); |
||
| 1149 | } |
||
| 1150 | |||
| 1151 | // CDropboxPerson |
||
| 1152 | $criteria = [ |
||
| 1153 | 'userId' => $userId, |
||
| 1154 | ]; |
||
| 1155 | $result = $em->getRepository(CDropboxPerson::class)->findBy($criteria); |
||
| 1156 | $cDropboxPerson = []; |
||
| 1157 | /** @var CDropboxPerson $item */ |
||
| 1158 | foreach ($result as $item) { |
||
| 1159 | $list = [ |
||
| 1160 | 'File #'.$item->getFileId(), |
||
| 1161 | 'Course #'.$item->getCId(), |
||
| 1162 | ]; |
||
| 1163 | $cDropboxPerson[] = implode(', ', $list); |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | // CDropboxPerson |
||
| 1167 | $criteria = [ |
||
| 1168 | 'authorUserId' => $userId, |
||
| 1169 | ]; |
||
| 1170 | $result = $em->getRepository(CDropboxFeedback::class)->findBy($criteria); |
||
| 1171 | $cDropboxFeedback = []; |
||
| 1172 | /** @var CDropboxFeedback $item */ |
||
| 1173 | foreach ($result as $item) { |
||
| 1174 | $date = $item->getFeedbackDate() ? $item->getFeedbackDate()->format($dateFormat) : ''; |
||
| 1175 | $list = [ |
||
| 1176 | 'File #'.$item->getFileId(), |
||
| 1177 | 'Feedback: '.$item->getFeedback(), |
||
| 1178 | 'Date: '.$date, |
||
| 1179 | ]; |
||
| 1180 | $cDropboxFeedback[] = implode(', ', $list); |
||
| 1181 | } |
||
| 1182 | |||
| 1183 | // CNotebook |
||
| 1184 | $criteria = [ |
||
| 1185 | 'userId' => $userId, |
||
| 1186 | ]; |
||
| 1187 | $result = $em->getRepository(CNotebook::class)->findBy($criteria); |
||
| 1188 | $cNotebook = []; |
||
| 1189 | /** @var CNotebook $item */ |
||
| 1190 | foreach ($result as $item) { |
||
| 1191 | $date = $item->getUpdateDate() ? $item->getUpdateDate()->format($dateFormat) : ''; |
||
| 1192 | $list = [ |
||
| 1193 | 'Title: '.$item->getTitle(), |
||
| 1194 | 'Date: '.$date, |
||
| 1195 | ]; |
||
| 1196 | $cNotebook[] = implode(', ', $list); |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | // CLpView |
||
| 1200 | $criteria = [ |
||
| 1201 | 'userId' => $userId, |
||
| 1202 | ]; |
||
| 1203 | $result = $em->getRepository(CLpView::class)->findBy($criteria); |
||
| 1204 | $cLpView = []; |
||
| 1205 | /** @var CLpView $item */ |
||
| 1206 | foreach ($result as $item) { |
||
| 1207 | $list = [ |
||
| 1208 | //'Id #'.$item->getId(), |
||
| 1209 | 'LP #'.$item->getLpId(), |
||
| 1210 | 'Progress: '.$item->getProgress(), |
||
| 1211 | 'Course #'.$item->getCId(), |
||
| 1212 | 'Session #'.$item->getSessionId(), |
||
| 1213 | ]; |
||
| 1214 | $cLpView[] = implode(', ', $list); |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | // CStudentPublication |
||
| 1218 | $criteria = [ |
||
| 1219 | 'userId' => $userId, |
||
| 1220 | ]; |
||
| 1221 | $result = $em->getRepository(CStudentPublication::class)->findBy($criteria); |
||
| 1222 | $cStudentPublication = []; |
||
| 1223 | /** @var CStudentPublication $item */ |
||
| 1224 | foreach ($result as $item) { |
||
| 1225 | $list = [ |
||
| 1226 | 'Title: '.$item->getTitle(), |
||
| 1227 | 'URL: '.$item->getUrl(), |
||
| 1228 | ]; |
||
| 1229 | $cStudentPublication[] = implode(', ', $list); |
||
| 1230 | } |
||
| 1231 | |||
| 1232 | // CStudentPublicationComment |
||
| 1233 | $criteria = [ |
||
| 1234 | 'userId' => $userId, |
||
| 1235 | ]; |
||
| 1236 | $result = $em->getRepository(CStudentPublicationComment::class)->findBy($criteria); |
||
| 1237 | $cStudentPublicationComment = []; |
||
| 1238 | /** @var CStudentPublicationComment $item */ |
||
| 1239 | foreach ($result as $item) { |
||
| 1240 | $date = $item->getSentAt() ? $item->getSentAt()->format($dateFormat) : ''; |
||
| 1241 | $list = [ |
||
| 1242 | 'Commment: '.$item->getComment(), |
||
| 1243 | 'File '.$item->getFile(), |
||
| 1244 | 'Course # '.$item->getCId(), |
||
| 1245 | 'Date: '.$date, |
||
| 1246 | ]; |
||
| 1247 | $cStudentPublicationComment[] = implode(', ', $list); |
||
| 1248 | } |
||
| 1249 | |||
| 1250 | // CWiki |
||
| 1251 | $criteria = [ |
||
| 1252 | 'userId' => $userId, |
||
| 1253 | ]; |
||
| 1254 | $result = $em->getRepository(CWiki::class)->findBy($criteria); |
||
| 1255 | $cWiki = []; |
||
| 1256 | /** @var CWiki $item */ |
||
| 1257 | foreach ($result as $item) { |
||
| 1258 | $list = [ |
||
| 1259 | 'Title: '.$item->getTitle(), |
||
| 1260 | 'Progress: '.$item->getProgress(), |
||
| 1261 | 'IP: '.$item->getUserIp(), |
||
| 1262 | ]; |
||
| 1263 | $cWiki[] = implode(', ', $list); |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | // Ticket |
||
| 1267 | $criteria = [ |
||
| 1268 | 'insertUserId' => $userId, |
||
| 1269 | ]; |
||
| 1270 | $result = $em->getRepository(Ticket::class)->findBy($criteria); |
||
| 1271 | $ticket = []; |
||
| 1272 | /** @var Ticket $item */ |
||
| 1273 | foreach ($result as $item) { |
||
| 1274 | $list = [ |
||
| 1275 | 'Code: '.$item->getCode(), |
||
| 1276 | 'Subject: '.$item->getSubject(), |
||
| 1277 | ]; |
||
| 1278 | $ticket[] = implode(', ', $list); |
||
| 1279 | } |
||
| 1280 | |||
| 1281 | // Message |
||
| 1282 | $criteria = [ |
||
| 1283 | 'insertUserId' => $userId, |
||
| 1284 | ]; |
||
| 1285 | $result = $em->getRepository(TicketMessage::class)->findBy($criteria); |
||
| 1286 | $ticketMessage = []; |
||
| 1287 | /** @var \Chamilo\CoreBundle\Entity\TicketMessage $item */ |
||
| 1288 | foreach ($result as $item) { |
||
| 1289 | $date = $item->getInsertDateTime() ? $item->getInsertDateTime()->format($dateFormat) : ''; |
||
| 1290 | $list = [ |
||
| 1291 | 'Subject: '.$item->getSubject(), |
||
| 1292 | 'IP: '.$item->getIpAddress(), |
||
| 1293 | 'Status: '.$item->getStatus(), |
||
| 1294 | 'Creation date: '.$date, |
||
| 1295 | ]; |
||
| 1296 | $ticketMessage[] = implode(', ', $list); |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | // SkillRelUserComment |
||
| 1300 | $criteria = [ |
||
| 1301 | 'feedbackGiver' => $userId, |
||
| 1302 | ]; |
||
| 1303 | $result = $em->getRepository(SkillRelUserComment::class)->findBy($criteria); |
||
| 1304 | $skillRelUserComment = []; |
||
| 1305 | /** @var SkillRelUserComment $item */ |
||
| 1306 | foreach ($result as $item) { |
||
| 1307 | $date = $item->getFeedbackDateTime() ? $item->getFeedbackDateTime()->format($dateFormat) : ''; |
||
| 1308 | $list = [ |
||
| 1309 | 'Feedback: '.$item->getFeedbackText(), |
||
| 1310 | 'Value: '.$item->getFeedbackValue(), |
||
| 1311 | 'Created at: '.$date, |
||
| 1312 | ]; |
||
| 1313 | $skillRelUserComment[] = implode(', ', $list); |
||
| 1314 | } |
||
| 1315 | |||
| 1316 | // UserRelCourseVote |
||
| 1317 | $criteria = [ |
||
| 1318 | 'userId' => $userId, |
||
| 1319 | ]; |
||
| 1320 | $result = $em->getRepository(UserRelCourseVote::class)->findBy($criteria); |
||
| 1321 | $userRelCourseVote = []; |
||
| 1322 | /** @var UserRelCourseVote $item */ |
||
| 1323 | foreach ($result as $item) { |
||
| 1324 | $list = [ |
||
| 1325 | 'Course #'.$item->getCId(), |
||
| 1326 | 'Session #'.$item->getSessionId(), |
||
| 1327 | 'Vote: '.$item->getVote(), |
||
| 1328 | ]; |
||
| 1329 | $userRelCourseVote[] = implode(', ', $list); |
||
| 1330 | } |
||
| 1331 | |||
| 1332 | $user->setDropBoxSentFiles( |
||
| 1333 | [ |
||
| 1334 | 'Friends' => $friendList, |
||
| 1335 | 'Events' => $eventList, |
||
| 1336 | 'GradebookCertificate' => $gradebookCertificate, |
||
| 1337 | |||
| 1338 | 'TrackECourseAccess' => $trackECourseAccessList, |
||
| 1339 | 'TrackELogin' => $trackELoginList, |
||
| 1340 | 'TrackEAccess' => $trackEAccessList, |
||
| 1341 | 'TrackEDefault' => $trackEDefault, |
||
| 1342 | 'TrackEOnline' => $trackEOnlineList, |
||
| 1343 | 'TrackEUploads' => $trackEUploads, |
||
| 1344 | 'TrackELastaccess' => $trackELastaccess, |
||
| 1345 | 'GradebookResult' => $gradebookResult, |
||
| 1346 | 'Downloads' => $trackEDownloads, |
||
| 1347 | 'UserCourseCategory' => $userCourseCategory, |
||
| 1348 | 'SkillRelUserComment' => $skillRelUserComment, |
||
| 1349 | 'UserRelCourseVote' => $userRelCourseVote, |
||
| 1350 | |||
| 1351 | // courses |
||
| 1352 | 'AttendanceResult' => $cAttendanceResult, |
||
| 1353 | 'Blog' => $cBlog, |
||
| 1354 | 'DocumentsAdded' => $documents, |
||
| 1355 | 'Chat' => $chatFiles, |
||
| 1356 | 'ForumPost' => $cForumPostList, |
||
| 1357 | 'ForumThread' => $cForumThreadList, |
||
| 1358 | 'TrackEExercises' => $trackEExercises, |
||
| 1359 | 'TrackEAttempt' => $trackEAttempt, |
||
| 1360 | |||
| 1361 | 'GroupRelUser' => $cGroupRelUser, |
||
| 1362 | 'Message' => $messageList, |
||
| 1363 | 'Survey' => $cSurveyAnswer, |
||
| 1364 | 'StudentPublication' => $cStudentPublication, |
||
| 1365 | 'StudentPublicationComment' => $cStudentPublicationComment, |
||
| 1366 | 'DropboxFile' => $cDropboxFile, |
||
| 1367 | 'DropboxPerson' => $cDropboxPerson, |
||
| 1368 | 'DropboxFeedback' => $cDropboxFeedback, |
||
| 1369 | |||
| 1370 | 'LpView' => $cLpView, |
||
| 1371 | 'Notebook' => $cNotebook, |
||
| 1372 | |||
| 1373 | 'Wiki' => $cWiki, |
||
| 1374 | // Tickets |
||
| 1375 | |||
| 1376 | 'Ticket' => $ticket, |
||
| 1377 | 'TicketMessage' => $ticketMessage, |
||
| 1378 | ] |
||
| 1379 | ); |
||
| 1380 | |||
| 1381 | $user->setDropBoxReceivedFiles([]); |
||
| 1382 | //$user->setGroups([]); |
||
| 1383 | $user->setCurriculumItems([]); |
||
| 1384 | |||
| 1385 | $portals = $user->getPortals(); |
||
| 1386 | if (!empty($portals)) { |
||
| 1387 | $list = []; |
||
| 1388 | /** @var AccessUrlRelUser $portal */ |
||
| 1389 | foreach ($portals as $portal) { |
||
| 1390 | $portalInfo = \UrlManager::get_url_data_from_id($portal->getUrl()->getId()); |
||
| 1391 | $list[] = $portalInfo['url']; |
||
| 1392 | } |
||
| 1393 | } |
||
| 1394 | $user->setPortals($list); |
||
| 1395 | |||
| 1396 | $coachList = $user->getSessionAsGeneralCoach(); |
||
| 1397 | $list = []; |
||
| 1398 | /** @var Session $session */ |
||
| 1399 | foreach ($coachList as $session) { |
||
| 1400 | $list[] = $session->getName(); |
||
| 1401 | } |
||
| 1402 | $user->setSessionAsGeneralCoach($list); |
||
| 1403 | |||
| 1404 | $skillRelUserList = $user->getAchievedSkills(); |
||
| 1405 | $list = []; |
||
| 1406 | /** @var SkillRelUser $skillRelUser */ |
||
| 1407 | foreach ($skillRelUserList as $skillRelUser) { |
||
| 1408 | $list[] = $skillRelUser->getSkill()->getName(); |
||
| 1409 | } |
||
| 1410 | $user->setAchievedSkills($list); |
||
| 1411 | $user->setCommentedUserSkills([]); |
||
| 1412 | |||
| 1413 | //$extraFieldValues = new \ExtraFieldValue('user'); |
||
| 1414 | |||
| 1415 | $lastLogin = $user->getLastLogin(); |
||
| 1416 | if (empty($lastLogin)) { |
||
| 1417 | $login = $this->getLastLogin($user); |
||
| 1418 | if ($login) { |
||
| 1419 | $lastLogin = $login->getLoginDate(); |
||
| 1420 | } |
||
| 1421 | } |
||
| 1422 | $user->setLastLogin($lastLogin); |
||
| 1423 | |||
| 1424 | /*$dateNormalizer = new GetSetMethodNormalizer(); |
||
| 1425 | $dateNormalizer->setCircularReferenceHandler(function ($object) { |
||
| 1426 | return get_class($object); |
||
| 1427 | });*/ |
||
| 1428 | |||
| 1429 | $ignore = [ |
||
| 1430 | 'twoStepVerificationCode', |
||
| 1431 | 'biography', |
||
| 1432 | 'dateOfBirth', |
||
| 1433 | 'gender', |
||
| 1434 | 'facebookData', |
||
| 1435 | 'facebookName', |
||
| 1436 | 'facebookUid', |
||
| 1437 | 'gplusData', |
||
| 1438 | 'gplusName', |
||
| 1439 | 'gplusUid', |
||
| 1440 | 'locale', |
||
| 1441 | 'timezone', |
||
| 1442 | 'twitterData', |
||
| 1443 | 'twitterName', |
||
| 1444 | 'twitterUid', |
||
| 1445 | 'gplusUid', |
||
| 1446 | 'token', |
||
| 1447 | 'website', |
||
| 1448 | 'plainPassword', |
||
| 1449 | 'completeNameWithUsername', |
||
| 1450 | 'completeName', |
||
| 1451 | 'completeNameWithClasses', |
||
| 1452 | 'salt', |
||
| 1453 | ]; |
||
| 1454 | |||
| 1455 | $callback = function ($dateTime) { |
||
| 1456 | return $dateTime instanceof \DateTime ? $dateTime->format(\DateTime::ATOM) : ''; |
||
| 1457 | }; |
||
| 1458 | |||
| 1459 | $defaultContext = [ |
||
| 1460 | AbstractNormalizer::CALLBACKS => [ |
||
| 1461 | 'createdAt' => $callback, |
||
| 1462 | 'lastLogin' => $callback, |
||
| 1463 | 'registrationDate' => $callback, |
||
| 1464 | 'memberSince' => $callback, |
||
| 1465 | ], |
||
| 1466 | AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) { |
||
| 1467 | return get_class($object); |
||
| 1468 | }, |
||
| 1469 | ]; |
||
| 1470 | |||
| 1471 | $normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext); |
||
| 1472 | $serializer = new Serializer( |
||
| 1473 | [$normalizer], |
||
| 1474 | [new JsonEncoder()], |
||
| 1475 | [AbstractNormalizer::IGNORED_ATTRIBUTES => $ignore] |
||
| 1476 | ); |
||
| 1477 | |||
| 1478 | return $serializer->serialize($user, 'json'); |
||
| 1479 | } |
||
| 1480 | |||
| 1481 | /** |
||
| 1482 | * Get the last login from the track_e_login table. |
||
| 1483 | * This might be different from user.last_login in the case of legacy users |
||
| 1484 | * as user.last_login was only implemented in 1.10 version with a default |
||
| 1485 | * value of NULL (not the last record from track_e_login). |
||
| 1486 | * |
||
| 1487 | * @throws \Exception |
||
| 1488 | * |
||
| 1489 | * @return TrackELogin|null |
||
| 1490 | */ |
||
| 1491 | public function getLastLogin(User $user) |
||
| 1505 | } |
||
| 1506 | } |
||
| 1507 |