How to reproduce this problem (see http://moodle.org/mod/forum/discuss.php?d=61665#p351718) :
1. Create a course (C1).
2. Create a quiz (Z1) in C1.
3. Create a question (Q1) in Default category and add it to Z1.
4. Publish Q1.
5. Create a course (C2).
6. Create a quiz (Z2) in C2.
7. Add Q1 to Z2.
8. Delete C2.
9. View Z1. It will have been destroyed.
I've been studying this problem for several hours and it seems to me that there is a problem with the data model for quizzes and questions which causes this bug. Of course, I might not have an accurate picture of how the data model works. The use of comma separated question ids in the questions column of the quiz table implies to me that quiz/question relationships are maintained at the application layer instead of in the data model. The quiz_question_instances table implies by name and indicates in documentation (http://docs.moodle.org/en/Quiz_database_structure#quiz_question_instances) that it can be used to link questions to quizzes, but my experimentation has led me to conclude that it is the comma separated values in quiz.questions that define the relationship. If that is the case, I would humbly suggest that we reorganize the data model for quizzes and questions to facilitate relational integrity preservation at the database level, and allow for more efficient queries of quiz/question data. At least we could let quiz_question_instances define the relationships instead of quiz.questions. This would require a substantial amount of code changes, but I think it would be well worth it.