ImsLtiPlugin.php 4.7 KB

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