thematic.lib.php 55 KB

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