How to edit a section programmatically?

How to edit a section programmatically?

by Pat Sej -
Number of replies: 8
Hello everyone!

After a lot of research, I come to you with this simple (I thought) question. How to modify an existing section programmatically (description among others) in php or shell through moodle's API?

I've succeeded to edit a section name by calling a Moodle's web service core_update_inplace_editable.

I tried to change the itemtype to target the "sectionsummary" (or "summary" or "sectiondescription") but it didn't work.
Without a proper documentation for the values it is just guess work...

Would someone know how to do that?
Can Moosh be of any help?

I've came across a plugin that claims to add web services functions: https://moodle.org/plugins/webservice_restful.
I'm reluctant to use it for two reasons.
As it's not part of the core, I don't know if it will be maintained in the future. And it may bring new vulnerabilities.
Have you used it?

Thanks in advance for your help!

Cheers,
Patrice

--------
Here is an example of a shell script using Curl to do edit a section name.

func="core_update_inplace_editable"
params="component=format_topics" # for courses in "topics" format
params="$params&itemtype=sectionname"
params="$params&itemid=3" # the section id returned by the core_course_get_contents function
params="$params&value=NewValue"
curl "$srvUrl/webservice/rest/server.php?wstoken=$token&wsfunction=$func&$params&moodlewsrestformat=json"


Average of ratings: -
In reply to Pat Sej

Re: How to edit a section programmatically?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you're relying on the Web Service API, it doesn't look like it currently possible to edit a section summary using web service function. The closest is core_course_edit_section, but currently that only "Performs an action on course section (change visibility, set marker, delete)".

If you have the option of writing code that runs inside Moodle, you can call the course_update_section() function to programmatically edit a section. You could write a plugin that defines a new Web Service function to do this.

Average of ratings: Useful (1)
In reply to Mark Johnson

Re: How to edit a section programmatically?

by Pat Sej -
Hey Mark,

Thank you very much for your answer.

I'm surprised that it's not implemented. That would be great to know the limitations of the API directly from the documentation. Would avoid wasting hours look for that mixed

Where can I find examples on how to create this type of Web Service myself?
Are there existing projects I can inspire me from?

Thx wink
In reply to Pat Sej

Re: How to edit a section programmatically?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Pat, There is a list of implemented Web Service API functions on the wiki. To expand the API, there is documentation covering contributing a change to core Moodle or adding a web service function to your plugin. It's probably best to start off adding it to your plugin, then once you've got it right you could consider contributing it to core if you think its more generally useful.

For examples, look for plugins with a db/services.php and classes/external.php file. These are the files where web service functions are defined.

Average of ratings: Useful (2)
In reply to Mark Johnson

Re: How to edit a section programmatically?

by Pat Sej -
Hi Mark,

Thanks for your guidance!

Cheers wink
Patrice
In reply to Pat Sej

Re: How to edit a section programmatically?

by corvus albus -
Hi Patrice,

just have a look at https://github.com/corvus-albus/moodle-local_wsmanagesections. I have written a plugin with rest-api functions, that lets you manage sections. Renaming is one option, setting section format options another. In addition you can create and move sections.
The plugin is registered at https://moodle.org/plugins but not yet approved.
It is not exatly what you asked for: Changing the section description isn't part of it. But renaming should be much easier than with core_update_inplace_editable.

Cheers,
corvus albus
Average of ratings: Useful (1)
In reply to corvus albus

Re: How to edit a section programmatically?

by corvus albus -
Hi Patrice,

your problem description was the starting point to thing about the plugin again. It is now possible to update the following settings:
name, summary, summaryformat, visibility, highlight, and section format options.
After all editing the summary is possible with the plugin (https://github.com/corvus-albus/moodle-local_wsmanagesections).

Cheers,
corvus albus
Average of ratings: Useful (2)
In reply to corvus albus

Re: How to edit a section programmatically?

by Pat Sej -
Hi Corvus Albus,

It's quite interesting. Thanks you very much!
Does it handle images?

How difficult would it be to code the more web services? For instance if we want to create / modify a Page activity (or even an H5P) ?

Cheers,
Patrice
In reply to Pat Sej

Re: How to edit a section programmatically?

by corvus albus -
Hi Patrice,

it's not clear to me, what you mean with "handling images".
It does allow embedding images in the sections summary. If you have "summaryformat=1", which means html-format, then you can place an image into the summary by <img>-tag. For example:
'summary' = 'Section 3  <img src="https://moodle.org/theme/image.php/moodleorg/theme_moodleorg/1594982425/sitebar/moodle_sitebar_logo_grayhat"></p>'
displays "Section 3" followed by the icon you see in the top left corner of this forum page as the sections summary.
But the plugin does not allow you to modify the moduls and activities displayed in the section. I looked for this in the Rest-Api and found nothing to manipulate moduls, just to get infos about the moduls. I think this is reasonable because in my opinion you can't place moduls in a course without observing it from a students point of view. It is best to do this via webbrowser.
When it comes to h5p I would download the h5p-file. This is just a zipped file. You can access it and modify the content.json which is part of the content-directory. To spread activities to a number of courses I would build a template course and import it to the target courses. This can be done by Rest-Api as well.
It really depends on your scenario to find an appropriate way. In my view editing activities via rest-api is not the best one. I wouldn't put much work in it.

Cheers,
corvus albus