questions.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. $criteria->orWhere($criteria->expr()->eq('description', $description));
  45. $criteria->orWhere($criteria->expr()->eq('description', '<p>'.$description.'</p>'));
  46. }
  47. if (!empty($title)) {
  48. $criteria->orWhere($criteria->expr()->contains('question', "%$title%"));
  49. }
  50. $questions = $repo->matching($criteria);
  51. if (empty($id)) {
  52. $id = '';
  53. }
  54. $params = [
  55. 'id' => $id,
  56. 'title' => Security::remove_XSS($title),
  57. 'description' => Security::remove_XSS($description),
  58. 'form_sent' => 1,
  59. ];
  60. $url = api_get_self().'?'.http_build_query($params);
  61. $form->setDefaults($params);
  62. $questionCount = count($questions);
  63. $paginator = new Paginator();
  64. $pagination = $paginator->paginate($questions, $page, $length);
  65. $pagination->setItemNumberPerPage($length);
  66. $pagination->setCurrentPageNumber($page);
  67. $pagination->renderer = function ($data) use ($url) {
  68. $render = '<ul class="pagination">';
  69. for ($i = 1; $i <= $data['pageCount']; $i++) {
  70. $page = (int) $i;
  71. $pageContent = '<li><a href="'.$url.'&page='.$page.'">'.$page.'</a></li>';
  72. if ($data['current'] == $page) {
  73. $pageContent = '<li class="active"><a href="#" >'.$page.'</a></li>';
  74. }
  75. $render .= $pageContent;
  76. }
  77. $render .= '</ul>';
  78. return $render;
  79. };
  80. if ($pagination) {
  81. $urlExercise = api_get_path(WEB_CODE_PATH).'exercise/admin.php?';
  82. $exerciseUrl = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?';
  83. /** @var CQuizQuestion $question */
  84. foreach ($pagination as $question) {
  85. $courseId = $question->getCId();
  86. $courseInfo = api_get_course_info_by_id($courseId);
  87. $courseCode = $courseInfo['code'];
  88. $question->courseCode = $courseCode;
  89. // Creating empty exercise
  90. $exercise = new Exercise($courseId);
  91. $questionObject = Question::read($question->getId(), $courseInfo);
  92. ob_start();
  93. ExerciseLib::showQuestion(
  94. $exercise,
  95. $question->getId(),
  96. false,
  97. null,
  98. null,
  99. false,
  100. true,
  101. false,
  102. true,
  103. true
  104. );
  105. $question->questionData = ob_get_contents();
  106. $exerciseData = '';
  107. $exerciseId = 0;
  108. if (!empty($questionObject->exerciseList)) {
  109. // Question exists in a valid exercise
  110. $exerciseData .= get_lang('Exercises').'<br />';
  111. foreach ($questionObject->exerciseList as $exerciseId) {
  112. $exercise = new Exercise();
  113. $exercise->course_id = $question->getCId();
  114. $exercise->read($exerciseId);
  115. $exerciseData .= $exercise->title.'&nbsp;';
  116. $exerciseData .= Display::url(
  117. Display::return_icon('edit.png', get_lang('Edit')),
  118. $urlExercise.http_build_query([
  119. 'cidReq' => $courseCode,
  120. 'id_session' => $exercise->sessionId,
  121. 'exerciseId' => $exerciseId,
  122. 'type' => $question->getType(),
  123. 'editQuestion' => $question->getId(),
  124. ])
  125. );
  126. }
  127. $question->questionData .= '<br />'.$exerciseData;
  128. } else {
  129. // Question exists but it's orphan or it belongs to a deleted exercise
  130. $question->questionData .= Display::url(
  131. Display::return_icon('edit.png', get_lang('Edit')),
  132. $urlExercise.http_build_query([
  133. 'cidReq' => $courseCode,
  134. 'id_session' => $exercise->sessionId,
  135. 'exerciseId' => $exerciseId,
  136. 'type' => $question->getType(),
  137. 'editQuestion' => $question->getId(),
  138. ])
  139. );
  140. $question->questionData .= Display::url(
  141. Display::return_icon('delete.png', get_lang('Delete')),
  142. $url.'&'.http_build_query([
  143. 'courseId' => $question->getCId(),
  144. 'questionId' => $question->getId(),
  145. 'action' => 'delete',
  146. ])
  147. ).'<br />';
  148. // This means the question is added in a deleted exercise
  149. if ($questionObject->getCountExercise() > 0) {
  150. $exerciseList = $questionObject->getExerciseListWhereQuestionExists();
  151. if (!empty($exerciseList)) {
  152. $question->questionData .= '<br />'.get_lang('Exercises').'<br />';
  153. /** @var CQuiz $exercise */
  154. foreach ($exerciseList as $exercise) {
  155. $question->questionData .= $exercise->getTitle();
  156. if ($exercise->getActive() == -1) {
  157. $question->questionData .= '- ('.get_lang('ExerciseDeleted').' #'.$exercise->getIid().') ';
  158. }
  159. $question->questionData .= '<br />';
  160. }
  161. }
  162. } else {
  163. // This question is orphan :(
  164. $question->questionData .= '&nbsp;'.get_lang('OrphanQuestion');
  165. }
  166. }
  167. ob_end_clean();
  168. }
  169. }
  170. }
  171. $formContent = $form->returnForm();
  172. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  173. switch ($action) {
  174. case 'delete':
  175. $questionId = isset($_REQUEST['questionId']) ? $_REQUEST['questionId'] : '';
  176. $courseId = isset($_REQUEST['courseId']) ? $_REQUEST['courseId'] : '';
  177. $courseInfo = api_get_course_info_by_id($courseId);
  178. $objQuestionTmp = Question::read($questionId, $courseInfo);
  179. if (!empty($objQuestionTmp)) {
  180. $result = $objQuestionTmp->delete();
  181. if ($result) {
  182. Display::addFlash(
  183. Display::return_message(
  184. get_lang('Deleted').' #'.$questionId.' - "'.$objQuestionTmp->question.'"'
  185. )
  186. );
  187. }
  188. }
  189. header("Location: $url");
  190. exit;
  191. break;
  192. }
  193. $tpl = new Template(get_lang('Questions'));
  194. $tpl->assign('form', $formContent);
  195. $tpl->assign('pagination', $pagination);
  196. $tpl->assign('pagination_length', $length);
  197. $tpl->assign('question_count', $questionCount);
  198. $layout = $tpl->get_template('admin/questions.tpl');
  199. $tpl->display($layout);