123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\CoreBundle\Entity\Course;
- use Chamilo\CoreBundle\Entity\Session;
- use Chamilo\PluginBundle\Entity\EmbedRegistry\Embed;
- use Symfony\Component\Filesystem\Filesystem;
- /**
- * Class EmbedRegistryPlugin.
- */
- class EmbedRegistryPlugin extends Plugin
- {
- const SETTING_ENABLED = 'tool_enabled';
- const SETTING_TITLE = 'tool_title';
- const SETTING_EXTERNAL_URL = 'external_url';
- const TBL_EMBED = 'plugin_embed_registry_embed';
- /**
- * EmbedRegistryPlugin constructor.
- */
- protected function __construct()
- {
- $authors = [
- 'Angel Fernando Quiroz Campos',
- ];
- parent::__construct(
- '1.0',
- implode(', ', $authors),
- [
- self::SETTING_ENABLED => 'boolean',
- self::SETTING_TITLE => 'text',
- self::SETTING_EXTERNAL_URL => 'text',
- ]
- );
- }
- /**
- * @return string
- */
- public function getToolTitle()
- {
- $title = $this->get(self::SETTING_TITLE);
- if (!empty($title)) {
- return $title;
- }
- return $this->get_title();
- }
- /**
- * @return EmbedRegistryPlugin|null
- */
- public static function create()
- {
- static $result = null;
- return $result ? $result : $result = new self();
- }
- public function install()
- {
- $entityPath = $this->getEntityPath();
- if (!is_dir($entityPath)) {
- if (!is_writable(dirname($entityPath))) {
- Display::addFlash(
- Display::return_message(
- get_lang('ErrorCreatingDir').' '.$entityPath,
- 'error'
- )
- );
- return false;
- }
- mkdir($entityPath, api_get_permissions_for_new_directories());
- }
- $fs = new Filesystem();
- $fs->mirror(__DIR__.'/Entity/', $entityPath, null, ['override']);
- $this->createPluginTables();
- }
- public function uninstall()
- {
- $entityPath = $this->getEntityPath();
- $fs = new Filesystem();
- if ($fs->exists($entityPath)) {
- $fs->remove($entityPath);
- }
- Database::query('DROP TABLE IF EXISTS '.self::TBL_EMBED);
- }
- /**
- * @return EmbedRegistryPlugin
- */
- public function performActionsAfterConfigure()
- {
- $em = Database::getManager();
- $this->deleteCourseToolLinks();
- if ('true' === $this->get(self::SETTING_ENABLED)) {
- $courses = $em->createQuery('SELECT c.id FROM ChamiloCoreBundle:Course c')->getResult();
- foreach ($courses as $course) {
- $this->createLinkToCourseTool($this->getToolTitle(), $course['id']);
- }
- }
- return $this;
- }
- /**
- * @param int $courseId
- */
- public function doWhenDeletingCourse($courseId)
- {
- Database::getManager()
- ->createQuery('DELETE FROM ChamiloPluginBundle:EmbedRegistry\Embed e WHERE e.course = :course')
- ->execute(['course' => (int) $courseId]);
- }
- /**
- * @param int $sessionId
- */
- public function doWhenDeletingSession($sessionId)
- {
- Database::getManager()
- ->createQuery('DELETE FROM ChamiloPluginBundle:EmbedRegistry\Embed e WHERE e.session = :session')
- ->execute(['session' => (int) $sessionId]);
- }
- /**
- * @param Course $course
- * @param Session|null $session
- *
- * @throws \Doctrine\ORM\NonUniqueResultException
- *
- * @return Embed
- */
- public function getCurrentEmbed(Course $course, Session $session = null)
- {
- $embedRepo = Database::getManager()->getRepository('ChamiloPluginBundle:EmbedRegistry\Embed');
- $qb = $embedRepo->createQueryBuilder('e');
- $query = $qb
- ->where('e.displayStartDate <= :now')
- ->andWhere('e.displayEndDate >= :now')
- ->andWhere(
- $qb->expr()->eq('e.course', $course->getId())
- );
- $query->andWhere(
- $session
- ? $qb->expr()->eq('e.session', $session->getId())
- : $qb->expr()->isNull('e.session')
- );
- $query = $query
- ->orderBy('e.displayStartDate', 'DESC')
- ->setMaxResults(1)
- ->setParameters(['now' => api_get_utc_datetime(null, false, true)])
- ->getQuery();
- return $query->getOneOrNullResult();
- }
- /**
- * @param Embed $embed
- *
- * @return string
- */
- public function formatDisplayDate(Embed $embed)
- {
- $startDate = sprintf(
- '<time datetime="%s">%s</time>',
- $embed->getDisplayStartDate()->format(DateTime::W3C),
- api_convert_and_format_date($embed->getDisplayStartDate())
- );
- $endDate = sprintf(
- '<time datetime="%s">%s</time>',
- $embed->getDisplayEndDate()->format(DateTime::W3C),
- api_convert_and_format_date($embed->getDisplayEndDate())
- );
- return sprintf(get_lang('FromDateXToDateY'), $startDate, $endDate);
- }
- /**
- * @param Embed $embed
- *
- * @return string
- */
- public function getViewUrl(Embed $embed)
- {
- return api_get_path(WEB_PLUGIN_PATH).'embedregistry/view.php?id='.$embed->getId().'&'.api_get_cidreq();
- }
- /**
- * @param Embed $embed
- *
- * @throws \Doctrine\ORM\Query\QueryException
- *
- * @return int
- */
- public function getMembersCount(Embed $embed)
- {
- $dql = 'SELECT COUNT(DISTINCT tea.accessUserId) FROM ChamiloCoreBundle:TrackEAccess tea
- WHERE
- tea.accessTool = :tool AND
- (tea.accessDate >= :start_date AND tea.accessDate <= :end_date) AND
- tea.cId = :course';
- $params = [
- 'tool' => 'plugin_'.$this->get_name(),
- 'start_date' => $embed->getDisplayStartDate(),
- 'end_date' => $embed->getDisplayEndDate(),
- 'course' => $embed->getCourse(),
- ];
- if ($embed->getSession()) {
- $dql .= ' AND tea.accessSessionId = :session ';
- $params['session'] = $embed->getSession();
- }
- $count = Database::getManager()
- ->createQuery($dql)
- ->setParameters($params)
- ->getSingleScalarResult();
- return $count;
- }
- public function saveEventAccessTool()
- {
- $tableAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
- $params = [
- 'access_user_id' => api_get_user_id(),
- 'c_id' => api_get_course_int_id(),
- 'access_tool' => 'plugin_'.$this->get_name(),
- 'access_date' => api_get_utc_datetime(),
- 'access_session_id' => api_get_session_id(),
- 'user_ip' => api_get_real_ip(),
- ];
- Database::insert($tableAccess, $params);
- }
- private function createPluginTables()
- {
- $connection = Database::getManager()->getConnection();
- if ($connection->getSchemaManager()->tablesExist(self::TBL_EMBED)) {
- return;
- }
- $queries = [
- 'CREATE TABLE plugin_embed_registry_embed (id INT AUTO_INCREMENT NOT NULL, c_id INT NOT NULL, session_id INT DEFAULT NULL, title LONGTEXT NOT NULL, display_start_date DATETIME NOT NULL, display_end_date DATETIME NOT NULL, html_code LONGTEXT NOT NULL, INDEX IDX_5236D25991D79BD3 (c_id), INDEX IDX_5236D259613FECDF (session_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB',
- 'ALTER TABLE plugin_embed_registry_embed ADD CONSTRAINT FK_5236D25991D79BD3 FOREIGN KEY (c_id) REFERENCES course (id)',
- 'ALTER TABLE plugin_embed_registry_embed ADD CONSTRAINT FK_5236D259613FECDF FOREIGN KEY (session_id) REFERENCES session (id)',
- ];
- foreach ($queries as $query) {
- Database::query($query);
- }
- }
- /**
- * @return string
- */
- private function getEntityPath()
- {
- return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName();
- }
- private function deleteCourseToolLinks()
- {
- Database::getManager()
- ->createQuery('DELETE FROM ChamiloCourseBundle:CTool t WHERE t.category = :category AND t.link LIKE :link')
- ->execute(['category' => 'plugin', 'link' => 'embedregistry/start.php%']);
- }
- }
|