简体   繁体   中英

Add link on course page depending on course custom field

I am using Moodle 2.7 and have the following custom field for the courses in the database table mdl_course_info_field :

Full name: School course

Shrot name: school

Type: Menu of choices

Choices:

  • Highschool course

  • Prepschool course

The target was to show the link on every course page, where under the settings the chechbox for Highschool course is used. In the file mymoodle/local/link/functions.js there is the link :

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

How to check, if the checkbox is choosen an then to show the link on the course page?

You can use a renderer to display a course header:

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

So you could include the school link in the course header - here is an example:

In /course/format/formatname/lib.php

Add this function to 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);
}

Also add this class to /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;
    }
}

Then in /course/format/formatname/renderer.php

Add this function to 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;
}

I suggest you add the link to the course as a URL resource.

Then make sure 'conditional activities' are enabled for the site.

Finally edit the URL activity to restrict access to users who have the correct user profile field.

See https://docs.moodle.org/27/en/Conditional_activities_settings for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM