203203
204204 function toggleDropdown(event) {
205205 event.preventDefault();
206- var isDisplayed = dropdownMenu.style.display === 'block';
207- dropdownMenu.style.display = isDisplayed ? 'none' : 'block';
206+ var isDisplayed = dropdownMenu && dropdownMenu .style.display === 'block';
207+ if (dropdownMenu) dropdownMenu.style.display = isDisplayed ? 'none' : 'block';
208208 }
209209
210- actionButton.addEventListener('click', toggleDropdown);
211- document.addEventListener('click', function(event) {
212- if (!dropdownMenu.contains(event.target) && !actionButton.contains(event.target)) {
213- dropdownMenu.style.display = 'none';
214- }
215- });
210+ if (actionButton) {
211+ actionButton.addEventListener('click', toggleDropdown);
212+ document.addEventListener('click', function(event) {
213+ if (dropdownMenu && !dropdownMenu.contains(event.target) && !actionButton.contains(event.target)) {
214+ dropdownMenu.style.display = 'none';
215+ }
216+ });
217+ }
216218 });
217219
218220 function submit_form(obj) {
@@ -233,20 +235,20 @@ function confirm_your_choice() {
233235</script> " ;
234236
235237$ url = api_get_self ().'? ' .api_get_cidreq ().'& ' .http_build_query (
236- [
237- 'fromExercise ' => $ fromExercise ,
238- 'session_id ' => $ session_id ,
239- 'selected_course ' => $ selected_course ,
240- 'courseCategoryId ' => $ courseCategoryId ,
241- 'exerciseId ' => $ exerciseId ,
242- 'exerciseLevel ' => $ exerciseLevel ,
243- 'answerType ' => $ answerType ,
244- 'question_id ' => $ questionId ,
245- 'description ' => Security::remove_XSS ($ description ),
246- 'course_id_changed ' => $ course_id_changed ,
247- 'exercise_id_changed ' => $ exercise_id_changed ,
248- ]
249- );
238+ [
239+ 'fromExercise ' => $ fromExercise ,
240+ 'session_id ' => $ session_id ,
241+ 'selected_course ' => $ selected_course ,
242+ 'courseCategoryId ' => $ courseCategoryId ,
243+ 'exerciseId ' => $ exerciseId ,
244+ 'exerciseLevel ' => $ exerciseLevel ,
245+ 'answerType ' => $ answerType ,
246+ 'question_id ' => $ questionId ,
247+ 'description ' => Security::remove_XSS ($ description ),
248+ 'course_id_changed ' => $ course_id_changed ,
249+ 'exercise_id_changed ' => $ exercise_id_changed ,
250+ ]
251+ );
250252
251253if (isset ($ _REQUEST ['action ' ])) {
252254 switch ($ _REQUEST ['action ' ]) {
@@ -497,9 +499,9 @@ function confirm_your_choice() {
497499 ->setSelected ($ exerciseLevel );
498500$ form
499501 ->addSelect (
500- 'answerType ' ,
502+ 'answerType ' ,
501503 get_lang ('Answer type ' ),
502- $ new_question_list ,
504+ $ new_question_list ,
503505 ['onchange ' => 'submit_form(this); ' , 'id ' => 'answerType ' ]
504506 )
505507 ->setSelected ($ answerType );
@@ -819,29 +821,19 @@ function getQuestions(
819821*/
820822
821823if ($ fromExercise <= 0 ) {
822- // NOT IN A TEST - NOT IN THE COURSE
823- $ actionLabel = get_lang ('Re-use in current test ' );
824- $ actionIcon1 = get_lang ('Must be in a test ' );
825- $ actionIcon2 = '' ;
826- // We are not in this course, to messy if we link to the question in another course
827- $ questionTagA = 0 ;
828- if ($ selected_course == api_get_course_int_id ()) {
829- // NOT IN A TEST - IN THE COURSE
830- $ actionLabel = get_lang ('Edit ' );
831- $ actionIcon1 = 'edit ' ;
832- $ actionIcon2 = 'delete ' ;
833- // We are in the course, question title can be a link to the question edit page
834- $ questionTagA = 1 ;
835- }
824+ // OUTSIDE a test → show edit/delete column
825+ $ actionLabel = get_lang ('Actions ' );
826+ $ actionIcon1 = 'edit ' ;
827+ $ actionIcon2 = 'delete ' ;
828+ $ questionTagA = 1 ;
836829} else {
837- // IN A TEST - NOT IN THE COURSE
830+ // INSIDE a test → show reuse options
838831 $ actionLabel = get_lang ('Re-use a copy inside the current test ' );
839832 $ actionIcon1 = 'clone ' ;
840833 $ actionIcon2 = 'add ' ;
841834 $ questionTagA = 0 ;
842835
843836 if ($ selected_course == api_get_course_int_id ()) {
844- // IN A TEST - IN THE COURSE
845837 $ actionLabel = get_lang ('Re-use in current test ' );
846838 $ actionIcon1 = 'add ' ;
847839 $ actionIcon2 = '' ;
@@ -861,7 +853,8 @@ function getQuestions(
861853 continue ;
862854 }
863855 $ sessionId = isset ($ question ['session_id ' ]) ? $ question ['session_id ' ] : null ;
864- if (!$ objExercise ->hasQuestion ($ question ['iid ' ])) {
856+
857+ if ($ fromExercise > 0 && !$ objExercise ->hasQuestion ($ question ['iid ' ])) {
865858 $ row [] = Display::input (
866859 'checkbox ' ,
867860 'questions[] ' ,
@@ -885,34 +878,37 @@ function getQuestions(
885878 $ row [] = $ question_type ;
886879 $ row [] = TestCategory::getCategoryNameForQuestion ($ questionId , $ selected_course );
887880 $ row [] = $ question ['level ' ];
888- $ row [] = get_action_icon_for_question (
889- $ actionIcon1 ,
890- $ fromExercise ,
881+
882+ $ row [] =
883+ get_action_icon_for_question (
884+ $ actionIcon1 ,
885+ $ fromExercise ,
891886 $ questionId ,
892- $ question ['type ' ],
893- $ question ['question ' ],
894- $ selected_course ,
895- $ courseCategoryId ,
896- $ exerciseLevel ,
897- $ answerType ,
898- $ session_id ,
899- $ question ['exerciseId ' ],
900- $ objExercise
901- ).' ' .
902- get_action_icon_for_question (
903- $ actionIcon2 ,
904- $ fromExercise ,
905- $ questionId ,
906- $ question ['type ' ],
907- $ question ['question ' ],
908- $ selected_course ,
909- $ courseCategoryId ,
910- $ exerciseLevel ,
911- $ answerType ,
912- $ session_id ,
913- $ question ['exerciseId ' ],
914- $ objExercise
915- );
887+ $ question ['type ' ],
888+ $ question ['question ' ],
889+ $ selected_course ,
890+ $ courseCategoryId ,
891+ $ exerciseLevel ,
892+ $ answerType ,
893+ $ session_id ,
894+ $ question ['exerciseId ' ],
895+ $ objExercise
896+ ).' ' .
897+ get_action_icon_for_question (
898+ $ actionIcon2 ,
899+ $ fromExercise ,
900+ $ questionId ,
901+ $ question ['type ' ],
902+ $ question ['question ' ],
903+ $ selected_course ,
904+ $ courseCategoryId ,
905+ $ exerciseLevel ,
906+ $ answerType ,
907+ $ session_id ,
908+ $ question ['exerciseId ' ],
909+ $ objExercise
910+ );
911+
916912 $ data [] = $ row ;
917913 }
918914}
@@ -998,6 +994,7 @@ function getQuestions(
998994$ table ->display ();
999995echo '</form> ' ;
1000996
997+ // --- Bulk actions toolbar (only when we are inside a test) ------------------
1001998$ html = '<div class="btn-toolbar question-pool-table-actions"> ' ;
1002999$ html .= '<div class="btn-group"> ' ;
10031000$ html .= '<a
@@ -1011,28 +1008,31 @@ class="btn btn--plain"
10111008 onclick="javascript: setCheckbox(false, \'' .$ tableId .'\'); return false;">
10121009 ' .get_lang ('Unselect all ' ).'</a> ' ;
10131010$ html .= '</div> ' ;
1014- $ html .= '<div class="btn-group">
1015- <button class="btn btn--plain action-button"> ' .get_lang ('Actions ' ).'</button>
1016- <ul class="dropdown-menu" id="action-dropdown" style="display: none;"> ' ;
1017-
1018- $ actionLabel = get_lang ('Re-use a copy inside the current test ' );
1019- $ actions = ['clone ' => get_lang ('Re-use a copy inside the current test ' )];
1020- if ($ selected_course == api_get_course_int_id ()) {
1021- $ actions = ['reuse ' => get_lang ('Re-use in current test ' )];
1022- }
10231011
1024- foreach ($ actions as $ action => $ label ) {
1025- $ html .= '<li>
1026- <a
1027- data-action =" ' .$ action .'"
1028- href="#"
1029- onclick="javascript:action_click(this, \'' .$ tableId .'\');"> ' .
1030- $ label .'
1031- </a>
1032- </li> ' ;
1012+ if ($ fromExercise > 0 ) {
1013+ $ html .= '<div class="btn-group">
1014+ <button class="btn btn--plain action-button"> ' .get_lang ('Actions ' ).'</button>
1015+ <ul class="dropdown-menu" id="action-dropdown" style="display: none;"> ' ;
1016+
1017+ $ actions = ['clone ' => get_lang ('Re-use a copy inside the current test ' )];
1018+ if ($ selected_course == api_get_course_int_id ()) {
1019+ $ actions = ['reuse ' => get_lang ('Re-use in current test ' )];
1020+ }
1021+
1022+ foreach ($ actions as $ action => $ label ) {
1023+ $ html .= '<li>
1024+ <a
1025+ data-action =" ' .$ action .'"
1026+ href="#"
1027+ onclick="javascript:action_click(this, \'' .$ tableId .'\');"> ' .
1028+ $ label .'
1029+ </a>
1030+ </li> ' ;
1031+ }
1032+ $ html .= '</ul> ' ;
1033+ $ html .= '</div> ' ; //btn-group
10331034}
1034- $ html .= '</ul> ' ;
1035- $ html .= '</div> ' ; //btn-group
1035+
10361036$ html .= '</div> ' ; //toolbar
10371037
10381038echo $ html ;
@@ -1172,7 +1172,7 @@ function get_action_icon_for_question(
11721172 break ;
11731173 case 'add ' :
11741174 $ res = '- ' ;
1175- if (!$ myObjEx ->hasQuestion ($ in_questionid )) {
1175+ if ($ from_exercise > 0 && !$ myObjEx ->hasQuestion ($ in_questionid )) {
11761176 $ res = "<a href=' " .api_get_self ().'? ' .
11771177 api_get_cidreq ().$ getParams ."&recup= $ in_questionid&fromExercise= $ from_exercise'> " ;
11781178 $ res .= Display::getMdiIcon (ActionIcon::VIEW_DETAILS , 'ch-tool-icon ' , null , ICON_SIZE_SMALL , get_lang ('Use this question in the test as a link (not a copy) ' ));
@@ -1190,43 +1190,35 @@ function get_action_icon_for_question(
11901190
11911191 break ;
11921192 default :
1193- $ res = $ in_action ;
1194-
1193+ // When no action is expected, return empty string to keep layout clean
1194+ $ res = '' ;
11951195 break ;
11961196 }
11971197
11981198 return $ res ;
11991199}
12001200
12011201/**
1202- * @param int $questionId
1203- *
1204- * @return bool
1202+ * Checks whether a question is used by any quiz.
12051203 */
12061204function isQuestionInActiveQuiz ($ questionId )
12071205{
12081206 $ tblQuizRelQuestion = Database::get_course_table (TABLE_QUIZ_TEST_QUESTION );
12091207 $ tblQuiz = Database::get_course_table (TABLE_QUIZ_TEST );
12101208
12111209 $ questionId = (int ) $ questionId ;
1212-
12131210 if (empty ($ questionId )) {
12141211 return false ;
12151212 }
12161213
1217- $ result = Database::fetch_assoc (
1218- Database::query (
1219- "SELECT COUNT(qq.question_id) count
1220- FROM $ tblQuizRelQuestion qq
1221- INNER JOIN $ tblQuiz q
1222- ON qq.quiz_id = q.iid
1223- WHERE
1224- q.active = 1 AND
1225- qq.question_id = $ questionId "
1226- )
1227- );
1214+ $ sql = "SELECT COUNT(qq.question_id) AS count
1215+ FROM $ tblQuizRelQuestion qq
1216+ INNER JOIN $ tblQuiz q ON q.iid = qq.quiz_id
1217+ WHERE qq.question_id = $ questionId " ;
1218+
1219+ $ row = Database::fetch_assoc (Database::query ($ sql ));
12281220
1229- return $ result ['count ' ] > 0 ;
1221+ return ! empty ( $ row ) && ( int ) $ row ['count ' ] > 0 ;
12301222}
12311223
12321224/**
0 commit comments