extra_field.ajax.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\Tag;
  4. require_once __DIR__.'/../global.inc.php';
  5. $action = isset($_GET['a']) ? $_GET['a'] : '';
  6. switch ($action) {
  7. case 'get_second_select_options':
  8. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  9. $field_id = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : null;
  10. $option_value_id = isset($_REQUEST['option_value_id']) ? $_REQUEST['option_value_id'] : null;
  11. if (!empty($type) && !empty($field_id) && !empty($option_value_id)) {
  12. $field_options = new ExtraFieldOption($type);
  13. echo $field_options->get_second_select_field_options_by_field(
  14. $option_value_id,
  15. true
  16. );
  17. }
  18. break;
  19. case 'search_tags':
  20. header('Content-Type: application/json');
  21. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  22. $fieldId = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : null;
  23. $tag = isset($_REQUEST['q']) ? $_REQUEST['q'] : null;
  24. $result = [];
  25. if (empty($tag)) {
  26. echo json_encode(['items' => $result]);
  27. exit;
  28. }
  29. $extraFieldOption = new ExtraFieldOption($type);
  30. $tags = Database::getManager()
  31. ->getRepository('ChamiloCoreBundle:Tag')
  32. ->createQueryBuilder('t')
  33. ->where("t.tag LIKE :tag")
  34. ->andWhere('t.fieldId = :field')
  35. ->setParameter('field', $fieldId)
  36. ->setParameter('tag', "$tag%")
  37. ->getQuery()
  38. ->getResult();
  39. /** @var Tag $tag */
  40. foreach ($tags as $tag) {
  41. $result[] = [
  42. 'id' => $tag->getTag(),
  43. 'text' => $tag->getTag()
  44. ];
  45. }
  46. echo json_encode(['items' => $result]);
  47. break;
  48. case 'search_options_from_tags':
  49. $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
  50. $fieldId = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : null;
  51. $tag = isset($_REQUEST['tag']) ? $_REQUEST['tag'] : null;
  52. $extraFieldOption = new ExtraFieldOption($type);
  53. $from = isset($_REQUEST['from']) ? $_REQUEST['from'] : '';
  54. $search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
  55. $options = isset($_REQUEST['options']) ? json_decode($_REQUEST['options']) : '';
  56. $extraField = new ExtraField('session');
  57. $result = $extraField->searchOptionsFromTags($from, $search, $options);
  58. $options = [];
  59. $groups = [];
  60. foreach ($result as $data) {
  61. $groups[$data['display_text']][] = [
  62. 'id' => $data['id'],
  63. 'text' => $data['tag']
  64. ];
  65. }
  66. foreach ($groups as $key => $data) {
  67. $options[] = [
  68. 'text' => $key,
  69. 'children' => $groups[$key]
  70. ];
  71. }
  72. echo json_encode($options);
  73. break;
  74. case 'order':
  75. $variable = isset($_REQUEST['field_variable']) ? $_REQUEST['field_variable'] : '';
  76. $save = isset($_REQUEST['save']) ? $_REQUEST['save'] : '';
  77. $values = isset($_REQUEST['values']) ? json_decode($_REQUEST['values']) : '';
  78. $extraField = new ExtraField('session');
  79. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(str_replace('extra_', '', $variable));
  80. $em = Database::getManager();
  81. $search = [
  82. 'user' => api_get_user_id(),
  83. 'field' => $extraFieldInfo['id']
  84. ];
  85. $extraFieldSavedSearch = $em->getRepository('ChamiloCoreBundle:ExtraFieldSavedSearch')->findOneBy($search);
  86. if ($save) {
  87. $extraField = new \Chamilo\CoreBundle\Entity\ExtraFieldSavedSearch('session');
  88. if ($extraFieldSavedSearch) {
  89. $extraFieldSavedSearch->setValue($values);
  90. $em->merge($extraFieldSavedSearch);
  91. $em->flush();
  92. }
  93. }
  94. if ($extraFieldInfo) {
  95. /** @var \Chamilo\CoreBundle\Entity\ExtraFieldSavedSearch $options */
  96. $extraFieldSavedSearch = $em->getRepository('ChamiloCoreBundle:ExtraFieldSavedSearch')->findOneBy($search);
  97. $values = $extraFieldSavedSearch->getValue();
  98. $url = api_get_self().'?a=order&save=1&field_variable='.$variable;
  99. $html = '
  100. <script>
  101. $(function() {
  102. $( "#sortable" ).sortable();
  103. $( "#sortable" ).disableSelection();
  104. $( "#link_'.$variable.'" ).on("click", function() {
  105. var newList = [];
  106. $("#sortable").find("li").each(function(){
  107. newList.push($(this).text());
  108. });
  109. var save = JSON.stringify(newList);
  110. $.ajax({
  111. url: "'.$url.'",
  112. dataType: "json",
  113. data: "values="+save,
  114. success: function(data) {
  115. console.log(data);
  116. }
  117. });
  118. alert("'.get_lang('Saved').'");
  119. location.reload();
  120. return false;
  121. });
  122. });
  123. </script>';
  124. $html .= '<ul id="sortable">';
  125. foreach ($values as $value) {
  126. $html .= '<li class="ui-state-default">';
  127. $html .= $value;
  128. $html .= '</li>';
  129. }
  130. $html .= '</ul>';
  131. $html .= Display::url(get_lang('Save'), '#', ['id' => 'link_'.$variable, 'class' => 'btn btn-primary']);
  132. echo $html;
  133. }
  134. break;
  135. default:
  136. exit;
  137. break;
  138. }
  139. exit;