thematic.lib.php 56 KB

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