根据课程自定义字段在课程页面上添加链接


Add link on course page depending on course custom field

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

全名:学校课程

灌木名称:学校

类型:选项菜单

选择:

  • 高中课程

  • 预备课程

目标是在每个课程页面上显示链接,在设置下使用高中课程复选框。文件mymoodle/local/link/functions.js中有链接:

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

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

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

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

所以你可以在课程标题中包含学校链接——这里有一个例子:

/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);
}

同时将此类添加到/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;
}

我建议您将课程链接添加为URL资源。

然后确保为站点启用了"条件活动"。

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

请参阅https://docs.moodle.org/27/en/Conditional_activities_settings了解更多详细信息。