thematic.lib.php 49 KB

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