question_list_admin.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Code library for HotPotatoes integration.
  6. *
  7. * @package chamilo.exercise
  8. *
  9. * @author Olivier Brouckaert & Julio Montoya & Hubert Borderiou 21-10-2011 (Question by category)
  10. * QUESTION LIST ADMINISTRATION
  11. *
  12. * This script allows to manage the question list
  13. * It is included from the script admin.php
  14. */
  15. // deletes a question from the exercise (not from the data base)
  16. if ($deleteQuestion) {
  17. // if the question exists
  18. if ($objQuestionTmp = Question::read($deleteQuestion)) {
  19. $objQuestionTmp->delete($exerciseId);
  20. // if the question has been removed from the exercise
  21. if ($objExercise->removeFromList($deleteQuestion)) {
  22. $nbrQuestions--;
  23. }
  24. }
  25. // destruction of the Question object
  26. unset($objQuestionTmp);
  27. }
  28. $ajax_url = api_get_path(WEB_AJAX_PATH)."exercise.ajax.php?".api_get_cidreq()."&exercise_id=".intval($exerciseId);
  29. ?>
  30. <div id="dialog-confirm"
  31. title="<?php echo get_lang("ConfirmYourChoice"); ?>"
  32. style="display:none;">
  33. <p>
  34. <?php echo get_lang("AreYouSureToDelete"); ?>
  35. </p>
  36. </div>
  37. <script>
  38. $(function () {
  39. $("#dialog:ui-dialog").dialog("destroy");
  40. $("#dialog-confirm").dialog({
  41. autoOpen: false,
  42. show: "blind",
  43. resizable: false,
  44. height: 150,
  45. modal: false
  46. });
  47. $(".opener").click(function () {
  48. var targetUrl = $(this).attr("href");
  49. $("#dialog-confirm").dialog({
  50. modal: true,
  51. buttons: {
  52. "<?php echo get_lang("Yes"); ?>": function () {
  53. location.href = targetUrl;
  54. $(this).dialog("close");
  55. },
  56. "<?php echo get_lang("No"); ?>": function () {
  57. $(this).dialog("close");
  58. }
  59. }
  60. });
  61. $("#dialog-confirm").dialog("open");
  62. return false;
  63. });
  64. var stop = false;
  65. $("#question_list h3").click(function (event) {
  66. if (stop) {
  67. event.stopImmediatePropagation();
  68. event.preventDefault();
  69. stop = false;
  70. }
  71. });
  72. /* We can add links in the accordion header */
  73. $(".btn-actions .edition a.btn").click(function () {
  74. //Avoid the redirecto when selecting the delete button
  75. if (this.id.indexOf('delete') == -1) {
  76. newWind = window.open(this.href, "_self");
  77. newWind.focus();
  78. return false;
  79. }
  80. });
  81. $("#question_list").accordion({
  82. icons: null,
  83. heightStyle: "content",
  84. active: false, // all items closed by default
  85. collapsible: true,
  86. header: ".header_operations",
  87. beforeActivate: function (e, ui) {
  88. var data = ui.newHeader.data();
  89. if (typeof data === 'undefined') {
  90. return;
  91. }
  92. var exerciseId = data.exercise || 0,
  93. questionId = data.question || 0;
  94. if (!questionId || !exerciseId) {
  95. return;
  96. }
  97. var $pnlQuestion = $('#pnl-question-' + questionId);
  98. if ($pnlQuestion.html().trim().length) {
  99. return;
  100. }
  101. $pnlQuestion.html('<span class="fa fa-spinner fa-spin fa-3x fa-fw" aria-hidden="true"></span>');
  102. $.get('<?php echo api_get_path(WEB_AJAX_PATH); ?>exercise.ajax.php?<?php echo api_get_cidreq(); ?>', {
  103. a: 'show_question',
  104. exercise: exerciseId,
  105. question: questionId
  106. }, function (response) {
  107. $pnlQuestion.html(response)
  108. });
  109. }
  110. })
  111. .sortable({
  112. cursor: "move", // works?
  113. update: function (event, ui) {
  114. var order = $(this).sortable("serialize") + "&a=update_question_order&exercise_id=<?php echo $exerciseId; ?>";
  115. $.post("<?php echo $ajax_url; ?>", order, function (result) {
  116. $("#message").html(result);
  117. });
  118. },
  119. axis: "y",
  120. placeholder: "ui-state-highlight", //defines the yellow highlight
  121. handle: ".moved", //only the class "moved"
  122. stop: function () {
  123. stop = true;
  124. }
  125. });
  126. });
  127. </script>
  128. <?php
  129. //we filter the type of questions we can add
  130. Question::display_type_menu($objExercise);
  131. // Re sets the question list
  132. $objExercise->setQuestionList();
  133. echo '<div id="message"></div>';
  134. $token = Security::get_token();
  135. //deletes a session when using don't know question type (ugly fix)
  136. Session::erase('less_answer');
  137. // If we are in a test
  138. $inATest = isset($exerciseId) && $exerciseId > 0;
  139. if (!$inATest) {
  140. echo "<div class='alert alert-warning'>"
  141. .get_lang("ChoiceQuestionType")
  142. ."</div>";
  143. } else {
  144. echo '
  145. <div class="row hidden-xs">
  146. <div class="col-sm-5"><strong>'.get_lang('Questions').'</strong></div>
  147. <div class="col-sm-1 text-center"><strong>'.get_lang('Type').'</strong></div>
  148. <div class="col-sm-2"><strong>'.get_lang('Category').'</strong></div>
  149. <div class="col-sm-1 text-right"><strong>'.get_lang('Difficulty').'</strong></div>
  150. <div class="col-sm-1 text-right"><strong>'.get_lang('MaximumScore').'</strong></div>
  151. <div class="col-sm-2 text-right"><strong>'.get_lang('Actions').'</strong></div>
  152. </div>
  153. <div id="question_list">
  154. ';
  155. if ($nbrQuestions) {
  156. // Always getting list from DB
  157. //$questionList = $objExercise->selectQuestionList(true);
  158. // In the building exercise mode show question list ordered as is.
  159. $objExercise->setCategoriesGrouping(false);
  160. // Show exercises as in category settings
  161. //$questionList = $objExercise->getQuestionListWithMediasUncompressed();
  162. // In building mode show all questions not render by teacher order.
  163. $objExercise->questionSelectionType = EX_Q_SELECTION_ORDERED;
  164. // Get question list
  165. $questionList = $objExercise->selectQuestionList(true, true);
  166. $category_list = TestCategory::getListOfCategoriesNameForTest(
  167. $objExercise->id,
  168. false
  169. );
  170. if (is_array($questionList)) {
  171. foreach ($questionList as $id) {
  172. //To avoid warning messages
  173. if (!is_numeric($id)) {
  174. continue;
  175. }
  176. /** @var Question $objQuestionTmp */
  177. $objQuestionTmp = Question::read($id);
  178. $clone_link = Display::url(
  179. Display::return_icon(
  180. 'cd.png',
  181. get_lang('Copy'),
  182. [],
  183. ICON_SIZE_TINY
  184. ),
  185. api_get_self().'?'.api_get_cidreq().'&clone_question='.$id,
  186. ['class' => 'btn btn-default btn-sm']
  187. );
  188. $edit_link = ($objQuestionTmp->type == CALCULATED_ANSWER && $objQuestionTmp->isAnswered())
  189. ? Display::span(
  190. Display::return_icon(
  191. 'edit_na.png',
  192. get_lang('QuestionEditionNotAvailableBecauseItIsAlreadyAnsweredHoweverYouCanCopyItAndModifyTheCopy'),
  193. [],
  194. ICON_SIZE_TINY
  195. ),
  196. ['class' => 'btn btn-default btn-sm']
  197. )
  198. : Display::url(
  199. Display::return_icon(
  200. 'edit.png',
  201. get_lang('Modify'),
  202. [],
  203. ICON_SIZE_TINY
  204. ),
  205. api_get_self().'?'.api_get_cidreq().'&'
  206. .http_build_query([
  207. 'type' => $objQuestionTmp->selectType(),
  208. 'myid' => 1,
  209. 'editQuestion' => $id,
  210. ]),
  211. ['class' => 'btn btn-default btn-sm']
  212. );
  213. $delete_link = null;
  214. if ($objExercise->edit_exercise_in_lp == true) {
  215. $delete_link = Display::url(
  216. Display::return_icon(
  217. 'delete.png',
  218. get_lang('RemoveFromTest'),
  219. [],
  220. ICON_SIZE_TINY
  221. ),
  222. api_get_self().'?'.api_get_cidreq().'&'
  223. .http_build_query([
  224. 'exerciseId' => $exerciseId,
  225. 'deleteQuestion' => $id,
  226. ]),
  227. [
  228. 'id' => "delete_$id",
  229. 'class' => 'opener btn btn-default btn-sm',
  230. ]
  231. );
  232. }
  233. $btnActions = implode(
  234. PHP_EOL,
  235. [$edit_link, $clone_link, $delete_link]
  236. );
  237. $title = Security::remove_XSS($objQuestionTmp->selectTitle());
  238. $title = strip_tags($title);
  239. $move = Display::returnFontAwesomeIcon("arrows moved", 1, true);
  240. // Question name
  241. $questionName =
  242. '<a href="#" title = "'.Security::remove_XSS($title).'">
  243. '.$move.' '.cut($title, 42).'
  244. </a>';
  245. // Question type
  246. list($typeImg, $typeExpl) = $objQuestionTmp->get_type_icon_html();
  247. $questionType = Display::return_icon($typeImg, $typeExpl);
  248. // Question category
  249. $txtQuestionCat = Security::remove_XSS(
  250. TestCategory::getCategoryNameForQuestion($objQuestionTmp->id)
  251. );
  252. if (empty($txtQuestionCat)) {
  253. $txtQuestionCat = "-";
  254. }
  255. // Question level
  256. $txtQuestionLevel = $objQuestionTmp->level;
  257. if (empty($objQuestionTmp->level)) {
  258. $txtQuestionLevel = '-';
  259. }
  260. $questionLevel = $txtQuestionLevel;
  261. // Question score
  262. $questionScore = $objQuestionTmp->selectWeighting();
  263. echo '
  264. <div id="question_id_list_'.$id.'">
  265. <div class="header_operations" data-exercise="'.$objExercise->selectId().'"
  266. data-question="'.$id.'">
  267. <div class="row">
  268. <div class="question col-sm-5 col-xs-12">'
  269. .$questionName.'
  270. </div>
  271. <div class="type text-center col-sm-1 col-xs-12">
  272. <span class="visible-xs-inline">'.get_lang('Type').' </span>'
  273. .$questionType.'
  274. </div>
  275. <div class="category col-sm-2 col-xs-12" title="'.$txtQuestionCat.'">
  276. <span class="visible-xs-inline">'.get_lang('Category').' </span>'
  277. .cut($txtQuestionCat, 42).'
  278. </div>
  279. <div class="level text-right col-sm-1 col-xs-6">
  280. <span class="visible-xs-inline">'.get_lang('Difficulty').' </span>'
  281. .$questionLevel.'
  282. </div>
  283. <div class="score text-right col-sm-1 col-xs-6">
  284. <span class="visible-xs-inline">'.get_lang('Score').' </span>'
  285. .$questionScore.'
  286. </div>
  287. <div class="btn-actions text-right col-sm-2 col-xs-6">
  288. <div class="edition">'.$btnActions.'</div>
  289. </div>
  290. </div>
  291. </div>
  292. <div class="question-list-description-block" id="pnl-question-'.$id.'">
  293. </div>
  294. </div>
  295. ';
  296. unset($objQuestionTmp);
  297. }
  298. }
  299. }
  300. if (!$nbrQuestions) {
  301. echo Display::return_message(get_lang('NoQuestion'), 'warning');
  302. }
  303. echo '</div>'; //question list div
  304. }