tracking.lib.php 174 KB

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