查看文章 |
转载:http://hi.baidu.com/heshaoyue Moodle在表现层的实现有多种机制,分别针对页面、表单、导航条、页面头部、页面底部等。 1、针对页面的实现,直接编辑HTML页面,然后在业务逻辑处理完毕之后,include编辑好的html页面即可。这种机制的实现可以看login/index.php和 login/index_form.php页面。 2、针对表单的实现,一般是创建一个父类为moodleform的类,如
class test_form extends moodleform {
//定义表单元素 function definition() {
//获得表单引用 $mform =& $this->_form;
//添加header对象 $mform->addElement('header', '', get_string('createuserandpass'), '');
//添加text对象, $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"'); $mform->setType('username', PARAM_NOTAGS); $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
}
//定义过滤 function definition_after_data(){}
//定义验证逻辑 function validation($data, $files) {} }
然后,业务逻辑层中声明test_form对象,即
$test = new test _form();
最后调用test_form对象的display方法,即可把表单对象显示出来。
$ test ->display();
表单元素的类型和规则如下: $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = array( 'group' =>array('HTML/QuickForm/group.php','HTML_QuickForm_group'), 'hidden' =>array('HTML/QuickForm/hidden.php','HTML_QuickForm_hidden'), 'reset' =>array('HTML/QuickForm/reset.php','HTML_QuickForm_reset'), 'checkbox' =>array('HTML/QuickForm/checkbox.php','HTML_QuickForm_checkbox'), 'file' =>array('HTML/QuickForm/file.php','HTML_QuickForm_file'), 'image' =>array('HTML/QuickForm/image.php','HTML_QuickForm_image'), 'password' =>array('HTML/QuickForm/password.php','HTML_QuickForm_password'), 'radio' =>array('HTML/QuickForm/radio.php','HTML_QuickForm_radio'), 'button' =>array('HTML/QuickForm/button.php','HTML_QuickForm_button'), 'submit' =>array('HTML/QuickForm/submit.php','HTML_QuickForm_submit'), 'select' =>array('HTML/QuickForm/select.php','HTML_QuickForm_select'), 'hiddenselect' =>array('HTML/QuickForm/hiddenselect.php','HTML_QuickForm_hiddenselect'), 'text' =>array('HTML/QuickForm/text.php','HTML_QuickForm_text'), 'textarea' =>array('HTML/QuickForm/textarea.php','HTML_QuickForm_textarea'), 'link' =>array('HTML/QuickForm/link.php','HTML_QuickForm_link'), 'advcheckbox' =>array('HTML/QuickForm/advcheckbox.php','HTML_QuickForm_advcheckbox'), 'date' =>array('HTML/QuickForm/date.php','HTML_QuickForm_date'), 'static' =>array('HTML/QuickForm/static.php','HTML_QuickForm_static'), 'header' =>array('HTML/QuickForm/header.php', 'HTML_QuickForm_header'), 'html' =>array('HTML/QuickForm/html.php', 'HTML_QuickForm_html'), 'hierselect' =>array('HTML/QuickForm/hierselect.php', 'HTML_QuickForm_hierselect'), 'autocomplete' =>array('HTML/QuickForm/autocomplete.php', 'HTML_QuickForm_autocomplete'), 'xbutton' =>array('HTML/QuickForm/xbutton.php','HTML_QuickForm_xbutton') );
$GLOBALS['_HTML_QuickForm_registered_rules'] = array( 'required' => array('html_quickform_rule_required', 'HTML/QuickForm/Rule/Required.php'), 'maxlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'), 'minlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'), 'rangelength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'), 'email' => array('html_quickform_rule_email', 'HTML/QuickForm/Rule/Email.php'), 'regex' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'), 'lettersonly' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'), 'alphanumeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'), 'numeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'), 'nopunctuation' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'), 'nonzero' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'), 'callback' => array('html_quickform_rule_callback', 'HTML/QuickForm/Rule/Callback.php'), 'compare' => array('html_quickform_rule_compare', 'HTML/QuickForm/Rule/Compare.php') );
具体可以查看lib/pear/HTML/QuickForm.php,和各个表单元素和规则的实现文件。
3、针对导航条的实现,一般是直接调用build_navigation函数即可。使用方法可以看下面一个例子。
$newaccount = get_string('newaccount'); $login = get_string('login');
if (empty($CFG->langmenu)) { $langmenu = ''; } else { //获得语言列表 $currlang = current_language(); $langs = get_list_of_languages(); //生成语言下拉列表 $langmenu = popup_form ("$CFG->wwwroot/login/signup.php?lang=", $langs, "chooselang", $currlang, "", "", "", true); }
//定义导航数组 $navlinks = array(); $navlinks[] = array('name' => $login, 'link' => "index.php", 'type' => 'misc'); $navlinks[] = array('name' => $newaccount, 'link' => null, 'type' => 'misc'); // 这里可以添加更多的导航信息,省略 //生成导航 $navigation = build_navigation($navlinks);
这里需要注意的是,build_navigation函数并不直接把生成的html发送到浏览器中,而是作为返回值返回。
4、针对页面头部的实现,一般是直接调用print_header函数即可,即
print_header($newaccount, $newaccount, $navigation, $mform_signup->focus(), "", true, "<div class=\"langmenu\">$langmenu</div>");
这样,就把生成的头部文件发送到浏览器中了。print_header的实现,会根据系统的主题/皮肤,找到header.html,并进行处理。
ob_start(); include($CFG->header); //$CFG->header = $CFG->themedir .'/'. $theme .'/header.html'; $output = ob_get_contents(); ob_end_clean();
更加详细请查看lib/weblib.php文件的实现。
5、针对页面底部的实现,一般是在业务逻辑的代码最后面,直接调用print_footer函数即可,即
print_footer();
print_footer的实现与print_header一样,都是借用php中的页面缓存机制来实现,这样能够在一定程度上提高系统的反应速度,增强用户体验。具体实现,这里就不再详述。 |