| Total Complexity | 369 |
| Total Lines | 2789 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UserGroup 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 UserGroup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class UserGroup extends Model |
||
| 13 | { |
||
| 14 | const SOCIAL_CLASS = 1; |
||
| 15 | const NORMAL_CLASS = 0; |
||
| 16 | public $columns = [ |
||
| 17 | 'id', |
||
| 18 | 'name', |
||
| 19 | 'description', |
||
| 20 | 'group_type', |
||
| 21 | 'picture', |
||
| 22 | 'url', |
||
| 23 | 'allow_members_leave_group', |
||
| 24 | 'visibility', |
||
| 25 | 'updated_at', |
||
| 26 | 'created_at', |
||
| 27 | ]; |
||
| 28 | |||
| 29 | public $useMultipleUrl = false; |
||
| 30 | public $groupType = 0; |
||
| 31 | public $showGroupTypeSetting = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Set ups DB tables. |
||
| 35 | */ |
||
| 36 | public function __construct() |
||
| 37 | { |
||
| 38 | $this->table = Database::get_main_table(TABLE_USERGROUP); |
||
| 39 | $this->usergroup_rel_user_table = Database::get_main_table(TABLE_USERGROUP_REL_USER); |
||
| 40 | $this->usergroup_rel_course_table = Database::get_main_table(TABLE_USERGROUP_REL_COURSE); |
||
| 41 | $this->usergroup_rel_session_table = Database::get_main_table(TABLE_USERGROUP_REL_SESSION); |
||
| 42 | $this->access_url_rel_usergroup = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP); |
||
| 43 | $this->table_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 44 | $this->table_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 45 | $this->useMultipleUrl = api_get_configuration_value('multiple_access_urls'); |
||
| 46 | if ($this->allowTeachers()) { |
||
| 47 | $this->columns[] = 'author_id'; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | public function getUseMultipleUrl() |
||
| 55 | { |
||
| 56 | return $this->useMultipleUrl; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return int |
||
| 61 | */ |
||
| 62 | public function getTotalCount() |
||
| 63 | { |
||
| 64 | $row = Database::select('count(*) as count', $this->table, [], 'first'); |
||
| 65 | |||
| 66 | return $row['count']; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param int $id |
||
| 71 | * @param bool $getCount |
||
| 72 | * |
||
| 73 | * @return array|int |
||
| 74 | */ |
||
| 75 | public function getUserGroupUsers($id, $getCount = false) |
||
| 76 | { |
||
| 77 | $id = (int) $id; |
||
| 78 | |||
| 79 | if ($getCount) { |
||
| 80 | $select = 'COUNT(u.id) count '; |
||
| 81 | } else { |
||
| 82 | $select = ' u.* '; |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($this->useMultipleUrl) { |
||
| 86 | $urlId = api_get_current_access_url_id(); |
||
| 87 | $sql = "SELECT $select |
||
| 88 | FROM ".$this->usergroup_rel_user_table." u |
||
| 89 | INNER JOIN ".$this->access_url_rel_usergroup." a |
||
| 90 | ON (u.user_id = a.user_id) |
||
| 91 | WHERE usergroup_id = $id AND access_url_id = $urlId "; |
||
| 92 | } else { |
||
| 93 | $sql = "SELECT $select |
||
| 94 | FROM ".$this->usergroup_rel_user_table." u |
||
| 95 | WHERE usergroup_id = $id"; |
||
| 96 | } |
||
| 97 | $result = Database::query($sql); |
||
| 98 | if ($getCount) { |
||
| 99 | if (Database::num_rows($result)) { |
||
| 100 | $row = Database::fetch_array($result); |
||
| 101 | |||
| 102 | return $row['count']; |
||
| 103 | } |
||
| 104 | |||
| 105 | return 0; |
||
| 106 | } else { |
||
| 107 | $list = []; |
||
| 108 | $showCalendar = api_get_plugin_setting('learning_calendar', 'enabled') === 'true'; |
||
| 109 | $calendarPlugin = null; |
||
| 110 | if ($showCalendar) { |
||
| 111 | $calendarPlugin = LearningCalendarPlugin::create(); |
||
| 112 | } |
||
| 113 | $url = api_get_path(WEB_PLUGIN_PATH).'learning_calendar/calendar.php?'; |
||
| 114 | while ($data = Database::fetch_array($result)) { |
||
| 115 | $userId = $data['user_id']; |
||
| 116 | $userInfo = api_get_user_info($userId); |
||
| 117 | $data['name'] = $userInfo['complete_name_with_username']; |
||
| 118 | |||
| 119 | if ($showCalendar) { |
||
| 120 | $calendar = $calendarPlugin->getUserCalendar($userId); |
||
| 121 | $data['calendar_id'] = 0; |
||
| 122 | $data['calendar'] = ''; |
||
| 123 | if (!empty($calendar)) { |
||
| 124 | $calendarInfo = $calendarPlugin->getCalendar($calendar['calendar_id']); |
||
| 125 | if ($calendarInfo) { |
||
| 126 | $data['calendar_id'] = $calendar['calendar_id']; |
||
| 127 | $data['calendar'] = Display::url( |
||
| 128 | $calendarInfo['title'], |
||
| 129 | $url.'&id='.$calendar['calendar_id'] |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | $courseAndSessionList = Tracking::show_user_progress( |
||
| 135 | $userId, |
||
| 136 | 0, |
||
| 137 | '', |
||
| 138 | true, |
||
| 139 | true, |
||
| 140 | true |
||
| 141 | ); |
||
| 142 | |||
| 143 | $stats = $calendarPlugin->getUserStats($userId, $courseAndSessionList); |
||
| 144 | $evaluations = $calendarPlugin->getGradebookEvaluationListToString($userId, $courseAndSessionList); |
||
| 145 | $data['gradebook_items'] = $evaluations; |
||
| 146 | $totalTime = 0; |
||
| 147 | foreach ($courseAndSessionList as $sessionId => $course) { |
||
| 148 | foreach ($course as $courseId) { |
||
| 149 | $totalTime += Tracking::get_time_spent_on_the_course($userId, $courseId, $sessionId); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $data['time_spent'] = api_time_to_hms($totalTime); |
||
| 154 | $data['lp_day_completed'] = $stats['completed']; |
||
| 155 | $data['days_diff'] = $stats['completed'] - $stats['user_event_count']; |
||
| 156 | } |
||
| 157 | $data['id'] = $data['user_id']; |
||
| 158 | $list[] = $data; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $list; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param int $type |
||
| 167 | * |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | public function get_count($type = -1) |
||
| 171 | { |
||
| 172 | $authorCondition = ''; |
||
| 173 | if ($this->allowTeachers()) { |
||
| 174 | if (!api_is_platform_admin()) { |
||
| 175 | $userId = api_get_user_id(); |
||
| 176 | $authorCondition = " AND author_id = $userId"; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($this->useMultipleUrl) { |
||
| 181 | $urlId = api_get_current_access_url_id(); |
||
| 182 | $sql = "SELECT count(u.id) as count FROM ".$this->table." u |
||
| 183 | INNER JOIN ".$this->access_url_rel_usergroup." a |
||
| 184 | ON (u.id = a.usergroup_id) |
||
| 185 | WHERE access_url_id = $urlId $authorCondition |
||
| 186 | "; |
||
| 187 | |||
| 188 | $result = Database::query($sql); |
||
| 189 | if (Database::num_rows($result)) { |
||
| 190 | $row = Database::fetch_array($result); |
||
| 191 | |||
| 192 | return $row['count']; |
||
| 193 | } |
||
| 194 | |||
| 195 | return 0; |
||
| 196 | } else { |
||
| 197 | $typeCondition = ''; |
||
| 198 | if ($type != -1) { |
||
| 199 | $type = (int) $type; |
||
| 200 | $typeCondition = " AND group_type = $type "; |
||
| 201 | } |
||
| 202 | |||
| 203 | $sql = "SELECT count(a.id) as count |
||
| 204 | FROM {$this->table} a |
||
| 205 | WHERE 1 =1 |
||
| 206 | $typeCondition |
||
| 207 | $authorCondition |
||
| 208 | "; |
||
| 209 | $result = Database::query($sql); |
||
| 210 | if (Database::num_rows($result)) { |
||
| 211 | $row = Database::fetch_array($result); |
||
| 212 | |||
| 213 | return $row['count']; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param int $course_id |
||
| 220 | * @param int $type |
||
| 221 | * |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | public function getUserGroupByCourseWithDataCount($course_id, $type = -1) |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $name |
||
| 270 | * |
||
| 271 | * @return mixed |
||
| 272 | */ |
||
| 273 | public function get_id_by_name($name) |
||
| 274 | { |
||
| 275 | $row = Database::select( |
||
| 276 | 'id', |
||
| 277 | $this->table, |
||
| 278 | ['where' => ['name = ?' => $name]], |
||
| 279 | 'first' |
||
| 280 | ); |
||
| 281 | |||
| 282 | return $row['id']; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Displays the title + grid. |
||
| 287 | */ |
||
| 288 | public function returnGrid() |
||
| 289 | { |
||
| 290 | // action links |
||
| 291 | $html = '<div class="actions">'; |
||
| 292 | if (api_is_platform_admin()) { |
||
| 293 | $html .= '<a href="../admin/index.php">'. |
||
| 294 | Display::return_icon( |
||
| 295 | 'back.png', |
||
| 296 | get_lang('BackTo').' '.get_lang('PlatformAdmin'), |
||
| 297 | '', |
||
| 298 | ICON_SIZE_MEDIUM |
||
| 299 | ). |
||
| 300 | '</a>'; |
||
| 301 | } |
||
| 302 | |||
| 303 | $html .= '<a href="'.api_get_self().'?action=add">'. |
||
| 304 | Display::return_icon('new_class.png', get_lang('AddClasses'), '', ICON_SIZE_MEDIUM). |
||
| 305 | '</a>'; |
||
| 306 | $html .= Display::url( |
||
| 307 | Display::return_icon('import_csv.png', get_lang('Import'), [], ICON_SIZE_MEDIUM), |
||
| 308 | 'usergroup_import.php' |
||
| 309 | ); |
||
| 310 | $html .= Display::url( |
||
| 311 | Display::return_icon('export_csv.png', get_lang('Export'), [], ICON_SIZE_MEDIUM), |
||
| 312 | 'usergroup_export.php' |
||
| 313 | ); |
||
| 314 | $html .= '</div>'; |
||
| 315 | $html .= Display::grid_html('usergroups'); |
||
| 316 | |||
| 317 | return $html; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Displays the title + grid. |
||
| 322 | */ |
||
| 323 | public function displayToolBarUserGroupUsers() |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get HTML grid. |
||
| 336 | */ |
||
| 337 | public function display_teacher_view() |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Gets a list of course ids by user group. |
||
| 344 | * |
||
| 345 | * @param int $id user group id |
||
| 346 | * @param bool $loadCourseData |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function get_courses_by_usergroup($id, $loadCourseData = false) |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param array $options |
||
| 410 | * @param int $type |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public function getUserGroupInCourse($options = [], $type = -1) |
||
| 415 | { |
||
| 416 | if ($this->useMultipleUrl) { |
||
| 417 | $sql = "SELECT u.* FROM {$this->usergroup_rel_course_table} usergroup |
||
| 418 | INNER JOIN {$this->table} u |
||
| 419 | ON (u.id = usergroup.usergroup_id) |
||
| 420 | INNER JOIN {$this->table_course} c |
||
| 421 | ON (usergroup.course_id = c.id) |
||
| 422 | INNER JOIN {$this->access_url_rel_usergroup} a |
||
| 423 | ON (a.usergroup_id = u.id) |
||
| 424 | "; |
||
| 425 | } else { |
||
| 426 | $sql = "SELECT u.* FROM {$this->usergroup_rel_course_table} usergroup |
||
| 427 | INNER JOIN {$this->table} u |
||
| 428 | ON (u.id = usergroup.usergroup_id) |
||
| 429 | INNER JOIN {$this->table_course} c |
||
| 430 | ON (usergroup.course_id = c.id) |
||
| 431 | "; |
||
| 432 | } |
||
| 433 | |||
| 434 | $conditions = Database::parse_conditions($options); |
||
| 435 | |||
| 436 | $typeCondition = ''; |
||
| 437 | if ($type != -1) { |
||
| 438 | $type = (int) $type; |
||
| 439 | $typeCondition = " AND group_type = $type "; |
||
| 440 | } |
||
| 441 | |||
| 442 | if (empty($conditions)) { |
||
| 443 | $conditions .= "WHERE 1 = 1 $typeCondition "; |
||
| 444 | } else { |
||
| 445 | $conditions .= " $typeCondition "; |
||
| 446 | } |
||
| 447 | |||
| 448 | $sql .= $conditions; |
||
| 449 | |||
| 450 | if ($this->useMultipleUrl) { |
||
| 451 | $urlId = api_get_current_access_url_id(); |
||
| 452 | $sql .= " AND access_url_id = $urlId "; |
||
| 453 | } |
||
| 454 | |||
| 455 | if (isset($options['LIMIT'])) { |
||
| 456 | $limits = explode(',', $options['LIMIT']); |
||
| 457 | $limits = array_map('intval', $limits); |
||
| 458 | if (isset($limits[0]) && isset($limits[1])) { |
||
| 459 | $sql .= " LIMIT ".$limits[0].', '.$limits[1]; |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | $result = Database::query($sql); |
||
| 464 | $array = Database::store_result($result, 'ASSOC'); |
||
| 465 | |||
| 466 | return $array; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param array $options |
||
| 471 | * @param int $type |
||
| 472 | * |
||
| 473 | * @return array|bool |
||
| 474 | */ |
||
| 475 | public function getUserGroupNotInCourse($options = [], $type = -1) |
||
| 476 | { |
||
| 477 | $course_id = null; |
||
| 478 | if (isset($options['course_id'])) { |
||
| 479 | $course_id = (int) $options['course_id']; |
||
| 480 | unset($options['course_id']); |
||
| 481 | } |
||
| 482 | |||
| 483 | if (empty($course_id)) { |
||
| 484 | return false; |
||
| 485 | } |
||
| 486 | |||
| 487 | $typeCondition = ''; |
||
| 488 | if ($type != -1) { |
||
| 489 | $type = (int) $type; |
||
| 490 | $typeCondition = " AND group_type = $type "; |
||
| 491 | } |
||
| 492 | |||
| 493 | if ($this->useMultipleUrl) { |
||
| 494 | $urlId = api_get_current_access_url_id(); |
||
| 495 | $sql = "SELECT DISTINCT u.* |
||
| 496 | FROM {$this->table} u |
||
| 497 | INNER JOIN {$this->access_url_rel_usergroup} a |
||
| 498 | ON (a.usergroup_id = u.id) |
||
| 499 | LEFT OUTER JOIN {$this->usergroup_rel_course_table} urc |
||
| 500 | ON (u.id = urc.usergroup_id AND course_id = $course_id) |
||
| 501 | "; |
||
| 502 | } else { |
||
| 503 | $sql = "SELECT DISTINCT u.* |
||
| 504 | FROM {$this->table} u |
||
| 505 | LEFT OUTER JOIN {$this->usergroup_rel_course_table} urc |
||
| 506 | ON (u.id = urc.usergroup_id AND course_id = $course_id) |
||
| 507 | "; |
||
| 508 | } |
||
| 509 | $conditions = Database::parse_conditions($options); |
||
| 510 | |||
| 511 | if (empty($conditions)) { |
||
| 512 | $conditions .= "WHERE 1 = 1 $typeCondition "; |
||
| 513 | } else { |
||
| 514 | $conditions .= " $typeCondition "; |
||
| 515 | } |
||
| 516 | |||
| 517 | $sql .= $conditions; |
||
| 518 | |||
| 519 | if ($this->useMultipleUrl) { |
||
| 520 | $sql .= " AND access_url_id = $urlId"; |
||
| 521 | } |
||
| 522 | |||
| 523 | if (isset($options['LIMIT'])) { |
||
| 524 | $limits = explode(',', $options['LIMIT']); |
||
| 525 | $limits = array_map('intval', $limits); |
||
| 526 | if (isset($limits[0]) && isset($limits[1])) { |
||
| 527 | $sql .= " LIMIT ".$limits[0].', '.$limits[1]; |
||
| 528 | } |
||
| 529 | } |
||
| 530 | |||
| 531 | $result = Database::query($sql); |
||
| 532 | $array = Database::store_result($result, 'ASSOC'); |
||
| 533 | |||
| 534 | return $array; |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param int $course_id |
||
| 539 | * |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | public function get_usergroup_by_course($course_id) |
||
| 543 | { |
||
| 544 | if ($this->useMultipleUrl) { |
||
| 545 | $urlId = api_get_current_access_url_id(); |
||
| 546 | $options = [ |
||
| 547 | 'where' => [ |
||
| 548 | 'c.course_id = ? AND access_url_id = ?' => [ |
||
| 549 | $course_id, |
||
| 550 | $urlId, |
||
| 551 | ], |
||
| 552 | ], |
||
| 553 | ]; |
||
| 554 | $from = $this->usergroup_rel_course_table." as c |
||
| 555 | INNER JOIN ".$this->access_url_rel_usergroup." a |
||
| 556 | ON c.usergroup_id = a.usergroup_id"; |
||
| 557 | } else { |
||
| 558 | $options = ['where' => ['c.course_id = ?' => $course_id]]; |
||
| 559 | $from = $this->usergroup_rel_course_table." c"; |
||
| 560 | } |
||
| 561 | |||
| 562 | $results = Database::select('c.usergroup_id', $from, $options); |
||
| 563 | $array = []; |
||
| 564 | if (!empty($results)) { |
||
| 565 | foreach ($results as $row) { |
||
| 566 | $array[] = $row['usergroup_id']; |
||
| 567 | } |
||
| 568 | } |
||
| 569 | |||
| 570 | return $array; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @param int $usergroup_id |
||
| 575 | * @param int $course_id |
||
| 576 | * |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | public function usergroup_was_added_in_course($usergroup_id, $course_id) |
||
| 580 | { |
||
| 581 | $results = Database::select( |
||
| 582 | 'usergroup_id', |
||
| 583 | $this->usergroup_rel_course_table, |
||
| 584 | ['where' => ['course_id = ? AND usergroup_id = ?' => [$course_id, $usergroup_id]]] |
||
| 585 | ); |
||
| 586 | |||
| 587 | if (empty($results)) { |
||
| 588 | return false; |
||
| 589 | } |
||
| 590 | |||
| 591 | return true; |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Gets a list of session ids by user group. |
||
| 596 | * |
||
| 597 | * @param int $id user group id |
||
| 598 | * |
||
| 599 | * @return array |
||
| 600 | */ |
||
| 601 | public function get_sessions_by_usergroup($id) |
||
| 602 | { |
||
| 603 | $results = Database::select( |
||
| 604 | 'session_id', |
||
| 605 | $this->usergroup_rel_session_table, |
||
| 606 | ['where' => ['usergroup_id = ?' => $id]] |
||
| 607 | ); |
||
| 608 | |||
| 609 | $array = []; |
||
| 610 | if (!empty($results)) { |
||
| 611 | foreach ($results as $row) { |
||
| 612 | $array[] = $row['session_id']; |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | return $array; |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Gets a list of user ids by user group. |
||
| 621 | * |
||
| 622 | * @param int $id user group id |
||
| 623 | * @param array $roles |
||
| 624 | * |
||
| 625 | * @return array with a list of user ids |
||
| 626 | */ |
||
| 627 | public function get_users_by_usergroup($id = null, $roles = []) |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Gets a list of user ids by user group. |
||
| 669 | * |
||
| 670 | * @param int $id user group id |
||
| 671 | * @param int $relation |
||
| 672 | * |
||
| 673 | * @return array with a list of user ids |
||
| 674 | */ |
||
| 675 | public function getUsersByUsergroupAndRelation($id, $relation = 0) |
||
| 676 | { |
||
| 677 | $relation = (int) $relation; |
||
| 678 | if (empty($relation)) { |
||
| 679 | $conditions = ['where' => ['usergroup_id = ? AND (relation_type = 0 OR relation_type IS NULL OR relation_type = "") ' => [$id]]]; |
||
| 680 | } else { |
||
| 681 | $conditions = ['where' => ['usergroup_id = ? AND relation_type = ?' => [$id, $relation]]]; |
||
| 682 | } |
||
| 683 | |||
| 684 | $results = Database::select( |
||
| 685 | 'user_id', |
||
| 686 | $this->usergroup_rel_user_table, |
||
| 687 | $conditions |
||
| 688 | ); |
||
| 689 | |||
| 690 | $array = []; |
||
| 691 | if (!empty($results)) { |
||
| 692 | foreach ($results as $row) { |
||
| 693 | $array[] = $row['user_id']; |
||
| 694 | } |
||
| 695 | } |
||
| 696 | |||
| 697 | return $array; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Get the group list for a user. |
||
| 702 | * |
||
| 703 | * @param int $userId The user ID |
||
| 704 | * @param int $filterByType Optional. The type of group |
||
| 705 | * |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | public function getUserGroupListByUser($userId, $filterByType = null) |
||
| 709 | { |
||
| 710 | $userId = (int) $userId; |
||
| 711 | if ($this->useMultipleUrl) { |
||
| 712 | $urlId = api_get_current_access_url_id(); |
||
| 713 | $from = $this->usergroup_rel_user_table." u |
||
| 714 | INNER JOIN {$this->access_url_rel_usergroup} a |
||
| 715 | ON (a.usergroup_id AND u.usergroup_id) |
||
| 716 | INNER JOIN {$this->table} g |
||
| 717 | ON (u.usergroup_id = g.id) |
||
| 718 | "; |
||
| 719 | $where = ['where' => ['user_id = ? AND access_url_id = ? ' => [$userId, $urlId]]]; |
||
| 720 | } else { |
||
| 721 | $from = $this->usergroup_rel_user_table." u |
||
| 722 | INNER JOIN {$this->table} g |
||
| 723 | ON (u.usergroup_id = g.id) |
||
| 724 | "; |
||
| 725 | $where = ['where' => ['user_id = ?' => $userId]]; |
||
| 726 | } |
||
| 727 | |||
| 728 | if ($filterByType !== null) { |
||
| 729 | $where['where'][' AND g.group_type = ?'] = (int) $filterByType; |
||
| 730 | } |
||
| 731 | |||
| 732 | $results = Database::select( |
||
| 733 | 'g.*', |
||
| 734 | $from, |
||
| 735 | $where |
||
| 736 | ); |
||
| 737 | $array = []; |
||
| 738 | if (!empty($results)) { |
||
| 739 | foreach ($results as $row) { |
||
| 740 | $array[] = $row; |
||
| 741 | } |
||
| 742 | } |
||
| 743 | |||
| 744 | return $array; |
||
| 745 | } |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Gets the usergroup id list by user id. |
||
| 749 | * |
||
| 750 | * @param int $userId user id |
||
| 751 | * |
||
| 752 | * @return array |
||
| 753 | */ |
||
| 754 | public function get_usergroup_by_user($userId) |
||
| 755 | { |
||
| 756 | $userId = (int) $userId; |
||
| 757 | if ($this->useMultipleUrl) { |
||
| 758 | $urlId = api_get_current_access_url_id(); |
||
| 759 | $from = $this->usergroup_rel_user_table." u |
||
| 760 | INNER JOIN {$this->access_url_rel_usergroup} a ON (a.usergroup_id = u.usergroup_id)"; |
||
| 761 | $where = ['where' => ['user_id = ? AND access_url_id = ? ' => [$userId, $urlId]]]; |
||
| 762 | } else { |
||
| 763 | $from = $this->usergroup_rel_user_table.' u '; |
||
| 764 | $where = ['where' => ['user_id = ?' => $userId]]; |
||
| 765 | } |
||
| 766 | |||
| 767 | $results = Database::select( |
||
| 768 | 'u.usergroup_id', |
||
| 769 | $from, |
||
| 770 | $where |
||
| 771 | ); |
||
| 772 | |||
| 773 | $array = []; |
||
| 774 | if (!empty($results)) { |
||
| 775 | foreach ($results as $row) { |
||
| 776 | $array[] = $row['usergroup_id']; |
||
| 777 | } |
||
| 778 | } |
||
| 779 | |||
| 780 | return $array; |
||
| 781 | } |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Subscribes sessions to a group (also adding the members of the group in the session and course). |
||
| 785 | * |
||
| 786 | * @param int $usergroup_id usergroup id |
||
| 787 | * @param array $list list of session ids |
||
| 788 | * @param bool $deleteCurrentSessions Optional. Empty the session list for the usergroup (class) |
||
| 789 | */ |
||
| 790 | public function subscribe_sessions_to_usergroup($usergroup_id, $list, $deleteCurrentSessions = true) |
||
| 791 | { |
||
| 792 | $current_list = self::get_sessions_by_usergroup($usergroup_id); |
||
| 793 | $user_list = self::get_users_by_usergroup($usergroup_id); |
||
| 794 | |||
| 795 | $delete_items = $new_items = []; |
||
| 796 | if (!empty($list)) { |
||
| 797 | foreach ($list as $session_id) { |
||
| 798 | if (!in_array($session_id, $current_list)) { |
||
| 799 | $new_items[] = $session_id; |
||
| 800 | } |
||
| 801 | } |
||
| 802 | } |
||
| 803 | if ($deleteCurrentSessions) { |
||
| 804 | if (!empty($current_list)) { |
||
| 805 | foreach ($current_list as $session_id) { |
||
| 806 | if (!in_array($session_id, $list)) { |
||
| 807 | $delete_items[] = $session_id; |
||
| 808 | } |
||
| 809 | } |
||
| 810 | } |
||
| 811 | |||
| 812 | // Deleting items |
||
| 813 | if (!empty($delete_items)) { |
||
| 814 | foreach ($delete_items as $session_id) { |
||
| 815 | if (!empty($user_list)) { |
||
| 816 | foreach ($user_list as $user_id) { |
||
| 817 | SessionManager::unsubscribe_user_from_session($session_id, $user_id); |
||
| 818 | } |
||
| 819 | } |
||
| 820 | Database::delete( |
||
| 821 | $this->usergroup_rel_session_table, |
||
| 822 | ['usergroup_id = ? AND session_id = ?' => [$usergroup_id, $session_id]] |
||
| 823 | ); |
||
| 824 | } |
||
| 825 | } |
||
| 826 | } |
||
| 827 | |||
| 828 | // Adding new relationships. |
||
| 829 | if (!empty($new_items)) { |
||
| 830 | foreach ($new_items as $session_id) { |
||
| 831 | $params = ['session_id' => $session_id, 'usergroup_id' => $usergroup_id]; |
||
| 832 | Database::insert($this->usergroup_rel_session_table, $params); |
||
| 833 | |||
| 834 | if (!empty($user_list)) { |
||
| 835 | SessionManager::subscribeUsersToSession( |
||
| 836 | $session_id, |
||
| 837 | $user_list, |
||
| 838 | null, |
||
| 839 | false |
||
| 840 | ); |
||
| 841 | } |
||
| 842 | } |
||
| 843 | } |
||
| 844 | } |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Subscribes courses to a group (also adding the members of the group in the course). |
||
| 848 | * |
||
| 849 | * @param int $usergroup_id usergroup id |
||
| 850 | * @param array $list list of course ids (integers) |
||
| 851 | * @param bool $delete_groups |
||
| 852 | */ |
||
| 853 | public function subscribe_courses_to_usergroup($usergroup_id, $list, $delete_groups = true) |
||
| 899 | ); |
||
| 900 | } |
||
| 901 | } |
||
| 902 | } |
||
| 903 | } |
||
| 904 | |||
| 905 | /** |
||
| 906 | * @param int $usergroup_id |
||
| 907 | * @param array $delete_items |
||
| 908 | */ |
||
| 909 | public function unsubscribe_courses_from_usergroup($usergroup_id, $delete_items) |
||
| 910 | { |
||
| 911 | // Deleting items. |
||
| 912 | if (!empty($delete_items)) { |
||
| 913 | $user_list = self::get_users_by_usergroup($usergroup_id); |
||
| 914 | |||
| 915 | foreach ($delete_items as $course_id) { |
||
| 916 | $course_info = api_get_course_info_by_id($course_id); |
||
| 917 | if ($course_info) { |
||
| 918 | if (!empty($user_list)) { |
||
| 919 | foreach ($user_list as $user_id) { |
||
| 920 | CourseManager::unsubscribe_user( |
||
| 921 | $user_id, |
||
| 922 | $course_info['code'] |
||
| 923 | ); |
||
| 924 | } |
||
| 925 | } |
||
| 926 | |||
| 927 | Database::delete( |
||
| 928 | $this->usergroup_rel_course_table, |
||
| 929 | [ |
||
| 930 | 'usergroup_id = ? AND course_id = ?' => [ |
||
| 931 | $usergroup_id, |
||
| 932 | $course_id, |
||
| 933 | ], |
||
| 934 | ] |
||
| 935 | ); |
||
| 936 | } |
||
| 937 | } |
||
| 938 | } |
||
| 939 | } |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Subscribe users to a group. |
||
| 943 | * |
||
| 944 | * @param int $usergroup_id usergroup id |
||
| 945 | * @param array $list list of user ids |
||
| 946 | * @param bool $delete_users_not_present_in_list |
||
| 947 | * @param int $relationType |
||
| 948 | */ |
||
| 949 | public function subscribe_users_to_usergroup( |
||
| 950 | $usergroup_id, |
||
| 951 | $list, |
||
| 952 | $delete_users_not_present_in_list = true, |
||
| 953 | $relationType = 0 |
||
| 954 | ) { |
||
| 955 | $current_list = self::get_users_by_usergroup($usergroup_id); |
||
| 956 | $course_list = self::get_courses_by_usergroup($usergroup_id); |
||
| 957 | $session_list = self::get_sessions_by_usergroup($usergroup_id); |
||
| 958 | $session_list = array_filter($session_list); |
||
| 959 | $relationType = (int) $relationType; |
||
| 960 | |||
| 961 | $delete_items = []; |
||
| 962 | $new_items = []; |
||
| 963 | |||
| 964 | if (!empty($list)) { |
||
| 965 | foreach ($list as $user_id) { |
||
| 966 | if (!in_array($user_id, $current_list)) { |
||
| 967 | $new_items[] = $user_id; |
||
| 968 | } |
||
| 969 | } |
||
| 970 | } |
||
| 971 | |||
| 972 | if (!empty($current_list)) { |
||
| 973 | foreach ($current_list as $user_id) { |
||
| 974 | if (!in_array($user_id, $list)) { |
||
| 975 | $delete_items[] = $user_id; |
||
| 976 | } |
||
| 977 | } |
||
| 978 | } |
||
| 979 | |||
| 980 | // Deleting items |
||
| 981 | if (!empty($delete_items) && $delete_users_not_present_in_list) { |
||
| 982 | foreach ($delete_items as $user_id) { |
||
| 983 | // Removing courses |
||
| 984 | if (!empty($course_list)) { |
||
| 985 | foreach ($course_list as $course_id) { |
||
| 986 | $course_info = api_get_course_info_by_id($course_id); |
||
| 987 | CourseManager::unsubscribe_user($user_id, $course_info['code']); |
||
| 988 | } |
||
| 989 | } |
||
| 990 | // Removing sessions |
||
| 991 | if (!empty($session_list)) { |
||
| 992 | foreach ($session_list as $session_id) { |
||
| 993 | SessionManager::unsubscribe_user_from_session($session_id, $user_id); |
||
| 994 | } |
||
| 995 | } |
||
| 996 | |||
| 997 | if (empty($relationType)) { |
||
| 998 | Database::delete( |
||
| 999 | $this->usergroup_rel_user_table, |
||
| 1000 | [ |
||
| 1001 | 'usergroup_id = ? AND user_id = ? AND (relation_type = "0" OR relation_type IS NULL OR relation_type = "")' => [ |
||
| 1002 | $usergroup_id, |
||
| 1003 | $user_id, |
||
| 1004 | ], |
||
| 1005 | ] |
||
| 1006 | ); |
||
| 1007 | } else { |
||
| 1008 | Database::delete( |
||
| 1009 | $this->usergroup_rel_user_table, |
||
| 1010 | [ |
||
| 1011 | 'usergroup_id = ? AND user_id = ? AND relation_type = ?' => [ |
||
| 1012 | $usergroup_id, |
||
| 1013 | $user_id, |
||
| 1014 | $relationType, |
||
| 1015 | ], |
||
| 1016 | ] |
||
| 1017 | ); |
||
| 1018 | } |
||
| 1019 | } |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | // Adding new relationships |
||
| 1023 | if (!empty($new_items)) { |
||
| 1024 | // Adding sessions |
||
| 1025 | if (!empty($session_list)) { |
||
| 1026 | foreach ($session_list as $session_id) { |
||
| 1027 | SessionManager::subscribeUsersToSession($session_id, $new_items, null, false); |
||
| 1028 | } |
||
| 1029 | } |
||
| 1030 | |||
| 1031 | foreach ($new_items as $user_id) { |
||
| 1032 | // Adding courses |
||
| 1033 | if (!empty($course_list)) { |
||
| 1034 | foreach ($course_list as $course_id) { |
||
| 1035 | $course_info = api_get_course_info_by_id($course_id); |
||
| 1036 | CourseManager::subscribeUser($user_id, $course_info['code']); |
||
| 1037 | } |
||
| 1038 | } |
||
| 1039 | $params = [ |
||
| 1040 | 'user_id' => $user_id, |
||
| 1041 | 'usergroup_id' => $usergroup_id, |
||
| 1042 | 'relation_type' => $relationType, |
||
| 1043 | ]; |
||
| 1044 | Database::insert($this->usergroup_rel_user_table, $params); |
||
| 1045 | } |
||
| 1046 | } |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * @param string $name |
||
| 1051 | * |
||
| 1052 | * @return bool |
||
| 1053 | */ |
||
| 1054 | public function usergroup_exists($name) |
||
| 1055 | { |
||
| 1056 | $name = Database::escape_string($name); |
||
| 1057 | if ($this->useMultipleUrl) { |
||
| 1058 | $urlId = api_get_current_access_url_id(); |
||
| 1059 | $sql = "SELECT * FROM $this->table u |
||
| 1060 | INNER JOIN {$this->access_url_rel_usergroup} a |
||
| 1061 | ON (a.usergroup_id = u.id) |
||
| 1062 | WHERE name = '".$name."' AND access_url_id = $urlId"; |
||
| 1063 | } else { |
||
| 1064 | $sql = "SELECT * FROM $this->table WHERE name = '".$name."'"; |
||
| 1065 | } |
||
| 1066 | |||
| 1067 | $res = Database::query($sql); |
||
| 1068 | |||
| 1069 | return Database::num_rows($res) != 0; |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * @return bool |
||
| 1074 | */ |
||
| 1075 | public function allowTeachers() |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * @param int $sidx |
||
| 1082 | * @param int $sord |
||
| 1083 | * @param int $start |
||
| 1084 | * @param int $limit |
||
| 1085 | * |
||
| 1086 | * @return array |
||
| 1087 | */ |
||
| 1088 | public function getUsergroupsPagination($sidx, $sord, $start, $limit) |
||
| 1089 | { |
||
| 1090 | $sord = in_array(strtolower($sord), ['asc', 'desc']) ? $sord : 'desc'; |
||
| 1091 | |||
| 1092 | $start = (int) $start; |
||
| 1093 | $limit = (int) $limit; |
||
| 1094 | if ($this->useMultipleUrl) { |
||
| 1095 | $urlId = api_get_current_access_url_id(); |
||
| 1096 | $from = $this->table." u INNER JOIN {$this->access_url_rel_usergroup} a ON (u.id = a.usergroup_id)"; |
||
| 1097 | $where = [' access_url_id = ?' => $urlId]; |
||
| 1098 | } else { |
||
| 1099 | $from = $this->table.' u '; |
||
| 1100 | $where = []; |
||
| 1101 | } |
||
| 1102 | |||
| 1103 | if ($this->allowTeachers()) { |
||
| 1104 | if (!api_is_platform_admin()) { |
||
| 1105 | $userId = api_get_user_id(); |
||
| 1106 | $where = [' author_id = ?' => $userId]; |
||
| 1107 | } |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | $result = Database::select( |
||
| 1111 | 'u.*', |
||
| 1112 | $from, |
||
| 1113 | [ |
||
| 1114 | 'where' => $where, |
||
| 1115 | 'order' => "name $sord", |
||
| 1116 | 'LIMIT' => "$start , $limit", |
||
| 1117 | ] |
||
| 1118 | ); |
||
| 1119 | |||
| 1120 | $new_result = []; |
||
| 1121 | if (!empty($result)) { |
||
| 1122 | foreach ($result as $group) { |
||
| 1123 | $group['sessions'] = count($this->get_sessions_by_usergroup($group['id'])); |
||
| 1124 | $group['courses'] = count($this->get_courses_by_usergroup($group['id'])); |
||
| 1125 | $roles = []; |
||
| 1126 | switch ($group['group_type']) { |
||
| 1127 | case 0: |
||
| 1128 | $group['group_type'] = Display::label(get_lang('Class'), 'info'); |
||
| 1129 | $roles = [0]; |
||
| 1130 | break; |
||
| 1131 | case 1: |
||
| 1132 | $group['group_type'] = Display::label(get_lang('Social'), 'success'); |
||
| 1133 | $roles = [ |
||
| 1134 | GROUP_USER_PERMISSION_ADMIN, |
||
| 1135 | GROUP_USER_PERMISSION_READER, |
||
| 1136 | GROUP_USER_PERMISSION_MODERATOR, |
||
| 1137 | GROUP_USER_PERMISSION_HRM, |
||
| 1138 | ]; |
||
| 1139 | break; |
||
| 1140 | } |
||
| 1141 | $group['users'] = Display::url( |
||
| 1142 | count($this->get_users_by_usergroup($group['id'], $roles)), |
||
| 1143 | api_get_path(WEB_CODE_PATH).'admin/usergroup_users.php?id='.$group['id'] |
||
| 1144 | ); |
||
| 1145 | $new_result[] = $group; |
||
| 1146 | } |
||
| 1147 | $result = $new_result; |
||
| 1148 | } |
||
| 1149 | $columns = ['name', 'users', 'courses', 'sessions', 'group_type']; |
||
| 1150 | |||
| 1151 | if (!in_array($sidx, $columns)) { |
||
| 1152 | $sidx = 'name'; |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | // Multidimensional sort |
||
| 1156 | $result = msort($result, $sidx, $sord); |
||
| 1157 | |||
| 1158 | return $result; |
||
| 1159 | } |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * @param array $options |
||
| 1163 | * |
||
| 1164 | * @return array |
||
| 1165 | */ |
||
| 1166 | public function getDataToExport($options = []) |
||
| 1167 | { |
||
| 1168 | if ($this->useMultipleUrl) { |
||
| 1169 | $urlId = api_get_current_access_url_id(); |
||
| 1170 | $from = $this->table." u INNER JOIN {$this->access_url_rel_usergroup} a |
||
| 1171 | ON (u.id = a.usergroup_id)"; |
||
| 1172 | $options = ['where' => ['access_url_id = ? ' => $urlId]]; |
||
| 1173 | if ($this->allowTeachers()) { |
||
| 1174 | $options['where'] = [' author_id = ? ' => api_get_user_id()]; |
||
| 1175 | } |
||
| 1176 | $classes = Database::select('a.id, name, description', $from, $options); |
||
| 1177 | } else { |
||
| 1178 | if ($this->allowTeachers()) { |
||
| 1179 | $options['where'] = [' author_id = ? ' => api_get_user_id()]; |
||
| 1180 | } |
||
| 1181 | $classes = Database::select('id, name, description', $this->table, $options); |
||
| 1182 | } |
||
| 1183 | |||
| 1184 | $result = []; |
||
| 1185 | if (!empty($classes)) { |
||
| 1186 | foreach ($classes as $data) { |
||
| 1187 | $users = $this->getUserListByUserGroup($data['id']); |
||
| 1188 | $userToString = null; |
||
| 1189 | if (!empty($users)) { |
||
| 1190 | $userNameList = []; |
||
| 1191 | foreach ($users as $userData) { |
||
| 1192 | $userNameList[] = $userData['username']; |
||
| 1193 | } |
||
| 1194 | $userToString = implode(',', $userNameList); |
||
| 1195 | } |
||
| 1196 | $data['users'] = $userToString; |
||
| 1197 | $result[] = $data; |
||
| 1198 | } |
||
| 1199 | } |
||
| 1200 | |||
| 1201 | return $result; |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | /** |
||
| 1205 | * @param string $firstLetter |
||
| 1206 | * |
||
| 1207 | * @return array |
||
| 1208 | */ |
||
| 1209 | public function filterByFirstLetter($firstLetter) |
||
| 1210 | { |
||
| 1211 | $firstLetter = Database::escape_string($firstLetter); |
||
| 1212 | $sql = "SELECT id, name FROM $this->table |
||
| 1213 | WHERE |
||
| 1214 | name LIKE '".$firstLetter."%' OR |
||
| 1215 | name LIKE '".api_strtolower($firstLetter)."%' |
||
| 1216 | ORDER BY name DESC "; |
||
| 1217 | |||
| 1218 | $result = Database::query($sql); |
||
| 1219 | |||
| 1220 | return Database::store_result($result); |
||
| 1221 | } |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Select user group not in list. |
||
| 1225 | * |
||
| 1226 | * @param array $list |
||
| 1227 | * |
||
| 1228 | * @return array |
||
| 1229 | */ |
||
| 1230 | public function getUserGroupNotInList($list) |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * @param $params |
||
| 1247 | * @param bool $show_query |
||
| 1248 | * |
||
| 1249 | * @return bool|int |
||
| 1250 | */ |
||
| 1251 | public function save($params, $show_query = false) |
||
| 1291 | } |
||
| 1292 | |||
| 1293 | /** |
||
| 1294 | * {@inheritdoc} |
||
| 1295 | */ |
||
| 1296 | public function update($values, $showQuery = false) |
||
| 1324 | } |
||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * @param int $groupId |
||
| 1328 | * @param string $picture |
||
| 1329 | * @param string $cropParameters |
||
| 1330 | * |
||
| 1331 | * @return bool|string |
||
| 1332 | */ |
||
| 1333 | public function manageFileUpload($groupId, $picture, $cropParameters) |
||
| 1334 | { |
||
| 1335 | if (!empty($picture['name'])) { |
||
| 1336 | return $this->update_group_picture( |
||
| 1337 | $groupId, |
||
| 1338 | $picture['name'], |
||
| 1339 | $picture['tmp_name'], |
||
| 1340 | $cropParameters |
||
| 1341 | ); |
||
| 1342 | } |
||
| 1343 | |||
| 1344 | return false; |
||
| 1345 | } |
||
| 1346 | |||
| 1347 | /** |
||
| 1348 | * @param $group_id |
||
| 1349 | * |
||
| 1350 | * @return string |
||
| 1351 | */ |
||
| 1352 | public function delete_group_picture($group_id) |
||
| 1355 | } |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * Creates new group pictures in various sizes of a user, or deletes user pfotos. |
||
| 1359 | * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php. |
||
| 1360 | * |
||
| 1361 | * @param int The group id |
||
| 1362 | * @param string $file The common file name for the newly created photos. |
||
| 1363 | * It will be checked and modified for compatibility with the file system. |
||
| 1364 | * If full name is provided, path component is ignored. |
||
| 1365 | * If an empty name is provided, then old user photos are deleted only, |
||
| 1366 | * |
||
| 1367 | * @see UserManager::delete_user_picture() as the prefered way for deletion. |
||
| 1368 | * |
||
| 1369 | * @param string $source_file the full system name of the image from which user photos will be created |
||
| 1370 | * @param string $cropParameters |
||
| 1371 | * |
||
| 1372 | * @return mixed Returns the resulting common file name of created images which usually should be stored in database. |
||
| 1373 | * When an image is removed the function returns an empty string. In case of internal error or negative validation it returns FALSE. |
||
| 1374 | * |
||
| 1375 | * @see UserManager::delete_user_picture() as the prefered way for deletion. |
||
| 1376 | */ |
||
| 1377 | public function update_group_picture($group_id, $file = null, $source_file = null, $cropParameters = null) |
||
| 1378 | { |
||
| 1379 | // Validation 1. |
||
| 1380 | $group_id = (int) $group_id; |
||
| 1381 | |||
| 1382 | if (empty($group_id)) { |
||
| 1383 | return false; |
||
| 1384 | } |
||
| 1385 | $delete = empty($file); |
||
| 1386 | if (empty($source_file)) { |
||
| 1387 | $source_file = $file; |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | // User-reserved directory where photos have to be placed. |
||
| 1391 | $path_info = self::get_group_picture_path_by_id($group_id, 'system', true); |
||
| 1392 | |||
| 1393 | $path = $path_info['dir']; |
||
| 1394 | |||
| 1395 | // If this directory does not exist - we create it. |
||
| 1396 | if (!is_dir($path)) { |
||
| 1397 | $res = @mkdir($path, api_get_permissions_for_new_directories(), true); |
||
| 1398 | if ($res === false) { |
||
| 1399 | // There was an issue creating the directory $path, probably |
||
| 1400 | // permissions-related |
||
| 1401 | return false; |
||
| 1402 | } |
||
| 1403 | } |
||
| 1404 | |||
| 1405 | // Exit if only deletion has been requested. Return an empty picture name. |
||
| 1406 | if ($delete) { |
||
| 1407 | return ''; |
||
| 1408 | } |
||
| 1409 | |||
| 1410 | // Validation 2. |
||
| 1411 | $allowed_types = ['jpg', 'jpeg', 'png', 'gif']; |
||
| 1412 | $file = str_replace('\\', '/', $file); |
||
| 1413 | $filename = (($pos = strrpos($file, '/')) !== false) ? substr($file, $pos + 1) : $file; |
||
| 1414 | $extension = strtolower(substr(strrchr($filename, '.'), 1)); |
||
| 1415 | if (!in_array($extension, $allowed_types)) { |
||
| 1416 | return false; |
||
| 1417 | } |
||
| 1418 | $avatar = 'group_avatar.jpg'; |
||
| 1419 | $filename = $group_id.'_'.$filename; |
||
| 1420 | $groupImageBig = $path.'big_'.$filename; |
||
| 1421 | $groupImage = $path.'medium_'.$filename; |
||
| 1422 | |||
| 1423 | if (file_exists($groupImageBig)) { |
||
| 1424 | unlink($groupImageBig); |
||
| 1425 | } |
||
| 1426 | if (file_exists($groupImage)) { |
||
| 1427 | unlink($groupImage); |
||
| 1428 | } |
||
| 1429 | |||
| 1430 | $image = new Image($source_file); |
||
| 1431 | $image->crop($cropParameters); |
||
| 1432 | |||
| 1433 | //Resize the images in two formats |
||
| 1434 | $medium = new Image($source_file); |
||
| 1435 | $medium->resize(128); |
||
| 1436 | $medium->send_image($groupImage, -1, 'jpg'); |
||
| 1437 | $normal = new Image($source_file); |
||
| 1438 | $normal->resize(450); |
||
| 1439 | $normal->send_image($groupImageBig, -1, 'jpg'); |
||
| 1440 | |||
| 1441 | return $filename; |
||
| 1442 | } |
||
| 1443 | |||
| 1444 | /** |
||
| 1445 | * @return mixed |
||
| 1446 | */ |
||
| 1447 | public function getGroupType() |
||
| 1448 | { |
||
| 1449 | return $this->groupType; |
||
| 1450 | } |
||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * @param int $id |
||
| 1454 | * |
||
| 1455 | * @return bool|void |
||
| 1456 | */ |
||
| 1457 | public function delete($id) |
||
| 1458 | { |
||
| 1459 | $id = (int) $id; |
||
| 1460 | if ($this->useMultipleUrl) { |
||
| 1461 | $this->unsubscribeToUrl($id, api_get_current_access_url_id()); |
||
| 1462 | } |
||
| 1463 | |||
| 1464 | $sql = "DELETE FROM $this->usergroup_rel_user_table |
||
| 1465 | WHERE usergroup_id = $id"; |
||
| 1466 | Database::query($sql); |
||
| 1467 | |||
| 1468 | $sql = "DELETE FROM $this->usergroup_rel_course_table |
||
| 1469 | WHERE usergroup_id = $id"; |
||
| 1470 | Database::query($sql); |
||
| 1471 | |||
| 1472 | $sql = "DELETE FROM $this->usergroup_rel_session_table |
||
| 1473 | WHERE usergroup_id = $id"; |
||
| 1474 | Database::query($sql); |
||
| 1475 | |||
| 1476 | /*$sql = "DELETE FROM $this->usergroup_rel_ |
||
| 1477 | WHERE usergroup_id = $id"; |
||
| 1478 | Database::query($sql);*/ |
||
| 1479 | |||
| 1480 | parent::delete($id); |
||
| 1481 | } |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * @param int $id |
||
| 1485 | * @param int $urlId |
||
| 1486 | */ |
||
| 1487 | public function subscribeToUrl($id, $urlId) |
||
| 1488 | { |
||
| 1489 | Database::insert( |
||
| 1490 | $this->access_url_rel_usergroup, |
||
| 1491 | [ |
||
| 1492 | 'access_url_id' => $urlId, |
||
| 1493 | 'usergroup_id' => $id, |
||
| 1494 | ] |
||
| 1495 | ); |
||
| 1496 | } |
||
| 1497 | |||
| 1498 | /** |
||
| 1499 | * @param int $id |
||
| 1500 | * @param int $urlId |
||
| 1501 | */ |
||
| 1502 | public function unsubscribeToUrl($id, $urlId) |
||
| 1503 | { |
||
| 1504 | Database::delete( |
||
| 1505 | $this->access_url_rel_usergroup, |
||
| 1506 | [ |
||
| 1507 | 'access_url_id = ? AND usergroup_id = ? ' => [$urlId, $id], |
||
| 1508 | ] |
||
| 1509 | ); |
||
| 1510 | } |
||
| 1511 | |||
| 1512 | /** |
||
| 1513 | * @param $needle |
||
| 1514 | * |
||
| 1515 | * @return xajaxResponse |
||
| 1516 | */ |
||
| 1517 | public static function searchUserGroupAjax($needle) |
||
| 1518 | { |
||
| 1519 | $response = new xajaxResponse(); |
||
| 1520 | $return = ''; |
||
| 1521 | |||
| 1522 | if (!empty($needle)) { |
||
| 1523 | // xajax send utf8 datas... datas in db can be non-utf8 datas |
||
| 1524 | $charset = api_get_system_encoding(); |
||
| 1525 | $needle = api_convert_encoding($needle, $charset, 'utf-8'); |
||
| 1526 | $needle = Database::escape_string($needle); |
||
| 1527 | // search courses where username or firstname or lastname begins likes $needle |
||
| 1528 | $sql = 'SELECT id, name |
||
| 1529 | FROM '.Database::get_main_table(TABLE_USERGROUP).' u |
||
| 1530 | WHERE name LIKE "'.$needle.'%" |
||
| 1531 | ORDER BY name |
||
| 1532 | LIMIT 11'; |
||
| 1533 | $result = Database::query($sql); |
||
| 1534 | $i = 0; |
||
| 1535 | while ($data = Database::fetch_array($result)) { |
||
| 1536 | $i++; |
||
| 1537 | if ($i <= 10) { |
||
| 1538 | $return .= '<a |
||
| 1539 | href="javascript: void(0);" |
||
| 1540 | onclick="javascript: add_user_to_url(\''.addslashes($data['id']).'\',\''.addslashes($data['name']).' \')">'.$data['name'].' </a><br />'; |
||
| 1541 | } else { |
||
| 1542 | $return .= '...<br />'; |
||
| 1543 | } |
||
| 1544 | } |
||
| 1545 | } |
||
| 1546 | $response->addAssign('ajax_list_courses', 'innerHTML', api_utf8_encode($return)); |
||
| 1547 | |||
| 1548 | return $response; |
||
| 1549 | } |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * Get user list by usergroup. |
||
| 1553 | * |
||
| 1554 | * @param int $id |
||
| 1555 | * |
||
| 1556 | * @return array |
||
| 1557 | */ |
||
| 1558 | public function getUserListByUserGroup($id) |
||
| 1559 | { |
||
| 1560 | $id = (int) $id; |
||
| 1561 | $sql = "SELECT u.* FROM ".$this->table_user." u |
||
| 1562 | INNER JOIN ".$this->usergroup_rel_user_table." c |
||
| 1563 | ON c.user_id = u.id |
||
| 1564 | WHERE c.usergroup_id = $id" |
||
| 1565 | ; |
||
| 1566 | $result = Database::query($sql); |
||
| 1567 | |||
| 1568 | return Database::store_result($result); |
||
| 1569 | } |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * @param FormValidator $form |
||
| 1573 | * @param string $type |
||
| 1574 | * @param array $data |
||
| 1575 | */ |
||
| 1576 | public function setForm($form, $type = 'add', $data = []) |
||
| 1577 | { |
||
| 1578 | switch ($type) { |
||
| 1579 | case 'add': |
||
| 1580 | $header = get_lang('Add'); |
||
| 1581 | break; |
||
| 1582 | case 'edit': |
||
| 1583 | $header = get_lang('Edit'); |
||
| 1584 | break; |
||
| 1585 | } |
||
| 1586 | |||
| 1587 | $form->addElement('header', $header); |
||
| 1588 | |||
| 1589 | //Name |
||
| 1590 | $form->addElement('text', 'name', get_lang('Name'), ['maxlength' => 255]); |
||
| 1591 | $form->applyFilter('name', 'trim'); |
||
| 1592 | |||
| 1593 | $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 1594 | $form->addRule('name', '', 'maxlength', 255); |
||
| 1595 | |||
| 1596 | // Description |
||
| 1597 | $form->addHtmlEditor( |
||
| 1598 | 'description', |
||
| 1599 | get_lang('Description'), |
||
| 1600 | true, |
||
| 1601 | false, |
||
| 1602 | [ |
||
| 1603 | 'ToolbarSet' => 'Minimal', |
||
| 1604 | ] |
||
| 1605 | ); |
||
| 1606 | $form->applyFilter('description', 'trim'); |
||
| 1607 | |||
| 1608 | if ($this->showGroupTypeSetting) { |
||
| 1609 | $form->addElement( |
||
| 1610 | 'checkbox', |
||
| 1611 | 'group_type', |
||
| 1612 | null, |
||
| 1613 | get_lang('SocialGroup') |
||
| 1614 | ); |
||
| 1615 | } |
||
| 1616 | |||
| 1617 | // url |
||
| 1618 | $form->addElement('text', 'url', get_lang('Url')); |
||
| 1619 | $form->applyFilter('url', 'trim'); |
||
| 1620 | |||
| 1621 | // Picture |
||
| 1622 | //$allowed_picture_types = $this->getAllowedPictureExtensions(); |
||
| 1623 | |||
| 1624 | // Picture |
||
| 1625 | $form->addFile( |
||
| 1626 | 'picture', |
||
| 1627 | get_lang('AddPicture'), |
||
| 1628 | ['id' => 'picture', 'class' => 'picture-form', 'crop_image' => true, 'crop_ratio' => '1 / 1'] |
||
| 1629 | ); |
||
| 1630 | |||
| 1631 | if (isset($data['picture']) && strlen($data['picture']) > 0) { |
||
| 1632 | $picture = $this->get_picture_group($data['id'], $data['picture'], 80); |
||
| 1633 | $img = '<img src="'.$picture.'" />'; |
||
| 1634 | $form->addElement('label', null, $img); |
||
| 1635 | $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage')); |
||
| 1636 | } |
||
| 1637 | |||
| 1638 | $form->addElement('select', 'visibility', get_lang('GroupPermissions'), $this->getGroupStatusList()); |
||
| 1639 | $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>'); |
||
| 1640 | $form->addElement('checkbox', 'allow_members_leave_group', '', get_lang('AllowMemberLeaveGroup')); |
||
| 1641 | |||
| 1642 | // Setting the form elements |
||
| 1643 | if ($type === 'add') { |
||
| 1644 | $form->addButtonCreate($header); |
||
| 1645 | } else { |
||
| 1646 | $form->addButtonUpdate($header); |
||
| 1647 | } |
||
| 1648 | } |
||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * Gets the current group image. |
||
| 1652 | * |
||
| 1653 | * @param string $id group id |
||
| 1654 | * @param string picture group name |
||
| 1655 | * @param string height |
||
| 1656 | * @param string picture size it can be small_, medium_ or big_ |
||
| 1657 | * @param string style css |
||
| 1658 | * |
||
| 1659 | * @return array with the file and the style of an image i.e $array['file'] $array['style'] |
||
| 1660 | */ |
||
| 1661 | public function get_picture_group( |
||
| 1662 | $id, |
||
| 1663 | $picture_file, |
||
| 1664 | $height, |
||
| 1665 | $size_picture = GROUP_IMAGE_SIZE_MEDIUM, |
||
| 1666 | $style = '' |
||
| 1667 | ) { |
||
| 1668 | $picture = null; |
||
| 1669 | //$picture['style'] = $style; |
||
| 1670 | if ($picture_file === 'unknown.jpg') { |
||
| 1671 | $picture = Display::returnIconPath($picture_file); |
||
| 1672 | |||
| 1673 | return $picture; |
||
| 1674 | } |
||
| 1675 | |||
| 1676 | switch ($size_picture) { |
||
| 1677 | case GROUP_IMAGE_SIZE_ORIGINAL: |
||
| 1678 | $size_picture = ''; |
||
| 1679 | break; |
||
| 1680 | case GROUP_IMAGE_SIZE_BIG: |
||
| 1681 | $size_picture = 'big_'; |
||
| 1682 | break; |
||
| 1683 | case GROUP_IMAGE_SIZE_MEDIUM: |
||
| 1684 | $size_picture = 'medium_'; |
||
| 1685 | break; |
||
| 1686 | case GROUP_IMAGE_SIZE_SMALL: |
||
| 1687 | $size_picture = 'small_'; |
||
| 1688 | break; |
||
| 1689 | default: |
||
| 1690 | $size_picture = 'medium_'; |
||
| 1691 | } |
||
| 1692 | |||
| 1693 | $image_array_sys = $this->get_group_picture_path_by_id($id, 'system', false, true); |
||
| 1694 | $image_array = $this->get_group_picture_path_by_id($id, 'web', false, true); |
||
| 1695 | $file = $image_array_sys['dir'].$size_picture.$picture_file; |
||
| 1696 | if (file_exists($file)) { |
||
| 1697 | $picture = $image_array['dir'].$size_picture.$picture_file; |
||
| 1698 | //$picture['style'] = ''; |
||
| 1699 | if ($height > 0) { |
||
| 1700 | $dimension = api_getimagesize($picture); |
||
| 1701 | $margin = ($height - $dimension['width']) / 2; |
||
| 1702 | //@ todo the padding-top should not be here |
||
| 1703 | } |
||
| 1704 | } else { |
||
| 1705 | $file = $image_array_sys['dir'].$picture_file; |
||
| 1706 | if (file_exists($file) && !is_dir($file)) { |
||
| 1707 | $picture = $image_array['dir'].$picture_file; |
||
| 1708 | } else { |
||
| 1709 | $picture = Display::returnIconPath('group_na.png', 64); |
||
| 1710 | } |
||
| 1711 | } |
||
| 1712 | |||
| 1713 | return $picture; |
||
| 1714 | } |
||
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Gets the group picture URL or path from group ID (returns an array). |
||
| 1718 | * The return format is a complete path, enabling recovery of the directory |
||
| 1719 | * with dirname() or the file with basename(). This also works for the |
||
| 1720 | * functions dealing with the user's productions, as they are located in |
||
| 1721 | * the same directory. |
||
| 1722 | * |
||
| 1723 | * @param int User ID |
||
| 1724 | * @param string Type of path to return (can be 'none', 'system', 'rel', 'web') |
||
| 1725 | * @param bool Whether we want to have the directory name returned 'as if' |
||
| 1726 | * there was a file or not (in the case we want to know which directory to create - |
||
| 1727 | * otherwise no file means no split subdir) |
||
| 1728 | * @param bool If we want that the function returns the /main/img/unknown.jpg image set it at true |
||
| 1729 | * |
||
| 1730 | * @return array Array of 2 elements: 'dir' and 'file' which contain the dir |
||
| 1731 | * and file as the name implies if image does not exist it will return the unknown |
||
| 1732 | * image if anonymous parameter is true if not it returns an empty er's |
||
| 1733 | */ |
||
| 1734 | public function get_group_picture_path_by_id($id, $type = 'none', $preview = false, $anonymous = false) |
||
| 1735 | { |
||
| 1736 | switch ($type) { |
||
| 1737 | case 'system': // Base: absolute system path. |
||
| 1738 | $base = api_get_path(SYS_UPLOAD_PATH); |
||
| 1739 | break; |
||
| 1740 | case 'rel': // Base: semi-absolute web path (no server base). |
||
| 1741 | $base = api_get_path(REL_CODE_PATH); |
||
| 1742 | break; |
||
| 1743 | case 'web': // Base: absolute web path. |
||
| 1744 | $base = api_get_path(WEB_UPLOAD_PATH); |
||
| 1745 | break; |
||
| 1746 | case 'none': |
||
| 1747 | default: // Base: empty, the result path below will be relative. |
||
| 1748 | $base = ''; |
||
| 1749 | } |
||
| 1750 | $id = (int) $id; |
||
| 1751 | |||
| 1752 | if (empty($id) || empty($type)) { |
||
| 1753 | return $anonymous ? ['dir' => $base.'img/', 'file' => 'unknown.jpg'] : ['dir' => '', 'file' => '']; |
||
| 1754 | } |
||
| 1755 | |||
| 1756 | $group_table = Database::get_main_table(TABLE_USERGROUP); |
||
| 1757 | $sql = "SELECT picture FROM $group_table WHERE id = ".$id; |
||
| 1758 | $res = Database::query($sql); |
||
| 1759 | |||
| 1760 | if (!Database::num_rows($res)) { |
||
| 1761 | return $anonymous ? ['dir' => $base.'img/', 'file' => 'unknown.jpg'] : ['dir' => '', 'file' => '']; |
||
| 1762 | } |
||
| 1763 | $user = Database::fetch_array($res); |
||
| 1764 | $picture_filename = trim($user['picture']); |
||
| 1765 | |||
| 1766 | if (api_get_setting('split_users_upload_directory') === 'true') { |
||
| 1767 | if (!empty($picture_filename)) { |
||
| 1768 | $dir = $base.'groups/'.substr($picture_filename, 0, 1).'/'.$id.'/'; |
||
| 1769 | } elseif ($preview) { |
||
| 1770 | $dir = $base.'groups/'.substr((string) $id, 0, 1).'/'.$id.'/'; |
||
| 1771 | } else { |
||
| 1772 | $dir = $base.'groups/'.$id.'/'; |
||
| 1773 | } |
||
| 1774 | } else { |
||
| 1775 | $dir = $base.'groups/'.$id.'/'; |
||
| 1776 | } |
||
| 1777 | |||
| 1778 | return ['dir' => $dir, 'file' => $picture_filename]; |
||
| 1779 | } |
||
| 1780 | |||
| 1781 | /** |
||
| 1782 | * @return array |
||
| 1783 | */ |
||
| 1784 | public function getAllowedPictureExtensions() |
||
| 1785 | { |
||
| 1786 | return ['jpg', 'jpeg', 'png', 'gif']; |
||
| 1787 | } |
||
| 1788 | |||
| 1789 | /** |
||
| 1790 | * @return array |
||
| 1791 | */ |
||
| 1792 | public function getGroupStatusList() |
||
| 1793 | { |
||
| 1794 | $status = [ |
||
| 1795 | GROUP_PERMISSION_OPEN => get_lang('Open'), |
||
| 1796 | GROUP_PERMISSION_CLOSED => get_lang('Closed'), |
||
| 1797 | ]; |
||
| 1798 | |||
| 1799 | return $status; |
||
| 1800 | } |
||
| 1801 | |||
| 1802 | /** |
||
| 1803 | * @param int $type |
||
| 1804 | */ |
||
| 1805 | public function setGroupType($type) |
||
| 1806 | { |
||
| 1807 | $this->groupType = (int) $type; |
||
| 1808 | } |
||
| 1809 | |||
| 1810 | /** |
||
| 1811 | * @param int $group_id |
||
| 1812 | * @param int $user_id |
||
| 1813 | * |
||
| 1814 | * @return bool |
||
| 1815 | */ |
||
| 1816 | public function is_group_admin($group_id, $user_id = 0) |
||
| 1817 | { |
||
| 1818 | if (empty($user_id)) { |
||
| 1819 | $user_id = api_get_user_id(); |
||
| 1820 | } |
||
| 1821 | $user_role = $this->get_user_group_role($user_id, $group_id); |
||
| 1822 | if (in_array($user_role, [GROUP_USER_PERMISSION_ADMIN])) { |
||
| 1823 | return true; |
||
| 1824 | } else { |
||
| 1825 | return false; |
||
| 1826 | } |
||
| 1827 | } |
||
| 1828 | |||
| 1829 | /** |
||
| 1830 | * @param int $group_id |
||
| 1831 | * @param int $user_id |
||
| 1832 | * |
||
| 1833 | * @return bool |
||
| 1834 | */ |
||
| 1835 | public function isGroupModerator($group_id, $user_id = 0) |
||
| 1836 | { |
||
| 1837 | if (empty($user_id)) { |
||
| 1838 | $user_id = api_get_user_id(); |
||
| 1839 | } |
||
| 1840 | $user_role = $this->get_user_group_role($user_id, $group_id); |
||
| 1841 | if (in_array($user_role, [GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_MODERATOR])) { |
||
| 1842 | return true; |
||
| 1843 | } else { |
||
| 1844 | return false; |
||
| 1845 | } |
||
| 1846 | } |
||
| 1847 | |||
| 1848 | /** |
||
| 1849 | * @param int $group_id |
||
| 1850 | * @param int $user_id |
||
| 1851 | * |
||
| 1852 | * @return bool |
||
| 1853 | */ |
||
| 1854 | public function is_group_member($group_id, $user_id = 0) |
||
| 1873 | } |
||
| 1874 | } |
||
| 1875 | |||
| 1876 | /** |
||
| 1877 | * Gets the relationship between a group and a User. |
||
| 1878 | * |
||
| 1879 | * @author Julio Montoya |
||
| 1880 | * |
||
| 1881 | * @param int $user_id |
||
| 1882 | * @param int $group_id |
||
| 1883 | * |
||
| 1884 | * @return int 0 if there are not relationship otherwise returns the user group |
||
| 1885 | * */ |
||
| 1886 | public function get_user_group_role($user_id, $group_id) |
||
| 1887 | { |
||
| 1888 | $table_group_rel_user = $this->usergroup_rel_user_table; |
||
| 1889 | $return_value = 0; |
||
| 1890 | if (!empty($user_id) && !empty($group_id)) { |
||
| 1891 | $sql = "SELECT relation_type FROM $table_group_rel_user |
||
| 1892 | WHERE |
||
| 1893 | usergroup_id = ".intval($group_id)." AND |
||
| 1894 | user_id = ".intval($user_id)." "; |
||
| 1895 | $result = Database::query($sql); |
||
| 1896 | if (Database::num_rows($result) > 0) { |
||
| 1897 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 1898 | $return_value = $row['relation_type']; |
||
| 1899 | } |
||
| 1900 | } |
||
| 1901 | |||
| 1902 | return $return_value; |
||
| 1903 | } |
||
| 1904 | |||
| 1905 | /** |
||
| 1906 | * @param int $userId |
||
| 1907 | * @param int $groupId |
||
| 1908 | * |
||
| 1909 | * @return string |
||
| 1910 | */ |
||
| 1911 | public function getUserRoleToString($userId, $groupId) |
||
| 1912 | { |
||
| 1913 | $role = self::get_user_group_role($userId, $groupId); |
||
| 1914 | $roleToString = ''; |
||
| 1915 | |||
| 1916 | switch ($role) { |
||
| 1917 | case GROUP_USER_PERMISSION_ADMIN: |
||
| 1918 | $roleToString = get_lang('Admin'); |
||
| 1919 | break; |
||
| 1920 | case GROUP_USER_PERMISSION_READER: |
||
| 1921 | $roleToString = get_lang('Reader'); |
||
| 1922 | break; |
||
| 1923 | case GROUP_USER_PERMISSION_PENDING_INVITATION: |
||
| 1924 | $roleToString = get_lang('PendingInvitation'); |
||
| 1925 | break; |
||
| 1926 | case GROUP_USER_PERMISSION_MODERATOR: |
||
| 1927 | $roleToString = get_lang('Moderator'); |
||
| 1928 | break; |
||
| 1929 | case GROUP_USER_PERMISSION_HRM: |
||
| 1930 | $roleToString = get_lang('Drh'); |
||
| 1931 | break; |
||
| 1932 | } |
||
| 1933 | |||
| 1934 | return $roleToString; |
||
| 1935 | } |
||
| 1936 | |||
| 1937 | /** |
||
| 1938 | * Add a group of users into a group of URLs. |
||
| 1939 | * |
||
| 1940 | * @author Julio Montoya |
||
| 1941 | * |
||
| 1942 | * @param array $user_list |
||
| 1943 | * @param array $group_list |
||
| 1944 | * @param int $relation_type |
||
| 1945 | * |
||
| 1946 | * @return array |
||
| 1947 | */ |
||
| 1948 | public function add_users_to_groups($user_list, $group_list, $relation_type = GROUP_USER_PERMISSION_READER) |
||
| 1949 | { |
||
| 1950 | $table_url_rel_group = $this->usergroup_rel_user_table; |
||
| 1951 | $result_array = []; |
||
| 1952 | $relation_type = (int) $relation_type; |
||
| 1953 | |||
| 1954 | if (is_array($user_list) && is_array($group_list)) { |
||
| 1955 | foreach ($group_list as $group_id) { |
||
| 1956 | foreach ($user_list as $user_id) { |
||
| 1957 | $role = self::get_user_group_role($user_id, $group_id); |
||
| 1958 | if ($role == 0) { |
||
| 1959 | $sql = "INSERT INTO $table_url_rel_group |
||
| 1960 | SET |
||
| 1961 | user_id = ".intval($user_id).", |
||
| 1962 | usergroup_id = ".intval($group_id).", |
||
| 1963 | relation_type = ".$relation_type; |
||
| 1964 | |||
| 1965 | $result = Database::query($sql); |
||
| 1966 | if ($result) { |
||
| 1967 | $result_array[$group_id][$user_id] = 1; |
||
| 1968 | } else { |
||
| 1969 | $result_array[$group_id][$user_id] = 0; |
||
| 1970 | } |
||
| 1971 | } |
||
| 1972 | } |
||
| 1973 | } |
||
| 1974 | } |
||
| 1975 | |||
| 1976 | return $result_array; |
||
| 1977 | } |
||
| 1978 | |||
| 1979 | /** |
||
| 1980 | * Deletes an url and session relationship. |
||
| 1981 | * |
||
| 1982 | * @author Julio Montoya |
||
| 1983 | * |
||
| 1984 | * @param int $userId |
||
| 1985 | * @param int $groupId |
||
| 1986 | * |
||
| 1987 | * @return bool true if success |
||
| 1988 | * */ |
||
| 1989 | public function delete_user_rel_group($userId, $groupId) |
||
| 1990 | { |
||
| 1991 | $userId = (int) $userId; |
||
| 1992 | $groupId = (int) $groupId; |
||
| 1993 | if (empty($userId) || empty($groupId)) { |
||
| 1994 | return false; |
||
| 1995 | } |
||
| 1996 | |||
| 1997 | $table = $this->usergroup_rel_user_table; |
||
| 1998 | $sql = "DELETE FROM $table |
||
| 1999 | WHERE |
||
| 2000 | user_id = $userId AND |
||
| 2001 | usergroup_id = $groupId"; |
||
| 2002 | |||
| 2003 | $result = Database::query($sql); |
||
| 2004 | |||
| 2005 | return $result; |
||
| 2006 | } |
||
| 2007 | |||
| 2008 | /** |
||
| 2009 | * Add a user into a group. |
||
| 2010 | * |
||
| 2011 | * @author Julio Montoya |
||
| 2012 | * |
||
| 2013 | * @param int $user_id |
||
| 2014 | * @param int $group_id |
||
| 2015 | * @param int $relation_type |
||
| 2016 | * |
||
| 2017 | * @return bool true if success |
||
| 2018 | */ |
||
| 2019 | public function add_user_to_group($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER) |
||
| 2020 | { |
||
| 2021 | $table_url_rel_group = $this->usergroup_rel_user_table; |
||
| 2022 | if (!empty($user_id) && !empty($group_id)) { |
||
| 2023 | $role = self::get_user_group_role($user_id, $group_id); |
||
| 2024 | |||
| 2025 | if ($role == 0) { |
||
| 2026 | $sql = "INSERT INTO $table_url_rel_group |
||
| 2027 | SET |
||
| 2028 | user_id = ".intval($user_id).", |
||
| 2029 | usergroup_id = ".intval($group_id).", |
||
| 2030 | relation_type = ".intval($relation_type); |
||
| 2031 | Database::query($sql); |
||
| 2032 | } elseif ($role == GROUP_USER_PERMISSION_PENDING_INVITATION) { |
||
| 2033 | //if somebody already invited me I can be added |
||
| 2034 | self::update_user_role($user_id, $group_id, GROUP_USER_PERMISSION_READER); |
||
| 2035 | } |
||
| 2036 | } |
||
| 2037 | |||
| 2038 | return true; |
||
| 2039 | } |
||
| 2040 | |||
| 2041 | /** |
||
| 2042 | * Updates the group_rel_user table with a given user and group ids. |
||
| 2043 | * |
||
| 2044 | * @author Julio Montoya |
||
| 2045 | * |
||
| 2046 | * @param int $user_id |
||
| 2047 | * @param int $group_id |
||
| 2048 | * @param int $relation_type |
||
| 2049 | */ |
||
| 2050 | public function update_user_role($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER) |
||
| 2060 | } |
||
| 2061 | |||
| 2062 | /** |
||
| 2063 | * Gets the inner join from users and group table. |
||
| 2064 | * |
||
| 2065 | * @return array Database::store_result of the result |
||
| 2066 | * |
||
| 2067 | * @author Julio Montoya |
||
| 2068 | * */ |
||
| 2069 | public function get_groups_by_user($user_id, $relation_type = GROUP_USER_PERMISSION_READER, $with_image = false) |
||
| 2070 | { |
||
| 2071 | $table_group_rel_user = $this->usergroup_rel_user_table; |
||
| 2072 | $tbl_group = $this->table; |
||
| 2073 | $user_id = (int) $user_id; |
||
| 2074 | |||
| 2075 | if ($relationType == 0) { |
||
| 2076 | $relationCondition = ''; |
||
| 2077 | } else { |
||
| 2078 | if (is_array($relationType)) { |
||
| 2079 | $relationType = array_map('intval', $relationType); |
||
| 2080 | $relationType = implode("','", $relationType); |
||
| 2081 | $relationCondition = " AND ( gu.relation_type IN ('$relationType')) "; |
||
| 2082 | } else { |
||
| 2083 | $relationType = (int) $relationType; |
||
| 2084 | $relationCondition = " AND gu.relation_type = $relationType "; |
||
| 2085 | } |
||
| 2086 | } |
||
| 2087 | |||
| 2088 | $sql = "SELECT |
||
| 2089 | g.picture, |
||
| 2090 | g.name, |
||
| 2091 | g.description, |
||
| 2092 | g.id , |
||
| 2093 | gu.relation_type |
||
| 2094 | FROM $tbl_group g |
||
| 2095 | INNER JOIN $table_group_rel_user gu |
||
| 2096 | ON gu.usergroup_id = g.id |
||
| 2097 | WHERE |
||
| 2098 | g.group_type = ".self::SOCIAL_CLASS." AND |
||
| 2099 | gu.user_id = $user_id |
||
| 2100 | $relationCondition |
||
| 2101 | ORDER BY created_at DESC "; |
||
| 2102 | $result = Database::query($sql); |
||
| 2103 | |||
| 2104 | $array = []; |
||
| 2105 | if (Database::num_rows($result) > 0) { |
||
| 2106 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 2107 | if ($with_image) { |
||
| 2108 | $picture = self::get_picture_group($row['id'], $row['picture'], 80); |
||
| 2109 | $img = '<img src="'.$picture['file'].'" />'; |
||
| 2110 | $row['picture'] = $img; |
||
| 2111 | } |
||
| 2112 | $array[$row['id']] = $row; |
||
| 2113 | } |
||
| 2114 | } |
||
| 2115 | |||
| 2116 | return $array; |
||
| 2117 | } |
||
| 2118 | |||
| 2119 | /** Gets the inner join of users and group table |
||
| 2120 | * @param int quantity of records |
||
| 2121 | * @param bool show groups with image or not |
||
| 2122 | * |
||
| 2123 | * @return array with group content |
||
| 2124 | * |
||
| 2125 | * @author Julio Montoya |
||
| 2126 | * */ |
||
| 2127 | public function get_groups_by_popularity($num = 6, $with_image = true) |
||
| 2128 | { |
||
| 2129 | $table_group_rel_user = $this->usergroup_rel_user_table; |
||
| 2130 | $tbl_group = $this->table; |
||
| 2131 | if (empty($num)) { |
||
| 2132 | $num = 6; |
||
| 2133 | } else { |
||
| 2134 | $num = intval($num); |
||
| 2135 | } |
||
| 2136 | // only show admins and readers |
||
| 2137 | $where_relation_condition = " WHERE g.group_type = ".self::SOCIAL_CLASS." AND |
||
| 2138 | gu.relation_type IN ('".GROUP_USER_PERMISSION_ADMIN."' , '".GROUP_USER_PERMISSION_READER."', '".GROUP_USER_PERMISSION_HRM."') "; |
||
| 2139 | $sql = "SELECT DISTINCT count(user_id) as count, g.picture, g.name, g.description, g.id |
||
| 2140 | FROM $tbl_group g |
||
| 2141 | INNER JOIN $table_group_rel_user gu |
||
| 2142 | ON gu.usergroup_id = g.id $where_relation_condition |
||
| 2143 | GROUP BY g.id |
||
| 2144 | ORDER BY count DESC |
||
| 2145 | LIMIT $num"; |
||
| 2146 | |||
| 2147 | $result = Database::query($sql); |
||
| 2148 | $array = []; |
||
| 2149 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 2150 | $description = Security::remove_XSS($row['description'], STUDENT, true); |
||
| 2151 | $row['description'] = cut($description, 250, true); |
||
| 2152 | $row['name'] = Security::remove_XSS($row['name'], STUDENT, true); |
||
| 2153 | $row['url'] = "group_view.php?id=".$row['id']; |
||
| 2154 | if ($with_image) { |
||
| 2155 | $picture = self::get_picture_group( |
||
| 2156 | $row['id'], |
||
| 2157 | $row['picture'], |
||
| 2158 | null, |
||
| 2159 | GROUP_IMAGE_SIZE_MEDIUM |
||
| 2160 | ); |
||
| 2161 | $row['picture'] = $picture; |
||
| 2162 | } |
||
| 2163 | if (empty($row['id'])) { |
||
| 2164 | continue; |
||
| 2165 | } |
||
| 2166 | $array[$row['id']] = $row; |
||
| 2167 | } |
||
| 2168 | |||
| 2169 | return $array; |
||
| 2170 | } |
||
| 2171 | |||
| 2172 | /** Gets the last groups created |
||
| 2173 | * @param int $num quantity of records |
||
| 2174 | * @param bool $with_image show groups with image or not |
||
| 2175 | * |
||
| 2176 | * @return array with group content |
||
| 2177 | * |
||
| 2178 | * @author Julio Montoya |
||
| 2179 | * */ |
||
| 2180 | public function get_groups_by_age($num = 6, $with_image = true) |
||
| 2181 | { |
||
| 2182 | $table_group_rel_user = $this->usergroup_rel_user_table; |
||
| 2183 | $tbl_group = $this->table; |
||
| 2184 | |||
| 2185 | if (empty($num)) { |
||
| 2186 | $num = 6; |
||
| 2187 | } else { |
||
| 2188 | $num = intval($num); |
||
| 2189 | } |
||
| 2190 | |||
| 2191 | $where = " WHERE |
||
| 2192 | g.group_type = ".self::SOCIAL_CLASS." AND |
||
| 2193 | gu.relation_type IN |
||
| 2194 | ('".GROUP_USER_PERMISSION_ADMIN."' , |
||
| 2195 | '".GROUP_USER_PERMISSION_READER."', |
||
| 2196 | '".GROUP_USER_PERMISSION_MODERATOR."', |
||
| 2197 | '".GROUP_USER_PERMISSION_HRM."') |
||
| 2198 | "; |
||
| 2199 | $sql = "SELECT DISTINCT |
||
| 2200 | count(user_id) as count, |
||
| 2201 | g.picture, |
||
| 2202 | g.name, |
||
| 2203 | g.description, |
||
| 2204 | g.id |
||
| 2205 | FROM $tbl_group g |
||
| 2206 | INNER JOIN $table_group_rel_user gu |
||
| 2207 | ON gu.usergroup_id = g.id |
||
| 2208 | $where |
||
| 2209 | GROUP BY g.id |
||
| 2210 | ORDER BY created_at DESC |
||
| 2211 | LIMIT $num "; |
||
| 2212 | |||
| 2213 | $result = Database::query($sql); |
||
| 2214 | $array = []; |
||
| 2215 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 2216 | $description = Security::remove_XSS($row['description'], STUDENT, true); |
||
| 2217 | $row['description'] = cut($description, 250, true); |
||
| 2218 | $row['name'] = Security::remove_XSS($row['name'], STUDENT, true); |
||
| 2219 | $row['url'] = "group_view.php?id=".$row['id']; |
||
| 2220 | if ($with_image) { |
||
| 2221 | $picture = self::get_picture_group( |
||
| 2222 | $row['id'], |
||
| 2223 | $row['picture'], |
||
| 2224 | null, |
||
| 2225 | GROUP_IMAGE_SIZE_MEDIUM |
||
| 2226 | ); |
||
| 2227 | $row['picture'] = $picture; |
||
| 2228 | } |
||
| 2229 | if (empty($row['id'])) { |
||
| 2230 | continue; |
||
| 2231 | } |
||
| 2232 | $array[$row['id']] = $row; |
||
| 2233 | } |
||
| 2234 | |||
| 2235 | return $array; |
||
| 2236 | } |
||
| 2237 | |||
| 2238 | /** |
||
| 2239 | * Gets the group's members. |
||
| 2240 | * |
||
| 2241 | * @param int group id |
||
| 2242 | * @param bool show image or not of the group |
||
| 2243 | * @param array list of relation type use constants |
||
| 2244 | * @param int from value |
||
| 2245 | * @param int limit |
||
| 2246 | * @param array image configuration, i.e array('height'=>'20px', 'size'=> '20px') |
||
| 2247 | * |
||
| 2248 | * @return array list of users in a group |
||
| 2249 | */ |
||
| 2250 | public function get_users_by_group( |
||
| 2251 | $group_id, |
||
| 2252 | $with_image = false, |
||
| 2253 | $relation_type = [], |
||
| 2254 | $from = null, |
||
| 2255 | $limit = null, |
||
| 2256 | $image_conf = ['size' => USER_IMAGE_SIZE_MEDIUM, 'height' => 80] |
||
| 2257 | ) { |
||
| 2258 | $table_group_rel_user = $this->usergroup_rel_user_table; |
||
| 2259 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 2260 | $group_id = intval($group_id); |
||
| 2261 | |||
| 2262 | if (empty($group_id)) { |
||
| 2263 | return []; |
||
| 2264 | } |
||
| 2265 | |||
| 2266 | $limit_text = ''; |
||
| 2267 | if (isset($from) && isset($limit)) { |
||
| 2268 | $from = intval($from); |
||
| 2269 | $limit = intval($limit); |
||
| 2270 | $limit_text = "LIMIT $from, $limit"; |
||
| 2271 | } |
||
| 2272 | |||
| 2273 | if (count($relation_type) == 0) { |
||
| 2274 | $where_relation_condition = ''; |
||
| 2275 | } else { |
||
| 2276 | $new_relation_type = []; |
||
| 2277 | foreach ($relation_type as $rel) { |
||
| 2278 | $rel = intval($rel); |
||
| 2279 | $new_relation_type[] = "'$rel'"; |
||
| 2280 | } |
||
| 2281 | $relation_type = implode(',', $new_relation_type); |
||
| 2282 | if (!empty($relation_type)) { |
||
| 2283 | $where_relation_condition = "AND gu.relation_type IN ($relation_type) "; |
||
| 2284 | } |
||
| 2285 | } |
||
| 2286 | |||
| 2287 | $sql = "SELECT picture_uri as image, u.id, CONCAT (u.firstname,' ', u.lastname) as fullname, relation_type |
||
| 2288 | FROM $tbl_user u |
||
| 2289 | INNER JOIN $table_group_rel_user gu |
||
| 2290 | ON (gu.user_id = u.id) |
||
| 2291 | WHERE |
||
| 2292 | gu.usergroup_id= $group_id |
||
| 2293 | $where_relation_condition |
||
| 2294 | ORDER BY relation_type, firstname |
||
| 2295 | $limit_text"; |
||
| 2296 | |||
| 2297 | $result = Database::query($sql); |
||
| 2298 | $array = []; |
||
| 2299 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 2300 | if ($with_image) { |
||
| 2301 | $userInfo = api_get_user_info($row['id']); |
||
| 2302 | $userPicture = UserManager::getUserPicture($row['id']); |
||
| 2303 | |||
| 2304 | $row['image'] = '<img src="'.$userPicture.'" />'; |
||
| 2305 | $row['user_info'] = $userInfo; |
||
| 2306 | } |
||
| 2307 | $array[$row['id']] = $row; |
||
| 2308 | } |
||
| 2309 | |||
| 2310 | return $array; |
||
| 2311 | } |
||
| 2312 | |||
| 2313 | /** |
||
| 2314 | * Gets all the members of a group no matter the relationship for |
||
| 2315 | * more specifications use get_users_by_group. |
||
| 2316 | * |
||
| 2317 | * @param int group id |
||
| 2318 | * |
||
| 2319 | * @return array |
||
| 2320 | */ |
||
| 2321 | public function get_all_users_by_group($group_id) |
||
| 2322 | { |
||
| 2323 | $table_group_rel_user = $this->usergroup_rel_user_table; |
||
| 2324 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 2325 | $group_id = intval($group_id); |
||
| 2326 | |||
| 2327 | if (empty($group_id)) { |
||
| 2328 | return []; |
||
| 2329 | } |
||
| 2330 | |||
| 2331 | $sql = "SELECT u.id, u.firstname, u.lastname, relation_type |
||
| 2332 | FROM $tbl_user u |
||
| 2333 | INNER JOIN $table_group_rel_user gu |
||
| 2334 | ON (gu.user_id = u.id) |
||
| 2335 | WHERE gu.usergroup_id= $group_id |
||
| 2336 | ORDER BY relation_type, firstname"; |
||
| 2337 | |||
| 2338 | $result = Database::query($sql); |
||
| 2339 | $array = []; |
||
| 2340 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 2341 | $array[$row['id']] = $row; |
||
| 2342 | } |
||
| 2343 | |||
| 2344 | return $array; |
||
| 2345 | } |
||
| 2346 | |||
| 2347 | /** |
||
| 2348 | * Shows the left column of the group page. |
||
| 2349 | * |
||
| 2350 | * @param int group id |
||
| 2351 | * @param int user id |
||
| 2352 | * |
||
| 2353 | * @return string |
||
| 2354 | */ |
||
| 2355 | public function show_group_column_information($group_id, $user_id, $show = '') |
||
| 2356 | { |
||
| 2357 | $html = ''; |
||
| 2358 | $group_info = $this->get($group_id); |
||
| 2359 | |||
| 2360 | //my relation with the group is set here |
||
| 2361 | $my_group_role = self::get_user_group_role($user_id, $group_id); |
||
| 2362 | |||
| 2363 | // Loading group permission |
||
| 2364 | $links = ''; |
||
| 2365 | switch ($my_group_role) { |
||
| 2366 | case GROUP_USER_PERMISSION_READER: |
||
| 2367 | // I'm just a reader |
||
| 2368 | $relation_group_title = get_lang('IAmAReader'); |
||
| 2369 | $links .= '<li class="'.($show == 'invite_friends' ? 'active' : '').'"><a href="group_invitation.php?id='.$group_id.'">'. |
||
| 2370 | Display::return_icon('invitation_friend.png', get_lang('InviteFriends')).get_lang('InviteFriends').'</a></li>'; |
||
| 2371 | if (self::canLeave($group_info)) { |
||
| 2372 | $links .= '<li><a href="group_view.php?id='.$group_id.'&action=leave&u='.api_get_user_id().'">'. |
||
| 2373 | Display::return_icon('group_leave.png', get_lang('LeaveGroup')).get_lang('LeaveGroup').'</a></li>'; |
||
| 2374 | } |
||
| 2375 | break; |
||
| 2376 | case GROUP_USER_PERMISSION_ADMIN: |
||
| 2377 | $relation_group_title = get_lang('IAmAnAdmin'); |
||
| 2378 | $links .= '<li class="'.($show == 'group_edit' ? 'active' : '').'"><a href="group_edit.php?id='.$group_id.'">'. |
||
| 2379 | Display::return_icon('group_edit.png', get_lang('EditGroup')).get_lang('EditGroup').'</a></li>'; |
||
| 2380 | $links .= '<li class="'.($show == 'member_list' ? 'active' : '').'"><a href="group_waiting_list.php?id='.$group_id.'">'. |
||
| 2381 | Display::return_icon('waiting_list.png', get_lang('WaitingList')).get_lang('WaitingList').'</a></li>'; |
||
| 2382 | $links .= '<li class="'.($show == 'invite_friends' ? 'active' : '').'"><a href="group_invitation.php?id='.$group_id.'">'. |
||
| 2383 | Display::return_icon('invitation_friend.png', get_lang('InviteFriends')).get_lang('InviteFriends').'</a></li>'; |
||
| 2384 | if (self::canLeave($group_info)) { |
||
| 2385 | $links .= '<li><a href="group_view.php?id='.$group_id.'&action=leave&u='.api_get_user_id().'">'. |
||
| 2386 | Display::return_icon('group_leave.png', get_lang('LeaveGroup')).get_lang('LeaveGroup').'</a></li>'; |
||
| 2387 | } |
||
| 2388 | break; |
||
| 2389 | case GROUP_USER_PERMISSION_PENDING_INVITATION: |
||
| 2390 | // $links .= '<li><a href="groups.php?id='.$group_id.'&action=join&u='.api_get_user_id().'">'.Display::return_icon('addd.gif', get_lang('YouHaveBeenInvitedJoinNow'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('YouHaveBeenInvitedJoinNow').'</span></a></li>'; |
||
| 2391 | break; |
||
| 2392 | case GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER: |
||
| 2393 | $relation_group_title = get_lang('WaitingForAdminResponse'); |
||
| 2394 | break; |
||
| 2395 | case GROUP_USER_PERMISSION_MODERATOR: |
||
| 2396 | $relation_group_title = get_lang('IAmAModerator'); |
||
| 2397 | //$links .= '<li><a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=400&width=610&&user_friend='.api_get_user_id().'&group_id='.$group_id.'&action=add_message_group" class="thickbox" title="'.get_lang('ComposeMessage').'">'.Display::return_icon('compose_message.png', get_lang('NewTopic'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('NewTopic').'</span></a></li>'; |
||
| 2398 | //$links .= '<li><a href="groups.php?id='.$group_id.'">'. Display::return_icon('message_list.png', get_lang('MessageList'), array('hspace'=>'6')).'<span class="'.($show=='messages_list'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('MessageList').'</span></a></li>'; |
||
| 2399 | //$links .= '<li><a href="group_members.php?id='.$group_id.'">'. Display::return_icon('member_list.png', get_lang('MemberList'), array('hspace'=>'6')).'<span class="'.($show=='member_list'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('MemberList').'</span></a></li>'; |
||
| 2400 | if ($group_info['visibility'] == GROUP_PERMISSION_CLOSED) { |
||
| 2401 | $links .= '<li><a href="group_waiting_list.php?id='.$group_id.'">'. |
||
| 2402 | Display::return_icon('waiting_list.png', get_lang('WaitingList')).get_lang('WaitingList').'</a></li>'; |
||
| 2403 | } |
||
| 2404 | $links .= '<li><a href="group_invitation.php?id='.$group_id.'">'. |
||
| 2405 | Display::return_icon('invitation_friend.png', get_lang('InviteFriends')).get_lang('InviteFriends').'</a></li>'; |
||
| 2406 | if (self::canLeave($group_info)) { |
||
| 2407 | $links .= '<li><a href="group_view.php?id='.$group_id.'&action=leave&u='.api_get_user_id().'">'. |
||
| 2408 | Display::return_icon('group_leave.png', get_lang('LeaveGroup')).get_lang('LeaveGroup').'</a></li>'; |
||
| 2409 | } |
||
| 2410 | break; |
||
| 2411 | case GROUP_USER_PERMISSION_HRM: |
||
| 2412 | $relation_group_title = get_lang('IAmAHRM'); |
||
| 2413 | $links .= '<li><a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=400&width=610&&user_friend='.api_get_user_id().'&group_id='.$group_id.'&action=add_message_group" class="ajax" title="'.get_lang('ComposeMessage').'" data-size="lg" data-title="'.get_lang('ComposeMessage').'">'. |
||
| 2414 | Display::return_icon('new-message.png', get_lang('NewTopic')).get_lang('NewTopic').'</a></li>'; |
||
| 2415 | $links .= '<li><a href="group_view.php?id='.$group_id.'">'. |
||
| 2416 | Display::return_icon('message_list.png', get_lang('MessageList')).get_lang('MessageList').'</a></li>'; |
||
| 2417 | $links .= '<li><a href="group_invitation.php?id='.$group_id.'">'. |
||
| 2418 | Display::return_icon('invitation_friend.png', get_lang('InviteFriends')).get_lang('InviteFriends').'</a></li>'; |
||
| 2419 | $links .= '<li><a href="group_members.php?id='.$group_id.'">'. |
||
| 2420 | Display::return_icon('member_list.png', get_lang('MemberList')).get_lang('MemberList').'</a></li>'; |
||
| 2421 | $links .= '<li><a href="group_view.php?id='.$group_id.'&action=leave&u='.api_get_user_id().'">'. |
||
| 2422 | Display::return_icon('delete_data.gif', get_lang('LeaveGroup')).get_lang('LeaveGroup').'</a></li>'; |
||
| 2423 | break; |
||
| 2424 | default: |
||
| 2425 | //$links .= '<li><a href="groups.php?id='.$group_id.'&action=join&u='.api_get_user_id().'">'.Display::return_icon('addd.gif', get_lang('JoinGroup'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('JoinGroup').'</a></span></li>'; |
||
| 2426 | break; |
||
| 2427 | } |
||
| 2428 | if (!empty($links)) { |
||
| 2429 | $list = '<ul class="nav nav-pills">'; |
||
| 2430 | $list .= $links; |
||
| 2431 | $list .= '</ul>'; |
||
| 2432 | $html .= Display::panelCollapse( |
||
| 2433 | get_lang('SocialGroups'), |
||
| 2434 | $list, |
||
| 2435 | 'sm-groups', |
||
| 2436 | [], |
||
| 2437 | 'groups-acordeon', |
||
| 2438 | 'groups-collapse' |
||
| 2439 | ); |
||
| 2440 | } |
||
| 2441 | |||
| 2442 | return $html; |
||
| 2443 | } |
||
| 2444 | |||
| 2445 | /** |
||
| 2446 | * @param int $group_id |
||
| 2447 | * @param int $topic_id |
||
| 2448 | */ |
||
| 2449 | public function delete_topic($group_id, $topic_id) |
||
| 2450 | { |
||
| 2451 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 2452 | $topic_id = intval($topic_id); |
||
| 2453 | $group_id = intval($group_id); |
||
| 2454 | $sql = "UPDATE $table_message SET |
||
| 2455 | msg_status = 3 |
||
| 2456 | WHERE |
||
| 2457 | group_id = $group_id AND |
||
| 2458 | (id = '$topic_id' OR parent_id = $topic_id) |
||
| 2459 | "; |
||
| 2460 | Database::query($sql); |
||
| 2461 | } |
||
| 2462 | |||
| 2463 | /** |
||
| 2464 | * @param string $user_id |
||
| 2465 | * @param string $relation_type |
||
| 2466 | * @param bool $with_image |
||
| 2467 | * |
||
| 2468 | * @return int |
||
| 2469 | */ |
||
| 2470 | public function get_groups_by_user_count( |
||
| 2471 | $user_id = '', |
||
| 2472 | $relation_type = GROUP_USER_PERMISSION_READER, |
||
| 2473 | $with_image = false |
||
| 2474 | ) { |
||
| 2475 | $table_group_rel_user = $this->usergroup_rel_user_table; |
||
| 2476 | $tbl_group = $this->table; |
||
| 2477 | $user_id = intval($user_id); |
||
| 2478 | |||
| 2479 | if ($relation_type == 0) { |
||
| 2480 | $where_relation_condition = ''; |
||
| 2481 | } else { |
||
| 2482 | $relation_type = intval($relation_type); |
||
| 2483 | $where_relation_condition = "AND gu.relation_type = $relation_type "; |
||
| 2484 | } |
||
| 2485 | |||
| 2486 | $sql = "SELECT count(g.id) as count |
||
| 2487 | FROM $tbl_group g |
||
| 2488 | INNER JOIN $table_group_rel_user gu |
||
| 2489 | ON gu.usergroup_id = g.id |
||
| 2490 | WHERE gu.user_id = $user_id $where_relation_condition "; |
||
| 2491 | |||
| 2492 | $result = Database::query($sql); |
||
| 2493 | if (Database::num_rows($result) > 0) { |
||
| 2494 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 2495 | |||
| 2496 | return $row['count']; |
||
| 2497 | } |
||
| 2498 | |||
| 2499 | return 0; |
||
| 2500 | } |
||
| 2501 | |||
| 2502 | /** |
||
| 2503 | * @param string $tag |
||
| 2504 | * @param int $from |
||
| 2505 | * @param int $number_of_items |
||
| 2506 | * |
||
| 2507 | * @return array |
||
| 2508 | */ |
||
| 2509 | public function get_all_group_tags($tag, $from = 0, $number_of_items = 10, $getCount = false) |
||
| 2510 | { |
||
| 2511 | $group_table = $this->table; |
||
| 2512 | $tag = Database::escape_string($tag); |
||
| 2513 | $from = intval($from); |
||
| 2514 | $number_of_items = intval($number_of_items); |
||
| 2515 | $return = []; |
||
| 2516 | |||
| 2517 | $keyword = $tag; |
||
| 2518 | $sql = "SELECT g.id, g.name, g.description, g.url, g.picture |
||
| 2519 | FROM $group_table g"; |
||
| 2520 | if (isset($keyword)) { |
||
| 2521 | $sql .= " WHERE ( |
||
| 2522 | g.name LIKE '%".$keyword."%' OR |
||
| 2523 | g.description LIKE '%".$keyword."%' OR |
||
| 2524 | g.url LIKE '%".$keyword."%' |
||
| 2525 | )"; |
||
| 2526 | } |
||
| 2527 | |||
| 2528 | $direction = 'ASC'; |
||
| 2529 | if (!in_array($direction, ['ASC', 'DESC'])) { |
||
| 2530 | $direction = 'ASC'; |
||
| 2531 | } |
||
| 2532 | |||
| 2533 | $from = intval($from); |
||
| 2534 | $number_of_items = intval($number_of_items); |
||
| 2535 | |||
| 2536 | $sql .= " LIMIT $from,$number_of_items"; |
||
| 2537 | |||
| 2538 | $res = Database::query($sql); |
||
| 2539 | if (Database::num_rows($res) > 0) { |
||
| 2540 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 2541 | if (!in_array($row['id'], $return)) { |
||
| 2542 | $return[$row['id']] = $row; |
||
| 2543 | } |
||
| 2544 | } |
||
| 2545 | } |
||
| 2546 | |||
| 2547 | return $return; |
||
| 2548 | } |
||
| 2549 | |||
| 2550 | /** |
||
| 2551 | * @param int $group_id |
||
| 2552 | * |
||
| 2553 | * @return array |
||
| 2554 | */ |
||
| 2555 | public static function get_parent_groups($group_id) |
||
| 2556 | { |
||
| 2557 | $t_rel_group = Database::get_main_table(TABLE_USERGROUP_REL_USERGROUP); |
||
| 2558 | $group_id = (int) $group_id; |
||
| 2559 | |||
| 2560 | $max_level = 10; |
||
| 2561 | $select_part = "SELECT "; |
||
| 2562 | $cond_part = ''; |
||
| 2563 | for ($i = 1; $i <= $max_level; $i++) { |
||
| 2564 | $g_number = $i; |
||
| 2565 | $rg_number = $i - 1; |
||
| 2566 | if ($i == $max_level) { |
||
| 2567 | $select_part .= "rg$rg_number.group_id as id_$rg_number "; |
||
| 2568 | } else { |
||
| 2569 | $select_part .= "rg$rg_number.group_id as id_$rg_number, "; |
||
| 2570 | } |
||
| 2571 | if ($i == 1) { |
||
| 2572 | $cond_part .= "FROM $t_rel_group rg0 LEFT JOIN $t_rel_group rg$i on rg$rg_number.group_id = rg$i.subgroup_id "; |
||
| 2573 | } else { |
||
| 2574 | $cond_part .= " LEFT JOIN $t_rel_group rg$i on rg$rg_number.group_id = rg$i.subgroup_id "; |
||
| 2575 | } |
||
| 2576 | } |
||
| 2577 | $sql = $select_part.' '.$cond_part."WHERE rg0.subgroup_id='$group_id'"; |
||
| 2578 | $res = Database::query($sql); |
||
| 2579 | $temp_arr = Database::fetch_array($res, 'NUM'); |
||
| 2580 | $toReturn = []; |
||
| 2581 | if (is_array($temp_arr)) { |
||
| 2582 | foreach ($temp_arr as $elt) { |
||
| 2583 | if (isset($elt)) { |
||
| 2584 | $toReturn[] = $elt; |
||
| 2585 | } |
||
| 2586 | } |
||
| 2587 | } |
||
| 2588 | |||
| 2589 | return $toReturn; |
||
| 2590 | } |
||
| 2591 | |||
| 2592 | /** |
||
| 2593 | * Get the group member list by a user and his group role. |
||
| 2594 | * |
||
| 2595 | * @param int $userId The user ID |
||
| 2596 | * @param int $relationType Optional. The relation type. GROUP_USER_PERMISSION_ADMIN by default |
||
| 2597 | * @param bool $includeSubgroupsUsers Optional. Whether include the users from subgroups |
||
| 2598 | * |
||
| 2599 | * @return array |
||
| 2600 | */ |
||
| 2601 | public function getGroupUsersByUser( |
||
| 2602 | $userId, |
||
| 2603 | $relationType = GROUP_USER_PERMISSION_ADMIN, |
||
| 2604 | $includeSubgroupsUsers = true |
||
| 2605 | ) { |
||
| 2606 | $userId = (int) $userId; |
||
| 2607 | $groups = $this->get_groups_by_user($userId, $relationType); |
||
| 2608 | $groupsId = array_keys($groups); |
||
| 2609 | $subgroupsId = []; |
||
| 2610 | $userIdList = []; |
||
| 2611 | |||
| 2612 | if ($includeSubgroupsUsers) { |
||
| 2613 | foreach ($groupsId as $groupId) { |
||
| 2614 | $subgroupsId = array_merge($subgroupsId, self::getGroupsByDepthLevel($groupId)); |
||
| 2615 | } |
||
| 2616 | |||
| 2617 | $groupsId = array_merge($groupsId, $subgroupsId); |
||
| 2618 | } |
||
| 2619 | |||
| 2620 | $groupsId = array_unique($groupsId); |
||
| 2621 | |||
| 2622 | if (empty($groupsId)) { |
||
| 2623 | return []; |
||
| 2624 | } |
||
| 2625 | |||
| 2626 | foreach ($groupsId as $groupId) { |
||
| 2627 | $groupUsers = self::get_users_by_group($groupId); |
||
| 2628 | |||
| 2629 | if (empty($groupUsers)) { |
||
| 2630 | continue; |
||
| 2631 | } |
||
| 2632 | |||
| 2633 | foreach ($groupUsers as $member) { |
||
| 2634 | if ($member['user_id'] == $userId) { |
||
| 2635 | continue; |
||
| 2636 | } |
||
| 2637 | |||
| 2638 | $userIdList[] = intval($member['user_id']); |
||
| 2639 | } |
||
| 2640 | } |
||
| 2641 | |||
| 2642 | return array_unique($userIdList); |
||
| 2643 | } |
||
| 2644 | |||
| 2645 | /** |
||
| 2646 | * Get the subgroups ID from a group. |
||
| 2647 | * The default $levels value is 10 considering it as a extensive level of depth. |
||
| 2648 | * |
||
| 2649 | * @param int $groupId The parent group ID |
||
| 2650 | * @param int $levels The depth levels |
||
| 2651 | * |
||
| 2652 | * @return array The list of ID |
||
| 2653 | */ |
||
| 2654 | public static function getGroupsByDepthLevel($groupId, $levels = 10) |
||
| 2655 | { |
||
| 2656 | $groups = []; |
||
| 2657 | $groupId = (int) $groupId; |
||
| 2658 | |||
| 2659 | $groupTable = Database::get_main_table(TABLE_USERGROUP); |
||
| 2660 | $groupRelGroupTable = Database::get_main_table(TABLE_USERGROUP_REL_USERGROUP); |
||
| 2661 | |||
| 2662 | $select = "SELECT "; |
||
| 2663 | $from = "FROM $groupTable g1 "; |
||
| 2664 | |||
| 2665 | for ($i = 1; $i <= $levels; $i++) { |
||
| 2666 | $tableIndexNumber = $i; |
||
| 2667 | $tableIndexJoinNumber = $i - 1; |
||
| 2668 | $select .= "g$i.id as id_$i "; |
||
| 2669 | $select .= ($i != $levels ? ", " : null); |
||
| 2670 | |||
| 2671 | if ($i == 1) { |
||
| 2672 | $from .= "INNER JOIN $groupRelGroupTable gg0 ON g1.id = gg0.subgroup_id and gg0.group_id = $groupId "; |
||
| 2673 | } else { |
||
| 2674 | $from .= "LEFT JOIN $groupRelGroupTable gg$tableIndexJoinNumber "; |
||
| 2675 | $from .= " ON g$tableIndexJoinNumber.id = gg$tableIndexJoinNumber.group_id "; |
||
| 2676 | $from .= "LEFT JOIN $groupTable g$tableIndexNumber "; |
||
| 2677 | $from .= " ON gg$tableIndexJoinNumber.subgroup_id = g$tableIndexNumber.id "; |
||
| 2678 | } |
||
| 2679 | } |
||
| 2680 | |||
| 2681 | $result = Database::query("$select $from"); |
||
| 2682 | |||
| 2683 | while ($item = Database::fetch_assoc($result)) { |
||
| 2684 | foreach ($item as $myGroupId) { |
||
| 2685 | if (!empty($myGroupId)) { |
||
| 2686 | $groups[] = $myGroupId; |
||
| 2687 | } |
||
| 2688 | } |
||
| 2689 | } |
||
| 2690 | |||
| 2691 | return array_map('intval', $groups); |
||
| 2692 | } |
||
| 2693 | |||
| 2694 | /** |
||
| 2695 | * Set a parent group. |
||
| 2696 | * |
||
| 2697 | * @param int $group_id |
||
| 2698 | * @param int $parent_group_id if 0, we delete the parent_group association |
||
| 2699 | * @param int $relation_type |
||
| 2700 | * |
||
| 2701 | * @return \Doctrine\DBAL\Statement |
||
| 2702 | */ |
||
| 2703 | public function setParentGroup($group_id, $parent_group_id, $relation_type = 1) |
||
| 2704 | { |
||
| 2705 | $table = Database::get_main_table(TABLE_USERGROUP_REL_USERGROUP); |
||
| 2706 | $group_id = intval($group_id); |
||
| 2707 | $parent_group_id = intval($parent_group_id); |
||
| 2708 | if ($parent_group_id == 0) { |
||
| 2709 | $sql = "DELETE FROM $table WHERE subgroup_id = $group_id"; |
||
| 2710 | } else { |
||
| 2711 | $sql = "SELECT group_id FROM $table WHERE subgroup_id = $group_id"; |
||
| 2712 | $res = Database::query($sql); |
||
| 2713 | if (Database::num_rows($res) == 0) { |
||
| 2714 | $sql = "INSERT INTO $table SET |
||
| 2715 | group_id = $parent_group_id, |
||
| 2716 | subgroup_id = $group_id, |
||
| 2717 | relation_type = $relation_type"; |
||
| 2718 | } else { |
||
| 2719 | $sql = "UPDATE $table SET |
||
| 2720 | group_id = $parent_group_id, |
||
| 2721 | relation_type = $relation_type |
||
| 2722 | WHERE subgroup_id = $group_id"; |
||
| 2723 | } |
||
| 2724 | } |
||
| 2725 | $res = Database::query($sql); |
||
| 2726 | |||
| 2727 | return $res; |
||
| 2728 | } |
||
| 2729 | |||
| 2730 | /** |
||
| 2731 | * Filter the groups/classes info to get a name list only. |
||
| 2732 | * |
||
| 2733 | * @param int $userId The user ID |
||
| 2734 | * @param int $filterByType Optional. The type of group |
||
| 2735 | * |
||
| 2736 | * @return array |
||
| 2737 | */ |
||
| 2738 | public function getNameListByUser($userId, $filterByType = null) |
||
| 2743 | } |
||
| 2744 | |||
| 2745 | /** |
||
| 2746 | * Get the HTML necessary for display the groups/classes name list. |
||
| 2747 | * |
||
| 2748 | * @param int $userId The user ID |
||
| 2749 | * @param int $filterByType Optional. The type of group |
||
| 2750 | * |
||
| 2751 | * @return string |
||
| 2752 | */ |
||
| 2753 | public function getLabelsFromNameList($userId, $filterByType = null) |
||
| 2754 | { |
||
| 2755 | $groupsNameListParsed = $this->getNameListByUser($userId, $filterByType); |
||
| 2756 | |||
| 2757 | if (empty($groupsNameListParsed)) { |
||
| 2758 | return ''; |
||
| 2759 | } |
||
| 2760 | |||
| 2761 | $nameList = '<ul class="list-unstyled">'; |
||
| 2762 | |||
| 2763 | foreach ($groupsNameListParsed as $name) { |
||
| 2764 | $nameList .= '<li>'.Display::span($name, ['class' => 'label label-info']).'</li>'; |
||
| 2765 | } |
||
| 2766 | |||
| 2767 | $nameList .= '</ul>'; |
||
| 2768 | |||
| 2769 | return $nameList; |
||
| 2770 | } |
||
| 2771 | |||
| 2772 | /** |
||
| 2773 | * @param array $groupInfo |
||
| 2774 | * |
||
| 2775 | * @return bool |
||
| 2776 | */ |
||
| 2777 | public static function canLeave($groupInfo) |
||
| 2780 | } |
||
| 2781 | |||
| 2782 | /** |
||
| 2783 | * Check permissions and blocks the page. |
||
| 2784 | * |
||
| 2785 | * @param array $userGroupInfo |
||
| 2786 | */ |
||
| 2787 | public function protectScript($userGroupInfo = []) |
||
| 2788 | { |
||
| 2789 | api_block_anonymous_users(); |
||
| 2790 | |||
| 2791 | if (!api_is_platform_admin()) { |
||
| 2801 | } |
||
| 2802 | } |
||
| 2803 | } |
||
| 2804 | } |
||
| 2805 |