tracking.lib.php 199 KB

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