Hello, I'm trying to create a Moodle feature that will make possible to auto-generate a quiz by defining some rules, for example:
- Generate a quiz with:
RULE 1: 50 questions in total,
RULE 2: include 20 questions from category X
RULE 3: include 30 from category Y
RULE 4: exclude questions that are present in exam: 'EXAM TITLE'
After reading some Moodle plugin documentation, I've come the the conclusion that the best plugin type for this would be a 'quiz report' plugin. So the user will have to create an empty quiz and then use this plugin to insert the questions in the quiz following the rules defined.
Can anyone tell me a starting point to learn the functions/APIs needed to create a quiz programmatically?
Thanks a lot
Thanks Tim for your answer, let me explain a little bit better this use case:
Moodle in this case is being used as a digital platform to make an official certification exam. This is how we are using Moodle:
- Moodle is being used mainly as a digital exam platform
- This is an official certification exam orginized by a medical society
- All students (about 20 each year) that want to be certified, attends to a class the date/time they have to take the exam. The organizers give them iPads so they can connect to the exam platform and do the exam in person, but digitally
- All exam must be exactly the same, with the same numer of questions, this is why we cannot use the feature 'include a question randomly' from the quiz module
- The exam has been created in advance, following a set of rules (N questions from category X, M questions from category Y...) and has been review by the exam organizers
What we want to do is, instead of preparing the examen manually following the set of rules, we want Moodle do it for us, so not any person is envolved in the process of the exam preparation.
I don't see a way of doing this but by creating a plugin that includes the questions automatically in an empty exam...
Oh, I see, you want all the people in one session to get the same exam. Now I see why you need to code it.
Anyway, it does not really matter what type of plugin you create (admin tool, or local, might be best. I would not use a quiz report, because typically the quiz report is used for a quiz that already exists, and you want to create new quizzes using this tool).
Either admin tools or local plugins have a way to hook into the settings navigation, so you can get a link to access them where yu want it in the UI.
To built the quzzes, you probably just need quiz_add_instance from mod/quiz/lib.php and quiz_add_quiz_question from mod/quiz/locallib.php. The code in quiz_start_new_attempt (in locallib.php) will show you how to find questions from the question bank.
I hope that helps.
Thanks a lot Tim for you quick reply!
I was thinking about 'quiz report' plugin type because my first approach was:
- Manually create a course: '2018 Course Exam'
- Manually create/set a quiz inside the course: 'Exam 2018'
- Using the plugin (autoquiz) to populate the exam with questions following the defined rules
Other approach would be using the plugin from outside any course/quiz and creating the course, quiz and populate the quiz automatically using the plugin. This deliver a better user experience so I think I'd go for it.
Do you know what files I should have a look to create a course programatically?
Several ways you might be able to see roughly the code to create something in Moodle.
- Find the code behind the user-interface for that functionality, which in this case is probably course/edit.php. https://github.com/moodle/moodle/blob/master/course/edit.php#L162
- It is designed for testing, not use in real code, but there is lib/testing/generator/data_generator.php. https://github.com/moodle/moodle/blob/master/lib/testing/generator/data_generator.php#L363, or more specifically https://github.com/moodle/moodle/blob/master/lib/testing/generator/data_generator.php#L429
- Also, for some things, there are web services, for example, core_course_create_courses https://github.com/moodle/moodle/blob/master/lib/db/services.php#L337, so the implementation of that is https://github.com/moodle/moodle/blob/master/course/externallib.php#L749, which again points to the create_course function https://github.com/moodle/moodle/blob/master/course/externallib.php#L831
Thanks a lot Tim!