tracking.lib.php 204 KB

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