thematic.lib.php 55 KB

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