| Total Complexity | 924 |
| Total Lines | 7930 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Tracking often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Tracking, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Tracking |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Get group reporting. |
||
| 33 | * |
||
| 34 | * @param int $course_id |
||
| 35 | * @param int $sessionId |
||
| 36 | * @param int $group_id |
||
| 37 | * @param string $type |
||
| 38 | * @param int $start |
||
| 39 | * @param int $limit |
||
| 40 | * @param int $sidx |
||
| 41 | * @param string $sord |
||
| 42 | * @param array $where_condition |
||
| 43 | * |
||
| 44 | * @return array|null |
||
| 45 | */ |
||
| 46 | public static function get_group_reporting( |
||
| 47 | $courseId, |
||
| 48 | $sessionId = 0, |
||
| 49 | $group_id = 0, |
||
| 50 | $type = 'all', |
||
| 51 | $start = 0, |
||
| 52 | $limit = 1000, |
||
| 53 | $sidx = 1, |
||
| 54 | $sord = 'desc', |
||
| 55 | $where_condition = [] |
||
| 56 | ) { |
||
| 57 | $courseId = (int) $courseId; |
||
| 58 | $sessionId = (int) $sessionId; |
||
| 59 | |||
| 60 | if (empty($courseId)) { |
||
| 61 | return null; |
||
| 62 | } |
||
| 63 | $course = api_get_course_entity($courseId); |
||
| 64 | |||
| 65 | $session = api_get_session_entity($sessionId); |
||
| 66 | if ('count' === $type) { |
||
| 67 | return GroupManager::get_group_list(null, $course, null, $sessionId, true); |
||
| 68 | } |
||
| 69 | |||
| 70 | $groupList = GroupManager::get_group_list(null, $course, null, $sessionId, false, null, true); |
||
| 71 | $parsedResult = []; |
||
| 72 | if (!empty($groupList)) { |
||
| 73 | foreach ($groupList as $group) { |
||
| 74 | $users = GroupManager::get_users($group->getIid(), true, null, null, false, $courseId); |
||
| 75 | $time = 0; |
||
| 76 | $avg_student_score = 0; |
||
| 77 | $avg_student_progress = 0; |
||
| 78 | $work = 0; |
||
| 79 | $messages = 0; |
||
| 80 | foreach ($users as $user_data) { |
||
| 81 | $user = api_get_user_entity($user_data['user_id']); |
||
| 82 | $time += self::get_time_spent_on_the_course( |
||
| 83 | $user_data['user_id'], |
||
| 84 | $courseId, |
||
| 85 | $sessionId |
||
| 86 | ); |
||
| 87 | $average = self::get_avg_student_score( |
||
| 88 | $user_data['user_id'], |
||
| 89 | $course, |
||
| 90 | [], |
||
| 91 | $session |
||
| 92 | ); |
||
| 93 | if (is_numeric($average)) { |
||
| 94 | $avg_student_score += $average; |
||
| 95 | } |
||
| 96 | $avg_student_progress += self::get_avg_student_progress( |
||
| 97 | $user_data['user_id'], |
||
| 98 | $course, |
||
| 99 | [], |
||
| 100 | $session |
||
| 101 | ); |
||
| 102 | $work += Container::getStudentPublicationRepository()->countUserPublications( |
||
| 103 | $user, |
||
| 104 | $course, |
||
| 105 | $session |
||
| 106 | ); |
||
| 107 | $messages += Container::getForumPostRepository()->countUserForumPosts($user, $course, $session); |
||
| 108 | } |
||
| 109 | |||
| 110 | $countUsers = count($users); |
||
| 111 | $averageProgress = empty($countUsers) ? 0 : round($avg_student_progress / $countUsers, 2); |
||
| 112 | $averageScore = empty($countUsers) ? 0 : round($avg_student_score / $countUsers, 2); |
||
| 113 | |||
| 114 | $groupItem = [ |
||
| 115 | 'id' => $group->getIid(), |
||
| 116 | 'title' => $group->getTitle(), |
||
| 117 | 'time' => api_time_to_hms($time), |
||
| 118 | 'progress' => $averageProgress, |
||
| 119 | 'score' => $averageScore, |
||
| 120 | 'works' => $work, |
||
| 121 | 'messages' => $messages, |
||
| 122 | ]; |
||
| 123 | $parsedResult[] = $groupItem; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return $parsedResult; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param int $session_id |
||
| 132 | * @param string $origin |
||
| 133 | * @param bool $export_csv |
||
| 134 | * @param int $lp_id |
||
| 135 | * @param int $lp_item_id |
||
| 136 | * @param int $extendId |
||
| 137 | * @param int $extendAttemptId |
||
| 138 | * @param string $extendedAttempt |
||
| 139 | * @param string $extendedAll |
||
| 140 | * @param string $type classic or simple |
||
| 141 | * @param bool $allowExtend Optional. Allow or not extend te results |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public static function getLpStats( |
||
| 1399 | } |
||
| 1400 | |||
| 1401 | /** |
||
| 1402 | * @param int $userId |
||
| 1403 | * @param bool $getCount |
||
| 1404 | * |
||
| 1405 | * @return array |
||
| 1406 | */ |
||
| 1407 | public static function getStats($userId, $getCount = false) |
||
| 1713 | ]; |
||
| 1714 | } |
||
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Calculates the time spent on the platform by a user. |
||
| 1718 | * |
||
| 1719 | * @param int|array $userId |
||
| 1720 | * @param string $timeFilter type of time filter: 'last_week' or 'custom' |
||
| 1721 | * @param string $start_date start date date('Y-m-d H:i:s') |
||
| 1722 | * @param string $end_date end date date('Y-m-d H:i:s') |
||
| 1723 | * @param bool $returnAllRecords |
||
| 1724 | * |
||
| 1725 | * @return int|array |
||
| 1726 | */ |
||
| 1727 | public static function get_time_spent_on_the_platform( |
||
| 1728 | $userId, |
||
| 1729 | $timeFilter = 'last_7_days', |
||
| 1730 | $start_date = null, |
||
| 1731 | $end_date = null, |
||
| 1732 | $returnAllRecords = false |
||
| 1733 | ) { |
||
| 1734 | $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 1735 | $condition_time = ''; |
||
| 1736 | |||
| 1737 | if (is_array($userId)) { |
||
| 1738 | $userList = array_map('intval', $userId); |
||
| 1739 | $userCondition = " login_user_id IN ('".implode("','", $userList)."')"; |
||
| 1740 | } else { |
||
| 1741 | $userId = (int) $userId; |
||
| 1742 | $userCondition = " login_user_id = $userId "; |
||
| 1743 | } |
||
| 1744 | |||
| 1745 | $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 1746 | $access_url_id = api_get_current_access_url_id(); |
||
| 1747 | $url_table = ", $tbl_url_rel_user as url_users"; |
||
| 1748 | $url_condition = " AND u.login_user_id = url_users.user_id AND access_url_id='$access_url_id'"; |
||
| 1749 | |||
| 1750 | if (empty($timeFilter)) { |
||
| 1751 | $timeFilter = 'last_week'; |
||
| 1752 | } |
||
| 1753 | |||
| 1754 | $today = new DateTime('now', new DateTimeZone('UTC')); |
||
| 1755 | |||
| 1756 | switch ($timeFilter) { |
||
| 1757 | case 'last_7_days': |
||
| 1758 | $newDate = new DateTime('-7 day', new DateTimeZone('UTC')); |
||
| 1759 | $condition_time = " AND (login_date >= '{$newDate->format('Y-m-d H:i:s')}'"; |
||
| 1760 | $condition_time .= " AND logout_date <= '{$today->format('Y-m-d H:i:s')}') "; |
||
| 1761 | break; |
||
| 1762 | case 'last_30_days': |
||
| 1763 | $newDate = new DateTime('-30 days', new DateTimeZone('UTC')); |
||
| 1764 | $condition_time = " AND (login_date >= '{$newDate->format('Y-m-d H:i:s')}'"; |
||
| 1765 | $condition_time .= "AND logout_date <= '{$today->format('Y-m-d H:i:s')}') "; |
||
| 1766 | break; |
||
| 1767 | case 'wide': |
||
| 1768 | if (!empty($start_date) && !empty($end_date)) { |
||
| 1769 | $start_date = Database::escape_string($start_date); |
||
| 1770 | $end_date = Database::escape_string($end_date); |
||
| 1771 | $condition_time = ' AND ( |
||
| 1772 | (login_date >= "'.$start_date.'" AND login_date <= "'.$end_date.'") OR |
||
| 1773 | (logout_date >= "'.$start_date.'" AND logout_date <= "'.$end_date.'") OR |
||
| 1774 | (login_date <= "'.$start_date.'" AND logout_date >= "'.$end_date.'") |
||
| 1775 | ) '; |
||
| 1776 | } |
||
| 1777 | break; |
||
| 1778 | case 'custom': |
||
| 1779 | if (!empty($start_date) && !empty($end_date)) { |
||
| 1780 | $start_date = Database::escape_string($start_date); |
||
| 1781 | $end_date = Database::escape_string($end_date); |
||
| 1782 | $condition_time = ' AND (login_date >= "'.$start_date.'" AND logout_date <= "'.$end_date.'" ) '; |
||
| 1783 | } |
||
| 1784 | break; |
||
| 1785 | } |
||
| 1786 | |||
| 1787 | if ($returnAllRecords) { |
||
| 1788 | $sql = "SELECT login_date, logout_date, TIMESTAMPDIFF(SECOND, login_date, logout_date) diff |
||
| 1789 | FROM $tbl_track_login u $url_table |
||
| 1790 | WHERE $userCondition $condition_time $url_condition |
||
| 1791 | ORDER BY login_date"; |
||
| 1792 | $rs = Database::query($sql); |
||
| 1793 | |||
| 1794 | return Database::store_result($rs, 'ASSOC'); |
||
| 1795 | } |
||
| 1796 | |||
| 1797 | $sql = "SELECT SUM(TIMESTAMPDIFF(SECOND, login_date, logout_date)) diff |
||
| 1798 | FROM $tbl_track_login u $url_table |
||
| 1799 | WHERE $userCondition $condition_time $url_condition"; |
||
| 1800 | $rs = Database::query($sql); |
||
| 1801 | $row = Database::fetch_assoc($rs); |
||
| 1802 | $diff = $row['diff']; |
||
| 1803 | |||
| 1804 | if ($diff >= 0) { |
||
| 1805 | return $diff; |
||
| 1806 | } |
||
| 1807 | |||
| 1808 | return -1; |
||
| 1809 | } |
||
| 1810 | |||
| 1811 | /** |
||
| 1812 | * @param string $startDate |
||
| 1813 | * @param string $endDate |
||
| 1814 | * |
||
| 1815 | * @return int |
||
| 1816 | */ |
||
| 1817 | public static function getTotalTimeSpentOnThePlatform( |
||
| 1818 | $startDate = '', |
||
| 1819 | $endDate = '' |
||
| 1820 | ) { |
||
| 1821 | $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 1822 | $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 1823 | |||
| 1824 | $access_url_id = api_get_current_access_url_id(); |
||
| 1825 | $url_table = ", ".$tbl_url_rel_user." as url_users"; |
||
| 1826 | $url_condition = " AND u.login_user_id = url_users.user_id AND access_url_id='$access_url_id'"; |
||
| 1827 | |||
| 1828 | if (!empty($startDate) && !empty($endDate)) { |
||
| 1829 | $startDate = Database::escape_string($startDate); |
||
| 1830 | $endDate = Database::escape_string($endDate); |
||
| 1831 | $condition_time = ' (login_date >= "'.$startDate.'" AND logout_date <= "'.$endDate.'" ) '; |
||
| 1832 | } |
||
| 1833 | $sql = "SELECT SUM(TIMESTAMPDIFF(SECOND, login_date, logout_date)) diff |
||
| 1834 | FROM $tbl_track_login u $url_table |
||
| 1835 | WHERE $condition_time $url_condition"; |
||
| 1836 | $rs = Database::query($sql); |
||
| 1837 | $row = Database::fetch_assoc($rs); |
||
| 1838 | $diff = $row['diff']; |
||
| 1839 | |||
| 1840 | if ($diff >= 0) { |
||
| 1841 | return $diff; |
||
| 1842 | } |
||
| 1843 | |||
| 1844 | return -1; |
||
| 1845 | } |
||
| 1846 | |||
| 1847 | /** |
||
| 1848 | * Checks if the "lp_minimum_time" feature is available for the course. |
||
| 1849 | * |
||
| 1850 | * @param int $sessionId |
||
| 1851 | * @param int $courseId |
||
| 1852 | * |
||
| 1853 | * @return bool |
||
| 1854 | */ |
||
| 1855 | public static function minimumTimeAvailable($sessionId, $courseId) |
||
| 1856 | { |
||
| 1857 | if ('true' !== api_get_setting('lp.lp_minimum_time')) { |
||
| 1858 | return false; |
||
| 1859 | } |
||
| 1860 | |||
| 1861 | if (!empty($sessionId)) { |
||
| 1862 | $extraFieldValue = new ExtraFieldValue('session'); |
||
| 1863 | $value = $extraFieldValue->get_values_by_handler_and_field_variable($sessionId, 'new_tracking_system'); |
||
| 1864 | |||
| 1865 | if ($value && isset($value['value']) && 1 == $value['value']) { |
||
| 1866 | return true; |
||
| 1867 | } |
||
| 1868 | } else { |
||
| 1869 | if ($courseId) { |
||
| 1870 | $extraFieldValue = new ExtraFieldValue('course'); |
||
| 1871 | $value = $extraFieldValue->get_values_by_handler_and_field_variable($courseId, 'new_tracking_system'); |
||
| 1872 | if ($value && isset($value['value']) && 1 == $value['value']) { |
||
| 1873 | return true; |
||
| 1874 | } |
||
| 1875 | } |
||
| 1876 | } |
||
| 1877 | |||
| 1878 | return false; |
||
| 1879 | } |
||
| 1880 | |||
| 1881 | /** |
||
| 1882 | * Calculates the time spent on the course. |
||
| 1883 | * |
||
| 1884 | * @param array|int $userId |
||
| 1885 | * @param int $courseId |
||
| 1886 | * @param int $sessionId |
||
| 1887 | * |
||
| 1888 | * @return int Time in seconds |
||
| 1889 | */ |
||
| 1890 | public static function get_time_spent_on_the_course( |
||
| 1891 | $userId, |
||
| 1892 | $courseId, |
||
| 1893 | $sessionId = 0 |
||
| 1894 | ) { |
||
| 1895 | $courseId = (int) $courseId; |
||
| 1896 | |||
| 1897 | if (empty($courseId) || empty($userId)) { |
||
| 1898 | return 0; |
||
| 1899 | } |
||
| 1900 | |||
| 1901 | if (self::minimumTimeAvailable($sessionId, $courseId)) { |
||
| 1902 | $courseTime = self::getCalculateTime($userId, $courseId, $sessionId); |
||
| 1903 | |||
| 1904 | return isset($courseTime['total_time']) ? $courseTime['total_time'] : 0; |
||
| 1905 | } |
||
| 1906 | |||
| 1907 | $conditionUser = ''; |
||
| 1908 | $sessionId = (int) $sessionId; |
||
| 1909 | if (is_array($userId)) { |
||
| 1910 | $userId = array_map('intval', $userId); |
||
| 1911 | $conditionUser = " AND user_id IN (".implode(',', $userId).") "; |
||
| 1912 | } else { |
||
| 1913 | if (!empty($userId)) { |
||
| 1914 | $userId = (int) $userId; |
||
| 1915 | $conditionUser = " AND user_id = $userId "; |
||
| 1916 | } |
||
| 1917 | } |
||
| 1918 | |||
| 1919 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 1920 | $sql = "SELECT |
||
| 1921 | SUM(UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) as nb_seconds |
||
| 1922 | FROM $table |
||
| 1923 | WHERE |
||
| 1924 | UNIX_TIMESTAMP(logout_course_date) > UNIX_TIMESTAMP(login_course_date) AND |
||
| 1925 | c_id = '$courseId' "; |
||
| 1926 | |||
| 1927 | if (-1 != $sessionId) { |
||
| 1928 | $sql .= "AND session_id = '$sessionId' "; |
||
| 1929 | } |
||
| 1930 | |||
| 1931 | $sql .= $conditionUser; |
||
| 1932 | |||
| 1933 | $rs = Database::query($sql); |
||
| 1934 | $row = Database::fetch_array($rs); |
||
| 1935 | |||
| 1936 | return $row['nb_seconds']; |
||
| 1937 | } |
||
| 1938 | |||
| 1939 | /** |
||
| 1940 | * Get first connection date for a student. |
||
| 1941 | * |
||
| 1942 | * @param int $student_id |
||
| 1943 | * |
||
| 1944 | * @return string|bool Date format long without day or false if there are no connections |
||
| 1945 | */ |
||
| 1946 | public static function get_first_connection_date($student_id) |
||
| 1947 | { |
||
| 1948 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 1949 | $sql = 'SELECT login_date |
||
| 1950 | FROM '.$table.' |
||
| 1951 | WHERE login_user_id = '.intval($student_id).' |
||
| 1952 | ORDER BY login_date ASC |
||
| 1953 | LIMIT 0,1'; |
||
| 1954 | |||
| 1955 | $rs = Database::query($sql); |
||
| 1956 | if (Database::num_rows($rs) > 0) { |
||
| 1957 | if ($first_login_date = Database::result($rs, 0, 0)) { |
||
| 1958 | return api_convert_and_format_date( |
||
| 1959 | $first_login_date, |
||
| 1960 | DATE_FORMAT_SHORT |
||
| 1961 | ); |
||
| 1962 | } |
||
| 1963 | } |
||
| 1964 | |||
| 1965 | return false; |
||
| 1966 | } |
||
| 1967 | |||
| 1968 | /** |
||
| 1969 | * Get las connection date for a student. |
||
| 1970 | * |
||
| 1971 | * @param int $student_id |
||
| 1972 | * @param bool $warning_message Show a warning message (optional) |
||
| 1973 | * @param bool $return_timestamp True for returning results in timestamp (optional) |
||
| 1974 | * |
||
| 1975 | * @return string|int|bool Date format long without day, false if there are no connections or |
||
| 1976 | * timestamp if parameter $return_timestamp is true |
||
| 1977 | */ |
||
| 1978 | public static function get_last_connection_date( |
||
| 1979 | $student_id, |
||
| 1980 | $warning_message = false, |
||
| 1981 | $return_timestamp = false |
||
| 1982 | ) { |
||
| 1983 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 1984 | $sql = 'SELECT login_date |
||
| 1985 | FROM '.$table.' |
||
| 1986 | WHERE login_user_id = '.intval($student_id).' |
||
| 1987 | ORDER BY login_date |
||
| 1988 | DESC LIMIT 0,1'; |
||
| 1989 | |||
| 1990 | $rs = Database::query($sql); |
||
| 1991 | if (Database::num_rows($rs) > 0) { |
||
| 1992 | if ($last_login_date = Database::result($rs, 0, 0)) { |
||
| 1993 | $last_login_date = api_get_local_time($last_login_date); |
||
| 1994 | if ($return_timestamp) { |
||
| 1995 | return api_strtotime($last_login_date, 'UTC'); |
||
| 1996 | } else { |
||
| 1997 | if (!$warning_message) { |
||
| 1998 | return api_format_date($last_login_date, DATE_FORMAT_SHORT); |
||
| 1999 | } else { |
||
| 2000 | $timestamp = api_strtotime($last_login_date, 'UTC'); |
||
| 2001 | $currentTimestamp = time(); |
||
| 2002 | |||
| 2003 | //If the last connection is > than 7 days, the text is red |
||
| 2004 | //345600 = 7 days in seconds |
||
| 2005 | if ($currentTimestamp - $timestamp > 604800) { |
||
| 2006 | return '<em style="color: #F00;">'.api_format_date($last_login_date, DATE_FORMAT_SHORT).'</em>'; |
||
| 2007 | } else { |
||
| 2008 | return api_format_date($last_login_date, DATE_FORMAT_SHORT); |
||
| 2009 | } |
||
| 2010 | } |
||
| 2011 | } |
||
| 2012 | } |
||
| 2013 | } |
||
| 2014 | |||
| 2015 | return false; |
||
| 2016 | } |
||
| 2017 | |||
| 2018 | /** |
||
| 2019 | * Get first user's connection date on the course. |
||
| 2020 | * |
||
| 2021 | * @param int User id |
||
| 2022 | * @param int $courseId |
||
| 2023 | * @param int Session id (optional, default=0) |
||
| 2024 | * @param bool $convert_date |
||
| 2025 | * |
||
| 2026 | * @return string|bool Date with format long without day or false if there is no date |
||
| 2027 | */ |
||
| 2028 | public static function get_first_connection_date_on_the_course( |
||
| 2029 | $student_id, |
||
| 2030 | $courseId, |
||
| 2031 | $sessionId = 0, |
||
| 2032 | $convert_date = true |
||
| 2033 | ) { |
||
| 2034 | $student_id = (int) $student_id; |
||
| 2035 | $courseId = (int) $courseId; |
||
| 2036 | $sessionId = (int) $sessionId; |
||
| 2037 | |||
| 2038 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 2039 | $sql = 'SELECT login_course_date |
||
| 2040 | FROM '.$table.' |
||
| 2041 | WHERE |
||
| 2042 | user_id = '.$student_id.' AND |
||
| 2043 | c_id = '.$courseId.' AND |
||
| 2044 | session_id = '.$sessionId.' |
||
| 2045 | ORDER BY login_course_date ASC |
||
| 2046 | LIMIT 0,1'; |
||
| 2047 | $rs = Database::query($sql); |
||
| 2048 | if (Database::num_rows($rs) > 0) { |
||
| 2049 | if ($first_login_date = Database::result($rs, 0, 0)) { |
||
| 2050 | if (empty($first_login_date)) { |
||
| 2051 | return false; |
||
| 2052 | } |
||
| 2053 | |||
| 2054 | if ($convert_date) { |
||
| 2055 | return api_convert_and_format_date( |
||
| 2056 | $first_login_date, |
||
| 2057 | DATE_FORMAT_SHORT |
||
| 2058 | ); |
||
| 2059 | } |
||
| 2060 | |||
| 2061 | return $first_login_date; |
||
| 2062 | } |
||
| 2063 | } |
||
| 2064 | |||
| 2065 | return false; |
||
| 2066 | } |
||
| 2067 | |||
| 2068 | /** |
||
| 2069 | * Get last user's connection date on the course. |
||
| 2070 | * |
||
| 2071 | * @param int User id |
||
| 2072 | * @param array $courseInfo real_id and code are used |
||
| 2073 | * @param int Session id (optional, default=0) |
||
| 2074 | * @param bool $convert_date |
||
| 2075 | * |
||
| 2076 | * @return string|bool Date with format long without day or false if there is no date |
||
| 2077 | */ |
||
| 2078 | public static function get_last_connection_date_on_the_course( |
||
| 2079 | $student_id, |
||
| 2080 | $courseInfo, |
||
| 2081 | $sessionId = 0, |
||
| 2082 | $convert_date = true |
||
| 2083 | ) { |
||
| 2084 | // protect data |
||
| 2085 | $student_id = (int) $student_id; |
||
| 2086 | $sessionId = (int) $sessionId; |
||
| 2087 | |||
| 2088 | if (empty($courseInfo) || empty($student_id)) { |
||
| 2089 | return false; |
||
| 2090 | } |
||
| 2091 | |||
| 2092 | $courseId = (int) $courseInfo['real_id']; |
||
| 2093 | |||
| 2094 | if (empty($courseId)) { |
||
| 2095 | return false; |
||
| 2096 | } |
||
| 2097 | |||
| 2098 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 2099 | |||
| 2100 | if (self::minimumTimeAvailable($sessionId, $courseId)) { |
||
| 2101 | // Show the last date on which the user acceed the session when it was active |
||
| 2102 | $where_condition = ''; |
||
| 2103 | $userInfo = api_get_user_info($student_id); |
||
| 2104 | if (STUDENT == $userInfo['status'] && !empty($sessionId)) { |
||
| 2105 | // fin de acceso a la sesión |
||
| 2106 | $sessionInfo = SessionManager::fetch($sessionId); |
||
| 2107 | $last_access = $sessionInfo['access_end_date']; |
||
| 2108 | if (!empty($last_access)) { |
||
| 2109 | $where_condition = ' AND logout_course_date < "'.$last_access.'" '; |
||
| 2110 | } |
||
| 2111 | } |
||
| 2112 | $sql = "SELECT logout_course_date |
||
| 2113 | FROM $table |
||
| 2114 | WHERE user_id = $student_id AND |
||
| 2115 | c_id = $courseId AND |
||
| 2116 | session_id = $sessionId $where_condition |
||
| 2117 | ORDER BY logout_course_date DESC |
||
| 2118 | LIMIT 0,1"; |
||
| 2119 | |||
| 2120 | $rs = Database::query($sql); |
||
| 2121 | if (Database::num_rows($rs) > 0) { |
||
| 2122 | if ($last_login_date = Database::result($rs, 0, 0)) { |
||
| 2123 | if (empty($last_login_date)) { |
||
| 2124 | return false; |
||
| 2125 | } |
||
| 2126 | if ($convert_date) { |
||
| 2127 | return api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT); |
||
| 2128 | } |
||
| 2129 | |||
| 2130 | return $last_login_date; |
||
| 2131 | } |
||
| 2132 | } |
||
| 2133 | } else { |
||
| 2134 | $sql = "SELECT logout_course_date |
||
| 2135 | FROM $table |
||
| 2136 | WHERE user_id = $student_id AND |
||
| 2137 | c_id = $courseId AND |
||
| 2138 | session_id = $sessionId |
||
| 2139 | ORDER BY logout_course_date DESC |
||
| 2140 | LIMIT 0,1"; |
||
| 2141 | |||
| 2142 | $rs = Database::query($sql); |
||
| 2143 | if (Database::num_rows($rs) > 0) { |
||
| 2144 | if ($last_login_date = Database::result($rs, 0, 0)) { |
||
| 2145 | if (empty($last_login_date)) { |
||
| 2146 | return false; |
||
| 2147 | } |
||
| 2148 | //see #5736 |
||
| 2149 | $last_login_date_timestamp = api_strtotime($last_login_date); |
||
| 2150 | $now = time(); |
||
| 2151 | //If the last connection is > than 7 days, the text is red |
||
| 2152 | //345600 = 7 days in seconds |
||
| 2153 | if ($now - $last_login_date_timestamp > 604800) { |
||
| 2154 | if ($convert_date) { |
||
| 2155 | $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT); |
||
| 2156 | $icon = null; |
||
| 2157 | if (api_is_allowed_to_edit()) { |
||
| 2158 | $url = api_get_path(WEB_CODE_PATH). |
||
| 2159 | 'announcements/announcements.php?action=add&remind_inactive='.$student_id.'&cid='.$courseInfo['real_id']; |
||
| 2160 | $icon = '<a href="'.$url.'" title="'.get_lang('Remind inactive user').'"> |
||
| 2161 | '.Display::getMdiIcon( |
||
| 2162 | StateIcon::WARNING, |
||
| 2163 | 'ch-tool-icon', |
||
| 2164 | null, |
||
| 2165 | ICON_SIZE_SMALL |
||
| 2166 | ).' |
||
| 2167 | </a>'; |
||
| 2168 | } |
||
| 2169 | |||
| 2170 | return $icon.Display::label($last_login_date, 'warning'); |
||
| 2171 | } |
||
| 2172 | |||
| 2173 | return $last_login_date; |
||
| 2174 | } else { |
||
| 2175 | if ($convert_date) { |
||
| 2176 | return api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT); |
||
| 2177 | } |
||
| 2178 | |||
| 2179 | return $last_login_date; |
||
| 2180 | } |
||
| 2181 | } |
||
| 2182 | } |
||
| 2183 | } |
||
| 2184 | |||
| 2185 | return false; |
||
| 2186 | } |
||
| 2187 | |||
| 2188 | public static function getLastConnectionInAnyCourse($studentId) |
||
| 2189 | { |
||
| 2190 | $studentId = (int) $studentId; |
||
| 2191 | |||
| 2192 | if (empty($studentId)) { |
||
| 2193 | return false; |
||
| 2194 | } |
||
| 2195 | |||
| 2196 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 2197 | $sql = "SELECT logout_course_date |
||
| 2198 | FROM $table |
||
| 2199 | WHERE user_id = $studentId |
||
| 2200 | ORDER BY logout_course_date DESC |
||
| 2201 | LIMIT 1"; |
||
| 2202 | $result = Database::query($sql); |
||
| 2203 | if (Database::num_rows($result)) { |
||
| 2204 | $row = Database::fetch_array($result); |
||
| 2205 | |||
| 2206 | return $row['logout_course_date']; |
||
| 2207 | } |
||
| 2208 | |||
| 2209 | return false; |
||
| 2210 | } |
||
| 2211 | |||
| 2212 | /** |
||
| 2213 | * Get last course access by course/session. |
||
| 2214 | */ |
||
| 2215 | public static function getLastConnectionDateByCourse($courseId, $sessionId = 0) |
||
| 2216 | { |
||
| 2217 | $courseId = (int) $courseId; |
||
| 2218 | $sessionId = (int) $sessionId; |
||
| 2219 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 2220 | |||
| 2221 | $sql = "SELECT logout_course_date |
||
| 2222 | FROM $table |
||
| 2223 | WHERE |
||
| 2224 | c_id = $courseId AND |
||
| 2225 | session_id = $sessionId |
||
| 2226 | ORDER BY logout_course_date DESC |
||
| 2227 | LIMIT 0,1"; |
||
| 2228 | |||
| 2229 | $result = Database::query($sql); |
||
| 2230 | if (Database::num_rows($result)) { |
||
| 2231 | $row = Database::fetch_array($result); |
||
| 2232 | if ($row) { |
||
| 2233 | return $row['logout_course_date']; |
||
| 2234 | } |
||
| 2235 | } |
||
| 2236 | |||
| 2237 | return ''; |
||
| 2238 | } |
||
| 2239 | |||
| 2240 | /** |
||
| 2241 | * Get count of the connections to the course during a specified period. |
||
| 2242 | * |
||
| 2243 | * @param int $courseId |
||
| 2244 | * @param int Session id (optional) |
||
| 2245 | * @param int Datetime from which to collect data (defaults to 0) |
||
| 2246 | * @param int Datetime to which to collect data (defaults to now) |
||
| 2247 | * |
||
| 2248 | * @return int count connections |
||
| 2249 | */ |
||
| 2250 | public static function get_course_connections_count( |
||
| 2251 | $courseId, |
||
| 2252 | $sessionId = 0, |
||
| 2253 | $start = 0, |
||
| 2254 | $stop = null |
||
| 2255 | ) { |
||
| 2256 | if ($start < 0) { |
||
| 2257 | $start = 0; |
||
| 2258 | } |
||
| 2259 | if (!isset($stop) || $stop < 0) { |
||
| 2260 | $stop = api_get_utc_datetime(); |
||
| 2261 | } |
||
| 2262 | |||
| 2263 | // Given we're storing in cache, round the start and end times |
||
| 2264 | // to the lower minute |
||
| 2265 | $roundedStart = substr($start, 0, -2).'00'; |
||
| 2266 | $roundedStop = substr($stop, 0, -2).'00'; |
||
| 2267 | $roundedStart = Database::escape_string($roundedStart); |
||
| 2268 | $roundedStop = Database::escape_string($roundedStop); |
||
| 2269 | $month_filter = " AND login_course_date > '$roundedStart' AND login_course_date < '$roundedStop' "; |
||
| 2270 | $courseId = (int) $courseId; |
||
| 2271 | $sessionId = (int) $sessionId; |
||
| 2272 | $count = 0; |
||
| 2273 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 2274 | $sql = "SELECT count(*) as count_connections |
||
| 2275 | FROM $table |
||
| 2276 | WHERE |
||
| 2277 | c_id = $courseId AND |
||
| 2278 | session_id = $sessionId |
||
| 2279 | $month_filter"; |
||
| 2280 | |||
| 2281 | //This query can be very slow (several seconds on an indexed table |
||
| 2282 | // with 14M rows). As such, we'll try to use APCu if it is |
||
| 2283 | // available to store the resulting value for a few seconds |
||
| 2284 | $cacheAvailable = api_get_configuration_value('apc'); |
||
| 2285 | if (true === $cacheAvailable) { |
||
| 2286 | $apc = apcu_cache_info(true); |
||
| 2287 | $apc_end = $apc['start_time'] + $apc['ttl']; |
||
| 2288 | $apc_var = api_get_configuration_value('apc_prefix').'course_access_'.$courseId.'_'.$sessionId.'_'.strtotime($roundedStart).'_'.strtotime($roundedStop); |
||
| 2289 | if (apcu_exists($apc_var) && (time() < $apc_end) && |
||
| 2290 | apcu_fetch($apc_var) > 0 |
||
| 2291 | ) { |
||
| 2292 | $count = apcu_fetch($apc_var); |
||
| 2293 | } else { |
||
| 2294 | $rs = Database::query($sql); |
||
| 2295 | if (Database::num_rows($rs) > 0) { |
||
| 2296 | $row = Database::fetch_object($rs); |
||
| 2297 | $count = $row->count_connections; |
||
| 2298 | } |
||
| 2299 | apcu_clear_cache(); |
||
| 2300 | apcu_store($apc_var, $count, 60); |
||
| 2301 | } |
||
| 2302 | } else { |
||
| 2303 | $rs = Database::query($sql); |
||
| 2304 | if (Database::num_rows($rs) > 0) { |
||
| 2305 | $row = Database::fetch_object($rs); |
||
| 2306 | $count = $row->count_connections; |
||
| 2307 | } |
||
| 2308 | } |
||
| 2309 | |||
| 2310 | return $count; |
||
| 2311 | } |
||
| 2312 | |||
| 2313 | /** |
||
| 2314 | * Get count courses per student. |
||
| 2315 | * |
||
| 2316 | * @param int $user_id Student id |
||
| 2317 | * @param bool $include_sessions Include sessions (optional) |
||
| 2318 | * |
||
| 2319 | * @return int count courses |
||
| 2320 | */ |
||
| 2321 | public static function count_course_per_student($user_id, $include_sessions = true) |
||
| 2342 | } |
||
| 2343 | |||
| 2344 | /** |
||
| 2345 | * Gets the score average from all tests in a course by student. |
||
| 2346 | * |
||
| 2347 | * @param $student_id |
||
| 2348 | * @param $course_code |
||
| 2349 | * @param int $exercise_id |
||
| 2350 | * @param null $sessionId |
||
| 2351 | * @param int $active_filter 2 for consider all tests |
||
| 2352 | * 1 for active <> -1 |
||
| 2353 | * 0 for active <> 0 |
||
| 2354 | * @param int $into_lp 1 for all exercises |
||
| 2355 | * 0 for without LP |
||
| 2356 | * @param mixed id |
||
| 2357 | * @param string code |
||
| 2358 | * @param int id (optional), filtered by exercise |
||
| 2359 | * @param int id (optional), if param $sessionId is null |
||
| 2360 | * it'll return results including sessions, 0 = session is not |
||
| 2361 | * filtered |
||
| 2362 | * |
||
| 2363 | * @return string value (number %) Which represents a round integer about the score average |
||
| 2364 | */ |
||
| 2365 | public static function get_avg_student_exercise_score( |
||
| 2366 | $student_id, |
||
| 2367 | $course_code, |
||
| 2368 | $exercise_id = 0, |
||
| 2369 | $sessionId = null, |
||
| 2370 | $active_filter = 1, |
||
| 2371 | $into_lp = 0 |
||
| 2372 | ) { |
||
| 2373 | $course_code = Database::escape_string($course_code); |
||
| 2374 | $course_info = api_get_course_info($course_code); |
||
| 2375 | if (!empty($course_info)) { |
||
| 2376 | // table definition |
||
| 2377 | $tbl_course_quiz = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 2378 | $tbl_stats_exercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 2379 | |||
| 2380 | // Compose a filter based on optional exercise given |
||
| 2381 | $condition_quiz = ''; |
||
| 2382 | if (!empty($exercise_id)) { |
||
| 2383 | $exercise_id = (int) $exercise_id; |
||
| 2384 | $condition_quiz = " AND iid = $exercise_id "; |
||
| 2385 | } |
||
| 2386 | |||
| 2387 | // Compose a filter based on optional session id given |
||
| 2388 | $condition_session = ''; |
||
| 2389 | $session = null; |
||
| 2390 | if (isset($sessionId)) { |
||
| 2391 | $session = api_get_session_entity($course_info['real_id']); |
||
| 2392 | } |
||
| 2393 | $sessionCondition = api_get_session_condition($sessionId); |
||
| 2394 | |||
| 2395 | $condition_active = ''; |
||
| 2396 | if (1 == $active_filter) { |
||
| 2397 | $condition_active = 'AND active <> -1'; |
||
| 2398 | } elseif (0 == $active_filter) { |
||
| 2399 | $condition_active = 'AND active <> 0'; |
||
| 2400 | } |
||
| 2401 | $condition_into_lp = ''; |
||
| 2402 | $select_lp_id = ''; |
||
| 2403 | if (0 == $into_lp) { |
||
| 2404 | $condition_into_lp = 'AND orig_lp_id = 0 AND orig_lp_item_id = 0'; |
||
| 2405 | } else { |
||
| 2406 | $select_lp_id = ', orig_lp_id as lp_id '; |
||
| 2407 | } |
||
| 2408 | |||
| 2409 | $quizRepo = Container::getQuizRepository(); |
||
| 2410 | $course = api_get_course_entity($course_info['real_id']); |
||
| 2411 | $qb = $quizRepo->getResourcesByCourse($course, $session); |
||
| 2412 | $qb |
||
| 2413 | ->select('count(resource)') |
||
| 2414 | ->setMaxResults(1); |
||
| 2415 | $count_quiz = $qb->getQuery()->getSingleScalarResult(); |
||
| 2416 | |||
| 2417 | /*$sql = "SELECT count(iid) |
||
| 2418 | FROM $tbl_course_quiz |
||
| 2419 | WHERE c_id = {$course_info['real_id']} $condition_active $condition_quiz "; |
||
| 2420 | $count_quiz = 0; |
||
| 2421 | $countQuizResult = Database::query($sql); |
||
| 2422 | if (!empty($countQuizResult)) { |
||
| 2423 | $count_quiz = Database::fetch_row($countQuizResult); |
||
| 2424 | }*/ |
||
| 2425 | if (!empty($count_quiz) && !empty($student_id)) { |
||
| 2426 | if (is_array($student_id)) { |
||
| 2427 | $student_id = array_map('intval', $student_id); |
||
| 2428 | $condition_user = " AND exe_user_id IN (".implode(',', $student_id).") "; |
||
| 2429 | } else { |
||
| 2430 | $student_id = (int) $student_id; |
||
| 2431 | $condition_user = " AND exe_user_id = '$student_id' "; |
||
| 2432 | } |
||
| 2433 | |||
| 2434 | if (empty($exercise_id)) { |
||
| 2435 | $sql = "SELECT iid FROM $tbl_course_quiz |
||
| 2436 | WHERE c_id = {$course_info['real_id']} $condition_active $condition_quiz"; |
||
| 2437 | $result = Database::query($sql); |
||
| 2438 | $exercise_list = []; |
||
| 2439 | $exercise_id = null; |
||
| 2440 | if (!empty($result) && Database::num_rows($result)) { |
||
| 2441 | while ($row = Database::fetch_array($result)) { |
||
| 2442 | $exercise_list[] = $row['iid']; |
||
| 2443 | } |
||
| 2444 | } |
||
| 2445 | if (!empty($exercise_list)) { |
||
| 2446 | $exercise_id = implode("','", $exercise_list); |
||
| 2447 | } |
||
| 2448 | } |
||
| 2449 | |||
| 2450 | $sql = "SELECT |
||
| 2451 | SUM(score/max_score*100) as avg_score, |
||
| 2452 | COUNT(*) as num_attempts |
||
| 2453 | $select_lp_id |
||
| 2454 | FROM $tbl_stats_exercise |
||
| 2455 | WHERE |
||
| 2456 | exe_exo_id IN ('".$exercise_id."') |
||
| 2457 | $condition_user AND |
||
| 2458 | status = '' AND |
||
| 2459 | c_id = {$course_info['real_id']} |
||
| 2460 | $sessionCondition |
||
| 2461 | $condition_into_lp |
||
| 2462 | ORDER BY exe_date DESC"; |
||
| 2463 | |||
| 2464 | $res = Database::query($sql); |
||
| 2465 | $row = Database::fetch_array($res); |
||
| 2466 | $quiz_avg_score = null; |
||
| 2467 | |||
| 2468 | if (!empty($row['avg_score'])) { |
||
| 2469 | $quiz_avg_score = round($row['avg_score'], 2); |
||
| 2470 | } |
||
| 2471 | |||
| 2472 | if (!empty($row['num_attempts'])) { |
||
| 2473 | $quiz_avg_score = round($quiz_avg_score / $row['num_attempts'], 2); |
||
| 2474 | } |
||
| 2475 | if (is_array($student_id)) { |
||
| 2476 | $quiz_avg_score = round($quiz_avg_score / count($student_id), 2); |
||
| 2477 | } |
||
| 2478 | if (0 == $into_lp) { |
||
| 2479 | return $quiz_avg_score; |
||
| 2480 | } else { |
||
| 2481 | if (!empty($row['lp_id'])) { |
||
| 2482 | $tbl_lp = Database::get_course_table(TABLE_LP_MAIN); |
||
| 2483 | $sql = "SELECT title FROM $tbl_lp WHERE iid = ".(int) $row['lp_id']; |
||
| 2484 | $result = Database::query($sql); |
||
| 2485 | $row_lp = Database::fetch_row($result); |
||
| 2486 | $lp_name = null; |
||
| 2487 | if ($row_lp && isset($row_lp[0])) { |
||
| 2488 | $lp_name = $row_lp[0]; |
||
| 2489 | } |
||
| 2490 | |||
| 2491 | return [$quiz_avg_score, $lp_name]; |
||
| 2492 | } |
||
| 2493 | |||
| 2494 | return [$quiz_avg_score, null]; |
||
| 2495 | } |
||
| 2496 | } |
||
| 2497 | } |
||
| 2498 | |||
| 2499 | return null; |
||
| 2500 | } |
||
| 2501 | |||
| 2502 | /** |
||
| 2503 | * Get count student's exercise COMPLETED attempts. |
||
| 2504 | * |
||
| 2505 | * @param int $student_id |
||
| 2506 | * @param int $courseId |
||
| 2507 | * @param int $exercise_id |
||
| 2508 | * @param int $lp_id |
||
| 2509 | * @param int $lp_item_id |
||
| 2510 | * @param int $sessionId |
||
| 2511 | * @param int $find_all_lp 0 = just LP specified |
||
| 2512 | * 1 = LP specified or whitout LP, |
||
| 2513 | * 2 = all rows |
||
| 2514 | * |
||
| 2515 | * @internal param \Student $int id |
||
| 2516 | * @internal param \Course $string code |
||
| 2517 | * @internal param \Exercise $int id |
||
| 2518 | * @internal param \Learning $int path id (optional), |
||
| 2519 | * for showing attempts inside a learning path $lp_id and $lp_item_id params are required |
||
| 2520 | * @internal param \Learning $int path item id (optional), |
||
| 2521 | * for showing attempts inside a learning path $lp_id and $lp_item_id params are required |
||
| 2522 | * |
||
| 2523 | * @return int count of attempts |
||
| 2524 | */ |
||
| 2525 | public static function count_student_exercise_attempts( |
||
| 2526 | $student_id, |
||
| 2527 | $courseId, |
||
| 2528 | $exercise_id, |
||
| 2529 | $lp_id = 0, |
||
| 2530 | $lp_item_id = 0, |
||
| 2531 | $sessionId = 0, |
||
| 2532 | $find_all_lp = 0 |
||
| 2533 | ) { |
||
| 2534 | $courseId = intval($courseId); |
||
| 2535 | $student_id = intval($student_id); |
||
| 2536 | $exercise_id = intval($exercise_id); |
||
| 2537 | $sessionId = intval($sessionId); |
||
| 2538 | |||
| 2539 | $lp_id = intval($lp_id); |
||
| 2540 | $lp_item_id = intval($lp_item_id); |
||
| 2541 | $tbl_stats_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 2542 | $sessionCondition = api_get_session_condition($sessionId); |
||
| 2543 | $sql = "SELECT COUNT(exe_id) as essais |
||
| 2544 | FROM $tbl_stats_exercises |
||
| 2545 | WHERE |
||
| 2546 | c_id = $courseId AND |
||
| 2547 | exe_exo_id = $exercise_id AND |
||
| 2548 | status = '' AND |
||
| 2549 | exe_user_id= $student_id |
||
| 2550 | $sessionCondition |
||
| 2551 | "; |
||
| 2552 | |||
| 2553 | if (1 == $find_all_lp) { |
||
| 2554 | $sql .= "AND (orig_lp_id = $lp_id OR orig_lp_id = 0) |
||
| 2555 | AND (orig_lp_item_id = $lp_item_id OR orig_lp_item_id = 0)"; |
||
| 2556 | } elseif (0 == $find_all_lp) { |
||
| 2557 | $sql .= "AND orig_lp_id = $lp_id |
||
| 2558 | AND orig_lp_item_id = $lp_item_id"; |
||
| 2559 | } |
||
| 2560 | |||
| 2561 | $rs = Database::query($sql); |
||
| 2562 | $row = Database::fetch_row($rs); |
||
| 2563 | $count_attempts = $row[0]; |
||
| 2564 | |||
| 2565 | return $count_attempts; |
||
| 2566 | } |
||
| 2567 | |||
| 2568 | /** |
||
| 2569 | * Get count student's exercise progress. |
||
| 2570 | * |
||
| 2571 | * @param CQuiz[] $exerciseList |
||
| 2572 | * @param int $user_id |
||
| 2573 | * @param int $courseId |
||
| 2574 | * @param int $sessionId |
||
| 2575 | * |
||
| 2576 | * @return string |
||
| 2577 | */ |
||
| 2578 | public static function get_exercise_student_progress( |
||
| 2579 | $exerciseList, |
||
| 2580 | $user_id, |
||
| 2581 | $courseId, |
||
| 2582 | $sessionId |
||
| 2583 | ) { |
||
| 2584 | $courseId = (int) $courseId; |
||
| 2585 | $user_id = (int) $user_id; |
||
| 2586 | $sessionId = (int) $sessionId; |
||
| 2587 | |||
| 2588 | if (empty($exerciseList)) { |
||
| 2589 | return '0%'; |
||
| 2590 | } |
||
| 2591 | $tbl_stats_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 2592 | $exerciseIdList = []; |
||
| 2593 | foreach ($exerciseList as $exercise) { |
||
| 2594 | $exerciseIdList[] = $exercise->getIid(); |
||
| 2595 | } |
||
| 2596 | $exercise_list_imploded = implode("' ,'", $exerciseIdList); |
||
| 2597 | $sessionCondition = api_get_session_condition($sessionId); |
||
| 2598 | $sql = "SELECT COUNT(DISTINCT exe_exo_id) |
||
| 2599 | FROM $tbl_stats_exercises |
||
| 2600 | WHERE |
||
| 2601 | c_id = $courseId AND |
||
| 2602 | session_id = $sessionId AND |
||
| 2603 | exe_user_id = $user_id AND |
||
| 2604 | status = '' AND |
||
| 2605 | exe_exo_id IN ('$exercise_list_imploded') |
||
| 2606 | $sessionCondition |
||
| 2607 | "; |
||
| 2608 | |||
| 2609 | $rs = Database::query($sql); |
||
| 2610 | $count = 0; |
||
| 2611 | if ($rs) { |
||
| 2612 | $row = Database::fetch_row($rs); |
||
| 2613 | $count = (int) $row[0]; |
||
| 2614 | } |
||
| 2615 | $count = (0 != $count) ? 100 * round($count / count($exerciseList), 2).'%' : '0%'; |
||
| 2616 | |||
| 2617 | return $count; |
||
| 2618 | } |
||
| 2619 | |||
| 2620 | /** |
||
| 2621 | * @param CQuiz $exercise_list |
||
| 2622 | * @param int $user_id |
||
| 2623 | * @param int $courseId |
||
| 2624 | * @param int $sessionId |
||
| 2625 | * |
||
| 2626 | * @return string |
||
| 2627 | */ |
||
| 2628 | public static function get_exercise_student_average_best_attempt( |
||
| 2629 | $exercise_list, |
||
| 2630 | $user_id, |
||
| 2631 | $courseId, |
||
| 2632 | $sessionId |
||
| 2633 | ) { |
||
| 2634 | $result = 0; |
||
| 2635 | if (!empty($exercise_list)) { |
||
| 2636 | foreach ($exercise_list as $exercise_data) { |
||
| 2637 | $exercise_id = $exercise_data->getIid(); |
||
| 2638 | $best_attempt = Event::get_best_attempt_exercise_results_per_user( |
||
| 2639 | $user_id, |
||
| 2640 | $exercise_id, |
||
| 2641 | $courseId, |
||
| 2642 | $sessionId |
||
| 2643 | ); |
||
| 2644 | |||
| 2645 | if (!empty($best_attempt) && !empty($best_attempt['max_score'])) { |
||
| 2646 | $result += $best_attempt['score'] / $best_attempt['max_score']; |
||
| 2647 | } |
||
| 2648 | } |
||
| 2649 | $result = $result / count($exercise_list); |
||
| 2650 | $result = round($result, 2) * 100; |
||
| 2651 | } |
||
| 2652 | |||
| 2653 | return $result.'%'; |
||
| 2654 | } |
||
| 2655 | |||
| 2656 | /** |
||
| 2657 | * Returns the average student progress in the learning paths of the given |
||
| 2658 | * course, it will take into account the progress that were not started. |
||
| 2659 | * |
||
| 2660 | * @param int|array $studentId |
||
| 2661 | * @param Course $course The course object |
||
| 2662 | * @param array $lpIdList Limit average to listed lp ids |
||
| 2663 | * @param SessionEntity $session Session id (optional), |
||
| 2664 | * if parameter $sessionId is null(default) it'll return results including |
||
| 2665 | * sessions, 0 = session is not filtered |
||
| 2666 | * @param bool $returnArray Will return an array of the type: |
||
| 2667 | * [sum_of_progresses, number] if it is set to true |
||
| 2668 | * @param bool $onlySeriousGame Optional. Limit average to lp on seriousgame mode |
||
| 2669 | * |
||
| 2670 | * @return float Average progress of the user in this course from 0 to 100 |
||
| 2671 | */ |
||
| 2672 | public static function get_avg_student_progress( |
||
| 2673 | $studentId, |
||
| 2674 | Course $course = null, |
||
| 2675 | $lpIdList = [], |
||
| 2676 | SessionEntity $session = null, |
||
| 2677 | $returnArray = false, |
||
| 2678 | $onlySeriousGame = false |
||
| 2679 | ) { |
||
| 2680 | // If there is at least one learning path and one student. |
||
| 2681 | if (empty($studentId)) { |
||
| 2682 | return false; |
||
| 2683 | } |
||
| 2684 | if (empty($course)) { |
||
| 2685 | return false; |
||
| 2686 | } |
||
| 2687 | |||
| 2688 | $repo = Container::getLpRepository(); |
||
| 2689 | $qb = $repo->findAllByCourse($course, $session); |
||
| 2690 | $lps = $qb->getQuery()->getResult(); |
||
| 2691 | $filteredLP = []; |
||
| 2692 | |||
| 2693 | $sessionId = null !== $session ? $session->getId() : 0; |
||
| 2694 | |||
| 2695 | /** @var CLp $lp */ |
||
| 2696 | foreach ($lps as $lp) { |
||
| 2697 | $filteredLP[] = $lp->getIid(); |
||
| 2698 | } |
||
| 2699 | |||
| 2700 | if (empty($filteredLP)) { |
||
| 2701 | return false; |
||
| 2702 | } |
||
| 2703 | |||
| 2704 | $lpViewTable = Database::get_course_table(TABLE_LP_VIEW); |
||
| 2705 | /*$lpConditions = []; |
||
| 2706 | $lpConditions['c_id = ? '] = $courseInfo['real_id']; |
||
| 2707 | |||
| 2708 | if ($sessionId > 0) { |
||
| 2709 | $lpConditions['AND (session_id = ? OR session_id = 0 OR session_id IS NULL)'] = $sessionId; |
||
| 2710 | } else { |
||
| 2711 | $lpConditions['AND session_id = ?'] = $sessionId; |
||
| 2712 | } |
||
| 2713 | |||
| 2714 | if (is_array($lpIdList) && count($lpIdList) > 0) { |
||
| 2715 | $placeHolders = []; |
||
| 2716 | for ($i = 0; $i < count($lpIdList); $i++) { |
||
| 2717 | $placeHolders[] = '?'; |
||
| 2718 | } |
||
| 2719 | $lpConditions['AND iid IN('.implode(', ', $placeHolders).') '] = $lpIdList; |
||
| 2720 | } |
||
| 2721 | |||
| 2722 | if ($onlySeriousGame) { |
||
| 2723 | $lpConditions['AND seriousgame_mode = ? '] = true; |
||
| 2724 | } |
||
| 2725 | |||
| 2726 | $resultLP = Database::select( |
||
| 2727 | 'iid', |
||
| 2728 | $lPTable, |
||
| 2729 | ['where' => $lpConditions] |
||
| 2730 | ); |
||
| 2731 | $filteredLP = array_keys($resultLP); |
||
| 2732 | |||
| 2733 | if (empty($filteredLP)) { |
||
| 2734 | return false; |
||
| 2735 | }*/ |
||
| 2736 | |||
| 2737 | $conditions = [ |
||
| 2738 | //" c_id = {$courseInfo['real_id']} ", |
||
| 2739 | " lp_view.lp_id IN (".implode(', ', $filteredLP).") ", |
||
| 2740 | ]; |
||
| 2741 | |||
| 2742 | $groupBy = 'GROUP BY lp_id'; |
||
| 2743 | |||
| 2744 | if (is_array($studentId)) { |
||
| 2745 | $studentId = array_map('intval', $studentId); |
||
| 2746 | $conditions[] = " lp_view.user_id IN (".implode(',', $studentId).") "; |
||
| 2747 | } else { |
||
| 2748 | $studentId = (int) $studentId; |
||
| 2749 | $conditions[] = " lp_view.user_id = '$studentId' "; |
||
| 2750 | |||
| 2751 | if (empty($lpIdList)) { |
||
| 2752 | $lpList = new LearnpathList( |
||
| 2753 | $studentId, |
||
| 2754 | ['real_id' => $course->getId()], |
||
| 2755 | $sessionId, |
||
| 2756 | null, |
||
| 2757 | false, |
||
| 2758 | null, |
||
| 2759 | true |
||
| 2760 | ); |
||
| 2761 | $lpList = $lpList->get_flat_list(); |
||
| 2762 | if (!empty($lpList)) { |
||
| 2763 | /** @var $lp */ |
||
| 2764 | foreach ($lpList as $lpId => $lp) { |
||
| 2765 | $lpIdList[] = $lp['lp_old_id']; |
||
| 2766 | } |
||
| 2767 | } |
||
| 2768 | } |
||
| 2769 | } |
||
| 2770 | |||
| 2771 | if (!empty($sessionId)) { |
||
| 2772 | $conditions[] = " session_id = $sessionId "; |
||
| 2773 | } else { |
||
| 2774 | $conditions[] = ' (session_id = 0 OR session_id IS NULL) '; |
||
| 2775 | } |
||
| 2776 | |||
| 2777 | $conditionToString = implode('AND', $conditions); |
||
| 2778 | $sql = "SELECT lp_id, view_count, progress |
||
| 2779 | FROM $lpViewTable lp_view |
||
| 2780 | WHERE |
||
| 2781 | $conditionToString |
||
| 2782 | $groupBy |
||
| 2783 | ORDER BY view_count DESC"; |
||
| 2784 | |||
| 2785 | $result = Database::query($sql); |
||
| 2786 | |||
| 2787 | $progress = []; |
||
| 2788 | $viewCount = []; |
||
| 2789 | while ($row = Database::fetch_assoc($result)) { |
||
| 2790 | if (!isset($viewCount[$row['lp_id']])) { |
||
| 2791 | $progress[$row['lp_id']] = $row['progress']; |
||
| 2792 | } |
||
| 2793 | $viewCount[$row['lp_id']] = $row['view_count']; |
||
| 2794 | } |
||
| 2795 | |||
| 2796 | // Fill with lp ids |
||
| 2797 | $newProgress = []; |
||
| 2798 | if (!empty($lpIdList)) { |
||
| 2799 | foreach ($lpIdList as $lpId) { |
||
| 2800 | if (isset($progress[$lpId])) { |
||
| 2801 | $newProgress[] = $progress[$lpId]; |
||
| 2802 | } |
||
| 2803 | } |
||
| 2804 | $total = count($lpIdList); |
||
| 2805 | } else { |
||
| 2806 | $newProgress = $progress; |
||
| 2807 | $total = count($newProgress); |
||
| 2808 | } |
||
| 2809 | |||
| 2810 | $average = 0; |
||
| 2811 | $sum = 0; |
||
| 2812 | if (!empty($newProgress)) { |
||
| 2813 | $sum = array_sum($newProgress); |
||
| 2814 | $average = $sum / $total; |
||
| 2815 | } |
||
| 2816 | |||
| 2817 | if ($returnArray) { |
||
| 2818 | return [ |
||
| 2819 | $sum, |
||
| 2820 | $total, |
||
| 2821 | ]; |
||
| 2822 | } |
||
| 2823 | |||
| 2824 | return round($average, 1); |
||
| 2825 | } |
||
| 2826 | |||
| 2827 | /** |
||
| 2828 | * This function gets: |
||
| 2829 | * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max scores. |
||
| 2830 | * 2. The score average from all Tests (quiz) in all LP in a course-> All the answers / All the max scores. |
||
| 2831 | * 3. And finally it will return the average between 1. and 2. |
||
| 2832 | * |
||
| 2833 | * @param mixed $student_id Array of user ids or an user id |
||
| 2834 | * @param array $lp_ids List of LP ids |
||
| 2835 | * @param SessionEntity $session |
||
| 2836 | * if param $sessionId is null(default) it'll return results |
||
| 2837 | * including sessions, 0 = session is not filtered |
||
| 2838 | * @param bool $return_array Returns an array of the |
||
| 2839 | * type [sum_score, num_score] if set to true |
||
| 2840 | * @param bool $get_only_latest_attempt_results get only the latest attempts or ALL attempts |
||
| 2841 | * @param bool $getOnlyBestAttempt |
||
| 2842 | * |
||
| 2843 | * @return string value (number %) Which represents a round integer explain in got in 3 |
||
| 2844 | * |
||
| 2845 | * @todo improve performance, when loading 1500 users with 20 lps the script dies |
||
| 2846 | * This function does not take the results of a Test out of a LP |
||
| 2847 | */ |
||
| 2848 | public static function get_avg_student_score( |
||
| 2849 | $student_id, |
||
| 2850 | Course $course, |
||
| 2851 | $lp_ids = [], |
||
| 2852 | SessionEntity $session = null, |
||
| 2853 | $return_array = false, |
||
| 2854 | $get_only_latest_attempt_results = false, |
||
| 2855 | $getOnlyBestAttempt = false |
||
| 2856 | ) { |
||
| 2857 | if (empty($student_id)) { |
||
| 2858 | return null; |
||
| 2859 | } |
||
| 2860 | |||
| 2861 | $debug = false; |
||
| 2862 | $tbl_stats_exercices = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 2863 | $tbl_stats_attempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 2864 | |||
| 2865 | // Get course tables names |
||
| 2866 | $tbl_quiz_questions = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 2867 | $lp_table = Database::get_course_table(TABLE_LP_MAIN); |
||
| 2868 | $lp_item_table = Database::get_course_table(TABLE_LP_ITEM); |
||
| 2869 | $lp_view_table = Database::get_course_table(TABLE_LP_VIEW); |
||
| 2870 | $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW); |
||
| 2871 | $courseId = $course->getId(); |
||
| 2872 | |||
| 2873 | // Compose a filter based on optional learning paths list given |
||
| 2874 | $condition_lp = ''; |
||
| 2875 | if (count($lp_ids) > 0) { |
||
| 2876 | $condition_lp = " iid IN(".implode(',', $lp_ids).") "; |
||
| 2877 | } |
||
| 2878 | |||
| 2879 | // Compose a filter based on optional session id |
||
| 2880 | $sessionId = null; |
||
| 2881 | if (null !== $session) { |
||
| 2882 | $sessionId = $session->getId(); |
||
| 2883 | } |
||
| 2884 | $sessionCondition = api_get_session_condition($sessionId); |
||
| 2885 | |||
| 2886 | //$sessionId = (int) $sessionId; |
||
| 2887 | /*if (count($lp_ids) > 0) { |
||
| 2888 | $condition_session = " AND session_id = $sessionId "; |
||
| 2889 | } else { |
||
| 2890 | $condition_session = " WHERE session_id = $sessionId "; |
||
| 2891 | } |
||
| 2892 | |||
| 2893 | // Check the real number of LPs corresponding to the filter in the |
||
| 2894 | // database (and if no list was given, get them all) |
||
| 2895 | if (empty($sessionId)) { |
||
| 2896 | $sql = "SELECT DISTINCT(iid), use_max_score |
||
| 2897 | FROM $lp_table |
||
| 2898 | WHERE |
||
| 2899 | c_id = $courseId AND |
||
| 2900 | (session_id = 0 OR session_id IS NULL) $condition_lp "; |
||
| 2901 | } else { |
||
| 2902 | |||
| 2903 | }*/ |
||
| 2904 | |||
| 2905 | $lp_list = $use_max_score = []; |
||
| 2906 | if (empty($condition_lp)) { |
||
| 2907 | $repo = Container::getLpRepository(); |
||
| 2908 | $qb = $repo->findAllByCourse($course, $session); |
||
| 2909 | $lps = $qb->getQuery()->getResult(); |
||
| 2910 | /** @var CLp $lp */ |
||
| 2911 | foreach ($lps as $lp) { |
||
| 2912 | $lpId = $lp->getIid(); |
||
| 2913 | $lp_list[] = $lpId; |
||
| 2914 | $use_max_score[$lpId] = $lp->getUseMaxScore(); |
||
| 2915 | } |
||
| 2916 | } else { |
||
| 2917 | $sql = "SELECT DISTINCT(iid), use_max_score |
||
| 2918 | FROM $lp_table |
||
| 2919 | WHERE $condition_lp "; |
||
| 2920 | $res_row_lp = Database::query($sql); |
||
| 2921 | while ($row_lp = Database::fetch_array($res_row_lp)) { |
||
| 2922 | $lp_list[] = $row_lp['iid']; |
||
| 2923 | $use_max_score[$row_lp['iid']] = $row_lp['use_max_score']; |
||
| 2924 | } |
||
| 2925 | } |
||
| 2926 | |||
| 2927 | if (empty($lp_list)) { |
||
| 2928 | return null; |
||
| 2929 | } |
||
| 2930 | |||
| 2931 | // prepare filter on users |
||
| 2932 | if (is_array($student_id)) { |
||
| 2933 | array_walk($student_id, 'intval'); |
||
| 2934 | $condition_user1 = " AND user_id IN (".implode(',', $student_id).") "; |
||
| 2935 | } else { |
||
| 2936 | $condition_user1 = " AND user_id = $student_id "; |
||
| 2937 | } |
||
| 2938 | |||
| 2939 | // Getting latest LP result for a student |
||
| 2940 | //@todo problem when a course have more than 1500 users |
||
| 2941 | $sql = "SELECT MAX(view_count) as vc, iid, progress, lp_id, user_id |
||
| 2942 | FROM $lp_view_table |
||
| 2943 | WHERE |
||
| 2944 | lp_id IN (".implode(',', $lp_list).") |
||
| 2945 | $condition_user1 |
||
| 2946 | GROUP BY lp_id, user_id"; |
||
| 2947 | //AND session_id = $sessionId |
||
| 2948 | |||
| 2949 | $rs_last_lp_view_id = Database::query($sql); |
||
| 2950 | $global_result = 0; |
||
| 2951 | if (Database::num_rows($rs_last_lp_view_id) > 0) { |
||
| 2952 | // Cycle through each line of the results (grouped by lp_id, user_id) |
||
| 2953 | while ($row_lp_view = Database::fetch_array($rs_last_lp_view_id)) { |
||
| 2954 | $count_items = 0; |
||
| 2955 | $lpPartialTotal = 0; |
||
| 2956 | $list = []; |
||
| 2957 | $lp_view_id = $row_lp_view['iid']; |
||
| 2958 | $lp_id = $row_lp_view['lp_id']; |
||
| 2959 | $user_id = $row_lp_view['user_id']; |
||
| 2960 | |||
| 2961 | if ($debug) { |
||
| 2962 | echo '<h2>LP id '.$lp_id.'</h2>'; |
||
| 2963 | echo "get_only_latest_attempt_results: $get_only_latest_attempt_results <br />"; |
||
| 2964 | echo "getOnlyBestAttempt: $getOnlyBestAttempt <br />"; |
||
| 2965 | } |
||
| 2966 | |||
| 2967 | if ($get_only_latest_attempt_results || $getOnlyBestAttempt) { |
||
| 2968 | // Getting lp_items done by the user |
||
| 2969 | $sql = "SELECT DISTINCT lp_item_id |
||
| 2970 | FROM $lp_item_view_table |
||
| 2971 | WHERE |
||
| 2972 | lp_view_id = $lp_view_id |
||
| 2973 | ORDER BY lp_item_id"; |
||
| 2974 | $res_lp_item = Database::query($sql); |
||
| 2975 | |||
| 2976 | while ($row_lp_item = Database::fetch_assoc($res_lp_item)) { |
||
| 2977 | $my_lp_item_id = $row_lp_item['lp_item_id']; |
||
| 2978 | $order = ' view_count DESC'; |
||
| 2979 | if ($getOnlyBestAttempt) { |
||
| 2980 | $order = ' lp_iv.score DESC'; |
||
| 2981 | } |
||
| 2982 | |||
| 2983 | // Getting the most recent attempt |
||
| 2984 | $sql = "SELECT |
||
| 2985 | lp_iv.iid as lp_item_view_id, |
||
| 2986 | lp_iv.score as score, |
||
| 2987 | lp_i.max_score, |
||
| 2988 | lp_iv.max_score as max_score_item_view, |
||
| 2989 | lp_i.path, |
||
| 2990 | lp_i.item_type, |
||
| 2991 | lp_i.iid |
||
| 2992 | FROM $lp_item_view_table as lp_iv |
||
| 2993 | INNER JOIN $lp_item_table as lp_i |
||
| 2994 | ON ( |
||
| 2995 | lp_i.iid = lp_iv.lp_item_id |
||
| 2996 | ) |
||
| 2997 | WHERE |
||
| 2998 | lp_item_id = $my_lp_item_id AND |
||
| 2999 | lp_view_id = $lp_view_id AND |
||
| 3000 | (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."') |
||
| 3001 | ORDER BY $order |
||
| 3002 | LIMIT 1"; |
||
| 3003 | |||
| 3004 | $res_lp_item_result = Database::query($sql); |
||
| 3005 | while ($row_max_score = Database::fetch_assoc($res_lp_item_result)) { |
||
| 3006 | $list[] = $row_max_score; |
||
| 3007 | } |
||
| 3008 | } |
||
| 3009 | } else { |
||
| 3010 | // For the currently analysed view, get the score and |
||
| 3011 | // max_score of each item if it is a sco or a TOOL_QUIZ |
||
| 3012 | $sql = "SELECT |
||
| 3013 | lp_iv.iid as lp_item_view_id, |
||
| 3014 | lp_iv.score as score, |
||
| 3015 | lp_i.max_score, |
||
| 3016 | lp_iv.max_score as max_score_item_view, |
||
| 3017 | lp_i.path, |
||
| 3018 | lp_i.item_type, |
||
| 3019 | lp_i.iid |
||
| 3020 | FROM $lp_item_view_table as lp_iv |
||
| 3021 | INNER JOIN $lp_item_table as lp_i |
||
| 3022 | ON lp_i.iid = lp_iv.lp_item_id |
||
| 3023 | WHERE |
||
| 3024 | lp_view_id = $lp_view_id AND |
||
| 3025 | (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."') |
||
| 3026 | "; |
||
| 3027 | $res_max_score = Database::query($sql); |
||
| 3028 | while ($row_max_score = Database::fetch_assoc($res_max_score)) { |
||
| 3029 | $list[] = $row_max_score; |
||
| 3030 | } |
||
| 3031 | } |
||
| 3032 | |||
| 3033 | // Go through each scorable element of this view |
||
| 3034 | $score_of_scorm_calculate = 0; |
||
| 3035 | foreach ($list as $row_max_score) { |
||
| 3036 | // Came from the original lp_item |
||
| 3037 | $max_score = $row_max_score['max_score']; |
||
| 3038 | // Came from the lp_item_view |
||
| 3039 | $max_score_item_view = $row_max_score['max_score_item_view']; |
||
| 3040 | $score = $row_max_score['score']; |
||
| 3041 | if ($debug) { |
||
| 3042 | echo '<h3>Item Type: '.$row_max_score['item_type'].'</h3>'; |
||
| 3043 | } |
||
| 3044 | |||
| 3045 | if ('sco' === $row_max_score['item_type']) { |
||
| 3046 | /* Check if it is sco (easier to get max_score) |
||
| 3047 | when there's no max score, we assume 100 as the max score, |
||
| 3048 | as the SCORM 1.2 says that the value should always be between 0 and 100. |
||
| 3049 | */ |
||
| 3050 | if (0 == $max_score || is_null($max_score) || '' == $max_score) { |
||
| 3051 | // Chamilo style |
||
| 3052 | if ($use_max_score[$lp_id]) { |
||
| 3053 | $max_score = 100; |
||
| 3054 | } else { |
||
| 3055 | // Overwrites max score = 100 to use the one that came in the lp_item_view see BT#1613 |
||
| 3056 | $max_score = $max_score_item_view; |
||
| 3057 | } |
||
| 3058 | } |
||
| 3059 | // Avoid division by zero errors |
||
| 3060 | if (!empty($max_score)) { |
||
| 3061 | $lpPartialTotal += $score / $max_score; |
||
| 3062 | } |
||
| 3063 | if ($debug) { |
||
| 3064 | var_dump("lpPartialTotal: $lpPartialTotal"); |
||
| 3065 | var_dump("score: $score"); |
||
| 3066 | var_dump("max_score: $max_score"); |
||
| 3067 | } |
||
| 3068 | } else { |
||
| 3069 | // Case of a TOOL_QUIZ element |
||
| 3070 | $item_id = $row_max_score['iid']; |
||
| 3071 | $item_path = $row_max_score['path']; |
||
| 3072 | $lp_item_view_id = (int) $row_max_score['lp_item_view_id']; |
||
| 3073 | |||
| 3074 | if (empty($lp_item_view_id)) { |
||
| 3075 | $lpItemCondition = ' (orig_lp_item_view_id = 0 OR orig_lp_item_view_id IS NULL) '; |
||
| 3076 | } else { |
||
| 3077 | $lpItemCondition = " orig_lp_item_view_id = $lp_item_view_id "; |
||
| 3078 | } |
||
| 3079 | |||
| 3080 | // Get last attempt to this exercise through |
||
| 3081 | // the current lp for the current user |
||
| 3082 | $order = 'exe_date DESC'; |
||
| 3083 | if ($getOnlyBestAttempt) { |
||
| 3084 | $order = 'score DESC'; |
||
| 3085 | } |
||
| 3086 | $sql = "SELECT exe_id, score |
||
| 3087 | FROM $tbl_stats_exercices |
||
| 3088 | WHERE |
||
| 3089 | exe_exo_id = '$item_path' AND |
||
| 3090 | exe_user_id = $user_id AND |
||
| 3091 | orig_lp_item_id = $item_id AND |
||
| 3092 | $lpItemCondition AND |
||
| 3093 | c_id = $courseId AND |
||
| 3094 | status = '' |
||
| 3095 | $sessionCondition |
||
| 3096 | ORDER BY $order |
||
| 3097 | LIMIT 1"; |
||
| 3098 | |||
| 3099 | $result_last_attempt = Database::query($sql); |
||
| 3100 | $num = Database::num_rows($result_last_attempt); |
||
| 3101 | if ($num > 0) { |
||
| 3102 | $attemptResult = Database::fetch_assoc($result_last_attempt); |
||
| 3103 | $id_last_attempt = $attemptResult['exe_id']; |
||
| 3104 | // We overwrite the score with the best one not the one saved in the LP (latest) |
||
| 3105 | if ($getOnlyBestAttempt && false == $get_only_latest_attempt_results) { |
||
| 3106 | if ($debug) { |
||
| 3107 | echo "Following score comes from the track_exercise table not in the LP because the score is the best<br />"; |
||
| 3108 | } |
||
| 3109 | $score = $attemptResult['score']; |
||
| 3110 | } |
||
| 3111 | |||
| 3112 | if ($debug) { |
||
| 3113 | echo "Attempt id: $id_last_attempt with score $score<br />"; |
||
| 3114 | } |
||
| 3115 | // Within the last attempt number tracking, get the sum of |
||
| 3116 | // the max_scores of all questions that it was |
||
| 3117 | // made of (we need to make this call dynamic because of random questions selection) |
||
| 3118 | $sql = "SELECT SUM(t.ponderation) as maxscore FROM |
||
| 3119 | ( |
||
| 3120 | SELECT DISTINCT |
||
| 3121 | question_id, |
||
| 3122 | marks, |
||
| 3123 | ponderation |
||
| 3124 | FROM $tbl_stats_attempts AS at |
||
| 3125 | INNER JOIN $tbl_quiz_questions AS q |
||
| 3126 | ON (q.iid = at.question_id) |
||
| 3127 | WHERE |
||
| 3128 | exe_id ='$id_last_attempt' |
||
| 3129 | ) |
||
| 3130 | AS t"; |
||
| 3131 | |||
| 3132 | $res_max_score_bis = Database::query($sql); |
||
| 3133 | $row_max_score_bis = Database::fetch_array($res_max_score_bis); |
||
| 3134 | |||
| 3135 | if (!empty($row_max_score_bis['maxscore'])) { |
||
| 3136 | $max_score = $row_max_score_bis['maxscore']; |
||
| 3137 | } |
||
| 3138 | if (!empty($max_score) && floatval($max_score) > 0) { |
||
| 3139 | $lpPartialTotal += $score / $max_score; |
||
| 3140 | } |
||
| 3141 | if ($debug) { |
||
| 3142 | var_dump("score: $score"); |
||
| 3143 | var_dump("max_score: $max_score"); |
||
| 3144 | var_dump("lpPartialTotal: $lpPartialTotal"); |
||
| 3145 | } |
||
| 3146 | } |
||
| 3147 | } |
||
| 3148 | |||
| 3149 | if (in_array($row_max_score['item_type'], ['quiz', 'sco'])) { |
||
| 3150 | // Normal way |
||
| 3151 | if ($use_max_score[$lp_id]) { |
||
| 3152 | $count_items++; |
||
| 3153 | } else { |
||
| 3154 | if ('' != $max_score) { |
||
| 3155 | $count_items++; |
||
| 3156 | } |
||
| 3157 | } |
||
| 3158 | if ($debug) { |
||
| 3159 | echo '$count_items: '.$count_items; |
||
| 3160 | } |
||
| 3161 | } |
||
| 3162 | } |
||
| 3163 | |||
| 3164 | $score_of_scorm_calculate += $count_items ? (($lpPartialTotal / $count_items) * 100) : 0; |
||
| 3165 | $global_result += $score_of_scorm_calculate; |
||
| 3166 | |||
| 3167 | if ($debug) { |
||
| 3168 | var_dump("count_items: $count_items"); |
||
| 3169 | var_dump("score_of_scorm_calculate: $score_of_scorm_calculate"); |
||
| 3170 | var_dump("global_result: $global_result"); |
||
| 3171 | } |
||
| 3172 | } |
||
| 3173 | } |
||
| 3174 | |||
| 3175 | $lp_with_quiz = 0; |
||
| 3176 | foreach ($lp_list as $lp_id) { |
||
| 3177 | // Check if LP have a score we assume that all SCO have an score |
||
| 3178 | $sql = "SELECT count(iid) as count |
||
| 3179 | FROM $lp_item_table |
||
| 3180 | WHERE |
||
| 3181 | (item_type = 'quiz' OR item_type = 'sco') AND |
||
| 3182 | lp_id = ".$lp_id; |
||
| 3183 | $result_have_quiz = Database::query($sql); |
||
| 3184 | if (Database::num_rows($result_have_quiz) > 0) { |
||
| 3185 | $row = Database::fetch_assoc($result_have_quiz); |
||
| 3186 | if (is_numeric($row['count']) && 0 != $row['count']) { |
||
| 3187 | $lp_with_quiz++; |
||
| 3188 | } |
||
| 3189 | } |
||
| 3190 | } |
||
| 3191 | |||
| 3192 | if ($debug) { |
||
| 3193 | echo '<h3>$lp_with_quiz '.$lp_with_quiz.' </h3>'; |
||
| 3194 | echo '<h3>Final return</h3>'; |
||
| 3195 | } |
||
| 3196 | |||
| 3197 | if (0 != $lp_with_quiz) { |
||
| 3198 | if (!$return_array) { |
||
| 3199 | $score_of_scorm_calculate = round(($global_result / $lp_with_quiz), 2); |
||
| 3200 | if ($debug) { |
||
| 3201 | var_dump($score_of_scorm_calculate); |
||
| 3202 | } |
||
| 3203 | if (empty($lp_ids)) { |
||
| 3204 | if ($debug) { |
||
| 3205 | echo '<h2>All lps fix: '.$score_of_scorm_calculate.'</h2>'; |
||
| 3206 | } |
||
| 3207 | } |
||
| 3208 | |||
| 3209 | return $score_of_scorm_calculate; |
||
| 3210 | } |
||
| 3211 | |||
| 3212 | if ($debug) { |
||
| 3213 | var_dump($global_result, $lp_with_quiz); |
||
| 3214 | } |
||
| 3215 | |||
| 3216 | return [$global_result, $lp_with_quiz]; |
||
| 3217 | } |
||
| 3218 | |||
| 3219 | return '-'; |
||
| 3220 | } |
||
| 3221 | |||
| 3222 | /** |
||
| 3223 | * This function gets: |
||
| 3224 | * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max scores. |
||
| 3225 | * 2. The score average from all Tests (quiz) in all LP in a course-> All the answers / All the max scores. |
||
| 3226 | * 3. And finally it will return the average between 1. and 2. |
||
| 3227 | * This function does not take the results of a Test out of a LP. |
||
| 3228 | * |
||
| 3229 | * @param int|array $student_id Array of user ids or an user id |
||
| 3230 | * @param string $course_code Course code |
||
| 3231 | * @param array $lp_ids List of LP ids |
||
| 3232 | * @param int $sessionId Session id (optional), if param $sessionId is 0(default) |
||
| 3233 | * it'll return results including sessions, 0 = session is not filtered |
||
| 3234 | * |
||
| 3235 | * @return string value (number %) Which represents a round integer explain in got in 3 |
||
| 3236 | */ |
||
| 3237 | public static function getAverageStudentScore( |
||
| 3238 | $student_id, |
||
| 3239 | $course_code = '', |
||
| 3240 | $lp_ids = [], |
||
| 3241 | $sessionId = 0 |
||
| 3242 | ) { |
||
| 3243 | if (empty($student_id)) { |
||
| 3244 | return 0; |
||
| 3245 | } |
||
| 3246 | |||
| 3247 | $conditions = []; |
||
| 3248 | if (!empty($course_code)) { |
||
| 3249 | $course = api_get_course_info($course_code); |
||
| 3250 | $courseId = $course['real_id']; |
||
| 3251 | //$conditions[] = " lp.c_id = $courseId"; |
||
| 3252 | } |
||
| 3253 | |||
| 3254 | // Get course tables names |
||
| 3255 | $lp_table = Database::get_course_table(TABLE_LP_MAIN); |
||
| 3256 | $lp_item_table = Database::get_course_table(TABLE_LP_ITEM); |
||
| 3257 | $lp_view_table = Database::get_course_table(TABLE_LP_VIEW); |
||
| 3258 | $lp_item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW); |
||
| 3259 | |||
| 3260 | // Compose a filter based on optional learning paths list given |
||
| 3261 | if (!empty($lp_ids) && count($lp_ids) > 0) { |
||
| 3262 | $conditions[] = ' lp.iid IN ('.implode(',', $lp_ids).') '; |
||
| 3263 | } |
||
| 3264 | |||
| 3265 | // Compose a filter based on optional session id |
||
| 3266 | $sessionId = (int) $sessionId; |
||
| 3267 | if (!empty($sessionId)) { |
||
| 3268 | $conditions[] = " lp_view.session_id = $sessionId "; |
||
| 3269 | } |
||
| 3270 | |||
| 3271 | if (is_array($student_id)) { |
||
| 3272 | array_walk($student_id, 'intval'); |
||
| 3273 | $conditions[] = " lp_view.user_id IN (".implode(',', $student_id).") "; |
||
| 3274 | } else { |
||
| 3275 | $student_id = (int) $student_id; |
||
| 3276 | $conditions[] = " lp_view.user_id = $student_id "; |
||
| 3277 | } |
||
| 3278 | |||
| 3279 | $conditionsToString = implode(' AND ', $conditions); |
||
| 3280 | $sql = "SELECT |
||
| 3281 | SUM(lp_iv.score) sum_score, |
||
| 3282 | SUM(lp_i.max_score) sum_max_score |
||
| 3283 | FROM $lp_table as lp |
||
| 3284 | INNER JOIN $lp_item_table as lp_i |
||
| 3285 | ON lp.iid = lp_i.lp_id |
||
| 3286 | INNER JOIN $lp_view_table as lp_view |
||
| 3287 | ON lp_view.lp_id = lp_i.lp_id |
||
| 3288 | INNER JOIN $lp_item_view_table as lp_iv |
||
| 3289 | ON |
||
| 3290 | lp_i.iid = lp_iv.lp_item_id AND |
||
| 3291 | lp_iv.lp_view_id = lp_view.iid |
||
| 3292 | WHERE (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."') AND |
||
| 3293 | $conditionsToString |
||
| 3294 | "; |
||
| 3295 | $result = Database::query($sql); |
||
| 3296 | $row = Database::fetch_assoc($result); |
||
| 3297 | |||
| 3298 | if (empty($row['sum_max_score'])) { |
||
| 3299 | return 0; |
||
| 3300 | } |
||
| 3301 | |||
| 3302 | return ($row['sum_score'] / $row['sum_max_score']) * 100; |
||
| 3303 | } |
||
| 3304 | |||
| 3305 | /** |
||
| 3306 | * This function gets time spent in learning path for a student inside a course. |
||
| 3307 | * |
||
| 3308 | * @param int|array $student_id Student id(s) |
||
| 3309 | * @param Course $course Course code |
||
| 3310 | * @param array $lp_ids Limit average to listed lp ids |
||
| 3311 | * @param int $sessionId Session id (optional), if param $sessionId is null(default) |
||
| 3312 | * it'll return results including sessions, 0 = session is not filtered |
||
| 3313 | * |
||
| 3314 | * @return int Total time in seconds |
||
| 3315 | */ |
||
| 3316 | public static function get_time_spent_in_lp( |
||
| 3317 | $student_id, |
||
| 3318 | Course $course, |
||
| 3319 | $lp_ids = [], |
||
| 3320 | $sessionId = 0 |
||
| 3321 | ) { |
||
| 3322 | $student_id = (int) $student_id; |
||
| 3323 | $sessionId = (int) $sessionId; |
||
| 3324 | $total_time = 0; |
||
| 3325 | |||
| 3326 | if (!empty($course)) { |
||
| 3327 | $lpTable = Database::get_course_table(TABLE_LP_MAIN); |
||
| 3328 | $lpItemTable = Database::get_course_table(TABLE_LP_ITEM); |
||
| 3329 | $lpViewTable = Database::get_course_table(TABLE_LP_VIEW); |
||
| 3330 | $lpItemViewTable = Database::get_course_table(TABLE_LP_ITEM_VIEW); |
||
| 3331 | $trackExercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 3332 | $courseId = $course->getId(); |
||
| 3333 | |||
| 3334 | // Compose a filter based on optional learning paths list given |
||
| 3335 | $condition_lp = ''; |
||
| 3336 | if (count($lp_ids) > 0) { |
||
| 3337 | $condition_lp = " AND iid IN(".implode(',', $lp_ids).") "; |
||
| 3338 | } |
||
| 3339 | |||
| 3340 | // Check the real number of LPs corresponding to the filter in the |
||
| 3341 | // database (and if no list was given, get them all) |
||
| 3342 | $sql = "SELECT DISTINCT(iid) FROM $lpTable |
||
| 3343 | WHERE 1=1 $condition_lp"; |
||
| 3344 | $result = Database::query($sql); |
||
| 3345 | $session_condition = api_get_session_condition($sessionId); |
||
| 3346 | |||
| 3347 | // calculates time |
||
| 3348 | if (Database::num_rows($result) > 0) { |
||
| 3349 | while ($row = Database::fetch_array($result)) { |
||
| 3350 | $lp_id = (int) $row['iid']; |
||
| 3351 | $lp = Container::getLpRepository()->find($lp_id); |
||
| 3352 | // Start Exercise in LP total_time |
||
| 3353 | // Get duration time from track_e_exercises.exe_duration instead of lp_view_item.total_time |
||
| 3354 | $list = learnpath::get_flat_ordered_items_list($lp, 0, $courseId); |
||
| 3355 | foreach ($list as $itemId) { |
||
| 3356 | $sql = "SELECT max(view_count) |
||
| 3357 | FROM $lpViewTable |
||
| 3358 | WHERE |
||
| 3359 | c_id = $courseId AND |
||
| 3360 | lp_id = $lp_id AND |
||
| 3361 | user_id = $student_id |
||
| 3362 | $session_condition"; |
||
| 3363 | $res = Database::query($sql); |
||
| 3364 | $view = ''; |
||
| 3365 | if (Database::num_rows($res) > 0) { |
||
| 3366 | $myrow = Database::fetch_array($res); |
||
| 3367 | $view = $myrow[0]; |
||
| 3368 | } |
||
| 3369 | $viewCondition = null; |
||
| 3370 | if (!empty($view)) { |
||
| 3371 | $viewCondition = " AND v.view_count = $view "; |
||
| 3372 | } |
||
| 3373 | $sql = "SELECT |
||
| 3374 | iv.iid, |
||
| 3375 | iv.total_time as mytime, |
||
| 3376 | i.iid as myid, |
||
| 3377 | iv.view_count as iv_view_count, |
||
| 3378 | path |
||
| 3379 | FROM $lpItemTable as i |
||
| 3380 | INNER JOIN $lpItemViewTable as iv |
||
| 3381 | ON (i.iid = iv.lp_item_id) |
||
| 3382 | INNER JOIN $lpViewTable as v |
||
| 3383 | ON (iv.lp_view_id = v.iid) |
||
| 3384 | WHERE |
||
| 3385 | v.c_id = $courseId AND |
||
| 3386 | i.iid = $itemId AND |
||
| 3387 | i.lp_id = $lp_id AND |
||
| 3388 | v.user_id = $student_id AND |
||
| 3389 | item_type = 'quiz' AND |
||
| 3390 | path <> '' AND |
||
| 3391 | v.session_id = $sessionId |
||
| 3392 | $viewCondition |
||
| 3393 | ORDER BY iv.view_count DESC "; |
||
| 3394 | |||
| 3395 | $resultRow = Database::query($sql); |
||
| 3396 | if (Database::num_rows($resultRow)) { |
||
| 3397 | $row = Database::fetch_array($resultRow); |
||
| 3398 | $totalTimeInLpItemView = $row['mytime']; |
||
| 3399 | $lpItemViewId = $row['iid']; |
||
| 3400 | $sessionCondition = api_get_session_condition($sessionId); |
||
| 3401 | $sql = 'SELECT SUM(exe_duration) exe_duration |
||
| 3402 | FROM '.$trackExercises.' |
||
| 3403 | WHERE |
||
| 3404 | exe_exo_id="'.$row['path'].'" AND |
||
| 3405 | exe_user_id="'.$student_id.'" AND |
||
| 3406 | orig_lp_id = "'.$lp_id.'" AND |
||
| 3407 | orig_lp_item_id = "'.$row['myid'].'" AND |
||
| 3408 | c_id = '.$courseId.' AND |
||
| 3409 | status <> "incomplete" |
||
| 3410 | '.$sessionCondition.' |
||
| 3411 | ORDER BY exe_date DESC '; |
||
| 3412 | |||
| 3413 | $sumScoreResult = Database::query($sql); |
||
| 3414 | $durationRow = Database::fetch_assoc($sumScoreResult); |
||
| 3415 | if (!empty($durationRow['exe_duration'])) { |
||
| 3416 | $exeDuration = $durationRow['exe_duration']; |
||
| 3417 | if ($exeDuration != $totalTimeInLpItemView && |
||
| 3418 | !empty($lpItemViewId) && |
||
| 3419 | !empty($exeDuration) |
||
| 3420 | ) { |
||
| 3421 | // Update c_lp_item_view.total_time |
||
| 3422 | $sqlUpdate = "UPDATE $lpItemViewTable |
||
| 3423 | SET total_time = '$exeDuration' |
||
| 3424 | WHERE iid = ".$lpItemViewId; |
||
| 3425 | Database::query($sqlUpdate); |
||
| 3426 | } |
||
| 3427 | } |
||
| 3428 | } |
||
| 3429 | } |
||
| 3430 | |||
| 3431 | // End total_time fix |
||
| 3432 | |||
| 3433 | // Calculate total time |
||
| 3434 | $sql = "SELECT SUM(total_time) |
||
| 3435 | FROM $lpItemViewTable AS item_view |
||
| 3436 | INNER JOIN $lpViewTable AS view |
||
| 3437 | ON ( |
||
| 3438 | item_view.lp_view_id = view.iid |
||
| 3439 | ) |
||
| 3440 | WHERE |
||
| 3441 | view.c_id = $courseId AND |
||
| 3442 | view.lp_id = $lp_id AND |
||
| 3443 | view.user_id = $student_id AND |
||
| 3444 | session_id = $sessionId"; |
||
| 3445 | |||
| 3446 | $rs = Database::query($sql); |
||
| 3447 | if (Database::num_rows($rs) > 0) { |
||
| 3448 | $total_time += Database::result($rs, 0, 0); |
||
| 3449 | } |
||
| 3450 | } |
||
| 3451 | } |
||
| 3452 | } |
||
| 3453 | |||
| 3454 | return $total_time; |
||
| 3455 | } |
||
| 3456 | |||
| 3457 | /** |
||
| 3458 | * This function gets last connection time to one learning path. |
||
| 3459 | * |
||
| 3460 | * @param int|array $student_id Student id(s) |
||
| 3461 | * @param string $course_code Course code |
||
| 3462 | * @param int $lp_id Learning path id |
||
| 3463 | * @param int $sessionId |
||
| 3464 | * |
||
| 3465 | * @return int last connection timestamp |
||
| 3466 | */ |
||
| 3467 | public static function get_last_connection_time_in_lp( |
||
| 3468 | $student_id, |
||
| 3469 | $course_code, |
||
| 3470 | $lp_id, |
||
| 3471 | $sessionId = 0 |
||
| 3472 | ) { |
||
| 3473 | $course = api_get_course_info($course_code); |
||
| 3474 | if (empty($course)) { |
||
| 3475 | return 0; |
||
| 3476 | } |
||
| 3477 | |||
| 3478 | $courseId = $course['real_id']; |
||
| 3479 | $student_id = (int) $student_id; |
||
| 3480 | $lp_id = (int) $lp_id; |
||
| 3481 | $sessionId = (int) $sessionId; |
||
| 3482 | $lastTime = 0; |
||
| 3483 | |||
| 3484 | if (self::minimumTimeAvailable($sessionId, $courseId)) { |
||
| 3485 | $sql = "SELECT MAX(date_reg) max |
||
| 3486 | FROM track_e_access_complete |
||
| 3487 | WHERE |
||
| 3488 | user_id = $student_id AND |
||
| 3489 | c_id = $courseId AND |
||
| 3490 | session_id = $sessionId AND |
||
| 3491 | tool = 'learnpath' AND |
||
| 3492 | tool_id = $lp_id AND |
||
| 3493 | action = 'view' AND |
||
| 3494 | login_as = 0 |
||
| 3495 | ORDER BY date_reg ASC |
||
| 3496 | LIMIT 1"; |
||
| 3497 | $rs = Database::query($sql); |
||
| 3498 | |||
| 3499 | $lastConnection = 0; |
||
| 3500 | if (Database::num_rows($rs) > 0) { |
||
| 3501 | $value = Database::fetch_array($rs); |
||
| 3502 | if (isset($value['max']) && !empty($value['max'])) { |
||
| 3503 | $lastConnection = api_strtotime($value['max'], 'UTC'); |
||
| 3504 | } |
||
| 3505 | } |
||
| 3506 | |||
| 3507 | if (!empty($lastConnection)) { |
||
| 3508 | return $lastConnection; |
||
| 3509 | } |
||
| 3510 | } |
||
| 3511 | if (!empty($course)) { |
||
| 3512 | $lp_table = Database::get_course_table(TABLE_LP_MAIN); |
||
| 3513 | $t_lpv = Database::get_course_table(TABLE_LP_VIEW); |
||
| 3514 | $t_lpiv = Database::get_course_table(TABLE_LP_ITEM_VIEW); |
||
| 3515 | |||
| 3516 | // Check the real number of LPs corresponding to the filter in the |
||
| 3517 | // database (and if no list was given, get them all) |
||
| 3518 | $sql = "SELECT iid FROM $lp_table |
||
| 3519 | WHERE iid = $lp_id "; |
||
| 3520 | $row = Database::query($sql); |
||
| 3521 | $count = Database::num_rows($row); |
||
| 3522 | |||
| 3523 | // calculates last connection time |
||
| 3524 | if ($count > 0) { |
||
| 3525 | $sql = 'SELECT MAX(start_time) |
||
| 3526 | FROM '.$t_lpiv.' AS item_view |
||
| 3527 | INNER JOIN '.$t_lpv.' AS view |
||
| 3528 | ON (item_view.lp_view_id = view.iid) |
||
| 3529 | WHERE |
||
| 3530 | status != "not attempted" AND |
||
| 3531 | view.c_id = '.$courseId.' AND |
||
| 3532 | view.lp_id = '.$lp_id.' AND |
||
| 3533 | view.user_id = '.$student_id.' AND |
||
| 3534 | view.session_id = '.$sessionId; |
||
| 3535 | $rs = Database::query($sql); |
||
| 3536 | if (Database::num_rows($rs) > 0) { |
||
| 3537 | $lastTime = Database::result($rs, 0, 0); |
||
| 3538 | } |
||
| 3539 | } |
||
| 3540 | } |
||
| 3541 | |||
| 3542 | return $lastTime; |
||
| 3543 | } |
||
| 3544 | |||
| 3545 | public static function getFirstConnectionTimeInLp( |
||
| 3546 | $student_id, |
||
| 3547 | $course_code, |
||
| 3548 | $lp_id, |
||
| 3549 | $sessionId = 0 |
||
| 3550 | ) { |
||
| 3551 | $course = api_get_course_info($course_code); |
||
| 3552 | $student_id = (int) $student_id; |
||
| 3553 | $lp_id = (int) $lp_id; |
||
| 3554 | $sessionId = (int) $sessionId; |
||
| 3555 | $time = 0; |
||
| 3556 | |||
| 3557 | if (!empty($course)) { |
||
| 3558 | $courseId = $course['real_id']; |
||
| 3559 | $lp_table = Database::get_course_table(TABLE_LP_MAIN); |
||
| 3560 | $t_lpv = Database::get_course_table(TABLE_LP_VIEW); |
||
| 3561 | $t_lpiv = Database::get_course_table(TABLE_LP_ITEM_VIEW); |
||
| 3562 | |||
| 3563 | // Check the real number of LPs corresponding to the filter in the |
||
| 3564 | // database (and if no list was given, get them all) |
||
| 3565 | $sql = "SELECT iid FROM $lp_table |
||
| 3566 | WHERE iid = $lp_id "; |
||
| 3567 | $row = Database::query($sql); |
||
| 3568 | $count = Database::num_rows($row); |
||
| 3569 | |||
| 3570 | // calculates first connection time |
||
| 3571 | if ($count > 0) { |
||
| 3572 | $sql = 'SELECT MIN(start_time) |
||
| 3573 | FROM '.$t_lpiv.' AS item_view |
||
| 3574 | INNER JOIN '.$t_lpv.' AS view |
||
| 3575 | ON (item_view.lp_view_id = view.iid) |
||
| 3576 | WHERE |
||
| 3577 | status != "not attempted" AND |
||
| 3578 | view.c_id = '.$courseId.' AND |
||
| 3579 | view.lp_id = '.$lp_id.' AND |
||
| 3580 | view.user_id = '.$student_id.' AND |
||
| 3581 | view.session_id = '.$sessionId; |
||
| 3582 | $rs = Database::query($sql); |
||
| 3583 | if (Database::num_rows($rs) > 0) { |
||
| 3584 | $time = Database::result($rs, 0, 0); |
||
| 3585 | } |
||
| 3586 | } |
||
| 3587 | } |
||
| 3588 | |||
| 3589 | return $time; |
||
| 3590 | } |
||
| 3591 | |||
| 3592 | /** |
||
| 3593 | * gets the list of students followed by coach. |
||
| 3594 | * |
||
| 3595 | * @param int $coach_id Coach id |
||
| 3596 | * |
||
| 3597 | * @return array List of students |
||
| 3598 | */ |
||
| 3599 | public static function get_student_followed_by_coach($coach_id) |
||
| 3600 | { |
||
| 3601 | $coach_id = (int) $coach_id; |
||
| 3602 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 3603 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3604 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3605 | $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 3606 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3607 | |||
| 3608 | $access_url_id = api_get_current_access_url_id(); |
||
| 3609 | |||
| 3610 | $students = []; |
||
| 3611 | // At first, courses where $coach_id is coach of the course // |
||
| 3612 | $sql = 'SELECT scu.session_id, scu.c_id |
||
| 3613 | FROM '.$tbl_session_course_user.' scu |
||
| 3614 | INNER JOIN '.$tbl_session_rel_access_url.' sru |
||
| 3615 | ON (scu.session_id=sru.session_id) |
||
| 3616 | WHERE |
||
| 3617 | scu.user_id='.$coach_id.' AND |
||
| 3618 | scu.status = '.SessionEntity::COURSE_COACH.' AND |
||
| 3619 | sru.access_url_id = '.$access_url_id; |
||
| 3620 | |||
| 3621 | $result = Database::query($sql); |
||
| 3622 | |||
| 3623 | while ($a_courses = Database::fetch_array($result)) { |
||
| 3624 | $courseId = $a_courses['c_id']; |
||
| 3625 | $sessionId = $a_courses['session_id']; |
||
| 3626 | |||
| 3627 | $sql = "SELECT DISTINCT srcru.user_id |
||
| 3628 | FROM $tbl_session_course_user AS srcru |
||
| 3629 | INNER JOIN $tbl_session_user sru |
||
| 3630 | ON (srcru.user_id = sru.user_id AND srcru.session_id = sru.session_id) |
||
| 3631 | WHERE |
||
| 3632 | sru.relation_type = ".SessionEntity::STUDENT." AND |
||
| 3633 | srcru.c_id = '$courseId' AND |
||
| 3634 | srcru.session_id = '$sessionId'"; |
||
| 3635 | |||
| 3636 | $rs = Database::query($sql); |
||
| 3637 | while ($row = Database::fetch_array($rs)) { |
||
| 3638 | $students[$row['user_id']] = $row['user_id']; |
||
| 3639 | } |
||
| 3640 | } |
||
| 3641 | |||
| 3642 | // Then, courses where $coach_id is coach of the session |
||
| 3643 | $sql = "SELECT srcru.user_id |
||
| 3644 | FROM $tbl_session_course_user srcru |
||
| 3645 | INNER JOIN $tbl_session_course src |
||
| 3646 | ON (srcru.c_id = src.c_id AND srcru.session_id = src.session_id) |
||
| 3647 | INNER JOIN $tbl_session s |
||
| 3648 | ON srcru.session_id = s.id AND src.session_id = s.id |
||
| 3649 | INNER JOIN $tbl_session_user sru on s.id = sru.session_id |
||
| 3650 | WHERE |
||
| 3651 | srcru.status = ".SessionEntity::STUDENT." AND |
||
| 3652 | sru.relation_type = ".SessionEntity::GENERAL_COACH." AND |
||
| 3653 | sru.user_id = $coach_id"; |
||
| 3654 | |||
| 3655 | if (-1 != $access_url_id) { |
||
| 3656 | $sql = "SELECT srcru.user_id |
||
| 3657 | FROM $tbl_session_course_user srcru |
||
| 3658 | INNER JOIN $tbl_session_course src |
||
| 3659 | ON (srcru.c_id = src.c_id AND srcru.session_id = src.session_id) |
||
| 3660 | INNER JOIN $tbl_session s |
||
| 3661 | ON srcru.session_id = s.id AND src.session_id = s.id |
||
| 3662 | INNER JOIN $tbl_session_user sru |
||
| 3663 | ON s.id = sru.session_id |
||
| 3664 | INNER JOIN $tbl_session_rel_access_url aurs |
||
| 3665 | ON s.id = aurs.session_id |
||
| 3666 | WHERE |
||
| 3667 | srcru.status = ".SessionEntity::STUDENT." AND |
||
| 3668 | sru.relation_type = ".SessionEntity::GENERAL_COACH." AND |
||
| 3669 | sru.user_id = $coach_id AND |
||
| 3670 | aurs.access_url_id = $access_url_id"; |
||
| 3671 | } |
||
| 3672 | |||
| 3673 | $result = Database::query($sql); |
||
| 3674 | while ($row = Database::fetch_array($result)) { |
||
| 3675 | $students[$row['user_id']] = $row['user_id']; |
||
| 3676 | } |
||
| 3677 | |||
| 3678 | return $students; |
||
| 3679 | } |
||
| 3680 | |||
| 3681 | /** |
||
| 3682 | * Check if a coach is allowed to follow a student. |
||
| 3683 | * |
||
| 3684 | * @param int Coach id |
||
| 3685 | * @param int Student id |
||
| 3686 | * |
||
| 3687 | * @return bool |
||
| 3688 | */ |
||
| 3689 | public static function is_allowed_to_coach_student($coach_id, $student_id) |
||
| 3690 | { |
||
| 3691 | $coach_id = intval($coach_id); |
||
| 3692 | $student_id = intval($student_id); |
||
| 3693 | |||
| 3694 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3695 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3696 | $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 3697 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3698 | |||
| 3699 | // At first, courses where $coach_id is coach of the course |
||
| 3700 | $sql = 'SELECT 1 FROM '.$tbl_session_course_user.' |
||
| 3701 | WHERE user_id='.$coach_id.' AND status = '.SessionEntity::COURSE_COACH; |
||
| 3702 | $result = Database::query($sql); |
||
| 3703 | if (Database::num_rows($result) > 0) { |
||
| 3704 | return true; |
||
| 3705 | } |
||
| 3706 | |||
| 3707 | // Then, courses where $coach_id is coach of the session |
||
| 3708 | $sql = "SELECT srcru.user_id |
||
| 3709 | FROM $tbl_session_course_user srcru |
||
| 3710 | INNER JOIN $tbl_session_course src |
||
| 3711 | ON (srcru.c_id = src.c_id AND srcru.session_id = src.session_id) |
||
| 3712 | INNER JOIN $tbl_session s |
||
| 3713 | ON srcru.session_id = s.id AND src.session_id = s.id |
||
| 3714 | INNER JOIN $tblSessionRelUser sru |
||
| 3715 | ON s.id = sru.session_id |
||
| 3716 | WHERE |
||
| 3717 | (srcru.status = ".SessionEntity::STUDENT." AND srcru.user_id = $student_id) AND |
||
| 3718 | (sru.relation_type = ".SessionEntity::GENERAL_COACH." AND sru.user_id = $coach_id)"; |
||
| 3719 | $result = Database::query($sql); |
||
| 3720 | if (Database::num_rows($result) > 0) { |
||
| 3721 | return true; |
||
| 3722 | } |
||
| 3723 | |||
| 3724 | return false; |
||
| 3725 | } |
||
| 3726 | |||
| 3727 | /** |
||
| 3728 | * Get courses followed by coach. |
||
| 3729 | * |
||
| 3730 | * @param int Coach id |
||
| 3731 | * @param int Session id (optional) |
||
| 3732 | * |
||
| 3733 | * @return array Courses list |
||
| 3734 | */ |
||
| 3735 | public static function get_courses_followed_by_coach($coach_id, $sessionId = 0) |
||
| 3736 | { |
||
| 3737 | $coach_id = intval($coach_id); |
||
| 3738 | if (!empty($sessionId)) { |
||
| 3739 | $sessionId = intval($sessionId); |
||
| 3740 | } |
||
| 3741 | |||
| 3742 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3743 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3744 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3745 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3746 | $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 3747 | $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 3748 | |||
| 3749 | // At first, courses where $coach_id is coach of the course. |
||
| 3750 | $sql = 'SELECT DISTINCT c.code |
||
| 3751 | FROM '.$tbl_session_course_user.' sc |
||
| 3752 | INNER JOIN '.$tbl_course.' c |
||
| 3753 | ON (c.id = sc.c_id) |
||
| 3754 | WHERE sc.user_id = '.$coach_id.' AND sc.status = '.SessionEntity::COURSE_COACH; |
||
| 3755 | |||
| 3756 | $access_url_id = api_get_current_access_url_id(); |
||
| 3757 | $sql = 'SELECT DISTINCT c.code |
||
| 3758 | FROM '.$tbl_session_course_user.' scu |
||
| 3759 | INNER JOIN '.$tbl_course.' c |
||
| 3760 | ON (c.code = scu.c_id) |
||
| 3761 | INNER JOIN '.$tbl_course_rel_access_url.' cru |
||
| 3762 | ON (c.id = cru.c_id) |
||
| 3763 | WHERE |
||
| 3764 | scu.user_id='.$coach_id.' AND |
||
| 3765 | scu.status = '.SessionEntity::COURSE_COACH.' AND |
||
| 3766 | cru.access_url_id = '.$access_url_id; |
||
| 3767 | |||
| 3768 | if (!empty($sessionId)) { |
||
| 3769 | $sql .= ' AND session_id='.$sessionId; |
||
| 3770 | } |
||
| 3771 | |||
| 3772 | $courseList = []; |
||
| 3773 | $result = Database::query($sql); |
||
| 3774 | while ($row = Database::fetch_array($result)) { |
||
| 3775 | $courseList[$row['code']] = $row['code']; |
||
| 3776 | } |
||
| 3777 | |||
| 3778 | // Then, courses where $coach_id is coach of the session |
||
| 3779 | $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 3780 | $access_url_id = api_get_current_access_url_id(); |
||
| 3781 | $sql = "SELECT DISTINCT c.code |
||
| 3782 | FROM $tbl_session_course as session_course |
||
| 3783 | INNER JOIN $tbl_course c |
||
| 3784 | ON (c.id = session_course.c_id) |
||
| 3785 | INNER JOIN $tbl_session as session |
||
| 3786 | ON session.id = session_course.session_id |
||
| 3787 | INNER JOIN $tblSessionRelUser session_user |
||
| 3788 | ON (session.id = session_user.session_id |
||
| 3789 | AND session_user.user_id = $coach_id |
||
| 3790 | AND session_user.relation_type = ".SessionEntity::GENERAL_COACH.") |
||
| 3791 | INNER JOIN $tbl_course as course |
||
| 3792 | ON course.id = session_course.c_id |
||
| 3793 | INNER JOIN $tbl_course_rel_access_url course_rel_url |
||
| 3794 | ON (course_rel_url.c_id = c.id)"; |
||
| 3795 | |||
| 3796 | if (!empty($sessionId)) { |
||
| 3797 | $sql .= ' WHERE session_course.session_id='.$sessionId; |
||
| 3798 | $sql .= ' AND access_url_id = '.$access_url_id; |
||
| 3799 | } else { |
||
| 3800 | $sql .= ' WHERE access_url_id = '.$access_url_id; |
||
| 3801 | } |
||
| 3802 | |||
| 3803 | $result = Database::query($sql); |
||
| 3804 | while ($row = Database::fetch_array($result)) { |
||
| 3805 | $courseList[$row['code']] = $row['code']; |
||
| 3806 | } |
||
| 3807 | |||
| 3808 | return $courseList; |
||
| 3809 | } |
||
| 3810 | |||
| 3811 | /** |
||
| 3812 | * Get sessions coached by user. |
||
| 3813 | * |
||
| 3814 | * @param int $coach_id |
||
| 3815 | * @param int $start |
||
| 3816 | * @param int $limit |
||
| 3817 | * @param bool $getCount |
||
| 3818 | * @param string $keyword |
||
| 3819 | * @param string $description |
||
| 3820 | * @param string $orderByName |
||
| 3821 | * @param string $orderByDirection |
||
| 3822 | * @param array $options |
||
| 3823 | * |
||
| 3824 | * @return mixed |
||
| 3825 | */ |
||
| 3826 | public static function get_sessions_coached_by_user( |
||
| 3827 | $coach_id, |
||
| 3828 | $start = 0, |
||
| 3829 | $limit = 0, |
||
| 3830 | $getCount = false, |
||
| 3831 | $keyword = '', |
||
| 3832 | $description = '', |
||
| 3833 | $orderByName = '', |
||
| 3834 | $orderByDirection = '', |
||
| 3835 | $options = [] |
||
| 3836 | ) { |
||
| 3837 | // table definition |
||
| 3838 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3839 | $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 3840 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3841 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 3842 | |||
| 3843 | $coach_id = (int) $coach_id; |
||
| 3844 | |||
| 3845 | $select = ' SELECT * FROM '; |
||
| 3846 | if ($getCount) { |
||
| 3847 | $select = ' SELECT count(DISTINCT id) as count FROM '; |
||
| 3848 | } |
||
| 3849 | |||
| 3850 | $limitCondition = null; |
||
| 3851 | if (!empty($start) && !empty($limit)) { |
||
| 3852 | $limitCondition = " LIMIT ".intval($start).", ".intval($limit); |
||
| 3853 | } |
||
| 3854 | |||
| 3855 | $keywordCondition = null; |
||
| 3856 | if (!empty($keyword)) { |
||
| 3857 | $keyword = Database::escape_string($keyword); |
||
| 3858 | $keywordCondition = " AND (title LIKE '%$keyword%' ) "; |
||
| 3859 | |||
| 3860 | if (!empty($description)) { |
||
| 3861 | $description = Database::escape_string($description); |
||
| 3862 | $keywordCondition = " AND (title LIKE '%$keyword%' OR description LIKE '%$description%' ) "; |
||
| 3863 | } |
||
| 3864 | } |
||
| 3865 | |||
| 3866 | $extraFieldModel = new ExtraFieldModel('session'); |
||
| 3867 | $conditions = $extraFieldModel->parseConditions($options); |
||
| 3868 | $sqlInjectJoins = $conditions['inject_joins']; |
||
| 3869 | $extraFieldsConditions = $conditions['where']; |
||
| 3870 | $sqlInjectWhere = $conditions['inject_where']; |
||
| 3871 | $injectExtraFields = $conditions['inject_extra_fields']; |
||
| 3872 | |||
| 3873 | $access_url_id = api_get_current_access_url_id(); |
||
| 3874 | |||
| 3875 | $orderBy = ''; |
||
| 3876 | if (!empty($orderByName)) { |
||
| 3877 | if (in_array($orderByName, ['title', 'access_start_date'])) { |
||
| 3878 | $orderByDirection = in_array(strtolower($orderByDirection), ['asc', 'desc']) ? $orderByDirection : 'asc'; |
||
| 3879 | $orderByName = Database::escape_string($orderByName); |
||
| 3880 | $orderBy .= " ORDER BY `$orderByName` $orderByDirection"; |
||
| 3881 | } |
||
| 3882 | } |
||
| 3883 | |||
| 3884 | $sql = " |
||
| 3885 | $select |
||
| 3886 | ( |
||
| 3887 | SELECT DISTINCT |
||
| 3888 | s.id, |
||
| 3889 | title, |
||
| 3890 | $injectExtraFields |
||
| 3891 | access_start_date, |
||
| 3892 | access_end_date |
||
| 3893 | FROM $tbl_session s |
||
| 3894 | INNER JOIN $tbl_session_rel_access_url session_rel_url |
||
| 3895 | ON (s.id = session_rel_url.session_id) |
||
| 3896 | $sqlInjectJoins |
||
| 3897 | INNER JOIN $tblSessionRelUser sru ON s.id = sru.session_id |
||
| 3898 | WHERE |
||
| 3899 | (sru.user_id = $coach_id AND sru.relation_type = ".SessionEntity::GENERAL_COACH.") AND |
||
| 3900 | access_url_id = $access_url_id |
||
| 3901 | $keywordCondition |
||
| 3902 | $extraFieldsConditions |
||
| 3903 | $sqlInjectWhere |
||
| 3904 | UNION |
||
| 3905 | SELECT DISTINCT |
||
| 3906 | s.id, |
||
| 3907 | s.title, |
||
| 3908 | $injectExtraFields |
||
| 3909 | s.access_start_date, |
||
| 3910 | s.access_end_date |
||
| 3911 | FROM $tbl_session as s |
||
| 3912 | INNER JOIN $tbl_session_course_user as session_course_user |
||
| 3913 | ON |
||
| 3914 | s.id = session_course_user.session_id AND |
||
| 3915 | session_course_user.user_id = $coach_id AND |
||
| 3916 | session_course_user.status = ".SessionEntity::COURSE_COACH." |
||
| 3917 | INNER JOIN $tbl_session_rel_access_url session_rel_url |
||
| 3918 | ON (s.id = session_rel_url.session_id) |
||
| 3919 | $sqlInjectJoins |
||
| 3920 | WHERE |
||
| 3921 | access_url_id = $access_url_id |
||
| 3922 | $keywordCondition |
||
| 3923 | $extraFieldsConditions |
||
| 3924 | $sqlInjectWhere |
||
| 3925 | ) as sessions $limitCondition $orderBy |
||
| 3926 | "; |
||
| 3927 | |||
| 3928 | $rs = Database::query($sql); |
||
| 3929 | if ($getCount) { |
||
| 3930 | $row = Database::fetch_array($rs); |
||
| 3931 | |||
| 3932 | return $row['count']; |
||
| 3933 | } |
||
| 3934 | |||
| 3935 | $sessions = []; |
||
| 3936 | while ($row = Database::fetch_array($rs)) { |
||
| 3937 | if ('0000-00-00 00:00:00' === $row['access_start_date']) { |
||
| 3938 | $row['access_start_date'] = null; |
||
| 3939 | } |
||
| 3940 | |||
| 3941 | $sessions[$row['id']] = $row; |
||
| 3942 | } |
||
| 3943 | |||
| 3944 | if (!empty($sessions)) { |
||
| 3945 | foreach ($sessions as &$session) { |
||
| 3946 | if (empty($session['access_start_date'])) { |
||
| 3947 | $session['status'] = get_lang('active'); |
||
| 3948 | } else { |
||
| 3949 | $time_start = api_strtotime($session['access_start_date'], 'UTC'); |
||
| 3950 | $time_end = api_strtotime($session['access_end_date'], 'UTC'); |
||
| 3951 | if ($time_start < time() && time() < $time_end) { |
||
| 3952 | $session['status'] = get_lang('active'); |
||
| 3953 | } else { |
||
| 3954 | if (time() < $time_start) { |
||
| 3955 | $session['status'] = get_lang('Not yet begun'); |
||
| 3956 | } else { |
||
| 3957 | if (time() > $time_end) { |
||
| 3958 | $session['status'] = get_lang('Past'); |
||
| 3959 | } |
||
| 3960 | } |
||
| 3961 | } |
||
| 3962 | } |
||
| 3963 | } |
||
| 3964 | } |
||
| 3965 | |||
| 3966 | return $sessions; |
||
| 3967 | } |
||
| 3968 | |||
| 3969 | /** |
||
| 3970 | * Get courses list from a session. |
||
| 3971 | * |
||
| 3972 | * @param int Session id |
||
| 3973 | * |
||
| 3974 | * @return array Courses list |
||
| 3975 | */ |
||
| 3976 | public static function get_courses_list_from_session($sessionId) |
||
| 3977 | { |
||
| 3978 | $sessionId = (int) $sessionId; |
||
| 3979 | |||
| 3980 | // table definition |
||
| 3981 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3982 | $courseTable = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3983 | |||
| 3984 | $sql = "SELECT DISTINCT code, c_id |
||
| 3985 | FROM $tbl_session_course sc |
||
| 3986 | INNER JOIN $courseTable c |
||
| 3987 | ON sc.c_id = c.id |
||
| 3988 | WHERE session_id= $sessionId"; |
||
| 3989 | |||
| 3990 | $result = Database::query($sql); |
||
| 3991 | |||
| 3992 | $courses = []; |
||
| 3993 | while ($row = Database::fetch_array($result)) { |
||
| 3994 | $courses[$row['code']] = $row; |
||
| 3995 | } |
||
| 3996 | |||
| 3997 | return $courses; |
||
| 3998 | } |
||
| 3999 | |||
| 4000 | /** |
||
| 4001 | * Count the number of documents that an user has uploaded to a course. |
||
| 4002 | * |
||
| 4003 | * @param int|array Student id(s) |
||
| 4004 | * @param string Course code |
||
| 4005 | * @param int Session id (optional), |
||
| 4006 | * if param $sessionId is null(default) |
||
| 4007 | * return count of assignments including sessions, 0 = session is not filtered |
||
| 4008 | * |
||
| 4009 | * @return int Number of documents |
||
| 4010 | */ |
||
| 4011 | public static function count_student_uploaded_documents( |
||
| 4012 | $student_id, |
||
| 4013 | $course_code, |
||
| 4014 | $sessionId = null |
||
| 4015 | ) { |
||
| 4016 | $a_course = api_get_course_info($course_code); |
||
| 4017 | $repo = Container::getDocumentRepository(); |
||
| 4018 | |||
| 4019 | $user = api_get_user_entity($student_id); |
||
| 4020 | $course = api_get_course_entity($a_course['real_id']); |
||
| 4021 | $session = api_get_session_entity($sessionId); |
||
| 4022 | //$group = api_get_group_entity(api_get_group_id()); |
||
| 4023 | |||
| 4024 | $qb = $repo->getResourcesByCourseLinkedToUser($user, $course, $session); |
||
| 4025 | |||
| 4026 | $qb->select('count(resource)'); |
||
| 4027 | $count = $qb->getQuery()->getSingleScalarResult(); |
||
| 4028 | |||
| 4029 | return $count; |
||
| 4030 | } |
||
| 4031 | |||
| 4032 | /** |
||
| 4033 | * This function counts the number of post by course. |
||
| 4034 | * |
||
| 4035 | * @param string $courseId |
||
| 4036 | * @param int $sessionId (optional), if is null(default) it'll return results including sessions, |
||
| 4037 | * 0 = session is not filtered |
||
| 4038 | * @param int $groupId |
||
| 4039 | * |
||
| 4040 | * @return int The number of post by course |
||
| 4041 | */ |
||
| 4042 | public static function count_number_of_posts_by_course($courseId, $sessionId = null, $groupId = 0) |
||
| 4043 | { |
||
| 4044 | $repo = Container::getForumPostRepository(); |
||
| 4045 | $course = api_get_course_entity($courseId); |
||
| 4046 | $session = api_get_session_entity($sessionId); |
||
| 4047 | $group = api_get_group_entity($groupId); |
||
| 4048 | $qb = $repo->getResourcesByCourse($course, $session, $group); |
||
| 4049 | |||
| 4050 | $qb->select('count(resource)'); |
||
| 4051 | $count = $qb->getQuery()->getSingleScalarResult(); |
||
| 4052 | |||
| 4053 | return $count; |
||
| 4054 | } |
||
| 4055 | |||
| 4056 | /** |
||
| 4057 | * This function counts the number of threads by course. |
||
| 4058 | * |
||
| 4059 | * @param int Course id |
||
| 4060 | * @param int Session id (optional), |
||
| 4061 | * if param $sessionId is null(default) it'll return results including |
||
| 4062 | * sessions, 0 = session is not filtered |
||
| 4063 | * @param int $groupId |
||
| 4064 | * |
||
| 4065 | * @return int The number of threads by course |
||
| 4066 | */ |
||
| 4067 | public static function count_number_of_threads_by_course( |
||
| 4068 | $courseId, |
||
| 4069 | $sessionId = null, |
||
| 4070 | $groupId = 0 |
||
| 4071 | ) { |
||
| 4072 | $repo = Container::getForumThreadRepository(); |
||
| 4073 | $course = api_get_course_entity($courseId); |
||
| 4074 | $session = api_get_session_entity($sessionId); |
||
| 4075 | $group = api_get_group_entity($groupId); |
||
| 4076 | $qb = $repo->getResourcesByCourse($course, $session, $group); |
||
| 4077 | |||
| 4078 | $qb->select('count(resource)'); |
||
| 4079 | $count = $qb->getQuery()->getSingleScalarResult(); |
||
| 4080 | |||
| 4081 | return $count; |
||
| 4082 | } |
||
| 4083 | |||
| 4084 | /** |
||
| 4085 | * This function counts the number of forums by course. |
||
| 4086 | * |
||
| 4087 | * @param int Course id |
||
| 4088 | * @param int Session id (optional), |
||
| 4089 | * if param $sessionId is null(default) it'll return results |
||
| 4090 | * including sessions, 0 = session is not filtered |
||
| 4091 | * @param int $groupId |
||
| 4092 | * |
||
| 4093 | * @return int The number of forums by course |
||
| 4094 | */ |
||
| 4095 | public static function count_number_of_forums_by_course( |
||
| 4096 | $courseId, |
||
| 4097 | $sessionId = null, |
||
| 4098 | $groupId = 0 |
||
| 4099 | ) { |
||
| 4100 | $repo = Container::getForumRepository(); |
||
| 4101 | $course = api_get_course_entity($courseId); |
||
| 4102 | $session = api_get_session_entity($sessionId); |
||
| 4103 | $group = api_get_group_entity($groupId); |
||
| 4104 | |||
| 4105 | $qb = $repo->getResourcesByCourse($course, $session, $group); |
||
| 4106 | $qb->select('count(resource)'); |
||
| 4107 | $count = $qb->getQuery()->getSingleScalarResult(); |
||
| 4108 | |||
| 4109 | return $count; |
||
| 4110 | } |
||
| 4111 | |||
| 4112 | /** |
||
| 4113 | * This function counts the chat last connections by course in x days. |
||
| 4114 | * |
||
| 4115 | * @param string Course code |
||
| 4116 | * @param int Last x days |
||
| 4117 | * @param int Session id (optional) |
||
| 4118 | * |
||
| 4119 | * @return int Chat last connections by course in x days |
||
| 4120 | */ |
||
| 4121 | public static function chat_connections_during_last_x_days_by_course( |
||
| 4122 | $course_code, |
||
| 4123 | $last_days, |
||
| 4124 | $session_id = 0 |
||
| 4125 | ) { |
||
| 4126 | $course_info = api_get_course_info($course_code); |
||
| 4127 | if (empty($course_info)) { |
||
| 4128 | return null; |
||
| 4129 | } |
||
| 4130 | $courseId = $course_info['real_id']; |
||
| 4131 | |||
| 4132 | // Protect data |
||
| 4133 | $last_days = (int) $last_days; |
||
| 4134 | $session_id = (int) $session_id; |
||
| 4135 | |||
| 4136 | $tbl_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS); |
||
| 4137 | $now = api_get_utc_datetime(); |
||
| 4138 | |||
| 4139 | $sql = "SELECT count(*) FROM $tbl_stats_access |
||
| 4140 | WHERE |
||
| 4141 | DATE_SUB('$now',INTERVAL $last_days DAY) <= access_date AND |
||
| 4142 | c_id = '$courseId' AND |
||
| 4143 | access_tool='".TOOL_CHAT."' AND |
||
| 4144 | session_id = '$session_id' "; |
||
| 4145 | $result = Database::query($sql); |
||
| 4146 | if (Database::num_rows($result)) { |
||
| 4147 | $row = Database::fetch_row($result); |
||
| 4148 | $count = $row[0]; |
||
| 4149 | |||
| 4150 | return $count; |
||
| 4151 | } |
||
| 4152 | |||
| 4153 | return 0; |
||
| 4154 | } |
||
| 4155 | |||
| 4156 | /** |
||
| 4157 | * This function gets the last student's connection in chat. |
||
| 4158 | * |
||
| 4159 | * @param int Student id |
||
| 4160 | * @param string Course code |
||
| 4161 | * @param int Session id (optional) |
||
| 4162 | * |
||
| 4163 | * @return string datetime formatted without day (e.g: February 23, 2010 10:20:50 ) |
||
| 4164 | */ |
||
| 4165 | public static function chat_last_connection( |
||
| 4166 | $student_id, |
||
| 4167 | $courseId, |
||
| 4168 | $session_id = 0 |
||
| 4169 | ) { |
||
| 4170 | $student_id = (int) $student_id; |
||
| 4171 | $courseId = (int) $courseId; |
||
| 4172 | $session_id = (int) $session_id; |
||
| 4173 | $date_time = ''; |
||
| 4174 | |||
| 4175 | // table definition |
||
| 4176 | $tbl_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); |
||
| 4177 | $sql = "SELECT access_date |
||
| 4178 | FROM $tbl_stats_access |
||
| 4179 | WHERE |
||
| 4180 | access_tool='".TOOL_CHAT."' AND |
||
| 4181 | access_user_id='$student_id' AND |
||
| 4182 | c_id = $courseId AND |
||
| 4183 | session_id = '$session_id' |
||
| 4184 | ORDER BY access_date DESC limit 1"; |
||
| 4185 | $rs = Database::query($sql); |
||
| 4186 | if (Database::num_rows($rs) > 0) { |
||
| 4187 | $row = Database::fetch_array($rs); |
||
| 4188 | $date_time = api_convert_and_format_date( |
||
| 4189 | $row['access_date'], |
||
| 4190 | null, |
||
| 4191 | date_default_timezone_get() |
||
| 4192 | ); |
||
| 4193 | } |
||
| 4194 | |||
| 4195 | return $date_time; |
||
| 4196 | } |
||
| 4197 | |||
| 4198 | /** |
||
| 4199 | * Get count student's visited links. |
||
| 4200 | * |
||
| 4201 | * @param int $student_id Student id |
||
| 4202 | * @param int $courseId |
||
| 4203 | * @param int $session_id Session id (optional) |
||
| 4204 | * |
||
| 4205 | * @return int count of visited links |
||
| 4206 | */ |
||
| 4207 | public static function count_student_visited_links($student_id, $courseId, $session_id = 0) |
||
| 4208 | { |
||
| 4209 | $student_id = (int) $student_id; |
||
| 4210 | $courseId = (int) $courseId; |
||
| 4211 | $session_id = (int) $session_id; |
||
| 4212 | |||
| 4213 | // table definition |
||
| 4214 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS); |
||
| 4215 | |||
| 4216 | $sql = 'SELECT 1 |
||
| 4217 | FROM '.$table.' |
||
| 4218 | WHERE |
||
| 4219 | links_user_id= '.$student_id.' AND |
||
| 4220 | c_id = "'.$courseId.'" AND |
||
| 4221 | session_id = '.$session_id.' '; |
||
| 4222 | |||
| 4223 | $rs = Database::query($sql); |
||
| 4224 | |||
| 4225 | return Database::num_rows($rs); |
||
| 4226 | } |
||
| 4227 | |||
| 4228 | public static function countStudentDownloadedDocuments(int $studentId, int $courseId, int $sessionId = 0): int |
||
| 4229 | { |
||
| 4230 | $em = Database::getManager(); |
||
| 4231 | $qb = $em->createQueryBuilder(); |
||
| 4232 | |||
| 4233 | $qb->select('COUNT(td.downId)') |
||
| 4234 | ->from(TrackEDownloads::class, 'td') |
||
| 4235 | ->leftJoin('td.resourceLink', 'rl') |
||
| 4236 | ->where('td.downUserId = :studentId') |
||
| 4237 | ->andWhere('rl.course = :courseId') |
||
| 4238 | ->setParameter('studentId', $studentId) |
||
| 4239 | ->setParameter('courseId', $courseId); |
||
| 4240 | |||
| 4241 | if ($sessionId > 0) { |
||
| 4242 | $qb->andWhere('rl.session = :sessionId') |
||
| 4243 | ->setParameter('sessionId', $sessionId); |
||
| 4244 | } |
||
| 4245 | |||
| 4246 | $query = $qb->getQuery(); |
||
| 4247 | |||
| 4248 | return (int) $query->getSingleScalarResult(); |
||
| 4249 | } |
||
| 4250 | |||
| 4251 | /** |
||
| 4252 | * Get course list inside a session from a student. |
||
| 4253 | * |
||
| 4254 | * @param int $user_id Student id |
||
| 4255 | * @param int $sessionId Session id (optional) |
||
| 4256 | * |
||
| 4257 | * @return array Courses list |
||
| 4258 | */ |
||
| 4259 | public static function get_course_list_in_session_from_student($user_id, $sessionId = 0) |
||
| 4260 | { |
||
| 4261 | $user_id = (int) $user_id; |
||
| 4262 | $sessionId = (int) $sessionId; |
||
| 4263 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4264 | $courseTable = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 4265 | |||
| 4266 | $sql = "SELECT c.code |
||
| 4267 | FROM $tbl_session_course_user sc |
||
| 4268 | INNER JOIN $courseTable c |
||
| 4269 | WHERE |
||
| 4270 | user_id= $user_id AND |
||
| 4271 | session_id = $sessionId"; |
||
| 4272 | $result = Database::query($sql); |
||
| 4273 | $courses = []; |
||
| 4274 | while ($row = Database::fetch_array($result)) { |
||
| 4275 | $courses[$row['code']] = $row['code']; |
||
| 4276 | } |
||
| 4277 | |||
| 4278 | return $courses; |
||
| 4279 | } |
||
| 4280 | |||
| 4281 | /** |
||
| 4282 | * Get inactive students in course. |
||
| 4283 | * |
||
| 4284 | * @param int $courseId |
||
| 4285 | * @param string|int $since Since login course date (optional, default = 'never') |
||
| 4286 | * @param int $session_id (optional) |
||
| 4287 | * |
||
| 4288 | * @return array Inactive users |
||
| 4289 | */ |
||
| 4290 | public static function getInactiveStudentsInCourse( |
||
| 4291 | $courseId, |
||
| 4292 | $since = 'never', |
||
| 4293 | $session_id = 0 |
||
| 4294 | ) { |
||
| 4295 | $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 4296 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4297 | $table_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 4298 | $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 4299 | $now = api_get_utc_datetime(); |
||
| 4300 | $courseId = (int) $courseId; |
||
| 4301 | $session_id = (int) $session_id; |
||
| 4302 | |||
| 4303 | if (empty($courseId)) { |
||
| 4304 | return false; |
||
| 4305 | } |
||
| 4306 | |||
| 4307 | if ('never' === $since) { |
||
| 4308 | if (empty($session_id)) { |
||
| 4309 | $sql = 'SELECT course_user.user_id |
||
| 4310 | FROM '.$table_course_rel_user.' course_user |
||
| 4311 | LEFT JOIN '.$tbl_track_login.' stats_login |
||
| 4312 | ON course_user.user_id = stats_login.user_id AND |
||
| 4313 | relation_type<>'.COURSE_RELATION_TYPE_RRHH.' |
||
| 4314 | INNER JOIN '.$tableCourse.' c |
||
| 4315 | ON (c.id = course_user.c_id) |
||
| 4316 | WHERE |
||
| 4317 | course_user.c_id = '.$courseId.' AND |
||
| 4318 | stats_login.login_course_date IS NULL |
||
| 4319 | GROUP BY course_user.user_id'; |
||
| 4320 | } else { |
||
| 4321 | $sql = 'SELECT session_course_user.user_id |
||
| 4322 | FROM '.$tbl_session_course_user.' session_course_user |
||
| 4323 | LEFT JOIN '.$tbl_track_login.' stats_login |
||
| 4324 | ON session_course_user.user_id = stats_login.user_id |
||
| 4325 | INNER JOIN '.$tableCourse.' c |
||
| 4326 | ON (c.id = session_course_user.c_id) |
||
| 4327 | WHERE |
||
| 4328 | session_course_user.c_id = '.$courseId.' AND |
||
| 4329 | stats_login.login_course_date IS NULL |
||
| 4330 | GROUP BY session_course_user.user_id'; |
||
| 4331 | } |
||
| 4332 | } else { |
||
| 4333 | $since = (int) $since; |
||
| 4334 | if (empty($session_id)) { |
||
| 4335 | $inner = 'INNER JOIN '.$table_course_rel_user.' course_user |
||
| 4336 | ON course_user.user_id = stats_login.user_id AND course_user.c_id = c.id '; |
||
| 4337 | } else { |
||
| 4338 | $inner = 'INNER JOIN '.$tbl_session_course_user.' session_course_user |
||
| 4339 | ON |
||
| 4340 | c.id = session_course_user.c_id AND |
||
| 4341 | session_course_user.session_id = '.$session_id.' AND |
||
| 4342 | session_course_user.user_id = stats_login.user_id '; |
||
| 4343 | } |
||
| 4344 | |||
| 4345 | $sql = 'SELECT |
||
| 4346 | stats_login.user_id, |
||
| 4347 | MAX(login_course_date) max_date |
||
| 4348 | FROM '.$tbl_track_login.' stats_login |
||
| 4349 | INNER JOIN '.$tableCourse.' c |
||
| 4350 | ON (c.id = stats_login.c_id) |
||
| 4351 | '.$inner.' |
||
| 4352 | WHERE c.id = '.$courseId.' |
||
| 4353 | GROUP BY stats_login.user_id |
||
| 4354 | HAVING DATE_SUB("'.$now.'", INTERVAL '.$since.' DAY) > max_date '; |
||
| 4355 | } |
||
| 4356 | |||
| 4357 | $rs = Database::query($sql); |
||
| 4358 | |||
| 4359 | $allow = 'true' === api_get_plugin_setting('pausetraining', 'tool_enable'); |
||
| 4360 | $allowPauseFormation = 'true' === api_get_plugin_setting('pausetraining', 'allow_users_to_edit_pause_formation'); |
||
| 4361 | |||
| 4362 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 4363 | $users = []; |
||
| 4364 | while ($user = Database::fetch_array($rs)) { |
||
| 4365 | $userId = $user['user_id']; |
||
| 4366 | |||
| 4367 | if ($allow && $allowPauseFormation) { |
||
| 4368 | $pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation'); |
||
| 4369 | if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) { |
||
| 4370 | // Skip user because he paused his formation. |
||
| 4371 | continue; |
||
| 4372 | } |
||
| 4373 | } |
||
| 4374 | |||
| 4375 | $users[] = $userId; |
||
| 4376 | } |
||
| 4377 | |||
| 4378 | return $users; |
||
| 4379 | } |
||
| 4380 | |||
| 4381 | /** |
||
| 4382 | * get count clicks about tools most used by course. |
||
| 4383 | * |
||
| 4384 | * @param int $courseId |
||
| 4385 | * @param int Session id (optional), |
||
| 4386 | * if param $session_id is null(default) it'll return results |
||
| 4387 | * including sessions, 0 = session is not filtered |
||
| 4388 | * |
||
| 4389 | * @return array tools data |
||
| 4390 | */ |
||
| 4391 | public static function get_tools_most_used_by_course($courseId, $session_id = null) |
||
| 4392 | { |
||
| 4393 | $courseId = (int) $courseId; |
||
| 4394 | $data = []; |
||
| 4395 | $TABLETRACK_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); |
||
| 4396 | $condition_session = ''; |
||
| 4397 | if (isset($session_id)) { |
||
| 4398 | $session_id = (int) $session_id; |
||
| 4399 | $condition_session = ' AND session_id = '.$session_id; |
||
| 4400 | } |
||
| 4401 | $sql = "SELECT |
||
| 4402 | access_tool, |
||
| 4403 | COUNT(DISTINCT access_user_id), |
||
| 4404 | count(access_tool) as count_access_tool |
||
| 4405 | FROM $TABLETRACK_ACCESS |
||
| 4406 | WHERE |
||
| 4407 | access_tool IS NOT NULL AND |
||
| 4408 | access_tool != '' AND |
||
| 4409 | c_id = '$courseId' |
||
| 4410 | $condition_session |
||
| 4411 | GROUP BY access_tool |
||
| 4412 | ORDER BY count_access_tool DESC |
||
| 4413 | LIMIT 0, 3"; |
||
| 4414 | $rs = Database::query($sql); |
||
| 4415 | if (Database::num_rows($rs) > 0) { |
||
| 4416 | while ($row = Database::fetch_array($rs)) { |
||
| 4417 | $data[] = $row; |
||
| 4418 | } |
||
| 4419 | } |
||
| 4420 | |||
| 4421 | return $data; |
||
| 4422 | } |
||
| 4423 | |||
| 4424 | /** |
||
| 4425 | * get documents most downloaded by course. |
||
| 4426 | * |
||
| 4427 | * @param string Course code |
||
| 4428 | * @param int Session id (optional), |
||
| 4429 | * if param $session_id is null(default) it'll return results including |
||
| 4430 | * sessions, 0 = session is not filtered |
||
| 4431 | * @param int Limit (optional, default = 0, 0 = without limit) |
||
| 4432 | * |
||
| 4433 | * @return array documents downloaded |
||
| 4434 | */ |
||
| 4435 | public static function get_documents_most_downloaded_by_course( |
||
| 4436 | $course_code, |
||
| 4437 | $session_id = 0, |
||
| 4438 | $limit = 0 |
||
| 4439 | ) { |
||
| 4440 | $courseId = api_get_course_int_id($course_code); |
||
| 4441 | $data = []; |
||
| 4442 | |||
| 4443 | $TABLETRACK_DOWNLOADS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS); |
||
| 4444 | $tableResourceLink = Database::get_main_table('resource_link'); |
||
| 4445 | $tableResourceNode = Database::get_main_table('resource_node'); |
||
| 4446 | $condition_session = ''; |
||
| 4447 | $session_id = intval($session_id); |
||
| 4448 | if (!empty($session_id)) { |
||
| 4449 | $condition_session = ' AND l.session_id = '.$session_id; |
||
| 4450 | } |
||
| 4451 | $sql = "SELECT t.resource_link_id as lid, |
||
| 4452 | n.path as npath, |
||
| 4453 | n.title as ntitle, |
||
| 4454 | n.uuid as uuid, |
||
| 4455 | n.id as nid, |
||
| 4456 | COUNT(t.down_id) as count_down |
||
| 4457 | FROM $TABLETRACK_DOWNLOADS t |
||
| 4458 | INNER JOIN $tableResourceLink l |
||
| 4459 | ON t.resource_link_id = l.id |
||
| 4460 | INNER JOIN $tableResourceNode n |
||
| 4461 | ON l.resource_node_id = n.id |
||
| 4462 | WHERE l.c_id = $courseId |
||
| 4463 | $condition_session |
||
| 4464 | GROUP BY nid |
||
| 4465 | ORDER BY count_down DESC |
||
| 4466 | LIMIT 0, $limit"; |
||
| 4467 | $rs = Database::query($sql); |
||
| 4468 | |||
| 4469 | if (Database::num_rows($rs) > 0) { |
||
| 4470 | while ($row = Database::fetch_array($rs)) { |
||
| 4471 | $data[] = $row; |
||
| 4472 | } |
||
| 4473 | } |
||
| 4474 | |||
| 4475 | return $data; |
||
| 4476 | } |
||
| 4477 | |||
| 4478 | /** |
||
| 4479 | * get links most visited by course. |
||
| 4480 | * |
||
| 4481 | * @param string Course code |
||
| 4482 | * @param int Session id (optional), |
||
| 4483 | * if param $session_id is null(default) it'll |
||
| 4484 | * return results including sessions, 0 = session is not filtered |
||
| 4485 | * |
||
| 4486 | * @return array links most visited |
||
| 4487 | */ |
||
| 4488 | public static function get_links_most_visited_by_course($course_code, $session_id = null) |
||
| 4489 | { |
||
| 4490 | $course_code = Database::escape_string($course_code); |
||
| 4491 | $course_info = api_get_course_info($course_code); |
||
| 4492 | $courseId = $course_info['real_id']; |
||
| 4493 | $data = []; |
||
| 4494 | |||
| 4495 | $TABLETRACK_LINKS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LINKS); |
||
| 4496 | $TABLECOURSE_LINKS = Database::get_course_table(TABLE_LINK); |
||
| 4497 | |||
| 4498 | $condition_session = ''; |
||
| 4499 | if (isset($session_id)) { |
||
| 4500 | $session_id = intval($session_id); |
||
| 4501 | $condition_session = ' AND sl.session_id = '.$session_id; |
||
| 4502 | } |
||
| 4503 | |||
| 4504 | $sql = "SELECT cl.title, cl.url,count(DISTINCT sl.links_user_id), count(cl.title) as count_visits |
||
| 4505 | FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl |
||
| 4506 | WHERE |
||
| 4507 | sl.links_link_id = cl.iid AND |
||
| 4508 | sl.c_id = $courseId |
||
| 4509 | $condition_session |
||
| 4510 | GROUP BY cl.title, cl.url |
||
| 4511 | ORDER BY count_visits DESC |
||
| 4512 | LIMIT 0, 3"; |
||
| 4513 | $rs = Database::query($sql); |
||
| 4514 | if (Database::num_rows($rs) > 0) { |
||
| 4515 | while ($row = Database::fetch_array($rs)) { |
||
| 4516 | $data[] = $row; |
||
| 4517 | } |
||
| 4518 | } |
||
| 4519 | |||
| 4520 | return $data; |
||
| 4521 | } |
||
| 4522 | |||
| 4523 | /** |
||
| 4524 | * Shows the user progress (when clicking in the Progress tab). |
||
| 4525 | * |
||
| 4526 | * @param int $user_id |
||
| 4527 | * @param int $session_id |
||
| 4528 | * @param string $extra_params |
||
| 4529 | * @param bool $show_courses |
||
| 4530 | * @param bool $showAllSessions |
||
| 4531 | * @param bool $returnArray |
||
| 4532 | * |
||
| 4533 | * @return string|array |
||
| 4534 | */ |
||
| 4535 | public static function show_user_progress( |
||
| 4536 | $user_id, |
||
| 4537 | $session_id = 0, |
||
| 4538 | $extra_params = '', |
||
| 4539 | $show_courses = true, |
||
| 4540 | $showAllSessions = true, |
||
| 4541 | $returnArray = false |
||
| 4542 | ) { |
||
| 4543 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 4544 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 4545 | $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 4546 | $tbl_access_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 4547 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4548 | $tbl_access_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 4549 | |||
| 4550 | $trackingColumns = [ |
||
| 4551 | 'course_session' => [ |
||
| 4552 | 'course_title' => true, |
||
| 4553 | 'published_exercises' => true, |
||
| 4554 | 'new_exercises' => true, |
||
| 4555 | 'my_average' => true, |
||
| 4556 | 'average_exercise_result' => true, |
||
| 4557 | 'time_spent' => true, |
||
| 4558 | 'lp_progress' => true, |
||
| 4559 | 'score' => true, |
||
| 4560 | 'best_score' => true, |
||
| 4561 | 'last_connection' => true, |
||
| 4562 | 'details' => true, |
||
| 4563 | ], |
||
| 4564 | ]; |
||
| 4565 | |||
| 4566 | $trackingColumnsConfig = api_get_setting('session.tracking_columns', true); |
||
| 4567 | if (!empty($trackingColumnsConfig)) { |
||
| 4568 | $trackingColumns = $trackingColumnsConfig; |
||
| 4569 | } |
||
| 4570 | |||
| 4571 | $user_id = (int) $user_id; |
||
| 4572 | $session_id = (int) $session_id; |
||
| 4573 | $urlId = api_get_current_access_url_id(); |
||
| 4574 | |||
| 4575 | $sql = "SELECT c.id, c.code, title |
||
| 4576 | FROM $tbl_course_user cu |
||
| 4577 | INNER JOIN $tbl_course c |
||
| 4578 | ON (cu.c_id = c.id) |
||
| 4579 | INNER JOIN $tbl_access_rel_course a |
||
| 4580 | ON (a.c_id = c.id) |
||
| 4581 | WHERE |
||
| 4582 | cu.user_id = $user_id AND |
||
| 4583 | relation_type<> ".COURSE_RELATION_TYPE_RRHH." AND |
||
| 4584 | access_url_id = $urlId |
||
| 4585 | ORDER BY title"; |
||
| 4586 | |||
| 4587 | $rs = Database::query($sql); |
||
| 4588 | $courses = $course_in_session = $temp_course_in_session = []; |
||
| 4589 | $courseIdList = []; |
||
| 4590 | while ($row = Database::fetch_assoc($rs)) { |
||
| 4591 | $courses[$row['id']] = $row['title']; |
||
| 4592 | $courseIdList[] = $row['id']; |
||
| 4593 | } |
||
| 4594 | |||
| 4595 | $orderBy = ' ORDER BY title '; |
||
| 4596 | $extraInnerJoin = null; |
||
| 4597 | |||
| 4598 | if (SessionManager::orderCourseIsEnabled() && !empty($session_id)) { |
||
| 4599 | $orderBy = ' ORDER BY s.id, src.position '; |
||
| 4600 | $tableSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 4601 | $extraInnerJoin = " INNER JOIN $tableSessionRelCourse src |
||
| 4602 | ON (cu.c_id = src.c_id AND src.session_id = $session_id) "; |
||
| 4603 | } |
||
| 4604 | |||
| 4605 | $sessionCondition = ''; |
||
| 4606 | if (!empty($session_id)) { |
||
| 4607 | $sessionCondition = " AND s.id = $session_id"; |
||
| 4608 | } |
||
| 4609 | |||
| 4610 | // Get the list of sessions where the user is subscribed as student |
||
| 4611 | $sql = "SELECT DISTINCT c.code, s.id as session_id, s.title |
||
| 4612 | FROM $tbl_session_course_user cu |
||
| 4613 | INNER JOIN $tbl_access_rel_session a |
||
| 4614 | ON (a.session_id = cu.session_id) |
||
| 4615 | INNER JOIN $tbl_session s |
||
| 4616 | ON (s.id = a.session_id) |
||
| 4617 | INNER JOIN $tbl_course c |
||
| 4618 | ON (c.id = cu.c_id) |
||
| 4619 | $extraInnerJoin |
||
| 4620 | WHERE |
||
| 4621 | cu.user_id = $user_id AND |
||
| 4622 | access_url_id = ".$urlId." |
||
| 4623 | $sessionCondition |
||
| 4624 | $orderBy "; |
||
| 4625 | |||
| 4626 | $rs = Database::query($sql); |
||
| 4627 | $simple_session_array = []; |
||
| 4628 | while ($row = Database::fetch_assoc($rs)) { |
||
| 4629 | $course_info = api_get_course_info($row['code']); |
||
| 4630 | $temp_course_in_session[$row['session_id']]['course_list'][$course_info['real_id']] = $course_info; |
||
| 4631 | $temp_course_in_session[$row['session_id']]['title'] = $row['title']; |
||
| 4632 | $simple_session_array[$row['session_id']] = $row['title']; |
||
| 4633 | } |
||
| 4634 | |||
| 4635 | foreach ($simple_session_array as $my_session_id => $session_title) { |
||
| 4636 | $course_list = $temp_course_in_session[$my_session_id]['course_list']; |
||
| 4637 | $my_course_data = []; |
||
| 4638 | foreach ($course_list as $courseId => $course_data) { |
||
| 4639 | $my_course_data[$courseId] = $course_data['title']; |
||
| 4640 | } |
||
| 4641 | |||
| 4642 | if (empty($session_id)) { |
||
| 4643 | $my_course_data = utf8_sort($my_course_data); |
||
| 4644 | } |
||
| 4645 | |||
| 4646 | $final_course_data = []; |
||
| 4647 | foreach ($my_course_data as $course_id => $value) { |
||
| 4648 | if (isset($course_list[$course_id])) { |
||
| 4649 | $final_course_data[$course_id] = $course_list[$course_id]; |
||
| 4650 | } |
||
| 4651 | } |
||
| 4652 | $course_in_session[$my_session_id]['course_list'] = $final_course_data; |
||
| 4653 | $course_in_session[$my_session_id]['title'] = $session_title; |
||
| 4654 | } |
||
| 4655 | |||
| 4656 | if ($returnArray) { |
||
| 4657 | $course_in_session[0] = $courseIdList; |
||
| 4658 | |||
| 4659 | return $course_in_session; |
||
| 4660 | } |
||
| 4661 | |||
| 4662 | $html = ''; |
||
| 4663 | // Course list |
||
| 4664 | if ($show_courses) { |
||
| 4665 | if (!empty($courses)) { |
||
| 4666 | $html .= Display::page_subheader( |
||
| 4667 | Display::getMdiIcon( |
||
| 4668 | 'book-open-page-variant', |
||
| 4669 | 'ch-tool-icon', |
||
| 4670 | null, |
||
| 4671 | ICON_SIZE_SMALL, |
||
| 4672 | get_lang('My courses') |
||
| 4673 | ).' '.get_lang('My courses') |
||
| 4674 | ); |
||
| 4675 | |||
| 4676 | $columns = [ |
||
| 4677 | 'course_title' => get_lang('Course'), |
||
| 4678 | 'time_spent' => get_lang('Time spent in the course'), |
||
| 4679 | 'progress' => get_lang('Progress'), |
||
| 4680 | 'best_score_in_lp' => get_lang('Best score in learning path'), |
||
| 4681 | 'best_score_not_in_lp' => get_lang('Best score not in learning path'), |
||
| 4682 | 'latest_login' => get_lang('Latest login'), |
||
| 4683 | 'details' => get_lang('Details'), |
||
| 4684 | ]; |
||
| 4685 | $availableColumns = []; |
||
| 4686 | if (isset($trackingColumns['my_progress_courses'])) { |
||
| 4687 | $availableColumns = $trackingColumns['my_progress_courses']; |
||
| 4688 | } |
||
| 4689 | $html .= '<div class="table-responsive">'; |
||
| 4690 | $html .= '<table class="table table-striped table-hover">'; |
||
| 4691 | $html .= '<thead><tr>'; |
||
| 4692 | foreach ($columns as $columnKey => $name) { |
||
| 4693 | if (!empty($availableColumns)) { |
||
| 4694 | if (isset($availableColumns[$columnKey]) && false == $availableColumns[$columnKey]) { |
||
| 4695 | continue; |
||
| 4696 | } |
||
| 4697 | } |
||
| 4698 | $html .= Display::tag('th', $name); |
||
| 4699 | } |
||
| 4700 | $html .= '</tr></thead><tbody>'; |
||
| 4701 | |||
| 4702 | foreach ($courses as $courseId => $course_title) { |
||
| 4703 | $course = api_get_course_entity($courseId); |
||
| 4704 | $courseCode = $course->getCode(); |
||
| 4705 | |||
| 4706 | $total_time_login = self::get_time_spent_on_the_course( |
||
| 4707 | $user_id, |
||
| 4708 | $courseId |
||
| 4709 | ); |
||
| 4710 | $time = api_time_to_hms($total_time_login); |
||
| 4711 | $progress = self::get_avg_student_progress( |
||
| 4712 | $user_id, |
||
| 4713 | $course |
||
| 4714 | ); |
||
| 4715 | $bestScore = self::get_avg_student_score( |
||
| 4716 | $user_id, |
||
| 4717 | $course, |
||
| 4718 | [], |
||
| 4719 | null, |
||
| 4720 | false, |
||
| 4721 | false, |
||
| 4722 | true |
||
| 4723 | ); |
||
| 4724 | |||
| 4725 | /*$exerciseList = ExerciseLib::get_all_exercises( |
||
| 4726 | $courseInfo, |
||
| 4727 | 0, |
||
| 4728 | false, |
||
| 4729 | null, |
||
| 4730 | false, |
||
| 4731 | 1 |
||
| 4732 | );*/ |
||
| 4733 | |||
| 4734 | $qb = Container::getQuizRepository()->findAllByCourse($course, null, null, 1, false); |
||
| 4735 | /** @var CQuiz[] $exercises */ |
||
| 4736 | $exercises = $qb->getQuery()->getResult(); |
||
| 4737 | |||
| 4738 | $bestScoreAverageNotInLP = 0; |
||
| 4739 | if (!empty($exercises)) { |
||
| 4740 | foreach ($exercises as $exerciseData) { |
||
| 4741 | $results = Event::get_best_exercise_results_by_user( |
||
| 4742 | $exerciseData->getIid(), |
||
| 4743 | $courseId, |
||
| 4744 | 0, |
||
| 4745 | $user_id |
||
| 4746 | ); |
||
| 4747 | $best = 0; |
||
| 4748 | if (!empty($results)) { |
||
| 4749 | foreach ($results as $result) { |
||
| 4750 | if (!empty($result['max_score'])) { |
||
| 4751 | $score = $result['score'] / $result['max_score']; |
||
| 4752 | if ($score > $best) { |
||
| 4753 | $best = $score; |
||
| 4754 | } |
||
| 4755 | } |
||
| 4756 | } |
||
| 4757 | } |
||
| 4758 | $bestScoreAverageNotInLP += $best; |
||
| 4759 | } |
||
| 4760 | $bestScoreAverageNotInLP = round($bestScoreAverageNotInLP / count($exercises) * 100, 2); |
||
| 4761 | } |
||
| 4762 | |||
| 4763 | $last_connection = self::get_last_connection_date_on_the_course( |
||
| 4764 | $user_id, |
||
| 4765 | ['real_id' => $courseId] |
||
| 4766 | ); |
||
| 4767 | |||
| 4768 | if (is_null($progress) || empty($progress)) { |
||
| 4769 | $progress = '0%'; |
||
| 4770 | } else { |
||
| 4771 | $progress = $progress.'%'; |
||
| 4772 | } |
||
| 4773 | |||
| 4774 | if (isset($_GET['course']) && |
||
| 4775 | $courseCode == $_GET['course'] && |
||
| 4776 | empty($_GET['session_id']) |
||
| 4777 | ) { |
||
| 4778 | $html .= '<tr class="row_odd" style="background-color:#FBF09D">'; |
||
| 4779 | } else { |
||
| 4780 | $html .= '<tr class="row_even">'; |
||
| 4781 | } |
||
| 4782 | $url = api_get_course_url($courseId, $session_id); |
||
| 4783 | $course_url = Display::url($course_title, $url, ['target' => SESSION_LINK_TARGET]); |
||
| 4784 | if (empty($bestScore)) { |
||
| 4785 | $bestScoreResult = '-'; |
||
| 4786 | } else { |
||
| 4787 | $bestScoreResult = $bestScore.'%'; |
||
| 4788 | } |
||
| 4789 | if (empty($bestScoreAverageNotInLP)) { |
||
| 4790 | $bestScoreNotInLP = '-'; |
||
| 4791 | } else { |
||
| 4792 | $bestScoreNotInLP = $bestScoreAverageNotInLP.'%'; |
||
| 4793 | } |
||
| 4794 | |||
| 4795 | $detailsLink = ''; |
||
| 4796 | if (isset($_GET['course']) && |
||
| 4797 | $courseCode == $_GET['course'] && |
||
| 4798 | empty($_GET['session_id']) |
||
| 4799 | ) { |
||
| 4800 | $detailsLink .= '<a href="#course_session_header">'; |
||
| 4801 | $detailsLink .= Display::getMdiIcon( |
||
| 4802 | 'fast-forward-outline', |
||
| 4803 | 'ch-tool-icon', |
||
| 4804 | null, |
||
| 4805 | ICON_SIZE_SMALL, |
||
| 4806 | get_lang('Details') |
||
| 4807 | ); |
||
| 4808 | $detailsLink .= '</a>'; |
||
| 4809 | } else { |
||
| 4810 | $detailsLink .= '<a href="'.api_get_self().'?course='.$courseCode.$extra_params.'#course_session_header">'; |
||
| 4811 | $detailsLink .= Display::getMdiIcon( |
||
| 4812 | 'fast-forward-outline', |
||
| 4813 | 'ch-tool-icon', |
||
| 4814 | null, |
||
| 4815 | ICON_SIZE_SMALL, |
||
| 4816 | get_lang('Details') |
||
| 4817 | ); |
||
| 4818 | $detailsLink .= '</a>'; |
||
| 4819 | } |
||
| 4820 | |||
| 4821 | $result = [ |
||
| 4822 | 'course_title' => $course_url, |
||
| 4823 | 'time_spent' => $time, |
||
| 4824 | 'progress' => $progress, |
||
| 4825 | 'best_score_in_lp' => $bestScoreResult, |
||
| 4826 | 'best_score_not_in_lp' => $bestScoreNotInLP, |
||
| 4827 | 'latest_login' => $last_connection, |
||
| 4828 | 'details' => $detailsLink, |
||
| 4829 | ]; |
||
| 4830 | |||
| 4831 | foreach ($result as $columnKey => $data) { |
||
| 4832 | if (!empty($availableColumns)) { |
||
| 4833 | if (isset($availableColumns[$columnKey]) && false == $availableColumns[$columnKey]) { |
||
| 4834 | continue; |
||
| 4835 | } |
||
| 4836 | } |
||
| 4837 | $html .= '<td>'.$data.'</td>'; |
||
| 4838 | } |
||
| 4839 | |||
| 4840 | $html .= '</tr>'; |
||
| 4841 | } |
||
| 4842 | $html .= '</tbody></table>'; |
||
| 4843 | $html .= '</div>'; |
||
| 4844 | } |
||
| 4845 | } |
||
| 4846 | |||
| 4847 | // Session list |
||
| 4848 | if (!empty($course_in_session)) { |
||
| 4849 | $main_session_graph = ''; |
||
| 4850 | // Load graphics only when calling to an specific session |
||
| 4851 | $all_exercise_graph_name_list = []; |
||
| 4852 | $my_results = []; |
||
| 4853 | $all_exercise_graph_list = []; |
||
| 4854 | $all_exercise_start_time = []; |
||
| 4855 | foreach ($course_in_session as $my_session_id => $session_data) { |
||
| 4856 | $course_list = $session_data['course_list']; |
||
| 4857 | $user_count = count(SessionManager::get_users_by_session($my_session_id)); |
||
| 4858 | $exercise_graph_name_list = []; |
||
| 4859 | $exercise_graph_list = []; |
||
| 4860 | |||
| 4861 | foreach ($course_list as $course_data) { |
||
| 4862 | $course = api_get_course_entity($course_data['real_id']); |
||
| 4863 | $courseId = $course->getId(); |
||
| 4864 | /*$exercise_list = ExerciseLib::get_all_exercises( |
||
| 4865 | $course_data, |
||
| 4866 | $my_session_id, |
||
| 4867 | false, |
||
| 4868 | null, |
||
| 4869 | false, |
||
| 4870 | 1 |
||
| 4871 | );*/ |
||
| 4872 | |||
| 4873 | $qb = Container::getQuizRepository()->findAllByCourse($course, null, null, 1, false); |
||
| 4874 | /** @var CQuiz[] $exercises */ |
||
| 4875 | $exercises = $qb->getQuery()->getResult(); |
||
| 4876 | $countExercises = count($exercises); |
||
| 4877 | foreach ($exercises as $exercise_data) { |
||
| 4878 | //$exercise_obj = new Exercise($course_data['real_id']); |
||
| 4879 | //$exercise_obj->read($exercise_data['id']); |
||
| 4880 | // Exercise is not necessary to be visible to show results check the result_disable configuration instead |
||
| 4881 | //$visible_return = $exercise_obj->is_visible(); |
||
| 4882 | $disabled = $exercise_data->getResultsDisabled(); |
||
| 4883 | $exerciseId = $exercise_data->getIid(); |
||
| 4884 | if (0 == $disabled || 2 == $disabled) { |
||
| 4885 | $best_average = (int) |
||
| 4886 | ExerciseLib::get_best_average_score_by_exercise( |
||
| 4887 | $exerciseId, |
||
| 4888 | $courseId, |
||
| 4889 | $my_session_id, |
||
| 4890 | $user_count |
||
| 4891 | ) |
||
| 4892 | ; |
||
| 4893 | |||
| 4894 | $exercise_graph_list[] = $best_average; |
||
| 4895 | $all_exercise_graph_list[] = $best_average; |
||
| 4896 | |||
| 4897 | $user_result_data = ExerciseLib::get_best_attempt_by_user( |
||
| 4898 | api_get_user_id(), |
||
| 4899 | $exerciseId, |
||
| 4900 | $courseId, |
||
| 4901 | $my_session_id |
||
| 4902 | ); |
||
| 4903 | |||
| 4904 | $score = 0; |
||
| 4905 | if (!empty($user_result_data['max_score']) && 0 != intval($user_result_data['max_score'])) { |
||
| 4906 | $score = intval($user_result_data['score'] / $user_result_data['max_score'] * 100); |
||
| 4907 | } |
||
| 4908 | $start = $exercise_data->getStartTime() ? $exercise_data->getStartTime()->getTimestamp() : null; |
||
| 4909 | $time = null !== $start ? $start : 0; |
||
| 4910 | $all_exercise_start_time[] = $time; |
||
| 4911 | $my_results[] = $score; |
||
| 4912 | $exerciseTitle = $exercise_data->getTitle(); |
||
| 4913 | if ($countExercises <= 10) { |
||
| 4914 | $title = cut($course_data['title'], 30)." \n ".cut($exerciseTitle, 30); |
||
| 4915 | $exercise_graph_name_list[] = $title; |
||
| 4916 | $all_exercise_graph_name_list[] = $title; |
||
| 4917 | } else { |
||
| 4918 | // if there are more than 10 results, space becomes difficult to find, |
||
| 4919 | // so only show the title of the exercise, not the tool |
||
| 4920 | $title = cut($exerciseTitle, 30); |
||
| 4921 | $exercise_graph_name_list[] = $title; |
||
| 4922 | $all_exercise_graph_name_list[] = $title; |
||
| 4923 | } |
||
| 4924 | } |
||
| 4925 | } |
||
| 4926 | } |
||
| 4927 | } |
||
| 4928 | |||
| 4929 | // Complete graph |
||
| 4930 | if (!empty($my_results) && !empty($all_exercise_graph_list)) { |
||
| 4931 | asort($all_exercise_start_time); |
||
| 4932 | |||
| 4933 | //Fix exams order |
||
| 4934 | $final_all_exercise_graph_name_list = []; |
||
| 4935 | $my_results_final = []; |
||
| 4936 | $final_all_exercise_graph_list = []; |
||
| 4937 | |||
| 4938 | foreach ($all_exercise_start_time as $key => $time) { |
||
| 4939 | $label_time = ''; |
||
| 4940 | if (!empty($time)) { |
||
| 4941 | $label_time = date('d-m-y', $time); |
||
| 4942 | } |
||
| 4943 | $final_all_exercise_graph_name_list[] = $all_exercise_graph_name_list[$key].' '.$label_time; |
||
| 4944 | $my_results_final[] = $my_results[$key]; |
||
| 4945 | $final_all_exercise_graph_list[] = $all_exercise_graph_list[$key]; |
||
| 4946 | } |
||
| 4947 | $main_session_graph = self::generate_session_exercise_graph( |
||
| 4948 | $final_all_exercise_graph_name_list, |
||
| 4949 | $my_results_final, |
||
| 4950 | $final_all_exercise_graph_list |
||
| 4951 | ); |
||
| 4952 | } |
||
| 4953 | |||
| 4954 | $sessionIcon = Display::getMdiIcon( |
||
| 4955 | 'google-classroom', |
||
| 4956 | 'ch-tool-icon', |
||
| 4957 | null, |
||
| 4958 | ICON_SIZE_SMALL, |
||
| 4959 | get_lang('Course sessions') |
||
| 4960 | ); |
||
| 4961 | |||
| 4962 | $anchor = Display::url('', '', ['name' => 'course_session_header']); |
||
| 4963 | $html .= $anchor.Display::page_subheader( |
||
| 4964 | $sessionIcon.' '.get_lang('Course sessions') |
||
| 4965 | ); |
||
| 4966 | |||
| 4967 | $html .= '<div class="table-responsive">'; |
||
| 4968 | $html .= '<table class="table table-striped table-hover">'; |
||
| 4969 | $html .= '<thead>'; |
||
| 4970 | $html .= '<tr> |
||
| 4971 | '.Display::tag('th', get_lang('Session'), ['width' => '300px']).' |
||
| 4972 | '.Display::tag('th', get_lang('Tests available'), ['width' => '300px']).' |
||
| 4973 | '.Display::tag('th', get_lang('New exercises')).' |
||
| 4974 | '.Display::tag('th', get_lang('Average exercise result')).' |
||
| 4975 | '.Display::tag('th', get_lang('Details')).' |
||
| 4976 | </tr>'; |
||
| 4977 | $html .= '</thead>'; |
||
| 4978 | $html .= '<tbody>'; |
||
| 4979 | |||
| 4980 | $session = api_get_session_entity($my_session_id); |
||
| 4981 | |||
| 4982 | foreach ($course_in_session as $my_session_id => $session_data) { |
||
| 4983 | $course_list = $session_data['course_list']; |
||
| 4984 | $session_name = $session_data['title']; |
||
| 4985 | if (false == $showAllSessions) { |
||
| 4986 | if (isset($session_id) && !empty($session_id)) { |
||
| 4987 | if ($session_id != $my_session_id) { |
||
| 4988 | continue; |
||
| 4989 | } |
||
| 4990 | } |
||
| 4991 | } |
||
| 4992 | |||
| 4993 | $all_exercises = 0; |
||
| 4994 | $all_unanswered_exercises_by_user = 0; |
||
| 4995 | $all_average = 0; |
||
| 4996 | $stats_array = []; |
||
| 4997 | |||
| 4998 | foreach ($course_list as $course_data) { |
||
| 4999 | $courseId = $course_data['real_id']; |
||
| 5000 | $course = api_get_course_entity($courseId); |
||
| 5001 | |||
| 5002 | // All exercises in the course @todo change for a real count |
||
| 5003 | //$exercises = ExerciseLib::get_all_exercises($course_data, $my_session_id); |
||
| 5004 | |||
| 5005 | $qb = Container::getQuizRepository()->findAllByCourse($course, $session, null, 2); |
||
| 5006 | |||
| 5007 | /** @var CQuiz[] $exercises */ |
||
| 5008 | $exercises = $qb->getQuery()->getResult(); |
||
| 5009 | $count_exercises = count($exercises); |
||
| 5010 | |||
| 5011 | // Count of user results |
||
| 5012 | $done_exercises = null; |
||
| 5013 | $answered_exercises = 0; |
||
| 5014 | if (!empty($exercises)) { |
||
| 5015 | foreach ($exercises as $exercise_item) { |
||
| 5016 | $attempts = Event::count_exercise_attempts_by_user( |
||
| 5017 | api_get_user_id(), |
||
| 5018 | $exercise_item->getIid(), |
||
| 5019 | $courseId, |
||
| 5020 | $my_session_id |
||
| 5021 | ); |
||
| 5022 | if ($attempts > 1) { |
||
| 5023 | $answered_exercises++; |
||
| 5024 | } |
||
| 5025 | } |
||
| 5026 | } |
||
| 5027 | |||
| 5028 | // Average |
||
| 5029 | $average = ExerciseLib::get_average_score_by_course( |
||
| 5030 | $courseId, |
||
| 5031 | $my_session_id |
||
| 5032 | ); |
||
| 5033 | $all_exercises += $count_exercises; |
||
| 5034 | $all_unanswered_exercises_by_user += $count_exercises - $answered_exercises; |
||
| 5035 | $all_average += $average; |
||
| 5036 | } |
||
| 5037 | |||
| 5038 | if (!empty($course_list)) { |
||
| 5039 | $all_average = $all_average / count($course_list); |
||
| 5040 | } |
||
| 5041 | |||
| 5042 | if (isset($_GET['session_id']) && $my_session_id == $_GET['session_id']) { |
||
| 5043 | $html .= '<tr style="background-color:#FBF09D">'; |
||
| 5044 | } else { |
||
| 5045 | $html .= '<tr>'; |
||
| 5046 | } |
||
| 5047 | $url = api_get_path(WEB_CODE_PATH)."session/index.php?session_id={$my_session_id}"; |
||
| 5048 | |||
| 5049 | $html .= Display::tag('td', Display::url($session_title, $url, ['target' => SESSION_LINK_TARGET])); |
||
| 5050 | $html .= Display::tag('td', $all_exercises); |
||
| 5051 | $html .= Display::tag('td', $all_unanswered_exercises_by_user); |
||
| 5052 | $html .= Display::tag('td', ExerciseLib::convert_to_percentage($all_average)); |
||
| 5053 | |||
| 5054 | if (isset($_GET['session_id']) && $my_session_id == $_GET['session_id']) { |
||
| 5055 | $icon = Display::url( |
||
| 5056 | Display::getMdiIcon( |
||
| 5057 | 'fast-forward-outline', |
||
| 5058 | 'ch-tool-icon', |
||
| 5059 | null, |
||
| 5060 | ICON_SIZE_SMALL, |
||
| 5061 | get_lang('Details') |
||
| 5062 | ), |
||
| 5063 | api_get_self().'?session_id='.$my_session_id.'#course_session_list' |
||
| 5064 | ); |
||
| 5065 | } else { |
||
| 5066 | $icon = Display::url( |
||
| 5067 | Display::getMdiIcon( |
||
| 5068 | 'fast-forward-outline', |
||
| 5069 | 'ch-tool-icon', |
||
| 5070 | null, |
||
| 5071 | ICON_SIZE_SMALL, |
||
| 5072 | get_lang('Details') |
||
| 5073 | ), |
||
| 5074 | api_get_self().'?session_id='.$my_session_id.'#course_session_list' |
||
| 5075 | ); |
||
| 5076 | } |
||
| 5077 | $html .= Display::tag('td', $icon); |
||
| 5078 | $html .= '</tr>'; |
||
| 5079 | } |
||
| 5080 | $html .= '</tbody>'; |
||
| 5081 | $html .= '</table></div><br />'; |
||
| 5082 | $html .= Display::div( |
||
| 5083 | $main_session_graph, |
||
| 5084 | [ |
||
| 5085 | 'id' => 'session_graph', |
||
| 5086 | 'class' => 'chart-session', |
||
| 5087 | 'style' => 'position:relative; text-align: center;', |
||
| 5088 | ] |
||
| 5089 | ); |
||
| 5090 | |||
| 5091 | // Checking selected session. |
||
| 5092 | if (isset($_GET['session_id'])) { |
||
| 5093 | $session_id_from_get = (int) $_GET['session_id']; |
||
| 5094 | $session_data = $course_in_session[$session_id_from_get]; |
||
| 5095 | $course_list = $session_data['course_list']; |
||
| 5096 | |||
| 5097 | $html .= '<a name= "course_session_list"></a>'; |
||
| 5098 | $html .= Display::tag('h3', $session_data['title'].' - '.get_lang('Course list')); |
||
| 5099 | |||
| 5100 | $html .= '<div class="table-responsive">'; |
||
| 5101 | $html .= '<table class="table table-hover table-striped">'; |
||
| 5102 | |||
| 5103 | $columnHeaders = [ |
||
| 5104 | 'course_title' => [ |
||
| 5105 | get_lang('Course'), |
||
| 5106 | ['width' => '300px'], |
||
| 5107 | ], |
||
| 5108 | 'published_exercises' => [ |
||
| 5109 | get_lang('Tests available'), |
||
| 5110 | ], |
||
| 5111 | 'new_exercises' => [ |
||
| 5112 | get_lang('New exercises'), |
||
| 5113 | ], |
||
| 5114 | 'my_average' => [ |
||
| 5115 | get_lang('My average'), |
||
| 5116 | ], |
||
| 5117 | 'average_exercise_result' => [ |
||
| 5118 | get_lang('Average exercise result'), |
||
| 5119 | ], |
||
| 5120 | 'time_spent' => [ |
||
| 5121 | get_lang('Time spent in the course'), |
||
| 5122 | ], |
||
| 5123 | 'lp_progress' => [ |
||
| 5124 | get_lang('Learning path progress'), |
||
| 5125 | ], |
||
| 5126 | 'score' => [ |
||
| 5127 | get_lang('Score'). |
||
| 5128 | Display::getMdiIcon( |
||
| 5129 | ActionIcon::INFORMATION, |
||
| 5130 | 'ch-tool-icon', |
||
| 5131 | null, |
||
| 5132 | ICON_SIZE_SMALL, |
||
| 5133 | get_lang('Average of tests in Learning Paths') |
||
| 5134 | ), |
||
| 5135 | ], |
||
| 5136 | 'best_score' => [ |
||
| 5137 | get_lang('Best score'), |
||
| 5138 | ], |
||
| 5139 | 'last_connection' => [ |
||
| 5140 | get_lang('Latest login'), |
||
| 5141 | ], |
||
| 5142 | 'details' => [ |
||
| 5143 | get_lang('Details'), |
||
| 5144 | ], |
||
| 5145 | ]; |
||
| 5146 | |||
| 5147 | $html .= '<thead><tr>'; |
||
| 5148 | foreach ($columnHeaders as $key => $columnSetting) { |
||
| 5149 | if (isset($trackingColumns['course_session']) && |
||
| 5150 | in_array($key, $trackingColumns['course_session']) && |
||
| 5151 | $trackingColumns['course_session'][$key] |
||
| 5152 | ) { |
||
| 5153 | $settings = isset($columnSetting[1]) ? $columnSetting[1] : []; |
||
| 5154 | $html .= Display::tag( |
||
| 5155 | 'th', |
||
| 5156 | $columnSetting[0], |
||
| 5157 | $settings |
||
| 5158 | ); |
||
| 5159 | } |
||
| 5160 | } |
||
| 5161 | |||
| 5162 | $html .= '</tr> |
||
| 5163 | </thead> |
||
| 5164 | <tbody>'; |
||
| 5165 | |||
| 5166 | foreach ($course_list as $course_data) { |
||
| 5167 | $course_code = $course_data['code']; |
||
| 5168 | $course_title = $course_data['title']; |
||
| 5169 | $courseId = $course_data['real_id']; |
||
| 5170 | $course = api_get_course_entity($courseId); |
||
| 5171 | $session = api_get_session_entity($session_id_from_get); |
||
| 5172 | |||
| 5173 | $qb = Container::getQuizRepository()->findAllByCourse($course, $session, null, 2); |
||
| 5174 | |||
| 5175 | /** @var CQuiz[] $exercises */ |
||
| 5176 | $exercises = $qb->getQuery()->getResult(); |
||
| 5177 | $count_exercises = 0; |
||
| 5178 | if (!empty($exercises)) { |
||
| 5179 | $count_exercises = count($exercises); |
||
| 5180 | } |
||
| 5181 | |||
| 5182 | $answered_exercises = 0; |
||
| 5183 | foreach ($exercises as $exercise_item) { |
||
| 5184 | $attempts = Event::count_exercise_attempts_by_user( |
||
| 5185 | api_get_user_id(), |
||
| 5186 | $exercise_item->getIid(), |
||
| 5187 | $courseId, |
||
| 5188 | $session_id_from_get |
||
| 5189 | ); |
||
| 5190 | if ($attempts > 1) { |
||
| 5191 | $answered_exercises++; |
||
| 5192 | } |
||
| 5193 | } |
||
| 5194 | |||
| 5195 | $unanswered_exercises = $count_exercises - $answered_exercises; |
||
| 5196 | |||
| 5197 | // Average |
||
| 5198 | $average = ExerciseLib::get_average_score_by_course( |
||
| 5199 | $courseId, |
||
| 5200 | $session_id_from_get |
||
| 5201 | ); |
||
| 5202 | $my_average = ExerciseLib::get_average_score_by_course_by_user( |
||
| 5203 | api_get_user_id(), |
||
| 5204 | $courseId, |
||
| 5205 | $session_id_from_get |
||
| 5206 | ); |
||
| 5207 | |||
| 5208 | $bestScore = self::get_avg_student_score( |
||
| 5209 | $user_id, |
||
| 5210 | $course, |
||
| 5211 | [], |
||
| 5212 | $session, |
||
| 5213 | false, |
||
| 5214 | false, |
||
| 5215 | true |
||
| 5216 | ); |
||
| 5217 | |||
| 5218 | $stats_array[$course_code] = [ |
||
| 5219 | 'exercises' => $count_exercises, |
||
| 5220 | 'unanswered_exercises_by_user' => $unanswered_exercises, |
||
| 5221 | 'done_exercises' => $done_exercises, |
||
| 5222 | 'average' => $average, |
||
| 5223 | 'my_average' => $my_average, |
||
| 5224 | 'best_score' => $bestScore, |
||
| 5225 | ]; |
||
| 5226 | |||
| 5227 | $last_connection = self::get_last_connection_date_on_the_course( |
||
| 5228 | $user_id, |
||
| 5229 | $course_data, |
||
| 5230 | $session_id_from_get |
||
| 5231 | ); |
||
| 5232 | |||
| 5233 | $progress = self::get_avg_student_progress( |
||
| 5234 | $user_id, |
||
| 5235 | $course, |
||
| 5236 | [], |
||
| 5237 | $session |
||
| 5238 | ); |
||
| 5239 | |||
| 5240 | $total_time_login = self::get_time_spent_on_the_course( |
||
| 5241 | $user_id, |
||
| 5242 | $courseId, |
||
| 5243 | $session_id_from_get |
||
| 5244 | ); |
||
| 5245 | $time = api_time_to_hms($total_time_login); |
||
| 5246 | |||
| 5247 | $percentage_score = self::get_avg_student_score( |
||
| 5248 | $user_id, |
||
| 5249 | $course, |
||
| 5250 | [], |
||
| 5251 | $session |
||
| 5252 | ); |
||
| 5253 | $courseCodeFromGet = isset($_GET['course']) ? $_GET['course'] : null; |
||
| 5254 | |||
| 5255 | if ($course_code == $courseCodeFromGet && $_GET['session_id'] == $session_id_from_get) { |
||
| 5256 | $html .= '<tr class="row_odd" style="background-color:#FBF09D" >'; |
||
| 5257 | } else { |
||
| 5258 | $html .= '<tr class="row_even">'; |
||
| 5259 | } |
||
| 5260 | |||
| 5261 | $url = api_get_course_url($courseId, $session_id_from_get); |
||
| 5262 | $course_url = Display::url( |
||
| 5263 | $course_title, |
||
| 5264 | $url, |
||
| 5265 | ['target' => SESSION_LINK_TARGET] |
||
| 5266 | ); |
||
| 5267 | |||
| 5268 | if (is_numeric($progress)) { |
||
| 5269 | $progress = $progress.'%'; |
||
| 5270 | } else { |
||
| 5271 | $progress = '0%'; |
||
| 5272 | } |
||
| 5273 | if (is_numeric($percentage_score)) { |
||
| 5274 | $percentage_score = $percentage_score.'%'; |
||
| 5275 | } else { |
||
| 5276 | $percentage_score = '0%'; |
||
| 5277 | } |
||
| 5278 | |||
| 5279 | if (is_numeric($stats_array[$course_code]['best_score'])) { |
||
| 5280 | $bestScore = $stats_array[$course_code]['best_score'].'%'; |
||
| 5281 | } else { |
||
| 5282 | $bestScore = '-'; |
||
| 5283 | } |
||
| 5284 | |||
| 5285 | if (empty($last_connection) || is_bool($last_connection)) { |
||
| 5286 | $last_connection = ''; |
||
| 5287 | } |
||
| 5288 | |||
| 5289 | if ($course_code == $courseCodeFromGet && |
||
| 5290 | $_GET['session_id'] == $session_id_from_get |
||
| 5291 | ) { |
||
| 5292 | $details = Display::url( |
||
| 5293 | Display::getMdiIcon('fast-forward-outline', 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Details')), |
||
| 5294 | '#course_session_data' |
||
| 5295 | ); |
||
| 5296 | } else { |
||
| 5297 | $url = api_get_self(). |
||
| 5298 | '?course='.$course_code.'&session_id='.$session_id_from_get.$extra_params.'#course_session_data'; |
||
| 5299 | $details = Display::url( |
||
| 5300 | Display::getMdiIcon('fast-forward-outline', 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Details') |
||
| 5301 | ), |
||
| 5302 | $url |
||
| 5303 | ); |
||
| 5304 | } |
||
| 5305 | |||
| 5306 | $data = [ |
||
| 5307 | 'course_title' => $course_url, |
||
| 5308 | 'published_exercises' => $stats_array[$course_code]['exercises'], // exercise available |
||
| 5309 | 'new_exercises' => $stats_array[$course_code]['unanswered_exercises_by_user'], |
||
| 5310 | 'my_average' => ExerciseLib::convert_to_percentage($stats_array[$course_code]['my_average']), |
||
| 5311 | 'average_exercise_result' => 0 == $stats_array[$course_code]['average'] ? '-' : '('.ExerciseLib::convert_to_percentage($stats_array[$course_code]['average']).')', |
||
| 5312 | 'time_spent' => $time, |
||
| 5313 | 'lp_progress' => $progress, |
||
| 5314 | 'score' => $percentage_score, |
||
| 5315 | 'best_score' => $bestScore, |
||
| 5316 | 'last_connection' => $last_connection, |
||
| 5317 | 'details' => $details, |
||
| 5318 | ]; |
||
| 5319 | |||
| 5320 | foreach ($data as $key => $value) { |
||
| 5321 | if (in_array($key, $trackingColumns['course_session']) |
||
| 5322 | && $trackingColumns['course_session'][$key] |
||
| 5323 | ) { |
||
| 5324 | $html .= Display::tag('td', $value); |
||
| 5325 | } |
||
| 5326 | } |
||
| 5327 | $html .= '</tr>'; |
||
| 5328 | } |
||
| 5329 | $html .= '</tbody></table></div>'; |
||
| 5330 | } |
||
| 5331 | } |
||
| 5332 | |||
| 5333 | $pluginCalendar = 'true' === api_get_plugin_setting('learning_calendar', 'enabled'); |
||
| 5334 | if ($pluginCalendar) { |
||
| 5335 | $course_in_session[0] = $courseIdList; |
||
| 5336 | $plugin = LearningCalendarPlugin::create(); |
||
| 5337 | $html .= $plugin->getUserStatsPanel($user_id, $course_in_session); |
||
| 5338 | } |
||
| 5339 | |||
| 5340 | return $html; |
||
| 5341 | } |
||
| 5342 | |||
| 5343 | /** |
||
| 5344 | * Shows the user detail progress (when clicking in the details link). |
||
| 5345 | * |
||
| 5346 | * @param int $userId |
||
| 5347 | * @param int $courseId |
||
| 5348 | * @param int $sessionId |
||
| 5349 | * @param bool $showDiagram |
||
| 5350 | * |
||
| 5351 | * @return string html code |
||
| 5352 | */ |
||
| 5353 | public static function show_course_detail($userId, $courseId, $sessionId = 0, $showDiagram = false) |
||
| 5354 | { |
||
| 5355 | $html = ''; |
||
| 5356 | $courseId = (int) $courseId; |
||
| 5357 | |||
| 5358 | if (empty($courseId)) { |
||
| 5359 | return ''; |
||
| 5360 | } |
||
| 5361 | $userId = (int) $userId; |
||
| 5362 | $sessionId = (int) $sessionId; |
||
| 5363 | $course = api_get_course_entity($courseId); |
||
| 5364 | if (null === $course) { |
||
| 5365 | return ''; |
||
| 5366 | } |
||
| 5367 | $courseCode = $course->getCode(); |
||
| 5368 | |||
| 5369 | $html .= '<a name="course_session_data"></a>'; |
||
| 5370 | $html .= Display::page_subheader($course->getTitle()); |
||
| 5371 | |||
| 5372 | if ($showDiagram && !empty($sessionId)) { |
||
| 5373 | $visibility = api_get_session_visibility($sessionId); |
||
| 5374 | if (SESSION_AVAILABLE === $visibility) { |
||
| 5375 | $html .= Display::page_subheader2($course->getTitle()); |
||
| 5376 | } |
||
| 5377 | } |
||
| 5378 | |||
| 5379 | $html .= '<div class="table-responsive">'; |
||
| 5380 | $html .= '<table class="table table-striped table-hover">'; |
||
| 5381 | |||
| 5382 | // Course details |
||
| 5383 | $html .= ' |
||
| 5384 | <thead> |
||
| 5385 | <tr> |
||
| 5386 | <th>'.get_lang('Tests').'</th> |
||
| 5387 | <th>'.get_lang('Attempts').'</th> |
||
| 5388 | <th>'.get_lang('Best attempt').'</th> |
||
| 5389 | <th>'.get_lang('Ranking').'</th> |
||
| 5390 | <th>'.get_lang('Best result in course').'</th> |
||
| 5391 | <th>'.get_lang('Statistics').' ' |
||
| 5392 | .Display::getMdiIcon( |
||
| 5393 | ActionIcon::INFORMATION, |
||
| 5394 | 'ch-tool-icon', |
||
| 5395 | null, |
||
| 5396 | ICON_SIZE_SMALL, |
||
| 5397 | get_lang('In case of multiple attempts') |
||
| 5398 | ). |
||
| 5399 | '</th> |
||
| 5400 | </tr> |
||
| 5401 | </thead> |
||
| 5402 | <tbody>'; |
||
| 5403 | $session = null; |
||
| 5404 | if (empty($sessionId)) { |
||
| 5405 | $user_list = CourseManager::get_user_list_from_course_code( |
||
| 5406 | $courseCode, |
||
| 5407 | $sessionId, |
||
| 5408 | null, |
||
| 5409 | null, |
||
| 5410 | STUDENT |
||
| 5411 | ); |
||
| 5412 | } else { |
||
| 5413 | $session = api_get_session_entity($sessionId); |
||
| 5414 | $user_list = CourseManager::get_user_list_from_course_code( |
||
| 5415 | $courseCode, |
||
| 5416 | $sessionId, |
||
| 5417 | null, |
||
| 5418 | null, |
||
| 5419 | 0 |
||
| 5420 | ); |
||
| 5421 | } |
||
| 5422 | |||
| 5423 | // Show exercise results of invisible exercises? see BT#4091 |
||
| 5424 | /*$exercise_list = ExerciseLib::get_all_exercises( |
||
| 5425 | $course_info, |
||
| 5426 | $session_id, |
||
| 5427 | false, |
||
| 5428 | null, |
||
| 5429 | false, |
||
| 5430 | 2 |
||
| 5431 | );*/ |
||
| 5432 | $qb = Container::getQuizRepository()->findAllByCourse($course, $session, null, 2, false); |
||
| 5433 | /** @var CQuiz[] $exercises */ |
||
| 5434 | $exercises = $qb->getQuery()->getResult(); |
||
| 5435 | |||
| 5436 | $to_graph_exercise_result = []; |
||
| 5437 | if (!empty($exercises)) { |
||
| 5438 | $weighting = $exe_id = 0; |
||
| 5439 | foreach ($exercises as $exercise) { |
||
| 5440 | $exerciseId = $exercise->getIid(); |
||
| 5441 | $exercise_obj = new Exercise($courseId); |
||
| 5442 | $exercise_obj->read($exerciseId); |
||
| 5443 | $visible_return = $exercise_obj->is_visible(); |
||
| 5444 | $score = $weighting = $attempts = 0; |
||
| 5445 | |||
| 5446 | // Getting count of attempts by user |
||
| 5447 | $attempts = Event::count_exercise_attempts_by_user( |
||
| 5448 | api_get_user_id(), |
||
| 5449 | $exercise->getIid(), |
||
| 5450 | $courseId, |
||
| 5451 | $sessionId |
||
| 5452 | ); |
||
| 5453 | |||
| 5454 | $html .= '<tr class="row_even">'; |
||
| 5455 | $url = api_get_path(WEB_CODE_PATH). |
||
| 5456 | "exercise/overview.php?cid={$courseId}&sid=$sessionId&exerciseId={$exerciseId}"; |
||
| 5457 | |||
| 5458 | if (true == $visible_return['value']) { |
||
| 5459 | $exerciseTitle = Display::url( |
||
| 5460 | $exercise->getTitle(), |
||
| 5461 | $url, |
||
| 5462 | ['target' => SESSION_LINK_TARGET] |
||
| 5463 | ); |
||
| 5464 | } elseif (-1 == $exercise->getActive()) { |
||
| 5465 | $exerciseTitle = sprintf(get_lang('%s (deleted)'), $exercise->getTitle()); |
||
| 5466 | } |
||
| 5467 | |||
| 5468 | $html .= Display::tag('td', $exerciseTitle); |
||
| 5469 | $resultsDisabled = $exercise->getResultsDisabled(); |
||
| 5470 | |||
| 5471 | // Exercise configuration show results or show only score |
||
| 5472 | if (0 == $resultsDisabled || 2 == $resultsDisabled) { |
||
| 5473 | //For graphics |
||
| 5474 | $best_exercise_stats = Event::get_best_exercise_results_by_user( |
||
| 5475 | $exerciseId, |
||
| 5476 | $courseId, |
||
| 5477 | $sessionId |
||
| 5478 | ); |
||
| 5479 | |||
| 5480 | $to_graph_exercise_result[$exerciseId] = [ |
||
| 5481 | 'title' => $exerciseTitle, |
||
| 5482 | 'data' => $best_exercise_stats, |
||
| 5483 | ]; |
||
| 5484 | |||
| 5485 | $latest_attempt_url = ''; |
||
| 5486 | $best_score = $position = $percentage_score_result = '-'; |
||
| 5487 | $graph = $normal_graph = null; |
||
| 5488 | |||
| 5489 | // Getting best results |
||
| 5490 | $best_score_data = ExerciseLib::get_best_attempt_in_course( |
||
| 5491 | $exerciseId, |
||
| 5492 | $courseId, |
||
| 5493 | $sessionId |
||
| 5494 | ); |
||
| 5495 | |||
| 5496 | $best_score = ''; |
||
| 5497 | if (!empty($best_score_data)) { |
||
| 5498 | $best_score = ExerciseLib::show_score( |
||
| 5499 | $best_score_data['score'], |
||
| 5500 | $best_score_data['max_score'] |
||
| 5501 | ); |
||
| 5502 | } |
||
| 5503 | |||
| 5504 | if ($attempts > 0) { |
||
| 5505 | $exercise_stat = ExerciseLib::get_best_attempt_by_user( |
||
| 5506 | api_get_user_id(), |
||
| 5507 | $exerciseId, |
||
| 5508 | $courseId, |
||
| 5509 | $sessionId |
||
| 5510 | ); |
||
| 5511 | if (!empty($exercise_stat)) { |
||
| 5512 | // Always getting the BEST attempt |
||
| 5513 | $score = $exercise_stat['score']; |
||
| 5514 | $weighting = $exercise_stat['max_score']; |
||
| 5515 | $exe_id = $exercise_stat['exe_id']; |
||
| 5516 | |||
| 5517 | $latest_attempt_url .= api_get_path(WEB_CODE_PATH). |
||
| 5518 | 'exercise/result.php?id='.$exe_id.'&cid='.$courseId.'&show_headers=1&sid='.$sessionId; |
||
| 5519 | $percentage_score_result = Display::url( |
||
| 5520 | ExerciseLib::show_score($score, $weighting), |
||
| 5521 | $latest_attempt_url |
||
| 5522 | ); |
||
| 5523 | $my_score = 0; |
||
| 5524 | if (!empty($weighting) && 0 != intval($weighting)) { |
||
| 5525 | $my_score = $score / $weighting; |
||
| 5526 | } |
||
| 5527 | //@todo this function slows the page |
||
| 5528 | if (is_int($user_list)) { |
||
| 5529 | $user_list = [$user_list]; |
||
| 5530 | } |
||
| 5531 | $position = ExerciseLib::get_exercise_result_ranking( |
||
| 5532 | $my_score, |
||
| 5533 | $exe_id, |
||
| 5534 | $exerciseId, |
||
| 5535 | $courseCode, |
||
| 5536 | $sessionId, |
||
| 5537 | $user_list |
||
| 5538 | ); |
||
| 5539 | |||
| 5540 | $graph = self::generate_exercise_result_thumbnail_graph( |
||
| 5541 | $to_graph_exercise_result[$exerciseId] |
||
| 5542 | ); |
||
| 5543 | $normal_graph = self::generate_exercise_result_graph( |
||
| 5544 | $to_graph_exercise_result[$exerciseId] |
||
| 5545 | ); |
||
| 5546 | } |
||
| 5547 | } |
||
| 5548 | $html .= Display::div( |
||
| 5549 | $normal_graph, |
||
| 5550 | [ |
||
| 5551 | 'id' => 'main_graph_'.$exerciseId, |
||
| 5552 | 'class' => 'dialog', |
||
| 5553 | 'style' => 'display:none', |
||
| 5554 | ] |
||
| 5555 | ); |
||
| 5556 | |||
| 5557 | if (empty($graph)) { |
||
| 5558 | $graph = '-'; |
||
| 5559 | } else { |
||
| 5560 | $graph = Display::url( |
||
| 5561 | '<img src="'.$graph.'" >', |
||
| 5562 | $normal_graph, |
||
| 5563 | [ |
||
| 5564 | 'id' => $exerciseId, |
||
| 5565 | 'class' => 'expand-image', |
||
| 5566 | ] |
||
| 5567 | ); |
||
| 5568 | } |
||
| 5569 | |||
| 5570 | $html .= Display::tag('td', $attempts); |
||
| 5571 | $html .= Display::tag('td', $percentage_score_result); |
||
| 5572 | $html .= Display::tag('td', $position); |
||
| 5573 | $html .= Display::tag('td', $best_score); |
||
| 5574 | $html .= Display::tag('td', $graph); |
||
| 5575 | } else { |
||
| 5576 | // Exercise configuration NO results |
||
| 5577 | $html .= Display::tag('td', $attempts); |
||
| 5578 | $html .= Display::tag('td', '-'); |
||
| 5579 | $html .= Display::tag('td', '-'); |
||
| 5580 | $html .= Display::tag('td', '-'); |
||
| 5581 | $html .= Display::tag('td', '-'); |
||
| 5582 | } |
||
| 5583 | $html .= '</tr>'; |
||
| 5584 | } |
||
| 5585 | } else { |
||
| 5586 | $html .= '<tr><td colspan="5">'.get_lang('There is no test for the moment').'</td></tr>'; |
||
| 5587 | } |
||
| 5588 | $html .= '</tbody></table></div>'; |
||
| 5589 | |||
| 5590 | $columnHeaders = [ |
||
| 5591 | 'lp' => get_lang('Learning paths'), |
||
| 5592 | 'time' => get_lang('Time spent'), |
||
| 5593 | 'progress' => get_lang('Progress'), |
||
| 5594 | 'score' => get_lang('Score'), |
||
| 5595 | 'best_score' => get_lang('Best score'), |
||
| 5596 | 'last_connection' => get_lang('Latest login'), |
||
| 5597 | ]; |
||
| 5598 | |||
| 5599 | $headers = ''; |
||
| 5600 | $trackingColumns = api_get_setting('session.tracking_columns', true); |
||
| 5601 | if (isset($trackingColumns['my_progress_lp'])) { |
||
| 5602 | foreach ($columnHeaders as $key => $value) { |
||
| 5603 | if (!isset($trackingColumns['my_progress_lp'][$key]) || |
||
| 5604 | false == $trackingColumns['my_progress_lp'][$key] |
||
| 5605 | ) { |
||
| 5606 | unset($columnHeaders[$key]); |
||
| 5607 | } |
||
| 5608 | } |
||
| 5609 | } |
||
| 5610 | |||
| 5611 | $columnHeadersKeys = array_keys($columnHeaders); |
||
| 5612 | foreach ($columnHeaders as $columnName) { |
||
| 5613 | $headers .= Display::tag( |
||
| 5614 | 'th', |
||
| 5615 | $columnName |
||
| 5616 | ); |
||
| 5617 | } |
||
| 5618 | |||
| 5619 | // LP table results |
||
| 5620 | $html .= '<div class="table-responsive">'; |
||
| 5621 | $html .= '<table class="table table-striped table-hover">'; |
||
| 5622 | $html .= '<thead><tr>'; |
||
| 5623 | $html .= $headers; |
||
| 5624 | $html .= '</tr></thead><tbody>'; |
||
| 5625 | |||
| 5626 | $list = new LearnpathList( |
||
| 5627 | api_get_user_id(), |
||
| 5628 | ['real_id' => $courseId], |
||
| 5629 | $sessionId, |
||
| 5630 | 'resource.publishedOn ASC', |
||
| 5631 | true, |
||
| 5632 | null, |
||
| 5633 | true |
||
| 5634 | ); |
||
| 5635 | |||
| 5636 | $lp_list = $list->get_flat_list(); |
||
| 5637 | |||
| 5638 | if (!empty($lp_list)) { |
||
| 5639 | foreach ($lp_list as $lp_id => $learnpath) { |
||
| 5640 | if (!$learnpath['lp_visibility']) { |
||
| 5641 | continue; |
||
| 5642 | } |
||
| 5643 | |||
| 5644 | $progress = self::get_avg_student_progress( |
||
| 5645 | $userId, |
||
| 5646 | $course, |
||
| 5647 | [$lp_id], |
||
| 5648 | $session |
||
| 5649 | ); |
||
| 5650 | $last_connection_in_lp = self::get_last_connection_time_in_lp( |
||
| 5651 | $userId, |
||
| 5652 | $course->getCode(), |
||
| 5653 | $lp_id, |
||
| 5654 | $sessionId |
||
| 5655 | ); |
||
| 5656 | |||
| 5657 | $time_spent_in_lp = self::get_time_spent_in_lp( |
||
| 5658 | $userId, |
||
| 5659 | $course, |
||
| 5660 | [$lp_id], |
||
| 5661 | $sessionId |
||
| 5662 | ); |
||
| 5663 | $percentage_score = self::get_avg_student_score( |
||
| 5664 | $userId, |
||
| 5665 | $course, |
||
| 5666 | [$lp_id], |
||
| 5667 | $session |
||
| 5668 | ); |
||
| 5669 | |||
| 5670 | $bestScore = self::get_avg_student_score( |
||
| 5671 | $userId, |
||
| 5672 | $course, |
||
| 5673 | [$lp_id], |
||
| 5674 | $session, |
||
| 5675 | false, |
||
| 5676 | false, |
||
| 5677 | true |
||
| 5678 | ); |
||
| 5679 | |||
| 5680 | if (is_numeric($progress)) { |
||
| 5681 | $progress = $progress.'%'; |
||
| 5682 | } |
||
| 5683 | if (is_numeric($percentage_score)) { |
||
| 5684 | $percentage_score = $percentage_score.'%'; |
||
| 5685 | } else { |
||
| 5686 | $percentage_score = '0%'; |
||
| 5687 | } |
||
| 5688 | |||
| 5689 | if (is_numeric($bestScore)) { |
||
| 5690 | $bestScore = $bestScore.'%'; |
||
| 5691 | } else { |
||
| 5692 | $bestScore = '-'; |
||
| 5693 | } |
||
| 5694 | |||
| 5695 | $time_spent_in_lp = api_time_to_hms($time_spent_in_lp); |
||
| 5696 | $last_connection = '-'; |
||
| 5697 | if (!empty($last_connection_in_lp)) { |
||
| 5698 | $last_connection = api_convert_and_format_date( |
||
| 5699 | $last_connection_in_lp, |
||
| 5700 | DATE_TIME_FORMAT_LONG |
||
| 5701 | ); |
||
| 5702 | } |
||
| 5703 | |||
| 5704 | $url = api_get_path(WEB_CODE_PATH). |
||
| 5705 | "lp/lp_controller.php?cid={$courseId}&sid=$sessionId&lp_id=$lp_id&action=view"; |
||
| 5706 | $html .= '<tr class="row_even">'; |
||
| 5707 | |||
| 5708 | if (in_array('lp', $columnHeadersKeys)) { |
||
| 5709 | if (0 == $learnpath['lp_visibility']) { |
||
| 5710 | $html .= Display::tag('td', $learnpath['lp_name']); |
||
| 5711 | } else { |
||
| 5712 | $html .= Display::tag( |
||
| 5713 | 'td', |
||
| 5714 | Display::url( |
||
| 5715 | $learnpath['lp_name'], |
||
| 5716 | $url, |
||
| 5717 | ['target' => SESSION_LINK_TARGET] |
||
| 5718 | ) |
||
| 5719 | ); |
||
| 5720 | } |
||
| 5721 | } |
||
| 5722 | |||
| 5723 | if (in_array('time', $columnHeadersKeys)) { |
||
| 5724 | $html .= Display::tag( |
||
| 5725 | 'td', |
||
| 5726 | $time_spent_in_lp |
||
| 5727 | ); |
||
| 5728 | } |
||
| 5729 | |||
| 5730 | if (in_array('progress', $columnHeadersKeys)) { |
||
| 5731 | $html .= Display::tag( |
||
| 5732 | 'td', |
||
| 5733 | $progress |
||
| 5734 | ); |
||
| 5735 | } |
||
| 5736 | |||
| 5737 | if (in_array('score', $columnHeadersKeys)) { |
||
| 5738 | $html .= Display::tag('td', $percentage_score); |
||
| 5739 | } |
||
| 5740 | if (in_array('best_score', $columnHeadersKeys)) { |
||
| 5741 | $html .= Display::tag('td', $bestScore); |
||
| 5742 | } |
||
| 5743 | |||
| 5744 | if (in_array('last_connection', $columnHeadersKeys)) { |
||
| 5745 | $html .= Display::tag('td', $last_connection, ['width' => '180px']); |
||
| 5746 | } |
||
| 5747 | $html .= '</tr>'; |
||
| 5748 | } |
||
| 5749 | } else { |
||
| 5750 | $html .= '<tr> |
||
| 5751 | <td colspan="4" align="center"> |
||
| 5752 | '.get_lang('No learning path').' |
||
| 5753 | </td> |
||
| 5754 | </tr>'; |
||
| 5755 | } |
||
| 5756 | $html .= '</tbody></table></div>'; |
||
| 5757 | |||
| 5758 | $html .= self::displayUserSkills($userId, $courseId, $sessionId); |
||
| 5759 | |||
| 5760 | return $html; |
||
| 5761 | } |
||
| 5762 | |||
| 5763 | /** |
||
| 5764 | * Generates an histogram. |
||
| 5765 | * |
||
| 5766 | * @param array $names list of exercise names |
||
| 5767 | * @param array $my_results my results 0 to 100 |
||
| 5768 | * @param array $average average scores 0-100 |
||
| 5769 | * |
||
| 5770 | * @return string |
||
| 5771 | */ |
||
| 5772 | public static function generate_session_exercise_graph($names, $my_results, $average) |
||
| 5773 | { |
||
| 5774 | //$html = api_get_js('chartjs/Chart.js'); |
||
| 5775 | $canvas = Display::tag('canvas', '', ['id' => 'session_graph_chart']); |
||
| 5776 | $html = Display::tag('div', $canvas, ['style' => 'width:100%']); |
||
| 5777 | $jsStr = " var data = { |
||
| 5778 | labels:".json_encode($names).", |
||
| 5779 | datasets: [ |
||
| 5780 | { |
||
| 5781 | label: '".get_lang('My results')."', |
||
| 5782 | backgroundColor: 'rgb(255, 99, 132)', |
||
| 5783 | stack: 'Stack1', |
||
| 5784 | data: ".json_encode($my_results).", |
||
| 5785 | }, |
||
| 5786 | { |
||
| 5787 | label: '".get_lang('Average score')."', |
||
| 5788 | backgroundColor: 'rgb(75, 192, 192)', |
||
| 5789 | stack: 'Stack2', |
||
| 5790 | data: ".json_encode($average).", |
||
| 5791 | }, |
||
| 5792 | ], |
||
| 5793 | }; |
||
| 5794 | var ctx = document.getElementById('session_graph_chart').getContext('2d'); |
||
| 5795 | var myBarChart = new Chart(ctx, { |
||
| 5796 | type: 'bar', |
||
| 5797 | data: data, |
||
| 5798 | options: { |
||
| 5799 | title: { |
||
| 5800 | display: true, |
||
| 5801 | text: '".get_lang('TestsInTimeProgressChart')."' |
||
| 5802 | }, |
||
| 5803 | tooltips: { |
||
| 5804 | mode: 'index', |
||
| 5805 | intersect: false |
||
| 5806 | }, |
||
| 5807 | responsive: true, |
||
| 5808 | scales: { |
||
| 5809 | yAxes: [{ |
||
| 5810 | ticks: { |
||
| 5811 | // Include a dollar sign in the ticks |
||
| 5812 | callback: function(value, index, values) { |
||
| 5813 | return value + '%'; |
||
| 5814 | } |
||
| 5815 | } |
||
| 5816 | }] |
||
| 5817 | } |
||
| 5818 | } |
||
| 5819 | });"; |
||
| 5820 | $html .= Display::tag('script', $jsStr); |
||
| 5821 | |||
| 5822 | return $html; |
||
| 5823 | } |
||
| 5824 | |||
| 5825 | /** |
||
| 5826 | * Returns a thumbnail of the function generate_exercise_result_graph. |
||
| 5827 | * |
||
| 5828 | * @param array $attempts |
||
| 5829 | */ |
||
| 5830 | public static function generate_exercise_result_thumbnail_graph($attempts) |
||
| 6011 | } |
||
| 6012 | |||
| 6013 | /** |
||
| 6014 | * Generates a big graph with the number of best results. |
||
| 6015 | * |
||
| 6016 | * @param array |
||
| 6017 | */ |
||
| 6018 | public static function generate_exercise_result_graph($attempts) |
||
| 6019 | { |
||
| 6020 | $exercise_title = strip_tags($attempts['title']); |
||
| 6021 | $attempts = $attempts['data']; |
||
| 6022 | $my_exercise_result_array = $exercise_result = []; |
||
| 6023 | if (empty($attempts)) { |
||
| 6024 | return null; |
||
| 6025 | } |
||
| 6026 | foreach ($attempts as $attempt) { |
||
| 6027 | if (api_get_user_id() == $attempt['exe_user_id']) { |
||
| 6028 | if (0 != $attempt['max_score']) { |
||
| 6029 | $my_exercise_result_array[] = $attempt['score'] / $attempt['max_score']; |
||
| 6030 | } |
||
| 6031 | } else { |
||
| 6032 | if (0 != $attempt['max_score']) { |
||
| 6033 | $exercise_result[] = $attempt['score'] / $attempt['max_score']; |
||
| 6034 | } |
||
| 6035 | } |
||
| 6036 | } |
||
| 6037 | |||
| 6038 | //Getting best result |
||
| 6039 | rsort($my_exercise_result_array); |
||
| 6040 | $my_exercise_result = 0; |
||
| 6041 | if (isset($my_exercise_result_array[0])) { |
||
| 6042 | $my_exercise_result = $my_exercise_result_array[0] * 100; |
||
| 6043 | } |
||
| 6044 | |||
| 6045 | $max = 100; |
||
| 6046 | $pieces = 5; |
||
| 6047 | $part = round($max / $pieces); |
||
| 6048 | $x_axis = []; |
||
| 6049 | $final_array = []; |
||
| 6050 | $my_final_array = []; |
||
| 6051 | |||
| 6052 | for ($i = 1; $i <= $pieces; $i++) { |
||
| 6053 | $sum = 1; |
||
| 6054 | if (1 == $i) { |
||
| 6055 | $sum = 0; |
||
| 6056 | } |
||
| 6057 | $min = ($i - 1) * $part + $sum; |
||
| 6058 | $max = ($i) * $part; |
||
| 6059 | $x_axis[] = $min." - ".$max; |
||
| 6060 | $count = 0; |
||
| 6061 | foreach ($exercise_result as $result) { |
||
| 6062 | $percentage = $result * 100; |
||
| 6063 | if ($percentage >= $min && $percentage <= $max) { |
||
| 6064 | $count++; |
||
| 6065 | } |
||
| 6066 | } |
||
| 6067 | $final_array[] = $count; |
||
| 6068 | |||
| 6069 | if ($my_exercise_result >= $min && $my_exercise_result <= $max) { |
||
| 6070 | $my_final_array[] = 1; |
||
| 6071 | } else { |
||
| 6072 | $my_final_array[] = 0; |
||
| 6073 | } |
||
| 6074 | } |
||
| 6075 | |||
| 6076 | //Fix to remove the data of the user with my data |
||
| 6077 | |||
| 6078 | for ($i = 0; $i <= count($my_final_array); $i++) { |
||
| 6079 | if (!empty($my_final_array[$i])) { |
||
| 6080 | $my_final_array[$i] = $final_array[$i] + 1; //Add my result |
||
| 6081 | $final_array[$i] = 0; |
||
| 6082 | } |
||
| 6083 | } |
||
| 6084 | |||
| 6085 | // Dataset definition |
||
| 6086 | $dataSet = new pData(); |
||
| 6087 | $dataSet->addPoints($final_array, 'Serie1'); |
||
| 6088 | $dataSet->addPoints($my_final_array, 'Serie2'); |
||
| 6089 | $dataSet->addPoints($x_axis, 'Serie3'); |
||
| 6090 | |||
| 6091 | $dataSet->setSerieDescription('Serie1', get_lang('Score')); |
||
| 6092 | $dataSet->setSerieDescription('Serie2', get_lang('My results')); |
||
| 6093 | $dataSet->setAbscissa('Serie3'); |
||
| 6094 | |||
| 6095 | $dataSet->setXAxisName(get_lang('Score')); |
||
| 6096 | $dataSet->normalize(100, "%"); |
||
| 6097 | |||
| 6098 | $dataSet->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true); |
||
| 6099 | |||
| 6100 | // Cache definition |
||
| 6101 | $cachePath = api_get_path(SYS_ARCHIVE_PATH); |
||
| 6102 | $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]); |
||
| 6103 | $chartHash = $myCache->getHash($dataSet); |
||
| 6104 | |||
| 6105 | if ($myCache->isInCache($chartHash)) { |
||
| 6106 | $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash; |
||
| 6107 | $myCache->saveFromCache($chartHash, $imgPath); |
||
| 6108 | $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash; |
||
| 6109 | } else { |
||
| 6110 | /* Create the pChart object */ |
||
| 6111 | $widthSize = 480; |
||
| 6112 | $heightSize = 250; |
||
| 6113 | $fontSize = 8; |
||
| 6114 | $myPicture = new pImage($widthSize, $heightSize, $dataSet); |
||
| 6115 | |||
| 6116 | /* Turn of Antialiasing */ |
||
| 6117 | $myPicture->Antialias = false; |
||
| 6118 | |||
| 6119 | /* Add a border to the picture */ |
||
| 6120 | $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]); |
||
| 6121 | |||
| 6122 | /* Set the default font */ |
||
| 6123 | $myPicture->setFontProperties( |
||
| 6124 | [ |
||
| 6125 | 'FontName' => api_get_path( |
||
| 6126 | SYS_FONTS_PATH |
||
| 6127 | ).'opensans/OpenSans-Regular.ttf', |
||
| 6128 | 'FontSize' => 10, |
||
| 6129 | ] |
||
| 6130 | ); |
||
| 6131 | |||
| 6132 | /* Write the chart title */ |
||
| 6133 | $myPicture->drawText( |
||
| 6134 | 250, |
||
| 6135 | 20, |
||
| 6136 | $exercise_title, |
||
| 6137 | [ |
||
| 6138 | 'FontSize' => 12, |
||
| 6139 | 'Align' => TEXT_ALIGN_BOTTOMMIDDLE, |
||
| 6140 | ] |
||
| 6141 | ); |
||
| 6142 | |||
| 6143 | /* Define the chart area */ |
||
| 6144 | $myPicture->setGraphArea(50, 50, $widthSize - 20, $heightSize - 30); |
||
| 6145 | |||
| 6146 | /* Draw the scale */ |
||
| 6147 | $scaleSettings = [ |
||
| 6148 | 'GridR' => 200, |
||
| 6149 | 'GridG' => 200, |
||
| 6150 | 'GridB' => 200, |
||
| 6151 | 'DrawSubTicks' => true, |
||
| 6152 | 'CycleBackground' => true, |
||
| 6153 | 'Mode' => SCALE_MODE_MANUAL, |
||
| 6154 | 'ManualScale' => [ |
||
| 6155 | '0' => [ |
||
| 6156 | 'Min' => 0, |
||
| 6157 | 'Max' => 100, |
||
| 6158 | ], |
||
| 6159 | ], |
||
| 6160 | ]; |
||
| 6161 | $myPicture->drawScale($scaleSettings); |
||
| 6162 | |||
| 6163 | /* Turn on shadow computing */ |
||
| 6164 | $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]); |
||
| 6165 | |||
| 6166 | /* Draw the chart */ |
||
| 6167 | $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]); |
||
| 6168 | $settings = [ |
||
| 6169 | 'DisplayValues' => true, |
||
| 6170 | 'DisplaySize' => $fontSize, |
||
| 6171 | 'DisplayR' => 0, |
||
| 6172 | 'DisplayG' => 0, |
||
| 6173 | 'DisplayB' => 0, |
||
| 6174 | 'DisplayOrientation' => ORIENTATION_HORIZONTAL, |
||
| 6175 | 'Gradient' => false, |
||
| 6176 | 'Surrounding' => 30, |
||
| 6177 | 'InnerSurrounding' => 25, |
||
| 6178 | ]; |
||
| 6179 | $myPicture->drawStackedBarChart($settings); |
||
| 6180 | |||
| 6181 | $legendSettings = [ |
||
| 6182 | 'Mode' => LEGEND_HORIZONTAL, |
||
| 6183 | 'Style' => LEGEND_NOBORDER, |
||
| 6184 | ]; |
||
| 6185 | $myPicture->drawLegend($widthSize / 2, 30, $legendSettings); |
||
| 6186 | |||
| 6187 | /* Write and save into cache */ |
||
| 6188 | $myCache->writeToCache($chartHash, $myPicture); |
||
| 6189 | $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash; |
||
| 6190 | $myCache->saveFromCache($chartHash, $imgPath); |
||
| 6191 | $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash; |
||
| 6192 | } |
||
| 6193 | |||
| 6194 | return $imgPath; |
||
| 6195 | } |
||
| 6196 | |||
| 6197 | /** |
||
| 6198 | * @param FormValidator $form |
||
| 6199 | * |
||
| 6200 | * @return mixed |
||
| 6201 | */ |
||
| 6202 | public static function setUserSearchForm($form) |
||
| 6203 | { |
||
| 6204 | $form->addElement('text', 'keyword', get_lang('Keyword')); |
||
| 6205 | $form->addSelect( |
||
| 6206 | 'active', |
||
| 6207 | get_lang('Status'), |
||
| 6208 | [1 => get_lang('active'), 0 => get_lang('inactive')] |
||
| 6209 | ); |
||
| 6210 | |||
| 6211 | $form->addSelect( |
||
| 6212 | 'sleeping_days', |
||
| 6213 | get_lang('Inactive days'), |
||
| 6214 | [ |
||
| 6215 | '', |
||
| 6216 | 1 => 1, |
||
| 6217 | 5 => 5, |
||
| 6218 | 15 => 15, |
||
| 6219 | 30 => 30, |
||
| 6220 | 60 => 60, |
||
| 6221 | 90 => 90, |
||
| 6222 | 120 => 120, |
||
| 6223 | ] |
||
| 6224 | ); |
||
| 6225 | |||
| 6226 | $form->addButtonSearch(get_lang('Search')); |
||
| 6227 | |||
| 6228 | return $form; |
||
| 6229 | } |
||
| 6230 | |||
| 6231 | /** |
||
| 6232 | * Get the progress of a exercise. |
||
| 6233 | * |
||
| 6234 | * @param int $sessionId The session ID (session.id) |
||
| 6235 | * @param int $courseId The course ID (course.id) |
||
| 6236 | * @param int $exerciseId The quiz ID (c_quiz.id) |
||
| 6237 | * @param string $date_from |
||
| 6238 | * @param string $date_to |
||
| 6239 | * @param array $options An array of options you can pass to the query (limit, where and order) |
||
| 6240 | * |
||
| 6241 | * @return array An array with the data of exercise(s) progress |
||
| 6242 | */ |
||
| 6243 | public static function get_exercise_progress( |
||
| 6244 | $sessionId = 0, |
||
| 6245 | $courseId = 0, |
||
| 6246 | $exerciseId = 0, |
||
| 6247 | $date_from = null, |
||
| 6248 | $date_to = null, |
||
| 6249 | $options = [] |
||
| 6250 | ) { |
||
| 6251 | $sessionId = intval($sessionId); |
||
| 6252 | $courseId = intval($courseId); |
||
| 6253 | $exerciseId = intval($exerciseId); |
||
| 6254 | $date_from = Database::escape_string($date_from); |
||
| 6255 | $date_to = Database::escape_string($date_to); |
||
| 6256 | /* |
||
| 6257 | * This method gets the data by blocks, as previous attempts at one single |
||
| 6258 | * query made it take ages. The logic of query division is described below |
||
| 6259 | */ |
||
| 6260 | // Get tables names |
||
| 6261 | $tuser = Database::get_main_table(TABLE_MAIN_USER); |
||
| 6262 | $tquiz = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 6263 | $tquiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 6264 | $tquiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 6265 | $tquiz_rel_question = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); |
||
| 6266 | $ttrack_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 6267 | $ttrack_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 6268 | |||
| 6269 | $sessions = []; |
||
| 6270 | $courses = []; |
||
| 6271 | // if session ID is defined but course ID is empty, get all the courses |
||
| 6272 | // from that session |
||
| 6273 | if (!empty($sessionId) && empty($courseId)) { |
||
| 6274 | // $courses is an array of course int id as index and course details hash as value |
||
| 6275 | $courses = SessionManager::get_course_list_by_session_id($sessionId); |
||
| 6276 | $sessions[$sessionId] = api_get_session_info($sessionId); |
||
| 6277 | } elseif (empty($sessionId) && !empty($courseId)) { |
||
| 6278 | // if, to the contrary, course is defined but not sessions, get the sessions that include this course |
||
| 6279 | // $sessions is an array like: [0] => ('id' => 3, 'title' => 'Session 35'), [1] => () etc; |
||
| 6280 | $course = api_get_course_info_by_id($courseId); |
||
| 6281 | $sessionsTemp = SessionManager::get_session_by_course($courseId); |
||
| 6282 | $courses[$courseId] = $course; |
||
| 6283 | foreach ($sessionsTemp as $sessionItem) { |
||
| 6284 | $sessions[$sessionItem['id']] = $sessionItem; |
||
| 6285 | } |
||
| 6286 | } elseif (!empty($courseId) && !empty($sessionId)) { |
||
| 6287 | //none is empty |
||
| 6288 | $course = api_get_course_info_by_id($courseId); |
||
| 6289 | $courses[$courseId] = [$course['code']]; |
||
| 6290 | $courses[$courseId]['code'] = $course['code']; |
||
| 6291 | $sessions[$sessionId] = api_get_session_info($sessionId); |
||
| 6292 | } else { |
||
| 6293 | //both are empty, not enough data, return an empty array |
||
| 6294 | return []; |
||
| 6295 | } |
||
| 6296 | // Now we have two arrays of courses and sessions with enough data to proceed |
||
| 6297 | // If no course could be found, we shouldn't return anything. |
||
| 6298 | // Course sessions can be empty (then we only return the pure-course-context results) |
||
| 6299 | if (count($courses) < 1) { |
||
| 6300 | return []; |
||
| 6301 | } |
||
| 6302 | |||
| 6303 | $data = []; |
||
| 6304 | // The following loop is less expensive than what it seems: |
||
| 6305 | // - if a course was defined, then we only loop through sessions |
||
| 6306 | // - if a session was defined, then we only loop through courses |
||
| 6307 | // - if a session and a course were defined, then we only loop once |
||
| 6308 | foreach ($courses as $courseIdx => $courseData) { |
||
| 6309 | $where = ''; |
||
| 6310 | $whereParams = []; |
||
| 6311 | $whereSessionParams = ''; |
||
| 6312 | if (count($sessions > 0)) { |
||
| 6313 | foreach ($sessions as $sessionIdx => $sessionData) { |
||
| 6314 | if (!empty($sessionIdx)) { |
||
| 6315 | $whereSessionParams .= $sessionIdx.','; |
||
| 6316 | } |
||
| 6317 | } |
||
| 6318 | $whereSessionParams = substr($whereSessionParams, 0, -1); |
||
| 6319 | } |
||
| 6320 | |||
| 6321 | if (!empty($exerciseId)) { |
||
| 6322 | $exerciseId = intval($exerciseId); |
||
| 6323 | $where .= ' AND q.iid = %d '; |
||
| 6324 | $whereParams[] = $exerciseId; |
||
| 6325 | } |
||
| 6326 | |||
| 6327 | /* |
||
| 6328 | * This feature has been disabled for now, to avoid having to |
||
| 6329 | * join two very large tables |
||
| 6330 | //2 = show all questions (wrong and correct answered) |
||
| 6331 | if ($answer != 2) { |
||
| 6332 | $answer = intval($answer); |
||
| 6333 | //$where .= ' AND qa.correct = %d'; |
||
| 6334 | //$whereParams[] = $answer; |
||
| 6335 | } |
||
| 6336 | */ |
||
| 6337 | |||
| 6338 | $limit = ''; |
||
| 6339 | if (!empty($options['limit'])) { |
||
| 6340 | $limit = " LIMIT ".$options['limit']; |
||
| 6341 | } |
||
| 6342 | |||
| 6343 | if (!empty($options['where'])) { |
||
| 6344 | $where .= ' AND '.Database::escape_string($options['where']); |
||
| 6345 | } |
||
| 6346 | |||
| 6347 | $order = ''; |
||
| 6348 | if (!empty($options['order'])) { |
||
| 6349 | $order = " ORDER BY ".$options['order']; |
||
| 6350 | } |
||
| 6351 | |||
| 6352 | if (!empty($date_to) && !empty($date_from)) { |
||
| 6353 | $where .= sprintf(" AND (te.start_date BETWEEN '%s 00:00:00' AND '%s 23:59:59')", $date_from, $date_to); |
||
| 6354 | } |
||
| 6355 | |||
| 6356 | $sql = "SELECT |
||
| 6357 | te.session_id, |
||
| 6358 | ta.id as attempt_id, |
||
| 6359 | te.exe_user_id as user_id, |
||
| 6360 | te.exe_id as exercise_attempt_id, |
||
| 6361 | ta.question_id, |
||
| 6362 | ta.answer as answer_id, |
||
| 6363 | ta.tms as time, |
||
| 6364 | te.exe_exo_id as quiz_id, |
||
| 6365 | CONCAT ('c', q.c_id, '_e', q.iid) as exercise_id, |
||
| 6366 | q.title as quiz_title, |
||
| 6367 | qq.description as description |
||
| 6368 | FROM $ttrack_exercises te |
||
| 6369 | INNER JOIN $ttrack_attempt ta |
||
| 6370 | ON ta.exe_id = te.exe_id |
||
| 6371 | INNER JOIN $tquiz q |
||
| 6372 | ON q.iid = te.exe_exo_id |
||
| 6373 | INNER JOIN $tquiz_rel_question rq |
||
| 6374 | ON rq.quiz_id = q.iid AND rq.c_id = q.c_id |
||
| 6375 | INNER JOIN $tquiz_question qq |
||
| 6376 | ON |
||
| 6377 | qq.iid = rq.question_id AND |
||
| 6378 | qq.c_id = rq.c_id AND |
||
| 6379 | qq.position = rq.question_order AND |
||
| 6380 | ta.question_id = rq.question_id |
||
| 6381 | WHERE |
||
| 6382 | te.c_id = $courseIdx ".(empty($whereSessionParams) ? '' : "AND te.session_id IN ($whereSessionParams)")." |
||
| 6383 | AND q.c_id = $courseIdx |
||
| 6384 | $where $order $limit"; |
||
| 6385 | $sql_query = vsprintf($sql, $whereParams); |
||
| 6386 | |||
| 6387 | // Now browse through the results and get the data |
||
| 6388 | $rs = Database::query($sql_query); |
||
| 6389 | $userIds = []; |
||
| 6390 | $questionIds = []; |
||
| 6391 | $answerIds = []; |
||
| 6392 | while ($row = Database::fetch_array($rs)) { |
||
| 6393 | //only show if exercise is visible |
||
| 6394 | if (api_get_item_visibility($courseData, 'quiz', $row['exercise_id'])) { |
||
| 6395 | $userIds[$row['user_id']] = $row['user_id']; |
||
| 6396 | $questionIds[$row['question_id']] = $row['question_id']; |
||
| 6397 | $answerIds[$row['question_id']][$row['answer_id']] = $row['answer_id']; |
||
| 6398 | $row['session'] = $sessions[$row['session_id']]; |
||
| 6399 | $data[] = $row; |
||
| 6400 | } |
||
| 6401 | } |
||
| 6402 | // Now fill questions data. Query all questions and answers for this test to avoid |
||
| 6403 | $sqlQuestions = "SELECT tq.c_id, tq.iid as question_id, tq.question, tqa.iid, |
||
| 6404 | tqa.answer, tqa.correct, tq.position, tqa.iid as answer_id |
||
| 6405 | FROM $tquiz_question tq, $tquiz_answer tqa |
||
| 6406 | WHERE |
||
| 6407 | tqa.question_id = tq.iid AND |
||
| 6408 | tqa.c_id = tq.c_id AND |
||
| 6409 | tq.c_id = $courseIdx AND |
||
| 6410 | tq.iid IN (".implode(',', $questionIds).")"; |
||
| 6411 | |||
| 6412 | $resQuestions = Database::query($sqlQuestions); |
||
| 6413 | $answer = []; |
||
| 6414 | $question = []; |
||
| 6415 | while ($rowQuestion = Database::fetch_assoc($resQuestions)) { |
||
| 6416 | $questionId = $rowQuestion['question_id']; |
||
| 6417 | $answerId = $rowQuestion['answer_id']; |
||
| 6418 | $answer[$questionId][$answerId] = [ |
||
| 6419 | 'position' => $rowQuestion['position'], |
||
| 6420 | 'question' => $rowQuestion['question'], |
||
| 6421 | 'answer' => $rowQuestion['answer'], |
||
| 6422 | 'correct' => $rowQuestion['correct'], |
||
| 6423 | ]; |
||
| 6424 | $question[$questionId]['question'] = $rowQuestion['question']; |
||
| 6425 | } |
||
| 6426 | |||
| 6427 | // Now fill users data |
||
| 6428 | $sqlUsers = "SELECT id as user_id, username, lastname, firstname |
||
| 6429 | FROM $tuser |
||
| 6430 | WHERE active <> ".USER_SOFT_DELETED." AND id IN (".implode(',', $userIds).")"; |
||
| 6431 | $resUsers = Database::query($sqlUsers); |
||
| 6432 | while ($rowUser = Database::fetch_assoc($resUsers)) { |
||
| 6433 | $users[$rowUser['user_id']] = $rowUser; |
||
| 6434 | } |
||
| 6435 | |||
| 6436 | foreach ($data as $id => $row) { |
||
| 6437 | $rowQuestId = $row['question_id']; |
||
| 6438 | $rowAnsId = $row['answer_id']; |
||
| 6439 | $data[$id]['session'] = $sessions[$row['session_id']]['title']; |
||
| 6440 | $data[$id]['firstname'] = $users[$row['user_id']]['firstname']; |
||
| 6441 | $data[$id]['lastname'] = $users[$row['user_id']]['lastname']; |
||
| 6442 | $data[$id]['username'] = $users[$row['user_id']]['username']; |
||
| 6443 | $data[$id]['answer'] = $answer[$rowQuestId][$rowAnsId]['answer']; |
||
| 6444 | $data[$id]['correct'] = (0 == $answer[$rowQuestId][$rowAnsId]['correct'] ? get_lang('No') : get_lang('Yes')); |
||
| 6445 | $data[$id]['question'] = $question[$rowQuestId]['question']; |
||
| 6446 | $data[$id]['question_id'] = $rowQuestId; |
||
| 6447 | $data[$id]['description'] = $row['description']; |
||
| 6448 | } |
||
| 6449 | |||
| 6450 | /* |
||
| 6451 | The minimum expected array structure at the end is: |
||
| 6452 | attempt_id, |
||
| 6453 | session title, |
||
| 6454 | exercise_id, |
||
| 6455 | quiz_title, |
||
| 6456 | username, |
||
| 6457 | lastname, |
||
| 6458 | firstname, |
||
| 6459 | time, |
||
| 6460 | question_id, |
||
| 6461 | question, |
||
| 6462 | answer, |
||
| 6463 | */ |
||
| 6464 | } |
||
| 6465 | |||
| 6466 | return $data; |
||
| 6467 | } |
||
| 6468 | |||
| 6469 | /** |
||
| 6470 | * @param string $tool |
||
| 6471 | * @param SessionEntity |null $session |
||
| 6472 | * |
||
| 6473 | * @return CStudentPublication|null |
||
| 6474 | */ |
||
| 6475 | public static function getLastStudentPublication( |
||
| 6476 | User $user, |
||
| 6477 | $tool, |
||
| 6478 | Course $course, |
||
| 6479 | SessionEntity $session = null |
||
| 6480 | ) { |
||
| 6481 | // @todo |
||
| 6482 | return null; |
||
| 6483 | |||
| 6484 | return Database::getManager() |
||
| 6485 | ->createQuery(" |
||
| 6486 | SELECT csp |
||
| 6487 | FROM ChamiloCourseBundle:CStudentPublication csp |
||
| 6488 | INNER JOIN ChamiloCourseBundle:CItemProperty cip |
||
| 6489 | WITH ( |
||
| 6490 | csp.iid = cip.ref AND |
||
| 6491 | csp.session = cip.session AND |
||
| 6492 | csp.cId = cip.course AND |
||
| 6493 | csp.userId = cip.lasteditUserId |
||
| 6494 | ) |
||
| 6495 | WHERE |
||
| 6496 | cip.session = :session AND cip.course = :course AND cip.lasteditUserId = :user AND cip.tool = :tool |
||
| 6497 | ORDER BY csp.iid DESC |
||
| 6498 | ") |
||
| 6499 | ->setMaxResults(1) |
||
| 6500 | ->setParameters([ |
||
| 6501 | 'tool' => $tool, |
||
| 6502 | 'session' => $session, |
||
| 6503 | 'course' => $course, |
||
| 6504 | 'user' => $user, |
||
| 6505 | ]) |
||
| 6506 | ->getOneOrNullResult(); |
||
| 6507 | } |
||
| 6508 | |||
| 6509 | /** |
||
| 6510 | * Get the HTML code for show a block with the achieved user skill on course/session. |
||
| 6511 | * |
||
| 6512 | * @param int $userId |
||
| 6513 | * @param int $courseId |
||
| 6514 | * @param int $sessionId |
||
| 6515 | * @param bool $forceView forces the view of the skills, not checking for deeper access |
||
| 6516 | * |
||
| 6517 | * @return string |
||
| 6518 | */ |
||
| 6519 | public static function displayUserSkills($userId, $courseId = 0, $sessionId = 0, $forceView = false) |
||
| 6520 | { |
||
| 6521 | if (false === SkillModel::isAllowed($userId, false) && false == $forceView) { |
||
| 6522 | return ''; |
||
| 6523 | } |
||
| 6524 | $skillManager = new SkillModel(); |
||
| 6525 | |||
| 6526 | return $skillManager->getUserSkillsTable($userId, $courseId, $sessionId)['table']; |
||
| 6527 | } |
||
| 6528 | |||
| 6529 | /** |
||
| 6530 | * @param int $userId |
||
| 6531 | * @param int $courseId |
||
| 6532 | * @param int $sessionId |
||
| 6533 | * |
||
| 6534 | * @return array |
||
| 6535 | */ |
||
| 6536 | public static function getCalculateTime($userId, $courseId, $sessionId) |
||
| 6695 | } |
||
| 6696 | |||
| 6697 | /** |
||
| 6698 | * Gets the IP of a given user, using the last login before the given date. |
||
| 6699 | * |
||
| 6700 | * @param int User ID |
||
| 6701 | * @param string Datetime |
||
| 6702 | * @param bool Whether to return the IP as a link or just as an IP |
||
| 6703 | * @param string If defined and return_as_link if true, will be used as the text to be shown as the link |
||
| 6704 | * |
||
| 6705 | * @return string IP address (or false on error) |
||
| 6706 | * @assert (0,0) === false |
||
| 6707 | */ |
||
| 6708 | public static function get_ip_from_user_event( |
||
| 6709 | $user_id, |
||
| 6710 | $event_date, |
||
| 6711 | $return_as_link = false, |
||
| 6712 | $body_replace = null |
||
| 6713 | ) { |
||
| 6714 | if (empty($user_id) || empty($event_date)) { |
||
| 6715 | return false; |
||
| 6716 | } |
||
| 6717 | $user_id = intval($user_id); |
||
| 6718 | $event_date = Database::escape_string($event_date); |
||
| 6719 | $table_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 6720 | $sql_ip = "SELECT login_date, user_ip |
||
| 6721 | FROM $table_login |
||
| 6722 | WHERE login_user_id = $user_id AND login_date < '$event_date' |
||
| 6723 | ORDER BY login_date DESC LIMIT 1"; |
||
| 6724 | $ip = ''; |
||
| 6725 | $res_ip = Database::query($sql_ip); |
||
| 6726 | if (false !== $res_ip && Database::num_rows($res_ip) > 0) { |
||
| 6727 | $row_ip = Database::fetch_row($res_ip); |
||
| 6728 | if ($return_as_link) { |
||
| 6729 | $ip = Display::url( |
||
| 6730 | (empty($body_replace) ? $row_ip[1] : $body_replace), |
||
| 6731 | 'http://www.whatsmyip.org/ip-geo-location/?ip='.$row_ip[1], |
||
| 6732 | ['title' => get_lang('Trace IP'), 'target' => '_blank'] |
||
| 6733 | ); |
||
| 6734 | } else { |
||
| 6735 | $ip = $row_ip[1]; |
||
| 6736 | } |
||
| 6737 | } |
||
| 6738 | |||
| 6739 | return $ip; |
||
| 6740 | } |
||
| 6741 | |||
| 6742 | /** |
||
| 6743 | * @param int $userId |
||
| 6744 | * @param array $courseInfo |
||
| 6745 | * @param int $sessionId |
||
| 6746 | * |
||
| 6747 | * @return array |
||
| 6748 | */ |
||
| 6749 | public static function getToolInformation( |
||
| 6750 | $userId, |
||
| 6751 | $courseInfo, |
||
| 6752 | $sessionId = 0 |
||
| 6753 | ) { |
||
| 6754 | $csvContent = []; |
||
| 6755 | $courseToolInformation = ''; |
||
| 6756 | $headerTool = [ |
||
| 6757 | [get_lang('Title')], |
||
| 6758 | [get_lang('Created at')], |
||
| 6759 | [get_lang('Updated at')], |
||
| 6760 | ]; |
||
| 6761 | |||
| 6762 | $headerListForCSV = []; |
||
| 6763 | foreach ($headerTool as $item) { |
||
| 6764 | $headerListForCSV[] = $item[0]; |
||
| 6765 | } |
||
| 6766 | |||
| 6767 | $courseForumInformationArray = getForumCreatedByUser( |
||
| 6768 | $userId, |
||
| 6769 | $courseInfo, |
||
| 6770 | $sessionId |
||
| 6771 | ); |
||
| 6772 | |||
| 6773 | if (!empty($courseForumInformationArray)) { |
||
| 6774 | $csvContent[] = []; |
||
| 6775 | $csvContent[] = [get_lang('Forums')]; |
||
| 6776 | $csvContent[] = $headerListForCSV; |
||
| 6777 | foreach ($courseForumInformationArray as $row) { |
||
| 6778 | $csvContent[] = $row; |
||
| 6779 | } |
||
| 6780 | |||
| 6781 | $courseToolInformation .= Display::page_subheader2( |
||
| 6782 | get_lang('Forums') |
||
| 6783 | ); |
||
| 6784 | $courseToolInformation .= Display::return_sortable_table( |
||
| 6785 | $headerTool, |
||
| 6786 | $courseForumInformationArray |
||
| 6787 | ); |
||
| 6788 | } |
||
| 6789 | |||
| 6790 | $courseWorkInformationArray = getWorkCreatedByUser( |
||
| 6791 | $userId, |
||
| 6792 | $courseInfo['real_id'], |
||
| 6793 | $sessionId |
||
| 6794 | ); |
||
| 6795 | |||
| 6796 | if (!empty($courseWorkInformationArray)) { |
||
| 6797 | $csvContent[] = null; |
||
| 6798 | $csvContent[] = [get_lang('Assignments')]; |
||
| 6799 | $csvContent[] = $headerListForCSV; |
||
| 6800 | |||
| 6801 | foreach ($courseWorkInformationArray as $row) { |
||
| 6802 | $csvContent[] = $row; |
||
| 6803 | } |
||
| 6804 | $csvContent[] = null; |
||
| 6805 | |||
| 6806 | $courseToolInformation .= Display::page_subheader2( |
||
| 6807 | get_lang('Assignments') |
||
| 6808 | ); |
||
| 6809 | $courseToolInformation .= Display::return_sortable_table( |
||
| 6810 | $headerTool, |
||
| 6811 | $courseWorkInformationArray |
||
| 6812 | ); |
||
| 6813 | } |
||
| 6814 | |||
| 6815 | $courseToolInformationTotal = null; |
||
| 6816 | if (!empty($courseToolInformation)) { |
||
| 6817 | $sessionTitle = null; |
||
| 6818 | if (!empty($sessionId)) { |
||
| 6819 | $sessionTitle = ' ('.api_get_session_name($sessionId).')'; |
||
| 6820 | } |
||
| 6821 | |||
| 6822 | $courseToolInformationTotal .= Display::page_subheader( |
||
| 6823 | $courseInfo['title'].$sessionTitle |
||
| 6824 | ); |
||
| 6825 | $courseToolInformationTotal .= $courseToolInformation; |
||
| 6826 | } |
||
| 6827 | |||
| 6828 | return [ |
||
| 6829 | 'array' => $csvContent, |
||
| 6830 | 'html' => $courseToolInformationTotal, |
||
| 6831 | ]; |
||
| 6832 | } |
||
| 6833 | |||
| 6834 | /** |
||
| 6835 | * @param int $sessionId |
||
| 6836 | * |
||
| 6837 | * @return bool |
||
| 6838 | */ |
||
| 6839 | public static function isAllowToTrack($sessionId) |
||
| 6840 | { |
||
| 6841 | return |
||
| 6842 | api_is_platform_admin(true, true) || |
||
| 6843 | (!empty($sessionId) && api_get_session_entity($sessionId)->hasUserAsGeneralCoach(api_get_user_entity())) || |
||
| 6844 | api_is_allowed_to_create_course() || |
||
| 6845 | api_is_course_tutor() || |
||
| 6846 | api_is_course_admin(); |
||
| 6847 | } |
||
| 6848 | |||
| 6849 | public static function getCourseLpProgress($userId, $sessionId) |
||
| 6850 | { |
||
| 6851 | $controller = new IndexManager(get_lang('MyCourses')); |
||
| 6852 | $data = $controller->returnCoursesAndSessions($userId); |
||
| 6853 | $courseList = $data['courses']; |
||
| 6854 | $result = []; |
||
| 6855 | if ($courseList) { |
||
| 6856 | //$counter = 1; |
||
| 6857 | foreach ($courseList as $course) { |
||
| 6858 | $courseId = $course['course_id']; |
||
| 6859 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 6860 | if (empty($courseInfo)) { |
||
| 6861 | continue; |
||
| 6862 | } |
||
| 6863 | $courseCode = $courseInfo['code']; |
||
| 6864 | $lpTimeList = self::getCalculateTime($userId, $courseId, $sessionId); |
||
| 6865 | |||
| 6866 | // total progress |
||
| 6867 | $list = new LearnpathList( |
||
| 6868 | $userId, |
||
| 6869 | $courseInfo, |
||
| 6870 | 0, |
||
| 6871 | 'resource.publishedOn ASC', |
||
| 6872 | true, |
||
| 6873 | null, |
||
| 6874 | true |
||
| 6875 | ); |
||
| 6876 | |||
| 6877 | $list = $list->get_flat_list(); |
||
| 6878 | $totalProgress = 0; |
||
| 6879 | $totalTime = 0; |
||
| 6880 | if (!empty($list)) { |
||
| 6881 | foreach ($list as $lp_id => $learnpath) { |
||
| 6882 | if (!$learnpath['lp_visibility']) { |
||
| 6883 | continue; |
||
| 6884 | } |
||
| 6885 | $lpProgress = self::get_avg_student_progress($userId, $courseCode, [$lp_id], $sessionId); |
||
| 6886 | $time = isset($lpTimeList[TOOL_LEARNPATH][$lp_id]) ? $lpTimeList[TOOL_LEARNPATH][$lp_id] : 0; |
||
| 6887 | if (100 == $lpProgress) { |
||
| 6888 | if (!empty($time)) { |
||
| 6889 | $timeInMinutes = $time / 60; |
||
| 6890 | $min = (int) learnpath::getAccumulateWorkTimePrerequisite($lp_id, $courseId); |
||
| 6891 | if ($timeInMinutes >= $min) { |
||
| 6892 | $totalProgress++; |
||
| 6893 | } |
||
| 6894 | } |
||
| 6895 | } |
||
| 6896 | $totalTime += $time; |
||
| 6897 | } |
||
| 6898 | |||
| 6899 | if (!empty($totalProgress)) { |
||
| 6900 | $totalProgress = (float) api_number_format($totalProgress / count($list) * 100, 2); |
||
| 6901 | } |
||
| 6902 | } |
||
| 6903 | |||
| 6904 | $progress = self::get_avg_student_progress($userId, $courseCode, [], $sessionId); |
||
| 6905 | |||
| 6906 | $result[] = [ |
||
| 6907 | 'module' => $courseInfo['name'], |
||
| 6908 | 'progress' => $progress, |
||
| 6909 | 'qualification' => $totalProgress, |
||
| 6910 | 'activeTime' => $totalTime, |
||
| 6911 | ]; |
||
| 6912 | } |
||
| 6913 | } |
||
| 6914 | |||
| 6915 | return $result; |
||
| 6916 | } |
||
| 6917 | |||
| 6918 | /** |
||
| 6919 | * @param int $userId |
||
| 6920 | * @param int $courseId |
||
| 6921 | * @param int $sessionId |
||
| 6922 | * |
||
| 6923 | * @return int |
||
| 6924 | */ |
||
| 6925 | public static function getNumberOfCourseAccessDates($userId, $courseId, $sessionId) |
||
| 6926 | { |
||
| 6927 | $tblTrackCourseAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 6928 | $sessionCondition = api_get_session_condition($sessionId); |
||
| 6929 | $courseId = (int) $courseId; |
||
| 6930 | $userId = (int) $userId; |
||
| 6931 | |||
| 6932 | $sql = "SELECT COUNT(DISTINCT (DATE(login_course_date))) AS c |
||
| 6933 | FROM $tblTrackCourseAccess |
||
| 6934 | WHERE c_id = $courseId $sessionCondition AND user_id = $userId"; |
||
| 6935 | |||
| 6936 | $result = Database::fetch_assoc(Database::query($sql)); |
||
| 6937 | |||
| 6938 | return (int) $result['c']; |
||
| 6939 | } |
||
| 6940 | |||
| 6941 | public static function processUserDataMove( |
||
| 6942 | $user_id, |
||
| 6943 | $course_info, |
||
| 6944 | $origin_session_id, |
||
| 6945 | $new_session_id, |
||
| 6946 | $update_database, |
||
| 6947 | $debug = false |
||
| 6948 | ) { |
||
| 6949 | // Begin with the import process |
||
| 6950 | $origin_course_code = $course_info['code']; |
||
| 6951 | $course_id = $course_info['real_id']; |
||
| 6952 | $user_id = (int) $user_id; |
||
| 6953 | $origin_session_id = (int) $origin_session_id; |
||
| 6954 | $new_session_id = (int) $new_session_id; |
||
| 6955 | $session = api_get_session_entity($new_session_id); |
||
| 6956 | $em = Database::getManager(); |
||
| 6957 | |||
| 6958 | $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 6959 | $TBL_TRACK_ATTEMPT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
| 6960 | $TBL_TRACK_E_COURSE_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 6961 | $TBL_TRACK_E_LAST_ACCESS = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS); |
||
| 6962 | $TBL_LP_VIEW = Database::get_course_table(TABLE_LP_VIEW); |
||
| 6963 | $TBL_NOTEBOOK = Database::get_course_table(TABLE_NOTEBOOK); |
||
| 6964 | $TBL_STUDENT_PUBLICATION = Database::get_course_table(TABLE_STUDENT_PUBLICATION); |
||
| 6965 | $TBL_STUDENT_PUBLICATION_ASSIGNMENT = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT); |
||
| 6966 | $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 6967 | |||
| 6968 | $TBL_DROPBOX_FILE = Database::get_course_table(TABLE_DROPBOX_FILE); |
||
| 6969 | $TBL_DROPBOX_POST = Database::get_course_table(TABLE_DROPBOX_POST); |
||
| 6970 | $TBL_AGENDA = Database::get_course_table(TABLE_AGENDA); |
||
| 6971 | |||
| 6972 | //1. track_e_exercises |
||
| 6973 | //ORIGINAL COURSE |
||
| 6974 | $sessionCondition = api_get_session_condition($origin_session_id); |
||
| 6975 | $sql = "SELECT * FROM $TABLETRACK_EXERCICES |
||
| 6976 | WHERE c_id = $course_id AND exe_user_id = $user_id $sessionCondition"; |
||
| 6977 | $res = Database::query($sql); |
||
| 6978 | $list = []; |
||
| 6979 | while ($row = Database::fetch_assoc($res)) { |
||
| 6980 | $list[$row['exe_id']] = $row; |
||
| 6981 | } |
||
| 6982 | |||
| 6983 | $result_message = []; |
||
| 6984 | $result_message_compare = []; |
||
| 6985 | if (!empty($list)) { |
||
| 6986 | foreach ($list as $exe_id => $data) { |
||
| 6987 | if ($update_database) { |
||
| 6988 | $sql = "UPDATE $TABLETRACK_EXERCICES SET session_id = '$new_session_id' WHERE exe_id = $exe_id"; |
||
| 6989 | Database::query($sql); |
||
| 6990 | |||
| 6991 | //$sql = "UPDATE $TBL_TRACK_ATTEMPT SET session_id = '$new_session_id' WHERE exe_id = $exe_id"; |
||
| 6992 | //Database::query($sql); |
||
| 6993 | |||
| 6994 | $repoTrackQualify = $em->getRepository(TrackEAttemptQualify::class); |
||
| 6995 | /** @var TrackEAttemptQualify $trackQualify */ |
||
| 6996 | $trackQualify = $repoTrackQualify->findBy([ |
||
| 6997 | 'exeId' => $exe_id |
||
| 6998 | ]); |
||
| 6999 | if ($trackQualify) { |
||
| 7000 | $trackQualify->setSessionId($new_session_id); |
||
| 7001 | $em->persist($trackQualify); |
||
| 7002 | $em->flush(); |
||
| 7003 | } |
||
| 7004 | |||
| 7005 | if (!isset($result_message[$TABLETRACK_EXERCICES])) { |
||
| 7006 | $result_message[$TABLETRACK_EXERCICES] = 0; |
||
| 7007 | } |
||
| 7008 | $result_message[$TABLETRACK_EXERCICES]++; |
||
| 7009 | } else { |
||
| 7010 | if (!empty($data['orig_lp_id']) && !empty($data['orig_lp_item_id'])) { |
||
| 7011 | $result_message['TRACK_E_EXERCISES'][$exe_id] = $data; |
||
| 7012 | } else { |
||
| 7013 | $result_message['TRACK_E_EXERCISES_IN_LP'][$exe_id] = $data; |
||
| 7014 | } |
||
| 7015 | } |
||
| 7016 | } |
||
| 7017 | } |
||
| 7018 | |||
| 7019 | // DESTINY COURSE |
||
| 7020 | if (!$update_database) { |
||
| 7021 | $sql = "SELECT * FROM $TABLETRACK_EXERCICES |
||
| 7022 | WHERE |
||
| 7023 | c_id = $course_id AND |
||
| 7024 | session_id = $new_session_id AND |
||
| 7025 | exe_user_id = $user_id "; |
||
| 7026 | $res = Database::query($sql); |
||
| 7027 | $list = []; |
||
| 7028 | while ($row = Database::fetch_assoc($res)) { |
||
| 7029 | $list[$row['exe_id']] = $row; |
||
| 7030 | } |
||
| 7031 | |||
| 7032 | if (!empty($list)) { |
||
| 7033 | foreach ($list as $exe_id => $data) { |
||
| 7034 | if ($update_database) { |
||
| 7035 | $sql = "UPDATE $TABLETRACK_EXERCICES |
||
| 7036 | SET session_id = '$new_session_id' |
||
| 7037 | WHERE exe_id = $exe_id"; |
||
| 7038 | Database::query($sql); |
||
| 7039 | $result_message[$TABLETRACK_EXERCICES]++; |
||
| 7040 | } else { |
||
| 7041 | if (!empty($data['orig_lp_id']) && !empty($data['orig_lp_item_id'])) { |
||
| 7042 | $result_message_compare['TRACK_E_EXERCISES'][$exe_id] = $data; |
||
| 7043 | } else { |
||
| 7044 | $result_message_compare['TRACK_E_EXERCISES_IN_LP'][$exe_id] = $data; |
||
| 7045 | } |
||
| 7046 | } |
||
| 7047 | } |
||
| 7048 | } |
||
| 7049 | } |
||
| 7050 | |||
| 7051 | // 2.track_e_attempt, track_e_attempt_recording, track_e_downloads |
||
| 7052 | // Nothing to do because there are not relationship with a session |
||
| 7053 | // 3. track_e_course_access |
||
| 7054 | $sql = "SELECT * FROM $TBL_TRACK_E_COURSE_ACCESS |
||
| 7055 | WHERE c_id = $course_id AND session_id = $origin_session_id AND user_id = $user_id "; |
||
| 7056 | $res = Database::query($sql); |
||
| 7057 | $list = []; |
||
| 7058 | while ($row = Database::fetch_assoc($res)) { |
||
| 7059 | $list[$row['course_access_id']] = $row; |
||
| 7060 | } |
||
| 7061 | |||
| 7062 | if (!empty($list)) { |
||
| 7063 | foreach ($list as $id => $data) { |
||
| 7064 | if ($update_database) { |
||
| 7065 | $sql = "UPDATE $TBL_TRACK_E_COURSE_ACCESS |
||
| 7066 | SET session_id = $new_session_id |
||
| 7067 | WHERE course_access_id = $id"; |
||
| 7068 | if ($debug) { |
||
| 7069 | echo $sql; |
||
| 7070 | } |
||
| 7071 | Database::query($sql); |
||
| 7072 | if (!isset($result_message[$TBL_TRACK_E_COURSE_ACCESS])) { |
||
| 7073 | $result_message[$TBL_TRACK_E_COURSE_ACCESS] = 0; |
||
| 7074 | } |
||
| 7075 | $result_message[$TBL_TRACK_E_COURSE_ACCESS]++; |
||
| 7076 | } |
||
| 7077 | } |
||
| 7078 | } |
||
| 7079 | |||
| 7080 | // 4. track_e_lastaccess |
||
| 7081 | $sql = "SELECT access_id FROM $TBL_TRACK_E_LAST_ACCESS |
||
| 7082 | WHERE |
||
| 7083 | c_id = $course_id AND |
||
| 7084 | session_id = $origin_session_id AND |
||
| 7085 | access_user_id = $user_id "; |
||
| 7086 | $res = Database::query($sql); |
||
| 7087 | $list = []; |
||
| 7088 | while ($row = Database::fetch_assoc($res)) { |
||
| 7089 | $list[] = $row['access_id']; |
||
| 7090 | } |
||
| 7091 | |||
| 7092 | if (!empty($list)) { |
||
| 7093 | foreach ($list as $id) { |
||
| 7094 | if ($update_database) { |
||
| 7095 | $sql = "UPDATE $TBL_TRACK_E_LAST_ACCESS |
||
| 7096 | SET session_id = $new_session_id |
||
| 7097 | WHERE access_id = $id"; |
||
| 7098 | if ($debug) { |
||
| 7099 | echo $sql; |
||
| 7100 | } |
||
| 7101 | Database::query($sql); |
||
| 7102 | if (!isset($result_message[$TBL_TRACK_E_LAST_ACCESS])) { |
||
| 7103 | $result_message[$TBL_TRACK_E_LAST_ACCESS] = 0; |
||
| 7104 | } |
||
| 7105 | $result_message[$TBL_TRACK_E_LAST_ACCESS]++; |
||
| 7106 | } |
||
| 7107 | } |
||
| 7108 | } |
||
| 7109 | |||
| 7110 | // 5. lp_item_view |
||
| 7111 | // CHECK ORIGIN |
||
| 7112 | $sql = "SELECT * FROM $TBL_LP_VIEW |
||
| 7113 | WHERE user_id = $user_id AND session_id = $origin_session_id AND c_id = $course_id "; |
||
| 7114 | $res = Database::query($sql); |
||
| 7115 | |||
| 7116 | // Getting the list of LPs in the new session |
||
| 7117 | $lp_list = new LearnpathList($user_id, $course_info, $new_session_id); |
||
| 7118 | $flat_list = $lp_list->get_flat_list(); |
||
| 7119 | $list = []; |
||
| 7120 | while ($row = Database::fetch_assoc($res)) { |
||
| 7121 | // Checking if the LP exist in the new session |
||
| 7122 | //if (in_array($row['lp_id'], array_keys($flat_list))) { |
||
| 7123 | $list[$row['id']] = $row; |
||
| 7124 | //} |
||
| 7125 | } |
||
| 7126 | |||
| 7127 | if (!empty($list)) { |
||
| 7128 | foreach ($list as $id => $data) { |
||
| 7129 | if ($update_database) { |
||
| 7130 | $sql = "UPDATE $TBL_LP_VIEW |
||
| 7131 | SET session_id = $new_session_id |
||
| 7132 | WHERE c_id = $course_id AND iid = $id "; |
||
| 7133 | if ($debug) { |
||
| 7134 | var_dump($sql); |
||
| 7135 | } |
||
| 7136 | $res = Database::query($sql); |
||
| 7137 | if ($debug) { |
||
| 7138 | var_dump($res); |
||
| 7139 | } |
||
| 7140 | if (!isset($result_message[$TBL_LP_VIEW])) { |
||
| 7141 | $result_message[$TBL_LP_VIEW] = 0; |
||
| 7142 | } |
||
| 7143 | $result_message[$TBL_LP_VIEW]++; |
||
| 7144 | } else { |
||
| 7145 | // Getting all information of that lp_item_id |
||
| 7146 | $score = self::get_avg_student_score( |
||
| 7147 | $user_id, |
||
| 7148 | $origin_course_code, |
||
| 7149 | [$data['lp_id']], |
||
| 7150 | $origin_session_id |
||
| 7151 | ); |
||
| 7152 | $progress = self::get_avg_student_progress( |
||
| 7153 | $user_id, |
||
| 7154 | $origin_course_code, |
||
| 7155 | [$data['lp_id']], |
||
| 7156 | $origin_session_id |
||
| 7157 | ); |
||
| 7158 | $result_message['LP_VIEW'][$data['lp_id']] = [ |
||
| 7159 | 'score' => $score, |
||
| 7160 | 'progress' => $progress, |
||
| 7161 | ]; |
||
| 7162 | } |
||
| 7163 | } |
||
| 7164 | } |
||
| 7165 | |||
| 7166 | // Check destination. |
||
| 7167 | if (!$update_database) { |
||
| 7168 | $sql = "SELECT * FROM $TBL_LP_VIEW |
||
| 7169 | WHERE user_id = $user_id AND session_id = $new_session_id AND c_id = $course_id"; |
||
| 7170 | $res = Database::query($sql); |
||
| 7171 | |||
| 7172 | // Getting the list of LPs in the new session |
||
| 7173 | $lp_list = new LearnpathList($user_id, $course_info, $new_session_id); |
||
| 7174 | $flat_list = $lp_list->get_flat_list(); |
||
| 7175 | |||
| 7176 | $list = []; |
||
| 7177 | while ($row = Database::fetch_assoc($res)) { |
||
| 7178 | //Checking if the LP exist in the new session |
||
| 7179 | //if (in_array($row['lp_id'], array_keys($flat_list))) { |
||
| 7180 | $list[$row['id']] = $row; |
||
| 7181 | //} |
||
| 7182 | } |
||
| 7183 | |||
| 7184 | if (!empty($list)) { |
||
| 7185 | foreach ($list as $id => $data) { |
||
| 7186 | // Getting all information of that lp_item_id |
||
| 7187 | $score = self::get_avg_student_score( |
||
| 7188 | $user_id, |
||
| 7189 | $origin_course_code, |
||
| 7190 | [$data['lp_id']], |
||
| 7191 | $new_session_id |
||
| 7192 | ); |
||
| 7193 | $progress = self::get_avg_student_progress( |
||
| 7194 | $user_id, |
||
| 7195 | $origin_course_code, |
||
| 7196 | [$data['lp_id']], |
||
| 7197 | $new_session_id |
||
| 7198 | ); |
||
| 7199 | $result_message_compare['LP_VIEW'][$data['lp_id']] = [ |
||
| 7200 | 'score' => $score, |
||
| 7201 | 'progress' => $progress, |
||
| 7202 | ]; |
||
| 7203 | } |
||
| 7204 | } |
||
| 7205 | } |
||
| 7206 | |||
| 7207 | // 6. Agenda |
||
| 7208 | // calendar_event_attachment no problems no session_id |
||
| 7209 | $sql = "SELECT ref FROM $TBL_ITEM_PROPERTY |
||
| 7210 | WHERE tool = 'calendar_event' AND insert_user_id = $user_id AND c_id = $course_id "; |
||
| 7211 | $res = Database::query($sql); |
||
| 7212 | while ($row = Database::fetch_assoc($res)) { |
||
| 7213 | $id = $row['ref']; |
||
| 7214 | if ($update_database) { |
||
| 7215 | $sql = "UPDATE $TBL_AGENDA SET session_id = $new_session_id WHERE c_id = $course_id AND iid = $id "; |
||
| 7216 | if ($debug) { |
||
| 7217 | var_dump($sql); |
||
| 7218 | } |
||
| 7219 | $res_update = Database::query($sql); |
||
| 7220 | if ($debug) { |
||
| 7221 | var_dump($res_update); |
||
| 7222 | } |
||
| 7223 | if (!isset($result_message['agenda'])) { |
||
| 7224 | $result_message['agenda'] = 0; |
||
| 7225 | } |
||
| 7226 | $result_message['agenda']++; |
||
| 7227 | } |
||
| 7228 | } |
||
| 7229 | |||
| 7230 | // 7. Forum ?? So much problems when trying to import data |
||
| 7231 | // 8. Student publication - Works |
||
| 7232 | $sql = "SELECT ref FROM $TBL_ITEM_PROPERTY |
||
| 7233 | WHERE tool = 'work' AND insert_user_id = $user_id AND c_id = $course_id"; |
||
| 7234 | if ($debug) { |
||
| 7235 | echo $sql; |
||
| 7236 | } |
||
| 7237 | $res = Database::query($sql); |
||
| 7238 | while ($row = Database::fetch_assoc($res)) { |
||
| 7239 | $id = $row['ref']; |
||
| 7240 | $sql = "SELECT * FROM $TBL_STUDENT_PUBLICATION |
||
| 7241 | WHERE iid = $id AND session_id = $origin_session_id AND c_id = $course_id"; |
||
| 7242 | $sub_res = Database::query($sql); |
||
| 7243 | if (Database::num_rows($sub_res) > 0) { |
||
| 7244 | $data = Database::fetch_assoc($sub_res); |
||
| 7245 | if ($debug) { |
||
| 7246 | var_dump($data); |
||
| 7247 | } |
||
| 7248 | $parent_id = $data['parent_id']; |
||
| 7249 | if (isset($data['parent_id']) && !empty($data['parent_id'])) { |
||
| 7250 | $sql = "SELECT * FROM $TBL_STUDENT_PUBLICATION |
||
| 7251 | WHERE iid = $parent_id AND c_id = $course_id"; |
||
| 7252 | $select_res = Database::query($sql); |
||
| 7253 | $parent_data = Database::fetch_assoc($select_res); |
||
| 7254 | if ($debug) { |
||
| 7255 | var_dump($parent_data); |
||
| 7256 | } |
||
| 7257 | |||
| 7258 | $sys_course_path = api_get_path(SYS_COURSE_PATH); |
||
| 7259 | $course_dir = $sys_course_path.$course_info['path']; |
||
| 7260 | $base_work_dir = $course_dir.'/work'; |
||
| 7261 | |||
| 7262 | // Creating the parent folder in the session if does not exists already |
||
| 7263 | //@todo ugly fix |
||
| 7264 | $search_this = "folder_moved_from_session_id_$origin_session_id"; |
||
| 7265 | $search_this2 = $parent_data['url']; |
||
| 7266 | $sql = "SELECT * FROM $TBL_STUDENT_PUBLICATION |
||
| 7267 | WHERE description like '%$search_this%' AND |
||
| 7268 | url LIKE '%$search_this2%' AND |
||
| 7269 | session_id = $new_session_id AND |
||
| 7270 | c_id = $course_id |
||
| 7271 | ORDER BY id desc LIMIT 1"; |
||
| 7272 | if ($debug) { |
||
| 7273 | echo $sql; |
||
| 7274 | } |
||
| 7275 | $sub_res = Database::query($sql); |
||
| 7276 | $num_rows = Database::num_rows($sub_res); |
||
| 7277 | |||
| 7278 | $new_parent_id = 0; |
||
| 7279 | if ($num_rows > 0) { |
||
| 7280 | $new_result = Database::fetch_assoc($sub_res); |
||
| 7281 | $created_dir = $new_result['url']; |
||
| 7282 | $new_parent_id = $new_result['id']; |
||
| 7283 | } else { |
||
| 7284 | if ($update_database) { |
||
| 7285 | $dir_name = substr($parent_data['url'], 1); |
||
| 7286 | $created_dir = create_unexisting_work_directory($base_work_dir, $dir_name); |
||
| 7287 | $created_dir = '/'.$created_dir; |
||
| 7288 | $now = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC')); |
||
| 7289 | // Creating directory |
||
| 7290 | $publication = (new CStudentPublication()) |
||
| 7291 | ->setTitle($parent_data['title']) |
||
| 7292 | ->setDescription( |
||
| 7293 | $parent_data['description']."folder_moved_from_session_id_$origin_session_id" |
||
| 7294 | ) |
||
| 7295 | ->setActive(false) |
||
| 7296 | ->setAccepted(true) |
||
| 7297 | ->setPostGroupId(0) |
||
| 7298 | ->setHasProperties($parent_data['has_properties']) |
||
| 7299 | ->setWeight($parent_data['weight']) |
||
| 7300 | ->setContainsFile($parent_data['contains_file']) |
||
| 7301 | ->setFiletype('folder') |
||
| 7302 | ->setSentDate($now) |
||
| 7303 | ->setQualification($parent_data['qualification']) |
||
| 7304 | ->setParentId(0) |
||
| 7305 | ->setQualificatorId(0) |
||
| 7306 | ->setUserId($parent_data['user_id']) |
||
| 7307 | ->setAllowTextAssignment($parent_data['allow_text_assignment']) |
||
| 7308 | ->setSession($session); |
||
| 7309 | |||
| 7310 | $publication->setDocumentId($parent_data['document_id']); |
||
| 7311 | |||
| 7312 | Database::getManager()->persist($publication); |
||
| 7313 | Database::getManager()->flush(); |
||
| 7314 | $id = $publication->getIid(); |
||
| 7315 | //Folder created |
||
| 7316 | //api_item_property_update($course_info, 'work', $id, 'DirectoryCreated', api_get_user_id()); |
||
| 7317 | $new_parent_id = $id; |
||
| 7318 | if (!isset($result_message[$TBL_STUDENT_PUBLICATION.' - new folder created called: '.$created_dir])) { |
||
| 7319 | $result_message[$TBL_STUDENT_PUBLICATION.' - new folder created called: '.$created_dir] = 0; |
||
| 7320 | } |
||
| 7321 | $result_message[$TBL_STUDENT_PUBLICATION.' - new folder created called: '.$created_dir]++; |
||
| 7322 | } |
||
| 7323 | } |
||
| 7324 | |||
| 7325 | //Creating student_publication_assignment if exists |
||
| 7326 | $sql = "SELECT * FROM $TBL_STUDENT_PUBLICATION_ASSIGNMENT |
||
| 7327 | WHERE publication_id = $parent_id AND c_id = $course_id"; |
||
| 7328 | if ($debug) { |
||
| 7329 | var_dump($sql); |
||
| 7330 | } |
||
| 7331 | $rest_select = Database::query($sql); |
||
| 7332 | if (Database::num_rows($rest_select) > 0) { |
||
| 7333 | if ($update_database && $new_parent_id) { |
||
| 7334 | $assignment_data = Database::fetch_assoc($rest_select); |
||
| 7335 | $sql_add_publication = "INSERT INTO ".$TBL_STUDENT_PUBLICATION_ASSIGNMENT." SET |
||
| 7336 | c_id = '$course_id', |
||
| 7337 | expires_on = '".$assignment_data['expires_on']."', |
||
| 7338 | ends_on = '".$assignment_data['ends_on']."', |
||
| 7339 | add_to_calendar = '".$assignment_data['add_to_calendar']."', |
||
| 7340 | enable_qualification = '".$assignment_data['enable_qualification']."', |
||
| 7341 | publication_id = '".$new_parent_id."'"; |
||
| 7342 | if ($debug) { |
||
| 7343 | echo $sql_add_publication; |
||
| 7344 | } |
||
| 7345 | Database::query($sql_add_publication); |
||
| 7346 | $id = (int) Database::insert_id(); |
||
| 7347 | if ($id) { |
||
| 7348 | $sql_update = "UPDATE $TBL_STUDENT_PUBLICATION |
||
| 7349 | SET has_properties = '".$id."', |
||
| 7350 | view_properties = '1' |
||
| 7351 | WHERE iid = ".$new_parent_id; |
||
| 7352 | if ($debug) { |
||
| 7353 | echo $sql_update; |
||
| 7354 | } |
||
| 7355 | Database::query($sql_update); |
||
| 7356 | if (!isset($result_message[$TBL_STUDENT_PUBLICATION_ASSIGNMENT])) { |
||
| 7357 | $result_message[$TBL_STUDENT_PUBLICATION_ASSIGNMENT] = 0; |
||
| 7358 | } |
||
| 7359 | $result_message[$TBL_STUDENT_PUBLICATION_ASSIGNMENT]++; |
||
| 7360 | } |
||
| 7361 | } |
||
| 7362 | } |
||
| 7363 | |||
| 7364 | $doc_url = $data['url']; |
||
| 7365 | $new_url = str_replace($parent_data['url'], $created_dir, $doc_url); |
||
| 7366 | |||
| 7367 | if ($update_database) { |
||
| 7368 | // Creating a new work |
||
| 7369 | $data['sent_date'] = new DateTime($data['sent_date'], new DateTimeZone('UTC')); |
||
| 7370 | |||
| 7371 | $data['post_group_id'] = (int) $data['post_group_id']; |
||
| 7372 | $publication = (new CStudentPublication()) |
||
| 7373 | ->setTitle($data['title']) |
||
| 7374 | ->setDescription($data['description'].' file moved') |
||
| 7375 | ->setActive($data['active']) |
||
| 7376 | ->setAccepted($data['accepted']) |
||
| 7377 | ->setPostGroupId($data['post_group_id']) |
||
| 7378 | ->setSentDate($data['sent_date']) |
||
| 7379 | ->setParentId($new_parent_id) |
||
| 7380 | ->setWeight($data['weight']) |
||
| 7381 | ->setHasProperties(0) |
||
| 7382 | ->setWeight($data['weight']) |
||
| 7383 | ->setContainsFile($data['contains_file']) |
||
| 7384 | ->setSession($session) |
||
| 7385 | ->setUserId($data['user_id']) |
||
| 7386 | ->setFiletype('file') |
||
| 7387 | ->setDocumentId(0) |
||
| 7388 | ; |
||
| 7389 | |||
| 7390 | $em->persist($publication); |
||
| 7391 | $em->flush(); |
||
| 7392 | |||
| 7393 | $id = $publication->getIid(); |
||
| 7394 | /*api_item_property_update( |
||
| 7395 | $course_info, |
||
| 7396 | 'work', |
||
| 7397 | $id, |
||
| 7398 | 'DocumentAdded', |
||
| 7399 | $user_id, |
||
| 7400 | null, |
||
| 7401 | null, |
||
| 7402 | null, |
||
| 7403 | null, |
||
| 7404 | $new_session_id |
||
| 7405 | );*/ |
||
| 7406 | if (!isset($result_message[$TBL_STUDENT_PUBLICATION])) { |
||
| 7407 | $result_message[$TBL_STUDENT_PUBLICATION] = 0; |
||
| 7408 | } |
||
| 7409 | $result_message[$TBL_STUDENT_PUBLICATION]++; |
||
| 7410 | $full_file_name = $course_dir.'/'.$doc_url; |
||
| 7411 | $new_file = $course_dir.'/'.$new_url; |
||
| 7412 | |||
| 7413 | if (file_exists($full_file_name)) { |
||
| 7414 | // deleting old assignment |
||
| 7415 | $result = copy($full_file_name, $new_file); |
||
| 7416 | if ($result) { |
||
| 7417 | unlink($full_file_name); |
||
| 7418 | if (isset($data['id'])) { |
||
| 7419 | $sql = "DELETE FROM $TBL_STUDENT_PUBLICATION WHERE id= ".$data['id']; |
||
| 7420 | if ($debug) { |
||
| 7421 | var_dump($sql); |
||
| 7422 | } |
||
| 7423 | Database::query($sql); |
||
| 7424 | } |
||
| 7425 | api_item_property_update( |
||
| 7426 | $course_info, |
||
| 7427 | 'work', |
||
| 7428 | $data['id'], |
||
| 7429 | 'DocumentDeleted', |
||
| 7430 | api_get_user_id() |
||
| 7431 | ); |
||
| 7432 | } |
||
| 7433 | } |
||
| 7434 | } |
||
| 7435 | } |
||
| 7436 | } |
||
| 7437 | } |
||
| 7438 | |||
| 7439 | //9. Survey Pending |
||
| 7440 | //10. Dropbox - not neccesary to move categories (no presence of session_id) |
||
| 7441 | $sql = "SELECT id FROM $TBL_DROPBOX_FILE |
||
| 7442 | WHERE uploader_id = $user_id AND session_id = $origin_session_id AND c_id = $course_id"; |
||
| 7443 | if ($debug) { |
||
| 7444 | var_dump($sql); |
||
| 7445 | } |
||
| 7446 | $res = Database::query($sql); |
||
| 7447 | while ($row = Database::fetch_assoc($res)) { |
||
| 7448 | $id = (int) $row['id']; |
||
| 7449 | if ($update_database) { |
||
| 7450 | $sql = "UPDATE $TBL_DROPBOX_FILE SET session_id = $new_session_id WHERE c_id = $course_id AND iid = $id"; |
||
| 7451 | if ($debug) { |
||
| 7452 | var_dump($sql); |
||
| 7453 | } |
||
| 7454 | Database::query($sql); |
||
| 7455 | if ($debug) { |
||
| 7456 | var_dump($res); |
||
| 7457 | } |
||
| 7458 | |||
| 7459 | $sql = "UPDATE $TBL_DROPBOX_POST SET session_id = $new_session_id WHERE file_id = $id"; |
||
| 7460 | if ($debug) { |
||
| 7461 | var_dump($sql); |
||
| 7462 | } |
||
| 7463 | Database::query($sql); |
||
| 7464 | if ($debug) { |
||
| 7465 | var_dump($res); |
||
| 7466 | } |
||
| 7467 | if (!isset($result_message[$TBL_DROPBOX_FILE])) { |
||
| 7468 | $result_message[$TBL_DROPBOX_FILE] = 0; |
||
| 7469 | } |
||
| 7470 | $result_message[$TBL_DROPBOX_FILE]++; |
||
| 7471 | } |
||
| 7472 | } |
||
| 7473 | |||
| 7474 | // 11. Notebook |
||
| 7475 | /*$sql = "SELECT notebook_id FROM $TBL_NOTEBOOK |
||
| 7476 | WHERE |
||
| 7477 | user_id = $user_id AND |
||
| 7478 | session_id = $origin_session_id AND |
||
| 7479 | course = '$origin_course_code' AND |
||
| 7480 | c_id = $course_id"; |
||
| 7481 | if ($debug) { |
||
| 7482 | var_dump($sql); |
||
| 7483 | } |
||
| 7484 | $res = Database::query($sql); |
||
| 7485 | while ($row = Database::fetch_assoc($res)) { |
||
| 7486 | $id = $row['notebook_id']; |
||
| 7487 | if ($update_database) { |
||
| 7488 | $sql = "UPDATE $TBL_NOTEBOOK |
||
| 7489 | SET session_id = $new_session_id |
||
| 7490 | WHERE c_id = $course_id AND notebook_id = $id"; |
||
| 7491 | if ($debug) { |
||
| 7492 | var_dump($sql); |
||
| 7493 | } |
||
| 7494 | $res = Database::query($sql); |
||
| 7495 | if ($debug) { |
||
| 7496 | var_dump($res); |
||
| 7497 | } |
||
| 7498 | } |
||
| 7499 | }*/ |
||
| 7500 | |||
| 7501 | if ($update_database) { |
||
| 7502 | echo Display::return_message(get_lang('StatsMoved')); |
||
| 7503 | if (is_array($result_message)) { |
||
| 7504 | foreach ($result_message as $table => $times) { |
||
| 7505 | echo 'Table '.$table.' - '.$times.' records updated <br />'; |
||
| 7506 | } |
||
| 7507 | } |
||
| 7508 | } else { |
||
| 7509 | echo '<h4>'.get_lang('UserInformationOfThisCourse').'</h4>'; |
||
| 7510 | echo '<br />'; |
||
| 7511 | echo '<table class="table" width="100%">'; |
||
| 7512 | echo '<tr>'; |
||
| 7513 | echo '<td width="50%" valign="top">'; |
||
| 7514 | |||
| 7515 | if (0 == $origin_session_id) { |
||
| 7516 | echo '<h5>'.get_lang('OriginCourse').'</h5>'; |
||
| 7517 | } else { |
||
| 7518 | echo '<h5>'.get_lang('OriginSession').' #'.$origin_session_id.'</h5>'; |
||
| 7519 | } |
||
| 7520 | self::compareUserData($result_message); |
||
| 7521 | echo '</td>'; |
||
| 7522 | echo '<td width="50%" valign="top">'; |
||
| 7523 | if (0 == $new_session_id) { |
||
| 7524 | echo '<h5>'.get_lang('DestinyCourse').'</h5>'; |
||
| 7525 | } else { |
||
| 7526 | echo '<h5>'.get_lang('DestinySession').' #'.$new_session_id.'</h5>'; |
||
| 7527 | } |
||
| 7528 | self::compareUserData($result_message_compare); |
||
| 7529 | echo '</td>'; |
||
| 7530 | echo '</tr>'; |
||
| 7531 | echo '</table>'; |
||
| 7532 | } |
||
| 7533 | } |
||
| 7534 | |||
| 7535 | public static function compareUserData($result_message) |
||
| 7582 | } |
||
| 7583 | } |
||
| 7584 | } |
||
| 7585 | |||
| 7586 | private static function generateQuizzesTable(array $courseInfo, int $sessionId = 0): string |
||
| 7587 | { |
||
| 7588 | if (empty($sessionId)) { |
||
| 7589 | $userList = CourseManager::get_user_list_from_course_code( |
||
| 7590 | $courseInfo['code'], |
||
| 7591 | $sessionId, |
||
| 7592 | null, |
||
| 7593 | null, |
||
| 7594 | STUDENT |
||
| 7595 | ); |
||
| 7596 | } else { |
||
| 7597 | $userList = CourseManager::get_user_list_from_course_code($courseInfo['code'], $sessionId, null, null, 0); |
||
| 7598 | } |
||
| 7599 | |||
| 7600 | $exerciseList = ExerciseLib::get_all_exercises($courseInfo, $sessionId, false, null); |
||
| 7601 | |||
| 7602 | if (empty($exerciseList)) { |
||
| 7603 | return Display::return_message(get_lang('NoEx')); |
||
| 7604 | } |
||
| 7605 | |||
| 7606 | $toGraphExerciseResult = []; |
||
| 7607 | |||
| 7608 | $quizzesTable = new SortableTableFromArray([], 0, 0, 'quizzes'); |
||
| 7609 | $quizzesTable->setHeaders( |
||
| 7610 | [ |
||
| 7611 | get_lang('Exercises'), |
||
| 7612 | get_lang('Attempts'), |
||
| 7613 | get_lang('BestAttempt'), |
||
| 7614 | get_lang('Ranking'), |
||
| 7615 | get_lang('BestResultInCourse'), |
||
| 7616 | get_lang('Statistics').Display::getMdiIcon( |
||
| 7759 | ); |
||
| 7760 | } |
||
| 7761 | |||
| 7762 | private static function generateLearningPathsTable(int $userId, array $courseInfo, int $sessionId = 0): string |
||
| 7763 | { |
||
| 7764 | $columnHeaders = [ |
||
| 7765 | 'lp' => get_lang('LearningPath'), |
||
| 7766 | 'time' => get_lang('LatencyTimeSpent'), |
||
| 7767 | 'progress' => get_lang('Progress'), |
||
| 7768 | 'score' => get_lang('Score'), |
||
| 7959 | ); |
||
| 7960 | } |
||
| 7961 | } |
||
| 7962 | |||
| 7963 | /** |
||
| 7964 | * @todo move into a proper file |
||
| 7965 | */ |
||
| 7966 | class TrackingCourseLog |
||
| 7967 | { |
||
| 7968 | /** |
||
| 9224 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.