Skip to content
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

Course: Add resource_link_id in track_e_download and save download - refs #4861 #5086

Merged
merged 1 commit into from
Jan 16, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/vue/views/assignments/AssignmentsCreate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ function onSubmit(publicationStudent) {
.catch((error) => showErrorNotification(error))
.finally(() => (isFormLoading.value = false))
}
</script>
</script>
1 change: 1 addition & 0 deletions public/main/inc/lib/document.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,7 @@ public static function displaySimpleQuota($course_quota, $already_consumed_space
public static function enough_space($file_size, $max_dir_space)
{
if ($max_dir_space) {
$max_dir_space = $max_dir_space * 1024 * 1024;
$courseEntity = api_get_course_entity();
$repo = Container::getDocumentRepository();
$total = $repo->getFolderSize($courseEntity->getResourceNode(), $courseEntity);
Expand Down
28 changes: 12 additions & 16 deletions public/main/inc/lib/events.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Chamilo\CoreBundle\Entity\Session as SessionEntity;
use Chamilo\CoreBundle\Entity\TrackEAttemptQualify;
use Chamilo\CoreBundle\Entity\TrackEDefault;
use Chamilo\CoreBundle\Entity\TrackEDownloads;
use Chamilo\CoreBundle\Entity\TrackEExercise;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Framework\Container;
Expand Down Expand Up @@ -272,24 +273,19 @@ public static function event_download($documentUrl)
return false;
}

$table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS);
$documentUrl = Database::escape_string($documentUrl);
$user = api_get_user_entity();
$course = api_get_course_entity();
$session = api_get_session_entity();

$reallyNow = api_get_utc_datetime();
$userId = api_get_user_id();
$courseId = api_get_course_int_id();
$sessionId = api_get_session_id();
$em = Database::getManager();

return Database::insert(
$table,
[
'down_user_id' => $userId,
'c_id' => $courseId,
'down_doc_path' => $documentUrl,
'down_date' => $reallyNow,
'session_id' => $sessionId,
]
);
$repository = $em->getRepository(TrackEDownloads::class);

$resourceLinkId = $course->getFirstResourceLink()->getId();

$downloadId = $repository->saveDownload($user->getId(), $resourceLinkId, $documentUrl);

return $downloadId;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/CoreBundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Entity\ResourceNode;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\TrackEDownloads;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface;
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
use Chamilo\CoreBundle\Tool\ToolChain;
Expand Down Expand Up @@ -171,7 +173,7 @@ public function linkAction(Request $request, RouterInterface $router)
* @return RedirectResponse|StreamedResponse
*/
#[Route('/{tool}/{type}/{id}/download', name: 'chamilo_core_resource_download', methods: ['GET'])]
public function downloadAction(Request $request)
public function downloadAction(Request $request, EntityManagerInterface $entityManager)
{
$id = $request->get('id');
$resourceNode = $this->getResourceNodeRepository()->findOneBy(['uuid' => $id]);
Expand All @@ -190,6 +192,14 @@ public function downloadAction(Request $request)

// If resource node has a file just download it. Don't download the children.
if ($resourceNode->hasResourceFile()) {

/** @var ?User $user */
$user = $this->getUser();
$resourceLinkId = $resourceNode->getResourceLinks()->first()->getId();
$url = $resourceNode->getResourceFile()->getOriginalName();
$downloadRepository = $entityManager->getRepository(TrackEDownloads::class);
$downloadId = $downloadRepository->saveDownload($user->getId(), $resourceLinkId, $url);

// Redirect to download single file.
return $this->processFile($request, $resourceNode, 'download');
}
Expand Down
100 changes: 33 additions & 67 deletions src/CoreBundle/Entity/TrackEDownloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Chamilo\CoreBundle\Entity;

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

Expand All @@ -14,9 +15,7 @@
*/
#[ORM\Table(name: 'track_e_downloads')]
#[ORM\Index(name: 'idx_ted_user_id', columns: ['down_user_id'])]
#[ORM\Index(name: 'idx_ted_c_id', columns: ['c_id'])]
#[ORM\Index(name: 'session_id', columns: ['session_id'])]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: TrackEDownloadsRepository::class)]
class TrackEDownloads
{
#[ORM\Column(name: 'down_id', type: 'integer')]
Expand All @@ -30,132 +29,99 @@ class TrackEDownloads
#[ORM\Column(name: 'down_date', type: 'datetime', nullable: false)]
protected DateTime $downDate;

#[ORM\Column(name: 'c_id', type: 'integer', nullable: false)]
protected int $cId;

#[ORM\Column(name: 'down_doc_path', type: 'string', length: 255, nullable: false)]
protected string $downDocPath;

#[ORM\Column(name: 'session_id', type: 'integer', nullable: false)]
protected int $sessionId;
#[ORM\ManyToOne(targetEntity: ResourceLink::class, cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(name: 'resource_link_id', referencedColumnName: 'id', onDelete: 'SET NULL', nullable: true)]
protected ?ResourceLink $resourceLink = null;

/**
* Set downUserId.
* Set downDocPath.
*
* @return TrackEDownloads
*/
public function setDownUserId(int $downUserId)
public function setDownDocPath(string $downDocPath)
{
$this->downUserId = $downUserId;
$this->downDocPath = $downDocPath;

return $this;
}

/**
* Get downUserId.
*
* @return int
*/
public function getDownUserId()
{
return $this->downUserId;
}

/**
* Set downDate.
* Get downDocPath.
*
* @return TrackEDownloads
* @return string
*/
public function setDownDate(DateTime $downDate)
public function getDownDocPath()
{
$this->downDate = $downDate;

return $this;
return $this->downDocPath;
}

/**
* Get downDate.
* Get downId.
*
* @return DateTime
* @return int
*/
public function getDownDate()
public function getDownId()
{
return $this->downDate;
return $this->downId;
}

/**
* Set cId.
* Set downUserId.
*
* @return TrackEDownloads
*/
public function setCId(int $cId)
public function setDownUserId(int $downUserId)
{
$this->cId = $cId;
$this->downUserId = $downUserId;

return $this;
}

/**
* Get cId.
* Get downUserId.
*
* @return int
*/
public function getCId()
public function getDownUserId()
{
return $this->cId;
return $this->downUserId;
}

/**
* Set downDocPath.
* Set downDate.
*
* @return TrackEDownloads
*/
public function setDownDocPath(string $downDocPath)
public function setDownDate(DateTime $downDate)
{
$this->downDocPath = $downDocPath;
$this->downDate = $downDate;

return $this;
}

/**
* Get downDocPath.
* Get downDate.
*
* @return string
* @return DateTime
*/
public function getDownDocPath()
public function getDownDate()
{
return $this->downDocPath;
return $this->downDate;
}

/**
* Set sessionId.
* Set resourceLink.
*
* @param ResourceLink|null $resourceLink
* @return TrackEDownloads
*/
public function setSessionId(int $sessionId)
public function setResourceLink(?ResourceLink $resourceLink): self
{
$this->sessionId = $sessionId;
$this->resourceLink = $resourceLink;

return $this;
}

/**
* Get sessionId.
*
* @return int
*/
public function getSessionId()
{
return $this->sessionId;
}

/**
* Get downId.
*
* @return int
*/
public function getDownId()
{
return $this->downId;
}
}
6 changes: 6 additions & 0 deletions src/CoreBundle/Framework/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Chamilo\CoreBundle\Repository\SocialPostRepository;
use Chamilo\CoreBundle\Repository\SysAnnouncementRepository;
use Chamilo\CoreBundle\Repository\TagRepository;
use Chamilo\CoreBundle\Repository\TrackEDownloadsRepository;
use Chamilo\CoreBundle\Repository\TrackEExerciseRepository;
use Chamilo\CoreBundle\Repository\TrackELoginRecordRepository;
use Chamilo\CoreBundle\Serializer\UserToJsonNormalizer;
Expand Down Expand Up @@ -574,6 +575,11 @@ public static function getTrackEExerciseRepository(): TrackEExerciseRepository
return self::$container->get(TrackEExerciseRepository::class);
}

public static function getTrackEDownloadsRepository(): TrackEDownloadsRepository
{
return self::$container->get(TrackEDownloadsRepository::class);
}

public static function getWikiRepository(): CWikiRepository
{
return self::$container->get(CWikiRepository::class);
Expand Down
36 changes: 36 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20240114215900.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;

final class Version20240114215900 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'Modify track_e_downloads table structure';
}

public function up(Schema $schema): void
{
$this->addSql('DROP INDEX idx_ted_c_id ON track_e_downloads;');
$this->addSql('DROP INDEX down_session_id ON track_e_downloads;');
$this->addSql('ALTER TABLE track_e_downloads ADD resource_link_id INT DEFAULT NULL, DROP c_id, DROP down_session_id;');
$this->addSql('ALTER TABLE track_e_downloads ADD CONSTRAINT FK_EEDF4DA6F004E599 FOREIGN KEY (resource_link_id) REFERENCES resource_link (id) ON DELETE SET NULL;');
$this->addSql('CREATE INDEX IDX_EEDF4DA6F004E599 ON track_e_downloads (resource_link_id);');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE track_e_downloads DROP FOREIGN KEY FK_EEDF4DA6F004E599;');
$this->addSql('DROP INDEX IDX_EEDF4DA6F004E599 ON track_e_downloads;');
$this->addSql('ALTER TABLE track_e_downloads DROP resource_link_id;');
$this->addSql('ALTER TABLE track_e_downloads ADD c_id INT NOT NULL;');
$this->addSql('ALTER TABLE track_e_downloads ADD down_session_id INT DEFAULT NULL;');
$this->addSql('CREATE INDEX idx_ted_c_id ON track_e_downloads (c_id);');
$this->addSql('CREATE INDEX down_session_id ON track_e_downloads (down_session_id);');
}
}
39 changes: 39 additions & 0 deletions src/CoreBundle/Repository/TrackEDownloadsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

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

namespace Chamilo\CoreBundle\Repository;

use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Entity\TrackEDownloads;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

public function saveDownload(int $userId, int $resourceLinkId, string $documentUrl)
{
$download = new TrackEDownloads();
$download->setDownDocPath($documentUrl);
$download->setDownUserId($userId);
$download->setDownDate(new DateTime());

$resourceLink = $this->_em->getRepository(ResourceLink::class)->find($resourceLinkId);
if ($resourceLink) {
$download->setResourceLink($resourceLink);
}

$this->_em->persist($download);
$this->_em->flush();

return $download->getDownId();
}
}