extra_field_workflow.php 8.1 KB

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