Skip to content

Commit

Permalink
Announcement: Improve queries and documentation in course_announcemen…
Browse files Browse the repository at this point in the history
…t.php - refs BT#18956
  • Loading branch information
ywarnier committed Aug 15, 2021
1 parent 6f1a04e commit 4e43809
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions main/cron/course_announcement.php
@@ -1,6 +1,9 @@
<?php
/* For licensing terms, see /license.txt */

use \Chamilo\CourseBundle\Entity\CItemProperty;
use \Chamilo\CoreBundle\Entity\Session;

require __DIR__.'/../inc/global.inc.php';

if (php_sapi_name() != 'cli') {
Expand All @@ -11,6 +14,7 @@
exit;
}

// Get all pending (visible) announcements where e-mail sent is empty
$dql = "SELECT a
FROM ChamiloCourseBundle:CAnnouncement a
JOIN ChamiloCourseBundle:CItemProperty ip
Expand All @@ -32,6 +36,8 @@
$extraFieldValue = new ExtraFieldValue('course_announcement');
$today = date('Y-m-d');

// For each announcement, check rules about sending the notification at a
// specific date
foreach ($result as $announcement) {
$sendNotification = $extraFieldValue->get_values_by_handler_and_field_variable($announcement->getId(), 'send_notification_at_a_specific_date');

Expand All @@ -43,23 +49,34 @@
WHERE ip.ref = :announcementId
AND ip.course = :courseId
AND ip.tool = '".TOOL_ANNOUNCEMENT."'
ORDER BY iid DESC";
ORDER BY ip.iid DESC";

$sql = Database::getManager()->createQuery($query);
$itemProperty = $sql->execute(['announcementId' => $announcement->getId(), 'courseId' => $announcement->getCId()]);
if (empty($itemProperty) or !isset($itemProperty[0])) {
continue;
}
/* @var CItemProperty $itemPropertyObject */
$itemPropertyObject = $itemProperty[0];
// Check if the last record for this announcement was not a removal
if ($itemProperty[0]['lastedit_type'] == 'AnnouncementDeleted' or $itemProperty[0]['visibility'] == 2) {
if ($itemPropertyObject->getLastEditType() == 'AnnouncementDeleted' or $itemPropertyObject->getVisibility() == 2) {
continue;
}
/* @var \Chamilo\CoreBundle\Entity\Session $sessionObject */
$sessionObject = $itemProperty[0]->getSession();
$sessionId = $sessionObject->getId();
/* @var Session $sessionObject */
$sessionObject = $itemPropertyObject->getSession();
if (!empty($sessionObject)) {
$sessionId = $sessionObject->getId();
} else {
$sessionId = null;
}
$courseInfo = api_get_course_info_by_id($announcement->getCId());
$senderId = $itemProperty[0]->getInsertUser()->getId();
$sendToUsersInSession = (int) $extraFieldValue->get_values_by_handler_and_field_variable($announcement->getId(), 'send_to_users_in_session')['value'];
$senderId = $itemPropertyObject->getInsertUser()->getId();
// Check if we need to send it to all users of all sessions that
// include this course.
$sendToUsersInSession = (int) $extraFieldValue->get_values_by_handler_and_field_variable(
$announcement->getId(),
'send_to_users_in_session'
)['value'];

$messageSentTo = AnnouncementManager::sendEmail(
$courseInfo,
Expand Down

0 comments on commit 4e43809

Please sign in to comment.