- Moodle 1.9 Extension Development
- Jonathan Moore Michael Churchward
- 694字
- 2021-08-06 17:24:06
Reviewing a real world block
In this section we will take a look at a real world block. This block was originally created for a large college. It's called the Instructor Contact Block. It is designed to make it easier for students to communicate with their instructors. Although we won't cover every line of code, there are a couple of things to pay special attention to. First, we will see a real world example of creating a new block to improve Moodle's functionality. Secondly, we will see how to access some information from a user profile and display it on the screen. Additionally, we will take a quick look at how to gather some configuration data for our block.
Let's have a look at the overall results of the Instructor Contact Block. The following screenshot shows an example of the block configured for an instructor in a course:

The next screenshot shows the settings screen for this block:

We will now look in more detail at the code that generates this configuration screen.
Reviewing block_instructor_contact.php
Let's start by looking at the main block file, block_instructor_contact.php
. For space and complexity reasons we will not examine every line of code. This block uses more of the Moodle API to perform its operations. Below we examine parts of the get_content()
function. Note that we reference the global arrays $COURSE
, and $CFG
. We use the $COURSE
array to reference information from our course, such as pulling up a list of assigned instructors. Also notice that we use require_once
to include a library file for the block. We can do this any time that we want to separate out library functions that we will use across multiple files in a module, or even from other modules:
function get_content() { global $COURSE, $CFG; require_once($CFG->dirroot . '/blocks/instructor_contact/lib.php');
Let's look a little further down in the get_content
function. In this section, we introduce the get_complete_user_data()
function, which is used to pull information from the assigned instructor's profile.
if (!empty($this->config->selectinstructor)) { $user = get_complete_user_data('id', $this->config-> selectinstructor);
A little further into the file, we see the end results of pulling the user's data. We can now read the instructor's e-mail address for display, by using $user->email
. Note that in the full source code for this block we pull several fields from the profile. Also note that some faculty members wished to have the ability to provide an alternate e-mail address from the one in their profile. The if-else if structure of this section of the code first checks if an alternate value has been provided in the block configuration:
if (!empty($this->config->emailoverride)) { $email = $this->config->emailoverride; }//if else if (!empty($this->config->emailuserdefault)) { $email = $user->email; }
Configuring the instructor contact block
The instructor contact block also uses config_instance.html
, just like our Hello World block. We load the global $COURSE
array so that we can use it in a get_context_instance()
function call. We will use the resulting context later in this file to grab a list of all of the instructors for the course:
global $COURSE; $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
In this section, we check for data in two configuration fields. Note the similarity with the method that we use in the Hello World block:
$officehours = (!empty($this->config->officehours)) ? $this->config->officehours : ''; $emailoverride = (!empty($this->config->emailoverride)) ? $this->config->emailoverride : '';
Because a course may have more than one instructor, our Instructor Contact Block needs to have an option to pick which instructor's information to display. An instructor is defined as any user with the capability gradereport/grader:view
. To get our list of instructors we call the get_users_by_capability
function. We then take the results and iterate with a foreach
loop to create a pull-down menu for the user to select which instructor's information to display.
$users = get_users_by_capability($context,'gradereport/grader:view', 'u.id, u.username, u.firstname, u.lastname', 'u.lastname ASC, u.firstname ASC', '', '', '', '', false);
foreach ($users as $user) {
if ($this->config->selectinstructor == $user->id) {
echo '<option value="'.$user->id.'" selected="selected">' . fullname($user) . '</option>';
}
else {
echo '<option value="'.$user->id.'">' . fullname($user) . '</option>';
We can see in the case of our real world block that there is a bit more complexity. We also see how to access some additional functionality that Moodle provides to plugin authors.
- Cacti 0.8 Beginner's Guide
- Maya建模技術(shù)解析
- NHibernate 3.0 Cookbook
- Power Query從入門(mén)到精通
- Photoshop CS6完美創(chuàng)意設(shè)計(jì):不一樣的圖像藝術(shù)處理
- 三維建模與3D打印從入門(mén)到精通
- AutoCAD 2016中文版基礎(chǔ)教程(全圖解視頻版)
- Photoshop+AE UI動(dòng)效設(shè)計(jì)從新手到高手
- Moodle Course Conversion: Beginner's Guide
- 中文版AutoCAD 2022從入門(mén)到精通
- Science Teaching with Moodle 2.0
- NHibernate 3 Beginner's Guide
- Photoshop CC中文版基礎(chǔ)教程
- 剪輯師寶典:視頻剪輯思維與案例實(shí)戰(zhàn)
- 同花順軟件操作技巧與實(shí)戰(zhàn)指南