Moodle开发笔记1-基础知识

来源:互联网 发布:新手单反相机推荐知乎 编辑:程序博客网 时间:2024/06/05 04:51

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 foruser-uploaded content, Moodle data also stores the session data for userslogged into the system, if file-based sessions are being used.

 

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

 

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

 

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

 

Moodle some important folders

admin:

Contain the PHP files that control theadministrative user's interface. 其中包括一个cron.php: it run as a batch process to perform systemmaintenance tasks such as message delivery and course backups, 同时它也用于处理batch operations

 

auth:

Contain all of the authentication modules for Moodle. “auth”目录里每一个子目录就是一个authentication module. 这些authentication modules controlthe creation of users, user profile data, and user access to the system.

 

backup:

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

 

blocks:

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

course:

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

enrol:

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

 

files:

The files component controls file uploads, access control, and the viewingof files. Files will see a major rewrite in Moodle 2.0. Moodle 2.0 willallow 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 databaseduring page creation. Filters match andmodify the page before it is displayed. It needs to be carefully developed, with performanceimplications in mind.

lang:

Contains thecore system language strings. Language string mappings are also stored in theMoodle data lang folder.

 

lib:

Contains the core system library functions. As we develop modules andcustomizations, 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 coursedelivered using Moodle. Activity modules are more challenging to create thanblocks, because they back up, restore, and store grades.

my:

It provides a listing of courses a learner is assigned, includinga summary of upcoming course activities. The user can also add and removeblocks on his or her portal page. my provides a goodlocation 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 ofthe built-in Moodle themes and any custom themes. Each theme has its ownfolder.

 

 

 

Include Moodle Libraries

先说2个很有用的关于path的变量

$CFG->dirroot  指向moodle root folder

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

 

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

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 onthe specified arguments, and will generate errors or warnings if somethingother 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: Acourse page

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

 

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

 

config.php & setup.php

所有的entrypoint php files的第一行都是

require_once('../config.php')

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

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

 

注意config.phpincludes /lib/setup.php

setup.phpperforms all of the initialprogram execution required tocomplete the execution environment setup. This includes defining several otherimportant global variables, including$SESSION, $COURSE, $THEME, and$db.

 

 

setup.phpsets up and connects databaseaccording 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 variablesfrom 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的特定formathandle. 首先要先include courseformatphp file.

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

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

 

format.php用于处理特定的coursepage的输出,包括the blocks and maincontent.

 

 

print_footer函数用于输出footer

print_footer(NULL, $course);

 

 

 

Configuration Moodle

Moodle的设置分别处于3个地方:

·        直接在config.phphard 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 andsetup.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 SafeMode

$CFG->passwordsaltmain = 'xxxx';

这时config.php的最必须的设置,如果想在config.php里进行更多的设置,则要参看config-dist.phpall configuration settting,然后修改config.php

 

上述设置你可以直接在config.php里修改。

 

除了config.php之外的所有其他设置都存储在databasemdl_config table and mdl_config_plugins table里。

那么moodle何时把这些来自database的设置赋给$CFG?

就是在config.phpincludelib/setup.phpsetup.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.phphard code你希望的设置,在config.php最后一行includesetup.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_pluginstable,它多了一个”plugin” field,这就使你只要保证该设置的name在该plugin里是唯一的即可

 

注意:plugin的设置则会放在plugin变量里,而不是存在$CFG

 

通常,我们都是通过administration interfacesset 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, androles

Moodle login function uses PHP's 'cookie' functionsto 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 withthe 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 allpermissions

·        teacher                    Canteach a course, develop, and update course content

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

·        student                    Can take a course

·        course creator          Can create course shells and can belimited to a course category

·        authenticated user     Any logged in user has this role

·        guest                       Access permission fornon-logged in users

这些role都可以assign给上面的一个或多个context

 

Each user can have multiple roles that inheritpermissions from all of the context levels applicable to a given access requestfrom the user

 

 

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

 

Examples of capabilities are:

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

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

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

 

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

·        Not Set

·        Allow

·        Prohibit

·        Prevent

 

注意:开发者可以通过创建capabilitiescontrol access to our new functionality. Carefulconsideration should be given as to which context is thebest location for a new capability. Capabilities should generallybe placed at the lowest context level at whichthey can function

 

 

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

 

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

 

总结来说

·       Contextsare specific elements in Moodle

·       Rolesare associated with all contexts

·       Capabilitiesare assigned to roles in a given context

·       Usersare assigned roles in a given context

 

普通系统使用User, Role, CapabilityOK了,为什么moodle还要加多一个context??

这是因为

·        (??not sure) 同一个user在不同的contextrole不同,比如在system contextuseradmin role,而他在course “foo”里是instructor role

·        每个user role在不同的context里的capability都不同。

 

获取context对象的函数是get_context_instance()

 

例:

获取system context对象

$context = get_context_instance(CONTEXT_SYSTEM);

 

获取当前coursecontext

global $COURSE;

$context =get_context_instance(CONTEXT_COURSE, $COURSE->id);

 

获取context之后,下列2个函数是用来check当前loginuser在该context里是否有所指定的capability

·        require_capability tests the current user's capabilities to see if they have thespecified capability in the specified context, If they don't, the page isredirected to an error page

·        has_capability 功能require_capability类似,但不会redirect to error page,而是return true or false

 

例:

$context = get_context_instance(CONTEXT_SYSTEM);

require_capability('moodle/site:doanything', $context);

上面的例子是check current usersystem context里是否有'moodle/site:doanything'capability

 

如何为你的moodle plugin/module自定义capability?

plugin/moduleroot目录下创建一个db目录,然后在db目录下创建一个access.php,该文件用来定义capability

 

下例是在helloworldblock里定义一个block/helloworld:view capability,该capabilitytyperead,该capability是属于system context level里,并设置只有admin role user拥有该capability,其他role没有。

 

<?php

$block_helloworld_capabilities = array(

'block/helloworld:view' => array(

'captype'=> 'read',

'contextlevel'=> CONTEXT_SYSTEM,

'legacy' => array(

'guest'=> CAP_PREVENT,

'student'=> CAP_PREVENT,

'teacher'=> CAP_PREVENT,

'editingteacher'=> CAP_PREVENT,

'coursecreator'=> CAP_PREVENT,

'admin'=> CAP_ALLOW

)

)

);

?>

 

注意:该capability适用于任何使用了该block的地方。无论你是把该block加到home page,还是admin page,还是My Moodle page,还是course page,该capability都适用。但由于该capability是定义在system context level,只有那些在systemcontext level具有admin roleuser才能够看到这个block

 

 

 

总共有5种类型的Moodle plugin

·        block

·        filter

·        activity module

·        theme

·        course format

 

原创粉丝点击