extra_field_workflow.php 8.1 KB

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