myspace.lib.php 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use CpChart\Chart\Cache as pCache;
  4. use CpChart\Chart\Data as pData;
  5. use CpChart\Chart\Image as pImage;
  6. /**
  7. * Class MySpace
  8. * @package chamilo.reporting
  9. */
  10. class MySpace
  11. {
  12. /**
  13. * Get admin actions
  14. * @return string
  15. */
  16. public static function getAdminActions()
  17. {
  18. $actions = array(
  19. //array('url' => api_get_path(WEB_CODE_PATH).'mySpace/index.php', 'content' => get_lang('Home')),
  20. array(
  21. 'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=coaches',
  22. 'content' => get_lang('DisplayCoaches'),
  23. ),
  24. array(
  25. 'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=user',
  26. 'content' => get_lang('DisplayUserOverview'),
  27. ),
  28. array(
  29. 'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=session',
  30. 'content' => get_lang('DisplaySessionOverview'),
  31. ),
  32. array(
  33. 'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=course',
  34. 'content' => get_lang('DisplayCourseOverview'),
  35. ),
  36. array(
  37. 'url' => api_get_path(WEB_CODE_PATH).'tracking/question_course_report.php?view=admin',
  38. 'content' => get_lang('LPQuestionListResults'),
  39. ),
  40. array(
  41. 'url' => api_get_path(WEB_CODE_PATH).'tracking/course_session_report.php?view=admin',
  42. 'content' => get_lang('LPExerciseResultsBySession'),
  43. ),
  44. [
  45. 'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=accessoverview',
  46. 'content' => get_lang('DisplayAccessOverview').' ('.get_lang('Beta').')',
  47. ],
  48. );
  49. return Display::actions($actions, null);
  50. }
  51. public static function getTopMenu()
  52. {
  53. $menu_items = array();
  54. $menu_items[] = Display::url(
  55. Display::return_icon('stats.png', get_lang('MyStats'), '', ICON_SIZE_MEDIUM),
  56. api_get_path(WEB_CODE_PATH)."auth/my_progress.php"
  57. );
  58. $menu_items[] = Display::url(
  59. Display::return_icon('teacher.png', get_lang('TeacherInterface'), array(), 32),
  60. api_get_path(WEB_CODE_PATH).'mySpace/?view=teacher'
  61. );
  62. $menu_items[] = Display::url(Display::return_icon('star_na.png', get_lang('AdminInterface'), array(), 32), '#');
  63. $menu_items[] = Display::url(
  64. Display::return_icon('quiz.png', get_lang('ExamTracking'), array(), 32),
  65. api_get_path(WEB_CODE_PATH).'tracking/exams.php'
  66. );
  67. $menu = null;
  68. foreach ($menu_items as $item) {
  69. $menu .= $item;
  70. }
  71. $menu .= '<br />';
  72. return $menu;
  73. }
  74. /**
  75. * This function serves exporting data in CSV format.
  76. * @param array $header The header labels.
  77. * @param array $data The data array.
  78. * @param string $file_name The name of the file which contains exported data.
  79. * @return string mixed Returns a message (string) if an error occurred.
  80. */
  81. public function export_csv($header, $data, $file_name = 'export.csv')
  82. {
  83. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  84. $archive_url = api_get_path(WEB_CODE_PATH).'course_info/download.php?archive_path=&archive=';
  85. $message = '';
  86. if (!$open = fopen($archive_path.$file_name, 'w+')) {
  87. $message = get_lang('noOpen');
  88. } else {
  89. $info = '';
  90. foreach ($header as $value) {
  91. $info .= $value.';';
  92. }
  93. $info .= "\r\n";
  94. foreach ($data as $row) {
  95. foreach ($row as $value) {
  96. $info .= $value.';';
  97. }
  98. $info .= "\r\n";
  99. }
  100. fwrite($open, $info);
  101. fclose($open);
  102. @chmod($file_name, api_get_permissions_for_new_files());
  103. header("Location:".$archive_url.$file_name);
  104. }
  105. return $message;
  106. }
  107. /**
  108. * Gets the connections to a course as an array of login and logout time
  109. *
  110. * @param int $userId User id
  111. * @param int $courseId
  112. * @param int $sessionId Session id (optional, default = 0)
  113. * @return array Connections
  114. */
  115. public static function get_connections_to_course($userId, $courseId, $sessionId = 0)
  116. {
  117. // Database table definitions
  118. $tbl_track_course = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  119. // protect data
  120. $userId = (int) $userId;
  121. $courseId = (int) $courseId;
  122. $sessionId = (int) $sessionId;
  123. $sql = 'SELECT login_course_date, logout_course_date
  124. FROM ' . $tbl_track_course.'
  125. WHERE
  126. user_id = '.$userId.' AND
  127. c_id = '.$courseId.' AND
  128. session_id = '.$sessionId.'
  129. ORDER BY login_course_date ASC';
  130. $rs = Database::query($sql);
  131. $connections = array();
  132. while ($row = Database::fetch_array($rs)) {
  133. $timestamp_login_date = api_strtotime($row['login_course_date'], 'UTC');
  134. $timestamp_logout_date = api_strtotime($row['logout_course_date'], 'UTC');
  135. $connections[] = array('login' => $timestamp_login_date, 'logout' => $timestamp_logout_date);
  136. }
  137. return $connections;
  138. }
  139. /**
  140. * @param $user_id
  141. * @param $course_list
  142. * @param int $session_id
  143. * @return array|bool
  144. */
  145. public static function get_connections_from_course_list(
  146. $user_id,
  147. $course_list,
  148. $session_id = 0
  149. ) {
  150. // Database table definitions
  151. $tbl_track_course = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  152. if (empty($course_list)) {
  153. return false;
  154. }
  155. // protect data
  156. $user_id = intval($user_id);
  157. $session_id = intval($session_id);
  158. $new_course_list = array();
  159. foreach ($course_list as $course_item) {
  160. $courseInfo = api_get_course_info($course_item['code']);
  161. $courseId = $courseInfo['real_id'];
  162. $new_course_list[] = '"'.$courseId.'"';
  163. }
  164. $course_list = implode(', ', $new_course_list);
  165. if (empty($course_list)) {
  166. return false;
  167. }
  168. $sql = 'SELECT login_course_date, logout_course_date, c_id
  169. FROM ' . $tbl_track_course.'
  170. WHERE
  171. user_id = '.$user_id.' AND
  172. c_id IN ('.$course_list.') AND
  173. session_id = '.$session_id.'
  174. ORDER BY login_course_date ASC';
  175. $rs = Database::query($sql);
  176. $connections = array();
  177. while ($row = Database::fetch_array($rs)) {
  178. $timestamp_login_date = api_strtotime($row['login_course_date'], 'UTC');
  179. $timestamp_logout_date = api_strtotime($row['logout_course_date'], 'UTC');
  180. $connections[] = array(
  181. 'login' => $timestamp_login_date,
  182. 'logout' => $timestamp_logout_date,
  183. 'c_id' => $row['c_id']
  184. );
  185. }
  186. return $connections;
  187. }
  188. /**
  189. * Creates a small table in the last column of the table with the user overview
  190. *
  191. * @param integer $user_id the id of the user
  192. * @param array $url_params additional url parameters
  193. * @param array $row the row information (the other columns)
  194. * @return string html code
  195. */
  196. public static function course_info_tracking_filter($user_id, $url_params, $row)
  197. {
  198. // the table header
  199. $return = '<table class="data_table" style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  200. // database table definition
  201. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  202. // getting all the courses of the user
  203. $sql = "SELECT * FROM $tbl_course_user
  204. WHERE
  205. user_id = '".intval($user_id)."' AND
  206. relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
  207. $result = Database::query($sql);
  208. while ($row = Database::fetch_array($result)) {
  209. $courseInfo = api_get_course_info_by_id($row['c_id']);
  210. if (empty($courseInfo)) {
  211. continue;
  212. }
  213. $courseCode = $courseInfo['code'];
  214. $courseId = $courseInfo['real_id'];
  215. $return .= '<tr>';
  216. // course code
  217. $return .= ' <td width="157px" >'.cut($courseCode, 20, true).'</td>';
  218. // time spent in the course
  219. $return .= ' <td><div>'.api_time_to_hms(Tracking::get_time_spent_on_the_course($user_id, $courseId)).'</div></td>';
  220. // student progress in course
  221. $return .= ' <td><div>'.round(Tracking::get_avg_student_progress($user_id, $courseCode), 2).'</div></td>';
  222. // student score
  223. $avg_score = Tracking::get_avg_student_score($user_id, $courseCode);
  224. if (is_numeric($avg_score)) {
  225. $avg_score = round($avg_score, 2);
  226. } else {
  227. $$avg_score = '-';
  228. }
  229. $return .= ' <td><div>'.$avg_score.'</div></td>';
  230. // student tes score
  231. //$return .= ' <td><div style="width:40px">'.round(Tracking::get_avg_student_exercise_score ($user_id, $courseCode),2).'%</div></td>';
  232. // student messages
  233. $return .= ' <td><div>'.Tracking::count_student_messages($user_id, $courseCode).'</div></td>';
  234. // student assignments
  235. $return .= ' <td><div>'.Tracking::count_student_assignments($user_id, $courseCode).'</div></td>';
  236. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  237. $exercises_results = self::exercises_results($user_id, $courseCode);
  238. $return .= ' <td width="105px"><div>'.(is_null($exercises_results['percentage']) ? '' : $exercises_results['score_obtained'].'/'.$exercises_results['score_possible'].' ( '.$exercises_results['percentage'].'% )').'</div></td>';
  239. $return .= ' <td><div>'.$exercises_results['questions_answered'].'</div></td>';
  240. $return .= ' <td><div>'.Tracking::get_last_connection_date_on_the_course($user_id, $courseInfo).'</div></td>';
  241. $return .= '<tr>';
  242. }
  243. $return .= '</table>';
  244. return $return;
  245. }
  246. /**
  247. * Display a sortable table that contains an overview off all the
  248. * reporting progress of all users and all courses the user is subscribed to
  249. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  250. * @version Dokeos 1.8.6
  251. * @since October 2008
  252. */
  253. public static function display_tracking_user_overview()
  254. {
  255. self::display_user_overview_export_options();
  256. $t_head = '<table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  257. $t_head .= '<tr>';
  258. $t_head .= '<th width="155px" style="border-left:0;border-bottom:0"><span>'.get_lang('Course').'</span></th>';
  259. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
  260. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
  261. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
  262. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
  263. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
  264. $t_head .= '<th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
  265. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
  266. $t_head .= '<th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
  267. $t_head .= '</tr></table>';
  268. $addparams = array('view' => 'admin', 'display' => 'user');
  269. $table = new SortableTable(
  270. 'tracking_user_overview',
  271. array('MySpace', 'get_number_of_users_tracking_overview'),
  272. array('MySpace', 'get_user_data_tracking_overview'),
  273. 0
  274. );
  275. $table->additional_parameters = $addparams;
  276. $table->set_header(0, get_lang('OfficialCode'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  277. if (api_is_western_name_order()) {
  278. $table->set_header(1, get_lang('FirstName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  279. $table->set_header(2, get_lang('LastName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  280. } else {
  281. $table->set_header(1, get_lang('LastName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  282. $table->set_header(2, get_lang('FirstName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  283. }
  284. $table->set_header(3, get_lang('LoginName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  285. $table->set_header(4, $t_head, false, array('style' => 'width:90%;border:0;padding:0;font-size:7.5pt;'), array('style' => 'width:90%;padding:0;font-size:7.5pt;'));
  286. $table->set_column_filter(4, array('MySpace', 'course_info_tracking_filter'));
  287. $table->display();
  288. }
  289. /**
  290. * @param $export_csv
  291. */
  292. public static function display_tracking_coach_overview($export_csv)
  293. {
  294. if ($export_csv) {
  295. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  296. } else {
  297. $is_western_name_order = api_is_western_name_order();
  298. }
  299. $sort_by_first_name = api_sort_by_first_name();
  300. $tracking_column = isset($_GET['tracking_list_coaches_column']) ? $_GET['tracking_list_coaches_column'] : ($is_western_name_order xor $sort_by_first_name) ? 1 : 0;
  301. $tracking_direction = (isset($_GET['tracking_list_coaches_direction']) && in_array(strtoupper($_GET['tracking_list_coaches_direction']), array('ASC', 'DESC', 'ASCENDING', 'DESCENDING', '0', '1'))) ? $_GET['tracking_list_coaches_direction'] : 'DESC';
  302. // Prepare array for column order - when impossible, use some of user names.
  303. if ($is_western_name_order) {
  304. $order = array(
  305. 0 => 'firstname',
  306. 1 => 'lastname',
  307. 2 => ($sort_by_first_name ? 'firstname' : 'lastname'),
  308. 3 => 'login_date',
  309. 4 => ($sort_by_first_name ? 'firstname' : 'lastname'),
  310. 5 => ($sort_by_first_name ? 'firstname' : 'lastname'),
  311. );
  312. } else {
  313. $order = array(
  314. 0 => 'lastname',
  315. 1 => 'firstname',
  316. 2 => ($sort_by_first_name ? 'firstname' : 'lastname'),
  317. 3 => 'login_date',
  318. 4 => ($sort_by_first_name ? 'firstname' : 'lastname'),
  319. 5 => ($sort_by_first_name ? 'firstname' : 'lastname'),
  320. );
  321. }
  322. $table = new SortableTable(
  323. 'tracking_list_coaches_myspace',
  324. array('MySpace', 'count_coaches'),
  325. null,
  326. ($is_western_name_order xor $sort_by_first_name) ? 1 : 0
  327. );
  328. $parameters['view'] = 'admin';
  329. $table->set_additional_parameters($parameters);
  330. if ($is_western_name_order) {
  331. $table->set_header(0, get_lang('FirstName'), true);
  332. $table->set_header(1, get_lang('LastName'), true);
  333. } else {
  334. $table->set_header(0, get_lang('LastName'), true);
  335. $table->set_header(1, get_lang('FirstName'), true);
  336. }
  337. $table->set_header(2, get_lang('TimeSpentOnThePlatform'), false);
  338. $table->set_header(3, get_lang('LastConnexion'), false);
  339. $table->set_header(4, get_lang('NbStudents'), false);
  340. $table->set_header(5, get_lang('CountCours'), false);
  341. $table->set_header(6, get_lang('NumberOfSessions'), false);
  342. $table->set_header(7, get_lang('Sessions'), false);
  343. if ($is_western_name_order) {
  344. $csv_header[] = array(
  345. get_lang('FirstName'),
  346. get_lang('LastName'),
  347. get_lang('TimeSpentOnThePlatform'),
  348. get_lang('LastConnexion'),
  349. get_lang('NbStudents'),
  350. get_lang('CountCours'),
  351. get_lang('NumberOfSessions')
  352. );
  353. } else {
  354. $csv_header[] = array(
  355. get_lang('LastName'),
  356. get_lang('FirstName'),
  357. get_lang('TimeSpentOnThePlatform'),
  358. get_lang('LastConnexion'),
  359. get_lang('NbStudents'),
  360. get_lang('CountCours'),
  361. get_lang('NumberOfSessions')
  362. );
  363. }
  364. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  365. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  366. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  367. $tbl_sessions = Database::get_main_table(TABLE_MAIN_SESSION);
  368. $sqlCoachs = "SELECT DISTINCT
  369. scu.user_id as id_coach,
  370. u.id as user_id,
  371. lastname,
  372. firstname,
  373. MAX(login_date) as login_date
  374. FROM $tbl_user u, $tbl_session_course_user scu, $tbl_track_login
  375. WHERE
  376. scu.user_id = u.id AND scu.status=2 AND login_user_id=u.id
  377. GROUP BY user_id ";
  378. if (api_is_multiple_url_enabled()) {
  379. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  380. $access_url_id = api_get_current_access_url_id();
  381. if ($access_url_id != -1) {
  382. $sqlCoachs = "SELECT DISTINCT
  383. scu.user_id as id_coach,
  384. u.id as user_id,
  385. lastname,
  386. firstname,
  387. MAX(login_date) as login_date
  388. FROM $tbl_user u,
  389. $tbl_session_course_user scu,
  390. $tbl_track_login ,
  391. $tbl_session_rel_access_url session_rel_url
  392. WHERE
  393. scu.user_id = u.id AND
  394. scu.status = 2 AND
  395. login_user_id = u.id AND
  396. access_url_id = $access_url_id AND
  397. session_rel_url.session_id = scu.session_id
  398. GROUP BY u.id";
  399. }
  400. }
  401. if (!empty($order[$tracking_column])) {
  402. $sqlCoachs .= " ORDER BY ".$order[$tracking_column]." ".$tracking_direction;
  403. }
  404. $result_coaches = Database::query($sqlCoachs);
  405. //$total_no_coaches = Database::num_rows($result_coaches);
  406. $global_coaches = array();
  407. while ($coach = Database::fetch_array($result_coaches)) {
  408. $global_coaches[$coach['user_id']] = $coach;
  409. }
  410. $sql_session_coach = "SELECT session.id_coach, u.id as user_id, lastname, firstname, MAX(login_date) as login_date
  411. FROM $tbl_user u , $tbl_sessions as session, $tbl_track_login
  412. WHERE id_coach = u.id AND login_user_id = u.id
  413. GROUP BY u.id
  414. ORDER BY login_date $tracking_direction";
  415. if (api_is_multiple_url_enabled()) {
  416. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  417. $access_url_id = api_get_current_access_url_id();
  418. if ($access_url_id != -1) {
  419. $sql_session_coach = "SELECT session.id_coach, u.id as user_id, lastname, firstname, MAX(login_date) as login_date
  420. FROM $tbl_user u , $tbl_sessions as session, $tbl_track_login , $tbl_session_rel_access_url as session_rel_url
  421. WHERE
  422. id_coach = u.id AND
  423. login_user_id = u.id AND
  424. access_url_id = $access_url_id AND
  425. session_rel_url.session_id = session.id
  426. GROUP BY u.id
  427. ORDER BY login_date $tracking_direction";
  428. }
  429. }
  430. $result_sessions_coach = Database::query($sql_session_coach);
  431. //$total_no_coaches += Database::num_rows($result_sessions_coach);
  432. while ($coach = Database::fetch_array($result_sessions_coach)) {
  433. $global_coaches[$coach['user_id']] = $coach;
  434. }
  435. $all_datas = array();
  436. foreach ($global_coaches as $id_coach => $coaches) {
  437. $time_on_platform = api_time_to_hms(Tracking::get_time_spent_on_the_platform($coaches['user_id']));
  438. $last_connection = Tracking::get_last_connection_date($coaches['user_id']);
  439. $nb_students = count(Tracking::get_student_followed_by_coach($coaches['user_id']));
  440. $nb_courses = count(Tracking::get_courses_followed_by_coach($coaches['user_id']));
  441. $nb_sessions = count(Tracking::get_sessions_coached_by_user($coaches['user_id']));
  442. $table_row = array();
  443. if ($is_western_name_order) {
  444. $table_row[] = $coaches['firstname'];
  445. $table_row[] = $coaches['lastname'];
  446. } else {
  447. $table_row[] = $coaches['lastname'];
  448. $table_row[] = $coaches['firstname'];
  449. }
  450. $table_row[] = $time_on_platform;
  451. $table_row[] = $last_connection;
  452. $table_row[] = $nb_students;
  453. $table_row[] = $nb_courses;
  454. $table_row[] = $nb_sessions;
  455. $table_row[] = '<a href="session.php?id_coach='.$coaches['user_id'].'">
  456. '.Display::return_icon('2rightarrow.png').'
  457. </a>';
  458. $all_datas[] = $table_row;
  459. if ($is_western_name_order) {
  460. $csv_content[] = array(
  461. api_html_entity_decode($coaches['firstname'], ENT_QUOTES),
  462. api_html_entity_decode($coaches['lastname'], ENT_QUOTES),
  463. $time_on_platform,
  464. $last_connection,
  465. $nb_students,
  466. $nb_courses,
  467. $nb_sessions
  468. );
  469. } else {
  470. $csv_content[] = array(
  471. api_html_entity_decode($coaches['lastname'], ENT_QUOTES),
  472. api_html_entity_decode($coaches['firstname'], ENT_QUOTES),
  473. $time_on_platform,
  474. $last_connection,
  475. $nb_students,
  476. $nb_courses,
  477. $nb_sessions
  478. );
  479. }
  480. }
  481. if ($tracking_column != 3) {
  482. if ($tracking_direction == 'DESC') {
  483. usort($all_datas, array('MySpace', 'rsort_users'));
  484. } else {
  485. usort($all_datas, array('MySpace', 'sort_users'));
  486. }
  487. }
  488. if ($export_csv && $tracking_column != 3) {
  489. usort($csv_content, 'sort_users');
  490. }
  491. if ($export_csv) {
  492. $csv_content = array_merge($csv_header, $csv_content);
  493. }
  494. foreach ($all_datas as $row) {
  495. $table -> addRow($row, 'align="right"');
  496. }
  497. $table -> display();
  498. }
  499. public static function count_coaches()
  500. {
  501. global $total_no_coaches;
  502. return $total_no_coaches;
  503. }
  504. public static function sort_users($a, $b)
  505. {
  506. return api_strcmp(trim(api_strtolower($a[$_SESSION['tracking_column']])), trim(api_strtolower($b[$_SESSION['tracking_column']])));
  507. }
  508. public static function rsort_users($a, $b)
  509. {
  510. return api_strcmp(trim(api_strtolower($b[$_SESSION['tracking_column']])), trim(api_strtolower($a[$_SESSION['tracking_column']])));
  511. }
  512. /**
  513. * Display a sortable table that contains an overview off all the progress of the user in a session
  514. * @author César Perales <cesar.perales@beeznest.com>, Beeznest Team
  515. */
  516. public static function display_tracking_lp_progress_overview($sessionId = '', $courseId = '', $date_from, $date_to)
  517. {
  518. $course = api_get_course_info_by_id($courseId);
  519. /**
  520. * Column name
  521. * The order is important you need to check the $column variable in the model.ajax.php file
  522. */
  523. $columns = array(
  524. get_lang('Username'),
  525. get_lang('FirstName'),
  526. get_lang('LastName'),
  527. );
  528. //add lessons of course
  529. $lessons = LearnpathList::get_course_lessons($course['code'], $sessionId);
  530. //create columns array
  531. foreach ($lessons as $lesson_id => $lesson) {
  532. $columns[] = $lesson['name'];
  533. }
  534. $columns[] = get_lang('Total');
  535. /**
  536. * Column config
  537. */
  538. $column_model = array(
  539. array(
  540. 'name' => 'username',
  541. 'index' => 'username',
  542. 'align' => 'left',
  543. 'search' => 'true',
  544. 'wrap_cell' => "true",
  545. ),
  546. array(
  547. 'name' => 'firstname',
  548. 'index' => 'firstname',
  549. 'align' => 'left',
  550. 'search' => 'true',
  551. ),
  552. array(
  553. 'name' => 'lastname',
  554. 'index' => 'lastname',
  555. 'align' => 'left',
  556. 'search' => 'true',
  557. ),
  558. );
  559. // Get dinamic column names
  560. foreach ($lessons as $lesson_id => $lesson) {
  561. $column_model[] = array(
  562. 'name' => $lesson['id'],
  563. 'index' => $lesson['id'],
  564. 'align' => 'left',
  565. 'search' => 'true',
  566. );
  567. }
  568. $column_model[] = array(
  569. 'name' => 'total',
  570. 'index' => 'total',
  571. 'align' => 'left',
  572. 'search' => 'true',
  573. );
  574. $action_links = '';
  575. // jqgrid will use this URL to do the selects
  576. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_session_lp_progress&session_id='.$sessionId.'&course_id='.$courseId.'&date_to='.$date_to.'&date_from='.$date_from;
  577. //Table Id
  578. $tableId = 'lpProgress';
  579. //Autowidth
  580. $extra_params['autowidth'] = 'true';
  581. //height auto
  582. $extra_params['height'] = 'auto';
  583. $table = Display::grid_js(
  584. $tableId,
  585. $url,
  586. $columns,
  587. $column_model,
  588. $extra_params,
  589. array(),
  590. $action_links,
  591. true
  592. );
  593. $return = '<script>$(function() {'.$table.
  594. 'jQuery("#'.$tableId.'").jqGrid("navGrid","#'.$tableId.'_pager",{view:false, edit:false, add:false, del:false, search:false, excel:true});
  595. jQuery("#'.$tableId.'").jqGrid("navButtonAdd","#'.$tableId.'_pager",{
  596. caption:"",
  597. title:"' . get_lang('ExportExcel').'",
  598. onClickButton : function () {
  599. jQuery("#'.$tableId.'").jqGrid("excelExport",{"url":"'.$url.'&export_format=xls"});
  600. }
  601. });
  602. });</script>';
  603. $return .= Display::grid_html($tableId);
  604. return $return;
  605. }
  606. /**
  607. * Display a sortable table that contains an overview off all the progress of the user in a session
  608. * @param int $sessionId The session ID
  609. * @param int $courseId The course ID
  610. * @param int $exerciseId The quiz ID
  611. * @param int $answer Answer status (0 = incorrect, 1 = correct, 2 = both)
  612. * @return string HTML array of results formatted for gridJS
  613. * @author César Perales <cesar.perales@beeznest.com>, Beeznest Team
  614. */
  615. public static function display_tracking_exercise_progress_overview(
  616. $sessionId = 0,
  617. $courseId = 0,
  618. $exerciseId = 0,
  619. $date_from = null,
  620. $date_to = null
  621. ) {
  622. $date_from = Security::remove_XSS($date_from);
  623. $date_to = Security::remove_XSS($date_to);
  624. /**
  625. * Column names
  626. * The column order is important. Check $column variable in the main/inc/ajax/model.ajax.php file
  627. */
  628. $columns = array(
  629. get_lang('Session'),
  630. get_lang('ExerciseId'),
  631. get_lang('ExerciseName'),
  632. get_lang('Username'),
  633. get_lang('LastName'),
  634. get_lang('FirstName'),
  635. get_lang('Time'),
  636. get_lang('QuestionId'),
  637. get_lang('QuestionTitle'),
  638. get_lang('WorkDescription'),
  639. get_lang('Answer'),
  640. get_lang('Correct')
  641. );
  642. /**
  643. * Column config
  644. */
  645. $column_model = array(
  646. array('name'=>'session', 'index'=>'session', 'align'=>'left', 'search' => 'true', 'wrap_cell' => "true"),
  647. array('name'=>'exercise_id', 'index'=>'exercise_id', 'align'=>'left', 'search' => 'true'),
  648. array('name'=>'quiz_title', 'index'=>'quiz_title', 'align'=>'left', 'search' => 'true'),
  649. array('name'=>'username', 'index'=>'username', 'align'=>'left', 'search' => 'true'),
  650. array('name'=>'lastname', 'index'=>'lastname', 'align'=>'left', 'search' => 'true'),
  651. array('name'=>'firstname', 'index'=>'firstname', 'align'=>'left', 'search' => 'true'),
  652. array('name'=>'time', 'index'=>'time', 'align'=>'left', 'search' => 'true', 'wrap_cell' => "true"),
  653. array('name'=>'question_id', 'index'=>'question_id', 'align'=>'left', 'search' => 'true'),
  654. array('name'=>'question', 'index'=>'question', 'align'=>'left', 'search' => 'true', 'wrap_cell' => "true"),
  655. array('name'=>'description', 'index'=>'description', 'align'=>'left', 'width' => '550', 'search' => 'true', 'wrap_cell' => "true"),
  656. array('name'=>'answer', 'index'=>'answer', 'align'=>'left', 'search' => 'true', 'wrap_cell' => "true"),
  657. array('name'=>'correct', 'index'=>'correct', 'align'=>'left', 'search' => 'true', 'wrap_cell' => "true"),
  658. );
  659. //get dynamic column names
  660. // jqgrid will use this URL to do the selects
  661. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_exercise_progress&session_id='.$sessionId.'&course_id='.$courseId.'&exercise_id='.$exerciseId.'&date_to='.$date_to.'&date_from='.$date_from;
  662. // Autowidth
  663. $extra_params['autowidth'] = 'true';
  664. // height auto
  665. $extra_params['height'] = 'auto';
  666. $tableId = 'exerciseProgressOverview';
  667. $table = Display::grid_js($tableId, $url, $columns, $column_model, $extra_params, array(), '', true);
  668. $return = '<script>$(function() {'.$table.
  669. 'jQuery("#'.$tableId.'").jqGrid("navGrid","#'.$tableId.'_pager",{view:false, edit:false, add:false, del:false, search:false, excel:true});
  670. jQuery("#'.$tableId.'").jqGrid("navButtonAdd","#'.$tableId.'_pager",{
  671. caption:"",
  672. title:"' . get_lang('ExportExcel').'",
  673. onClickButton : function () {
  674. jQuery("#'.$tableId.'").jqGrid("excelExport",{"url":"'.$url.'&export_format=xls"});
  675. }
  676. });
  677. });</script>';
  678. $return .= Display::grid_html($tableId);
  679. return $return;
  680. }
  681. /**
  682. * Displays a form with all the additionally defined user fields of the profile
  683. * and give you the opportunity to include these in the CSV export
  684. *
  685. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  686. * @version 1.8.6
  687. * @since November 2008
  688. */
  689. public static function display_user_overview_export_options()
  690. {
  691. $message = '';
  692. $defaults = [];
  693. // include the user manager and formvalidator library
  694. if (isset($_GET['export']) && $_GET['export'] == 'options') {
  695. // get all the defined extra fields
  696. $extrafields = UserManager::get_extra_fields(
  697. 0,
  698. 50,
  699. 5,
  700. 'ASC',
  701. false,
  702. 1
  703. );
  704. // creating the form with all the defined extra fields
  705. $form = new FormValidator(
  706. 'exportextrafields',
  707. 'post',
  708. api_get_self()."?view=".Security::remove_XSS($_GET['view']).'&display='.Security::remove_XSS($_GET['display']).'&export='.Security::remove_XSS($_GET['export'])
  709. );
  710. if (is_array($extrafields) && count($extrafields) > 0) {
  711. foreach ($extrafields as $key => $extra) {
  712. $form->addElement('checkbox', 'extra_export_field'.$extra[0], '', $extra[3]);
  713. }
  714. $form->addButtonSave(get_lang('Ok'), 'submit');
  715. // setting the default values for the form that contains all the extra fields
  716. if (is_array($_SESSION['additional_export_fields'])) {
  717. foreach ($_SESSION['additional_export_fields'] as $key => $value) {
  718. $defaults['extra_export_field'.$value] = 1;
  719. }
  720. }
  721. $form->setDefaults($defaults);
  722. } else {
  723. $form->addElement('html', Display::return_message(get_lang('ThereAreNotExtrafieldsAvailable'), 'warning'));
  724. }
  725. if ($form->validate()) {
  726. // exporting the form values
  727. $values = $form->exportValues();
  728. // re-initialising the session that contains the additional fields that need to be exported
  729. $_SESSION['additional_export_fields'] = array();
  730. // adding the fields that are checked to the session
  731. $message = '';
  732. foreach ($values as $field_ids => $value) {
  733. if ($value == 1 && strstr($field_ids, 'extra_export_field')) {
  734. $_SESSION['additional_export_fields'][] = str_replace('extra_export_field', '', $field_ids);
  735. }
  736. }
  737. // adding the fields that will be also exported to a message string
  738. if (is_array($_SESSION['additional_export_fields'])) {
  739. foreach ($_SESSION['additional_export_fields'] as $key => $extra_field_export) {
  740. $message .= '<li>'.$extrafields[$extra_field_export][3].'</li>';
  741. }
  742. }
  743. // Displaying a feedback message
  744. if (!empty($_SESSION['additional_export_fields'])) {
  745. echo Display::return_message(get_lang('FollowingFieldsWillAlsoBeExported').': <br /><ul>'.$message.'</ul>', 'confirm', false);
  746. } else {
  747. echo Display::return_message(get_lang('NoAdditionalFieldsWillBeExported'), 'confirm', false);
  748. }
  749. } else {
  750. $form->display();
  751. }
  752. } else {
  753. if (!empty($_SESSION['additional_export_fields'])) {
  754. // get all the defined extra fields
  755. $extrafields = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  756. foreach ($_SESSION['additional_export_fields'] as $key => $extra_field_export) {
  757. $message .= '<li>'.$extrafields[$extra_field_export][3].'</li>';
  758. }
  759. echo Display::return_message(get_lang('FollowingFieldsWillAlsoBeExported').': <br /><ul>'.$message.'</ul>', 'normal', false);
  760. }
  761. }
  762. }
  763. /**
  764. * Display a sortable table that contains an overview of all the reporting progress of all courses
  765. */
  766. public static function display_tracking_course_overview()
  767. {
  768. $t_head = '<table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  769. $t_head .= '<tr>';
  770. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
  771. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
  772. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
  773. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
  774. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
  775. $t_head .= '<th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
  776. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
  777. $t_head .= '<th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
  778. $t_head .= '</tr></table>';
  779. $addparams = array('view' => 'admin', 'display' => 'courseoverview');
  780. $table = new SortableTable(
  781. 'tracking_session_overview',
  782. array('MySpace', 'get_total_number_courses'),
  783. array('MySpace', 'get_course_data_tracking_overview'),
  784. 1
  785. );
  786. $table->additional_parameters = $addparams;
  787. $table->set_header(0, '', false, null, array('style' => 'display: none'));
  788. $table->set_header(1, get_lang('Course'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  789. $table->set_header(2, $t_head, false, array('style' => 'width:90%;border:0;padding:0;font-size:7.5pt;'), array('style' => 'width:90%;padding:0;font-size:7.5pt;'));
  790. $table->set_column_filter(2, array('MySpace', 'course_tracking_filter'));
  791. $table->display();
  792. }
  793. /**
  794. * Get the total number of courses
  795. *
  796. * @return integer Total number of courses
  797. */
  798. public static function get_total_number_courses()
  799. {
  800. return CourseManager::count_courses(api_get_current_access_url_id());
  801. }
  802. /**
  803. * Get data for the courses
  804. *
  805. * @param int $from Inferior limit
  806. * @param int $numberItems Number of items to select
  807. * @param string $column Column to order on
  808. * @param string $direction Order direction
  809. * @return array Results
  810. */
  811. public static function get_course_data_tracking_overview($from, $numberItems, $column, $direction)
  812. {
  813. $courses = CourseManager::get_courses_list(
  814. $from,
  815. $numberItems,
  816. $column,
  817. $direction,
  818. -1,
  819. '',
  820. api_get_current_access_url_id()
  821. );
  822. $list = [];
  823. foreach ($courses as $course) {
  824. $list[] = [
  825. '0' => $course['code'],
  826. 'col0' => $course['code'],
  827. '1' => $course['title'],
  828. 'col1' => $course['title']
  829. ];
  830. }
  831. return $list;
  832. }
  833. /**
  834. * Fills in course reporting data
  835. *
  836. * @param integer course code
  837. * @param array $url_params additional url parameters
  838. * @param array $row the row information (the other columns)
  839. * @return string html code
  840. */
  841. public static function course_tracking_filter($course_code, $url_params, $row)
  842. {
  843. $course_code = $row[0];
  844. $courseInfo = api_get_course_info($course_code);
  845. $courseId = $courseInfo['real_id'];
  846. // the table header
  847. $return = '<table class="data_table" style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  848. // database table definition
  849. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  850. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  851. // getting all the courses of the user
  852. $sql = "SELECT *
  853. FROM $tbl_user AS u
  854. INNER JOIN $tbl_course_rel_user AS cu
  855. ON cu.user_id = u.user_id
  856. WHERE cu.c_id = '".$courseId."'";
  857. $result = Database::query($sql);
  858. $time_spent = 0;
  859. $progress = 0;
  860. $nb_progress_lp = 0;
  861. $score = 0;
  862. $nb_score_lp = 0;
  863. $nb_messages = 0;
  864. $nb_assignments = 0;
  865. $last_login_date = false;
  866. $total_score_obtained = 0;
  867. $total_score_possible = 0;
  868. $total_questions_answered = 0;
  869. while ($row = Database::fetch_object($result)) {
  870. // get time spent in the course and session
  871. $time_spent += Tracking::get_time_spent_on_the_course($row->user_id, $courseInfo['real_id']);
  872. $progress_tmp = Tracking::get_avg_student_progress($row->user_id, $course_code, array(), null, true);
  873. $progress += $progress_tmp[0];
  874. $nb_progress_lp += $progress_tmp[1];
  875. $score_tmp = Tracking::get_avg_student_score($row->user_id, $course_code, array(), null, true);
  876. if (is_array($score_tmp)) {
  877. $score += $score_tmp[0];
  878. $nb_score_lp += $score_tmp[1];
  879. }
  880. $nb_messages += Tracking::count_student_messages($row->user_id, $course_code);
  881. $nb_assignments += Tracking::count_student_assignments($row->user_id, $course_code);
  882. $last_login_date_tmp = Tracking::get_last_connection_date_on_the_course($row->user_id, $courseInfo, null, false);
  883. if ($last_login_date_tmp != false && $last_login_date == false) { // TODO: To be cleaned
  884. $last_login_date = $last_login_date_tmp;
  885. } else if ($last_login_date_tmp != false && $last_login_date != false) { // TODO: Repeated previous condition. To be cleaned.
  886. // Find the max and assign it to first_login_date
  887. if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
  888. $last_login_date = $last_login_date_tmp;
  889. }
  890. }
  891. $exercise_results_tmp = self::exercises_results($row->user_id, $course_code);
  892. $total_score_obtained += $exercise_results_tmp['score_obtained'];
  893. $total_score_possible += $exercise_results_tmp['score_possible'];
  894. $total_questions_answered += $exercise_results_tmp['questions_answered'];
  895. }
  896. if ($nb_progress_lp > 0) {
  897. $avg_progress = round($progress / $nb_progress_lp, 2);
  898. } else {
  899. $avg_progress = 0;
  900. }
  901. if ($nb_score_lp > 0) {
  902. $avg_score = round($score / $nb_score_lp, 2);
  903. } else {
  904. $avg_score = '-';
  905. }
  906. if ($last_login_date) {
  907. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  908. } else {
  909. $last_login_date = '-';
  910. }
  911. if ($total_score_possible > 0) {
  912. $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
  913. } else {
  914. $total_score_percentage = 0;
  915. }
  916. if ($total_score_percentage > 0) {
  917. $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
  918. } else {
  919. $total_score = '-';
  920. }
  921. $return .= '<tr>';
  922. // time spent in the course
  923. $return .= ' <td style="width:164px;">'.api_time_to_hms($time_spent).'</td>';
  924. // student progress in course
  925. $return .= ' <td>'.$avg_progress.'</td>';
  926. // student score
  927. $return .= ' <td>'.$avg_score.'</td>';
  928. // student messages
  929. $return .= ' <td>'.$nb_messages.'</td>';
  930. // student assignments
  931. $return .= ' <td>'.$nb_assignments.'</td>';
  932. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  933. $return .= '<td width="105px;">'.$total_score.'</td>';
  934. $return .= '<td>'.$total_questions_answered.'</td>';
  935. // last connection
  936. $return .= ' <td>'.$last_login_date.'</td>';
  937. $return .= '</tr>';
  938. $return .= '</table>';
  939. return $return;
  940. }
  941. /**
  942. * This function exports the table that we see in display_tracking_course_overview()
  943. *
  944. */
  945. public static function export_tracking_course_overview()
  946. {
  947. // database table definition
  948. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  949. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  950. // the values of the sortable table
  951. if ($_GET['tracking_course_overview_page_nr']) {
  952. $from = $_GET['tracking_course_overview_page_nr'];
  953. } else {
  954. $from = 0;
  955. }
  956. if ($_GET['tracking_course_overview_column']) {
  957. $orderby = $_GET['tracking_course_overview_column'];
  958. } else {
  959. $orderby = 0;
  960. }
  961. if ($_GET['tracking_course_overview_direction']) {
  962. $direction = $_GET['tracking_course_overview_direction'];
  963. } else {
  964. $direction = 'ASC';
  965. }
  966. $course_data = self::get_course_data_tracking_overview(
  967. $from,
  968. 1000,
  969. $orderby,
  970. $direction
  971. );
  972. $csv_content = array();
  973. // the first line of the csv file with the column headers
  974. $csv_row = array();
  975. $csv_row[] = get_lang('Course');
  976. $csv_row[] = get_lang('AvgTimeSpentInTheCourse');
  977. $csv_row[] = get_lang('AvgStudentsProgress');
  978. $csv_row[] = get_lang('AvgCourseScore');
  979. $csv_row[] = get_lang('TotalNumberOfMessages');
  980. $csv_row[] = get_lang('TotalNumberOfAssignments');
  981. $csv_row[] = get_lang('TotalExercisesScoreObtained');
  982. $csv_row[] = get_lang('TotalExercisesScorePossible');
  983. $csv_row[] = get_lang('TotalExercisesAnswered');
  984. $csv_row[] = get_lang('TotalExercisesScorePercentage');
  985. $csv_row[] = get_lang('LatestLogin');
  986. $csv_content[] = $csv_row;
  987. // the other lines (the data)
  988. foreach ($course_data as $key => $course) {
  989. $course_code = $course[0];
  990. $courseInfo = api_get_course_info($course_code);
  991. $course_title = $courseInfo['title'];
  992. $courseId = $courseInfo['real_id'];
  993. $csv_row = array();
  994. $csv_row[] = $course_title;
  995. // getting all the courses of the session
  996. $sql = "SELECT *
  997. FROM $tbl_user AS u
  998. INNER JOIN $tbl_course_rel_user AS cu
  999. ON cu.user_id = u.user_id
  1000. WHERE cu.c_id = '".$courseId."'";
  1001. $result = Database::query($sql);
  1002. $time_spent = 0;
  1003. $progress = 0;
  1004. $nb_progress_lp = 0;
  1005. $score = 0;
  1006. $nb_score_lp = 0;
  1007. $nb_messages = 0;
  1008. $nb_assignments = 0;
  1009. $last_login_date = false;
  1010. $total_score_obtained = 0;
  1011. $total_score_possible = 0;
  1012. $total_questions_answered = 0;
  1013. while ($row = Database::fetch_object($result)) {
  1014. // get time spent in the course and session
  1015. $time_spent += Tracking::get_time_spent_on_the_course($row->user_id, $courseId);
  1016. $progress_tmp = Tracking::get_avg_student_progress($row->user_id, $course_code, array(), null, true);
  1017. $progress += $progress_tmp[0];
  1018. $nb_progress_lp += $progress_tmp[1];
  1019. $score_tmp = Tracking::get_avg_student_score($row->user_id, $course_code, array(), null, true);
  1020. if (is_array($score_tmp)) {
  1021. $score += $score_tmp[0];
  1022. $nb_score_lp += $score_tmp[1];
  1023. }
  1024. $nb_messages += Tracking::count_student_messages($row->user_id, $course_code);
  1025. $nb_assignments += Tracking::count_student_assignments($row->user_id, $course_code);
  1026. $last_login_date_tmp = Tracking::get_last_connection_date_on_the_course($row->user_id, $courseInfo, null, false);
  1027. if ($last_login_date_tmp != false && $last_login_date == false) { // TODO: To be cleaned.
  1028. $last_login_date = $last_login_date_tmp;
  1029. } else if ($last_login_date_tmp != false && $last_login_date == false) { // TODO: Repeated previous condition. To be cleaned.
  1030. // Find the max and assign it to first_login_date
  1031. if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
  1032. $last_login_date = $last_login_date_tmp;
  1033. }
  1034. }
  1035. $exercise_results_tmp = self::exercises_results($row->user_id, $course_code);
  1036. $total_score_obtained += $exercise_results_tmp['score_obtained'];
  1037. $total_score_possible += $exercise_results_tmp['score_possible'];
  1038. $total_questions_answered += $exercise_results_tmp['questions_answered'];
  1039. }
  1040. if ($nb_progress_lp > 0) {
  1041. $avg_progress = round($progress / $nb_progress_lp, 2);
  1042. } else {
  1043. $avg_progress = 0;
  1044. }
  1045. if ($nb_score_lp > 0) {
  1046. $avg_score = round($score / $nb_score_lp, 2);
  1047. } else {
  1048. $avg_score = '-';
  1049. }
  1050. if ($last_login_date) {
  1051. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  1052. } else {
  1053. $last_login_date = '-';
  1054. }
  1055. if ($total_score_possible > 0) {
  1056. $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
  1057. } else {
  1058. $total_score_percentage = 0;
  1059. }
  1060. // time spent in the course
  1061. $csv_row[] = api_time_to_hms($time_spent);
  1062. // student progress in course
  1063. $csv_row[] = $avg_progress;
  1064. // student score
  1065. $csv_row[] = $avg_score;
  1066. // student messages
  1067. $csv_row[] = $nb_messages;
  1068. // student assignments
  1069. $csv_row[] = $nb_assignments;
  1070. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  1071. $csv_row[] = $total_score_obtained;
  1072. $csv_row[] = $total_score_possible;
  1073. $csv_row[] = $total_questions_answered;
  1074. $csv_row[] = $total_score_percentage;
  1075. // last connection
  1076. $csv_row[] = $last_login_date;
  1077. $csv_content[] = $csv_row;
  1078. }
  1079. Export::arrayToCsv($csv_content, 'reporting_course_overview');
  1080. exit;
  1081. }
  1082. /**
  1083. * Display a sortable table that contains an overview of all the reporting
  1084. * progress of all sessions and all courses the user is subscribed to
  1085. * @author Guillaume Viguier <guillaume@viguierjust.com>
  1086. */
  1087. public static function display_tracking_session_overview()
  1088. {
  1089. $t_head = '<table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  1090. $t_head .= '<tr>';
  1091. $t_head .= '<th width="155px" style="border-left:0;border-bottom:0"><span>'.get_lang('Course').'</span></th>';
  1092. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
  1093. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
  1094. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
  1095. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
  1096. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
  1097. $t_head .= '<th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
  1098. $t_head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
  1099. $t_head .= '<th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
  1100. $t_head .= '</tr></table>';
  1101. $addparams = array('view' => 'admin', 'display' => 'sessionoverview');
  1102. $table = new SortableTable(
  1103. 'tracking_session_overview',
  1104. array('MySpace', 'get_total_number_sessions'),
  1105. array('MySpace', 'get_session_data_tracking_overview'),
  1106. 1
  1107. );
  1108. $table->additional_parameters = $addparams;
  1109. $table->set_header(0, '', false, null, array('style' => 'display: none'));
  1110. $table->set_header(1, get_lang('Session'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  1111. $table->set_header(2, $t_head, false, array('style' => 'width:90%;border:0;padding:0;font-size:7.5pt;'), array('style' => 'width:90%;padding:0;font-size:7.5pt;'));
  1112. $table->set_column_filter(2, array('MySpace', 'session_tracking_filter'));
  1113. $table->display();
  1114. }
  1115. /**
  1116. * Get the total number of sessions
  1117. *
  1118. * @return integer Total number of sessions
  1119. */
  1120. public static function get_total_number_sessions()
  1121. {
  1122. return SessionManager::count_sessions(api_get_current_access_url_id());
  1123. }
  1124. /**
  1125. * Get data for the sessions
  1126. *
  1127. * @param int $from Inferior limit
  1128. * @param int $numberItems Number of items to select
  1129. * @param string $column Column to order on
  1130. * @param string $direction Order direction
  1131. * @return array Results
  1132. */
  1133. public static function get_session_data_tracking_overview($from, $numberItems, $column, $direction)
  1134. {
  1135. $from = (int) $from;
  1136. $numberItems = (int) $numberItems;
  1137. $direction = Database::escape_string($direction);
  1138. $columnName = 'name';
  1139. if ($column === 1) {
  1140. $columnName = 'id';
  1141. }
  1142. $options = [
  1143. 'order' => " $columnName $direction",
  1144. 'limit' => " $from,$numberItems"
  1145. ];
  1146. $sessions = SessionManager::get_sessions_admin($options);
  1147. $list = [];
  1148. foreach ($sessions as $session) {
  1149. $list[] = [
  1150. '0' => $session['id'],
  1151. 'col0' => $session['id'],
  1152. '1' => strip_tags($session['name']),
  1153. 'col1' => strip_tags($session['name'])
  1154. ];
  1155. }
  1156. return $list;
  1157. }
  1158. /**
  1159. * Fills in session reporting data
  1160. *
  1161. * @param integer $user_id the id of the user
  1162. * @param array $url_params additonal url parameters
  1163. * @param array $row the row information (the other columns)
  1164. * @return string html code
  1165. */
  1166. public static function session_tracking_filter($session_id, $url_params, $row)
  1167. {
  1168. $session_id = $row[0];
  1169. // the table header
  1170. $return = '<table class="data_table" style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  1171. // database table definition
  1172. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1173. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1174. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1175. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1176. // getting all the courses of the user
  1177. $sql = "SELECT * FROM $tbl_course AS c
  1178. INNER JOIN $tbl_session_rel_course AS sc
  1179. ON sc.c_id = c.id
  1180. WHERE sc.session_id = '".$session_id."'";
  1181. $result = Database::query($sql);
  1182. while ($row = Database::fetch_object($result)) {
  1183. $courseId = $row->c_id;
  1184. $courseInfo = api_get_course_info_by_id($courseId);
  1185. $return .= '<tr>';
  1186. // course code
  1187. $return .= ' <td width="157px" >'.$row->title.'</td>';
  1188. // get the users in the course
  1189. $sql = "SELECT u.user_id
  1190. FROM $tbl_user AS u
  1191. INNER JOIN $tbl_session_rel_course_rel_user AS scu
  1192. ON u.user_id = scu.user_id
  1193. WHERE scu.session_id = '".$session_id."' AND scu.c_id = '".$courseId."'";
  1194. $result_users = Database::query($sql);
  1195. $time_spent = 0;
  1196. $progress = 0;
  1197. $nb_progress_lp = 0;
  1198. $score = 0;
  1199. $nb_score_lp = 0;
  1200. $nb_messages = 0;
  1201. $nb_assignments = 0;
  1202. $last_login_date = false;
  1203. $total_score_obtained = 0;
  1204. $total_score_possible = 0;
  1205. $total_questions_answered = 0;
  1206. while ($row_user = Database::fetch_object($result_users)) {
  1207. // get time spent in the course and session
  1208. $time_spent += Tracking::get_time_spent_on_the_course($row_user->user_id, $courseId, $session_id);
  1209. $progress_tmp = Tracking::get_avg_student_progress($row_user->user_id, $row->code, array(), $session_id, true);
  1210. $progress += $progress_tmp[0];
  1211. $nb_progress_lp += $progress_tmp[1];
  1212. $score_tmp = Tracking::get_avg_student_score($row_user->user_id, $row->code, array(), $session_id, true);
  1213. if (is_array($score_tmp)) {
  1214. $score += $score_tmp[0];
  1215. $nb_score_lp += $score_tmp[1];
  1216. }
  1217. $nb_messages += Tracking::count_student_messages($row_user->user_id, $row->code, $session_id);
  1218. $nb_assignments += Tracking::count_student_assignments($row_user->user_id, $row->code, $session_id);
  1219. $last_login_date_tmp = Tracking::get_last_connection_date_on_the_course($row_user->user_id, $courseInfo, $session_id, false);
  1220. if ($last_login_date_tmp != false && $last_login_date == false) {
  1221. // TODO: To be cleaned.
  1222. $last_login_date = $last_login_date_tmp;
  1223. } else if ($last_login_date_tmp != false && $last_login_date != false) {
  1224. // TODO: Repeated previous condition! To be cleaned.
  1225. // Find the max and assign it to first_login_date
  1226. if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
  1227. $last_login_date = $last_login_date_tmp;
  1228. }
  1229. }
  1230. $exercise_results_tmp = self::exercises_results($row_user->user_id, $row->code, $session_id);
  1231. $total_score_obtained += $exercise_results_tmp['score_obtained'];
  1232. $total_score_possible += $exercise_results_tmp['score_possible'];
  1233. $total_questions_answered += $exercise_results_tmp['questions_answered'];
  1234. }
  1235. if ($nb_progress_lp > 0) {
  1236. $avg_progress = round($progress / $nb_progress_lp, 2);
  1237. } else {
  1238. $avg_progress = 0;
  1239. }
  1240. if ($nb_score_lp > 0) {
  1241. $avg_score = round($score / $nb_score_lp, 2);
  1242. } else {
  1243. $avg_score = '-';
  1244. }
  1245. if ($last_login_date) {
  1246. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  1247. } else {
  1248. $last_login_date = '-';
  1249. }
  1250. if ($total_score_possible > 0) {
  1251. $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
  1252. } else {
  1253. $total_score_percentage = 0;
  1254. }
  1255. if ($total_score_percentage > 0) {
  1256. $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
  1257. } else {
  1258. $total_score = '-';
  1259. }
  1260. // time spent in the course
  1261. $return .= ' <td><div>'.api_time_to_hms($time_spent).'</div></td>';
  1262. // student progress in course
  1263. $return .= ' <td><div>'.$avg_progress.'</div></td>';
  1264. // student score
  1265. $return .= ' <td><div>'.$avg_score.'</div></td>';
  1266. // student messages
  1267. $return .= ' <td><div>'.$nb_messages.'</div></td>';
  1268. // student assignments
  1269. $return .= ' <td><div>'.$nb_assignments.'</div></td>';
  1270. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  1271. $return .= '<td width="105px;">'.$total_score.'</td>';
  1272. $return .= '<td>'.$total_questions_answered.'</td>';
  1273. // last connection
  1274. $return .= ' <td><div>'.$last_login_date.'</div></td>';
  1275. $return .= '<tr>';
  1276. }
  1277. $return .= '</table>';
  1278. return $return;
  1279. }
  1280. /**
  1281. * This function exports the table that we see in display_tracking_session_overview()
  1282. *
  1283. */
  1284. public static function export_tracking_session_overview()
  1285. {
  1286. // database table definition
  1287. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1288. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1289. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1290. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  1291. // the values of the sortable table
  1292. if ($_GET['tracking_session_overview_page_nr']) {
  1293. $from = $_GET['tracking_session_overview_page_nr'];
  1294. } else {
  1295. $from = 0;
  1296. }
  1297. if ($_GET['tracking_session_overview_column']) {
  1298. $orderby = $_GET['tracking_session_overview_column'];
  1299. } else {
  1300. $orderby = 0;
  1301. }
  1302. if ($_GET['tracking_session_overview_direction']) {
  1303. $direction = $_GET['tracking_session_overview_direction'];
  1304. } else {
  1305. $direction = 'ASC';
  1306. }
  1307. $session_data = self::get_session_data_tracking_overview($from, 1000, $orderby, $direction);
  1308. $csv_content = array();
  1309. // the first line of the csv file with the column headers
  1310. $csv_row = array();
  1311. $csv_row[] = get_lang('Session');
  1312. $csv_row[] = get_lang('Course');
  1313. $csv_row[] = get_lang('AvgTimeSpentInTheCourse');
  1314. $csv_row[] = get_lang('AvgStudentsProgress');
  1315. $csv_row[] = get_lang('AvgCourseScore');
  1316. $csv_row[] = get_lang('TotalNumberOfMessages');
  1317. $csv_row[] = get_lang('TotalNumberOfAssignments');
  1318. $csv_row[] = get_lang('TotalExercisesScoreObtained');
  1319. $csv_row[] = get_lang('TotalExercisesScorePossible');
  1320. $csv_row[] = get_lang('TotalExercisesAnswered');
  1321. $csv_row[] = get_lang('TotalExercisesScorePercentage');
  1322. $csv_row[] = get_lang('LatestLogin');
  1323. $csv_content[] = $csv_row;
  1324. // the other lines (the data)
  1325. foreach ($session_data as $key => $session) {
  1326. $session_id = $session[0];
  1327. $session_title = $session[1];
  1328. // getting all the courses of the session
  1329. $sql = "SELECT * FROM $tbl_course AS c
  1330. INNER JOIN $tbl_session_rel_course AS sc
  1331. ON sc.c_id = c.id
  1332. WHERE sc.session_id = '".$session_id."';";
  1333. $result = Database::query($sql);
  1334. while ($row = Database::fetch_object($result)) {
  1335. $courseId = $row->c_id;
  1336. $courseInfo = api_get_course_info_by_id($courseId);
  1337. $csv_row = array();
  1338. $csv_row[] = $session_title;
  1339. $csv_row[] = $row->title;
  1340. // get the users in the course
  1341. $sql = "SELECT scu.user_id
  1342. FROM $tbl_user AS u
  1343. INNER JOIN $tbl_session_rel_course_rel_user AS scu
  1344. ON u.user_id = scu.user_id
  1345. WHERE scu.session_id = '".$session_id."' AND scu.c_id = '".$courseId."'";
  1346. $result_users = Database::query($sql);
  1347. $time_spent = 0;
  1348. $progress = 0;
  1349. $nb_progress_lp = 0;
  1350. $score = 0;
  1351. $nb_score_lp = 0;
  1352. $nb_messages = 0;
  1353. $nb_assignments = 0;
  1354. $last_login_date = false;
  1355. $total_score_obtained = 0;
  1356. $total_score_possible = 0;
  1357. $total_questions_answered = 0;
  1358. while ($row_user = Database::fetch_object($result_users)) {
  1359. // get time spent in the course and session
  1360. $time_spent += Tracking::get_time_spent_on_the_course($row_user->user_id, $courseId, $session_id);
  1361. $progress_tmp = Tracking::get_avg_student_progress($row_user->user_id, $row->code, array(), $session_id, true);
  1362. $progress += $progress_tmp[0];
  1363. $nb_progress_lp += $progress_tmp[1];
  1364. $score_tmp = Tracking::get_avg_student_score($row_user->user_id, $row->code, array(), $session_id, true);
  1365. if (is_array($score_tmp)) {
  1366. $score += $score_tmp[0];
  1367. $nb_score_lp += $score_tmp[1];
  1368. }
  1369. $nb_messages += Tracking::count_student_messages(
  1370. $row_user->user_id,
  1371. $row->code,
  1372. $session_id
  1373. );
  1374. $nb_assignments += Tracking::count_student_assignments(
  1375. $row_user->user_id,
  1376. $row->code,
  1377. $session_id
  1378. );
  1379. $last_login_date_tmp = Tracking:: get_last_connection_date_on_the_course(
  1380. $row_user->user_id,
  1381. $courseInfo,
  1382. $session_id,
  1383. false
  1384. );
  1385. if ($last_login_date_tmp != false && $last_login_date == false) { // TODO: To be cleaned.
  1386. $last_login_date = $last_login_date_tmp;
  1387. } else if ($last_login_date_tmp != false && $last_login_date == false) { // TODO: Repeated previous condition. To be cleaned.
  1388. // Find the max and assign it to first_login_date
  1389. if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
  1390. $last_login_date = $last_login_date_tmp;
  1391. }
  1392. }
  1393. $exercise_results_tmp = self::exercises_results($row_user->user_id, $row->code, $session_id);
  1394. $total_score_obtained += $exercise_results_tmp['score_obtained'];
  1395. $total_score_possible += $exercise_results_tmp['score_possible'];
  1396. $total_questions_answered += $exercise_results_tmp['questions_answered'];
  1397. }
  1398. if ($nb_progress_lp > 0) {
  1399. $avg_progress = round($progress / $nb_progress_lp, 2);
  1400. } else {
  1401. $avg_progress = 0;
  1402. }
  1403. if ($nb_score_lp > 0) {
  1404. $avg_score = round($score / $nb_score_lp, 2);
  1405. } else {
  1406. $avg_score = '-';
  1407. }
  1408. if ($last_login_date) {
  1409. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  1410. } else {
  1411. $last_login_date = '-';
  1412. }
  1413. if ($total_score_possible > 0) {
  1414. $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
  1415. } else {
  1416. $total_score_percentage = 0;
  1417. }
  1418. if ($total_score_percentage > 0) {
  1419. $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
  1420. } else {
  1421. $total_score = '-';
  1422. }
  1423. // time spent in the course
  1424. $csv_row[] = api_time_to_hms($time_spent);
  1425. // student progress in course
  1426. $csv_row[] = $avg_progress;
  1427. // student score
  1428. $csv_row[] = $avg_score;
  1429. // student messages
  1430. $csv_row[] = $nb_messages;
  1431. // student assignments
  1432. $csv_row[] = $nb_assignments;
  1433. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  1434. $csv_row[] = $total_score_obtained;
  1435. $csv_row[] = $total_score_possible;
  1436. $csv_row[] = $total_questions_answered;
  1437. $csv_row[] = $total_score_percentage;
  1438. // last connection
  1439. $csv_row[] = $last_login_date;
  1440. $csv_content[] = $csv_row;
  1441. }
  1442. }
  1443. Export::arrayToCsv($csv_content, 'reporting_session_overview');
  1444. exit;
  1445. }
  1446. /**
  1447. * Get general information about the exercise performance of the user
  1448. * the total obtained score (all the score on all the questions)
  1449. * the maximum score that could be obtained
  1450. * the number of questions answered
  1451. * the success percentage
  1452. * @param integer $user_id the id of the user
  1453. * @param string $course_code the course code
  1454. * @return array
  1455. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1456. * @version Dokeos 1.8.6
  1457. * @since November 2008
  1458. */
  1459. public static function exercises_results($user_id, $course_code, $session_id = false)
  1460. {
  1461. $courseId = api_get_course_int_id($course_code);
  1462. $sql = 'SELECT exe_result, exe_weighting
  1463. FROM '.Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES)."
  1464. WHERE
  1465. c_id = ' . $courseId . ' AND
  1466. exe_user_id = '".intval($user_id)."'";
  1467. if ($session_id !== false) {
  1468. $sql .= " AND session_id = '".$session_id."' ";
  1469. }
  1470. $result = Database::query($sql);
  1471. $score_obtained = 0;
  1472. $score_possible = 0;
  1473. $questions_answered = 0;
  1474. while ($row = Database::fetch_array($result)) {
  1475. $score_obtained += $row['exe_result'];
  1476. $score_possible += $row['exe_weighting'];
  1477. $questions_answered++;
  1478. }
  1479. if ($score_possible != 0) {
  1480. $percentage = round(($score_obtained / $score_possible * 100), 2);
  1481. } else {
  1482. $percentage = null;
  1483. }
  1484. return array(
  1485. 'score_obtained' => $score_obtained,
  1486. 'score_possible' => $score_possible,
  1487. 'questions_answered' => $questions_answered,
  1488. 'percentage' => $percentage
  1489. );
  1490. }
  1491. /**
  1492. * This function exports the table that we see in display_tracking_user_overview()
  1493. *
  1494. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1495. * @version Dokeos 1.8.6
  1496. * @since October 2008
  1497. */
  1498. public static function export_tracking_user_overview()
  1499. {
  1500. // database table definitions
  1501. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1502. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  1503. // the values of the sortable table
  1504. if ($_GET['tracking_user_overview_page_nr']) {
  1505. $from = $_GET['tracking_user_overview_page_nr'];
  1506. } else {
  1507. $from = 0;
  1508. }
  1509. if ($_GET['tracking_user_overview_column']) {
  1510. $orderby = $_GET['tracking_user_overview_column'];
  1511. } else {
  1512. $orderby = 0;
  1513. }
  1514. if ($is_western_name_order != api_is_western_name_order() && ($orderby == 1 || $orderby == 2)) {
  1515. // Swapping the sorting column if name order for export is different than the common name order.
  1516. $orderby = 3 - $orderby;
  1517. }
  1518. if ($_GET['tracking_user_overview_direction']) {
  1519. $direction = $_GET['tracking_user_overview_direction'];
  1520. } else {
  1521. $direction = 'ASC';
  1522. }
  1523. $user_data = self::get_user_data_tracking_overview($from, 1000, $orderby, $direction);
  1524. // the first line of the csv file with the column headers
  1525. $csv_row = array();
  1526. $csv_row[] = get_lang('OfficialCode');
  1527. if ($is_western_name_order) {
  1528. $csv_row[] = get_lang('FirstName', '');
  1529. $csv_row[] = get_lang('LastName', '');
  1530. } else {
  1531. $csv_row[] = get_lang('LastName', '');
  1532. $csv_row[] = get_lang('FirstName', '');
  1533. }
  1534. $csv_row[] = get_lang('LoginName');
  1535. $csv_row[] = get_lang('CourseCode');
  1536. // the additional user defined fields (only those that were selected to be exported)
  1537. $fields = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  1538. if (is_array($_SESSION['additional_export_fields'])) {
  1539. foreach ($_SESSION['additional_export_fields'] as $key => $extra_field_export) {
  1540. $csv_row[] = $fields[$extra_field_export][3];
  1541. $field_names_to_be_exported[] = 'extra_'.$fields[$extra_field_export][1];
  1542. }
  1543. }
  1544. $csv_row[] = get_lang('AvgTimeSpentInTheCourse', '');
  1545. $csv_row[] = get_lang('AvgStudentsProgress', '');
  1546. $csv_row[] = get_lang('AvgCourseScore', '');
  1547. $csv_row[] = get_lang('AvgExercisesScore', '');
  1548. $csv_row[] = get_lang('AvgMessages', '');
  1549. $csv_row[] = get_lang('AvgAssignments', '');
  1550. $csv_row[] = get_lang('TotalExercisesScoreObtained', '');
  1551. $csv_row[] = get_lang('TotalExercisesScorePossible', '');
  1552. $csv_row[] = get_lang('TotalExercisesAnswered', '');
  1553. $csv_row[] = get_lang('TotalExercisesScorePercentage', '');
  1554. $csv_row[] = get_lang('FirstLogin', '');
  1555. $csv_row[] = get_lang('LatestLogin', '');
  1556. $csv_content[] = $csv_row;
  1557. // the other lines (the data)
  1558. foreach ($user_data as $key => $user) {
  1559. // getting all the courses of the user
  1560. $sql = "SELECT * FROM $tbl_course_user
  1561. WHERE user_id = '".intval($user[4])."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
  1562. $result = Database::query($sql);
  1563. while ($row = Database::fetch_row($result)) {
  1564. $courseInfo = api_get_course_info($row['course_code']);
  1565. $courseId = $courseInfo['real_id'];
  1566. $csv_row = array();
  1567. // user official code
  1568. $csv_row[] = $user[0];
  1569. // user first|last name
  1570. $csv_row[] = $user[1];
  1571. // user last|first name
  1572. $csv_row[] = $user[2];
  1573. // user login name
  1574. $csv_row[] = $user[3];
  1575. // course code
  1576. $csv_row[] = $row[0];
  1577. // the additional defined user fields
  1578. $extra_fields = self::get_user_overview_export_extra_fields($user[4]);
  1579. if (is_array($field_names_to_be_exported)) {
  1580. foreach ($field_names_to_be_exported as $key => $extra_field_export) {
  1581. $csv_row[] = $extra_fields[$extra_field_export];
  1582. }
  1583. }
  1584. // time spent in the course
  1585. $csv_row[] = api_time_to_hms(Tracking::get_time_spent_on_the_course($user[4], $courseId));
  1586. // student progress in course
  1587. $csv_row[] = round(Tracking::get_avg_student_progress($user[4], $row[0]), 2);
  1588. // student score
  1589. $csv_row[] = round(Tracking::get_avg_student_score($user[4], $row[0]), 2);
  1590. // student tes score
  1591. $csv_row[] = round(Tracking::get_avg_student_exercise_score($user[4], $row[0]), 2);
  1592. // student messages
  1593. $csv_row[] = Tracking::count_student_messages($user[4], $row[0]);
  1594. // student assignments
  1595. $csv_row[] = Tracking::count_student_assignments($user[4], $row[0]);
  1596. // student exercises results
  1597. $exercises_results = self::exercises_results($user[4], $row[0]);
  1598. $csv_row[] = $exercises_results['score_obtained'];
  1599. $csv_row[] = $exercises_results['score_possible'];
  1600. $csv_row[] = $exercises_results['questions_answered'];
  1601. $csv_row[] = $exercises_results['percentage'];
  1602. // first connection
  1603. $csv_row[] = Tracking::get_first_connection_date_on_the_course($user[4], $courseId);
  1604. // last connection
  1605. $csv_row[] = strip_tags(Tracking::get_last_connection_date_on_the_course($user[4], $courseInfo));
  1606. $csv_content[] = $csv_row;
  1607. }
  1608. }
  1609. Export::arrayToCsv($csv_content, 'reporting_user_overview');
  1610. exit;
  1611. }
  1612. /**
  1613. * Get data for courses list in sortable with pagination
  1614. * @return array
  1615. */
  1616. public static function get_course_data($from, $number_of_items, $column, $direction)
  1617. {
  1618. global $courses, $csv_content, $charset, $session_id;
  1619. // definition database tables
  1620. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1621. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1622. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1623. $course_data = array();
  1624. $courses_code = array_keys($courses);
  1625. foreach ($courses_code as &$code) {
  1626. $code = "'$code'";
  1627. }
  1628. // get all courses with limit
  1629. $sql = "SELECT course.code as col1, course.title as col2
  1630. FROM $tbl_course course
  1631. WHERE course.code IN (".implode(',', $courses_code).")";
  1632. if (!in_array($direction, array('ASC', 'DESC'))) $direction = 'ASC';
  1633. $column = intval($column);
  1634. $from = intval($from);
  1635. $number_of_items = intval($number_of_items);
  1636. $sql .= " ORDER BY col$column $direction ";
  1637. $sql .= " LIMIT $from,$number_of_items";
  1638. $res = Database::query($sql);
  1639. while ($row_course = Database::fetch_row($res)) {
  1640. $course_code = $row_course[0];
  1641. $courseInfo = api_get_course_info($course_code);
  1642. $courseId = $courseInfo['real_id'];
  1643. $avg_assignments_in_course = $avg_messages_in_course = $nb_students_in_course = $avg_progress_in_course = $avg_score_in_course = $avg_time_spent_in_course = $avg_score_in_exercise = 0;
  1644. // students directly subscribed to the course
  1645. if (empty($session_id)) {
  1646. $sql = "SELECT user_id
  1647. FROM $tbl_course_user as course_rel_user
  1648. WHERE
  1649. course_rel_user.status='5' AND
  1650. course_rel_user.c_id = '$courseId'";
  1651. } else {
  1652. $sql = "SELECT user_id FROM $tbl_session_course_user srcu
  1653. WHERE
  1654. c_id = '$courseId' AND
  1655. session_id = '$session_id' AND
  1656. status<>2";
  1657. }
  1658. $rs = Database::query($sql);
  1659. $users = array();
  1660. while ($row = Database::fetch_array($rs)) {
  1661. $users[] = $row['user_id'];
  1662. }
  1663. if (count($users) > 0) {
  1664. $nb_students_in_course = count($users);
  1665. $avg_assignments_in_course = Tracking::count_student_assignments($users, $course_code, $session_id);
  1666. $avg_messages_in_course = Tracking::count_student_messages($users, $course_code, $session_id);
  1667. $avg_progress_in_course = Tracking::get_avg_student_progress($users, $course_code, array(), $session_id);
  1668. $avg_score_in_course = Tracking::get_avg_student_score($users, $course_code, array(), $session_id);
  1669. $avg_score_in_exercise = Tracking::get_avg_student_exercise_score($users, $course_code, 0, $session_id);
  1670. $avg_time_spent_in_course = Tracking::get_time_spent_on_the_course($users, $courseInfo['real_id'], $session_id);
  1671. $avg_progress_in_course = round($avg_progress_in_course / $nb_students_in_course, 2);
  1672. if (is_numeric($avg_score_in_course)) {
  1673. $avg_score_in_course = round($avg_score_in_course / $nb_students_in_course, 2);
  1674. }
  1675. $avg_time_spent_in_course = api_time_to_hms($avg_time_spent_in_course / $nb_students_in_course);
  1676. } else {
  1677. $avg_time_spent_in_course = null;
  1678. $avg_progress_in_course = null;
  1679. $avg_score_in_course = null;
  1680. $avg_score_in_exercise = null;
  1681. $avg_messages_in_course = null;
  1682. $avg_assignments_in_course = null;
  1683. }
  1684. $table_row = array();
  1685. $table_row[] = $row_course[1];
  1686. $table_row[] = $nb_students_in_course;
  1687. $table_row[] = $avg_time_spent_in_course;
  1688. $table_row[] = is_null($avg_progress_in_course) ? '' : $avg_progress_in_course.'%';
  1689. $table_row[] = is_null($avg_score_in_course) ? '' : $avg_score_in_course.'%';
  1690. $table_row[] = is_null($avg_score_in_exercise) ? '' : $avg_score_in_exercise.'%';
  1691. $table_row[] = $avg_messages_in_course;
  1692. $table_row[] = $avg_assignments_in_course;
  1693. //set the "from" value to know if I access the Reporting by the chamilo tab or the course link
  1694. $table_row[] = '<center><a href="../../tracking/courseLog.php?cidReq='.$course_code.'&from=myspace&id_session='.$session_id.'">
  1695. '.Display::return_icon('2rightarrow.png').'
  1696. </a>
  1697. </center>';
  1698. $csv_content[] = array(
  1699. api_html_entity_decode($row_course[1], ENT_QUOTES, $charset),
  1700. $nb_students_in_course,
  1701. $avg_time_spent_in_course,
  1702. is_null($avg_progress_in_course) ? null : $avg_progress_in_course.'%',
  1703. is_null($avg_score_in_course) ? null : is_numeric($avg_score_in_course) ? $avg_score_in_course.'%' : $avg_score_in_course,
  1704. is_null($avg_score_in_exercise) ? null : $avg_score_in_exercise.'%',
  1705. $avg_messages_in_course,
  1706. $avg_assignments_in_course,
  1707. );
  1708. $course_data[] = $table_row;
  1709. }
  1710. return $course_data;
  1711. }
  1712. /**
  1713. * Get the number of users of the platform
  1714. *
  1715. * @return integer
  1716. *
  1717. */
  1718. public static function get_number_of_users_tracking_overview()
  1719. {
  1720. return UserManager::get_number_of_users(0, api_get_current_access_url_id());
  1721. }
  1722. /**
  1723. * Get all the data for the sortable table of the reporting progress of
  1724. * all users and all the courses the user is subscribed to.
  1725. *
  1726. * @param int $from
  1727. * @param int $numberItems
  1728. * @param int $column
  1729. * @param string $direction
  1730. * @return array
  1731. */
  1732. public static function get_user_data_tracking_overview($from, $numberItems, $column, $direction)
  1733. {
  1734. $isWestern = api_is_western_name_order();
  1735. switch ($column) {
  1736. case '0':
  1737. $column = 'official_code';
  1738. break;
  1739. case '1':
  1740. $column = $isWestern ? 'firstname' : 'lastname';
  1741. break;
  1742. case '2':
  1743. $column = $isWestern ? 'lastname' : 'firstname';
  1744. break;
  1745. case '3':
  1746. $column = 'username';
  1747. break;
  1748. case '4':
  1749. $column = 'username';
  1750. break;
  1751. }
  1752. $order = [
  1753. "$column $direction"
  1754. ];
  1755. $userList = UserManager::get_user_list([], $order, $from, $numberItems);
  1756. $return = [];
  1757. foreach ($userList as $user) {
  1758. $firstPosition = $user['lastname'];
  1759. $secondPosition = $user['firstname'];
  1760. if ($isWestern) {
  1761. $firstPosition = $user['firstname'];
  1762. $secondPosition = $user['lastname'];
  1763. }
  1764. $return[] = [
  1765. '0' => $user['official_code'],
  1766. 'col0' => $user['official_code'],
  1767. '1' => $firstPosition,
  1768. 'col1' => $firstPosition,
  1769. '2' => $secondPosition,
  1770. 'col2' => $secondPosition,
  1771. '3' => $user['username'],
  1772. 'col3' => $user['username'],
  1773. '4' => $user['user_id'],
  1774. 'col4' => $user['user_id']
  1775. ];
  1776. }
  1777. return $return;
  1778. }
  1779. /**
  1780. * Get all information that the user with user_id = $user_data has
  1781. * entered in the additionally defined profile fields
  1782. * @param integer $user_id the id of the user
  1783. * @return array
  1784. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1785. * @version Dokeos 1.8.6
  1786. * @since November 2008
  1787. */
  1788. public static function get_user_overview_export_extra_fields($user_id)
  1789. {
  1790. // include the user manager
  1791. $extra_data = UserManager::get_extra_user_data($user_id, true);
  1792. return $extra_data;
  1793. }
  1794. /**
  1795. * Checks if a username exist in the DB otherwise it create a "double"
  1796. * i.e. if we look into for jmontoya but the user's name already exist we create the user jmontoya2
  1797. * the return array will be array(username=>'jmontoya', sufix='2')
  1798. * @param string firstname
  1799. * @param string lastname
  1800. * @param string username
  1801. * @return array with the username, the sufix
  1802. * @author Julio Montoya Armas
  1803. */
  1804. public static function make_username($firstname, $lastname, $username, $language = null, $encoding = null)
  1805. {
  1806. // if username exist
  1807. if (!UserManager::is_username_available($username) || empty($username)) {
  1808. $i = 0;
  1809. while (1) {
  1810. if ($i == 0) {
  1811. $sufix = '';
  1812. } else {
  1813. $sufix = $i;
  1814. }
  1815. $desired_username = UserManager::create_username(
  1816. $firstname,
  1817. $lastname,
  1818. $language,
  1819. $encoding
  1820. );
  1821. if (UserManager::is_username_available($desired_username.$sufix)) {
  1822. break;
  1823. } else {
  1824. $i++;
  1825. }
  1826. }
  1827. $username_array = array('username' => $desired_username, 'sufix' => $sufix);
  1828. return $username_array;
  1829. } else {
  1830. $username_array = array('username' => $username, 'sufix' => '');
  1831. return $username_array;
  1832. }
  1833. }
  1834. /**
  1835. * Checks if there are repeted users in a given array
  1836. * @param array $usernames list of the usernames in the uploaded file
  1837. * @param array $user_array $user_array['username'] and $user_array['sufix'] where sufix is the number part in a login i.e -> jmontoya2
  1838. * @return array with the $usernames array and the $user_array array
  1839. * @author Julio Montoya
  1840. */
  1841. public static function check_user_in_array($usernames, $user_array)
  1842. {
  1843. $user_list = array_keys($usernames);
  1844. $username = $user_array['username'].$user_array['sufix'];
  1845. if (in_array($username, $user_list)) {
  1846. $user_array['sufix'] += $usernames[$username];
  1847. $usernames[$username]++;
  1848. } else {
  1849. $usernames[$username] = 1;
  1850. }
  1851. $result_array = array($usernames, $user_array);
  1852. return $result_array;
  1853. }
  1854. /**
  1855. * Checks whether a username has been already subscribed in a session.
  1856. * @param string $username a given username
  1857. * @param array $course_list the array with the course list id
  1858. * @param int $id_session the session id
  1859. * @return int 0 if the user is not subscribed otherwise it returns the user_id of the given username
  1860. * @author Julio Montoya
  1861. */
  1862. public static function user_available_in_session($username, $course_list, $id_session)
  1863. {
  1864. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1865. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1866. $id_session = intval($id_session);
  1867. $username = Database::escape_string($username);
  1868. foreach ($course_list as $courseId) {
  1869. $courseId = intval($courseId);
  1870. $sql = " SELECT u.user_id FROM $tbl_session_rel_course_rel_user rel
  1871. INNER JOIN $table_user u
  1872. ON (rel.user_id = u.user_id)
  1873. WHERE
  1874. rel.session_id='$id_session' AND
  1875. u.status='5' AND
  1876. u.username ='$username' AND
  1877. rel.c_id='$courseId'";
  1878. $rs = Database::query($sql);
  1879. if (Database::num_rows($rs) > 0) {
  1880. return Database::result($rs, 0, 0);
  1881. } else {
  1882. return 0;
  1883. }
  1884. }
  1885. return 0;
  1886. }
  1887. /**
  1888. * This function checks whether some users in the uploaded file
  1889. * repeated and creates unique usernames if necesary.
  1890. * A case: Within the file there is an user repeted twice (Julio Montoya / Julio Montoya)
  1891. * and the username fields are empty.
  1892. * Then, this function would create unique usernames based on the first and the last name.
  1893. * Two users wiould be created - jmontoya and jmontoya2.
  1894. * Of course, if in the database there is a user with the name jmontoya,
  1895. * the newly created two users registered would be jmontoya2 and jmontoya3.
  1896. * @param $users list of users
  1897. * @return array
  1898. * @author Julio Montoya Armas
  1899. */
  1900. function check_all_usernames($users, $course_list, $id_session)
  1901. {
  1902. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1903. $usernames = array();
  1904. $new_users = array();
  1905. foreach ($users as $index => $user) {
  1906. $desired_username = array();
  1907. if (empty($user['UserName'])) {
  1908. $desired_username = self::make_username($user['FirstName'], $user['LastName'], '');
  1909. $pre_username = $desired_username['username'].$desired_username['sufix'];
  1910. $user['UserName'] = $pre_username;
  1911. $user['create'] = '1';
  1912. } else {
  1913. if (UserManager::is_username_available($user['UserName'])) {
  1914. $desired_username = self::make_username($user['FirstName'], $user['LastName'], $user['UserName']);
  1915. $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
  1916. $user['create'] = '1';
  1917. } else {
  1918. $is_session_avail = self::user_available_in_session($user['UserName'], $course_list, $id_session);
  1919. if ($is_session_avail == 0) {
  1920. $user_name = $user['UserName'];
  1921. $sql_select = "SELECT user_id FROM $table_user WHERE username ='$user_name' ";
  1922. $rs = Database::query($sql_select);
  1923. $user['create'] = Database::result($rs, 0, 0); // This should be the ID because the user exists.
  1924. } else {
  1925. $user['create'] = $is_session_avail;
  1926. }
  1927. }
  1928. }
  1929. // Usernames is the current list of users in the file.
  1930. $result_array = self::check_user_in_array($usernames, $desired_username);
  1931. $usernames = $result_array[0];
  1932. $desired_username = $result_array[1];
  1933. $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
  1934. $new_users[] = $user;
  1935. }
  1936. return $new_users;
  1937. }
  1938. /**
  1939. * This functions checks whether there are users that are already
  1940. * registered in the DB by different creator than the current coach.
  1941. * @param string a given username
  1942. * @param array $users the array with the course list ids
  1943. * @return array
  1944. * @author Julio Montoya Armas
  1945. */
  1946. public function get_user_creator($users)
  1947. {
  1948. $errors = array();
  1949. foreach ($users as $index => $user) {
  1950. // database table definition
  1951. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1952. $username = Database::escape_string($user['UserName']);
  1953. $sql = "SELECT creator_id FROM $table_user WHERE username='$username' ";
  1954. $rs = Database::query($sql);
  1955. $creator_id = Database::result($rs, 0, 0);
  1956. // check if we are the creators or not
  1957. if ($creator_id != '') {
  1958. if ($creator_id != api_get_user_id()) {
  1959. $user['error'] = get_lang('UserAlreadyRegisteredByOtherCreator');
  1960. $errors[] = $user;
  1961. }
  1962. }
  1963. }
  1964. return $errors;
  1965. }
  1966. /**
  1967. * Validates imported data.
  1968. * @param $users list of users
  1969. */
  1970. function validate_data($users, $id_session = null)
  1971. {
  1972. $errors = array();
  1973. $new_users = array();
  1974. foreach ($users as $index => $user) {
  1975. // 1. Check whether mandatory fields are set.
  1976. $mandatory_fields = array('LastName', 'FirstName');
  1977. if (api_get_setting('registration', 'email') == 'true') {
  1978. $mandatory_fields[] = 'Email';
  1979. }
  1980. foreach ($mandatory_fields as $key => $field) {
  1981. if (!isset ($user[$field]) || strlen($user[$field]) == 0) {
  1982. $user['error'] = get_lang($field.'Mandatory');
  1983. $errors[] = $user;
  1984. }
  1985. }
  1986. // 2. Check whether the username is too long.
  1987. if (UserManager::is_username_too_long($user['UserName'])) {
  1988. $user['error'] = get_lang('UserNameTooLong');
  1989. $errors[] = $user;
  1990. }
  1991. $user['UserName'] = trim($user['UserName']);
  1992. if (empty($user['UserName'])) {
  1993. $user['UserName'] = UserManager::create_username($user['FirstName'], $user['LastName']);
  1994. }
  1995. $new_users[] = $user;
  1996. }
  1997. $results = array('errors' => $errors, 'users' => $new_users);
  1998. return $results;
  1999. }
  2000. /**
  2001. * Adds missing user-information (which isn't required, like password, etc).
  2002. */
  2003. function complete_missing_data($user)
  2004. {
  2005. // 1. Generate a password if it is necessary.
  2006. if (!isset ($user['Password']) || strlen($user['Password']) == 0) {
  2007. $user['Password'] = api_generate_password();
  2008. }
  2009. return $user;
  2010. }
  2011. /**
  2012. * Saves imported data.
  2013. */
  2014. public function save_data($users, $course_list, $id_session)
  2015. {
  2016. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2017. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  2018. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2019. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  2020. $id_session = intval($id_session);
  2021. $sendMail = $_POST['sendMail'] ? 1 : 0;
  2022. // Adding users to the platform.
  2023. $new_users = array();
  2024. foreach ($users as $index => $user) {
  2025. $user = self::complete_missing_data($user);
  2026. // coach only will registered users
  2027. $default_status = STUDENT;
  2028. if ($user['create'] == COURSEMANAGER) {
  2029. $user['id'] = UserManager:: create_user(
  2030. $user['FirstName'],
  2031. $user['LastName'],
  2032. $default_status,
  2033. $user['Email'],
  2034. $user['UserName'],
  2035. $user['Password'],
  2036. $user['OfficialCode'],
  2037. api_get_setting('PlatformLanguage'),
  2038. $user['PhoneNumber'],
  2039. ''
  2040. );
  2041. $user['added_at_platform'] = 1;
  2042. } else {
  2043. $user['id'] = $user['create'];
  2044. $user['added_at_platform'] = 0;
  2045. }
  2046. $new_users[] = $user;
  2047. }
  2048. // Update user list.
  2049. $users = $new_users;
  2050. // Inserting users.
  2051. $super_list = array();
  2052. foreach ($course_list as $enreg_course) {
  2053. $nbr_users = 0;
  2054. $new_users = array();
  2055. $enreg_course = Database::escape_string($enreg_course);
  2056. foreach ($users as $index => $user) {
  2057. $userid = intval($user['id']);
  2058. $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(session_id, c_id, user_id)
  2059. VALUES('$id_session','$enreg_course','$userid')";
  2060. $course_session = array('course' => $enreg_course, 'added' => 1);
  2061. $result = Database::query($sql);
  2062. if (Database::affected_rows($result)) {
  2063. $nbr_users++;
  2064. }
  2065. $new_users[] = $user;
  2066. }
  2067. $super_list[] = $new_users;
  2068. //update the nbr_users field
  2069. $sql_select = "SELECT COUNT(user_id) as nbUsers FROM $tbl_session_rel_course_rel_user
  2070. WHERE session_id='$id_session' AND c_id='$enreg_course'";
  2071. $rs = Database::query($sql_select);
  2072. list($nbr_users) = Database::fetch_array($rs);
  2073. $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users
  2074. WHERE session_id='$id_session' AND c_id='$enreg_course'";
  2075. Database::query($sql_update);
  2076. $sql_update = "UPDATE $tbl_session SET nbr_users= '$nbr_users' WHERE id='$id_session'";
  2077. Database::query($sql_update);
  2078. }
  2079. $new_users = array();
  2080. foreach ($users as $index => $user) {
  2081. $userid = $user['id'];
  2082. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_user(session_id, user_id, registered_at)
  2083. VALUES ('$id_session','$userid', '".api_get_utc_datetime()."')";
  2084. Database::query($sql_insert);
  2085. $user['added_at_session'] = 1;
  2086. $new_users[] = $user;
  2087. }
  2088. $users = $new_users;
  2089. $registered_users = get_lang('FileImported').'<br /> Import file results : <br />';
  2090. // Sending emails.
  2091. $addedto = '';
  2092. if ($sendMail) {
  2093. $i = 0;
  2094. foreach ($users as $index => $user) {
  2095. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  2096. $emailbody = get_lang('Dear').' '.
  2097. api_get_person_name($user['FirstName'], $user['LastName']).",\n\n".
  2098. get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".
  2099. get_lang('Username')." : $user[UserName]\n".
  2100. get_lang('Pass')." : $user[Password]\n\n".
  2101. get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".api_get_path(WEB_PATH)." \n\n".
  2102. get_lang('Problem')."\n\n".
  2103. get_lang('SignatureFormula').",\n\n".
  2104. api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".
  2105. get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".
  2106. api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator');
  2107. api_mail_html(
  2108. api_get_person_name($user['FirstName'], $user['LastName'], null, PERSON_NAME_EMAIL_ADDRESS),
  2109. $user['Email'],
  2110. $emailsubject,
  2111. $emailbody
  2112. );
  2113. $userInfo = api_get_user_info($user['id']);
  2114. if (($user['added_at_platform'] == 1 && $user['added_at_session'] == 1) || $user['added_at_session'] == 1) {
  2115. if ($user['added_at_platform'] == 1) {
  2116. $addedto = get_lang('UserCreatedPlatform');
  2117. } else {
  2118. $addedto = ' ';
  2119. }
  2120. if ($user['added_at_session'] == 1) {
  2121. $addedto .= get_lang('UserInSession');
  2122. }
  2123. } else {
  2124. $addedto = get_lang('UserNotAdded');
  2125. }
  2126. $registered_users .= UserManager::getUserProfileLink($userInfo)." - ".$addedto.'<br />';
  2127. }
  2128. } else {
  2129. $i = 0;
  2130. foreach ($users as $index => $user) {
  2131. $userInfo = api_get_user_info($user['id']);
  2132. if (($user['added_at_platform'] == 1 && $user['added_at_session'] == 1) || $user['added_at_session'] == 1) {
  2133. if ($user['added_at_platform'] == 1) {
  2134. $addedto = get_lang('UserCreatedPlatform');
  2135. } else {
  2136. $addedto = ' ';
  2137. }
  2138. if ($user['added_at_session'] == 1) {
  2139. $addedto .= ' '.get_lang('UserInSession');
  2140. }
  2141. } else {
  2142. $addedto = get_lang('UserNotAdded');
  2143. }
  2144. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  2145. }
  2146. }
  2147. Display::addFlash(Display::return_message($registered_users));
  2148. header('Location: course.php?id_session='.$id_session);
  2149. exit;
  2150. }
  2151. /**
  2152. * Reads CSV-file.
  2153. * @param string $file Path to the CSV-file
  2154. * @return array All userinformation read from the file
  2155. */
  2156. function parse_csv_data($file)
  2157. {
  2158. $users = Import::csvToArray($file);
  2159. foreach ($users as $index => $user) {
  2160. if (isset ($user['Courses'])) {
  2161. $user['Courses'] = explode('|', trim($user['Courses']));
  2162. }
  2163. $users[$index] = $user;
  2164. }
  2165. return $users;
  2166. }
  2167. /**
  2168. * XML-parser: the handler at the beginning of element.
  2169. */
  2170. function element_start($parser, $data)
  2171. {
  2172. $data = api_utf8_decode($data);
  2173. global $user;
  2174. global $current_tag;
  2175. switch ($data) {
  2176. case 'Contact' :
  2177. $user = array();
  2178. break;
  2179. default:
  2180. $current_tag = $data;
  2181. }
  2182. }
  2183. /**
  2184. * XML-parser: the handler at the end of element.
  2185. */
  2186. function element_end($parser, $data)
  2187. {
  2188. $data = api_utf8_decode($data);
  2189. global $user;
  2190. global $users;
  2191. global $current_value;
  2192. global $purification_option_for_usernames;
  2193. $user[$data] = $current_value;
  2194. switch ($data) {
  2195. case 'Contact' :
  2196. $user['UserName'] = UserManager::purify_username($user['UserName'], $purification_option_for_usernames);
  2197. $users[] = $user;
  2198. break;
  2199. default:
  2200. $user[$data] = $current_value;
  2201. break;
  2202. }
  2203. }
  2204. /**
  2205. * XML-parser: the handler for character data.
  2206. */
  2207. function character_data($parser, $data)
  2208. {
  2209. $data = trim(api_utf8_decode($data));
  2210. global $current_value;
  2211. $current_value = $data;
  2212. }
  2213. /**
  2214. * Reads XML-file.
  2215. * @param string $file Path to the XML-file
  2216. * @return array All userinformation read from the file
  2217. */
  2218. function parse_xml_data($file)
  2219. {
  2220. global $current_tag;
  2221. global $current_value;
  2222. global $user;
  2223. global $users;
  2224. $users = array();
  2225. $parser = xml_parser_create('UTF-8');
  2226. xml_set_element_handler($parser, array('MySpace', 'element_start'), array('MySpace', 'element_end'));
  2227. xml_set_character_data_handler($parser, "character_data");
  2228. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  2229. xml_parse($parser, api_utf8_encode_xml(file_get_contents($file)));
  2230. xml_parser_free($parser);
  2231. return $users;
  2232. }
  2233. public static function displayTrackingAccessOverView($courseId, $sessionId, $studentId)
  2234. {
  2235. $courseId = intval($courseId);
  2236. $sessionId = intval($sessionId);
  2237. $studentId = intval($studentId);
  2238. $em = Database::getManager();
  2239. $sessionRepo = $em->getRepository('ChamiloCoreBundle:Session');
  2240. $courseList = [];
  2241. $sessionList = [];
  2242. $studentList = [];
  2243. if (!empty($courseId)) {
  2244. $course = $em->find('ChamiloCoreBundle:Course', $courseId);
  2245. $courseList[$course->getId()] = $course->getTitle();
  2246. }
  2247. if (!empty($sessionId)) {
  2248. $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
  2249. $sessionList[$session->getId()] = $session->getName();
  2250. }
  2251. if (!empty($studentId)) {
  2252. $student = $em->find('ChamiloUserBundle:User', $studentId);
  2253. $studentList[$student->getId()] = $student->getCompleteName();
  2254. }
  2255. $form = new FormValidator('access_overview', 'GET');
  2256. $form->addElement(
  2257. 'select_ajax',
  2258. 'course_id',
  2259. get_lang('SearchCourse'),
  2260. $courseList,
  2261. [
  2262. 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?'.http_build_query([
  2263. 'a' => 'search_course_by_session_all',
  2264. 'session_id' => $sessionId
  2265. ])
  2266. ]
  2267. );
  2268. $form->addElement(
  2269. 'select_ajax',
  2270. 'session_id',
  2271. get_lang('SearchSession'),
  2272. $sessionList,
  2273. [
  2274. 'url_function' => "
  2275. function () {
  2276. var params = $.param({
  2277. a: 'search_session_by_course',
  2278. course_id: $('#course_id').val() || 0
  2279. });
  2280. return '" . api_get_path(WEB_AJAX_PATH)."session.ajax.php?' + params;
  2281. }
  2282. "
  2283. ]
  2284. );
  2285. $form->addSelect(
  2286. 'profile',
  2287. get_lang('Profile'),
  2288. [
  2289. '' => get_lang('Select'),
  2290. STUDENT => get_lang('Student'),
  2291. COURSEMANAGER => get_lang('CourseManager'),
  2292. DRH => get_lang('Drh')
  2293. ],
  2294. ['id' => 'profile']
  2295. );
  2296. $form->addElement(
  2297. 'select_ajax',
  2298. 'student_id',
  2299. get_lang('SearchUsers'),
  2300. $studentList,
  2301. [
  2302. 'placeholder' => get_lang('All'),
  2303. 'url_function' => "
  2304. function () {
  2305. var params = $.param({
  2306. a: 'search_user_by_course',
  2307. session_id: $('#session_id').val(),
  2308. course_id: $('#course_id').val()
  2309. });
  2310. return '" . api_get_path(WEB_AJAX_PATH)."course.ajax.php?' + params;
  2311. }
  2312. "
  2313. ]
  2314. );
  2315. $form->addDateRangePicker(
  2316. 'date',
  2317. get_lang('DateRange'),
  2318. true,
  2319. [
  2320. 'id' => 'date_range',
  2321. 'format' => 'YYYY-MM-DD',
  2322. 'timePicker' => 'false',
  2323. 'validate_format' => 'Y-m-d'
  2324. ]
  2325. );
  2326. $form->addHidden('display', 'accessoverview');
  2327. $form->addRule('course_id', get_lang('Required'), 'required');
  2328. $form->addRule('profile', get_lang('Required'), 'required');
  2329. $form->addButton('submit', get_lang('Generate'), 'gear', 'primary');
  2330. $table = null;
  2331. if ($form->validate()) {
  2332. $table = new SortableTable(
  2333. 'tracking_access_overview',
  2334. ['MySpace', 'getNumberOfTrackAccessOverview'],
  2335. ['MySpace', 'getUserDataAccessTrackingOverview'],
  2336. 0
  2337. );
  2338. $table->additional_parameters = $form->exportValues();
  2339. $table->set_header(0, get_lang('LoginDate'), true);
  2340. $table->set_header(1, get_lang('Username'), true);
  2341. if (api_is_western_name_order()) {
  2342. $table->set_header(2, get_lang('FirstName'), true);
  2343. $table->set_header(3, get_lang('LastName'), true);
  2344. } else {
  2345. $table->set_header(2, get_lang('LastName'), true);
  2346. $table->set_header(3, get_lang('FirstName'), true);
  2347. }
  2348. $table->set_header(4, get_lang('Clicks'), false);
  2349. $table->set_header(5, get_lang('IP'), false);
  2350. $table->set_header(6, get_lang('TimeLoggedIn'), false);
  2351. }
  2352. $template = new Template(
  2353. null, false, false, false, false, false, false
  2354. );
  2355. $template->assign('form', $form->returnForm());
  2356. $template->assign('table', $table ? $table->return_table() : null);
  2357. echo $template->fetch(
  2358. $template->get_template('my_space/accessoverview.tpl')
  2359. );
  2360. }
  2361. /**
  2362. * @return int
  2363. */
  2364. public static function getNumberOfTrackAccessOverview()
  2365. {
  2366. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  2367. $sql = "SELECT COUNT(course_access_id) count FROM $table";
  2368. $result = Database::query($sql);
  2369. $row = Database::fetch_assoc($result);
  2370. return $row['count'];
  2371. }
  2372. /**
  2373. * @param $from
  2374. * @param $numberItems
  2375. * @param $column
  2376. * @param $orderDirection
  2377. * @return array
  2378. */
  2379. public static function getUserDataAccessTrackingOverview($from, $numberItems, $column, $orderDirection)
  2380. {
  2381. $user = Database::get_main_table(TABLE_MAIN_USER);
  2382. $course = Database::get_main_table(TABLE_MAIN_COURSE);
  2383. $track_e_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  2384. $track_e_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  2385. global $export_csv;
  2386. if ($export_csv) {
  2387. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  2388. } else {
  2389. $is_western_name_order = api_is_western_name_order();
  2390. }
  2391. //TODO add course name
  2392. $sql = "SELECT
  2393. a.login_course_date as col0,
  2394. u.username as col1,
  2395. " . (
  2396. $is_western_name_order ? "
  2397. u.firstname AS col2,
  2398. u.lastname AS col3,
  2399. " : "
  2400. u.lastname AS col2,
  2401. u.firstname AS col3,
  2402. " )."
  2403. a.logout_course_date,
  2404. c.title,
  2405. c.code,
  2406. u.user_id
  2407. FROM $track_e_course_access a
  2408. INNER JOIN $user u ON a.user_id = u.user_id
  2409. INNER JOIN $course c ON a.c_id = c.id";
  2410. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  2411. $sessionId = intval($_GET['session_id']);
  2412. $sql .= " WHERE a.session_id = ".$sessionId;
  2413. }
  2414. $sql .= " ORDER BY col$column $orderDirection ";
  2415. $sql .= " LIMIT $from,$numberItems";
  2416. $result = Database::query($sql);
  2417. //$clicks = Tracking::get_total_clicks_by_session();
  2418. $data = array();
  2419. while ($user = Database::fetch_assoc($result)) {
  2420. $data[] = $user;
  2421. }
  2422. $return = [];
  2423. //TODO: Dont use numeric index
  2424. foreach ($data as $key => $info) {
  2425. $start_date = $info['col0'];
  2426. $end_date = $info['logout_course_date'];
  2427. $return[$info['user_id']] = array(
  2428. $start_date,
  2429. $info['col1'],
  2430. $info['col2'],
  2431. $info['col3'],
  2432. $info['user_id'],
  2433. 'ip',
  2434. //TODO is not correct/precise, it counts the time not logged between two loggins
  2435. gmdate("H:i:s", strtotime($end_date) - strtotime($start_date))
  2436. );
  2437. }
  2438. foreach ($return as $key => $info) {
  2439. $ipResult = Database::select(
  2440. 'user_ip',
  2441. $track_e_login,
  2442. ['where' => [
  2443. '? BETWEEN login_date AND logout_date' => $info[0]
  2444. ]],
  2445. 'first'
  2446. );
  2447. $return[$key][5] = $ipResult['user_ip'];
  2448. }
  2449. return $return;
  2450. }
  2451. /**
  2452. * Gets the connections to a course as an array of login and logout time
  2453. *
  2454. * @param int $user_id
  2455. * @param int $courseId
  2456. * @author Jorge Frisancho Jibaja
  2457. * @author Julio Montoya <gugli100@gmail.com> fixing the function
  2458. * @version OCT-22- 2010
  2459. * @return array
  2460. */
  2461. public static function get_connections_to_course_by_date($user_id, $courseId, $start_date, $end_date)
  2462. {
  2463. // Database table definitions
  2464. $tbl_track_course = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  2465. $course_info = api_get_course_info_by_id($courseId);
  2466. $user_id = intval($user_id);
  2467. $courseId = intval($courseId);
  2468. $connections = array();
  2469. if (!empty($course_info)) {
  2470. $end_date = add_day_to($end_date);
  2471. $sql = "SELECT login_course_date, logout_course_date
  2472. FROM $tbl_track_course
  2473. WHERE
  2474. user_id = $user_id AND
  2475. c_id = $courseId AND
  2476. login_course_date BETWEEN '$start_date' AND '$end_date' AND
  2477. logout_course_date BETWEEN '$start_date' AND '$end_date'
  2478. ORDER BY login_course_date ASC";
  2479. $rs = Database::query($sql);
  2480. while ($row = Database::fetch_array($rs)) {
  2481. $login_date = $row['login_course_date'];
  2482. $logout_date = $row['logout_course_date'];
  2483. $timestamp_login_date = strtotime($login_date);
  2484. $timestamp_logout_date = strtotime($logout_date);
  2485. $connections[] = array(
  2486. 'login' => $timestamp_login_date,
  2487. 'logout' => $timestamp_logout_date
  2488. );
  2489. }
  2490. }
  2491. return $connections;
  2492. }
  2493. }
  2494. /**
  2495. * @param $user_id
  2496. * @param int $courseId
  2497. * @param null $start_date
  2498. * @param null $end_date
  2499. * @return array
  2500. */
  2501. function get_stats($user_id, $courseId, $start_date = null, $end_date = null)
  2502. {
  2503. // Database table definitions
  2504. $tbl_track_course = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  2505. $course_info = api_get_course_info_by_id($courseId);
  2506. if (!empty($course_info)) {
  2507. $stringStartDate = '';
  2508. $stringEndDate = '';
  2509. if ($start_date != null && $end_date != null) {
  2510. $end_date = add_day_to($end_date);
  2511. $stringStartDate = "AND login_course_date BETWEEN '$start_date' AND '$end_date'";
  2512. $stringEndDate = "AND logout_course_date BETWEEN '$start_date' AND '$end_date'";
  2513. }
  2514. $user_id = intval($user_id);
  2515. $courseId = intval($courseId);
  2516. $sql = "SELECT
  2517. SEC_TO_TIME(avg(time_to_sec(timediff(logout_course_date,login_course_date)))) as avrg,
  2518. SEC_TO_TIME(sum(time_to_sec(timediff(logout_course_date,login_course_date)))) as total,
  2519. count(user_id) as times
  2520. FROM $tbl_track_course
  2521. WHERE
  2522. user_id = $user_id AND
  2523. c_id = $courseId $stringStartDate $stringEndDate
  2524. ORDER BY login_course_date ASC";
  2525. $rs = Database::query($sql);
  2526. $result = array();
  2527. if ($row = Database::fetch_array($rs)) {
  2528. $foo_avg = $row['avrg'];
  2529. $foo_total = $row['total'];
  2530. $foo_times = $row['times'];
  2531. $result = array('avg' => $foo_avg, 'total' => $foo_total, 'times' => $foo_times);
  2532. }
  2533. }
  2534. return $result;
  2535. }
  2536. function add_day_to($end_date) {
  2537. $foo_date = strtotime($end_date);
  2538. $foo_date = strtotime(" +1 day", $foo_date);
  2539. $foo_date = date("Y-m-d", $foo_date);
  2540. return $foo_date;
  2541. }
  2542. /**
  2543. *
  2544. * @param array
  2545. * @author Jorge Frisancho Jibaja
  2546. * @version OCT-22- 2010
  2547. * @return array
  2548. */
  2549. function convert_to_array($sql_result)
  2550. {
  2551. $result_to_print = '<table>';
  2552. foreach ($sql_result as $key => $data) {
  2553. $result_to_print .= '<tr><td>'.date('d-m-Y (H:i:s)', $data['login']).'</td><td>'.api_time_to_hms($data['logout'] - $data['login']).'</tr></td>'."\n";
  2554. }
  2555. $result_to_print .= '</table>';
  2556. $result_to_print = array("result"=>$result_to_print);
  2557. return $result_to_print;
  2558. }
  2559. /**
  2560. * Converte an array to a table in html
  2561. *
  2562. * @param array $sql_result
  2563. * @author Jorge Frisancho Jibaja
  2564. * @version OCT-22- 2010
  2565. * @return string
  2566. */
  2567. function convert_to_string($sql_result)
  2568. {
  2569. $result_to_print = '<table>';
  2570. if (!empty($sql_result)) {
  2571. foreach ($sql_result as $key => $data) {
  2572. $result_to_print .= '<tr><td>'.date('d-m-Y (H:i:s)', $data['login']).'</td><td>'.api_time_to_hms($data['logout'] - $data['login']).'</tr></td>'."\n";
  2573. }
  2574. }
  2575. $result_to_print .= '</table>';
  2576. return $result_to_print;
  2577. }
  2578. /**
  2579. * This function draw the graphic to be displayed on the user view as an image
  2580. *
  2581. * @param array $sql_result
  2582. * @param string $start_date
  2583. * @param string $end_date
  2584. * @param string $type
  2585. * @author Jorge Frisancho Jibaja
  2586. * @version OCT-22- 2010
  2587. * @return string
  2588. */
  2589. function grapher($sql_result, $start_date, $end_date, $type = "")
  2590. {
  2591. if (empty($start_date)) { $start_date = ""; }
  2592. if (empty($end_date)) { $end_date = ""; }
  2593. if ($type == "") { $type = 'day'; }
  2594. $main_year = $main_month_year = $main_day = [];
  2595. $period = new DatePeriod(
  2596. new DateTime($start_date),
  2597. new DateInterval('P1D'),
  2598. new DateTime($end_date)
  2599. );
  2600. foreach ($period as $date) {
  2601. $main_day[$date->format('d-m-Y')] = 0;
  2602. }
  2603. $period = new DatePeriod(
  2604. new DateTime($start_date),
  2605. new DateInterval('P1M'),
  2606. new DateTime($end_date)
  2607. );
  2608. foreach ($period as $date) {
  2609. $main_month_year[$date->format('m-Y')] = 0;
  2610. }
  2611. $i = 0;
  2612. if (is_array($sql_result) && count($sql_result) > 0) {
  2613. foreach ($sql_result as $key => $data) {
  2614. //creating the main array
  2615. if (isset($main_month_year[date('m-Y', $data['login'])])) {
  2616. $main_month_year[date('m-Y', $data['login'])] += float_format(($data['logout'] - $data['login']) / 60, 0);
  2617. }
  2618. if (isset($main_day[date('d-m-Y', $data['login'])])) {
  2619. $main_day[date('d-m-Y', $data['login'])] += float_format(($data['logout'] - $data['login']) / 60, 0);
  2620. }
  2621. if ($i > 500) {
  2622. break;
  2623. }
  2624. $i++;
  2625. }
  2626. switch ($type) {
  2627. case 'day':
  2628. $main_date = $main_day;
  2629. break;
  2630. case 'month':
  2631. $main_date = $main_month_year;
  2632. break;
  2633. case 'year':
  2634. $main_date = $main_year;
  2635. break;
  2636. }
  2637. // the nice graphics :D
  2638. $labels = array_keys($main_date);
  2639. if (count($main_date) == 1) {
  2640. $labels = $labels[0];
  2641. $main_date = $main_date[$labels];
  2642. }
  2643. /* Create and populate the pData object */
  2644. $myData = new pData();
  2645. $myData->addPoints($main_date, 'Serie1');
  2646. if (count($main_date) != 1) {
  2647. $myData->addPoints($labels, 'Labels');
  2648. $myData->setSerieDescription('Labels', 'Months');
  2649. $myData->setAbscissa('Labels');
  2650. }
  2651. $myData->setSerieWeight('Serie1', 1);
  2652. $myData->setSerieDescription('Serie1', get_lang('MyResults'));
  2653. $myData->setAxisName(0, get_lang('Minutes'));
  2654. $myData->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true);
  2655. // Cache definition
  2656. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  2657. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  2658. $chartHash = $myCache->getHash($myData);
  2659. if ($myCache->isInCache($chartHash)) {
  2660. //if we already created the img
  2661. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  2662. $myCache->saveFromCache($chartHash, $imgPath);
  2663. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  2664. } else {
  2665. /* Define width, height and angle */
  2666. $mainWidth = 760;
  2667. $mainHeight = 230;
  2668. $angle = 50;
  2669. /* Create the pChart object */
  2670. $myPicture = new pImage($mainWidth, $mainHeight, $myData);
  2671. /* Turn of Antialiasing */
  2672. $myPicture->Antialias = false;
  2673. /* Draw the background */
  2674. $settings = array("R" => 255, "G" => 255, "B" => 255);
  2675. $myPicture->drawFilledRectangle(0, 0, $mainWidth, $mainHeight, $settings);
  2676. /* Add a border to the picture */
  2677. $myPicture->drawRectangle(
  2678. 0,
  2679. 0,
  2680. $mainWidth - 1,
  2681. $mainHeight - 1,
  2682. array("R" => 0, "G" => 0, "B" => 0)
  2683. );
  2684. /* Set the default font */
  2685. $myPicture->setFontProperties(
  2686. array(
  2687. "FontName" => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
  2688. "FontSize" => 10)
  2689. );
  2690. /* Write the chart title */
  2691. $myPicture->drawText(
  2692. $mainWidth / 2,
  2693. 30,
  2694. get_lang('TimeSpentInTheCourse'),
  2695. array(
  2696. "FontSize" => 12,
  2697. "Align" => TEXT_ALIGN_BOTTOMMIDDLE
  2698. )
  2699. );
  2700. /* Set the default font */
  2701. $myPicture->setFontProperties(
  2702. array(
  2703. "FontName" => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
  2704. "FontSize" => 8
  2705. )
  2706. );
  2707. /* Define the chart area */
  2708. $myPicture->setGraphArea(50, 40, $mainWidth - 40, $mainHeight - 80);
  2709. /* Draw the scale */
  2710. $scaleSettings = array(
  2711. 'XMargin' => 10,
  2712. 'YMargin' => 10,
  2713. 'Floating' => true,
  2714. 'GridR' => 200,
  2715. 'GridG' => 200,
  2716. 'GridB' => 200,
  2717. 'DrawSubTicks' => true,
  2718. 'CycleBackground' => true,
  2719. 'LabelRotation' => $angle,
  2720. 'Mode' => SCALE_MODE_ADDALL_START0,
  2721. );
  2722. $myPicture->drawScale($scaleSettings);
  2723. /* Turn on Antialiasing */
  2724. $myPicture->Antialias = true;
  2725. /* Enable shadow computing */
  2726. $myPicture->setShadow(
  2727. true,
  2728. array(
  2729. "X" => 1,
  2730. "Y" => 1,
  2731. "R" => 0,
  2732. "G" => 0,
  2733. "B" => 0,
  2734. "Alpha" => 10
  2735. )
  2736. );
  2737. /* Draw the line chart */
  2738. $myPicture->setFontProperties(
  2739. array(
  2740. "FontName" => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
  2741. "FontSize" => 10
  2742. )
  2743. );
  2744. $myPicture->drawSplineChart();
  2745. $myPicture->drawPlotChart(
  2746. array(
  2747. "DisplayValues" => true,
  2748. "PlotBorder" => true,
  2749. "BorderSize" => 1,
  2750. "Surrounding" => -60,
  2751. "BorderAlpha" => 80
  2752. )
  2753. );
  2754. /* Do NOT Write the chart legend */
  2755. /* Write and save into cache */
  2756. $myCache->writeToCache($chartHash, $myPicture);
  2757. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  2758. $myCache->saveFromCache($chartHash, $imgPath);
  2759. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  2760. }
  2761. $html = '<img src="'.$imgPath.'">';
  2762. return $html;
  2763. } else {
  2764. $foo_img = api_convert_encoding('<div id="messages" class="warning-message">'.get_lang('GraphicNotAvailable').'</div>', 'UTF-8');
  2765. return $foo_img;
  2766. }
  2767. }