Skip to content

Commit 774085d

Browse files
authored
feat(27775): Add property to added multimediaObject in lms (#66)
* feat(27775): Add property to added multimediaObject in lms * feat(27775): Save years in array without overwriting * feat(27775): Save years in array without overwriting, code improvement
1 parent aaa2d75 commit 774085d

File tree

3 files changed

+116
-9
lines changed

3 files changed

+116
-9
lines changed

Controller/EmbedLMSController.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pumukit\LmsBundle\Controller;
6+
7+
use Doctrine\ODM\MongoDB\DocumentManager;
8+
use Pumukit\SchemaBundle\Document\MultimediaObject;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\HttpFoundation\JsonResponse;
11+
use Symfony\Component\HttpFoundation\Request;
12+
use Symfony\Component\Routing\Annotation\Route;
13+
14+
class EmbedLMSController extends AbstractController
15+
{
16+
private $documentManager;
17+
18+
public function __construct(DocumentManager $documentManager)
19+
{
20+
$this->documentManager = $documentManager;
21+
}
22+
23+
/**
24+
* @Route("/lms/embed", name="pumukit_lms_embed", methods={"POST"})
25+
*/
26+
public function embedLms(Request $request): JsonResponse
27+
{
28+
$data = json_decode($request->getContent(), true);
29+
$mmId = $data['mmId'] ?? null;
30+
31+
if (!$mmId) {
32+
return new JsonResponse(['error' => 'Missing mmId'], 400);
33+
}
34+
35+
$multimediaObject = $this->documentManager->getRepository(MultimediaObject::class)->findOneBy([
36+
'_id' => $mmId,
37+
]);
38+
39+
if (!$multimediaObject) {
40+
return new JsonResponse(['error' => 'MultimediaObject not found'], 404);
41+
}
42+
43+
$lms_years = (array) $multimediaObject->getProperty('embedded_in_lms');
44+
$lms_years[] = date('Y');
45+
$lms_years = array_unique($lms_years);
46+
$multimediaObject->setProperty('embedded_in_lms', $lms_years);
47+
48+
$this->documentManager->flush();
49+
50+
return new JsonResponse(['status' => 'ok', 'mmId' => $mmId]);
51+
}
52+
}

Resources/data/override/4.x/PumukitNewAdminBundle/MultimediaObject/list.html.twig

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
{% endif %}
2828
<td headers="th-icons">
2929
{% if is_naked() and mm.containsAnyTagWithCodes(['PUCHOPENEDX', 'PUCHMOODLE', 'PUCHLMS'])%}
30-
<a class="moodlepr-add" href="#" title="{% trans %}Publish to LMS{% endtrans %}" onclick="parent.window.postMessage({'mmId': '{{ mm.id }}'}, '*'); return false;"><i class="mdi-editor-insert-photo"></i></a>
30+
<a class="moodlepr-add" href="#" data-mm="{{ mm.id }}" title="{% trans %}Publish to LMS{% endtrans %}"><i class="mdi-editor-insert-photo"></i></a>
3131
{% endif %}
3232
</td>
3333
<td headers="th-icons">
@@ -142,14 +142,37 @@
142142
</style>
143143
<script>
144144
$(function(){
145-
parent.postMessage('enableMoodlePRAdd?', '*');
146-
window.addEventListener('message', function(e){
147-
if(e.data.moodlepradd != 'OK'){
148-
return;
149-
}
150-
$('.moodlepr-add').show();
151-
});
152-
});
145+
parent.postMessage('enableMoodlePRAdd?', '*');
146+
window.addEventListener('message', function(e){
147+
if(e.data.moodlepradd != 'OK'){
148+
return;
149+
}
150+
$('.moodlepr-add').show();
151+
});
152+
});
153+
154+
function embedToLMS(mmId) {
155+
parent.window.postMessage({ mmId }, '*');
156+
157+
$.ajax({
158+
url: "{{ path('pumukit_lms_embed') }}",
159+
method: 'POST',
160+
contentType: 'application/json',
161+
data: JSON.stringify({ mmId: mmId }),
162+
success: function(data) {
163+
console.log('Server response:', data);
164+
},
165+
error: function(xhr, status, error) {
166+
console.error('Error sending request:', error);
167+
}
168+
});
169+
}
170+
171+
$(document).on('click', '.moodlepr-add', function (e) {
172+
e.preventDefault();
173+
const mmId = $(this).data('mm');
174+
embedToLMS(mmId);
175+
});
153176
</script>
154177
{# TODO variables
155178
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{% if is_naked() %}
2+
<td headers="th-icons" style="width: 1em;">
3+
{% if mm.containsAnyTagWithCodes(['PUCHOPENEDX', 'PUCHMOODLE', 'PUCHLMS'])%}
4+
<a class="moodlepr-add" href="#" data-mm="{{ mm.id }}" title="{% trans %}Publish to LMS{% endtrans %}"><i class="mdi-editor-insert-photo"></i></a>
5+
{% endif %}
6+
</td>
7+
{% endif %}
8+
9+
<script>
10+
function embedToLMS(mmId) {
11+
parent.window.postMessage({ mmId }, '*');
12+
13+
$.ajax({
14+
url: "{{ path('pumukit_lms_embed') }}",
15+
method: 'POST',
16+
contentType: 'application/json',
17+
data: JSON.stringify({ mmId: mmId }),
18+
success: function(data) {
19+
console.log('Server response:', data);
20+
},
21+
error: function(xhr, status, error) {
22+
console.error('Error sending request:', error);
23+
}
24+
});
25+
}
26+
27+
$(document).on('click', '.moodlepr-add', function (e) {
28+
e.preventDefault();
29+
const mmId = $(this).data('mm');
30+
embedToLMS(mmId);
31+
});
32+
</script>

0 commit comments

Comments
 (0)