Can I use an HTML page for sitewide block config?

Can I use an HTML page for sitewide block config?

by Mike Worth -
Number of replies: 11
Is there a good reason why global block settings are done in php (settings.php) while instance config is done in HTML (config_instance.html)?

Can I set some system-wide config on a page I write myself and then do some server-side processing before saving it (just inplode an array)?

Thanks,
Mike
Average of ratings: -
In reply to Mike Worth

Re: Can I use an HTML page for sitewide block config?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Yes, settings.php files are much nicer to use, and allow things like the admin menu search and defaults system to work. There used to be a config_global.html but it was replaced.

No one has got around to replacing config_instance.html with something better like a moodleform yet.

If you need to let users select a list of things, wouldn't an admin_setting_configmulticheckbox do what you want with minimum hassle to you?
In reply to Tim Hunt

Re: Can I use an HTML page for sitewide block config?

by Mike Worth -
What I want to do is to have a select box with all the roles in, and an another role' button that will add a second select box. The user can then select as many roles as they like and then this array of select boxes can be imploded into a comma separated list to be stored in the database. Is there a way to get the javascript for this and the server side imploding into a settings.php?

Thanks,
Mike
In reply to Mike Worth

Re: Can I use an HTML page for sitewide block config?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Right well, what you do is create a setting of type admin_setting_pickroles ...

Argh! I see, that type of settings is only available in Moodle 2.0. Since, presumably, you want to do this for Moodle 1.9, that makes things a little more complex, but not much. Here is what to do.
  1. Copy the code below (taken from Moodle 2.0) into a file called something like adminsettingpickroles.php.
  2. In you settings.php file, add the code if (!class_exists('admin_setting_pickroles')) { require_once('adminsettingpickroles.php'); } (doing it this way will mean that your code won't bread when you upgrade to Moodle 2.0.)
  3. Then just create a setting of type admin_setting_pickroles and it will do exactly what you want.
More generally, if you want more exotic types of admin settings, you can define your own subclass of admin_setting that does whatever you want. However, it is rarely necessary in my experience.


/**
 * Admin setting that allows a user to pick appropriate roles for something.
 */
class admin_setting_pickroles extends admin_setting_configmulticheckbox {
 private $types;

 /**
 * @param string $name Name of config variable
 * @param string $visiblename Display name
 * @param string $description Description
 * @param array $types Array of capabilities (usually moodle/legacy:something)
 * which identify roles that will be enabled by default. Default is the
 * student role
 */
 public function __construct($name, $visiblename, $description, $types) {
 parent::__construct($name, $visiblename, $description, NULL, NULL);
 $this->types = $types;
 }

 public function load_choices() {
 global $CFG, $DB;
 if (empty($CFG->rolesactive)) {
 return false;
 }
 if (is_array($this->choices)) {
 return true;
 }
 if ($roles = get_all_roles()) {
 $this->choices = array();
 foreach($roles as $role) {
 $this->choices[$role->id] = format_string($role->name);
 }
 return true;
 } else {
 return false;
 }
 }

 public function get_defaultsetting() {
 global $CFG;

 if (empty($CFG->rolesactive)) {
 return null;
 }
 $result = array();
 foreach($this->types as $capability) {
 if ($caproles = get_roles_with_capability($capability, CAP_ALLOW)) {
 foreach ($caproles as $caprole) {
 $result[$caprole->id] = 1;
 }
 }
 }
 return $result;
 }
}
In reply to Tim Hunt

Re: Can I use an HTML page for sitewide block config?

by Mike Worth -
Excellent- that does just what I want. Just one little thing- Warning: Invalid argument supplied for foreach() in /usr/share/moodle/blocks/messageteacher/adminsettingpickroles.php on line 48.

Thanks,
Mike
In reply to Mike Worth

Re: Can I use an HTML page for sitewide block config?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, you will have to tell me what the code around line 48 looks like. I am not telepathic wink

The answer will be that one of the functions that returns an array of things that we then iterate over, for example get_roles_with_capability, will be returning false, when it would be more logical for it to return an empty array. Sticking in an

if (empty($somthing)) {
$something = array();
}

should fix it.
In reply to Tim Hunt

Re: Can I use an HTML page for sitewide block config?

by Mike Worth -
The code in question is what you just gave me up there ^wink

Line 48 is: foreach($this->types as $capability) {

Maybe this needs fixing in moodle 2 if that's where the code came from?

Thank,
Mike
In reply to Mike Worth

Re: Can I use an HTML page for sitewide block config?

by Sharon Goodson -
You guys make me chuckle big grin

side question - would Tim's code be appropriate (in theory) to use in other instances of this error?
In reply to Mike Worth

Re: Can I use an HTML page for sitewide block config?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
No. In Moodle 2, when they redid the database functions, one of the things they made sure of, is that get_records always returns an array (empty, if there are no records) or throws an exception if there is an error. That makes it much easier to use.

It is only in Moodle 1.9 where things are weird, and you have to do checks like that.In Moodle 1.9, get_records returns an array if there were one or more records in the result set. It returns false if there were no rows, or if an error occurred. Grrr!

So, to answer Sharon's question. In Moodle <= 1.9, if you want to loop over the result from a get_records, you need some sort of test to make sure you actually have some records to loop over. Of course, to do things properly, if get_records returns false, you should really check to see whether it was a database error, or just no records, but normally life is too short.
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Can I use an HTML page for sitewide block config?

by Mike Worth -
So this means that lots of contributed modules/blocks are going to need some pretty significant changes then? I've always liked being able to do:

if($records=get_records()){
foreach ($records as $record){
blah blah blah
}
}else{
echo 'no entries found';
}

sad
In reply to Mike Worth

Re: Can I use an HTML page for sitewide block config?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
No, not really. That code is pretty much equivalent to mine, and will mostly go on working. The bit that will need to change is changing

$records=get_records()

to

$records=$DB->get_records()

That is a bit of a pain, but is close to being a search-and-replace, and was done for good reasons. The really great thing is that the need for addslashes/stripslashes is completely gone, which is good for security and good for eliminating bugs where strings containing apostrophes get messed up.
In reply to Tim Hunt

Re: Can I use an HTML page for sitewide block config?

by Sharon Goodson -
Thanks! Life is short, I guess that's one of the reasons I've tried to learn php by osmosis - but Moodle has taught me the life is too short to try and learn php via osmosis (if I want to do the things I want to do, that is - in Moodle and in life *lol*).

Time to crack the books a bit wider so I can apply all this properly!