根据课程自定义字段在课程页面添加 link

Add link on course page depending on course custom field

我正在使用 Moodle 2.7 并且在 课程 中有以下 自定义字段数据库 table mdl_course_info_field:

Full name: School course

Shrot name: school

Type: Menu of choices

Choices:

  • Highschool course

  • Prepschool course

目标是在每个课程页面上显示 link,在设置下 chechbox 用于 Highschool 课程用来。在文件 mymoodle/local/link/functions.js 中有 link:

if($('#page-course-view-topcollmytheme .orangebar p')) {
    $('#page-course-view-topcollmytheme #section-0 .content > .summary').append('<button class="highschoollink">Hig school course</button>');
}

如何检查,如果复选框被选中然后以在课程页面上显示link?

我建议您将 link 作为 URL 资源添加到课程中。

然后确保为网站启用 'conditional activities'。

最后编辑 URL activity 以限制具有正确用户配置文件字段的用户访问。

有关详细信息,请参阅 https://docs.moodle.org/27/en/Conditional_activities_settings

您可以使用渲染器来显示课程 header:

https://tracker.moodle.org/browse/MDL-36048

因此您可以在课程 header 中包含学校 link - 这是一个示例:

/course/format/formatname/lib.php

将此功能添加到 class format_formatname

/**
 * Display's a header at the top of the sections.
 *
 * @return renderable class
 */
public function course_content_header() {
    global $DB, $PAGE, $USER;

    if (!isset($PAGE)) {
        return null;
    }

    // Only display if we are on the course-view page.
    if (strpos($PAGE->pagetype, 'course-view-') !== 0) {
        return null;
    }

    $sql = "SELECT d.data
            FROM {course_info_field} f
            JOIN {course_info_data} d ON d.fieldid = f.id AND d.courseid = :courseid
            WHERE f.shortname = :shortname";
    $params = array('courseid' => $this->courseid, 'shortname' => 'school');
    $schoolname = $DB->get_field_sql($sql, $params);
    $schoolurl = '';
    // You should store the school url in the database somewhere.
    // Using switch code for this example.
    switch ($schoolname) {
        case 'high school' :
            $schoolurl = new moodle_url('http://www.schoolsite.com');
            break;
        ...
    }

    return new format_formatname_coursecontentheader($schoolname, $schoolurl);
}

同时将此 class 添加到 /course/format/formatname/lib.php

class format_formatname_coursecontentheader implements renderable {
    /**
     * School name
     *
     * @var string $schoolname
     */
    public $schoolname;

    /**
     * School url
     *
     * @var string $schoolurl
     */
    public $schoolurl;

    /**
     * Class storing information to be passed and displayed in the course content header
     *
     * @param string $schoolname
     * @param moodle_url $schoolurl
     */
    public function __construct($schoolname, $schoolurl) {
        $this->schoolname = $schoolname;
        $this->schoolurl = $fields->schoolurl;
    }
}

然后在/course/format/formatname/renderer.php

将此功能添加到 class format_formatname_renderer

/**
 * Renders course header
 *
 * @param renderable $courseheader
 * @return string
 */
public function render_format_formatname_coursecontentheader($courseheader) {
    $output = '';

    $schoolname = $courseheader->schoolname;
    $schoolurl = $courseheader->schoolurl;

    $link = html_writer::link($schoolurl, $schoolname);

    $output .= html_writer::div($link, 'format-formatname-schoollink');

    return $output;
}