thematic.lib.php 48 KB

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