Hi,

Using Quiz module and its Multichoice extension in a project in combination with the Flag module caused some Notices to appear.

These notices are coming from the getNodeProperties() method of the QuizQuestion class because the $row is actually FALSE instead of object

  /**
   * Getter function returning properties to be loaded when the node is loaded.
   *
   * @see load hook in quiz_question.module (quiz_question_load)
   *
   * @return array
   */
  public function getNodeProperties() {
    if (isset($this->nodeProperties)) {
      return $this->nodeProperties;
    }
    $result = db_query('SELECT *
      FROM {quiz_question_properties}
      WHERE nid = :nid AND vid = :vid', array(':nid' => $this->node->nid, ':vid' => $this->node->vid));
    $row = $result->fetch();
    $props['max_score'] = $row->max_score;
    $props['feedback']['value'] = $row->feedback;
    $props['feedback']['format'] = $row->feedback_format;
    $props['is_quiz_question'] = TRUE;
    $this->nodeProperties = $props;
    return $props;
  }

The specific lines are:

    $props['max_score'] = $row->max_score;
    $props['feedback']['value'] = $row->feedback;
    $props['feedback']['format'] = $row->feedback_format;

The reason for $row being FALSE is that the getNodeProperties() is called too early because of the Flag module. The flag module hooks on field_attach_insert, which is invoked on node_save.
When the Flag module hooks there it executes series of function ending at some point to entity_load() and this one is then ending to call getNodeProperties() before the node is actually saved. Which means there is no record in the quiz_question_properties table and therefore the $row is FALSE.

It will be really hard to make changes on the Flag module to fix their issue as it will require changes in some of their core functions, therefore I decided to make a quick patch for the Quiz module to just prevent the notices in cases like this.

It is coming in the next comment.

Regards,
Lachezar

Comments

lachezar.valchev’s picture

Status: Needs work » Needs review
StatusFileSize
new1.5 KB

Hi,

Here is the patch.

Regards,
Lachezar

captainack’s picture

I saw the same problem. Patch worked for me :)

djdevin’s picture

The patch works however I don't think this is the root of the issue. getNodeProperties() should never be called unless the node actually exists. We use Flag too so let me investigate this first.

captainack’s picture

PS in case you have any trouble reproducing this, in my case the problem arose with flags configured only on non-quiz/question-related content types. Not sure if it's necessary or not, but thought I'd mention it in case it helps.

djdevin’s picture

Version: 7.x-5.0-alpha10 » 7.x-5.x-dev
Assigned: lachezar.valchev » Unassigned
Status: Needs review » Fixed

There are many cases where this can happen and the patch appears to fix it. I triggered it the following ways:

1) Loading a quiz result with a deleted question
2) Deleting a question and a quiz result at the same time (race condition - properties are deleted before the node is deleted)
3) Accessing the question properties before being loaded (race condition - properties not loaded yet)

Patch seems fine. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.