tracking.lib.php 202 KB

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