Skip to content

Commit 5ed552c

Browse files
committed
Admin: Questions bank: Add PDF export - refs #3946
1 parent 5868d60 commit 5ed552c

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

main/admin/questions.php

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
Session::erase('objAnswer');
2020

2121
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
22+
$action = $_REQUEST['action'] ?? '';
23+
$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : '';
24+
$description = $_REQUEST['description'] ?? '';
25+
$title = $_REQUEST['title'] ?? '';
26+
$page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1;
2227

2328
// Prepare lists for form
2429
// Courses list
@@ -81,23 +86,23 @@
8186
'selected_course',
8287
[get_lang('Course'), get_lang('CourseInWhichTheQuestionWasInitiallyCreated')],
8388
$courseSelectionList,
84-
['onchange' => 'mark_course_id_changed(); submit_form(this);', 'id' => 'selected_course']
89+
['id' => 'selected_course']
8590
)
8691
->setSelected($selectedCourse);
8792
$form
8893
->addSelect(
8994
'question_level',
9095
get_lang('Difficulty'),
9196
$levels,
92-
['onchange' => 'submit_form(this);', 'id' => 'question_level']
97+
['id' => 'question_level']
9398
)
9499
->setSelected($questionLevel);
95100
$form
96101
->addSelect(
97102
'answer_type',
98103
get_lang('AnswerType'),
99104
$questionTypesList,
100-
['onchange' => 'submit_form(this);', 'id' => 'answer_type']
105+
['id' => 'answer_type']
101106
)
102107
->setSelected($answerType);
103108

@@ -112,13 +117,18 @@
112117
$questionCount = 0;
113118
$start = 0;
114119
$end = 0;
115-
120+
$pdfContent = '';
121+
122+
$params = [
123+
'id' => $id,
124+
'title' => Security::remove_XSS($title),
125+
'description' => Security::remove_XSS($description),
126+
'selected_course' => $selectedCourse,
127+
'question_level' => $questionLevel,
128+
'answer_type' => $answerType,
129+
];
116130
if ($formSent) {
117-
$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : '';
118-
$description = $_REQUEST['description'] ?? '';
119-
$title = $_REQUEST['title'] ?? '';
120-
$page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1;
121-
131+
$params['form_sent'] = 1;
122132
$em = Database::getManager();
123133
$repo = $em->getRepository('ChamiloCourseBundle:CQuizQuestion');
124134
$criteria = new Criteria();
@@ -149,19 +159,14 @@
149159

150160
$questions = $repo->matching($criteria);
151161

152-
if (empty($id)) {
153-
$id = '';
154-
}
155-
$params = [
156-
'id' => $id,
157-
'title' => Security::remove_XSS($title),
158-
'description' => Security::remove_XSS($description),
159-
'form_sent' => 1,
160-
];
161162
$url = api_get_self().'?'.http_build_query($params);
162163
$form->setDefaults($params);
163164
$questionCount = count($questions);
164165

166+
if ('export_pdf' === $action) {
167+
$length = $questionCount;
168+
}
169+
165170
$paginator = new Paginator();
166171
$pagination = $paginator->paginate($questions, $page, $length);
167172
$pagination->setItemNumberPerPage($length);
@@ -209,6 +214,7 @@
209214
$question->courseCode = $courseCode;
210215
// Creating empty exercise
211216
$exercise = new Exercise($courseId);
217+
/* @var Question $questionObject */
212218
$questionObject = Question::read($question->getIid(), $courseInfo);
213219

214220
ob_start();
@@ -226,9 +232,17 @@
226232
);
227233
$question->questionData = ob_get_contents();
228234

235+
if ('export_pdf' === $action) {
236+
$pdfContent .= '<span style="color:#000; font-weight:bold; font-size:x-large;">#'.$question->getIid().'. '.$question->getQuestion().'</span><br />';
237+
$pdfContent .= '<span style="color:#444;">('.$questionTypesList[$question->getType()].') ['.get_lang('Source').': '.$courseCode.']</span><br />';
238+
$pdfContent .= $question->getDescription().'<br />';
239+
$pdfContent .= $question->questionData;
240+
continue;
241+
}
242+
229243
$deleteUrl = $url.'&'.http_build_query([
230244
'courseId' => $question->getCId(),
231-
'questionId' => $question->getId(),
245+
'questionId' => $question->getIid(),
232246
'action' => 'delete',
233247
]);
234248

@@ -250,7 +264,7 @@
250264
'id_session' => $exercise->sessionId,
251265
'exerciseId' => $exerciseId,
252266
'type' => $question->getType(),
253-
'editQuestion' => $question->getId(),
267+
'editQuestion' => $question->getIid(),
254268
]
255269
),
256270
['target' => '_blank']
@@ -309,8 +323,17 @@
309323

310324
$formContent = $form->returnForm();
311325

312-
$action = $_REQUEST['action'] ?? '';
313326
switch ($action) {
327+
case 'export_pdf':
328+
$pdfContent = Security::remove_XSS($pdfContent);
329+
$pdfParams = [
330+
'filename' => 'questions-export-'.api_get_local_time(),
331+
'pdf_date' => api_get_local_time(),
332+
'orientation' => 'P',
333+
];
334+
$pdf = new PDF('A4', $pdfParams['orientation'], $pdfParams);
335+
$pdf->html_to_pdf_with_template($pdfContent, false, false, true);
336+
exit;
314337
case 'delete':
315338
$questionId = $_REQUEST['questionId'] ?? '';
316339
$courseId = $_REQUEST['courseId'] ?? '';
@@ -338,30 +361,21 @@
338361
Display::return_icon('back.png', get_lang('PlatformAdmin'), [], ICON_SIZE_MEDIUM),
339362
api_get_path(WEB_CODE_PATH).'admin/index.php'
340363
);
341-
$actionsRight = '';
342-
/*
364+
365+
$exportUrl = api_get_path(WEB_CODE_PATH)
366+
.'admin/questions.php?action=export_pdf&'
367+
.http_build_query($params);
368+
343369
$actionsRight = Display::url(
344370
Display::return_icon('pdf.png', get_lang('ExportToPDF'), [], ICON_SIZE_MEDIUM),
345-
api_get_path(WEB_CODE_PATH).'admin/questions.php?action=exportpdf'
371+
$exportUrl
346372
);
347-
*/
348373

349374
$toolbar = Display::toolbarAction(
350375
'toolbar-admin-questions',
351376
[$actionsLeft, $actionsRight]
352377
);
353378

354-
$htmlHeadXtra[] = "
355-
<script>
356-
function submit_form(obj) {
357-
document.question_pool.submit();
358-
}
359-
360-
function mark_course_id_changed() {
361-
$('#course_id_changed').val('1');
362-
}
363-
</script>";
364-
365379
$tpl = new Template(get_lang('Questions'));
366380
$tpl->assign('form', $formContent);
367381
$tpl->assign('toolbar', $toolbar);

0 commit comments

Comments
 (0)