Skip to content

Instantly share code, notes, and snippets.

@marinaglancy
Last active April 24, 2019 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marinaglancy/e70f77b2830dacf45447f24e85ae3e88 to your computer and use it in GitHub Desktop.
Save marinaglancy/e70f77b2830dacf45447f24e85ae3e88 to your computer and use it in GitHub Desktop.
Test the select and course elements
<?php
require_once(__DIR__ . '/config.php');
require_once($CFG->libdir.'/formslib.php');
require_login();
$PAGE->set_url(new moodle_url('/testselect.php'));
$PAGE->set_context(context_system::instance());
class testform extends moodleform {
public function definition()
{
global $DB;
$this->_form->addElement('course', 'courses', 'Courses', ['multiple' => 1]);
$this->_form->setDefault('courses', [3,2]);
$courseslist = $DB->get_records_menu('course', null, 'sortorder', 'id, fullname');
$this->_form->addElement('select', 'cselect', 'Courses select', $courseslist, ['multiple' => 1, 'size' => 5]);
$this->_form->setDefault('cselect', [3,2]);
$this->_form->addElement('tags', 'interests', get_string('interestslist'),
array('itemtype' => 'user', 'component' => 'core'));
$this->_form->addElement('course', 'courses1', 'Single course');
//$this->_form->setDefault('courses1', [2]);
$this->_form->addElement('advcheckbox', 'enablecselect1', 'Enable cselect1 and text1');
$this->_form->addElement('select', 'cselect1', 'Courses select (1)', $courseslist);
$this->_form->disabledIf('cselect1', 'enablecselect1', 'unchecked');
$this->_form->addElement('text', 'text1', 'Text input', $courseslist);
$this->_form->disabledIf('text1', 'enablecselect1', 'unchecked');
$this->_form->setType('text1', PARAM_TEXT);
$this->add_action_buttons();
}
}
$form = new testform();
echo $OUTPUT->header();
if ($data = $form->get_data()) {
print_object($data);
}
$form->display();
echo $OUTPUT->footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment