tracking.lib.php 174 KB

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