diff --git a/public/main/admin/questions.php b/public/main/admin/questions.php index 9bc0dc4ba0..ba4ba449e8 100644 --- a/public/main/admin/questions.php +++ b/public/main/admin/questions.php @@ -20,14 +20,86 @@ Session::erase('objExercise'); Session::erase('objQuestion'); Session::erase('objAnswer'); -$interbreadcrumb[] = ['url' => '/admin', 'name' => get_lang('Administration')]; +$interbreadcrumb[] = ['url' => Container::getRouter()->generate('admin'), 'name' => get_lang('Administration')]; +$action = $_REQUEST['action'] ?? ''; +$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : ''; +$description = $_REQUEST['description'] ?? ''; +$title = $_REQUEST['title'] ?? ''; +$page = !empty($_GET['page']) ? (int) $_GET['page'] : 1; + +// Prepare lists for form +// Courses list +$selectedCourse = isset($_GET['selected_course']) ? (int) $_GET['selected_course'] : null; +$courseList = CourseManager::get_courses_list(0, 0, 'title'); +$courseSelectionList = ['-1' => get_lang('Select')]; + +foreach ($courseList as $item) { + $course = api_get_course_entity($item['real_id']); + $courseSelectionList[$course->getId()] = ''; + + if ($course->getId() == api_get_course_int_id()) { + $courseSelectionList[$course->getId()] = '>    '; + } + + $courseSelectionList[$course->getId()] .= $course->getTitle(); +} + +// Difficulty list (only from 0 to 5) +$questionLevel = isset($_REQUEST['question_level']) ? (int) $_REQUEST['question_level'] : -1; +$levels = [ + -1 => get_lang('All'), + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, +]; + +// Answer type +$answerType = isset($_REQUEST['answer_type']) ? (int) $_REQUEST['answer_type'] : null; +$questionList = Question::getQuestionTypeList(); +$questionTypesList = []; +$questionTypesList['-1'] = get_lang('All'); + +foreach ($questionList as $key => $item) { + $questionTypesList[$key] = get_lang($item[1]); +} $form = new FormValidator('admin_questions', 'get'); $form->addHeader(get_lang('Questions')); $form->addText('id', get_lang('Id'), false); $form->addText('title', get_lang('Title'), false); $form->addText('description', get_lang('Description'), false); +$form + ->addSelect( + 'selected_course', + [get_lang('Course'), get_lang('Course in which the question was initially created.')], + $courseSelectionList, + ['id' => 'selected_course'] + ) + ->setSelected($selectedCourse) +; +$form + ->addSelect( + 'question_level', + get_lang('Difficulty'), + $levels, + ['id' => 'question_level'] + ) + ->setSelected($questionLevel) +; +$form + ->addSelect( + 'answer_type', + get_lang('Answer type'), + $questionTypesList, + ['id' => 'answer_type'] + ) + ->setSelected($answerType) +; $form->addHidden('form_sent', 1); +$form->addHidden('course_id_changed', '0'); $form->addButtonSearch(get_lang('Search')); $questions = []; @@ -37,12 +109,19 @@ $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 = isset($_REQUEST['description']) ? $_REQUEST['description'] : ''; - $title = isset($_REQUEST['title']) ? $_REQUEST['title'] : ''; - $page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1; + $params['form_sent'] = 1; $em = Database::getManager(); $repo = $em->getRepository(CQuizQuestion::class); @@ -61,23 +140,27 @@ if ($formSent) { $criteria->orWhere($criteria->expr()->contains('question', "%$title%")); } - $questions = $repo->matching($criteria); +// if (-1 !== $selectedCourse) { +// $criteria->andWhere($criteria->expr()->eq('cId', $selectedCourse)); +// } - if (empty($id)) { - $id = ''; + if (-1 !== $questionLevel) { + $criteria->andWhere($criteria->expr()->eq('level', $questionLevel)); + } + if (-1 !== $answerType) { + $criteria->andWhere($criteria->expr()->eq('type', $answerType)); } - $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); + $questions = $repo->matching($criteria); + $url = api_get_self().'?'.http_build_query($params); + $form->setDefaults($params); $questionCount = count($questions); + if ('export_pdf' === $action) { + $length = $questionCount; + } + $paginator = new Paginator(Container::$container->get('event_dispatcher')); $pagination = $paginator->paginate($questions, $page, $length); $pagination->setItemNumberPerPage($length); @@ -142,6 +225,14 @@ 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(), @@ -222,11 +313,20 @@ if ($formSent) { $formContent = $form->returnForm(); -$action = isset($_REQUEST['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 = isset($_REQUEST['questionId']) ? $_REQUEST['questionId'] : ''; - $courseId = isset($_REQUEST['courseId']) ? $_REQUEST['courseId'] : ''; + $questionId = $_REQUEST['questionId'] ?? ''; + $courseId = $_REQUEST['courseId'] ?? ''; $courseInfo = api_get_course_info_by_id($courseId); if (!empty($courseInfo)) { @@ -245,16 +345,36 @@ switch ($action) { header("Location: $url"); exit; - break; } +$actionsLeft = Display::url( + Display::return_icon('back.png', get_lang('Administration'), [], ICON_SIZE_MEDIUM), + Container::getRouter()->generate('admin'), +); + +$exportUrl = Container::getRouter()->generate( + 'legacy_main', + ['name' => 'admin/questions.php', 'action' => 'export_pdf', ...$params] +); + +$actionsRight = Display::url( + Display::return_icon('pdf.png', get_lang('Export to PDF'), [], ICON_SIZE_MEDIUM), + $exportUrl +); + +$toolbar = Display::toolbarAction( + 'toolbar-admin-questions', + [$actionsLeft, $actionsRight] +); + $tpl = new Template(get_lang('Questions')); $tpl->assign('form', $formContent); +$tpl->assign('toolbar', $toolbar); $tpl->assign('pagination', $pagination); $tpl->assign('pagination_length', $length); $tpl->assign('start', $start); $tpl->assign('end', $end); $tpl->assign('question_count', $questionCount); -$layout = $tpl->get_template('admin/questions.tpl'); +$layout = $tpl->get_template('admin/questions.html.twig'); $tpl->display($layout); diff --git a/public/main/template/default/admin/questions.html.twig b/public/main/template/default/admin/questions.html.twig index 59327da3ed..1ff78d8d35 100644 --- a/public/main/template/default/admin/questions.html.twig +++ b/public/main/template/default/admin/questions.html.twig @@ -2,24 +2,26 @@ {% import '@ChamiloCore/Macros/box.html.twig' as display %} {% block content %} - {{ toolbar }} - {{ form }} -{# {% for question in pagination %}#} - {% for i in start..end %} - {% if pagination[i] is defined %} - {% set question = pagination[i] %} - {{ display.collapse( - question.iid, - '#' ~ question.courseCode ~'-'~ question.iid ~ ' - ' ~ question.question, - question.questionData, - false, - false - ) - }} - {% endif %} - {% endfor %} + {% autoescape false %} + {{ toolbar }} + {{ form }} + {# {% for question in pagination %}#} + {% for i in start..end %} + {% if pagination[i] is defined %} + {% set question = pagination[i] %} + {{ display.collapse( + question.iid, + '#' ~ question.courseCode ~'-'~ question.iid ~ ' - ' ~ question.question, + question.questionData, + false, + false + ) + }} + {% endif %} + {% endfor %} - {% if question_count > pagination_length %} - {{ pagination }} - {% endif %} + {% if question_count > pagination_length %} + {{ pagination }} + {% endif %} + {% endautoescape %} {% endblock %}