thematic.lib.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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. $param_gradebook = '';
  83. if (isset($_SESSION['gradebook'])) {
  84. $param_gradebook = '&gradebook='.$_SESSION['gradebook'];
  85. }
  86. $user_info = api_get_user_info(api_get_user_id());
  87. while ($thematic = Database::fetch_row($res)) {
  88. $session_star = '';
  89. if (api_get_session_id() == $thematic[3]) {
  90. $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
  91. }
  92. $thematic[1] = '<a href="index.php?'.api_get_cidreq().'&action=thematic_details&thematic_id='.$thematic[0].$param_gradebook.'">'.Security::remove_XSS($thematic[1], STUDENT).$session_star.'</a>';
  93. if (api_is_allowed_to_edit(null, true)) {
  94. $actions = '';
  95. if (api_get_session_id()) {
  96. if (api_get_session_id() == $thematic[3]) {
  97. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].$param_gradebook.'">'.
  98. Display::return_icon('lesson_plan.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  99. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].$param_gradebook.'">'.
  100. Display::return_icon('lesson_plan_calendar.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  101. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].$param_gradebook.'">'.
  102. Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>';
  103. $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].$param_gradebook.'">'.
  104. Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>';
  105. } else {
  106. $actions .= Display::return_icon('lesson_plan_na.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'&nbsp;';
  107. $actions .= Display::return_icon('lesson_plan_calendar_na.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'&nbsp;';
  108. $actions .= Display::return_icon('edit_na.png',get_lang('Edit'),'',ICON_SIZE_SMALL);
  109. $actions .= Display::return_icon('delete_na.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'&nbsp;';
  110. $actions .= Display::url(Display::return_icon('cd.gif', get_lang('Copy')), 'index.php?'.api_get_cidreq().'&action=thematic_copy&thematic_id='.$thematic[0].$param_gradebook);
  111. }
  112. } else {
  113. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].$param_gradebook.'">'.
  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].$param_gradebook.'">'.
  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].$param_gradebook.'">'.
  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].$param_gradebook.'">'.
  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].$param_gradebook.'">'.
  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].$param_gradebook.'">'.
  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. ];
  678. $last_id = Database::insert($tbl_thematic_advance, $params);
  679. if ($last_id) {
  680. $sql = "UPDATE $tbl_thematic_advance SET id = iid WHERE iid = $last_id";
  681. Database::query($sql);
  682. api_item_property_update(
  683. $_course,
  684. 'thematic_advance',
  685. $last_id,
  686. "ThematicAdvanceAdded",
  687. $user_id
  688. );
  689. }
  690. } else {
  691. $params = [
  692. 'thematic_id' => $thematic_id,
  693. 'attendance_id' => $attendance_id,
  694. 'content' => $content,
  695. 'start_date' => api_get_utc_datetime($start_date),
  696. 'duration' => $duration
  697. ];
  698. Database::update(
  699. $tbl_thematic_advance,
  700. $params,
  701. ['id = ? AND c_id = ?' => [$id, $this->course_int_id]]
  702. );
  703. api_item_property_update(
  704. $_course,
  705. 'thematic_advance',
  706. $id,
  707. "ThematicAdvanceUpdated",
  708. $user_id
  709. );
  710. }
  711. return $last_id;
  712. }
  713. /**
  714. * delete thematic advance
  715. * @param int Thematic advance id
  716. * @return int Affected rows
  717. */
  718. public function thematic_advance_destroy($thematic_advance_id)
  719. {
  720. $_course = api_get_course_info();
  721. $course_id = api_get_course_int_id();
  722. // definition database table
  723. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  724. // protect data
  725. $thematic_advance_id = intval($thematic_advance_id);
  726. $user_id = api_get_user_id();
  727. $sql = "DELETE FROM $tbl_thematic_advance
  728. WHERE c_id = $course_id AND id = $thematic_advance_id ";
  729. $result = Database::query($sql);
  730. $affected_rows = Database::affected_rows($result);
  731. if ($affected_rows) {
  732. api_item_property_update(
  733. $_course,
  734. 'thematic_advance',
  735. $thematic_advance_id,
  736. "ThematicAdvanceDeleted",
  737. $user_id
  738. );
  739. }
  740. return $affected_rows;
  741. }
  742. /**
  743. * get thematic plan data
  744. * @param int Thematic id (optional), get data by thematic id
  745. * @param int Thematic plan description type (optional), get data by description type
  746. * @return array Thematic plan data
  747. */
  748. public function get_thematic_plan_data($thematic_id = null, $description_type = null)
  749. {
  750. // definition database table
  751. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  752. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  753. $course_id = api_get_course_int_id();
  754. $data = array();
  755. $condition = '';
  756. if (isset($thematic_id)) {
  757. $thematic_id = intval($thematic_id);
  758. $condition .= " AND thematic_id = $thematic_id ";
  759. }
  760. if (isset($description_type)) {
  761. $description_type = intval($description_type);
  762. $condition .= " AND description_type = $description_type ";
  763. }
  764. $items_from_course = api_get_item_property_by_tool(
  765. 'thematic_plan',
  766. api_get_course_id(),
  767. 0
  768. );
  769. $items_from_session = api_get_item_property_by_tool(
  770. 'thematic_plan',
  771. api_get_course_id(),
  772. api_get_session_id()
  773. );
  774. $thematic_plan_complete_list = array();
  775. $thematic_plan_id_list = array();
  776. if (!empty($items_from_course)) {
  777. foreach($items_from_course as $item) {
  778. $thematic_plan_id_list[] = $item['ref'];
  779. $thematic_plan_complete_list[$item['ref']] = $item;
  780. }
  781. }
  782. if (!empty($items_from_session)) {
  783. foreach($items_from_session as $item) {
  784. $thematic_plan_id_list[] = $item['ref'];
  785. $thematic_plan_complete_list[$item['ref']] = $item;
  786. }
  787. }
  788. if (!empty($thematic_plan_id_list)) {
  789. $sql = "SELECT
  790. tp.id, thematic_id, tp.title, description, description_type, t.session_id
  791. FROM $tbl_thematic_plan tp
  792. INNER JOIN $tbl_thematic t ON (t.id=tp.thematic_id)
  793. WHERE
  794. t.c_id = $course_id AND
  795. tp.c_id = $course_id
  796. $condition AND
  797. tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
  798. $rs = Database::query($sql);
  799. if (Database::num_rows($rs)) {
  800. if (!isset($thematic_id) && !isset($description_type)) {
  801. // group all data group by thematic id
  802. $tmp = array();
  803. while ($row = Database::fetch_array($rs,'ASSOC')) {
  804. $tmp[] = $row['thematic_id'];
  805. if (in_array($row['thematic_id'], $tmp)) {
  806. $row['session_id'] = $thematic_plan_complete_list[$row['id']];
  807. $data[$row['thematic_id']][$row['description_type']] = $row;
  808. }
  809. }
  810. } else {
  811. while ($row = Database::fetch_array($rs,'ASSOC')) {
  812. $row['session_id'] = $thematic_plan_complete_list[$row['id']];
  813. $data[] = $row;
  814. }
  815. }
  816. }
  817. }
  818. return $data;
  819. }
  820. /**
  821. * insert or update a thematic plan
  822. * @return int affected rows
  823. */
  824. public function thematic_plan_save()
  825. {
  826. $_course = api_get_course_info();
  827. // definition database table
  828. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  829. // protect data
  830. $thematic_id = intval($this->thematic_id);
  831. $title = $this->thematic_plan_title;
  832. $description = $this->thematic_plan_description;
  833. $description_type = intval($this->thematic_plan_description_type);
  834. $user_id = api_get_user_id();
  835. $course_id = api_get_course_int_id();
  836. $list = api_get_item_property_by_tool(
  837. 'thematic_plan',
  838. api_get_course_id(),
  839. api_get_session_id()
  840. );
  841. $elements_to_show = array();
  842. foreach($list as $value) {
  843. $elements_to_show[]= $value['ref'];
  844. }
  845. $condition = '';
  846. if (!empty($elements_to_show)) {
  847. $condition = "AND id IN (".implode(',', $elements_to_show).") ";
  848. }
  849. // check thematic plan type already exists
  850. $sql = "SELECT id FROM $tbl_thematic_plan
  851. WHERE
  852. c_id = $course_id AND
  853. thematic_id = $thematic_id AND
  854. description_type = '$description_type'";
  855. $rs = Database::query($sql);
  856. $affected_rows = 0;
  857. if (Database::num_rows($rs) > 0) {
  858. $row_thematic_plan = Database::fetch_array($rs);
  859. $thematic_plan_id = $row_thematic_plan['id'];
  860. $update = false;
  861. if (in_array($thematic_plan_id, $elements_to_show)) {
  862. $update = true;
  863. }
  864. if ($update) {
  865. // update
  866. $params = [
  867. 'title' => $title,
  868. 'description' => $description
  869. ];
  870. Database::update(
  871. $tbl_thematic_plan,
  872. $params,
  873. ['c_id = ? AND id = ?' => [$course_id, $thematic_plan_id]]
  874. );
  875. api_item_property_update(
  876. $_course,
  877. 'thematic_plan',
  878. $thematic_plan_id,
  879. "ThematicPlanUpdated",
  880. $user_id
  881. );
  882. } else {
  883. // insert
  884. $params = [
  885. 'c_id' => $this->course_int_id,
  886. 'thematic_id' => $thematic_id,
  887. 'title' => $title,
  888. 'description' => $description,
  889. 'description_type' => $description_type
  890. ];
  891. $last_id = Database::insert($tbl_thematic_plan, $params);
  892. if ($last_id) {
  893. $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $last_id";
  894. Database::query($sql);
  895. api_item_property_update(
  896. $_course,
  897. 'thematic_plan',
  898. $last_id,
  899. "ThematicPlanAdded",
  900. $user_id
  901. );
  902. }
  903. }
  904. } else {
  905. // insert
  906. $params = [
  907. 'c_id' => $this->course_int_id,
  908. 'thematic_id' => $thematic_id,
  909. 'title' => $title,
  910. 'description' => $description,
  911. 'description_type' => $description_type
  912. ];
  913. $last_id = Database::insert($tbl_thematic_plan, $params);
  914. if ($last_id) {
  915. $sql = "UPDATE $tbl_thematic_plan SET id = iid WHERE iid = $last_id";
  916. Database::query($sql);
  917. api_item_property_update(
  918. $_course,
  919. 'thematic_plan',
  920. $last_id,
  921. "ThematicPlanAdded",
  922. $user_id
  923. );
  924. }
  925. }
  926. return $affected_rows;
  927. }
  928. /**
  929. * delete a thematic plan description
  930. * @param int $thematic_id Thematic id
  931. * @param int $description_type Description type
  932. * @return int Affected rows
  933. */
  934. public function thematic_plan_destroy($thematic_id, $description_type)
  935. {
  936. $_course = api_get_course_info();
  937. // definition database table
  938. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  939. // protect data
  940. $thematic_id = intval($thematic_id);
  941. $description_type = intval($description_type);
  942. $user_id = api_get_user_id();
  943. $course_info = api_get_course_info();
  944. $course_id = $course_info['real_id'];
  945. // get thematic plan id
  946. $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
  947. $thematic_plan_id = $thematic_plan_data[0]['id'];
  948. // delete
  949. $sql = "DELETE FROM $tbl_thematic_plan
  950. WHERE
  951. c_id = $course_id AND
  952. thematic_id = $thematic_id AND
  953. description_type = $description_type ";
  954. $result = Database::query($sql);
  955. $affected_rows = Database::affected_rows($result);
  956. if ($affected_rows) {
  957. api_item_property_update(
  958. $_course,
  959. 'thematic_plan',
  960. $thematic_plan_id,
  961. "ThematicPlanDeleted",
  962. $user_id
  963. );
  964. }
  965. return $affected_rows;
  966. }
  967. /**
  968. * Get next description type for a new thematic plan description (option 'others')
  969. * @param int Thematic id
  970. * @return int New Description type
  971. */
  972. public function get_next_description_type($thematic_id)
  973. {
  974. // definition database table
  975. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  976. // protect data
  977. $thematic_id = intval($thematic_id);
  978. $course_id = api_get_course_int_id();
  979. $sql = "SELECT MAX(description_type) as max
  980. FROM $tbl_thematic_plan
  981. WHERE
  982. c_id = $course_id AND
  983. thematic_id = $thematic_id AND
  984. description_type >= ".ADD_THEMATIC_PLAN." ";
  985. $rs = Database::query($sql);
  986. $row = Database::fetch_array($rs);
  987. $last_description_type = $row['max'];
  988. if (isset($last_description_type)) {
  989. $next_description_type = $last_description_type + 1;
  990. } else {
  991. $next_description_type = ADD_THEMATIC_PLAN;
  992. }
  993. return $next_description_type;
  994. }
  995. /**
  996. * update done thematic advances from thematic details interface
  997. * @param int Thematic id
  998. * @return int Affected rows
  999. */
  1000. public function update_done_thematic_advances($thematic_advance_id)
  1001. {
  1002. $_course = api_get_course_info();
  1003. $thematic_data = $this->get_thematic_list(null, api_get_course_id());
  1004. $thematic_advance_data = $this->get_thematic_advance_list(null, api_get_course_id(), true);
  1005. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  1006. $affected_rows = 0;
  1007. $user_id = api_get_user_id();
  1008. $all = array();
  1009. if (!empty($thematic_data)) {
  1010. foreach ($thematic_data as $thematic) {
  1011. $thematic_id = $thematic['id'];
  1012. if (!empty($thematic_advance_data[$thematic['id']])) {
  1013. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  1014. $all[] = $thematic_advance['id'];
  1015. }
  1016. }
  1017. }
  1018. }
  1019. $error = null;
  1020. $a_thematic_advance_ids = array();
  1021. $course_id = api_get_course_int_id();
  1022. $sessionId = api_get_session_id();
  1023. if (!empty($thematic_data)) {
  1024. foreach ($thematic_data as $thematic) {
  1025. $my_affected_rows = 0;
  1026. $thematic_id = $thematic['id'];
  1027. if (!empty($thematic_advance_data[$thematic['id']])) {
  1028. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  1029. $item_info = api_get_item_property_info(
  1030. api_get_course_int_id(),
  1031. 'thematic_advance',
  1032. $thematic_advance['id'],
  1033. $sessionId
  1034. );
  1035. if ($item_info['id_session'] == $sessionId) {
  1036. $a_thematic_advance_ids[] = $thematic_advance['id'];
  1037. // update done thematic for previous advances ((done_advance = 1))
  1038. $upd = "UPDATE $tbl_thematic_advance SET
  1039. done_advance = 1
  1040. WHERE c_id = $course_id AND id = ".$thematic_advance['id']." ";
  1041. $result = Database::query($upd);
  1042. $my_affected_rows = Database::affected_rows($result);
  1043. $affected_rows += $my_affected_rows;
  1044. //if ($my_affected_rows) {
  1045. api_item_property_update(
  1046. $_course,
  1047. 'thematic_advance',
  1048. $thematic_advance['id'],
  1049. "ThematicAdvanceDone",
  1050. $user_id
  1051. );
  1052. //}
  1053. if ($thematic_advance['id'] == $thematic_advance_id) {
  1054. break 2;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. }
  1060. }
  1061. // Update done thematic for others advances (done_advance = 0)
  1062. if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
  1063. $diff = array_diff($all, $a_thematic_advance_ids);
  1064. if (!empty($diff)) {
  1065. $upd = "UPDATE $tbl_thematic_advance SET done_advance = 0
  1066. WHERE c_id = $course_id AND id IN(".implode(',',$diff).") ";
  1067. Database::query($upd);
  1068. }
  1069. // update item_property
  1070. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1071. $sql = "SELECT ref FROM $tbl_item_property
  1072. WHERE
  1073. c_id = $course_id AND
  1074. tool='thematic_advance' AND
  1075. lastedit_type='ThematicAdvanceDone' AND
  1076. session_id = $sessionId ";
  1077. // get all thematic advance done
  1078. $rs_thematic_done = Database::query($sql);
  1079. if (Database::num_rows($rs_thematic_done) > 0) {
  1080. while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
  1081. $ref = $row_thematic_done['ref'];
  1082. if (in_array($ref, $a_thematic_advance_ids)) { continue; }
  1083. // update items
  1084. $sql = "UPDATE $tbl_item_property SET
  1085. lastedit_date='".api_get_utc_datetime()."',
  1086. lastedit_type='ThematicAdvanceUpdated',
  1087. lastedit_user_id = $user_id
  1088. WHERE
  1089. c_id = $course_id AND
  1090. tool='thematic_advance' AND
  1091. ref=$ref AND
  1092. session_id = $sessionId ";
  1093. Database::query($sql);
  1094. }
  1095. }
  1096. }
  1097. return $affected_rows;
  1098. }
  1099. /**
  1100. * Get last done thematic advance from thematic details interface
  1101. * @return int Last done thematic advance id
  1102. */
  1103. public function get_last_done_thematic_advance()
  1104. {
  1105. $thematic_data = $this->get_thematic_list();
  1106. $thematic_advance_data = $this->get_thematic_advance_list(
  1107. null,
  1108. api_get_course_id(),
  1109. true
  1110. );
  1111. $a_thematic_advance_ids = array();
  1112. $last_done_advance_id = 0;
  1113. if (!empty($thematic_data)) {
  1114. foreach ($thematic_data as $thematic) {
  1115. if (!empty($thematic_advance_data[$thematic['id']])) {
  1116. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  1117. if ($thematic_advance['done_advance'] == 1) {
  1118. $a_thematic_advance_ids[] = $thematic_advance['id'];
  1119. }
  1120. }
  1121. }
  1122. }
  1123. }
  1124. if (!empty($a_thematic_advance_ids)) {
  1125. $last_done_advance_id = array_pop($a_thematic_advance_ids);
  1126. $last_done_advance_id = intval($last_done_advance_id);
  1127. }
  1128. return $last_done_advance_id;
  1129. }
  1130. /**
  1131. * Get next thematic advance not done from thematic details interface
  1132. * @param int Offset (if you want to get an item that is not directly the next)
  1133. * @return int next thematic advance not done
  1134. */
  1135. public function get_next_thematic_advance_not_done($offset = 1)
  1136. {
  1137. $thematic_data = $this->get_thematic_list();
  1138. $thematic_advance_data = $this->get_thematic_advance_list();
  1139. $a_thematic_advance_ids = array();
  1140. $next_advance_not_done = 0;
  1141. if (!empty($thematic_data)) {
  1142. foreach ($thematic_data as $thematic) {
  1143. if (!empty($thematic_advance_data[$thematic['id']])) {
  1144. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  1145. if ($thematic_advance['done_advance'] == 0) {
  1146. $a_thematic_advance_ids[] = $thematic_advance['id'];
  1147. }
  1148. }
  1149. }
  1150. }
  1151. }
  1152. if (!empty($a_thematic_advance_ids)) {
  1153. for ($i = 0; $i < $offset; $i++) {
  1154. $next_advance_not_done = array_shift($a_thematic_advance_ids);
  1155. }
  1156. $next_advance_not_done = intval($next_advance_not_done);
  1157. }
  1158. return $next_advance_not_done;
  1159. }
  1160. /**
  1161. * Get total average of thematic advances
  1162. * @param string Course code (optional)
  1163. * @param int Session id (optional)
  1164. * @return float Average of thematic advances
  1165. */
  1166. public function get_total_average_of_thematic_advances($course_code = null, $session_id = null)
  1167. {
  1168. if (empty($course_code)) {
  1169. $course_code = api_get_course_id();
  1170. }
  1171. if (api_get_session_id()) {
  1172. $thematic_data = $this->get_thematic_list(null, $course_code );
  1173. } else {
  1174. $thematic_data = $this->get_thematic_list(null, $course_code, 0);
  1175. }
  1176. $new_thematic_data = array();
  1177. if (!empty($thematic_data)) {
  1178. foreach ($thematic_data as $item) {
  1179. $new_thematic_data[] = $item;
  1180. }
  1181. $thematic_data = $new_thematic_data;
  1182. }
  1183. $a_average_of_advances_by_thematic = array();
  1184. $total_average = 0;
  1185. if (!empty($thematic_data)) {
  1186. foreach ($thematic_data as $thematic) {
  1187. $thematic_id = $thematic['id'];
  1188. $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic($thematic_id, $course_code);
  1189. }
  1190. }
  1191. // calculate total average
  1192. if (!empty($a_average_of_advances_by_thematic)) {
  1193. $count_tematics = count($thematic_data);
  1194. $score = array_sum($a_average_of_advances_by_thematic);
  1195. $total_average = round(($score * 100) / ($count_tematics * 100));
  1196. }
  1197. return $total_average;
  1198. }
  1199. /**
  1200. * Get average of advances by thematic
  1201. * @param int Thematic id
  1202. * @param string Course code (optional)
  1203. * @return float Average of thematic advances
  1204. */
  1205. public function get_average_of_advances_by_thematic($thematic_id, $course_code = null)
  1206. {
  1207. $thematic_advance_data = $this->get_thematic_advance_by_thematic_id($thematic_id, $course_code);
  1208. $average = 0;
  1209. if (!empty($thematic_advance_data)) {
  1210. // get all done advances by thematic
  1211. $advances = array();
  1212. $count_done_advances = 0;
  1213. foreach ($thematic_advance_data as $thematic_advance) {
  1214. if ($thematic_advance['done_advance'] == 1) {
  1215. $count_done_advances++;
  1216. }
  1217. $advances[] = $thematic_advance['done_advance'];
  1218. }
  1219. // calculate average by thematic
  1220. $count_total_advances = count($advances);
  1221. $average = round(($count_done_advances*100)/$count_total_advances);
  1222. }
  1223. return $average;
  1224. }
  1225. /**
  1226. * set attributes for fields of thematic table
  1227. * @param int Thematic id
  1228. * @param string Thematic title
  1229. * @param string Thematic content
  1230. * @param int Session id
  1231. * @return void
  1232. */
  1233. public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0)
  1234. {
  1235. $this->thematic_id = $id;
  1236. $this->thematic_title = $title;
  1237. $this->thematic_content = $content;
  1238. $this->session_id = $session_id;
  1239. }
  1240. /**
  1241. * set attributes for fields of thematic_plan table
  1242. * @param int Thematic id
  1243. * @param string Thematic plan title
  1244. * @param string Thematic plan description
  1245. * @param int Thematic plan description type
  1246. * @return void
  1247. */
  1248. public function set_thematic_plan_attributes($thematic_id = 0, $title = '', $description = '', $description_type = 0)
  1249. {
  1250. $this->thematic_id = $thematic_id;
  1251. $this->thematic_plan_title = $title;
  1252. $this->thematic_plan_description = $description;
  1253. $this->thematic_plan_description_type = $description_type;
  1254. }
  1255. /**
  1256. * set attributes for fields of thematic_advance table
  1257. * @param int Thematic advance id
  1258. * @param int Thematic id
  1259. * @param int Attendance id
  1260. * @param string Content
  1261. * @param string Date and time
  1262. * @param int Duration in hours
  1263. * @return void
  1264. */
  1265. public function set_thematic_advance_attributes(
  1266. $id = null,
  1267. $thematic_id = 0,
  1268. $attendance_id = 0,
  1269. $content = '',
  1270. $start_date = '0000-00-00 00:00:00',
  1271. $duration = 0
  1272. ) {
  1273. $this->thematic_advance_id = $id;
  1274. $this->thematic_id = $thematic_id;
  1275. $this->attendance_id = $attendance_id;
  1276. $this->thematic_advance_content = $content;
  1277. $this->start_date = $start_date;
  1278. $this->duration = $duration;
  1279. }
  1280. /**
  1281. * set thematic id
  1282. * @param int Thematic id
  1283. * @return void
  1284. */
  1285. public function set_thematic_id($thematic_id)
  1286. {
  1287. $this->thematic_id = $thematic_id;
  1288. }
  1289. /**
  1290. * get thematic id
  1291. * @return void
  1292. */
  1293. public function get_thematic_id()
  1294. {
  1295. return $this->thematic_id;
  1296. }
  1297. /**
  1298. * Get thematic plan titles by default
  1299. * @return array
  1300. */
  1301. public function get_default_thematic_plan_title()
  1302. {
  1303. $default_thematic_plan_titles = array();
  1304. $default_thematic_plan_titles[1]= get_lang('Objectives');
  1305. $default_thematic_plan_titles[2]= get_lang('SkillToAcquire');
  1306. $default_thematic_plan_titles[3]= get_lang('Methodology');
  1307. $default_thematic_plan_titles[4]= get_lang('Infrastructure');
  1308. $default_thematic_plan_titles[5]= get_lang('Assessment');
  1309. $default_thematic_plan_titles[6]= get_lang('Others');
  1310. return $default_thematic_plan_titles;
  1311. }
  1312. /**
  1313. * Get thematic plan icons by default
  1314. * @return array
  1315. */
  1316. public function get_default_thematic_plan_icon()
  1317. {
  1318. $default_thematic_plan_icon = array();
  1319. $default_thematic_plan_icon[1]= 'icons/32/objective.png';
  1320. $default_thematic_plan_icon[2]= 'icons/32/skills.png';
  1321. $default_thematic_plan_icon[3]= 'icons/32/strategy.png';
  1322. $default_thematic_plan_icon[4]= 'icons/32/laptop.png';
  1323. $default_thematic_plan_icon[5]= 'icons/32/assessment.png';
  1324. $default_thematic_plan_icon[6]= 'icons/32/wizard.png';
  1325. return $default_thematic_plan_icon;
  1326. }
  1327. /**
  1328. * Get questions by default for help
  1329. * @return array
  1330. */
  1331. public function get_default_question()
  1332. {
  1333. $question = array();
  1334. $question[1]= get_lang('ObjectivesQuestions');
  1335. $question[2]= get_lang('SkillToAcquireQuestions');
  1336. $question[3]= get_lang('MethodologyQuestions');
  1337. $question[4]= get_lang('InfrastructureQuestions');
  1338. $question[5]= get_lang('AssessmentQuestions');
  1339. return $question;
  1340. }
  1341. }