thematic_advance.php 8.9 KB

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