extra_field_workflow.php 8.1 KB

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