tracking.lib.php 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the tracking library for Dokeos.
  5. * Include/require it in your code to use its functionality.
  6. *
  7. * @package chamilo.library
  8. * @author Julio Montoya <gugli100@gmail.com> (Score average fixes)
  9. */
  10. class Tracking {
  11. /**
  12. * Calculates the time spent on the platform by a user
  13. * @param int User id
  14. * @param bool True for calculating only time spent last week (optional)
  15. * @param int Timestamp for filtering by Day (optional, default = 0)
  16. * @return timestamp $nb_seconds
  17. */
  18. public static function get_time_spent_on_the_platform($user_id, $last_week = false, $by_day = 0) {
  19. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  20. $condition_time = '';
  21. if ($last_week) {
  22. $a_last_week = get_last_week();
  23. $fday_last_week = date('Y-m-d H:i:s',$a_last_week[0]);
  24. $lday_last_week = date('Y-m-d H:i:s',$a_last_week[6]);
  25. $condition_time = ' AND (login_date >= "'.$fday_last_week.'" AND logout_date <= "'.$lday_last_week.'") ';
  26. } else if (!empty($by_day)) {
  27. $fdate_time = date('Y-m-d',$by_day).' 00:00:00';
  28. $ldate_time = date('Y-m-d',$by_day).' 23:59:59';
  29. $condition_time = ' AND (login_date >= "'.$fdate_time.'" AND logout_date <= "'.$ldate_time.'" ) ';
  30. }
  31. $sql = 'SELECT login_date, logout_date FROM '.$tbl_track_login.'
  32. WHERE login_user_id = '.intval($user_id).$condition_time;
  33. $rs = Database::query($sql);
  34. $nb_seconds = 0;
  35. $wrong_logout_dates = false;
  36. while ($a_connections = Database::fetch_array($rs)) {
  37. $s_login_date = $a_connections["login_date"];
  38. $s_logout_date = $a_connections["logout_date"];
  39. $i_timestamp_login_date = strtotime($s_login_date);
  40. $i_timestamp_logout_date = strtotime($s_logout_date);
  41. if($i_timestamp_logout_date>0)
  42. {
  43. $nb_seconds += ($i_timestamp_logout_date - $i_timestamp_login_date);
  44. }
  45. else
  46. { // there are wrong datas in db, then we can't give a wrong time
  47. $wrong_logout_dates = true;
  48. }
  49. }
  50. if($nb_seconds>0 || !$wrong_logout_dates)
  51. {
  52. return $nb_seconds;
  53. }
  54. else
  55. {
  56. return -1; //-1 means we have wrong datas in the db
  57. }
  58. }
  59. /**
  60. * Calculates the time spent on the course
  61. * @param integer User id
  62. * @param string Course code
  63. * @param int Session id (optional)
  64. * @return timestamp Time in seconds
  65. */
  66. public static function get_time_spent_on_the_course($user_id, $course_code, $session_id = 0) {
  67. // protect datas
  68. $course_code = Database::escape_string($course_code);
  69. $session_id = intval($session_id);
  70. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  71. $condition_user = "";
  72. if (is_array($user_id)) {
  73. $condition_user = " AND user_id IN (".implode(',',$user_id).") ";
  74. } else {
  75. $user_id = intval($user_id);
  76. $condition_user = " AND user_id = '$user_id' ";
  77. }
  78. $sql = " SELECT SUM(UNIX_TIMESTAMP(logout_course_date)-UNIX_TIMESTAMP(login_course_date)) as nb_seconds
  79. FROM $tbl_track_course
  80. WHERE course_code='$course_code' AND session_id = '$session_id' $condition_user";
  81. $rs = Database::query($sql);
  82. $row = Database::fetch_array($rs);
  83. return $row['nb_seconds'];
  84. }
  85. /**
  86. * Get first connection date for a student
  87. * @param int Student id
  88. * @return string|bool Date format long without day or false if there are no connections
  89. */
  90. public static function get_first_connection_date($student_id) {
  91. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  92. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  93. WHERE login_user_id = ' . intval($student_id) . '
  94. ORDER BY login_date ASC LIMIT 0,1';
  95. $rs = Database::query($sql);
  96. if(Database::num_rows($rs)>0) {
  97. if ($first_login_date = Database::result($rs, 0, 0)) {
  98. return api_convert_and_format_date($first_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  99. }
  100. }
  101. return false;
  102. }
  103. /**
  104. * Get las connection date for a student
  105. * @param int Student id
  106. * @param bool Show a warning message (optional)
  107. * @param bool True for returning results in timestamp (optional)
  108. * @return string|int|bool Date format long without day, false if there are no connections or timestamp if parameter $return_timestamp is true
  109. */
  110. public static function get_last_connection_date($student_id, $warning_message = false, $return_timestamp = false) {
  111. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  112. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  113. WHERE login_user_id = ' . intval($student_id) . '
  114. ORDER BY login_date DESC LIMIT 0,1';
  115. $rs = Database::query($sql);
  116. if(Database::num_rows($rs)>0) {
  117. if ($last_login_date = Database::result($rs, 0, 0)) {
  118. $last_login_date = api_get_local_time($last_login_date);
  119. if ($return_timestamp) {
  120. return api_strtotime($last_login_date,'UTC');
  121. } else {
  122. if (!$warning_message) {
  123. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  124. } else {
  125. $timestamp = api_strtotime($last_login_date,'UTC');
  126. $currentTimestamp = time();
  127. //If the last connection is > than 7 days, the text is red
  128. //345600 = 7 days in seconds
  129. if ($currentTimestamp - $timestamp > 604800)
  130. {
  131. return '<span style="color: #F00;">' . api_format_date($last_login_date, DATE_FORMAT_SHORT) . '</span>';
  132. }
  133. else
  134. {
  135. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * Get first user's connection date on the course
  145. * @param int User id
  146. * @param string Course code
  147. * @param int Session id (optional, default=0)
  148. * @return string|bool Date with format long without day or false if there is no date
  149. */
  150. public static function get_first_connection_date_on_the_course($student_id, $course_code, $session_id = 0, $convert_date = true) {
  151. // protect data
  152. $student_id = intval($student_id);
  153. $course_code = Database::escape_string($course_code);
  154. $session_id = intval($session_id);
  155. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  156. $sql = 'SELECT login_course_date FROM '.$tbl_track_login.'
  157. WHERE user_id = '.$student_id.'
  158. AND course_code = "'.$course_code.'"
  159. AND session_id = '.$session_id.'
  160. ORDER BY login_course_date ASC LIMIT 0,1';
  161. $rs = Database::query($sql);
  162. if (Database::num_rows($rs)>0) {
  163. if ($first_login_date = Database::result($rs, 0, 0)) {
  164. if ($convert_date) {
  165. return api_convert_and_format_date($first_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  166. } else {
  167. return $first_login_date;
  168. }
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * Get last user's connection date on the course
  175. * @param int User id
  176. * @param string Course code
  177. * @param int Session id (optional, default=0)
  178. * @return string|bool Date with format long without day or false if there is no date
  179. */
  180. public static function get_last_connection_date_on_the_course($student_id, $course_code, $session_id = 0, $convert_date = true) {
  181. // protect data
  182. $student_id = intval($student_id);
  183. $course_code = Database::escape_string($course_code);
  184. $session_id = intval($session_id);
  185. $tbl_track_e_course_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  186. $sql = 'SELECT login_course_date FROM '.$tbl_track_e_course_access.'
  187. WHERE user_id = '.$student_id.'
  188. AND course_code = "'.$course_code.'"
  189. AND session_id = '.$session_id.'
  190. ORDER BY login_course_date DESC LIMIT 0,1';
  191. $rs = Database::query($sql);
  192. if (Database::num_rows($rs)>0) {
  193. if ($last_login_date = Database::result($rs, 0, 0)) {
  194. $last_login_date = api_get_local_time($last_login_date, null, date_default_timezone_get());
  195. $timestamp = api_strtotime($last_login_date);
  196. $currentTimestamp = time();
  197. //If the last connection is > than 7 days, the text is red
  198. //345600 = 7 days in seconds
  199. if ($currentTimestamp - $timestamp > 604800) {
  200. if ($convert_date) {
  201. return '<span style="color: #F00;">' . api_format_date($last_login_date, DATE_FORMAT_SHORT) . (api_is_allowed_to_edit()?' <a href="'.api_get_path(REL_CODE_PATH).'announcements/announcements.php?action=add&remind_inactive='.$student_id.'" title="'.get_lang('RemindInactiveUser').'"><img align="middle" src="'.api_get_path(WEB_IMG_PATH).'messagebox_warning.gif" /></a>':'').'</span>';
  202. } else {
  203. return $last_login_date;
  204. }
  205. } else {
  206. if ($convert_date) {
  207. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  208. } else {
  209. return $last_login_date;
  210. }
  211. }
  212. }
  213. }
  214. return false;
  215. }
  216. /**
  217. * Get count connections on the course
  218. * @param string Course code
  219. * @param int Session id (optional)
  220. * @param int number of month (optional)
  221. * @return int count connections
  222. */
  223. public static function get_count_connections_on_the_course($course_code, $session_id = 0, $month = null) {
  224. // protect data
  225. $month_filter = '';
  226. if (isset($month)) {
  227. $month = intval($month);
  228. $month_filter = " AND MONTH(login_course_date)= $month ";
  229. }
  230. $course_code = Database::escape_string($course_code);
  231. $session_id = intval($session_id);
  232. $count = 0;
  233. $tbl_track_e_course_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  234. $sql = "SELECT count(*) as count_connections FROM $tbl_track_e_course_access WHERE course_code = '$course_code' AND session_id = $session_id $month_filter";
  235. $rs = Database::query($sql);
  236. if (Database::num_rows($rs)>0) {
  237. $row = Database::fetch_object($rs);
  238. $count = $row->count_connections;
  239. }
  240. return $count;
  241. }
  242. /**
  243. * Get count courses per student
  244. * @param int Student id
  245. * @param bool Include sessions (optional)
  246. * @return int count courses
  247. */
  248. public static function count_course_per_student($user_id, $include_sessions = true) {
  249. $user_id = intval($user_id);
  250. $tbl_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  251. $tbl_session_course_rel_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  252. $sql = 'SELECT DISTINCT course_code
  253. FROM ' . $tbl_course_rel_user . '
  254. WHERE user_id = ' . $user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH;
  255. $rs = Database::query($sql);
  256. $nb_courses = Database::num_rows($rs);
  257. if ($include_sessions) {
  258. $sql = 'SELECT DISTINCT course_code
  259. FROM ' . $tbl_session_course_rel_user . '
  260. WHERE id_user = ' . $user_id;
  261. $rs = Database::query($sql);
  262. $nb_courses += Database::num_rows($rs);
  263. }
  264. return $nb_courses;
  265. }
  266. /**
  267. * This function gets the score average from all tests in a course by student
  268. * @param int|array Student(s) id
  269. * @param string Course code
  270. * @param int Exercise id (optional), filtered by exercise
  271. * @param int Session id (optional), if param $session_id is null it'll return results including sessions, 0 = session is not filtered
  272. * @return string value (number %) Which represents a round integer about the score average.
  273. */
  274. public static function get_avg_student_exercise_score($student_id, $course_code, $exercise_id = 0, $session_id = null) {
  275. // protect datas
  276. $course_code = Database::escape_string($course_code);
  277. // get the informations of the course
  278. $a_course = CourseManager :: get_course_information($course_code);
  279. if(!empty($a_course['db_name'])) {
  280. // table definition
  281. $tbl_course_quiz = Database::get_course_table(TABLE_QUIZ_TEST,$a_course['db_name']);
  282. $tbl_stats_exercise = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  283. // Compose a filter based on optional exercise given
  284. $condition_quiz = "";
  285. if (!empty($exercise_id)) {
  286. $exercise_id = intval($exercise_id);
  287. $condition_quiz =" AND id = $exercise_id ";
  288. }
  289. // Compose a filter based on optional session id given
  290. $condition_session = "";
  291. if (isset($session_id)) {
  292. $session_id = intval($session_id);
  293. $condition_session = " AND session_id = $session_id ";
  294. }
  295. $sql = "SELECT count(id) FROM $tbl_course_quiz WHERE active <> -1 $condition_quiz ";
  296. $count_quiz = Database::fetch_row(Database::query($sql));
  297. $quiz_avg_total_score = 0;
  298. if (!empty($count_quiz[0]) && !empty($student_id)) {
  299. $condition_user = "";
  300. if (is_array($student_id)) {
  301. $condition_user = " AND exe_user_id IN (".implode(',',$student_id).") ";
  302. } else {
  303. $condition_user = " AND exe_user_id = '$student_id' ";
  304. }
  305. if (empty($exercise_id)) {
  306. $sql = "SELECT id FROM $tbl_course_quiz WHERE active <> -1 $condition_quiz";
  307. $exercises = Database::fetch_row(Database::query($sql));
  308. $exercise_list = array();
  309. $exercise_id = 0;
  310. if (!empty($exercises)) {
  311. foreach($exercises as $row) {
  312. $exercise_list[] = $row['id'];
  313. }
  314. $exercise_id = implode("','",$exercise_list);
  315. }
  316. }
  317. $count_quiz = Database::fetch_row(Database::query($sql));
  318. $sql = "SELECT SUM(exe_result/exe_weighting*100) as avg_score, COUNT(*) as num_attempts
  319. FROM $tbl_stats_exercise
  320. WHERE exe_exo_id IN ('".$exercise_id."')
  321. $condition_user
  322. AND orig_lp_id = 0
  323. AND exe_cours_id = '$course_code'
  324. AND orig_lp_item_id = 0 $condition_session
  325. ORDER BY exe_date DESC";
  326. $res = Database::query($sql);
  327. $row = Database::fetch_array($res);
  328. $quiz_avg_score = 0;
  329. if (!empty($row['avg_score'])) {
  330. $quiz_avg_score = round($row['avg_score'],2);
  331. }
  332. if(!empty($row['num_attempts'])) {
  333. $quiz_avg_score = round($quiz_avg_score / $row['num_attempts'], 2);
  334. }
  335. return $quiz_avg_score;
  336. }
  337. }
  338. return null;
  339. }
  340. /**
  341. * Get count student's exercise attempts
  342. * @param int Student id
  343. * @param string Course code
  344. * @param int Exercise id
  345. * @param int Learning path id (optional), for showing attempts inside a learning path $lp_id and $lp_item_id params are required.
  346. * @param int Learning path item id (optional), for showing attempts inside a learning path $lp_id and $lp_item_id params are required.
  347. * @return int count of attempts
  348. */
  349. public function count_student_exercise_attempts($student_id, $course_code, $exercise_id, $lp_id = 0, $lp_item_id = 0, $session_id = 0) {
  350. $course_code = Database::escape_string($course_code);
  351. $course_info = CourseManager :: get_course_information($course_code);
  352. $student_id = intval($student_id);
  353. $exercise_id = intval($exercise_id);
  354. $session_id = intval($session_id);
  355. $count_attempts = 0;
  356. if (!empty($lp_id)) $lp_id = intval($lp_id);
  357. if (!empty($lp_item_id)) $lp_id = intval($lp_item_id);
  358. if (!empty($course_info['db_name'])) {
  359. $tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES, $course_info['db_name']);
  360. $sql = "SELECT COUNT(ex.exe_id) as essais FROM $tbl_stats_exercices AS ex
  361. WHERE ex.exe_cours_id = '$course_code'
  362. AND ex.exe_exo_id = $exercise_id
  363. AND orig_lp_id = $lp_id
  364. AND orig_lp_item_id = $lp_item_id
  365. AND exe_user_id= $student_id
  366. AND session_id = $session_id ";
  367. $rs = Database::query($sql);
  368. $row = Database::fetch_row($rs);
  369. $count_attempts = $row[0];
  370. }
  371. return $count_attempts;
  372. }
  373. /**
  374. * Returns the average student progress in the learning paths of the given
  375. * course.
  376. * @param int/array Student id(s)
  377. * @param string Course code
  378. * @param array Limit average to listed lp ids
  379. * @param int Session id (optional), if parameter $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  380. * @param bool Will return an array of the type: [sum_of_progresses, number] if it is set to true
  381. * @return double Average progress of the user in this course
  382. */
  383. public static function get_avg_student_progress($student_id, $course_code, $lp_ids = array(), $session_id = null, $return_array = false) {
  384. // get the informations of the course
  385. $a_course = CourseManager :: get_course_information($course_code);
  386. if (!empty($a_course['db_name'])) {
  387. // table definition
  388. $tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW, $a_course['db_name']);
  389. $tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN, $a_course['db_name']);
  390. // Compose a filter based on optional learning paths list given
  391. $condition_lp = "";
  392. if (!empty($lp_ids)) {
  393. if (count($lp_ids) > 0) {
  394. $condition_lp =" WHERE id IN(".implode(',',$lp_ids).") ";
  395. }
  396. }
  397. $session_id = intval($session_id);
  398. $sql = "SELECT id FROM $tbl_course_lp lp $condition_lp";
  399. $res_count_lp = Database::query($sql);
  400. // count the number of learning paths
  401. $lp_id = array();
  402. while ($row_lp = Database::fetch_array($res_count_lp,'ASSOC')) {
  403. //$visibility = api_get_item_visibility($a_course, TOOL_LEARNPATH, $row_lp['id'], $session_id);
  404. // if ($visibility == 1) {
  405. $lp_id[] = $row_lp['id'];
  406. //}
  407. }
  408. $count_lp = count($lp_id);
  409. $avg_progress = 0;
  410. //if there is at least one learning path and one student
  411. if ($count_lp>0 && !empty($student_id)) {
  412. $condition_user = "";
  413. if (is_array($student_id)) {
  414. $$r = array_walk($student_id,'intval');
  415. $condition_user = " lp_view.user_id IN (".implode(',',$student_id).") AND ";
  416. } else {
  417. $student_id = intval($student_id);
  418. $condition_user = " lp_view.user_id = '$student_id' AND ";
  419. }
  420. // Get last view for each student (in case of multi-attempt)
  421. // Also filter on LPs of this session
  422. $sql_maxes = "SELECT MAX(view_count), progress FROM $tbl_course_lp_view lp_view ".
  423. "WHERE $condition_user session_id = $session_id AND lp_view.lp_id IN (".implode(',',$lp_id).") ".
  424. "GROUP BY lp_id, user_id";
  425. $res_maxes = Database::query($sql_maxes);
  426. $sum = 0;
  427. while ($row_maxes = Database::fetch_array($res_maxes)) {
  428. $sum += $row_maxes[1];
  429. }
  430. // average progress = total sum divided by the number of views
  431. // summed up.
  432. $number_items = count($lp_id);
  433. if ($number_items == 0) {
  434. return 0; //not necessary to return something else if there is no view
  435. }
  436. if (!$return_array) {
  437. $avg_progress = round($sum / $number_items, 1);
  438. return $avg_progress;
  439. } else {
  440. return array($sum, $number_items);
  441. }
  442. }
  443. }
  444. return null;
  445. }
  446. /**
  447. * This function gets:
  448. * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max scores.
  449. * 2. The score average from all Tests (quiz) in all LP in a course-> All the answers / All the max scores.
  450. * 3. And finally it will return the average between 1. and 2.
  451. * @todo improve performance, when loading 1500 users with 20 lps the script dies
  452. * This function does not take the results of a Test out of a LP
  453. *
  454. * @param mixed Array of user ids or an user id
  455. * @param string Course code
  456. * @param array List of LP ids
  457. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  458. * @param bool Returns an array of the type [sum_score, num_score] if set to true
  459. * @return string Value (number %) Which represents a round integer explain in got in 3.
  460. */
  461. public static function get_avg_student_score($student_id, $course_code, $lp_ids=array(), $session_id = null, $return_array = false) {
  462. // get global tables names
  463. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  464. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  465. $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  466. $tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  467. $tbl_stats_attempts = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  468. $course = CourseManager :: get_course_information($course_code);
  469. if (!empty($course['db_name'])) {
  470. // get course tables names
  471. $tbl_quiz_questions = Database :: get_course_table(TABLE_QUIZ_QUESTION,$course['db_name']);
  472. $lp_table = Database :: get_course_table(TABLE_LP_MAIN,$course['db_name']);
  473. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course['db_name']);
  474. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW,$course['db_name']);
  475. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW,$course['db_name']);
  476. // Compose a filter based on optional learning paths list given
  477. $condition_lp = "";
  478. if(count($lp_ids) > 0) {
  479. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  480. }
  481. // Compose a filter based on optional session id
  482. $condition_session = "";
  483. if (isset($session_id)) {
  484. $session_id = intval($session_id);
  485. if (count($lp_ids) > 0) {
  486. $condition_session = " AND session_id = $session_id ";
  487. } else {
  488. $condition_session = " WHERE session_id = $session_id ";
  489. }
  490. }
  491. // Check the real number of LPs corresponding to the filter in the
  492. // database (and if no list was given, get them all)
  493. if (empty($session_id)) {
  494. $sql = "SELECT DISTINCT(id), use_max_score FROM $lp_table WHERE session_id = 0 $condition_lp ";
  495. } else {
  496. $sql = "SELECT DISTINCT(id), use_max_score FROM $lp_table WHERE 1 $condition_lp ";
  497. }
  498. $res_row_lp = Database::query($sql);
  499. $count_row_lp = Database::num_rows($res_row_lp);
  500. $lp_list = $use_max_score = array();
  501. while ($row_lp = Database::fetch_array($res_row_lp)) {
  502. $lp_list[] = $row_lp['id'];
  503. $use_max_score[$row_lp['id']] = $row_lp['use_max_score'];
  504. }
  505. // Init local variables that will be used through the calculation
  506. $lp_scorm_score_total = 0;
  507. $lp_scorm_result_score_total = 0;
  508. $lp_scorm_loop=0;
  509. $lp_count = 0;
  510. $progress = 0;
  511. // prepare filter on users
  512. $condition_user1 = "";
  513. if (is_array($student_id)) {
  514. array_walk($student_id,'intval');
  515. $condition_user1 =" AND user_id IN (".implode(',',$student_id).") ";
  516. } else {
  517. $condition_user1 =" AND user_id = '$student_id' ";
  518. }
  519. if ($count_row_lp>0 && !empty($student_id)) {
  520. // Get all views through learning paths filter
  521. //@todo problem when a course have more than 1500 users
  522. $sql = "SELECT MAX(view_count) as vc, id, progress, lp_id, user_id FROM $lp_view_table ".
  523. "WHERE lp_id IN (".implode(',',$lp_list).") $condition_user1 AND session_id= $session_id GROUP BY lp_id,user_id";
  524. //var_dump( $sql );
  525. $rs_last_lp_view_id = Database::query($sql);
  526. $global_count_item = 0;
  527. $global_result = 0;
  528. if (Database::num_rows($rs_last_lp_view_id) > 0) {
  529. // Cycle through each line of the results (grouped by lp_id, user_id)
  530. while ($row_lp_view = Database::fetch_array($rs_last_lp_view_id)) {
  531. $lp_view_id = $row_lp_view['id'];
  532. $progress = $row_lp_view['progress'];
  533. $lp_id = $row_lp_view['lp_id'];
  534. $user_id = $row_lp_view['user_id'];
  535. // For the currently analysed view, get the score and
  536. // max_score of each item if it is a sco or a TOOL_QUIZ
  537. $sql_max_score = "SELECT lp_iv.score as score,lp_i.max_score, lp_iv.max_score as max_score_item_view, lp_i.path, lp_i.item_type, lp_i.id as iid".
  538. " FROM $lp_item_view_table as lp_iv INNER JOIN $lp_item_table as lp_i ".
  539. " ON lp_i.id = lp_iv.lp_item_id ".
  540. " AND (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."') ".
  541. " WHERE lp_view_id='$lp_view_id'";
  542. //echo $sql_max_score; echo '<br />';
  543. $res_max_score = Database::query($sql_max_score);
  544. $count_total_loop = 0;
  545. $num_rows_max_score = Database::num_rows($res_max_score);
  546. // Go through each scorable element of this view
  547. $count_items = 0;
  548. $lp_partial_total = 0;
  549. $score_of_scorm_calculate = 0;
  550. while ($row_max_score = Database::fetch_array($res_max_score,'ASSOC')) {
  551. $max_score = $row_max_score['max_score']; //Came from the original lp_item
  552. $max_score_item_view = $row_max_score['max_score_item_view']; //Came from the lp_item_view
  553. $score = $row_max_score['score'];
  554. if ($row_max_score['item_type'] == 'sco') {
  555. // Check if it is sco (easier to get max_score)
  556. //when there's no max score, we assume 100 as the max score, as the SCORM 1.2 says that the value should always be between 0 and 100.
  557. if ($max_score == 0 || is_null($max_score) || $max_score == '') {
  558. //Chamilo style
  559. if ($use_max_score[$lp_id]) {
  560. $max_score = 100;
  561. } else {
  562. //Overwrites max score = 100 to use the one that came in the lp_item_view see BT#1613
  563. $max_score = $max_score_item_view;
  564. }
  565. }
  566. //Avoid division by zero errors
  567. if (!empty($max_score)) {
  568. $lp_scorm_result_score_total += $score/$max_score;
  569. $lp_partial_total += $score/$max_score;
  570. $current_value = $score/$max_score;
  571. }
  572. } else {
  573. // Case of a TOOL_QUIZ element
  574. $item_id = $row_max_score['iid'];
  575. $item_path = $row_max_score['path'];
  576. // Get last attempt to this exercise through
  577. // the current lp for the current user
  578. $sql_last_attempt = "SELECT exe_id FROM $tbl_stats_exercices ".
  579. " WHERE exe_exo_id = '$item_path' ".
  580. " AND exe_user_id = '$user_id' ".
  581. // " AND orig_lp_id = '$lp_id' ". //lp_id is already defined by the item_id
  582. " AND orig_lp_item_id = '$item_id' ".
  583. " AND exe_cours_id = '$course_code' AND session_id = $session_id".
  584. " ORDER BY exe_date DESC limit 1";
  585. $result_last_attempt = Database::query($sql_last_attempt);
  586. $num = Database :: num_rows($result_last_attempt);
  587. if ($num > 0 ) {
  588. $id_last_attempt = Database :: result($result_last_attempt, 0, 0);
  589. // Within the last attempt number tracking, get the sum of
  590. // the max_scores of all questions that it was
  591. // made of (we need to make this call dynamic
  592. // because of random questions selection)
  593. $sql = "SELECT SUM(t.ponderation) as maxscore ".
  594. " FROM ( SELECT distinct question_id, marks, ponderation ".
  595. " FROM $tbl_stats_attempts AS at " .
  596. " INNER JOIN $tbl_quiz_questions AS q ".
  597. " ON (q.id = at.question_id) ".
  598. " WHERE exe_id ='$id_last_attempt' ) AS t";
  599. $res_max_score_bis = Database::query($sql);
  600. $row_max_score_bis = Database :: fetch_array($res_max_score_bis);
  601. if (!empty($row_max_score_bis['maxscore'])) {
  602. $max_score = $row_max_score_bis['maxscore'];
  603. }
  604. if (!empty($max_score)) {
  605. $lp_scorm_result_score_total += ($score/$max_score);
  606. $lp_partial_total += ($score/$max_score);
  607. $current_value = $score/$max_score;
  608. }
  609. } else {
  610. //$lp_scorm_result_score_total += 0;
  611. }
  612. }
  613. // Normal way
  614. if ($use_max_score[$lp_id]) {
  615. $count_items++;
  616. } else {
  617. if ($max_score != '') {
  618. $count_items++;
  619. }
  620. }
  621. } //end while
  622. $global_count_item +=$count_items;
  623. //echo 'lp_view '.$lp_view_id.' - $count_items '.$count_items.' lp partiual '.$lp_partial_total.' <br />';
  624. $score_of_scorm_calculate += $count_items?(($lp_partial_total/$count_items)*100):0;
  625. // var_dump($score_of_scorm_calculate);
  626. $global_result += $score_of_scorm_calculate;
  627. } // end while
  628. /*echo '<br>$lp_scorm_result_score_total:'.($global_result); echo '<br>';
  629. echo ("lp score :".($score_of_scorm_calculate));
  630. echo '<br>';*/
  631. }
  632. $lp_with_quiz = 0;
  633. $total_lp = 0;
  634. //var_dump($lp_list);
  635. foreach($lp_list as $lp_id) {
  636. //check if LP have a score
  637. $sql = "SELECT count(id) as count FROM $lp_item_table
  638. WHERE item_type = 'quiz' AND lp_id = ".$lp_id." ";
  639. $result_have_quiz = Database::query($sql);
  640. if (Database::num_rows($result_have_quiz) > 0 ) {
  641. $row = Database::fetch_array($result_have_quiz,'ASSOC');
  642. if (is_numeric($row['count']) && $row['count'] != 0) {
  643. $lp_with_quiz ++;
  644. }
  645. }
  646. $total_lp ++;
  647. }
  648. //var_dump($lp_with_quiz);
  649. if ($lp_with_quiz != 0 ) {
  650. if (!$return_array) {
  651. //var_dump($global_result,$lp_with_quiz );
  652. $score_of_scorm_calculate = round(($global_result/$lp_with_quiz),2);
  653. return $score_of_scorm_calculate;
  654. } else {
  655. return array($global_result, $lp_with_quiz);
  656. }
  657. } else {
  658. return '-';
  659. }
  660. }
  661. }
  662. return null;
  663. }
  664. /**
  665. * This function gets time spent in learning path for a student inside a course
  666. * @param int|array Student id(s)
  667. * @param string Course code
  668. * @param array Limit average to listed lp ids
  669. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  670. * @return int Total time
  671. */
  672. public static function get_time_spent_in_lp($student_id, $course_code, $lp_ids = array(), $session_id = null) {
  673. $course = CourseManager :: get_course_information($course_code);
  674. $student_id = intval($student_id);
  675. $total_time = 0;
  676. if (!empty($course['db_name'])) {
  677. $lp_table = Database :: get_course_table(TABLE_LP_MAIN, $course['db_name']);
  678. $t_lpv = Database :: get_course_table(TABLE_LP_VIEW, $course['db_name']);
  679. $t_lpiv = Database :: get_course_table(TABLE_LP_ITEM_VIEW, $course['db_name']);
  680. // Compose a filter based on optional learning paths list given
  681. $condition_lp = "";
  682. if(count($lp_ids) > 0) {
  683. $condition_lp =" WHERE id IN(".implode(',',$lp_ids).") ";
  684. }
  685. // Compose a filter based on optional session id
  686. $condition_session = "";
  687. $session_id = intval($session_id);
  688. if (isset($session_id)) {
  689. if (count($lp_ids) > 0) {
  690. $condition_session = " AND session_id = $session_id ";
  691. } else {
  692. $condition_session = " WHERE session_id = $session_id ";
  693. }
  694. }
  695. // Check the real number of LPs corresponding to the filter in the
  696. // database (and if no list was given, get them all)
  697. //$res_row_lp = Database::query("SELECT DISTINCT(id) FROM $lp_table $condition_lp $condition_session");
  698. $res_row_lp = Database::query("SELECT DISTINCT(id) FROM $lp_table $condition_lp");
  699. $count_row_lp = Database::num_rows($res_row_lp);
  700. // calculates time
  701. if ($count_row_lp > 0) {
  702. while ($row_lp = Database::fetch_array($res_row_lp)) {
  703. $lp_id = intval($row_lp['id']);
  704. $sql = 'SELECT SUM(total_time)
  705. FROM '.$t_lpiv.' AS item_view
  706. INNER JOIN '.$t_lpv.' AS view
  707. ON item_view.lp_view_id = view.id
  708. AND view.lp_id = '.$lp_id.'
  709. AND view.user_id = '.$student_id.' AND session_id = '.$session_id;
  710. $rs = Database::query($sql);
  711. if (Database :: num_rows($rs) > 0) {
  712. $total_time += Database :: result($rs, 0, 0);
  713. }
  714. }
  715. }
  716. }
  717. return $total_time;
  718. }
  719. /**
  720. * This function gets last connection time to one learning path
  721. * @param int|array Student id(s)
  722. * @param string Course code
  723. * @param int Learning path id
  724. * @return int Total time
  725. */
  726. public static function get_last_connection_time_in_lp($student_id, $course_code, $lp_id, $session_id=0) {
  727. $course = CourseManager :: get_course_information($course_code);
  728. $student_id = intval($student_id);
  729. $lp_id = intval($lp_id);
  730. $last_time = 0;
  731. $session_id = intval($session_id);
  732. if (!empty($course['db_name'])) {
  733. $lp_table = Database :: get_course_table(TABLE_LP_MAIN, $course['db_name']);
  734. $t_lpv = Database :: get_course_table(TABLE_LP_VIEW, $course['db_name']);
  735. $t_lpiv = Database :: get_course_table(TABLE_LP_ITEM_VIEW, $course['db_name']);
  736. // Check the real number of LPs corresponding to the filter in the
  737. // database (and if no list was given, get them all)
  738. $res_row_lp = Database::query("SELECT id FROM $lp_table WHERE id = $lp_id ");
  739. $count_row_lp = Database::num_rows($res_row_lp);
  740. // calculates last connection time
  741. if ($count_row_lp > 0) {
  742. $sql = 'SELECT MAX(start_time)
  743. FROM ' . $t_lpiv . ' AS item_view
  744. INNER JOIN ' . $t_lpv . ' AS view
  745. ON item_view.lp_view_id = view.id
  746. AND view.lp_id = '.$lp_id.'
  747. AND view.user_id = '.$student_id.'
  748. AND view.session_id = '.$session_id;
  749. $rs = Database::query($sql);
  750. if (Database :: num_rows($rs) > 0) {
  751. $last_time = Database :: result($rs, 0, 0);
  752. }
  753. }
  754. }
  755. return $last_time;
  756. }
  757. /**
  758. * gets the list of students followed by coach
  759. * @param int Coach id
  760. * @return array List of students
  761. */
  762. public static function get_student_followed_by_coach($coach_id) {
  763. $coach_id = intval($coach_id);
  764. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  765. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  766. $tbl_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER);
  767. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  768. $a_students = array ();
  769. // At first, courses where $coach_id is coach of the course //
  770. $sql = 'SELECT id_session, course_code FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id.' AND status=2';
  771. global $_configuration;
  772. if ($_configuration['multiple_access_urls']) {
  773. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  774. $access_url_id = api_get_current_access_url_id();
  775. if ($access_url_id != -1) {
  776. $sql = 'SELECT scu.id_session, scu.course_code
  777. FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_session_rel_access_url.' sru
  778. ON (scu.id_session=sru.session_id)
  779. WHERE scu.id_user=' . $coach_id.' AND scu.status=2 AND sru.access_url_id = '.$access_url_id;
  780. }
  781. }
  782. $result = Database::query($sql);
  783. while ($a_courses = Database::fetch_array($result)) {
  784. $course_code = $a_courses["course_code"];
  785. $id_session = $a_courses["id_session"];
  786. $sql = "SELECT distinct srcru.id_user
  787. FROM $tbl_session_course_user AS srcru, $tbl_session_user sru
  788. WHERE srcru.id_user = sru.id_user AND sru.relation_type<>".SESSION_RELATION_TYPE_RRHH." AND srcru.id_session = sru.id_session AND srcru.course_code='$course_code' AND srcru.id_session='$id_session'";
  789. $rs = Database::query($sql);
  790. while ($row = Database::fetch_array($rs)) {
  791. $a_students[$row['id_user']] = $row['id_user'];
  792. }
  793. }
  794. // Then, courses where $coach_id is coach of the session //
  795. $sql = 'SELECT session_course_user.id_user
  796. FROM ' . $tbl_session_course_user . ' as session_course_user
  797. INNER JOIN '.$tbl_session_user.' sru ON session_course_user.id_user = sru.id_user AND session_course_user.id_session = sru.id_session
  798. INNER JOIN ' . $tbl_session_course . ' as session_course
  799. ON session_course.course_code = session_course_user.course_code
  800. AND session_course_user.id_session = session_course.id_session
  801. INNER JOIN ' . $tbl_session . ' as session
  802. ON session.id = session_course.id_session
  803. AND session.id_coach = ' . $coach_id;
  804. if ($_configuration['multiple_access_urls']) {
  805. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  806. $access_url_id = api_get_current_access_url_id();
  807. if ($access_url_id != -1){
  808. $sql = 'SELECT session_course_user.id_user
  809. FROM ' . $tbl_session_course_user . ' as session_course_user
  810. INNER JOIN '.$tbl_session_user.' sru ON session_course_user.id_user = sru.id_user AND session_course_user.id_session = sru.id_session
  811. INNER JOIN ' . $tbl_session_course . ' as session_course
  812. ON session_course.course_code = session_course_user.course_code
  813. AND session_course_user.id_session = session_course.id_session
  814. INNER JOIN ' . $tbl_session . ' as session
  815. ON session.id = session_course.id_session
  816. AND session.id_coach = ' . $coach_id.'
  817. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  818. ON session.id = session_rel_url.session_id WHERE access_url_id = '.$access_url_id;
  819. }
  820. }
  821. $result = Database::query($sql);
  822. while ($row = Database::fetch_array($result)) {
  823. $a_students[$row['id_user']] = $row['id_user'];
  824. }
  825. return $a_students;
  826. }
  827. /**
  828. * Get student followed by a coach inside a session
  829. * @param int Session id
  830. * @param int Coach id
  831. * @return array students list
  832. */
  833. public static function get_student_followed_by_coach_in_a_session($id_session, $coach_id) {
  834. $coach_id = intval($coach_id);
  835. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  836. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  837. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  838. $a_students = array ();
  839. //////////////////////////////////////////////////////////////
  840. // At first, courses where $coach_id is coach of the course //
  841. //////////////////////////////////////////////////////////////
  842. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_session="' . $id_session . '" AND id_user=' . $coach_id.' AND status=2';
  843. $result = Database::query($sql);
  844. while ($a_courses = Database::fetch_array($result)) {
  845. $course_code = $a_courses["course_code"];
  846. $sql = "SELECT distinct srcru.id_user
  847. FROM $tbl_session_course_user AS srcru
  848. WHERE course_code='$course_code' and id_session = '" . $id_session . "'";
  849. $rs = Database::query($sql);
  850. while ($row = Database::fetch_array($rs)) {
  851. $a_students[$row['id_user']] = $row['id_user'];
  852. }
  853. }
  854. //////////////////////////////////////////////////////////////
  855. // Then, courses where $coach_id is coach of the session //
  856. //////////////////////////////////////////////////////////////
  857. $dsl_session_coach = 'SELECT id_coach FROM ' . $tbl_session . ' WHERE id="' . $id_session . '" AND id_coach="' . $coach_id . '"';
  858. $result = Database::query($dsl_session_coach);
  859. //He is the session_coach so we select all the users in the session
  860. if (Database::num_rows($result) > 0) {
  861. $sql = 'SELECT DISTINCT srcru.id_user FROM ' . $tbl_session_course_user . ' AS srcru WHERE id_session="' . $id_session . '"';
  862. $result = Database::query($sql);
  863. while ($row = Database::fetch_array($result)) {
  864. $a_students[$row['id_user']] = $row['id_user'];
  865. }
  866. }
  867. return $a_students;
  868. }
  869. /**
  870. * Check if a coach is allowed to follow a student
  871. * @param int Coach id
  872. * @param int Student id
  873. * @return bool
  874. */
  875. public static function is_allowed_to_coach_student($coach_id, $student_id) {
  876. $coach_id = intval($coach_id);
  877. $student_id = intval($student_id);
  878. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  879. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  880. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  881. //////////////////////////////////////////////////////////////
  882. // At first, courses where $coach_id is coach of the course //
  883. //////////////////////////////////////////////////////////////
  884. /*$sql = 'SELECT 1
  885. FROM ' . $tbl_session_course_user . ' AS session_course_user
  886. INNER JOIN ' . $tbl_session_course . ' AS session_course
  887. ON session_course.course_code = session_course_user.course_code
  888. AND id_coach=' . $coach_id . '
  889. WHERE id_user=' . $student_id;*/
  890. $sql = 'SELECT 1 FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id .' AND status=2';
  891. $result = Database::query($sql);
  892. if (Database::num_rows($result) > 0) {
  893. return true;
  894. }
  895. //////////////////////////////////////////////////////////////
  896. // Then, courses where $coach_id is coach of the session //
  897. //////////////////////////////////////////////////////////////
  898. $sql = 'SELECT session_course_user.id_user
  899. FROM ' . $tbl_session_course_user . ' as session_course_user
  900. INNER JOIN ' . $tbl_session_course . ' as session_course
  901. ON session_course.course_code = session_course_user.course_code
  902. INNER JOIN ' . $tbl_session . ' as session
  903. ON session.id = session_course.id_session
  904. AND session.id_coach = ' . $coach_id . '
  905. WHERE id_user = ' . $student_id;
  906. $result = Database::query($sql);
  907. if (Database::num_rows($result) > 0) {
  908. return true;
  909. }
  910. return false;
  911. }
  912. /**
  913. * Get courses followed by coach
  914. * @param int Coach id
  915. * @param int Session id (optional)
  916. * @return array Courses list
  917. */
  918. public static function get_courses_followed_by_coach($coach_id, $id_session = '')
  919. {
  920. $coach_id = intval($coach_id);
  921. if (!empty ($id_session))
  922. $id_session = intval($id_session);
  923. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  924. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  925. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  926. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  927. //////////////////////////////////////////////////////////////
  928. // At first, courses where $coach_id is coach of the course //
  929. //////////////////////////////////////////////////////////////
  930. $sql = 'SELECT DISTINCT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id.' AND status=2';
  931. global $_configuration;
  932. if ($_configuration['multiple_access_urls']) {
  933. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  934. $access_url_id = api_get_current_access_url_id();
  935. if ($access_url_id != -1){
  936. $sql = 'SELECT DISTINCT scu.course_code FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_course_rel_access_url.' cru
  937. ON (scu.course_code = cru.course_code)
  938. WHERE scu.id_user=' . $coach_id.' AND scu.status=2 AND cru.access_url_id = '.$access_url_id;
  939. }
  940. }
  941. if (!empty ($id_session))
  942. $sql .= ' AND id_session=' . $id_session;
  943. $result = Database::query($sql);
  944. while ($row = Database::fetch_array($result)) {
  945. $a_courses[$row['course_code']] = $row['course_code'];
  946. }
  947. //////////////////////////////////////////////////////////////
  948. // Then, courses where $coach_id is coach of the session //
  949. //////////////////////////////////////////////////////////////
  950. $sql = 'SELECT DISTINCT session_course.course_code
  951. FROM ' . $tbl_session_course . ' as session_course
  952. INNER JOIN ' . $tbl_session . ' as session
  953. ON session.id = session_course.id_session
  954. AND session.id_coach = ' . $coach_id . '
  955. INNER JOIN ' . $tbl_course . ' as course
  956. ON course.code = session_course.course_code';
  957. if ($_configuration['multiple_access_urls']) {
  958. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  959. $access_url_id = api_get_current_access_url_id();
  960. if ($access_url_id != -1){
  961. $sql = 'SELECT DISTINCT session_course.course_code
  962. FROM ' . $tbl_session_course . ' as session_course
  963. INNER JOIN ' . $tbl_session . ' as session
  964. ON session.id = session_course.id_session
  965. AND session.id_coach = ' . $coach_id . '
  966. INNER JOIN ' . $tbl_course . ' as course
  967. ON course.code = session_course.course_code
  968. INNER JOIN '.$tbl_course_rel_access_url.' course_rel_url
  969. ON (session_course.course_code = course_rel_url.course_code)';
  970. }
  971. }
  972. if (!empty ($id_session)) {
  973. $sql .= ' WHERE session_course.id_session=' . $id_session;
  974. if ($_configuration['multiple_access_urls'])
  975. $sql .= ' AND access_url_id = '.$access_url_id;
  976. } else {
  977. if ($_configuration['multiple_access_urls'])
  978. $sql .= ' WHERE access_url_id = '.$access_url_id;
  979. }
  980. $result = Database::query($sql);
  981. while ($row = Database::fetch_array($result)) {
  982. $a_courses[$row['course_code']] = $row['course_code'];
  983. }
  984. return $a_courses;
  985. }
  986. /**
  987. * Get sessions coached by user
  988. * @param int Coach id
  989. * @return array Sessions list
  990. */
  991. public static function get_sessions_coached_by_user($coach_id) {
  992. // table definition
  993. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  994. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  995. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  996. // protect datas
  997. $coach_id = intval($coach_id);
  998. // session where we are general coach
  999. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  1000. FROM ' . $tbl_session . '
  1001. WHERE id_coach=' . $coach_id;
  1002. global $_configuration;
  1003. if ($_configuration['multiple_access_urls']) {
  1004. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1005. $access_url_id = api_get_current_access_url_id();
  1006. if ($access_url_id != -1){
  1007. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  1008. FROM ' . $tbl_session . ' session INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  1009. ON (session.id = session_rel_url.session_id)
  1010. WHERE id_coach=' . $coach_id.' AND access_url_id = '.$access_url_id;
  1011. }
  1012. }
  1013. $rs = Database::query($sql);
  1014. while ($row = Database::fetch_array($rs))
  1015. {
  1016. $a_sessions[$row["id"]] = $row;
  1017. }
  1018. // session where we are coach of a course
  1019. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  1020. FROM ' . $tbl_session . ' as session
  1021. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  1022. ON session.id = session_course_user.id_session
  1023. AND session_course_user.id_user=' . $coach_id.' AND session_course_user.status=2';
  1024. global $_configuration;
  1025. if ($_configuration['multiple_access_urls']) {
  1026. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1027. $access_url_id = api_get_current_access_url_id();
  1028. if ($access_url_id != -1){
  1029. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  1030. FROM ' . $tbl_session . ' as session
  1031. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  1032. ON session.id = session_course_user.id_session AND session_course_user.id_user=' . $coach_id.' AND session_course_user.status=2
  1033. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  1034. ON (session.id = session_rel_url.session_id)
  1035. WHERE access_url_id = '.$access_url_id;
  1036. }
  1037. }
  1038. $rs = Database::query($sql);
  1039. while ($row = Database::fetch_array($rs))
  1040. {
  1041. $a_sessions[$row["id"]] = $row;
  1042. }
  1043. if (is_array($a_sessions)) {
  1044. foreach ($a_sessions as & $session) {
  1045. if ($session['date_start'] == '0000-00-00') {
  1046. $session['status'] = get_lang('SessionActive');
  1047. }
  1048. else {
  1049. $date_start = explode('-', $session['date_start']);
  1050. $time_start = mktime(0, 0, 0, $date_start[1], $date_start[2], $date_start[0]);
  1051. $date_end = explode('-', $session['date_end']);
  1052. $time_end = mktime(0, 0, 0, $date_end[1], $date_end[2], $date_end[0]);
  1053. if ($time_start < time() && time() < $time_end) {
  1054. $session['status'] = get_lang('SessionActive');
  1055. }
  1056. else{
  1057. if (time() < $time_start) {
  1058. $session['status'] = get_lang('SessionFuture');
  1059. }
  1060. else{
  1061. if (time() > $time_end) {
  1062. $session['status'] = get_lang('SessionPast');
  1063. }
  1064. }
  1065. }
  1066. }
  1067. }
  1068. }
  1069. return $a_sessions;
  1070. }
  1071. /**
  1072. * Get courses list from a session
  1073. * @param int Session id
  1074. * @return array Courses list
  1075. */
  1076. public static function get_courses_list_from_session($session_id) {
  1077. //protect datas
  1078. $session_id = intval($session_id);
  1079. // table definition
  1080. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1081. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1082. $sql = 'SELECT DISTINCT course_code
  1083. FROM ' . $tbl_session_course . '
  1084. WHERE id_session=' . $session_id;
  1085. $rs = Database::query($sql);
  1086. $a_courses = array ();
  1087. while ($row = Database::fetch_array($rs)) {
  1088. $a_courses[$row['course_code']] = $row;
  1089. }
  1090. return $a_courses;
  1091. }
  1092. /**
  1093. * Count assignments per student
  1094. * @param int|array Student id(s)
  1095. * @param string Course code
  1096. * @param int Session id (optional), if param $session_id is null(default) return count of assignments including sessions, 0 = session is not filtered
  1097. * @return int Count of assignments
  1098. */
  1099. public static function count_student_assignments($student_id, $course_code, $session_id = null) {
  1100. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  1101. // protect datas
  1102. $course_code = Database::escape_string($course_code);
  1103. // get the informations of the course
  1104. $a_course = CourseManager :: get_course_information($course_code);
  1105. if (!empty($a_course['db_name'])) {
  1106. // table definition
  1107. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY, $a_course['db_name']);
  1108. $tbl_student_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION, $a_course['db_name']);
  1109. $condition_user = "";
  1110. if (is_array($student_id)) {
  1111. $condition_user = " AND ip.insert_user_id IN (".implode(',',$student_id).") ";
  1112. } else {
  1113. $condition_user = " AND ip.insert_user_id = '$student_id' ";
  1114. }
  1115. $condition_session = "";
  1116. if (isset($session_id)) {
  1117. $session_id = intval($session_id);
  1118. $condition_session = " AND pub.session_id = $session_id ";
  1119. }
  1120. $sql = "SELECT count(ip.tool) FROM $tbl_item_property ip INNER JOIN $tbl_student_publication pub ON ip.ref = pub.id WHERE ip.tool='work' $condition_user $condition_session ";
  1121. $rs = Database::query($sql);
  1122. $row = Database::fetch_row($rs);
  1123. return $row[0];
  1124. }
  1125. return null;
  1126. }
  1127. /**
  1128. * Count messages per student inside forum tool
  1129. * @param int Student id
  1130. * @param string Course code
  1131. * @param int Session id (optional), if param $session_id is null(default) return count of messages including sessions, 0 = session is not filtered
  1132. * @return int Count of messages
  1133. */
  1134. function count_student_messages($student_id, $course_code, $session_id = null) {
  1135. require_once (api_get_path(LIBRARY_PATH) . 'course.lib.php');
  1136. // protect datas
  1137. $student_id = intval($student_id);
  1138. $course_code = addslashes($course_code);
  1139. // get the informations of the course
  1140. $a_course = CourseManager :: get_course_information($course_code);
  1141. if(!empty($a_course['db_name']))
  1142. {
  1143. // table definition
  1144. $tbl_forum_post = Database :: get_course_table(TABLE_FORUM_POST, $a_course['db_name']);
  1145. $tbl_forum = Database :: get_course_table(TABLE_FORUM, $a_course['db_name']);
  1146. $condition_user = "";
  1147. if (is_array($student_id)) {
  1148. $condition_user = " WHERE post.poster_id IN (".implode(',',$student_id).") ";
  1149. } else {
  1150. $condition_user = " WHERE post.poster_id = '$student_id' ";
  1151. }
  1152. $condition_session = "";
  1153. if (isset($session_id)) {
  1154. $session_id = intval($session_id);
  1155. $condition_session = " AND forum.session_id = $session_id";
  1156. }
  1157. $sql = "SELECT 1 FROM $tbl_forum_post post INNER JOIN $tbl_forum forum ON forum.forum_id = post.forum_id $condition_user $condition_session ";
  1158. $rs = Database::query($sql);
  1159. return Database::num_rows($rs);
  1160. }
  1161. else
  1162. {
  1163. return null;
  1164. }
  1165. }
  1166. /**
  1167. * This function counts the number of post by course
  1168. * @param string Course code
  1169. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1170. * @return int The number of post by course
  1171. */
  1172. public static function count_number_of_posts_by_course($course_code, $session_id = null) {
  1173. //protect data
  1174. $course_code = Database::escape_string($course_code);
  1175. // get the informations of the course
  1176. $a_course = CourseManager :: get_course_information($course_code);
  1177. $count = 0;
  1178. if (!empty($a_course['db_name'])) {
  1179. $tbl_posts = Database :: get_course_table(TABLE_FORUM_POST, $a_course['db_name']);
  1180. $tbl_forums = Database :: get_course_table(TABLE_FORUM, $a_course['db_name']);
  1181. $condition_session = '';
  1182. if (isset($session_id)) {
  1183. $session_id = intval($session_id);
  1184. $condition_session = ' WHERE f.session_id = '. $session_id;
  1185. }
  1186. $sql = "SELECT count(*) FROM $tbl_posts p INNER JOIN $tbl_forums f ON f.forum_id = p.forum_id $condition_session ";
  1187. $result = Database::query($sql);
  1188. $row = Database::fetch_row($result);
  1189. $count = $row[0];
  1190. return $count;
  1191. } else {
  1192. return null;
  1193. }
  1194. }
  1195. /**
  1196. * This function counts the number of threads by course
  1197. * @param string Course code
  1198. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1199. * @return int The number of threads by course
  1200. */
  1201. public static function count_number_of_threads_by_course($course_code, $session_id = null) {
  1202. //protect data
  1203. $course_code = Database::escape_string($course_code);
  1204. // get the informations of the course
  1205. $a_course = CourseManager :: get_course_information($course_code);
  1206. $count = 0;
  1207. if (!empty($a_course['db_name'])) {
  1208. $tbl_threads = Database :: get_course_table(TABLE_FORUM_THREAD, $a_course['db_name']);
  1209. $tbl_forums = Database :: get_course_table(TABLE_FORUM, $a_course['db_name']);
  1210. $condition_session = '';
  1211. if (isset($session_id)) {
  1212. $session_id = intval($session_id);
  1213. $condition_session = ' WHERE f.session_id = '. $session_id;
  1214. }
  1215. $sql = "SELECT count(*) FROM $tbl_threads t INNER JOIN $tbl_forums f ON f.forum_id = t.forum_id $condition_session ";
  1216. $result = Database::query($sql);
  1217. $row = Database::fetch_row($result);
  1218. $count = $row[0];
  1219. return $count;
  1220. } else {
  1221. return null;
  1222. }
  1223. }
  1224. /**
  1225. * This function counts the number of forums by course
  1226. * @param string Course code
  1227. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1228. * @return int The number of forums by course
  1229. */
  1230. public static function count_number_of_forums_by_course($course_code, $session_id = null) {
  1231. //protect data
  1232. $course_code = addslashes($course_code);
  1233. // get the informations of the course
  1234. $a_course = CourseManager :: get_course_information($course_code);
  1235. $count = 0;
  1236. if (!empty($a_course['db_name'])) {
  1237. $condition_session = '';
  1238. if (isset($session_id)) {
  1239. $session_id = intval($session_id);
  1240. $condition_session = ' WHERE session_id = '. $session_id;
  1241. }
  1242. $tbl_forums = Database :: get_course_table(TABLE_FORUM, $a_course['db_name']);
  1243. $sql = "SELECT count(*) FROM $tbl_forums $condition_session";
  1244. $result = Database::query($sql);
  1245. $row = Database::fetch_row($result);
  1246. $count = $row[0];
  1247. return $count;
  1248. } else {
  1249. return null;
  1250. }
  1251. }
  1252. /**
  1253. * This function counts the chat last connections by course in x days
  1254. * @param string Course code
  1255. * @param int Last x days
  1256. * @param int Session id (optional)
  1257. * @return int Chat last connections by course in x days
  1258. */
  1259. public static function chat_connections_during_last_x_days_by_course($course_code,$last_days, $session_id = 0) {
  1260. //protect data
  1261. $last_days = intval($last_days);
  1262. $course_code = Database::escape_string($course_code);
  1263. $session_id = intval($session_id);
  1264. // get the informations of the course
  1265. $a_course = CourseManager :: get_course_information($course_code);
  1266. $count = 0;
  1267. if (!empty($a_course['db_name'])) {
  1268. $tbl_stats_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS, $a_course['db_name']);
  1269. $sql = "SELECT count(*) FROM $tbl_stats_access WHERE DATE_SUB(NOW(),INTERVAL $last_days DAY) <= access_date
  1270. AND access_cours_code = '$course_code' AND access_tool='".TOOL_CHAT."' AND access_session_id='$session_id' ";
  1271. $result = Database::query($sql);
  1272. $row = Database::fetch_row($result);
  1273. $count = $row[0];
  1274. return $count;
  1275. } else {
  1276. return null;
  1277. }
  1278. }
  1279. /**
  1280. * This function gets the last student's connection in chat
  1281. * @param int Student id
  1282. * @param string Course code
  1283. * @param int Session id (optional)
  1284. * @return string datetime formatted without day (e.g: February 23, 2010 10:20:50 )
  1285. */
  1286. public static function chat_last_connection($student_id, $course_code, $session_id = 0) {
  1287. //protect datas
  1288. $student_id = intval($student_id);
  1289. $course_code= Database::escape_string($course_code);
  1290. $session_id = intval($session_id);
  1291. $date_time = '';
  1292. // table definition
  1293. $tbl_stats_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1294. $sql = "SELECT access_date FROM $tbl_stats_access
  1295. WHERE access_tool='".TOOL_CHAT."' AND access_user_id='$student_id' AND access_cours_code = '$course_code' AND access_session_id = '$session_id' ORDER BY access_date DESC limit 1";
  1296. $rs = Database::query($sql);
  1297. if (Database::num_rows($rs) > 0) {
  1298. $row = Database::fetch_array($rs);
  1299. $date_time = api_convert_and_format_date($row['access_date'], null, date_default_timezone_get());
  1300. }
  1301. return $date_time;
  1302. }
  1303. /**
  1304. * Get count student's visited links
  1305. * @param int Student id
  1306. * @param string Course code
  1307. * @param int Session id (optional)
  1308. * @return int count of visited links
  1309. */
  1310. public static function count_student_visited_links($student_id, $course_code, $session_id = 0) {
  1311. // protect datas
  1312. $student_id = intval($student_id);
  1313. $course_code = Database::escape_string($course_code);
  1314. $session_id = intval($session_id);
  1315. // table definition
  1316. $tbl_stats_links = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1317. $sql = 'SELECT 1
  1318. FROM '.$tbl_stats_links.'
  1319. WHERE links_user_id= '.$student_id.'
  1320. AND links_cours_id = "'.$course_code.'"
  1321. AND links_session_id = '.$session_id.' ';
  1322. $rs = Database::query($sql);
  1323. return Database::num_rows($rs);
  1324. }
  1325. /**
  1326. * Get count student downloaded documents
  1327. * @param int Student id
  1328. * @param string Course code
  1329. * @param int Session id (optional)
  1330. * @return int Count downloaded documents
  1331. */
  1332. public static function count_student_downloaded_documents($student_id, $course_code, $session_id = 0) {
  1333. // protect datas
  1334. $student_id = intval($student_id);
  1335. $course_code = Database::escape_string($course_code);
  1336. $session_id = intval($session_id);
  1337. // table definition
  1338. $tbl_stats_documents = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1339. $sql = 'SELECT 1
  1340. FROM ' . $tbl_stats_documents . '
  1341. WHERE down_user_id = '.$student_id.'
  1342. AND down_cours_id = "'.$course_code.'"
  1343. AND down_session_id = '.$session_id.' ';
  1344. $rs = Database::query($sql);
  1345. return Database::num_rows($rs);
  1346. }
  1347. /**
  1348. * Get course list inside a session from a student
  1349. * @param int Student id
  1350. * @param int Session id (optional)
  1351. * @return array Courses list
  1352. */
  1353. public static function get_course_list_in_session_from_student($user_id, $id_session = 0) {
  1354. $user_id = intval($user_id);
  1355. $id_session = intval($id_session);
  1356. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1357. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user="' . $user_id . '" AND id_session="' . $id_session . '"';
  1358. $result = Database::query($sql);
  1359. $a_courses = array ();
  1360. while ($row = Database::fetch_array($result)) {
  1361. $a_courses[$row['course_code']] = $row['course_code'];
  1362. }
  1363. return $a_courses;
  1364. }
  1365. /**
  1366. * Get inactives students in course
  1367. * @param string Course code
  1368. * @param string Since login course date (optional, default = 'never')
  1369. * @param int Session id (optional)
  1370. * @return array Inactives users
  1371. */
  1372. public static function get_inactives_students_in_course($course_code, $since = 'never', $session_id=0) {
  1373. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1374. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1375. $table_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1376. $inner = '';
  1377. if($session_id!=0)
  1378. {
  1379. $inner = ' INNER JOIN '.$tbl_session_course_user.' session_course_user
  1380. ON stats_login.course_code = session_course_user.course_code
  1381. AND session_course_user.id_session = '.intval($session_id).'
  1382. AND session_course_user.id_user = stats_login.user_id ';
  1383. }
  1384. $sql = 'SELECT user_id, MAX(login_course_date) max_date FROM'.$tbl_track_login.' stats_login'.$inner.'
  1385. GROUP BY user_id
  1386. HAVING DATE_SUB( NOW(), INTERVAL '.$since.' DAY) > max_date ';
  1387. //HAVING DATE_ADD(max_date, INTERVAL '.$since.' DAY) < NOW() ';
  1388. if ($since == 'never') {
  1389. $sql = 'SELECT course_user.user_id FROM '.$table_course_rel_user.' course_user
  1390. LEFT JOIN '. $tbl_track_login.' stats_login
  1391. ON course_user.user_id = stats_login.user_id AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' '.
  1392. $inner.'
  1393. WHERE course_user.course_code = \''.Database::escape_string($course_code).'\'
  1394. AND stats_login.login_course_date IS NULL
  1395. GROUP BY course_user.user_id';
  1396. }
  1397. $rs = Database::query($sql);
  1398. $inactive_users = array();
  1399. while($user = Database::fetch_array($rs))
  1400. {
  1401. $inactive_users[] = $user['user_id'];
  1402. }
  1403. return $inactive_users;
  1404. }
  1405. /**
  1406. * Get count login per student
  1407. * @param int Student id
  1408. * @param string Course code
  1409. * @param int Session id (optional)
  1410. * @return int count login
  1411. */
  1412. public static function count_login_per_student($student_id, $course_code, $session_id = 0) {
  1413. $student_id = intval($student_id);
  1414. $course_code = Database::escape_string($course_code);
  1415. $session_id = intval($session_id);
  1416. $tbl_course_rel_user = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1417. $sql = 'SELECT '.$student_id.'
  1418. FROM ' . $tbl_course_rel_user . '
  1419. WHERE access_user_id=' . $student_id . '
  1420. AND access_cours_code="' . $course_code . '" AND access_session_id = "'.$session_id.'" ';
  1421. $rs = Database::query($sql);
  1422. $nb_login = Database::num_rows($rs);
  1423. return $nb_login;
  1424. }
  1425. /**
  1426. * Get students followed by a human resources manager
  1427. * @param int Drh id
  1428. * @return array Student list
  1429. */
  1430. public static function get_student_followed_by_drh($hr_dept_id) {
  1431. $hr_dept_id = intval($hr_dept_id);
  1432. $a_students = array ();
  1433. $tbl_organism = Database :: get_main_table(TABLE_MAIN_ORGANISM);
  1434. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  1435. $sql = 'SELECT DISTINCT user_id FROM '.$tbl_user.' as user
  1436. WHERE hr_dept_id='.$hr_dept_id;
  1437. $rs = Database::query($sql);
  1438. while($user = Database :: fetch_array($rs))
  1439. {
  1440. $a_students[$user['user_id']] = $user['user_id'];
  1441. }
  1442. return $a_students;
  1443. }
  1444. /**
  1445. * Gets the average of test and scorm inside a learning path
  1446. * @param int User id
  1447. * @param string Course id
  1448. * @return float average of test
  1449. * @author isaac flores paz <florespaz@bidsoftperu.com>
  1450. * @deprecated get_avg_student_score should be use
  1451. */
  1452. public static function get_average_test_scorm_and_lp ($user_id,$course_id) {
  1453. //the score inside the Reporting table
  1454. $course_info = api_get_course_info($course_id);
  1455. $lp_table = Database :: get_course_table(TABLE_LP_MAIN,$course_info['dbName']);
  1456. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW,$course_info['dbName']);
  1457. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW,$course_info['dbName']);
  1458. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM,$course_info['dbName']);
  1459. $sql_type='SELECT id, lp_type FROM '.$lp_table;
  1460. $rs_type=Database::query($sql_type);
  1461. $average_data=0;
  1462. $count_loop=0;
  1463. $lp_list = array();
  1464. while ($row_type=Database::fetch_array($rs_type)) {
  1465. $lp_list[] = $row_type['id'];
  1466. if ($row_type['lp_type']==1) {
  1467. //lp dokeos
  1468. $sql = "SELECT id FROM $lp_view_table WHERE user_id = '".intval($user_id)."' and lp_id='".$row_type['id']."'";
  1469. $rs_last_lp_view_id = Database::query($sql);
  1470. $lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
  1471. $sql_list_view='SELECT li.max_score,lv.user_id,liw.score,(liw.score/li.max_score) as sum_data FROM '.$lp_item_table.' li INNER JOIN '.$lp_view_table.' lv
  1472. ON li.lp_id=lv.lp_id INNER JOIN '.$lp_item_view_table.' liw ON liw.lp_item_id=li.id WHERE lv.user_id="'.$user_id.'" AND li.item_type="quiz" AND liw.lp_view_id="'.$lp_view_id.'"';
  1473. $sum=0;
  1474. $tot=0;
  1475. $rs_list_view1=Database::query($sql_list_view);
  1476. while ($row_list_view=Database::fetch_array($rs_list_view1)) {
  1477. $sum=$sum+$row_list_view['sum_data'];
  1478. $tot++;
  1479. }
  1480. if ($tot==0) {
  1481. $tot=1;
  1482. }
  1483. $average_data1=$sum/$tot;
  1484. $sql_list_view='';
  1485. $rs_last_lp_view_id='';
  1486. } elseif ($row_type['lp_type']==2) {//lp scorm
  1487. $sql = "SELECT id FROM $lp_view_table WHERE user_id = '".intval($user_id)."' and lp_id='".$row_type['id']."'";
  1488. $rs_last_lp_view_id = Database::query($sql);
  1489. $lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
  1490. $sql_list_view='SELECT li.max_score,lv.user_id,liw.score,((liw.score/li.max_score)*100) as sum_data FROM '.$lp_item_table.' li INNER JOIN '.$lp_view_table.' lv
  1491. ON li.lp_id=lv.lp_id INNER JOIN '.$lp_item_view_table.' liw ON liw.lp_item_id=li.id WHERE lv.user_id="'.$user_id.'" AND (li.item_type="sco" OR li.item_type="quiz") AND liw.lp_view_id="'.$lp_view_id.'"';
  1492. $tot=0;
  1493. $sum=0;
  1494. $rs_list_view2=Database::query($sql_list_view);
  1495. while ($row_list_view=Database::fetch_array($rs_list_view2)) {
  1496. $sum=$sum+$row_list_view['sum_data'];
  1497. $tot++;
  1498. }
  1499. if ($tot==0) {
  1500. $tot=1;
  1501. }
  1502. $average_data2=$sum/$tot;
  1503. }
  1504. $average_data_sum=$average_data_sum+$average_data1+$average_data2;
  1505. $average_data2=0;
  1506. $average_data1=0;
  1507. $count_loop++;
  1508. }
  1509. //We only count the LP that have an exercise to get the average
  1510. $lp_with_quiz = 0;
  1511. foreach($lp_list as $lp_id) {
  1512. //check if LP have a score
  1513. $sql = "SELECT count(id) as count FROM $lp_item_table
  1514. WHERE item_type = 'quiz' AND lp_id = ".$lp_id." ";
  1515. $result_have_quiz = Database::query($sql);
  1516. if (Database::num_rows($result_have_quiz) > 0 ) {
  1517. $row = Database::fetch_array($result_have_quiz,'ASSOC');
  1518. if (is_numeric($row['count']) && $row['count'] != 0) {
  1519. $lp_with_quiz++;
  1520. }
  1521. }
  1522. }
  1523. if ($lp_with_quiz > 0) {
  1524. $avg_student_score = round(($average_data_sum / $lp_with_quiz * 100), 2);
  1525. }
  1526. return $avg_student_score;
  1527. }
  1528. /**
  1529. * get count clicks about tools most used by course
  1530. * @param string Course code
  1531. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1532. * @return array tools data
  1533. */
  1534. public static function get_tools_most_used_by_course($course_code, $session_id = null) {
  1535. //protect data
  1536. $course_code = Database::escape_string($course_code);
  1537. $data = array();
  1538. $TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1539. $condition_session = '';
  1540. if (isset($session_id)) {
  1541. $session_id = intval($session_id);
  1542. $condition_session = ' AND access_session_id = '. $session_id;
  1543. }
  1544. $sql = "SELECT access_tool, COUNT(DISTINCT access_user_id),count( access_tool ) as count_access_tool
  1545. FROM $TABLETRACK_ACCESS
  1546. WHERE access_tool IS NOT NULL
  1547. AND access_cours_code = '$course_code'
  1548. $condition_session
  1549. GROUP BY access_tool
  1550. ORDER BY count_access_tool DESC
  1551. LIMIT 0, 3";
  1552. $rs = Database::query($sql);
  1553. if (Database::num_rows($rs) > 0) {
  1554. while ($row = Database::fetch_array($rs)) {
  1555. $data[] = $row;
  1556. }
  1557. }
  1558. return $data;
  1559. }
  1560. /**
  1561. * get documents most downloaded by course
  1562. * @param string Course code
  1563. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1564. * @param int Limit (optional, default = 0, 0 = without limit)
  1565. * @return array documents downloaded
  1566. */
  1567. public static function get_documents_most_downloaded_by_course($course_code, $session_id = null, $limit = 0) {
  1568. //protect data
  1569. $course_code = Database::escape_string($course_code);
  1570. $data = array();
  1571. $TABLETRACK_DOWNLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1572. $condition_session = '';
  1573. if (isset($session_id)) {
  1574. $session_id = intval($session_id);
  1575. $condition_session = ' AND down_session_id = '. $session_id;
  1576. }
  1577. $sql = "SELECT down_doc_path, COUNT(DISTINCT down_user_id), COUNT(down_doc_path) as count_down
  1578. FROM $TABLETRACK_DOWNLOADS
  1579. WHERE down_cours_id = '$course_code'
  1580. $condition_session
  1581. GROUP BY down_doc_path
  1582. ORDER BY count_down DESC
  1583. LIMIT 0, $limit";
  1584. $rs = Database::query($sql);
  1585. if (Database::num_rows($rs) > 0) {
  1586. while ($row = Database::fetch_array($rs)) {
  1587. $data[] = $row;
  1588. }
  1589. }
  1590. return $data;
  1591. }
  1592. /**
  1593. * get links most visited by course
  1594. * @param string Course code
  1595. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1596. * @return array links most visited
  1597. */
  1598. public static function get_links_most_visited_by_course($course_code, $session_id = null) {
  1599. //protect data
  1600. $course_code = Database::escape_string($course_code);
  1601. $course_info=api_get_course_info($course_code);
  1602. $data = array();
  1603. $TABLETRACK_LINKS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1604. $TABLECOURSE_LINKS = Database::get_course_table(TABLE_LINK, $course_info['dbName']);
  1605. $condition_session = '';
  1606. if (isset($session_id)) {
  1607. $session_id = intval($session_id);
  1608. $condition_session = ' AND cl.session_id = '.$session_id;
  1609. }
  1610. $sql = "SELECT cl.title, cl.url,count(DISTINCT sl.links_user_id), count(cl.title) as count_visits
  1611. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  1612. WHERE sl.links_link_id = cl.id
  1613. AND sl.links_cours_id = '$_cid'
  1614. $condition_session
  1615. GROUP BY cl.title, cl.url
  1616. ORDER BY count_visits DESC
  1617. LIMIT 0, 3";
  1618. $rs = Database::query($sql);
  1619. if (Database::num_rows($rs) > 0) {
  1620. while ($row = Database::fetch_array($rs)) {
  1621. $data[] = $row;
  1622. }
  1623. }
  1624. return $data;
  1625. }
  1626. /**
  1627. * Shows the user progress (when clicking in the Progress tab)
  1628. * @param int user id
  1629. * @return string html code
  1630. */
  1631. function show_user_progress($user_id, $session_id = 0, $extra_params = '', $show_courses = true) {
  1632. global $_configuration;
  1633. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1634. $tbl_access_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1635. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1636. $tbl_access_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1637. $tbl_access_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1638. $user_id = intval($user_id);
  1639. // get course list
  1640. if ($_configuration['multiple_access_urls']) {
  1641. $sql = 'SELECT cu.course_code FROM '.$tbl_course_user.' cu INNER JOIN '.$tbl_access_rel_course.' a ON(a.course_code = cu.course_code) WHERE user_id='.$user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND access_url_id = '.api_get_current_access_url_id().'';
  1642. } else {
  1643. $sql = 'SELECT course_code FROM '.$tbl_course_user.' WHERE user_id='.$user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' ';
  1644. }
  1645. $rs = Database::query($sql);
  1646. $courses = $course_in_session = array();
  1647. while($row = Database :: fetch_array($rs)) {
  1648. $courses[$row['course_code']] = CourseManager::get_course_information($row['course_code']);
  1649. }
  1650. // Get the list of sessions where the user is subscribed as student
  1651. if ($_configuration['multiple_access_urls']) {
  1652. $sql = 'SELECT DISTINCT cu.course_code, id_session as session_id FROM '.$tbl_session_course_user.' cu INNER JOIN '.$tbl_access_rel_session.' a ON(a.session_id = cu.id_session) WHERE id_user='.$user_id.' AND access_url_id = '.api_get_current_access_url_id().'';
  1653. } else {
  1654. $sql = 'SELECT DISTINCT course_code, id_session as session_id FROM '.$tbl_session_course_user.' WHERE id_user='.$user_id;
  1655. }
  1656. $rs = Database::query($sql);
  1657. while($row = Database :: fetch_array($rs)) {
  1658. $course_in_session[$row['session_id']][$row['course_code']] = CourseManager::get_course_information($row['course_code']);
  1659. }
  1660. $html = '';
  1661. if ($show_courses) {
  1662. if (!empty($courses)) {
  1663. $html = '<table class="data_table" width="100%">';
  1664. $html .= '<tr class="tableName">
  1665. <td colspan="6">
  1666. <h1>'.get_lang('MyCourses').'</h1>
  1667. </td>
  1668. </tr>';
  1669. $html .= '
  1670. <tr>
  1671. <th width="300px">'.get_lang('Course').'</th>
  1672. '.Display::tag('th', get_lang('Time'), array('class'=>'head')).'
  1673. '.Display::tag('th', get_lang('Progress'), array('class'=>'head')).'
  1674. '.Display::tag('th', get_lang('Score').Display :: return_icon('info3.gif', get_lang('ScormAndLPTestTotalAverage'), array('align' => 'absmiddle', 'hspace' => '3px')),array('class'=>'head')).'
  1675. '.Display::tag('th', get_lang('LastConnexion'), array('class'=>'head')).'
  1676. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  1677. </tr>';
  1678. $i = 0;
  1679. foreach ($courses as $enreg) {
  1680. $weighting = 0;
  1681. $total_time_login = Tracking :: get_time_spent_on_the_course($user_id, $enreg['code']);
  1682. $time = api_time_to_hms($total_time_login);
  1683. $progress = Tracking :: get_avg_student_progress($user_id, $enreg['code']);
  1684. $percentage_score = Tracking :: get_avg_student_score($user_id, $enreg['code'], array());
  1685. $last_connection = Tracking :: get_last_connection_date_on_the_course($user_id, $enreg['code']);
  1686. if ($enreg['code'] == $_GET['course'] && empty($_GET['session_id'])) {
  1687. $html .= '<tr class="row_odd" style="background-color:#FBF09D">';
  1688. } else {
  1689. $html .= '<tr class="row_even">';
  1690. }
  1691. $html .= '<td>'.$enreg['title'].'</td>';
  1692. $html .= '<td align="center">'.$time.'</td>';
  1693. $html .= '<td align="center">'.$progress.'%</td>';
  1694. $html .= '<td align="center">';
  1695. if (is_numeric($percentage_score)) {
  1696. $html .= $percentage_score.'%';
  1697. } else {
  1698. $html .= '0%';
  1699. }
  1700. $html .= '</td>';
  1701. $html .= '<td align="center">'.$last_connection.'</td>';
  1702. $html .= '<td align="center">';
  1703. if ($enreg['code'] == $_GET['course'] && empty($_GET['session_id'])) {
  1704. $html .= '<a href="#">';
  1705. $html .= Display::return_icon('2rightarrow_na.gif', get_lang('Details'));
  1706. } else {
  1707. $html .= '<a href="'.api_get_self().'?course='.$enreg['code'].$extra_params.'">';
  1708. $html .= Display::return_icon('2rightarrow.gif', get_lang('Details'));
  1709. }
  1710. $html .= '</a>';
  1711. $html .= '</td></tr>';
  1712. $i = $i ? 0 : 1;
  1713. }
  1714. $html .= '</table>';
  1715. }
  1716. }
  1717. //Courses in a session
  1718. if (!empty($course_in_session)) {
  1719. $html .= '<br />';
  1720. $html .= Display::tag('h1',get_lang('Sessions'));
  1721. foreach ($course_in_session as $key=>$session) {
  1722. if (isset($session_id) && !empty($session_id)) {
  1723. if ($session_id != $key) {
  1724. continue;
  1725. }
  1726. }
  1727. $html .= Display::tag('h2',api_get_session_name($key));
  1728. $html .= '
  1729. <table class="data_table" width="100%">';
  1730. $html .= '
  1731. <tr>
  1732. <th width="300px">'.get_lang('Course').'</th>
  1733. '.Display::tag('th', get_lang('PublishedExercises'),array('class'=>'head')).'
  1734. '.Display::tag('th', get_lang('DoneExercises'), array('class'=>'head')).'
  1735. '.Display::tag('th', get_lang('AverageExerciseResult'), array('class'=>'head')).'
  1736. '.Display::tag('th', get_lang('Time') , array('class'=>'head')).'
  1737. '.Display::tag('th', get_lang('LPProgress') , array('class'=>'head')).'
  1738. '.Display::tag('th', get_lang('Score').Display :: return_icon('info3.gif', get_lang('ScormAndLPTestTotalAverage'), array ('align' => 'absmiddle', 'hspace' => '3px')), array('class'=>'head')).'
  1739. '.Display::tag('th', get_lang('LastConnexion'), array('class'=>'head')).'
  1740. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  1741. </tr>';
  1742. foreach ($session as $enreg) {
  1743. $weighting = 0;
  1744. $last_connection = Tracking :: get_last_connection_date_on_the_course($user_id, $enreg['code'], $key);
  1745. $progress = Tracking :: get_avg_student_progress($user_id, $enreg['code'],array(), $key);
  1746. $total_time_login = Tracking :: get_time_spent_on_the_course($user_id, $enreg['code'], $key);
  1747. $time = api_time_to_hms($total_time_login);
  1748. $percentage_score = Tracking :: get_avg_student_score($user_id, $enreg['code'], array(), $key);
  1749. if ($enreg['code'] == $_GET['course'] && $_GET['session_id'] == $key) {
  1750. $html .= '<tr class="row_odd" style="background-color:#FBF09D" >';
  1751. } else {
  1752. $html .= '<tr class="row_even">';
  1753. }
  1754. $html .= Display::tag('td', $enreg['title']);
  1755. //All exercises in the course
  1756. $exercises = get_all_exercises($enreg, $key);
  1757. //Count of user results
  1758. $done_exercises = get_all_exercise_results_by_course($enreg['code'], $key);
  1759. //Averaga
  1760. $averate = get_average_score_by_course($enreg['code'], $key);
  1761. $html .= Display::tag('td', count($exercises));
  1762. $html .= Display::tag('td', $done_exercises);
  1763. $html .= Display::tag('td', $averate);
  1764. $html .= Display::tag('td', $time, array('align'=>'center'));
  1765. if (is_numeric($progress)) {
  1766. $progress = $progress.'%';
  1767. } else {
  1768. $progress = '0%';
  1769. }
  1770. $html .= Display::tag('td', $progress, array('align'=>'center'));
  1771. if (is_numeric($percentage_score)) {
  1772. $percentage_score = $percentage_score.'%';
  1773. } else {
  1774. $percentage_score = '0%';
  1775. }
  1776. $html .= Display::tag('td', $percentage_score, array('align'=>'center'));
  1777. $html .= Display::tag('td', $last_connection, array('align'=>'center'));
  1778. if ($enreg['code'] == $_GET['course'] && $_GET['session_id'] == $key) {
  1779. $details = '<a href="#">';
  1780. $details .=Display::return_icon('2rightarrow_na.gif', get_lang('Details'));
  1781. } else {
  1782. $details = '<a href="'.api_get_self().'?course='.$enreg['code'].'&session_id='.$key.$extra_params.'">';
  1783. $details .=Display::return_icon('2rightarrow.gif', get_lang('Details'));
  1784. }
  1785. $details .= '</a>';
  1786. $html .= Display::tag('td', $details, array('align'=>'center'));
  1787. $i = $i ? 0 : 1;
  1788. $html .= '</tr>';
  1789. }
  1790. $html .= '</table>';
  1791. }
  1792. }
  1793. return $html;
  1794. }
  1795. /**
  1796. * Shows the user detail progress (when clicking in the details link)
  1797. * @param int user id
  1798. * @param string course code
  1799. * @param int session id
  1800. * @return string html code
  1801. */
  1802. function show_course_detail($user_id, $course_code, $session_id) {
  1803. $html = '';
  1804. if (isset($course_code)) {
  1805. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  1806. $user_id = intval($user_id);
  1807. $session_id = intval($session_id);
  1808. $course = Database::escape_string($course_code);
  1809. $course_info = CourseManager::get_course_information($course);
  1810. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  1811. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1812. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1813. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1814. $tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW, $course_info['db_name']);
  1815. $tbl_course_lp_view_item = Database :: get_course_table(TABLE_LP_ITEM_VIEW, $course_info['db_name']);
  1816. $tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN, $course_info['db_name']);
  1817. $tbl_course_lp_item = Database :: get_course_table(TABLE_LP_ITEM, $course_info['db_name']);
  1818. $tbl_course_quiz = Database :: get_course_table(TABLE_QUIZ_TEST, $course_info['db_name']);
  1819. //get coach and session_name if there is one and if session_mode is activated
  1820. /*
  1821. if (api_get_setting('use_session_mode') == 'true') {
  1822. if ($_configuration['multiple_access_urls']) {
  1823. $sql = 'SELECT id_session
  1824. FROM '.$tbl_session_course_user.' session_course_user INNER JOIN '.$tbl_access_rel_session.' a ON(session_course_user.id_session = a.session_id)
  1825. WHERE session_course_user.id_user = '.intval($_user['user_id']).'
  1826. AND session_course_user.course_code = "'.Database::escape_string($course).'" AND access_url_id = '.api_get_current_access_url_id().'
  1827. ORDER BY id_session DESC';
  1828. } else {
  1829. $sql = 'SELECT id_session
  1830. FROM '.$tbl_session_course_user.' session_course_user
  1831. WHERE session_course_user.id_user = '.intval($_user['user_id']).'
  1832. AND session_course_user.course_code = "'.Database::escape_string($course).'"
  1833. ORDER BY id_session DESC';
  1834. }
  1835. $rs = Database::query($sql);
  1836. $row = Database::fetch_array($rs);
  1837. if ($session_id > 0) {
  1838. // get session name and coach of the session
  1839. $sql = 'SELECT name, id_coach FROM '.$tbl_session.'
  1840. WHERE id='.$session_id;
  1841. $rs = Database::query($sql);
  1842. $session_name = Database::result($rs, 0, 'name');
  1843. $session_coach_id = intval(Database::result($rs, 0, 'id_coach'));
  1844. $sql = 'SELECT id_user FROM ' . $tbl_session_course_user . '
  1845. WHERE id_session=' . $session_id . '
  1846. AND course_code = "' . Database :: escape_string($course) . '" AND status=2';
  1847. $rs = Database::query($sql);
  1848. $course_coachs = array();
  1849. while ($row_coachs = Database::fetch_array($rs)) {
  1850. $course_coachs[] = $row_coachs['id_user'];
  1851. }
  1852. if (!empty($course_coachs)) {
  1853. $info_tutor_name = array();
  1854. foreach ($course_coachs as $course_coach) {
  1855. $coach_infos = UserManager :: get_user_info_by_id($course_coach);
  1856. $info_tutor_name[] = api_get_person_name($coach_infos['firstname'], $coach_infos['lastname']);
  1857. }
  1858. $course_info['tutor_name'] = implode(",",$info_tutor_name);
  1859. } else if($session_coach_id != 0) {
  1860. $coach_info = UserManager :: get_user_info_by_id($session_coach_id);
  1861. $course_info['tutor_name'] = api_get_person_name($coach_info['firstname'], $coach_info['lastname']);
  1862. }
  1863. }
  1864. } // end if (api_get_setting('use_session_mode') == 'true')*/
  1865. //$tableTitle = $course_info['title'].' | '.get_lang('Coach').' : '.$course_info['tutor_name'].((!empty($session_name)) ? ' | '.get_lang('Session').' : '.$session_name : '');
  1866. $session_name = api_get_session_name($session_id);
  1867. $tableTitle = $course_info['title'];
  1868. $html .='
  1869. <table class="data_table" width="100%">
  1870. <tr class="tableName">
  1871. <td colspan="4">
  1872. '.Display::tag('h3', $tableTitle).'
  1873. </td>
  1874. </tr><tr>';
  1875. $html .= Display::tag('th', get_lang('Learnpath'), array('class'=>'head', 'style'=>'color:#000'));
  1876. $html .= Display::tag('th', get_lang('Time'), array('class'=>'head', 'style'=>'color:#000'));
  1877. $html .= Display::tag('th', get_lang('Progress'), array('class'=>'head', 'style'=>'color:#000'));
  1878. $html .= Display::tag('th', get_lang('LastConnexion'), array('class'=>'head', 'style'=>'color:#000'));
  1879. $html .= '</tr>';
  1880. if (empty($session_id)) {
  1881. $sql_learnpath = "SELECT lp.name,lp.id FROM ".$tbl_course_lp." AS lp WHERE session_id = 0 ORDER BY lp.display_order";
  1882. } else {
  1883. $sql_learnpath = "SELECT lp.name,lp.id FROM ".$tbl_course_lp." AS lp ORDER BY lp.display_order";
  1884. }
  1885. $result_learnpath = Database::query($sql_learnpath);
  1886. if (Database::num_rows($result_learnpath) > 0) {
  1887. while($learnpath = Database::fetch_array($result_learnpath)) {
  1888. $progress = Tracking::get_avg_student_progress($user_id, $course, array($learnpath['id']), $session_id);
  1889. $last_connection_in_lp = Tracking::get_last_connection_time_in_lp($user_id, $course, $learnpath['id'], $session_id);
  1890. $time_spent_in_lp = Tracking::get_time_spent_in_lp($user_id, $course, array($learnpath['id']), $session_id);
  1891. $time_spent_in_lp = api_time_to_hms($time_spent_in_lp);
  1892. $html .= "<tr>";
  1893. $html .= Display::tag('td', $learnpath['name']);
  1894. $html .= Display::tag('td', $time_spent_in_lp, array('align'=>'center'));
  1895. if (is_numeric($progress)) {
  1896. $progress = $progress.'%';
  1897. }
  1898. $html .= Display::tag('td', $progress, array('align'=>'center'));
  1899. $last_connection = '-';
  1900. if (!empty($last_connection_in_lp)) {
  1901. $last_connection = api_get_utc_datetime($last_connection_in_lp);
  1902. }
  1903. $html .= Display::tag('td', $last_connection, array('align'=>'center','width'=>'180px'));
  1904. $html .= "</tr>";
  1905. }
  1906. } else {
  1907. $html .= '<tr>
  1908. <td colspan="4" align="center">
  1909. '.get_lang('NoLearnpath').'
  1910. </td>
  1911. </tr>';
  1912. }
  1913. $html .='</table><br />
  1914. <table class="data_table" width="100%">
  1915. <tr>';
  1916. // This code was commented on purpose see BT#924
  1917. /*$sql = 'SELECT visibility FROM '.$course_info['db_name'].'.'.TABLE_TOOL_LIST.' WHERE name="quiz"';
  1918. $result_visibility_tests = Database::query($sql);
  1919. if (Database::result($result_visibility_tests, 0, 'visibility') == 1) {*/
  1920. if (empty($session_id)) {
  1921. $sql_exercices = "SELECT quiz.title,id, results_disabled FROM ".$tbl_course_quiz." AS quiz WHERE active='1' AND session_id = 0";
  1922. } else {
  1923. $sql_exercices = "SELECT quiz.title,id, results_disabled FROM ".$tbl_course_quiz." AS quiz WHERE active='1'";
  1924. }
  1925. $html .= '<tr>
  1926. <th class="head" style="color:#000">'.get_lang('Exercices').'</th>
  1927. <th class="head" style="color:#000">'.get_lang('Attempts').'</th>
  1928. <th class="head" style="color:#000">'.get_lang('LatestAttempt').'</th>
  1929. <th class="head" style="color:#000">'.get_lang('Position').'</th>
  1930. <th class="head" style="color:#000">'.get_lang('BestResultInCourse').'</th>
  1931. </tr>';
  1932. $result_exercices = Database::query($sql_exercices);
  1933. if (Database::num_rows($result_exercices) > 0) {
  1934. while ($exercices = Database::fetch_array($result_exercices)) {
  1935. $score = $weighting = $attempts = 0;
  1936. $exercise_stats = get_all_exercise_results($exercices['id'], $course_info['code'], $session_id);
  1937. //User attempts we assume the latest item in the loop is the latest attempt
  1938. if (!empty($exercise_stats)) {
  1939. //$best_score = 0; $best_score_array = array();
  1940. foreach($exercise_stats as $exercise_stat) {
  1941. if ($exercise_stat['exe_user_id'] == $user_id) {
  1942. //Always getting the latest attempt
  1943. $score = $exercise_stat['exe_result'];
  1944. $weighting = $exercise_stat['exe_weighting'];
  1945. $exe_id = $exercise_stat['exe_id'];
  1946. //Use this to take the average
  1947. //$score = $score + $exercise_stat['exe_result'];
  1948. //$weighting = $weighting + $exercise_stat['exe_weighting'];
  1949. //Getting my best score
  1950. /*
  1951. if ($score > $best_score ) {
  1952. $best_score = $score;
  1953. $best_score_array = $exercise_stat;
  1954. }*/
  1955. $attempts++;
  1956. }
  1957. }
  1958. }
  1959. $html .= '<tr>';
  1960. $html .= Display::tag('td', $exercices['title']);
  1961. //Exercise configuration show results
  1962. if ($exercices['results_disabled'] == 0) {
  1963. $latest_attempt_url = '';
  1964. $percentage_score_result = '-';
  1965. $position = '-';
  1966. $best_score = '-';
  1967. $best_score_data = get_best_score($exercices['id'], $course_info['code'], $session_id);
  1968. $best_score = show_score($best_score_data['exe_result'], $best_score_data['exe_weighting']);
  1969. if ($attempts > 0) {
  1970. $latest_attempt_url .= '<a href="../exercice/exercise_show.php?origin=myprogress&id='.$exe_id.'&cidReq='.$course_info['code'].'&id_session='.$session_id.'"> '.Display::return_icon('quiz.gif', get_lang('Quiz')).' </a>';
  1971. $percentage_score_result = show_score($score, $weighting).' '.$latest_attempt_url;
  1972. $my_score = 0;
  1973. if (!empty($weighting)) {
  1974. $my_score = $score/$weighting;
  1975. }
  1976. $position = get_exercise_result_ranking($my_score, $exe_id, $exercices['id'], $course_info['code'], $session_id);
  1977. }
  1978. $html .= Display::tag('td', $attempts, array('align'=>'center'));
  1979. $html .= Display::tag('td', $percentage_score_result, array('align'=>'center'));
  1980. $html .= Display::tag('td', $position, array('align'=>'center'));
  1981. $html .= Display::tag('td', $best_score, array('align'=>'center'));
  1982. //$html .= Display::tag('td', $latest_attempt_url, array('align'=>'center', 'width'=>'25'));
  1983. } else {
  1984. // Exercise configuration NO results
  1985. $html .= Display::tag('td', get_lang('CantShowResults'), array('align'=>'center'));
  1986. $html .= Display::tag('td', '--', array('align'=>'center'));
  1987. $html .= Display::tag('td', '--', array('align'=>'center'));
  1988. $html .= Display::tag('td', '--', array('align'=>'center'));
  1989. $html .= Display::tag('td', '--', array('align'=>'center'));
  1990. }
  1991. $html .= '</tr>';
  1992. }
  1993. } else {
  1994. $html .= '<tr><td colspan="5" align="center">'.get_lang('NoEx').'</td></tr>';
  1995. }
  1996. $html .= '</table>';
  1997. }
  1998. return $html;
  1999. }
  2000. }
  2001. class TrackingCourseLog {
  2002. function count_item_resources() {
  2003. global $session_id;
  2004. $table_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2005. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  2006. $sql = "SELECT count(tool) AS total_number_of_items FROM $table_item_property track_resource, $table_user user" .
  2007. " WHERE track_resource.insert_user_id = user.user_id AND id_session = $session_id ";
  2008. if (isset($_GET['keyword'])) {
  2009. $keyword = Database::escape_string(trim($_GET['keyword']));
  2010. $sql .= " AND (user.username LIKE '%".$keyword."%' OR lastedit_type LIKE '%".$keyword."%' OR tool LIKE '%".$keyword."%')";
  2011. }
  2012. $sql .= " AND tool IN ('document', 'learnpath', 'quiz', 'glossary', 'link', 'course_description', 'announcement', 'thematic', 'thematic_advance', 'thematic_plan')";
  2013. $res = Database::query($sql);
  2014. $obj = Database::fetch_object($res);
  2015. return $obj->total_number_of_items;
  2016. }
  2017. function get_item_resources_data($from, $number_of_items, $column, $direction) {
  2018. global $dateTimeFormatLong, $session_id;
  2019. $table_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2020. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  2021. $table_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  2022. $sql = "SELECT
  2023. tool as col0,
  2024. lastedit_type as col1,
  2025. ref as ref,
  2026. user.username as col3,
  2027. insert_date as col5,
  2028. visibility as col6
  2029. FROM $table_item_property track_resource, $table_user user
  2030. WHERE track_resource.insert_user_id = user.user_id AND id_session = $session_id ";
  2031. if (isset($_GET['keyword'])) {
  2032. $keyword = Database::escape_string(trim($_GET['keyword']));
  2033. $sql .= " AND (user.username LIKE '%".$keyword."%' OR lastedit_type LIKE '%".$keyword."%' OR tool LIKE '%".$keyword."%') ";
  2034. }
  2035. $sql .= " AND tool IN ('document', 'learnpath', 'quiz', 'glossary', 'link', 'course_description', 'announcement', 'thematic', 'thematic_advance', 'thematic_plan')";
  2036. if ($column == 0) { $column = '0'; }
  2037. if ($column != '' && $direction != '') {
  2038. if ($column != 2 && $column != 4) {
  2039. $sql .= " ORDER BY col$column $direction";
  2040. }
  2041. } else {
  2042. $sql .= " ORDER BY col5 DESC ";
  2043. }
  2044. $sql .= " LIMIT $from, $number_of_items ";
  2045. $res = Database::query($sql);
  2046. $resources = array ();
  2047. $thematic_tools = array('thematic', 'thematic_advance', 'thematic_plan');
  2048. while ($row = Database::fetch_array($res)) {
  2049. $ref = $row['ref'];
  2050. $table_name = TrackingCourseLog::get_tool_name_table($row['col0']);
  2051. $table_tool = Database :: get_course_table($table_name['table_name']);
  2052. $id = $table_name['id_tool'];
  2053. if ($row['col0'] == 'thematic_plan' || $row['col0'] == 'thematic_advance') {
  2054. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  2055. $rs_thematic = Database::query("SELECT thematic_id FROM $table_tool WHERE id=$ref");
  2056. $row_thematic = Database::fetch_array($rs_thematic);
  2057. $thematic_id = $row_thematic['thematic_id'];
  2058. $query = "SELECT session.id, session.name, user.username FROM $tbl_thematic t, $table_session session, $table_user user" .
  2059. " WHERE t.session_id = session.id AND session.id_coach = user.user_id AND t.id = $thematic_id";
  2060. } else {
  2061. $query = "SELECT session.id, session.name, user.username FROM $table_tool tool, $table_session session, $table_user user" .
  2062. " WHERE tool.session_id = session.id AND session.id_coach = user.user_id AND tool.$id = $ref";
  2063. }
  2064. $recorset = Database::query($query);
  2065. if (!empty($recorset)) {
  2066. $obj = Database::fetch_object($recorset);
  2067. $name_session = '';
  2068. $coach_name = '';
  2069. if (!empty($obj)) {
  2070. $name_session = $obj->name;
  2071. $coach_name = $obj->username;
  2072. }
  2073. $url_tool = api_get_path(WEB_CODE_PATH).$table_name['link_tool'];
  2074. $row[0] = '';
  2075. if ($row['col6'] != 2) {
  2076. if (in_array($row['col0'], $thematic_tools)) {
  2077. $exp_thematic_tool = explode('_', $row['col0']);
  2078. $thematic_tool_title = '';
  2079. if (is_array($exp_thematic_tool)) {
  2080. foreach ($exp_thematic_tool as $exp) {
  2081. $thematic_tool_title .= api_ucfirst($exp);
  2082. }
  2083. } else {
  2084. $thematic_tool_title = api_ucfirst($row['col0']);
  2085. }
  2086. $row[0] = '<a href="'.$url_tool.'?'.api_get_cidreq().'&action=thematic_details">'.get_lang($thematic_tool_title).'</a>';
  2087. } else {
  2088. $row[0] = '<a href="'.$url_tool.'?'.api_get_cidreq().'&'.$obj->id.'">'.get_lang('Tool'.api_ucfirst($row['col0'])).'</a>';
  2089. }
  2090. } else {
  2091. $row[0] = api_ucfirst($row['col0']);
  2092. }
  2093. $row[1] = get_lang($row[1]);
  2094. $row[5] = api_convert_and_format_date($row['col5'], null, date_default_timezone_get());
  2095. $row[4] = '';
  2096. switch ($table_name['table_name']) {
  2097. case 'document' :
  2098. $query_document = "SELECT tool.title as title FROM $table_tool tool" .
  2099. " WHERE id = $ref";
  2100. $rs_document = Database::query($query_document);
  2101. $obj_document = Database::fetch_object($rs_document);
  2102. $row[4] = $obj_document->title;
  2103. break;
  2104. case 'announcement':
  2105. $query_document = "SELECT title FROM $table_tool " .
  2106. " WHERE id = $ref";
  2107. $rs_document = Database::query($query_document);
  2108. $obj_document = Database::fetch_object($rs_document);
  2109. $row[4] = $obj_document->title;
  2110. break;
  2111. case 'glossary':
  2112. $query_document = "SELECT name FROM $table_tool " .
  2113. " WHERE glossary_id = $ref";
  2114. $rs_document = Database::query($query_document);
  2115. $obj_document = Database::fetch_object($rs_document);
  2116. $row[4] = $obj_document->name;
  2117. break;
  2118. case 'lp':
  2119. $query_document = "SELECT name FROM $table_tool " .
  2120. " WHERE id = $ref";
  2121. $rs_document = Database::query($query_document);
  2122. $obj_document = Database::fetch_object($rs_document);
  2123. $row[4] = $obj_document->name;
  2124. break;
  2125. case 'quiz':
  2126. $query_document = "SELECT title FROM $table_tool " .
  2127. " WHERE id = $ref";
  2128. $rs_document = Database::query($query_document);
  2129. $obj_document = Database::fetch_object($rs_document);
  2130. $row[4] = $obj_document->title;
  2131. break;
  2132. case 'course_description':
  2133. $query_document = "SELECT title FROM $table_tool " .
  2134. " WHERE id = $ref";
  2135. $rs_document = Database::query($query_document);
  2136. $obj_document = Database::fetch_object($rs_document);
  2137. $row[4] = $obj_document->title;
  2138. break;
  2139. case 'thematic':
  2140. $rs = Database::query("SELECT title FROM $table_tool WHERE id = $ref");
  2141. if (Database::num_rows($rs) > 0) {
  2142. $obj = Database::fetch_object($rs);
  2143. $row[4] = $obj->title;
  2144. }
  2145. break;
  2146. case 'thematic_advance':
  2147. $rs = Database::query("SELECT content FROM $table_tool WHERE id = $ref");
  2148. if (Database::num_rows($rs) > 0) {
  2149. $obj = Database::fetch_object($rs);
  2150. $row[4] = $obj->content;
  2151. }
  2152. break;
  2153. case 'thematic_plan':
  2154. $rs = Database::query("SELECT title FROM $table_tool WHERE id = $ref");
  2155. if (Database::num_rows($rs) > 0) {
  2156. $obj = Database::fetch_object($rs);
  2157. $row[4] = $obj->title;
  2158. }
  2159. break;
  2160. default:
  2161. break;
  2162. }
  2163. $row2 = $name_session;
  2164. if (!empty($coach_name)) {
  2165. $row2 .= '<br />'.get_lang('Coach').': '.$coach_name;
  2166. }
  2167. $row[2] = $row2;
  2168. $resources[] = $row;
  2169. }
  2170. }
  2171. return $resources;
  2172. }
  2173. function get_tool_name_table($tool) {
  2174. switch ($tool) {
  2175. case 'document':
  2176. $table_name = TABLE_DOCUMENT;
  2177. $link_tool = 'document/document.php';
  2178. $id_tool = 'id';
  2179. break;
  2180. case 'learnpath':
  2181. $table_name = TABLE_LP_MAIN;
  2182. $link_tool = 'newscorm/lp_controller.php';
  2183. $id_tool = 'id';
  2184. break;
  2185. case 'quiz':
  2186. $table_name = TABLE_QUIZ_TEST;
  2187. $link_tool = 'exercice/exercice.php';
  2188. $id_tool = 'id';
  2189. break;
  2190. case 'glossary':
  2191. $table_name = TABLE_GLOSSARY;
  2192. $link_tool = 'glossary/index.php';
  2193. $id_tool = 'glossary_id';
  2194. break;
  2195. case 'link':
  2196. $table_name = TABLE_LINK;
  2197. $link_tool = 'link/link.php';
  2198. $id_tool = 'id';
  2199. break;
  2200. case 'course_description':
  2201. $table_name = TABLE_COURSE_DESCRIPTION;
  2202. $link_tool = 'course_description/';
  2203. $id_tool = 'id';
  2204. break;
  2205. case 'announcement':
  2206. $table_name = TABLE_ANNOUNCEMENT;
  2207. $link_tool = 'announcements/announcements.php';
  2208. $id_tool = 'id';
  2209. break;
  2210. case 'thematic':
  2211. $table_name = TABLE_THEMATIC;
  2212. $link_tool = 'course_progress/index.php';
  2213. $id_tool = 'id';
  2214. break;
  2215. case 'thematic_advance':
  2216. $table_name = TABLE_THEMATIC_ADVANCE;
  2217. $link_tool = 'course_progress/index.php';
  2218. $id_tool = 'id';
  2219. break;
  2220. case 'thematic_plan':
  2221. $table_name = TABLE_THEMATIC_PLAN;
  2222. $link_tool = 'course_progress/index.php';
  2223. $id_tool = 'id';
  2224. break;
  2225. default:
  2226. $table_name = $tool;
  2227. break;
  2228. }
  2229. return array('table_name' => $table_name,'link_tool' => $link_tool,'id_tool' => $id_tool);
  2230. }
  2231. function display_additional_profile_fields() {
  2232. // getting all the extra profile fields that are defined by the platform administrator
  2233. $extra_fields = UserManager :: get_extra_fields(0,50,5,'ASC');
  2234. // creating the form
  2235. $return = '<form action="courseLog.php" method="get" name="additional_profile_field_form" id="additional_profile_field_form">';
  2236. // the select field with the additional user profile fields (= this is where we select the field of which we want to see
  2237. // the information the users have entered or selected.
  2238. $return .= '<select name="additional_profile_field">';
  2239. $return .= '<option value="-">'.get_lang('SelectFieldToAdd').'</option>';
  2240. foreach ($extra_fields as $key=>$field) {
  2241. // show only extra fields that are visible, added by J.Montoya
  2242. if ($field[6]==1) {
  2243. if ($field[0] == $_GET['additional_profile_field'] ) {
  2244. $selected = 'selected="selected"';
  2245. } else {
  2246. $selected = '';
  2247. }
  2248. $return .= '<option value="'.$field[0].'" '.$selected.'>'.$field[3].'</option>';
  2249. }
  2250. }
  2251. $return .= '</select>';
  2252. // the form elements for the $_GET parameters (because the form is passed through GET
  2253. foreach ($_GET as $key=>$value){
  2254. if ($key <> 'additional_profile_field') {
  2255. $return .= '<input type="hidden" name="'.Security::remove_XSS($key).'" value="'.Security::remove_XSS($value).'" />';
  2256. }
  2257. }
  2258. // the submit button
  2259. $return .= '<button class="save" type="submit">'.get_lang('AddAdditionalProfileField').'</button>';
  2260. $return .= '</form>';
  2261. return $return;
  2262. }
  2263. /**
  2264. * This function gets all the information of a certrain ($field_id) additional profile field.
  2265. * It gets the information of all the users so that it can be displayed in the sortable table or in the csv or xls export
  2266. *
  2267. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  2268. * @since October 2009
  2269. * @version 1.8.7
  2270. */
  2271. function get_addtional_profile_information_of_field($field_id){
  2272. // Database table definition
  2273. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2274. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2275. $sql = "SELECT user.user_id, field.field_value FROM $table_user user, $table_user_field_values field
  2276. WHERE user.user_id = field.user_id
  2277. AND field.field_id='".intval($field_id)."'";
  2278. $result = Database::query($sql);
  2279. while($row = Database::fetch_array($result)) {
  2280. $return[$row['user_id']][] = $row['field_value'];
  2281. }
  2282. return $return;
  2283. }
  2284. /**
  2285. * This function gets all the information of a certrain ($field_id) additional profile field for a specific list of users is more efficent than get_addtional_profile_information_of_field() function
  2286. * It gets the information of all the users so that it can be displayed in the sortable table or in the csv or xls export
  2287. *
  2288. * @author Julio Montoya <gugli100@gmail.com>
  2289. * @param int field id
  2290. * @param array list of user ids
  2291. * @return array
  2292. * @since Nov 2009
  2293. * @version 1.8.6.2
  2294. */
  2295. function get_addtional_profile_information_of_field_by_user($field_id, $users) {
  2296. // Database table definition
  2297. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2298. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2299. $result_extra_field = UserManager::get_extra_field_information($field_id);
  2300. if (!empty($users)) {
  2301. if ($result_extra_field['field_type'] == USER_FIELD_TYPE_TAG ) {
  2302. foreach($users as $user_id) {
  2303. $user_result = UserManager::get_user_tags($user_id, $field_id);
  2304. $tag_list = array();
  2305. foreach($user_result as $item) {
  2306. $tag_list[] = $item['tag'];
  2307. }
  2308. $return[$user_id][] = implode(', ',$tag_list);
  2309. }
  2310. } else {
  2311. $new_user_array = array();
  2312. foreach($users as $user_id) {
  2313. $new_user_array[]= "'".$user_id."'";
  2314. }
  2315. $users = implode(',',$new_user_array);
  2316. //selecting only the necessary information NOT ALL the user list
  2317. $sql = "SELECT user.user_id, field.field_value FROM $table_user user INNER JOIN $table_user_field_values field
  2318. ON (user.user_id = field.user_id)
  2319. WHERE field.field_id=".intval($field_id)." AND user.user_id IN ($users)";
  2320. $result = Database::query($sql);
  2321. while($row = Database::fetch_array($result)) {
  2322. // get option value for field type double select by id
  2323. if (!empty($row['field_value'])) {
  2324. if ($result_extra_field['field_type'] == USER_FIELD_TYPE_DOUBLE_SELECT) {
  2325. $id_double_select = explode(';',$row['field_value']);
  2326. if (is_array($id_double_select)) {
  2327. $value1 = $result_extra_field['options'][$id_double_select[0]]['option_value'];
  2328. $value2 = $result_extra_field['options'][$id_double_select[1]]['option_value'];
  2329. $row['field_value'] = ($value1.';'.$value2);
  2330. }
  2331. }
  2332. }
  2333. // get other value from extra field
  2334. $return[$row['user_id']][] = $row['field_value'];
  2335. }
  2336. }
  2337. }
  2338. return $return;
  2339. }
  2340. /**
  2341. * count the number of students in this course (used for SortableTable)
  2342. * Deprecated
  2343. */
  2344. function count_student_in_course() {
  2345. global $nbStudents;
  2346. return $nbStudents;
  2347. }
  2348. function sort_users($a, $b) {
  2349. return strcmp(trim(api_strtolower($a[$_SESSION['tracking_column']])), trim(api_strtolower($b[$_SESSION['tracking_column']])));
  2350. }
  2351. function sort_users_desc($a, $b) {
  2352. return strcmp( trim(api_strtolower($b[$_SESSION['tracking_column']])), trim(api_strtolower($a[$_SESSION['tracking_column']])));
  2353. }
  2354. /**
  2355. * Get number of users for sortable with pagination
  2356. * @return int
  2357. */
  2358. function get_number_of_users() {
  2359. global $user_ids;
  2360. return count($user_ids);
  2361. }
  2362. /**
  2363. * Get data for users list in sortable with pagination
  2364. * @return array
  2365. */
  2366. function get_user_data($from, $number_of_items, $column, $direction) {
  2367. global $user_ids, $course_code, $additional_user_profile_info, $export_csv, $is_western_name_order, $csv_content, $session_id, $_configuration;
  2368. $course_code = Database::escape_string($course_code);
  2369. $course_info = CourseManager :: get_course_information($course_code);
  2370. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  2371. $access_url_id = api_get_current_access_url_id();
  2372. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  2373. // get all users data from a course for sortable with limit
  2374. $condition_user = "";
  2375. if (is_array($user_ids)) {
  2376. $condition_user = " WHERE user.user_id IN (".implode(',',$user_ids).") ";
  2377. } else {
  2378. $condition_user = " WHERE user.user_id = '$user_ids' ";
  2379. }
  2380. if ($_configuration['multiple_access_urls']) {
  2381. $url_table = ", ".$tbl_url_rel_user."as url_users";
  2382. $url_condition = " AND user.user_id = url_users.user_id AND access_url_id='$access_url_id'";
  2383. }
  2384. $sql = "SELECT user.user_id as col0,
  2385. user.official_code as col1,
  2386. user.lastname as col2,
  2387. user.firstname as col3
  2388. FROM $tbl_user as user $url_table
  2389. $condition_user $url_condition";
  2390. if (!in_array($direction, array('ASC','DESC'))) {
  2391. $direction = 'ASC';
  2392. }
  2393. $column = intval($column);
  2394. $from = intval($from);
  2395. $number_of_items = intval($number_of_items);
  2396. $sql .= " ORDER BY col$column $direction ";
  2397. $sql .= " LIMIT $from,$number_of_items";
  2398. $res = Database::query($sql);
  2399. $users = array ();
  2400. $t = time();
  2401. $row = array();
  2402. while ($user = Database::fetch_row($res)) {
  2403. $row[0] = $user[1];
  2404. if ($is_western_name_order) {
  2405. $row[1] = $user[3];
  2406. $row[2] = $user[2];
  2407. } else {
  2408. $row[1] = $user[2];
  2409. $row[2] = $user[3];
  2410. }
  2411. $row[3] = api_time_to_hms(Tracking::get_time_spent_on_the_course($user[0], $course_code, $session_id));
  2412. $avg_student_score = Tracking::get_avg_student_score($user[0], $course_code, array(), $session_id);
  2413. //echo '<b>student '.$user[0].' $avg_student_score:</b> '.$avg_student_score; echo '<br />';
  2414. $avg_student_progress = Tracking::get_avg_student_progress($user[0], $course_code, array(), $session_id);
  2415. if (empty($avg_student_progress)) {$avg_student_progress=0;}
  2416. $row[4] = $avg_student_progress.'%';
  2417. //if (empty($avg_student_score)) {$avg_student_score=0;}
  2418. if(is_numeric($avg_student_score)) {
  2419. $row[5] = $avg_student_score.'%';
  2420. } else {
  2421. $row[5] = $avg_student_score;
  2422. }
  2423. $row[6] = Tracking::count_student_assignments($user[0], $course_code, $session_id);
  2424. $row[7] = Tracking::count_student_messages($user[0], $course_code, $session_id);
  2425. $row[8] = Tracking::get_first_connection_date_on_the_course($user[0], $course_code, $session_id);
  2426. $row[9] = Tracking::get_last_connection_date_on_the_course($user[0], $course_code, $session_id);
  2427. // we need to display an additional profile field
  2428. if (isset($_GET['additional_profile_field']) AND is_numeric($_GET['additional_profile_field'])) {
  2429. if (is_array($additional_user_profile_info[$user[0]])) {
  2430. $row[10]=implode(', ', $additional_user_profile_info[$user[0]]);
  2431. } else {
  2432. $row[10]='&nbsp;';
  2433. }
  2434. }
  2435. $row[11] = '<center><a href="../mySpace/myStudents.php?student='.$user[0].'&details=true&course='.$course_code.'&origin=tracking_course&id_session='.$session_id.'"><img src="'.api_get_path(WEB_IMG_PATH).'2rightarrow.gif" border="0" /></a></center>';
  2436. if ($export_csv) {
  2437. $row[8] = strip_tags($row[8]);
  2438. $row[9] = strip_tags($row[9]);
  2439. unset($row[10]);
  2440. unset($row[11]);
  2441. $csv_content[] = $row;
  2442. }
  2443. // store columns in array $users
  2444. $users[] = array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],$row[8],$row[9],$row[10],$row[11]);
  2445. }
  2446. return $users;
  2447. }
  2448. }
  2449. class TrackingUserLog {
  2450. /**
  2451. * Displays the number of logins every month for a specific user in a specific course.
  2452. */
  2453. function display_login_tracking_info($view, $user_id, $course_id, $session_id = 0)
  2454. {
  2455. $MonthsLong = $GLOBALS['MonthsLong'];
  2456. // protected data
  2457. $user_id = intval($user_id);
  2458. $session_id = intval($session_id);
  2459. $course_id = Database::escape_string($course_id);
  2460. $track_access_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  2461. $tempView = $view;
  2462. if(substr($view,0,1) == '1') {
  2463. $new_view = substr_replace($view,'0',0,1);
  2464. echo "
  2465. <tr>
  2466. <td valign='top'>
  2467. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font>" .
  2468. "<b>".get_lang('LoginsAndAccessTools')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".$user_id."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=10000'>".get_lang('ExportAsCSV')."</a>]
  2469. </td>
  2470. </tr>
  2471. ";
  2472. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('LoginsDetails')."<br>";
  2473. $sql = "SELECT UNIX_TIMESTAMP(access_date), count(access_date)
  2474. FROM $track_access_table
  2475. WHERE access_user_id = '$user_id'
  2476. AND access_cours_code = '$course_id'
  2477. AND access_session_id = '$session_id'
  2478. GROUP BY YEAR(access_date),MONTH(access_date)
  2479. ORDER BY YEAR(access_date),MONTH(access_date) ASC";
  2480. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  2481. //$results = getManyResults2Col($sql);
  2482. $results = getManyResults3Col($sql);
  2483. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  2484. echo "<tr>
  2485. <td class='secLine'>
  2486. ".get_lang('LoginsTitleMonthColumn')."
  2487. </td>
  2488. <td class='secLine'>
  2489. ".get_lang('LoginsTitleCountColumn')."
  2490. </td>
  2491. </tr>";
  2492. $total = 0;
  2493. if (is_array($results)) {
  2494. for($j = 0 ; $j < count($results) ; $j++) {
  2495. echo "<tr>";
  2496. echo "<td class='content'><a href='logins_details.php?uInfo=".$user_id."&reqdate=".$results[$j][0]."&view=".Security::remove_XSS($view)."'>".$MonthsLong[date('n', $results[$j][0])-1].' '.date('Y', $results[$j][0])."</a></td>";
  2497. echo "<td valign='top' align='right' class='content'>".$results[$j][1]."</td>";
  2498. echo"</tr>";
  2499. $total = $total + $results[$j][1];
  2500. }
  2501. echo "<tr>";
  2502. echo "<td>".get_lang('Total')."</td>";
  2503. echo "<td align='right' class='content'>".$total."</td>";
  2504. echo"</tr>";
  2505. } else {
  2506. echo "<tr>";
  2507. echo "<td colspan='2'><center>".get_lang('NoResult')."</center></td>";
  2508. echo"</tr>";
  2509. }
  2510. echo "</table>";
  2511. echo "</td></tr>";
  2512. } else {
  2513. $new_view = substr_replace($view,'1',0,1);
  2514. echo "
  2515. <tr>
  2516. <td valign='top'>
  2517. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=".$user_id."&view=".Security::remove_XSS($new_view)."' class='specialLink'>".get_lang('LoginsAndAccessTools')."</a>
  2518. </td>
  2519. </tr>
  2520. ";
  2521. }
  2522. }
  2523. /**
  2524. * Displays the exercise results for a specific user in a specific course.
  2525. * @todo remove globals
  2526. */
  2527. function display_exercise_tracking_info($view, $user_id, $course_id)
  2528. {
  2529. global $TABLECOURSE_EXERCICES, $TABLETRACK_EXERCICES, $dateTimeFormatLong;
  2530. if(substr($view,1,1) == '1')
  2531. {
  2532. $new_view = substr_replace($view,'0',1,1);
  2533. echo "<tr>
  2534. <td valign='top'>
  2535. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font><b>".get_lang('ExercicesResults')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=01000'>".get_lang('ExportAsCSV')."</a>]
  2536. </td>
  2537. </tr>";
  2538. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('ExercicesDetails')."<br />";
  2539. $sql = "SELECT ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  2540. FROM $TABLECOURSE_EXERCICES AS ce , $TABLETRACK_EXERCICES AS te
  2541. WHERE te.exe_cours_id = '".Database::escape_string($course_id)."'
  2542. AND te.exe_user_id = '".Database::escape_string($user_id)."'
  2543. AND te.exe_exo_id = ce.id
  2544. ORDER BY ce.title ASC, te.exe_date ASC";
  2545. $hpsql = "SELECT te.exe_name, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  2546. FROM $TBL_TRACK_HOTPOTATOES AS te
  2547. WHERE te.exe_user_id = '".Database::escape_string($user_id)."' AND te.exe_cours_id = '".Database::escape_string($course_id)."'
  2548. ORDER BY te.exe_cours_id ASC, te.exe_date ASC";
  2549. $hpresults = getManyResultsXCol($hpsql, 4);
  2550. $NoTestRes = 0;
  2551. $NoHPTestRes = 0;
  2552. echo "<tr>\n<td style='padding-left : 40px;padding-right : 40px;'>\n";
  2553. $results = getManyResultsXCol($sql, 4);
  2554. echo "<table cellpadding='2' cellspacing='1' border='0' align='center'>\n";
  2555. echo "
  2556. <tr bgcolor='#E6E6E6'>
  2557. <td>
  2558. ".get_lang('ExercicesTitleExerciceColumn')."
  2559. </td>
  2560. <td>
  2561. ".get_lang('Date')."
  2562. </td>
  2563. <td>
  2564. ".get_lang('ExercicesTitleScoreColumn')."
  2565. </td>
  2566. </tr>";
  2567. if (is_array($results)) {
  2568. for($i = 0; $i < sizeof($results); $i++) {
  2569. $display_date = api_convert_and_format_date($results[$i][3], null, date_default_timezone_get());
  2570. echo "<tr>\n";
  2571. echo "<td class='content'>".$results[$i][0]."</td>\n";
  2572. echo "<td class='content'>".$display_date."</td>\n";
  2573. echo "<td valign='top' align='right' class='content'>".$results[$i][1]." / ".$results[$i][2]."</td>\n";
  2574. echo "</tr>\n";
  2575. }
  2576. } else {
  2577. // istvan begin
  2578. $NoTestRes = 1;
  2579. }
  2580. // The Result of Tests
  2581. if(is_array($hpresults)) {
  2582. for($i = 0; $i < sizeof($hpresults); $i++) {
  2583. $title = GetQuizName($hpresults[$i][0],'');
  2584. if ($title == '')
  2585. $title = basename($hpresults[$i][0]);
  2586. $display_date = api_convert_and_format_date($hpresults[$i][3], null, date_default_timezone_get());
  2587. ?>
  2588. <tr>
  2589. <td class="content"><?php echo $title; ?></td>
  2590. <td class="content" align="center"><?php echo $display_date; ?></td>
  2591. <td class="content" align="center"><?php echo $hpresults[$i][1]; ?> / <?php echo $hpresults[$i][2]; ?></td>
  2592. </tr>
  2593. <?php }
  2594. } else {
  2595. $NoHPTestRes = 1;
  2596. }
  2597. if ($NoTestRes == 1 && $NoHPTestRes == 1) {
  2598. echo "<tr>\n";
  2599. echo "<td colspan='3'><center>".get_lang('NoResult')."</center></td>\n";
  2600. echo "</tr>\n";
  2601. }
  2602. echo "</table>";
  2603. echo "</td>\n</tr>\n";
  2604. } else {
  2605. $new_view = substr_replace($view,'1',1,1);
  2606. echo "
  2607. <tr>
  2608. <td valign='top'>
  2609. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=$user_id&view=".$new_view."' class='specialLink'>".get_lang('ExercicesResults')."</a>
  2610. </td>
  2611. </tr>";
  2612. }
  2613. }
  2614. /**
  2615. * Displays the student publications for a specific user in a specific course.
  2616. * @todo remove globals
  2617. */
  2618. function display_student_publications_tracking_info($view, $user_id, $course_id)
  2619. {
  2620. global $TABLETRACK_UPLOADS, $TABLECOURSE_WORK, $dateTimeFormatLong, $_course;
  2621. if(substr($view,2,1) == '1') {
  2622. $new_view = substr_replace($view,'0',2,1);
  2623. echo "<tr>
  2624. <td valign='top'>
  2625. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font><b>".get_lang('WorkUploads')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=00100'>".get_lang('ExportAsCSV')."</a>]
  2626. </td>
  2627. </tr>";
  2628. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('WorksDetails')."<br>";
  2629. $sql = "SELECT u.upload_date, w.title, w.author,w.url
  2630. FROM $TABLETRACK_UPLOADS u , $TABLECOURSE_WORK w
  2631. WHERE u.upload_work_id = w.id
  2632. AND u.upload_user_id = '".Database::escape_string($user_id)."'
  2633. AND u.upload_cours_id = '".Database::escape_string($course_id)."'
  2634. ORDER BY u.upload_date DESC";
  2635. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  2636. $results = getManyResultsXCol($sql,4);
  2637. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  2638. echo "<tr>
  2639. <td class='secLine' width='40%'>
  2640. ".get_lang('WorkTitle')."
  2641. </td>
  2642. <td class='secLine' width='30%'>
  2643. ".get_lang('WorkAuthors')."
  2644. </td>
  2645. <td class='secLine' width='30%'>
  2646. ".get_lang('Date')."
  2647. </td>
  2648. </tr>";
  2649. if (is_array($results)) {
  2650. for($j = 0 ; $j < count($results) ; $j++) {
  2651. $pathToFile = api_get_path(WEB_COURSE_PATH).$_course['path']."/".$results[$j][3];
  2652. $beautifulDate = api_convert_and_format_date($results[$j][0], null, date_default_timezone_get());
  2653. echo "<tr>";
  2654. echo "<td class='content'>"
  2655. ."<a href ='".$pathToFile."'>".$results[$j][1]."</a>"
  2656. ."</td>";
  2657. echo "<td class='content'>".$results[$j][2]."</td>";
  2658. echo "<td class='content'>".$beautifulDate."</td>";
  2659. echo"</tr>";
  2660. }
  2661. } else {
  2662. echo "<tr>";
  2663. echo "<td colspan='3'><center>".get_lang('NoResult')."</center></td>";
  2664. echo"</tr>";
  2665. }
  2666. echo "</table>";
  2667. echo "</td></tr>";
  2668. } else {
  2669. $new_view = substr_replace($view,'1',2,1);
  2670. echo "
  2671. <tr>
  2672. <td valign='top'>
  2673. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."' class='specialLink'>".get_lang('WorkUploads')."</a>
  2674. </td>
  2675. </tr>
  2676. ";
  2677. }
  2678. }
  2679. /**
  2680. * Displays the links followed for a specific user in a specific course.
  2681. * @todo remove globals
  2682. */
  2683. function display_links_tracking_info($view, $user_id, $course_id)
  2684. {
  2685. global $TABLETRACK_LINKS, $TABLECOURSE_LINKS;
  2686. if(substr($view,3,1) == '1') {
  2687. $new_view = substr_replace($view,'0',3,1);
  2688. echo "
  2689. <tr>
  2690. <td valign='top'>
  2691. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font><b>".get_lang('LinksAccess')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=00010'>".get_lang('ExportAsCSV')."</a>]
  2692. </td>
  2693. </tr>
  2694. ";
  2695. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('LinksDetails')."<br>";
  2696. $sql = "SELECT cl.title, cl.url
  2697. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  2698. WHERE sl.links_link_id = cl.id
  2699. AND sl.links_cours_id = '".Database::escape_string($course_id)."'
  2700. AND sl.links_user_id = '".Database::escape_string($user_id)."'
  2701. GROUP BY cl.title, cl.url";
  2702. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  2703. $results = getManyResults2Col($sql);
  2704. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  2705. echo "<tr>
  2706. <td class='secLine'>
  2707. ".get_lang('LinksTitleLinkColumn')."
  2708. </td>
  2709. </tr>";
  2710. if (is_array($results)) {
  2711. for($j = 0 ; $j < count($results) ; $j++) {
  2712. echo "<tr>";
  2713. echo "<td class='content'><a href='".$results[$j][1]."'>".$results[$j][0]."</a></td>";
  2714. echo"</tr>";
  2715. }
  2716. } else {
  2717. echo "<tr>";
  2718. echo "<td ><center>".get_lang('NoResult')."</center></td>";
  2719. echo"</tr>";
  2720. }
  2721. echo "</table>";
  2722. echo "</td></tr>";
  2723. } else {
  2724. $new_view = substr_replace($view,'1',3,1);
  2725. echo "
  2726. <tr>
  2727. <td valign='top'>
  2728. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."' class='specialLink'>".get_lang('LinksAccess')."</a>
  2729. </td>
  2730. </tr>
  2731. ";
  2732. }
  2733. }
  2734. /**
  2735. * Displays the documents downloaded for a specific user in a specific course.
  2736. * @param string kind of view inside tracking info
  2737. * @param int User id
  2738. * @param string Course code
  2739. * @param int Session id (optional, default = 0)
  2740. * @return void
  2741. */
  2742. function display_document_tracking_info($view, $user_id, $course_id, $session_id = 0) {
  2743. // protect data
  2744. $user_id = intval($user_id);
  2745. $course_id = Database::escape_string($course_id);
  2746. $session_id = intval($session_id);
  2747. $downloads_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  2748. if(substr($view,4,1) == '1')
  2749. {
  2750. $new_view = substr_replace($view,'0',4,1);
  2751. echo "
  2752. <tr>
  2753. <td valign='top'>
  2754. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font><b>".get_lang('DocumentsAccess')."</b>&nbsp;&nbsp;&nbsp;[<a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."'>".get_lang('Close')."</a>]&nbsp;&nbsp;&nbsp;[<a href='userLogCSV.php?".api_get_cidreq()."&uInfo=".Security::remove_XSS($_GET['uInfo'])."&view=00001'>".get_lang('ExportAsCSV')."</a>]
  2755. </td>
  2756. </tr>
  2757. ";
  2758. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('DocumentsDetails')."<br>";
  2759. $sql = "SELECT down_doc_path
  2760. FROM $downloads_table
  2761. WHERE down_cours_id = '".$course_id."'
  2762. AND down_user_id = '$user_id'
  2763. AND down_session_id = '$session_id'
  2764. GROUP BY down_doc_path";
  2765. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  2766. $results = getManyResults1Col($sql);
  2767. echo "<table cellpadding='2' cellspacing='1' border='0' align='center'>";
  2768. echo "<tr>
  2769. <td class='secLine'>
  2770. ".get_lang('DocumentsTitleDocumentColumn')."
  2771. </td>
  2772. </tr>";
  2773. if (is_array($results)) {
  2774. for($j = 0 ; $j < count($results) ; $j++) {
  2775. echo "<tr>";
  2776. echo "<td class='content'>".$results[$j]."</td>";
  2777. echo"</tr>";
  2778. }
  2779. } else {
  2780. echo "<tr>";
  2781. echo "<td><center>".get_lang('NoResult')."</center></td>";
  2782. echo"</tr>";
  2783. }
  2784. echo "</table>";
  2785. echo "</td></tr>";
  2786. } else {
  2787. $new_view = substr_replace($view,'1',4,1);
  2788. echo "
  2789. <tr>
  2790. <td valign='top'>
  2791. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=".Security::remove_XSS($user_id)."&view=".Security::remove_XSS($new_view)."' class='specialLink'>".get_lang('DocumentsAccess')."</a>
  2792. </td>
  2793. </tr>
  2794. ";
  2795. }
  2796. }
  2797. }
  2798. class TrackingUserLogCSV {
  2799. /**
  2800. * Displays the number of logins every month for a specific user in a specific course.
  2801. */
  2802. function display_login_tracking_info($view, $user_id, $course_id, $session_id = 0)
  2803. {
  2804. $MonthsLong = $GLOBALS['MonthsLong'];
  2805. $track_access_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  2806. // protected data
  2807. $user_id = intval($user_id);
  2808. $session_id = intval($session_id);
  2809. $course_id = Database::escape_string($course_id);
  2810. $tempView = $view;
  2811. if(substr($view,0,1) == '1')
  2812. {
  2813. $new_view = substr_replace($view,'0',0,1);
  2814. $title[1]= get_lang('LoginsAndAccessTools').get_lang('LoginsDetails');
  2815. $sql = "SELECT UNIX_TIMESTAMP(access_date), count(access_date)
  2816. FROM $track_access_table
  2817. WHERE access_user_id = '$user_id'
  2818. AND access_cours_code = '".$course_id."'
  2819. AND access_session_id = '$session_id'
  2820. GROUP BY YEAR(access_date),MONTH(access_date)
  2821. ORDER BY YEAR(access_date),MONTH(access_date) ASC";
  2822. //$results = getManyResults2Col($sql);
  2823. $results = getManyResults3Col($sql);
  2824. $title_line= get_lang('LoginsTitleMonthColumn').';'.get_lang('LoginsTitleCountColumn')."\n";
  2825. $line='';
  2826. $total = 0;
  2827. if (is_array($results))
  2828. {
  2829. for($j = 0 ; $j < count($results) ; $j++)
  2830. {
  2831. $line .= $results[$j][0].';'.$results[$j][1]."\n";
  2832. $total = $total + $results[$j][1];
  2833. }
  2834. $line .= get_lang('Total').";".$total."\n";
  2835. }
  2836. else
  2837. {
  2838. $line= get_lang('NoResult')."</center></td>";
  2839. }
  2840. }
  2841. else
  2842. {
  2843. $new_view = substr_replace($view,'1',0,1);
  2844. }
  2845. return array($title_line, $line);
  2846. }
  2847. /**
  2848. * Displays the exercise results for a specific user in a specific course.
  2849. * @todo remove globals
  2850. */
  2851. function display_exercise_tracking_info($view, $user_id, $course_id) {
  2852. global $TABLECOURSE_EXERCICES, $TABLETRACK_EXERCICES, $TABLETRACK_HOTPOTATOES, $dateTimeFormatLong;
  2853. if (substr($view,1,1) == '1') {
  2854. $new_view = substr_replace($view,'0',1,1);
  2855. $title[1]= get_lang('ExercicesDetails');
  2856. $line='';
  2857. $sql = "SELECT ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  2858. FROM $TABLECOURSE_EXERCICES AS ce , $TABLETRACK_EXERCICES AS te
  2859. WHERE te.exe_cours_id = '$course_id'
  2860. AND te.exe_user_id = '$user_id'
  2861. AND te.exe_exo_id = ce.id
  2862. ORDER BY ce.title ASC, te.exe_date ASC";
  2863. $hpsql = "SELECT te.exe_name, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  2864. FROM $TABLETRACK_HOTPOTATOES AS te
  2865. WHERE te.exe_user_id = '$user_id' AND te.exe_cours_id = '$course_id'
  2866. ORDER BY te.exe_cours_id ASC, te.exe_date ASC";
  2867. $hpresults = getManyResultsXCol($hpsql, 4);
  2868. $NoTestRes = 0;
  2869. $NoHPTestRes = 0;
  2870. $results = getManyResultsXCol($sql, 4);
  2871. $title_line=get_lang('ExercicesTitleExerciceColumn').";".get_lang('Date').';'.get_lang('ExercicesTitleScoreColumn')."\n";
  2872. if (is_array($results))
  2873. {
  2874. for($i = 0; $i < sizeof($results); $i++)
  2875. {
  2876. $display_date = api_convert_and_format_date($results[$i][3], null, date_default_timezone_get());
  2877. $line .= $results[$i][0].";".$display_date.";".$results[$i][1]." / ".$results[$i][2]."\n";
  2878. }
  2879. }
  2880. else // istvan begin
  2881. {
  2882. $NoTestRes = 1;
  2883. }
  2884. // The Result of Tests
  2885. if(is_array($hpresults))
  2886. {
  2887. for($i = 0; $i < sizeof($hpresults); $i++)
  2888. {
  2889. $title = GetQuizName($hpresults[$i][0],'');
  2890. if ($title == '')
  2891. $title = basename($hpresults[$i][0]);
  2892. $display_date = api_convert_and_format_date($hpresults[$i][3], null, date_default_timezone_get());
  2893. $line .= $title.';'.$display_date.';'.$hpresults[$i][1].'/'.$hpresults[$i][2]."\n";
  2894. }
  2895. }
  2896. else
  2897. {
  2898. $NoHPTestRes = 1;
  2899. }
  2900. if ($NoTestRes == 1 && $NoHPTestRes == 1)
  2901. {
  2902. $line=get_lang('NoResult');
  2903. }
  2904. }
  2905. else
  2906. {
  2907. $new_view = substr_replace($view,'1',1,1);
  2908. }
  2909. return array($title_line, $line);
  2910. }
  2911. /**
  2912. * Displays the student publications for a specific user in a specific course.
  2913. * @todo remove globals
  2914. */
  2915. function display_student_publications_tracking_info($view, $user_id, $course_id) {
  2916. global $TABLETRACK_UPLOADS, $TABLECOURSE_WORK, $dateTimeFormatLong, $_course;
  2917. if (substr($view,2,1) == '1') {
  2918. $new_view = substr_replace($view,'0',2,1);
  2919. $sql = "SELECT u.upload_date, w.title, w.author, w.url
  2920. FROM $TABLETRACK_UPLOADS u , $TABLECOURSE_WORK w
  2921. WHERE u.upload_work_id = w.id
  2922. AND u.upload_user_id = '$user_id'
  2923. AND u.upload_cours_id = '$course_id'
  2924. ORDER BY u.upload_date DESC";
  2925. $results = getManyResultsXCol($sql,4);
  2926. $title[1]=get_lang('WorksDetails');
  2927. $line='';
  2928. $title_line=get_lang('WorkTitle').";".get_lang('WorkAuthors').";".get_lang('Date')."\n";
  2929. if (is_array($results)) {
  2930. for($j = 0 ; $j < count($results) ; $j++) {
  2931. $pathToFile = api_get_path(WEB_COURSE_PATH).$_course['path']."/".$results[$j][3];
  2932. $beautifulDate = api_convert_and_format_date($results[$j][0], null, date_default_timezone_get());
  2933. $line .= $results[$j][1].";".$results[$j][2].";".$beautifulDate."\n";
  2934. }
  2935. } else {
  2936. $line= get_lang('NoResult');
  2937. }
  2938. } else {
  2939. $new_view = substr_replace($view,'1',2,1);
  2940. }
  2941. return array($title_line, $line);
  2942. }
  2943. /**
  2944. * Displays the links followed for a specific user in a specific course.
  2945. * @todo remove globals
  2946. */
  2947. function display_links_tracking_info($view, $user_id, $course_id) {
  2948. global $TABLETRACK_LINKS, $TABLECOURSE_LINKS;
  2949. if (substr($view,3,1) == '1') {
  2950. $new_view = substr_replace($view,'0',3,1);
  2951. $title[1]=get_lang('LinksDetails');
  2952. $sql = "SELECT cl.title, cl.url
  2953. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  2954. WHERE sl.links_link_id = cl.id
  2955. AND sl.links_cours_id = '$course_id'
  2956. AND sl.links_user_id = '$user_id'
  2957. GROUP BY cl.title, cl.url";
  2958. $results = getManyResults2Col($sql);
  2959. $title_line= get_lang('LinksTitleLinkColumn')."\n";
  2960. if (is_array($results)) {
  2961. for ($j = 0 ; $j < count($results) ; $j++) {
  2962. $line .= $results[$j][0]."\n";
  2963. }
  2964. } else {
  2965. $line=get_lang('NoResult');
  2966. }
  2967. } else {
  2968. $new_view = substr_replace($view,'1',3,1);
  2969. }
  2970. return array($title_line, $line);
  2971. }
  2972. /**
  2973. * Displays the documents downloaded for a specific user in a specific course.
  2974. * @param string kind of view inside tracking info
  2975. * @param int User id
  2976. * @param string Course code
  2977. * @param int Session id (optional, default = 0)
  2978. * @return void
  2979. */
  2980. function display_document_tracking_info($view, $user_id, $course_id, $session_id = 0) {
  2981. // protect data
  2982. $user_id = intval($user_id);
  2983. $course_id = Database::escape_string($course_id);
  2984. $session_id = intval($session_id);
  2985. $downloads_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  2986. if (substr($view,4,1) == '1') {
  2987. $new_view = substr_replace($view,'0',4,1);
  2988. $title[1]= get_lang('DocumentsDetails');
  2989. $sql = "SELECT down_doc_path
  2990. FROM $downloads_table
  2991. WHERE down_cours_id = '$course_id'
  2992. AND down_user_id = '$user_id'
  2993. AND down_session_id = '$session_id'
  2994. GROUP BY down_doc_path";
  2995. $results = getManyResults1Col($sql);
  2996. $title_line = get_lang('DocumentsTitleDocumentColumn')."\n";
  2997. if (is_array($results)) {
  2998. for ($j = 0 ; $j < count($results) ; $j++) {
  2999. $line .= $results[$j]."\n";
  3000. }
  3001. } else {
  3002. $line = get_lang('NoResult');
  3003. }
  3004. } else {
  3005. $new_view = substr_replace($view,'1',4,1);
  3006. }
  3007. return array($title_line, $line);
  3008. }
  3009. }