extra_field_workflow.php 8.1 KB

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