myspace.lib.php 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * MySpace class definition
  5. * @package chamilo.reporting
  6. */
  7. /**
  8. * Init
  9. */
  10. /**
  11. * MySpace class definition
  12. */
  13. class MySpace {
  14. /**
  15. * This function serves exporting data in CSV format.
  16. * @param array $header The header labels.
  17. * @param array $data The data array.
  18. * @param string $file_name The name of the file which contains exported data.
  19. * @return string mixed Returns a message (string) if an error occurred.
  20. */
  21. function export_csv($header, $data, $file_name = 'export.csv') {
  22. $archive_path = api_get_path(SYS_ARCHIVE_PATH);
  23. $archive_url = api_get_path(WEB_CODE_PATH).'course_info/download.php?archive=';
  24. if (!$open = fopen($archive_path.$file_name, 'w+')) {
  25. $message = get_lang('noOpen');
  26. } else {
  27. $info = '';
  28. foreach ($header as $value) {
  29. $info .= $value.';';
  30. }
  31. $info .= "\r\n";
  32. foreach ($data as $row) {
  33. foreach ($row as $value) {
  34. $info .= $value.';';
  35. }
  36. $info .= "\r\n";
  37. }
  38. fwrite($open, $info);
  39. fclose($open);
  40. @chmod($file_name, api_get_permissions_for_new_files());
  41. header("Location:".$archive_url.$file_name);
  42. }
  43. return $message;
  44. }
  45. /**
  46. * Gets the connections to a course as an array of login and logout time
  47. *
  48. * @param int User ud
  49. * @param string Course code
  50. * @param int Session id (optional, default = 0)
  51. * @return array Conections
  52. */
  53. static function get_connections_to_course($user_id, $courseId, $session_id = 0) {
  54. // Database table definitions
  55. $tbl_track_course = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  56. // protect data
  57. $user_id = intval($user_id);
  58. $courseId = Database::escape_string($courseId);
  59. $session_id = intval($session_id);
  60. $sql = 'SELECT login_course_date, logout_course_date FROM ' . $tbl_track_course . '
  61. WHERE user_id = '.$user_id.' AND
  62. c_id ="'.$courseId.'" AND
  63. session_id = '.$session_id.'
  64. ORDER BY login_course_date ASC';
  65. $rs = Database::query($sql);
  66. $connections = array();
  67. while ($row = Database::fetch_array($rs)) {
  68. $timestamp_login_date = api_strtotime($row['login_course_date'], 'UTC');
  69. $timestamp_logout_date = api_strtotime($row['logout_course_date'], 'UTC');
  70. $connections[] = array('login' => $timestamp_login_date, 'logout' => $timestamp_logout_date);
  71. }
  72. return $connections;
  73. }
  74. static function get_connections_from_course_list($user_id, $course_list, $session_id = 0) {
  75. // Database table definitions
  76. $tbl_track_course = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  77. if (empty($course_list)) {
  78. return false;
  79. }
  80. $user_id = intval($user_id);
  81. $session_id = intval($session_id);
  82. $new_course_list = array();;
  83. foreach ($course_list as $course_item) {
  84. $new_course_list[] = '"'.Database::escape_string($course_item['real_id']).'"';
  85. }
  86. $course_list = implode(', ', $new_course_list);
  87. if (empty($course_list)) {
  88. return false;
  89. }
  90. $sql = 'SELECT login_course_date, logout_course_date, c_id FROM '.$tbl_track_course.'
  91. WHERE user_id = '.$user_id.' AND
  92. c_id IN ('.$course_list.') AND
  93. session_id = '.$session_id.'
  94. ORDER BY login_course_date ASC';
  95. $rs = Database::query($sql);
  96. $connections = array();
  97. while ($row = Database::fetch_array($rs)) {
  98. $timestamp_login_date = api_strtotime($row['login_course_date'], 'UTC');
  99. $timestamp_logout_date = api_strtotime($row['logout_course_date'], 'UTC');
  100. $connections[] = array('login' => $timestamp_login_date, 'logout' => $timestamp_logout_date, 'c_id' => $row['c_id']);
  101. }
  102. return $connections;
  103. }
  104. /**
  105. * Creates a small table in the last column of the table with the user overview
  106. *
  107. * @param integer $user_id the id of the user
  108. * @param array $url_params additonal url parameters
  109. * @param array $row the row information (the other columns)
  110. * @return string html code
  111. */
  112. function course_info_tracking_filter($user_id, $url_params, $row) {
  113. // the table header
  114. $return = '<table class="data_table" style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  115. /*$return .= ' <tr>';
  116. $return .= ' <th>'.get_lang('Course').'</th>';
  117. $return .= ' <th>'.get_lang('AvgTimeSpentInTheCourse').'</th>';
  118. $return .= ' <th>'.get_lang('AvgStudentsProgress').'</th>';
  119. $return .= ' <th>'.get_lang('AvgCourseScore').'</th>';
  120. $return .= ' <th>'.get_lang('AvgExercisesScore').'</th>';
  121. $return .= ' <th>'.get_lang('AvgMessages').'</th>';
  122. $return .= ' <th>'.get_lang('AvgAssignments').'</th>';
  123. $return .= ' <th>'.get_lang('TotalExercisesScoreObtained').'</th>';
  124. $return .= ' <th>'.get_lang('TotalExercisesScorePossible').'</th>';
  125. $return .= ' <th>'.get_lang('TotalExercisesAnswered').'</th>';
  126. $return .= ' <th>'.get_lang('TotalExercisesScorePercentage').'</th>';
  127. $return .= ' <th>'.get_lang('FirstLogin').'</th>';
  128. $return .= ' <th>'.get_lang('LatestLogin').'</th>';
  129. $return .= ' </tr>';*/
  130. // database table definition
  131. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  132. // Getting all the courses of the user
  133. $sql = "SELECT c_id FROM $tbl_course_user
  134. WHERE user_id = '".Database::escape_string($user_id)."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
  135. $result = Database::query($sql);
  136. while ($row = Database::fetch_array($result, 'ASSOC')) {
  137. $courseInfo = api_get_course_info_by_id($row['c_id']);
  138. $courseCode = $courseInfo['code'];
  139. $courseId = $courseInfo['real_id'];
  140. $return .= '<tr>';
  141. // course code
  142. $return .= ' <td width="157px" >'.Text::cut($courseInfo['code'], 20, true).'</td>';
  143. // time spent in the course
  144. $return .= ' <td><div>'.api_time_to_hms(Tracking :: get_time_spent_on_the_course($user_id, $courseId)).'</div></td>';
  145. // student progress in course
  146. $return .= ' <td><div>'.round(Tracking :: get_avg_student_progress($user_id, $courseId), 2).'</div></td>';
  147. // student score
  148. $avg_score = Tracking :: get_avg_student_score($user_id, $courseId);
  149. if (is_numeric($avg_score)) {
  150. $avg_score = round($avg_score,2);
  151. } else {
  152. $$avg_score = '-';
  153. }
  154. $return .= ' <td><div>'.$avg_score.'</div></td>';
  155. // student tes score
  156. //$return .= ' <td><div style="width:40px">'.round(Tracking :: get_avg_student_exercise_score ($user_id, $courseCode),2).'%</div></td>';
  157. // student messages
  158. $return .= ' <td><div>'.Tracking :: count_student_messages($user_id, $courseId).'</div></td>';
  159. // student assignments
  160. $return .= ' <td><div>'.Tracking :: count_student_assignments($user_id, $courseId).'</div></td>';
  161. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  162. $exercises_results = MySpace::exercises_results($user_id, $courseId);
  163. $return .= ' <td width="105px"><div>'.(is_null($exercises_results['percentage']) ? '' : $exercises_results['score_obtained'].'/'.$exercises_results['score_possible'].' ( '.$exercises_results['percentage'].'% )').'</div></td>';
  164. //$return .= ' <td><div>'.$exercises_results['score_possible'].'</div></td>';
  165. $return .= ' <td><div>'.$exercises_results['questions_answered'].'</div></td>';
  166. //$return .= ' <td><div>'.$exercises_results['percentage'].'% </div></td>';
  167. // first connection
  168. //$return .= ' <td width="60px">'.Tracking :: get_first_connection_date_on_the_course ($user_id, $courseCode).'</td>';
  169. // last connection
  170. $return .= ' <td><div>'.Tracking :: get_last_connection_date_on_the_course ($user_id, $courseId).'</div></td>';
  171. $return .= '<tr>';
  172. }
  173. $return .= '</table>';
  174. return $return;
  175. }
  176. /**
  177. * Display a sortable table that contains an overview off all the reporting progress of all users and all courses the user is subscribed to
  178. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  179. * @version Dokeos 1.8.6
  180. * @since October 2008
  181. */
  182. function display_tracking_user_overview() {
  183. MySpace::display_user_overview_export_options();
  184. $t_head = ' <table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  185. //$t_head .= ' <caption>'.get_lang('CourseInformation').'</caption>';
  186. $t_head .= '<tr>';
  187. $t_head .= ' <th width="155px" style="border-left:0;border-bottom:0"><span>'.get_lang('Course').'</span></th>';
  188. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
  189. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
  190. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
  191. //$t_head .= ' <th><div style="width:40px">'.get_lang('AvgExercisesScore').'</div></th>';
  192. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
  193. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
  194. $t_head .= ' <th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
  195. //$t_head .= ' <th><div>'.get_lang('TotalExercisesScorePossible').'</div></th>';
  196. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
  197. //$t_head .= ' <th><div>'.get_lang('TotalExercisesScorePercentage').'</div></th>';
  198. //$t_head .= ' <th><div style="width:60px">'.get_lang('FirstLogin').'</div></th>';
  199. $t_head .= ' <th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
  200. $t_head .= ' </tr></table>';
  201. $addparams = array('view' => 'admin', 'display' => 'useroverview');
  202. $table = new SortableTable('tracking_user_overview', array('MySpace','get_number_of_users_tracking_overview'), array('MySpace','get_user_data_tracking_overview'), 0);
  203. $table->additional_parameters = $addparams;
  204. $table->set_header(0, get_lang('OfficialCode'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  205. if (api_is_western_name_order()) {
  206. $table->set_header(1, get_lang('FirstName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  207. $table->set_header(2, get_lang('LastName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  208. } else {
  209. $table->set_header(1, get_lang('LastName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  210. $table->set_header(2, get_lang('FirstName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  211. }
  212. $table->set_header(3, get_lang('LoginName'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  213. $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;'));
  214. $table->set_column_filter(4, array('MySpace','course_info_tracking_filter'));
  215. $table->display();
  216. }
  217. /**
  218. * Displays a form with all the additionally defined user fields of the profile
  219. * and give you the opportunity to include these in the CSV export
  220. *
  221. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  222. * @version Dokeos 1.8.6
  223. * @since November 2008
  224. */
  225. function display_user_overview_export_options() {
  226. // include the user manager and formvalidator library
  227. if ($_GET['export'] == 'options') {
  228. // get all the defined extra fields
  229. $extrafields = UserManager::get_extra_fields(0, 50, 5, 'ASC', false, 1);
  230. // creating the form with all the defined extra fields
  231. $form = new FormValidator('exportextrafields', 'post', api_get_self()."?view=".Security::remove_XSS($_GET['view']).'&display='.Security::remove_XSS($_GET['display']).'&export='.Security::remove_XSS($_GET['export']));
  232. if (is_array($extrafields) && count($extrafields) > 0) {
  233. foreach ($extrafields as $key => $extra) {
  234. $form->addElement('checkbox', 'extra_export_field'.$extra[0], '', $extra[3]);
  235. }
  236. $form->addElement('style_submit_button','submit', get_lang('Ok'),'class="save"' );
  237. // setting the default values for the form that contains all the extra fields
  238. if (is_array($_SESSION['additional_export_fields'])) {
  239. foreach ($_SESSION['additional_export_fields'] as $key => $value) {
  240. $defaults['extra_export_field'.$value] = 1;
  241. }
  242. }
  243. $form->setDefaults($defaults);
  244. } else {
  245. $form->addElement('html', Display::display_warning_message(get_lang('ThereAreNotExtrafieldsAvailable')));
  246. }
  247. if ($form->validate()) {
  248. // exporting the form values
  249. $values = $form->exportValues();
  250. // re-initialising the session that contains the additional fields that need to be exported
  251. $_SESSION['additional_export_fields'] = array();
  252. // adding the fields that are checked to the session
  253. $message = '';
  254. foreach ($values as $field_ids => $value) {
  255. if ($value == 1 && strstr($field_ids,'extra_export_field')) {
  256. $_SESSION['additional_export_fields'][] = str_replace('extra_export_field', '', $field_ids);
  257. }
  258. }
  259. // adding the fields that will be also exported to a message string
  260. if (is_array($_SESSION['additional_export_fields'])) {
  261. foreach ($_SESSION['additional_export_fields'] as $key => $extra_field_export) {
  262. $message .= '<li>'.$extrafields[$extra_field_export][3].'</li>';
  263. }
  264. }
  265. // Displaying a feedback message
  266. if (!empty($_SESSION['additional_export_fields'])) {
  267. Display::display_confirmation_message(get_lang('FollowingFieldsWillAlsoBeExported').': <br /><ul>'.$message.'</ul>', false);
  268. } else {
  269. Display::display_confirmation_message(get_lang('NoAdditionalFieldsWillBeExported'), false);
  270. }
  271. $message = '';
  272. } else {
  273. $form->display();
  274. }
  275. } else {
  276. if (!empty($_SESSION['additional_export_fields'])) {
  277. // get all the defined extra fields
  278. $extrafields = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  279. foreach ($_SESSION['additional_export_fields'] as $key => $extra_field_export) {
  280. $message .= '<li>'.$extrafields[$extra_field_export][3].'</li>';
  281. }
  282. Display::display_normal_message(get_lang('FollowingFieldsWillAlsoBeExported').': <br /><ul>'.$message.'</ul>', false);
  283. $message = '';
  284. }
  285. }
  286. }
  287. /**
  288. * Display a sortable table that contains an overview of all the reporting progress of all courses
  289. */
  290. function display_tracking_course_overview()
  291. {
  292. //MySpace::display_user_overview_export_options();
  293. $t_head = ' <table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  294. //$t_head .= ' <caption>'.get_lang('CourseInformation').'</caption>';
  295. $t_head .= '<tr>';
  296. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
  297. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
  298. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
  299. //$t_head .= ' <th><div style="width:40px">'.get_lang('AvgExercisesScore').'</div></th>';
  300. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
  301. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
  302. $t_head .= ' <th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
  303. //$t_head .= ' <th><div>'.get_lang('TotalExercisesScorePossible').'</div></th>';
  304. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
  305. //$t_head .= ' <th><div>'.get_lang('TotalExercisesScorePercentage').'</div></th>';
  306. //$t_head .= ' <th><div style="width:60px">'.get_lang('FirstLogin').'</div></th>';
  307. $t_head .= ' <th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
  308. $t_head .= ' </tr></table>';
  309. $addparams = array('view' => 'admin', 'display' => 'courseoverview');
  310. $table = new SortableTable('tracking_session_overview', array('MySpace','get_total_number_courses'), array('MySpace','get_course_data_tracking_overview'), 1);
  311. $table->additional_parameters = $addparams;
  312. $table->set_header(0, '', false, null, array('style' => 'display: none'));
  313. $table->set_header(1, get_lang('Course'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  314. $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;'));
  315. $table->set_column_filter(2, array('MySpace','course_tracking_filter'));
  316. $table->display();
  317. }
  318. /**
  319. * Get the total number of courses
  320. *
  321. * @return integer Total number of courses
  322. */
  323. public function get_total_number_courses() {
  324. // database table definition
  325. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  326. return Database::count_rows($main_course_table);
  327. }
  328. /**
  329. * Get data for the courses
  330. *
  331. * @param int Inferior limit
  332. * @param int Number of items to select
  333. * @param string Column to order on
  334. * @param string Order direction
  335. * @return array Results
  336. */
  337. public function get_course_data_tracking_overview($from, $number_of_items, $column, $direction) {
  338. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  339. $from = intval($from);
  340. $number_of_items = intval($number_of_items);
  341. $sql = "SELECT code AS col0, title AS col1, id FROM $main_course_table";
  342. $sql .= " ORDER BY col$column $direction ";
  343. $sql .= " LIMIT $from,$number_of_items";
  344. $result = Database::query($sql);
  345. $return = array ();
  346. while ($course = Database::fetch_row($result)) {
  347. $return[] = $course;
  348. }
  349. return $return;
  350. }
  351. /**
  352. * Fills in course reporting data
  353. *
  354. * @param integer course code
  355. * @param array $url_params additonal url parameters
  356. * @param array $row the row information (the other columns)
  357. * @return string html code
  358. */
  359. function course_tracking_filter($course_code, $url_params, $row) {
  360. $course_code = $row['code'];
  361. $courseId = $row['real_id'];
  362. // the table header
  363. $return = '<table class="data_table" style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  364. $courseInfo = api_get_course_info($course_code);
  365. $courseId = $courseInfo['real_id'];
  366. // database table definition
  367. $tbl_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  368. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  369. // getting all the courses of the user
  370. $sql = "SELECT * FROM $tbl_user AS u
  371. INNER JOIN $tbl_course_rel_user AS cu ON cu.user_id = u.user_id
  372. WHERE cu.c_id = '".$courseId."' AND ISNULL(cu.role);";
  373. $result = Database::query($sql);
  374. $time_spent = 0;
  375. $progress = 0;
  376. $nb_progress_lp = 0;
  377. $score = 0;
  378. $nb_score_lp = 0;
  379. $nb_messages = 0;
  380. $nb_assignments = 0;
  381. $last_login_date = false;
  382. $total_score_obtained = 0;
  383. $total_score_possible = 0;
  384. $total_questions_answered = 0;
  385. while ($row = Database::fetch_object($result)) {
  386. // get time spent in the course and session
  387. $time_spent += Tracking::get_time_spent_on_the_course($row->user_id, $courseId);
  388. $progress_tmp = Tracking::get_avg_student_progress($row->user_id, $courseId, array(), null, true);
  389. $progress += $progress_tmp[0];
  390. $nb_progress_lp += $progress_tmp[1];
  391. $score_tmp = Tracking :: get_avg_student_score($row->user_id, $courseId, array(), null, true);
  392. if(is_array($score_tmp)) {
  393. $score += $score_tmp[0];
  394. $nb_score_lp += $score_tmp[1];
  395. }
  396. $nb_messages += Tracking::count_student_messages($row->user_id, $courseId);
  397. $nb_assignments += Tracking::count_student_assignments($row->user_id, $courseId);
  398. $last_login_date_tmp = Tracking :: get_last_connection_date_on_the_course ($row->user_id, $courseId, null, false);
  399. if($last_login_date_tmp != false && $last_login_date == false) { // TODO: To be cleaned
  400. $last_login_date = $last_login_date_tmp;
  401. } else if($last_login_date_tmp != false && $last_login_date != false) { // TODO: Repeated previous condition. To be cleaned.
  402. // Find the max and assign it to first_login_date
  403. if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
  404. $last_login_date = $last_login_date_tmp;
  405. }
  406. }
  407. $exercise_results_tmp = MySpace::exercises_results($row->user_id, $row->c_id);
  408. $total_score_obtained += $exercise_results_tmp['score_obtained'];
  409. $total_score_possible += $exercise_results_tmp['score_possible'];
  410. $total_questions_answered += $exercise_results_tmp['questions_answered'];
  411. }
  412. if($nb_progress_lp > 0) {
  413. $avg_progress = round($progress / $nb_progress_lp, 2);
  414. } else {
  415. $avg_progress = 0;
  416. }
  417. if($nb_score_lp > 0) {
  418. $avg_score = round($score / $nb_score_lp, 2);
  419. } else {
  420. $avg_score = '-';
  421. }
  422. if($last_login_date) {
  423. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  424. } else {
  425. $last_login_date = '-';
  426. }
  427. if($total_score_possible > 0) {
  428. $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
  429. } else {
  430. $total_score_percentage = 0;
  431. }
  432. if($total_score_percentage > 0) {
  433. $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
  434. } else {
  435. $total_score = '-';
  436. }
  437. $return .= '<tr>';
  438. // time spent in the course
  439. $return .= ' <td style="width:164px;">'.api_time_to_hms($time_spent).'</td>';
  440. // student progress in course
  441. $return .= ' <td>'.$avg_progress.'</td>';
  442. // student score
  443. $return .= ' <td>'.$avg_score.'</td>';
  444. // student messages
  445. $return .= ' <td>'.$nb_messages.'</td>';
  446. // student assignments
  447. $return .= ' <td>'.$nb_assignments.'</td>';
  448. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  449. $return .= '<td width="105px;">'.$total_score.'</td>';
  450. $return .= '<td>'.$total_questions_answered.'</td>';
  451. // last connection
  452. $return .= ' <td>'.$last_login_date.'</td>';
  453. $return .= '</tr>';
  454. $return .= '</table>';
  455. return $return;
  456. }
  457. /**
  458. * This function exports the table that we see in display_tracking_course_overview()
  459. *
  460. */
  461. function export_tracking_course_overview()
  462. {
  463. // database table definition
  464. $tbl_course_rel_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  465. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  466. // the values of the sortable table
  467. if ($_GET['tracking_course_overview_page_nr']) {
  468. $from = $_GET['tracking_course_overview_page_nr'];
  469. } else {
  470. $from = 0;
  471. }
  472. if ($_GET['tracking_course_overview_column']) {
  473. $orderby = $_GET['tracking_course_overview_column'];
  474. } else {
  475. $orderby = 0;
  476. }
  477. if ($_GET['tracking_course_overview_direction']) {
  478. $direction = $_GET['tracking_course_overview_direction'];
  479. } else {
  480. $direction = 'ASC';
  481. }
  482. $course_data = MySpace::get_course_data_tracking_overview($from, 1000, $orderby, $direction);
  483. $csv_content = array();
  484. // the first line of the csv file with the column headers
  485. $csv_row = array();
  486. $csv_row[] = get_lang('Course', '');
  487. $csv_row[] = get_lang('AvgTimeSpentInTheCourse', '');
  488. $csv_row[] = get_lang('AvgStudentsProgress', '');
  489. $csv_row[] = get_lang('AvgCourseScore', '');
  490. $csv_row[] = get_lang('TotalNumberOfMessages', '');
  491. $csv_row[] = get_lang('TotalNumberOfAssignments', '');
  492. $csv_row[] = get_lang('TotalExercisesScoreObtained', '');
  493. $csv_row[] = get_lang('TotalExercisesScorePossible', '');
  494. $csv_row[] = get_lang('TotalExercisesAnswered', '');
  495. $csv_row[] = get_lang('TotalExercisesScorePercentage', '');
  496. $csv_row[] = get_lang('LatestLogin', '');
  497. $csv_content[] = $csv_row;
  498. // the other lines (the data)
  499. foreach ($course_data as $course) {
  500. $course_code = $course[0];
  501. $course_title = $course[1];
  502. $courseId = $course[2];
  503. $csv_row = array();
  504. $csv_row[] = $course_title;
  505. // getting all the courses of the session
  506. $sql = "SELECT * FROM $tbl_user AS u INNER JOIN $tbl_course_rel_user AS cu ON cu.user_id = u.user_id
  507. WHERE cu.c_id = '".$courseId."' AND ISNULL(cu.role);";
  508. $result = Database::query($sql);
  509. $time_spent = 0;
  510. $progress = 0;
  511. $nb_progress_lp = 0;
  512. $score = 0;
  513. $nb_score_lp = 0;
  514. $nb_messages = 0;
  515. $nb_assignments = 0;
  516. $last_login_date = false;
  517. $total_score_obtained = 0;
  518. $total_score_possible = 0;
  519. $total_questions_answered = 0;
  520. while ($row = Database::fetch_object($result)) {
  521. // get time spent in the course and session
  522. $time_spent += Tracking::get_time_spent_on_the_course($row->user_id, $courseId);
  523. $progress_tmp = Tracking::get_avg_student_progress($row->user_id, $courseId, array(), null, true);
  524. $progress += $progress_tmp[0];
  525. $nb_progress_lp += $progress_tmp[1];
  526. $score_tmp = Tracking :: get_avg_student_score($row->user_id, $courseId, array(), null, true);
  527. if(is_array($score_tmp)) {
  528. $score += $score_tmp[0];
  529. $nb_score_lp += $score_tmp[1];
  530. }
  531. $nb_messages += Tracking::count_student_messages($row->user_id, $courseId);
  532. $nb_assignments += Tracking::count_student_assignments($row->user_id, $courseId);
  533. $last_login_date_tmp = Tracking :: get_last_connection_date_on_the_course($row->user_id, $courseId, null, false);
  534. if($last_login_date_tmp != false && $last_login_date == false) { // TODO: To be cleaned.
  535. $last_login_date = $last_login_date_tmp;
  536. } else if($last_login_date_tmp != false && $last_login_date == false) { // TODO: Repeated previous condition. To be cleaned.
  537. // Find the max and assign it to first_login_date
  538. if(strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
  539. $last_login_date = $last_login_date_tmp;
  540. }
  541. }
  542. $exercise_results_tmp = MySpace::exercises_results($row->user_id, $row->c_id);
  543. $total_score_obtained += $exercise_results_tmp['score_obtained'];
  544. $total_score_possible += $exercise_results_tmp['score_possible'];
  545. $total_questions_answered += $exercise_results_tmp['questions_answered'];
  546. }
  547. if($nb_progress_lp > 0) {
  548. $avg_progress = round($progress / $nb_progress_lp, 2);
  549. } else {
  550. $avg_progress = 0;
  551. }
  552. if($nb_score_lp > 0) {
  553. $avg_score = round($score / $nb_score_lp, 2);
  554. } else {
  555. $avg_score = '-';
  556. }
  557. if($last_login_date) {
  558. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  559. } else {
  560. $last_login_date = '-';
  561. }
  562. if($total_score_possible > 0) {
  563. $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
  564. } else {
  565. $total_score_percentage = 0;
  566. }
  567. // time spent in the course
  568. $csv_row[] = api_time_to_hms($time_spent);
  569. // student progress in course
  570. $csv_row[] = $avg_progress;
  571. // student score
  572. $csv_row[] = $avg_score;
  573. // student messages
  574. $csv_row[] = $nb_messages;
  575. // student assignments
  576. $csv_row[] = $nb_assignments;
  577. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  578. $csv_row[] = $total_score_obtained;
  579. $csv_row[] = $total_score_possible;
  580. $csv_row[] = $total_questions_answered;
  581. $csv_row[] = $total_score_percentage;
  582. // last connection
  583. $csv_row[] = $last_login_date;
  584. $csv_content[] = $csv_row;
  585. }
  586. Export :: export_table_csv($csv_content, 'reporting_course_overview');
  587. exit;
  588. }
  589. /**
  590. * Display a sortable table that contains an overview of all the reporting progress of all sessions and all courses the user is subscribed to
  591. * @author Guillaume Viguier <guillaume@viguierjust.com>
  592. */
  593. function display_tracking_session_overview() {
  594. //MySpace::display_user_overview_export_options();
  595. $t_head = ' <table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  596. //$t_head .= ' <caption>'.get_lang('CourseInformation').'</caption>';
  597. $t_head .= '<tr>';
  598. $t_head .= ' <th width="155px" style="border-left:0;border-bottom:0"><span>'.get_lang('Course').'</span></th>';
  599. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
  600. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
  601. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
  602. //$t_head .= ' <th><div style="width:40px">'.get_lang('AvgExercisesScore').'</div></th>';
  603. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
  604. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
  605. $t_head .= ' <th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
  606. //$t_head .= ' <th><div>'.get_lang('TotalExercisesScorePossible').'</div></th>';
  607. $t_head .= ' <th style="padding:0;border-bottom:0"><span>'.Text::cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
  608. //$t_head .= ' <th><div>'.get_lang('TotalExercisesScorePercentage').'</div></th>';
  609. //$t_head .= ' <th><div style="width:60px">'.get_lang('FirstLogin').'</div></th>';
  610. $t_head .= ' <th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
  611. $t_head .= ' </tr></table>';
  612. $addparams = array('view' => 'admin', 'display' => 'sessionoverview');
  613. $table = new SortableTable('tracking_session_overview', array('MySpace','get_total_number_sessions'), array('MySpace','get_session_data_tracking_overview'), 1);
  614. $table->additional_parameters = $addparams;
  615. $table->set_header(0, '', false, null, array('style' => 'display: none'));
  616. $table->set_header(1, get_lang('Session'), true, array('style' => 'font-size:8pt'), array('style' => 'font-size:8pt'));
  617. $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;'));
  618. $table->set_column_filter(2, array('MySpace','session_tracking_filter'));
  619. $table->display();
  620. }
  621. /**
  622. * Get the total number of sessions
  623. *
  624. * @return integer Total number of sessions
  625. */
  626. public function get_total_number_sessions() {
  627. // database table definition
  628. $main_session_table = Database :: get_main_table(TABLE_MAIN_SESSION);
  629. return Database::count_rows($main_session_table);
  630. }
  631. /**
  632. * Get data for the sessions
  633. *
  634. * @param int Inferior limit
  635. * @param int Number of items to select
  636. * @param string Column to order on
  637. * @param string Order direction
  638. * @return array Results
  639. */
  640. public function get_session_data_tracking_overview($from, $number_of_items, $column, $direction) {
  641. //global $_configuration;
  642. // database table definition
  643. //$access_url_id = api_get_current_access_url_id();
  644. //$tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  645. $main_session_table = Database :: get_main_table(TABLE_MAIN_SESSION);
  646. /*if ($_configuration['multiple_access_urls']) {
  647. $condition_multi_url = ", $tbl_url_rel_user as url_user WHERE user.user_id=url_user.user_id AND access_url_id='$access_url_id'";
  648. }
  649. global $export_csv;
  650. if ($export_csv) {
  651. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  652. } else {
  653. $is_western_name_order = api_is_western_name_order();
  654. }*/
  655. $sql = "SELECT id AS col0, name AS col1 FROM $main_session_table";
  656. $sql .= " ORDER BY col$column $direction ";
  657. $sql .= " LIMIT $from,$number_of_items";
  658. $result = Database::query($sql);
  659. $return = array ();
  660. while ($session = Database::fetch_row($result)) {
  661. $return[] = $session;
  662. }
  663. return $return;
  664. }
  665. /**
  666. * Fills in session reporting data
  667. *
  668. * @param integer $user_id the id of the user
  669. * @param array $url_params additonal url parameters
  670. * @param array $row the row information (the other columns)
  671. * @return string html code
  672. */
  673. function session_tracking_filter($session_id, $url_params, $row) {
  674. $session_id = $row['code'];
  675. // the table header
  676. $return = '<table class="data_table" style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
  677. /*$return .= ' <tr>';
  678. $return .= ' <th>'.get_lang('Course').'</th>';
  679. $return .= ' <th>'.get_lang('AvgTimeSpentInTheCourse').'</th>';
  680. $return .= ' <th>'.get_lang('AvgStudentsProgress').'</th>';
  681. $return .= ' <th>'.get_lang('AvgCourseScore').'</th>';
  682. $return .= ' <th>'.get_lang('AvgExercisesScore').'</th>';
  683. $return .= ' <th>'.get_lang('AvgMessages').'</th>';
  684. $return .= ' <th>'.get_lang('AvgAssignments').'</th>';
  685. $return .= ' <th>'.get_lang('TotalExercisesScoreObtained').'</th>';
  686. $return .= ' <th>'.get_lang('TotalExercisesScorePossible').'</th>';
  687. $return .= ' <th>'.get_lang('TotalExercisesAnswered').'</th>';
  688. $return .= ' <th>'.get_lang('TotalExercisesScorePercentage').'</th>';
  689. $return .= ' <th>'.get_lang('FirstLogin').'</th>';
  690. $return .= ' <th>'.get_lang('LatestLogin').'</th>';
  691. $return .= ' </tr>';*/
  692. // Database table definition
  693. $tbl_session_rel_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  694. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  695. $tbl_session_rel_course_rel_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  696. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  697. // getting all the courses of the user
  698. $sql = "SELECT c.title, c.code, c.id FROM $tbl_course AS c INNER JOIN $tbl_session_rel_course AS sc ON sc.c_id = c.id
  699. WHERE sc.id_session = '".$session_id."';";
  700. $result = Database::query($sql);
  701. while ($row = Database::fetch_object($result)) {
  702. $return .= '<tr>';
  703. // course code
  704. $return .= '<td width="157px" >'.$row->title.'</td>';
  705. // get the users in the course
  706. $sql = "SELECT user_id FROM $tbl_user AS u INNER JOIN $tbl_session_rel_course_rel_user AS scu ON u.user_id = scu.id_user
  707. WHERE scu.id_session = '".$session_id."' AND scu.c_id = '".$row->id."'";
  708. $result_users = Database::query($sql);
  709. $time_spent = 0;
  710. $progress = 0;
  711. $nb_progress_lp = 0;
  712. $score = 0;
  713. $nb_score_lp = 0;
  714. $nb_messages = 0;
  715. $nb_assignments = 0;
  716. $last_login_date = false;
  717. $total_score_obtained = 0;
  718. $total_score_possible = 0;
  719. $total_questions_answered = 0;
  720. while ($row_user = Database::fetch_object($result_users)) {
  721. // get time spent in the course and session
  722. $time_spent += Tracking::get_time_spent_on_the_course($row_user->user_id, $row->id, $session_id);
  723. $progress_tmp = Tracking::get_avg_student_progress($row_user->user_id, $row->id, array(), $session_id, true);
  724. $progress += $progress_tmp[0];
  725. $nb_progress_lp += $progress_tmp[1];
  726. $score_tmp = Tracking :: get_avg_student_score($row_user->user_id, $row->id, array(), $session_id, true);
  727. if (is_array($score_tmp)) {
  728. $score += $score_tmp[0];
  729. $nb_score_lp += $score_tmp[1];
  730. }
  731. $nb_messages += Tracking::count_student_messages($row_user->user_id, $row->id, $session_id);
  732. $nb_assignments += Tracking::count_student_assignments($row_user->user_id, $row->id, $session_id);
  733. $last_login_date_tmp = Tracking :: get_last_connection_date_on_the_course ($row_user->user_id, $row->id, $session_id, false);
  734. if ($last_login_date_tmp != false && $last_login_date == false) { // TODO: To be cleaned.
  735. $last_login_date = $last_login_date_tmp;
  736. } else if($last_login_date_tmp != false && $last_login_date != false) { // TODO: Repeated previous condition! To be cleaned.
  737. // Find the max and assign it to first_login_date
  738. if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
  739. $last_login_date = $last_login_date_tmp;
  740. }
  741. }
  742. $exercise_results_tmp = MySpace::exercises_results($row_user->user_id, $row->id, $session_id);
  743. $total_score_obtained += $exercise_results_tmp['score_obtained'];
  744. $total_score_possible += $exercise_results_tmp['score_possible'];
  745. $total_questions_answered += $exercise_results_tmp['questions_answered'];
  746. }
  747. if ($nb_progress_lp > 0) {
  748. $avg_progress = round($progress / $nb_progress_lp, 2);
  749. } else {
  750. $avg_progress = 0;
  751. }
  752. if($nb_score_lp > 0) {
  753. $avg_score = round($score / $nb_score_lp, 2);
  754. } else {
  755. $avg_score = '-';
  756. }
  757. if($last_login_date) {
  758. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  759. } else {
  760. $last_login_date = '-';
  761. }
  762. if($total_score_possible > 0) {
  763. $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
  764. } else {
  765. $total_score_percentage = 0;
  766. }
  767. if($total_score_percentage > 0) {
  768. $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
  769. } else {
  770. $total_score = '-';
  771. }
  772. // time spent in the course
  773. $return .= ' <td><div>'.api_time_to_hms($time_spent).'</div></td>';
  774. // student progress in course
  775. $return .= ' <td><div>'.$avg_progress.'</div></td>';
  776. // student score
  777. $return .= ' <td><div>'.$avg_score.'</div></td>';
  778. // student messages
  779. $return .= ' <td><div>'.$nb_messages.'</div></td>';
  780. // student assignments
  781. $return .= ' <td><div>'.$nb_assignments.'</div></td>';
  782. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  783. $return .= '<td width="105px;">'.$total_score.'</td>';
  784. $return .= '<td>'.$total_questions_answered.'</td>';
  785. // last connection
  786. $return .= ' <td><div>'.$last_login_date.'</div></td>';
  787. $return .= '<tr>';
  788. }
  789. $return .= '</table>';
  790. return $return;
  791. }
  792. /**
  793. * This function exports the table that we see in display_tracking_session_overview()
  794. *
  795. */
  796. function export_tracking_session_overview() {
  797. // database table definition
  798. $tbl_session_rel_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  799. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  800. $tbl_session_rel_course_rel_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  801. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  802. // the values of the sortable table
  803. if ($_GET['tracking_session_overview_page_nr']) {
  804. $from = $_GET['tracking_session_overview_page_nr'];
  805. } else {
  806. $from = 0;
  807. }
  808. if ($_GET['tracking_session_overview_column']) {
  809. $orderby = $_GET['tracking_session_overview_column'];
  810. } else {
  811. $orderby = 0;
  812. }
  813. if ($_GET['tracking_session_overview_direction']) {
  814. $direction = $_GET['tracking_session_overview_direction'];
  815. } else {
  816. $direction = 'ASC';
  817. }
  818. $session_data = MySpace::get_session_data_tracking_overview($from, 1000, $orderby, $direction);
  819. $csv_content = array();
  820. // the first line of the csv file with the column headers
  821. $csv_row = array();
  822. $csv_row[] = get_lang('Session');
  823. $csv_row[] = get_lang('Course', '');
  824. $csv_row[] = get_lang('AvgTimeSpentInTheCourse', '');
  825. $csv_row[] = get_lang('AvgStudentsProgress', '');
  826. $csv_row[] = get_lang('AvgCourseScore', '');
  827. $csv_row[] = get_lang('TotalNumberOfMessages', '');
  828. $csv_row[] = get_lang('TotalNumberOfAssignments', '');
  829. $csv_row[] = get_lang('TotalExercisesScoreObtained', '');
  830. $csv_row[] = get_lang('TotalExercisesScorePossible', '');
  831. $csv_row[] = get_lang('TotalExercisesAnswered', '');
  832. $csv_row[] = get_lang('TotalExercisesScorePercentage', '');
  833. $csv_row[] = get_lang('LatestLogin', '');
  834. $csv_content[] = $csv_row;
  835. // the other lines (the data)
  836. foreach ($session_data as $key => $session) {
  837. $session_id = $session[0];
  838. $session_title = $session[1];
  839. // getting all the courses of the session
  840. $sql = "SELECT c.title, c.code, c.id FROM $tbl_course AS c INNER JOIN $tbl_session_rel_course AS sc ON sc.c_id = c.id
  841. WHERE sc.id_session = '".$session_id."';";
  842. $result = Database::query($sql);
  843. while ($row = Database::fetch_object($result)) {
  844. $csv_row = array();
  845. $csv_row[] = $session_title;
  846. $csv_row[] = $row->title;
  847. // get the users in the course
  848. $sql = "SELECT user_id FROM $tbl_user AS u INNER JOIN $tbl_session_rel_course_rel_user AS scu ON u.user_id = scu.id_user
  849. WHERE scu.id_session = '".$session_id."' AND scu.c_id = '".$row->id."'";
  850. $result_users = Database::query($sql);
  851. $time_spent = 0;
  852. $progress = 0;
  853. $nb_progress_lp = 0;
  854. $score = 0;
  855. $nb_score_lp = 0;
  856. $nb_messages = 0;
  857. $nb_assignments = 0;
  858. $last_login_date = false;
  859. $total_score_obtained = 0;
  860. $total_score_possible = 0;
  861. $total_questions_answered = 0;
  862. while ($row_user = Database::fetch_object($result_users)) {
  863. // get time spent in the course and session
  864. $time_spent += Tracking::get_time_spent_on_the_course($row_user->user_id, $row->id, $session_id);
  865. $progress_tmp = Tracking::get_avg_student_progress($row_user->user_id, $row->id, array(), $session_id, true);
  866. $progress += $progress_tmp[0];
  867. $nb_progress_lp += $progress_tmp[1];
  868. $score_tmp = Tracking :: get_avg_student_score($row_user->user_id, $row->id, array(), $session_id, true);
  869. if(is_array($score_tmp)) {
  870. $score += $score_tmp[0];
  871. $nb_score_lp += $score_tmp[1];
  872. }
  873. $nb_messages += Tracking::count_student_messages($row_user->user_id, $row->id, $session_id);
  874. $nb_assignments += Tracking::count_student_assignments($row_user->user_id, $row->id, $session_id);
  875. $last_login_date_tmp = Tracking :: get_last_connection_date_on_the_course ($row_user->user_id, $row->id, $session_id, false);
  876. if ($last_login_date_tmp != false && $last_login_date == false) { // TODO: To be cleaned.
  877. $last_login_date = $last_login_date_tmp;
  878. } else if($last_login_date_tmp != false && $last_login_date == false) { // TODO: Repeated previous condition. To be cleaned.
  879. // Find the max and assign it to first_login_date
  880. if(strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
  881. $last_login_date = $last_login_date_tmp;
  882. }
  883. }
  884. $exercise_results_tmp = MySpace::exercises_results($row_user->user_id, $row->id, $session_id);
  885. $total_score_obtained += $exercise_results_tmp['score_obtained'];
  886. $total_score_possible += $exercise_results_tmp['score_possible'];
  887. $total_questions_answered += $exercise_results_tmp['questions_answered'];
  888. }
  889. if($nb_progress_lp > 0) {
  890. $avg_progress = round($progress / $nb_progress_lp, 2);
  891. } else {
  892. $avg_progress = 0;
  893. }
  894. if($nb_score_lp > 0) {
  895. $avg_score = round($score / $nb_score_lp, 2);
  896. } else {
  897. $avg_score = '-';
  898. }
  899. if($last_login_date) {
  900. $last_login_date = api_convert_and_format_date($last_login_date, DATE_FORMAT_SHORT, date_default_timezone_get());
  901. } else {
  902. $last_login_date = '-';
  903. }
  904. if($total_score_possible > 0) {
  905. $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
  906. } else {
  907. $total_score_percentage = 0;
  908. }
  909. if($total_score_percentage > 0) {
  910. $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
  911. } else {
  912. $total_score = '-';
  913. }
  914. // time spent in the course
  915. $csv_row[] = api_time_to_hms($time_spent);
  916. // student progress in course
  917. $csv_row[] = $avg_progress;
  918. // student score
  919. $csv_row[] = $avg_score;
  920. // student messages
  921. $csv_row[] = $nb_messages;
  922. // student assignments
  923. $csv_row[] = $nb_assignments;
  924. // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
  925. $csv_row[] = $total_score_obtained;
  926. $csv_row[] = $total_score_possible;
  927. $csv_row[] = $total_questions_answered;
  928. $csv_row[] = $total_score_percentage;
  929. // last connection
  930. $csv_row[] = $last_login_date;
  931. $csv_content[] = $csv_row;
  932. }
  933. }
  934. Export :: export_table_csv($csv_content, 'reporting_session_overview');
  935. exit;
  936. }
  937. /**
  938. * Get general information about the exercise performance of the user
  939. * the total obtained score (all the score on all the questions)
  940. * the maximum score that could be obtained
  941. * the number of questions answered
  942. * the success percentage
  943. * @param integer $user_id the id of the user
  944. * @param string $course_code the course code
  945. * @return array
  946. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  947. * @version Dokeos 1.8.6
  948. * @since November 2008
  949. */
  950. function exercises_results($user_id, $courseId, $session_id = false) {
  951. $sql = 'SELECT exe_result , exe_weighting
  952. FROM '.Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES)."
  953. WHERE c_id = '".Database::escape_string($courseId)."'
  954. AND exe_user_id = '".Database::escape_string($user_id)."'";
  955. if ($session_id !== false) {
  956. $sql .= " AND session_id = '".$session_id."' ";
  957. }
  958. $result = Database::query($sql);
  959. $score_obtained = 0;
  960. $score_possible = 0;
  961. $questions_answered = 0;
  962. while ($row = Database::fetch_array($result)) {
  963. $score_obtained += $row['exe_result'];
  964. $score_possible += $row['exe_weighting'];
  965. $questions_answered ++;
  966. }
  967. if ($score_possible != 0) {
  968. $percentage = round(($score_obtained / $score_possible * 100), 2);
  969. } else {
  970. $percentage = null;
  971. }
  972. return array(
  973. 'score_obtained' => $score_obtained,
  974. 'score_possible' => $score_possible,
  975. 'questions_answered' => $questions_answered,
  976. 'percentage' => $percentage
  977. );
  978. }
  979. /**
  980. * This function exports the table that we see in display_tracking_user_overview()
  981. *
  982. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  983. * @version Dokeos 1.8.6
  984. * @since October 2008
  985. */
  986. function export_tracking_user_overview()
  987. {
  988. // database table definitions
  989. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  990. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  991. $sort_by_first_name = api_sort_by_first_name();
  992. // the values of the sortable table
  993. if ($_GET['tracking_user_overview_page_nr']) {
  994. $from = $_GET['tracking_user_overview_page_nr'];
  995. } else {
  996. $from = 0;
  997. }
  998. if ($_GET['tracking_user_overview_column']) {
  999. $orderby = $_GET['tracking_user_overview_column'];
  1000. } else {
  1001. $orderby = 0;
  1002. }
  1003. if ($is_western_name_order != api_is_western_name_order() && ($orderby == 1 || $orderby == 2)) {
  1004. // Swapping the sorting column if name order for export is different than the common name order.
  1005. $orderby = 3 - $orderby;
  1006. }
  1007. if ($_GET['tracking_user_overview_direction']) {
  1008. $direction = $_GET['tracking_user_overview_direction'];
  1009. } else {
  1010. $direction = 'ASC';
  1011. }
  1012. $user_data = MySpace::get_user_data_tracking_overview($from, 1000, $orderby, $direction);
  1013. // the first line of the csv file with the column headers
  1014. $csv_row = array();
  1015. $csv_row[] = get_lang('OfficialCode');
  1016. if ($is_western_name_order) {
  1017. $csv_row[] = get_lang('FirstName', '');
  1018. $csv_row[] = get_lang('LastName', '');
  1019. } else {
  1020. $csv_row[] = get_lang('LastName', '');
  1021. $csv_row[] = get_lang('FirstName', '');
  1022. }
  1023. $csv_row[] = get_lang('LoginName');
  1024. $csv_row[] = get_lang('CourseCode');
  1025. // the additional user defined fields (only those that were selected to be exported)
  1026. $fields = UserManager::get_extra_fields(0, 50, 5, 'ASC');
  1027. if (is_array($_SESSION['additional_export_fields'])) {
  1028. foreach ($_SESSION['additional_export_fields'] as $key => $extra_field_export) {
  1029. $csv_row[] = $fields[$extra_field_export][3];
  1030. $field_names_to_be_exported[] = 'extra_'.$fields[$extra_field_export][1];
  1031. }
  1032. }
  1033. $csv_row[] = get_lang('AvgTimeSpentInTheCourse', '');
  1034. $csv_row[] = get_lang('AvgStudentsProgress', '');
  1035. $csv_row[] = get_lang('AvgCourseScore', '');
  1036. $csv_row[] = get_lang('AvgExercisesScore', '');
  1037. $csv_row[] = get_lang('AvgMessages', '');
  1038. $csv_row[] = get_lang('AvgAssignments', '');
  1039. $csv_row[] = get_lang('TotalExercisesScoreObtained', '');
  1040. $csv_row[] = get_lang('TotalExercisesScorePossible', '');
  1041. $csv_row[] = get_lang('TotalExercisesAnswered', '');
  1042. $csv_row[] = get_lang('TotalExercisesScorePercentage', '');
  1043. $csv_row[] = get_lang('FirstLogin', '');
  1044. $csv_row[] = get_lang('LatestLogin', '');
  1045. $csv_content[] = $csv_row;
  1046. // the other lines (the data)
  1047. foreach ($user_data as $key => $user) {
  1048. // getting all the courses of the user
  1049. $sql = "SELECT c_id FROM $tbl_course_user
  1050. WHERE user_id = '".Database::escape_string($user[4])."' AND relation_type <> ".COURSE_RELATION_TYPE_RRHH." ";
  1051. $result = Database::query($sql);
  1052. while ($row = Database::fetch_array($result, 'ASSOC')) {
  1053. $csv_row = array();
  1054. // user official code
  1055. $csv_row[] = $user[0];
  1056. // user first|last name
  1057. $csv_row[] = $user[1];
  1058. // user last|first name
  1059. $csv_row[] = $user[2];
  1060. // user login name
  1061. $csv_row[] = $user[3];
  1062. // course code
  1063. $csv_row[] = $row['code'];
  1064. // the additional defined user fields
  1065. $extra_fields = MySpace::get_user_overview_export_extra_fields($user[4]);
  1066. if (is_array($field_names_to_be_exported)) {
  1067. foreach ($field_names_to_be_exported as $key => $extra_field_export) {
  1068. $csv_row[] = $extra_fields[$extra_field_export];
  1069. }
  1070. }
  1071. // time spent in the course
  1072. $csv_row[] = api_time_to_hms(Tracking :: get_time_spent_on_the_course ($user[4], $row['id']));
  1073. // student progress in course
  1074. $csv_row[] = round(Tracking :: get_avg_student_progress ($user[4], $row['id']), 2);
  1075. // student score
  1076. $csv_row[] = round(Tracking :: get_avg_student_score ($user[4], $row['id']), 2);
  1077. // student tes score
  1078. $csv_row[] = round(Tracking :: get_avg_student_exercise_score ($user[4], $row['id']), 2);
  1079. // student messages
  1080. $csv_row[] = Tracking :: count_student_messages ($user[4], $row['id']);
  1081. // student assignments
  1082. $csv_row[] = Tracking :: count_student_assignments ($user[4], $row['id']);
  1083. // student exercises results
  1084. $exercises_results = MySpace::exercises_results($user[4], $row['id']);
  1085. $csv_row[] = $exercises_results['score_obtained'];
  1086. $csv_row[] = $exercises_results['score_possible'];
  1087. $csv_row[] = $exercises_results['questions_answered'];
  1088. $csv_row[] = $exercises_results['percentage'];
  1089. // first connection
  1090. $csv_row[] = Tracking :: get_first_connection_date_on_the_course ($user[4], $row['id']);
  1091. // last connection
  1092. $csv_row[] = strip_tags(Tracking :: get_last_connection_date_on_the_course ($user[4], $row['id']));
  1093. $csv_content[] = $csv_row;
  1094. }
  1095. }
  1096. Export :: export_table_csv($csv_content, 'reporting_user_overview');
  1097. exit;
  1098. }
  1099. /**
  1100. * Get data for courses list in sortable with pagination
  1101. * @return array
  1102. */
  1103. static function get_course_data($from, $number_of_items, $column, $direction, $courses, $csv_content, $charset)
  1104. {
  1105. $session_id = api_get_session_id();
  1106. // definition database tables
  1107. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  1108. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1109. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1110. $course_data = array();
  1111. $courseList = isset($courses) ? array_keys($courses) : array();
  1112. foreach($courseList as &$code) {
  1113. $code = "'$code'";
  1114. }
  1115. // get all courses with limit
  1116. $sql = "SELECT course.code, course.title, id
  1117. FROM $tbl_course course
  1118. WHERE course.id IN (".implode(',',$courseList).")";
  1119. if (!in_array($direction, array('ASC','DESC')))
  1120. $direction = 'ASC';
  1121. $column = intval($column);
  1122. $from = intval($from);
  1123. $number_of_items = intval($number_of_items);
  1124. if (!empty($column)) {
  1125. $sql .= " ORDER BY $column $direction ";
  1126. }
  1127. if (!empty($from)) {
  1128. $sql .= " LIMIT $from,$number_of_items";
  1129. }
  1130. $res = Database::query($sql);
  1131. while ($row_course = Database::fetch_array($res, 'ASSOC')) {
  1132. $courseCode = $row_course['code'];
  1133. $courseId = $row_course['id'];
  1134. $nb_students_in_course = $avg_progress_in_course = $avg_score_in_course = $avg_time_spent_in_course = $avg_score_in_exercise = 0;
  1135. // students directly subscribed to the course
  1136. if (empty($session_id)) {
  1137. $sql = "SELECT user_id FROM $tbl_course_user as course_rel_user WHERE course_rel_user.status='5' AND course_rel_user.c_id='$courseId'";
  1138. } else {
  1139. $sql = "SELECT id_user as user_id FROM $tbl_session_course_user srcu WHERE srcu.c_id= $courseId AND id_session = '$session_id' AND srcu.status<>2";
  1140. }
  1141. $rs = Database::query($sql);
  1142. $users = array();
  1143. while ($row = Database::fetch_array($rs)) {
  1144. $users[] = $row['user_id'];
  1145. }
  1146. if (count($users) > 0) {
  1147. $nb_students_in_course = count($users);
  1148. $avg_assignments_in_course = Tracking::count_student_assignments($users, $courseId, $session_id);
  1149. $avg_messages_in_course = Tracking::count_student_messages($users, $courseId, $session_id);
  1150. $avg_progress_in_course = Tracking::get_avg_student_progress($users, $courseId, array(), $session_id);
  1151. $avg_score_in_course = Tracking::get_avg_student_score($users, $courseId, array(), $session_id);
  1152. $avg_score_in_exercise = Tracking::get_avg_student_exercise_score($users, $courseId, 0, $session_id);
  1153. $avg_time_spent_in_course = Tracking::get_time_spent_on_the_course($users, $courseId, $session_id);
  1154. $avg_progress_in_course = round($avg_progress_in_course / $nb_students_in_course, 2);
  1155. if (is_numeric($avg_score_in_course)) {
  1156. $avg_score_in_course = round($avg_score_in_course / $nb_students_in_course, 2);
  1157. }
  1158. $avg_time_spent_in_course = api_time_to_hms($avg_time_spent_in_course / $nb_students_in_course);
  1159. } else {
  1160. $avg_time_spent_in_course = null;
  1161. $avg_progress_in_course = null;
  1162. $avg_score_in_course = null;
  1163. $avg_score_in_exercise = null;
  1164. $avg_messages_in_course = null;
  1165. $avg_assignments_in_course = null;
  1166. }
  1167. $table_row = array();
  1168. $table_row[] = $row_course['title'];
  1169. $table_row[] = $nb_students_in_course;
  1170. $table_row[] = $avg_time_spent_in_course;
  1171. $table_row[] = is_null($avg_progress_in_course) ? '' : $avg_progress_in_course.'%';
  1172. $table_row[] = is_null($avg_score_in_course) ? '' : $avg_score_in_course.'%';
  1173. $table_row[] = is_null($avg_score_in_exercise) ? '' : $avg_score_in_exercise.'%';
  1174. $table_row[] = $avg_messages_in_course;
  1175. $table_row[] = $avg_assignments_in_course;
  1176. //set the "from" value to know if I access the Reporting by the chamilo tab or the course link
  1177. $table_row[] = '<center><a href="../tracking/courseLog.php?cidReq='.$courseCode.'&from=myspace&id_session='.$session_id.'">
  1178. <img src="'.api_get_path(WEB_IMG_PATH).'2rightarrow.gif" border="0" /></a>
  1179. </center>';
  1180. $csv_content[] = array(
  1181. api_html_entity_decode($row_course['title'], ENT_QUOTES, $charset),
  1182. $nb_students_in_course,
  1183. $avg_time_spent_in_course,
  1184. is_null($avg_progress_in_course) ? null : $avg_progress_in_course.'%',
  1185. is_null($avg_score_in_course) ? null : is_numeric($avg_score_in_course) ? $avg_score_in_course.'%' : $avg_score_in_course ,
  1186. is_null($avg_score_in_exercise) ? null : $avg_score_in_exercise.'%',
  1187. $avg_messages_in_course,
  1188. $avg_assignments_in_course,
  1189. );
  1190. $course_data[] = $table_row;
  1191. }
  1192. return $course_data;
  1193. }
  1194. /**
  1195. * get the numer of users of the platform
  1196. *
  1197. * @return integer
  1198. *
  1199. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1200. * @version Dokeos 1.8.6
  1201. * @since October 2008
  1202. */
  1203. function get_number_of_users_tracking_overview() {
  1204. // database table definition
  1205. $main_user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1206. return Database::count_rows($main_user_table);
  1207. }
  1208. /**
  1209. * get all the data for the sortable table of the reporting progress of all users and all the courses the user is subscribed to.
  1210. *
  1211. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1212. * @version Dokeos 1.8.6
  1213. * @since October 2008
  1214. */
  1215. function get_user_data_tracking_overview($from, $number_of_items, $column, $direction) {
  1216. global $_configuration;
  1217. // database table definition
  1218. $access_url_id = api_get_current_access_url_id();
  1219. $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  1220. $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
  1221. if ($_configuration['multiple_access_urls']) {
  1222. $condition_multi_url = ", $tbl_url_rel_user as url_user WHERE user.user_id=url_user.user_id AND access_url_id='$access_url_id'";
  1223. }
  1224. global $export_csv;
  1225. if ($export_csv) {
  1226. $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
  1227. } else {
  1228. $is_western_name_order = api_is_western_name_order();
  1229. }
  1230. $sql = "SELECT
  1231. official_code AS col0,
  1232. ".($is_western_name_order ? "
  1233. firstname AS col1,
  1234. lastname AS col2,
  1235. " : "
  1236. lastname AS col1,
  1237. firstname AS col2,
  1238. ").
  1239. "username AS col3,
  1240. user.user_id AS col4
  1241. FROM
  1242. $main_user_table as user $condition_multi_url
  1243. ";
  1244. $sql .= " ORDER BY col$column $direction ";
  1245. $sql .= " LIMIT $from,$number_of_items";
  1246. $result = Database::query($sql);
  1247. $return = array ();
  1248. while ($user = Database::fetch_row($result)) {
  1249. $return[] = $user;
  1250. }
  1251. return $return;
  1252. }
  1253. /**
  1254. * Get all information that the user with user_id = $user_data has
  1255. * entered in the additionally defined profile fields
  1256. * @param integer $user_id the id of the user
  1257. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1258. * @version Dokeos 1.8.6
  1259. * @since November 2008
  1260. */
  1261. function get_user_overview_export_extra_fields($user_id) {
  1262. $extra_data = UserManager::get_extra_user_data($user_id, true);
  1263. return $extra_data;
  1264. }
  1265. /**
  1266. * Checks if a username exist in the DB otherwise it create a "double"
  1267. * i.e. if we look into for jmontoya but the user's name already exist we create the user jmontoya2
  1268. * the return array will be array(username=>'jmontoya', sufix='2')
  1269. * @param string firstname
  1270. * @param string lastname
  1271. * @param string username
  1272. * @return array with the username, the sufix
  1273. * @author Julio Montoya Armas
  1274. */
  1275. function make_username($firstname, $lastname, $username, $language = null, $encoding = null) {
  1276. // if username exist
  1277. if (!UserManager::is_username_available($username) || empty($username)) {
  1278. $i = 0;
  1279. while (1) {
  1280. if ($i == 0) {
  1281. $sufix = '';
  1282. } else {
  1283. $sufix = $i;
  1284. }
  1285. $desired_username = UserManager::create_username($firstname, $lastname, $language, $encoding);
  1286. if (UserManager::is_username_available($desired_username.$sufix)) {
  1287. break;
  1288. } else {
  1289. $i++;
  1290. }
  1291. }
  1292. $username_array = array('username' => $desired_username , 'sufix' => $sufix);
  1293. return $username_array;
  1294. } else {
  1295. $username_array = array('username' => $username, 'sufix' => '');
  1296. return $username_array;
  1297. }
  1298. }
  1299. /**
  1300. * Checks if there are repeted users in a given array
  1301. * @param array $usernames list of the usernames in the uploaded file
  1302. * @param array $user_array['username'] and $user_array['sufix'] where sufix is the number part in a login i.e -> jmontoya2
  1303. * @return array with the $usernames array and the $user_array array
  1304. * @author Julio Montoya Armas
  1305. */
  1306. function check_user_in_array($usernames, $user_array) {
  1307. $user_list = array_keys($usernames);
  1308. $username = $user_array['username'].$user_array['sufix'];
  1309. if (in_array($username, $user_list)) {
  1310. $user_array['sufix'] += $usernames[$username];
  1311. $usernames[$username]++;
  1312. } else {
  1313. $usernames[$username] = 1;
  1314. }
  1315. $result_array = array($usernames, $user_array);
  1316. return $result_array;
  1317. }
  1318. /**
  1319. * Checks whether a username has been already subscribed in a session.
  1320. * @param string a given username
  1321. * @param array the array with the course list codes
  1322. * @param the session id
  1323. * @return 0 if the user is not subscribed otherwise it returns the user_id of the given username
  1324. * @author Julio Montoya Armas
  1325. */
  1326. function user_available_in_session($username, $course_list, $id_session) {
  1327. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1328. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1329. $id_session = intval($id_session);
  1330. $username = Database::escape_string($username);
  1331. foreach ($course_list as $courseId) {
  1332. $courseId = intval($courseId);
  1333. $sql_select = " SELECT u.user_id FROM $tbl_session_rel_course_rel_user rel INNER JOIN $table_user u
  1334. ON (rel.id_user=u.user_id)
  1335. WHERE rel.id_session='$id_session' AND u.status='5' AND u.username ='$username' AND rel.c_id ='$courseId'";
  1336. $rs = Database::query($sql_select);
  1337. if (Database::num_rows($rs) > 0) {
  1338. return Database::result($rs, 0, 0);
  1339. } else {
  1340. return 0;
  1341. }
  1342. }
  1343. }
  1344. /**
  1345. This function checks whether some users in the uploaded file repeated and creates unique usernames if necesary.
  1346. A case: Within the file there is an user repeted twice (Julio Montoya / Julio Montoya) and the username fields are empty.
  1347. Then, this function would create unique usernames based on the first and the last name. Two users wiould be created - jmontoya and jmontoya2.
  1348. Of course, if in the database there is a user with the name jmontoya, the newly created two users registered would be jmontoya2 and jmontoya3.
  1349. @param $users list of users
  1350. @author Julio Montoya Armas
  1351. */
  1352. function check_all_usernames($users, $course_list, $id_session) {
  1353. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1354. $usernames = array();
  1355. $new_users = array();
  1356. foreach ($users as $index => $user) {
  1357. $desired_username = array();
  1358. if (empty($user['UserName'])) {
  1359. $desired_username = MySpace::make_username($user['FirstName'], $user['LastName'], '');
  1360. $pre_username = $desired_username['username'].$desired_username['sufix'];
  1361. $user['UserName'] = $pre_username;
  1362. $user['create'] = '1';
  1363. } else {
  1364. if (UserManager::is_username_available($user['UserName'])) {
  1365. $desired_username = MySpace::make_username($user['FirstName'], $user['LastName'], $user['UserName']);
  1366. $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
  1367. $user['create'] = '1';
  1368. } else {
  1369. $is_session_avail = MySpace::user_available_in_session($user['UserName'], $course_list, $id_session);
  1370. if ($is_session_avail == 0) {
  1371. $user_name = $user['UserName'];
  1372. $sql_select = "SELECT user_id FROM $table_user WHERE username ='$user_name' ";
  1373. $rs = Database::query($sql_select);
  1374. $user['create'] = Database::result($rs, 0, 0); // This should be the ID because the user exists.
  1375. } else {
  1376. $user['create'] = $is_session_avail;
  1377. }
  1378. }
  1379. }
  1380. // Usernames is the current list of users in the file.
  1381. $result_array = MySpace::check_user_in_array($usernames, $desired_username);
  1382. $usernames = $result_array[0];
  1383. $desired_username = $result_array[1];
  1384. $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
  1385. $new_users[] = $user;
  1386. }
  1387. return $new_users;
  1388. }
  1389. /**
  1390. * This functions checks whether there are users that are already registered in the DB by different creator than the current coach.
  1391. * @param string a given username
  1392. * @param array the array with the course list codes
  1393. * @param the session id
  1394. * @author Julio Montoya Armas
  1395. */
  1396. function get_user_creator($users, $course_list, $id_session) {
  1397. $errors = array();
  1398. foreach ($users as $user) {
  1399. // database table definition
  1400. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1401. $username = Database::escape_string($user['UserName']);
  1402. //echo "<br>";
  1403. $sql = "SELECT creator_id FROM $table_user WHERE username='$username' ";
  1404. $rs = Database::query($sql);
  1405. $creator_id = Database::result($rs, 0, 0);
  1406. // check if we are the creators or not
  1407. if ($creator_id != '') {
  1408. if ($creator_id != api_get_user_id()) {
  1409. $user['error'] = get_lang('UserAlreadyRegisteredByOtherCreator');
  1410. $errors[] = $user;
  1411. }
  1412. }
  1413. }
  1414. return $errors;
  1415. }
  1416. /**
  1417. * Validates imported data.
  1418. * @param list of users
  1419. */
  1420. function validate_data($users, $id_session = null) {
  1421. $errors = array();
  1422. $usernames = array();
  1423. $new_users = array();
  1424. foreach ($users as $index => $user) {
  1425. // 1. Check whether mandatory fields are set.
  1426. $mandatory_fields = array('LastName', 'FirstName');
  1427. if (api_get_setting('registration', 'email') == 'true') {
  1428. $mandatory_fields[] = 'Email';
  1429. }
  1430. foreach ($mandatory_fields as $key => $field) {
  1431. if (!isset ($user[$field]) || strlen($user[$field]) == 0) {
  1432. $user['error'] = get_lang($field.'Mandatory');
  1433. $errors[] = $user;
  1434. }
  1435. }
  1436. // 2. Check whether the username is too long.
  1437. if (UserManager::is_username_too_long($user['UserName'])) {
  1438. $user['error'] = get_lang('UserNameTooLong');
  1439. $errors[] = $user;
  1440. }
  1441. $user['UserName'] = trim($user['UserName']);
  1442. if (empty($user['UserName'])) {
  1443. $user['UserName'] = UserManager::create_username($user['FirstName'], $user['LastName']);
  1444. }
  1445. $new_users[] = $user;
  1446. }
  1447. $results = array('errors' => $errors, 'users' => $new_users);
  1448. return $results;
  1449. }
  1450. /**
  1451. * Adds missing user-information (which isn't required, like password, etc).
  1452. */
  1453. function complete_missing_data($user) {
  1454. // 1. Generate a password if it is necessary.
  1455. if (!isset ($user['Password']) || strlen($user['Password']) == 0) {
  1456. $user['Password'] = api_generate_password();
  1457. }
  1458. return $user;
  1459. }
  1460. /**
  1461. * Saves imported data.
  1462. */
  1463. function save_data($users, $course_list, $id_session) {
  1464. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1465. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1466. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1467. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1468. $id_session = intval($id_session);
  1469. $sendMail = $_POST['sendMail'] ? 1 : 0;
  1470. // Adding users to the platform.
  1471. $new_users = array();
  1472. foreach ($users as $user) {
  1473. $user = MySpace::complete_missing_data($user);
  1474. // coach only will registered users
  1475. $default_status = '5';
  1476. if ($user['create'] == '1') {
  1477. $user['id'] = UserManager :: create_user($user['FirstName'], $user['LastName'], $default_status, $user['Email'], $user['UserName'], $user['Password'], $user['OfficialCode'], api_get_setting('PlatformLanguage'), $user['PhoneNumber'], '');
  1478. $user['added_at_platform'] = 1;
  1479. } else {
  1480. $user['id'] = $user['create'];
  1481. $user['added_at_platform'] = 0;
  1482. }
  1483. $new_users[] = $user;
  1484. }
  1485. // Update user list.
  1486. $users = $new_users;
  1487. // Inserting users.
  1488. $super_list = array();
  1489. foreach ($course_list as $enreg_course) {
  1490. $nbr_users = 0;
  1491. $new_users = array();
  1492. $enreg_course = Database::escape_string($enreg_course);
  1493. foreach ($users as $user) {
  1494. $userid = intval($user['id']);
  1495. $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,c_id,id_user) VALUES('$id_session','$enreg_course','$userid')";
  1496. $result = Database::query($sql);
  1497. if (Database::affected_rows($result)) {
  1498. $nbr_users++;
  1499. }
  1500. $new_users[] = $user;
  1501. }
  1502. $super_list[] = $new_users;
  1503. //update the nbr_users field
  1504. $sql_select = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND c_id ='$enreg_course'";
  1505. $rs = Database::query($sql_select);
  1506. list($nbr_users) = Database::fetch_array($rs);
  1507. $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND c_id='$enreg_course'";
  1508. Database::query($sql_update);
  1509. $sql_update = "UPDATE $tbl_session SET nbr_users= '$nbr_users' WHERE id='$id_session'";
  1510. Database::query($sql_update);
  1511. }
  1512. // We don't delete the users (thoughts while dreaming)
  1513. //$sql_delete = "DELETE FROM $tbl_session_rel_user WHERE id_session = '$id_session'";
  1514. //Database::query($sql_delete);
  1515. $new_users = array();
  1516. foreach ($users as $user) {
  1517. $userid = $user['id'];
  1518. $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$userid')";
  1519. Database::query($sql_insert);
  1520. $user['added_at_session'] = 1;
  1521. $new_users[] = $user;
  1522. }
  1523. $users = $new_users;
  1524. $registered_users = get_lang('FileImported').'<br /> Import file results : <br />';
  1525. // Sending emails
  1526. if ($sendMail) {
  1527. $i = 0;
  1528. $from = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  1529. $fromEmail = api_get_setting('emailAdministrator');
  1530. foreach ($users as $user) {
  1531. $emailto = api_get_person_name($user['FirstName'], $user['LastName'], null, PERSON_NAME_EMAIL_ADDRESS).' <'.$user['Email'].'>';
  1532. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  1533. $emailbody = get_lang('Dear').' '.api_get_person_name($user['FirstName'], $user['LastName']).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : $user[UserName]\n".get_lang('Pass')." : $user[Password]\n\n".get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".api_get_path(WEB_PATH)." \n\n".get_lang('Problem')."\n\n".get_lang('Formula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator');
  1534. //$emailheaders = 'From: '.api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS).' <'.api_get_setting('emailAdministrator').">\n";
  1535. //$emailheaders .= 'Reply-To: '.api_get_setting('emailAdministrator');
  1536. api_mail_html($emailto, $recipient_email, $emailsubject, $emailbody, $from, $fromEmail);
  1537. if (($user['added_at_platform'] == 1 && $user['added_at_session'] == 1) || $user['added_at_session'] == 1) {
  1538. if ($user['added_at_platform'] == 1) {
  1539. $addedto = get_lang('UserCreatedPlatform');
  1540. } else {
  1541. $addedto = ' ';
  1542. }
  1543. if ($user['added_at_session'] == 1) {
  1544. $addedto .= get_lang('UserInSession');
  1545. }
  1546. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  1547. } else {
  1548. $addedto = get_lang('UserNotAdded');
  1549. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  1550. }
  1551. }
  1552. } else {
  1553. foreach ($users as $user) {
  1554. if (($user['added_at_platform'] == 1 && $user['added_at_session'] == 1) || $user['added_at_session'] == 1) {
  1555. if ($user['added_at_platform'] == 1) {
  1556. $addedto = get_lang('UserCreatedPlatform');
  1557. } else {
  1558. $addedto = ' ';
  1559. }
  1560. if ($user['added_at_session'] == 1) {
  1561. $addedto .= ' '.get_lang('UserInSession');
  1562. }
  1563. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  1564. } else {
  1565. $addedto = get_lang('UserNotAdded');
  1566. $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".api_get_person_name($user['FirstName'], $user['LastName'])."</a> - ".$addedto.'<br />';
  1567. }
  1568. }
  1569. }
  1570. header('Location: course.php?id_session='.$id_session.'&action=show_message&message='.urlencode($registered_users));
  1571. exit ();
  1572. //header('Location: resume_session.php?id_session='.$id_session);
  1573. }
  1574. /**
  1575. * Reads CSV-file.
  1576. * @param string $file Path to the CSV-file
  1577. * @return array All userinformation read from the file
  1578. */
  1579. function parse_csv_data($file) {
  1580. $users = Import :: csv_to_array($file);
  1581. foreach ($users as $index => $user) {
  1582. if (isset ($user['Courses'])) {
  1583. $user['Courses'] = explode('|', trim($user['Courses']));
  1584. }
  1585. $users[$index] = $user;
  1586. }
  1587. return $users;
  1588. }
  1589. /**
  1590. * XML-parser: the handler at the beginning of element.
  1591. */
  1592. function element_start($parser, $data) {
  1593. $data = api_utf8_decode($data);
  1594. global $user;
  1595. global $current_tag;
  1596. switch ($data) {
  1597. case 'Contact' :
  1598. $user = array ();
  1599. break;
  1600. default :
  1601. $current_tag = $data;
  1602. }
  1603. }
  1604. /**
  1605. * XML-parser: the handler at the end of element.
  1606. */
  1607. function element_end($parser, $data) {
  1608. $data = api_utf8_decode($data);
  1609. global $user;
  1610. global $users;
  1611. global $current_value;
  1612. global $purification_option_for_usernames;
  1613. $user[$data] = $current_value;
  1614. switch ($data) {
  1615. case 'Contact' :
  1616. $user['UserName'] = UserManager::purify_username($user['UserName'], $purification_option_for_usernames);
  1617. $users[] = $user;
  1618. break;
  1619. default :
  1620. $user[$data] = $current_value;
  1621. break;
  1622. }
  1623. }
  1624. /**
  1625. * XML-parser: the handler for character data.
  1626. */
  1627. function character_data($parser, $data) {
  1628. $data = trim(api_utf8_decode($data));
  1629. global $current_value;
  1630. $current_value = $data;
  1631. }
  1632. /**
  1633. * Reads XML-file.
  1634. * @param string $file Path to the XML-file
  1635. * @return array All userinformation read from the file
  1636. */
  1637. function parse_xml_data($file) {
  1638. global $current_tag;
  1639. global $current_value;
  1640. global $user;
  1641. global $users;
  1642. $users = array ();
  1643. $parser = xml_parser_create('UTF-8');
  1644. xml_set_element_handler($parser, array('MySpace','element_start'), array('MySpace','element_end'));
  1645. xml_set_character_data_handler($parser, "character_data");
  1646. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  1647. xml_parse($parser, Text::api_utf8_encode_xml(file_get_contents($file)));
  1648. xml_parser_free($parser);
  1649. return $users;
  1650. }
  1651. }
  1652. function get_stats($user_id, $courseId, $start_date = null, $end_date = null) {
  1653. // Database table definitions
  1654. $tbl_track_course = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1655. $strg_sd = "";
  1656. $strg_ed = "";
  1657. if ($start_date != null && $end_date != null){
  1658. $end_date = add_day_to( $end_date );
  1659. $strg_sd = "AND login_course_date BETWEEN '$start_date' AND '$end_date'";
  1660. $strg_ed = "AND logout_course_date BETWEEN '$start_date' AND '$end_date'";
  1661. }
  1662. $sql = 'SELECT SEC_TO_TIME(avg(time_to_sec(timediff(logout_course_date,login_course_date)))) as avrg,
  1663. SEC_TO_TIME(sum(time_to_sec(timediff(logout_course_date,login_course_date)))) as total,
  1664. count(user_id) as times
  1665. FROM ' . $tbl_track_course . '
  1666. WHERE user_id = ' . intval($user_id) . ' AND
  1667. c_id = "' . Database::escape_string($courseId) . '" '.$strg_sd.' '.$strg_ed.' '.'
  1668. ORDER BY login_course_date ASC';
  1669. $rs = Database::query($sql);
  1670. $result = array();
  1671. if (Database::num_rows($rs)) {
  1672. if ($row = Database::fetch_array($rs)) {
  1673. $foo_avg = $row['avrg'];
  1674. $foo_total = $row['total'];
  1675. $foo_times = $row['times'];
  1676. $result = array('avg' => $foo_avg, 'total' => $foo_total, 'times' => $foo_times);
  1677. }
  1678. return $result;
  1679. }
  1680. return array();
  1681. }
  1682. function add_day_to($end_date) {
  1683. $foo_date = strtotime( $end_date );
  1684. $foo_date = strtotime(" +1 day", $foo_date);
  1685. $foo_date = date("Y-m-d", $foo_date);
  1686. return $foo_date;
  1687. }
  1688. /**
  1689. * Gets the connections to a course as an array of login and logout time
  1690. *
  1691. * @param int $user_id
  1692. * @param string $course_code
  1693. * @author Jorge Frisancho Jibaja
  1694. * @author Julio Montoya <gugli100@gmail.com> fixing the function
  1695. * @version OCT-22- 2010
  1696. * @return array
  1697. */
  1698. function get_connections_to_course_by_date($user_id, $courseId, $start_date, $end_date) {
  1699. // Database table definitions
  1700. $tbl_track_course = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  1701. $user_id = intval($user_id);
  1702. if (!empty($course_info)) {
  1703. $end_date = add_day_to($end_date);
  1704. $sql = "SELECT login_course_date, logout_course_date FROM $tbl_track_course
  1705. WHERE user_id = $user_id
  1706. AND c_id = '$courseId'
  1707. AND login_course_date BETWEEN '$start_date' AND '$end_date'
  1708. AND logout_course_date BETWEEN '$start_date' AND '$end_date'
  1709. ORDER BY login_course_date ASC";
  1710. $rs = Database::query($sql);
  1711. $connections = array();
  1712. while ($row = Database::fetch_array($rs)) {
  1713. $login_date = $row['login_course_date'];
  1714. $logout_date = $row['logout_course_date'];
  1715. $timestamp_login_date = strtotime($login_date);
  1716. $timestamp_logout_date = strtotime($logout_date);
  1717. $connections[] = array('login' => $timestamp_login_date, 'logout' => $timestamp_logout_date);
  1718. }
  1719. }
  1720. return $connections;
  1721. }
  1722. /**
  1723. *
  1724. *
  1725. * @param array
  1726. * @author Jorge Frisancho Jibaja
  1727. * @version OCT-22- 2010
  1728. * @return array
  1729. */
  1730. function convert_to_array($sql_result){
  1731. $result_to_print = '<table>';
  1732. foreach ($sql_result as $key => $data) {
  1733. $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";
  1734. }
  1735. $result_to_print .= '</table>';
  1736. $result_to_print = array("result"=>$result_to_print);
  1737. return $result_to_print;
  1738. }
  1739. /**
  1740. * Converte an array to a table in html
  1741. *
  1742. * @param array $sql_result
  1743. * @author Jorge Frisancho Jibaja
  1744. * @version OCT-22- 2010
  1745. * @return string
  1746. */
  1747. function convert_to_string($sql_result){
  1748. $result_to_print = '<table>';
  1749. if (!empty($sql_result)) {
  1750. foreach ($sql_result as $key => $data) {
  1751. $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";
  1752. }
  1753. }
  1754. $result_to_print .= '</table>';
  1755. return $result_to_print;
  1756. }
  1757. /**
  1758. * This function draw the graphic to be displayed on the user view as an image
  1759. *
  1760. * @param array $sql_result
  1761. * @param string $start_date
  1762. * @param string $end_date
  1763. * @param string $type
  1764. * @author Jorge Frisancho Jibaja
  1765. * @version OCT-22- 2010
  1766. * @return string
  1767. */
  1768. function grapher($sql_result, $start_date, $end_date, $type = "") {
  1769. if (empty($start_date)) { $start_date =""; }
  1770. if (empty($end_date)) { $end_date =""; }
  1771. if ($type == ""){ $type = 'day'; }
  1772. $main_year = $main_month_year = $main_day = array();
  1773. // get last 8 days/months
  1774. $last_days = 5;
  1775. $last_months = 3;
  1776. for ($i = $last_days; $i >= 0; $i--) {
  1777. $main_day[date ('d-m-Y', mktime () - $i * 3600 * 24)] = 0;
  1778. }
  1779. for ($i = $last_months; $i >= 0; $i--) {
  1780. $main_month_year[date ('m-Y', mktime () - $i * 30 * 3600 * 24)] = 0;
  1781. }
  1782. $i = 0;
  1783. if (is_array($sql_result) && count($sql_result) > 0) {
  1784. foreach ($sql_result as $key => $data) {
  1785. //creating the main array
  1786. $main_month_year[date('m-Y', $data['login'])] += Text::float_format(($data['logout'] - $data['login']) / 60, 0);
  1787. $main_day[date('d-m-Y', $data['login'])] += Text::float_format(($data['logout'] - $data['login']) / 60, 0);
  1788. if ($i > 500) {
  1789. break;
  1790. }
  1791. $i++;
  1792. }
  1793. switch ($type) {
  1794. case 'day':
  1795. $main_date = $main_day;
  1796. break;
  1797. case 'month':
  1798. $main_date = $main_month_year;
  1799. break;
  1800. case 'year':
  1801. $main_date = $main_year;
  1802. break;
  1803. }
  1804. // the nice graphics :D
  1805. $labels = array_keys($main_date);
  1806. if (count($main_date) == 1) {
  1807. $labels = $labels[0];
  1808. $main_date = $main_date[$labels];
  1809. }
  1810. $data_set = new pData();
  1811. $data_set->AddPoint($main_date, 'Q');
  1812. if (count($main_date)!= 1) {
  1813. $data_set->AddPoint($labels, 'Date');
  1814. }
  1815. $data_set->AddAllSeries();
  1816. $data_set->RemoveSerie('Date');
  1817. $data_set->SetAbsciseLabelSerie('Date');
  1818. $data_set->SetYAxisName(get_lang('Minutes', ''));
  1819. $graph_id = api_get_user_id().'AccessDetails'.api_get_course_id().$start_date.$end_date.$type;
  1820. $data_set->AddAllSeries();
  1821. $cache = new pCache(api_get_path(SYS_ARCHIVE_PATH));
  1822. // the graph id
  1823. $data = $data_set->GetData();
  1824. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  1825. //if (0) {
  1826. //if we already created the img
  1827. // echo 'in cache';
  1828. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  1829. } else {
  1830. // if the image does not exist in the archive/ folder
  1831. // Initialise the graph
  1832. $test = new pChart(760, 230);
  1833. //which schema of color will be used
  1834. $quant_resources = count($data[0]) - 1;
  1835. // Adding the color schemma
  1836. $test->loadColorPalette(api_get_path(LIBRARY_PATH).'pchart/palette/default.txt');
  1837. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 8);
  1838. $test->setGraphArea(70, 30, 680, 200);
  1839. $test->drawFilledRoundedRectangle(7, 7, 693, 223, 5, 240, 240, 240);
  1840. $test->drawRoundedRectangle(5, 5, 695, 225, 5, 230, 230, 230);
  1841. $test->drawGraphArea(255, 255, 255, TRUE);
  1842. $test->drawScale($data_set->GetData(), $data_set->GetDataDescription(), SCALE_START0, 150, 150, 150, TRUE, 0, 0);
  1843. $test->drawGrid(4, TRUE, 230, 230, 230, 50);
  1844. $test->setLineStyle(2);
  1845. // Draw the 0 line
  1846. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 6);
  1847. $test->drawTreshold(0, 143, 55, 72, TRUE, TRUE);
  1848. if (count($main_date) == 1) {
  1849. //Draw a graph
  1850. echo '<strong>'.$labels.'</strong><br/>';
  1851. $test->drawBarGraph($data_set->GetData(), $data_set->GetDataDescription(), TRUE);
  1852. } else {
  1853. //Draw the line graph
  1854. $test->drawLineGraph($data_set->GetData(), $data_set->GetDataDescription());
  1855. $test->drawPlotGraph($data_set->GetData(), $data_set->GetDataDescription(), 3, 2, 255, 255, 255);
  1856. }
  1857. // Finish the graph
  1858. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 8);
  1859. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 10);
  1860. $test->drawTitle(60, 22, get_lang('AccessDetails'), 50, 50, 50, 585);
  1861. //------------------
  1862. //echo 'not in cache';
  1863. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  1864. ob_start();
  1865. $test->Stroke();
  1866. ob_end_clean();
  1867. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  1868. }
  1869. $foo_img = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  1870. return $foo_img;
  1871. } else {
  1872. $foo_img = api_convert_encoding('<div id="messages" class="warning-message">'.get_lang('GraphicNotAvailable').'</div>','UTF-8');
  1873. return $foo_img;
  1874. }
  1875. }