tour_plugin.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * The Tour class allows a guided tour in HTML5 of the Chamilo interface
  5. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  6. * @package chamilo.plugin.tour
  7. */
  8. class Tour extends Plugin
  9. {
  10. /**
  11. * Class constructor
  12. */
  13. protected function __construct()
  14. {
  15. $parameters = array(
  16. 'show_tour' => 'boolean',
  17. 'theme' => 'text'
  18. );
  19. parent::__construct('1.0', 'Angel Fernando Quiroz Campos', $parameters);
  20. }
  21. /**
  22. * Instance the plugin
  23. * @staticvar null $result
  24. * @return Tour
  25. */
  26. static function create()
  27. {
  28. static $result = null;
  29. return $result ? $result : $result = new self();
  30. }
  31. /**
  32. * Install the plugin
  33. * @return void
  34. */
  35. public function install()
  36. {
  37. $this->installDatabase();
  38. }
  39. /**
  40. * Uninstall the plugin
  41. * @return void
  42. */
  43. public function uninstall()
  44. {
  45. $this->unistallDatabase();
  46. }
  47. /**
  48. * Create the database tables for the plugin
  49. * @return void
  50. */
  51. private function installDatabase()
  52. {
  53. $pluginTourLogTable = Database::get_main_table(TABLE_TOUR_LOG);
  54. $sql = "CREATE TABLE IF NOT EXISTS $pluginTourLogTable ("
  55. . "id int UNSIGNED NOT NULL AUTO_INCREMENT, "
  56. . "page_class varchar(255) NOT NULL, "
  57. . "user_id int UNSIGNED NOT NULL, "
  58. . "visualization_datetime datetime NOT NULL, "
  59. . "PRIMARY KEY PK_tour_log (id))";
  60. Database::query($sql);
  61. }
  62. /**
  63. * Drop the database tables for the plugin
  64. * @return void
  65. */
  66. private function unistallDatabase()
  67. {
  68. $pluginTourLogTable = Database::get_main_table(TABLE_TOUR_LOG);
  69. $sql = "DROP TABLE IF EXISTS $pluginTourLogTable";
  70. Database::query($sql);
  71. }
  72. /**
  73. * Check whether the tour should be displayed to the user
  74. * @param string $currentPageClass The class of the current page
  75. * @param int $userId The user id
  76. * @return boolean If the user has seen the tour return false, otherwise return true
  77. */
  78. public function checkTourForUser($currentPageClass, $userId)
  79. {
  80. $pluginTourLogTable = Database::get_main_table(TABLE_TOUR_LOG);
  81. $checkResult = Database::select('count(1) as qty', $pluginTourLogTable, array(
  82. 'where' => array(
  83. "page_class = '?' AND " => $currentPageClass,
  84. "user_id = ?" => intval($userId)
  85. )), 'first');
  86. if ($checkResult !== false) {
  87. if ($checkResult['qty'] > 0) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. /**
  94. * Set the tour as seen
  95. * @param string $currentPageClass The class of the current page
  96. * @param int $userId The user id
  97. * @return void
  98. */
  99. public function saveCompletedTour($currentPageClass, $userId)
  100. {
  101. $pluginTourLogTable = Database::get_main_table(TABLE_TOUR_LOG);
  102. Database::insert($pluginTourLogTable, array(
  103. 'page_class' => $currentPageClass,
  104. 'user_id' => intval($userId),
  105. 'visualization_datetime' => api_get_utc_datetime()
  106. ));
  107. }
  108. /**
  109. * Get the configuration to show the tour in pages
  110. * @return array The config data
  111. */
  112. public function getTourConfig()
  113. {
  114. $pluginPath = api_get_path(PLUGIN_PATH) . 'tour/';
  115. $jsonContent = file_get_contents($pluginPath . 'config/tour.json');
  116. $jsonData = json_decode($jsonContent, true);
  117. return $jsonData;
  118. }
  119. }