tracking.lib.php 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the tracking library for Chamilo
  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. /**
  11. * Code
  12. */
  13. define('SESSION_LINK_TARGET','_self');
  14. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  15. //require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php'; moved to autoload
  16. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpathList.class.php';
  17. /**
  18. * Class
  19. * @package chamilo.library
  20. */
  21. class Tracking {
  22. /**
  23. * Calculates the time spent on the platform by a user
  24. * @param int User id
  25. * @param string type of time filter: 'last_week' or 'custom'
  26. * @param strgin start date date('Y-m-d H:i:s')
  27. * @param strgin end date date('Y-m-d H:i:s')
  28. * @return timestamp $nb_seconds
  29. */
  30. public static function get_time_spent_on_the_platform($user_id, $time_filter = 'last_7_days', $start_date = null, $end_date = null) {
  31. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  32. $condition_time = '';
  33. if (empty($time_filter)) {
  34. $time_filter = 'last_week';
  35. }
  36. $today = date('Y-m-d H:i:s');
  37. switch ($time_filter) {
  38. case 'last_7_days':
  39. $new_date = strtotime('-7 day');
  40. $new_date = date('Y-m-d H:i:s', $new_date);
  41. $condition_time = ' AND (login_date >= "'.$new_date.'" AND logout_date <= "'.$today.'") ';
  42. break;
  43. case 'last_30_days':
  44. $new_date = strtotime('-30 day');
  45. $new_date = date('Y-m-d H:i:s', $new_date);
  46. $condition_time = ' AND (login_date >= "'.$new_date.'" AND logout_date <= "'.$today.'") ';
  47. break;
  48. case 'custom':
  49. if (!empty($start_date) && !empty($end_date)) {
  50. $condition_time = ' AND (login_date >= "'.$start_date.'" AND logout_date <= "'.$end_date.'" ) ';
  51. }
  52. break;
  53. }
  54. $sql = 'SELECT login_date, logout_date FROM '.$tbl_track_login.'
  55. WHERE login_user_id = '.intval($user_id).$condition_time;
  56. $rs = Database::query($sql);
  57. $nb_seconds = 0;
  58. $wrong_logout_dates = false;
  59. while ($a_connections = Database::fetch_array($rs)) {
  60. $s_login_date = $a_connections["login_date"];
  61. $s_logout_date = $a_connections["logout_date"];
  62. $i_timestamp_login_date = strtotime($s_login_date);
  63. $i_timestamp_logout_date = strtotime($s_logout_date);
  64. if ($i_timestamp_logout_date > 0) {
  65. // @TODO YW 20110708: for some reason the result here is often
  66. // negative, resulting in a negative time total. Considering the
  67. // logout_date is > 0, this can only mean that the database
  68. // contains items where the login_date is higher (=later) than
  69. // the logout date for a specific connexion. This has to be
  70. // analyzed and fixed. Also see the get_time_spent_on_the_course
  71. // for SQL summing.
  72. $nb_seconds += abs($i_timestamp_logout_date - $i_timestamp_login_date);
  73. } else { // there are wrong datas in db, then we can't give a wrong time
  74. $wrong_logout_dates = true;
  75. }
  76. }
  77. if($nb_seconds>0 || !$wrong_logout_dates)
  78. {
  79. return $nb_seconds;
  80. }
  81. else
  82. {
  83. return -1; //-1 means we have wrong datas in the db
  84. }
  85. }
  86. /**
  87. * Calculates the time spent on the course
  88. * @param integer User id
  89. * @param string Course code
  90. * @param int Session id (optional)
  91. * @return timestamp Time in seconds
  92. */
  93. public static function get_time_spent_on_the_course($user_id, $course_code, $session_id = 0) {
  94. // protect datas
  95. $course_code = Database::escape_string($course_code);
  96. $session_id = intval($session_id);
  97. $tbl_track_course = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  98. $condition_user = "";
  99. if (is_array($user_id)) {
  100. $user_id = array_map('intval', $user_id);
  101. $condition_user = " AND user_id IN (".implode(',',$user_id).") ";
  102. } else {
  103. $user_id = intval($user_id);
  104. $condition_user = " AND user_id = '$user_id' ";
  105. }
  106. $sql = "SELECT SUM(UNIX_TIMESTAMP(logout_course_date) - UNIX_TIMESTAMP(login_course_date)) as nb_seconds
  107. FROM $tbl_track_course
  108. WHERE UNIX_TIMESTAMP(logout_course_date) > UNIX_TIMESTAMP(login_course_date) AND course_code='$course_code' AND session_id = '$session_id' $condition_user";
  109. $rs = Database::query($sql);
  110. $row = Database::fetch_array($rs);
  111. return $row['nb_seconds'];
  112. }
  113. /**
  114. * Get first connection date for a student
  115. * @param int Student id
  116. * @return string|bool Date format long without day or false if there are no connections
  117. */
  118. public static function get_first_connection_date($student_id) {
  119. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  120. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  121. WHERE login_user_id = ' . intval($student_id) . '
  122. ORDER BY login_date ASC LIMIT 0,1';
  123. $rs = Database::query($sql);
  124. if(Database::num_rows($rs)>0) {
  125. if ($first_login_date = Database::result($rs, 0, 0)) {
  126. return api_convert_and_format_date($first_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  127. }
  128. }
  129. return false;
  130. }
  131. /**
  132. * Get las connection date for a student
  133. * @param int Student id
  134. * @param bool Show a warning message (optional)
  135. * @param bool True for returning results in timestamp (optional)
  136. * @return string|int|bool Date format long without day, false if there are no connections or timestamp if parameter $return_timestamp is true
  137. */
  138. public static function get_last_connection_date($student_id, $warning_message = false, $return_timestamp = false) {
  139. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  140. $sql = 'SELECT login_date FROM ' . $tbl_track_login . '
  141. WHERE login_user_id = ' . intval($student_id) . '
  142. ORDER BY login_date DESC LIMIT 0,1';
  143. $rs = Database::query($sql);
  144. if(Database::num_rows($rs)>0) {
  145. if ($last_login_date = Database::result($rs, 0, 0)) {
  146. $last_login_date = api_get_local_time($last_login_date);
  147. if ($return_timestamp) {
  148. return api_strtotime($last_login_date,'UTC');
  149. } else {
  150. if (!$warning_message) {
  151. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  152. } else {
  153. $timestamp = api_strtotime($last_login_date,'UTC');
  154. $currentTimestamp = time();
  155. //If the last connection is > than 7 days, the text is red
  156. //345600 = 7 days in seconds
  157. if ($currentTimestamp - $timestamp > 604800) {
  158. return '<span style="color: #F00;">' . api_format_date($last_login_date, DATE_FORMAT_SHORT) . '</span>';
  159. } else {
  160. return api_format_date($last_login_date, DATE_FORMAT_SHORT);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. return false;
  167. }
  168. /**
  169. * Get first user's connection date on the course
  170. * @param int User id
  171. * @param string Course code
  172. * @param int Session id (optional, default=0)
  173. * @return string|bool Date with format long without day or false if there is no date
  174. */
  175. public static function get_first_connection_date_on_the_course($student_id, $course_code, $session_id = 0, $convert_date = true) {
  176. // protect data
  177. $student_id = intval($student_id);
  178. $course_code = Database::escape_string($course_code);
  179. $session_id = intval($session_id);
  180. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  181. $sql = 'SELECT login_course_date FROM '.$tbl_track_login.'
  182. WHERE user_id = '.$student_id.'
  183. AND course_code = "'.$course_code.'"
  184. AND session_id = '.$session_id.'
  185. ORDER BY login_course_date ASC LIMIT 0,1';
  186. $rs = Database::query($sql);
  187. if (Database::num_rows($rs) > 0) {
  188. if ($first_login_date = Database::result($rs, 0, 0)) {
  189. if ($convert_date) {
  190. return api_convert_and_format_date($first_login_date, DATE_FORMAT_SHORT);
  191. } else {
  192. return $first_login_date;
  193. }
  194. }
  195. }
  196. return false;
  197. }
  198. /**
  199. * Get last user's connection date on the course
  200. * @param int User id
  201. * @param string Course code
  202. * @param int Session id (optional, default=0)
  203. * @return string|bool Date with format long without day or false if there is no date
  204. */
  205. public static function get_last_connection_date_on_the_course($student_id, $course_code, $session_id = 0, $convert_date = true) {
  206. // protect data
  207. $student_id = intval($student_id);
  208. $course_code = Database::escape_string($course_code);
  209. $session_id = intval($session_id);
  210. $tbl_track_e_course_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  211. $sql = 'SELECT login_course_date
  212. FROM '.$tbl_track_e_course_access.'
  213. WHERE user_id = '.$student_id.' AND
  214. course_code = "'.$course_code.'" AND
  215. session_id = '.$session_id.'
  216. ORDER BY login_course_date DESC
  217. LIMIT 0,1';
  218. $rs = Database::query($sql);
  219. if (Database::num_rows($rs) > 0) {
  220. if ($last_login_date = Database::result($rs, 0, 0)) {
  221. if (empty($last_login_date) || $last_login_date == '0000-00-00 00:00:00') {
  222. return false;
  223. }
  224. //$last_login_date_timestamp = api_strtotime($last_login_date, 'UTC');
  225. //see #5736
  226. $last_login_date_timestamp = api_strtotime($last_login_date);
  227. $now = time();
  228. //If the last connection is > than 7 days, the text is red
  229. //345600 = 7 days in seconds
  230. if ($now - $last_login_date_timestamp > 604800) {
  231. if ($convert_date) {
  232. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT);
  233. $icon = 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 src="'.api_get_path(WEB_IMG_PATH).'messagebox_warning.gif" /> </a>': null;
  234. return $icon. Display::label($last_login_date, 'warning');
  235. } else {
  236. return $last_login_date;
  237. }
  238. } else {
  239. if ($convert_date) {
  240. return api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT);
  241. } else {
  242. return $last_login_date;
  243. }
  244. }
  245. }
  246. }
  247. return false;
  248. }
  249. /**
  250. * Get count of the connections to the course during a specified period
  251. * @param string Course code
  252. * @param int Session id (optional)
  253. * @param int Datetime from which to collect data (defaults to 0)
  254. * @param int Datetime to which to collect data (defaults to now)
  255. * @return int count connections
  256. */
  257. public static function get_course_connections_count($course_code, $session_id = 0, $start = 0, $stop = null) {
  258. // protect data
  259. $month_filter = '';
  260. if ($start < 0) {
  261. $start = 0;
  262. }
  263. if (!isset($stop) or ($stop < 0)) {
  264. $stop = api_get_utc_datetime();
  265. }
  266. $month_filter = " AND login_course_date > '$start' AND login_course_date < '$stop' ";
  267. $course_code = Database::escape_string($course_code);
  268. $session_id = intval($session_id);
  269. $count = 0;
  270. $tbl_track_e_course_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  271. $sql = "SELECT count(*) as count_connections FROM $tbl_track_e_course_access WHERE course_code = '$course_code' AND session_id = $session_id $month_filter";
  272. $rs = Database::query($sql);
  273. if (Database::num_rows($rs)>0) {
  274. $row = Database::fetch_object($rs);
  275. $count = $row->count_connections;
  276. }
  277. return $count;
  278. }
  279. /**
  280. * Get count courses per student
  281. * @param int Student id
  282. * @param bool Include sessions (optional)
  283. * @return int count courses
  284. */
  285. public static function count_course_per_student($user_id, $include_sessions = true) {
  286. $user_id = intval($user_id);
  287. $tbl_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  288. $tbl_session_course_rel_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  289. $sql = 'SELECT DISTINCT course_code
  290. FROM ' . $tbl_course_rel_user . '
  291. WHERE user_id = ' . $user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH;
  292. $rs = Database::query($sql);
  293. $nb_courses = Database::num_rows($rs);
  294. if ($include_sessions) {
  295. $sql = 'SELECT DISTINCT course_code
  296. FROM ' . $tbl_session_course_rel_user . '
  297. WHERE id_user = ' . $user_id;
  298. $rs = Database::query($sql);
  299. $nb_courses += Database::num_rows($rs);
  300. }
  301. return $nb_courses;
  302. }
  303. /**
  304. * Gets the score average from all tests in a course by student
  305. *
  306. * @param mixed Student(s) id
  307. * @param string Course code
  308. * @param int Exercise id (optional), filtered by exercise
  309. * @param int Session id (optional), if param $session_id is null it'll return results including sessions, 0 = session is not filtered
  310. * @return string value (number %) Which represents a round integer about the score average.
  311. */
  312. public static function get_avg_student_exercise_score($student_id, $course_code, $exercise_id = 0, $session_id = null) {
  313. $course_info = api_get_course_info($course_code);
  314. if (!empty($course_info)) {
  315. // table definition
  316. $tbl_course_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  317. $tbl_stats_exercise = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  318. // Compose a filter based on optional exercise given
  319. $condition_quiz = "";
  320. if (!empty($exercise_id)) {
  321. $exercise_id = intval($exercise_id);
  322. $condition_quiz =" AND id = $exercise_id ";
  323. }
  324. // Compose a filter based on optional session id given
  325. $condition_session = "";
  326. if (isset($session_id)) {
  327. $session_id = intval($session_id);
  328. $condition_session = " AND session_id = $session_id ";
  329. }
  330. $sql = "SELECT count(id) FROM $tbl_course_quiz
  331. WHERE c_id = {$course_info['real_id']} AND active <> -1 $condition_quiz ";
  332. $count_quiz = Database::fetch_row(Database::query($sql));
  333. if (!empty($count_quiz[0]) && !empty($student_id)) {
  334. $condition_user = "";
  335. if (is_array($student_id)) {
  336. $condition_user = " AND exe_user_id IN (".implode(',',$student_id).") ";
  337. } else {
  338. $condition_user = " AND exe_user_id = '$student_id' ";
  339. }
  340. if (empty($exercise_id)) {
  341. $sql = "SELECT id FROM $tbl_course_quiz
  342. WHERE c_id = {$course_info['real_id']} AND active <> -1 $condition_quiz";
  343. $result = Database::query($sql);
  344. $exercise_list = array();
  345. $exercise_id = null;
  346. if (Database::num_rows($result)) {
  347. while ($row = Database::fetch_array($result)) {
  348. $exercise_list[] = $row['id'];
  349. }
  350. }
  351. if (!empty($exercise_list)) {
  352. $exercise_id = implode("','",$exercise_list);
  353. }
  354. }
  355. $count_quiz = Database::fetch_row(Database::query($sql));
  356. $sql = "SELECT SUM(exe_result/exe_weighting*100) as avg_score, COUNT(*) as num_attempts
  357. FROM $tbl_stats_exercise
  358. WHERE exe_exo_id IN ('".$exercise_id."')
  359. $condition_user AND
  360. orig_lp_id = 0 AND
  361. status = '' AND
  362. exe_cours_id = '$course_code' AND
  363. orig_lp_item_id = 0 $condition_session
  364. ORDER BY exe_date DESC";
  365. $res = Database::query($sql);
  366. $row = Database::fetch_array($res);
  367. $quiz_avg_score = 0;
  368. if (!empty($row['avg_score'])) {
  369. $quiz_avg_score = round($row['avg_score'],2);
  370. }
  371. if(!empty($row['num_attempts'])) {
  372. $quiz_avg_score = round($quiz_avg_score / $row['num_attempts'], 2);
  373. }
  374. if (is_array($student_id)) {
  375. $quiz_avg_score = round($quiz_avg_score / count($student_id), 2);
  376. }
  377. return $quiz_avg_score;
  378. }
  379. }
  380. return null;
  381. }
  382. /**
  383. * Get count student's exercise COMPLETED attempts
  384. * @param int Student id
  385. * @param string Course code
  386. * @param int Exercise id
  387. * @param int Learning path id (optional), for showing attempts inside a learning path $lp_id and $lp_item_id params are required.
  388. * @param int Learning path item id (optional), for showing attempts inside a learning path $lp_id and $lp_item_id params are required.
  389. * @return int count of attempts
  390. */
  391. public static function count_student_exercise_attempts($student_id, $course_code, $exercise_id, $lp_id = 0, $lp_item_id = 0, $session_id = 0) {
  392. $course_code = Database::escape_string($course_code);
  393. $student_id = intval($student_id);
  394. $exercise_id = intval($exercise_id);
  395. $session_id = intval($session_id);
  396. $count_attempts = 0;
  397. if (!empty($lp_id)) $lp_id = intval($lp_id);
  398. if (!empty($lp_item_id)) $lp_id = intval($lp_item_id);
  399. $tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  400. $sql = "SELECT COUNT(ex.exe_id) as essais FROM $tbl_stats_exercices AS ex
  401. WHERE ex.exe_cours_id = '$course_code'
  402. AND ex.exe_exo_id = $exercise_id
  403. AND status = ''
  404. AND orig_lp_id = $lp_id
  405. AND orig_lp_item_id = $lp_item_id
  406. AND exe_user_id= $student_id
  407. AND session_id = $session_id ";
  408. $rs = Database::query($sql);
  409. $row = Database::fetch_row($rs);
  410. $count_attempts = $row[0];
  411. return $count_attempts;
  412. }
  413. /**
  414. * Get count student's exercise progress
  415. * @param int user id
  416. * @param string course code
  417. * @param int session id
  418. */
  419. static function get_exercise_student_progress($exercise_list, $user_id, $course_code, $session_id) {
  420. $course_code = Database::escape_string($course_code);
  421. $user_id = intval($user_id);
  422. $session_id = intval($session_id);
  423. if (empty($exercise_list)) {
  424. return '0%';
  425. }
  426. $tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  427. $exercise_list = array_keys($exercise_list);
  428. $exercise_list = array_map('intval', $exercise_list);
  429. $exercise_list_imploded = implode("' ,'", $exercise_list);
  430. $sql = "SELECT COUNT(DISTINCT ex.exe_exo_id) FROM $tbl_stats_exercices AS ex
  431. WHERE ex.exe_cours_id = '$course_code' AND
  432. ex.session_id = $session_id AND
  433. ex.exe_user_id = $user_id AND ex.exe_exo_id IN ('$exercise_list_imploded') ";
  434. $rs = Database::query($sql);
  435. $count = 0;
  436. if ($rs) {
  437. $row = Database::fetch_row($rs);
  438. $count = $row[0];
  439. }
  440. $count = ($count != 0 ) ? 100*round(intval($count)/count($exercise_list), 2) .'%' : '0%';
  441. return $count;
  442. }
  443. static function get_exercise_student_average_best_attempt($exercise_list, $user_id, $course_code, $session_id) {
  444. $result = 0;
  445. if (!empty($exercise_list)) {
  446. foreach ($exercise_list as $exercise_data) {
  447. $exercise_id = $exercise_data['id'];
  448. $best_attempt = get_best_attempt_exercise_results_per_user($user_id, $exercise_id , $course_code, $session_id);
  449. if (!empty($best_attempt)) {
  450. $result += $best_attempt['exe_result']/$best_attempt['exe_weighting'];
  451. }
  452. }
  453. $result = $result/ count($exercise_list);
  454. $result = round($result, 2)*100;
  455. }
  456. return $result.'%';
  457. }
  458. /**
  459. * Returns the average student progress in the learning paths of the given
  460. * course.
  461. * @param int/array Student id(s)
  462. * @param string Course code
  463. * @param array Limit average to listed lp ids
  464. * @param int Session id (optional), if parameter $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  465. * @param bool Will return an array of the type: [sum_of_progresses, number] if it is set to true
  466. * @return double Average progress of the user in this course
  467. */
  468. public static function get_avg_student_progress($student_id, $course_code, $lp_ids = array(), $session_id = null, $return_array = false) {
  469. // get the informations of the course
  470. $course_info = api_get_course_info($course_code);
  471. if (!empty($course_info)) {
  472. // table definition
  473. $tbl_course_lp_view = Database :: get_course_table(TABLE_LP_VIEW);
  474. $tbl_course_lp = Database :: get_course_table(TABLE_LP_MAIN);
  475. // Compose a filter based on optional learning paths list given
  476. $condition_lp = "";
  477. if (!empty($lp_ids)) {
  478. if (count($lp_ids) > 0) {
  479. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  480. }
  481. }
  482. $session_id = intval($session_id);
  483. $sql = "SELECT id FROM $tbl_course_lp lp WHERE c_id = {$course_info['real_id']} $condition_lp";
  484. $res_count_lp = Database::query($sql);
  485. // count the number of learning paths
  486. $lp_id = array();
  487. while ($row_lp = Database::fetch_array($res_count_lp,'ASSOC')) {
  488. $lp_id[] = $row_lp['id'];
  489. }
  490. $count_lp = count($lp_id);
  491. $avg_progress = 0;
  492. //if there is at least one learning path and one student
  493. if ($count_lp>0 && !empty($student_id)) {
  494. $condition_user = "";
  495. if (is_array($student_id)) {
  496. array_walk($student_id,'intval');
  497. $condition_user = " lp_view.user_id IN (".implode(',',$student_id).") AND ";
  498. } else {
  499. $student_id = intval($student_id);
  500. $condition_user = " lp_view.user_id = '$student_id' AND ";
  501. }
  502. // Get last view for each student (in case of multi-attempt)
  503. // Also filter on LPs of this session
  504. $sql_maxes = "SELECT MAX(view_count), progress FROM $tbl_course_lp_view lp_view
  505. WHERE c_id = {$course_info['real_id']} AND
  506. $condition_user session_id = $session_id AND
  507. lp_view.lp_id IN (".implode(',',$lp_id).")
  508. GROUP BY lp_id, user_id";
  509. $res_maxes = Database::query($sql_maxes);
  510. $sum = 0;
  511. while ($row_maxes = Database::fetch_array($res_maxes)) {
  512. $sum += $row_maxes[1];
  513. }
  514. // average progress = total sum divided by the number of views
  515. // summed up.
  516. $number_items = count($lp_id);
  517. if ($number_items == 0) {
  518. return 0; //not necessary to return something else if there is no view
  519. }
  520. if (!$return_array) {
  521. $avg_progress = round($sum / $number_items, 1);
  522. return $avg_progress;
  523. } else {
  524. return array($sum, $number_items);
  525. }
  526. }
  527. }
  528. return null;
  529. }
  530. /**
  531. * This function gets:
  532. * 1. The score average from all SCORM Test items in all LP in a course-> All the answers / All the max scores.
  533. * 2. The score average from all Tests (quiz) in all LP in a course-> All the answers / All the max scores.
  534. * 3. And finally it will return the average between 1. and 2.
  535. * @todo improve performance, when loading 1500 users with 20 lps the script dies
  536. * This function does not take the results of a Test out of a LP
  537. *
  538. * @param mixed Array of user ids or an user id
  539. * @param string Course code
  540. * @param array List of LP ids
  541. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  542. * @param bool Returns an array of the type [sum_score, num_score] if set to true
  543. * @param bool get only the latest attempts or ALL attempts
  544. * @return string Value (number %) Which represents a round integer explain in got in 3.
  545. */
  546. public static function get_avg_student_score($student_id, $course_code, $lp_ids = array(), $session_id = null, $return_array = false, $get_only_latest_attempt_results = false) {
  547. $debug = false;
  548. if (empty($lp_ids)) {
  549. $debug = false;
  550. }
  551. if ($debug) echo '<h1>Tracking::get_avg_student_score</h1>';
  552. $tbl_stats_exercices = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  553. $tbl_stats_attempts = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  554. $course = api_get_course_info($course_code);
  555. if (!empty($course)) {
  556. // get course tables names
  557. $tbl_quiz_questions = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  558. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  559. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  560. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  561. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  562. $course_id = $course['real_id'];
  563. // Compose a filter based on optional learning paths list given
  564. $condition_lp = "";
  565. if (count($lp_ids) > 0) {
  566. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  567. }
  568. // Compose a filter based on optional session id
  569. $condition_session = "";
  570. $session_id = intval($session_id);
  571. if (count($lp_ids) > 0) {
  572. $condition_session = " AND session_id = $session_id ";
  573. } else {
  574. $condition_session = " WHERE session_id = $session_id ";
  575. }
  576. // Check the real number of LPs corresponding to the filter in the
  577. // database (and if no list was given, get them all)
  578. if (empty($session_id)) {
  579. $sql = "SELECT DISTINCT(id), use_max_score FROM $lp_table WHERE c_id = $course_id AND session_id = 0 $condition_lp ";
  580. } else {
  581. $sql = "SELECT DISTINCT(id), use_max_score FROM $lp_table WHERE c_id = $course_id $condition_lp ";
  582. }
  583. $res_row_lp = Database::query($sql);
  584. $count_row_lp = Database::num_rows($res_row_lp);
  585. $lp_list = $use_max_score = array();
  586. while ($row_lp = Database::fetch_array($res_row_lp)) {
  587. $lp_list[] = $row_lp['id'];
  588. $use_max_score[$row_lp['id']] = $row_lp['use_max_score'];
  589. }
  590. if ($debug) {
  591. echo 'Use max score or not list '; var_dump($use_max_score);
  592. }
  593. // Init local variables that will be used through the calculation
  594. $progress = 0;
  595. // prepare filter on users
  596. $condition_user1 = "";
  597. if (is_array($student_id)) {
  598. array_walk($student_id, 'intval');
  599. $condition_user1 =" AND user_id IN (".implode(',', $student_id).") ";
  600. } else {
  601. $condition_user1 =" AND user_id = $student_id ";
  602. }
  603. if ($count_row_lp > 0 && !empty($student_id)) {
  604. // Getting latest LP result for a student
  605. //@todo problem when a course have more than 1500 users
  606. $sql = "SELECT MAX(view_count) as vc, id, progress, lp_id, user_id FROM $lp_view_table
  607. WHERE c_id = $course_id AND
  608. lp_id IN (".implode(',',$lp_list).")
  609. $condition_user1 AND
  610. session_id = $session_id
  611. GROUP BY lp_id, user_id";
  612. if ($debug) echo $sql;
  613. $rs_last_lp_view_id = Database::query($sql);
  614. $global_result = 0;
  615. if (Database::num_rows($rs_last_lp_view_id) > 0) {
  616. // Cycle through each line of the results (grouped by lp_id, user_id)
  617. while ($row_lp_view = Database::fetch_array($rs_last_lp_view_id)) {
  618. $count_items = 0;
  619. $lp_partial_total = 0;
  620. $list = array();
  621. $lp_view_id = $row_lp_view['id'];
  622. $progress = $row_lp_view['progress'];
  623. $lp_id = $row_lp_view['lp_id'];
  624. $user_id = $row_lp_view['user_id'];
  625. if ($debug) echo '<h2>LP id '.$lp_id.'</h2>';
  626. if ($get_only_latest_attempt_results) {
  627. //Getting lp_items done by the user
  628. $sql = "SELECT DISTINCT lp_item_id
  629. FROM $lp_item_view_table
  630. WHERE c_id = $course_id AND
  631. lp_view_id = $lp_view_id
  632. ORDER BY lp_item_id";
  633. $res_lp_item = Database::query($sql);
  634. while ($row_lp_item = Database::fetch_array($res_lp_item,'ASSOC')) {
  635. $my_lp_item_id = $row_lp_item['lp_item_id'];
  636. //Getting the most recent attempt
  637. $sql = "SELECT lp_iv.id as lp_item_view_id,
  638. lp_iv.score as score,
  639. lp_i.max_score,
  640. lp_iv.max_score as max_score_item_view,
  641. lp_i.path,
  642. lp_i.item_type,
  643. lp_i.id as iid
  644. FROM $lp_item_view_table as lp_iv
  645. INNER JOIN $lp_item_table as lp_i
  646. ON lp_i.id = lp_iv.lp_item_id AND
  647. lp_iv.c_id = $course_id AND
  648. lp_i.c_id = $course_id AND
  649. (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."')
  650. WHERE lp_item_id = $my_lp_item_id AND
  651. lp_view_id = $lp_view_id
  652. ORDER BY view_count DESC
  653. LIMIT 1";
  654. $res_lp_item_result = Database::query($sql);
  655. while ($row_max_score = Database::fetch_array($res_lp_item_result,'ASSOC')) {
  656. $list[]= $row_max_score;
  657. }
  658. }
  659. } else {
  660. // For the currently analysed view, get the score and
  661. // max_score of each item if it is a sco or a TOOL_QUIZ
  662. $sql_max_score = "SELECT lp_iv.id as lp_item_view_id,
  663. lp_iv.score as score,
  664. lp_i.max_score,
  665. lp_iv.max_score as max_score_item_view,
  666. lp_i.path,
  667. lp_i.item_type,
  668. lp_i.id as iid
  669. FROM $lp_item_view_table as lp_iv
  670. INNER JOIN $lp_item_table as lp_i
  671. ON lp_i.id = lp_iv.lp_item_id AND
  672. lp_iv.c_id = $course_id AND
  673. lp_i.c_id = $course_id AND
  674. (lp_i.item_type='sco' OR lp_i.item_type='".TOOL_QUIZ."')
  675. WHERE lp_view_id = $lp_view_id ";
  676. if ($debug) echo $sql_max_score.'<br />';
  677. $res_max_score = Database::query($sql_max_score);
  678. while ($row_max_score = Database::fetch_array($res_max_score,'ASSOC')) {
  679. $list[]= $row_max_score;
  680. }
  681. }
  682. // Go through each scorable element of this view
  683. $score_of_scorm_calculate = 0;
  684. foreach ($list as $row_max_score) {
  685. $max_score = $row_max_score['max_score']; //Came from the original lp_item
  686. $max_score_item_view = $row_max_score['max_score_item_view']; //Came from the lp_item_view
  687. $score = $row_max_score['score'];
  688. if ($debug) echo '<h3>Item Type: ' .$row_max_score['item_type'].'</h3>';
  689. if ($row_max_score['item_type'] == 'sco') {
  690. //var_dump($row_max_score);
  691. // Check if it is sco (easier to get max_score)
  692. //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.
  693. if ($max_score == 0 || is_null($max_score) || $max_score == '') {
  694. //Chamilo style
  695. if ($use_max_score[$lp_id]) {
  696. $max_score = 100;
  697. } else {
  698. //Overwrites max score = 100 to use the one that came in the lp_item_view see BT#1613
  699. $max_score = $max_score_item_view;
  700. }
  701. }
  702. //Avoid division by zero errors
  703. if (!empty($max_score)) {
  704. $lp_partial_total += $score/$max_score;
  705. }
  706. if ($debug) echo '<b>$lp_partial_total, $score, $max_score '.$lp_partial_total.' '.$score.' '.$max_score.'</b><br />';
  707. } else {
  708. // Case of a TOOL_QUIZ element
  709. $item_id = $row_max_score['iid'];
  710. $item_path = $row_max_score['path'];
  711. $lp_item_view_id = $row_max_score['lp_item_view_id'];
  712. // Get last attempt to this exercise through
  713. // the current lp for the current user
  714. $sql_last_attempt = "SELECT exe_id FROM $tbl_stats_exercices WHERE
  715. exe_exo_id = '$item_path' AND
  716. exe_user_id = $user_id AND
  717. orig_lp_item_id = $item_id AND
  718. orig_lp_item_view_id = $lp_item_view_id AND
  719. exe_cours_id = '$course_code' AND
  720. session_id = $session_id
  721. ORDER BY exe_date DESC LIMIT 1";
  722. if ($debug) echo $sql_last_attempt .'<br />';
  723. $result_last_attempt = Database::query($sql_last_attempt);
  724. $num = Database :: num_rows($result_last_attempt);
  725. if ($num > 0 ) {
  726. $id_last_attempt = Database :: result($result_last_attempt, 0, 0);
  727. if ($debug) echo $id_last_attempt.'<br />';
  728. /* if (in_array($id_last_attempt, $exe_list)) {
  729. continue;
  730. }*/
  731. //$exe_list[] = $id_last_attempt;
  732. //echo $id_last_attempt .'<br />';
  733. // Within the last attempt number tracking, get the sum of
  734. // the max_scores of all questions that it was
  735. // made of (we need to make this call dynamic because of random questions selection)
  736. $sql = "SELECT SUM(t.ponderation) as maxscore FROM
  737. (
  738. SELECT DISTINCT question_id, marks, ponderation
  739. FROM $tbl_stats_attempts AS at INNER JOIN $tbl_quiz_questions AS q ON (q.id = at.question_id)
  740. WHERE exe_id ='$id_last_attempt' AND q.c_id = $course_id
  741. )
  742. AS t";
  743. if ($debug) echo '$sql: '.$sql.' <br />';
  744. $res_max_score_bis = Database::query($sql);
  745. $row_max_score_bis = Database::fetch_array($res_max_score_bis);
  746. if (!empty($row_max_score_bis['maxscore'])) {
  747. $max_score = $row_max_score_bis['maxscore'];
  748. }
  749. if (!empty($max_score) && floatval($max_score) > 0) {
  750. $lp_partial_total += $score/$max_score;
  751. }
  752. if ($debug) echo '$lp_partial_total, $score, $max_score <b>'.$lp_partial_total.' '.$score.' '.$max_score.'</b><br />';
  753. }
  754. }
  755. if (in_array($row_max_score['item_type'], array('quiz','sco'))) {
  756. // Normal way
  757. if ($use_max_score[$lp_id]) {
  758. $count_items++;
  759. } else {
  760. if ($max_score != '') {
  761. $count_items++;
  762. }
  763. }
  764. if ($debug) echo '$count_items: '.$count_items;
  765. }
  766. } //end for
  767. $score_of_scorm_calculate += $count_items?(($lp_partial_total/$count_items)*100):0;
  768. if ($debug) echo '<h3>$count_items '.$count_items.'</h3>';
  769. if ($debug) echo '<h3>$score_of_scorm_calculate '.$score_of_scorm_calculate.'</h3>';
  770. // var_dump($score_of_scorm_calculate);
  771. $global_result += $score_of_scorm_calculate;
  772. if ($debug) echo '<h3>$global_result '.$global_result.'</h3>';
  773. } // end while
  774. }
  775. $lp_with_quiz = 0;
  776. if ($debug) var_dump($lp_list);
  777. foreach ($lp_list as $lp_id) {
  778. //Check if LP have a score we asume that all SCO have an score
  779. $sql = "SELECT count(id) as count FROM $lp_item_table
  780. WHERE c_id = $course_id AND (item_type = 'quiz' OR item_type = 'sco') AND lp_id = ".$lp_id;
  781. if ($debug) echo $sql;
  782. $result_have_quiz = Database::query($sql);
  783. if (Database::num_rows($result_have_quiz) > 0 ) {
  784. $row = Database::fetch_array($result_have_quiz,'ASSOC');
  785. if (is_numeric($row['count']) && $row['count'] != 0) {
  786. $lp_with_quiz ++;
  787. }
  788. }
  789. }
  790. if ($debug) echo '<h3>$lp_with_quiz '.$lp_with_quiz.' </h3>';
  791. if ($debug) echo '<h3>Final return</h3>';
  792. if ($lp_with_quiz != 0 ) {
  793. if (!$return_array) {
  794. $score_of_scorm_calculate = round(($global_result/$lp_with_quiz),2);
  795. if ($debug) var_dump($score_of_scorm_calculate);
  796. if (empty($lp_ids)) {
  797. //$score_of_scorm_calculate = round($score_of_scorm_calculate/count($lp_list),2);
  798. if ($debug) echo '<h2>All lps fix: '.$score_of_scorm_calculate.'</h2>';
  799. }
  800. return $score_of_scorm_calculate;
  801. } else {
  802. if ($debug) var_dump($global_result, $lp_with_quiz);
  803. return array($global_result, $lp_with_quiz);
  804. }
  805. } else {
  806. return '-';
  807. }
  808. }
  809. }
  810. return null;
  811. }
  812. /**
  813. * This function gets time spent in learning path for a student inside a course
  814. * @param int|array Student id(s)
  815. * @param string Course code
  816. * @param array Limit average to listed lp ids
  817. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  818. * @return int Total time
  819. */
  820. public static function get_time_spent_in_lp($student_id, $course_code, $lp_ids = array(), $session_id = null) {
  821. $course = CourseManager :: get_course_information($course_code);
  822. $student_id = intval($student_id);
  823. $total_time = 0;
  824. if (!empty($course)) {
  825. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  826. $t_lpv = Database :: get_course_table(TABLE_LP_VIEW);
  827. $t_lpiv = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  828. $course_id = $course['real_id'];
  829. // Compose a filter based on optional learning paths list given
  830. $condition_lp = "";
  831. if(count($lp_ids) > 0) {
  832. $condition_lp =" AND id IN(".implode(',',$lp_ids).") ";
  833. }
  834. // Compose a filter based on optional session id
  835. $condition_session = "";
  836. $session_id = intval($session_id);
  837. if (isset($session_id)) {
  838. $condition_session = " AND session_id = $session_id ";
  839. }
  840. // Check the real number of LPs corresponding to the filter in the
  841. // database (and if no list was given, get them all)
  842. //$res_row_lp = Database::query("SELECT DISTINCT(id) FROM $lp_table $condition_lp $condition_session");
  843. $res_row_lp = Database::query("SELECT DISTINCT(id) FROM $lp_table WHERE c_id = $course_id $condition_lp");
  844. $count_row_lp = Database::num_rows($res_row_lp);
  845. // calculates time
  846. if ($count_row_lp > 0) {
  847. while ($row_lp = Database::fetch_array($res_row_lp)) {
  848. $lp_id = intval($row_lp['id']);
  849. $sql = 'SELECT SUM(total_time)
  850. FROM '.$t_lpiv.' AS item_view
  851. INNER JOIN '.$t_lpv.' AS view
  852. ON item_view.lp_view_id = view.id
  853. WHERE
  854. item_view.c_id = '.$course_id.' AND
  855. view.c_id = '.$course_id.' AND
  856. view.lp_id = '.$lp_id.'
  857. AND view.user_id = '.$student_id.' AND
  858. session_id = '.$session_id;
  859. $rs = Database::query($sql);
  860. if (Database :: num_rows($rs) > 0) {
  861. $total_time += Database :: result($rs, 0, 0);
  862. }
  863. }
  864. }
  865. }
  866. return $total_time;
  867. }
  868. /**
  869. * This function gets last connection time to one learning path
  870. * @param int|array Student id(s)
  871. * @param string Course code
  872. * @param int Learning path id
  873. * @return int Total time
  874. */
  875. public static function get_last_connection_time_in_lp($student_id, $course_code, $lp_id, $session_id = 0) {
  876. $course = CourseManager :: get_course_information($course_code);
  877. $student_id = intval($student_id);
  878. $lp_id = intval($lp_id);
  879. $last_time = 0;
  880. $session_id = intval($session_id);
  881. if (!empty($course)) {
  882. $course_id = $course['real_id'];
  883. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  884. $t_lpv = Database :: get_course_table(TABLE_LP_VIEW);
  885. $t_lpiv = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  886. // Check the real number of LPs corresponding to the filter in the
  887. // database (and if no list was given, get them all)
  888. $res_row_lp = Database::query("SELECT id FROM $lp_table WHERE c_id = $course_id AND id = $lp_id ");
  889. $count_row_lp = Database::num_rows($res_row_lp);
  890. // calculates last connection time
  891. if ($count_row_lp > 0) {
  892. $sql = 'SELECT MAX(start_time)
  893. FROM ' . $t_lpiv . ' AS item_view
  894. INNER JOIN ' . $t_lpv . ' AS view
  895. ON item_view.lp_view_id = view.id
  896. WHERE
  897. item_view.c_id = '.$course_id.' AND
  898. view.c_id = '.$course_id.' AND
  899. view.lp_id = '.$lp_id.'
  900. AND view.user_id = '.$student_id.'
  901. AND view.session_id = '.$session_id;
  902. $rs = Database::query($sql);
  903. if (Database :: num_rows($rs) > 0) {
  904. $last_time = Database :: result($rs, 0, 0);
  905. }
  906. }
  907. }
  908. return $last_time;
  909. }
  910. /**
  911. * gets the list of students followed by coach
  912. * @param int Coach id
  913. * @return array List of students
  914. */
  915. public static function get_student_followed_by_coach($coach_id) {
  916. $coach_id = intval($coach_id);
  917. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  918. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  919. $tbl_session_user = Database :: get_main_table(TABLE_MAIN_SESSION_USER);
  920. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  921. $a_students = array ();
  922. // At first, courses where $coach_id is coach of the course //
  923. $sql = 'SELECT id_session, course_code FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id.' AND status=2';
  924. if (api_is_multiple_url_enabled()) {
  925. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  926. $access_url_id = api_get_current_access_url_id();
  927. if ($access_url_id != -1) {
  928. $sql = 'SELECT scu.id_session, scu.course_code
  929. FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_session_rel_access_url.' sru
  930. ON (scu.id_session=sru.session_id)
  931. WHERE scu.id_user=' . $coach_id.' AND scu.status=2 AND sru.access_url_id = '.$access_url_id;
  932. }
  933. }
  934. $result = Database::query($sql);
  935. while ($a_courses = Database::fetch_array($result)) {
  936. $course_code = $a_courses["course_code"];
  937. $id_session = $a_courses["id_session"];
  938. $sql = "SELECT distinct srcru.id_user
  939. FROM $tbl_session_course_user AS srcru, $tbl_session_user sru
  940. 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'";
  941. $rs = Database::query($sql);
  942. while ($row = Database::fetch_array($rs)) {
  943. $a_students[$row['id_user']] = $row['id_user'];
  944. }
  945. }
  946. // Then, courses where $coach_id is coach of the session //
  947. $sql = 'SELECT session_course_user.id_user
  948. FROM ' . $tbl_session_course_user . ' as session_course_user
  949. INNER JOIN '.$tbl_session_user.' sru ON session_course_user.id_user = sru.id_user AND session_course_user.id_session = sru.id_session
  950. INNER JOIN ' . $tbl_session_course . ' as session_course
  951. ON session_course.course_code = session_course_user.course_code
  952. AND session_course_user.id_session = session_course.id_session
  953. INNER JOIN ' . $tbl_session . ' as session
  954. ON session.id = session_course.id_session
  955. AND session.id_coach = ' . $coach_id;
  956. if (api_is_multiple_url_enabled()) {
  957. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  958. $access_url_id = api_get_current_access_url_id();
  959. if ($access_url_id != -1){
  960. $sql = 'SELECT session_course_user.id_user
  961. FROM ' . $tbl_session_course_user . ' as session_course_user
  962. INNER JOIN '.$tbl_session_user.' sru ON session_course_user.id_user = sru.id_user AND session_course_user.id_session = sru.id_session
  963. INNER JOIN ' . $tbl_session_course . ' as session_course
  964. ON session_course.course_code = session_course_user.course_code
  965. AND session_course_user.id_session = session_course.id_session
  966. INNER JOIN ' . $tbl_session . ' as session
  967. ON session.id = session_course.id_session
  968. AND session.id_coach = ' . $coach_id.'
  969. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  970. ON session.id = session_rel_url.session_id WHERE access_url_id = '.$access_url_id;
  971. }
  972. }
  973. $result = Database::query($sql);
  974. while ($row = Database::fetch_array($result)) {
  975. $a_students[$row['id_user']] = $row['id_user'];
  976. }
  977. return $a_students;
  978. }
  979. /**
  980. * Get student followed by a coach inside a session
  981. * @param int Session id
  982. * @param int Coach id
  983. * @return array students list
  984. */
  985. public static function get_student_followed_by_coach_in_a_session($id_session, $coach_id) {
  986. $coach_id = intval($coach_id);
  987. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  988. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  989. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  990. $a_students = array ();
  991. // At first, courses where $coach_id is coach of the course //
  992. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_session="' . $id_session . '" AND id_user=' . $coach_id.' AND status=2';
  993. $result = Database::query($sql);
  994. while ($a_courses = Database::fetch_array($result)) {
  995. $course_code = $a_courses["course_code"];
  996. $sql = "SELECT distinct srcru.id_user
  997. FROM $tbl_session_course_user AS srcru
  998. WHERE course_code='$course_code' and id_session = '" . $id_session . "'";
  999. $rs = Database::query($sql);
  1000. while ($row = Database::fetch_array($rs)) {
  1001. $a_students[$row['id_user']] = $row['id_user'];
  1002. }
  1003. }
  1004. // Then, courses where $coach_id is coach of the session //
  1005. $dsl_session_coach = 'SELECT id_coach FROM ' . $tbl_session . ' WHERE id="' . $id_session . '" AND id_coach="' . $coach_id . '"';
  1006. $result = Database::query($dsl_session_coach);
  1007. //He is the session_coach so we select all the users in the session
  1008. if (Database::num_rows($result) > 0) {
  1009. $sql = 'SELECT DISTINCT srcru.id_user FROM ' . $tbl_session_course_user . ' AS srcru WHERE id_session="' . $id_session . '"';
  1010. $result = Database::query($sql);
  1011. while ($row = Database::fetch_array($result)) {
  1012. $a_students[$row['id_user']] = $row['id_user'];
  1013. }
  1014. }
  1015. return $a_students;
  1016. }
  1017. /**
  1018. * Check if a coach is allowed to follow a student
  1019. * @param int Coach id
  1020. * @param int Student id
  1021. * @return bool
  1022. */
  1023. public static function is_allowed_to_coach_student($coach_id, $student_id) {
  1024. $coach_id = intval($coach_id);
  1025. $student_id = intval($student_id);
  1026. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1027. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1028. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1029. // At first, courses where $coach_id is coach of the course //
  1030. $sql = 'SELECT 1 FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id .' AND status=2';
  1031. $result = Database::query($sql);
  1032. if (Database::num_rows($result) > 0) {
  1033. return true;
  1034. }
  1035. // Then, courses where $coach_id is coach of the session
  1036. $sql = 'SELECT session_course_user.id_user
  1037. FROM ' . $tbl_session_course_user . ' as session_course_user
  1038. INNER JOIN ' . $tbl_session_course . ' as session_course
  1039. ON session_course.course_code = session_course_user.course_code
  1040. INNER JOIN ' . $tbl_session . ' as session
  1041. ON session.id = session_course.id_session
  1042. AND session.id_coach = ' . $coach_id . '
  1043. WHERE id_user = ' . $student_id;
  1044. $result = Database::query($sql);
  1045. if (Database::num_rows($result) > 0) {
  1046. return true;
  1047. }
  1048. return false;
  1049. }
  1050. /**
  1051. * Get courses followed by coach
  1052. * @param int Coach id
  1053. * @param int Session id (optional)
  1054. * @return array Courses list
  1055. */
  1056. public static function get_courses_followed_by_coach($coach_id, $id_session = '') {
  1057. $coach_id = intval($coach_id);
  1058. if (!empty ($id_session))
  1059. $id_session = intval($id_session);
  1060. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1061. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1062. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1063. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  1064. // At first, courses where $coach_id is coach of the course
  1065. $sql = 'SELECT DISTINCT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user=' . $coach_id.' AND status=2';
  1066. global $_configuration;
  1067. if ($_configuration['multiple_access_urls']) {
  1068. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1069. $access_url_id = api_get_current_access_url_id();
  1070. if ($access_url_id != -1){
  1071. $sql = 'SELECT DISTINCT scu.course_code FROM ' . $tbl_session_course_user . ' scu INNER JOIN '.$tbl_course_rel_access_url.' cru
  1072. ON (scu.course_code = cru.course_code)
  1073. WHERE scu.id_user=' . $coach_id.' AND scu.status=2 AND cru.access_url_id = '.$access_url_id;
  1074. }
  1075. }
  1076. if (!empty ($id_session))
  1077. $sql .= ' AND id_session=' . $id_session;
  1078. $result = Database::query($sql);
  1079. while ($row = Database::fetch_array($result)) {
  1080. $a_courses[$row['course_code']] = $row['course_code'];
  1081. }
  1082. // Then, courses where $coach_id is coach of the session
  1083. $sql = 'SELECT DISTINCT session_course.course_code
  1084. FROM ' . $tbl_session_course . ' as session_course
  1085. INNER JOIN ' . $tbl_session . ' as session
  1086. ON session.id = session_course.id_session
  1087. AND session.id_coach = ' . $coach_id . '
  1088. INNER JOIN ' . $tbl_course . ' as course
  1089. ON course.code = session_course.course_code';
  1090. if ($_configuration['multiple_access_urls']) {
  1091. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1092. $access_url_id = api_get_current_access_url_id();
  1093. if ($access_url_id != -1){
  1094. $sql = 'SELECT DISTINCT session_course.course_code
  1095. FROM ' . $tbl_session_course . ' as session_course
  1096. INNER JOIN ' . $tbl_session . ' as session
  1097. ON session.id = session_course.id_session
  1098. AND session.id_coach = ' . $coach_id . '
  1099. INNER JOIN ' . $tbl_course . ' as course
  1100. ON course.code = session_course.course_code
  1101. INNER JOIN '.$tbl_course_rel_access_url.' course_rel_url
  1102. ON (session_course.course_code = course_rel_url.course_code)';
  1103. }
  1104. }
  1105. if (!empty ($id_session)) {
  1106. $sql .= ' WHERE session_course.id_session=' . $id_session;
  1107. if ($_configuration['multiple_access_urls'])
  1108. $sql .= ' AND access_url_id = '.$access_url_id;
  1109. } else {
  1110. if ($_configuration['multiple_access_urls'])
  1111. $sql .= ' WHERE access_url_id = '.$access_url_id;
  1112. }
  1113. $result = Database::query($sql);
  1114. while ($row = Database::fetch_array($result)) {
  1115. $a_courses[$row['course_code']] = $row['course_code'];
  1116. }
  1117. return $a_courses;
  1118. }
  1119. /**
  1120. * Get sessions coached by user
  1121. * @param int Coach id
  1122. * @return array Sessions list
  1123. */
  1124. public static function get_sessions_coached_by_user($coach_id) {
  1125. // table definition
  1126. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1127. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1128. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1129. // protect datas
  1130. $coach_id = intval($coach_id);
  1131. // session where we are general coach
  1132. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  1133. FROM ' . $tbl_session . '
  1134. WHERE id_coach=' . $coach_id;
  1135. global $_configuration;
  1136. if ($_configuration['multiple_access_urls']) {
  1137. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1138. $access_url_id = api_get_current_access_url_id();
  1139. if ($access_url_id != -1){
  1140. $sql = 'SELECT DISTINCT id, name, date_start, date_end
  1141. FROM ' . $tbl_session . ' session INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  1142. ON (session.id = session_rel_url.session_id)
  1143. WHERE id_coach=' . $coach_id.' AND access_url_id = '.$access_url_id;
  1144. }
  1145. }
  1146. $rs = Database::query($sql);
  1147. while ($row = Database::fetch_array($rs)) {
  1148. $a_sessions[$row["id"]] = $row;
  1149. }
  1150. // session where we are coach of a course
  1151. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  1152. FROM ' . $tbl_session . ' as session
  1153. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  1154. ON session.id = session_course_user.id_session
  1155. AND session_course_user.id_user=' . $coach_id.' AND session_course_user.status=2';
  1156. global $_configuration;
  1157. if ($_configuration['multiple_access_urls']) {
  1158. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1159. $access_url_id = api_get_current_access_url_id();
  1160. if ($access_url_id != -1){
  1161. $sql = 'SELECT DISTINCT session.id, session.name, session.date_start, session.date_end
  1162. FROM ' . $tbl_session . ' as session
  1163. INNER JOIN ' . $tbl_session_course_user . ' as session_course_user
  1164. ON session.id = session_course_user.id_session AND session_course_user.id_user=' . $coach_id.' AND session_course_user.status=2
  1165. INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
  1166. ON (session.id = session_rel_url.session_id)
  1167. WHERE access_url_id = '.$access_url_id;
  1168. }
  1169. }
  1170. $rs = Database::query($sql);
  1171. while ($row = Database::fetch_array($rs)) {
  1172. $a_sessions[$row["id"]] = $row;
  1173. }
  1174. if (is_array($a_sessions)) {
  1175. foreach ($a_sessions as & $session) {
  1176. if ($session['date_start'] == '0000-00-00') {
  1177. $session['status'] = get_lang('SessionActive');
  1178. }
  1179. else {
  1180. $date_start = explode('-', $session['date_start']);
  1181. $time_start = mktime(0, 0, 0, $date_start[1], $date_start[2], $date_start[0]);
  1182. $date_end = explode('-', $session['date_end']);
  1183. $time_end = mktime(0, 0, 0, $date_end[1], $date_end[2], $date_end[0]);
  1184. if ($time_start < time() && time() < $time_end) {
  1185. $session['status'] = get_lang('SessionActive');
  1186. }
  1187. else{
  1188. if (time() < $time_start) {
  1189. $session['status'] = get_lang('SessionFuture');
  1190. }
  1191. else{
  1192. if (time() > $time_end) {
  1193. $session['status'] = get_lang('SessionPast');
  1194. }
  1195. }
  1196. }
  1197. }
  1198. }
  1199. }
  1200. return $a_sessions;
  1201. }
  1202. /**
  1203. * Get courses list from a session
  1204. * @param int Session id
  1205. * @return array Courses list
  1206. */
  1207. public static function get_courses_list_from_session($session_id) {
  1208. //protect datas
  1209. $session_id = intval($session_id);
  1210. // table definition
  1211. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1212. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  1213. $sql = 'SELECT DISTINCT course_code
  1214. FROM ' . $tbl_session_course . '
  1215. WHERE id_session=' . $session_id;
  1216. $rs = Database::query($sql);
  1217. $a_courses = array ();
  1218. while ($row = Database::fetch_array($rs)) {
  1219. $a_courses[$row['course_code']] = $row;
  1220. }
  1221. return $a_courses;
  1222. }
  1223. /**
  1224. * Count the number of documents that an user has uploaded to a course
  1225. * @param int|array Student id(s)
  1226. * @param string Course code
  1227. * @param int Session id (optional), if param $session_id is null(default) return count of assignments including sessions, 0 = session is not filtered
  1228. * @return int Number of documents
  1229. */
  1230. public static function count_student_uploaded_documents($student_id, $course_code, $session_id = null){
  1231. // protect datas
  1232. $course_code = Database::escape_string($course_code);
  1233. // get the informations of the course
  1234. $a_course = CourseManager :: get_course_information($course_code);
  1235. if (!empty($a_course)) {
  1236. // table definition
  1237. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1238. $tbl_document = Database :: get_course_table(TABLE_DOCUMENT);
  1239. $course_id = $a_course['real_id'];
  1240. $condition_user = "";
  1241. if (is_array($student_id)) {
  1242. $condition_user = " AND ip.insert_user_id IN (".implode(',',$student_id).") ";
  1243. } else {
  1244. $condition_user = " AND ip.insert_user_id = '$student_id' ";
  1245. }
  1246. $condition_session = "";
  1247. if (isset($session_id)) {
  1248. $session_id = intval($session_id);
  1249. $condition_session = " AND pub.session_id = $session_id ";
  1250. }
  1251. $sql = "SELECT count(ip.tool)
  1252. FROM $tbl_item_property ip INNER JOIN $tbl_document pub
  1253. ON ip.ref = pub.id
  1254. WHERE ip.c_id = $course_id AND
  1255. pub.c_id = $course_id AND
  1256. pub.filetype ='file' AND
  1257. ip.tool = 'document'
  1258. $condition_user $condition_session ";
  1259. $rs = Database::query($sql);
  1260. $row = Database::fetch_row($rs);
  1261. return $row[0];
  1262. }
  1263. return null;
  1264. }
  1265. /**
  1266. * Count assignments per student
  1267. * @param int|array Student id(s)
  1268. * @param string Course code
  1269. * @param int Session id (optional), if param $session_id is null(default) return count of assignments including sessions, 0 = session is not filtered
  1270. * @return int Count of assignments
  1271. */
  1272. public static function count_student_assignments($student_id, $course_code, $session_id = null) {
  1273. // protect datas
  1274. $course_code = Database::escape_string($course_code);
  1275. // get the informations of the course
  1276. $a_course = CourseManager :: get_course_information($course_code);
  1277. if (!empty($a_course)) {
  1278. // table definition
  1279. $tbl_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1280. $tbl_student_publication = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  1281. $course_id = $a_course['real_id'];
  1282. $condition_user = "";
  1283. if (is_array($student_id)) {
  1284. $condition_user = " AND ip.insert_user_id IN (".implode(',',$student_id).") ";
  1285. } else {
  1286. $condition_user = " AND ip.insert_user_id = '$student_id' ";
  1287. }
  1288. $condition_session = "";
  1289. if (isset($session_id)) {
  1290. $session_id = intval($session_id);
  1291. $condition_session = " AND pub.session_id = $session_id ";
  1292. }
  1293. $sql = "SELECT count(ip.tool)
  1294. FROM $tbl_item_property ip INNER JOIN $tbl_student_publication pub
  1295. ON ip.ref = pub.id
  1296. WHERE ip.c_id = $course_id AND
  1297. pub.c_id = $course_id AND
  1298. ip.tool='work'
  1299. $condition_user $condition_session ";
  1300. $rs = Database::query($sql);
  1301. $row = Database::fetch_row($rs);
  1302. return $row[0];
  1303. }
  1304. return null;
  1305. }
  1306. /**
  1307. * Count messages per student inside forum tool
  1308. * @param int Student id
  1309. * @param string Course code
  1310. * @param int Session id (optional), if param $session_id is null(default) return count of messages including sessions, 0 = session is not filtered
  1311. * @return int Count of messages
  1312. */
  1313. public static function count_student_messages($student_id, $course_code, $session_id = null) {
  1314. // protect datas
  1315. $student_id = intval($student_id);
  1316. $course_code = addslashes($course_code);
  1317. // get the informations of the course
  1318. $a_course = CourseManager :: get_course_information($course_code);
  1319. if (!empty($a_course)) {
  1320. // table definition
  1321. $tbl_forum_post = Database :: get_course_table(TABLE_FORUM_POST);
  1322. $tbl_forum = Database :: get_course_table(TABLE_FORUM);
  1323. $course_id = $a_course['real_id'];
  1324. $condition_user = "";
  1325. if (is_array($student_id)) {
  1326. $condition_user = " AND post.poster_id IN (".implode(',',$student_id).") ";
  1327. } else {
  1328. $condition_user = " AND post.poster_id = '$student_id' ";
  1329. }
  1330. $condition_session = "";
  1331. if (isset($session_id)) {
  1332. $session_id = intval($session_id);
  1333. $condition_session = " AND forum.session_id = $session_id";
  1334. }
  1335. $sql = "SELECT 1 FROM $tbl_forum_post post INNER JOIN $tbl_forum forum
  1336. ON forum.forum_id = post.forum_id
  1337. WHERE post.c_id = $course_id AND
  1338. forum.c_id = $course_id
  1339. $condition_user $condition_session ";
  1340. $rs = Database::query($sql);
  1341. return Database::num_rows($rs);
  1342. } else {
  1343. return null;
  1344. }
  1345. }
  1346. /**
  1347. * This function counts the number of post by course
  1348. * @param string Course code
  1349. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1350. * @return int The number of post by course
  1351. */
  1352. public static function count_number_of_posts_by_course($course_code, $session_id = null) {
  1353. //protect data
  1354. $course_code = Database::escape_string($course_code);
  1355. // get the informations of the course
  1356. $a_course = CourseManager :: get_course_information($course_code);
  1357. $count = 0;
  1358. if (!empty($a_course)) {
  1359. $tbl_posts = Database :: get_course_table(TABLE_FORUM_POST);
  1360. $tbl_forums = Database :: get_course_table(TABLE_FORUM);
  1361. $condition_session = '';
  1362. if (isset($session_id)) {
  1363. $session_id = intval($session_id);
  1364. $condition_session = ' AND f.session_id = '. $session_id;
  1365. }
  1366. $course_id = $a_course['real_id'];
  1367. $sql = "SELECT count(*) FROM $tbl_posts p INNER JOIN $tbl_forums f
  1368. ON f.forum_id = p.forum_id
  1369. WHERE p.c_id = $course_id AND
  1370. f.c_id = $course_id
  1371. $condition_session
  1372. ";
  1373. $result = Database::query($sql);
  1374. $row = Database::fetch_row($result);
  1375. $count = $row[0];
  1376. return $count;
  1377. } else {
  1378. return null;
  1379. }
  1380. }
  1381. /**
  1382. * This function counts the number of threads by course
  1383. * @param string Course code
  1384. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1385. * @return int The number of threads by course
  1386. */
  1387. public static function count_number_of_threads_by_course($course_code, $session_id = null) {
  1388. $course_info = api_get_course_info($course_code);
  1389. if (empty($course_info)) {
  1390. return null;
  1391. }
  1392. $course_id = $course_info['real_id'];
  1393. $count = 0;
  1394. $tbl_threads = Database :: get_course_table(TABLE_FORUM_THREAD);
  1395. $tbl_forums = Database :: get_course_table(TABLE_FORUM);
  1396. $condition_session = '';
  1397. if (isset($session_id)) {
  1398. $session_id = intval($session_id);
  1399. $condition_session = ' AND f.session_id = '. $session_id;
  1400. }
  1401. $sql = "SELECT count(*) FROM $tbl_threads t INNER JOIN $tbl_forums f ON f.forum_id = t.forum_id
  1402. WHERE t.c_id = $course_id AND f.c_id = $course_id $condition_session ";
  1403. $result = Database::query($sql);
  1404. if (Database::num_rows($result)) {
  1405. $row = Database::fetch_row($result);
  1406. $count = $row[0];
  1407. return $count;
  1408. } else {
  1409. return null;
  1410. }
  1411. }
  1412. /**
  1413. * This function counts the number of forums by course
  1414. * @param string Course code
  1415. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1416. * @return int The number of forums by course
  1417. */
  1418. public static function count_number_of_forums_by_course($course_code, $session_id = null) {
  1419. $course_info = api_get_course_info($course_code);
  1420. if (empty($course_info)) {
  1421. return null;
  1422. }
  1423. $course_id = $course_info['real_id'];
  1424. $condition_session = '';
  1425. if (isset($session_id)) {
  1426. $session_id = intval($session_id);
  1427. $condition_session = ' session_id = '. $session_id;
  1428. }
  1429. $tbl_forums = Database :: get_course_table(TABLE_FORUM);
  1430. $sql = "SELECT count(*) FROM $tbl_forums WHERE c_id = $course_id AND $condition_session";
  1431. $result = Database::query($sql);
  1432. if (Database::num_rows($result)) {
  1433. $row = Database::fetch_row($result);
  1434. $count = $row[0];
  1435. return $count;
  1436. } else {
  1437. return null;
  1438. }
  1439. }
  1440. /**
  1441. * This function counts the chat last connections by course in x days
  1442. * @param string Course code
  1443. * @param int Last x days
  1444. * @param int Session id (optional)
  1445. * @return int Chat last connections by course in x days
  1446. */
  1447. public static function chat_connections_during_last_x_days_by_course($course_code,$last_days, $session_id = 0) {
  1448. $course_info = api_get_course_info($course_code);
  1449. if (empty($course_info)) {
  1450. return null;
  1451. }
  1452. $course_id = $course_info['real_id'];
  1453. //protect data
  1454. $last_days = intval($last_days);
  1455. $course_code = Database::escape_string($course_code);
  1456. $session_id = intval($session_id);
  1457. $count = 0;
  1458. $tbl_stats_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1459. $sql = "SELECT count(*) FROM $tbl_stats_access WHERE DATE_SUB(NOW(),INTERVAL $last_days DAY) <= access_date
  1460. AND access_cours_code = '$course_code' AND access_tool='".TOOL_CHAT."' AND access_session_id='$session_id' ";
  1461. $result = Database::query($sql);
  1462. if (Database::num_rows($result)) {
  1463. $row = Database::fetch_row($result);
  1464. $count = $row[0];
  1465. return $count;
  1466. } else {
  1467. return null;
  1468. }
  1469. }
  1470. /**
  1471. * This function gets the last student's connection in chat
  1472. * @param int Student id
  1473. * @param string Course code
  1474. * @param int Session id (optional)
  1475. * @return string datetime formatted without day (e.g: February 23, 2010 10:20:50 )
  1476. */
  1477. public static function chat_last_connection($student_id, $course_code, $session_id = 0) {
  1478. //protect datas
  1479. $student_id = intval($student_id);
  1480. $course_code= Database::escape_string($course_code);
  1481. $session_id = intval($session_id);
  1482. $date_time = '';
  1483. // table definition
  1484. $tbl_stats_access = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1485. $sql = "SELECT access_date FROM $tbl_stats_access
  1486. 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";
  1487. $rs = Database::query($sql);
  1488. if (Database::num_rows($rs) > 0) {
  1489. $row = Database::fetch_array($rs);
  1490. $date_time = api_convert_and_format_date($row['access_date'], null, date_default_timezone_get());
  1491. }
  1492. return $date_time;
  1493. }
  1494. /**
  1495. * Get count student's visited links
  1496. * @param int Student id
  1497. * @param string Course code
  1498. * @param int Session id (optional)
  1499. * @return int count of visited links
  1500. */
  1501. public static function count_student_visited_links($student_id, $course_code, $session_id = 0) {
  1502. // protect datas
  1503. $student_id = intval($student_id);
  1504. $course_code = Database::escape_string($course_code);
  1505. $session_id = intval($session_id);
  1506. // table definition
  1507. $tbl_stats_links = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1508. $sql = 'SELECT 1
  1509. FROM '.$tbl_stats_links.'
  1510. WHERE links_user_id= '.$student_id.'
  1511. AND links_cours_id = "'.$course_code.'"
  1512. AND links_session_id = '.$session_id.' ';
  1513. $rs = Database::query($sql);
  1514. return Database::num_rows($rs);
  1515. }
  1516. /**
  1517. * Get count student downloaded documents
  1518. * @param int Student id
  1519. * @param string Course code
  1520. * @param int Session id (optional)
  1521. * @return int Count downloaded documents
  1522. */
  1523. public static function count_student_downloaded_documents($student_id, $course_code, $session_id = 0) {
  1524. // protect datas
  1525. $student_id = intval($student_id);
  1526. $course_code = Database::escape_string($course_code);
  1527. $session_id = intval($session_id);
  1528. // table definition
  1529. $tbl_stats_documents = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1530. $sql = 'SELECT 1
  1531. FROM ' . $tbl_stats_documents . '
  1532. WHERE down_user_id = '.$student_id.'
  1533. AND down_cours_id = "'.$course_code.'"
  1534. AND down_session_id = '.$session_id.' ';
  1535. $rs = Database::query($sql);
  1536. return Database::num_rows($rs);
  1537. }
  1538. /**
  1539. * Get course list inside a session from a student
  1540. * @param int Student id
  1541. * @param int Session id (optional)
  1542. * @return array Courses list
  1543. */
  1544. public static function get_course_list_in_session_from_student($user_id, $id_session = 0) {
  1545. $user_id = intval($user_id);
  1546. $id_session = intval($id_session);
  1547. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1548. $sql = 'SELECT course_code FROM ' . $tbl_session_course_user . ' WHERE id_user="' . $user_id . '" AND id_session="' . $id_session . '"';
  1549. $result = Database::query($sql);
  1550. $a_courses = array ();
  1551. while ($row = Database::fetch_array($result)) {
  1552. $a_courses[$row['course_code']] = $row['course_code'];
  1553. }
  1554. return $a_courses;
  1555. }
  1556. /**
  1557. * Get inactives students in course
  1558. * @param string Course code
  1559. * @param string Since login course date (optional, default = 'never')
  1560. * @param int Session id (optional)
  1561. * @return array Inactives users
  1562. */
  1563. public static function get_inactives_students_in_course($course_code, $since = 'never', $session_id=0) {
  1564. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1565. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1566. $table_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1567. $inner = '';
  1568. if($session_id!=0)
  1569. {
  1570. $inner = ' INNER JOIN '.$tbl_session_course_user.' session_course_user
  1571. ON stats_login.course_code = session_course_user.course_code
  1572. AND session_course_user.id_session = '.intval($session_id).'
  1573. AND session_course_user.id_user = stats_login.user_id ';
  1574. }
  1575. $sql = 'SELECT user_id, MAX(login_course_date) max_date FROM'.$tbl_track_login.' stats_login'.$inner.'
  1576. GROUP BY user_id
  1577. HAVING DATE_SUB( NOW(), INTERVAL '.$since.' DAY) > max_date ';
  1578. //HAVING DATE_ADD(max_date, INTERVAL '.$since.' DAY) < NOW() ';
  1579. if ($since == 'never') {
  1580. $sql = 'SELECT course_user.user_id FROM '.$table_course_rel_user.' course_user
  1581. LEFT JOIN '. $tbl_track_login.' stats_login
  1582. ON course_user.user_id = stats_login.user_id AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' '.
  1583. $inner.'
  1584. WHERE course_user.course_code = \''.Database::escape_string($course_code).'\'
  1585. AND stats_login.login_course_date IS NULL
  1586. GROUP BY course_user.user_id';
  1587. }
  1588. $rs = Database::query($sql);
  1589. $inactive_users = array();
  1590. while($user = Database::fetch_array($rs))
  1591. {
  1592. $inactive_users[] = $user['user_id'];
  1593. }
  1594. return $inactive_users;
  1595. }
  1596. /**
  1597. * Get count login per student
  1598. * @param int Student id
  1599. * @param string Course code
  1600. * @param int Session id (optional)
  1601. * @return int count login
  1602. */
  1603. public static function count_login_per_student($student_id, $course_code, $session_id = 0) {
  1604. $student_id = intval($student_id);
  1605. $course_code = Database::escape_string($course_code);
  1606. $session_id = intval($session_id);
  1607. $tbl_course_rel_user = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  1608. $sql = 'SELECT '.$student_id.'
  1609. FROM ' . $tbl_course_rel_user . '
  1610. WHERE access_user_id=' . $student_id . '
  1611. AND access_cours_code="' . $course_code . '" AND access_session_id = "'.$session_id.'" ';
  1612. $rs = Database::query($sql);
  1613. $nb_login = Database::num_rows($rs);
  1614. return $nb_login;
  1615. }
  1616. /**
  1617. * Get students followed by a human resources manager
  1618. * @param int Drh id
  1619. * @return array Student list
  1620. */
  1621. public static function get_student_followed_by_drh($hr_dept_id) {
  1622. $hr_dept_id = intval($hr_dept_id);
  1623. $a_students = array();
  1624. $tbl_organism = Database :: get_main_table(TABLE_MAIN_ORGANISM);
  1625. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  1626. $sql = 'SELECT DISTINCT user_id FROM '.$tbl_user.' as user
  1627. WHERE hr_dept_id='.$hr_dept_id;
  1628. $rs = Database::query($sql);
  1629. while($user = Database :: fetch_array($rs))
  1630. {
  1631. $a_students[$user['user_id']] = $user['user_id'];
  1632. }
  1633. return $a_students;
  1634. }
  1635. /**
  1636. * Gets the average of test and scorm inside a learning path
  1637. * @param int User id
  1638. * @param string Course id
  1639. * @return float average of test
  1640. * @author isaac flores paz
  1641. * @deprecated get_avg_student_score should be use
  1642. */
  1643. public static function get_average_test_scorm_and_lp ($user_id, $course_id) {
  1644. //the score inside the Reporting table
  1645. $course_info = api_get_course_info($course_id);
  1646. $course_id = $course_info['real_id'];
  1647. $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
  1648. $lp_view_table = Database :: get_course_table(TABLE_LP_VIEW);
  1649. $lp_item_view_table = Database :: get_course_table(TABLE_LP_ITEM_VIEW);
  1650. $lp_item_table = Database :: get_course_table(TABLE_LP_ITEM);
  1651. $sql_type = "SELECT id, lp_type FROM $lp_table WHERE c_id = $course_id";
  1652. $rs_type=Database::query($sql_type);
  1653. $average_data=0;
  1654. $count_loop=0;
  1655. $lp_list = array();
  1656. while ($row_type = Database::fetch_array($rs_type)) {
  1657. $lp_list[] = $row_type['id'];
  1658. if ($row_type['lp_type']==1) {
  1659. //lp chamilo
  1660. $sql = "SELECT id FROM $lp_view_table WHERE c_id = $course_id AND user_id = '".intval($user_id)."' and lp_id='".$row_type['id']."'";
  1661. $rs_last_lp_view_id = Database::query($sql);
  1662. $lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
  1663. $sql_list_view="SELECT li.max_score,lv.user_id,liw.score,(liw.score/li.max_score) as sum_data
  1664. FROM $lp_item_table li INNER JOIN $lp_view_table lv
  1665. ON li.lp_id=lv.lp_id INNER JOIN $lp_item_view_table liw ON liw.lp_item_id=li.id
  1666. WHERE li.c_id = $course_id AND
  1667. liw.c_id = $course_id AND
  1668. lv.c_id = $course_id AND
  1669. lv.user_id= $user_id AND li.item_type = 'quiz' AND liw.lp_view_id= $lp_view_id";
  1670. $sum=0;
  1671. $tot=0;
  1672. $rs_list_view1=Database::query($sql_list_view);
  1673. while ($row_list_view=Database::fetch_array($rs_list_view1)) {
  1674. $sum=$sum+$row_list_view['sum_data'];
  1675. $tot++;
  1676. }
  1677. if ($tot==0) {
  1678. $tot=1;
  1679. }
  1680. $average_data1=$sum/$tot;
  1681. $sql_list_view='';
  1682. $rs_last_lp_view_id='';
  1683. } elseif ($row_type['lp_type']==2) {
  1684. //lp scorm
  1685. $sql = "SELECT id FROM $lp_view_table WHERE c_id = $course_id AND user_id = '".intval($user_id)."' and lp_id='".$row_type['id']."'";
  1686. $rs_last_lp_view_id = Database::query($sql);
  1687. $lp_view_id = intval(Database::result($rs_last_lp_view_id,0,'id'));
  1688. $sql_list_view = "SELECT li.max_score,lv.user_id,liw.score,((liw.score/li.max_score)*100) as sum_data
  1689. FROM $lp_item_table li INNER JOIN $lp_view_table lv
  1690. ON li.lp_id=lv.lp_id INNER JOIN $lp_item_view_table liw ON liw.lp_item_id=li.id
  1691. WHERE li.c_id = $course_id AND
  1692. liw.c_id = $course_id AND
  1693. lv.c_id = $course_id AND
  1694. lv.user_id= $user_id AND (li.item_type = 'sco' OR li.item_type='quiz') AND liw.lp_view_id = $lp_view_id";
  1695. $tot=0;
  1696. $sum=0;
  1697. $rs_list_view2=Database::query($sql_list_view);
  1698. while ($row_list_view=Database::fetch_array($rs_list_view2)) {
  1699. $sum=$sum+$row_list_view['sum_data'];
  1700. $tot++;
  1701. }
  1702. if ($tot==0) {
  1703. $tot=1;
  1704. }
  1705. $average_data2=$sum/$tot;
  1706. }
  1707. $average_data_sum=$average_data_sum+$average_data1+$average_data2;
  1708. $average_data2=0;
  1709. $average_data1=0;
  1710. $count_loop++;
  1711. }
  1712. //We only count the LP that have an exercise to get the average
  1713. $lp_with_quiz = 0;
  1714. foreach($lp_list as $lp_id) {
  1715. //check if LP have a score
  1716. $sql = "SELECT count(id) as count FROM $lp_item_table
  1717. WHERE c_id = $course_id AND item_type = 'quiz' AND lp_id = ".$lp_id." ";
  1718. $result_have_quiz = Database::query($sql);
  1719. if (Database::num_rows($result_have_quiz) > 0 ) {
  1720. $row = Database::fetch_array($result_have_quiz,'ASSOC');
  1721. if (is_numeric($row['count']) && $row['count'] != 0) {
  1722. $lp_with_quiz++;
  1723. }
  1724. }
  1725. }
  1726. if ($lp_with_quiz > 0) {
  1727. $avg_student_score = round(($average_data_sum / $lp_with_quiz * 100), 2);
  1728. }
  1729. return $avg_student_score;
  1730. }
  1731. /**
  1732. * get count clicks about tools most used by course
  1733. * @param string Course code
  1734. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1735. * @return array tools data
  1736. */
  1737. public static function get_tools_most_used_by_course($course_code, $session_id = null) {
  1738. //protect data
  1739. $course_code = Database::escape_string($course_code);
  1740. $data = array();
  1741. $TABLETRACK_ACCESS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1742. $condition_session = '';
  1743. if (isset($session_id)) {
  1744. $session_id = intval($session_id);
  1745. $condition_session = ' AND access_session_id = '. $session_id;
  1746. }
  1747. $sql = "SELECT access_tool, COUNT(DISTINCT access_user_id),count( access_tool ) as count_access_tool
  1748. FROM $TABLETRACK_ACCESS
  1749. WHERE access_tool IS NOT NULL AND access_tool != '' AND access_cours_code = '$course_code' $condition_session
  1750. GROUP BY access_tool
  1751. ORDER BY count_access_tool DESC
  1752. LIMIT 0, 3";
  1753. $rs = Database::query($sql);
  1754. if (Database::num_rows($rs) > 0) {
  1755. while ($row = Database::fetch_array($rs)) {
  1756. $data[] = $row;
  1757. }
  1758. }
  1759. return $data;
  1760. }
  1761. /**
  1762. * get documents most downloaded by course
  1763. * @param string Course code
  1764. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1765. * @param int Limit (optional, default = 0, 0 = without limit)
  1766. * @return array documents downloaded
  1767. */
  1768. public static function get_documents_most_downloaded_by_course($course_code, $session_id = null, $limit = 0) {
  1769. //protect data
  1770. $course_code = Database::escape_string($course_code);
  1771. $data = array();
  1772. $TABLETRACK_DOWNLOADS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  1773. $condition_session = '';
  1774. if (isset($session_id)) {
  1775. $session_id = intval($session_id);
  1776. $condition_session = ' AND down_session_id = '. $session_id;
  1777. }
  1778. $sql = "SELECT down_doc_path, COUNT(DISTINCT down_user_id), COUNT(down_doc_path) as count_down
  1779. FROM $TABLETRACK_DOWNLOADS
  1780. WHERE down_cours_id = '$course_code'
  1781. $condition_session
  1782. GROUP BY down_doc_path
  1783. ORDER BY count_down DESC
  1784. LIMIT 0, $limit";
  1785. $rs = Database::query($sql);
  1786. if (Database::num_rows($rs) > 0) {
  1787. while ($row = Database::fetch_array($rs)) {
  1788. $data[] = $row;
  1789. }
  1790. }
  1791. return $data;
  1792. }
  1793. /**
  1794. * get links most visited by course
  1795. * @param string Course code
  1796. * @param int Session id (optional), if param $session_id is null(default) it'll return results including sessions, 0 = session is not filtered
  1797. * @return array links most visited
  1798. */
  1799. public static function get_links_most_visited_by_course($course_code, $session_id = null) {
  1800. //protect data
  1801. $course_code = Database::escape_string($course_code);
  1802. $course_info = api_get_course_info($course_code);
  1803. $course_id = $course_info['real_id'];
  1804. $data = array();
  1805. $TABLETRACK_LINKS = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LINKS);
  1806. $TABLECOURSE_LINKS = Database::get_course_table(TABLE_LINK);
  1807. $condition_session = '';
  1808. if (isset($session_id)) {
  1809. $session_id = intval($session_id);
  1810. $condition_session = ' AND cl.session_id = '.$session_id;
  1811. }
  1812. $sql = "SELECT cl.title, cl.url,count(DISTINCT sl.links_user_id), count(cl.title) as count_visits
  1813. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  1814. WHERE cl.c_id = $course_id AND
  1815. sl.links_link_id = cl.id
  1816. AND sl.links_cours_id = '$course_code'
  1817. $condition_session
  1818. GROUP BY cl.title, cl.url
  1819. ORDER BY count_visits DESC
  1820. LIMIT 0, 3";
  1821. $rs = Database::query($sql);
  1822. if (Database::num_rows($rs) > 0) {
  1823. while ($row = Database::fetch_array($rs)) {
  1824. $data[] = $row;
  1825. }
  1826. }
  1827. return $data;
  1828. }
  1829. /**
  1830. * Shows the user progress (when clicking in the Progress tab)
  1831. * @param int user id
  1832. * @return string html code
  1833. */
  1834. static function show_user_progress($user_id, $session_id = 0, $extra_params = '', $show_courses = true) {
  1835. global $_configuration;
  1836. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  1837. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1838. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1839. $tbl_access_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1840. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1841. $tbl_access_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1842. $user_id = intval($user_id);
  1843. // get course list
  1844. if ($_configuration['multiple_access_urls']) {
  1845. $sql = 'SELECT cu.course_code as code, title
  1846. FROM '.$tbl_course_user.' cu INNER JOIN '.$tbl_access_rel_course.' a ON(a.course_code = cu.course_code) INNER JOIN '.$tbl_course.' c ON( cu.course_code = c.code)
  1847. WHERE user_id='.$user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND access_url_id = '.api_get_current_access_url_id().' ORDER BY title ';
  1848. } else {
  1849. $sql = 'SELECT course_code as code, title FROM '.$tbl_course_user.' u INNER JOIN '.$tbl_course.' c ON(course_code = c.code)
  1850. WHERE u.user_id='.$user_id.' AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' ORDER BY title ';
  1851. }
  1852. $rs = Database::query($sql);
  1853. $courses = $course_in_session = $temp_course_in_session = array();
  1854. while($row = Database :: fetch_array($rs, 'ASSOC')) {
  1855. $courses[$row['code']] = $row['title'];
  1856. }
  1857. // Get the list of sessions where the user is subscribed as student
  1858. if ($_configuration['multiple_access_urls']) {
  1859. $sql = 'SELECT DISTINCT cu.course_code, id_session as session_id, name FROM '.$tbl_session_course_user.' cu INNER JOIN '.$tbl_access_rel_session.' a ON(a.session_id = cu.id_session) INNER JOIN '.$tbl_session.' s ON(s.id = a.session_id)
  1860. WHERE id_user='.$user_id.' AND access_url_id = '.api_get_current_access_url_id().' ORDER BY name ';
  1861. } else {
  1862. $sql = 'SELECT DISTINCT course_code, id_session as session_id, name FROM '.$tbl_session_course_user.' u INNER JOIN '.$tbl_session.' s ON(s.id = u.id_session)
  1863. WHERE id_user='.$user_id.' ORDER BY name ';
  1864. }
  1865. $rs = Database::query($sql);
  1866. $simple_session_array = array();
  1867. while ($row = Database :: fetch_array($rs)) {
  1868. $course_info = CourseManager::get_course_information($row['course_code']);
  1869. $temp_course_in_session[$row['session_id']]['course_list'][$course_info['id']] = $course_info;
  1870. $temp_course_in_session[$row['session_id']]['name'] = $row['name'];
  1871. $simple_session_array[$row['session_id']] = $row['name'];
  1872. }
  1873. foreach($simple_session_array as $my_session_id => $session_name) {
  1874. $course_list = $temp_course_in_session[$my_session_id]['course_list'];
  1875. $my_course_data = array();
  1876. foreach ($course_list as $course_data) {
  1877. $my_course_data[$course_data['id']] = $course_data['title'];
  1878. }
  1879. $my_course_data = utf8_sort($my_course_data);
  1880. $final_course_data = array();
  1881. foreach($my_course_data as $course_id => $value) {
  1882. $final_course_data[$course_id] = $course_list[$course_id];
  1883. }
  1884. $course_in_session[$my_session_id]['course_list'] = $final_course_data;
  1885. $course_in_session[$my_session_id]['name'] = $session_name;
  1886. }
  1887. $html = '';
  1888. // Course list
  1889. if ($show_courses) {
  1890. if (!empty($courses)) {
  1891. $html .= Display::page_subheader(Display::return_icon('course.png', get_lang('MyCourses'), array(), ICON_SIZE_SMALL).' '.get_lang('MyCourses'));
  1892. $html .= '<table class="data_table" width="100%">';
  1893. $html .= '<tr>
  1894. '.Display::tag('th', get_lang('Course'), array('width'=>'300px')).'
  1895. '.Display::tag('th', get_lang('TimeSpentInTheCourse'), array('class'=>'head')).'
  1896. '.Display::tag('th', get_lang('Progress'), array('class'=>'head')).'
  1897. '.Display::tag('th', get_lang('Score').Display::return_icon('info3.gif', get_lang('ScormAndLPTestTotalAverage'), array('align' => 'absmiddle', 'hspace' => '3px')),array('class'=>'head')).'
  1898. '.Display::tag('th', get_lang('LastConnexion'), array('class'=>'head')).'
  1899. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  1900. </tr>';
  1901. foreach ($courses as $course_code => $course_title) {
  1902. $weighting = 0;
  1903. $total_time_login = Tracking :: get_time_spent_on_the_course($user_id, $course_code);
  1904. $time = api_time_to_hms($total_time_login);
  1905. $progress = Tracking :: get_avg_student_progress($user_id, $course_code);
  1906. $percentage_score = Tracking :: get_avg_student_score($user_id, $course_code, array());
  1907. $last_connection = Tracking :: get_last_connection_date_on_the_course($user_id, $course_code);
  1908. if (is_null($progress)) {
  1909. $progress = '0%';
  1910. } else {
  1911. $progress = $progress.'%';
  1912. }
  1913. if ($course_code == $_GET['course'] && empty($_GET['session_id'])) {
  1914. $html .= '<tr class="row_odd" style="background-color:#FBF09D">';
  1915. } else {
  1916. $html .= '<tr class="row_even">';
  1917. }
  1918. $url = api_get_course_url($course_code, $session_id);
  1919. $course_url = Display::url($course_title, $url, array('target'=>SESSION_LINK_TARGET));
  1920. $html .= '<td>'.$course_url.'</td>';
  1921. $html .= '<td align="center">'.$time.'</td>';
  1922. $html .= '<td align="center">'.$progress.'</td>';
  1923. $html .= '<td align="center">';
  1924. if (is_numeric($percentage_score)) {
  1925. $html .= $percentage_score.'%';
  1926. } else {
  1927. $html .= '0%';
  1928. }
  1929. $html .= '</td>';
  1930. $html .= '<td align="center">'.$last_connection.'</td>';
  1931. $html .= '<td align="center">';
  1932. if ($course_code == $_GET['course'] && empty($_GET['session_id'])) {
  1933. $html .= '<a href="#">';
  1934. $html .= Display::return_icon('2rightarrow_na.gif', get_lang('Details'));
  1935. } else {
  1936. $html .= '<a href="'.api_get_self().'?course='.$course_code.$extra_params.'">';
  1937. $html .= Display::return_icon('2rightarrow.gif', get_lang('Details'));
  1938. }
  1939. $html .= '</a>';
  1940. $html .= '</td></tr>';
  1941. }
  1942. $html .= '</table>';
  1943. }
  1944. }
  1945. // Session list
  1946. if (!empty($course_in_session)) {
  1947. $main_session_graph = '';
  1948. if (!isset($_GET['session_id']) && !isset($_GET['course'])) {
  1949. //Load graphics only when calling to an specific session
  1950. $session_graph = array();
  1951. $all_exercise_graph_name_list = array();
  1952. $my_results = array();
  1953. $all_exercise_graph_list = array();
  1954. $all_exercise_start_time = array();
  1955. foreach ($course_in_session as $my_session_id => $session_data) {
  1956. $course_list = $session_data['course_list'];
  1957. $session_name = $session_data['name'];
  1958. $user_count = count(SessionManager::get_users_by_session($my_session_id));
  1959. $exercise_graph_name_list = array();
  1960. //$user_results = array();
  1961. $exercise_graph_list = array();
  1962. foreach ($course_list as $course_data) {
  1963. $exercise_list = get_all_exercises($course_data, $my_session_id);
  1964. foreach ($exercise_list as $exercise_data) {
  1965. $exercise_obj = new Exercise($course_data['id']);
  1966. $exercise_obj->read($exercise_data['id']);
  1967. //Exercise is not necessary to be visible to show results check the result_disable configuration instead
  1968. //$visible_return = $exercise_obj->is_visible();
  1969. if ($exercise_data['results_disabled'] == 0 || $exercise_data['results_disabled'] == 2) {
  1970. //if ($visible_return['value'] == true) {
  1971. $best_average = intval(get_best_average_score_by_exercise($exercise_data['id'], $course_data['code'], $my_session_id, $user_count));
  1972. $exercise_graph_list[] = $best_average;
  1973. $all_exercise_graph_list[] = $best_average;
  1974. $user_result_data = get_best_attempt_by_user(api_get_user_id(), $exercise_data['id'], $course_data['code'], $my_session_id);
  1975. $score = 0;
  1976. if (!empty($user_result_data['exe_weighting']) && intval($user_result_data['exe_weighting']) != 0) {
  1977. $score = intval($user_result_data['exe_result']/$user_result_data['exe_weighting'] * 100);
  1978. }
  1979. //$user_results[] = $score;
  1980. $time = api_strtotime($exercise_data['start_time']) ? api_strtotime($exercise_data['start_time'], 'UTC') : 0;
  1981. $all_exercise_start_time[] = $time;
  1982. $my_results[] = $score;
  1983. if (count($exercise_list)<=10) {
  1984. $title = cut($course_data['title'], 30)." \n ".cut($exercise_data['title'], 30);
  1985. $exercise_graph_name_list[]= $title;
  1986. $all_exercise_graph_name_list[] = $title;
  1987. } else {
  1988. // if there are more than 10 results, space becomes difficult to find, so only show the title of the exercise, not the tool
  1989. $title = cut($exercise_data['title'], 30);
  1990. $exercise_graph_name_list[]= $title;
  1991. $all_exercise_graph_name_list[]= $title;
  1992. }
  1993. }
  1994. }
  1995. }
  1996. }
  1997. //Complete graph
  1998. if (!empty($my_results) && !empty($all_exercise_graph_list)) {
  1999. asort($all_exercise_start_time);
  2000. //Fix exams order
  2001. $final_all_exercise_graph_name_list = array();
  2002. $my_results_final = array();
  2003. $final_all_exercise_graph_list = array();
  2004. foreach ($all_exercise_start_time as $key => $time) {
  2005. $label_time = '';
  2006. if (!empty($time)) {
  2007. $label_time = date('d-m-y', $time);
  2008. //$label_time = api_format_date($time, DATE_FORMAT_NUMBER);
  2009. }
  2010. $final_all_exercise_graph_name_list[] = $all_exercise_graph_name_list[$key].' '.$label_time;
  2011. $my_results_final[] = $my_results[$key];
  2012. $final_all_exercise_graph_list[] = $all_exercise_graph_list[$key];
  2013. }
  2014. //var_dump($final_all_exercise_graph_name_list, $final_all_user_results, $final_all_exercise_graph_list);
  2015. $main_session_graph = self::generate_session_exercise_graph($final_all_exercise_graph_name_list, $my_results_final, $final_all_exercise_graph_list);
  2016. }
  2017. }
  2018. $html .= Display::page_subheader(Display::return_icon('session.png', get_lang('Sessions'), array(), ICON_SIZE_SMALL).' '.get_lang('Sessions'));
  2019. $html .= '<table class="data_table" width="100%">';
  2020. //'.Display::tag('th', get_lang('DoneExercises'), array('class'=>'head')).'
  2021. $html .= '<tr>
  2022. '.Display::tag('th', get_lang('Session'), array('width'=>'300px')).'
  2023. '.Display::tag('th', get_lang('PublishedExercises'), array('width'=>'300px')).'
  2024. '.Display::tag('th', get_lang('NewExercises'), array('class'=>'head')).'
  2025. '.Display::tag('th', get_lang('AverageExerciseResult'), array('class'=>'head')).'
  2026. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  2027. </tr>';
  2028. foreach ($course_in_session as $my_session_id => $session_data) {
  2029. $course_list = $session_data['course_list'];
  2030. $session_name = $session_data['name'];
  2031. if (isset($session_id) && !empty($session_id)) {
  2032. if ($session_id != $my_session_id) {
  2033. continue;
  2034. }
  2035. }
  2036. $all_exercises = 0;
  2037. $all_unanswered_exercises_by_user = 0;
  2038. $all_average = 0;
  2039. $stats_array = array();
  2040. foreach($course_list as $course_data) {
  2041. //All exercises in the course @todo change for a real count
  2042. $exercises = get_all_exercises($course_data, $my_session_id);
  2043. $count_exercises = 0;
  2044. if (is_array($exercises) && !empty($exercises)) {
  2045. $count_exercises = count($exercises);
  2046. }
  2047. //Count of user results
  2048. //$done_exercises = get_count_exercises_attempted_by_course($course_data['code'], $my_session_id);
  2049. $done_exercises = null;
  2050. $answered_exercises = 0;
  2051. if (!empty($exercises)) {
  2052. foreach($exercises as $exercise_item) {
  2053. $attempts = count_exercise_attempts_by_user(api_get_user_id(), $exercise_item['id'], $course_data['code'], $my_session_id);
  2054. if ($attempts > 1) {
  2055. $answered_exercises++;
  2056. }
  2057. }
  2058. }
  2059. //Average
  2060. $average = get_average_score_by_course($course_data['code'], $my_session_id);
  2061. $all_exercises += $count_exercises;
  2062. $all_unanswered_exercises_by_user += $count_exercises - $answered_exercises;
  2063. //$all_done_exercise += $done_exercises;
  2064. $all_average += $average;
  2065. //$stats_array[$course_data['code']] = array('exercises'=>$count_exercises, 'unanswered_exercises_by_user'=>$answered_exercises,'done_exercises'=>$done_exercises, 'average'=>$average);
  2066. }
  2067. $all_average = $all_average / count($course_list);
  2068. if (isset($_GET['session_id']) && $my_session_id == $_GET['session_id']) {
  2069. $html .= '<tr style="background-color:#FBF09D">';
  2070. } else {
  2071. $html .= '<tr>';
  2072. }
  2073. $url = api_get_path(WEB_CODE_PATH)."session/?session_id={$my_session_id}";
  2074. $html .= Display::tag('td', Display::url($session_name, $url, array('target'=>SESSION_LINK_TARGET)));
  2075. $html .= Display::tag('td', $all_exercises);
  2076. $html .= Display::tag('td', $all_unanswered_exercises_by_user);
  2077. //$html .= Display::tag('td', $all_done_exercise);
  2078. $html .= Display::tag('td', convert_to_percentage($all_average));
  2079. if (isset($_GET['session_id']) && $my_session_id == $_GET['session_id']) {
  2080. $icon = Display::url(Display::return_icon('2rightarrow_na.gif', get_lang('Details')), '?session_id='.$my_session_id);
  2081. } else {
  2082. $icon = Display::url(Display::return_icon('2rightarrow.gif', get_lang('Details')), '?session_id='.$my_session_id);
  2083. }
  2084. $html .= Display::tag('td', $icon);
  2085. $html .= '</tr>';
  2086. }
  2087. $html .= '</table><br />';
  2088. $html .= Display::div($main_session_graph, array('id'=>'session_graph','class'=>'chart-session', 'style'=>'position:relative; text-align: center;') );
  2089. //Checking selected session
  2090. if (isset($_GET['session_id'])) {
  2091. $session_id_from_get = intval($_GET['session_id']);
  2092. $session_data = $course_in_session[$session_id_from_get];
  2093. $course_list = $session_data['course_list'];
  2094. $html .= Display::tag('h3',$session_data['name'].' - '.get_lang('CourseList'));
  2095. $html .= '<table class="data_table" width="100%">';
  2096. //'.Display::tag('th', get_lang('DoneExercises'), array('class'=>'head')).'
  2097. $html .= '
  2098. <tr>
  2099. <th width="300px">'.get_lang('Course').'</th>
  2100. '.Display::tag('th', get_lang('PublishedExercises'), array('class'=>'head')).'
  2101. '.Display::tag('th', get_lang('NewExercises'), array('class'=>'head')).'
  2102. '.Display::tag('th', get_lang('MyAverage'), array('class'=>'head')).'
  2103. '.Display::tag('th', get_lang('AverageExerciseResult'), array('class'=>'head')).'
  2104. '.Display::tag('th', get_lang('TimeSpentInTheCourse'), array('class'=>'head')).'
  2105. '.Display::tag('th', get_lang('LPProgress') , array('class'=>'head')).'
  2106. '.Display::tag('th', get_lang('Score').Display::return_icon('info3.gif', get_lang('ScormAndLPTestTotalAverage'), array ('align' => 'absmiddle', 'hspace' => '3px')), array('class'=>'head')).'
  2107. '.Display::tag('th', get_lang('LastConnexion'), array('class'=>'head')).'
  2108. '.Display::tag('th', get_lang('Details'), array('class'=>'head')).'
  2109. </tr>';
  2110. foreach ($course_list as $course_data) {
  2111. $course_code = $course_data['code'];
  2112. $course_title = $course_data['title'];
  2113. //All exercises in the course @todo change for a real count
  2114. $exercises = get_all_exercises($course_data, $session_id_from_get);
  2115. $count_exercises = 0;
  2116. if (!empty($exercises)) {
  2117. $count_exercises = count($exercises);
  2118. }
  2119. //Count of user results
  2120. //$done_exercises = get_best_exercise_results_by_course($course_code, $session_id_from_get);
  2121. //From course exercises NOT from LP exercises!!!
  2122. //$done_exercises = get_count_exercises_attempted_by_course($course_code, $session_id_from_get);
  2123. $answered_exercises = 0;
  2124. foreach($exercises as $exercise_item) {
  2125. $attempts = count_exercise_attempts_by_user(api_get_user_id(), $exercise_item['id'], $course_code, $session_id_from_get);
  2126. if ($attempts > 1) {
  2127. $answered_exercises++;
  2128. }
  2129. }
  2130. $unanswered_exercises = $count_exercises - $answered_exercises;
  2131. //Average
  2132. $average = get_average_score_by_course($course_code, $session_id_from_get);
  2133. $my_average = get_average_score_by_course_by_user(api_get_user_id(), $course_code, $session_id_from_get);
  2134. $stats_array[$course_code] = array( 'exercises' => $count_exercises,
  2135. 'unanswered_exercises_by_user' => $unanswered_exercises,
  2136. 'done_exercises' => $done_exercises,
  2137. 'average' => $average,
  2138. 'my_average' => $my_average);
  2139. $weighting = 0;
  2140. $last_connection = Tracking :: get_last_connection_date_on_the_course($user_id, $course_code, $session_id_from_get);
  2141. $progress = Tracking :: get_avg_student_progress($user_id, $course_code,array(), $session_id_from_get);
  2142. $total_time_login = Tracking :: get_time_spent_on_the_course($user_id, $course_code, $session_id_from_get);
  2143. $time = api_time_to_hms($total_time_login);
  2144. $percentage_score = Tracking :: get_avg_student_score($user_id, $course_code, array(), $session_id_from_get);
  2145. if ($course_code == $_GET['course'] && $_GET['session_id'] == $session_id_from_get) {
  2146. $html .= '<tr class="row_odd" style="background-color:#FBF09D" >';
  2147. } else {
  2148. $html .= '<tr class="row_even">';
  2149. }
  2150. $url = api_get_course_url($course_code, $session_id_from_get);
  2151. $course_url = Display::url($course_title, $url, array('target'=>SESSION_LINK_TARGET));
  2152. $html .= Display::tag('td', $course_url);
  2153. $html .= Display::tag('td', $stats_array[$course_code]['exercises']);
  2154. $html .= Display::tag('td', $stats_array[$course_code]['unanswered_exercises_by_user']);
  2155. //$html .= Display::tag('td', $stats_array[$course_code]['done_exercises']);
  2156. $html .= Display::tag('td', convert_to_percentage($stats_array[$course_code]['my_average']));
  2157. $html .= Display::tag('td', $stats_array[$course_code]['average'] == 0 ? '-' : '('.convert_to_percentage($stats_array[$course_code]['average']).')');
  2158. $html .= Display::tag('td', $time, array('align'=>'center'));
  2159. if (is_numeric($progress)) {
  2160. $progress = $progress.'%';
  2161. } else {
  2162. $progress = '0%';
  2163. }
  2164. //Progress
  2165. $html .= Display::tag('td', $progress, array('align'=>'center'));
  2166. if (is_numeric($percentage_score)) {
  2167. $percentage_score = $percentage_score.'%';
  2168. } else {
  2169. $percentage_score = '0%';
  2170. }
  2171. //Score
  2172. $html .= Display::tag('td', $percentage_score, array('align'=>'center'));
  2173. $html .= Display::tag('td', $last_connection, array('align'=>'center'));
  2174. if ($course_code == $_GET['course'] && $_GET['session_id'] == $session_id_from_get) {
  2175. $details = '<a href="#">';
  2176. $details .=Display::return_icon('2rightarrow_na.gif', get_lang('Details'));
  2177. } else {
  2178. $details = '<a href="'.api_get_self().'?course='.$course_code.'&session_id='.$session_id_from_get.$extra_params.'">';
  2179. $details .=Display::return_icon('2rightarrow.gif', get_lang('Details'));
  2180. }
  2181. $details .= '</a>';
  2182. $html .= Display::tag('td', $details, array('align'=>'center'));
  2183. $html .= '</tr>';
  2184. }
  2185. $html .= '</table>';
  2186. if (!empty($session_graph[$session_id_from_get])) {
  2187. //$html .= Display::div($session_graph[$session_id_from_get], array('id'=>'session_graph','class'=>'chart-session', 'style'=>'position:relative; text-align: center;') );
  2188. }
  2189. }
  2190. }
  2191. return $html;
  2192. }
  2193. /**
  2194. * Shows the user detail progress (when clicking in the details link)
  2195. * @param int user id
  2196. * @param string course code
  2197. * @param int session id
  2198. * @return string html code
  2199. */
  2200. static function show_course_detail($user_id, $course_code, $session_id) {
  2201. $html = '';
  2202. if (isset($course_code)) {
  2203. $user_id = intval($user_id);
  2204. $session_id = intval($session_id);
  2205. $course = Database::escape_string($course_code);
  2206. $course_info = CourseManager::get_course_information($course);
  2207. //$course_id = $course_info['real_id'];
  2208. //$session_name = api_get_session_name($session_id);
  2209. $html .= Display::page_subheader($course_info['title']);
  2210. $html .= '<table class="data_table" width="100%">';
  2211. //Course details
  2212. $html .= '<tr>
  2213. <th class="head" style="color:#000">'.get_lang('Exercices').'</th>
  2214. <th class="head" style="color:#000">'.get_lang('Attempts').'</th>
  2215. <th class="head" style="color:#000">'.get_lang('BestAttempt').'</th>
  2216. <th class="head" style="color:#000">'.get_lang('Ranking').'</th>
  2217. <th class="head" style="color:#000">'.get_lang('BestResultInCourse').'</th>
  2218. <th class="head" style="color:#000">'.get_lang('Statistics').' '.Display :: return_icon('info3.gif', get_lang('OnlyBestResultsPerStudent'), array('align' => 'absmiddle', 'hspace' => '3px')).'</th>
  2219. </tr>';
  2220. if (empty($session_id)) {
  2221. $user_list = CourseManager::get_user_list_from_course_code($course, $session_id, null, null, STUDENT);
  2222. } else {
  2223. $user_list = CourseManager::get_user_list_from_course_code($course, $session_id, null, null, 0);
  2224. }
  2225. //$exercise_list = get_all_exercises($course_info, $session_id, true);
  2226. // Show exercise results of invisible exercises? see BT#4091
  2227. $exercise_list = get_all_exercises($course_info, $session_id, false);
  2228. $to_graph_exercise_result = array();
  2229. if (!empty($exercise_list)) {
  2230. $score = $weighting = $exe_id = 0;
  2231. foreach ($exercise_list as $exercices) {
  2232. $exercise_obj = new Exercise($course_info['real_id']);
  2233. $exercise_obj->read($exercices['id']);
  2234. $visible_return = $exercise_obj->is_visible();
  2235. $score = $weighting = $attempts = 0;
  2236. //Getting count of attempts by user
  2237. $attempts = count_exercise_attempts_by_user(api_get_user_id(), $exercices['id'], $course_info['code'], $session_id);
  2238. $html .= '<tr class="row_even">';
  2239. $url = api_get_path(WEB_CODE_PATH)."exercice/overview.php?cidReq={$course_info['code']}&id_session=$session_id&exerciseId={$exercices['id']}";
  2240. if ($visible_return['value'] == true) {
  2241. $exercices['title'] = Display::url($exercices['title'], $url, array('target'=>SESSION_LINK_TARGET));
  2242. }
  2243. $html .= Display::tag('td', $exercices['title']);
  2244. //Exercise configuration show results or show only score
  2245. if ($exercices['results_disabled'] == 0 || $exercices['results_disabled'] == 2) {
  2246. //For graphics
  2247. $best_exercise_stats = get_best_exercise_results_by_user($exercices['id'], $course_info['code'], $session_id);
  2248. $to_graph_exercise_result[$exercices['id']] = array('title'=>$exercices['title'], 'data'=>$best_exercise_stats);
  2249. $latest_attempt_url = '';
  2250. $best_score = $position = $percentage_score_result = '-';
  2251. $graph = $normal_graph = null;
  2252. //Getting best results
  2253. $best_score_data = get_best_attempt_in_course($exercices['id'], $course_info['code'], $session_id);
  2254. $best_score = show_score($best_score_data['exe_result'], $best_score_data['exe_weighting']);
  2255. if ($attempts > 0) {
  2256. $exercise_stat = get_best_attempt_by_user(api_get_user_id(), $exercices['id'], $course_info['code'], $session_id);
  2257. if (!empty($exercise_stat)) {
  2258. //Always getting the BEST attempt
  2259. $score = $exercise_stat['exe_result'];
  2260. $weighting = $exercise_stat['exe_weighting'];
  2261. $exe_id = $exercise_stat['exe_id'];
  2262. $latest_attempt_url .= api_get_path(WEB_CODE_PATH).'exercice/result.php?id='.$exe_id.'&cidReq='.$course_info['code'].'&show_headers=1&id_session='.$session_id;
  2263. $percentage_score_result = Display::url(show_score($score, $weighting), $latest_attempt_url);
  2264. $my_score = 0;
  2265. if (!empty($weighting) && intval($weighting) != 0) {
  2266. $my_score = $score/$weighting;
  2267. }
  2268. $position = get_exercise_result_ranking($my_score, $exe_id, $exercices['id'], $course_info['code'], $session_id, $user_list);
  2269. $graph = self::generate_exercise_result_thumbnail_graph($to_graph_exercise_result[$exercices['id']]);
  2270. $normal_graph = self::generate_exercise_result_graph($to_graph_exercise_result[$exercices['id']]);
  2271. }
  2272. }
  2273. $html .= Display::div($normal_graph, array('id'=>'main_graph_'.$exercices['id'],'class'=>'dialog', 'style'=>'display:none') );
  2274. if (empty($graph)) {
  2275. $graph = '-';
  2276. } else {
  2277. $graph = Display::url($graph, '#', array('id'=>$exercices['id'], 'class'=>'opener'));
  2278. }
  2279. $html .= Display::tag('td', $attempts, array('align'=>'center'));
  2280. $html .= Display::tag('td', $percentage_score_result, array('align'=>'center'));
  2281. $html .= Display::tag('td', $position, array('align'=>'center'));
  2282. $html .= Display::tag('td', $best_score, array('align'=>'center'));
  2283. $html .= Display::tag('td', $graph, array('align'=>'center'));
  2284. //$html .= Display::tag('td', $latest_attempt_url, array('align'=>'center', 'width'=>'25'));
  2285. } else {
  2286. // Exercise configuration NO results
  2287. $html .= Display::tag('td', $attempts, array('align'=>'center'));
  2288. $html .= Display::tag('td', '-', array('align'=>'center'));
  2289. $html .= Display::tag('td', '-', array('align'=>'center'));
  2290. $html .= Display::tag('td', '-', array('align'=>'center'));
  2291. $html .= Display::tag('td', '-', array('align'=>'center'));
  2292. }
  2293. $html .= '</tr>';
  2294. }
  2295. } else {
  2296. $html .= '<tr><td colspan="5" align="center">'.get_lang('NoEx').'</td></tr>';
  2297. }
  2298. $html .= '</table>';
  2299. //LP table results
  2300. $html .='<table class="data_table">';
  2301. $html .= Display::tag('th', get_lang('Learnpaths'), array('class'=>'head', 'style'=>'color:#000'));
  2302. $html .= Display::tag('th', get_lang('LatencyTimeSpent'), array('class'=>'head', 'style'=>'color:#000'));
  2303. $html .= Display::tag('th', get_lang('Progress'), array('class'=>'head', 'style'=>'color:#000'));
  2304. $html .= Display::tag('th', get_lang('Score'), array('class'=>'head', 'style'=>'color:#000'));
  2305. $html .= Display::tag('th', get_lang('LastConnexion'), array('class'=>'head', 'style'=>'color:#000'));
  2306. $html .= '</tr>';
  2307. $list = new LearnpathList(api_get_user_id(), $course_info['code'], $session_id, 'publicated_on ASC', true);
  2308. $lp_list = $list->get_flat_list();
  2309. if (!empty($lp_list) > 0) {
  2310. foreach($lp_list as $lp_id => $learnpath) {
  2311. $progress = Tracking::get_avg_student_progress($user_id, $course, array($lp_id), $session_id);
  2312. $last_connection_in_lp = Tracking::get_last_connection_time_in_lp($user_id, $course, $lp_id, $session_id);
  2313. $time_spent_in_lp = Tracking::get_time_spent_in_lp($user_id, $course, array($lp_id), $session_id);
  2314. $percentage_score = Tracking::get_avg_student_score($user_id, $course, array($lp_id), $session_id);
  2315. if (is_numeric($percentage_score)) {
  2316. $percentage_score = $percentage_score.'%';
  2317. } else {
  2318. $percentage_score = '0%';
  2319. }
  2320. $time_spent_in_lp = api_time_to_hms($time_spent_in_lp);
  2321. $html .= '<tr class="row_even">';
  2322. $url = api_get_path(WEB_CODE_PATH)."newscorm/lp_controller.php?cidReq={$course_code}&id_session=$session_id&lp_id=$lp_id&action=view";
  2323. $html .= Display::tag('td', Display::url($learnpath['lp_name'], $url, array('target'=>SESSION_LINK_TARGET)));
  2324. $html .= Display::tag('td', $time_spent_in_lp, array('align'=>'center'));
  2325. if (is_numeric($progress)) {
  2326. $progress = $progress.'%';
  2327. }
  2328. $html .= Display::tag('td', $progress, array('align'=>'center'));
  2329. $html .= Display::tag('td', $percentage_score);
  2330. $last_connection = '-';
  2331. if (!empty($last_connection_in_lp)) {
  2332. $last_connection = api_convert_and_format_date($last_connection_in_lp, DATE_TIME_FORMAT_LONG);
  2333. }
  2334. $html .= Display::tag('td', $last_connection, array('align'=>'center','width'=>'180px'));
  2335. $html .= "</tr>";
  2336. }
  2337. } else {
  2338. $html .= '<tr>
  2339. <td colspan="4" align="center">
  2340. '.get_lang('NoLearnpath').'
  2341. </td>
  2342. </tr>';
  2343. }
  2344. $html .='</table>';
  2345. }
  2346. return $html;
  2347. }
  2348. /**
  2349. * Generates an histogram
  2350. *
  2351. * @param array list of exercise names
  2352. * @param array my results 0 to 100
  2353. * @param array average scores 0-100
  2354. */
  2355. static function generate_session_exercise_graph($names, $my_results, $average) {
  2356. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  2357. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  2358. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  2359. $cache = new pCache();
  2360. // Dataset definition
  2361. $data_set = new pData();
  2362. // Dataset definition
  2363. $data_set->AddPoint($average, "Serie1");
  2364. $data_set->AddPoint($my_results, "Serie2");
  2365. $data_set->AddPoint($names, "Serie3");
  2366. $data_set->AddAllSeries();
  2367. $data_set->SetAbsciseLabelSerie('Serie3');
  2368. $data_set->SetSerieName(get_lang('AverageScore'),"Serie1");
  2369. $data_set->SetSerieName(get_lang('MyResults'), "Serie2");
  2370. //$data_set->SetYAxisName(get_lang("Percentage"));
  2371. $data_set->SetYAxisUnit("%");
  2372. // Initialise the graph
  2373. $main_width = 860;
  2374. $main_height = 500;
  2375. $y_label_angle = 50;
  2376. $data_set->RemoveSerie("Serie3");
  2377. $graph = new pChart($main_width, $main_height);
  2378. //See 3.2 BT#2797
  2379. $graph->setFixedScale(0,100);
  2380. $graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2381. $graph->setGraphArea(65,50,$main_width-20, $main_height-140);
  2382. $graph->drawFilledRoundedRectangle(7,7,$main_width-7,$main_height-7,5,240,240,240);
  2383. $graph->drawRoundedRectangle(5,5,$main_width-5,$main_height -5,5,230,230,230);
  2384. $graph->drawGraphArea(255,255,255,TRUE);
  2385. //SCALE_NORMAL, SCALE_START0, SCALE_ADDALLSTART0, SCALE_ADDALL
  2386. $graph->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_NORMAL ,150,150,150,TRUE,$y_label_angle,1, TRUE);
  2387. $graph->drawGrid(4,TRUE,230,230,230,70);
  2388. // Draw the 0 line
  2389. $graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',6);
  2390. $graph->drawTreshold(0,143,55,72,TRUE,TRUE);
  2391. // Draw the cubic curve graph
  2392. $graph->drawLineGraph($data_set->GetData(),$data_set->GetDataDescription());
  2393. $graph->drawPlotGraph($data_set->GetData(),$data_set->GetDataDescription(),1,1,230,255,255);
  2394. // Finish the graph
  2395. $graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',10);
  2396. $graph->drawLegend($main_width - 150,70,$data_set->GetDataDescription(),255,255,255);
  2397. $graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',11);
  2398. $graph->drawTitle(50, 30, get_lang('ExercisesInTimeProgressChart'), 50,50,50,$main_width-110, true);
  2399. // $main_graph = new pChart($main_width,$main_height);
  2400. $graph_id = 'generate_session_exercise_graph'.Security::remove_XSS($_GET['course']).'-'.intval($_GET['session_id']).'-'.api_get_user_id();
  2401. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  2402. //if (0) {
  2403. //if we already created the img
  2404. //echo 'in cache';
  2405. $img_file = $cache->GetHash($graph_id,$data_set->GetData());
  2406. } else {
  2407. $cache->WriteToCache($graph_id, $data_set->GetData(), $graph);
  2408. ob_start();
  2409. $graph->Stroke();
  2410. ob_end_clean();
  2411. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  2412. }
  2413. $html = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  2414. return $html;
  2415. }
  2416. /**
  2417. *
  2418. * Returns a thumbnail of the function generate_exercise_result_graph
  2419. * @param array attempts
  2420. */
  2421. static function generate_exercise_result_thumbnail_graph($attempts) {
  2422. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  2423. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  2424. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  2425. $exercise_title = $attempts['title'];
  2426. $attempts = $attempts['data'];
  2427. $my_exercise_result_array = $exercise_result = array();
  2428. if (empty($attempts)) {
  2429. return null;
  2430. }
  2431. foreach ($attempts as $attempt) {
  2432. if (api_get_user_id() == $attempt['exe_user_id']) {
  2433. if ($attempt['exe_weighting'] != 0 ) {
  2434. $my_exercise_result_array[]= $attempt['exe_result']/$attempt['exe_weighting'];
  2435. }
  2436. } else {
  2437. if ($attempt['exe_weighting'] != 0 ) {
  2438. $exercise_result[]= $attempt['exe_result']/$attempt['exe_weighting'];
  2439. }
  2440. }
  2441. }
  2442. //Getting best result
  2443. rsort($my_exercise_result_array);
  2444. $my_exercise_result = 0;
  2445. if (isset($my_exercise_result_array[0])) {
  2446. $my_exercise_result = $my_exercise_result_array[0] *100;
  2447. }
  2448. //var_dump($exercise_result, $my_exercise_result);
  2449. $max = 100;
  2450. $pieces = 5 ;
  2451. $part = round($max / $pieces);
  2452. $x_axis = array();
  2453. $final_array = array();
  2454. $my_final_array = array();
  2455. for ($i=1; $i <=$pieces; $i++) {
  2456. $sum = 1;
  2457. if ($i == 1) {
  2458. $sum = 0;
  2459. }
  2460. $min = ($i-1)*$part + $sum;
  2461. $max = ($i)*$part;
  2462. $x_axis[]= $min." - ".$max;
  2463. $count = 0;
  2464. foreach($exercise_result as $result) {
  2465. $percentage = $result*100;
  2466. //echo $percentage.' - '.$min.' - '.$max."<br />";
  2467. if ($percentage >= $min && $percentage <= $max) {
  2468. //echo ' is > ';
  2469. $count++;
  2470. }
  2471. }
  2472. //echo '<br />';
  2473. $final_array[]= $count;
  2474. if ($my_exercise_result >= $min && $my_exercise_result <= $max) {
  2475. $my_final_array[] = 1;
  2476. } else {
  2477. $my_final_array[] = 0;
  2478. }
  2479. }
  2480. //var_dump($my_final_array, $final_array); exit;
  2481. //Fix to remove the data of the user with my data
  2482. for($i = 0; $i<=count($my_final_array); $i++) {
  2483. if (!empty($my_final_array[$i])) {
  2484. $my_final_array[$i] = $final_array[$i] + 1; //Add my result
  2485. $final_array[$i] = 0;
  2486. }
  2487. }
  2488. //var_dump($my_final_array, $final_array); echo '<br />';
  2489. //echo '<pre>'; var_dump($my_exercise_result, $exercise_result,$x_axis);
  2490. $cache = new pCache();
  2491. // Dataset definition
  2492. $data_set = new pData();
  2493. $data_set->AddPoint($final_array,"Serie1");
  2494. $data_set->AddPoint($my_final_array,"Serie2");
  2495. //$data_set->AddPoint($x_axis,"Serie3");
  2496. $data_set->AddAllSeries();
  2497. // Initialise the graph
  2498. $main_width = 80;
  2499. $main_height = 35;
  2500. $thumbnail_graph = new pChart($main_width, $main_height);
  2501. $thumbnail_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2502. //$thumbnail_graph->setGraphArea(50,30,680,200);
  2503. $thumbnail_graph->drawFilledRoundedRectangle(2,2,$main_width-2,$main_height-2,2,230,230,230);
  2504. $thumbnail_graph->setGraphArea(5,5,$main_width-5,$main_height-5);
  2505. $thumbnail_graph->drawGraphArea(255,255,255);
  2506. //SCALE_NORMAL, SCALE_START0, SCALE_ADDALLSTART0
  2507. $thumbnail_graph->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALLSTART0, 150,150,150,FALSE,0,1,TRUE);
  2508. $thumbnail_graph->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription(), 100);
  2509. // Finish the graph
  2510. $graph_id = 'thumbnail_exercise_result_graph_'.Security::remove_XSS($_GET['course']).'-'.intval($_GET['session_id']).'-'.api_get_user_id();
  2511. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  2512. //if (0) {
  2513. //if we already created the img
  2514. //echo 'in cache';
  2515. $img_file = $cache->GetHash($graph_id,$data_set->GetData());
  2516. } else {
  2517. $cache->WriteToCache($graph_id, $data_set->GetData(), $thumbnail_graph);
  2518. ob_start();
  2519. $thumbnail_graph->Stroke();
  2520. ob_end_clean();
  2521. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  2522. }
  2523. $html = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  2524. return $html;
  2525. }
  2526. /**
  2527. * Generates a big graph with the number of best results
  2528. * @param array
  2529. */
  2530. static function generate_exercise_result_graph($attempts) {
  2531. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  2532. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  2533. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  2534. $exercise_title = strip_tags($attempts['title']);
  2535. $attempts = $attempts['data'];
  2536. $my_exercise_result_array = $exercise_result = array();
  2537. if (empty($attempts)) {
  2538. return null;
  2539. }
  2540. foreach ($attempts as $attempt) {
  2541. if (api_get_user_id() == $attempt['exe_user_id']) {
  2542. if ($attempt['exe_weighting'] != 0 ) {
  2543. $my_exercise_result_array[]= $attempt['exe_result']/$attempt['exe_weighting'];
  2544. }
  2545. } else {
  2546. if ($attempt['exe_weighting'] != 0 ) {
  2547. $exercise_result[]= $attempt['exe_result']/$attempt['exe_weighting'];
  2548. }
  2549. }
  2550. }
  2551. //Getting best result
  2552. rsort($my_exercise_result_array);
  2553. $my_exercise_result = 0;
  2554. if (isset($my_exercise_result_array[0])) {
  2555. $my_exercise_result = $my_exercise_result_array[0] *100;
  2556. }
  2557. //var_dump($exercise_result, $my_exercise_result);
  2558. $max = 100;
  2559. $pieces = 5 ;
  2560. $part = round($max / $pieces);
  2561. $x_axis = array();
  2562. $final_array = array();
  2563. $my_final_array = array();
  2564. for ($i=1; $i <=$pieces; $i++) {
  2565. $sum = 1;
  2566. if ($i == 1) {
  2567. $sum = 0;
  2568. }
  2569. $min = ($i-1)*$part + $sum;
  2570. $max = ($i)*$part;
  2571. $x_axis[]= $min." - ".$max;
  2572. $count = 0;
  2573. foreach($exercise_result as $result) {
  2574. $percentage = $result*100;
  2575. //echo $percentage.' - '.$min.' - '.$max."<br />";
  2576. if ($percentage >= $min && $percentage <= $max) {
  2577. //echo ' is > ';
  2578. $count++;
  2579. }
  2580. }
  2581. //echo '<br />';
  2582. $final_array[]= $count;
  2583. if ($my_exercise_result >= $min && $my_exercise_result <= $max) {
  2584. $my_final_array[] = 1;
  2585. } else {
  2586. $my_final_array[] = 0;
  2587. }
  2588. }
  2589. //var_dump($my_final_array, $final_array); exit;
  2590. //Fix to remove the data of the user with my data
  2591. for($i = 0; $i<=count($my_final_array); $i++) {
  2592. if (!empty($my_final_array[$i])) {
  2593. $my_final_array[$i] = $final_array[$i] + 1; //Add my result
  2594. $final_array[$i] = 0;
  2595. }
  2596. }
  2597. //var_dump($my_final_array, $final_array); echo '<br />';
  2598. //echo '<pre>'; var_dump($my_exercise_result, $exercise_result,$x_axis);
  2599. $cache = new pCache();
  2600. // Dataset definition
  2601. $data_set = new pData();
  2602. $data_set->AddPoint($final_array,"Serie1");
  2603. $data_set->AddPoint($my_final_array,"Serie2");
  2604. $data_set->AddPoint($x_axis,"Serie3");
  2605. $data_set->AddAllSeries();
  2606. $data_set->SetAbsciseLabelSerie('Serie3');
  2607. $data_set->SetSerieName(get_lang('Score'),"Serie1");
  2608. $data_set->SetSerieName(get_lang('MyResults'),"Serie2");
  2609. $data_set->SetXAxisName(get_lang("Score"));
  2610. // Initialise the graph
  2611. $main_width = 500;
  2612. $main_height = 250;
  2613. $main_graph = new pChart($main_width,$main_height);
  2614. $main_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2615. $main_graph->setGraphArea(50,30, $main_width -20,$main_height -50);
  2616. $main_graph->drawFilledRoundedRectangle(10,10, $main_width- 10,$main_height -10,5,240,240,240);
  2617. $main_graph->drawRoundedRectangle(7,7,$main_width - 7,$main_height - 7,5,230,230,230);
  2618. $main_graph->drawGraphArea(255,255,255,TRUE);
  2619. //SCALE_NORMAL, SCALE_START0, SCALE_ADDALLSTART0
  2620. $main_graph->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALLSTART0, 150,150,150,TRUE,0,1,TRUE);
  2621. $main_graph->drawGrid(4,TRUE,230,230,230,50);
  2622. // Draw the 0 line
  2623. $main_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',6);
  2624. // $main_graph->drawTreshold(0,143,55,72,TRUE,TRUE);
  2625. // Draw the bar graph
  2626. $data_set->RemoveSerie("Serie3");
  2627. //$main_graph->drawBarGraph($data_set->GetData(),$data_set->GetDataDescription(),TRUE);
  2628. //$main_graph->drawStackedBarGraph($data_set->GetData(),$data_set->GetDataDescription(),TRUE);
  2629. $main_graph->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription(), 100);
  2630. // Finish the graph
  2631. $main_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2632. $main_graph->drawLegend($main_width - 120,$main_height -100,$data_set->GetDataDescription(),255,255,255);
  2633. $main_graph->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  2634. $main_graph->drawTitle(180,22,$exercise_title,50,50,50);
  2635. $graph_id = 'exercise_result_graph'.Security::remove_XSS($_GET['course']).'-'.intval($_GET['session_id']).'-'.api_get_user_id();
  2636. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  2637. //if (0) {
  2638. //if we already created the img
  2639. //echo 'in cache';
  2640. $img_file = $cache->GetHash($graph_id,$data_set->GetData());
  2641. } else {
  2642. $cache->WriteToCache($graph_id, $data_set->GetData(), $main_graph);
  2643. ob_start();
  2644. $main_graph->Stroke();
  2645. ob_end_clean();
  2646. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  2647. }
  2648. $html = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  2649. return $html;
  2650. }
  2651. }
  2652. /**
  2653. * @package chamilo.tracking
  2654. */
  2655. class TrackingCourseLog {
  2656. function count_item_resources() {
  2657. global $session_id;
  2658. $course_id = api_get_course_int_id();
  2659. $table_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2660. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  2661. $sql = "SELECT count(tool) AS total_number_of_items FROM $table_item_property track_resource, $table_user user" .
  2662. " WHERE track_resource.c_id = $course_id AND track_resource.insert_user_id = user.user_id AND id_session = $session_id ";
  2663. if (isset($_GET['keyword'])) {
  2664. $keyword = Database::escape_string(trim($_GET['keyword']));
  2665. $sql .= " AND (user.username LIKE '%".$keyword."%' OR lastedit_type LIKE '%".$keyword."%' OR tool LIKE '%".$keyword."%')";
  2666. }
  2667. $sql .= " AND tool IN ('document', 'learnpath', 'quiz', 'glossary', 'link', 'course_description', 'announcement', 'thematic', 'thematic_advance', 'thematic_plan')";
  2668. $res = Database::query($sql);
  2669. $obj = Database::fetch_object($res);
  2670. return $obj->total_number_of_items;
  2671. }
  2672. function get_item_resources_data($from, $number_of_items, $column, $direction) {
  2673. global $dateTimeFormatLong, $session_id;
  2674. $course_id = api_get_course_int_id();
  2675. $table_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2676. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  2677. $table_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  2678. $session_id = intval($session_id);
  2679. $sql = "SELECT
  2680. tool as col0,
  2681. lastedit_type as col1,
  2682. ref as ref,
  2683. user.username as col3,
  2684. insert_date as col5,
  2685. visibility as col6,
  2686. user.user_id as user_id
  2687. FROM $table_item_property track_resource, $table_user user
  2688. WHERE track_resource.c_id = $course_id AND
  2689. track_resource.insert_user_id = user.user_id AND
  2690. id_session = $session_id ";
  2691. if (isset($_GET['keyword'])) {
  2692. $keyword = Database::escape_string(trim($_GET['keyword']));
  2693. $sql .= " AND (user.username LIKE '%".$keyword."%' OR lastedit_type LIKE '%".$keyword."%' OR tool LIKE '%".$keyword."%') ";
  2694. }
  2695. $sql .= " AND tool IN ('document', 'learnpath', 'quiz', 'glossary', 'link', 'course_description', 'announcement', 'thematic', 'thematic_advance', 'thematic_plan')";
  2696. if ($column == 0) {
  2697. $column = '0';
  2698. }
  2699. if ($column != '' && $direction != '') {
  2700. if ($column != 2 && $column != 4) {
  2701. $sql .= " ORDER BY col$column $direction";
  2702. }
  2703. } else {
  2704. $sql .= " ORDER BY col5 DESC ";
  2705. }
  2706. $sql .= " LIMIT $from, $number_of_items ";
  2707. $res = Database::query($sql);
  2708. $resources = array ();
  2709. $thematic_tools = array('thematic', 'thematic_advance', 'thematic_plan');
  2710. while ($row = Database::fetch_array($res)) {
  2711. $ref = $row['ref'];
  2712. $table_name = TrackingCourseLog::get_tool_name_table($row['col0']);
  2713. $table_tool = Database :: get_course_table($table_name['table_name']);
  2714. $id = $table_name['id_tool'];
  2715. $recorset = false;
  2716. if (in_array($row['col0'], array('thematic_plan', 'thematic_advance'))) {
  2717. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  2718. $sql = "SELECT thematic_id FROM $table_tool WHERE c_id = $course_id AND id = $ref";
  2719. $rs_thematic = Database::query($sql);
  2720. if (Database::num_rows($rs_thematic)) {
  2721. $row_thematic = Database::fetch_array($rs_thematic);
  2722. $thematic_id = $row_thematic['thematic_id'];
  2723. $query = "SELECT session.id, session.name, user.username FROM $tbl_thematic t, $table_session session, $table_user user" .
  2724. " WHERE t.c_id = $course_id AND t.session_id = session.id AND session.id_coach = user.user_id AND t.id = $thematic_id";
  2725. $recorset = Database::query($query);
  2726. }
  2727. } else {
  2728. $query = "SELECT session.id, session.name, user.username FROM $table_tool tool, $table_session session, $table_user user
  2729. WHERE tool.c_id = $course_id AND tool.session_id = session.id AND session.id_coach = user.user_id AND tool.$id = $ref";
  2730. $recorset = Database::query($query);
  2731. }
  2732. if (!empty($recorset)) {
  2733. $obj = Database::fetch_object($recorset);
  2734. $name_session = '';
  2735. $coach_name = '';
  2736. if (!empty($obj)) {
  2737. $name_session = $obj->name;
  2738. $coach_name = $obj->username;
  2739. }
  2740. $url_tool = api_get_path(WEB_CODE_PATH).$table_name['link_tool'];
  2741. $row[0] = '';
  2742. if ($row['col6'] != 2) {
  2743. if (in_array($row['col0'], $thematic_tools)) {
  2744. $exp_thematic_tool = explode('_', $row['col0']);
  2745. $thematic_tool_title = '';
  2746. if (is_array($exp_thematic_tool)) {
  2747. foreach ($exp_thematic_tool as $exp) {
  2748. $thematic_tool_title .= api_ucfirst($exp);
  2749. }
  2750. } else {
  2751. $thematic_tool_title = api_ucfirst($row['col0']);
  2752. }
  2753. $row[0] = '<a href="'.$url_tool.'?'.api_get_cidreq().'&action=thematic_details">'.get_lang($thematic_tool_title).'</a>';
  2754. } else {
  2755. $row[0] = '<a href="'.$url_tool.'?'.api_get_cidreq().'">'.get_lang('Tool'.api_ucfirst($row['col0'])).'</a>';
  2756. }
  2757. } else {
  2758. $row[0] = api_ucfirst($row['col0']);
  2759. }
  2760. $row[1] = get_lang($row[1]);
  2761. $row[6] = api_convert_and_format_date($row['col5'], null, date_default_timezone_get());
  2762. $row[5] = '';
  2763. //@todo Improve this code please
  2764. switch ($table_name['table_name']) {
  2765. case 'document' :
  2766. $query_document = "SELECT tool.title as title FROM $table_tool tool WHERE c_id = $course_id AND id = $ref";
  2767. $rs_document = Database::query($query_document);
  2768. $obj_document = Database::fetch_object($rs_document);
  2769. $row[5] = $obj_document->title;
  2770. break;
  2771. case 'announcement':
  2772. $query_document = "SELECT title FROM $table_tool WHERE c_id = $course_id AND id = $ref";
  2773. $rs_document = Database::query($query_document);
  2774. $obj_document = Database::fetch_object($rs_document);
  2775. $row[5] = $obj_document->title;
  2776. break;
  2777. case 'glossary':
  2778. $query_document = "SELECT name FROM $table_tool WHERE c_id = $course_id AND glossary_id = $ref";
  2779. $rs_document = Database::query($query_document);
  2780. $obj_document = Database::fetch_object($rs_document);
  2781. $row[5] = $obj_document->name;
  2782. break;
  2783. case 'lp':
  2784. $query_document = "SELECT name FROM $table_tool WHERE c_id = $course_id AND id = $ref";
  2785. $rs_document = Database::query($query_document);
  2786. $obj_document = Database::fetch_object($rs_document);
  2787. $row[5] = $obj_document->name;
  2788. break;
  2789. case 'quiz':
  2790. $query_document = "SELECT title FROM $table_tool WHERE c_id = $course_id AND id = $ref";
  2791. $rs_document = Database::query($query_document);
  2792. $obj_document = Database::fetch_object($rs_document);
  2793. $row[5] = $obj_document->title;
  2794. break;
  2795. case 'course_description':
  2796. $query_document = "SELECT title FROM $table_tool WHERE c_id = $course_id AND id = $ref";
  2797. $rs_document = Database::query($query_document);
  2798. $obj_document = Database::fetch_object($rs_document);
  2799. $row[5] = $obj_document->title;
  2800. break;
  2801. case 'thematic':
  2802. $rs = Database::query("SELECT title FROM $table_tool WHERE c_id = $course_id AND id = $ref");
  2803. if (Database::num_rows($rs) > 0) {
  2804. $obj = Database::fetch_object($rs);
  2805. $row[5] = $obj->title;
  2806. }
  2807. break;
  2808. case 'thematic_advance':
  2809. $rs = Database::query("SELECT content FROM $table_tool WHERE c_id = $course_id AND id = $ref");
  2810. if (Database::num_rows($rs) > 0) {
  2811. $obj = Database::fetch_object($rs);
  2812. $row[5] = $obj->content;
  2813. }
  2814. break;
  2815. case 'thematic_plan':
  2816. $rs = Database::query("SELECT title FROM $table_tool WHERE c_id = $course_id AND id = $ref");
  2817. if (Database::num_rows($rs) > 0) {
  2818. $obj = Database::fetch_object($rs);
  2819. $row[5] = $obj->title;
  2820. }
  2821. break;
  2822. default:
  2823. break;
  2824. }
  2825. $row2 = $name_session;
  2826. if (!empty($coach_name)) {
  2827. $row2 .= '<br />'.get_lang('Coach').': '.$coach_name;
  2828. }
  2829. $row[2] = $row2;
  2830. if (!empty($row['col3'])) {
  2831. $row['col3'] = Display::url($row['col3'],api_get_path(WEB_CODE_PATH).'user/userInfo.php?'.api_get_cidreq().'&origin=tracking&uInfo='.$row['user_id']);
  2832. $row[3] = $row['col3'];
  2833. $ip = TrackingUserLog::get_ip_from_user_event($row['user_id'], $row['col5'], true);
  2834. if (empty($ip)) {
  2835. $ip = get_lang('Unknown');
  2836. }
  2837. $row[4] = $ip;
  2838. }
  2839. $resources[] = $row;
  2840. }
  2841. }
  2842. return $resources;
  2843. }
  2844. function get_tool_name_table($tool) {
  2845. switch ($tool) {
  2846. case 'document':
  2847. $table_name = TABLE_DOCUMENT;
  2848. $link_tool = 'document/document.php';
  2849. $id_tool = 'id';
  2850. break;
  2851. case 'learnpath':
  2852. $table_name = TABLE_LP_MAIN;
  2853. $link_tool = 'newscorm/lp_controller.php';
  2854. $id_tool = 'id';
  2855. break;
  2856. case 'quiz':
  2857. $table_name = TABLE_QUIZ_TEST;
  2858. $link_tool = 'exercice/exercice.php';
  2859. $id_tool = 'id';
  2860. break;
  2861. case 'glossary':
  2862. $table_name = TABLE_GLOSSARY;
  2863. $link_tool = 'glossary/index.php';
  2864. $id_tool = 'glossary_id';
  2865. break;
  2866. case 'link':
  2867. $table_name = TABLE_LINK;
  2868. $link_tool = 'link/link.php';
  2869. $id_tool = 'id';
  2870. break;
  2871. case 'course_description':
  2872. $table_name = TABLE_COURSE_DESCRIPTION;
  2873. $link_tool = 'course_description/';
  2874. $id_tool = 'id';
  2875. break;
  2876. case 'announcement':
  2877. $table_name = TABLE_ANNOUNCEMENT;
  2878. $link_tool = 'announcements/announcements.php';
  2879. $id_tool = 'id';
  2880. break;
  2881. case 'thematic':
  2882. $table_name = TABLE_THEMATIC;
  2883. $link_tool = 'course_progress/index.php';
  2884. $id_tool = 'id';
  2885. break;
  2886. case 'thematic_advance':
  2887. $table_name = TABLE_THEMATIC_ADVANCE;
  2888. $link_tool = 'course_progress/index.php';
  2889. $id_tool = 'id';
  2890. break;
  2891. case 'thematic_plan':
  2892. $table_name = TABLE_THEMATIC_PLAN;
  2893. $link_tool = 'course_progress/index.php';
  2894. $id_tool = 'id';
  2895. break;
  2896. default:
  2897. $table_name = $tool;
  2898. break;
  2899. }
  2900. return array('table_name' => $table_name,'link_tool' => $link_tool,'id_tool' => $id_tool);
  2901. }
  2902. static function display_additional_profile_fields() {
  2903. // getting all the extra profile fields that are defined by the platform administrator
  2904. $extra_fields = UserManager :: get_extra_fields(0,50,5,'ASC');
  2905. // creating the form
  2906. $return = '<form action="courseLog.php" method="get" name="additional_profile_field_form" id="additional_profile_field_form">';
  2907. // the select field with the additional user profile fields (= this is where we select the field of which we want to see
  2908. // the information the users have entered or selected.
  2909. $return .= '<select name="additional_profile_field">';
  2910. $return .= '<option value="-">'.get_lang('SelectFieldToAdd').'</option>';
  2911. $extra_fields_to_show = 0;
  2912. foreach ($extra_fields as $key=>$field) {
  2913. // show only extra fields that are visible + and can be filtered, added by J.Montoya
  2914. if ($field[6]==1 && $field[8] == 1) {
  2915. if (isset($_GET['additional_profile_field']) && $field[0] == $_GET['additional_profile_field'] ) {
  2916. $selected = 'selected="selected"';
  2917. } else {
  2918. $selected = '';
  2919. }
  2920. $extra_fields_to_show++;
  2921. $return .= '<option value="'.$field[0].'" '.$selected.'>'.$field[3].'</option>';
  2922. }
  2923. }
  2924. $return .= '</select>';
  2925. // the form elements for the $_GET parameters (because the form is passed through GET
  2926. foreach ($_GET as $key=>$value){
  2927. if ($key <> 'additional_profile_field') {
  2928. $return .= '<input type="hidden" name="'.Security::remove_XSS($key).'" value="'.Security::remove_XSS($value).'" />';
  2929. }
  2930. }
  2931. // the submit button
  2932. $return .= '<button class="save" type="submit">'.get_lang('AddAdditionalProfileField').'</button>';
  2933. $return .= '</form>';
  2934. if ($extra_fields_to_show > 0) {
  2935. return $return;
  2936. } else {
  2937. return '';
  2938. }
  2939. }
  2940. /**
  2941. * This function gets all the information of a certrain ($field_id) additional profile field.
  2942. * 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
  2943. *
  2944. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  2945. * @since October 2009
  2946. * @version 1.8.7
  2947. */
  2948. function get_addtional_profile_information_of_field($field_id){
  2949. // Database table definition
  2950. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2951. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2952. $sql = "SELECT user.user_id, field.field_value FROM $table_user user, $table_user_field_values field
  2953. WHERE user.user_id = field.user_id
  2954. AND field.field_id='".intval($field_id)."'";
  2955. $result = Database::query($sql);
  2956. while($row = Database::fetch_array($result)) {
  2957. $return[$row['user_id']][] = $row['field_value'];
  2958. }
  2959. return $return;
  2960. }
  2961. /**
  2962. * 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
  2963. * 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
  2964. *
  2965. * @author Julio Montoya <gugli100@gmail.com>
  2966. * @param int field id
  2967. * @param array list of user ids
  2968. * @return array
  2969. * @since Nov 2009
  2970. * @version 1.8.6.2
  2971. */
  2972. function get_addtional_profile_information_of_field_by_user($field_id, $users) {
  2973. // Database table definition
  2974. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2975. $table_user_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  2976. $result_extra_field = UserManager::get_extra_field_information($field_id);
  2977. if (!empty($users)) {
  2978. if ($result_extra_field['field_type'] == USER_FIELD_TYPE_TAG ) {
  2979. foreach($users as $user_id) {
  2980. $user_result = UserManager::get_user_tags($user_id, $field_id);
  2981. $tag_list = array();
  2982. foreach($user_result as $item) {
  2983. $tag_list[] = $item['tag'];
  2984. }
  2985. $return[$user_id][] = implode(', ',$tag_list);
  2986. }
  2987. } else {
  2988. $new_user_array = array();
  2989. foreach($users as $user_id) {
  2990. $new_user_array[]= "'".$user_id."'";
  2991. }
  2992. $users = implode(',',$new_user_array);
  2993. //selecting only the necessary information NOT ALL the user list
  2994. $sql = "SELECT user.user_id, field.field_value FROM $table_user user INNER JOIN $table_user_field_values field
  2995. ON (user.user_id = field.user_id)
  2996. WHERE field.field_id=".intval($field_id)." AND user.user_id IN ($users)";
  2997. $result = Database::query($sql);
  2998. while($row = Database::fetch_array($result)) {
  2999. // get option value for field type double select by id
  3000. if (!empty($row['field_value'])) {
  3001. if ($result_extra_field['field_type'] == USER_FIELD_TYPE_DOUBLE_SELECT) {
  3002. $id_double_select = explode(';',$row['field_value']);
  3003. if (is_array($id_double_select)) {
  3004. $value1 = $result_extra_field['options'][$id_double_select[0]]['option_value'];
  3005. $value2 = $result_extra_field['options'][$id_double_select[1]]['option_value'];
  3006. $row['field_value'] = ($value1.';'.$value2);
  3007. }
  3008. }
  3009. }
  3010. // get other value from extra field
  3011. $return[$row['user_id']][] = $row['field_value'];
  3012. }
  3013. }
  3014. }
  3015. return $return;
  3016. }
  3017. /**
  3018. * count the number of students in this course (used for SortableTable)
  3019. * Deprecated
  3020. */
  3021. function count_student_in_course() {
  3022. global $nbStudents;
  3023. return $nbStudents;
  3024. }
  3025. function sort_users($a, $b) {
  3026. return strcmp(trim(api_strtolower($a[$_SESSION['tracking_column']])), trim(api_strtolower($b[$_SESSION['tracking_column']])));
  3027. }
  3028. function sort_users_desc($a, $b) {
  3029. return strcmp( trim(api_strtolower($b[$_SESSION['tracking_column']])), trim(api_strtolower($a[$_SESSION['tracking_column']])));
  3030. }
  3031. /**
  3032. * Get number of users for sortable with pagination
  3033. * @return int
  3034. */
  3035. static function get_number_of_users() {
  3036. global $user_ids;
  3037. return count($user_ids);
  3038. }
  3039. /**
  3040. * Get data for users list in sortable with pagination
  3041. * @return array
  3042. */
  3043. static function get_user_data($from, $number_of_items, $column, $direction) {
  3044. global $user_ids, $course_code, $additional_user_profile_info, $export_csv, $is_western_name_order, $csv_content, $session_id, $_configuration;
  3045. $course_code = Database::escape_string($course_code);
  3046. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  3047. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  3048. $access_url_id = api_get_current_access_url_id();
  3049. // get all users data from a course for sortable with limit
  3050. $condition_user = "";
  3051. if (is_array($user_ids)) {
  3052. $user_ids = array_map('intval', $user_ids);
  3053. $condition_user = " WHERE user.user_id IN (".implode(',',$user_ids).") ";
  3054. } else {
  3055. $user_ids = intval($user_ids);
  3056. $condition_user = " WHERE user.user_id = $user_ids ";
  3057. }
  3058. if (!empty($_GET['user_keyword'])) {
  3059. $keyword = trim(Database::escape_string($_GET['user_keyword']));
  3060. $condition_user .= " AND (user.firstname LIKE '%".$keyword."%' OR user.lastname LIKE '%".$keyword."%' OR user.username LIKE '%".$keyword."%' OR user.email LIKE '%".$keyword."%' ) ";
  3061. }
  3062. if ($_configuration['multiple_access_urls']) {
  3063. $url_table = ", ".$tbl_url_rel_user."as url_users";
  3064. $url_condition = " AND user.user_id = url_users.user_id AND access_url_id='$access_url_id'";
  3065. }
  3066. $sql = "SELECT user.user_id as user_id,
  3067. user.official_code as col0,
  3068. user.lastname as col1,
  3069. user.firstname as col2,
  3070. user.username as col3
  3071. FROM $tbl_user as user $url_table
  3072. $condition_user $url_condition";
  3073. if (!in_array($direction, array('ASC','DESC'))) {
  3074. $direction = 'ASC';
  3075. }
  3076. $column = intval($column);
  3077. if ($is_western_name_order) {
  3078. $original_column = $column;
  3079. if ($original_column == 1) {
  3080. $column = 2;
  3081. }
  3082. if ($original_column == 2) {
  3083. $column = 1;
  3084. }
  3085. }
  3086. $from = intval($from);
  3087. $number_of_items = intval($number_of_items);
  3088. $sql .= " ORDER BY col$column $direction ";
  3089. $sql .= " LIMIT $from,$number_of_items";
  3090. $res = Database::query($sql);
  3091. $users = array();
  3092. $t = time();
  3093. $course_info = api_get_course_info($course_code);
  3094. $total_surveys = 0;
  3095. $total_exercises = get_all_exercises($course_info, $session_id);
  3096. if (empty($session_id)) {
  3097. $survey_user_list = array();
  3098. $survey_list = survey_manager::get_surveys($course_code, $session_id);
  3099. $total_surveys = count($survey_list);
  3100. $survey_data = array();
  3101. foreach ($survey_list as $survey) {
  3102. $user_list = survey_manager::get_people_who_filled_survey($survey['survey_id'], false, $course_info['real_id']);
  3103. foreach ($user_list as $user_id) {
  3104. isset($survey_user_list[$user_id]) ? $survey_user_list[$user_id]++ : $survey_user_list[$user_id] = 1;
  3105. }
  3106. }
  3107. }
  3108. while ($user = Database::fetch_array($res, 'ASSOC')) {
  3109. $user['official_code'] = $user['col0'];
  3110. if ($is_western_name_order) {
  3111. $user['lastname'] = $user['col2'];
  3112. $user['firstname'] = $user['col1'];
  3113. } else {
  3114. $user['lastname'] = $user['col1'];
  3115. $user['firstname'] = $user['col2'];
  3116. }
  3117. $user['username'] = $user['col3'];
  3118. $user['time'] = api_time_to_hms(Tracking::get_time_spent_on_the_course($user['user_id'], $course_code, $session_id));
  3119. $avg_student_score = Tracking::get_avg_student_score($user['user_id'], $course_code, array(), $session_id);
  3120. $avg_student_progress = Tracking::get_avg_student_progress($user['user_id'], $course_code, array(), $session_id);
  3121. if (empty($avg_student_progress)) {
  3122. $avg_student_progress=0;
  3123. }
  3124. $user['average_progress'] = $avg_student_progress.'%';
  3125. $total_user_exercise = Tracking::get_exercise_student_progress($total_exercises, $user['user_id'], $course_code, $session_id);
  3126. $user['exercise_progress'] = $total_user_exercise;
  3127. $total_user_exercise = Tracking::get_exercise_student_average_best_attempt($total_exercises, $user['user_id'], $course_code, $session_id);
  3128. $user['exercise_average_best_attempt'] = $total_user_exercise;
  3129. if (is_numeric($avg_student_score)) {
  3130. $user['student_score'] = $avg_student_score.'%';
  3131. } else {
  3132. $user['student_score'] = $avg_student_score;
  3133. }
  3134. $user['count_assignments'] = Tracking::count_student_assignments($user['user_id'], $course_code, $session_id);
  3135. $user['count_messages'] = Tracking::count_student_messages($user['user_id'], $course_code, $session_id);
  3136. $user['first_connection'] = Tracking::get_first_connection_date_on_the_course($user['user_id'], $course_code, $session_id);
  3137. $user['last_connection'] = Tracking::get_last_connection_date_on_the_course($user['user_id'], $course_code, $session_id);
  3138. // we need to display an additional profile field
  3139. $user['additional'] = '';
  3140. if (isset($_GET['additional_profile_field']) AND is_numeric($_GET['additional_profile_field'])) {
  3141. if (isset($additional_user_profile_info[$user['user_id']]) && is_array($additional_user_profile_info[$user['user_id']])) {
  3142. $user['additional'] = implode(', ', $additional_user_profile_info[$user['user_id']]);
  3143. }
  3144. }
  3145. if (empty($session_id)) {
  3146. $user['survey'] = (isset($survey_user_list[$user['user_id']]) ? $survey_user_list[$user['user_id']] : 0) .' / '.$total_surveys;
  3147. }
  3148. $user['link'] = '<center><a href="../mySpace/myStudents.php?student='.$user['user_id'].'&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>';
  3149. // store columns in array $users
  3150. $is_western_name_order = api_is_western_name_order();
  3151. $user_row = array();
  3152. $user_row[]= $user['official_code']; //0
  3153. if ($is_western_name_order) {
  3154. $user_row[]= $user['lastname'];
  3155. $user_row[]= $user['firstname'];
  3156. } else {
  3157. $user_row[]= $user['firstname'];
  3158. $user_row[]= $user['lastname'];
  3159. }
  3160. $user_row[]= $user['username'];
  3161. $user_row[]= $user['time'];
  3162. $user_row[]= $user['average_progress'];
  3163. $user_row[]= $user['exercise_progress'];
  3164. $user_row[]= $user['exercise_average_best_attempt'];
  3165. $user_row[]= $user['student_score'];
  3166. $user_row[]= $user['count_assignments'];
  3167. $user_row[]= $user['count_messages'];
  3168. if (empty($session_id)) {
  3169. $user_row[]= $user['survey'];
  3170. }
  3171. $user_row[]= $user['first_connection'];
  3172. $user_row[]= $user['last_connection'];
  3173. if (isset($_GET['additional_profile_field']) AND is_numeric($_GET['additional_profile_field'])) {
  3174. $user_row[]= $user['additional'];
  3175. }
  3176. $user_row[]= $user['link'];
  3177. $users[] = $user_row;
  3178. if ($export_csv) {
  3179. if (empty($session_id)) {
  3180. $user_row = array_map('strip_tags', $user_row);
  3181. unset($user_row[14]);
  3182. unset($user_row[15]);
  3183. } else {
  3184. $user_row = array_map('strip_tags', $user_row);
  3185. unset($user_row[13]);
  3186. unset($user_row[14]);
  3187. }
  3188. $csv_content[] = $user_row;
  3189. }
  3190. }
  3191. return $users;
  3192. }
  3193. }
  3194. /**
  3195. * @package chamilo.tracking
  3196. */
  3197. class TrackingUserLog {
  3198. /**
  3199. * Displays the number of logins every month for a specific user in a specific course.
  3200. */
  3201. function display_login_tracking_info($view, $user_id, $course_id, $session_id = 0)
  3202. {
  3203. $MonthsLong = $GLOBALS['MonthsLong'];
  3204. // protected data
  3205. $user_id = intval($user_id);
  3206. $session_id = intval($session_id);
  3207. $course_id = Database::escape_string($course_id);
  3208. $track_access_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  3209. $tempView = $view;
  3210. if(substr($view,0,1) == '1') {
  3211. $new_view = substr_replace($view,'0',0,1);
  3212. echo "
  3213. <tr>
  3214. <td valign='top'>
  3215. <font color='#0000FF'>-&nbsp;&nbsp;&nbsp;</font>" .
  3216. "<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>]
  3217. </td>
  3218. </tr>
  3219. ";
  3220. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('LoginsDetails')."<br>";
  3221. $sql = "SELECT UNIX_TIMESTAMP(access_date), count(access_date)
  3222. FROM $track_access_table
  3223. WHERE access_user_id = '$user_id'
  3224. AND access_cours_code = '$course_id'
  3225. AND access_session_id = '$session_id'
  3226. GROUP BY YEAR(access_date),MONTH(access_date)
  3227. ORDER BY YEAR(access_date),MONTH(access_date) ASC";
  3228. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  3229. //$results = getManyResults2Col($sql);
  3230. $results = getManyResults3Col($sql);
  3231. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  3232. echo "<tr>
  3233. <td class='secLine'>
  3234. ".get_lang('LoginsTitleMonthColumn')."
  3235. </td>
  3236. <td class='secLine'>
  3237. ".get_lang('LoginsTitleCountColumn')."
  3238. </td>
  3239. </tr>";
  3240. $total = 0;
  3241. if (is_array($results)) {
  3242. for($j = 0 ; $j < count($results) ; $j++) {
  3243. echo "<tr>";
  3244. 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>";
  3245. echo "<td valign='top' align='right' class='content'>".$results[$j][1]."</td>";
  3246. echo"</tr>";
  3247. $total = $total + $results[$j][1];
  3248. }
  3249. echo "<tr>";
  3250. echo "<td>".get_lang('Total')."</td>";
  3251. echo "<td align='right' class='content'>".$total."</td>";
  3252. echo"</tr>";
  3253. } else {
  3254. echo "<tr>";
  3255. echo "<td colspan='2'><center>".get_lang('NoResult')."</center></td>";
  3256. echo"</tr>";
  3257. }
  3258. echo "</table>";
  3259. echo "</td></tr>";
  3260. } else {
  3261. $new_view = substr_replace($view,'1',0,1);
  3262. echo "
  3263. <tr>
  3264. <td valign='top'>
  3265. +<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>
  3266. </td>
  3267. </tr>
  3268. ";
  3269. }
  3270. }
  3271. /**
  3272. * Displays the exercise results for a specific user in a specific course.
  3273. * @todo remove globals
  3274. */
  3275. function display_exercise_tracking_info($view, $user_id, $course_id)
  3276. {
  3277. global $TABLECOURSE_EXERCICES, $TABLETRACK_EXERCICES, $dateTimeFormatLong;
  3278. if(substr($view,1,1) == '1')
  3279. {
  3280. $new_view = substr_replace($view,'0',1,1);
  3281. echo "<tr>
  3282. <td valign='top'>
  3283. <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>]
  3284. </td>
  3285. </tr>";
  3286. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('ExercicesDetails')."<br />";
  3287. $sql = "SELECT ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  3288. FROM $TABLECOURSE_EXERCICES AS ce , $TABLETRACK_EXERCICES AS te
  3289. WHERE te.exe_cours_id = '".Database::escape_string($course_id)."'
  3290. AND te.exe_user_id = '".Database::escape_string($user_id)."'
  3291. AND te.exe_exo_id = ce.id
  3292. ORDER BY ce.title ASC, te.exe_date ASC";
  3293. $hpsql = "SELECT te.exe_name, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  3294. FROM $TBL_TRACK_HOTPOTATOES AS te
  3295. WHERE te.exe_user_id = '".Database::escape_string($user_id)."' AND te.exe_cours_id = '".Database::escape_string($course_id)."'
  3296. ORDER BY te.exe_cours_id ASC, te.exe_date ASC";
  3297. $hpresults = getManyResultsXCol($hpsql, 4);
  3298. $NoTestRes = 0;
  3299. $NoHPTestRes = 0;
  3300. echo "<tr>\n<td style='padding-left : 40px;padding-right : 40px;'>\n";
  3301. $results = getManyResultsXCol($sql, 4);
  3302. echo "<table cellpadding='2' cellspacing='1' border='0' align='center'>\n";
  3303. echo "
  3304. <tr bgcolor='#E6E6E6'>
  3305. <td>
  3306. ".get_lang('ExercicesTitleExerciceColumn')."
  3307. </td>
  3308. <td>
  3309. ".get_lang('Date')."
  3310. </td>
  3311. <td>
  3312. ".get_lang('ExercicesTitleScoreColumn')."
  3313. </td>
  3314. </tr>";
  3315. if (is_array($results)) {
  3316. for($i = 0; $i < sizeof($results); $i++) {
  3317. $display_date = api_convert_and_format_date($results[$i][3], null, date_default_timezone_get());
  3318. echo "<tr>\n";
  3319. echo "<td class='content'>".$results[$i][0]."</td>\n";
  3320. echo "<td class='content'>".$display_date."</td>\n";
  3321. echo "<td valign='top' align='right' class='content'>".$results[$i][1]." / ".$results[$i][2]."</td>\n";
  3322. echo "</tr>\n";
  3323. }
  3324. } else {
  3325. // istvan begin
  3326. $NoTestRes = 1;
  3327. }
  3328. // The Result of Tests
  3329. if(is_array($hpresults)) {
  3330. for($i = 0; $i < sizeof($hpresults); $i++) {
  3331. $title = GetQuizName($hpresults[$i][0],'');
  3332. if ($title == '')
  3333. $title = basename($hpresults[$i][0]);
  3334. $display_date = api_convert_and_format_date($hpresults[$i][3], null, date_default_timezone_get());
  3335. ?>
  3336. <tr>
  3337. <td class="content"><?php echo $title; ?></td>
  3338. <td class="content" align="center"><?php echo $display_date; ?></td>
  3339. <td class="content" align="center"><?php echo $hpresults[$i][1]; ?> / <?php echo $hpresults[$i][2]; ?>
  3340. </td>
  3341. </tr>
  3342. <?php }
  3343. } else {
  3344. $NoHPTestRes = 1;
  3345. }
  3346. if ($NoTestRes == 1 && $NoHPTestRes == 1) {
  3347. echo "<tr>\n";
  3348. echo "<td colspan='3'><center>".get_lang('NoResult')."</center></td>\n";
  3349. echo "</tr>\n";
  3350. }
  3351. echo "</table>";
  3352. echo "</td>\n</tr>\n";
  3353. } else {
  3354. $new_view = substr_replace($view,'1',1,1);
  3355. echo "
  3356. <tr>
  3357. <td valign='top'>
  3358. +<font color='#0000FF'>&nbsp;&nbsp;</font><a href='".api_get_self()."?uInfo=$user_id&view=".$new_view."' class='specialLink'>".get_lang('ExercicesResults')."</a>
  3359. </td>
  3360. </tr>";
  3361. }
  3362. }
  3363. /**
  3364. * Displays the student publications for a specific user in a specific course.
  3365. * @todo remove globals
  3366. */
  3367. function display_student_publications_tracking_info($view, $user_id, $course_id)
  3368. {
  3369. global $TABLETRACK_UPLOADS, $TABLECOURSE_WORK, $dateTimeFormatLong, $_course;
  3370. if(substr($view,2,1) == '1') {
  3371. $new_view = substr_replace($view,'0',2,1);
  3372. echo "<tr>
  3373. <td valign='top'>
  3374. <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>]
  3375. </td>
  3376. </tr>";
  3377. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('WorksDetails')."<br>";
  3378. $sql = "SELECT u.upload_date, w.title, w.author,w.url
  3379. FROM $TABLETRACK_UPLOADS u , $TABLECOURSE_WORK w
  3380. WHERE u.upload_work_id = w.id
  3381. AND u.upload_user_id = '".Database::escape_string($user_id)."'
  3382. AND u.upload_cours_id = '".Database::escape_string($course_id)."'
  3383. ORDER BY u.upload_date DESC";
  3384. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  3385. $results = getManyResultsXCol($sql,4);
  3386. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  3387. echo "<tr>
  3388. <td class='secLine' width='40%'>
  3389. ".get_lang('WorkTitle')."
  3390. </td>
  3391. <td class='secLine' width='30%'>
  3392. ".get_lang('WorkAuthors')."
  3393. </td>
  3394. <td class='secLine' width='30%'>
  3395. ".get_lang('Date')."
  3396. </td>
  3397. </tr>";
  3398. if (is_array($results)) {
  3399. for($j = 0 ; $j < count($results) ; $j++) {
  3400. $pathToFile = api_get_path(WEB_COURSE_PATH).$_course['path']."/".$results[$j][3];
  3401. $beautifulDate = api_convert_and_format_date($results[$j][0], null, date_default_timezone_get());
  3402. echo "<tr>";
  3403. echo "<td class='content'>"
  3404. ."<a href ='".$pathToFile."'>".$results[$j][1]."</a>"
  3405. ."</td>";
  3406. echo "<td class='content'>".$results[$j][2]."</td>";
  3407. echo "<td class='content'>".$beautifulDate."</td>";
  3408. echo"</tr>";
  3409. }
  3410. } else {
  3411. echo "<tr>";
  3412. echo "<td colspan='3'><center>".get_lang('NoResult')."</center></td>";
  3413. echo"</tr>";
  3414. }
  3415. echo "</table>";
  3416. echo "</td></tr>";
  3417. } else {
  3418. $new_view = substr_replace($view,'1',2,1);
  3419. echo "
  3420. <tr>
  3421. <td valign='top'>
  3422. +<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>
  3423. </td>
  3424. </tr>
  3425. ";
  3426. }
  3427. }
  3428. /**
  3429. * Displays the links followed for a specific user in a specific course.
  3430. * @todo remove globals
  3431. */
  3432. function display_links_tracking_info($view, $user_id, $course_id)
  3433. {
  3434. global $TABLETRACK_LINKS, $TABLECOURSE_LINKS;
  3435. if(substr($view,3,1) == '1') {
  3436. $new_view = substr_replace($view,'0',3,1);
  3437. echo "
  3438. <tr>
  3439. <td valign='top'>
  3440. <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>]
  3441. </td>
  3442. </tr>
  3443. ";
  3444. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('LinksDetails')."<br>";
  3445. $sql = "SELECT cl.title, cl.url
  3446. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  3447. WHERE sl.links_link_id = cl.id
  3448. AND sl.links_cours_id = '".Database::escape_string($course_id)."'
  3449. AND sl.links_user_id = '".Database::escape_string($user_id)."'
  3450. GROUP BY cl.title, cl.url";
  3451. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  3452. $results = getManyResults2Col($sql);
  3453. echo "<table cellpadding='2' cellspacing='1' border='0' align=center>";
  3454. echo "<tr>
  3455. <td class='secLine'>
  3456. ".get_lang('LinksTitleLinkColumn')."
  3457. </td>
  3458. </tr>";
  3459. if (is_array($results)) {
  3460. for($j = 0 ; $j < count($results) ; $j++) {
  3461. echo "<tr>";
  3462. echo "<td class='content'><a href='".$results[$j][1]."'>".$results[$j][0]."</a></td>";
  3463. echo"</tr>";
  3464. }
  3465. } else {
  3466. echo "<tr>";
  3467. echo "<td ><center>".get_lang('NoResult')."</center></td>";
  3468. echo"</tr>";
  3469. }
  3470. echo "</table>";
  3471. echo "</td></tr>";
  3472. } else {
  3473. $new_view = substr_replace($view,'1',3,1);
  3474. echo "
  3475. <tr>
  3476. <td valign='top'>
  3477. +<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>
  3478. </td>
  3479. </tr>
  3480. ";
  3481. }
  3482. }
  3483. /**
  3484. * Displays the documents downloaded for a specific user in a specific course.
  3485. * @param string kind of view inside tracking info
  3486. * @param int User id
  3487. * @param string Course code
  3488. * @param int Session id (optional, default = 0)
  3489. * @return void
  3490. */
  3491. static function display_document_tracking_info($view, $user_id, $course_id, $session_id = 0) {
  3492. // protect data
  3493. $user_id = intval($user_id);
  3494. $course_id = Database::escape_string($course_id);
  3495. $session_id = intval($session_id);
  3496. $downloads_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  3497. if(substr($view,4,1) == '1')
  3498. {
  3499. $new_view = substr_replace($view,'0',4,1);
  3500. echo "
  3501. <tr>
  3502. <td valign='top'>
  3503. <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>]
  3504. </td>
  3505. </tr>
  3506. ";
  3507. echo "<tr><td style='padding-left : 40px;' valign='top'>".get_lang('DocumentsDetails')."<br>";
  3508. $sql = "SELECT down_doc_path
  3509. FROM $downloads_table
  3510. WHERE down_cours_id = '".$course_id."'
  3511. AND down_user_id = '$user_id'
  3512. AND down_session_id = '$session_id'
  3513. GROUP BY down_doc_path";
  3514. echo "<tr><td style='padding-left : 40px;padding-right : 40px;'>";
  3515. $results = getManyResults1Col($sql);
  3516. echo "<table cellpadding='2' cellspacing='1' border='0' align='center'>";
  3517. echo "<tr>
  3518. <td class='secLine'>
  3519. ".get_lang('DocumentsTitleDocumentColumn')."
  3520. </td>
  3521. </tr>";
  3522. if (is_array($results)) {
  3523. for($j = 0 ; $j < count($results) ; $j++) {
  3524. echo "<tr>";
  3525. echo "<td class='content'>".$results[$j]."</td>";
  3526. echo"</tr>";
  3527. }
  3528. } else {
  3529. echo "<tr>";
  3530. echo "<td><center>".get_lang('NoResult')."</center></td>";
  3531. echo"</tr>";
  3532. }
  3533. echo "</table>";
  3534. echo "</td></tr>";
  3535. } else {
  3536. $new_view = substr_replace($view,'1',4,1);
  3537. echo "
  3538. <tr>
  3539. <td valign='top'>
  3540. +<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>
  3541. </td>
  3542. </tr>
  3543. ";
  3544. }
  3545. }
  3546. /**
  3547. * Gets the IP of a given user, using the last login before the given date
  3548. * @param int User ID
  3549. * @param string Datetime
  3550. * @param bool Whether to return the IP as a link or just as an IP
  3551. * @param string If defined and return_as_link if true, will be used as the text to be shown as the link
  3552. * @return string IP address (or false on error)
  3553. * @assert (0,0) === false
  3554. */
  3555. static function get_ip_from_user_event($user_id, $event_date, $return_as_link = false, $body_replace = null) {
  3556. if (empty($user_id) or empty($event_date)) {
  3557. return false;
  3558. }
  3559. $table_login = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  3560. $sql_ip = "SELECT login_date, login_ip FROM $table_login WHERE login_user_id = $user_id AND login_date < '$event_date' ORDER BY login_date DESC LIMIT 1";
  3561. $ip = '';
  3562. $res_ip = Database::query($sql_ip);
  3563. if ($res_ip !== false && Database::num_rows($res_ip)>0) {
  3564. $row_ip = Database::fetch_row($res_ip);
  3565. if ($return_as_link) {
  3566. $ip = Display::url((empty($body_replace)?$row_ip[1]:$body_replace), 'http://www.whatsmyip.org/ip-geo-location/?ip='.$row_ip[1], array('title'=>get_lang('TraceIP'), 'target'=>'_blank'));
  3567. } else {
  3568. $ip = $row_ip[1];
  3569. }
  3570. }
  3571. return $ip;
  3572. }
  3573. }
  3574. /**
  3575. * @package chamilo.tracking
  3576. */
  3577. class TrackingUserLogCSV {
  3578. /**
  3579. * Displays the number of logins every month for a specific user in a specific course.
  3580. */
  3581. function display_login_tracking_info($view, $user_id, $course_id, $session_id = 0)
  3582. {
  3583. $MonthsLong = $GLOBALS['MonthsLong'];
  3584. $track_access_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ACCESS);
  3585. // protected data
  3586. $user_id = intval($user_id);
  3587. $session_id = intval($session_id);
  3588. $course_id = Database::escape_string($course_id);
  3589. $tempView = $view;
  3590. if(substr($view,0,1) == '1')
  3591. {
  3592. $new_view = substr_replace($view,'0',0,1);
  3593. $title[1]= get_lang('LoginsAndAccessTools').get_lang('LoginsDetails');
  3594. $sql = "SELECT UNIX_TIMESTAMP(access_date), count(access_date)
  3595. FROM $track_access_table
  3596. WHERE access_user_id = '$user_id'
  3597. AND access_cours_code = '".$course_id."'
  3598. AND access_session_id = '$session_id'
  3599. GROUP BY YEAR(access_date),MONTH(access_date)
  3600. ORDER BY YEAR(access_date),MONTH(access_date) ASC";
  3601. //$results = getManyResults2Col($sql);
  3602. $results = getManyResults3Col($sql);
  3603. $title_line= get_lang('LoginsTitleMonthColumn').';'.get_lang('LoginsTitleCountColumn')."\n";
  3604. $line='';
  3605. $total = 0;
  3606. if (is_array($results))
  3607. {
  3608. for($j = 0 ; $j < count($results) ; $j++)
  3609. {
  3610. $line .= $results[$j][0].';'.$results[$j][1]."\n";
  3611. $total = $total + $results[$j][1];
  3612. }
  3613. $line .= get_lang('Total').";".$total."\n";
  3614. }
  3615. else
  3616. {
  3617. $line= get_lang('NoResult')."</center></td>";
  3618. }
  3619. }
  3620. else
  3621. {
  3622. $new_view = substr_replace($view,'1',0,1);
  3623. }
  3624. return array($title_line, $line);
  3625. }
  3626. /**
  3627. * Displays the exercise results for a specific user in a specific course.
  3628. * @todo remove globals
  3629. */
  3630. function display_exercise_tracking_info($view, $user_id, $course_id) {
  3631. global $TABLECOURSE_EXERCICES, $TABLETRACK_EXERCICES, $TABLETRACK_HOTPOTATOES, $dateTimeFormatLong;
  3632. if (substr($view,1,1) == '1') {
  3633. $new_view = substr_replace($view,'0',1,1);
  3634. $title[1]= get_lang('ExercicesDetails');
  3635. $line='';
  3636. $sql = "SELECT ce.title, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  3637. FROM $TABLECOURSE_EXERCICES AS ce , $TABLETRACK_EXERCICES AS te
  3638. WHERE te.exe_cours_id = '$course_id'
  3639. AND te.exe_user_id = '$user_id'
  3640. AND te.exe_exo_id = ce.id
  3641. ORDER BY ce.title ASC, te.exe_date ASC";
  3642. $hpsql = "SELECT te.exe_name, te.exe_result , te.exe_weighting, UNIX_TIMESTAMP(te.exe_date)
  3643. FROM $TABLETRACK_HOTPOTATOES AS te
  3644. WHERE te.exe_user_id = '$user_id' AND te.exe_cours_id = '$course_id'
  3645. ORDER BY te.exe_cours_id ASC, te.exe_date ASC";
  3646. $hpresults = getManyResultsXCol($hpsql, 4);
  3647. $NoTestRes = 0;
  3648. $NoHPTestRes = 0;
  3649. $results = getManyResultsXCol($sql, 4);
  3650. $title_line=get_lang('ExercicesTitleExerciceColumn').";".get_lang('Date').';'.get_lang('ExercicesTitleScoreColumn')."\n";
  3651. if (is_array($results))
  3652. {
  3653. for($i = 0; $i < sizeof($results); $i++)
  3654. {
  3655. $display_date = api_convert_and_format_date($results[$i][3], null, date_default_timezone_get());
  3656. $line .= $results[$i][0].";".$display_date.";".$results[$i][1]." / ".$results[$i][2]."\n";
  3657. }
  3658. }
  3659. else // istvan begin
  3660. {
  3661. $NoTestRes = 1;
  3662. }
  3663. // The Result of Tests
  3664. if(is_array($hpresults))
  3665. {
  3666. for($i = 0; $i < sizeof($hpresults); $i++)
  3667. {
  3668. $title = GetQuizName($hpresults[$i][0],'');
  3669. if ($title == '')
  3670. $title = basename($hpresults[$i][0]);
  3671. $display_date = api_convert_and_format_date($hpresults[$i][3], null, date_default_timezone_get());
  3672. $line .= $title.';'.$display_date.';'.$hpresults[$i][1].'/'.$hpresults[$i][2]."\n";
  3673. }
  3674. }
  3675. else
  3676. {
  3677. $NoHPTestRes = 1;
  3678. }
  3679. if ($NoTestRes == 1 && $NoHPTestRes == 1)
  3680. {
  3681. $line=get_lang('NoResult');
  3682. }
  3683. }
  3684. else
  3685. {
  3686. $new_view = substr_replace($view,'1',1,1);
  3687. }
  3688. return array($title_line, $line);
  3689. }
  3690. /**
  3691. * Displays the student publications for a specific user in a specific course.
  3692. * @todo remove globals
  3693. */
  3694. function display_student_publications_tracking_info($view, $user_id, $course_id) {
  3695. global $TABLETRACK_UPLOADS, $TABLECOURSE_WORK, $dateTimeFormatLong, $_course;
  3696. if (substr($view,2,1) == '1') {
  3697. $new_view = substr_replace($view,'0',2,1);
  3698. $sql = "SELECT u.upload_date, w.title, w.author, w.url
  3699. FROM $TABLETRACK_UPLOADS u , $TABLECOURSE_WORK w
  3700. WHERE u.upload_work_id = w.id
  3701. AND u.upload_user_id = '$user_id'
  3702. AND u.upload_cours_id = '$course_id'
  3703. ORDER BY u.upload_date DESC";
  3704. $results = getManyResultsXCol($sql,4);
  3705. $title[1]=get_lang('WorksDetails');
  3706. $line='';
  3707. $title_line=get_lang('WorkTitle').";".get_lang('WorkAuthors').";".get_lang('Date')."\n";
  3708. if (is_array($results)) {
  3709. for($j = 0 ; $j < count($results) ; $j++) {
  3710. $pathToFile = api_get_path(WEB_COURSE_PATH).$_course['path']."/".$results[$j][3];
  3711. $beautifulDate = api_convert_and_format_date($results[$j][0], null, date_default_timezone_get());
  3712. $line .= $results[$j][1].";".$results[$j][2].";".$beautifulDate."\n";
  3713. }
  3714. } else {
  3715. $line= get_lang('NoResult');
  3716. }
  3717. } else {
  3718. $new_view = substr_replace($view,'1',2,1);
  3719. }
  3720. return array($title_line, $line);
  3721. }
  3722. /**
  3723. * Displays the links followed for a specific user in a specific course.
  3724. * @todo remove globals
  3725. */
  3726. function display_links_tracking_info($view, $user_id, $course_id) {
  3727. global $TABLETRACK_LINKS, $TABLECOURSE_LINKS;
  3728. if (substr($view,3,1) == '1') {
  3729. $new_view = substr_replace($view,'0',3,1);
  3730. $title[1]=get_lang('LinksDetails');
  3731. $sql = "SELECT cl.title, cl.url
  3732. FROM $TABLETRACK_LINKS AS sl, $TABLECOURSE_LINKS AS cl
  3733. WHERE sl.links_link_id = cl.id
  3734. AND sl.links_cours_id = '$course_id'
  3735. AND sl.links_user_id = '$user_id'
  3736. GROUP BY cl.title, cl.url";
  3737. $results = getManyResults2Col($sql);
  3738. $title_line= get_lang('LinksTitleLinkColumn')."\n";
  3739. if (is_array($results)) {
  3740. for ($j = 0 ; $j < count($results) ; $j++) {
  3741. $line .= $results[$j][0]."\n";
  3742. }
  3743. } else {
  3744. $line=get_lang('NoResult');
  3745. }
  3746. } else {
  3747. $new_view = substr_replace($view,'1',3,1);
  3748. }
  3749. return array($title_line, $line);
  3750. }
  3751. /**
  3752. * Displays the documents downloaded for a specific user in a specific course.
  3753. * @param string kind of view inside tracking info
  3754. * @param int User id
  3755. * @param string Course code
  3756. * @param int Session id (optional, default = 0)
  3757. * @return void
  3758. */
  3759. function display_document_tracking_info($view, $user_id, $course_id, $session_id = 0) {
  3760. // protect data
  3761. $user_id = intval($user_id);
  3762. $course_id = Database::escape_string($course_id);
  3763. $session_id = intval($session_id);
  3764. $downloads_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
  3765. if (substr($view,4,1) == '1') {
  3766. $new_view = substr_replace($view,'0',4,1);
  3767. $title[1]= get_lang('DocumentsDetails');
  3768. $sql = "SELECT down_doc_path
  3769. FROM $downloads_table
  3770. WHERE down_cours_id = '$course_id'
  3771. AND down_user_id = '$user_id'
  3772. AND down_session_id = '$session_id'
  3773. GROUP BY down_doc_path";
  3774. $results = getManyResults1Col($sql);
  3775. $title_line = get_lang('DocumentsTitleDocumentColumn')."\n";
  3776. if (is_array($results)) {
  3777. for ($j = 0 ; $j < count($results) ; $j++) {
  3778. $line .= $results[$j]."\n";
  3779. }
  3780. } else {
  3781. $line = get_lang('NoResult');
  3782. }
  3783. } else {
  3784. $new_view = substr_replace($view,'1',4,1);
  3785. }
  3786. return array($title_line, $line);
  3787. }
  3788. }