thematic.lib.php 47 KB

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