thematic.lib.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains class used like library, provides functions for thematic option inside attendance tool. It's also used like model to thematic_controller (MVC pattern)
  5. * Thematic class can be used to instanciate objects or as a library for thematic control
  6. * @author Christian Fasanando <christian1827@gmail.com>
  7. * @author Julio Montoya <gugli100@gmail.com> SQL fixes
  8. * @package chamilo.course_progress
  9. */
  10. class Thematic
  11. {
  12. private $session_id;
  13. private $thematic_id;
  14. private $thematic_title;
  15. private $thematic_content;
  16. private $thematic_plan_id;
  17. private $thematic_plan_title;
  18. private $thematic_plan_description;
  19. private $thematic_plan_description_type;
  20. private $thematic_advance_id;
  21. private $attendance_id;
  22. private $thematic_advance_content;
  23. private $start_date;
  24. private $duration;
  25. private $course_int_id;
  26. public function __construct() {
  27. $this->course_int_id = api_get_course_int_id();
  28. }
  29. /**
  30. * Get the total number of thematic inside current course and current session
  31. * @see SortableTable#get_total_number_of_items()
  32. */
  33. public function get_number_of_thematics() {
  34. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  35. $condition_session = '';
  36. if (!api_get_session_id()) {
  37. $condition_session = api_get_session_condition(0);
  38. }
  39. $course_id = api_get_course_int_id();
  40. $sql = "SELECT COUNT(id) AS total_number_of_items FROM $tbl_thematic WHERE c_id = $course_id AND active = 1 $condition_session ";
  41. $res = Database::query($sql);
  42. $obj = Database::fetch_object($res);
  43. return $obj->total_number_of_items;
  44. }
  45. /**
  46. * Get the thematics to display on the current page (fill the sortable-table)
  47. * @param int offset of first user to recover
  48. * @param int Number of users to get
  49. * @param int Column to sort on
  50. * @param string Order (ASC,DESC)
  51. * @see SortableTable#get_table_data($from)
  52. */
  53. public function get_thematic_data($from, $number_of_items, $column, $direction) {
  54. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  55. $condition_session = '';
  56. if (!api_get_session_id()) {
  57. $condition_session = api_get_session_condition(0);
  58. }
  59. $column = intval($column);
  60. $from = intval($from);
  61. $number_of_items = intval($number_of_items);
  62. if (!in_array($direction, array('ASC','DESC'))) {
  63. $direction = 'ASC';
  64. }
  65. $course_id = api_get_course_int_id();
  66. $sql = "SELECT id AS col0, title AS col1, display_order AS col2, session_id FROM $tbl_thematic
  67. WHERE c_id = $course_id AND active = 1 $condition_session
  68. ORDER BY col2 LIMIT $from,$number_of_items ";
  69. $res = Database::query($sql);
  70. $thematics = array ();
  71. $param_gradebook = '';
  72. if (isset($_SESSION['gradebook'])) {
  73. $param_gradebook = '&gradebook='.$_SESSION['gradebook'];
  74. }
  75. $user_info = api_get_user_info(api_get_user_id());
  76. while ($thematic = Database::fetch_row($res)) {
  77. $session_star = '';
  78. if (api_get_session_id() == $thematic[3]) {
  79. $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
  80. }
  81. $thematic[1] = '<a href="index.php?'.api_get_cidreq().'&action=thematic_details&thematic_id='.$thematic[0].$param_gradebook.'">'.Security::remove_XSS($thematic[1], STUDENT).$session_star.'</a>';
  82. if (api_is_allowed_to_edit(null, true)) {
  83. $actions = '';
  84. if (api_get_session_id()) {
  85. if (api_get_session_id() == $thematic[3]) {
  86. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('lesson_plan.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  87. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('lesson_plan_calendar.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  88. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('edit.png',get_lang('Edit'),'',22).'</a>';
  89. $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('delete.png',get_lang('Delete'),'',22).'</a>';
  90. } else {
  91. $actions .= Display::return_icon('lesson_plan_na.png',get_lang('ThematicPlan'),'',22).'&nbsp;';
  92. $actions .= Display::return_icon('lesson_plan_calendar_na.png',get_lang('ThematicAdvance'),'',22).'&nbsp;';
  93. $actions .= Display::return_icon('edit_na.png',get_lang('Edit'),'',22);
  94. $actions .= Display::return_icon('delete_na.png',get_lang('Delete'),'',22).'&nbsp;';
  95. $actions .= Display::url(Display::return_icon('cd.gif', get_lang('Copy')), 'index.php?'.api_get_cidreq().'&action=thematic_copy&thematic_id='.$thematic[0].$param_gradebook);
  96. }
  97. } else {
  98. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('lesson_plan.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  99. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('lesson_plan_calendar.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  100. if ($thematic[2] > 1) {
  101. $actions .= '<a href="'.api_get_self().'?action=moveup&'.api_get_cidreq().'&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('up.png', get_lang('Up'),'',22).'</a>';
  102. } else {
  103. $actions .= Display::return_icon('up_na.png','&nbsp;','',22);
  104. }
  105. if ($thematic[2] < self::get_max_thematic_item()) {
  106. $actions .= '<a href="'.api_get_self().'?action=movedown&a'.api_get_cidreq().'&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('down.png',get_lang('Down'),'',22).'</a>';
  107. } else {
  108. $actions .= Display::return_icon('down_na.png','&nbsp;','',22);
  109. }
  110. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('edit.png',get_lang('Edit'),'',22).'</a>';
  111. $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('delete.png',get_lang('Delete'),'',22).'</a>';
  112. }
  113. $thematics[] = array($thematic[0], $thematic[1], $actions);
  114. }
  115. }
  116. return $thematics;
  117. }
  118. /**
  119. * Get the maximum display order of the thematic item
  120. * @return int Maximum display order
  121. */
  122. public function get_max_thematic_item($use_session = true) {
  123. // Database table definition
  124. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  125. $session_id = api_get_session_id();
  126. if ($use_session) {
  127. $condition_session = api_get_session_condition($session_id);
  128. } else {
  129. $condition_session = '';
  130. }
  131. $course_id = api_get_course_int_id();
  132. $sql = "SELECT MAX(display_order) FROM $tbl_thematic WHERE c_id = $course_id AND active = 1 $condition_session";
  133. $rs = Database::query($sql);
  134. $row = Database::fetch_array($rs);
  135. return $row[0];
  136. }
  137. /**
  138. * Move a thematic
  139. *
  140. * @param string Direction (up, down)
  141. * @param int Thematic id
  142. */
  143. public function move_thematic($direction, $thematic_id) {
  144. // Database table definition
  145. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  146. // sort direction
  147. if ($direction == 'up') {
  148. $sortorder = 'DESC';
  149. } else {
  150. $sortorder = 'ASC';
  151. }
  152. $course_id = api_get_course_int_id();
  153. $session_id = api_get_session_id();
  154. $condition_session = api_get_session_condition($session_id);
  155. $sql = "SELECT id, display_order FROM $tbl_thematic WHERE c_id = $course_id AND active = 1 $condition_session ORDER BY display_order $sortorder";
  156. $res = Database::query($sql);
  157. $found = false;
  158. //Variable definition
  159. $current_id = 0;
  160. $next_id = 0;
  161. while ($row = Database::fetch_array($res)) {
  162. if ($found && empty($next_id)) {
  163. $next_id = intval($row['id']);
  164. $next_display_order = intval($row['display_order']);
  165. }
  166. if ($row['id'] == $thematic_id) {
  167. $current_id = intval($thematic_id);
  168. $current_display_order = intval($row['display_order']);
  169. $found = true;
  170. }
  171. }
  172. // get last done thematic advance before move thematic list
  173. $last_done_thematic_advance = $this->get_last_done_thematic_advance();
  174. if (!empty($next_display_order) && !empty($current_id)) {
  175. $sql = "UPDATE $tbl_thematic SET display_order = $next_display_order WHERE c_id = $course_id AND id = $current_id ";
  176. Database::query($sql);
  177. }
  178. if (!empty($current_display_order) && !empty($next_id)) {
  179. $sql = "UPDATE $tbl_thematic SET display_order = $current_display_order WHERE c_id = $course_id AND id = $next_id ";
  180. Database::query($sql);
  181. }
  182. // update done advances with de current thematic list
  183. $update_done_advances = $this->update_done_thematic_advances($last_done_thematic_advance);
  184. }
  185. /**
  186. * get thematic list
  187. * @param int Thematic id (optional), get list by id
  188. * @return array Thematic data
  189. */
  190. public function get_thematic_list($thematic_id = null, $course_code = null, $session_id = null) {
  191. // set current course and session
  192. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  193. $course_info = api_get_course_info($course_code);
  194. $course_id = $course_info['real_id'];
  195. if (isset($session_id)) {
  196. $session_id = intval($session_id);
  197. } else {
  198. $session_id = api_get_session_id();
  199. }
  200. $data = array();
  201. $condition = '';
  202. if (isset($thematic_id)) {
  203. $thematic_id = intval($thematic_id);
  204. $condition = " WHERE id = $thematic_id AND active = 1 ";
  205. } else {
  206. $condition_session = '';
  207. if (empty($session_id)) {
  208. $condition_session = api_get_session_condition(0);
  209. }
  210. $condition = " WHERE active = 1 $condition_session ";
  211. }
  212. $sql = "SELECT * FROM $tbl_thematic $condition AND c_id = $course_id ORDER BY display_order ";
  213. $res = Database::query($sql);
  214. if (Database::num_rows($res) > 0) {
  215. if (!empty($thematic_id)) {
  216. $data = Database::fetch_array($res,'ASSOC');
  217. } else {
  218. while ($row = Database::fetch_array($res,'ASSOC')) {
  219. $data[$row['id']] = $row;
  220. }
  221. }
  222. }
  223. return $data;
  224. }
  225. /**
  226. * insert or update a thematic
  227. * @return int last thematic id
  228. */
  229. public function thematic_save() {
  230. global $_course;
  231. // definition database table
  232. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  233. // protect data
  234. $id = intval($this->thematic_id);
  235. $title = Database::escape_string($this->thematic_title);
  236. $content = Database::escape_string($this->thematic_content);
  237. $session_id = intval($this->session_id);
  238. $user_id = api_get_user_id();
  239. // get the maximum display order of all the glossary items
  240. $max_thematic_item = $this->get_max_thematic_item(false);
  241. if (empty($id)) {
  242. // insert
  243. $sql = "INSERT INTO $tbl_thematic (c_id, title, content, active, display_order, session_id)
  244. VALUES ($this->course_int_id, '$title', '$content', 1, ".(intval($max_thematic_item)+1).", $session_id) ";
  245. Database::query($sql);
  246. $last_id = Database::insert_id();
  247. if (Database::affected_rows()) {
  248. // save inside item property table
  249. $last_id = Database::insert_id();
  250. api_item_property_update($_course, 'thematic', $last_id,"ThematicAdded", $user_id);
  251. }
  252. } else {
  253. // update
  254. $sql = "UPDATE $tbl_thematic SET title = '$title', content = '$content', session_id = $session_id WHERE id = $id AND c_id = {$this->course_int_id}";
  255. Database::query($sql);
  256. $last_id = $id;
  257. if (Database::affected_rows()) {
  258. // save inside item property table
  259. api_item_property_update($_course, 'thematic', $last_id,"ThematicUpdated", $user_id);
  260. }
  261. }
  262. return $last_id;
  263. }
  264. /**
  265. * Delete logically (set active field to 0) a thematic
  266. * @param int|array One or many thematic ids
  267. * @return int Affected rows
  268. */
  269. public function thematic_destroy($thematic_id) {
  270. global $_course;
  271. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  272. $affected_rows = 0;
  273. $user_id = api_get_user_id();
  274. $course_id = api_get_course_int_id();
  275. if (is_array($thematic_id)) {
  276. foreach ($thematic_id as $id) {
  277. $id = intval($id);
  278. $sql = "UPDATE $tbl_thematic SET active = 0 WHERE c_id = $course_id AND id = $id";
  279. Database::query($sql);
  280. $affected_rows += Database::affected_rows();
  281. if (!empty($affected_rows)) {
  282. // update row item property table
  283. api_item_property_update($_course, 'thematic', $id,"ThematicDeleted", $user_id);
  284. //api_item_property_update($_course, TOOL_COURSE_PROGRESS, $id,"delete", $user_id);
  285. }
  286. }
  287. } else {
  288. $thematic_id = intval($thematic_id);
  289. $sql = "UPDATE $tbl_thematic SET active = 0 WHERE c_id = $course_id AND id = $thematic_id";
  290. Database::query($sql);
  291. $affected_rows = Database::affected_rows();
  292. if (!empty($affected_rows)) {
  293. // update row item property table
  294. //api_item_property_update($_course, TOOL_COURSE_PROGRESS, $thematic_id,"delete", $user_id);
  295. api_item_property_update($_course, 'thematic', $thematic_id,"ThematicDeleted", $user_id);
  296. }
  297. }
  298. return $affected_rows;
  299. }
  300. public function copy($thematic_id) {
  301. $thematic = self::get_thematic_list($thematic_id, api_get_course_id(), 0);
  302. $thematic_copy = new Thematic();
  303. $thematic_copy->set_thematic_attributes('', $thematic['title'].' - '.get_lang('Copy'), $thematic['content'], api_get_session_id());
  304. $new_thematic_id = $thematic_copy->thematic_save();
  305. if (!empty($new_thematic_id)) {
  306. $thematic_advanced = self::get_thematic_advance_by_thematic_id($thematic_id);
  307. if(!empty($thematic_advanced)) {
  308. foreach($thematic_advanced as $item) {
  309. $thematic = new Thematic();
  310. $thematic->set_thematic_advance_attributes(0, $new_thematic_id, 0, $item['content'], $item['start_date'], $item['duration']);
  311. $thematic->thematic_advance_save();
  312. }
  313. }
  314. $thematic_plan = self::get_thematic_plan_data($thematic_id);
  315. if (!empty($thematic_plan)) {
  316. foreach ($thematic_plan as $item) {
  317. $thematic = new Thematic();
  318. $thematic->set_thematic_plan_attributes($new_thematic_id, $item['title'], $item['description'], $item['description_type']);
  319. $thematic->thematic_plan_save();
  320. }
  321. }
  322. }
  323. }
  324. /**
  325. * Get the total number of thematic advance inside current course
  326. * @see SortableTable#get_total_number_of_items()
  327. */
  328. public function get_number_of_thematic_advances() {
  329. global $thematic_id;
  330. $tbl_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  331. $course_id = api_get_course_int_id();
  332. $sql = "SELECT COUNT(id) AS total_number_of_items FROM $tbl_thematic_advance WHERE c_id = $course_id AND thematic_id = $thematic_id ";
  333. $res = Database::query($sql);
  334. $res = Database::query($sql);
  335. $obj = Database::fetch_object($res);
  336. return $obj->total_number_of_items;
  337. }
  338. /**
  339. * Get the thematic advances to display on the current page (fill the sortable-table)
  340. * @param int offset of first user to recover
  341. * @param int Number of users to get
  342. * @param int Column to sort on
  343. * @param string Order (ASC,DESC)
  344. * @see SortableTable#get_table_data($from)
  345. */
  346. public function get_thematic_advance_data($from, $number_of_items, $column, $direction) {
  347. global $thematic_id;
  348. $tbl_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  349. $thematic_data = self::get_thematic_list($thematic_id);
  350. $column = intval($column);
  351. $from = intval($from);
  352. $number_of_items = intval($number_of_items);
  353. if (!in_array($direction, array('ASC','DESC'))) {
  354. $direction = 'ASC';
  355. }
  356. $data = array();
  357. $course_id = api_get_course_int_id();
  358. if (api_is_allowed_to_edit(null, true)) {
  359. $sql = "SELECT id AS col0, start_date AS col1, duration AS col2, content AS col3 FROM $tbl_thematic_advance
  360. WHERE c_id = $course_id AND thematic_id = $thematic_id
  361. ORDER BY col$column $direction LIMIT $from,$number_of_items ";
  362. $list = api_get_item_property_by_tool('thematic_advance', api_get_course_id(), api_get_session_id());
  363. $elements = array();
  364. foreach ($list as $value) {
  365. $elements[] = $value['ref'];
  366. }
  367. $res = Database::query($sql);
  368. $i = 1;
  369. while ($thematic_advance = Database::fetch_row($res)) {
  370. if(in_array($thematic_advance[0], $elements)) {
  371. $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
  372. $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG);
  373. $actions = '';
  374. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.Display::return_icon('edit.png',get_lang('Edit'),'',22).'</a>';
  375. $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_advance_delete&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.Display::return_icon('delete.png',get_lang('Delete'),'',22).'</a></center>';
  376. $data[] = array($i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions);
  377. $i++;
  378. }
  379. }
  380. }
  381. return $data;
  382. }
  383. /**
  384. * get thematic advance data by tematic id
  385. * @param int Thematic id
  386. * @param string Course code (optional)
  387. * @return array data
  388. */
  389. public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null) {
  390. $course_info = api_get_course_info($course_code);
  391. $course_id = $course_info['real_id'];
  392. // set current course
  393. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  394. $thematic_id = intval($thematic_id);
  395. $data = array();
  396. $sql = "SELECT * FROM $tbl_thematic_advance WHERE c_id = $course_id AND thematic_id = $thematic_id ";
  397. $elements = array();
  398. $list = api_get_item_property_by_tool('thematic_advance', $course_info['code'], api_get_session_id());
  399. foreach($list as $value) {
  400. $elements[] = $value['ref'];
  401. }
  402. $res = Database::query($sql);
  403. if (Database::num_rows($res) > 0) {
  404. while ($row = Database::fetch_array($res, 'ASSOC')) {
  405. if (in_array($row['id'], $elements)) {
  406. $data[] = $row;
  407. }
  408. }
  409. }
  410. return $data;
  411. }
  412. public function get_thematic_advance_div($data) {
  413. $return_array = array();
  414. $uinfo = api_get_user_info();
  415. foreach ($data as $thematic_id => $thematic_advance_data) {
  416. foreach ($thematic_advance_data as $key => $thematic_advance) {
  417. $session_star = '';
  418. if (api_is_allowed_to_edit(null, true)) {
  419. if ($thematic_advance['session_id'] !=0) {
  420. $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
  421. }
  422. }
  423. //DATE_TIME_FORMAT_LONG
  424. $thematic_advance_item = '<div><strong>'.api_convert_and_format_date($thematic_advance['start_date'], DATE_FORMAT_LONG).$session_star.'</strong></div>';
  425. // $thematic_advance_item .= '<div>'.get_lang('DurationInHours').' : '.$thematic_advance['duration'].'</div>';
  426. $thematic_advance_item .= '<div>'.$thematic_advance['duration'].' '.get_lang('HourShort').'</div>';
  427. $thematic_advance_item .= '<div>'.Security::remove_XSS($thematic_advance['content'], STUDENT).'</div>';
  428. $return_array[$thematic_id][$thematic_advance['id']] = $thematic_advance_item;
  429. }
  430. }
  431. return $return_array;
  432. }
  433. public function get_thematic_plan_div($data) {
  434. $final_return = array();
  435. $uinfo = api_get_user_info();
  436. foreach ($data as $thematic_id => $thematic_plan_data) {
  437. $new_thematic_plan_data = array();
  438. foreach($thematic_plan_data as $thematic_item) {
  439. $thematic_simple_list[] = $thematic_item['description_type'];
  440. $new_thematic_plan_data[$thematic_item['description_type']] = $thematic_item;
  441. }
  442. $new_id = ADD_THEMATIC_PLAN;
  443. if (!empty($thematic_simple_list)) {
  444. foreach($thematic_simple_list as $item) {
  445. //if ($item >= ADD_THEMATIC_PLAN) {
  446. //$new_id = $item + 1;
  447. $default_thematic_plan_title[$item] = $new_thematic_plan_data[$item]['title'];
  448. //}
  449. }
  450. }
  451. $no_data = true;
  452. $session_star = '';
  453. $return = '<div id="thematic_plan_'.$thematic_id.'">';
  454. if (!empty($default_thematic_plan_title)) {
  455. foreach ($default_thematic_plan_title as $id=>$title) {
  456. $thematic_plan_data_item = '';
  457. //avoid others
  458. if ($title == 'Others' && empty($data[$thematic_id][$id]['description'])) {
  459. continue;
  460. }
  461. if (!empty($data[$thematic_id][$id]['title']) && !empty($data[$thematic_id][$id]['description'])) {
  462. if (api_is_allowed_to_edit(null, true)) {
  463. if ($data[$thematic_id][$id]['session_id'] !=0) {
  464. $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
  465. }
  466. }
  467. $return .= Display::tag('h3', Security::remove_XSS($data[$thematic_id][$id]['title'], STUDENT).$session_star);
  468. $return .= Security::remove_XSS($data[$thematic_id][$id]['description'], STUDENT);
  469. $no_data = false;
  470. }
  471. }
  472. }
  473. if ($no_data) {
  474. $return .= '<div><em>'.get_lang('StillDoNotHaveAThematicPlan').'</em></div>';
  475. }
  476. $return .= '</div>';
  477. $final_return[$thematic_id] = $return;
  478. }
  479. return $final_return;
  480. }
  481. /**
  482. * get thematic advance list
  483. * @param int Thematic advance id (optional), get data by thematic advance list
  484. * @param string Course code (optional)
  485. * @return array data
  486. */
  487. public function get_thematic_advance_list($thematic_advance_id = null, $course_code = null, $force_session_id = false) { // set current course
  488. $course_info = api_get_course_info($course_code);
  489. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  490. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  491. $data = array();
  492. $condition = '';
  493. if (isset($thematic_advance_id)) {
  494. $thematic_advance_id = intval($thematic_advance_id);
  495. $condition = " AND a.id = $thematic_advance_id ";
  496. }
  497. $course_id = $course_info['real_id'];
  498. /*if ($force_session_id) {
  499. $sql = "SELECT a.* FROM $tbl_thematic_advance a INNER JOIN $tbl_thematic t ON t.id = a.thematic_id WHERE 1 $condition AND t.session_id = ".api_get_session_id()." ORDER BY start_date ";
  500. } else {
  501. $sql = "SELECT * FROM $tbl_thematic_advance a WHERE 1 $condition ORDER BY start_date ";
  502. }*/
  503. $sql = "SELECT * FROM $tbl_thematic_advance a WHERE c_id = $course_id $condition ORDER BY start_date ";
  504. $elements = array();
  505. if ($force_session_id) {
  506. $list = api_get_item_property_by_tool('thematic_advance', $course_info['code'], api_get_session_id());
  507. foreach($list as $value) {
  508. $elements[$value['ref']]= $value;
  509. }
  510. }
  511. $res = Database::query($sql);
  512. if (Database::num_rows($res) > 0) {
  513. if (!empty($thematic_advance_id)) {
  514. $data = Database::fetch_array($res);
  515. } else {
  516. // group all data group by thematic id
  517. $tmp = array();
  518. while ($row = Database::fetch_array($res, 'ASSOC')) {
  519. $tmp[] = $row['thematic_id'];
  520. if (in_array($row['thematic_id'], $tmp)) {
  521. if ($force_session_id) {
  522. if (in_array($row['id'], array_keys($elements))) {
  523. $row['session_id'] = $elements[$row['id']]['id_session'];
  524. $data[$row['thematic_id']][$row['id']] = $row;
  525. }
  526. } else {
  527. $data[$row['thematic_id']][$row['id']] = $row;
  528. }
  529. }
  530. }
  531. }
  532. }
  533. return $data;
  534. }
  535. /**
  536. * insert or update a thematic advance
  537. * @todo problem
  538. * @return int last thematic advance id
  539. */
  540. public function thematic_advance_save() {
  541. global $_course;
  542. // definition database table
  543. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  544. // protect data
  545. $id = intval($this->thematic_advance_id);
  546. $tematic_id = intval($this->thematic_id);
  547. $attendance_id = intval($this->attendance_id);
  548. $content = Database::escape_string($this->thematic_advance_content);
  549. $start_date = Database::escape_string($this->start_date);
  550. $duration = intval($this->duration);
  551. $user_id = api_get_user_id();
  552. if (empty($id)) {
  553. // Insert
  554. $sql = "INSERT INTO $tbl_thematic_advance (c_id, thematic_id, attendance_id, content, start_date, duration)
  555. VALUES ($this->course_int_id, $tematic_id, $attendance_id, '$content', '".api_get_utc_datetime($start_date)."', '$duration') ";
  556. Database::query($sql);
  557. $last_id = Database::insert_id();
  558. if (Database::affected_rows()) {
  559. api_item_property_update($_course, 'thematic_advance', $last_id,"ThematicAdvanceAdded", $user_id);
  560. }
  561. } else {
  562. // update
  563. $sql = "UPDATE $tbl_thematic_advance SET thematic_id = '$tematic_id', attendance_id = '$attendance_id', content = '$content', start_date = '".api_get_utc_datetime($start_date)."', duration = '$duration'
  564. WHERE c_id = {$this->course_int_id} AND id = $id ";
  565. Database::query($sql);
  566. if (Database::affected_rows()) {
  567. api_item_property_update($_course, 'thematic_advance', $id, "ThematicAdvanceUpdated", $user_id);
  568. }
  569. }
  570. return $last_id;
  571. }
  572. /**
  573. * delete thematic advance
  574. * @param int Thematic advance id
  575. * @return int Affected rows
  576. */
  577. public function thematic_advance_destroy($thematic_advance_id) {
  578. global $_course;
  579. $course_id = api_get_course_int_id();
  580. // definition database table
  581. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  582. // protect data
  583. $thematic_advance_id = intval($thematic_advance_id);
  584. $user_id = api_get_user_id();
  585. $sql = "DELETE FROM $tbl_thematic_advance WHERE c_id = $course_id AND id = $thematic_advance_id ";
  586. Database::query($sql);
  587. $affected_rows = Database::affected_rows();
  588. if ($affected_rows) {
  589. api_item_property_update($_course, 'thematic_advance', $thematic_advance_id,"ThematicAdvanceDeleted", $user_id);
  590. }
  591. return $affected_rows;
  592. }
  593. /**
  594. * get thematic plan data
  595. * @param int Thematic id (optional), get data by thematic id
  596. * @param int Thematic plan description type (optional), get data by description type
  597. * @return array Thematic plan data
  598. */
  599. public function get_thematic_plan_data($thematic_id = null, $description_type = null) {
  600. // definition database table
  601. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  602. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  603. $course_id = api_get_course_int_id();
  604. $data = array();
  605. $condition = '';
  606. if (isset($thematic_id)) {
  607. $thematic_id = intval($thematic_id);
  608. $condition .= " AND thematic_id = $thematic_id ";
  609. }
  610. if (isset($description_type)) {
  611. $description_type = intval($description_type);
  612. $condition .= " AND description_type = $description_type ";
  613. }
  614. $items_from_course = api_get_item_property_by_tool('thematic_plan', api_get_course_id(), 0);
  615. $items_from_session = api_get_item_property_by_tool('thematic_plan', api_get_course_id(), api_get_session_id());
  616. $thematic_plan_complete_list = array();
  617. $thematic_plan_id_list = array();
  618. if (!empty($items_from_course)) {
  619. foreach($items_from_course as $item) {
  620. $thematic_plan_id_list[] = $item['ref'];
  621. $thematic_plan_complete_list[$item['ref']] = $item;
  622. }
  623. }
  624. if (!empty($items_from_session)) {
  625. foreach($items_from_session as $item) {
  626. $thematic_plan_id_list[] = $item['ref'];
  627. $thematic_plan_complete_list[$item['ref']] = $item;
  628. }
  629. }
  630. if (!empty($thematic_plan_id_list)) {
  631. $sql = "SELECT tp.id, thematic_id, tp.title, description, description_type, t.session_id
  632. FROM $tbl_thematic_plan tp INNER JOIN $tbl_thematic t ON (t.id=tp.thematic_id)
  633. WHERE t.c_id = $course_id AND tp.c_id = $course_id $condition AND tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
  634. $rs = Database::query($sql);
  635. if (Database::num_rows($rs)) {
  636. if (!isset($thematic_id) && !isset($description_type)) {
  637. // group all data group by thematic id
  638. $tmp = array();
  639. while ($row = Database::fetch_array($rs,'ASSOC')) {
  640. $tmp[] = $row['thematic_id'];
  641. if (in_array($row['thematic_id'], $tmp)) {
  642. $row['session_id'] = $thematic_plan_complete_list[$row['id']];
  643. $data[$row['thematic_id']][$row['description_type']] = $row;
  644. }
  645. }
  646. } else {
  647. while ($row = Database::fetch_array($rs,'ASSOC')) {
  648. $row['session_id'] = $thematic_plan_complete_list[$row['id']];
  649. $data[] = $row;
  650. }
  651. }
  652. }
  653. }
  654. return $data;
  655. }
  656. /**
  657. * insert or update a thematic plan
  658. * @return int affected rows
  659. */
  660. public function thematic_plan_save() {
  661. global $_course;
  662. // definition database table
  663. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  664. // protect data
  665. $thematic_id = intval($this->thematic_id);
  666. $title = Database::escape_string($this->thematic_plan_title);
  667. $description = Database::escape_string($this->thematic_plan_description);
  668. $description_type = intval($this->thematic_plan_description_type);
  669. $user_id = api_get_user_id();
  670. $course_id = api_get_course_int_id();
  671. $list = api_get_item_property_by_tool('thematic_plan', api_get_course_id(), api_get_session_id());
  672. $elements_to_show = array();
  673. foreach($list as $value) {
  674. $elements_to_show[]= $value['ref'];
  675. }
  676. $condition = '';
  677. if (!empty($elements_to_show)) {
  678. $condition = "AND id IN (".implode(',', $elements_to_show).") ";
  679. }
  680. // check thematic plan type already exists
  681. $sql = "SELECT id FROM $tbl_thematic_plan WHERE c_id = $course_id AND thematic_id = $thematic_id AND description_type = '$description_type'";
  682. $rs = Database::query($sql);
  683. $affected_rows = 0;
  684. if (Database::num_rows($rs) > 0) {
  685. //if (!empty($thematic_plan_data)) {
  686. $row_thematic_plan = Database::fetch_array($rs);
  687. $thematic_plan_id = $row_thematic_plan['id'];
  688. //Checking the session
  689. $thematic_plan_data = api_get_item_property_info(api_get_course_int_id(), 'thematic_plan', $thematic_plan_id);
  690. $update = false;
  691. if (in_array($thematic_plan_id, $elements_to_show)) {
  692. $update = true;
  693. }
  694. if ($update) {
  695. // update
  696. $upd = "UPDATE $tbl_thematic_plan SET title = '$title', description = '$description' WHERE c_id = $course_id AND id = $thematic_plan_id";
  697. Database::query($upd);
  698. $affected_rows = Database::affected_rows();
  699. if ($affected_rows) {
  700. api_item_property_update($_course, 'thematic_plan', $thematic_plan_id, "ThematicPlanUpdated", $user_id);
  701. }
  702. } else {
  703. // insert
  704. $ins = "INSERT INTO $tbl_thematic_plan (c_id, thematic_id, title, description, description_type)
  705. VALUES ($this->course_int_id, $thematic_id, '$title', '$description', $description_type) ";
  706. Database::query($ins);
  707. $last_id = Database::insert_id();
  708. $affected_rows = Database::affected_rows();
  709. if ($affected_rows) {
  710. api_item_property_update($_course, 'thematic_plan', $last_id,"ThematicPlanAdded", $user_id);
  711. }
  712. }
  713. } else {
  714. // insert
  715. $ins = "INSERT INTO $tbl_thematic_plan (c_id, thematic_id, title, description, description_type)
  716. VALUES($this->course_int_id, $thematic_id, '$title', '$description', $description_type) ";
  717. Database::query($ins);
  718. $last_id = Database::insert_id();
  719. $affected_rows = Database::affected_rows();
  720. if ($affected_rows) {
  721. api_item_property_update($_course, 'thematic_plan', $last_id,"ThematicPlanAdded", $user_id);
  722. }
  723. }
  724. return $affected_rows;
  725. }
  726. /**
  727. * delete a thematic plan description
  728. * @param int Thematic id
  729. * @param int Description type
  730. * @return int Affected rows
  731. */
  732. public function thematic_plan_destroy($thematic_id, $description_type) {
  733. global $_course;
  734. // definition database table
  735. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  736. // protect data
  737. $thematic_id = intval($thematic_id);
  738. $description_type = intval($description_type);
  739. $user_id = api_get_user_id();
  740. $course_id = $course_info['real_id'];
  741. // get thematic plan id
  742. $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
  743. $thematic_plan_id = $thematic_plan_data[0]['id'];
  744. // delete
  745. $sql = "DELETE FROM $tbl_thematic_plan WHERE c_id = $course_id AND thematic_id = $thematic_id AND description_type = $description_type ";
  746. Database::query($sql);
  747. $affected_rows = Database::affected_rows();
  748. if ($affected_rows) {
  749. api_item_property_update($_course, 'thematic_plan', $thematic_plan_id,"ThematicPlanDeleted", $user_id);
  750. }
  751. return $affected_rows;
  752. }
  753. /**
  754. * Get next description type for a new thematic plan description (option 'others')
  755. * @param int Thematic id
  756. * @return int New Description type
  757. */
  758. public function get_next_description_type($thematic_id) {
  759. // definition database table
  760. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  761. // protect data
  762. $thematic_id = intval($thematic_id);
  763. $description_type = intval($description_type);
  764. $next_description_type = 0;
  765. $course_id = api_get_course_int_id();
  766. $sql = "SELECT MAX(description_type) as max FROM $tbl_thematic_plan
  767. WHERE c_id = $course_id AND thematic_id = $thematic_id AND description_type >= ".ADD_THEMATIC_PLAN." ";
  768. $rs = Database::query($sql);
  769. $row = Database::fetch_array($rs);
  770. $last_description_type = $row['max'];
  771. if (isset($last_description_type)) {
  772. $row = Database::fetch_array($rs);
  773. $next_description_type = $last_description_type + 1;
  774. } else {
  775. $next_description_type = ADD_THEMATIC_PLAN;
  776. }
  777. return $next_description_type;
  778. }
  779. /**
  780. * update done thematic advances from thematic details interface
  781. * @param int Thematic id
  782. * @return int Affected rows
  783. */
  784. public function update_done_thematic_advances($thematic_advance_id) {
  785. global $_course;
  786. $thematic_data = $this->get_thematic_list(null, api_get_course_id());
  787. $thematic_advance_data = $this->get_thematic_advance_list(null, api_get_course_id(), true);
  788. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  789. $affected_rows = 0;
  790. $user_id = api_get_user_id();
  791. $all = array();
  792. if (!empty($thematic_data)) {
  793. foreach ($thematic_data as $thematic) {
  794. $thematic_id = $thematic['id'];
  795. if (!empty($thematic_advance_data[$thematic['id']])) {
  796. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  797. $all[] = $thematic_advance['id'];
  798. }
  799. }
  800. }
  801. }
  802. $error = null;
  803. $a_thematic_advance_ids = array();
  804. $course_id = api_get_course_int_id();
  805. if (!empty($thematic_data)) {
  806. foreach ($thematic_data as $thematic) {
  807. $my_affected_rows = 0;
  808. $thematic_id = $thematic['id'];
  809. if (!empty($thematic_advance_data[$thematic['id']])) {
  810. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  811. $item_info = api_get_item_property_info(api_get_course_int_id(), 'thematic_advance', $thematic_advance['id']);
  812. if ($item_info['id_session'] == api_get_session_id()) {
  813. $a_thematic_advance_ids[] = $thematic_advance['id'];
  814. // update done thematic for previous advances ((done_advance = 1))
  815. $upd = "UPDATE $tbl_thematic_advance SET done_advance = 1 WHERE c_id = $course_id AND id = ".$thematic_advance['id']." ";
  816. Database::query($upd);
  817. $my_affected_rows = Database::affected_rows();
  818. $affected_rows += $my_affected_rows;
  819. //if ($my_affected_rows) {
  820. api_item_property_update($_course, 'thematic_advance', $thematic_advance['id'], "ThematicAdvanceDone", $user_id);
  821. //}
  822. if ($thematic_advance['id'] == $thematic_advance_id) {
  823. break 2;
  824. }
  825. }
  826. }
  827. }
  828. }
  829. }
  830. // Update done thematic for others advances (done_advance = 0)
  831. if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
  832. $diff = array_diff($all, $a_thematic_advance_ids);
  833. if (!empty($diff)) {
  834. $upd = "UPDATE $tbl_thematic_advance SET done_advance = 0 WHERE c_id = $course_id AND id IN(".implode(',',$diff).") ";
  835. Database::query($upd);
  836. }
  837. // update item_property
  838. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  839. $session_id = api_get_session_id();
  840. // get all thematic advance done
  841. $rs_thematic_done = Database::query("SELECT ref FROM $tbl_item_property
  842. WHERE c_id = $course_id AND tool='thematic_advance' AND lastedit_type='ThematicAdvanceDone' AND id_session = $session_id ");
  843. if (Database::num_rows($rs_thematic_done) > 0) {
  844. while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
  845. $ref = $row_thematic_done['ref'];
  846. if (in_array($ref, $a_thematic_advance_ids)) { continue; }
  847. // update items
  848. Database::query("UPDATE $tbl_item_property SET lastedit_date='".api_get_utc_datetime()."', lastedit_type='ThematicAdvanceUpdated', lastedit_user_id = $user_id WHERE c_id = $course_id AND tool='thematic_advance' AND ref=$ref AND id_session = $session_id ");
  849. }
  850. }
  851. }
  852. return $affected_rows;
  853. }
  854. /**
  855. * Get last done thematic advance from thematic details interface
  856. * @return int Last done thematic advance id
  857. */
  858. public function get_last_done_thematic_advance() {
  859. $thematic_data = $this->get_thematic_list();
  860. $thematic_advance_data = $this->get_thematic_advance_list(null, api_get_course_id(), true);
  861. $a_thematic_advance_ids = array();
  862. $last_done_advance_id = 0;
  863. if (!empty($thematic_data)) {
  864. foreach ($thematic_data as $thematic) {
  865. $thematic_id = $thematic['id'];
  866. if (!empty($thematic_advance_data[$thematic['id']])) {
  867. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  868. if ($thematic_advance['done_advance'] == 1) {
  869. $a_thematic_advance_ids[] = $thematic_advance['id'];
  870. }
  871. }
  872. }
  873. }
  874. }
  875. if (!empty($a_thematic_advance_ids)) {
  876. $last_done_advance_id = array_pop($a_thematic_advance_ids);
  877. $last_done_advance_id = intval($last_done_advance_id);
  878. }
  879. return $last_done_advance_id;
  880. }
  881. /**
  882. * Get next thematic advance not done from thematic details interface
  883. * @return int next thematic advance not done
  884. */
  885. public function get_next_thematic_advance_not_done() {
  886. $thematic_data = $this->get_thematic_list();
  887. $thematic_advance_data = $this->get_thematic_advance_list();
  888. $a_thematic_advance_ids = array();
  889. $next_advance_not_done = 0;
  890. if (!empty($thematic_data)) {
  891. foreach ($thematic_data as $thematic) {
  892. $thematic_id = $thematic['id'];
  893. if (!empty($thematic_advance_data[$thematic['id']])) {
  894. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  895. if ($thematic_advance['done_advance'] == 0) {
  896. $a_thematic_advance_ids[] = $thematic_advance['id'];
  897. }
  898. }
  899. }
  900. }
  901. }
  902. if (!empty($a_thematic_advance_ids)) {
  903. $next_advance_not_done = array_shift($a_thematic_advance_ids);
  904. $next_advance_not_done = intval($next_advance_not_done);
  905. }
  906. return $next_advance_not_done;
  907. }
  908. /**
  909. * Get total average of thematic advances
  910. * @param string Course code (optional)
  911. * @param int Session id (optional)
  912. * @return float Average of thematic advances
  913. */
  914. public function get_total_average_of_thematic_advances($course_code = null, $session_id = null) {
  915. if (empty($course_code)) {
  916. $course_code = api_get_course_id();
  917. }
  918. if (api_get_session_id()) {
  919. $thematic_data = $this->get_thematic_list(null, $course_code );
  920. } else {
  921. $thematic_data = $this->get_thematic_list(null, $course_code, 0);
  922. }
  923. $new_thematic_data = array();
  924. if (!empty($thematic_data)) {
  925. foreach ($thematic_data as $item) {
  926. $new_thematic_data[] = $item;
  927. }
  928. $thematic_data = $new_thematic_data;
  929. }
  930. $thematic_advance_data = $this->get_thematic_advance_list(null, $course_code, true);
  931. $a_average_of_advances_by_thematic = array();
  932. $total_average = 0;
  933. if (!empty($thematic_data)) {
  934. foreach ($thematic_data as $thematic) {
  935. $thematic_id = $thematic['id'];
  936. $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic($thematic_id, $course_code);
  937. }
  938. }
  939. // calculate total average
  940. if (!empty($a_average_of_advances_by_thematic)) {
  941. $count_tematics = count($thematic_data);
  942. $score = array_sum($a_average_of_advances_by_thematic);
  943. $total_average = round(($score*100)/($count_tematics*100));
  944. }
  945. return $total_average;
  946. }
  947. /**
  948. * Get average of advances by thematic
  949. * @param int Thematic id
  950. * @param string Course code (optional)
  951. * @return float Average of thematic advances
  952. */
  953. public function get_average_of_advances_by_thematic($thematic_id, $course_code = null) {
  954. $thematic_advance_data = $this->get_thematic_advance_by_thematic_id($thematic_id, $course_code);
  955. $average = 0;
  956. if (!empty($thematic_advance_data)) {
  957. // get all done advances by thematic
  958. $advances = array();
  959. $count_done_advances = 0;
  960. $average = 0;
  961. foreach ($thematic_advance_data as $thematic_advance) {
  962. if ($thematic_advance['done_advance'] == 1) {
  963. $count_done_advances++;
  964. }
  965. $advances[] = $thematic_advance['done_advance'];
  966. }
  967. // calculate average by thematic
  968. $count_total_advances = count($advances);
  969. $average = round(($count_done_advances*100)/$count_total_advances);
  970. }
  971. return $average;
  972. }
  973. /**
  974. * set attributes for fields of thematic table
  975. * @param int Thematic id
  976. * @param string Thematic title
  977. * @param string Thematic content
  978. * @param int Session id
  979. * @return void
  980. */
  981. public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0) {
  982. $this->thematic_id = $id;
  983. $this->thematic_title = $title;
  984. $this->thematic_content = $content;
  985. $this->session_id = $session_id;
  986. }
  987. /**
  988. * set attributes for fields of thematic_plan table
  989. * @param int Thematic id
  990. * @param string Thematic plan title
  991. * @param string Thematic plan description
  992. * @param int Thematic plan description type
  993. * @return void
  994. */
  995. public function set_thematic_plan_attributes($thematic_id = 0, $title = '', $description = '', $description_type = 0) {
  996. $this->thematic_id = $thematic_id;
  997. $this->thematic_plan_title = $title;
  998. $this->thematic_plan_description = $description;
  999. $this->thematic_plan_description_type = $description_type;
  1000. }
  1001. /**
  1002. * set attributes for fields of thematic_advance table
  1003. * @param int Thematic advance id
  1004. * @param int Thematic id
  1005. * @param int Attendance id
  1006. * @param string Content
  1007. * @param string Date and time
  1008. * @param int Duration in hours
  1009. * @return void
  1010. */
  1011. public function set_thematic_advance_attributes($id = null, $thematic_id = 0, $attendance_id = 0, $content = '', $start_date = '0000-00-00 00:00:00', $duration = 0) {
  1012. $this->thematic_advance_id = $id;
  1013. $this->thematic_id = $thematic_id;
  1014. $this->attendance_id = $attendance_id;
  1015. $this->thematic_advance_content = $content;
  1016. $this->start_date = $start_date;
  1017. $this->duration = $duration;
  1018. }
  1019. /**
  1020. * set thematic id
  1021. * @param int Thematic id
  1022. * @return void
  1023. */
  1024. public function set_thematic_id($thematic_id) {
  1025. $this->thematic_id = $thematic_id;
  1026. }
  1027. /**
  1028. * get thematic id
  1029. * @return void
  1030. */
  1031. public function get_thematic_id() {
  1032. return $this->thematic_id;
  1033. }
  1034. /**
  1035. * Get thematic plan titles by default
  1036. * @return array
  1037. */
  1038. public function get_default_thematic_plan_title() {
  1039. $default_thematic_plan_titles = array();
  1040. $default_thematic_plan_titles[1]= get_lang('Objectives');
  1041. $default_thematic_plan_titles[2]= get_lang('SkillToAcquire');
  1042. $default_thematic_plan_titles[3]= get_lang('Methodology');
  1043. $default_thematic_plan_titles[4]= get_lang('Infrastructure');
  1044. $default_thematic_plan_titles[5]= get_lang('Assessment');
  1045. $default_thematic_plan_titles[6]= get_lang('Others');
  1046. return $default_thematic_plan_titles;
  1047. }
  1048. /**
  1049. * Get thematic plan icons by default
  1050. * @return array
  1051. */
  1052. public function get_default_thematic_plan_icon() {
  1053. $default_thematic_plan_icon = array();
  1054. $default_thematic_plan_icon[1]= 'icons/32/objective.png';
  1055. $default_thematic_plan_icon[2]= 'icons/32/skills.png';
  1056. $default_thematic_plan_icon[3]= 'icons/32/strategy.png';
  1057. $default_thematic_plan_icon[4]= 'icons/32/laptop.png';
  1058. $default_thematic_plan_icon[5]= 'icons/32/assessment.png';
  1059. $default_thematic_plan_icon[6]= 'icons/32/wizard.png';
  1060. return $default_thematic_plan_icon;
  1061. }
  1062. /**
  1063. * Get questions by default for help
  1064. * @return array
  1065. */
  1066. public function get_default_question() {
  1067. $question = array();
  1068. $question[1]= get_lang('ObjectivesQuestions');
  1069. $question[2]= get_lang('SkillToAcquireQuestions');
  1070. $question[3]= get_lang('MethodologyQuestions');
  1071. $question[4]= get_lang('InfrastructureQuestions');
  1072. $question[5]= get_lang('AssessmentQuestions');
  1073. return $question;
  1074. }
  1075. /**
  1076. * buid a string datetime from array
  1077. * @param array array containing data e.g: $array('Y'=>'2010', 'F' => '02', 'd' => '10', 'H' => '12', 'i' => '30')
  1078. * @return string date and time e.g: '2010-02-10 12:30:00'
  1079. */
  1080. public function build_datetime_from_array($array) {
  1081. $year = '0000';
  1082. $month = $day = $hours = $minutes = $seconds = '00';
  1083. if (isset($array['Y']) && isset($array['F']) && isset($array['d']) && isset($array['H']) && isset($array['i'])) {
  1084. $year = $array['Y'];
  1085. $month = $array['F'];
  1086. if (intval($month) < 10 ) $month = '0'.$month;
  1087. $day = $array['d'];
  1088. if (intval($day) < 10 ) $day = '0'.$day;
  1089. $hours = $array['H'];
  1090. if (intval($hours) < 10 ) $hours = '0'.$hours;
  1091. $minutes = $array['i'];
  1092. if (intval($minutes) < 10 ) $minutes = '0'.$minutes;
  1093. }
  1094. if (checkdate($month,$day,$year)) {
  1095. $datetime = $year.'-'.$month.'-'.$day.' '.$hours.':'.$minutes.':'.$seconds;
  1096. }
  1097. return $datetime;
  1098. }
  1099. }