tracking.lib.php 202 KB

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