questions.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CQuiz;
  4. use Chamilo\CourseBundle\Entity\CQuizQuestion;
  5. use ChamiloSession as Session;
  6. use Doctrine\Common\Collections\Criteria;
  7. use Knp\Component\Pager\Paginator;
  8. /**
  9. * @package chamilo.admin
  10. */
  11. $cidReset = true;
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. api_protect_admin_script();
  15. Session::erase('objExercise');
  16. Session::erase('objQuestion');
  17. Session::erase('objAnswer');
  18. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  19. $form = new FormValidator('admin_questions', 'get');
  20. $form->addHeader(get_lang('Questions'));
  21. $form->addText('id', get_lang('Id'), false);
  22. $form->addText('title', get_lang('Title'), false);
  23. $form->addText('description', get_lang('Description'), false);
  24. $form->addHidden('form_sent', 1);
  25. $form->addButtonSearch(get_lang('Search'));
  26. $questions = [];
  27. $pagination = '';
  28. $formSent = isset($_REQUEST['form_sent']) ? (int) $_REQUEST['form_sent'] : 0;
  29. $length = 20;
  30. $questionCount = 0;
  31. if ($formSent) {
  32. $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : '';
  33. $description = isset($_REQUEST['description']) ? $_REQUEST['description'] : '';
  34. $title = isset($_REQUEST['title']) ? $_REQUEST['title'] : '';
  35. $page = isset($_GET['page']) && !empty($_GET['page']) ? (int) $_GET['page'] : 1;
  36. $em = Database::getManager();
  37. $repo = $em->getRepository('ChamiloCourseBundle:CQuizQuestion');
  38. $criteria = new Criteria();
  39. if (!empty($id)) {
  40. $criteria->where($criteria->expr()->eq('iid', $id));
  41. }
  42. if (!empty($description)) {
  43. $criteria->orWhere($criteria->expr()->contains('description', $description."\r"));
  44. }
  45. if (!empty($title)) {
  46. $criteria->orWhere($criteria->expr()->contains('question', "%$title%"));
  47. }
  48. $questions = $repo->matching($criteria);
  49. if (empty($id)) {
  50. $id = '';
  51. }
  52. $params = [
  53. 'id' => $id,
  54. 'title' => Security::remove_XSS($title),
  55. 'description' => Security::remove_XSS($description),
  56. 'form_sent' => 1,
  57. ];
  58. $url = api_get_self().'?'.http_build_query($params);
  59. $form->setDefaults($params);
  60. $questionCount = count($questions);
  61. $paginator = new Paginator();
  62. $pagination = $paginator->paginate($questions, $page, $length);
  63. $pagination->setItemNumberPerPage($length);
  64. $pagination->setCurrentPageNumber($page);
  65. $pagination->renderer = function ($data) use ($url) {
  66. $render = '<ul class="pagination">';
  67. for ($i = 1; $i <= $data['pageCount']; $i++) {
  68. $page = (int) $i;
  69. $pageContent = '<li><a href="'.$url.'&page='.$page.'">'.$page.'</a></li>';
  70. if ($data['current'] == $page) {
  71. $pageContent = '<li class="active"><a href="#" >'.$page.'</a></li>';
  72. }
  73. $render .= $pageContent;
  74. }
  75. $render .= '</ul>';
  76. return $render;
  77. };
  78. if ($pagination) {
  79. $urlExercise = api_get_path(WEB_CODE_PATH).'exercise/admin.php?';
  80. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?';
  81. /** @var CQuizQuestion $question */
  82. foreach ($pagination as $question) {
  83. $courseId = $question->getCId();
  84. $courseInfo = api_get_course_info_by_id($courseId);
  85. $courseCode = $courseInfo['code'];
  86. $question->courseCode = $courseCode;
  87. // Creating empty exercise
  88. $exercise = new Exercise($courseId);
  89. $questionObject = Question::read($question->getId(), $courseInfo);
  90. ob_start();
  91. ExerciseLib::showQuestion(
  92. $exercise,
  93. $question->getId(),
  94. false,
  95. null,
  96. null,
  97. false,
  98. true,
  99. false,
  100. true,
  101. true
  102. );
  103. $question->questionData = ob_get_contents();
  104. $exerciseData = '';
  105. $exerciseId = 0;
  106. if (!empty($questionObject->exerciseList)) {
  107. // Question exists in a valid exercise
  108. $exerciseData .= get_lang('Exercises').'<br />';
  109. foreach ($questionObject->exerciseList as $exerciseId) {
  110. $exercise = new Exercise();
  111. $exercise->course_id = $question->getCId();
  112. $exercise->read($exerciseId);
  113. $exerciseData .= $exercise->title.'&nbsp;';
  114. $exerciseData .= Display::url(
  115. Display::return_icon('edit.png', get_lang('Edit')),
  116. $urlExercise.http_build_query([
  117. 'cidReq' => $courseCode,
  118. 'id_session' => $exercise->sessionId,
  119. 'exerciseId' => $exerciseId,
  120. 'type' => $question->getType(),
  121. 'editQuestion' => $question->getId(),
  122. ])
  123. );
  124. }
  125. $question->questionData .= '<br />'.$exerciseData;
  126. } else {
  127. // Question exists but it's orphan or it belongs to a deleted exercise
  128. $question->questionData .= Display::url(
  129. Display::return_icon('edit.png', get_lang('Edit')),
  130. $urlExercise.http_build_query([
  131. 'cidReq' => $courseCode,
  132. 'id_session' => $exercise->sessionId,
  133. 'exerciseId' => $exerciseId,
  134. 'type' => $question->getType(),
  135. 'editQuestion' => $question->getId(),
  136. ])
  137. );
  138. $question->questionData .= Display::url(
  139. Display::return_icon('delete.png', get_lang('Delete')),
  140. $url.'&'.http_build_query([
  141. 'courseId' => $question->getCId(),
  142. 'questionId' => $question->getId(),
  143. 'action' => 'delete',
  144. ])
  145. ).'<br />';
  146. // This means the question is added in a deleted exercise
  147. if ($questionObject->getCountExercise() > 0) {
  148. $exerciseList = $questionObject->getExerciseListWhereQuestionExists();
  149. if (!empty($exerciseList)) {
  150. $question->questionData .= '<br />'.get_lang('Exercises').'<br />';
  151. /** @var CQuiz $exercise */
  152. foreach ($exerciseList as $exercise) {
  153. $question->questionData .= $exercise->getTitle();
  154. if ($exercise->getActive() == -1) {
  155. $question->questionData .= '- ('.get_lang('ExerciseDeleted').' #'.$exercise->getIid().') ';
  156. }
  157. $question->questionData .= '<br />';
  158. }
  159. }
  160. } else {
  161. // This question is orphan :(
  162. $question->questionData .= '&nbsp;'.get_lang('OrphanQuestion');
  163. }
  164. }
  165. ob_end_clean();
  166. }
  167. }
  168. }
  169. $formContent = $form->returnForm();
  170. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  171. switch ($action) {
  172. case 'delete':
  173. $questionId = isset($_REQUEST['questionId']) ? $_REQUEST['questionId'] : '';
  174. $courseId = isset($_REQUEST['courseId']) ? $_REQUEST['courseId'] : '';
  175. $courseInfo = api_get_course_info_by_id($courseId);
  176. $objQuestionTmp = Question::read($questionId, $courseInfo);
  177. if (!empty($objQuestionTmp)) {
  178. $result = $objQuestionTmp->delete();
  179. if ($result) {
  180. Display::addFlash(
  181. Display::return_message(
  182. get_lang('Deleted').' #'.$questionId.' - "'.$objQuestionTmp->question.'"'
  183. )
  184. );
  185. }
  186. }
  187. header("Location: $url");
  188. exit;
  189. break;
  190. }
  191. $tpl = new Template(get_lang('Questions'));
  192. $tpl->assign('form', $formContent);
  193. $tpl->assign('pagination', $pagination);
  194. $tpl->assign('pagination_length', $length);
  195. $tpl->assign('question_count', $questionCount);
  196. $layout = $tpl->get_template('admin/questions.tpl');
  197. $tpl->display($layout);