extra_field_workflow.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraFieldOptionRelFieldOption;
  4. /**
  5. * @package chamilo.admin
  6. */
  7. $cidReset = true;
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. $this_section = SECTION_PLATFORM_ADMIN;
  10. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  11. api_protect_admin_script();
  12. // setting breadcrumbs
  13. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  14. $tool_name = null;
  15. $action = isset($_GET['action']) ? $_GET['action'] : null;
  16. $field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null;
  17. if (empty($field_id)) {
  18. api_not_allowed();
  19. }
  20. if (!in_array($type, ExtraField::getValidExtraFieldTypes())) {
  21. api_not_allowed();
  22. }
  23. $extraField = new ExtraField($type);
  24. $extraFieldInfo = $extraField->get($field_id);
  25. $check = Security::check_token('request');
  26. $token = Security::get_token();
  27. if ($action == 'add') {
  28. $interbreadcrumb[] = ['url' => 'extra_fields.php?type='.$extraField->type, 'name' => $extraField->pageName];
  29. $interbreadcrumb[] = [
  30. 'url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],
  31. 'name' => $extraFieldInfo['display_text'],
  32. ];
  33. $interbreadcrumb[] = [
  34. 'url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'],
  35. 'name' => get_lang('EditExtraFieldOptions'),
  36. ];
  37. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add')];
  38. } elseif ($action == 'edit') {
  39. $interbreadcrumb[] = [
  40. 'url' => 'extra_fields.php?type='.$extraField->type,
  41. 'name' => $extraField->pageName,
  42. ];
  43. $interbreadcrumb[] = [
  44. 'url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],
  45. 'name' => $extraFieldInfo['display_text'],
  46. ];
  47. $interbreadcrumb[] = [
  48. 'url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'],
  49. 'name' => get_lang('EditExtraFieldOptions'),
  50. ];
  51. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Edit')];
  52. } else {
  53. $interbreadcrumb[] = [
  54. 'url' => 'extra_fields.php?type='.$extraField->type,
  55. 'name' => $extraField->pageName,
  56. ];
  57. $interbreadcrumb[] = [
  58. 'url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],
  59. 'name' => $extraFieldInfo['display_text'],
  60. ];
  61. $interbreadcrumb[] = [
  62. 'url' => '#',
  63. 'name' => get_lang('EditExtraFieldOptions'),
  64. ];
  65. }
  66. $roleId = isset($_REQUEST['roleId']) ? $_REQUEST['roleId'] : null;
  67. //jqgrid will use this URL to do the selects
  68. $params = 'field_id='.$field_id.'&type='.$extraField->type.'&roleId='.$roleId;
  69. $paramsNoRole = 'field_id='.$field_id.'&type='.$extraField->type;
  70. // The order is important you need to check the the $column variable in the model.ajax.php file
  71. $columns = [get_lang('Name'), get_lang('Value'), get_lang('Order'), get_lang('Actions')];
  72. $htmlHeadXtra[] = '<script>
  73. function setHidden(obj) {
  74. var name = $(obj).attr("name");
  75. var hiddenName = "hidden_" + name;
  76. if ($("#" + hiddenName).attr("value") == 1) {
  77. $("#" + hiddenName).attr("value", 0);
  78. } else {
  79. $("#" + hiddenName).attr("value", 1);
  80. }
  81. }
  82. $(function() {
  83. $("#workflow_status").on("change", function() {
  84. var roleId = $(this).find(":selected").val();
  85. if (roleId != 0) {
  86. window.location.replace("'.api_get_self().'?'.$paramsNoRole.'&roleId="+roleId);
  87. }
  88. });
  89. $("[name=select_all]").on("click", function() {
  90. $("#workflow :checkbox").prop("checked", 1);
  91. $("#workflow :hidden").prop("value", 1);
  92. return false;
  93. });
  94. $("[name=unselect_all]").on("click", function() {
  95. $("#workflow :checkbox").prop("checked", 0);
  96. $("#workflow :hidden").prop("value", 0);
  97. return false;
  98. });
  99. });
  100. </script>';
  101. $obj = new ExtraFieldOption($type);
  102. $columns = ['display_text', 'option_value', 'option_order'];
  103. $result = Database::select(
  104. '*',
  105. $obj->table,
  106. [
  107. 'where' => ['field_id = ? ' => $field_id],
  108. 'order' => 'option_order ASC',
  109. ]
  110. );
  111. $table = new HTML_Table(['class' => 'data_table']);
  112. $column = 0;
  113. $row = 0;
  114. $table->setHeaderContents($row, $column, get_lang('CurrentStatus'));
  115. $column++;
  116. foreach ($result as $item) {
  117. $table->setHeaderContents($row, $column, $item['display_text']);
  118. $column++;
  119. }
  120. $row++;
  121. $form = new FormValidator('workflow', 'post', api_get_self().'?'.$params);
  122. //$options = api_get_user_roles();
  123. $options[0] = get_lang('SelectAnOption');
  124. $options[STUDENT] = get_lang('Student');
  125. $options[COURSEMANAGER] = get_lang('Teacher');
  126. ksort($options);
  127. $form->addElement('select', 'status', get_lang('SelectRole'), $options);
  128. $em = Database::getManager();
  129. $repo = $em->getRepository('ChamiloCoreBundle:ExtraFieldOptionRelFieldOption');
  130. $checks = $repo->findBy(
  131. ['fieldId' => $field_id, 'roleId' => $roleId]
  132. );
  133. $includedFields = [];
  134. if (!empty($checks)) {
  135. foreach ($checks as $availableField) {
  136. $includedFields[$availableField->getFieldOptionId()][] = $availableField->getRelatedFieldOptionId();
  137. }
  138. }
  139. foreach ($result as $item) {
  140. $column = 0;
  141. $table->setCellContents($row, $column, $item['display_text']);
  142. $column++;
  143. $value = null;
  144. foreach ($result as $itemCol) {
  145. $id = 'extra_field_status_'.$item['id'].'_'.$itemCol['id'];
  146. $idForm = 'extra_field_status['.$item['id'].']['.$itemCol['id'].']';
  147. $attributes = ['onclick' => 'setHidden(this)'];
  148. $value = 0;
  149. if (isset($includedFields[$itemCol['id']]) && in_array($item['id'], $includedFields[$itemCol['id']])) {
  150. $value = 1;
  151. $attributes['checked'] = 'checked';
  152. }
  153. $element = Display::input('checkbox', $id, null, $attributes);
  154. $table->setCellContents($row, $column, $element);
  155. $form->addElement('hidden', 'hidden_'.$idForm, $value, ['id' => 'hidden_'.$id]);
  156. $column++;
  157. }
  158. $row++;
  159. }
  160. if (!empty($roleId)) {
  161. $form->addElement('html', $table->toHtml());
  162. $group = [];
  163. $group[] = $form->addButtonSave(get_lang('Save'), 'submit', true);
  164. $group[] = $form->addButton(
  165. 'select_all',
  166. get_lang('SelectAll'),
  167. 'check',
  168. 'default',
  169. 'default',
  170. null,
  171. [],
  172. true
  173. );
  174. $group[] = $form->addButton(
  175. 'unselect_all',
  176. get_lang('UnSelectAll'),
  177. 'check',
  178. 'default',
  179. 'default',
  180. null,
  181. [],
  182. true
  183. );
  184. $form->addGroup($group, '', null, ' ');
  185. $form->setDefaults(['status' => $roleId]);
  186. } else {
  187. $form->addButtonUpdate(get_lang('Edit'));
  188. }
  189. if ($form->validate()) {
  190. $values = $form->getSubmitValues();
  191. $result = $values['hidden_extra_field_status'];
  192. if (!empty($result)) {
  193. foreach ($result as $id => $items) {
  194. foreach ($items as $subItemId => $value) {
  195. $extraFieldOptionRelFieldOption = $repo->findOneBy(
  196. [
  197. 'fieldId' => $field_id,
  198. 'fieldOptionId' => $subItemId,
  199. 'roleId' => $roleId,
  200. 'relatedFieldOptionId' => $id,
  201. ]
  202. );
  203. if ($value == 1) {
  204. if (empty($extraFieldOptionRelFieldOption)) {
  205. $extraFieldOptionRelFieldOption = new ExtraFieldOptionRelFieldOption();
  206. $extraFieldOptionRelFieldOption
  207. ->setFieldId($field_id)
  208. ->setFieldOptionId($subItemId)
  209. ->setRelatedFieldOptionId($id)
  210. ->setRoleId($roleId)
  211. ;
  212. $em->persist($extraFieldOptionRelFieldOption);
  213. }
  214. } else {
  215. if ($extraFieldOptionRelFieldOption) {
  216. $em->remove($extraFieldOptionRelFieldOption);
  217. }
  218. }
  219. }
  220. }
  221. $em->flush();
  222. Display::addFlash(Display::return_message(get_lang('Updated')));
  223. header('Location:'.api_get_self().'?'.$params);
  224. exit;
  225. }
  226. }
  227. Display::display_header($tool_name);
  228. echo Display::page_header($extraFieldInfo['display_text']);
  229. $form->display();
  230. Display::display_footer();