extra_field_workflow.php 8.2 KB

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