1. 程式人生 > >Moodle開發筆記1-基礎知識

Moodle開發筆記1-基礎知識

There are roughly 200 related tables in Moodle database. “ mdl_ is prefix of each table name.

Moodle data

Moodle data is the file storage location for user-uploaded content, Moodle data also stores the session data for users logged into the system, if file-based sessions are being used.

Moodle structures the data in this folder by either the user or by the course.

Each course has a folder, which is named with an integer value. The integer value is set to the internal database ID of the course in question

Moodle 2.0 uses an entirely new organizational model for user-uploaded files, which is based on a hashing algorithm.

Moodle some important folders

admin:

Contain the PHP files that control the administrative user's interface. 其中包括一個 cron.php : it run as a batch process to perform system maintenance tasks such as message delivery and course backups, 同時它也用於處理 batch operations

auth:

C ontain all of the authentication modules for Moodle. “auth”

目錄裡每一個子目錄就是一個 authentication module. 這些 authentication modules control the creation of users, user profile data, and user access to the system.

backup:

Contain the core course backup facilities for the backup, restore, and import of courses.

blocks :

blocks are used to display boxes of information in either the right-hand side or left-hand side column of the Moodle page. This is one of the simplest module types to make.

course:

This component of Moodle has obvious importance, given that Moodle is organized around courses. Developers are most likely to modify or add course formats and reports. Custom course formats can be used to change the layout of courses.

enrol:

Contains all of the enrollment modules for Moodle. Enrollment modules control the creation and management of course-level role assignments (enrollments).

files:

The files component controls file uploads, access control, and the viewing of files. Files will see a major rewrite in Moodle 2.0 . Moodle 2.0 will allow storing and using files in external file repositories such as Alfresco, Box.net, and Google Docs .

filter:

The filter system is fed user-entered content from the database during page creation. Filters match and modify the page before it is displayed . It needs to be carefully developed, with performance implications in mind.

lang:

Contains the core system language strings. Language string mappings are also stored in the Moodle data lang folder.

lib:

Contain s the core system library functions. As we develop modules and customizations, we will use classes and functions defined in this folder.

mod:

Contains activity modules such as assignment, quiz, wiki, forum, and lesson modules . Learning activities are the core of any course delivered using Moodle. Activity modules are more challenging to create than blocks, because they back up, restore, and store grades.

my:

It provides a listing of courses a learner is assigned, including a summary of upcoming course activities. The user can also add and remove blocks on his or her portal page. my provides a good location to display custom information with minimal core changes to Moodle. For example, we use my as a dashboard location in many of our customization projects .

theme:

Contains all of the built-in Moodle themes and any custom themes. Each theme has its own folder.

Include Moodle Libraries

先說 2個很有用的關於 path的變數

$CFG->dirroot 指向 moodle root folder

$CFG->libdir 指向 moodle root folder下的 lib folder

例如,若要 include moodle_home/lib下的 lib library,可以

require_once($CFG->libdir . '/blocklib.php');

optional_param & required_param

2個是 moodle特有的 function,用來代替 php自身的從 $GET, $POST, $COOKIE中獲取引數值

required_param函式則要求必須要所要的引數,而 optional_param則不需要一定存在所要的引數。

Both of these functions validate the data based on the specified arguments, and will generate errors or warnings if something other than what was expected is passed in

詳細描述上網查

例:

$id = optional_param('id', 0, PARAM_INT);

$name = optional_param('name', '', PARAM_RAW);

1個引數是 param name,第 2個引數是預設值

Moodle entry points

/index.php : The front page

/login/index.php : The login page

/admin/index.php : The main administration page

/course/view.php : A course page

/mod/*/view.php : A module page

For example, http://localhost/course/view.php?id=23

config.php & setup.php

所有的 entry point php files的第一行都是

require_once('../config.php')

config.php performs a number of initial parameter assignments in the global $CFG variable

Information in $CFG are the database, web URL, script directory, and data storage directory definitions.

注意 config.php includes /lib/setup.php

setup.php performs all of the initial program execution required to complete the execution environment setup. This includes defining several other important global variables, including $SESSION , $COURSE , $THEME , and $db .

setup.php sets up and connects database according to the settings defined in config.php .

Moodle 使用 ADOdb 來進行資料庫操作,使用 ADOdb 你需要 include

/lib/adodb/adodb.inc.php

setup.php 還會 inlude 一些常用的庫 , 還會 sets up some other critical global variables, loads configuration variables from the database, sets up caching, sessions, environment variables, themes, language, and locales.

get_record function

該函式是從 database 裡獲取 record

if (! ($course = get_record('course', 'id', $id)) ) {

error('Invalid course id');

}

require_login function

該函式是用來 check if the user is logged in site or course ( 有些 course 可能設定成不需要 login). 如果需要 login site ,但 user 又沒有 login ,就 redirect to login page 。如果 user 已經 login ,他正在嘗試 access a course ,但又沒有 enrollment 到該 course ,那麼執行該函式就會 redirects the user to the enrollment function

例:

require_login($course);

Displaying functions in Moodle

輸出 html header 的函式有 2

print_header

print_header_simple

上面函式用於輸出 html header, 包括 the theme info and 所要的 javascript file

$PAGE->print_header(get_string('course').': %fullname%', NULL, '', $bodytags);

輸出 html body 是由 course 的特定 format handle. 首先要先 include course format php file.

require($CFG->dirroot .'/course/format/'. $course->format .'/ format.php');

例如,如果 course 使用 topics format ,就會 include /course/format/topics/format.php.

format.php用於處理特定的 course page的輸出,包括 the blocks and main content.

print_footer 函式用於輸出 footer

print_footer(NULL, $course);

Configuration Moodle

Moodle 的設定分別處於 3 個地方:

· 直接在 config.php hard code

· mdl_config table 。可以通過 administrative code and interfaces 進行控制

· mdl_config_plugins table 。主要是儲存來自各個 plugin 的設定。可以通過 plugin administration 來進行控制

All configuration info 都存在變數 $CFG 裡( plugin 的設定則會放在 plugin 變數裡)。例如 $CFG->theme contains the text name of your site's selected theme.

config.php 一開始會呼叫 unset($CFG); 來保證在 config.php and setup.php 之前清除所有的設定

config.php 裡,包含下列的設定 :

$CFG->dbtype = 'mysql';

$CFG->dbhost = 'localhost';

$CFG->dbname = 'moodle';

$CFG->dbuser = 'xxx';

$CFG->dbpass = 'xxx';

$CFG->dbpersist = false;

$CFG->prefix = 'mdl_';

$CFG->wwwroot = 'http://xxxx:8080/moodle';

$CFG->dirroot = 'E:/develop/Zend/Apache2/htdocs/moodle';

$CFG->dataroot = 'E:/develop/Zend/Apache2/htdocs/moodledata';

$CFG->admin = 'admin';

$CFG->directorypermissions = xxx; // try 02777 on a server in Safe Mode

$CFG->passwordsaltmain = 'xxxx';

這時 config.php 的最必須的設定,如果想在 config.php 裡進行更多的設定,則要參看 config-dist.php all configuration settting ,然後修改 config.php

上述設定你可以直接在 config.php 裡修改。

除了 config.php 之外的所有其他設定都儲存在 database mdl_config table and mdl_config_plugins table 裡。

那麼 moodle 何時把這些來自 database 的設定賦給 $CFG?

就是在 config.php include lib/setup.php setup.php 呼叫了

$CFG = get_config();

來執行。 get_config() 函式來自 /lib/moodlelib.php library file

注意: get_config函式不會對於在呼叫之前已經存在的設定進行覆蓋。 ( will not overwrite any $CFG setting that has already been set) 。即它不會覆蓋 config.php 裡的設定 . 這意味著你可以在 config.php hard code 你希望的設定,在 config.php 最後一行 include setup.php ,但來自 database 的設定如果與 config.php 裡的設定同名,則不會覆蓋它。

configuration 進行修改是通過 set_config 函式。該函式會以

· name

· value

· plugin name (optional)

作為引數。如果不使用了第三個引數,那麼 set_confg 就會把設定儲存在 mdl_config table ,如果使用這個引數,則存在 mdl_config_plugins table

我們開發的通常是 plugin (modules, blocks, and so on) 。在開發過程中,如果你想新增設定的話,強烈建議使用 mdl_config_plugins table 來儲存,即呼叫 set_config 時要用到 plugin name 引數。這是因為: 設定的 name 必須唯一。如果你想新增設定到 mdl_config table裡,那麼就有可能該設定的 name已經存在,產生衝突。而對於 mdl_config_plugins table ,它多了一個 ”plugin” field ,這就使你只要保證該設定的 name 在該 plugin 裡是唯一的即可

注意: plugin 的設定則會放在 plugin 變數裡,而不是存在 $CFG

通常,我們都是通過 administration interfaces set configuration variables 。絕對多數的 Moodle configuration variables 都可以在 Site Administration block (用 admin login 後的 home page 會看到它)裡進行設定。

Moodle API

絕大多數的 api 都放在 lib 目錄下,該目錄下的 library php 的命名方式是

[function]lib.php

例如 textlib.php and weblib.php

幾乎所有的 core libraries are included when you load config.php via its inclusion of /lib/setup.php

最常用的 library

· moodlelib.php

· weblib.php

· dmllib.php

· accesslib.php

· grouplib.php

Moodle 還會用到一些開源的 library ,如

· PEAR

· ADOdb

· YUI

· XMLDB

Access control, logins, and roles

Moodle login function uses PHP's 'cookie' functions to set cookies into the current session.

Permissions can be assigned within six contexts :

· site/global

· course category

· course

· blocks

· activities

· user

· front page

Contexts are elements in the system associated with the defined context levels

Context 定義在 /lib/accesslib.php

define('CONTEXT_SYSTEM', 10);

define('CONTEXT_USER', 30);

define('CONTEXT_COURSECAT', 40);

define('CONTEXT_COURSE', 50);

define('CONTEXT_GROUP', 60);

define('CONTEXT_MODULE', 70);

define('CONTEXT_BLOCK', 80);

System” context 只有一個,其他的則有許多個,如 ”Course” context, “User” context

There are seven built-in roles

· administrator System administrator has all permissions

· teacher Can teach a course, develop, and update course content

· non-editing teacher Can teach a course but can't edit course content

· student Can take a course

· course creator Can create course shells and can be limited to a course category

· authenticated user Any logged in user has this role

· guest Access permission for non-logged in users

這些 role 都可以 assign 給上面的一個或多個 context

Each user can have multiple roles that inherit permissions from all of the context levels applicable to a given access request from the user

Capabilities are associated with context levels, and are specific access rules that can be granted to roles.

Examples of capabilities are:

· moodle/site:manageblocks : Can manage blocks at the site context level

· moodle/user:viewdetails : Can view details of a user at the user context level

· moodle/course:view : Can view a course at the course context level

每一個 capability 都可以 assign 給下列 4 access levels 的其中一個:

· Not Set

· Allow

· Prohibit

· Prevent

注意:開發者可以通過建立 capabilities control access to our new functionality. Careful consideration should be given as to which context is the best location for a new capability . Capabilities should generally be placed at the lowest context level at which they can function

Roles are specific identifiers that are associated with all contexts. Roles are primarily used to group capabilities for a context, so that these capabilities can be given to users. Capabilities are assigned to roles in specific contexts, either by default or by specific assignment (overriding).

users can be assigned to roles in specific contexts. This assignment gives them the accesses defined by the capabilities in that role for that context.

總結來說

· Contexts are specific elements in Moodle

· Roles are associated with all contexts

· Capabilities are assigned to roles in a given context

· Users are assigned roles in a given context

普通系統使用 User, Role, Capability OK 了,為什麼 moodle 還要加多一個 context ??

這是因為

· (??not sure) 同一個 user 在不同的 context role 不同,比如在 system context