thematic_advance.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * View (MVC patter) for thematic advance.
  5. *
  6. * @author Christian Fasanando <christian1827@gmail.com>
  7. * @author Julio Montoya <gugli100@gmail.com> BeezNest 2011 Bug fixing
  8. *
  9. * @package chamilo.course_progress
  10. */
  11. // protect a course script
  12. api_protect_course_script(true);
  13. $tpl = new Template(get_lang('Thematic control'));
  14. $toolbar = null;
  15. $formLayout = null;
  16. if ($action === 'thematic_advance_add' || $action === 'thematic_advance_edit') {
  17. $header_form = get_lang('New thematic advance');
  18. if ($action === 'thematic_advance_edit') {
  19. $header_form = get_lang('Edit thematic advance');
  20. }
  21. // display form
  22. $form = new FormValidator(
  23. 'thematic_advance',
  24. 'POST',
  25. api_get_self().'?'.api_get_cidreq()
  26. );
  27. $form->addElement('header', $header_form);
  28. //$form->addElement('hidden', 'thematic_advance_token',$token);
  29. $form->addElement('hidden', 'action', $action);
  30. if (!empty($thematic_advance_id)) {
  31. $form->addElement('hidden', 'thematic_advance_id', $thematic_advance_id);
  32. }
  33. if (!empty($thematic_id)) {
  34. $form->addElement('hidden', 'thematic_id', $thematic_id);
  35. }
  36. $radios = [];
  37. $radios[] = $form->createElement(
  38. 'radio',
  39. 'start_date_type',
  40. null,
  41. get_lang('Start date taken from an attendance date'),
  42. '1',
  43. [
  44. 'onclick' => 'check_per_attendance(this)',
  45. 'id' => 'from_attendance',
  46. ]
  47. );
  48. $radios[] = $form->createElement(
  49. 'radio',
  50. 'start_date_type',
  51. null,
  52. get_lang('Custom start date'),
  53. '2',
  54. [
  55. 'onclick' => 'check_per_custom_date(this)',
  56. 'id' => 'custom_date',
  57. ]
  58. );
  59. $form->addGroup($radios, null, get_lang('Start date options'));
  60. if (isset($thematic_advance_data['attendance_id']) &&
  61. $thematic_advance_data['attendance_id'] == 0) {
  62. $form->addElement('html', '<div id="div_custom_datetime" style="display:block">');
  63. } else {
  64. $form->addElement('html', '<div id="div_custom_datetime" style="display:none">');
  65. }
  66. $form->addElement('DateTimePicker', 'custom_start_date', get_lang('Start Date'));
  67. $form->addElement('html', '</div>');
  68. if (isset($thematic_advance_data['attendance_id']) &&
  69. $thematic_advance_data['attendance_id'] == 0
  70. ) {
  71. $form->addElement('html', '<div id="div_datetime_by_attendance" style="display:none">');
  72. } else {
  73. $form->addElement('html', '<div id="div_datetime_by_attendance" style="display:block">');
  74. }
  75. if (count($attendance_select) > 1) {
  76. $form->addElement(
  77. 'select',
  78. 'attendance_select',
  79. get_lang('Attendances'),
  80. $attendance_select,
  81. ['id' => 'id_attendance_select', 'onchange' => 'datetime_by_attendance(this.value)']
  82. );
  83. } else {
  84. $form->addElement(
  85. 'label',
  86. get_lang('Attendances'),
  87. '<strong><em>'.get_lang('There is no attendance sheet in this course').'</em></strong>'
  88. );
  89. }
  90. $form->addElement('html', '<div id="div_datetime_attendance">');
  91. if (!empty($calendar_select)) {
  92. $form->addElement(
  93. 'select',
  94. 'start_date_by_attendance',
  95. get_lang('Start Date'),
  96. $calendar_select,
  97. ['id' => 'start_date_select_calendar']
  98. );
  99. }
  100. $form->addElement('html', '</div>');
  101. $form->addElement('html', '</div>');
  102. $form->addText(
  103. 'duration_in_hours',
  104. get_lang('Duration in hours'),
  105. false,
  106. [
  107. 'size' => '3',
  108. 'id' => 'duration_in_hours_element',
  109. 'autofocus' => 'autofocus',
  110. ]
  111. );
  112. $form->addHtmlEditor(
  113. 'content',
  114. get_lang('Content'),
  115. false,
  116. false,
  117. [
  118. 'ToolbarStartExpanded' => 'false',
  119. 'ToolbarSet' => 'Basic',
  120. 'Height' => '150',
  121. ]
  122. );
  123. if ($action == 'thematic_advance_add') {
  124. $form->addButtonSave(get_lang('Save'));
  125. } else {
  126. $form->addButtonUpdate(get_lang('Save'));
  127. }
  128. $attendance_select_item_id = null;
  129. if (count($attendance_select) > 1) {
  130. $i = 1;
  131. foreach ($attendance_select as $key => $attendance_select_item) {
  132. if ($i == 2) {
  133. $attendance_select_item_id = $key;
  134. break;
  135. }
  136. $i++;
  137. }
  138. if (!empty($attendance_select_item_id)) {
  139. $default['attendance_select'] = $attendance_select_item_id;
  140. if ($thematic_advance_id) {
  141. echo '<script> datetime_by_attendance("'.$attendance_select_item_id.'", "'.$thematic_advance_id.'"); </script>';
  142. } else {
  143. echo '<script> datetime_by_attendance("'.$attendance_select_item_id.'", 0); </script>';
  144. }
  145. }
  146. }
  147. $default['start_date_type'] = 1;
  148. $default['custom_start_date'] = date('Y-m-d H:i:s', api_strtotime(api_get_local_time()));
  149. $default['duration_in_hours'] = 1;
  150. if (!empty($thematic_advance_data)) {
  151. // set default values
  152. $default['content'] = isset($thematic_advance_data['content']) ? $thematic_advance_data['content'] : null;
  153. $default['duration_in_hours'] = isset($thematic_advance_data['duration']) ? $thematic_advance_data['duration'] : 1;
  154. if (empty($thematic_advance_data['attendance_id'])) {
  155. $default['start_date_type'] = 1;
  156. $default['custom_start_date'] = null;
  157. if (isset($thematic_advance_data['start_date'])) {
  158. $default['custom_start_date'] = date(
  159. 'Y-m-d H:i:s',
  160. api_strtotime(api_get_local_time($thematic_advance_data['start_date']))
  161. );
  162. }
  163. } else {
  164. $default['start_date_type'] = 1;
  165. if (!empty($thematic_advance_data['start_date'])) {
  166. $default['start_date_by_attendance'] = api_get_local_time($thematic_advance_data['start_date']);
  167. }
  168. $default['attendance_select'] = $thematic_advance_data['attendance_id'];
  169. }
  170. }
  171. $form->setDefaults($default);
  172. if ($form->validate()) {
  173. $values = $form->exportValues();
  174. if (isset($_POST['start_date_by_attendance'])) {
  175. $values['start_date_by_attendance'] = $_POST['start_date_by_attendance'];
  176. }
  177. $thematic = new Thematic();
  178. $thematic->set_thematic_advance_attributes(
  179. isset($values['thematic_advance_id']) ? $values['thematic_advance_id'] : null,
  180. $values['thematic_id'],
  181. $values['start_date_type'] == 1 && isset($values['attendance_select']) ? $values['attendance_select'] : 0,
  182. $values['content'],
  183. $values['start_date_type'] == 2 ? $values['custom_start_date'] : $values['start_date_by_attendance'],
  184. $values['duration_in_hours']
  185. );
  186. $affected_rows = $thematic->thematic_advance_save();
  187. if ($affected_rows) {
  188. // get last done thematic advance before move thematic list
  189. $last_done_thematic_advance = $thematic->get_last_done_thematic_advance();
  190. // update done advances with de current thematic list
  191. if (!empty($last_done_thematic_advance)) {
  192. $thematic->update_done_thematic_advances($last_done_thematic_advance);
  193. }
  194. }
  195. $redirectUrlParams = 'course_progress/index.php?'.api_get_cidreq().'&'.
  196. http_build_query([
  197. 'action' => 'thematic_advance_list',
  198. 'thematic_id' => $values['thematic_id'],
  199. ]);
  200. Display::addFlash(Display::return_message(get_lang('Update successful')));
  201. header('Location: '.api_get_path(WEB_CODE_PATH).$redirectUrlParams);
  202. exit;
  203. }
  204. $formLayout = $form->returnForm();
  205. } elseif ($action == 'thematic_advance_list') {
  206. // thematic advance list
  207. echo '<div class="actions">';
  208. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=thematic_details">'.
  209. Display::return_icon('back.png', get_lang("Back to"), '', ICON_SIZE_MEDIUM).'</a>';
  210. if (api_is_allowed_to_edit(false, true)) {
  211. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=thematic_advance_add&thematic_id='.$thematic_id.'"> '.
  212. Display::return_icon('add.png', get_lang('New thematic advance'), '', ICON_SIZE_MEDIUM).'</a>';
  213. }
  214. echo '</div>';
  215. $table = new SortableTable(
  216. 'thematic_advance_list',
  217. ['Thematic', 'get_number_of_thematic_advances'],
  218. ['Thematic', 'get_thematic_advance_data']
  219. );
  220. //$table->set_additional_parameters($parameters);
  221. $table->set_header(0, '', false, ['style' => 'width:20px;']);
  222. $table->set_header(1, get_lang('Start Date'), false);
  223. $table->set_header(2, get_lang('Duration in hours'), false, ['style' => 'width:80px;']);
  224. $table->set_header(3, get_lang('Content'), false);
  225. if (api_is_allowed_to_edit(null, true)) {
  226. $table->set_header(
  227. 4,
  228. get_lang('Detail'),
  229. false,
  230. ['style' => 'text-align:center']
  231. );
  232. }
  233. $table->display();
  234. }
  235. $tpl->assign('form_thematic', $formLayout);
  236. $thematicLayout = $tpl->get_template('course_progress/thematic_advance.html.twig');
  237. $content = $tpl->fetch($thematicLayout);
  238. $tpl->assign('content', $content);
  239. $tpl->display_one_col_template();