map.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.social
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. api_block_anonymous_users();
  11. $fields = api_get_configuration_value('allow_social_map_fields');
  12. if (!$fields) {
  13. api_not_allowed(true);
  14. }
  15. $fields = isset($fields['fields']) ? $fields['fields'] : '';
  16. if (empty($fields)) {
  17. api_not_allowed(true);
  18. }
  19. $extraField = new ExtraField('user');
  20. $infoStage = $extraField->get_handler_field_info_by_field_variable($fields['0']);
  21. $infoVille = $extraField->get_handler_field_info_by_field_variable($fields['1']);
  22. if (empty($infoStage) || empty($infoVille)) {
  23. api_not_allowed(true);
  24. }
  25. $gMapsPlugin = GoogleMapsPlugin::create();
  26. $localization = $gMapsPlugin->get('enable_api') === 'true';
  27. if ($localization) {
  28. $apiKey = $gMapsPlugin->get('api_key');
  29. if (empty($apiKey)) {
  30. api_not_allowed(true);
  31. }
  32. } else {
  33. api_not_allowed(true);
  34. }
  35. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  36. $sql = "SELECT u.id, firstname, lastname, ev.value ville, ev2.value stage
  37. FROM $tableUser u
  38. INNER JOIN extra_field_values ev
  39. ON ev.item_id = u.id
  40. INNER JOIN extra_field_values ev2
  41. ON ev2.item_id = u.id
  42. WHERE
  43. ev.field_id = ".$infoStage['id']." AND
  44. ev2.field_id = ".$infoVille['id']." AND
  45. u.status = ".STUDENT." AND
  46. u.active = 1 AND
  47. (ev.value <> '' OR ev2.value <> '') AND
  48. (ev.value LIKE '%::%' OR ev2.value LIKE '%::%')
  49. ";
  50. $cacheDriver = new \Doctrine\Common\Cache\ApcuCache();
  51. $keyDate = 'map_cache_date';
  52. $keyData = 'map_cache_data';
  53. $now = time();
  54. // Refresh cache every day
  55. //$tomorrow = strtotime('+1 day', $now);
  56. $tomorrow = strtotime('+5 minute', $now);
  57. $loadFromDatabase = true;
  58. if ($cacheDriver->contains($keyData) && $cacheDriver->contains($keyDate)) {
  59. $savedDate = $cacheDriver->fetch($keyDate);
  60. $loadFromDatabase = false;
  61. if ($savedDate < $now) {
  62. $loadFromDatabase = true;
  63. }
  64. }
  65. $loadFromDatabase = true;
  66. if ($loadFromDatabase) {
  67. $result = Database::query($sql);
  68. $data = Database::store_result($result, 'ASSOC');
  69. $cacheDriver->save($keyData, $data);
  70. $cacheDriver->save($keyDate, $tomorrow);
  71. } else {
  72. $data = $cacheDriver->fetch($keyData);
  73. }
  74. foreach ($data as &$result) {
  75. // Clean process is made in twig with escape('js')
  76. $result['complete_name'] = $result['firstname'].' '.$result['lastname'];
  77. $result['lastname'] = '';
  78. $result['firstname'] = '';
  79. $parts = explode('::', $result['ville']);
  80. if (isset($parts[1]) && !empty($parts[1])) {
  81. $parts2 = explode(',', $parts[1]);
  82. $result['ville_lat'] = $parts2[0];
  83. $result['ville_long'] = $parts2[1];
  84. unset($result['ville']);
  85. }
  86. $parts = explode('::', $result['stage']);
  87. if (isset($parts[1]) && !empty($parts[1])) {
  88. $parts2 = explode(',', $parts[1]);
  89. $result['stage_lat'] = $parts2[0];
  90. $result['stage_long'] = $parts2[1];
  91. unset($result['stage']);
  92. }
  93. }
  94. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_JS_PATH).'map/markerclusterer.js"></script>';
  95. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_JS_PATH).'map/oms.min.js"></script>';
  96. $tpl = new Template(null);
  97. $tpl->assign('url', api_get_path(WEB_CODE_PATH).'social/profile.php');
  98. $tpl->assign(
  99. 'image_city',
  100. Display::return_icon(
  101. 'red-dot.png',
  102. '',
  103. [],
  104. ICON_SIZE_SMALL,
  105. false,
  106. true
  107. )
  108. );
  109. $tpl->assign(
  110. 'image_stage',
  111. Display::return_icon(
  112. 'blue-dot.png',
  113. '',
  114. [],
  115. ICON_SIZE_SMALL,
  116. false,
  117. true
  118. )
  119. );
  120. $tpl->assign('places', json_encode($data));
  121. $tpl->assign('api_key', $apiKey);
  122. $tpl->assign('field_1', $infoStage['display_text']);
  123. $tpl->assign('field_2', $infoVille['display_text']);
  124. $layout = $tpl->get_template('social/map.tpl');
  125. $tpl->display($layout);