I was just discussing this with Martin on Skype, during which we came up with an alternative option. Since I only just thought of this, we are not sure whether it actually solves all the problems, but let me describe it and you can all see what you think.
Just to recap, the problem we really want to solve are:
1. More flexible sharing options, so you can share questions between all the courses in a category, in addition to sharing with all the courses in a site and no sharing which you can do now.
2. Ability to have separate areas to store different types of question in a course, so they don't get mixed up with teacher-created questions. For example, student created questions, or questions specially made for the end of course exam, that only a few people are allowed to see.
3. A solution to bugs like MDL-6047
that are almost impossible to solve with the way that question sharing works now.
4. At the moment, there is one question bank per courses. To to check for access, you check capabilities at the course level, so the capability is associated with the course, not the question bank.
And the idea is this: At the moment, in the database, we have a link
mdl_question_categories -> mdl_course
and we change this to
mdl_question_categories -> mdl_context
(at least for module, course, coursecategory and site level contexts). Then, when you are editing a particular activity (e.g. quiz), you see all the questions from that context, and all parent contexts for which you have the moodle/question:usequestions capability (that is a new capability).
Module and course level questions can refer to resources in the course files area or the site files area. category and site level questions can only refer to the site files area.
By default, questions would be created in the course-level question bank (like now). It would be in cases like the ones mentioned in 2. that you would use module-level question bank.
To allow the sharing in 1., there would be a nice interface for moving questions from level to level.
If you moved a question that referred to course files up to category or site level, you would get a page that lists those resources, and offers you the chance to copy them to site files, or cancel. That deals with 3.
For 4. Capabilities would still be associated with a course or a module, not the question bank itself, but it is no longer hard-coded that that is course level.
Question banks would be backed up exactly when the corresponding thing is backed up. So when you backup a course, you get the course, category and site banks. And the banks for the activity modules you chose include in the backup that have their own banks.
On restore, we would keep the existing code that only restores what is necessary, and does not create duplicates, by using globally unique ids on categories and questions.
Now, does that actually work? what have we overlooked?