tracking.lib.php 174 KB

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