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