tracking.lib.php 164 KB

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