generate shortname during course creation

generate shortname during course creation

by Ger Tielemans -
Number of replies: 3
We use shortname as reference for the heldesk.
All courses have the format MOODLE-####
So MOODLE-#### is:
  • the left part of the backup-name of the course
  • and it is the file-dir-number of the course
  • we echo this number in the navigation-bar of the course
User can say to the heldesk: I have a question about MOODLE-####

My problem:
When I create a course, the number does not exist... so I can not fill-in the shortname on the course-section-page.

My question:
Is there at trick to rename the shortname of the course into MOODLE-#### as soon as this number is available? (= just after I close the fill-in-form for course creation?)


Average of ratings: -
In reply to Ger Tielemans

Re: generate shortname during course creation

by Ger Tielemans -
mm... must be something like "the next free ID in the table mdl_course" anyone?
In reply to Ger Tielemans

Re: generate shortname during course creation

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The only realiable way to do this in code is

// ...
$course->shortname = ...; // Something random
$id = insert_record('course', $course);
update_record('course', 'shortname', 'MOODLE-' . $id, 'id', $id);



Code like

// ...
$id = next_free_id('course');
$course->shortname = 'MOODLE-' . $id;
insert_record('course', $course);

will fail. This is a classic in concurrent programming. Imagine that two users click the create course button at the same time, and that the server executes them at slightly different rates. The only way to have bug-free code is to have the act of assigning the id to the record, and returning it as an atomic operation, as in the first example code.
In reply to Tim Hunt

Re: generate shortname during course creation

by Ger Tielemans -
yes, I considered that two user problem..

i was wondering how to lock the record for writing.

nice trick to use the update only on the moment of writing, then the record is locked

thanks