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

时间:2016-04-07 17:37:11

标签: javascript php mysql moodle

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

  

全名:学校课程

     

Shrot名字:学校

     

类型:选项菜单

     

选择:

     
      
  • 高中课程

  •   
  • Prepschool课程

  •   

目标是在每个课程页面上显示链接,其中使用高中课程 chechbox 设置。在文件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>');
}

如何检查,如果复选框选择然后以显示课程页面上的链接?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我建议您将该课程的链接添加为网址资源。

然后确保为该网站启用了“条件活动”。

最后编辑URL活动以限制对具有正确用户个人资料字段的用户的访问权限。

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