extra_field_workflow.php 8.3 KB

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