Replace isadmin() with has_capability in /course/edit_form.php

Replace isadmin() with has_capability in /course/edit_form.php

by Judy Hsu -
Number of replies: 2
Hi there...

As we have an seperate authentication system and we don't want our teachers to change the course Full name, Short name, Course ID number in the "Edit course settings" page, so I have implemented the following code in the /course/edit_form.php to add a isadmin() check

if (isadmin()) {
$mform->setHelpButton('category', array('coursecategory', get_string('category')));
$mform->setDefault('category', $category->id);
$mform->setType('category', PARAM_INT);

$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);

$mform->addElement('text','shortname', get_string('shortname'),'maxlength="100" size="20"');
$mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
$mform->setDefault('shortname', get_string('defaultcourseshortname'));
$mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
$mform->setType('shortname', PARAM_MULTILANG);

$mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
$mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
$mform->setType('idnumber', PARAM_RAW);

} else {
$mform->addElement('text','fullname', get_string('fullname'),'disabled maxlength="254" size="50"');
$mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
$mform->addElement('text','shortname', get_string('shortname'),'disabled maxlength="100" size="20"');
$mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
$mform->addElement('text','idnumber', get_string('idnumbercourse'),'disabled maxlength="100" size="10"');
$mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
}

However I think in moodle 1.9 this is probably not the best way to implement it as we should probably avoid using isadmin(). However I couldn't find a matching "has_capability()" to implmement this. Any suggestion? Thanks!
Attachment edit_course_settings.png
Average of ratings: -
In reply to Judy Hsu

Re: Replace isadmin() with has_capability in /course/edit_form.php

by Matt Campbell -
I have some users that have the ability to create courses in certain categories, so I'm using:

if (has_capability('moodle/course:create', $categorycontext)) {

This seems to work just fine for me - teachers can't see/edit the information, but my course creators can.

Thanks,
Matt
In reply to Judy Hsu

Re: Replace isadmin() with has_capability in /course/edit_form.php

by Birgit May -
Hello,

silly question, BUT: where in the file do i put the source code?

thanks
birgit