ImsLtiPlugin.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /* For license terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CTool;
  4. use Chamilo\CoreBundle\Entity\Course;
  5. /**
  6. * Description of MsiLti
  7. *
  8. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  9. */
  10. class ImsLtiPlugin extends Plugin
  11. {
  12. const TABLE_TOOL = 'plugin_msi_lti_tool';
  13. /**
  14. * Class cronstructor
  15. */
  16. protected function __construct()
  17. {
  18. $version = '1.0';
  19. $author = 'Angel Fernando Quiroz Campos';
  20. parent::__construct($version, $author, ['enabled' => 'boolean']);
  21. $this->setCourseSettings();
  22. }
  23. /**
  24. * Get the class instance
  25. * @staticvar MsiLtiPlugin $result
  26. * @return MsiLtiPlugin
  27. */
  28. public static function create()
  29. {
  30. static $result = null;
  31. return $result ?: $result = new self();
  32. }
  33. /**
  34. * Get the plugin directory name
  35. */
  36. public function get_name()
  37. {
  38. return 'ims_lti';
  39. }
  40. /**
  41. * Install the plugin. Setup the database
  42. */
  43. public function install()
  44. {
  45. $this->setupDatabase();
  46. }
  47. /**
  48. * Unistall plugin. Clear the database
  49. */
  50. public function uninstall()
  51. {
  52. $this->clearDatabase();
  53. }
  54. /**
  55. * Creates the plugin tables on database
  56. * @return boolean
  57. */
  58. private function setupDatabase()
  59. {
  60. $entityManager = Database::getManager();
  61. $connection = $entityManager->getConnection();
  62. $chamiloSchema = $connection->getSchemaManager();
  63. $pluginSchema = new \Doctrine\DBAL\Schema\Schema();
  64. $platform = $connection->getDatabasePlatform();
  65. if ($chamiloSchema->tablesExist([self::TABLE_TOOL])) {
  66. return false;
  67. }
  68. $toolTable = $pluginSchema->createTable(self::TABLE_TOOL);
  69. $toolTable->addColumn(
  70. 'id',
  71. \Doctrine\DBAL\Types\Type::INTEGER,
  72. ['autoincrement' => true, 'unsigned' => true]
  73. );
  74. $toolTable->addColumn('name', \Doctrine\DBAL\Types\Type::STRING);
  75. $toolTable->addColumn('description', \Doctrine\DBAL\Types\Type::TEXT, ['notnull' => false]);
  76. $toolTable->addColumn('launch_url', \Doctrine\DBAL\Types\Type::TEXT);
  77. $toolTable->addColumn('consumer_key', \Doctrine\DBAL\Types\Type::STRING);
  78. $toolTable->addColumn('shared_secret', \Doctrine\DBAL\Types\Type::STRING);
  79. $toolTable->addColumn('custom_params', \Doctrine\DBAL\Types\Type::TEXT);
  80. $toolTable->setPrimaryKey(['id']);
  81. $queries = $pluginSchema->toSql($platform);
  82. foreach ($queries as $query) {
  83. Database::query($query);
  84. }
  85. return true;
  86. }
  87. /**
  88. * Drops the plugin tables on database
  89. * @return boolean
  90. */
  91. private function clearDatabase()
  92. {
  93. $entityManager = Database::getManager();
  94. $connection = $entityManager->getConnection();
  95. $chamiloSchema = $connection->getSchemaManager();
  96. if (!$chamiloSchema->tablesExist([self::TABLE_TOOL])) {
  97. return false;
  98. }
  99. $sql = 'DROP TABLE IF EXISTS '.self::TABLE_TOOL;
  100. Database::query($sql);
  101. return true;
  102. }
  103. /**
  104. * Set the course settings
  105. */
  106. private function setCourseSettings()
  107. {
  108. $button = Display::toolbarButton(
  109. $this->get_lang('AddExternalTool'),
  110. api_get_path(WEB_PLUGIN_PATH).'ims_lti/add.php',
  111. 'cog',
  112. 'primary'
  113. );
  114. $this->course_settings = [
  115. [
  116. 'name' => $this->get_lang('ImsLtiDescription').$button.'<hr>',
  117. 'type' => 'html'
  118. ]
  119. ];
  120. }
  121. /**
  122. * Add the course tool
  123. * @param Course $course
  124. * @param ImsLtiTool $tool
  125. */
  126. public function addCourseTool(Course $course, ImsLtiTool $tool)
  127. {
  128. $em = Database::getManager();
  129. $cToolId = AddCourse::generateToolId($course->getId());
  130. $cTool = new CTool();
  131. $cTool
  132. ->setId($cToolId)
  133. ->setCId($course->getId())
  134. ->setName($tool->getName())
  135. ->setLink($this->get_name().'/start.php?'.http_build_query(['id' => $tool->getId()]))
  136. ->setImage($this->get_name().'.png')
  137. ->setVisibility(1)
  138. ->setAdmin(0)
  139. ->setAddress('squaregray.gif')
  140. ->setAddedTool('NO')
  141. ->setTarget('_self')
  142. ->setCategory('plugin')
  143. ->setSessionId(0);
  144. $em->persist($cTool);
  145. $em->flush();
  146. }
  147. /**
  148. * @return string
  149. */
  150. protected function getConfigExtraText()
  151. {
  152. $text = $this->get_lang('ImsLtiDescription');
  153. $text .= sprintf(
  154. $this->get_lang('ManageToolButton'),
  155. api_get_path(WEB_PLUGIN_PATH).'ims_lti/list.php'
  156. );
  157. return $text;
  158. }
  159. }