extra_field.ajax.php 5.8 KB

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