Quellcode durchsuchen

Allow edit questions that are not inside an exercise see BT#15534

Julio Montoya vor 5 Jahren
Ursprung
Commit
f8d91f9c2d

+ 10 - 4
main/admin/questions.php

@@ -54,6 +54,9 @@ if ($formSent) {
 
     $questions = $repo->matching($criteria);
 
+    if (empty($id)) {
+        $id = '';
+    }
     $params = [
         'id' => $id,
         'title' => $title,
@@ -127,7 +130,6 @@ if ($formSent) {
                         $url.http_build_query([
                             'cidReq' => $courseCode,
                             'id_session' => $exercise->sessionId,
-                            'myid' => 1,
                             'exerciseId' => $exerciseId,
                             'type' => $question->getType(),
                             'editQuestion' => $question->getId(),
@@ -136,10 +138,14 @@ if ($formSent) {
                 }
                 $question->questionData .= '<br />'.$exerciseData;
             } else {
-                $question->questionData .= get_lang('Course').': '.Display::url(
-                    $courseInfo['name'],
-                    $exerciseUrl.http_build_query([
+                $question->questionData = Display::url(
+                    Display::return_icon('edit.png', get_lang('Edit')),
+                    $url.http_build_query([
                         'cidReq' => $courseCode,
+                        'id_session' => $exercise->sessionId,
+                        'exerciseId' => $exerciseId,
+                        'type' => $question->getType(),
+                        'editQuestion' => $question->getId(),
                     ])
                 );
             }

+ 2 - 8
main/exercise/ReadingComprehension.php

@@ -155,10 +155,8 @@ class ReadingComprehension extends UniqueAnswer
         }
 
         // hidden values
-        $my_id = isset($_REQUEST['myid']) ? intval($_REQUEST['myid']) : null;
-        $form->addElement('hidden', 'myid', $my_id);
         $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
-        $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null;
+        $isContent = isset($_REQUEST['isContent']) ? (int) $_REQUEST['isContent'] : null;
 
         // default values
         $defaults = [];
@@ -172,12 +170,8 @@ class ReadingComprehension extends UniqueAnswer
             $form->setDefaults($defaults);
         }
 
-        if (!empty($_REQUEST['myid'])) {
+        if (!isset($_GET['newQuestion']) || $isContent) {
             $form->setDefaults($defaults);
-        } else {
-            if ($isContent == 1) {
-                $form->setDefaults($defaults);
-            }
         }
     }
 

+ 3 - 3
main/exercise/admin.php

@@ -301,7 +301,7 @@ if (!$exerciseId && $nameTools != get_lang('ExerciseManagement')) {
 }
 
 // if the question is duplicated, disable the link of tool name
-if ($modifyIn == 'thisExercise') {
+if ($modifyIn === 'thisExercise') {
     if ($buttonBack) {
         $modifyIn = 'allExercises';
     }
@@ -330,12 +330,12 @@ $inATest = isset($exerciseId) && $exerciseId > 0;
 
 if ($inATest) {
     echo '<div class="actions">';
-    if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion']) || isset($_GET['myid'])) {
+    if (isset($_GET['hotspotadmin']) || isset($_GET['newQuestion'])) {
         echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/admin.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'">'.
             Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).'</a>';
     }
 
-    if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['myid']) && !isset($_GET['editQuestion'])) {
+    if (!isset($_GET['hotspotadmin']) && !isset($_GET['newQuestion']) && !isset($_GET['editQuestion'])) {
         echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">'.
             Display::return_icon('back.png', get_lang('BackToExercisesList'), '', ICON_SIZE_MEDIUM).'</a>';
     }

+ 21 - 7
main/exercise/exercise.class.php

@@ -1720,23 +1720,37 @@ class Exercise
 
     /**
      * Updates question position.
+     *
+     * @return bool
+     *
      */
     public function update_question_positions()
     {
         $table = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
-        //Fixes #3483 when updating order
-        $question_list = $this->selectQuestionList(true);
-        if (!empty($question_list)) {
-            foreach ($question_list as $position => $questionId) {
+        // Fixes #3483 when updating order
+        $questionList = $this->selectQuestionList(true);
+
+        $this->id = (int) $this->id;
+
+        if (empty($this->id)) {
+            return false;
+        }
+
+        if (!empty($questionList)) {
+            foreach ($questionList as $position => $questionId) {
+                $position = (int) $position;
+                $questionId = (int) $questionId;
                 $sql = "UPDATE $table SET
-                            question_order ='".intval($position)."'
+                            question_order ='".$position."'
                         WHERE
                             c_id = ".$this->course_id." AND
-                            question_id = ".intval($questionId)." AND
-                            exercice_id=".intval($this->id);
+                            question_id = ".$questionId." AND
+                            exercice_id=".$this->id;
                 Database::query($sql);
             }
         }
+
+        return true;
     }
 
     /**

+ 1 - 1
main/exercise/multiple_answer.class.php

@@ -188,7 +188,7 @@ class MultipleAnswer extends Question
      */
     public function processAnswersCreation($form, $exercise)
     {
-        $questionWeighting = $nbrGoodAnswers = 0;
+        $questionWeighting = 0;
         $objAnswer = new Answer($this->id);
         $nb_answers = $form->getSubmitValue('nb_answers');
 

+ 1 - 1
main/exercise/multiple_answer_combination.class.php

@@ -182,7 +182,7 @@ class MultipleAnswerCombination extends Question
      */
     public function processAnswersCreation($form, $exercise)
     {
-        $questionWeighting = $nbrGoodAnswers = 0;
+        $questionWeighting = 0;
         $objAnswer = new Answer($this->id);
         $nb_answers = $form->getSubmitValue('nb_answers');
 

+ 1 - 1
main/exercise/multiple_answer_true_false.class.php

@@ -239,7 +239,7 @@ class MultipleAnswerTrueFalse extends Question
      */
     public function processAnswersCreation($form, $exercise)
     {
-        $questionWeighting = $nbrGoodAnswers = 0;
+        $questionWeighting = 0;
         $objAnswer = new Answer($this->id);
         $nb_answers = $form->getSubmitValue('nb_answers');
         $course_id = api_get_course_int_id();

+ 29 - 33
main/exercise/question.class.php

@@ -952,6 +952,7 @@ abstract class Question
             if ($exercise->questionFeedbackEnabled) {
                 $params['feedback'] = $this->feedback;
             }
+
             Database::update(
                 $TBL_QUESTIONS,
                 $params,
@@ -974,24 +975,17 @@ abstract class Question
                     api_get_user_id()
                 );
             }
-            if (api_get_setting('search_enabled') == 'true') {
-                if ($exerciseId != 0) {
-                    $this->search_engine_edit($exerciseId);
-                } else {
-                    /**
-                     * actually there is *not* an user interface for
-                     * creating questions without a relation with an exercise.
-                     */
-                }
+            if (api_get_setting('search_enabled') === 'true') {
+                $this->search_engine_edit($exerciseId);
             }
         } else {
-            // creates a new question
+            // Creates a new question
             $sql = "SELECT max(position)
                     FROM $TBL_QUESTIONS as question,
                     $TBL_EXERCISE_QUESTION as test_question
                     WHERE
                         question.id = test_question.question_id AND
-                        test_question.exercice_id = ".intval($exerciseId)." AND
+                        test_question.exercice_id = ".$exerciseId." AND
                         question.c_id = $c_id AND
                         test_question.c_id = $c_id ";
             $result = Database::query($sql);
@@ -1087,33 +1081,33 @@ abstract class Question
                     }
                 }
 
-                if (api_get_setting('search_enabled') == 'true') {
-                    if ($exerciseId != 0) {
-                        $this->search_engine_edit($exerciseId, true);
-                    } else {
-                        /**
-                         * actually there is *not* an user interface for
-                         * creating questions without a relation with an exercise.
-                         */
-                    }
+                if (api_get_setting('search_enabled') === 'true') {
+                    $this->search_engine_edit($exerciseId, true);
                 }
             }
         }
 
         // if the question is created in an exercise
-        if ($exerciseId) {
+        if (!empty($exerciseId)) {
             // adds the exercise into the exercise list of this question
             $this->addToList($exerciseId, true);
         }
     }
 
+    /**
+     * @param int  $exerciseId
+     * @param bool $addQs
+     * @param bool $rmQs
+     */
     public function search_engine_edit(
         $exerciseId,
         $addQs = false,
         $rmQs = false
     ) {
         // update search engine and its values table if enabled
-        if (api_get_setting('search_enabled') == 'true' && extension_loaded('xapian')) {
+        if (!empty($exerciseId) && api_get_setting('search_enabled') == 'true' &&
+            extension_loaded('xapian')
+        ) {
             $course_id = api_get_course_id();
             // get search_did
             $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
@@ -1260,9 +1254,11 @@ abstract class Question
     public function addToList($exerciseId, $fromSave = false)
     {
         $exerciseRelQuestionTable = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
-        $id = $this->id;
+        $id = (int) $this->id;
+        $exerciseId = (int) $exerciseId;
+
         // checks if the exercise ID is not in the list
-        if (!in_array($exerciseId, $this->exerciseList)) {
+        if (!empty($exerciseId) && !in_array($exerciseId, $this->exerciseList)) {
             $this->exerciseList[] = $exerciseId;
             $courseId = isset($this->course['real_id']) ? $this->course['real_id'] : 0;
             $newExercise = new Exercise($courseId);
@@ -1270,7 +1266,7 @@ abstract class Question
             $count = $newExercise->getQuestionCount();
             $count++;
             $sql = "INSERT INTO $exerciseRelQuestionTable (c_id, question_id, exercice_id, question_order)
-                    VALUES ({$this->course['real_id']}, ".intval($id).", ".intval($exerciseId).", '$count')";
+                    VALUES ({$this->course['real_id']}, ".$id.", ".$exerciseId.", '$count')";
             Database::query($sql);
 
             // we do not want to reindex if we had just saved adnd indexed the question
@@ -1606,10 +1602,10 @@ abstract class Question
         $form->addRule('questionName', get_lang('GiveQuestion'), 'required');
 
         // default content
-        $isContent = isset($_REQUEST['isContent']) ? intval($_REQUEST['isContent']) : null;
+        $isContent = isset($_REQUEST['isContent']) ? (int) $_REQUEST['isContent'] : null;
 
         // Question type
-        $answerType = isset($_REQUEST['answerType']) ? intval($_REQUEST['answerType']) : null;
+        $answerType = isset($_REQUEST['answerType']) ? (int) $_REQUEST['answerType'] : null;
         $form->addElement('hidden', 'answerType', $answerType);
 
         // html editor
@@ -1632,10 +1628,6 @@ abstract class Question
             $editorConfig
         );
 
-        // hidden values
-        $my_id = isset($_REQUEST['myid']) ? intval($_REQUEST['myid']) : null;
-        $form->addElement('hidden', 'myid', $my_id);
-
         if ($this->type != MEDIA_QUESTION) {
             // Advanced parameters
             $select_level = self::get_default_levels();
@@ -1746,13 +1738,17 @@ abstract class Question
             $form->setDefaults($defaults);
         }
 
-        if (!empty($_REQUEST['myid'])) {
+        if (!isset($_GET['newQuestion']) || $isContent) {
+            $form->setDefaults($defaults);
+        }
+
+        /*if (!empty($_REQUEST['myid'])) {
             $form->setDefaults($defaults);
         } else {
             if ($isContent == 1) {
                 $form->setDefaults($defaults);
             }
-        }
+        }*/
     }
 
     /**

+ 9 - 3
main/exercise/question_admin.inc.php

@@ -13,10 +13,10 @@
  */
 if (isset($_GET['editQuestion'])) {
     $objQuestion = Question::read($_GET['editQuestion']);
-    $action = api_get_self().'?'.api_get_cidreq()."&myid=1&modifyQuestion=".$modifyQuestion."&editQuestion=".$objQuestion->id.'&page='.$page;
+    $action = api_get_self().'?'.api_get_cidreq().'&modifyQuestion='.$modifyQuestion.'&editQuestion='.$objQuestion->id.'&page='.$page;
 } else {
     $objQuestion = Question::getInstance($_REQUEST['answerType']);
-    $action = api_get_self().'?'.api_get_cidreq()."&modifyQuestion=".$modifyQuestion."&newQuestion=".$newQuestion;
+    $action = api_get_self().'?'.api_get_cidreq().'&modifyQuestion='.$modifyQuestion.'&newQuestion='.$newQuestion;
 }
 
 if (is_object($objQuestion)) {
@@ -50,7 +50,7 @@ if (is_object($objQuestion)) {
     $objQuestion->createAnswersForm($form);
 
     // this variable  $show_quiz_edition comes from admin.php blocks the exercise/quiz modifications
-    if ($objExercise->edit_exercise_in_lp == false) {
+    if (!empty($objExercise->id) && $objExercise->edit_exercise_in_lp == false) {
         $form->freeze();
     }
 
@@ -65,6 +65,12 @@ if (is_object($objQuestion)) {
             $objQuestion->type != HOT_SPOT_DELINEATION
         ) {
             if (isset($_GET['editQuestion'])) {
+                if (empty($exerciseId)) {
+                    Display::addFlash(Display::return_message(get_lang('ItemUpdated')));
+                    $url = 'admin.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'&editQuestion='.$objQuestion->id;
+                    echo '<script type="text/javascript">window.location.href="'.$url.'"</script>';
+                    exit;
+                }
                 echo '<script type="text/javascript">window.location.href="admin.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'&page='.$page.'&message=ItemUpdated"</script>';
             } else {
                 // New question

+ 0 - 1
main/exercise/question_list_admin.inc.php

@@ -254,7 +254,6 @@ if (!$inATest) {
                         api_get_self().'?'.api_get_cidreq().'&'
                             .http_build_query([
                                 'type' => $objQuestionTmp->selectType(),
-                                'myid' => 1,
                                 'editQuestion' => $id,
                                 'page' => $page,
                             ]),