questions.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CQuizQuestion;
  4. use ChamiloSession as Session;
  5. use Doctrine\Common\Collections\Criteria;
  6. use Knp\Component\Pager\Paginator;
  7. /**
  8. * @package chamilo.admin
  9. */
  10. $cidReset = true;
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. $this_section = SECTION_PLATFORM_ADMIN;
  13. api_protect_admin_script();
  14. Session::erase('objExercise');
  15. Session::erase('objQuestion');
  16. Session::erase('objAnswer');
  17. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  18. $form = new FormValidator('admin_questions', 'get');
  19. $form->addHeader(get_lang('Questions'));
  20. $form->addText('id', get_lang('Id'), false);
  21. $form->addText('title', get_lang('Title'), false);
  22. $form->addText('description', get_lang('Description'), false);
  23. $form->addHidden('form_sent', 1);
  24. $form->addButtonSearch(get_lang('Search'));
  25. $questions = [];
  26. $pagination = '';
  27. $formSent = isset($_REQUEST['form_sent']) ? (int) $_REQUEST['form_sent'] : 0;
  28. $length = 20;
  29. $questionCount = 0;
  30. if ($formSent) {
  31. $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : '';
  32. $description = isset($_REQUEST['description']) ? Security::remove_XSS($_REQUEST['description']) : '';
  33. $title = isset($_REQUEST['title']) ? Security::remove_XSS($_REQUEST['title']) : '';
  34. $page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1;
  35. $em = Database::getManager();
  36. $repo = $em->getRepository('ChamiloCourseBundle:CQuizQuestion');
  37. $criteria = new Criteria();
  38. if (!empty($id)) {
  39. $criteria->where($criteria->expr()->eq('iid', $id));
  40. }
  41. if (!empty($description)) {
  42. $criteria->orWhere($criteria->expr()->contains('description', "%$description%"));
  43. }
  44. if (!empty($title)) {
  45. $criteria->orWhere($criteria->expr()->contains('question', "%$title%"));
  46. }
  47. $questions = $repo->matching($criteria);
  48. if (empty($id)) {
  49. $id = '';
  50. }
  51. $params = [
  52. 'id' => $id,
  53. 'title' => $title,
  54. 'description' => $description,
  55. 'form_sent' => 1,
  56. ];
  57. $url = api_get_self().'?'.http_build_query($params);
  58. $form->setDefaults($params);
  59. $questionCount = count($questions);
  60. $paginator = new Paginator();
  61. $pagination = $paginator->paginate($questions, $page, $length);
  62. $pagination->setItemNumberPerPage($length);
  63. $pagination->setCurrentPageNumber($page);
  64. $pagination->renderer = function ($data) use ($url) {
  65. $render = '<ul class="pagination">';
  66. for ($i = 1; $i <= $data['pageCount']; $i++) {
  67. $page = (int) $i;
  68. $pageContent = '<li><a href="'.$url.'&page='.$page.'">'.$page.'</a></li>';
  69. if ($data['current'] == $page) {
  70. $pageContent = '<li class="active"><a href="#" >'.$page.'</a></li>';
  71. }
  72. $render .= $pageContent;
  73. }
  74. $render .= '</ul>';
  75. return $render;
  76. };
  77. /** @var CQuizQuestion $question */
  78. if ($pagination) {
  79. $url = api_get_path(WEB_CODE_PATH).'exercise/admin.php?';
  80. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?';
  81. foreach ($pagination as $question) {
  82. $courseId = $question->getCId();
  83. $courseInfo = api_get_course_info_by_id($courseId);
  84. $courseCode = $courseInfo['code'];
  85. $question->courseCode = $courseCode;
  86. // Creating empty exercise
  87. $exercise = new Exercise($courseId);
  88. $questionObject = Question::read($question->getId(), $courseInfo);
  89. ob_start();
  90. ExerciseLib::showQuestion(
  91. $exercise,
  92. $question->getId(),
  93. false,
  94. null,
  95. null,
  96. false,
  97. true,
  98. false,
  99. true,
  100. true
  101. );
  102. $question->questionData = ob_get_contents();
  103. $exerciseData = '';
  104. $exerciseId = 0;
  105. if (!empty($questionObject->exerciseList)) {
  106. $exerciseData .= get_lang('Exercises').'<br />';
  107. foreach ($questionObject->exerciseList as $exerciseId) {
  108. $exercise = new Exercise();
  109. $exercise->course_id = $question->getCId();
  110. $exercise->read($exerciseId);
  111. $exerciseData .= $exercise->title.'&nbsp;';
  112. $exerciseData .= Display::url(
  113. Display::return_icon('edit.png', get_lang('Edit')),
  114. $url.http_build_query([
  115. 'cidReq' => $courseCode,
  116. 'id_session' => $exercise->sessionId,
  117. 'exerciseId' => $exerciseId,
  118. 'type' => $question->getType(),
  119. 'editQuestion' => $question->getId(),
  120. ])
  121. );
  122. }
  123. $question->questionData .= '<br />'.$exerciseData;
  124. } else {
  125. $question->questionData = Display::url(
  126. Display::return_icon('edit.png', get_lang('Edit')),
  127. $url.http_build_query([
  128. 'cidReq' => $courseCode,
  129. 'id_session' => $exercise->sessionId,
  130. 'exerciseId' => $exerciseId,
  131. 'type' => $question->getType(),
  132. 'editQuestion' => $question->getId(),
  133. ])
  134. );
  135. }
  136. ob_end_clean();
  137. }
  138. }
  139. }
  140. $formContent = $form->returnForm();
  141. $tpl = new Template(get_lang('Questions'));
  142. $tpl->assign('form', $formContent);
  143. $tpl->assign('pagination', $pagination);
  144. $tpl->assign('pagination_length', $length);
  145. $tpl->assign('question_count', $questionCount);
  146. $layout = $tpl->get_template('admin/questions.tpl');
  147. $tpl->display($layout);