Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of the customcert module for Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * This file contains the customcert element coursename's core interaction API.
19
 *
20
 * @package    customcertelement_coursename
21
 * @copyright  2013 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace customcertelement_coursename;
26
 
27
/**
28
 * The customcert element coursename's core interaction API.
29
 *
30
 * @package    customcertelement_coursename
31
 * @copyright  2013 Mark Nelson <markn@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class element extends \mod_customcert\element {
35
 
36
    /**
37
     * The course short name.
38
     */
39
    const COURSE_SHORT_NAME = 1;
40
 
41
    /**
42
     * The course fullname.
43
     */
44
    const COURSE_FULL_NAME = 2;
45
 
46
    /**
47
     * This function renders the form elements when adding a customcert element.
48
     *
49
     * @param \MoodleQuickForm $mform the edit_form instance
50
     */
51
    public function render_form_elements($mform) {
52
        // The course name display options.
53
        $mform->addElement('select', 'coursenamedisplay', get_string('coursenamedisplay', 'customcertelement_coursename'),
54
            self::get_course_name_display_options());
55
        $mform->setType('coursenamedisplay', PARAM_INT);
56
        $mform->addHelpButton('coursenamedisplay', 'coursenamedisplay', 'customcertelement_coursename');
57
 
58
        parent::render_form_elements($mform);
59
    }
60
 
61
    /**
62
     * This will handle how form data will be saved into the data column in the
63
     * customcert_elements table.
64
     *
65
     * @param \stdClass $data the form data
66
     * @return string the text
67
     */
68
    public function save_unique_data($data) {
69
        return $data->coursenamedisplay;
70
    }
71
 
72
    /**
73
     * Handles rendering the element on the pdf.
74
     *
75
     * @param \pdf $pdf the pdf object
76
     * @param bool $preview true if it is a preview, false otherwise
77
     * @param \stdClass $user the user we are rendering this for
78
     */
79
    public function render($pdf, $preview, $user) {
80
        \mod_customcert\element_helper::render_content($pdf, $this, $this->get_course_name_detail());
81
    }
82
 
83
    /**
84
     * Render the element in html.
85
     *
86
     * This function is used to render the element when we are using the
87
     * drag and drop interface to position it.
88
     *
89
     * @return string the html
90
     */
91
    public function render_html() {
92
        return \mod_customcert\element_helper::render_html_content($this, $this->get_course_name_detail());
93
    }
94
 
95
    /**
96
     * Sets the data on the form when editing an element.
97
     *
98
     * @param \MoodleQuickForm $mform the edit_form instance
99
     */
100
    public function definition_after_data($mform) {
101
        if (!empty($this->get_data())) {
102
            $element = $mform->getElement('coursenamedisplay');
103
            $element->setValue($this->get_data());
104
        }
105
        parent::definition_after_data($mform);
106
    }
107
 
108
    /**
109
     * Helper function that returns the selected course name detail (i.e. name or short description) for display.
110
     *
111
     * @return string
112
     */
113
    protected function get_course_name_detail(): string {
114
        $courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
115
        $course = get_course($courseid);
116
        $context = \mod_customcert\element_helper::get_context($this->get_id());
117
 
118
        // The name field to display.
119
        $field = $this->get_data();
120
        // The name value to display.
121
        $value = $course->fullname;
122
        if ($field == self::COURSE_SHORT_NAME) {
123
            $value = $course->shortname;
124
        }
125
 
126
        return format_string($value, true, ['context' => $context]);
127
    }
128
 
129
    /**
130
     * Helper function to return all the possible name display options.
131
     *
132
     * @return array returns an array of name options
133
     */
134
    public static function get_course_name_display_options(): array {
135
        return [
136
            self::COURSE_FULL_NAME => get_string('coursefullname', 'customcertelement_coursename'),
137
            self::COURSE_SHORT_NAME => get_string('courseshortname', 'customcertelement_coursename'),
138
        ];
139
    }
140
}