ImsLtiPlugin.php 4.8 KB

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