How can I Lock fullname and shortname fields?

How can I Lock fullname and shortname fields?

by Manuel de la Torre -
Number of replies: 11
I am using Moodle 1.8.4+.

Is it possible to lock certain course settings (fullname, shortname, etc...) so that only the administrator can change them?

We need to be this fields have a fixed initial value, because we use them to manage our site and for overall consistency, and professors always like to change them.

met.
Average of ratings: -
In reply to Manuel de la Torre

Re: How can I Lock fullname and shortname fields?

by ian lake -

Go to users authentication, choose type then click settings. Locking different items will prevent them changing - but be careful.

Ian

In reply to ian lake

Re: How can I Lock fullname and shortname fields?

by A. T. Wyatt -
Greetings, Ian!

I think your instructions refer to users, but the question was about courses.

I don't think I know a way to lock course fields. I asked my instructors not to change them, and they were all very gracious to comply!

Maybe someone will report a feature I have overlooked! smile

atw
In reply to A. T. Wyatt

Re: How can I Lock fullname and shortname fields?

by Manuel de la Torre -
atw,

You are right must of the instructors usually comply, the problem is with the few that doesn't!

met.
In reply to A. T. Wyatt

Re: How can I Lock fullname and shortname fields?

by ian lake -

blush My selective reading ability catches me out again.

i

In reply to Manuel de la Torre

Re: How can I Lock fullname and shortname fields?

by Matt Campbell -
I used to do this, back when we first started - I locked the shortname & idnumber fields in our courses. I haven't had to do this since we moved to 1.8.x, which uses mod form for most of this sort of thing. Let me look at the code over the weekend and I'll see if I can figure it out - unless someone else posts it first!

Thanks,
Matt
In reply to Matt Campbell

Re: How can I Lock fullname and shortname fields?

by Manuel de la Torre -
Matt,
I will really appreciate your help! Thanks!
Met.
In reply to Manuel de la Torre

Re: How can I Lock fullname and shortname fields?

by Matt Campbell -
Okay, I've just tested this in 1.9 and it works just fine. This part of the code works the same for 1.8, so give it a try and let me know if you have any problems.

The file you'll need to edit is /course/edit_form.php. Look for the following line:

/// form definition with new course defaults

You'll see it around lines 40-45 or so. This is where the form for course settings is being built.

You'll then need to look for the part of the form you want to lock. I'll use fullname for example:

$mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
$mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
$mform->setDefault('fullname', get_string('defaultcoursefullname'));
$mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
$mform->setType('fullname', PARAM_MULTILANG);

What you need to do is wrap the code with a check for roles, and then lock the field so instructors can't edit it (they won't even see it). My added code is in red.

if (has_capability('moodle/course:create', $categorycontext)) {
$mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
$mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
$mform->setDefault('fullname', get_string('defaultcoursefullname'));
$mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
$mform->setType('fullname', PARAM_MULTILANG);
} else {
$mform->addElement('hidden', 'fullname', null);
}

You should be able to use this code for about any of the settings in this form (replace fullname in the second to last line of my hack with the field name - e.g. shortname if you're locking the shortname.). Simply wrap it around the section just as you've seen here.

I've got a couple of caveats with this - this code allows anyone with course creation capabilities at the category level to edit the wrapped fields, so if you need it at a higher level, change the capability
'moodle/course:create', $categorycontext to the capability you need. But if you DO change the capability, be real careful with the required fields - you could run into problems if someone has the capability to create a course but can't see these fields to input something into them.

Let me know if you have any problems or questions.

Thanks,
Matt
In reply to Matt Campbell

Re: How can I Lock fullname and shortname fields?

by Manuel de la Torre -
Matt,

You hack worked beautiful! smile

Thanks,

Met.
In reply to Manuel de la Torre

Re: How can I Lock fullname and shortname fields?

by Matt Campbell -
Glad to help, and Happy Moodling!

Thanks,
Matt
Average of ratings: Useful (1)
In reply to Matt Campbell

Re: How can I Lock fullname and shortname fields?

by Chris Myers -
For anyone else that has need of this, I found that you can make a simple change to the code to lock individual fields, but still display them.

If you look at the addElement line for a particular field:

$mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');

you can add the word "readonly" between the size= statement and the tail ' . So in this case, it would be:

$mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50" readonly');

This way, the attribute will still be displayed, but will not be editable by the users.
In reply to Chris Myers

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.