官术网_书友最值得收藏!

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:

Reviewing a real world block

The next screenshot shows the settings screen for this block:

Reviewing a real world 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.

主站蜘蛛池模板: 湖北省| 兴宁市| 麻阳| 双柏县| 井冈山市| 永顺县| 若尔盖县| 读书| 阜新市| 嘉定区| 磐安县| 永嘉县| 西华县| 金塔县| 敦煌市| 冷水江市| 祁门县| 晋江市| 辉南县| 阳曲县| 深泽县| 西藏| 师宗县| 松溪县| 赤水市| 太湖县| 黄陵县| 上高县| 寻甸| 苗栗县| 梁山县| 宁武县| 深圳市| 雷波县| 铜川市| 肇东市| 安福县| 盐源县| 东乌| 新安县| 灵山县|