myspace.lib.php 116 KB

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