I am trying to recreate the whole process of quiz creation. Hence, the user will go through all the steps starting from giving a title to the quiz, its description up to adding questions from existing question bank available in the moodle database. I tried executing an insert query but I have to hard-code lot many values in the tables. Does anybody have any idea how I could achieve this?
Also could anybody tell me which PHP files are taking care of database insertion of the quiz and other information related to it i.e., the quiz slot, section, course module, etc.
Basically the process of the quiz creation wizard will be different but the database will remain as it is. Also the questions that will be added into the quiz will come from the database.
I found this the most useful page:
and subsequently looking in detail through the code, which is well-documented.
The question preview.php page is relatively easy to follow in its workings.
Hard-coding the tables? Not a recommended strategy IMO.
Hello Richard Sir,
Thanks for the reference, but the link that you provided talks more about quiz attempts and displaying the questions. What I want is a simple way to create a quiz inside moodle, but by using another quiz creation process (UI) that I created. Is there any easy way to add quiz data into the database?
I came across the modedit.php page in the course directory in which there is a snippet for adding the quiz into database. But it is not using a function, so to use it outside moodle won't be possible. Is there any API for creating/adding quiz to the database and generate the quiz url??
Why not have your creation process produce the questions in Moodle XML format for import into a question bank?
This doesn't help you create the quiz though, only the questions. You would still have to add the questions to a quiz within Moodle. I don't know about other APIs for creating/adding quiz, sorry.
Because different question types write to different tables in different ways (though with some commonality), I do not see how you could have a generic database call function for creating a question. However as Richard says it would be possible to pass in XML to the existing code to create questions.
I'll explain in detail what I am trying to achieve.
Firstly, I am not trying to create questions outside moodle. I already have questions in my moodle database. Also I am focusing only on multichoice type questions. The process of quiz creation is what I am trying to simplify. The way moodle does it, for some people, is a little complicated and difficult to understand. So I have created a wizard (a simple html, js and php application) outside moodle and fetching the courses, their categories and the questions from the moodle database instance that I have on my dev machine. I have all the data now. I am accepting the quiz title, description, start date, end date, time from the user. The course for which the quiz is being created and the questions are coming from my moodle database.
Now I am selecting the questions that I want to add to my quiz. After selecting questions, all I want to do is create the quiz in moodle and get the quiz URL. For that I will have to create an entry in my moodle database.
From what I have researched, there are multiple tables getting modified while creating quiz (for one course, only multichoice questions). They are mdl_quiz, mdl_quiz_slots, mdl_quiz_section. So is it possible via php to make these entries in the database and get my quiz up and running on the moodle? If it is how do I do it. Because in the table mdl_quiz table, there are large number of fields such as introformat, overduehandling, preferredbehaviour, and review-xxx fields for which I don't know what data to insert? So I am looking for some way to just get the quiz created in moodle.
Though the quiz/question type data structure are one of the better documented areas of Moodle. There has been considerable thought into using questions outside the quiz, but not on creating them.
For the quiz itself I would install a blank instance of Moodle then create a quiz and see what tables were updated with what values, then document that (and post it in the Moodle.org wiki ....
Because you are restricting yourself to Multiple choice your task is easier than if you were going for generic question creation.
If I were thinking of simplifying MC question creation I would consider creating a new question type, I would sub class the existing code and give it a name like Easy MultiChoice. Then I would gtive it a paged interface, like the backup whereby you could jump forwards or backwards through the process if there were acceptable defaults. I I would do things like default to the first option having 100% of the score and set the shuffle the options to be true. Checkout anything Tim Hunt has said about these things over the years as he is the total Quiz Meister Guru.
That is exactly what I did at first. I made a list of tables that are getting modified when I create a quiz. But there are obviously relationships between these tables which got complex for me and I couldn't make out which table to add data to first. So I got stuck there. Then there were certain fields which can't be left blank and some of the fields' default data didn't match the data with other quizzes. So then I got stuck there.
Thanks for the suggestion anyway, I will check what Tim has said!
If you are presenting users with a simplified user-interface, then probably there will be a lot of settings that can be changed on the standard Quiz settings form, which you just want to fix the value of. Therefore, you will have some hard-coded values.
I think that for creating the quiz, and adding questions, you should not write directly to the database. In the quiz code there are standard functions you can use. To see what they are, the easiest thing might be to look at the code that creates quizzes. For example add_moduleinfo in course/modlib.php and quiz_add_quiz_question in mod/quiz/locallib.php.
Exactly! While going through the database and trying to add data directly into the table, I had to hard-code values to get my results. That didn't work as I wanted to.
Regarding the functions you suggested I should use, thanks for that. I think those functions will help me with what I am working on. The problem I am now facing is whenever I import any file from moodle in my project, the page goes blank.
I imported the modlib.php file in my php file step2.php. After I imported the page goes blank. When I comment the require/include function, it comes back.
The wizard that I have created for quiz creation is a separate directory with its own libraries.
So how do I use the functions from the moodle files? Is there any way, other than importing the files without require or include?
I require_once the moodle/config.php file, but since my application is a separate application (outside moodle) it displayed a message saying the config.php file can't be used outside localhost/moodle directory and redirected me to the moodle homepage.
It it okay, or rather is it advisable to put your php app inside the moodle directory and then use the moodle inbuilt files?
Could you help me out with the $DB variable?
I need to use it in my application (which is outside moodle) to get the course object, the way it is retrieved in moodle/mod/quiz/edit.php.
I didn't get how $DB is instantiated.
I used the insert_record function to put data into quiz and quiz_sections table. There's a function quiz_add_instance which adds data to the tables quiz and quiz_section and returns the quiz_id. But this functionn calls another function quiz_after_add_or_update. In this function there is a variable $cmid. What is the use of cmid and how do I fetch/generate it to proceed further?
I already went through the link, but the thing I could not understand is the relation between section field with the course_module table.
The only 'section' field I am aware of is in mdl_course_sections.
mdl_course_sections has several fields:
- id - the unique id of the section (i.e. section 2 in two different courses will have a different id here)
- course - the course the section belongs to
- section - the section position within the course (0, 1, 2, 3, etc. for each course) (i.e. if there is a 2nd section in multiple courses, then all of them will have a 2 here).
- sequence - ordered, comma-separated list of ids for the mdl_course_modules table, the activities that are present in that course section
I could finally get all my required data into the database and created a quiz.
The tables I modified are:
quiz, quiz_sections, course_modules, course_sections and quiz_slots
Now since all the tables have data and I got a quiz id from using the DML functions provided by moodle, the quiz should show up but I am still not able to see the quiz on my moodle dashboard. Is there anything left to do now? Is there any other table that I might be missing out or anything that I haven't done? Really need some assistance now.
I'm trying to create a quiz programmatically as you are doing.
I've used the function quiz_add_instance included in mod/quiz/lib.php but still don't see the quiz in Moodle after having created it.
Did you come up with a solution?
Hi, I reply myself...
Finally I was able to do it!
I created a quiz programmatically and I'm able to see it on Moodle.
You just need to use this function (Tim mentioned it) properly:
$quiz = add_moduleinfo($quiz, $course, null)It is included in