Actualización

This commit is contained in:
Xes
2025-04-10 12:36:07 +02:00
parent 1da7c3f3b9
commit 4aff98e77b
3147 changed files with 320647 additions and 0 deletions

View File

@@ -0,0 +1,202 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\PluginBundle\Entity\CourseHomeNotify\Notification;
use Chamilo\PluginBundle\Entity\CourseHomeNotify\NotificationRelUser;
use Doctrine\ORM\Tools\SchemaTool;
/**
* Class CourseHomeNotifyPlugin.
*/
class CourseHomeNotifyPlugin extends Plugin
{
public const SETTING_ENABLED = 'enabled';
/**
* CourseHomeNotifyPlugin constructor.
*/
protected function __construct()
{
$settings = [
self::SETTING_ENABLED => 'boolean',
];
parent::__construct('0.1', 'Angel Fernando Quiroz Campos', $settings);
$this->isCoursePlugin = true;
$this->addCourseTool = false;
$this->setCourseSettings();
}
/**
* @return CourseHomeNotifyPlugin|null
*/
public static function create()
{
static $result = null;
return $result ? $result : $result = new self();
}
/**
* Install process.
* Create table in database. And setup Doctirne entity.
*
* @throws \Doctrine\ORM\Tools\ToolsException
*/
public function install()
{
$em = Database::getManager();
if ($em->getConnection()->getSchemaManager()->tablesExist(['course_home_notify_notification'])) {
return;
}
$schemaTool = new SchemaTool($em);
$schemaTool->createSchema(
[
$em->getClassMetadata(Notification::class),
$em->getClassMetadata(NotificationRelUser::class),
]
);
}
/**
* Uninstall process.
* Remove Doctrine entity. And drop table in database.
*/
public function uninstall()
{
$em = Database::getManager();
if (!$em->getConnection()->getSchemaManager()->tablesExist(['course_home_notify_notification'])) {
return;
}
$schemaTool = new SchemaTool($em);
$schemaTool->dropSchema(
[
$em->getClassMetadata(Notification::class),
$em->getClassMetadata(NotificationRelUser::class),
]
);
}
/**
* @param string $region
*
* @return string
*/
public function renderRegion($region)
{
if (
'main_bottom' !== $region
|| strpos($_SERVER['SCRIPT_NAME'], 'course_home/course_home.php') === false
) {
return '';
}
$courseId = api_get_course_int_id();
$userId = api_get_user_id();
if (empty($courseId) || empty($userId)) {
return '';
}
$course = api_get_course_entity($courseId);
$user = api_get_user_entity($userId);
$em = Database::getManager();
/** @var Notification $notification */
$notification = $em
->getRepository('ChamiloPluginBundle:CourseHomeNotify\Notification')
->findOneBy(['course' => $course]);
if (!$notification) {
return '';
}
$modalFooter = '';
$modalConfig = ['show' => true];
if ($notification->getExpirationLink()) {
/** @var NotificationRelUser $notificationUser */
$notificationUser = $em
->getRepository('ChamiloPluginBundle:CourseHomeNotify\NotificationRelUser')
->findOneBy(['notification' => $notification, 'user' => $user]);
if ($notificationUser) {
return '';
}
$contentUrl = api_get_path(WEB_PLUGIN_PATH).$this->get_name().'/content.php?hash='.$notification->getHash();
$link = Display::toolbarButton(
$this->get_lang('PleaseFollowThisLink'),
$contentUrl,
'external-link',
'link',
['id' => 'course-home-notify-link', 'target' => '_blank']
);
$modalConfig['keyboard'] = false;
$modalConfig['backdrop'] = 'static';
$modalFooter = '<div class="modal-footer">'.$link.'</div>';
}
$modal = '<div id="course-home-notify-modal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="'.get_lang('Close').'">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title">'.$this->get_lang('CourseNotice').'</h4>
</div>
<div class="modal-body">
'.$notification->getContent().'
</div>
'.$modalFooter.'
</div>
</div>
</div>';
$modal .= "<script>
$(document).ready(function () {
\$('#course-home-notify-modal').modal(".json_encode($modalConfig).");
\$('#course-home-notify-link').on('click', function () {
$('#course-home-notify-modal').modal('hide');
});
});
</script>";
return $modal;
}
/**
* Set the course settings.
*/
private function setCourseSettings()
{
if ('true' !== $this->get(self::SETTING_ENABLED)) {
return;
}
$name = $this->get_name();
$button = Display::toolbarButton(
$this->get_lang('SetNotification'),
api_get_path(WEB_PLUGIN_PATH).$name.'/configure.php?'.api_get_cidreq(),
'cog',
'primary'
);
$this->course_settings = [
[
'name' => '<p>'.$this->get_comment().'</p>'.$button.'<hr>',
'type' => 'html',
],
];
}
}

View File

@@ -0,0 +1,152 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\Entity\CourseHomeNotify;
use Chamilo\CoreBundle\Entity\Course;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Notification.
*
* @package Chamilo\PluginBundle\Entity\CourseHomeNotify
*
* @ORM\Table(name="course_home_notify_notification")
* @ORM\Entity()
*/
class Notification
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue()
*/
private $id = 0;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* @var string
*
* @ORM\Column(name="expiration_link", type="string")
*/
private $expirationLink;
/**
* @var string
*
* @ORM\Column(name="hash", type="string")
*/
private $hash;
/**
* @var Course
*
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
* @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $course;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*
* @return Notification
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* @param string $content
*
* @return Notification
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* @return string
*/
public function getExpirationLink()
{
return $this->expirationLink;
}
/**
* @param string $expirationLink
*
* @return Notification
*/
public function setExpirationLink($expirationLink)
{
$this->expirationLink = $expirationLink;
return $this;
}
/**
* @return string
*/
public function getHash()
{
return $this->hash;
}
/**
* @param string $hash
*
* @return Notification
*/
public function setHash($hash)
{
$this->hash = $hash;
return $this;
}
/**
* @return Course
*/
public function getCourse()
{
return $this->course;
}
/**
* @param Course $course
*
* @return Notification
*/
public function setCourse($course)
{
$this->course = $course;
return $this;
}
}

View File

@@ -0,0 +1,99 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\Entity\CourseHomeNotify;
use Chamilo\UserBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* Class NotificationRelUser.
*
* @package Chamilo\PluginBundle\Entity\CourseHomeNotify
*
* @ORM\Table(name="course_home_notify_notification_rel_user")
* @ORM\Entity()
*/
class NotificationRelUser
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue()
*/
private $id = 0;
/**
* @var Notification
*
* @ORM\ManyToOne(targetEntity="Chamilo\PluginBundle\Entity\CourseHomeNotify\Notification")
* @ORM\JoinColumn(name="notification_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $notification;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $user;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*
* @return NotificationRelUser
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return Notification
*/
public function getNotification()
{
return $this->notification;
}
/**
* @return NotificationRelUser
*/
public function setNotification(Notification $notification)
{
$this->notification = $notification;
return $this;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* @param User $user
*
* @return NotificationRelUser
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
}

View File

@@ -0,0 +1,17 @@
# Notify in course home
Show notifications when a user enter in course home page.
## Set up
*Prior to installing/uninstalling this plugin, you will need to make sure the src/Chamilo/PluginBundle/Entity folder is
temporarily writeable by the web server.*
* Install the plugin and enable.
* Go to Settings tool in course base.
* Display the _Notify in course home_ section. And set the notification.
## Adding a notification
The notification has a HTML content and an optional _Expiration link_ field.
If this field is set, then the notification will be displayed until the user visualizes it.
Otherwise the notification will be displayed every time the user enter to the course home page.

View File

@@ -0,0 +1,100 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/../../main/inc/global.inc.php';
use Chamilo\PluginBundle\Entity\CourseHomeNotify\Notification;
$plugin = CourseHomeNotifyPlugin::create();
$courseId = api_get_course_int_id();
if (
empty($courseId) ||
'true' !== $plugin->get(CourseHomeNotifyPlugin::SETTING_ENABLED)
) {
api_not_allowed(true);
}
$action = isset($_GET['action']) ? $_GET['action'] : '';
$course = api_get_course_entity($courseId);
$em = Database::getManager();
/** @var Notification $notification */
$notification = $em
->getRepository('ChamiloPluginBundle:CourseHomeNotify\Notification')
->findOneBy(['course' => $course]);
$actionLinks = '';
if ($notification) {
$actionLinks = Display::url(
Display::return_icon('delete.png', $plugin->get_lang('DeleteNotification'), [], ICON_SIZE_MEDIUM),
api_get_self().'?'.api_get_cidreq().'&action=delete'
);
if ('delete' === $action) {
$em->remove($notification);
$em->flush();
Display::addFlash(
Display::return_message($plugin->get_lang('NotificationDeleted'), 'success')
);
header('Location: '.api_get_course_url());
exit;
}
} else {
$notification = new Notification();
}
$form = new FormValidator('frm_course_home_notify');
$form->addHeader($plugin->get_lang('AddNotification'));
$form->applyFilter('title', 'trim');
$form->addHtmlEditor('content', get_lang('Content'), true, false, ['ToolbarSet' => 'Minimal']);
$form->addUrl(
'expiration_link',
[$plugin->get_lang('ExpirationLink'), $plugin->get_lang('ExpirationLinkHelp')],
false,
['placeholder' => 'https://']
);
$form->addButtonSave(get_lang('Save'));
if ($form->validate()) {
$values = $form->exportValues();
$notification
->setContent($values['content'])
->setExpirationLink($values['expiration_link'])
->setCourse($course)
->setHash(md5(uniqid()));
$em->persist($notification);
$em->flush();
Display::addFlash(
Display::return_message($plugin->get_lang('NotificationAdded'), 'success')
);
header('Location: '.api_get_course_url());
exit;
}
if ($notification) {
$form->setDefaults(
[
'content' => $notification->getContent(),
'expiration_link' => $notification->getExpirationLink(),
]
);
}
$template = new Template($plugin->get_title());
$template->assign('header', $plugin->get_title());
if ($actionLinks) {
$template->assign('actions', Display::toolbarAction('course-home-notify-actions', ['', $actionLinks]));
}
$template->assign('content', $form->returnForm());
$template->display_one_col_template();

View File

@@ -0,0 +1,52 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/../../main/inc/global.inc.php';
use Chamilo\PluginBundle\Entity\CourseHomeNotify\Notification;
use Chamilo\PluginBundle\Entity\CourseHomeNotify\NotificationRelUser;
api_block_anonymous_users(true);
api_protect_course_script(true);
$plugin = CourseHomeNotifyPlugin::create();
$userId = api_get_user_id();
$courseId = api_get_course_int_id();
if (
empty($courseId) ||
empty($userId) ||
'true' !== $plugin->get(CourseHomeNotifyPlugin::SETTING_ENABLED)
) {
api_not_allowed(true);
}
$user = api_get_user_entity($userId);
$course = api_get_course_entity($courseId);
$hash = isset($_GET['hash']) ? Security::remove_XSS($_GET['hash']) : null;
$em = Database::getManager();
/** @var Notification $notification */
$notification = $em
->getRepository('ChamiloPluginBundle:CourseHomeNotify\Notification')
->findOneBy(['course' => $course, 'hash' => $hash]);
if (!$notification) {
api_not_allowed(true);
}
$notificationUser = $em
->getRepository('ChamiloPluginBundle:CourseHomeNotify\NotificationRelUser')
->findOneBy(['notification' => $notification, 'user' => $user]);
if (!$notificationUser) {
$notificationUser = new NotificationRelUser();
$notificationUser
->setUser($user)
->setNotification($notification);
$em->persist($notificationUser);
$em->flush();
}
header('Location: '.$notification->getExpirationLink());

View File

@@ -0,0 +1,8 @@
<?php
/* For licensing terms, see /license.txt */
if (!api_is_platform_admin()) {
api_not_allowed(true);
}
CourseHomeNotifyPlugin::create()->install();

View File

@@ -0,0 +1,17 @@
<?php
/* For licensing terms, see /license.txt */
$strings['plugin_title'] = "Notify in course home";
$strings['plugin_comment'] = "Show notifications when a user enter in course home page.";
$strings['enabled'] = 'Enabled';
$strings['SetNotification'] = 'Set one notification on home page';
$strings['DeleteNotification'] = 'Delete notification on course home page';
$strings['AddNotification'] = 'Add notification';
$strings['ExpirationLink'] = 'Expiration link';
$strings['CourseNotice'] = 'Course notice';
$strings['PleaseFollowThisLink'] = 'Please, follow this link to continue';
$strings['ExpirationLinkHelp'] = 'The notification will be displayed until the user visualizes it.';
$strings['NotificationAdded'] = 'Course notification added: It will be displayed in course home page.';
$strings['NotificationDeleted'] = 'Course notification deleted';

View File

@@ -0,0 +1,17 @@
<?php
/* For licensing terms, see /license.txt */
$strings['plugin_title'] = "Notificar en página de inicio del curso";
$strings['plugin_comment'] = "Mostrar notificaciones cuando un usuario ingresa en la página principal del curso.";
$strings['enabled'] = 'Habilitado';
$strings['SetNotification'] = 'Establecer una notificación en la página de inicio';
$strings['DeleteNotification'] = 'Eliminar notificación en la página de inicio del curso';
$strings['AddNotification'] = 'Añadir notificación';
$strings['ExpirationLink'] = 'Enlace de caducidad';
$strings['CourseNotice'] = 'Aviso del curso';
$strings['PleaseFollowThisLink'] = 'Por favor, siga este enlace para continuar.';
$strings['ExpirationLinkHelp'] = 'La notificación se mostrará hasta que el usuario la visualice.';
$strings['NotificationAdded'] = 'Notificación del curso agregada: se mostrará en la página de inicio del curso.';
$strings['NotificationDeleted'] = 'Notificación del curso borrada';

View File

@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */
$plugin_info = CourseHomeNotifyPlugin::create()->get_info();

View File

@@ -0,0 +1,8 @@
<?php
/* For licensing terms, see /license.txt */
if (!api_is_platform_admin()) {
api_not_allowed(true);
}
CourseHomeNotifyPlugin::create()->uninstall();