Moodle开发笔记1-基础知识

15 篇文章 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 user admin role ,而他在 course “foo” 里是 instructor role

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

 

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

 

例:

获取 system context 对象

$context = get_context_instance(CONTEXT_SYSTEM);

 

获取当前 course context

global $COURSE;

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

 

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

·         require_capability tests the current user's capabilities to see if they have the specified capability in the specified context, If they don't, the page is redirected 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 user system context里是否有 'moodle/site:doanything' capability

 

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

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

 

下例是在 helloworld block 里定义一个 block/helloworld:view capability ,该 capability type read ,该 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,只有那些在 system context level具有 admin role user才能够看到这个 block

 

 

 

总共有 5 种类型的 Moodle plugin

·         block

·         filter

·         activity module

·         theme

·         course format

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值