I'm trying to write a game module that will allow questions from the question bank to be added and then played with. I'd like to be able to implement a question add feature similar to the quiz module where teachers pick and choose the questions for the quiz. I'd like to be able to pass a list of selected question ids to the game from where the questions can be rendered. Can someone with experience with the quiz module give me general guidance on how to reproduce just that part of the quiz for another module?
I would not necessarily copy how the quiz does it. That bit was coded quite a long time ago, and so is not done in a way that is currently recommended.
To let the use pick questions, you need to show a view of the question bank. The quiz does that with these classes https://github.com/moodle/moodle/tree/master/mod/quiz/classes/question/bank, and you can search to see where they are used.
Then you need to store the list of question ids somewhere. The quiz does that in the quiz_slots table.
P.S. it is not what you are asking here, but at some point you will probably need this docs page https://docs.moodle.org/dev/Using_the_question_engine_from_module at some time.
I am infact starting with the quizgame but the game uses a mod_form to select a question category and pulls in all questions within that category. I want teachers to pick the questions from possibly multiple categories. When you say "I would not necessarily copy how the quiz does it.", I'm not sure I follow. Based on your input I see that edit.php basically calls the classes you mention. I haven't quite identified where the quiz_slots table gets updated with the selected questions, but I'm sure it's there. So, if I replicate edit.php in the mod_quizgame, I should be able to get the same experience right? When you say don't copy how the quiz does it, I'm sure sure what you are cautioning me about. ( I'm going to heed to your caution. Just trying to clarify a bit more! )
The code in edit.php, edit_rest.php, classes/output/edit_renderer.php and classes/structure.php is not a good example of how to build Moodle code. For example
- These days, better to use templates to generate the display of which questions have been selected, rather than putting lots of complex code in a renderer. Also, the acutual HTML we output is pretty horrible. I am sure you can display the info you need on screen with much simpler HTML.
- The general design of the quiz_slots table. You will need a similar table adapted to your needs.
- Acutally, the idea of a class like structure.php to implement the DB updates required by the external API is a good one. I am just not sure if the specific code there is nice.
- The general way the quiz shows a view of the question bank in a pop-up (mod/quiz/amd/src/add_question_modal_launcher.js, mod/quiz/classes/question/bank/custom_view.php is probably OK.
I get your points. Not sure if I'm equipped enough to be able to code this though. I am not experienced enough to be able to pull this off. I'll see if I can find someone to help with this. Are you working on updating these for the quiz?
If you have the resources, there are Moodle Partners who can do custom development for you.