Skip to content

Session: Allow scheduled announcements to session users #5843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions public/main/inc/lib/ScheduledAnnouncement.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ public function sendPendingMessages($urlId = 0)
if (!empty($sessionInfo) && !empty($courseInfo)) {
$progress = Tracking::get_avg_student_progress(
$user['user_id'],
$courseInfo['code'],
api_get_course_entity($courseInfo['real_id']),
[],
$sessionId
api_get_session_entity($sessionId)
);
}

Expand Down
17 changes: 5 additions & 12 deletions public/main/inc/lib/extra_field_value.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,28 +291,22 @@ public function saveFieldValues(
}
break;
case ExtraField::FIELD_TYPE_FILE:
$cleanedName = api_replace_dangerous_char($value['name']);
$fileName = ExtraField::FIELD_TYPE_FILE."_{$params['item_id']}_$cleanedName";
if (isset($value['name']) && !empty($value['tmp_name']) && isset($value['error']) && 0 == $value['error']) {
$cleanedName = api_replace_dangerous_char($value['name']);
$fileName = ExtraField::FIELD_TYPE_FILE."_{$params['item_id']}_$cleanedName";

if (!empty($value['tmp_name']) && isset($value['error']) && 0 == $value['error']) {
$mimeType = mime_content_type($value['tmp_name']);
$file = new UploadedFile($value['tmp_name'], $fileName, $mimeType, null, true);
$asset = (new Asset())
->setCategory(Asset::EXTRA_FIELD)
->setTitle($fileName)
->setFile($file)
;
->setFile($file);

$em->persist($asset);
$em->flush();
$assetId = $asset->getId();

if ($assetId) {
/*$new_params = [
'item_id' => $params['item_id'],
'field_id' => $extraFieldInfo['id'],
'value' => $assetId,
'asset_id' => $assetId,
];*/
$field = Container::getExtraFieldRepository()->find($extraFieldInfo['id']);
$extraFieldValues = (new ExtraFieldValues())
->setItemId((int) $params['item_id'])
Expand All @@ -323,7 +317,6 @@ public function saveFieldValues(
;
$em->persist($extraFieldValues);
$em->flush();
//$this->save($new_params);
}
}
break;
Expand Down
27 changes: 14 additions & 13 deletions public/main/session/scheduled_announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
break;
}

$values['sent'] = isset($values['sent']) ? (int) $values['sent'] : 0;
$res = $object->save($values);

if ($res) {
Expand Down Expand Up @@ -129,7 +130,7 @@
if ($form->validate()) {
$values = $form->getSubmitValues();
$values['id'] = $id;
$values['sent'] = isset($values['sent']) ? 1 : '';
$values['sent'] = isset($values['sent']) ? (int)$values['sent'] : 0;
$values['date'] = api_get_utc_datetime($values['date']);
$res = $object->update($values);

Expand All @@ -141,13 +142,15 @@
get_lang('Update successful'),
'confirmation'
));
header("Location: $url");
exit;

$content = $object->getGrid($sessionId);
} else {
$item = $object->get($id);
$item['date'] = api_get_local_time($item['date']);
$form->setDefaults($item);
$content = $form->returnForm();
}
$item = $object->get($id);
$item['date'] = api_get_local_time($item['date']);
$form->setDefaults($item);
$content = $form->returnForm();

break;
case 'delete':
$object->delete($id);
Expand All @@ -171,27 +174,25 @@
[
'name' => 'subject',
'index' => 'subject',
'width' => '250',
'width' => '350',
'align' => 'left',
],
[
'name' => 'date',
'index' => 'date',
//'width' => '90',
//'align' => 'left',
'width' => '190',
'align' => 'left',
'sortable' => 'true',
],
[
'name' => 'sent',
'index' => 'sent',
//'width' => '90',
//'align' => 'left',
'sortable' => 'true',
],
[
'name' => 'actions',
'index' => 'actions',
'width' => '100',
'width' => '200',
'align' => 'left',
'formatter' => 'action_formatter',
'sortable' => 'false',
Expand Down
74 changes: 74 additions & 0 deletions src/CoreBundle/Command/SendScheduledAnnouncementsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/* For licensing terms, see /license.txt */

namespace Chamilo\CoreBundle\Command;

use Chamilo\CoreBundle\Framework\Container;
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Service\ScheduledAnnouncementService;
use Database;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class SendScheduledAnnouncementsCommand extends Command
{
protected static $defaultName = 'app:send-scheduled-announcements';

public function __construct(
private readonly AccessUrlRepository $accessUrlRepository,
private readonly ScheduledAnnouncementService $scheduledAnnouncementService,
private readonly EntityManager $em
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Send scheduled announcements to all users.')
->addOption('debug', null, null, 'If set, debug messages will be shown.');

Check failure on line 35 in src/CoreBundle/Command/SendScheduledAnnouncementsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

InvalidConsoleOptionValue

src/CoreBundle/Command/SendScheduledAnnouncementsCommand.php:35:40: InvalidConsoleOptionValue: Use Symfony\Component\Console\Input\InputOption constants
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
Database::setManager($this->em);

$container = $this->getApplication()->getKernel()->getContainer();
Container::setContainer($container);

$io = new SymfonyStyle($input, $output);
$debug = (bool) $input->getOption('debug');
$urlList = $this->accessUrlRepository->findAll();

if (empty($urlList)) {
$io->warning('No access URLs found.');
return Command::SUCCESS;
}

foreach ($urlList as $url) {
$urlId = $url->getId();
$io->writeln("Portal: #".$urlId." - ".$url->getUrl());

try {
$messagesSent = $this->scheduledAnnouncementService->sendPendingMessages($urlId, $debug);
$io->writeln("Messages sent: $messagesSent");

if ($debug) {
$io->writeln("Debug: Processed portal with ID ".$urlId);
}
} catch (\Exception $e) {
$io->error('Error processing portal with ID ' . $urlId . ': ' . $e->getMessage());
return Command::FAILURE;
}
}

$io->success('All scheduled announcements have been sent.');
return Command::SUCCESS;
}
}
3 changes: 2 additions & 1 deletion src/CoreBundle/Entity/ScheduledAnnouncement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

namespace Chamilo\CoreBundle\Entity;

use Chamilo\CoreBundle\Repository\ScheduledAnnouncementRepository;
use DateTime;
use Doctrine\ORM\Mapping as ORM;

/**
* ScheduledAnnouncement.
*/
#[ORM\Table(name: 'scheduled_announcements')]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: ScheduledAnnouncementRepository::class)]
class ScheduledAnnouncement
{
#[ORM\Column(name: 'id', type: 'integer')]
Expand Down
22 changes: 22 additions & 0 deletions src/CoreBundle/Repository/Node/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -1033,4 +1033,26 @@ public function getUserPicture(

return $url.$paramsToString;
}

/**
* Retrieves the list of DRH (HR) users related to a specific user and access URL.
*/
public function getDrhListFromUser(int $userId, int $accessUrlId): array
{
$qb = $this->createQueryBuilder('u');

$this->addAccessUrlQueryBuilder($accessUrlId, $qb);

$qb->select('u.id, u.username, u.firstname, u.lastname')
->innerJoin('u.friends', 'uru', Join::WITH, 'uru.friend = u.id')
->where('uru.user = :userId')
->andWhere('uru.relationType = :relationType')
->setParameter('userId', $userId)
->setParameter('relationType', UserRelUser::USER_RELATION_TYPE_RRHH);

$qb->orderBy('u.lastname', 'ASC')
->addOrderBy('u.firstname', 'ASC');

return $qb->getQuery()->getResult();
}
}
31 changes: 31 additions & 0 deletions src/CoreBundle/Repository/ScheduledAnnouncementRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/* For licensing terms, see /license.txt */

namespace Chamilo\CoreBundle\Repository;

use Chamilo\CoreBundle\Entity\ScheduledAnnouncement;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class ScheduledAnnouncementRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ScheduledAnnouncement::class);
}

/**
* Mark an announcement as sent.
*
* @param ScheduledAnnouncement $announcement
*/
public function markAsSent(ScheduledAnnouncement $announcement): void
{
$announcement->setSent(true);
$this->_em->persist($announcement);
$this->_em->flush();
}
}
Loading
Loading