1

My company received a maintenance contract for a website developed in Drupal 7. The Drupal 7 website uses the Quiz contrib module, ver. 7.x-4.0-rc10. The module is used to implement a feature where authenticated users answer questions to tests.

When an authenticated user completes the test and views his result, the following notice errors are displayed on the results page,

  • Notice: Undefined index: nid in quiz_take_quiz() (line 2078 of C:\wamp64\www\........\modules\contrib\quiz\quiz.module).
  • Notice: Undefined index: vid in quiz_take_quiz() (line 2078 of C:\wamp64\www\........\modules\contrib\quiz\quiz.module).
  • Notice: Trying to get property 'type' of non-object in quiz_take_quiz() (line 2082 of C:\wamp64\www\........\modules\contrib\quiz\quiz.module).
  • Notice: Undefined index: in quiz_take_quiz() (line 2082 of C:\wamp64\www\........\modules\contrib\quiz\quiz.module).
  • Notice: Trying to get property 'is_valid' of non-object in quiz_take_quiz() (line 2084 of C:\wamp64\www\........\modules\contrib\quiz\quiz.module).

The Quiz module has a function, quiz_take_quiz($quiz).

As part of contract signed by my company, I have to resolve all notices. Therefore, what I intend to do is override this function and modify the exact line which is responsible for throwing the above notices (line no. 2078) and add a variable validation there.

However being a newbie to Drupal development I don't know where to start and what to do. I do know that modifying the code in a contrib module is not the best practice. I have hidden the path to the .module file with ........ to maintain privacy.

Can anyone please provide me the solution to this?

  • 1
    That's an out-of-date version of the quiz module, so before anything else, bring it up to date. – Alfred Armstrong Nov 18 at 14:20
  • 1
    If @AlfredArmstrong suggestion doesn't fix it. First you need to replicate the bug on a fresh Drupal install to make sure it's not just you, that there isn't some other code in your code base that is making the nid undefined. – No Sssweat Nov 18 at 14:23
  • @AlfredArmstrong Thank you very much for your reply. Updating the module to the latest stable version is not a viable option as it changes the UI of an image question in the quiz completely. Doing so will need a UI developer to rework on a lot of things i.e. rewriting the styles. Hence whatever needs to be done, has to be done in this version only. I'm sorry I forgot to add this in the question. :( – razor Nov 18 at 14:43
  • 1
    @razor trouble is now that if you raise an issue for the module (which is the first step in solving your problem the right way) it'll be closed by the maintainer as referring to an old version. But you should still follow that route, I think, making a patch and applying it. drupal.org/node/707484 and drupal.org/patch/apply – Alfred Armstrong Nov 18 at 15:44
1

After going through all of your replies and a few internal discussions in my company, I have decided to modify the Quiz module's code by creating and applying a patch, quiz_notice_messages_in_result_screen.patch.

As per the information provided by Alfred Armstrong's the said patch will be solely used for our company's internal purposes, so as to find a way around the Drupal's general practice of reporting a contrib module's issues and suggesting patches for the same.

Below are the contents of the quiz_notice_messages_in_result_screen.patch file.

diff --git a/modules/contrib/quiz/quiz.module b/modules/contrib/quiz/quiz.module
index dd2b88c..e551aac 100644
--- a/modules/contrib/quiz/quiz.module
+++ b/modules/contrib/quiz/quiz.module
@@ -2075,13 +2075,19 @@ function quiz_take_quiz($quiz) {
     $_SESSION['quiz_' . $quiz->nid]['quiz_questions'][0]['rid'] = $_SESSION['quiz_' . $quiz->nid]['result_id'];
     $_SESSION['quiz_' . $quiz->nid]['previous_quiz_questions'][] = $_SESSION['quiz_' . $quiz->nid]['quiz_questions'][0];
     $former_question_array = array_shift($_SESSION['quiz_' . $quiz->nid]['quiz_questions']);
+    if (empty($former_question_array['nid']) || empty($former_question_array['vid'])) {
+      $former_question_array['nid'] = $quiz->nid;
+      $former_question_array['vid'] = $quiz->vid;
+    }
     $former_question = node_load($former_question_array['nid'], $former_question_array['vid']);

     // Call hook_evaluate_question().
     $types = _quiz_get_question_types();
-    $module = $types[$former_question->type]['module'];
+    $module = empty($types[$former_question->type]['module']) ? '' : $types[$former_question->type]['module'];
     $result = module_invoke($module, 'evaluate_question', $former_question, $_SESSION['quiz_' . $quiz->nid]['result_id']);
-    $q_passed_validation = $result->is_valid;
+    if(isset($result->is_valid)) {
+      $q_passed_validation = $result->is_valid;
+    }
     $check_jump = TRUE;
     if ($q_passed_validation === TRUE) {
       quiz_store_question_result($quiz, $result, array('set_msg' => TRUE, 'question_data' => $former_question_array));

Thank you No Sssweat, Alfred Armstrong and Alan Dixon for your inputs.

0

I think the assumption in your answer "I do know that modifying the code in a contrib module is not the best practice" is incorrect in this case, even though it's true in general.

If all you want to do is fix the notices, then just fork the project and even pull in the upstream patches that fix these notices.

The issues you note are almost certainly easy to fix and just related to php versions.

Most importantly: your question demonstrates why clients should not be hiring non-Drupal shops to do maintenance on their Drupal sites.

  • A clarification to my last line: I don't want to sound too snarky, and I also don't mean it as a criticism of you the poster - posting the question here shows that you are not the problem. My criticism is of the requirements: resolving notices should be a symptom of what you really want, not the requirement itself. What you really want is maintainable, secure code. The path you are proposing (overriding functions in a custom module) gives you neither of those, even if it can resolve the notices. Learning how Drupal works is worth it, even if it's a steep climb to start with. – Alan Dixon Nov 18 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.