Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SessionManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SessionManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class SessionManager |
||
| 21 | { |
||
| 22 | public static $_debug = false; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor |
||
| 26 | */ |
||
| 27 | public function __construct() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Fetches a session from the database |
||
| 33 | * @param int $id Session Id |
||
| 34 | * |
||
| 35 | * @return array Session details |
||
| 36 | */ |
||
| 37 | public static function fetch($id) |
||
| 38 | { |
||
| 39 | $em = Database::getManager(); |
||
| 40 | /** @var Session $session */ |
||
| 41 | $session = $em->find('ChamiloCoreBundle:Session', $id); |
||
| 42 | |||
| 43 | if (!$session) { |
||
| 44 | return []; |
||
| 45 | } |
||
| 46 | |||
| 47 | return [ |
||
| 48 | 'id' => $session->getId(), |
||
| 49 | 'id_coach' => $session->getGeneralCoach() ? $session->getGeneralCoach()->getId() : null, |
||
| 50 | 'session_category_id' => $session->getCategory() ? $session->getCategory()->getId() : null, |
||
| 51 | 'name' => $session->getName(), |
||
| 52 | 'description' => $session->getDescription(), |
||
| 53 | 'show_description' => $session->getShowDescription(), |
||
| 54 | 'duration' => $session->getDuration(), |
||
| 55 | 'nbr_courses' => $session->getNbrCourses(), |
||
| 56 | 'nbr_users' => $session->getNbrUsers(), |
||
| 57 | 'nbr_classes' => $session->getNbrClasses(), |
||
| 58 | 'session_admin_id' => $session->getSessionAdminId(), |
||
| 59 | 'visibility' => $session->getVisibility(), |
||
| 60 | 'promotion_id' => $session->getPromotionId(), |
||
| 61 | 'display_start_date' => $session->getDisplayStartDate() |
||
| 62 | ? $session->getDisplayStartDate()->format('Y-m-d H:i:s') |
||
| 63 | : null, |
||
| 64 | 'display_end_date' => $session->getDisplayEndDate() |
||
| 65 | ? $session->getDisplayEndDate()->format('Y-m-d H:i:s') |
||
| 66 | : null, |
||
| 67 | 'access_start_date' => $session->getAccessStartDate() |
||
| 68 | ? $session->getAccessStartDate()->format('Y-m-d H:i:s') |
||
| 69 | : null, |
||
| 70 | 'access_end_date' => $session->getAccessEndDate() |
||
| 71 | ? $session->getAccessEndDate()->format('Y-m-d H:i:s') |
||
| 72 | : null, |
||
| 73 | 'coach_access_start_date' => $session->getCoachAccessStartDate() |
||
| 74 | ? $session->getCoachAccessStartDate()->format('Y-m-d H:i:s') |
||
| 75 | : null, |
||
| 76 | 'coach_access_end_date' => $session->getCoachAccessEndDate() |
||
| 77 | ? $session->getCoachAccessEndDate()->format('Y-m-d H:i:s') |
||
| 78 | : null, |
||
| 79 | 'send_subscription_notification' => $session->getSendSubscriptionNotification() |
||
| 80 | ]; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Create a session |
||
| 85 | * @author Carlos Vargas <[email protected]>, from existing code |
||
| 86 | * @param string $name |
||
| 87 | * @param string $startDate (YYYY-MM-DD hh:mm:ss) |
||
| 88 | * @param string $endDate (YYYY-MM-DD hh:mm:ss) |
||
| 89 | * @param string $displayStartDate (YYYY-MM-DD hh:mm:ss) |
||
| 90 | * @param string $displayEndDate (YYYY-MM-DD hh:mm:ss) |
||
| 91 | * @param string $coachStartDate (YYYY-MM-DD hh:mm:ss) |
||
| 92 | * @param string $coachEndDate (YYYY-MM-DD hh:mm:ss) |
||
| 93 | * @param mixed $coachId If integer, this is the session coach id, if string, the coach ID will be looked for from the user table |
||
| 94 | * @param integer $sessionCategoryId ID of the session category in which this session is registered |
||
| 95 | * @param integer $visibility Visibility after end date (0 = read-only, 1 = invisible, 2 = accessible) |
||
| 96 | * @param bool $fixSessionNameIfExists |
||
| 97 | * @param string $duration |
||
| 98 | * @param string $description Optional. The session description |
||
| 99 | * @param int $showDescription Optional. Whether show the session description |
||
| 100 | * @param array $extraFields |
||
| 101 | * @param int $sessionAdminId Optional. If this sessions was created by a session admin, assign it to him |
||
| 102 | * @param boolean $sendSubscriptionNotification Optional. |
||
| 103 | * Whether send a mail notification to users being subscribed |
||
| 104 | * @todo use an array to replace all this parameters or use the model.lib.php ... |
||
| 105 | * @return mixed Session ID on success, error message otherwise |
||
| 106 | * */ |
||
| 107 | public static function create_session( |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $name |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | View Code Duplication | public static function session_name_exists($name) |
|
| 291 | { |
||
| 292 | $name = Database::escape_string($name); |
||
| 293 | $sql = "SELECT COUNT(*) as count FROM " . Database::get_main_table(TABLE_MAIN_SESSION) . " |
||
| 294 | WHERE name = '$name'"; |
||
| 295 | $result = Database::fetch_array(Database::query($sql)); |
||
| 296 | |||
| 297 | return $result['count'] > 0; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $where_condition |
||
| 302 | * |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | public static function get_count_admin($where_condition = '') |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Gets the admin session list callback of the session/session_list.php page |
||
| 420 | * @param array $options order and limit keys |
||
| 421 | * @param boolean $get_count Whether to get all the results or only the count |
||
| 422 | * @return mixed Integer for number of rows, or array of results |
||
| 423 | * @assert (array(),true) !== false |
||
| 424 | */ |
||
| 425 | public static function get_sessions_admin($options = array(), $get_count = false) |
||
| 426 | { |
||
| 427 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 428 | $sessionCategoryTable = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 429 | |||
| 430 | $where = 'WHERE 1 = 1 '; |
||
| 431 | $user_id = api_get_user_id(); |
||
| 432 | |||
| 433 | View Code Duplication | if (!api_is_platform_admin()) { |
|
| 434 | if (api_is_session_admin() && |
||
| 435 | api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false' |
||
| 436 | ) { |
||
| 437 | $where .=" AND s.session_admin_id = $user_id "; |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | if (!api_is_platform_admin() && api_is_teacher() && |
||
| 442 | api_get_setting('allow_teachers_to_create_sessions') == 'true' |
||
| 443 | ) { |
||
| 444 | $where .=" AND s.id_coach = $user_id "; |
||
| 445 | } |
||
| 446 | |||
| 447 | $extra_field = new ExtraFieldModel('session'); |
||
| 448 | $conditions = $extra_field->parseConditions($options); |
||
| 449 | $inject_joins = $conditions['inject_joins']; |
||
| 450 | $where .= $conditions['where']; |
||
| 451 | $inject_where = $conditions['inject_where']; |
||
| 452 | $inject_extra_fields = $conditions['inject_extra_fields']; |
||
| 453 | |||
| 454 | $order = $conditions['order']; |
||
| 455 | $limit = $conditions['limit']; |
||
| 456 | |||
| 457 | $isMakingOrder = false; |
||
| 458 | |||
| 459 | if ($get_count == true) { |
||
| 460 | $select = " SELECT count(DISTINCT s.id) as total_rows"; |
||
| 461 | } else { |
||
| 462 | $select = |
||
| 463 | "SELECT DISTINCT |
||
| 464 | s.name, |
||
| 465 | s.display_start_date, |
||
| 466 | s.display_end_date, |
||
| 467 | access_start_date, |
||
| 468 | access_end_date, |
||
| 469 | s.visibility, |
||
| 470 | s.session_category_id, |
||
| 471 | $inject_extra_fields |
||
| 472 | s.id |
||
| 473 | "; |
||
| 474 | |||
| 475 | $isMakingOrder = strpos($options['order'], 'category_name') === 0; |
||
| 476 | } |
||
| 477 | |||
| 478 | $isFilteringSessionCategory = strpos($where, 'category_name') !== false; |
||
| 479 | |||
| 480 | if ($isMakingOrder || $isFilteringSessionCategory) { |
||
| 481 | $inject_joins .= " LEFT JOIN $sessionCategoryTable sc ON s.session_category_id = sc.id "; |
||
| 482 | |||
| 483 | if ($isFilteringSessionCategory) { |
||
| 484 | $where = str_replace('category_name', 'sc.name', $where); |
||
| 485 | } |
||
| 486 | |||
| 487 | if ($isMakingOrder) { |
||
| 488 | $order = str_replace('category_name', 'sc.name', $order); |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | $query = "$select FROM $tbl_session s $inject_joins $where $inject_where"; |
||
| 493 | |||
| 494 | if (api_is_multiple_url_enabled()) { |
||
| 495 | $table_access_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 496 | $access_url_id = api_get_current_access_url_id(); |
||
| 497 | if ($access_url_id != -1) { |
||
| 498 | $where.= " AND ar.access_url_id = $access_url_id "; |
||
| 499 | $query = "$select |
||
| 500 | FROM $tbl_session s $inject_joins |
||
| 501 | INNER JOIN $table_access_url_rel_session ar |
||
| 502 | ON (ar.session_id = s.id) $where"; |
||
| 503 | } |
||
| 504 | } |
||
| 505 | |||
| 506 | $query .= $order; |
||
| 507 | $query .= $limit; |
||
| 508 | $result = Database::query($query); |
||
| 509 | |||
| 510 | $categories = self::get_all_session_category(); |
||
| 511 | $orderedCategories = array(); |
||
| 512 | if (!empty($categories)) { |
||
| 513 | foreach ($categories as $category) { |
||
| 514 | $orderedCategories[$category['id']] = $category['name']; |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | $formatted_sessions = array(); |
||
| 519 | |||
| 520 | if (Database::num_rows($result)) { |
||
| 521 | $sessions = Database::store_result($result, 'ASSOC'); |
||
| 522 | if ($get_count) { |
||
| 523 | return $sessions[0]['total_rows']; |
||
| 524 | } |
||
| 525 | |||
| 526 | foreach ($sessions as $session) { |
||
| 527 | $session_id = $session['id']; |
||
| 528 | $session['name'] = Display::url($session['name'], "resume_session.php?id_session=".$session['id']); |
||
| 529 | |||
| 530 | View Code Duplication | if (isset($session['session_active']) && $session['session_active'] == 1) { |
|
| 531 | $session['session_active'] = Display::return_icon('accept.png', get_lang('Active'), array(), ICON_SIZE_SMALL); |
||
| 532 | } else { |
||
| 533 | $session['session_active'] = Display::return_icon('error.png', get_lang('Inactive'), array(), ICON_SIZE_SMALL); |
||
| 534 | } |
||
| 535 | |||
| 536 | $session = self::convert_dates_to_local($session, true); |
||
| 537 | |||
| 538 | View Code Duplication | switch ($session['visibility']) { |
|
| 539 | case SESSION_VISIBLE_READ_ONLY: //1 |
||
| 540 | $session['visibility'] = get_lang('ReadOnly'); |
||
| 541 | break; |
||
| 542 | case SESSION_VISIBLE: //2 |
||
| 543 | case SESSION_AVAILABLE: //4 |
||
| 544 | $session['visibility'] = get_lang('Visible'); |
||
| 545 | break; |
||
| 546 | case SESSION_INVISIBLE: //3 |
||
| 547 | $session['visibility'] = api_ucfirst(get_lang('Invisible')); |
||
| 548 | break; |
||
| 549 | } |
||
| 550 | |||
| 551 | // Cleaning double selects. |
||
| 552 | View Code Duplication | foreach ($session as $key => &$value) { |
|
| 553 | if (isset($options_by_double[$key]) || isset($options_by_double[$key.'_second'])) { |
||
| 554 | $options = explode('::', $value); |
||
| 555 | } |
||
| 556 | $original_key = $key; |
||
| 557 | |||
| 558 | if (strpos($key, '_second') === false) { |
||
| 559 | } else { |
||
| 560 | $key = str_replace('_second', '', $key); |
||
| 561 | } |
||
| 562 | |||
| 563 | if (isset($options_by_double[$key])) { |
||
| 564 | if (isset($options[0])) { |
||
| 565 | if (isset($options_by_double[$key][$options[0]])) { |
||
| 566 | if (strpos($original_key, '_second') === false) { |
||
| 567 | $value = $options_by_double[$key][$options[0]]['option_display_text']; |
||
| 568 | } else { |
||
| 569 | $value = $options_by_double[$key][$options[1]]['option_display_text']; |
||
| 570 | } |
||
| 571 | } |
||
| 572 | } |
||
| 573 | } |
||
| 574 | } |
||
| 575 | $formatted_sessions[$session_id] = $session; |
||
| 576 | $categoryName = isset($orderedCategories[$session['session_category_id']]) ? $orderedCategories[$session['session_category_id']] : ''; |
||
| 577 | $formatted_sessions[$session_id]['category_name'] = $categoryName; |
||
| 578 | } |
||
| 579 | } |
||
| 580 | return $formatted_sessions; |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Get total of records for progress of learning paths in the given session |
||
| 585 | * @param int session id |
||
| 586 | * @return int |
||
| 587 | */ |
||
| 588 | public static function get_count_session_lp_progress($sessionId = 0) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Gets the progress of learning paths in the given session |
||
| 612 | * @param int $sessionId |
||
| 613 | * @param int $courseId |
||
| 614 | * @param string $date_from |
||
| 615 | * @param string $date_to |
||
| 616 | * @param array options order and limit keys |
||
| 617 | * @return array table with user name, lp name, progress |
||
| 618 | */ |
||
| 619 | public static function get_session_lp_progress($sessionId = 0, $courseId = 0, $date_from, $date_to, $options) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Gets the survey answers |
||
| 738 | * @param int $sessionId |
||
| 739 | * @param int $courseId |
||
| 740 | * @param int $surveyId |
||
| 741 | * @param array options order and limit keys |
||
| 742 | * @todo fix the query |
||
| 743 | * @return array table with user name, lp name, progress |
||
| 744 | */ |
||
| 745 | public static function get_survey_overview($sessionId = 0, $courseId = 0, $surveyId = 0, $date_from, $date_to, $options) |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Gets the progress of the given session |
||
| 849 | * @param int $sessionId |
||
| 850 | * @param int $courseId |
||
| 851 | * @param array options order and limit keys |
||
| 852 | * |
||
| 853 | * @return array table with user name, lp name, progress |
||
| 854 | */ |
||
| 855 | public static function get_session_progress($sessionId, $courseId, $date_from, $date_to, $options) |
||
| 856 | { |
||
| 857 | $sessionId = intval($sessionId); |
||
| 858 | |||
| 859 | $getAllSessions = false; |
||
| 860 | if (empty($sessionId)) { |
||
| 861 | $sessionId = 0; |
||
| 862 | $getAllSessions = true; |
||
| 863 | } |
||
| 864 | |||
| 865 | //tables |
||
| 866 | $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 867 | $user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 868 | $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION); |
||
| 869 | $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT); |
||
| 870 | $tbl_course_lp = Database::get_course_table(TABLE_LP_MAIN); |
||
| 871 | $wiki = Database::get_course_table(TABLE_WIKI); |
||
| 872 | $table_stats_default = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT); |
||
| 873 | $table_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS); |
||
| 874 | |||
| 875 | $course = api_get_course_info_by_id($courseId); |
||
| 876 | $where = " WHERE c_id = '%s' AND s.status <> 2 "; |
||
| 877 | |||
| 878 | $limit = null; |
||
| 879 | if (!empty($options['limit'])) { |
||
| 880 | $limit = " LIMIT " . $options['limit']; |
||
| 881 | } |
||
| 882 | |||
| 883 | if (!empty($options['where'])) { |
||
| 884 | $where .= ' '.$options['where']; |
||
| 885 | } |
||
| 886 | |||
| 887 | $order = null; |
||
| 888 | if (!empty($options['order'])) { |
||
| 889 | $order = " ORDER BY " . $options['order']; |
||
| 890 | } |
||
| 891 | |||
| 892 | //TODO, fix create report without session |
||
| 893 | $queryVariables = array($course['real_id']); |
||
| 894 | if (!empty($sessionId)) { |
||
| 895 | $where .= ' AND session_id = %s'; |
||
| 896 | $queryVariables[] = $sessionId; |
||
| 897 | $sql = "SELECT |
||
| 898 | u.user_id, u.lastname, u.firstname, u.username, |
||
| 899 | u.email, s.c_id, s.session_id |
||
| 900 | FROM $session_course_user s |
||
| 901 | INNER JOIN $user u |
||
| 902 | ON u.user_id = s.user_id |
||
| 903 | $where $order $limit"; |
||
| 904 | } else { |
||
| 905 | $sql = "SELECT |
||
| 906 | u.user_id, u.lastname, u.firstname, u.username, |
||
| 907 | u.email, s.c_id, s.session_id |
||
| 908 | FROM $session_course_user s |
||
| 909 | INNER JOIN $user u ON u.user_id = s.user_id |
||
| 910 | $where $order $limit"; |
||
| 911 | } |
||
| 912 | |||
| 913 | $sql_query = vsprintf($sql, $queryVariables); |
||
| 914 | $rs = Database::query($sql_query); |
||
| 915 | while ($user = Database::fetch_array($rs)) { |
||
| 916 | $users[$user['user_id']] = $user; |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Lessons |
||
| 921 | */ |
||
| 922 | $sql = "SELECT * FROM $tbl_course_lp WHERE c_id = %s "; //AND session_id = %s |
||
| 923 | $sql_query = sprintf($sql, $course['real_id']); |
||
| 924 | $result = Database::query($sql_query); |
||
| 925 | $arrLesson = array(array()); |
||
| 926 | View Code Duplication | while ($row = Database::fetch_array($result)) { |
|
| 927 | if (empty($arrLesson[$row['session_id']]['lessons_total'])) { |
||
| 928 | $arrLesson[$row['session_id']]['lessons_total'] = 1; |
||
| 929 | } else { |
||
| 930 | $arrLesson[$row['session_id']]['lessons_total'] ++; |
||
| 931 | } |
||
| 932 | } |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Exercises |
||
| 936 | */ |
||
| 937 | $exercises = ExerciseLib::get_all_exercises($course, $sessionId, false, '', $getAllSessions); |
||
| 938 | $exercises_total = count($exercises); |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Assignments |
||
| 942 | */ |
||
| 943 | //total |
||
| 944 | $params = [$course['real_id']]; |
||
| 945 | if ($getAllSessions) { |
||
| 946 | $sql = "SELECT count(w.id) as count |
||
| 947 | FROM $workTable w |
||
| 948 | LEFT JOIN $workTableAssignment a |
||
| 949 | ON (a.publication_id = w.id AND a.c_id = w.c_id) |
||
| 950 | WHERE w.c_id = %s |
||
| 951 | AND parent_id = 0 |
||
| 952 | AND active IN (1, 0)"; |
||
| 953 | } else { |
||
| 954 | $sql = "SELECT count(w.id) as count |
||
| 955 | FROM $workTable w |
||
| 956 | LEFT JOIN $workTableAssignment a |
||
| 957 | ON (a.publication_id = w.id AND a.c_id = w.c_id) |
||
| 958 | WHERE w.c_id = %s |
||
| 959 | AND parent_id = 0 |
||
| 960 | AND active IN (1, 0)"; |
||
| 961 | |||
| 962 | if (empty($sessionId)) { |
||
| 963 | $sql .= ' AND w.session_id = NULL '; |
||
| 964 | } else { |
||
| 965 | $sql .= ' AND w.session_id = %s '; |
||
| 966 | $params[] = $sessionId; |
||
| 967 | } |
||
| 968 | } |
||
| 969 | |||
| 970 | $sql_query = vsprintf($sql, $params); |
||
| 971 | $result = Database::query($sql_query); |
||
| 972 | $row = Database::fetch_array($result); |
||
| 973 | $assignments_total = $row['count']; |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Wiki |
||
| 977 | */ |
||
| 978 | if ($getAllSessions) { |
||
| 979 | $sql = "SELECT count(distinct page_id) as count FROM $wiki |
||
| 980 | WHERE c_id = %s"; |
||
| 981 | } else { |
||
| 982 | $sql = "SELECT count(distinct page_id) as count FROM $wiki |
||
| 983 | WHERE c_id = %s and session_id = %s"; |
||
| 984 | } |
||
| 985 | $sql_query = sprintf($sql, $course['real_id'], $sessionId); |
||
| 986 | $result = Database::query($sql_query); |
||
| 987 | $row = Database::fetch_array($result); |
||
| 988 | $wiki_total = $row['count']; |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Surveys |
||
| 992 | */ |
||
| 993 | $survey_user_list = array(); |
||
| 994 | $survey_list = SurveyManager::get_surveys($course['code'], $sessionId); |
||
| 995 | |||
| 996 | $surveys_total = count($survey_list); |
||
| 997 | View Code Duplication | foreach ($survey_list as $survey) { |
|
| 998 | $user_list = SurveyManager::get_people_who_filled_survey( |
||
| 999 | $survey['survey_id'], |
||
| 1000 | false, |
||
| 1001 | $course['real_id'] |
||
| 1002 | ); |
||
| 1003 | foreach ($user_list as $user_id) { |
||
| 1004 | isset($survey_user_list[$user_id]) ? $survey_user_list[$user_id] ++ : $survey_user_list[$user_id] = 1; |
||
| 1005 | } |
||
| 1006 | } |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Forums |
||
| 1010 | */ |
||
| 1011 | $forums_total = CourseManager::getCountForum( |
||
| 1012 | $course['real_id'], |
||
| 1013 | $sessionId, |
||
| 1014 | $getAllSessions |
||
| 1015 | ); |
||
| 1016 | |||
| 1017 | //process table info |
||
| 1018 | foreach ($users as $user) { |
||
| 1019 | //Course description |
||
| 1020 | $sql = "SELECT count(*) as count |
||
| 1021 | FROM $table_stats_access |
||
| 1022 | WHERE access_tool = 'course_description' |
||
| 1023 | AND c_id = '%s' |
||
| 1024 | AND access_session_id = %s |
||
| 1025 | AND access_user_id = %s "; |
||
| 1026 | $sql_query = sprintf($sql, $course['real_id'], $user['id_session'], $user['user_id']); |
||
| 1027 | |||
| 1028 | $result = Database::query($sql_query); |
||
| 1029 | $row = Database::fetch_array($result); |
||
| 1030 | $course_description_progress = ($row['count'] > 0) ? 100 : 0; |
||
| 1031 | |||
| 1032 | if (!empty($arrLesson[$user['id_session']]['lessons_total'])) { |
||
| 1033 | $lessons_total = $arrLesson[$user['id_session']]['lessons_total']; |
||
| 1034 | } else { |
||
| 1035 | $lessons_total = !empty($arrLesson[0]['lessons_total']) ? $arrLesson[0]['lessons_total'] : 0; |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | //Lessons |
||
| 1039 | //TODO: Lessons done and left is calculated by progress per item in lesson, maybe we should calculate it only per completed lesson? |
||
| 1040 | $lessons_progress = Tracking::get_avg_student_progress( |
||
| 1041 | $user['user_id'], |
||
| 1042 | $course['code'], |
||
| 1043 | array(), |
||
| 1044 | $user['id_session'] |
||
| 1045 | ); |
||
| 1046 | $lessons_done = ($lessons_progress * $lessons_total) / 100; |
||
| 1047 | $lessons_left = $lessons_total - $lessons_done; |
||
| 1048 | |||
| 1049 | //Exercises |
||
| 1050 | $exercises_progress = str_replace('%', '', Tracking::get_exercise_student_progress($exercises, $user['user_id'], $course['real_id'], $user['id_session'])); |
||
| 1051 | $exercises_done = round(($exercises_progress * $exercises_total) / 100); |
||
| 1052 | $exercises_left = $exercises_total - $exercises_done; |
||
| 1053 | |||
| 1054 | //Assignments |
||
| 1055 | $assignments_done = Tracking::count_student_assignments($user['user_id'], $course['code'], $user['id_session']); |
||
| 1056 | $assignments_left = $assignments_total - $assignments_done; |
||
| 1057 | if (!empty($assignments_total)) { |
||
| 1058 | $assignments_progress = round((( $assignments_done * 100 ) / $assignments_total), 2); |
||
| 1059 | } else { |
||
| 1060 | $assignments_progress = 0; |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | //Wiki |
||
| 1064 | //total revisions per user |
||
| 1065 | $sql = "SELECT count(*) as count |
||
| 1066 | FROM $wiki |
||
| 1067 | WHERE c_id = %s and session_id = %s and user_id = %s"; |
||
| 1068 | $sql_query = sprintf($sql, $course['real_id'], $user['id_session'], $user['user_id']); |
||
| 1069 | $result = Database::query($sql_query); |
||
| 1070 | $row = Database::fetch_array($result); |
||
| 1071 | $wiki_revisions = $row['count']; |
||
| 1072 | //count visited wiki pages |
||
| 1073 | $sql = "SELECT count(distinct default_value) as count |
||
| 1074 | FROM $table_stats_default |
||
| 1075 | WHERE |
||
| 1076 | default_user_id = %s AND |
||
| 1077 | default_event_type = 'wiki_page_view' AND |
||
| 1078 | default_value_type = 'wiki_page_id' AND |
||
| 1079 | c_id = %s |
||
| 1080 | "; |
||
| 1081 | $sql_query = sprintf($sql, $user['user_id'], $course['real_id']); |
||
| 1082 | $result = Database::query($sql_query); |
||
| 1083 | $row = Database::fetch_array($result); |
||
| 1084 | |||
| 1085 | $wiki_read = $row['count']; |
||
| 1086 | $wiki_unread = $wiki_total - $wiki_read; |
||
| 1087 | if (!empty($wiki_total)) { |
||
| 1088 | $wiki_progress = round((( $wiki_read * 100 ) / $wiki_total), 2); |
||
| 1089 | } else { |
||
| 1090 | $wiki_progress = 0; |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | //Surveys |
||
| 1094 | $surveys_done = (isset($survey_user_list[$user['user_id']]) ? $survey_user_list[$user['user_id']] : 0); |
||
| 1095 | $surveys_left = $surveys_total - $surveys_done; |
||
| 1096 | if (!empty($surveys_total)) { |
||
| 1097 | $surveys_progress = round((( $surveys_done * 100 ) / $surveys_total), 2); |
||
| 1098 | } else { |
||
| 1099 | $surveys_progress = 0; |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | //Forums |
||
| 1103 | $forums_done = CourseManager::getCountForumPerUser( |
||
| 1104 | $user['user_id'], |
||
| 1105 | $course['real_id'], |
||
| 1106 | $user['id_session'] |
||
| 1107 | ); |
||
| 1108 | $forums_left = $forums_total - $forums_done; |
||
| 1109 | if (!empty($forums_total)) { |
||
| 1110 | $forums_progress = round((( $forums_done * 100 ) / $forums_total), 2); |
||
| 1111 | } else { |
||
| 1112 | $forums_progress = 0; |
||
| 1113 | } |
||
| 1114 | |||
| 1115 | //Overall Total |
||
| 1116 | $overall_total = ($course_description_progress + $exercises_progress + $forums_progress + $assignments_progress + $wiki_progress + $surveys_progress) / 6; |
||
| 1117 | |||
| 1118 | $link = '<a href="' . api_get_path(WEB_CODE_PATH) . 'mySpace/myStudents.php?student=' . $user[0] . '&details=true&course=' . $course['code'] . '&id_session=' . $user['id_session'] . '"> %s </a>'; |
||
| 1119 | $linkForum = '<a href="' . api_get_path(WEB_CODE_PATH) . 'forum/index.php?cidReq=' . $course['code'] . '&id_session=' . $user['id_session'] . '"> %s </a>'; |
||
| 1120 | $linkWork = '<a href="' . api_get_path(WEB_CODE_PATH) . 'work/work.php?cidReq=' . $course['code'] . '&id_session=' . $user['id_session'] . '"> %s </a>'; |
||
| 1121 | $linkWiki = '<a href="' . api_get_path(WEB_CODE_PATH) . 'wiki/index.php?cidReq=' . $course['code'] . '&session_id=' . $user['id_session'] . '&action=statistics"> %s </a>'; |
||
| 1122 | $linkSurvey = '<a href="' . api_get_path(WEB_CODE_PATH) . 'survey/survey_list.php?cidReq=' . $course['code'] . '&id_session=' . $user['id_session'] . '"> %s </a>'; |
||
| 1123 | |||
| 1124 | $table[] = array( |
||
| 1125 | 'lastname' => $user[1], |
||
| 1126 | 'firstname' => $user[2], |
||
| 1127 | 'username' => $user[3], |
||
| 1128 | #'profile' => '', |
||
| 1129 | 'total' => round($overall_total, 2) . '%', |
||
| 1130 | 'courses' => sprintf($link, $course_description_progress . '%'), |
||
| 1131 | 'lessons' => sprintf($link, $lessons_progress . '%'), |
||
| 1132 | 'exercises' => sprintf($link, $exercises_progress . '%'), |
||
| 1133 | 'forums' => sprintf($link, $forums_progress . '%'), |
||
| 1134 | 'homeworks' => sprintf($link, $assignments_progress . '%'), |
||
| 1135 | 'wikis' => sprintf($link, $wiki_progress . '%'), |
||
| 1136 | 'surveys' => sprintf($link, $surveys_progress . '%'), |
||
| 1137 | //course description |
||
| 1138 | 'course_description_progress' => $course_description_progress . '%', |
||
| 1139 | //lessons |
||
| 1140 | 'lessons_total' => sprintf($link, $lessons_total), |
||
| 1141 | 'lessons_done' => sprintf($link, $lessons_done), |
||
| 1142 | 'lessons_left' => sprintf($link, $lessons_left), |
||
| 1143 | 'lessons_progress' => sprintf($link, $lessons_progress . '%'), |
||
| 1144 | //exercises |
||
| 1145 | 'exercises_total' => sprintf($link, $exercises_total), |
||
| 1146 | 'exercises_done' => sprintf($link, $exercises_done), |
||
| 1147 | 'exercises_left' => sprintf($link, $exercises_left), |
||
| 1148 | 'exercises_progress' => sprintf($link, $exercises_progress . '%'), |
||
| 1149 | //forums |
||
| 1150 | 'forums_total' => sprintf($linkForum, $forums_total), |
||
| 1151 | 'forums_done' => sprintf($linkForum, $forums_done), |
||
| 1152 | 'forums_left' => sprintf($linkForum, $forums_left), |
||
| 1153 | 'forums_progress' => sprintf($linkForum, $forums_progress . '%'), |
||
| 1154 | //assignments |
||
| 1155 | 'assignments_total' => sprintf($linkWork, $assignments_total), |
||
| 1156 | 'assignments_done' => sprintf($linkWork, $assignments_done), |
||
| 1157 | 'assignments_left' => sprintf($linkWork, $assignments_left), |
||
| 1158 | 'assignments_progress' => sprintf($linkWork, $assignments_progress . '%'), |
||
| 1159 | //wiki |
||
| 1160 | 'wiki_total' => sprintf($linkWiki, $wiki_total), |
||
| 1161 | 'wiki_revisions' => sprintf($linkWiki, $wiki_revisions), |
||
| 1162 | 'wiki_read' => sprintf($linkWiki, $wiki_read), |
||
| 1163 | 'wiki_unread' => sprintf($linkWiki, $wiki_unread), |
||
| 1164 | 'wiki_progress' => sprintf($linkWiki, $wiki_progress . '%'), |
||
| 1165 | //survey |
||
| 1166 | 'surveys_total' => sprintf($linkSurvey, $surveys_total), |
||
| 1167 | 'surveys_done' => sprintf($linkSurvey, $surveys_done), |
||
| 1168 | 'surveys_left' => sprintf($linkSurvey, $surveys_left), |
||
| 1169 | 'surveys_progress' => sprintf($linkSurvey, $surveys_progress . '%'), |
||
| 1170 | ); |
||
| 1171 | } |
||
| 1172 | |||
| 1173 | return $table; |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * @return int |
||
| 1178 | */ |
||
| 1179 | public static function get_number_of_tracking_access_overview() |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Get the ip, total of clicks, login date and time logged in for all user, in one session |
||
| 1189 | * @todo track_e_course_access table should have ip so we dont have to look for it in track_e_login |
||
| 1190 | * |
||
| 1191 | * @author César Perales <[email protected]>, Beeznest Team |
||
| 1192 | * @version 1.9.6 |
||
| 1193 | */ |
||
| 1194 | public static function get_user_data_access_tracking_overview( |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * Creates a new course code based in given code |
||
| 1333 | * |
||
| 1334 | * @param string $session_name |
||
| 1335 | * <code> |
||
| 1336 | * $wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3 |
||
| 1337 | * if the course code doest not exist in the DB the same course code will be returned |
||
| 1338 | * </code> |
||
| 1339 | * @return string wanted unused code |
||
| 1340 | */ |
||
| 1341 | View Code Duplication | public static function generateNextSessionName($session_name) |
|
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Edit a session |
||
| 1367 | * @author Carlos Vargas from existing code |
||
| 1368 | * @param integer $id Session primary key |
||
| 1369 | * @param string $name |
||
| 1370 | * @param string $startDate |
||
| 1371 | * @param string $endDate |
||
| 1372 | * @param string $displayStartDate |
||
| 1373 | * @param string $displayEndDate |
||
| 1374 | * @param string $coachStartDate |
||
| 1375 | * @param string $coachEndDate |
||
| 1376 | * @param integer $coachId |
||
| 1377 | * @param integer $sessionCategoryId |
||
| 1378 | * @param int $visibility |
||
| 1379 | * @param string $description |
||
| 1380 | * @param int $showDescription |
||
| 1381 | * @param int $duration |
||
| 1382 | * @param array $extraFields |
||
| 1383 | * @param int $sessionAdminId |
||
| 1384 | * @param boolean $sendSubscriptionNotification Optional. |
||
| 1385 | * Whether send a mail notification to users being subscribed |
||
| 1386 | * @return mixed |
||
| 1387 | */ |
||
| 1388 | public static function edit_session( |
||
| 1524 | |||
| 1525 | /** |
||
| 1526 | * Delete session |
||
| 1527 | * @author Carlos Vargas from existing code |
||
| 1528 | * @param array $id_checked an array to delete sessions |
||
| 1529 | * @param boolean $from_ws optional, true if the function is called |
||
| 1530 | * by a webservice, false otherwise. |
||
| 1531 | * @return void Nothing, or false on error |
||
| 1532 | * */ |
||
| 1533 | public static function delete($id_checked, $from_ws = false) |
||
| 1627 | |||
| 1628 | /** |
||
| 1629 | * @param int $id promotion id |
||
| 1630 | * |
||
| 1631 | * @return bool |
||
| 1632 | */ |
||
| 1633 | View Code Duplication | public static function clear_session_ref_promotion($id) |
|
| 1646 | |||
| 1647 | /** |
||
| 1648 | * Subscribes students to the given session and optionally (default) unsubscribes previous users |
||
| 1649 | * |
||
| 1650 | * @author Carlos Vargas from existing code |
||
| 1651 | * @author Julio Montoya. Cleaning code. |
||
| 1652 | * @param int $id_session |
||
| 1653 | * @param array $user_list |
||
| 1654 | * @param int $session_visibility |
||
| 1655 | * @param bool $empty_users |
||
| 1656 | * @return bool |
||
| 1657 | */ |
||
| 1658 | public static function subscribe_users_to_session( |
||
| 1876 | |||
| 1877 | /** |
||
| 1878 | * Returns user list of the current users subscribed in the course-session |
||
| 1879 | * @param int $sessionId |
||
| 1880 | * @param array $courseInfo |
||
| 1881 | * @param int $status |
||
| 1882 | * |
||
| 1883 | * @return array |
||
| 1884 | */ |
||
| 1885 | public static function getUsersByCourseSession( |
||
| 1921 | |||
| 1922 | /** |
||
| 1923 | * Remove a list of users from a course-session |
||
| 1924 | * @param array $userList |
||
| 1925 | * @param int $sessionId |
||
| 1926 | * @param array $courseInfo |
||
| 1927 | * @param int $status |
||
| 1928 | * @param bool $updateTotal |
||
| 1929 | * @return bool |
||
| 1930 | */ |
||
| 1931 | public static function removeUsersFromCourseSession( |
||
| 1986 | |||
| 1987 | /** |
||
| 1988 | * Subscribe a user to an specific course inside a session. |
||
| 1989 | * |
||
| 1990 | * @param array $user_list |
||
| 1991 | * @param int $session_id |
||
| 1992 | * @param string $course_code |
||
| 1993 | * @param int $session_visibility |
||
| 1994 | * @param bool $removeUsersNotInList |
||
| 1995 | * @return bool |
||
| 1996 | */ |
||
| 1997 | public static function subscribe_users_to_session_course( |
||
| 2104 | |||
| 2105 | /** |
||
| 2106 | * Unsubscribe user from session |
||
| 2107 | * |
||
| 2108 | * @param int Session id |
||
| 2109 | * @param int User id |
||
| 2110 | * @return bool True in case of success, false in case of error |
||
| 2111 | */ |
||
| 2112 | public static function unsubscribe_user_from_session($session_id, $user_id) |
||
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Subscribes courses to the given session and optionally (default) |
||
| 2172 | * unsubscribes previous users |
||
| 2173 | * @author Carlos Vargas from existing code |
||
| 2174 | * @param int $sessionId |
||
| 2175 | * @param array $courseList List of courses int ids |
||
| 2176 | * @param bool $removeExistingCoursesWithUsers Whether to unsubscribe |
||
| 2177 | * existing courses and users (true, default) or not (false) |
||
| 2178 | * @param $copyEvaluation from base course to session course |
||
| 2179 | * @return void Nothing, or false on error |
||
| 2180 | * */ |
||
| 2181 | public static function add_courses_to_session( |
||
| 2182 | $sessionId, |
||
| 2183 | $courseList, |
||
| 2184 | $removeExistingCoursesWithUsers = true, |
||
| 2185 | $copyEvaluation = false |
||
| 2186 | ) { |
||
| 2187 | $sessionId = intval($sessionId); |
||
| 2188 | |||
| 2189 | if (empty($sessionId) || empty($courseList)) { |
||
| 2190 | return false; |
||
| 2191 | } |
||
| 2192 | |||
| 2193 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 2194 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2195 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2196 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 2197 | |||
| 2198 | // Get list of courses subscribed to this session |
||
| 2199 | $sql = "SELECT c_id |
||
| 2200 | FROM $tbl_session_rel_course |
||
| 2201 | WHERE session_id = $sessionId"; |
||
| 2202 | $rs = Database::query($sql); |
||
| 2203 | $existingCourses = Database::store_result($rs); |
||
| 2204 | $nbr_courses = count($existingCourses); |
||
| 2205 | |||
| 2206 | // Get list of users subscribed to this session |
||
| 2207 | $sql = "SELECT user_id |
||
| 2208 | FROM $tbl_session_rel_user |
||
| 2209 | WHERE |
||
| 2210 | session_id = $sessionId AND |
||
| 2211 | relation_type<>" . SESSION_RELATION_TYPE_RRHH; |
||
| 2212 | $result = Database::query($sql); |
||
| 2213 | $user_list = Database::store_result($result); |
||
| 2214 | |||
| 2215 | // Remove existing courses from the session. |
||
| 2216 | if ($removeExistingCoursesWithUsers === true && !empty($existingCourses)) { |
||
| 2217 | foreach ($existingCourses as $existingCourse) { |
||
| 2218 | if (!in_array($existingCourse['c_id'], $courseList)) { |
||
| 2219 | |||
| 2220 | $sql = "DELETE FROM $tbl_session_rel_course |
||
| 2221 | WHERE |
||
| 2222 | c_id = " . $existingCourse['c_id'] . " AND |
||
| 2223 | session_id = $sessionId"; |
||
| 2224 | Database::query($sql); |
||
| 2225 | |||
| 2226 | $sql = "DELETE FROM $tbl_session_rel_course_rel_user |
||
| 2227 | WHERE |
||
| 2228 | c_id = ".$existingCourse['c_id']." AND |
||
| 2229 | session_id = $sessionId"; |
||
| 2230 | Database::query($sql); |
||
| 2231 | |||
| 2232 | Event::addEvent( |
||
| 2233 | LOG_SESSION_DELETE_COURSE, |
||
| 2234 | LOG_COURSE_ID, |
||
| 2235 | $existingCourse['c_id'], |
||
| 2236 | api_get_utc_datetime(), |
||
| 2237 | api_get_user_id(), |
||
| 2238 | $existingCourse['c_id'], |
||
| 2239 | $sessionId |
||
| 2240 | ); |
||
| 2241 | |||
| 2242 | CourseManager::remove_course_ranking( |
||
| 2243 | $existingCourse['c_id'], |
||
| 2244 | $sessionId |
||
| 2245 | ); |
||
| 2246 | |||
| 2247 | $nbr_courses--; |
||
| 2248 | } |
||
| 2249 | } |
||
| 2250 | } |
||
| 2251 | |||
| 2252 | // Pass through the courses list we want to add to the session |
||
| 2253 | foreach ($courseList as $courseId) { |
||
| 2254 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 2255 | |||
| 2256 | // If course doesn't exists continue! |
||
| 2257 | if (empty($courseInfo)) { |
||
| 2258 | continue; |
||
| 2259 | } |
||
| 2260 | |||
| 2261 | $exists = false; |
||
| 2262 | // check if the course we want to add is already subscribed |
||
| 2263 | foreach ($existingCourses as $existingCourse) { |
||
| 2264 | if ($courseId == $existingCourse['c_id']) { |
||
| 2265 | $exists = true; |
||
| 2266 | } |
||
| 2267 | } |
||
| 2268 | |||
| 2269 | if (!$exists) { |
||
| 2270 | // Copy gradebook categories and links (from base course) |
||
| 2271 | // to the new course session |
||
| 2272 | if ($copyEvaluation) { |
||
| 2273 | $cats = Category::load(null, null, $courseInfo['code']); |
||
| 2274 | if (!empty($cats)) { |
||
| 2275 | $categoryIdList = []; |
||
| 2276 | /** @var Category $cat */ |
||
| 2277 | foreach ($cats as $cat) { |
||
| 2278 | $categoryIdList[$cat->get_id()] = $cat->get_id(); |
||
| 2279 | } |
||
| 2280 | $newCategoryIdList = []; |
||
| 2281 | foreach ($cats as $cat) { |
||
| 2282 | $links = $cat->get_links(null, false, $courseInfo['code'], 0); |
||
| 2283 | |||
| 2284 | $cat->set_session_id($sessionId); |
||
| 2285 | $oldCategoryId= $cat->get_id(); |
||
| 2286 | $newId = $cat->add(); |
||
| 2287 | $newCategoryIdList[$oldCategoryId] = $newId; |
||
| 2288 | $parentId = $cat->get_parent_id(); |
||
| 2289 | |||
| 2290 | if (!empty($parentId)) { |
||
| 2291 | $newParentId = $newCategoryIdList[$parentId]; |
||
| 2292 | $cat->set_parent_id($newParentId); |
||
| 2293 | $cat->save(); |
||
| 2294 | } |
||
| 2295 | |||
| 2296 | /** @var AbstractLink $link */ |
||
| 2297 | foreach ($links as $link) { |
||
| 2298 | $newCategoryId = $newCategoryIdList[$link->get_category_id()]; |
||
| 2299 | $link->set_category_id($newCategoryId); |
||
| 2300 | $link->add(); |
||
| 2301 | } |
||
| 2302 | } |
||
| 2303 | |||
| 2304 | // Create |
||
| 2305 | DocumentManager::generateDefaultCertificate( |
||
| 2306 | $courseInfo, |
||
| 2307 | true, |
||
| 2308 | $sessionId |
||
| 2309 | ); |
||
| 2310 | } |
||
| 2311 | } |
||
| 2312 | |||
| 2313 | // If the course isn't subscribed yet |
||
| 2314 | $sql = "INSERT INTO $tbl_session_rel_course (session_id, c_id, nbr_users, position) |
||
| 2315 | VALUES ($sessionId, $courseId, 0, 0)"; |
||
| 2316 | Database::query($sql); |
||
| 2317 | |||
| 2318 | Event::addEvent( |
||
| 2319 | LOG_SESSION_ADD_COURSE, |
||
| 2320 | LOG_COURSE_ID, |
||
| 2321 | $courseId, |
||
| 2322 | api_get_utc_datetime(), |
||
| 2323 | api_get_user_id(), |
||
| 2324 | $courseId, |
||
| 2325 | $sessionId |
||
| 2326 | ); |
||
| 2327 | |||
| 2328 | // We add the current course in the existing courses array, |
||
| 2329 | // to avoid adding another time the current course |
||
| 2330 | $existingCourses[] = array('c_id' => $courseId); |
||
| 2331 | $nbr_courses++; |
||
| 2332 | |||
| 2333 | // subscribe all the users from the session to this course inside the session |
||
| 2334 | $nbr_users = 0; |
||
| 2335 | View Code Duplication | foreach ($user_list as $enreg_user) { |
|
| 2336 | $enreg_user_id = intval($enreg_user['user_id']); |
||
| 2337 | $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id) |
||
| 2338 | VALUES ($sessionId, $courseId, $enreg_user_id)"; |
||
| 2339 | $result = Database::query($sql); |
||
| 2340 | |||
| 2341 | Event::addEvent( |
||
| 2342 | LOG_SESSION_ADD_USER_COURSE, |
||
| 2343 | LOG_USER_ID, |
||
| 2344 | $enreg_user_id, |
||
| 2345 | api_get_utc_datetime(), |
||
| 2346 | api_get_user_id(), |
||
| 2347 | $courseId, |
||
| 2348 | $sessionId |
||
| 2349 | ); |
||
| 2350 | |||
| 2351 | if (Database::affected_rows($result)) { |
||
| 2352 | $nbr_users++; |
||
| 2353 | } |
||
| 2354 | } |
||
| 2355 | $sql = "UPDATE $tbl_session_rel_course |
||
| 2356 | SET nbr_users = $nbr_users |
||
| 2357 | WHERE session_id = $sessionId AND c_id = $courseId"; |
||
| 2358 | Database::query($sql); |
||
| 2359 | } |
||
| 2360 | } |
||
| 2361 | |||
| 2362 | $sql = "UPDATE $tbl_session |
||
| 2363 | SET nbr_courses = $nbr_courses |
||
| 2364 | WHERE id = $sessionId"; |
||
| 2365 | Database::query($sql); |
||
| 2366 | } |
||
| 2367 | |||
| 2368 | /** |
||
| 2369 | * Unsubscribe course from a session |
||
| 2370 | * |
||
| 2371 | * @param int $session_id |
||
| 2372 | * @param int $course_id |
||
| 2373 | * @return bool True in case of success, false otherwise |
||
| 2374 | */ |
||
| 2375 | public static function unsubscribe_course_from_session($session_id, $course_id) |
||
| 2422 | |||
| 2423 | /** |
||
| 2424 | * Creates a new extra field for a given session |
||
| 2425 | * @param string $variable Field's internal variable name |
||
| 2426 | * @param int $fieldType Field's type |
||
| 2427 | * @param string $displayText Field's language var name |
||
| 2428 | * @return int new extra field id |
||
| 2429 | */ |
||
| 2430 | View Code Duplication | public static function create_session_extra_field($variable, $fieldType, $displayText) |
|
| 2441 | |||
| 2442 | /** |
||
| 2443 | * Update an extra field value for a given session |
||
| 2444 | * @param integer Course ID |
||
| 2445 | * @param string Field variable name |
||
| 2446 | * @param string Field value |
||
| 2447 | * @return boolean true if field updated, false otherwise |
||
| 2448 | */ |
||
| 2449 | View Code Duplication | public static function update_session_extra_field_value($sessionId, $variable, $value = '') |
|
| 2459 | |||
| 2460 | /** |
||
| 2461 | * Checks the relationship between a session and a course. |
||
| 2462 | * @param int $session_id |
||
| 2463 | * @param int $courseId |
||
| 2464 | * @return bool Returns TRUE if the session and the course are related, FALSE otherwise. |
||
| 2465 | * */ |
||
| 2466 | View Code Duplication | public static function relation_session_course_exist($session_id, $courseId) |
|
| 2467 | { |
||
| 2468 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 2469 | $return_value = false; |
||
| 2470 | $sql = "SELECT c_id FROM $tbl_session_course |
||
| 2471 | WHERE |
||
| 2472 | session_id = " . intval($session_id) . " AND |
||
| 2473 | c_id = " . intval($courseId); |
||
| 2474 | $result = Database::query($sql); |
||
| 2475 | $num = Database::num_rows($result); |
||
| 2476 | if ($num > 0) { |
||
| 2477 | $return_value = true; |
||
| 2478 | } |
||
| 2479 | return $return_value; |
||
| 2480 | } |
||
| 2481 | |||
| 2482 | /** |
||
| 2483 | * Get the session information by name |
||
| 2484 | * @param string $session_name |
||
| 2485 | * @return mixed false if the session does not exist, array if the session exist |
||
| 2486 | * */ |
||
| 2487 | public static function get_session_by_name($session_name) |
||
| 2506 | |||
| 2507 | /** |
||
| 2508 | * Create a session category |
||
| 2509 | * @author Jhon Hinojosa <[email protected]>, from existing code |
||
| 2510 | * @param string name |
||
| 2511 | * @param integer year_start |
||
| 2512 | * @param integer month_start |
||
| 2513 | * @param integer day_start |
||
| 2514 | * @param integer year_end |
||
| 2515 | * @param integer month_end |
||
| 2516 | * @param integer day_end |
||
| 2517 | * @return $id_session; |
||
| 2518 | * */ |
||
| 2519 | public static function create_category_session( |
||
| 2576 | |||
| 2577 | /** |
||
| 2578 | * Edit a sessions categories |
||
| 2579 | * @author Jhon Hinojosa <[email protected]>,from existing code |
||
| 2580 | * @param integer id |
||
| 2581 | * @param string name |
||
| 2582 | * @param integer year_start |
||
| 2583 | * @param integer month_start |
||
| 2584 | * @param integer day_start |
||
| 2585 | * @param integer year_end |
||
| 2586 | * @param integer month_end |
||
| 2587 | * @param integer day_end |
||
| 2588 | * @return $id; |
||
| 2589 | * The parameter id is a primary key |
||
| 2590 | * */ |
||
| 2591 | public static function edit_category_session( |
||
| 2645 | |||
| 2646 | /** |
||
| 2647 | * Delete sessions categories |
||
| 2648 | * @author Jhon Hinojosa <[email protected]>, from existing code |
||
| 2649 | * @param array id_checked |
||
| 2650 | * @param bool include delete session |
||
| 2651 | * @param bool optional, true if the function is called by a webservice, false otherwise. |
||
| 2652 | * @return void Nothing, or false on error |
||
| 2653 | * The parameters is a array to delete sessions |
||
| 2654 | * */ |
||
| 2655 | public static function delete_session_category($id_checked, $delete_session = false, $from_ws = false) |
||
| 2697 | |||
| 2698 | /** |
||
| 2699 | * Get a list of sessions of which the given conditions match with an = 'cond' |
||
| 2700 | * @param array $conditions a list of condition example : |
||
| 2701 | * array('status' => STUDENT) or |
||
| 2702 | * array('s.name' => array('operator' => 'LIKE', value = '%$needle%')) |
||
| 2703 | * @param array $order_by a list of fields on which sort |
||
| 2704 | * @return array An array with all sessions of the platform. |
||
| 2705 | * @todo optional course code parameter, optional sorting parameters... |
||
| 2706 | */ |
||
| 2707 | public static function get_sessions_list($conditions = array(), $order_by = array(), $from = null, $to = null) |
||
| 2792 | |||
| 2793 | /** |
||
| 2794 | * Get the session category information by id |
||
| 2795 | * @param string session category ID |
||
| 2796 | * @return mixed false if the session category does not exist, array if the session category exists |
||
| 2797 | */ |
||
| 2798 | View Code Duplication | public static function get_session_category($id) |
|
| 2799 | { |
||
| 2800 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2801 | $id = intval($id); |
||
| 2802 | $sql = "SELECT id, name, date_start, date_end |
||
| 2803 | FROM $tbl_session_category |
||
| 2804 | WHERE id= $id"; |
||
| 2805 | $result = Database::query($sql); |
||
| 2806 | $num = Database::num_rows($result); |
||
| 2807 | if ($num > 0) { |
||
| 2808 | return Database::fetch_array($result); |
||
| 2809 | } else { |
||
| 2810 | return false; |
||
| 2811 | } |
||
| 2812 | } |
||
| 2813 | |||
| 2814 | /** |
||
| 2815 | * Get all session categories (filter by access_url_id) |
||
| 2816 | * @return mixed false if the session category does not exist, array if the session category exists |
||
| 2817 | */ |
||
| 2818 | public static function get_all_session_category() |
||
| 2833 | |||
| 2834 | /** |
||
| 2835 | * Assign a coach to course in session with status = 2 |
||
| 2836 | * @param int $user_id |
||
| 2837 | * @param int $session_id |
||
| 2838 | * @param int $courseId |
||
| 2839 | * @param bool $nocoach optional, if is true the user don't be a coach now, |
||
| 2840 | * otherwise it'll assign a coach |
||
| 2841 | * @return bool true if there are affected rows, otherwise false |
||
| 2842 | */ |
||
| 2843 | public static function set_coach_to_course_session( |
||
| 2958 | |||
| 2959 | /** |
||
| 2960 | * @param int $sessionId |
||
| 2961 | * @return bool |
||
| 2962 | */ |
||
| 2963 | View Code Duplication | public static function removeAllDrhFromSession($sessionId) |
|
| 2981 | |||
| 2982 | /** |
||
| 2983 | * Subscribes sessions to human resource manager (Dashboard feature) |
||
| 2984 | * @param array $userInfo Human Resource Manager info |
||
| 2985 | * @param array $sessions_list Sessions id |
||
| 2986 | * @param bool $sendEmail |
||
| 2987 | * @param bool $removeSessionsFromUser |
||
| 2988 | * @return int |
||
| 2989 | * */ |
||
| 2990 | public static function subscribeSessionsToDrh( |
||
| 3069 | |||
| 3070 | /** |
||
| 3071 | * @param int $sessionId |
||
| 3072 | * @return array |
||
| 3073 | */ |
||
| 3074 | public static function getDrhUsersInSession($sessionId) |
||
| 3078 | |||
| 3079 | /** |
||
| 3080 | * @param int $userId |
||
| 3081 | * @param int $sessionId |
||
| 3082 | * @return array |
||
| 3083 | */ |
||
| 3084 | public static function getSessionFollowedByDrh($userId, $sessionId) |
||
| 3125 | |||
| 3126 | /** |
||
| 3127 | * Get sessions followed by human resources manager |
||
| 3128 | * @param int $userId |
||
| 3129 | * @param int $start |
||
| 3130 | * @param int $limit |
||
| 3131 | * @param bool $getCount |
||
| 3132 | * @param bool $getOnlySessionId |
||
| 3133 | * @param bool $getSql |
||
| 3134 | * @param string $orderCondition |
||
| 3135 | * @param string $description |
||
| 3136 | * |
||
| 3137 | * @return array sessions |
||
| 3138 | */ |
||
| 3139 | public static function get_sessions_followed_by_drh( |
||
| 3163 | |||
| 3164 | /** |
||
| 3165 | * Get sessions followed by human resources manager |
||
| 3166 | * @param int $userId |
||
| 3167 | * @param int $status Optional |
||
| 3168 | * @param int $start |
||
| 3169 | * @param int $limit |
||
| 3170 | * @param bool $getCount |
||
| 3171 | * @param bool $getOnlySessionId |
||
| 3172 | * @param bool $getSql |
||
| 3173 | * @param string $orderCondition |
||
| 3174 | * @param string $keyword |
||
| 3175 | * @param string $description |
||
| 3176 | * @return array sessions |
||
| 3177 | */ |
||
| 3178 | public static function getSessionsFollowedByUser( |
||
| 3347 | |||
| 3348 | /** |
||
| 3349 | * Gets the list (or the count) of courses by session filtered by access_url |
||
| 3350 | * @param int $session_id The session id |
||
| 3351 | * @param string $course_name The course code |
||
| 3352 | * @param string $orderBy Field to order the data |
||
| 3353 | * @param boolean $getCount Optional. Count the session courses |
||
| 3354 | * @return array|int List of courses. Whether $getCount is true, return the count |
||
| 3355 | */ |
||
| 3356 | public static function get_course_list_by_session_id( |
||
| 3414 | |||
| 3415 | /** |
||
| 3416 | * Gets the list of courses by session filtered by access_url |
||
| 3417 | * |
||
| 3418 | * @param $userId |
||
| 3419 | * @param $sessionId |
||
| 3420 | * @param null $from |
||
| 3421 | * @param null $limit |
||
| 3422 | * @param null $column |
||
| 3423 | * @param null $direction |
||
| 3424 | * @param bool $getCount |
||
| 3425 | * @return array |
||
| 3426 | */ |
||
| 3427 | public static function getAllCoursesFollowedByUser( |
||
| 3428 | $userId, |
||
| 3429 | $sessionId, |
||
| 3430 | $from = null, |
||
| 3431 | $limit = null, |
||
| 3432 | $column = null, |
||
| 3433 | $direction = null, |
||
| 3434 | $getCount = false, |
||
| 3435 | $keyword = null |
||
| 3436 | ) { |
||
| 3437 | if (empty($sessionId)) { |
||
| 3438 | $sessionsSQL = SessionManager::get_sessions_followed_by_drh( |
||
| 3439 | $userId, |
||
| 3440 | null, |
||
| 3441 | null, |
||
| 3442 | null, |
||
| 3443 | true, |
||
| 3444 | true |
||
| 3445 | ); |
||
| 3446 | } else { |
||
| 3447 | $sessionsSQL = intval($sessionId); |
||
| 3448 | } |
||
| 3449 | |||
| 3450 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3451 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3452 | |||
| 3453 | if ($getCount) { |
||
| 3454 | $select = "SELECT COUNT(DISTINCT(c.code)) as count "; |
||
| 3455 | } else { |
||
| 3456 | $select = "SELECT DISTINCT c.* "; |
||
| 3457 | } |
||
| 3458 | |||
| 3459 | $keywordCondition = null; |
||
| 3460 | if (!empty($keyword)) { |
||
| 3461 | $keyword = Database::escape_string($keyword); |
||
| 3462 | $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) "; |
||
| 3463 | } |
||
| 3464 | |||
| 3465 | // Select the courses |
||
| 3466 | $sql = "$select |
||
| 3467 | FROM $tbl_course c |
||
| 3468 | INNER JOIN $tbl_session_rel_course src |
||
| 3469 | ON c.id = src.c_id |
||
| 3470 | WHERE |
||
| 3471 | src.session_id IN ($sessionsSQL) |
||
| 3472 | $keywordCondition |
||
| 3473 | "; |
||
| 3474 | View Code Duplication | if ($getCount) { |
|
| 3475 | $result = Database::query($sql); |
||
| 3476 | $row = Database::fetch_array($result,'ASSOC'); |
||
| 3477 | return $row['count']; |
||
| 3478 | } |
||
| 3479 | |||
| 3480 | View Code Duplication | if (isset($from) && isset($limit)) { |
|
| 3481 | $from = intval($from); |
||
| 3482 | $limit = intval($limit); |
||
| 3483 | $sql .= " LIMIT $from, $limit"; |
||
| 3484 | } |
||
| 3485 | |||
| 3486 | $result = Database::query($sql); |
||
| 3487 | $num_rows = Database::num_rows($result); |
||
| 3488 | $courses = array(); |
||
| 3489 | |||
| 3490 | if ($num_rows > 0) { |
||
| 3491 | while ($row = Database::fetch_array($result,'ASSOC')) { |
||
| 3492 | $courses[$row['id']] = $row; |
||
| 3493 | } |
||
| 3494 | } |
||
| 3495 | |||
| 3496 | return $courses; |
||
| 3497 | } |
||
| 3498 | |||
| 3499 | /** |
||
| 3500 | * Gets the list of courses by session filtered by access_url |
||
| 3501 | * @param int $session_id |
||
| 3502 | * @param string $course_name |
||
| 3503 | * @return array list of courses |
||
| 3504 | */ |
||
| 3505 | public static function get_course_list_by_session_id_like($session_id, $course_name = '') |
||
| 3539 | |||
| 3540 | |||
| 3541 | /** |
||
| 3542 | * Gets the count of courses by session filtered by access_url |
||
| 3543 | * @param int session id |
||
| 3544 | * @return array list of courses |
||
| 3545 | */ |
||
| 3546 | public static function getCourseCountBySessionId($session_id, $keyword = null) |
||
| 3575 | |||
| 3576 | /** |
||
| 3577 | * Get the session id based on the original id and field name in the extra fields. |
||
| 3578 | * Returns 0 if session was not found |
||
| 3579 | * |
||
| 3580 | * @param string $value Original session id |
||
| 3581 | * @param string $variable Original field name |
||
| 3582 | * @return int Session id |
||
| 3583 | */ |
||
| 3584 | public static function getSessionIdFromOriginalId($value, $variable) |
||
| 3598 | |||
| 3599 | /** |
||
| 3600 | * Get users by session |
||
| 3601 | * @param int $id session id |
||
| 3602 | * @param int $status filter by status coach = 2 |
||
| 3603 | * @param bool $getCount Optional. Allow get the number of rows from the result |
||
| 3604 | * @return array|int A list with an user list. If $getCount is true then return a the count of registers |
||
| 3605 | */ |
||
| 3606 | public static function get_users_by_session($id, $status = null, $getCount = false) |
||
| 3657 | |||
| 3658 | /** |
||
| 3659 | * The general coach (field: session.id_coach) |
||
| 3660 | * @param int $user_id user id |
||
| 3661 | * @param boolean $asPlatformAdmin The user is platform admin, return everything |
||
| 3662 | * @return array |
||
| 3663 | */ |
||
| 3664 | public static function get_sessions_by_general_coach($user_id, $asPlatformAdmin = false) |
||
| 3698 | |||
| 3699 | /** |
||
| 3700 | * @param int $user_id |
||
| 3701 | * @return array |
||
| 3702 | * @deprecated use get_sessions_by_general_coach() |
||
| 3703 | */ |
||
| 3704 | public static function get_sessions_by_coach($user_id) |
||
| 3709 | |||
| 3710 | /** |
||
| 3711 | * @param int $user_id |
||
| 3712 | * @param int $courseId |
||
| 3713 | * @param int $session_id |
||
| 3714 | * @return array|bool |
||
| 3715 | */ |
||
| 3716 | View Code Duplication | public static function get_user_status_in_course_session($user_id, $courseId, $session_id) |
|
| 3737 | |||
| 3738 | /** |
||
| 3739 | * Gets user status within a session |
||
| 3740 | * @param int $user_id |
||
| 3741 | * @param int $courseId |
||
| 3742 | * @param $session_id |
||
| 3743 | * @return int |
||
| 3744 | * @assert (null,null,null) === false |
||
| 3745 | */ |
||
| 3746 | public static function get_user_status_in_session($user_id, $courseId, $session_id) |
||
| 3767 | |||
| 3768 | /** |
||
| 3769 | * @param int $id |
||
| 3770 | * @return array |
||
| 3771 | */ |
||
| 3772 | public static function get_all_sessions_by_promotion($id) |
||
| 3777 | |||
| 3778 | /** |
||
| 3779 | * @param int $promotion_id |
||
| 3780 | * @param array $list |
||
| 3781 | */ |
||
| 3782 | public static function subscribe_sessions_to_promotion($promotion_id, $list) |
||
| 3797 | |||
| 3798 | /** |
||
| 3799 | * Updates a session status |
||
| 3800 | * @param int session id |
||
| 3801 | * @param int status |
||
| 3802 | */ |
||
| 3803 | public static function set_session_status($session_id, $status) |
||
| 3809 | |||
| 3810 | /** |
||
| 3811 | * Copies a session with the same data to a new session. |
||
| 3812 | * The new copy is not assigned to the same promotion. @see subscribe_sessions_to_promotions() for that |
||
| 3813 | * @param int Session ID |
||
| 3814 | * @param bool Whether to copy the relationship with courses |
||
| 3815 | * @param bool Whether to copy the relationship with users |
||
| 3816 | * @param bool New courses will be created |
||
| 3817 | * @param bool Whether to set exercises and learning paths in the new session to invisible by default |
||
| 3818 | * @return int The new session ID on success, 0 otherwise |
||
| 3819 | * @todo make sure the extra session fields are copied too |
||
| 3820 | */ |
||
| 3821 | public static function copy( |
||
| 3966 | |||
| 3967 | /** |
||
| 3968 | * @param int $user_id |
||
| 3969 | * @param int $session_id |
||
| 3970 | * @return bool |
||
| 3971 | */ |
||
| 3972 | static function user_is_general_coach($user_id, $session_id) |
||
| 3986 | |||
| 3987 | /** |
||
| 3988 | * Get the number of sessions |
||
| 3989 | * @param int ID of the URL we want to filter on (optional) |
||
| 3990 | * @return int Number of sessions |
||
| 3991 | */ |
||
| 3992 | View Code Duplication | public static function count_sessions($access_url_id = null) |
|
| 4005 | |||
| 4006 | /** |
||
| 4007 | * Protect a session to be edited. |
||
| 4008 | * @param int $id |
||
| 4009 | * @param bool $checkSession |
||
| 4010 | * @return mixed | bool true if pass the check, api_not_allowed otherwise |
||
| 4011 | */ |
||
| 4012 | public static function protectSession($id, $checkSession = true) |
||
| 4032 | |||
| 4033 | /** |
||
| 4034 | * @param int $id |
||
| 4035 | * @return bool |
||
| 4036 | */ |
||
| 4037 | private static function allowed($id) |
||
| 4069 | |||
| 4070 | /** |
||
| 4071 | * @return bool |
||
| 4072 | */ |
||
| 4073 | public static function allowToManageSessions() |
||
| 4088 | |||
| 4089 | /** |
||
| 4090 | * @return bool |
||
| 4091 | */ |
||
| 4092 | public static function allowOnlyMySessions() |
||
| 4103 | |||
| 4104 | /** |
||
| 4105 | * @return bool |
||
| 4106 | */ |
||
| 4107 | public static function allowManageAllSessions() |
||
| 4115 | |||
| 4116 | /** |
||
| 4117 | * @param $id |
||
| 4118 | * @return bool |
||
| 4119 | */ |
||
| 4120 | public static function protect_teacher_session_edit($id) |
||
| 4128 | |||
| 4129 | /** |
||
| 4130 | * @param int $courseId |
||
| 4131 | * @return array |
||
| 4132 | */ |
||
| 4133 | public static function get_session_by_course($courseId) |
||
| 4146 | |||
| 4147 | /** |
||
| 4148 | * @param int $user_id |
||
| 4149 | * @param bool $ignoreVisibilityForAdmins |
||
| 4150 | * @param bool $ignoreTimeLimit |
||
| 4151 | * |
||
| 4152 | * @return array |
||
| 4153 | */ |
||
| 4154 | public static function get_sessions_by_user($user_id, $ignoreVisibilityForAdmins = false, $ignoreTimeLimit = false) |
||
| 4176 | |||
| 4177 | /** |
||
| 4178 | * @param string $file |
||
| 4179 | * @param bool $updateSession options: |
||
| 4180 | * true: if the session exists it will be updated. |
||
| 4181 | * false: if session exists a new session will be created adding a counter session1, session2, etc |
||
| 4182 | * @param int $defaultUserId |
||
| 4183 | * @param mixed $logger |
||
| 4184 | * @param array $extraFields convert a file row to an extra field. Example in CSV file there's a SessionID then it will |
||
| 4185 | * converted to extra_external_session_id if you set this: array('SessionId' => 'extra_external_session_id') |
||
| 4186 | * @param string $extraFieldId |
||
| 4187 | * @param int $daysCoachAccessBeforeBeginning |
||
| 4188 | * @param int $daysCoachAccessAfterBeginning |
||
| 4189 | * @param int $sessionVisibility |
||
| 4190 | * @param array $fieldsToAvoidUpdate |
||
| 4191 | * @param bool $deleteUsersNotInList |
||
| 4192 | * @param bool $updateCourseCoaches |
||
| 4193 | * @param bool $sessionWithCoursesModifier |
||
| 4194 | * @param int $showDescription |
||
| 4195 | * @param array $teacherBackupList |
||
| 4196 | * @param array $groupBackup |
||
| 4197 | * @return array |
||
| 4198 | */ |
||
| 4199 | public static function importCSV( |
||
| 4200 | $file, |
||
| 4201 | $updateSession, |
||
| 4202 | $defaultUserId = null, |
||
| 4203 | $logger = null, |
||
| 4204 | $extraFields = array(), |
||
| 4205 | $extraFieldId = null, |
||
| 4206 | $daysCoachAccessBeforeBeginning = null, |
||
| 4207 | $daysCoachAccessAfterBeginning = null, |
||
| 4208 | $sessionVisibility = 1, |
||
| 4209 | $fieldsToAvoidUpdate = array(), |
||
| 4210 | $deleteUsersNotInList = false, |
||
| 4211 | $updateCourseCoaches = false, |
||
| 4212 | $sessionWithCoursesModifier = false, |
||
| 4213 | $addOriginalCourseTeachersAsCourseSessionCoaches = true, |
||
| 4214 | $removeAllTeachersFromCourse = true, |
||
| 4215 | $showDescription = null, |
||
| 4216 | &$teacherBackupList = array(), |
||
| 4217 | &$groupBackup = array() |
||
| 4218 | ) { |
||
| 4219 | $content = file($file); |
||
| 4220 | |||
| 4221 | $error_message = null; |
||
| 4222 | $session_counter = 0; |
||
| 4223 | $defaultUserId = empty($defaultUserId) ? api_get_user_id() : (int) $defaultUserId; |
||
| 4224 | |||
| 4225 | $eol = PHP_EOL; |
||
| 4226 | if (PHP_SAPI != 'cli') { |
||
| 4227 | $eol = '<br />'; |
||
| 4228 | } |
||
| 4229 | |||
| 4230 | $debug = false; |
||
| 4231 | if (isset($logger)) { |
||
| 4232 | $debug = true; |
||
| 4233 | } |
||
| 4234 | |||
| 4235 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 4236 | $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 4237 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 4238 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4239 | |||
| 4240 | $sessions = array(); |
||
| 4241 | |||
| 4242 | if (!api_strstr($content[0], ';')) { |
||
| 4243 | $error_message = get_lang('NotCSV'); |
||
| 4244 | } else { |
||
| 4245 | $tag_names = array(); |
||
| 4246 | |||
| 4247 | foreach ($content as $key => $enreg) { |
||
| 4248 | $enreg = explode(';', trim($enreg)); |
||
| 4249 | View Code Duplication | if ($key) { |
|
| 4250 | foreach ($tag_names as $tag_key => $tag_name) { |
||
| 4251 | if (isset($enreg[$tag_key])) { |
||
| 4252 | $sessions[$key - 1][$tag_name] = $enreg[$tag_key]; |
||
| 4253 | } |
||
| 4254 | } |
||
| 4255 | } else { |
||
| 4256 | foreach ($enreg as $tag_name) { |
||
| 4257 | $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\-]/', '', $tag_name); |
||
| 4258 | } |
||
| 4259 | if (!in_array('SessionName', $tag_names) || |
||
| 4260 | !in_array('DateStart', $tag_names) || |
||
| 4261 | !in_array('DateEnd', $tag_names) |
||
| 4262 | ) { |
||
| 4263 | $error_message = get_lang('NoNeededData'); |
||
| 4264 | break; |
||
| 4265 | } |
||
| 4266 | } |
||
| 4267 | } |
||
| 4268 | |||
| 4269 | $sessionList = array(); |
||
| 4270 | // Looping the sessions. |
||
| 4271 | foreach ($sessions as $enreg) { |
||
| 4272 | $user_counter = 0; |
||
| 4273 | $course_counter = 0; |
||
| 4274 | |||
| 4275 | if (isset($extraFields) && !empty($extraFields)) { |
||
| 4276 | foreach ($extraFields as $original => $to) { |
||
| 4277 | $enreg[$to] = isset($enreg[$original]) ? $enreg[$original] : null; |
||
| 4278 | } |
||
| 4279 | } |
||
| 4280 | |||
| 4281 | $session_name = $enreg['SessionName']; |
||
| 4282 | // Default visibility |
||
| 4283 | $visibilityAfterExpirationPerSession = $sessionVisibility; |
||
| 4284 | |||
| 4285 | if (isset($enreg['VisibilityAfterExpiration'])) { |
||
| 4286 | $visibility = $enreg['VisibilityAfterExpiration']; |
||
| 4287 | switch ($visibility) { |
||
| 4288 | case 'read_only': |
||
| 4289 | $visibilityAfterExpirationPerSession = SESSION_VISIBLE_READ_ONLY; |
||
| 4290 | break; |
||
| 4291 | case 'accessible': |
||
| 4292 | $visibilityAfterExpirationPerSession = SESSION_VISIBLE; |
||
| 4293 | break; |
||
| 4294 | case 'not_accessible': |
||
| 4295 | $visibilityAfterExpirationPerSession = SESSION_INVISIBLE; |
||
| 4296 | break; |
||
| 4297 | } |
||
| 4298 | } |
||
| 4299 | |||
| 4300 | if (empty($session_name)) { |
||
| 4301 | continue; |
||
| 4302 | } |
||
| 4303 | |||
| 4304 | // We assume the dates are already in UTC |
||
| 4305 | $dateStart = explode('/', $enreg['DateStart']); |
||
| 4306 | $dateEnd = explode('/', $enreg['DateEnd']); |
||
| 4307 | $dateStart = $dateStart[0].'-'.$dateStart[1].'-'.$dateStart[2].' 00:00:00'; |
||
| 4308 | $dateEnd = $dateEnd[0].'-'.$dateEnd[1].'-'.$dateEnd[2].' 23:59:59'; |
||
| 4309 | |||
| 4310 | $session_category_id = isset($enreg['SessionCategory']) ? $enreg['SessionCategory'] : null; |
||
| 4311 | $sessionDescription = isset($enreg['SessionDescription']) ? $enreg['SessionDescription'] : null; |
||
| 4312 | |||
| 4313 | $extraParameters = null; |
||
| 4314 | if (!is_null($showDescription)) { |
||
| 4315 | $extraParameters .= ' , show_description = '.intval($showDescription); |
||
| 4316 | } |
||
| 4317 | |||
| 4318 | $coachBefore = ''; |
||
| 4319 | $coachAfter = ''; |
||
| 4320 | |||
| 4321 | if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) { |
||
| 4322 | $date = new \DateTime($dateStart); |
||
| 4323 | $interval = new DateInterval( |
||
| 4324 | 'P'.$daysCoachAccessBeforeBeginning.'D' |
||
| 4325 | ); |
||
| 4326 | $date->sub($interval); |
||
| 4327 | $coachBefore = $date->format('Y-m-d h:i'); |
||
| 4328 | $coachBefore = api_get_utc_datetime($coachBefore); |
||
| 4329 | |||
| 4330 | $extraParameters .= " , coach_access_start_date = '$coachBefore'"; |
||
| 4331 | |||
| 4332 | $date = new \DateTime($dateEnd); |
||
| 4333 | $interval = new DateInterval('P'.$daysCoachAccessAfterBeginning.'D'); |
||
| 4334 | $date->add($interval); |
||
| 4335 | $coachAfter = $date->format('Y-m-d h:i'); |
||
| 4336 | |||
| 4337 | $coachAfter = api_get_utc_datetime($coachAfter); |
||
| 4338 | $extraParameters .= " , coach_access_end_date = '$coachAfter'"; |
||
| 4339 | } |
||
| 4340 | |||
| 4341 | $dateStart = api_get_utc_datetime($dateStart); |
||
| 4342 | $dateEnd = api_get_utc_datetime($dateEnd); |
||
| 4343 | |||
| 4344 | $extraSessionParameters = null; |
||
| 4345 | if (!empty($sessionDescription)) { |
||
| 4346 | $extraSessionParameters = " , description = '".Database::escape_string($sessionDescription)."'"; |
||
| 4347 | } |
||
| 4348 | |||
| 4349 | $sessionCondition = ''; |
||
| 4350 | if (!empty($session_category_id)) { |
||
| 4351 | $sessionCondition = " , session_category_id = '$session_category_id' "; |
||
| 4352 | } |
||
| 4353 | |||
| 4354 | // Searching a general coach. |
||
| 4355 | if (!empty($enreg['Coach'])) { |
||
| 4356 | $coach_id = UserManager::get_user_id_from_username($enreg['Coach']); |
||
| 4357 | if ($coach_id === false) { |
||
| 4358 | // If the coach-user does not exist - I'm the coach. |
||
| 4359 | $coach_id = $defaultUserId; |
||
| 4360 | } |
||
| 4361 | } else { |
||
| 4362 | $coach_id = $defaultUserId; |
||
| 4363 | } |
||
| 4364 | |||
| 4365 | if (!$updateSession) { |
||
| 4366 | // Always create a session. |
||
| 4367 | $unique_name = false; |
||
| 4368 | $i = 0; |
||
| 4369 | // Change session name, verify that session doesn't exist. |
||
| 4370 | $suffix = null; |
||
| 4371 | View Code Duplication | while (!$unique_name) { |
|
| 4372 | if ($i > 1) { |
||
| 4373 | $suffix = ' - ' . $i; |
||
| 4374 | } |
||
| 4375 | $sql = 'SELECT 1 FROM ' . $tbl_session . ' |
||
| 4376 | WHERE name="' . Database::escape_string($session_name). $suffix . '"'; |
||
| 4377 | $rs = Database::query($sql); |
||
| 4378 | |||
| 4379 | if (Database::result($rs, 0, 0)) { |
||
| 4380 | $i++; |
||
| 4381 | } else { |
||
| 4382 | $unique_name = true; |
||
| 4383 | $session_name .= $suffix; |
||
| 4384 | } |
||
| 4385 | } |
||
| 4386 | |||
| 4387 | // Creating the session. |
||
| 4388 | $sql = "INSERT IGNORE INTO $tbl_session SET |
||
| 4389 | name = '" . Database::escape_string($session_name). "', |
||
| 4390 | id_coach = '$coach_id', |
||
| 4391 | access_start_date = '$dateStart', |
||
| 4392 | access_end_date = '$dateEnd', |
||
| 4393 | display_start_date = '$dateStart', |
||
| 4394 | display_end_date = '$dateEnd', |
||
| 4395 | visibility = '$visibilityAfterExpirationPerSession', |
||
| 4396 | session_admin_id = " . $defaultUserId . " |
||
| 4397 | $sessionCondition $extraParameters $extraSessionParameters"; |
||
| 4398 | Database::query($sql); |
||
| 4399 | |||
| 4400 | $session_id = Database::insert_id(); |
||
| 4401 | if ($debug) { |
||
| 4402 | if ($session_id) { |
||
| 4403 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4404 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4405 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4406 | } |
||
| 4407 | } |
||
| 4408 | |||
| 4409 | $logger->addInfo("Sessions - Session created: #$session_id - $session_name"); |
||
| 4410 | } else { |
||
| 4411 | $logger->addError("Sessions - Session NOT created: $session_name"); |
||
| 4412 | } |
||
| 4413 | } |
||
| 4414 | $session_counter++; |
||
| 4415 | } else { |
||
| 4416 | $sessionId = null; |
||
| 4417 | if (isset($extraFields) && !empty($extraFields) && !empty($enreg['extra_'.$extraFieldId])) { |
||
| 4418 | $sessionId = self::getSessionIdFromOriginalId($enreg['extra_'.$extraFieldId], $extraFieldId); |
||
| 4419 | if (empty($sessionId)) { |
||
| 4420 | $my_session_result = false; |
||
| 4421 | } else { |
||
| 4422 | $my_session_result = true; |
||
| 4423 | } |
||
| 4424 | } else { |
||
| 4425 | $my_session_result = self::get_session_by_name($enreg['SessionName']); |
||
| 4426 | } |
||
| 4427 | |||
| 4428 | if ($my_session_result === false) { |
||
| 4429 | |||
| 4430 | // Creating a session. |
||
| 4431 | $sql = "INSERT IGNORE INTO $tbl_session SET |
||
| 4432 | name = '$session_name', |
||
| 4433 | id_coach = '$coach_id', |
||
| 4434 | access_start_date = '$dateStart', |
||
| 4435 | access_end_date = '$dateEnd', |
||
| 4436 | display_start_date = '$dateStart', |
||
| 4437 | display_end_date = '$dateEnd', |
||
| 4438 | visibility = '$visibilityAfterExpirationPerSession' |
||
| 4439 | $extraParameters |
||
| 4440 | $extraSessionParameters |
||
| 4441 | $sessionCondition |
||
| 4442 | "; |
||
| 4443 | |||
| 4444 | Database::query($sql); |
||
| 4445 | |||
| 4446 | // We get the last insert id. |
||
| 4447 | $my_session_result = SessionManager::get_session_by_name($enreg['SessionName']); |
||
| 4448 | $session_id = $my_session_result['id']; |
||
| 4449 | |||
| 4450 | if ($session_id) { |
||
| 4451 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4452 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4453 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4454 | } |
||
| 4455 | } |
||
| 4456 | if ($debug) { |
||
| 4457 | $logger->addInfo("Sessions - #$session_id created: $session_name"); |
||
| 4458 | } |
||
| 4459 | |||
| 4460 | // Delete session-user relation only for students |
||
| 4461 | $sql = "DELETE FROM $tbl_session_user |
||
| 4462 | WHERE session_id = '$session_id' AND relation_type <> " . SESSION_RELATION_TYPE_RRHH; |
||
| 4463 | Database::query($sql); |
||
| 4464 | |||
| 4465 | $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'"; |
||
| 4466 | Database::query($sql); |
||
| 4467 | |||
| 4468 | // Delete session-course-user relationships students and coaches. |
||
| 4469 | View Code Duplication | if ($updateCourseCoaches) { |
|
| 4470 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4471 | WHERE session_id = '$session_id' AND status in ('0', '2')"; |
||
| 4472 | Database::query($sql); |
||
| 4473 | } else { |
||
| 4474 | // Delete session-course-user relation ships *only* for students. |
||
| 4475 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4476 | WHERE session_id = '$session_id' AND status <> 2"; |
||
| 4477 | Database::query($sql); |
||
| 4478 | } |
||
| 4479 | } |
||
| 4480 | } else { |
||
| 4481 | // Updating the session. |
||
| 4482 | $params = array( |
||
| 4483 | 'id_coach' => $coach_id, |
||
| 4484 | 'access_start_date' => $dateStart, |
||
| 4485 | 'access_end_date' => $dateEnd, |
||
| 4486 | 'display_start_date' => $dateStart, |
||
| 4487 | 'display_end_date' => $dateEnd, |
||
| 4488 | 'visibility' => $visibilityAfterExpirationPerSession, |
||
| 4489 | 'session_category_id' => $session_category_id |
||
| 4490 | ); |
||
| 4491 | |||
| 4492 | if (!empty($sessionDescription)) { |
||
| 4493 | $params['description'] = $sessionDescription; |
||
| 4494 | } |
||
| 4495 | |||
| 4496 | if (!empty($fieldsToAvoidUpdate)) { |
||
| 4497 | foreach ($fieldsToAvoidUpdate as $field) { |
||
| 4498 | unset($params[$field]); |
||
| 4499 | } |
||
| 4500 | } |
||
| 4501 | |||
| 4502 | if (isset($sessionId) && !empty($sessionId)) { |
||
| 4503 | $session_id = $sessionId; |
||
| 4504 | if (!empty($enreg['SessionName'])) { |
||
| 4505 | ///$params['name'] = $enreg['SessionName']; |
||
| 4506 | $sessionName = Database::escape_string($enreg['SessionName']); |
||
| 4507 | $sql = "UPDATE $tbl_session SET name = '$sessionName' WHERE id = $session_id"; |
||
| 4508 | Database::query($sql); |
||
| 4509 | } |
||
| 4510 | } else { |
||
| 4511 | $my_session_result = SessionManager::get_session_by_name($session_name); |
||
| 4512 | $session_id = $my_session_result['id']; |
||
| 4513 | } |
||
| 4514 | |||
| 4515 | if ($debug) { |
||
| 4516 | $logger->addError("Sessions - Session #$session_id to be updated: '$session_name'"); |
||
| 4517 | } |
||
| 4518 | |||
| 4519 | if ($session_id) { |
||
| 4520 | if ($debug) { |
||
| 4521 | $logger->addError("Sessions - Session to be updated #$session_id"); |
||
| 4522 | } |
||
| 4523 | |||
| 4524 | $sessionInfo = api_get_session_info($session_id); |
||
| 4525 | $params['show_description'] = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : intval($showDescription); |
||
| 4526 | |||
| 4527 | if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) { |
||
| 4528 | View Code Duplication | if (empty($sessionInfo['nb_days_access_before_beginning']) || |
|
| 4529 | (!empty($sessionInfo['nb_days_access_before_beginning']) && |
||
| 4530 | $sessionInfo['nb_days_access_before_beginning'] < $daysCoachAccessBeforeBeginning) |
||
| 4531 | ) { |
||
| 4532 | $params['coach_access_start_date'] = $coachBefore; |
||
| 4533 | } |
||
| 4534 | |||
| 4535 | View Code Duplication | if (empty($sessionInfo['nb_days_access_after_end']) || |
|
| 4536 | (!empty($sessionInfo['nb_days_access_after_end']) && |
||
| 4537 | $sessionInfo['nb_days_access_after_end'] < $daysCoachAccessAfterBeginning) |
||
| 4538 | ) { |
||
| 4539 | $params['coach_access_end_date'] = $coachAfter; |
||
| 4540 | } |
||
| 4541 | } |
||
| 4542 | |||
| 4543 | Database::update($tbl_session, $params, array('id = ?' => $session_id)); |
||
| 4544 | |||
| 4545 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4546 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4547 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4548 | } |
||
| 4549 | } |
||
| 4550 | |||
| 4551 | // Delete session-user relation only for students |
||
| 4552 | $sql = "DELETE FROM $tbl_session_user |
||
| 4553 | WHERE session_id = '$session_id' AND relation_type <> " . SESSION_RELATION_TYPE_RRHH; |
||
| 4554 | Database::query($sql); |
||
| 4555 | |||
| 4556 | $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'"; |
||
| 4557 | Database::query($sql); |
||
| 4558 | |||
| 4559 | // Delete session-course-user relationships students and coaches. |
||
| 4560 | View Code Duplication | if ($updateCourseCoaches) { |
|
| 4561 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4562 | WHERE session_id = '$session_id' AND status in ('0', '2')"; |
||
| 4563 | Database::query($sql); |
||
| 4564 | } else { |
||
| 4565 | // Delete session-course-user relation ships *only* for students. |
||
| 4566 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4567 | WHERE session_id = '$session_id' AND status <> 2"; |
||
| 4568 | Database::query($sql); |
||
| 4569 | } |
||
| 4570 | } else { |
||
| 4571 | if ($debug) { |
||
| 4572 | $logger->addError( |
||
| 4573 | "Sessions - Session not found" |
||
| 4574 | ); |
||
| 4575 | } |
||
| 4576 | } |
||
| 4577 | } |
||
| 4578 | $session_counter++; |
||
| 4579 | } |
||
| 4580 | |||
| 4581 | $sessionList[] = $session_id; |
||
| 4582 | $users = explode('|', $enreg['Users']); |
||
| 4583 | |||
| 4584 | // Adding the relationship "Session - User" for students |
||
| 4585 | $userList = array(); |
||
| 4586 | |||
| 4587 | if (is_array($users)) { |
||
| 4588 | foreach ($users as $user) { |
||
| 4589 | $user_id = UserManager::get_user_id_from_username($user); |
||
| 4590 | if ($user_id !== false) { |
||
| 4591 | $userList[] = $user_id; |
||
| 4592 | // Insert new users. |
||
| 4593 | $sql = "INSERT IGNORE INTO $tbl_session_user SET |
||
| 4594 | user_id = '$user_id', |
||
| 4595 | session_id = '$session_id', |
||
| 4596 | registered_at = '" . api_get_utc_datetime() . "'"; |
||
| 4597 | Database::query($sql); |
||
| 4598 | if ($debug) { |
||
| 4599 | $logger->addInfo("Sessions - Adding User #$user_id ($user) to session #$session_id"); |
||
| 4600 | } |
||
| 4601 | $user_counter++; |
||
| 4602 | } |
||
| 4603 | } |
||
| 4604 | } |
||
| 4605 | |||
| 4606 | if ($deleteUsersNotInList) { |
||
| 4607 | // Getting user in DB in order to compare to the new list. |
||
| 4608 | $usersListInDatabase = self::get_users_by_session($session_id, 0); |
||
| 4609 | |||
| 4610 | if (!empty($usersListInDatabase)) { |
||
| 4611 | if (empty($userList)) { |
||
| 4612 | foreach ($usersListInDatabase as $userInfo) { |
||
| 4613 | self::unsubscribe_user_from_session($session_id, $userInfo['user_id']); |
||
| 4614 | } |
||
| 4615 | } else { |
||
| 4616 | foreach ($usersListInDatabase as $userInfo) { |
||
| 4617 | if (!in_array($userInfo['user_id'], $userList)) { |
||
| 4618 | self::unsubscribe_user_from_session($session_id, $userInfo['user_id']); |
||
| 4619 | } |
||
| 4620 | } |
||
| 4621 | } |
||
| 4622 | } |
||
| 4623 | } |
||
| 4624 | |||
| 4625 | $courses = explode('|', $enreg['Courses']); |
||
| 4626 | |||
| 4627 | // See BT#6449 |
||
| 4628 | $onlyAddFirstCoachOrTeacher = false; |
||
| 4629 | |||
| 4630 | if ($sessionWithCoursesModifier) { |
||
| 4631 | if (count($courses) >= 2) { |
||
| 4632 | // Only first teacher in course session; |
||
| 4633 | $onlyAddFirstCoachOrTeacher = true; |
||
| 4634 | |||
| 4635 | // Remove all teachers from course. |
||
| 4636 | $removeAllTeachersFromCourse = false; |
||
| 4637 | } |
||
| 4638 | } |
||
| 4639 | |||
| 4640 | foreach ($courses as $course) { |
||
| 4641 | $courseArray = bracketsToArray($course); |
||
| 4642 | $course_code = $courseArray[0]; |
||
| 4643 | |||
| 4644 | if (CourseManager::course_exists($course_code)) { |
||
| 4645 | |||
| 4646 | $courseInfo = api_get_course_info($course_code); |
||
| 4647 | $courseId = $courseInfo['real_id']; |
||
| 4648 | |||
| 4649 | // Adding the course to a session. |
||
| 4650 | $sql = "INSERT IGNORE INTO $tbl_session_course |
||
| 4651 | SET c_id = '$courseId', session_id='$session_id'"; |
||
| 4652 | Database::query($sql); |
||
| 4653 | |||
| 4654 | SessionManager::installCourse($session_id, $courseInfo['real_id']); |
||
| 4655 | |||
| 4656 | if ($debug) { |
||
| 4657 | $logger->addInfo("Sessions - Adding course '$course_code' to session #$session_id"); |
||
| 4658 | } |
||
| 4659 | |||
| 4660 | $course_counter++; |
||
| 4661 | |||
| 4662 | $course_coaches = isset($courseArray[1]) ? $courseArray[1] : null; |
||
| 4663 | $course_users = isset($courseArray[2]) ? $courseArray[2] : null; |
||
| 4664 | |||
| 4665 | $course_users = explode(',', $course_users); |
||
| 4666 | $course_coaches = explode(',', $course_coaches); |
||
| 4667 | |||
| 4668 | // Checking if the flag is set TeachersWillBeAddedAsCoachInAllCourseSessions (course_edit.php) |
||
| 4669 | $addTeachersToSession = true; |
||
| 4670 | |||
| 4671 | if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) { |
||
| 4672 | $addTeachersToSession = $courseInfo['add_teachers_to_sessions_courses']; |
||
| 4673 | } |
||
| 4674 | |||
| 4675 | // If any user provided for a course, use the users array. |
||
| 4676 | if (empty($course_users)) { |
||
| 4677 | if (!empty($userList)) { |
||
| 4678 | SessionManager::subscribe_users_to_session_course( |
||
| 4679 | $userList, |
||
| 4680 | $session_id, |
||
| 4681 | $course_code |
||
| 4682 | ); |
||
| 4683 | if ($debug) { |
||
| 4684 | $msg = "Sessions - Adding student list ".implode(', #', $userList)." to course: '$course_code' and session #$session_id"; |
||
| 4685 | $logger->addInfo($msg); |
||
| 4686 | } |
||
| 4687 | } |
||
| 4688 | } |
||
| 4689 | |||
| 4690 | // Adding coaches to session course user. |
||
| 4691 | if (!empty($course_coaches)) { |
||
| 4692 | $savedCoaches = array(); |
||
| 4693 | // only edit if add_teachers_to_sessions_courses is set. |
||
| 4694 | if ($addTeachersToSession) { |
||
| 4695 | if ($addOriginalCourseTeachersAsCourseSessionCoaches) { |
||
| 4696 | // Adding course teachers as course session teachers. |
||
| 4697 | $alreadyAddedTeachers = CourseManager::get_teacher_list_from_course_code( |
||
| 4698 | $course_code |
||
| 4699 | ); |
||
| 4700 | |||
| 4701 | if (!empty($alreadyAddedTeachers)) { |
||
| 4702 | $teachersToAdd = array(); |
||
| 4703 | foreach ($alreadyAddedTeachers as $user) { |
||
| 4704 | $teachersToAdd[] = $user['username']; |
||
| 4705 | } |
||
| 4706 | $course_coaches = array_merge( |
||
| 4707 | $course_coaches, |
||
| 4708 | $teachersToAdd |
||
| 4709 | ); |
||
| 4710 | } |
||
| 4711 | } |
||
| 4712 | |||
| 4713 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4714 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4715 | if ($coach_id !== false) { |
||
| 4716 | // Just insert new coaches |
||
| 4717 | SessionManager::updateCoaches($session_id, $courseId, array($coach_id), false); |
||
| 4718 | |||
| 4719 | if ($debug) { |
||
| 4720 | $logger->addInfo("Sessions - Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id"); |
||
| 4721 | } |
||
| 4722 | $savedCoaches[] = $coach_id; |
||
| 4723 | } else { |
||
| 4724 | $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol; |
||
| 4725 | } |
||
| 4726 | } |
||
| 4727 | } |
||
| 4728 | |||
| 4729 | // Custom courses/session coaches |
||
| 4730 | $teacherToAdd = null; |
||
| 4731 | // Only one coach is added. |
||
| 4732 | if ($onlyAddFirstCoachOrTeacher == true) { |
||
| 4733 | |||
| 4734 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4735 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4736 | if ($coach_id !== false) { |
||
| 4737 | $teacherToAdd = $coach_id; |
||
| 4738 | break; |
||
| 4739 | } |
||
| 4740 | } |
||
| 4741 | |||
| 4742 | // Un subscribe everyone that's not in the list. |
||
| 4743 | $teacherList = CourseManager::get_teacher_list_from_course_code($course_code); |
||
| 4744 | View Code Duplication | if (!empty($teacherList)) { |
|
| 4745 | foreach ($teacherList as $teacher) { |
||
| 4746 | if ($teacherToAdd != $teacher['user_id']) { |
||
| 4747 | |||
| 4748 | $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." |
||
| 4749 | WHERE |
||
| 4750 | user_id = ".$teacher['user_id']." AND |
||
| 4751 | course_code = '".$course_code."' |
||
| 4752 | "; |
||
| 4753 | |||
| 4754 | $result = Database::query($sql); |
||
| 4755 | $userCourseData = Database::fetch_array($result, 'ASSOC'); |
||
| 4756 | $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData; |
||
| 4757 | |||
| 4758 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)." |
||
| 4759 | WHERE |
||
| 4760 | user_id = ".$teacher['user_id']." AND |
||
| 4761 | c_id = '".$courseInfo['real_id']."' |
||
| 4762 | "; |
||
| 4763 | |||
| 4764 | $result = Database::query($sql); |
||
| 4765 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4766 | $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4767 | } |
||
| 4768 | |||
| 4769 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)." |
||
| 4770 | WHERE |
||
| 4771 | user_id = ".$teacher['user_id']." AND |
||
| 4772 | c_id = '".$courseInfo['real_id']."' |
||
| 4773 | "; |
||
| 4774 | |||
| 4775 | $result = Database::query($sql); |
||
| 4776 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4777 | $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4778 | } |
||
| 4779 | |||
| 4780 | CourseManager::unsubscribe_user( |
||
| 4781 | $teacher['user_id'], |
||
| 4782 | $course_code |
||
| 4783 | ); |
||
| 4784 | } |
||
| 4785 | } |
||
| 4786 | } |
||
| 4787 | |||
| 4788 | if (!empty($teacherToAdd)) { |
||
| 4789 | SessionManager::updateCoaches($session_id, $courseId, array($teacherToAdd), true); |
||
| 4790 | |||
| 4791 | $userCourseCategory = ''; |
||
| 4792 | View Code Duplication | if (isset($teacherBackupList[$teacherToAdd]) && |
|
| 4793 | isset($teacherBackupList[$teacherToAdd][$course_code]) |
||
| 4794 | ) { |
||
| 4795 | $courseUserData = $teacherBackupList[$teacherToAdd][$course_code]; |
||
| 4796 | $userCourseCategory = $courseUserData['user_course_cat']; |
||
| 4797 | } |
||
| 4798 | |||
| 4799 | CourseManager::subscribe_user( |
||
| 4800 | $teacherToAdd, |
||
| 4801 | $course_code, |
||
| 4802 | COURSEMANAGER, |
||
| 4803 | 0, |
||
| 4804 | $userCourseCategory |
||
| 4805 | ); |
||
| 4806 | |||
| 4807 | View Code Duplication | if (isset($groupBackup['user'][$teacherToAdd]) && |
|
| 4808 | isset($groupBackup['user'][$teacherToAdd][$course_code]) && |
||
| 4809 | !empty($groupBackup['user'][$teacherToAdd][$course_code]) |
||
| 4810 | ) { |
||
| 4811 | foreach ($groupBackup['user'][$teacherToAdd][$course_code] as $data) { |
||
| 4812 | GroupManager::subscribe_users( |
||
| 4813 | $teacherToAdd, |
||
| 4814 | $data['group_id'], |
||
| 4815 | $data['c_id'] |
||
| 4816 | ); |
||
| 4817 | } |
||
| 4818 | } |
||
| 4819 | |||
| 4820 | View Code Duplication | if (isset($groupBackup['tutor'][$teacherToAdd]) && |
|
| 4821 | isset($groupBackup['tutor'][$teacherToAdd][$course_code]) && |
||
| 4822 | !empty($groupBackup['tutor'][$teacherToAdd][$course_code]) |
||
| 4823 | ) { |
||
| 4824 | foreach ($groupBackup['tutor'][$teacherToAdd][$course_code] as $data) { |
||
| 4825 | GroupManager::subscribe_tutors( |
||
| 4826 | $teacherToAdd, |
||
| 4827 | $data['group_id'], |
||
| 4828 | $data['c_id'] |
||
| 4829 | ); |
||
| 4830 | } |
||
| 4831 | } |
||
| 4832 | } |
||
| 4833 | } |
||
| 4834 | |||
| 4835 | // See BT#6449#note-195 |
||
| 4836 | // All coaches are added. |
||
| 4837 | if ($removeAllTeachersFromCourse) { |
||
| 4838 | $teacherToAdd = null; |
||
| 4839 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4840 | $coach_id = UserManager::get_user_id_from_username( |
||
| 4841 | $course_coach |
||
| 4842 | ); |
||
| 4843 | if ($coach_id !== false) { |
||
| 4844 | $teacherToAdd[] = $coach_id; |
||
| 4845 | } |
||
| 4846 | } |
||
| 4847 | |||
| 4848 | if (!empty($teacherToAdd)) { |
||
| 4849 | // Deleting all course teachers and adding the only coach as teacher. |
||
| 4850 | $teacherList = CourseManager::get_teacher_list_from_course_code($course_code); |
||
| 4851 | |||
| 4852 | View Code Duplication | if (!empty($teacherList)) { |
|
| 4853 | foreach ($teacherList as $teacher) { |
||
| 4854 | if (!in_array($teacher['user_id'], $teacherToAdd)) { |
||
| 4855 | |||
| 4856 | $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." |
||
| 4857 | WHERE |
||
| 4858 | user_id = ".$teacher['user_id']." AND |
||
| 4859 | course_code = '".$course_code."' |
||
| 4860 | "; |
||
| 4861 | |||
| 4862 | $result = Database::query($sql); |
||
| 4863 | $userCourseData = Database::fetch_array($result, 'ASSOC'); |
||
| 4864 | $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData; |
||
| 4865 | |||
| 4866 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)." |
||
| 4867 | WHERE |
||
| 4868 | user_id = ".$teacher['user_id']." AND |
||
| 4869 | c_id = '".$courseInfo['real_id']."' |
||
| 4870 | "; |
||
| 4871 | |||
| 4872 | $result = Database::query($sql); |
||
| 4873 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4874 | $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4875 | } |
||
| 4876 | |||
| 4877 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)." |
||
| 4878 | WHERE |
||
| 4879 | user_id = ".$teacher['user_id']." AND |
||
| 4880 | c_id = '".$courseInfo['real_id']."' |
||
| 4881 | "; |
||
| 4882 | |||
| 4883 | $result = Database::query($sql); |
||
| 4884 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4885 | $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4886 | } |
||
| 4887 | |||
| 4888 | CourseManager::unsubscribe_user( |
||
| 4889 | $teacher['user_id'], |
||
| 4890 | $course_code |
||
| 4891 | ); |
||
| 4892 | } |
||
| 4893 | } |
||
| 4894 | } |
||
| 4895 | |||
| 4896 | foreach ($teacherToAdd as $teacherId) { |
||
| 4897 | $userCourseCategory = ''; |
||
| 4898 | View Code Duplication | if (isset($teacherBackupList[$teacherId]) && |
|
| 4899 | isset($teacherBackupList[$teacherId][$course_code]) |
||
| 4900 | ) { |
||
| 4901 | $courseUserData = $teacherBackupList[$teacherId][$course_code]; |
||
| 4902 | $userCourseCategory = $courseUserData['user_course_cat']; |
||
| 4903 | } |
||
| 4904 | |||
| 4905 | CourseManager::subscribe_user( |
||
| 4906 | $teacherId, |
||
| 4907 | $course_code, |
||
| 4908 | COURSEMANAGER, |
||
| 4909 | 0, |
||
| 4910 | $userCourseCategory |
||
| 4911 | ); |
||
| 4912 | |||
| 4913 | View Code Duplication | if (isset($groupBackup['user'][$teacherId]) && |
|
| 4914 | isset($groupBackup['user'][$teacherId][$course_code]) && |
||
| 4915 | !empty($groupBackup['user'][$teacherId][$course_code]) |
||
| 4916 | ) { |
||
| 4917 | foreach ($groupBackup['user'][$teacherId][$course_code] as $data) { |
||
| 4918 | GroupManager::subscribe_users( |
||
| 4919 | $teacherId, |
||
| 4920 | $data['group_id'], |
||
| 4921 | $data['c_id'] |
||
| 4922 | ); |
||
| 4923 | } |
||
| 4924 | } |
||
| 4925 | |||
| 4926 | View Code Duplication | if (isset($groupBackup['tutor'][$teacherId]) && |
|
| 4927 | isset($groupBackup['tutor'][$teacherId][$course_code]) && |
||
| 4928 | !empty($groupBackup['tutor'][$teacherId][$course_code]) |
||
| 4929 | ) { |
||
| 4930 | foreach ($groupBackup['tutor'][$teacherId][$course_code] as $data) { |
||
| 4931 | GroupManager::subscribe_tutors( |
||
| 4932 | $teacherId, |
||
| 4933 | $data['group_id'], |
||
| 4934 | $data['c_id'] |
||
| 4935 | ); |
||
| 4936 | } |
||
| 4937 | } |
||
| 4938 | } |
||
| 4939 | } |
||
| 4940 | } |
||
| 4941 | |||
| 4942 | // Continue default behaviour. |
||
| 4943 | if ($onlyAddFirstCoachOrTeacher == false) { |
||
| 4944 | // Checking one more time see BT#6449#note-149 |
||
| 4945 | $coaches = SessionManager::getCoachesByCourseSession($session_id, $courseId); |
||
| 4946 | // Update coaches if only there's 1 course see BT#6449#note-189 |
||
| 4947 | if (empty($coaches) || count($courses) == 1) { |
||
| 4948 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4949 | $course_coach = trim($course_coach); |
||
| 4950 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4951 | if ($coach_id !== false) { |
||
| 4952 | // Just insert new coaches |
||
| 4953 | SessionManager::updateCoaches( |
||
| 4954 | $session_id, |
||
| 4955 | $courseId, |
||
| 4956 | array($coach_id), |
||
| 4957 | false |
||
| 4958 | ); |
||
| 4959 | |||
| 4960 | if ($debug) { |
||
| 4961 | $logger->addInfo("Sessions - Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id"); |
||
| 4962 | } |
||
| 4963 | $savedCoaches[] = $coach_id; |
||
| 4964 | } else { |
||
| 4965 | $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol; |
||
| 4966 | } |
||
| 4967 | } |
||
| 4968 | } |
||
| 4969 | } |
||
| 4970 | } |
||
| 4971 | |||
| 4972 | // Adding Students, updating relationship "Session - Course - User". |
||
| 4973 | $course_users = array_filter($course_users); |
||
| 4974 | |||
| 4975 | if (!empty($course_users)) { |
||
| 4976 | View Code Duplication | foreach ($course_users as $user) { |
|
| 4977 | $user_id = UserManager::get_user_id_from_username($user); |
||
| 4978 | |||
| 4979 | if ($user_id !== false) { |
||
| 4980 | SessionManager::subscribe_users_to_session_course( |
||
| 4981 | array($user_id), |
||
| 4982 | $session_id, |
||
| 4983 | $course_code |
||
| 4984 | ); |
||
| 4985 | if ($debug) { |
||
| 4986 | $logger->addInfo("Sessions - Adding student: user #$user_id ($user) to course: '$course_code' and session #$session_id"); |
||
| 4987 | } |
||
| 4988 | } else { |
||
| 4989 | $error_message .= get_lang('UserDoesNotExist').': '.$user.$eol; |
||
| 4990 | } |
||
| 4991 | } |
||
| 4992 | } |
||
| 4993 | |||
| 4994 | $inserted_in_course[$course_code] = $courseInfo['title']; |
||
| 4995 | } |
||
| 4996 | } |
||
| 4997 | $access_url_id = api_get_current_access_url_id(); |
||
| 4998 | UrlManager::add_session_to_url($session_id, $access_url_id); |
||
| 4999 | $sql = "UPDATE $tbl_session SET nbr_users = '$user_counter', nbr_courses = '$course_counter' WHERE id = '$session_id'"; |
||
| 5000 | Database::query($sql); |
||
| 5001 | } |
||
| 5002 | } |
||
| 5003 | |||
| 5004 | return array( |
||
| 5005 | 'error_message' => $error_message, |
||
| 5006 | 'session_counter' => $session_counter, |
||
| 5007 | 'session_list' => $sessionList, |
||
| 5008 | ); |
||
| 5009 | } |
||
| 5010 | |||
| 5011 | /** |
||
| 5012 | * @param int $sessionId |
||
| 5013 | * @param int $courseId |
||
| 5014 | * @return array |
||
| 5015 | */ |
||
| 5016 | View Code Duplication | public static function getCoachesByCourseSession($sessionId, $courseId) |
|
| 5038 | |||
| 5039 | /** |
||
| 5040 | * @param int $sessionId |
||
| 5041 | * @param int $courseId |
||
| 5042 | * @return string |
||
| 5043 | */ |
||
| 5044 | public static function getCoachesByCourseSessionToString( |
||
| 5062 | |||
| 5063 | /** |
||
| 5064 | * Get all coaches added in the session - course relationship |
||
| 5065 | * @param int $sessionId |
||
| 5066 | * @return array |
||
| 5067 | */ |
||
| 5068 | public static function getCoachesBySession($sessionId) |
||
| 5087 | |||
| 5088 | /** |
||
| 5089 | * @param int $userId |
||
| 5090 | * @return array |
||
| 5091 | */ |
||
| 5092 | public static function getAllCoursesFromAllSessionFromDrh($userId) |
||
| 5106 | |||
| 5107 | /** |
||
| 5108 | * @param string $status |
||
| 5109 | * @param int $userId |
||
| 5110 | * @param bool $getCount |
||
| 5111 | * @param int $from |
||
| 5112 | * @param int $numberItems |
||
| 5113 | * @param int $column |
||
| 5114 | * @param string $direction |
||
| 5115 | * @param string $keyword |
||
| 5116 | * @param string $active |
||
| 5117 | * @param string $lastConnectionDate |
||
| 5118 | * @param array $sessionIdList |
||
| 5119 | * @param array $studentIdList |
||
| 5120 | * @param int $filterByStatus |
||
| 5121 | * @return array|int |
||
| 5122 | */ |
||
| 5123 | public static function getAllUsersFromCoursesFromAllSessionFromStatus( |
||
| 5124 | $status, |
||
| 5125 | $userId, |
||
| 5126 | $getCount = false, |
||
| 5127 | $from = null, |
||
| 5128 | $numberItems = null, |
||
| 5129 | $column = 1, |
||
| 5130 | $direction = 'asc', |
||
| 5131 | $keyword = null, |
||
| 5132 | $active = null, |
||
| 5133 | $lastConnectionDate = null, |
||
| 5134 | $sessionIdList = array(), |
||
| 5135 | $studentIdList = array(), |
||
| 5136 | $filterByStatus = null |
||
| 5137 | ) { |
||
| 5138 | $filterByStatus = intval($filterByStatus); |
||
| 5139 | |||
| 5140 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 5141 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 5142 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 5143 | $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 5144 | $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 5145 | $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 5146 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 5147 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 5148 | |||
| 5149 | $direction = in_array(strtolower($direction), array('asc', 'desc')) ? $direction : 'asc'; |
||
| 5150 | $column = Database::escape_string($column); |
||
| 5151 | $userId = intval($userId); |
||
| 5152 | |||
| 5153 | $limitCondition = null; |
||
| 5154 | |||
| 5155 | View Code Duplication | if (isset($from) && isset($numberItems)) { |
|
| 5156 | $from = intval($from); |
||
| 5157 | $numberItems = intval($numberItems); |
||
| 5158 | $limitCondition = "LIMIT $from, $numberItems"; |
||
| 5159 | } |
||
| 5160 | |||
| 5161 | $urlId = api_get_current_access_url_id(); |
||
| 5162 | |||
| 5163 | $sessionConditions = null; |
||
| 5164 | $courseConditions = null; |
||
| 5165 | $userConditions = null; |
||
| 5166 | |||
| 5167 | if (isset($active)) { |
||
| 5168 | $active = intval($active); |
||
| 5169 | $userConditions .= " AND active = $active"; |
||
| 5170 | } |
||
| 5171 | |||
| 5172 | $courseList = CourseManager::get_courses_followed_by_drh($userId, DRH); |
||
| 5173 | $courseConditions = ' AND 1 <> 1'; |
||
| 5174 | if (!empty($courseList)) { |
||
| 5175 | $courseIdList = array_column($courseList, 'id'); |
||
| 5176 | $courseConditions = ' AND c.id IN ("'.implode('","', $courseIdList).'")'; |
||
| 5177 | } |
||
| 5178 | |||
| 5179 | $userConditionsFromDrh = ''; |
||
| 5180 | |||
| 5181 | // Classic DRH |
||
| 5182 | if (empty($studentIdList)) { |
||
| 5183 | $studentListSql = UserManager::get_users_followed_by_drh( |
||
| 5184 | $userId, |
||
| 5185 | $filterByStatus, |
||
| 5186 | true, |
||
| 5187 | false |
||
| 5188 | ); |
||
| 5189 | $studentIdList = array_keys($studentListSql); |
||
| 5190 | $studentListSql = "'".implode("','", $studentIdList)."'"; |
||
| 5191 | } else { |
||
| 5192 | $studentIdList = array_map('intval', $studentIdList); |
||
| 5193 | $studentListSql = "'".implode("','", $studentIdList)."'"; |
||
| 5194 | } |
||
| 5195 | if (!empty($studentListSql)) { |
||
| 5196 | $userConditionsFromDrh = " AND u.user_id IN (".$studentListSql.") "; |
||
| 5197 | } |
||
| 5198 | |||
| 5199 | switch ($status) { |
||
| 5200 | case 'drh': |
||
| 5201 | break; |
||
| 5202 | case 'drh_all': |
||
| 5203 | // Show all by DRH |
||
| 5204 | if (empty($sessionIdList)) { |
||
| 5205 | $sessionsListSql = SessionManager::get_sessions_followed_by_drh( |
||
| 5206 | $userId, |
||
| 5207 | null, |
||
| 5208 | null, |
||
| 5209 | false, |
||
| 5210 | true, |
||
| 5211 | true |
||
| 5212 | ); |
||
| 5213 | } else { |
||
| 5214 | $sessionIdList = array_map('intval', $sessionIdList); |
||
| 5215 | $sessionsListSql = "'".implode("','", $sessionIdList)."'"; |
||
| 5216 | } |
||
| 5217 | if (!empty($sessionsListSql)) { |
||
| 5218 | $sessionConditions = " AND s.id IN (".$sessionsListSql.") "; |
||
| 5219 | } |
||
| 5220 | break; |
||
| 5221 | case 'session_admin': |
||
| 5222 | $sessionConditions = " AND s.id_coach = $userId "; |
||
| 5223 | $userConditionsFromDrh = ''; |
||
| 5224 | break; |
||
| 5225 | case 'admin': |
||
| 5226 | break; |
||
| 5227 | case 'teacher': |
||
| 5228 | $sessionConditions = " AND s.id_coach = $userId "; |
||
| 5229 | $userConditionsFromDrh = ''; |
||
| 5230 | break; |
||
| 5231 | } |
||
| 5232 | |||
| 5233 | $select = "SELECT DISTINCT u.* "; |
||
| 5234 | $masterSelect = "SELECT DISTINCT * FROM "; |
||
| 5235 | |||
| 5236 | if ($getCount) { |
||
| 5237 | $select = "SELECT DISTINCT u.user_id "; |
||
| 5238 | $masterSelect = "SELECT COUNT(DISTINCT(user_id)) as count FROM "; |
||
| 5239 | } |
||
| 5240 | |||
| 5241 | if (!empty($filterByStatus)) { |
||
| 5242 | $userConditions .= " AND u.status = ".$filterByStatus; |
||
| 5243 | } |
||
| 5244 | |||
| 5245 | if (!empty($lastConnectionDate)) { |
||
| 5246 | $lastConnectionDate = Database::escape_string($lastConnectionDate); |
||
| 5247 | $userConditions .= " AND u.last_login <= '$lastConnectionDate' "; |
||
| 5248 | } |
||
| 5249 | |||
| 5250 | View Code Duplication | if (!empty($keyword)) { |
|
| 5251 | $keyword = Database::escape_string($keyword); |
||
| 5252 | $userConditions .= " AND ( |
||
| 5253 | u.username LIKE '%$keyword%' OR |
||
| 5254 | u.firstname LIKE '%$keyword%' OR |
||
| 5255 | u.lastname LIKE '%$keyword%' OR |
||
| 5256 | u.official_code LIKE '%$keyword%' OR |
||
| 5257 | u.email LIKE '%$keyword%' |
||
| 5258 | )"; |
||
| 5259 | } |
||
| 5260 | |||
| 5261 | $where = " WHERE |
||
| 5262 | access_url_id = $urlId |
||
| 5263 | $userConditions |
||
| 5264 | "; |
||
| 5265 | |||
| 5266 | $userUnion = ''; |
||
| 5267 | if (!empty($userConditionsFromDrh)) { |
||
| 5268 | $userUnion = " |
||
| 5269 | UNION ( |
||
| 5270 | $select |
||
| 5271 | FROM $tbl_user u |
||
| 5272 | INNER JOIN $tbl_user_rel_access_url url ON (url.user_id = u.id) |
||
| 5273 | $where |
||
| 5274 | $userConditionsFromDrh |
||
| 5275 | )"; |
||
| 5276 | } |
||
| 5277 | |||
| 5278 | $sql = "$masterSelect ( |
||
| 5279 | ($select |
||
| 5280 | FROM $tbl_session s |
||
| 5281 | INNER JOIN $tbl_session_rel_course_rel_user su ON (s.id = su.session_id) |
||
| 5282 | INNER JOIN $tbl_user u ON (u.user_id = su.user_id) |
||
| 5283 | INNER JOIN $tbl_session_rel_access_url url ON (url.session_id = s.id) |
||
| 5284 | $where |
||
| 5285 | $sessionConditions |
||
| 5286 | ) UNION ( |
||
| 5287 | $select |
||
| 5288 | FROM $tbl_course c |
||
| 5289 | INNER JOIN $tbl_course_user cu ON (cu.c_id = c.id) |
||
| 5290 | INNER JOIN $tbl_user u ON (u.user_id = cu.user_id) |
||
| 5291 | INNER JOIN $tbl_course_rel_access_url url ON (url.c_id = c.id) |
||
| 5292 | $where |
||
| 5293 | $courseConditions |
||
| 5294 | ) $userUnion |
||
| 5295 | ) as t1 |
||
| 5296 | "; |
||
| 5297 | |||
| 5298 | if ($getCount) { |
||
| 5299 | $result = Database::query($sql); |
||
| 5300 | $count = 0; |
||
| 5301 | if (Database::num_rows($result)) { |
||
| 5302 | $rows = Database::fetch_array($result); |
||
| 5303 | $count = $rows['count']; |
||
| 5304 | } |
||
| 5305 | return $count; |
||
| 5306 | } |
||
| 5307 | |||
| 5308 | View Code Duplication | if (!empty($column) && !empty($direction)) { |
|
| 5309 | $column = str_replace('u.', '', $column); |
||
| 5310 | $sql .= " ORDER BY $column $direction "; |
||
| 5311 | } |
||
| 5312 | |||
| 5313 | $sql .= $limitCondition; |
||
| 5314 | $result = Database::query($sql); |
||
| 5315 | $result = Database::store_result($result); |
||
| 5316 | |||
| 5317 | return $result ; |
||
| 5318 | } |
||
| 5319 | |||
| 5320 | /** |
||
| 5321 | * @param int $sessionId |
||
| 5322 | * @param int $courseId |
||
| 5323 | * @param array $coachList |
||
| 5324 | * @param bool $deleteCoachesNotInList |
||
| 5325 | */ |
||
| 5326 | public static function updateCoaches( |
||
| 5359 | |||
| 5360 | /** |
||
| 5361 | * @param array $sessions |
||
| 5362 | * @param array $sessionsDestination |
||
| 5363 | * @return string |
||
| 5364 | */ |
||
| 5365 | public static function copyStudentsFromSession($sessions, $sessionsDestination) |
||
| 5413 | |||
| 5414 | /** |
||
| 5415 | * Assign coaches of a session(s) as teachers to a given course (or courses) |
||
| 5416 | * @param array A list of session IDs |
||
| 5417 | * @param array A list of course IDs |
||
| 5418 | * @return string |
||
| 5419 | */ |
||
| 5420 | public static function copyCoachesFromSessionToCourse($sessions, $courses) |
||
| 5479 | |||
| 5480 | /** |
||
| 5481 | * @param string $keyword |
||
| 5482 | * @param string $active |
||
| 5483 | * @param string $lastConnectionDate |
||
| 5484 | * @param array $sessionIdList |
||
| 5485 | * @param array $studentIdList |
||
| 5486 | * @param int $userStatus STUDENT|COURSEMANAGER constants |
||
| 5487 | * |
||
| 5488 | * @return array|int |
||
| 5489 | */ |
||
| 5490 | public static function getCountUserTracking( |
||
| 5542 | |||
| 5543 | /** |
||
| 5544 | * Get teachers followed by a user |
||
| 5545 | * @param int $userId |
||
| 5546 | * @param int $active |
||
| 5547 | * @param string $lastConnectionDate |
||
| 5548 | * @param bool $getCount |
||
| 5549 | * @param array $sessionIdList |
||
| 5550 | * @return array|int |
||
| 5551 | */ |
||
| 5552 | public static function getTeacherTracking( |
||
| 5553 | $userId, |
||
| 5554 | $active = 1, |
||
| 5555 | $lastConnectionDate = null, |
||
| 5556 | $getCount = false, |
||
| 5557 | $sessionIdList = array() |
||
| 5558 | ) { |
||
| 5559 | $teacherListId = array(); |
||
| 5560 | |||
| 5561 | if (api_is_drh() || api_is_platform_admin()) { |
||
| 5562 | // Followed teachers by drh |
||
| 5563 | if (api_drh_can_access_all_session_content()) { |
||
| 5564 | if (empty($sessionIdList)) { |
||
| 5565 | $sessions = SessionManager::get_sessions_followed_by_drh($userId); |
||
| 5566 | $sessionIdList = array(); |
||
| 5567 | foreach ($sessions as $session) { |
||
| 5568 | $sessionIdList[] = $session['id']; |
||
| 5569 | } |
||
| 5570 | } |
||
| 5571 | |||
| 5572 | $sessionIdList = array_map('intval', $sessionIdList); |
||
| 5573 | $sessionToString = implode("', '", $sessionIdList); |
||
| 5574 | |||
| 5575 | $course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 5576 | $sessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 5577 | $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 5578 | |||
| 5579 | // Select the teachers. |
||
| 5580 | $sql = "SELECT DISTINCT(cu.user_id) FROM $course c |
||
| 5581 | INNER JOIN $sessionCourse src ON c.id = src.c_id |
||
| 5582 | INNER JOIN $courseUser cu ON (cu.c_id = c.id) |
||
| 5583 | WHERE src.session_id IN ('$sessionToString') AND cu.status = 1"; |
||
| 5584 | $result = Database::query($sql); |
||
| 5585 | while($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 5586 | $teacherListId[$row['user_id']] = $row['user_id']; |
||
| 5587 | } |
||
| 5588 | } else { |
||
| 5589 | $teacherResult = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER); |
||
| 5590 | foreach ($teacherResult as $userInfo) { |
||
| 5591 | $teacherListId[] = $userInfo['user_id']; |
||
| 5592 | } |
||
| 5593 | } |
||
| 5594 | } |
||
| 5595 | |||
| 5596 | if (!empty($teacherListId)) { |
||
| 5597 | $tableUser = Database::get_main_table(TABLE_MAIN_USER); |
||
| 5598 | |||
| 5599 | $select = "SELECT DISTINCT u.* "; |
||
| 5600 | if ($getCount) { |
||
| 5601 | $select = "SELECT count(DISTINCT(u.user_id)) as count"; |
||
| 5602 | } |
||
| 5603 | |||
| 5604 | $sql = "$select FROM $tableUser u"; |
||
| 5605 | |||
| 5606 | if (!empty($lastConnectionDate)) { |
||
| 5607 | $tableLogin = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 5608 | //$sql .= " INNER JOIN $tableLogin l ON (l.login_user_id = u.user_id) "; |
||
| 5609 | } |
||
| 5610 | $active = intval($active); |
||
| 5611 | $teacherListId = implode("','", $teacherListId); |
||
| 5612 | $where = " WHERE u.active = $active AND u.user_id IN ('$teacherListId') "; |
||
| 5613 | |||
| 5614 | if (!empty($lastConnectionDate)) { |
||
| 5615 | $lastConnectionDate = Database::escape_string($lastConnectionDate); |
||
| 5616 | //$where .= " AND l.login_date <= '$lastConnectionDate' "; |
||
| 5617 | } |
||
| 5618 | |||
| 5619 | $sql .= $where; |
||
| 5620 | $result = Database::query($sql); |
||
| 5621 | if (Database::num_rows($result)) { |
||
| 5622 | if ($getCount) { |
||
| 5623 | $row = Database::fetch_array($result); |
||
| 5624 | return $row['count']; |
||
| 5625 | } else { |
||
| 5626 | |||
| 5627 | return Database::store_result($result, 'ASSOC'); |
||
| 5628 | } |
||
| 5629 | } |
||
| 5630 | } |
||
| 5631 | |||
| 5632 | return 0; |
||
| 5633 | } |
||
| 5634 | |||
| 5635 | /** |
||
| 5636 | * Get the list of course tools that have to be dealt with in case of |
||
| 5637 | * registering any course to a session |
||
| 5638 | * @return array The list of tools to be dealt with (literal names) |
||
| 5639 | */ |
||
| 5640 | public static function getCourseToolToBeManaged() |
||
| 5647 | |||
| 5648 | /** |
||
| 5649 | * Calls the methods bound to each tool when a course is registered into a session |
||
| 5650 | * @param int $sessionId |
||
| 5651 | * @param int $courseId |
||
| 5652 | * @return void |
||
| 5653 | */ |
||
| 5654 | View Code Duplication | public static function installCourse($sessionId, $courseId) |
|
| 5666 | |||
| 5667 | /** |
||
| 5668 | * Calls the methods bound to each tool when a course is unregistered from |
||
| 5669 | * a session |
||
| 5670 | * @param int $sessionId |
||
| 5671 | * @param int $courseId |
||
| 5672 | */ |
||
| 5673 | View Code Duplication | public static function unInstallCourse($sessionId, $courseId) |
|
| 5685 | |||
| 5686 | /** |
||
| 5687 | * @param int $sessionId |
||
| 5688 | * @param int $courseId |
||
| 5689 | */ |
||
| 5690 | public static function addCourseIntroduction($sessionId, $courseId) |
||
| 5691 | { |
||
| 5692 | // @todo create a tool intro lib |
||
| 5693 | $sessionId = intval($sessionId); |
||
| 5694 | $courseId = intval($courseId); |
||
| 5695 | |||
| 5696 | $TBL_INTRODUCTION = Database::get_course_table(TABLE_TOOL_INTRO); |
||
| 5697 | $sql = "SELECT * FROM $TBL_INTRODUCTION WHERE c_id = $courseId"; |
||
| 5698 | $result = Database::query($sql); |
||
| 5699 | $result = Database::store_result($result, 'ASSOC'); |
||
| 5700 | |||
| 5701 | if (!empty($result)) { |
||
| 5702 | foreach ($result as $result) { |
||
| 5703 | // @todo check if relation exits. |
||
| 5704 | $result['session_id'] = $sessionId; |
||
| 5705 | Database::insert($TBL_INTRODUCTION, $result); |
||
| 5706 | } |
||
| 5707 | } |
||
| 5708 | } |
||
| 5709 | |||
| 5710 | /** |
||
| 5711 | * @param int $sessionId |
||
| 5712 | * @param int $courseId |
||
| 5713 | */ |
||
| 5714 | public static function removeCourseIntroduction($sessionId, $courseId) |
||
| 5723 | |||
| 5724 | /** |
||
| 5725 | * @param int $sessionId |
||
| 5726 | * @param int $courseId |
||
| 5727 | */ |
||
| 5728 | public static function addCourseDescription($sessionId, $courseId) |
||
| 5735 | |||
| 5736 | /** |
||
| 5737 | * @param int $sessionId |
||
| 5738 | * @param int $courseId |
||
| 5739 | */ |
||
| 5740 | public static function removeCourseDescription($sessionId, $courseId) |
||
| 5744 | |||
| 5745 | /** |
||
| 5746 | * @param array $userSessionList format see self::importSessionDrhCSV() |
||
| 5747 | * @param bool $sendEmail |
||
| 5748 | * @param bool $removeOldRelationShips |
||
| 5749 | * @return string |
||
| 5750 | */ |
||
| 5751 | public static function subscribeDrhToSessionList($userSessionList, $sendEmail, $removeOldRelationShips) |
||
| 5769 | |||
| 5770 | /** |
||
| 5771 | * @param array $userSessionList format see self::importSessionDrhCSV() |
||
| 5772 | * |
||
| 5773 | * @return string |
||
| 5774 | */ |
||
| 5775 | public static function checkSubscribeDrhToSessionList($userSessionList) |
||
| 5816 | |||
| 5817 | /** |
||
| 5818 | * @param string $file |
||
| 5819 | * @param bool $sendEmail |
||
| 5820 | * @param bool $removeOldRelationShips |
||
| 5821 | * |
||
| 5822 | * @return string |
||
| 5823 | */ |
||
| 5824 | public static function importSessionDrhCSV($file, $sendEmail, $removeOldRelationShips) |
||
| 5847 | |||
| 5848 | /** |
||
| 5849 | * Courses re-ordering in resume_session.php flag see BT#8316 |
||
| 5850 | */ |
||
| 5851 | public static function orderCourseIsEnabled() |
||
| 5860 | |||
| 5861 | /** |
||
| 5862 | * @param string $direction (up/down) |
||
| 5863 | * @param int $sessionId |
||
| 5864 | * @param int $courseId |
||
| 5865 | * @return bool |
||
| 5866 | */ |
||
| 5867 | public static function move($direction, $sessionId, $courseId) |
||
| 5930 | |||
| 5931 | /** |
||
| 5932 | * @param int $sessionId |
||
| 5933 | * @param int $courseId |
||
| 5934 | * @return bool |
||
| 5935 | */ |
||
| 5936 | public static function moveUp($sessionId, $courseId) |
||
| 5940 | |||
| 5941 | /** |
||
| 5942 | * @param int $sessionId |
||
| 5943 | * @param string $courseCode |
||
| 5944 | * @return bool |
||
| 5945 | */ |
||
| 5946 | public static function moveDown($sessionId, $courseCode) |
||
| 5950 | |||
| 5951 | /** |
||
| 5952 | * Use the session duration to allow/block user access see BT#8317 |
||
| 5953 | * Needs these DB changes |
||
| 5954 | * ALTER TABLE session ADD COLUMN duration int; |
||
| 5955 | * ALTER TABLE session_rel_user ADD COLUMN duration int; |
||
| 5956 | */ |
||
| 5957 | public static function durationPerUserIsEnabled() |
||
| 5961 | |||
| 5962 | /** |
||
| 5963 | * Returns the number of days the student has left in a session when using |
||
| 5964 | * sessions durations |
||
| 5965 | * @param int $userId |
||
| 5966 | * @param int $sessionId |
||
| 5967 | * @param int $duration in days |
||
| 5968 | * @return int |
||
| 5969 | */ |
||
| 5970 | public static function getDayLeftInSession($sessionId, $userId, $duration) |
||
| 5993 | |||
| 5994 | /** |
||
| 5995 | * @param int $duration |
||
| 5996 | * @param int $userId |
||
| 5997 | * @param int $sessionId |
||
| 5998 | */ |
||
| 5999 | public static function editUserSessionDuration($duration, $userId, $sessionId) |
||
| 6014 | |||
| 6015 | /** |
||
| 6016 | * Gets one row from the session_rel_user table |
||
| 6017 | * @param int $userId |
||
| 6018 | * @param int $sessionId |
||
| 6019 | * |
||
| 6020 | * @return array |
||
| 6021 | */ |
||
| 6022 | View Code Duplication | public static function getUserSession($userId, $sessionId) |
|
| 6042 | |||
| 6043 | /** |
||
| 6044 | * Check if user is subscribed inside a session as student |
||
| 6045 | * @param int $sessionId The session id |
||
| 6046 | * @param int $userId The user id |
||
| 6047 | * @return boolean Whether is subscribed |
||
| 6048 | */ |
||
| 6049 | public static function isUserSubscribedAsStudent($sessionId, $userId) |
||
| 6072 | |||
| 6073 | /** |
||
| 6074 | * Get the session coached by a user (general coach and course-session coach) |
||
| 6075 | * @param int $coachId The coach id |
||
| 6076 | * @param boolean $checkSessionRelUserVisibility Check the session visibility |
||
| 6077 | * @param boolean $asPlatformAdmin The user is a platform admin and we want all sessions |
||
| 6078 | * @return array The session list |
||
| 6079 | */ |
||
| 6080 | public static function getSessionsCoachedByUser($coachId, $checkSessionRelUserVisibility = false, $asPlatformAdmin = false) |
||
| 6125 | |||
| 6126 | /** |
||
| 6127 | * Check if the course belongs to the session |
||
| 6128 | * @param int $sessionId The session id |
||
| 6129 | * @param string $courseCode The course code |
||
| 6130 | * |
||
| 6131 | * @return bool |
||
| 6132 | */ |
||
| 6133 | public static function sessionHasCourse($sessionId, $courseCode) |
||
| 6160 | |||
| 6161 | /** |
||
| 6162 | * Get the list of course coaches |
||
| 6163 | * @return array The list |
||
| 6164 | */ |
||
| 6165 | public static function getAllCourseCoaches() |
||
| 6203 | |||
| 6204 | /** |
||
| 6205 | * Calculate the total user time in the platform |
||
| 6206 | * @param int $userId The user id |
||
| 6207 | * @param string $from Optional. From date |
||
| 6208 | * @param string $until Optional. Until date |
||
| 6209 | * @return string The time (hh:mm:ss) |
||
| 6210 | */ |
||
| 6211 | public static function getTotalUserTimeInPlatform($userId, $from = '', $until = '') |
||
| 6240 | |||
| 6241 | /** |
||
| 6242 | * Get the courses list by a course coach |
||
| 6243 | * @param int $coachId The coach id |
||
| 6244 | * @return array (id, user_id, session_id, c_id, visibility, status, legal_agreement) |
||
| 6245 | */ |
||
| 6246 | public static function getCoursesListByCourseCoach($coachId) |
||
| 6258 | |||
| 6259 | /** |
||
| 6260 | * Get the count of user courses in session |
||
| 6261 | * @param int $sessionId The session id |
||
| 6262 | * @return array |
||
| 6263 | */ |
||
| 6264 | View Code Duplication | public static function getTotalUserCoursesInSession($sessionId) |
|
| 6265 | { |
||
| 6266 | $tableUser = Database::get_main_table(TABLE_MAIN_USER); |
||
| 6267 | $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 6268 | |||
| 6293 | |||
| 6294 | |||
| 6295 | /** |
||
| 6296 | * Returns list of a few data from session (name, short description, start |
||
| 6297 | * date, end date) and the given extra fields if defined based on a |
||
| 6298 | * session category Id. |
||
| 6299 | * @param int $categoryId The internal ID of the session category |
||
| 6300 | * @param string $target Value to search for in the session field values |
||
| 6301 | * @param array $extraFields A list of fields to be scanned and returned |
||
| 6302 | * @return mixed |
||
| 6303 | */ |
||
| 6304 | public static function getShortSessionListAndExtraByCategory( |
||
| 6401 | |||
| 6402 | /** |
||
| 6403 | * Return the Session Category id searched by name |
||
| 6404 | * @param string $categoryName Name attribute of session category used for search query |
||
| 6405 | * @param bool $force boolean used to get even if something is wrong (e.g not unique name) |
||
| 6406 | * @return int|array If success, return category id (int), else it will return an array |
||
| 6407 | * with the next structure: |
||
| 6408 | * array('error' => true, 'errorMessage' => ERROR_MESSAGE) |
||
| 6409 | */ |
||
| 6410 | public static function getSessionCategoryIdByName($categoryName, $force = false) |
||
| 6447 | |||
| 6448 | /** |
||
| 6449 | * Return all data from sessions (plus extra field, course and coach data) by category id |
||
| 6450 | * @param int $sessionCategoryId session category id used to search sessions |
||
| 6451 | * @return array If success, return session list and more session related data, else it will return an array |
||
| 6452 | * with the next structure: |
||
| 6453 | * array('error' => true, 'errorMessage' => ERROR_MESSAGE) |
||
| 6454 | */ |
||
| 6455 | public static function getSessionListAndExtraByCategoryId($sessionCategoryId) |
||
| 6583 | |||
| 6584 | /** |
||
| 6585 | * Return session description from session id |
||
| 6586 | * @param int $sessionId |
||
| 6587 | * @return string |
||
| 6588 | */ |
||
| 6589 | public static function getDescriptionFromSessionId($sessionId) |
||
| 6616 | |||
| 6617 | /** |
||
| 6618 | * Get a session list filtered by name, description or any of the given extra fields |
||
| 6619 | * @param string $term The term to search |
||
| 6620 | * @param array $extraFieldsToInclude Extra fields to include in the session data |
||
| 6621 | * @return array The list |
||
| 6622 | */ |
||
| 6623 | public static function searchSession($term, $extraFieldsToInclude = array()) |
||
| 6661 | |||
| 6662 | /** |
||
| 6663 | * @param $sessionId |
||
| 6664 | * @param array $extraFieldsToInclude |
||
| 6665 | * @return array |
||
| 6666 | */ |
||
| 6667 | public static function getFilteredExtraFields($sessionId, $extraFieldsToInclude = array()) |
||
| 6721 | |||
| 6722 | /** |
||
| 6723 | * @param int $sessionId |
||
| 6724 | * |
||
| 6725 | * @return bool |
||
| 6726 | */ |
||
| 6727 | public static function isValidId($sessionId) |
||
| 6744 | |||
| 6745 | /** |
||
| 6746 | * Get list of sessions based on users of a group for a group admin |
||
| 6747 | * @param int $userId The user id |
||
| 6748 | * @return array |
||
| 6749 | */ |
||
| 6750 | View Code Duplication | public static function getSessionsFollowedForGroupAdmin($userId) |
|
| 6797 | |||
| 6798 | /** |
||
| 6799 | * @param array $sessionInfo |
||
| 6800 | * @return string |
||
| 6801 | */ |
||
| 6802 | public static function getSessionVisibility($sessionInfo) |
||
| 6813 | |||
| 6814 | /** |
||
| 6815 | * Converts "start date" and "end date" to "From start date to end date" string |
||
| 6816 | * @param string $startDate |
||
| 6817 | * @param string $endDate |
||
| 6818 | * @param bool $showTime |
||
| 6819 | * @param bool $dateHuman |
||
| 6820 | * |
||
| 6821 | * @return string |
||
| 6822 | */ |
||
| 6823 | private static function convertSessionDateToString($startDate, $endDate, $showTime, $dateHuman) |
||
| 6850 | |||
| 6851 | /** |
||
| 6852 | * Returns a human readable string |
||
| 6853 | * @params array $sessionInfo An array with all the session dates |
||
| 6854 | * @return string |
||
| 6855 | */ |
||
| 6856 | public static function parseSessionDates($sessionInfo, $showTime = false) |
||
| 6886 | |||
| 6887 | /** |
||
| 6888 | * @param FormValidator $form |
||
| 6889 | * |
||
| 6890 | * @return array |
||
| 6891 | */ |
||
| 6892 | public static function setForm(FormValidator & $form, $sessionId = 0) |
||
| 6893 | { |
||
| 6894 | $categoriesList = SessionManager::get_all_session_category(); |
||
| 6895 | $userInfo = api_get_user_info(); |
||
| 6896 | |||
| 6897 | $categoriesOptions = array( |
||
| 6898 | '0' => get_lang('None') |
||
| 6899 | ); |
||
| 6900 | |||
| 6901 | if ($categoriesList != false) { |
||
| 6902 | foreach ($categoriesList as $categoryItem) { |
||
| 6903 | $categoriesOptions[$categoryItem['id']] = $categoryItem['name']; |
||
| 6904 | } |
||
| 6905 | } |
||
| 6906 | |||
| 6907 | // Database Table Definitions |
||
| 6908 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 6909 | |||
| 6910 | $form->addElement('text', 'name', get_lang('SessionName'), array( |
||
| 6911 | 'maxlength' => 50, |
||
| 6912 | )); |
||
| 6913 | $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 6914 | $form->addRule('name', get_lang('SessionNameAlreadyExists'), 'callback', 'check_session_name'); |
||
| 6915 | |||
| 6916 | if (!api_is_platform_admin() && api_is_teacher()) { |
||
| 6917 | $form->addElement( |
||
| 6918 | 'select', |
||
| 6919 | 'coach_username', |
||
| 6920 | get_lang('CoachName'), |
||
| 6921 | [api_get_user_id() => $userInfo['complete_name']], |
||
| 6922 | array( |
||
| 6923 | 'id' => 'coach_username', |
||
| 6924 | 'style' => 'width:370px;', |
||
| 6925 | ) |
||
| 6926 | ); |
||
| 6927 | } else { |
||
| 6928 | |||
| 6929 | $sql = "SELECT COUNT(1) FROM $tbl_user WHERE status = 1"; |
||
| 6930 | $rs = Database::query($sql); |
||
| 6931 | $countUsers = Database::result($rs, 0, 0); |
||
| 6932 | |||
| 6933 | if (intval($countUsers) < 50) { |
||
| 6934 | $orderClause = "ORDER BY "; |
||
| 6935 | $orderClause .= api_sort_by_first_name() ? "firstname, lastname, username" : "lastname, firstname, username"; |
||
| 6936 | |||
| 6937 | $sql = "SELECT user_id, lastname, firstname, username |
||
| 6938 | FROM $tbl_user |
||
| 6939 | WHERE status = '1' ". |
||
| 6940 | $orderClause; |
||
| 6941 | |||
| 6942 | View Code Duplication | if (api_is_multiple_url_enabled()) { |
|
| 6943 | $userRelAccessUrlTable = Database::get_main_table( |
||
| 6944 | TABLE_MAIN_ACCESS_URL_REL_USER |
||
| 6945 | ); |
||
| 6946 | $accessUrlId = api_get_current_access_url_id(); |
||
| 6947 | |||
| 6948 | if ($accessUrlId != -1) { |
||
| 6949 | $sql = "SELECT user.user_id, username, lastname, firstname |
||
| 6950 | FROM $tbl_user user |
||
| 6951 | INNER JOIN $userRelAccessUrlTable url_user |
||
| 6952 | ON (url_user.user_id = user.user_id) |
||
| 6953 | WHERE |
||
| 6954 | access_url_id = $accessUrlId AND |
||
| 6955 | status = 1 " |
||
| 6956 | .$orderClause; |
||
| 6957 | } |
||
| 6958 | } |
||
| 6959 | |||
| 6960 | $result = Database::query($sql); |
||
| 6961 | $coachesList = Database::store_result($result); |
||
| 6962 | |||
| 6963 | $coachesOptions = array(); |
||
| 6964 | View Code Duplication | foreach ($coachesList as $coachItem) { |
|
| 6965 | $coachesOptions[$coachItem['user_id']] = |
||
| 6966 | api_get_person_name($coachItem['firstname'], $coachItem['lastname']).' ('.$coachItem['username'].')'; |
||
| 6967 | } |
||
| 6968 | |||
| 6969 | $form->addElement( |
||
| 6970 | 'select', |
||
| 6971 | 'coach_username', |
||
| 6972 | get_lang('CoachName'), |
||
| 6973 | $coachesOptions |
||
| 6974 | ); |
||
| 6975 | } else { |
||
| 6976 | $form->addElement( |
||
| 6977 | 'select_ajax', |
||
| 6978 | 'coach_username', |
||
| 6979 | get_lang('CoachName'), |
||
| 6980 | null, |
||
| 6981 | [ |
||
| 6982 | 'url' => api_get_path(WEB_AJAX_PATH) . 'session.ajax.php?a=search_general_coach', |
||
| 6983 | 'width' => '100%', |
||
| 6984 | ] |
||
| 6985 | ); |
||
| 6986 | } |
||
| 6987 | } |
||
| 6988 | |||
| 6989 | $form->addRule('coach_username', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 6990 | $form->addHtml('<div id="ajax_list_coachs"></div>'); |
||
| 6991 | |||
| 6992 | $form->addButtonAdvancedSettings('advanced_params'); |
||
| 6993 | $form->addElement('html','<div id="advanced_params_options" style="display:none">'); |
||
| 6994 | |||
| 6995 | $form->addSelect('session_category', get_lang('SessionCategory'), $categoriesOptions, array( |
||
| 6996 | 'id' => 'session_category' |
||
| 6997 | )); |
||
| 6998 | |||
| 6999 | $form->addHtmlEditor( |
||
| 7000 | 'description', |
||
| 7001 | get_lang('Description'), |
||
| 7002 | false, |
||
| 7003 | false, |
||
| 7004 | array( |
||
| 7005 | 'ToolbarSet' => 'Minimal', |
||
| 7006 | ) |
||
| 7007 | ); |
||
| 7008 | |||
| 7009 | $form->addElement('checkbox', 'show_description', null, get_lang('ShowDescription')); |
||
| 7010 | |||
| 7011 | $visibilityGroup = array(); |
||
| 7012 | $visibilityGroup[] = $form->createElement('select', 'session_visibility', null, array( |
||
| 7013 | SESSION_VISIBLE_READ_ONLY => get_lang('SessionReadOnly'), |
||
| 7014 | SESSION_VISIBLE => get_lang('SessionAccessible'), |
||
| 7015 | SESSION_INVISIBLE => api_ucfirst(get_lang('SessionNotAccessible')), |
||
| 7016 | )); |
||
| 7017 | $form->addGroup($visibilityGroup, 'visibility_group', get_lang('SessionVisibility'), null, false); |
||
| 7018 | |||
| 7019 | $options = [ |
||
| 7020 | 0 => get_lang('ByDuration'), |
||
| 7021 | 1 => get_lang('ByDates') |
||
| 7022 | ]; |
||
| 7023 | |||
| 7024 | $form->addSelect('access', get_lang('Access'), $options, array( |
||
| 7025 | 'onchange' => 'accessSwitcher()', |
||
| 7026 | 'id' => 'access' |
||
| 7027 | )); |
||
| 7028 | |||
| 7029 | $form->addElement('html', '<div id="duration" style="display:none">'); |
||
| 7030 | |||
| 7031 | $form->addElement( |
||
| 7032 | 'number', |
||
| 7033 | 'duration', |
||
| 7034 | array( |
||
| 7035 | get_lang('SessionDurationTitle'), |
||
| 7036 | get_lang('SessionDurationDescription'), |
||
| 7037 | ), |
||
| 7038 | array( |
||
| 7039 | 'maxlength' => 50, |
||
| 7040 | ) |
||
| 7041 | ); |
||
| 7042 | |||
| 7043 | $form->addElement('html', '</div>'); |
||
| 7044 | $form->addElement('html', '<div id="date_fields" style="display:none">'); |
||
| 7045 | |||
| 7046 | // Dates |
||
| 7047 | $form->addDateTimePicker( |
||
| 7048 | 'access_start_date', |
||
| 7049 | array(get_lang('SessionStartDate'), get_lang('SessionStartDateComment')), |
||
| 7050 | array('id' => 'access_start_date') |
||
| 7051 | ); |
||
| 7052 | |||
| 7053 | $form->addDateTimePicker( |
||
| 7054 | 'access_end_date', |
||
| 7055 | array(get_lang('SessionEndDate'), get_lang('SessionEndDateComment')), |
||
| 7056 | array('id' => 'access_end_date') |
||
| 7057 | ); |
||
| 7058 | |||
| 7059 | $form->addRule( |
||
| 7060 | array('access_start_date', 'access_end_date'), |
||
| 7061 | get_lang('StartDateMustBeBeforeTheEndDate'), |
||
| 7062 | 'compare_datetime_text', |
||
| 7063 | '< allow_empty' |
||
| 7064 | ); |
||
| 7065 | |||
| 7066 | $form->addDateTimePicker( |
||
| 7067 | 'display_start_date', |
||
| 7068 | array( |
||
| 7069 | get_lang('SessionDisplayStartDate'), |
||
| 7070 | get_lang('SessionDisplayStartDateComment'), |
||
| 7071 | ), |
||
| 7072 | array('id' => 'display_start_date') |
||
| 7073 | ); |
||
| 7074 | $form->addDateTimePicker( |
||
| 7075 | 'display_end_date', |
||
| 7076 | array( |
||
| 7077 | get_lang('SessionDisplayEndDate'), |
||
| 7078 | get_lang('SessionDisplayEndDateComment'), |
||
| 7079 | ), |
||
| 7080 | array('id' => 'display_end_date') |
||
| 7081 | ); |
||
| 7082 | |||
| 7083 | $form->addRule( |
||
| 7084 | array('display_start_date', 'display_end_date'), |
||
| 7085 | get_lang('StartDateMustBeBeforeTheEndDate'), |
||
| 7086 | 'compare_datetime_text', |
||
| 7087 | '< allow_empty' |
||
| 7088 | ); |
||
| 7089 | |||
| 7090 | $form->addDateTimePicker( |
||
| 7091 | 'coach_access_start_date', |
||
| 7092 | array( |
||
| 7093 | get_lang('SessionCoachStartDate'), |
||
| 7094 | get_lang('SessionCoachStartDateComment'), |
||
| 7095 | ), |
||
| 7096 | array('id' => 'coach_access_start_date') |
||
| 7097 | ); |
||
| 7098 | |||
| 7099 | $form->addDateTimePicker( |
||
| 7100 | 'coach_access_end_date', |
||
| 7101 | array( |
||
| 7102 | get_lang('SessionCoachEndDate'), |
||
| 7103 | get_lang('SessionCoachEndDateComment'), |
||
| 7104 | ), |
||
| 7105 | array('id' => 'coach_access_end_date') |
||
| 7106 | ); |
||
| 7107 | |||
| 7108 | $form->addRule( |
||
| 7109 | array('coach_access_start_date', 'coach_access_end_date'), |
||
| 7110 | get_lang('StartDateMustBeBeforeTheEndDate'), |
||
| 7111 | 'compare_datetime_text', |
||
| 7112 | '< allow_empty' |
||
| 7113 | ); |
||
| 7114 | |||
| 7115 | $form->addElement('html', '</div>'); |
||
| 7116 | |||
| 7117 | $form->addCheckBox( |
||
| 7118 | 'send_subscription_notification', |
||
| 7119 | [ |
||
| 7120 | get_lang('SendSubscriptionNotification'), |
||
| 7121 | get_lang('SendAnEmailWhenAUserBeingSubscribed') |
||
| 7122 | ] |
||
| 7123 | ); |
||
| 7124 | |||
| 7125 | // Extra fields |
||
| 7126 | $extra_field = new ExtraFieldModel('session'); |
||
| 7127 | $extra = $extra_field->addElements($form, $sessionId); |
||
| 7128 | |||
| 7129 | $form->addElement('html', '</div>'); |
||
| 7130 | |||
| 7131 | $js = $extra['jquery_ready_content']; |
||
| 7132 | |||
| 7133 | return ['js' => $js]; |
||
| 7134 | } |
||
| 7135 | |||
| 7136 | /** |
||
| 7137 | * Gets the number of rows in the session table filtered through the given |
||
| 7138 | * array of parameters |
||
| 7139 | * @param array Array of options/filters/keys |
||
| 7140 | * @return integer The number of rows, or false on wrong param |
||
| 7141 | * @assert ('a') === false |
||
| 7142 | */ |
||
| 7143 | static function get_count_admin_complete($options = array()) |
||
| 7144 | { |
||
| 7145 | if (!is_array($options)) { |
||
| 7146 | return false; |
||
| 7147 | } |
||
| 7148 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 7149 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 7150 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 7151 | $sessionCourseUserTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 7152 | $courseTable = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 7153 | |||
| 7154 | $where = 'WHERE 1 = 1 '; |
||
| 7155 | $user_id = api_get_user_id(); |
||
| 7156 | |||
| 7157 | if (api_is_session_admin() && |
||
| 7158 | api_get_setting('allow_session_admins_to_see_all_sessions') == 'false' |
||
| 7159 | ) { |
||
| 7160 | $where.=" WHERE s.session_admin_id = $user_id "; |
||
| 7161 | } |
||
| 7162 | |||
| 7163 | View Code Duplication | if (!empty($options['where'])) { |
|
| 7164 | $options['where'] = str_replace('course_title', 'c.title', $options['where']); |
||
| 7165 | $options['where'] = str_replace("( session_active = '0' )", '1=1', $options['where']); |
||
| 7166 | |||
| 7167 | $options['where'] = str_replace( |
||
| 7168 | array("AND session_active = '1' )", " AND ( session_active = '1' )"), |
||
| 7169 | array(') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 " ) |
||
| 7170 | , $options['where'] |
||
| 7171 | ); |
||
| 7172 | |||
| 7173 | $options['where'] = str_replace( |
||
| 7174 | array("AND session_active = '0' )", " AND ( session_active = '0' )"), |
||
| 7175 | array(') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "), |
||
| 7176 | $options['where'] |
||
| 7177 | ); |
||
| 7178 | |||
| 7179 | if (!empty($options['extra'])) { |
||
| 7180 | $options['where'] = str_replace(' 1 = 1 AND', '', $options['where']); |
||
| 7181 | $options['where'] = str_replace('AND', 'OR', $options['where']); |
||
| 7182 | |||
| 7183 | foreach ($options['extra'] as $extra) { |
||
| 7184 | $options['where'] = str_replace($extra['field'], 'fv.field_id = '.$extra['id'].' AND fvo.option_value', $options['where']); |
||
| 7185 | } |
||
| 7186 | } |
||
| 7187 | $where .= ' AND '.$options['where']; |
||
| 7188 | } |
||
| 7189 | |||
| 7190 | $today = api_get_utc_datetime(); |
||
| 7191 | $query_rows = "SELECT count(*) as total_rows, c.title as course_title, s.name, |
||
| 7192 | IF ( |
||
| 7193 | (s.access_start_date <= '$today' AND '$today' < s.access_end_date) OR |
||
| 7194 | (s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR |
||
| 7195 | (s.access_start_date IS NULL AND s.access_end_date IS NULL) OR |
||
| 7196 | (s.access_start_date <= '$today' AND ('0000-00-00 00:00:00' = s.access_end_date OR s.access_end_date IS NULL )) OR |
||
| 7197 | ('$today' < s.access_end_date AND ('0000-00-00 00:00:00' = s.access_start_date OR s.access_start_date IS NULL) ) |
||
| 7198 | , 1, 0) as session_active |
||
| 7199 | FROM $tbl_session s |
||
| 7200 | LEFT JOIN $tbl_session_category sc |
||
| 7201 | ON s.session_category_id = sc.id |
||
| 7202 | INNER JOIN $tbl_user u |
||
| 7203 | ON s.id_coach = u.user_id |
||
| 7204 | INNER JOIN $sessionCourseUserTable scu |
||
| 7205 | ON s.id = scu.session_id |
||
| 7206 | INNER JOIN $courseTable c |
||
| 7207 | ON c.id = scu.c_id |
||
| 7208 | $where "; |
||
| 7209 | |||
| 7210 | if (api_is_multiple_url_enabled()) { |
||
| 7211 | $table_access_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 7212 | $access_url_id = api_get_current_access_url_id(); |
||
| 7213 | if ($access_url_id != -1) { |
||
| 7214 | $where.= " AND ar.access_url_id = $access_url_id "; |
||
| 7215 | |||
| 7216 | $query_rows = "SELECT count(*) as total_rows |
||
| 7217 | FROM $tbl_session s |
||
| 7218 | LEFT JOIN $tbl_session_category sc |
||
| 7219 | ON s.session_category_id = sc.id |
||
| 7220 | INNER JOIN $tbl_user u |
||
| 7221 | ON s.id_coach = u.user_id |
||
| 7222 | INNER JOIN $table_access_url_rel_session ar |
||
| 7223 | ON ar.session_id = s.id $where "; |
||
| 7224 | } |
||
| 7225 | } |
||
| 7226 | |||
| 7227 | $result = Database::query($query_rows); |
||
| 7228 | $num = 0; |
||
| 7229 | if (Database::num_rows($result)) { |
||
| 7230 | $rows = Database::fetch_array($result); |
||
| 7231 | $num = $rows['total_rows']; |
||
| 7232 | } |
||
| 7233 | |||
| 7234 | return $num; |
||
| 7235 | } |
||
| 7236 | |||
| 7237 | /** |
||
| 7238 | * @param string $list_type |
||
| 7239 | * @return array |
||
| 7240 | */ |
||
| 7241 | public static function getGridColumns($list_type = 'simple') |
||
| 7316 | |||
| 7317 | /** |
||
| 7318 | * Converts all dates sent through the param array (given form) to correct dates with timezones |
||
| 7319 | * @param array The dates The same array, with times converted |
||
| 7320 | * @param boolean $applyFormat Whether apply the DATE_TIME_FORMAT_SHORT format for sessions |
||
| 7321 | * @return array The same array, with times converted |
||
| 7322 | */ |
||
| 7323 | static function convert_dates_to_local($params, $applyFormat = false) |
||
| 7365 | |||
| 7366 | /** |
||
| 7367 | * Gets the admin session list callback of the session/session_list.php |
||
| 7368 | * page with all user/details in the right fomat |
||
| 7369 | * @param array |
||
| 7370 | * @result array Array of rows results |
||
| 7371 | * @asset ('a') === false |
||
| 7372 | */ |
||
| 7373 | public static function get_sessions_admin_complete($options = array()) |
||
| 7606 | |||
| 7607 | /** |
||
| 7608 | * Compare two arrays |
||
| 7609 | * @param array $array1 |
||
| 7610 | * @param array $array2 |
||
| 7611 | * |
||
| 7612 | * @return array |
||
| 7613 | */ |
||
| 7614 | static function compareArraysToMerge($array1, $array2) |
||
| 7629 | |||
| 7630 | /** |
||
| 7631 | * Get link to the admin page for this session |
||
| 7632 | * @param int $id Session ID |
||
| 7633 | * @return mixed URL to the admin page to manage the session, or false on error |
||
| 7634 | */ |
||
| 7635 | public static function getAdminPath($id) |
||
| 7644 | |||
| 7645 | /** |
||
| 7646 | * Get link to the user page for this session. |
||
| 7647 | * If a course is provided, build the link to the course |
||
| 7648 | * @param int $id Session ID |
||
| 7649 | * @param int $courseId Course ID (optional) in case the link has to send straight to the course |
||
| 7650 | * @return mixed URL to the page to use the session, or false on error |
||
| 7651 | */ |
||
| 7652 | public static function getPath($id, $courseId = 0) |
||
| 7670 | |||
| 7671 | /** |
||
| 7672 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7673 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7674 | * for course where user is coach |
||
| 7675 | * i.e. coach for the course or |
||
| 7676 | * main coach for a session the course is in |
||
| 7677 | * for a session category (or woth no session category if empty) |
||
| 7678 | * |
||
| 7679 | * @param $userId |
||
| 7680 | * |
||
| 7681 | * @return array |
||
| 7682 | */ |
||
| 7683 | public static function getSessionCourseForUser($userId) |
||
| 7709 | |||
| 7710 | /** |
||
| 7711 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7712 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7713 | * @param $userId |
||
| 7714 | * |
||
| 7715 | * @return array |
||
| 7716 | */ |
||
| 7717 | public static function getCoursesForCourseSessionCoach($userId) |
||
| 7744 | |||
| 7745 | /** |
||
| 7746 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7747 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7748 | * @param $userId |
||
| 7749 | * |
||
| 7750 | * @return array |
||
| 7751 | */ |
||
| 7752 | public static function getCoursesForMainSessionCoach($userId) |
||
| 7777 | |||
| 7778 | /** |
||
| 7779 | * Return an array of course_id used in session $sessionId |
||
| 7780 | * @param $sessionId |
||
| 7781 | * |
||
| 7782 | * @return array |
||
| 7783 | */ |
||
| 7784 | View Code Duplication | public static function getCoursesInSession($sessionId) |
|
| 7808 | |||
| 7809 | /** |
||
| 7810 | * Return an array of courses in session for user |
||
| 7811 | * and for each courses the list of session that use this course for user |
||
| 7812 | * |
||
| 7813 | * [0] => array |
||
| 7814 | * userCatId |
||
| 7815 | * userCatTitle |
||
| 7816 | * courseInUserCatList |
||
| 7817 | * [0] => array |
||
| 7818 | * courseId |
||
| 7819 | * title |
||
| 7820 | * courseCode |
||
| 7821 | * sessionCatList |
||
| 7822 | * [0] => array |
||
| 7823 | * catSessionId |
||
| 7824 | * catSessionName |
||
| 7825 | * sessionList |
||
| 7826 | * [0] => array |
||
| 7827 | * sessionId |
||
| 7828 | * sessionName |
||
| 7829 | * |
||
| 7830 | * @param $userId |
||
| 7831 | * |
||
| 7832 | * @return array |
||
| 7833 | * |
||
| 7834 | */ |
||
| 7835 | public static function getNamedSessionCourseForCoach($userId) |
||
| 7903 | |||
| 7904 | /** |
||
| 7905 | * @param array $listA |
||
| 7906 | * @param array $listB |
||
| 7907 | * @return int |
||
| 7908 | */ |
||
| 7909 | View Code Duplication | private static function compareCatSessionInfo($listA, $listB) |
|
| 7919 | |||
| 7920 | /** |
||
| 7921 | * @param array $listA |
||
| 7922 | * @param array $listB |
||
| 7923 | * @return int |
||
| 7924 | */ |
||
| 7925 | private static function compareBySessionName($listA, $listB) |
||
| 7939 | |||
| 7940 | /** |
||
| 7941 | * @param array $listA |
||
| 7942 | * @param array $listB |
||
| 7943 | * @return int |
||
| 7944 | */ |
||
| 7945 | View Code Duplication | private static function compareByUserCourseCat($listA, $listB) |
|
| 7955 | |||
| 7956 | /** |
||
| 7957 | * @param array $listA |
||
| 7958 | * @param array $listB |
||
| 7959 | * @return int |
||
| 7960 | */ |
||
| 7961 | View Code Duplication | private static function compareByCourse($listA, $listB) |
|
| 7971 | |||
| 7972 | /** |
||
| 7973 | * Return HTML code for displaying session_course_for_coach |
||
| 7974 | * @param $userId |
||
| 7975 | * @return string |
||
| 7976 | */ |
||
| 7977 | public static function getHtmlNamedSessionCourseForCoach($userId) |
||
| 8041 | } |
||
| 8042 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: