Skip to content

Learnpath: Enable per-item PDF export control - refs #2969 #6541

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions public/main/inc/ajax/lp.ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,36 @@
case 'get_lp_export_items':
$lpItems = [];
if ($lp) {
$items = learnpath::get_flat_ordered_items_list($lp);
$lpItemRepo = Container::getLpItemRepository();
$entries = learnpath::get_flat_ordered_items_list($lp, 0, true);
$lpItemRepo = Container::getLpItemRepository();
$documentRepo = Container::getDocumentRepository();
foreach ($items as $itemId) {
$item = $lpItemRepo->find($itemId);

if ('document' !== $item->getItemType()) {
foreach ($entries as $entry) {
$iid = (int) $entry['iid'];
$exportAllowed = !empty($entry['export_allowed']);
if (!$exportAllowed) {
continue;
}

/** @var CLpItem|null $item */
$item = $lpItemRepo->find($iid);
if (!$item || $item->getItemType() !== 'document') {
continue;
}

$document = $documentRepo->find((int) $item->getPath());
if (!$document instanceof CDocument) {
$doc = $documentRepo->find((int) $item->getPath());
if (!$doc instanceof CDocument) {
continue;
}

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

$lpItems[] = [
'id' => $item->getIid(),
'id' => $item->getIid(),
'title' => $item->getTitle(),
];
} catch (\Throwable $e) {
Expand Down
16 changes: 13 additions & 3 deletions public/main/lp/ScormExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -1020,15 +1020,25 @@ public static function exportToPdf(int $lpId, array $courseInfo, array $selected
$courseCode = $courseInfo['code'];
$scormPath = null;

$orderedItemIds = learnpath::get_flat_ordered_items_list($lp);
$entries = learnpath::get_flat_ordered_items_list($lp, 0, true);

foreach ($entries as $entry) {
$itemId = is_array($entry) ? (int) $entry['iid'] : (int) $entry;

foreach ($orderedItemIds as $itemId) {
if (!empty($selectedItems) && !in_array($itemId, $selectedItems)) {
continue;
}

if (is_array($entry) && empty($entry['export_allowed'])) {
continue;
}

/** @var CLpItem $item */
$item = $lpItemRepo->find($itemId);
if (!$item) {
continue;
}

$type = $item->getItemType();

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

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

Expand Down
68 changes: 45 additions & 23 deletions public/main/lp/learnpath.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,7 @@ public static function get_type_static($lp_id = 0)
*
* @return array Ordered list of item IDs (empty array on error)
*/
public static function get_flat_ordered_items_list(CLp $lp, $parent = 0)
public static function get_flat_ordered_items_list(CLp $lp, $parent = 0, $withExportFlag = false)
{
$parent = (int) $parent;
$lpItemRepo = Container::getLpItemRepository();
Expand All @@ -2577,32 +2577,41 @@ public static function get_flat_ordered_items_list(CLp $lp, $parent = 0)
$criteria = new Criteria();
$criteria
->where($criteria->expr()->neq('path', 'root'))
->orderBy(
[
'displayOrder' => Criteria::ASC,
]
);
->orderBy(['displayOrder' => Criteria::ASC]);
$items = $lp->getItems()->matching($criteria);
$items = $items->filter(
function (CLpItem $element) use ($parent) {
if ('root' === $element->getPath()) {
return false;
}

if (null !== $element->getParent()) {
return $element->getParent()->getIid() === $parent;
}
$items = $items->filter(function (CLpItem $element) use ($parent) {
if ('root' === $element->getPath()) {
return false;
}
if (null !== $element->getParent()) {
return $element->getParent()->getIid() === $parent;
}
return false;
});

if (!$withExportFlag) {
$ids = [];
foreach ($items as $item) {
$itemId = $item->getIid();
$ids[] = $itemId;
$subIds = self::get_flat_ordered_items_list($lp, $itemId, false);
foreach ($subIds as $subId) {
$ids[] = $subId;
}
}
);
return $ids;
}

$list = [];
foreach ($items as $item) {
$itemId = $item->getIid();
$sublist = self::get_flat_ordered_items_list($lp, $itemId);
$list[] = $itemId;
foreach ($sublist as $subItem) {
$list[] = $subItem;
$list[] = [
'iid' => $itemId,
'export_allowed' => $item->isExportAllowed() ? 1 : 0,
];
$subList = self::get_flat_ordered_items_list($lp, $itemId, true);
foreach ($subList as $subEntry) {
$list[] = $subEntry;
}
}

Expand Down Expand Up @@ -5671,6 +5680,17 @@ public function displayDocumentForm($action = 'add', $lpItem = null)
LearnPathItemForm::setForm($form, $action, $this, $lpItem);
}

if ($action === 'edit' && $lpItem !== null) {
$form->addElement(
'checkbox',
'export_allowed',
get_lang('Allow PDF export for this item')
);
$form->setDefaults([
'export_allowed' => $lpItem->isExportAllowed() ? 1 : 0
]);
}

switch ($action) {
case 'add':
$folders = DocumentManager::get_all_document_folders(
Expand All @@ -5686,11 +5706,11 @@ public function displayDocumentForm($action = 'add', $lpItem = null)
$form,
'directory_parent_id'
);

if ($data) {
$defaults['directory_parent_id'] = $data->getIid();
$form->setDefaults([
'directory_parent_id' => $data->getIid()
]);
}

break;
}

Expand All @@ -5699,6 +5719,8 @@ public function displayDocumentForm($action = 'add', $lpItem = null)
return $form->returnForm();
}



/**
* @param array $courseInfo
* @param string $content
Expand Down
12 changes: 12 additions & 0 deletions public/main/lp/lp_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,18 @@
if (isset($_POST['content_lp'])) {
$oLP->edit_document($courseInfo);
}

$exportAllowed = (isset($_POST['export_allowed']) && '1' === $_POST['export_allowed']);
$repo = Container::getLpItemRepository();
/** @var CLpItem $item */
$item = $repo->find((int) $_REQUEST['id']);
if ($item) {
$item->setExportAllowed($exportAllowed);
$em = Database::getManager();
$em->persist($item);
$em->flush();
}

$is_success = true;
$extraFieldValues = new ExtraFieldValue('lp_item');
$extraFieldValues->saveFieldValues($_POST);
Expand Down
Loading