map.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. $result['complete_name'] = addslashes(api_get_person_name($result['firstname'], $result['lastname']));
  76. $parts = explode('::', $result['ville']);
  77. if (isset($parts[1]) && !empty($parts[1])) {
  78. $parts2 = explode(',', $parts[1]);
  79. $result['ville_lat'] = $parts2[0];
  80. $result['ville_long'] = $parts2[1];
  81. unset($result['ville']);
  82. }
  83. $parts = explode('::', $result['stage']);
  84. if (isset($parts[1]) && !empty($parts[1])) {
  85. $parts2 = explode(',', $parts[1]);
  86. $result['stage_lat'] = $parts2[0];
  87. $result['stage_long'] = $parts2[1];
  88. unset($result['stage']);
  89. }
  90. }
  91. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_JS_PATH).'map/markerclusterer.js"></script>';
  92. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_JS_PATH).'map/oms.min.js"></script>';
  93. $tpl = new Template(null);
  94. $tpl->assign('url', api_get_path(WEB_CODE_PATH).'social/profile.php');
  95. $tpl->assign(
  96. 'image_city',
  97. Display::return_icon(
  98. 'red-dot.png',
  99. '',
  100. [],
  101. ICON_SIZE_SMALL,
  102. false,
  103. true
  104. )
  105. );
  106. $tpl->assign(
  107. 'image_stage',
  108. Display::return_icon(
  109. 'blue-dot.png',
  110. '',
  111. [],
  112. ICON_SIZE_SMALL,
  113. false,
  114. true
  115. )
  116. );
  117. $tpl->assign('places', json_encode($data));
  118. $tpl->assign('api_key', $apiKey);
  119. $tpl->assign('field_1', $infoStage['display_text']);
  120. $tpl->assign('field_2', $infoVille['display_text']);
  121. $layout = $tpl->get_template('social/map.tpl');
  122. $tpl->display($layout);