thematic.lib.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. * @package chamilo.attendance
  7. */
  8. /**
  9. * Thematic class can be used to instanciate objects or as a library for thematic control
  10. * @package chamilo.attendance
  11. */
  12. class Thematic
  13. {
  14. private $session_id;
  15. private $thematic_id;
  16. private $thematic_title;
  17. private $thematic_content;
  18. private $thematic_plan_id;
  19. private $thematic_plan_title;
  20. private $thematic_plan_description;
  21. private $thematic_plan_description_type;
  22. private $thematic_advance_id;
  23. private $attendance_id;
  24. private $thematic_advance_content;
  25. private $start_date;
  26. private $duration;
  27. public function __construct() {}
  28. /**
  29. * Get the total number of thematic inside current course and current session
  30. * @see SortableTable#get_total_number_of_items()
  31. */
  32. public function get_number_of_thematics() {
  33. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  34. $session_id = api_get_session_id();
  35. $condition_session = api_get_session_condition($session_id);
  36. $sql = "SELECT COUNT(id) AS total_number_of_items FROM $tbl_thematic WHERE active = 1 $condition_session ";
  37. $res = Database::query($sql);
  38. $res = Database::query($sql);
  39. $obj = Database::fetch_object($res);
  40. return $obj->total_number_of_items;
  41. }
  42. /**
  43. * Get the thematics to display on the current page (fill the sortable-table)
  44. * @param int offset of first user to recover
  45. * @param int Number of users to get
  46. * @param int Column to sort on
  47. * @param string Order (ASC,DESC)
  48. * @see SortableTable#get_table_data($from)
  49. */
  50. public function get_thematic_data($from, $number_of_items, $column, $direction) {
  51. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  52. $session_id = api_get_session_id();
  53. $condition_session = api_get_session_condition($session_id);
  54. $column = intval($column);
  55. $from = intval($from);
  56. $number_of_items = intval($number_of_items);
  57. if (!in_array($direction, array('ASC','DESC'))) {
  58. $direction = 'ASC';
  59. }
  60. $sql = "SELECT
  61. id AS col0,
  62. title AS col1,
  63. display_order AS col2
  64. FROM $tbl_thematic
  65. WHERE active = 1 $condition_session
  66. ORDER BY col2 LIMIT $from,$number_of_items ";
  67. $res = Database::query($sql);
  68. $thematics = array ();
  69. $param_gradebook = '';
  70. if (isset($_SESSION['gradebook'])) {
  71. $param_gradebook = '&gradebook='.$_SESSION['gradebook'];
  72. }
  73. while ($thematic = Database::fetch_row($res)) {
  74. $thematic[1] = '<a href="index.php?'.api_get_cidreq().'&action=thematic_details&thematic_id='.$thematic[0].$param_gradebook.'">'.$thematic[1].'</a>';
  75. if (api_is_allowed_to_edit(null, true)) {
  76. $actions = '';
  77. $actions .= '<center><a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('info.gif',get_lang('ThematicPlan')).'</a>&nbsp;';
  78. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('porcent.png',get_lang('ThematicAdvance')).'</a>&nbsp;';
  79. if ($thematic[2] > 1) {
  80. $actions .= '<a href="'.api_get_self().'?action=moveup&'.api_get_cidreq().'&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('up.gif', get_lang('Up')).'</a>';
  81. } else {
  82. $actions .= Display::return_icon('up_na.gif','&nbsp;');
  83. }
  84. if ($thematic[2] < self::get_max_thematic_item()) {
  85. $actions .= '<a href="'.api_get_self().'?action=movedown&a'.api_get_cidreq().'&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('down.gif',get_lang('Down')).'</a>';
  86. } else {
  87. $actions .= Display::return_icon('down_na.gif','&nbsp;');
  88. }
  89. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].$param_gradebook.'">'.Display::return_icon('edit.gif',get_lang('Edit')).'</a>&nbsp;';
  90. $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.gif',get_lang('Delete')).'</a></center>';
  91. $thematics[] = array($thematic[0], $thematic[1], $actions);
  92. }
  93. }
  94. return $thematics;
  95. }
  96. /**
  97. * Get the maximum display order of the thematic item
  98. * @return int Maximum display order
  99. */
  100. public function get_max_thematic_item() {
  101. // Database table definition
  102. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  103. $session_id = api_get_session_id();
  104. $condition_session = api_get_session_condition($session_id);
  105. $sql = "SELECT MAX(display_order) FROM $tbl_thematic WHERE active = 1 $condition_session";
  106. $rs = Database::query($sql);
  107. $dsp=0;
  108. $row = Database::fetch_array($rs);
  109. return $row[0];
  110. }
  111. /**
  112. * Move a thematic
  113. *
  114. * @param string Direction (up, down)
  115. * @param int Thematic id
  116. */
  117. public function move_thematic($direction, $thematic_id) {
  118. // Database table definition
  119. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  120. // sort direction
  121. if ($direction == 'up') {
  122. $sortorder = 'DESC';
  123. } else {
  124. $sortorder = 'ASC';
  125. }
  126. $session_id = api_get_session_id();
  127. $condition_session = api_get_session_condition($session_id);
  128. $sql = "SELECT * FROM $tbl_thematic WHERE active = 1 $condition_session ORDER BY display_order $sortorder";
  129. $res = Database::query($sql);
  130. $found = false;
  131. while ($row = Database::fetch_array($res)) {
  132. if ($found == true && empty($next_id)) {
  133. $next_id = intval($row['id']);
  134. $next_display_order = intval($row['display_order']);
  135. }
  136. if ($row['id'] == $thematic_id) {
  137. $current_id = intval($thematic_id);
  138. $current_display_order = intval($row['display_order']);
  139. $found = true;
  140. }
  141. }
  142. // get last done thematic advance before move thematic list
  143. $last_done_thematic_advance = $this->get_last_done_thematic_advance();
  144. $sql = "UPDATE $tbl_thematic SET display_order = $next_display_order WHERE id = $current_id ";
  145. Database::query($sql);
  146. $sql = "UPDATE $tbl_thematic SET display_order = $current_display_order WHERE id = $next_id ";
  147. Database::query($sql);
  148. // update done advances with de current thematic list
  149. $update_done_advances = $this->update_done_thematic_advances($last_done_thematic_advance);
  150. }
  151. /**
  152. * get thematic list
  153. * @param int Thematic id (optional), get list by id
  154. * @return array Thematic data
  155. */
  156. public function get_thematic_list($thematic_id = null, $course_code = null, $session_id = null) {
  157. // set current course and session
  158. if (isset($course_code)) {
  159. $course_info = api_get_course_info($course_code);
  160. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC, $course_info['dbName']);
  161. } else {
  162. $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC);
  163. }
  164. if (isset($session_id)) {
  165. $session_id = intval($session_id);
  166. } else {
  167. $session_id = api_get_session_id();
  168. }
  169. $data = array();
  170. $condition = '';
  171. if (isset($thematic_id)) {
  172. $thematic_id = intval($thematic_id);
  173. $condition = " WHERE id = $thematic_id ";
  174. } else {
  175. $condition_session = api_get_session_condition($session_id);
  176. $condition = " WHERE active = 1 $condition_session ";
  177. }
  178. $sql = "SELECT * FROM $tbl_thematic $condition ORDER BY display_order ";
  179. $res = Database::query($sql);
  180. if (Database::num_rows($res) > 0) {
  181. if (!empty($thematic_id)) {
  182. $data = Database::fetch_array($res);
  183. } else {
  184. while ($row = Database::fetch_array($res)) {
  185. $data[$row['id']] = $row;
  186. }
  187. }
  188. }
  189. return $data;
  190. }
  191. /**
  192. * insert or update a thematic
  193. * @return int last thematic id
  194. */
  195. public function thematic_save() {
  196. // definition database table
  197. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  198. // protect data
  199. $id = intval($this->thematic_id);
  200. $title = Database::escape_string($this->thematic_title);
  201. $content = Database::escape_string($this->thematic_content);
  202. $session_id = intval($this->session_id);
  203. // get the maximum display order of all the glossary items
  204. $max_thematic_item = $this->get_max_thematic_item();
  205. if (empty($id)) {
  206. // insert
  207. $sql = "INSERT INTO $tbl_thematic(title, content, active, display_order, session_id) VALUES ('$title', '$content', 1, ".(intval($max_thematic_item)+1).", $session_id) ";
  208. Database::query($sql);
  209. $last_id = Database::insert_id();
  210. } else {
  211. // update
  212. $sql = "UPDATE $tbl_thematic SET title = '$title', content = '$content', session_id = $session_id WHERE id = $id ";
  213. Database::query($sql);
  214. $last_id = $id;
  215. }
  216. return $last_id;
  217. }
  218. /**
  219. * Delete logically (set active field to 0) a thematic
  220. * @param int|array One or many thematic ids
  221. * @return int Affected rows
  222. */
  223. public function thematic_destroy($thematic_id) {
  224. $tbl_thematic = Database::get_course_table(TABLE_THEMATIC);
  225. $affected_rows = 0;
  226. if (is_array($thematic_id)) {
  227. foreach ($thematic_id as $id) {
  228. $id = intval($id);
  229. $sql = "UPDATE $tbl_thematic SET active = 0 WHERE id = $id";
  230. Database::query($sql);
  231. $affected_rows += Database::affected_rows();
  232. }
  233. } else {
  234. $thematic_id = intval($thematic_id);
  235. $sql = "UPDATE $tbl_thematic SET active = 0 WHERE id = $thematic_id";
  236. Database::query($sql);
  237. $affected_rows = Database::affected_rows();
  238. }
  239. return $affected_rows;
  240. }
  241. /**
  242. * Get the total number of thematic advance inside current course
  243. * @see SortableTable#get_total_number_of_items()
  244. */
  245. public function get_number_of_thematic_advances() {
  246. global $thematic_id;
  247. $tbl_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  248. $sql = "SELECT COUNT(id) AS total_number_of_items FROM $tbl_thematic_advance WHERE thematic_id = $thematic_id ";
  249. $res = Database::query($sql);
  250. $res = Database::query($sql);
  251. $obj = Database::fetch_object($res);
  252. return $obj->total_number_of_items;
  253. }
  254. /**
  255. * Get the thematic advances to display on the current page (fill the sortable-table)
  256. * @param int offset of first user to recover
  257. * @param int Number of users to get
  258. * @param int Column to sort on
  259. * @param string Order (ASC,DESC)
  260. * @see SortableTable#get_table_data($from)
  261. */
  262. public function get_thematic_advance_data($from, $number_of_items, $column, $direction) {
  263. global $thematic_id;
  264. $tbl_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  265. $thematic_data = self::get_thematic_list($thematic_id);
  266. $column = intval($column);
  267. $from = intval($from);
  268. $number_of_items = intval($number_of_items);
  269. if (!in_array($direction, array('ASC','DESC'))) {
  270. $direction = 'ASC';
  271. }
  272. $sql = "SELECT
  273. id AS col0,
  274. start_date AS col1,
  275. duration AS col2,
  276. content AS col3
  277. FROM $tbl_thematic_advance
  278. WHERE thematic_id = $thematic_id
  279. ORDER BY col$column $direction LIMIT $from,$number_of_items ";
  280. $res = Database::query($sql);
  281. $data = array ();
  282. $param_gradebook = '';
  283. if (isset($_SESSION['gradebook'])) {
  284. $param_gradebook = '&gradebook='.$_SESSION['gradebook'];
  285. }
  286. $i = 1;
  287. while ($thematic_advance = Database::fetch_row($res)) {
  288. $thematic_advance[1] = api_get_local_time($thematic_advance[1]);
  289. if (api_is_allowed_to_edit(null, true)) {
  290. $actions = '';
  291. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].$param_gradebook.'">'.Display::return_icon('edit.gif',get_lang('Edit')).'</a>&nbsp;';
  292. $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].$param_gradebook.'">'.Display::return_icon('delete.gif',get_lang('Delete')).'</a></center>';
  293. $data[] = array($i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions);
  294. }
  295. $i++;
  296. }
  297. return $data;
  298. }
  299. /**
  300. * get thematic advance data by tematic id
  301. * @param int Thematic id
  302. * @param string Course code (optional)
  303. * @return array data
  304. */
  305. public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null) {
  306. // set current course
  307. if (isset($course_code)) {
  308. $course_info = api_get_course_info($course_code);
  309. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE, $course_info['dbName']);
  310. } else {
  311. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  312. }
  313. $thematic_id = intval($thematic_id);
  314. $data = array();
  315. $sql = "SELECT * FROM $tbl_thematic_advance WHERE thematic_id = $thematic_id ";
  316. $res = Database::query($sql);
  317. if (Database::num_rows($res) > 0) {
  318. while ($row = Database::fetch_array($res)) {
  319. $data[] = $row;
  320. }
  321. }
  322. return $data;
  323. }
  324. /**
  325. * get thematic advance list
  326. * @param int Thematic advance id (optional), get data by thematic advance list
  327. * @param string Course code (optional)
  328. * @return array data
  329. */
  330. public function get_thematic_advance_list($thematic_advance_id = null, $course_code = null) {
  331. // set current course
  332. if (isset($course_code)) {
  333. $course_info = api_get_course_info($course_code);
  334. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE, $course_info['dbName']);
  335. } else {
  336. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  337. }
  338. $data = array();
  339. $condition = '';
  340. if (isset($thematic_advance_id)) {
  341. $thematic_advance_id = intval($thematic_advance_id);
  342. $condition = " WHERE id = $thematic_advance_id ";
  343. }
  344. $sql = "SELECT * FROM $tbl_thematic_advance $condition ORDER BY start_date ";
  345. $res = Database::query($sql);
  346. if (Database::num_rows($res) > 0) {
  347. if (!empty($thematic_advance_id)) {
  348. $data = Database::fetch_array($res);
  349. } else {
  350. // group all data group by thematic id
  351. $tmp = array();
  352. while ($row = Database::fetch_array($res)) {
  353. $tmp[] = $row['thematic_id'];
  354. if (in_array($row['thematic_id'], $tmp)) {
  355. $data[$row['thematic_id']][$row['id']] = $row;
  356. }
  357. }
  358. }
  359. }
  360. return $data;
  361. }
  362. /**
  363. * insert or update a thematic advance
  364. * @return int last thematic advance id
  365. */
  366. public function thematic_advance_save() {
  367. // definition database table
  368. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  369. // protect data
  370. $id = intval($this->thematic_advance_id);
  371. $tematic_id = intval($this->thematic_id);
  372. $attendance_id = intval($this->attendance_id);
  373. $content = Database::escape_string($this->thematic_advance_content);
  374. $start_date = Database::escape_string($this->start_date);
  375. $duration = intval($this->duration);
  376. if (empty($id)) {
  377. // insert
  378. $sql = "INSERT INTO $tbl_thematic_advance (thematic_id, attendance_id, content, start_date, duration) VALUES ($tematic_id, $attendance_id, '$content', '".api_get_utc_datetime($start_date)."', $duration) ";
  379. Database::query($sql);
  380. $last_id = Database::insert_id();
  381. } else {
  382. // update
  383. $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 WHERE id = $id ";
  384. Database::query($sql);
  385. $last_id = $id;
  386. }
  387. return $last_id;
  388. }
  389. /**
  390. * delete thematic advance
  391. * @param int Thematic advance id
  392. * @return int Affected rows
  393. */
  394. public function thematic_advance_destroy($thematic_advance_id) {
  395. // definition database table
  396. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  397. // protect data
  398. $thematic_advance_id = intval($thematic_advance_id);
  399. $sql = "DELETE FROM $tbl_thematic_advance WHERE id = $thematic_advance_id ";
  400. Database::query($sql);
  401. $affected_rows = Database::affected_rows();
  402. return $affected_rows;
  403. }
  404. /**
  405. * get thematic plan data
  406. * @param int Thematic id (optional), get data by thematic id
  407. * @param int Thematic plan description type (optional), get data by description type
  408. * @return array Thematic plan data
  409. */
  410. public function get_thematic_plan_data($thematic_id = null, $description_type = null) {
  411. // definition database table
  412. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  413. $data = array();
  414. $condition = '';
  415. if (isset($thematic_id)) {
  416. $thematic_id = intval($thematic_id);
  417. $condition .= " AND thematic_id = $thematic_id ";
  418. }
  419. if (isset($description_type)) {
  420. $description_type = intval($description_type);
  421. $condition .= " AND description_type = $description_type ";
  422. }
  423. $sql = "SELECT * FROM $tbl_thematic_plan WHERE 1 $condition";
  424. $rs = Database::query($sql);
  425. if (Database::num_rows($rs) > 0) {
  426. if (!isset($thematic_id) && !isset($description_type)) {
  427. // group all data group by thematic id
  428. $tmp = array();
  429. while ($row = Database::fetch_array($rs)) {
  430. $tmp[] = $row['thematic_id'];
  431. if (in_array($row['thematic_id'], $tmp)) {
  432. $data[$row['thematic_id']][$row['id']] = $row;
  433. }
  434. }
  435. } else {
  436. while ($row = Database::fetch_array($rs)) {
  437. $data[] = $row;
  438. }
  439. }
  440. }
  441. return $data;
  442. }
  443. /**
  444. * insert or update a thematic plan
  445. * @return int affected rows
  446. */
  447. public function thematic_plan_save() {
  448. // definition database table
  449. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  450. // protect data
  451. $thematic_id = intval($this->thematic_id);
  452. $title = Database::escape_string($this->thematic_plan_title);
  453. $description = Database::escape_string($this->thematic_plan_description);
  454. $description_type = intval($this->thematic_plan_description_type);
  455. // check thematic plan type already exists
  456. $sql = "SELECT id FROM $tbl_thematic_plan WHERE thematic_id = $thematic_id AND description_type = $description_type ";
  457. $rs = Database::query($sql);
  458. $affected_rows = 0;
  459. if (Database::num_rows($rs) > 0) {
  460. // update
  461. $upd = "UPDATE $tbl_thematic_plan SET title = '$title', description = '$description' WHERE thematic_id = $thematic_id AND description_type = $description_type ";
  462. Database::query($upd);
  463. $affected_rows = Database::affected_rows();
  464. } else {
  465. // insert
  466. $ins = "INSERT INTO $tbl_thematic_plan(thematic_id, title, description, description_type) VALUES($thematic_id, '$title', '$description', $description_type) ";
  467. Database::query($ins);
  468. $affected_rows = Database::affected_rows();
  469. }
  470. return $affected_rows;
  471. }
  472. /**
  473. * delete a thematic plan description
  474. * @param int Thematic id
  475. * @param int Description type
  476. * @return int Affected rows
  477. */
  478. public function thematic_plan_destroy($thematic_id, $description_type) {
  479. // definition database table
  480. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  481. // protect data
  482. $thematic_id = intval($thematic_id);
  483. $description_type = intval($description_type);
  484. $sql = "DELETE FROM $tbl_thematic_plan WHERE thematic_id = $thematic_id AND description_type = $description_type ";
  485. Database::query($sql);
  486. $affected_rows = Database::affected_rows();
  487. return $affected_rows;
  488. }
  489. /**
  490. * Get next description type for a new thematic plan description (option 'others')
  491. * @param int Thematic id
  492. * @return int New Description type
  493. */
  494. public function get_next_description_type($thematic_id) {
  495. // definition database table
  496. $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN);
  497. // protect data
  498. $thematic_id = intval($thematic_id);
  499. $description_type = intval($description_type);
  500. $next_description_type = 0;
  501. $sql = "SELECT MAX(description_type) as max FROM $tbl_thematic_plan WHERE thematic_id = $thematic_id AND description_type >= ".ADD_THEMATIC_PLAN." ";
  502. $rs = Database::query($sql);
  503. $row = Database::fetch_array($rs);
  504. $last_description_type = $row['max'];
  505. if (isset($last_description_type)) {
  506. $row = Database::fetch_array($rs);
  507. $next_description_type = $last_description_type + 1;
  508. } else {
  509. $next_description_type = ADD_THEMATIC_PLAN;
  510. }
  511. return $next_description_type;
  512. }
  513. /**
  514. * update done thematic advances from thematic details interface
  515. * @param int Thematic id
  516. * @return int Affected rows
  517. */
  518. public function update_done_thematic_advances ($thematic_advance_id) {
  519. $thematic_data = $this->get_thematic_list();
  520. $thematic_advance_data = $this->get_thematic_advance_list();
  521. $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE);
  522. $affected_rows = 0;
  523. $a_thematic_advance_ids = array();
  524. if (!empty($thematic_data)) {
  525. foreach ($thematic_data as $thematic) {
  526. $thematic_id = $thematic['id'];
  527. if (!empty($thematic_advance_data[$thematic['id']])) {
  528. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  529. $a_thematic_advance_ids[] = $thematic_advance['id'];
  530. // update done thematic for previous advances ((done_advance = 1))
  531. $upd = "UPDATE $tbl_thematic_advance set done_advance = 1 WHERE id = ".$thematic_advance['id']." ";
  532. Database::query($upd);
  533. $affected_rows += Database::affected_rows();
  534. if ($thematic_advance['id'] == $thematic_advance_id) {
  535. break 2;
  536. }
  537. }
  538. }
  539. }
  540. }
  541. // update done thematic for others advances (done_advance = 0)
  542. if (!empty($a_thematic_advance_ids)) {
  543. $upd = "UPDATE $tbl_thematic_advance set done_advance = 0 WHERE id NOT IN(".implode(',',$a_thematic_advance_ids).") ";
  544. Database::query($upd);
  545. }
  546. return $affected_rows;
  547. }
  548. /**
  549. * Get last done thematic advance from thematic details interface
  550. * @return int Last done thematic advance id
  551. */
  552. public function get_last_done_thematic_advance() {
  553. $thematic_data = $this->get_thematic_list();
  554. $thematic_advance_data = $this->get_thematic_advance_list();
  555. $a_thematic_advance_ids = array();
  556. $last_done_advance_id = 0;
  557. if (!empty($thematic_data)) {
  558. foreach ($thematic_data as $thematic) {
  559. $thematic_id = $thematic['id'];
  560. if (!empty($thematic_advance_data[$thematic['id']])) {
  561. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  562. if ($thematic_advance['done_advance'] == 1) {
  563. $a_thematic_advance_ids[] = $thematic_advance['id'];
  564. }
  565. }
  566. }
  567. }
  568. }
  569. if (!empty($a_thematic_advance_ids)) {
  570. $last_done_advance_id = array_pop($a_thematic_advance_ids);
  571. $last_done_advance_id = intval($last_done_advance_id);
  572. }
  573. return $last_done_advance_id;
  574. }
  575. /**
  576. * Get next thematic advance not done from thematic details interface
  577. * @return int next thematic advance not done
  578. */
  579. public function get_next_thematic_advance_not_done() {
  580. $thematic_data = $this->get_thematic_list();
  581. $thematic_advance_data = $this->get_thematic_advance_list();
  582. $a_thematic_advance_ids = array();
  583. $next_advance_not_done = 0;
  584. if (!empty($thematic_data)) {
  585. foreach ($thematic_data as $thematic) {
  586. $thematic_id = $thematic['id'];
  587. if (!empty($thematic_advance_data[$thematic['id']])) {
  588. foreach ($thematic_advance_data[$thematic['id']] as $thematic_advance) {
  589. if ($thematic_advance['done_advance'] == 0) {
  590. $a_thematic_advance_ids[] = $thematic_advance['id'];
  591. }
  592. }
  593. }
  594. }
  595. }
  596. if (!empty($a_thematic_advance_ids)) {
  597. $next_advance_not_done = array_shift($a_thematic_advance_ids);
  598. $next_advance_not_done = intval($next_advance_not_done);
  599. }
  600. return $next_advance_not_done;
  601. }
  602. /**
  603. * Get total average of thematic advances
  604. * @param string Course code (optional)
  605. * @param int Session id (optional)
  606. * @return float Average of thematic advances
  607. */
  608. public function get_total_average_of_thematic_advances($course_code = null, $session_id = null) {
  609. $thematic_data = $this->get_thematic_list(null, $course_code, $session_id);
  610. $thematic_advance_data = $this->get_thematic_advance_list(null, $course_code);
  611. $a_average_of_advances_by_thematic = array();
  612. $total_average = 0;
  613. if (!empty($thematic_data)) {
  614. foreach ($thematic_data as $thematic) {
  615. $thematic_id = $thematic['id'];
  616. $a_average_of_advances_by_thematic[$thematic_id] = $this->get_average_of_advances_by_thematic($thematic_id, $course_code);
  617. }
  618. }
  619. // calculate total average
  620. if (!empty($a_average_of_advances_by_thematic)) {
  621. $count_tematics = count($thematic_data);
  622. $score = array_sum($a_average_of_advances_by_thematic);
  623. $total_average = round(($score*100)/($count_tematics*100),2);
  624. }
  625. return $total_average;
  626. }
  627. /**
  628. * Get average of advances by thematic
  629. * @param int Thematic id
  630. * @param string Course code (optional)
  631. * @return float Average of thematic advances
  632. */
  633. public function get_average_of_advances_by_thematic($thematic_id, $course_code = null) {
  634. $thematic_advance_data = $this->get_thematic_advance_by_thematic_id($thematic_id, $course_code);
  635. $average = 0;
  636. if (!empty($thematic_advance_data)) {
  637. // get all done advances by thematic
  638. $advances = array();
  639. $count_done_advances = 0;
  640. $average = 0;
  641. foreach ($thematic_advance_data as $thematic_advance) {
  642. if ($thematic_advance['done_advance'] == 1) {
  643. $count_done_advances++;
  644. }
  645. $advances[] = $thematic_advance['done_advance'];
  646. }
  647. // calculate average by thematic
  648. $count_total_advances = count($advances);
  649. $average = round(($count_done_advances*100)/$count_total_advances,2);
  650. }
  651. return $average;
  652. }
  653. /**
  654. * set attributes for fields of thematic table
  655. * @param int Thematic id
  656. * @param string Thematic title
  657. * @param string Thematic content
  658. * @param int Session id
  659. * @return void
  660. */
  661. public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0) {
  662. $this->thematic_id = $id;
  663. $this->thematic_title = $title;
  664. $this->thematic_content = $content;
  665. $this->session_id = $session_id;
  666. }
  667. /**
  668. * set attributes for fields of thematic_plan table
  669. * @param int Thematic id
  670. * @param string Thematic plan title
  671. * @param string Thematic plan description
  672. * @param int Thematic plan description type
  673. * @return void
  674. */
  675. public function set_thematic_plan_attributes($thematic_id = 0, $title = '', $description = '', $description_type = 0) {
  676. $this->thematic_id = $thematic_id;
  677. $this->thematic_plan_title = $title;
  678. $this->thematic_plan_description = $description;
  679. $this->thematic_plan_description_type = $description_type;
  680. }
  681. /**
  682. * set attributes for fields of thematic_advance table
  683. * @param int Thematic advance id
  684. * @param int Thematic id
  685. * @param int Attendance id
  686. * @param string Content
  687. * @param string Date and time
  688. * @param int Duration in hours
  689. * @return void
  690. */
  691. 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) {
  692. $this->thematic_advance_id = $id;
  693. $this->thematic_id = $thematic_id;
  694. $this->attendance_id = $attendance_id;
  695. $this->thematic_advance_content = $content;
  696. $this->start_date = $start_date;
  697. $this->duration = $duration;
  698. }
  699. /**
  700. * set thematic id
  701. * @param int Thematic id
  702. * @return void
  703. */
  704. public function set_thematic_id($thematic_id) {
  705. $this->thematic_id = $thematic_id;
  706. }
  707. /**
  708. * get thematic id
  709. * @return void
  710. */
  711. public function get_thematic_id() {
  712. return $this->thematic_id;
  713. }
  714. /**
  715. * Get thematic plan titles by default
  716. * @return array
  717. */
  718. public function get_default_thematic_plan_title() {
  719. $default_thematic_plan_titles = array();
  720. $default_thematic_plan_titles[1]= get_lang('Objectives');
  721. $default_thematic_plan_titles[2]= get_lang('SkillToAcquire');
  722. $default_thematic_plan_titles[3]= get_lang('Infrastructure');
  723. $default_thematic_plan_titles[4]= get_lang('Methodology');
  724. $default_thematic_plan_titles[5]= get_lang('AditionalNotes');
  725. $default_thematic_plan_titles[6]= get_lang('Others');
  726. return $default_thematic_plan_titles;
  727. }
  728. /**
  729. * Get thematic plan icons by default
  730. * @return array
  731. */
  732. public function get_default_thematic_plan_icon() {
  733. $default_thematic_plan_icon = array();
  734. $default_thematic_plan_icon[1]= 'spire.gif';
  735. $default_thematic_plan_icon[2]= 'korganizer.gif';
  736. $default_thematic_plan_icon[3]= 'kcmdf_big.gif';
  737. $default_thematic_plan_icon[4]= 'misc.gif';
  738. $default_thematic_plan_icon[5]= 'ktip.gif';
  739. $default_thematic_plan_icon[6]= 'new_test.gif';
  740. return $default_thematic_plan_icon;
  741. }
  742. /**
  743. * Get questions by default for help
  744. * @return array
  745. */
  746. public function get_default_question() {
  747. $question = array();
  748. $question[1]= get_lang('ObjectivesQuestions');
  749. $question[2]= get_lang('SkillToAcquireQuestions');
  750. $question[3]= get_lang('InfrastructureQuestions');
  751. $question[4]= get_lang('MethodologyQuestions');
  752. $question[5]= get_lang('AditionalNotesQuestions');
  753. return $question;
  754. }
  755. /**
  756. * buid a string datetime from array
  757. * @param array array containing data e.g: $array('Y'=>'2010', 'F' => '02', 'd' => '10', 'H' => '12', 'i' => '30')
  758. * @return string date and time e.g: '2010-02-10 12:30:00'
  759. */
  760. public function build_datetime_from_array($array) {
  761. $year = '0000';
  762. $month = $day = $hours = $minutes = $seconds = '00';
  763. if (isset($array['Y']) && isset($array['F']) && isset($array['d']) && isset($array['H']) && isset($array['i'])) {
  764. $year = $array['Y'];
  765. $month = $array['F'];
  766. if (intval($month) < 10 ) $month = '0'.$month;
  767. $day = $array['d'];
  768. if (intval($day) < 10 ) $day = '0'.$day;
  769. $hours = $array['H'];
  770. if (intval($hours) < 10 ) $hours = '0'.$hours;
  771. $minutes = $array['i'];
  772. if (intval($minutes) < 10 ) $minutes = '0'.$minutes;
  773. }
  774. if (checkdate($month,$day,$year)) {
  775. $datetime = $year.'-'.$month.'-'.$day.' '.$hours.':'.$minutes.':'.$seconds;
  776. }
  777. return $datetime;
  778. }
  779. }
  780. ?>