thematic.lib.php 48 KB

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