thematic.lib.php 54 KB

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