thematic.lib.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. if (!empty($thematic_plan_data)) {
  490. foreach ($thematic_plan_data as $plan_data) {
  491. if (!empty($plan_data['title'])) {
  492. if (api_is_allowed_to_edit(null, true)) {
  493. if ($plan_data['session_id'] != 0) {
  494. $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']);
  495. }
  496. }
  497. $return .= Display::tag('h4', Security::remove_XSS($plan_data['title'], STUDENT).$session_star);
  498. $return .= Security::remove_XSS($plan_data['description'], STUDENT);
  499. $no_data = false;
  500. }
  501. }
  502. }
  503. if ($no_data) {
  504. $return .= '<div><em>'.get_lang('StillDoNotHaveAThematicPlan').'</em></div>';
  505. }
  506. $return .= '</div>';
  507. $final_return[$thematic_id] = $return;
  508. }
  509. return $final_return;
  510. }
  511. /**
  512. * get thematic advance list
  513. * @param int Thematic advance id (optional), get data by thematic advance list
  514. * @param string Course code (optional)
  515. * @return array data
  516. */
  517. public function get_thematic_advance_list($thematic_advance_id = null, $course_code = null, $force_session_id = false) { // set current course
  518. $course_info = api_get_course_info($course_code);
  519. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  520. $data = array();
  521. $condition = '';
  522. if (isset($thematic_advance_id)) {
  523. $thematic_advance_id = intval($thematic_advance_id);
  524. $condition = " AND a.id = $thematic_advance_id ";
  525. }
  526. $course_id = $course_info['real_id'];
  527. $sql = "SELECT * FROM $tbl_thematic_advance a WHERE c_id = $course_id $condition ORDER BY start_date ";
  528. $elements = array();
  529. if ($force_session_id) {
  530. $list = api_get_item_property_by_tool('thematic_advance', $course_info['code'], api_get_session_id());
  531. foreach($list as $value) {
  532. $elements[$value['ref']]= $value;
  533. }
  534. }
  535. $res = Database::query($sql);
  536. if (Database::num_rows($res) > 0) {
  537. if (!empty($thematic_advance_id)) {
  538. $data = Database::fetch_array($res);
  539. } else {
  540. // group all data group by thematic id
  541. $tmp = array();
  542. while ($row = Database::fetch_array($res, 'ASSOC')) {
  543. $tmp[] = $row['thematic_id'];
  544. if (in_array($row['thematic_id'], $tmp)) {
  545. if ($force_session_id) {
  546. if (in_array($row['id'], array_keys($elements))) {
  547. $row['session_id'] = $elements[$row['id']]['id_session'];
  548. $data[$row['thematic_id']][$row['id']] = $row;
  549. }
  550. } else {
  551. $data[$row['thematic_id']][$row['id']] = $row;
  552. }
  553. }
  554. }
  555. }
  556. }
  557. return $data;
  558. }
  559. /**
  560. * insert or update a thematic advance
  561. * @todo problem
  562. * @return int last thematic advance id
  563. */
  564. public function thematic_advance_save() {
  565. // definition database table
  566. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  567. // protect data
  568. $id = intval($this->thematic_advance_id);
  569. $tematic_id = intval($this->thematic_id);
  570. $attendance_id = intval($this->attendance_id);
  571. $content = Database::escape_string($this->thematic_advance_content);
  572. $start_date = Database::escape_string($this->start_date);
  573. $duration = intval($this->duration);
  574. $user_id = api_get_user_id();
  575. $course_id = $this->get_course_int_id();
  576. $course_info = api_get_course_info_by_id($course_id);
  577. if (empty($id)) {
  578. // Insert
  579. $sql = "INSERT INTO $tbl_thematic_advance (c_id, thematic_id, attendance_id, content, start_date, duration)
  580. VALUES ($course_id, $tematic_id, $attendance_id, '$content', '".api_get_utc_datetime($start_date)."', '$duration') ";
  581. Database::query($sql);
  582. $last_id = Database::insert_id();
  583. if (Database::affected_rows()) {
  584. api_item_property_update($course_info, 'thematic_advance', $last_id,"ThematicAdvanceAdded", $user_id);
  585. }
  586. } else {
  587. // update
  588. $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'
  589. WHERE c_id = $course_id AND id = $id ";
  590. Database::query($sql);
  591. if (Database::affected_rows()) {
  592. api_item_property_update($course_info, 'thematic_advance', $id, "ThematicAdvanceUpdated", $user_id);
  593. }
  594. }
  595. return $last_id;
  596. }
  597. /**
  598. * delete thematic advance
  599. * @param int Thematic advance id
  600. * @return int Affected rows
  601. */
  602. public function thematic_advance_destroy($thematic_advance_id) {
  603. global $_course;
  604. $course_id = $this->get_course_int_id();
  605. // definition database table
  606. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  607. // protect data
  608. $thematic_advance_id = intval($thematic_advance_id);
  609. $user_id = api_get_user_id();
  610. $sql = "DELETE FROM $tbl_thematic_advance WHERE c_id = $course_id AND id = $thematic_advance_id ";
  611. Database::query($sql);
  612. $affected_rows = Database::affected_rows();
  613. if ($affected_rows) {
  614. api_item_property_update($_course, 'thematic_advance', $thematic_advance_id,"ThematicAdvanceDeleted", $user_id);
  615. }
  616. return $affected_rows;
  617. }
  618. /**
  619. * get thematic plan data
  620. * @param int Thematic id (optional), get data by thematic id
  621. * @param int Thematic plan description type (optional), get data by description type
  622. * @return array Thematic plan data
  623. */
  624. public function get_thematic_plan_data($thematic_id = null, $description_type = null) {
  625. // definition database table
  626. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  627. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  628. $course_id = $this->get_course_int_id();
  629. $data = array();
  630. $condition = '';
  631. if (isset($thematic_id)) {
  632. $thematic_id = intval($thematic_id);
  633. $condition .= " AND thematic_id = $thematic_id ";
  634. }
  635. if (isset($description_type)) {
  636. //$description_type = intval($description_type);
  637. //$condition .= " AND description_type = $description_type ";
  638. }
  639. $items_from_course = api_get_item_property_by_tool('thematic_plan', api_get_course_id(), 0);
  640. $items_from_session = api_get_item_property_by_tool('thematic_plan', api_get_course_id(), api_get_session_id());
  641. $thematic_plan_complete_list = array();
  642. $thematic_plan_id_list = array();
  643. if (!empty($items_from_course)) {
  644. foreach($items_from_course as $item) {
  645. $thematic_plan_id_list[] = $item['ref'];
  646. $thematic_plan_complete_list[$item['ref']] = $item;
  647. }
  648. }
  649. if (!empty($items_from_session)) {
  650. foreach($items_from_session as $item) {
  651. $thematic_plan_id_list[] = $item['ref'];
  652. $thematic_plan_complete_list[$item['ref']] = $item;
  653. }
  654. }
  655. if (!empty($thematic_plan_id_list)) {
  656. $sql = "SELECT tp.id,
  657. thematic_id,
  658. tp.title,
  659. description,
  660. description_type,
  661. t.session_id
  662. FROM $tbl_thematic_plan tp
  663. INNER JOIN $tbl_thematic t ON (t.id = tp.thematic_id AND t.c_id = $course_id AND tp.c_id = $course_id)
  664. WHERE 1 = 1 $condition AND
  665. tp.id IN (".implode(', ', $thematic_plan_id_list).") ";
  666. $rs = Database::query($sql);
  667. if (Database::num_rows($rs)) {
  668. if (!isset($thematic_id)) {
  669. // group all data group by thematic id
  670. $tmp = array();
  671. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  672. $tmp[] = $row['thematic_id'];
  673. if (in_array($row['thematic_id'], $tmp)) {
  674. //$row['session_id'] = $thematic_plan_complete_list[$row['id']];
  675. $data[$row['thematic_id']][] = $row;
  676. }
  677. }
  678. } else {
  679. while ($row = Database::fetch_array($rs,'ASSOC')) {
  680. //$row['session_id'] = $thematic_plan_complete_list[$row['id']];
  681. $data[] = $row;
  682. }
  683. }
  684. }
  685. }
  686. return $data;
  687. }
  688. public function get_thematic_data_by_id($id) {
  689. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  690. $course_id = $this->get_course_int_id();
  691. $id = intval($id);
  692. $sql = "SELECT id FROM $tbl_thematic_plan
  693. WHERE c_id = $course_id AND
  694. id = $id ";
  695. $result = Database::query($sql);
  696. if (Database::num_rows($result)) {
  697. $row = Database::fetch_array($result, 'ASSOC');
  698. return $row;
  699. }
  700. return false;
  701. }
  702. /**
  703. * insert or update a thematic plan
  704. * @return int affected rows
  705. */
  706. public function thematic_plan_save() {
  707. // definition database table
  708. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  709. // protect data
  710. $thematic_id = intval($this->thematic_id);
  711. $title = Database::escape_string($this->thematic_plan_title);
  712. $description = Database::escape_string($this->thematic_plan_description);
  713. $description_type = intval($this->thematic_plan_description_type);
  714. $thematic_plan_id = intval($this->thematic_plan_id);
  715. $user_id = api_get_user_id();
  716. $course_id = $this->get_course_int_id();
  717. $session_id = $this->get_session_id();
  718. $course_info = api_get_course_info_by_id($course_id);
  719. $list = api_get_item_property_by_tool('thematic_plan', $course_info['code'], $session_id);
  720. $elements_to_show = array();
  721. foreach ($list as $value) {
  722. $elements_to_show[]= $value['ref'];
  723. }
  724. $condition = '';
  725. if (!empty($elements_to_show)) {
  726. $condition = "AND id IN (".implode(',', $elements_to_show).") ";
  727. }
  728. if ($thematic_plan_id) {
  729. // update
  730. $upd = "UPDATE $tbl_thematic_plan SET
  731. title = '$title',
  732. description = '$description'
  733. WHERE c_id = $course_id AND id = $thematic_plan_id";
  734. Database::query($upd);
  735. $affected_rows = Database::affected_rows();
  736. if ($affected_rows) {
  737. api_item_property_update($course_info, 'thematic_plan', $thematic_plan_id, "ThematicPlanUpdated", $user_id);
  738. }
  739. } else {
  740. // insert
  741. $ins = "INSERT INTO $tbl_thematic_plan (c_id, thematic_id, title, description, description_type)
  742. VALUES ($course_id, $thematic_id, '$title', '$description', $description_type) ";
  743. Database::query($ins);
  744. $last_id = Database::insert_id();
  745. $affected_rows = Database::affected_rows();
  746. if ($affected_rows) {
  747. api_item_property_update($course_info, 'thematic_plan', $last_id,"ThematicPlanAdded", $user_id);
  748. }
  749. }
  750. return $affected_rows;
  751. }
  752. /**
  753. * delete a thematic plan description
  754. * @param int Thematic id
  755. * @param int Description type
  756. * @return int Affected rows
  757. */
  758. public function thematic_plan_destroy($thematic_id, $description_type) {
  759. // definition database table
  760. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  761. // protect data
  762. $thematic_id = intval($thematic_id);
  763. $description_type = intval($description_type);
  764. $user_id = api_get_user_id();
  765. $course_id = $this->get_course_int_id();
  766. $course_info = api_get_course_info_by_id($course_id);
  767. // get thematic plan id
  768. $thematic_plan_data = $this->get_thematic_plan_data($thematic_id, $description_type);
  769. $thematic_plan_id = $thematic_plan_data[0]['id'];
  770. // delete
  771. $sql = "DELETE FROM $tbl_thematic_plan WHERE c_id = $course_id AND thematic_id = $thematic_id AND description_type = $description_type ";
  772. Database::query($sql);
  773. $affected_rows = Database::affected_rows();
  774. if ($affected_rows) {
  775. api_item_property_update($course_info, 'thematic_plan', $thematic_plan_id, "ThematicPlanDeleted", $user_id);
  776. }
  777. return $affected_rows;
  778. }
  779. public function thematic_plan_delete() {
  780. $course_id = $this->get_course_int_id();
  781. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  782. $thematic_plan_id = $this->thematic_plan_id;
  783. $course_info = api_get_course_info_by_id($course_id);
  784. if (empty($thematic_plan_id)) {
  785. return false;
  786. }
  787. $sql = "DELETE FROM $tbl_thematic_plan WHERE c_id = $course_id AND id = $thematic_plan_id ";
  788. Database::query($sql);
  789. api_item_property_update($course_info, 'thematic_plan', $thematic_plan_id, "ThematicPlanDeleted", api_get_user_id());
  790. }
  791. /**
  792. * Get next description type for a new thematic plan description (option 'others')
  793. * @param int Thematic id
  794. * @return int New Description type
  795. */
  796. public function get_next_description_type($thematic_id) {
  797. // definition database table
  798. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  799. // protect data
  800. $thematic_id = intval($thematic_id);
  801. $description_type = intval($description_type);
  802. $next_description_type = 0;
  803. $course_id = $this->get_course_int_id();
  804. $sql = "SELECT MAX(description_type) as max FROM $tbl_thematic_plan
  805. WHERE c_id = $course_id AND thematic_id = $thematic_id AND description_type >= ".ADD_THEMATIC_PLAN." ";
  806. $rs = Database::query($sql);
  807. $row = Database::fetch_array($rs);
  808. $last_description_type = $row['max'];
  809. if (isset($last_description_type)) {
  810. $row = Database::fetch_array($rs);
  811. $next_description_type = $last_description_type + 1;
  812. } else {
  813. $next_description_type = ADD_THEMATIC_PLAN;
  814. }
  815. return $next_description_type;
  816. }
  817. /**
  818. * update done thematic advances from thematic details interface
  819. * @param int Thematic id
  820. * @return int Affected rows
  821. */
  822. public function update_done_thematic_advances($thematic_advance_id) {
  823. global $_course;
  824. $thematic_data = $this->get_thematic_list(null, api_get_course_id());
  825. $thematic_advance_data = $this->get_thematic_advance_list(null, api_get_course_id(), true);
  826. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  827. $affected_rows = 0;
  828. $user_id = api_get_user_id();
  829. $all = array();
  830. if (!empty($thematic_data)) {
  831. foreach ($thematic_data as $thematic) {
  832. $thematic_id = $thematic['id'];
  833. if (!empty($thematic_advance_data[$thematic['id']])) {
  834. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  835. $all[] = $thematic_advance['id'];
  836. }
  837. }
  838. }
  839. }
  840. $error = null;
  841. $a_thematic_advance_ids = array();
  842. $course_id = $this->get_course_int_id();
  843. if (!empty($thematic_data)) {
  844. foreach ($thematic_data as $thematic) {
  845. $my_affected_rows = 0;
  846. $thematic_id = $thematic['id'];
  847. if (!empty($thematic_advance_data[$thematic['id']])) {
  848. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  849. $item_info = api_get_item_property_info(api_get_course_int_id(), 'thematic_advance', $thematic_advance['id']);
  850. if ($item_info['id_session'] == api_get_session_id()) {
  851. $a_thematic_advance_ids[] = $thematic_advance['id'];
  852. // update done thematic for previous advances ((done_advance = 1))
  853. $upd = "UPDATE $tbl_thematic_advance SET done_advance = 1 WHERE c_id = $course_id AND id = ".$thematic_advance['id']." ";
  854. Database::query($upd);
  855. $my_affected_rows = Database::affected_rows();
  856. $affected_rows += $my_affected_rows;
  857. //if ($my_affected_rows) {
  858. api_item_property_update($_course, 'thematic_advance', $thematic_advance['id'], "ThematicAdvanceDone", $user_id);
  859. //}
  860. if ($thematic_advance['id'] == $thematic_advance_id) {
  861. break 2;
  862. }
  863. }
  864. }
  865. }
  866. }
  867. }
  868. // Update done thematic for others advances (done_advance = 0)
  869. if (!empty($a_thematic_advance_ids) && count($a_thematic_advance_ids) > 0) {
  870. $diff = array_diff($all, $a_thematic_advance_ids);
  871. if (!empty($diff)) {
  872. $upd = "UPDATE $tbl_thematic_advance SET done_advance = 0 WHERE c_id = $course_id AND id IN(".implode(',',$diff).") ";
  873. Database::query($upd);
  874. }
  875. // update item_property
  876. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  877. $session_id = api_get_session_id();
  878. // get all thematic advance done
  879. $rs_thematic_done = Database::query("SELECT ref FROM $tbl_item_property
  880. WHERE c_id = $course_id AND tool='thematic_advance' AND lastedit_type='ThematicAdvanceDone' AND id_session = $session_id ");
  881. if (Database::num_rows($rs_thematic_done) > 0) {
  882. while ($row_thematic_done = Database::fetch_array($rs_thematic_done)) {
  883. $ref = $row_thematic_done['ref'];
  884. if (in_array($ref, $a_thematic_advance_ids)) { continue; }
  885. // update items
  886. 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 ");
  887. }
  888. }
  889. }
  890. return $affected_rows;
  891. }
  892. /**
  893. * Get last done thematic advance from thematic details interface
  894. * @return int Last done thematic advance id
  895. */
  896. public function get_last_done_thematic_advance() {
  897. $thematic_data = $this->get_thematic_list();
  898. $thematic_advance_data = $this->get_thematic_advance_list(null, api_get_course_id(), true);
  899. $a_thematic_advance_ids = array();
  900. $last_done_advance_id = 0;
  901. if (!empty($thematic_data)) {
  902. foreach ($thematic_data as $thematic) {
  903. $thematic_id = $thematic['id'];
  904. if (!empty($thematic_advance_data[$thematic['id']])) {
  905. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  906. if ($thematic_advance['done_advance'] == 1) {
  907. $a_thematic_advance_ids[] = $thematic_advance['id'];
  908. }
  909. }
  910. }
  911. }
  912. }
  913. if (!empty($a_thematic_advance_ids)) {
  914. $last_done_advance_id = array_pop($a_thematic_advance_ids);
  915. $last_done_advance_id = intval($last_done_advance_id);
  916. }
  917. return $last_done_advance_id;
  918. }
  919. /**
  920. * Get next thematic advance not done from thematic details interface
  921. * @return int next thematic advance not done
  922. */
  923. public function get_next_thematic_advance_not_done() {
  924. $thematic_data = $this->get_thematic_list();
  925. $thematic_advance_data = $this->get_thematic_advance_list();
  926. $a_thematic_advance_ids = array();
  927. $next_advance_not_done = 0;
  928. if (!empty($thematic_data)) {
  929. foreach ($thematic_data as $thematic) {
  930. $thematic_id = $thematic['id'];
  931. if (!empty($thematic_advance_data[$thematic['id']])) {
  932. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  933. if ($thematic_advance['done_advance'] == 0) {
  934. $a_thematic_advance_ids[] = $thematic_advance['id'];
  935. }
  936. }
  937. }
  938. }
  939. }
  940. if (!empty($a_thematic_advance_ids)) {
  941. $next_advance_not_done = array_shift($a_thematic_advance_ids);
  942. $next_advance_not_done = intval($next_advance_not_done);
  943. }
  944. return $next_advance_not_done;
  945. }
  946. /**
  947. * Get total average of thematic advances
  948. * @param string Course code (optional)
  949. * @param int Session id (optional)
  950. * @return float Average of thematic advances
  951. */
  952. public function get_total_average_of_thematic_advances($course_code = null, $session_id = null) {
  953. if (empty($course_code)) {
  954. $course_code = api_get_course_id();
  955. }
  956. if (api_get_session_id()) {
  957. $thematic_data = $this->get_thematic_list(null, $course_code );
  958. } else {
  959. $thematic_data = $this->get_thematic_list(null, $course_code, 0);
  960. }
  961. $new_thematic_data = array();
  962. if (!empty($thematic_data)) {
  963. foreach ($thematic_data as $item) {
  964. $new_thematic_data[] = $item;
  965. }
  966. $thematic_data = $new_thematic_data;
  967. }
  968. $thematic_advance_data = $this->get_thematic_advance_list(null, $course_code, true);
  969. $a_average_of_advances_by_thematic = array();
  970. $total_average = 0;
  971. if (!empty($thematic_data)) {
  972. foreach ($thematic_data as $thematic) {
  973. $thematic_id = $thematic['id'];
  974. $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic($thematic_id, $course_code);
  975. }
  976. }
  977. // calculate total average
  978. if (!empty($a_average_of_advances_by_thematic)) {
  979. $count_tematics = count($thematic_data);
  980. $score = array_sum($a_average_of_advances_by_thematic);
  981. $total_average = round(($score*100)/($count_tematics*100));
  982. }
  983. return $total_average;
  984. }
  985. /**
  986. * Get average of advances by thematic
  987. * @param int Thematic id
  988. * @param string Course code (optional)
  989. * @return float Average of thematic advances
  990. */
  991. public function get_average_of_advances_by_thematic($thematic_id, $course_code = null) {
  992. $thematic_advance_data = $this->get_thematic_advance_by_thematic_id($thematic_id, $course_code);
  993. $average = 0;
  994. if (!empty($thematic_advance_data)) {
  995. // get all done advances by thematic
  996. $advances = array();
  997. $count_done_advances = 0;
  998. $average = 0;
  999. foreach ($thematic_advance_data as $thematic_advance) {
  1000. if ($thematic_advance['done_advance'] == 1) {
  1001. $count_done_advances++;
  1002. }
  1003. $advances[] = $thematic_advance['done_advance'];
  1004. }
  1005. // calculate average by thematic
  1006. $count_total_advances = count($advances);
  1007. $average = round(($count_done_advances*100)/$count_total_advances);
  1008. }
  1009. return $average;
  1010. }
  1011. /**
  1012. * set attributes for fields of thematic table
  1013. * @param int Thematic id
  1014. * @param string Thematic title
  1015. * @param string Thematic content
  1016. * @param int Session id
  1017. * @return void
  1018. */
  1019. public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0) {
  1020. $this->thematic_id = $id;
  1021. $this->thematic_title = $title;
  1022. $this->thematic_content = $content;
  1023. $this->session_id = $session_id;
  1024. }
  1025. /**
  1026. * set attributes for fields of thematic_plan table
  1027. * @param int Thematic id
  1028. * @param string Thematic plan title
  1029. * @param string Thematic plan description
  1030. * @param int Thematic plan description type
  1031. * @return void
  1032. */
  1033. public function set_thematic_plan_attributes($thematic_id = 0, $title = '', $description = '', $description_type = 0, $thematic_plan_id = 0) {
  1034. $this->thematic_id = $thematic_id;
  1035. $this->thematic_plan_title = $title;
  1036. $this->thematic_plan_description = $description;
  1037. $this->thematic_plan_description_type = $description_type;
  1038. $this->thematic_plan_id = $thematic_plan_id;
  1039. }
  1040. /**
  1041. * set attributes for fields of thematic_advance table
  1042. * @param int Thematic advance id
  1043. * @param int Thematic id
  1044. * @param int Attendance id
  1045. * @param string Content
  1046. * @param string Date and time
  1047. * @param int Duration in hours
  1048. * @return void
  1049. */
  1050. 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) {
  1051. $this->thematic_advance_id = $id;
  1052. $this->thematic_id = $thematic_id;
  1053. $this->attendance_id = $attendance_id;
  1054. $this->thematic_advance_content = $content;
  1055. $this->start_date = $start_date;
  1056. $this->duration = $duration;
  1057. }
  1058. /**
  1059. * set thematic id
  1060. * @param int Thematic id
  1061. * @return void
  1062. */
  1063. public function set_thematic_id($thematic_id) {
  1064. $this->thematic_id = $thematic_id;
  1065. }
  1066. /**
  1067. * get thematic id
  1068. * @return void
  1069. */
  1070. public function get_thematic_id() {
  1071. return $this->thematic_id;
  1072. }
  1073. /**
  1074. * Get thematic plan titles by default
  1075. * @return array
  1076. */
  1077. public function get_default_thematic_plan_title() {
  1078. $default_thematic_plan_titles = array();
  1079. $default_thematic_plan_titles[1]= get_lang('Objectives');
  1080. $default_thematic_plan_titles[2]= get_lang('SkillToAcquire');
  1081. $default_thematic_plan_titles[3]= get_lang('Methodology');
  1082. $default_thematic_plan_titles[4]= get_lang('Infrastructure');
  1083. $default_thematic_plan_titles[5]= get_lang('Assessment');
  1084. $default_thematic_plan_titles[6]= get_lang('Others');
  1085. return $default_thematic_plan_titles;
  1086. }
  1087. /**
  1088. * Get thematic plan icons by default
  1089. * @return array
  1090. */
  1091. public function get_default_thematic_plan_icon() {
  1092. $default_thematic_plan_icon = array();
  1093. $default_thematic_plan_icon[1]= 'icons/32/objective.png';
  1094. $default_thematic_plan_icon[2]= 'icons/32/skills.png';
  1095. $default_thematic_plan_icon[3]= 'icons/32/strategy.png';
  1096. $default_thematic_plan_icon[4]= 'icons/32/laptop.png';
  1097. $default_thematic_plan_icon[5]= 'icons/32/assessment.png';
  1098. $default_thematic_plan_icon[6]= 'icons/32/wizard.png';
  1099. return $default_thematic_plan_icon;
  1100. }
  1101. /**
  1102. * Get questions by default for help
  1103. * @return array
  1104. */
  1105. public function get_default_question() {
  1106. $question = array();
  1107. $question[1]= get_lang('ObjectivesQuestions');
  1108. $question[2]= get_lang('SkillToAcquireQuestions');
  1109. $question[3]= get_lang('MethodologyQuestions');
  1110. $question[4]= get_lang('InfrastructureQuestions');
  1111. $question[5]= get_lang('AssessmentQuestions');
  1112. return $question;
  1113. }
  1114. /**
  1115. * buid a string datetime from array
  1116. * @param array array containing data e.g: $array('Y'=>'2010', 'F' => '02', 'd' => '10', 'H' => '12', 'i' => '30')
  1117. * @return string date and time e.g: '2010-02-10 12:30:00'
  1118. */
  1119. public function build_datetime_from_array($array) {
  1120. return Text::return_datetime_from_array($array);
  1121. }
  1122. }