thematic.lib.php 56 KB

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