- 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.
- 性能測試從零開始
- 做合成:Photoshop構圖+透視+紋理+造型+調色技術修煉
- Python 2.6 Graphics Cookbook
- 虛擬現實:開啟現實與夢想之門
- SolidWorks 2008機械設計一冊通
- Excel 2013電子表格處理
- Photoshop CS6實戰從入門到精通(超值版)
- Capture One 22 Pro高級實戰教程
- Photoshop CS6案例教程(第3版)
- Photoshop CC 2019 平面設計實例教程
- Mastering Zabbix
- TopSolid Wood軟件設計技術與應用
- Premiere Pro CC 2015中文版基礎與實例教程(第4版)
- Illustrator平面設計應用教程
- Moodle 2.0 for Business Beginner's Guide