Create courses using API

Create courses using API

by daniel parejo -
Number of replies: 4

Hello, how are you?

I'm using the Moodle API to create courses in this way. To do this, I created an external service with the core_course_create_courses function enabled, and I granted permission to my user, everything is correct in that regard.

I make the API call from Postman like any other API calls I've done before that have worked. The endpoint is: https://my.domain/webservice/rest/server.php

As parameters, I pass my wstoken and the wsfunction, which is core_course_create_courses. Then, in the body, using form-data, I pass the values of my course as follows:

courses[0][fullname] -> Course created by API 

courses[0][shortname] -> cursoapi 

courses[0][categoryid] -> 1

I pass these values because they are the only mandatory ones according to the documentation. When I make this call, I get the following response:

{"eventname":"\\core\\event\\course_created","component":"core","action":"created","target":"course","objecttable":"course","objectid":"48","crud":"c","edulevel":1,"contextid":109,"contextlevel":50,"contextinstanceid":"48","userid":"2","courseid":"48","relateduserid":null,"anonymous":0,"other":{"shortname":"cursoapi","fullname":"Course created by API"},"timecreated":1700126604}

This response is already very different from all the responses I have received from other calls I have made (for example, I had to insert users, and it worked correctly).

However, I don't see any errors in the response, and the call gives me a status of 200 OK, so I assume the call is correct. But when I go to check if the course has been created in the interface, I cannot find this course. I have checked the logs, but there is nothing apart from the log of having called this service.

I have been investigating for a long time, and I cannot find anything similar to this, and I don't know why the course is not being created correctly. I have also checked the database, and there is no record with the data that I have assigned.

Thank you very much for your help.

(PD: I am using Moodle 4.2)

Average of ratings: -
In reply to daniel parejo

Re: Create courses using API

by Rémi Chaussenot -
Hello Daniel,

I don't see anything wrong in what you say.

I do not use Postman, but instead a small php interface using curl (on moodle 3.11).

Is it possible to provide (in PM) a token/url to test with my script ?

Thanks
In reply to Rémi Chaussenot

Re: Create courses using API

by daniel parejo -
Hi Remy,

Thanks for your answer. Sadly i am not able to provide any url since its a project restricted by ip...

However thank you so much for answering.
In reply to daniel parejo

Re: Create courses using API

by Rémi Chaussenot -
Too bad!

The answer you get looks really like the main log of moodle.
Which is obviously, not the type of response we usually get from API.

Sorry i can't help more,

R.
In reply to daniel parejo

Re: Create courses using API

by daniel parejo -
Hi everyone,

As i keep trying to solve this, i look at the server logs where i have this moodle and saw this error:

Got error 'PHP message: Potential coding error - active database transaction detected during request shutdown:\n* line 261 of /lib/dml/moodle_read_slave_trait.php: call to moodle_database->start_delegated_transaction()\n* line 858 of /course/externallib.php: call to mysqli_native_moodle_database->start_delegated_transaction()\n* line 1499 of /webservice/lib.php: call to core_course_external::create_courses()\n* line 1345 of /webservice/lib.php: call to webservice_base_server->execute()\n* line 44 of /webservice/rest/server.php: call to webservice_base_server->run()\n'

So i go to the first file where the error is mentioned and comment one line:

/**
* On DBs that support it, switch to transaction mode and begin a transaction
* @return moodle_transaction
*/
public function start_delegated_transaction() {
$this->set_dbhwrite();
//return parent::start_delegated_transaction();
}

First of all, yes, my database support transactions, it is MariaDb and all the tables are in InnoDB format.

As seen in the code, if i comment the return line, the course is created correctly, but this code is core so i cant comment this, i hope this info help someone to help me find the solution.

Thanks