3. Moodle 项目帮助手册:程序编写准则 OSS 计算技术 - http://www.ossez.com 3 of 6
号之间没有空格。
function forum_set_display_mode($mode=0) {
global $USER, $CFG;
if ( $mode) {
$USER->mode = $mode;
} else if (empty( $USER->mode)) {
$USER->mode = $CFG->forum_displaymode;
}
}
5. 语句块必须总是使用大括号(即便是只有一行)。Moodle使用如下风格:
if ($quiz->attempts) {
if ( $numattempts > $quiz->attempts) {
error($strtoomanyattempts, "view.php?id=$cm->id");
}
}
6. 字符串应当尽可能用单引号定义以提高速度。
$var = 'some text without any variables';
$var = "with special characters like a new line n";
$var = 'a very, very long string with a '.$single.' variable in it';
$var = "some $text with $many variables $within it";
7. 实用的注释应当尽可能填写,用以解释代码流程和函数与变量的功能。
每个函数和类都应该使用流行的phpDoc格式编写,以便自动生成代码文档。
内嵌注释应使用 // 风格,并且整齐布局,使其能融入代码中并和代码对齐。
/**
* The description should be first, with asterisks laid out exactly
* like this example. If you want to refer to a another function,
* do it like this: {@link clean_param()}. Then, add descriptions
* for each parameter as follows.
*
* @param int $postid The PHP type is followed by the variable name
* @param array $scale The PHP type is followed by the variable name
* @param array $ratings The PHP type is followed by the variable name
* @return mixed
*/
function forum_get_ratings_mean($postid, $scale, $ratings=NULL) {
if (!$ratings) {
$ratings = array(); // Initialize the empty array
3 of 6 2012-09-29 01:44
4. Moodle 项目帮助手册:程序编写准则 OSS 计算技术 - http://www.ossez.com 4 of 6
if ($rates = get_records("forum_ratings", "post", $postid)) {
// Process each rating in turn
foreach ($rates as $rate) {
....etc
8. 换行可以被大方地使用——把东西分散开看起来会比较清楚。一般情况下,在花括号和普通
命令之间应当有一个换行符,但在花括号和变量或函数之间可以没有换行符:
foreach ($objects as $key => $thing) {
process($thing);
}
if ($x == $y) {
$a = $b;
} else if ($x == $z) {
$a = $c;
} else {
$a = $d;
}
数据库结构
1. 每个表格都必须有一个自增的id字段(INT10)作为主键。
2. 包含着模块中数据实例的主表格必须和模块同名(譬如widget),并且至少包含如下字段:
id - 如上一条所述
course - 每个实例所属的课程id
name - 每个实例的完整名称
3. 与模块相关的其它表格的命名规则是:如果它包含的信息是关于“things”的,则它的名字
应当是widget_things(注意采用复数形式)。
4. 字段名称应当简短,与变量名的规则相同。
5. 在可能的情况下,包含着对其它表格(如widget)引用的字段应当命名为widgetid。(注意这
是个新约定,有一些老的表格并未遵守)
6. 布尔字段应当使用小整数类型(如INT4)并存储为0或1,这样就可以在需要时扩展它。
7. 多数的表格应当有一个timemodified字段(INT10),并用PHP的time()函数取得的当前时间
戳来更新它。
4 of 6 2012-09-29 01:44