Reputation: 5362
I've checked the lib.php in the course folder but I don't understand what it's doing so I'm going to ask here.
I am trying to create a course with PHP and MySQL code, not with the Moodle API, by hooking up with the Moodle database.
So far, I've written a method to create a course by inserting values into the mdl_course table but nothing shows up in course homepage. It's in the database, it's enrollable and it's also visible but it doesn't show up.
I know course_sections and course_category is affected when a new course is created but how do calculate what values to put into each field?
What is modinfo in mdl_course? How do I calculate this?
How do I calculate sortorder in mdl_course? Right now I am finding the category of the where the course will be placed, then I find all the sortorders in that category, then I increment it by 1 as my new sortorder value. I'm not sure if that is correct.
Right now this is my method to create a course, obviously it is no where near complete:
public function createCourse()
{
//find category
//calculate sortorder i.e. search in mdl course for all course with category. select sortorder MAX then ++
require "/mysqli_connect.php";
$t = time();
$insert_q = "INSERT INTO mdl_course
(category, fullname, shortname, summary, startdate, maxbytes,
timecreated, newsitems, numsections, expirythreshold)
VALUES (30, 'Fullname', 'shortname', 'This is the summary', '$t', 268435456, '$t', 5, 10, 864000)";
$insert_r = mysqli_query($mysqli, $insert_q);
$insert_n = mysqli_affected_rows($mysqli);
//var_dump($insert_n);
if($insert_n == 1)
{
return true; //insert successful
}
else
{
return false;
}
}
Upvotes: 0
Views: 1752
Reputation: 4868
In my experience, Moodle is not the simplest or the most transparent system to graft your own code onto. An individual entity has many different table entries. For example, a lesson will appear in mdl_lessons, mdl_course_modules, mdl_grade items, and others. I would suggest using Moodle's own API for as much as possible. If you can't do that, you could try reverse-engineering it by adding a course through the standard interface, then checking your SQL log to see what table entries it made.
Regarding the bewildering mess that is modinfo, this post on the moodle forums says that it's automatically generated. This post explains that once a course's lessons and activities are all in place, you can call rebuild_course_cache($courseid)
to repopulate modinfo.
Upvotes: 4