Moodle 3.4. Quiz creation process in PHP

Re: Moodle 3.4. Quiz creation process in PHP

by Tim Hunt -
Number of replies: 9
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
In reply to Tim Hunt

Re: Moodle 3.4. Quiz creation process in PHP

by Mohanish Nagarkar -
Thanks!

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?

In reply to Mohanish Nagarkar

Re: Moodle 3.4. Quiz creation process in PHP

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Take a look at the docs on course modules - https://docs.moodle.org/dev/Course_module


In reply to Davo Smith

Re: Moodle 3.4. Quiz creation process in PHP

by Mohanish Nagarkar -

Hi Davo,

I already went through the link, but the thing I could not understand is the relation between section field with the course_module table.

In reply to Mohanish Nagarkar

Re: Moodle 3.4. Quiz creation process in PHP

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

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


In reply to Tim Hunt

Re: Moodle 3.4. Quiz creation process in PHP

by Mohanish Nagarkar -
Hi again Tim,

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.


Regards,

Mohanish

In reply to Mohanish Nagarkar

Re: Moodle 3.4. Quiz creation process in PHP

by Alexis HF -

Hello Mohanish,

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?

Thanks

In reply to Alexis HF

Re: Moodle 3.4. Quiz creation process in PHP

by Alexis HF -

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 

https://github.com/moodle/moodle/blob/master/course/modlib.php#L44


Average of ratings: Useful (1)
In reply to Alexis HF

Re: Moodle 3.4. Quiz creation process in PHP

by dipak kumar Burnwal -

Hi Alexis,

If possible can you please share the script that you have created for creating quizes.

Thanks, Dipak