Moodle开发笔记4-Theme开发

2024-04-19 22:48
文章标签 开发 笔记 theme moodle

本文主要是介绍Moodle开发笔记4-Theme开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

通过 Theme,你可以修改整个 moodle site的外观和风格。

 

一个 Theme最起码包含下列东东:

l       config.php 它设置了 theme要用到的 css file,以及其他 style option

l       styles.php 它是 standard的,不用动它。

l       header.html 它是所有 moodle page的开头部分。它实际是一个 php file

l       footer.html 它是所有 moodle page的结尾部分。它实际是一个 php file

l       theme用到的 css files。 你不需要完全重写所有的 theme css,你可以使用 standard theme css,然后在其基础上只重写你需要修改的部分。

 

Theme主要是通过 CSS file来影响外观,但这里并不讲解 css,而是 focus on programmer感兴趣、的 header.html and footer.html,这 2 file同样也是 theme重要部分。

 

下面的例子是

1.       通过对 header.html 的修改来改变导航 link path

2.       通过对 footer.html 的修改来添加 a link page footer

 

通过对 header.html 的修改来改变 navigation breadcrumbs (导航 link path .

具体要求是在 navigation breadcrumbs 中的 course link 之前添加该 course category link 。例如原来的 navigation link path ”Moodle Home > Course 01”, 那么修改之后要在 Course 01 之前添加 category link ,变成“ Moodle Home > Miscellaneous > Course 01 .

 

我们先来看看 moodle standard theme header.html

<html<?php echo $direction ?>>

<head>

<?php echo $meta ?>

<meta name="keywords" content="moodle, <?php echo $title ?> " />

<title><?php echo $title ?></title>

<link rel="shortcut icon"

href="<?php echo $CFG->themewww .'/'. current_theme() ?>/favicon.ico" />

<?php include("$CFG->javascript"); ?>

</head>

<body<?php

echo " $bodytags";

if ($focus) {

echo " οnlοad=/"setfocus()/"";

}

?>>

<div id="page">

<?php

//Accessibility: 'headermain' is now H1, see theme/standard/styles_layout.css: .headermain

if ($home) { // This is what gets printed on the home page only,下列输入只 for home page

?>

<?php print_container_start(true, '', ' header-home '); ?>

<h1 class="headermain"><?php echo $heading ?></h1>

<div class="headermenu"><?php echo $menu ?></div>

<?php print_container_end(); ?>

<?php } else if ($heading) {

// This is what gets printed on any other page with a heading

?>

<?php print_container_start(true, '', 'header' ); ?>

<h1 class="headermain"><?php echo $heading ?></h1>

<div class="headermenu"><?php echo $menu ?></div>

<?php print_container_end(); ?>

<?php } ?>

 

<?php

//Accessibility: breadcrumb trail/navbar now a DIV, not a table.

if ( $navigation ) { // This is the navigation bar with breadcrumbs

?>

<div class="navbar clearfix">

<!-- standard breadcrumb navigation on a page -->

<div class="breadcrumb"> <?php print_navigation($navigation); ?> </div>

<div class="navbutton"><?php echo $button; ?></div>

</div>

<?php } else if ($heading) { // If no navigation, but a heading, then print a line

?>

<hr />

<?php } ?>

<!-- END OF HEADER -->

<?php print_container_start(false, '', 'content' ); ?>

 

其中:

l       变量 $direction, $home, $meta, $navigation 等已经在之前的某个地方被定义好了

l       print_container_start and print_container_en d函数。这 2个函数用于 structure a page in a known way so that it is easier to change the look of the page by using CSS 每一个 print_container_start会对应一个 print_container_end print_container_start的第三个参数是一个 id来判断当前的 container是用什么 css style structure 。例如上面的 header.html使用了 3 print_container_start id分别是 header_home, header, content。其中 最后一个 print_container_start并没有使用 print_container_end,因为 print_container_end写在 footer.html

l       print_navigation 函数是用来 create standard breadcrumb navigation on a page

 

1.       假设我们自定义的 theme name 为“ customnav”,首先把 moodle_home/theme目录下创建目录“ customnav”,然后把 moodle_home/theme/standardlogo 目录下的所有 files都赋值到“ customnav”下。这样实际上你已经创建了一个 theme,它和 standardlogo theme一摸一样,只不过 theme name不同。

 

2.       要改变 navigation breadcrumbs ,我们只需要修改 header.html。根据上面的 header.html代码,我们要在 call print_navigation 函数之前修改变量 $ navigation ,使其包含 category info 即可

 

$ navigation 变量是一个 key-value array,它有 2 key:

l       newnav 其值总是 1 ,表示 using the 'new' navigation API 。当前例子不需要理会它

l       navlinks 其值是一个 string,它才是真正包含要输出的 navigation info的东东 ,我们要改动的就是该变量的值。

 

先分析 $navigation['navlinks'] 的值的格式:

它的每一个 navigation node都会用 <li> </li>包住。如果 navigation node的数目小于 2个,那么表明并没有进入到 course node里,不需要添加 category node,这是因为 course node一定是放在 $navigation['navlinks'] 里的第二个元素。

 

下面代码的逻辑是:

1)  先把 $navigation['navlinks'] explode to an array

2)  查看 array element count 是否大于等于 2

3)  如果是,则获取第 2 element,并从中提取 course short name(即去掉前后的 <li>/</li>/<a>/</a>

4)  然后根据提取的 course short name 来从数据库中获取该 course属于的 category

5)  然后重新把组装 $navigation['navlinks'] ,即把 category link和之前 explode出来的 array组装到该变量。

6)  最后就可以执行 print_navigation 函数了。

 

/// If there are navigation pieces, separate them out.

if (!empty($navigation['navlinks'])) {

    $pieces = explode ('<li>', $navigation['navlinks']);

} else {

    $pieces = array();

}

 

/// If its possible there is a course piece, find course short name.

$sname = '';

if (count($pieces) > 1) {

$sname = $pieces[1];

// 查看它是不是(通过查看是否包含字串 /course/view.php )有则表示有 <a>

    if (strpos($sname, '/course/view.php') !== false) {

        $sname = substr($sname, 0, strpos($sname, '</a></li>'));

    } else {

        $sname = substr($sname, 0, strpos($sname, '</li>'));

    }

    $sname = trim(substr($sname, strrpos ($sname, '>')+1));

}

 

///find the category by course short name

if ($sname != '') {

    $select = 'SELECT c.id, cc.id as catid, cc.name ';

    $from   = 'FROM '.$CFG->prefix.'course c ';

    $join  = 'INNER JOIN '.$CFG->prefix.'course_categories cc ON cc.id = c.category ';

    $where  = 'WHERE c.shortname = /''.$sname.'/'';

    $sql    = $select.$from.$join.$where;

    $catinfo = get_record_sql($sql);

}

 

/// If we have category information, place it in the nav.

if (!empty($catinfo)) {

    $catlink = $CFG->wwwroot.'/course/category.php?id='.$catinfo->catid;

$catpiece  = '<li>';

// get_separator function returns a character as separator of the breadcrumb pieces

$catpiece .= get_separator();

// $CFG->framename is a configuration item that contains the name of any defined

//HTML frame that the site may sit in (typically none).

$catpiece .= '<a οnclick="this.target=/''.$CFG->framename.'/'"

href="'.$catlink.'">'.$catinfo->name.'</a>';

    $catpiece .= '</li>';

 

    $navigation['navlinks'] = $pieces[0];

    foreach ($pieces as $i => $piece) {

        if ($i == 0) {

            continue;

        } else if ($i == 1) { //插入到第一个 element之前

            $navigation['navlinks'] .= $catpiece;

        }

        $navigation['navlinks'] .= '<li>'.$piece;

    }

}

 

 

通过对 footer.html 的修改来在 page footer 添加 a link

footer.html

print_container_start(false, '', 'footer');

后面添加下列代码

 

$termsn = '/'. SITEID .'/termsofuse.html';

$footertext = '';

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

 

/// Look for terms of use page

if (file_exists($CFG->dataroot.$termsn) ) {

    $link = get_file_url($termsn, null, 'httpscoursefile') ;

    /// To use with pop-up... Must be a relative URL.

    $plink = str_replace($CFG->wwwroot, '', $link);

} else {

    $link = false;

}

if ($link) {

    $footertext .= '<a href="'.$link .'" target="_blank">Terms of Use</a>';

} else {

     $footertext .= 'Terms of Use';

}

echo '<div class="footerlinks">'.$footertext.'</div><br />'; 

 

特别要注意的是:

1 $termsn = '/'. SITEID .'/termsofuse.html';

And file_exists($CFG->dataroot.$termsn)

 

SITEID 的值总是“ 1 (but it is safer to use the constant defined for this ),那么“ $CFG->dataroot /1/xxx ”则是指向 the site directory of the Moodle data area 下的东东。

 

2 get_file_url($termsn, null, 'httpscoursefile')

注意: get_file_url 函数的第一个参数不需要加 $CFG->dataroot,这和 file_exists不同

 

但这时上面的 link还不能用,因为

$CFG->dataroot .'/'. SITEID .'/termsofuse.html'

目前并不存在。你需要 upload a termsofuse.html moodle site

1.       login as admin role

2.       access “Site Administration > Front Page > Site Files”, click “upload a file”

 

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



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

相关文章

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark