thematic.lib.php 53 KB

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