Moodle开发笔记1-基础知识

2024-04-19 22:48

本文主要是介绍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 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

 

这篇关于Moodle开发笔记1-基础知识的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/918650

相关文章

VSCode开发中有哪些好用的插件和快捷键

《VSCode开发中有哪些好用的插件和快捷键》作为全球最受欢迎的编程工具,VSCode的快捷键体系是提升开发效率的核心密码,:本文主要介绍VSCode开发中有哪些好用的插件和快捷键的相关资料,文中... 目录前言1、vscode插件1.1 Live-server1.2 Auto Rename Tag1.3

Agent开发核心技术解析以及现代Agent架构设计

《Agent开发核心技术解析以及现代Agent架构设计》在人工智能领域,Agent并非一个全新的概念,但在大模型时代,它被赋予了全新的生命力,简单来说,Agent是一个能够自主感知环境、理解任务、制定... 目录一、回归本源:到底什么是Agent?二、核心链路拆解:Agent的"大脑"与"四肢"1. 规划模

Python+wxPython开发一个文件属性比对工具

《Python+wxPython开发一个文件属性比对工具》在日常的文件管理工作中,我们经常会遇到同一个文件存在多个版本,或者需要验证备份文件与源文件是否一致,下面我们就来看看如何使用wxPython模... 目录引言项目背景与需求应用场景核心需求运行结果技术选型程序设计界面布局核心功能模块关键代码解析文件大

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

Python的pandas库基础知识超详细教程

《Python的pandas库基础知识超详细教程》Pandas是Python数据处理核心库,提供Series和DataFrame结构,支持CSV/Excel/SQL等数据源导入及清洗、合并、统计等功能... 目录一、配置环境二、序列和数据表2.1 初始化2.2  获取数值2.3 获取索引2.4 索引取内容2

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建