tracking.lib.php 195 KB

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