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

相关文章

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

在 VSCode 中配置 C++ 开发环境的详细教程

《在VSCode中配置C++开发环境的详细教程》本文详细介绍了如何在VisualStudioCode(VSCode)中配置C++开发环境,包括安装必要的工具、配置编译器、设置调试环境等步骤,通... 目录如何在 VSCode 中配置 C++ 开发环境:详细教程1. 什么是 VSCode?2. 安装 VSCo

C#图表开发之Chart详解

《C#图表开发之Chart详解》C#中的Chart控件用于开发图表功能,具有Series和ChartArea两个重要属性,Series属性是SeriesCollection类型,包含多个Series对... 目录OverviChina编程ewSeries类总结OverviewC#中,开发图表功能的控件是Char

鸿蒙开发搭建flutter适配的开发环境

《鸿蒙开发搭建flutter适配的开发环境》文章详细介绍了在Windows系统上如何创建和运行鸿蒙Flutter项目,包括使用flutterdoctor检测环境、创建项目、编译HAP包以及在真机上运... 目录环境搭建创建运行项目打包项目总结环境搭建1.安装 DevEco Studio NEXT IDE

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

linux-基础知识3

打包和压缩 zip 安装zip软件包 yum -y install zip unzip 压缩打包命令: zip -q -r -d -u 压缩包文件名 目录和文件名列表 -q:不显示命令执行过程-r:递归处理,打包各级子目录和文件-u:把文件增加/替换到压缩包中-d:从压缩包中删除指定的文件 解压:unzip 压缩包名 打包文件 把压缩包从服务器下载到本地 把压缩包上传到服务器(zip

计组基础知识

操作系统的特征 并发共享虚拟异步 操作系统的功能 1、资源分配,资源回收硬件资源 CPU、内存、硬盘、I/O设备。2、为应⽤程序提供服务操作系统将硬件资源的操作封装起来,提供相对统⼀的接⼝(系统调⽤)供开发者调⽤。3、管理应⽤程序即控制进程的⽣命周期:进程开始时的环境配置和资源分配、进程结束后的资源回收、进程调度等。4、操作系统内核的功能(1)进程调度能⼒: 管理进程、线

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设