thematic.lib.php 55 KB

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