tracking.lib.php 203 KB

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