From 5ed552cd634d11770958fdaa73b71ec8aa701b2c Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Wed, 18 Aug 2021 00:49:08 +0200 Subject: [PATCH] Admin: Questions bank: Add PDF export - refs #3946 --- main/admin/questions.php | 86 +++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/main/admin/questions.php b/main/admin/questions.php index 54a75a2e0a..a4afbd6f1c 100644 --- a/main/admin/questions.php +++ b/main/admin/questions.php @@ -19,6 +19,11 @@ Session::erase('objQuestion'); Session::erase('objAnswer'); $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')]; +$action = $_REQUEST['action'] ?? ''; +$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : ''; +$description = $_REQUEST['description'] ?? ''; +$title = $_REQUEST['title'] ?? ''; +$page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1; // Prepare lists for form // Courses list @@ -81,7 +86,7 @@ $form 'selected_course', [get_lang('Course'), get_lang('CourseInWhichTheQuestionWasInitiallyCreated')], $courseSelectionList, - ['onchange' => 'mark_course_id_changed(); submit_form(this);', 'id' => 'selected_course'] + ['id' => 'selected_course'] ) ->setSelected($selectedCourse); $form @@ -89,7 +94,7 @@ $form 'question_level', get_lang('Difficulty'), $levels, - ['onchange' => 'submit_form(this);', 'id' => 'question_level'] + ['id' => 'question_level'] ) ->setSelected($questionLevel); $form @@ -97,7 +102,7 @@ $form 'answer_type', get_lang('AnswerType'), $questionTypesList, - ['onchange' => 'submit_form(this);', 'id' => 'answer_type'] + ['id' => 'answer_type'] ) ->setSelected($answerType); @@ -112,13 +117,18 @@ $length = 20; $questionCount = 0; $start = 0; $end = 0; - +$pdfContent = ''; + +$params = [ + 'id' => $id, + 'title' => Security::remove_XSS($title), + 'description' => Security::remove_XSS($description), + 'selected_course' => $selectedCourse, + 'question_level' => $questionLevel, + 'answer_type' => $answerType, +]; if ($formSent) { - $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : ''; - $description = $_REQUEST['description'] ?? ''; - $title = $_REQUEST['title'] ?? ''; - $page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1; - + $params['form_sent'] = 1; $em = Database::getManager(); $repo = $em->getRepository('ChamiloCourseBundle:CQuizQuestion'); $criteria = new Criteria(); @@ -149,19 +159,14 @@ if ($formSent) { $questions = $repo->matching($criteria); - if (empty($id)) { - $id = ''; - } - $params = [ - 'id' => $id, - 'title' => Security::remove_XSS($title), - 'description' => Security::remove_XSS($description), - 'form_sent' => 1, - ]; $url = api_get_self().'?'.http_build_query($params); $form->setDefaults($params); $questionCount = count($questions); + if ('export_pdf' === $action) { + $length = $questionCount; + } + $paginator = new Paginator(); $pagination = $paginator->paginate($questions, $page, $length); $pagination->setItemNumberPerPage($length); @@ -209,6 +214,7 @@ if ($formSent) { $question->courseCode = $courseCode; // Creating empty exercise $exercise = new Exercise($courseId); + /* @var Question $questionObject */ $questionObject = Question::read($question->getIid(), $courseInfo); ob_start(); @@ -226,9 +232,17 @@ if ($formSent) { ); $question->questionData = ob_get_contents(); + if ('export_pdf' === $action) { + $pdfContent .= '#'.$question->getIid().'. '.$question->getQuestion().'
'; + $pdfContent .= '('.$questionTypesList[$question->getType()].') ['.get_lang('Source').': '.$courseCode.']
'; + $pdfContent .= $question->getDescription().'
'; + $pdfContent .= $question->questionData; + continue; + } + $deleteUrl = $url.'&'.http_build_query([ 'courseId' => $question->getCId(), - 'questionId' => $question->getId(), + 'questionId' => $question->getIid(), 'action' => 'delete', ]); @@ -250,7 +264,7 @@ if ($formSent) { 'id_session' => $exercise->sessionId, 'exerciseId' => $exerciseId, 'type' => $question->getType(), - 'editQuestion' => $question->getId(), + 'editQuestion' => $question->getIid(), ] ), ['target' => '_blank'] @@ -309,8 +323,17 @@ if ($formSent) { $formContent = $form->returnForm(); -$action = $_REQUEST['action'] ?? ''; switch ($action) { + case 'export_pdf': + $pdfContent = Security::remove_XSS($pdfContent); + $pdfParams = [ + 'filename' => 'questions-export-'.api_get_local_time(), + 'pdf_date' => api_get_local_time(), + 'orientation' => 'P', + ]; + $pdf = new PDF('A4', $pdfParams['orientation'], $pdfParams); + $pdf->html_to_pdf_with_template($pdfContent, false, false, true); + exit; case 'delete': $questionId = $_REQUEST['questionId'] ?? ''; $courseId = $_REQUEST['courseId'] ?? ''; @@ -338,30 +361,21 @@ $actionsLeft = Display::url( Display::return_icon('back.png', get_lang('PlatformAdmin'), [], ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH).'admin/index.php' ); -$actionsRight = ''; -/* + +$exportUrl = api_get_path(WEB_CODE_PATH) + .'admin/questions.php?action=export_pdf&' + .http_build_query($params); + $actionsRight = Display::url( Display::return_icon('pdf.png', get_lang('ExportToPDF'), [], ICON_SIZE_MEDIUM), - api_get_path(WEB_CODE_PATH).'admin/questions.php?action=exportpdf' + $exportUrl ); -*/ $toolbar = Display::toolbarAction( 'toolbar-admin-questions', [$actionsLeft, $actionsRight] ); -$htmlHeadXtra[] = " -"; - $tpl = new Template(get_lang('Questions')); $tpl->assign('form', $formContent); $tpl->assign('toolbar', $toolbar);