I am looking to migrate quiz module data from d7 to d8. Except "node quiz results" & "node quiz result answers", I have migrated all using migration script. Is there any other way to migrate complete quiz data to d8, please suggest. Thank you!!

Issue fork quiz-3181926

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Devendra Mishra created an issue. See original summary.

dkmishra’s picture

Issue summary: View changes
dkmishra’s picture

Title: Quiz result migrate in D8 from D7 » Quiz result migration in D8 from D7
djdevin’s picture

There's currently no migration, I recommend instead of a migration script, you write migrations that can be used with the Drupal 8 migrate system: https://www.drupal.org/docs/drupal-apis/migrate-api/writing-migrations-f...

dkmishra’s picture

Hi @djdevin, thanks for the suggestion, I am trying to write migrations. Thank you!!

chris dart’s picture

My team has successfully built a migration for quiz questions. We have four migrations for the quiz, quiz_answers, quiz_multichoice, and quiz_relationship. These all rely on custom source plugins that query the following tables in the D7 site:

  • The main quiz migration relies on the D7 quiz_node_properties. That's the simplest migration as the database columns match almost 1:1. The source plugin extends DrupalSqlBase
  • The quiz_answers migration queries the D7 quiz_multichoice_answers table. But requires some magic to format the answer, feedback_if_chosen,feedback_if_not_chosen, and score_if_chosen properties. in the prepareRow() method on the migration source plugin. That plugin extends DrupalSqlBase
  • The quiz_multichoice migration uses a Source plugin that extends D7_node, so it doesn't extend the default node query, but does have a prepareRow method that has to get all the for multi choice properties array with a query to quiz_multichoice_properties table, an method to get the quiz properties using the quiz_node_properties table, and the answers array from quiz_multichoice_answers table.
  • The quiz_relationship migration uses a source plugin that extends DrupalSqlBase. It has a join of the D7 node table with a leftJoin on quiz_node_relationship.

I'd be happy to elaborate further if you need more guidance. Writing migrations is not easy, especially when you first get started. But once you get the hang of it, you begin to see how powerful, robust, and efficient it is.

djdevin’s picture

That seems really nice, could you submit a patch or merge request?

chris dart’s picture

I have written a quiz result migration as well. This doesn't work quite right yet. I haven't determined how to migrate which random questions for a give quiz result were associated with a quiz and which ones weren't. The result pages shows all possible answers instead of just the ones the user actually saw.
The parent migration is quiz_results. It relies on a custom source plugin that extends DrupalSqlBase and queries the D7 table quiz_node_results. It's a pretty simple 1:1 relationship and uses a migration_lookup for our quiz.quid migration (mentioned in the previous comment), and a migration_lookup for the uid based on a user migration.
The dependent quiz_results_answers migration also uses a source plugin that extends DrupalSqlBase. It has a more elaborate query that joins quiz_node_results_answers to quiz_multichoice_user_answers which is then joined to quiz_multichoice_user_answer_multi to get the answer_id

query =  $this->select('quiz_node_results_answers', 'ra')
      ->fields('ra', [
        'result_id',
        'question_nid',
        'question_vid',
        'is_correct',
        'is_skipped',
        'points_awarded',
        'answer_timestamp',
        'number',
        'is_doubtful',
      ])
      ->fields('multi',['answer_id'])
      ->condition('answer_timestamp', strtotime('-1 year'), '>')
      ->orderBy('ra.result_id', 'DESC');
    $query->join('quiz_multichoice_user_answers','user_answers','ra.result_id=user_answers.result_id');
    $query->join('quiz_multichoice_user_answer_multi','multi','user_answers.id = multi.user_answer_id');
    return $query;

I get all the answers, but the problem is that the quiz results page shows all the possible questions the user could have viewed instead of just the randomly selected ones they were actually offered. What is the error I'm making? What other table or value do I need to incorporate?

chris dart’s picture

Are you thinking of a quiz_migrate submodule? I'm not sure how to convert my work into a part of the quiz module proper. I can attempt to create a submodule with the plugins and the migrate_plus.migration.*.yml files in a /config directory in the submodule. Is that the best approach?

brooke_heaton’s picture

@chris-dart - I think a submodule with the configuration in a directory under /config/install would make sense. I'm currently needing to do a D7 to D8 migration and would be happy to help with this if you are able to push up a patch or Merge Request.

christian.wiedemann’s picture

@chris-dart could you just post your migration yaml files (Maybe simple as zip). That would help us extreme. Thanks!

chris dart’s picture

StatusFileSize
new12.08 KB

Attached is a "denatured" example of a quiz migration. There may be some elements that are assumed here (such as user migrations and other basic requirements before you start migrating). Some of the attached files would need to be in a module and others you can import directly (after editing) using the config import of your choice.

djdevin’s picture

Thanks! This could probably go directly into a module structured like https://git.drupalcode.org/project/course/-/tree/3.x/modules/course_migrate

By design, the templates should work as-is but are made to be copied into your own modules for any custom processing.

djdevin’s picture

Version: 8.x-5.x-dev » 6.x-dev
deepak goyal’s picture

Hi All, The quiz_relationship migration is not working anybody has any solution ?

arjenk made their first commit to this issue’s fork.

sch4lly made their first commit to this issue’s fork.

gennadiy’s picture

Hi guys,
Thank you for making this migration. Could someone please provide a bit of guidance as for how to use it?
1) what version of D7 quiz migration supports from, e.g. 7.x-5.7 or both, 7.x-4.x and 7.x-5.x;
2) will it migrate data to any version of D9 quiz, e.g. 6.x.x;
3) how to get ‎quiz_migrate‎.zip, e.g. download/composer or drush;
4) how to copy and activate;
5) where to look for errors if any.

Thank you!

ram4nd’s picture

You use it through https://www.drupal.org/project/migrate_upgrade
But the script is far from ready tough.

gennadiy’s picture

Thank you ram4nd,
I'm not sure about your note - from the last commits I see it is more of migration plugin (to be used with the Drupal 8/9 migrate system) than migration script.
Can't find how to download entire zipped last version of it (from git.drupalcode.org) and if there are any specific requirements for use.
I'm hoping I can just copy those files into quiz 6.0.0-alpha7 file structure and run web based migration: http://d9.website.com/upgrade

Thanks

gennadiy’s picture

How to use it?
More specifically:
1) what version of D7 quiz was installed that migration went successfully with? Was it quiz 7.x-5.7?
2) I assume that it should migrate data to any version of D9 quiz 6.x.x as it should have the same database schema;
3) for those who are not experienced with doing this kind of migrations how to do it exactly:
- where to get the latest version of plugin/script;
- how to use it exactly? what are the options and step-by-step instructions?
- where to look for errors if any.

We currently have quiz 7.x-4.0-rc11 installed with only used "Quiz Multichoice" and "True False." Migrating to D9.3.

Thank you!

somersoft’s picture

Whilst trying these changes, one of the migrations failed as this is the D7 table structure I am working with

MariaDB [d7]> describe quiz_multichoice_user_answer_multi;
+----------------+------------------+------+-----+---------+-------+
| Field          | Type             | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+-------+
| user_answer_id | int(10) unsigned | NO   | PRI | NULL    |       |
| answer_id      | int(10) unsigned | NO   | PRI | NULL    |       |
+----------------+------------------+------+-----+---------+-------+
2 rows in set (0.015 sec)

MariaDB [d7]> describe quiz_multichoice_user_answers;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| id               | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| result_answer_id | int(10) unsigned | YES  | UNI | NULL    |                |
| choice_order     | text             | YES  |     | NULL    |                |
+------------------+------------------+------+-----+---------+----------------+
3 rows in set (0.001 sec)

And in web/modules/contrib/quiz/modules/quiz_migrate/src/Plugin/migrate/source/QuizResultsAnswers.php there is the line
->condition('result_id', $result_id); in

  /**
   * @param \Drupal\migrate\Row $row
   *
   * @return bool
   * @throws \Exception
   */
  public function prepareRow(Row $row) {
    $result_id = $row->getSourceProperty('result_id');
    $query = $this->select('quiz_multichoice_user_answers', 'user_answers')
      ->fields('multi', ['answer_id'])
      ->condition('result_id', $result_id);
    $query->join('quiz_multichoice_user_answer_multi', 'multi', 'user_answers.id = multi.user_answer_id');
    $results = $query->execute()->fetchAllAssoc('answer_id');
    if (!empty($results)) {
      $result = reset($results);
      $row->setSourceProperty('answer_id', $result['answer_id']);
    }
    return parent::prepareRow($row);
  }

needs to be changed to

->condition('result_answer_id', $result_id);
arjenk’s picture

@somersoft: i agree, this should be changed. Although i think also some more changes to quiz_results_answers.yml are needed; not sure about the question_vid as a default value of 0 for vid will probably not find the question:

 question_vid:
    plugin: default_value
    default_value: 0
    source: '@question_id'

needs to be changed to

 question_vid:
    source: '@question_id'

And QuizResultsAnswers.php. For example remove this:

 ->condition('answer_timestamp', strtotime('2020-09-01'), '>')

And it needs support for other question types then multiple-choice.

arjenk’s picture

@gennadiy

I consider to add the following text to a README.md; You do not need the zip file, but you must get the module from this merge request somehow and follow the instructions below. Hope this helps. 7.x-4.0-rc11 is not tested by me, it is possible that the database is changed since then, perhaps a migration first to 7.x-5.7 is an option? Maybe someone else has experience here?

Quiz Drupal 7 -> Drupal 8/9 migration

This modules supports migration of quizzes/questions and its user
responses from Drupal 7 to Drupal 9. So far only quiz Drupal version
7.x-5.7 is tested.

The remainder of this readme uses drush, it should in theory be possible to
perform the migration with migration_ui, but drush is preferred.

This module uses core migrate,
[migrate_tools](https://drupal.org/project/migrate_tools) and the
[migrate_plus](https://drupal.org/project/migrate_plus) module. For
installations see the project pages, in short:

```
composer require drupal/migrate_tools
composer require drupal/migrate_plus
drush en migrate, migrate_drupal, migrate_tools, migrate_plus, quiz_migrate
```

After installation the migration config files needs to installed. They locate in the
migrations/ directory.

```
drush config-import --partial --source=/web/modules/contrib/quiz/modules/quiz_migrate/migrations
```

Note: when this is moved to config/install, this step wont be nessecary as they will be installed with the module.

Running the migration

Before running the migration you have to setup the migration source, see ...

Run a `drush migrate-status --group=quizzes` to get a status what will be imported.

```
drush migrate-import --group=quizzes
```

This should migrate all quizzes with its questions. To also import the quizresults use

```
drush migrate-import --group=quiz_results
```

-- And, as noted earlier, this migration is not ready for production yet.

gennadiy’s picture

@arjenk
Hi and thank you so much for providing these instructions, I will let you know if any issues arise.

gennadiy’s picture

@arjenk

> You do not need the zip file, but you must get the module from this merge request somehow

- was able to download the module one file at the time by this url:
https://git.drupalcode.org/issue/quiz-3181926/-/tree/1124067b86ed73805b7...

Then followed your instructions and got all those quizzes and questions migrated successfully.

Thank you so very much for your help!

P.S. I got errors while migrating and then found out db for D7 was pointing to the original one with quiz 7.x-4.0-rc11. After changing it to the one with upgraded quiz 7.x-5.7 everything went well.

UPDATE 1:
- the migrated quizzes/questions are not in place of the nodes from D7 with the same node IDs like with most migrations, but as an additional data available from /admin/quiz/quizzes instead of content menu, so the links to take quizzes are different and need to be replaced;
- also results are not migrated properly as arjenk was cautioning about.

UPDATE 2:
- after removing these extra records from D7 db:
# remove anonymous results:
DELETE FROM quiz_node_results WHERE uid = 0;
# remove unfinished results:
DELETE FROM quiz_node_results WHERE time_end IS NULL

and replacing line 49 in QuizResultsAnswers.php (was getting an error 1054 Unknown column "result_id" in "where clause"):
- ->condition('result_id', $result_id);
+ ->condition('result_answer_id', $result_id);

results were migrated successfully - same number of records in db and verified a few in both D7 and D9.

gennadiy’s picture

StatusFileSize
new22.91 KB

Here is a zipped module from @arjenk - see previous post. All those files together - you need to unzip it into your \web\modules\contrib\quiz\modules\, then follow instructions from #25.

arjenk’s picture

Status: Active » Needs review

This now works for me, would be great if more people could use their testset to test the migration process.

bohemier’s picture

StatusFileSize
new59.96 KB

Here is a patch with a rebase of the above MR with the current 6.x-dev

somersoft’s picture

Example JSON to use the 'plain diff' link from the issue fork to patch the quiz package in a composer.json file with the "cweagans/composer-patches" package installed.

"patches": {
    "drupal/quiz": {
      "Quiz result migration in D8 from D7 - 3181926": "https://git.drupalcode.org/project/quiz/-/merge_requests/9.diff"
    }
}
erwangel’s picture

Installation with #31 and migration steps from #25 worked for me going from D7 to D10.

However I got some errors with quiz_results_answers. I had 189 items in d7_quiz_result which were all migrated, but only 1516 items from the 1869 processed by quiz_results_answers were migrated. Perhaps the non migrated items are unfinished quizzes but there is nothing in migrate:messages that could help to debug. Also while rolling back quiz_results_answers , I got this error:

TypeError: Drupal\quiz\Entity\QuizResultAnswer::getQuizResult(): Return value must be of type Drupal\quiz\Entity\QuizResult, null returned in Drupal\quiz\Entity\QuizResultAnswer->getQuizResult() (line 26 of /path_to_site/web/modules/contrib/quiz/src/Entity/QuizResultAnswerEntityTrait.php) #0 /path_to_site/web/modules/contrib/quiz/src/Entity/QuizResultAnswer.php(162): Drupal\quiz\Entity\QuizResultAnswer->getQuizResult()

The other problem I got is with custom fields (I have a media_field in D7 quiz and question nodes) and usual drupal node fields like published y/n, author/date display, promoted, comments, etc or fields added by modules like metatag (essential for SEO), Any idea how to migrate these fields ? It is impossible to recreate all them as they are not accessible by "Re-use an existing field" and not available in the "Create a new field" options list. But perhaps this is not a migration problem but a limitation of the new quiz module concept. In such case, it is preferable to go the old way with quizzes and questions as nodes rather than trying to recreate a whole distinct entity.

johngonole’s picture

Just a general comment. Is drupal quiz going to have a migration built in when updating from Drupal 7 to Drupal 8,9,10 ?

When is Drupal quiz 6.0 series expected to get out of alpha and beta testing?

Drupal 7 is end of life soon. Is their an ETA for a production release?

smustgrave’s picture

Version: 6.x-dev » 7.0.x-dev
Status: Needs review » Needs work

Lets update for 7.0.x please

anaconda777’s picture

What is the process of migrating custom fields? Do I need to modify the quiz_migrate module files or just the migrate files?

somersoft’s picture

@anaconda777
If you need to migrate more fields in the fieldable content, then copy the migration configuration file, rename the file and id and add the fields. in the process mapping. The source plugin should add the registered fields to the row. For example code see Drupal\node\Plugin\migrate\source\d7\Node.
If not then copy the used source plugin class and extend the SQL query to add the field.
I have a module which has all the custom migration configuration files and plugins so it is a part of that.

smustgrave’s picture

Version: 7.0.x-dev » 8.0.x-dev
Status: Needs work » Postponed (maintainer needs more info)

Wanted to ask if this was still desired with D12 coming out later this year?

anaconda777’s picture

would be useful for me, if it would work with D11
this patch does not
https://www.drupal.org/files/issues/2023-06-29/3181926-quiz-migrate.patch