thematic.lib.php 56 KB

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