Skip to content

Commit 888b5e7

Browse files
Learnpath: Enable per-item PDF export control - refs #2969
Author: @christianbeeznest
1 parent 92a8227 commit 888b5e7

File tree

4 files changed

+87
-37
lines changed

4 files changed

+87
-37
lines changed

public/main/inc/ajax/lp.ajax.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,36 @@
3333
case 'get_lp_export_items':
3434
$lpItems = [];
3535
if ($lp) {
36-
$items = learnpath::get_flat_ordered_items_list($lp);
37-
$lpItemRepo = Container::getLpItemRepository();
36+
$entries = learnpath::get_flat_ordered_items_list($lp, 0, true);
37+
$lpItemRepo = Container::getLpItemRepository();
3838
$documentRepo = Container::getDocumentRepository();
39-
foreach ($items as $itemId) {
40-
$item = $lpItemRepo->find($itemId);
4139

42-
if ('document' !== $item->getItemType()) {
40+
foreach ($entries as $entry) {
41+
$iid = (int) $entry['iid'];
42+
$exportAllowed = !empty($entry['export_allowed']);
43+
if (!$exportAllowed) {
44+
continue;
45+
}
46+
47+
/** @var CLpItem|null $item */
48+
$item = $lpItemRepo->find($iid);
49+
if (!$item || $item->getItemType() !== 'document') {
4350
continue;
4451
}
4552

46-
$document = $documentRepo->find((int) $item->getPath());
47-
if (!$document instanceof CDocument) {
53+
$doc = $documentRepo->find((int) $item->getPath());
54+
if (!$doc instanceof CDocument) {
4855
continue;
4956
}
5057

51-
// Only export if it's a valid HTML file
5258
try {
53-
$content = $documentRepo->getResourceFileContent($document);
54-
if (!is_string($content) || !preg_match('/^\s*<(?!!--|!doctype|html|body)/i', $content)) {
59+
$content = $documentRepo->getResourceFileContent($doc);
60+
if (!is_string($content) || stripos(ltrim($content), '<') !== 0) {
5561
continue;
5662
}
5763

5864
$lpItems[] = [
59-
'id' => $item->getIid(),
65+
'id' => $item->getIid(),
6066
'title' => $item->getTitle(),
6167
];
6268
} catch (\Throwable $e) {

public/main/lp/ScormExport.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,15 +1020,25 @@ public static function exportToPdf(int $lpId, array $courseInfo, array $selected
10201020
$courseCode = $courseInfo['code'];
10211021
$scormPath = null;
10221022

1023-
$orderedItemIds = learnpath::get_flat_ordered_items_list($lp);
1023+
$entries = learnpath::get_flat_ordered_items_list($lp, 0, true);
1024+
1025+
foreach ($entries as $entry) {
1026+
$itemId = is_array($entry) ? (int) $entry['iid'] : (int) $entry;
10241027

1025-
foreach ($orderedItemIds as $itemId) {
10261028
if (!empty($selectedItems) && !in_array($itemId, $selectedItems)) {
10271029
continue;
10281030
}
10291031

1032+
if (is_array($entry) && empty($entry['export_allowed'])) {
1033+
continue;
1034+
}
1035+
10301036
/** @var CLpItem $item */
10311037
$item = $lpItemRepo->find($itemId);
1038+
if (!$item) {
1039+
continue;
1040+
}
1041+
10321042
$type = $item->getItemType();
10331043

10341044
switch ($type) {
@@ -1048,7 +1058,7 @@ public static function exportToPdf(int $lpId, array $courseInfo, array $selected
10481058
try {
10491059
$content = $documentRepo->getResourceFileContent($document);
10501060

1051-
if (!is_string($content) || !preg_match('/^\s*<(?!!--|!doctype|html|body)/i', $content)) {
1061+
if (!is_string($content) || stripos(ltrim($content), '<') !== 0) {
10521062
break;
10531063
}
10541064

public/main/lp/learnpath.class.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,7 +2574,7 @@ public static function get_type_static($lp_id = 0)
25742574
*
25752575
* @return array Ordered list of item IDs (empty array on error)
25762576
*/
2577-
public static function get_flat_ordered_items_list(CLp $lp, $parent = 0)
2577+
public static function get_flat_ordered_items_list(CLp $lp, $parent = 0, $withExportFlag = false)
25782578
{
25792579
$parent = (int) $parent;
25802580
$lpItemRepo = Container::getLpItemRepository();
@@ -2592,32 +2592,41 @@ public static function get_flat_ordered_items_list(CLp $lp, $parent = 0)
25922592
$criteria = new Criteria();
25932593
$criteria
25942594
->where($criteria->expr()->neq('path', 'root'))
2595-
->orderBy(
2596-
[
2597-
'displayOrder' => Criteria::ASC,
2598-
]
2599-
);
2595+
->orderBy(['displayOrder' => Criteria::ASC]);
26002596
$items = $lp->getItems()->matching($criteria);
2601-
$items = $items->filter(
2602-
function (CLpItem $element) use ($parent) {
2603-
if ('root' === $element->getPath()) {
2604-
return false;
2605-
}
2606-
2607-
if (null !== $element->getParent()) {
2608-
return $element->getParent()->getIid() === $parent;
2609-
}
2597+
$items = $items->filter(function (CLpItem $element) use ($parent) {
2598+
if ('root' === $element->getPath()) {
26102599
return false;
2600+
}
2601+
if (null !== $element->getParent()) {
2602+
return $element->getParent()->getIid() === $parent;
2603+
}
2604+
return false;
2605+
});
26112606

2607+
if (!$withExportFlag) {
2608+
$ids = [];
2609+
foreach ($items as $item) {
2610+
$itemId = $item->getIid();
2611+
$ids[] = $itemId;
2612+
$subIds = self::get_flat_ordered_items_list($lp, $itemId, false);
2613+
foreach ($subIds as $subId) {
2614+
$ids[] = $subId;
2615+
}
26122616
}
2613-
);
2617+
return $ids;
2618+
}
2619+
26142620
$list = [];
26152621
foreach ($items as $item) {
26162622
$itemId = $item->getIid();
2617-
$sublist = self::get_flat_ordered_items_list($lp, $itemId);
2618-
$list[] = $itemId;
2619-
foreach ($sublist as $subItem) {
2620-
$list[] = $subItem;
2623+
$list[] = [
2624+
'iid' => $itemId,
2625+
'export_allowed' => $item->isExportAllowed() ? 1 : 0,
2626+
];
2627+
$subList = self::get_flat_ordered_items_list($lp, $itemId, true);
2628+
foreach ($subList as $subEntry) {
2629+
$list[] = $subEntry;
26212630
}
26222631
}
26232632

@@ -5660,6 +5669,17 @@ public function displayDocumentForm($action = 'add', $lpItem = null)
56605669
LearnPathItemForm::setForm($form, $action, $this, $lpItem);
56615670
}
56625671

5672+
if ($action === 'edit' && $lpItem !== null) {
5673+
$form->addElement(
5674+
'checkbox',
5675+
'export_allowed',
5676+
get_lang('Allow PDF export for this item')
5677+
);
5678+
$form->setDefaults([
5679+
'export_allowed' => $lpItem->isExportAllowed() ? 1 : 0
5680+
]);
5681+
}
5682+
56635683
switch ($action) {
56645684
case 'add':
56655685
$folders = DocumentManager::get_all_document_folders(
@@ -5675,11 +5695,11 @@ public function displayDocumentForm($action = 'add', $lpItem = null)
56755695
$form,
56765696
'directory_parent_id'
56775697
);
5678-
56795698
if ($data) {
5680-
$defaults['directory_parent_id'] = $data->getIid();
5699+
$form->setDefaults([
5700+
'directory_parent_id' => $data->getIid()
5701+
]);
56815702
}
5682-
56835703
break;
56845704
}
56855705

@@ -5688,6 +5708,8 @@ public function displayDocumentForm($action = 'add', $lpItem = null)
56885708
return $form->returnForm();
56895709
}
56905710

5711+
5712+
56915713
/**
56925714
* @param array $courseInfo
56935715
* @param string $content

public/main/lp/lp_controller.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,18 @@
593593
if (isset($_POST['content_lp'])) {
594594
$oLP->edit_document($courseInfo);
595595
}
596+
597+
$exportAllowed = (isset($_POST['export_allowed']) && '1' === $_POST['export_allowed']);
598+
$repo = Container::getLpItemRepository();
599+
/** @var CLpItem $item */
600+
$item = $repo->find((int) $_REQUEST['id']);
601+
if ($item) {
602+
$item->setExportAllowed($exportAllowed);
603+
$em = Database::getManager();
604+
$em->persist($item);
605+
$em->flush();
606+
}
607+
596608
$is_success = true;
597609
$extraFieldValues = new ExtraFieldValue('lp_item');
598610
$extraFieldValues->saveFieldValues($_POST);

0 commit comments

Comments
 (0)