extra_field_workflow.php 8.3 KB

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