Passed
Pull Request — 1.11.x (#4368)
by Angel Fernando Quiroz
08:40
created

PortfolioAttachmentsTrait::generateAttachments()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 44
rs 9.1928
c 1
b 0
f 0
cc 5
nc 7
nop 2
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement;
6
7
use Chamilo\CoreBundle\Entity\PortfolioAttachment;
8
use Chamilo\UserBundle\Entity\User;
9
use UserManager;
10
use Xabbuh\XApi\Model\Attachment;
11
use Xabbuh\XApi\Model\IRI;
12
use Xabbuh\XApi\Model\IRL;
13
use Xabbuh\XApi\Model\LanguageMap;
14
15
trait PortfolioAttachmentsTrait
16
{
17
    /**
18
     * @param array<int, PortfolioAttachment> $portfolioAttachments
19
     *
20
     * @return array<int, Attachment>
21
     */
22
    protected function generateAttachments(array $portfolioAttachments, User $user): array
23
    {
24
        if (empty($portfolioAttachments)) {
25
            return [];
26
        }
27
28
        $attachments = [];
29
30
        $userDirectory = UserManager::getUserPathById($user->getId(), 'system');
31
        $attachmentsDirectory = $userDirectory.'portfolio_attachments/';
32
33
        $langIso = api_get_language_isocode();
34
35
        $cidreq = api_get_cidreq();
36
        $baseUrl = api_get_path(WEB_CODE_PATH).'portfolio/index.php?'.($cidreq ? $cidreq.'&' : '');
37
38
        foreach ($portfolioAttachments as $portfolioAttachment) {
39
            $attachmentFilename = $attachmentsDirectory.$portfolioAttachment->getPath();
40
41
            $display = LanguageMap::create(
42
                ['und' => $portfolioAttachment->getFilename()]
43
            );
44
            $description = null;
45
46
            if ($portfolioAttachment->getComment()) {
47
                $description = LanguageMap::create(
48
                    [$langIso => $portfolioAttachment->getComment()]
49
                );
50
            }
51
52
            $attachments[] = new Attachment(
53
                IRI::fromString('http://id.tincanapi.com/attachment/supporting_media'),
54
                mime_content_type($attachmentFilename),
55
                $portfolioAttachment->getSize(),
56
                hash_file('sha256', $attachmentFilename),
57
                $display,
58
                $description,
59
                IRL::fromString(
60
                    $baseUrl.http_build_query(['action' => 'download', 'file' => $portfolioAttachment->getPath()])
61
                )
62
            );
63
        }
64
65
        return $attachments;
66
    }
67
}
68