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

  • 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:

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.

主站蜘蛛池模板: 麻江县| 确山县| 湘西| 特克斯县| 伽师县| 西藏| 塔河县| 香港| 灵丘县| 横峰县| 灵寿县| 四子王旗| 曲阜市| 固阳县| 唐河县| 康保县| 宝清县| SHOW| 湾仔区| 星座| 萨嘎县| 新干县| 福建省| 株洲县| 卢氏县| 金秀| 海淀区| 凌云县| 施甸县| 洪雅县| 宝丰县| 磐安县| 塔河县| 西安市| 宁河县| 肇源县| 肇州县| 黄骅市| 保山市| 无棣县| 惠安县|