Moved helper function

This commit is contained in:
Mark Nelson 2017-08-19 20:09:19 +08:00
parent 538093223d
commit 3120130437
3 changed files with 40 additions and 40 deletions

View file

@ -288,7 +288,7 @@ class edit_form extends \moodleform {
} }
$group = array(); $group = array();
$group[] = $mform->createElement('select', 'element_' . $page->id, '', element::get_available_types()); $group[] = $mform->createElement('select', 'element_' . $page->id, '', element_helper::get_available_element_types());
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'), $group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'),
array(), false); array(), false);
$mform->addElement('group', 'elementgroup', '', $group, '', false); $mform->addElement('group', 'elementgroup', '', $group, '', false);

View file

@ -253,43 +253,4 @@ abstract class element {
return false; return false;
} }
/**
* Return the list of possible elements to add.
*
* @return array the list of element types that can be used.
*/
public static function get_available_types() {
global $CFG;
// Array to store the element types.
$options = array();
// Check that the directory exists.
$elementdir = "$CFG->dirroot/mod/customcert/element";
if (file_exists($elementdir)) {
// Get directory contents.
$elementfolders = new \DirectoryIterator($elementdir);
// Loop through the elements folder.
foreach ($elementfolders as $elementfolder) {
// If it is not a directory or it is '.' or '..', skip it.
if (!$elementfolder->isDir() || $elementfolder->isDot()) {
continue;
}
// Check that the standard class exists, if not we do
// not want to display it as an option as it will not work.
$foldername = $elementfolder->getFilename();
// Get the class name.
$classname = '\\customcertelement_' . $foldername . '\\element';
// Ensure the necessary class exists.
if (class_exists($classname)) {
$component = "customcertelement_{$foldername}";
$options[$foldername] = get_string('pluginname', $component);
}
}
}
\core_collator::asort($options);
return $options;
}
} }

View file

@ -378,4 +378,43 @@ class element_helper {
return $SITE->id; return $SITE->id;
} }
} }
/**
* Return the list of possible elements to add.
*
* @return array the list of element types that can be used.
*/
public static function get_available_element_types() {
global $CFG;
// Array to store the element types.
$options = array();
// Check that the directory exists.
$elementdir = "$CFG->dirroot/mod/customcert/element";
if (file_exists($elementdir)) {
// Get directory contents.
$elementfolders = new \DirectoryIterator($elementdir);
// Loop through the elements folder.
foreach ($elementfolders as $elementfolder) {
// If it is not a directory or it is '.' or '..', skip it.
if (!$elementfolder->isDir() || $elementfolder->isDot()) {
continue;
}
// Check that the standard class exists, if not we do
// not want to display it as an option as it will not work.
$foldername = $elementfolder->getFilename();
// Get the class name.
$classname = '\\customcertelement_' . $foldername . '\\element';
// Ensure the necessary class exists.
if (class_exists($classname)) {
$component = "customcertelement_{$foldername}";
$options[$foldername] = get_string('pluginname', $component);
}
}
}
\core_collator::asort($options);
return $options;
}
} }