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 coursefield's core interaction API.
19
 *
20
 * @package    customcertelement_coursefield
21
 * @copyright  2019 Catalyst IT
22
 * @author     Dan Marsden
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace customcertelement_coursefield;
27
 
28
/**
29
 * The customcert element coursefield's core interaction API.
30
 *
31
 * @package    customcertelement_coursefield
32
 * @copyright  2019 Catalyst IT
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class element extends \mod_customcert\element {
36
 
37
    /**
38
     * This function renders the form elements when adding a customcert element.
39
     *
40
     * @param \MoodleQuickForm $mform the edit form instance
41
     */
42
    public function render_form_elements($mform) {
43
        // Get the user profile fields.
44
        $coursefields = [
45
            'fullname' => get_string('fullnamecourse'),
46
            'shortname' => get_string('shortnamecourse'),
47
            'idnumber' => get_string('idnumbercourse'),
48
        ];
49
        // Get the course custom fields.
50
        $arrcustomfields = [];
51
        $handler = \core_course\customfield\course_handler::create();
52
        $customfields = $handler->get_fields();
53
 
54
        foreach ($customfields as $field) {
55
            $arrcustomfields[$field->get('id')] = $field->get_formatted_name();
56
        }
57
 
58
        // Combine the two.
59
        $fields = $coursefields + $arrcustomfields;
60
        \core_collator::asort($fields);
61
 
62
        // Create the select box where the user field is selected.
63
        $mform->addElement('select', 'coursefield', get_string('coursefield', 'customcertelement_coursefield'), $fields);
64
        $mform->setType('coursefield', PARAM_ALPHANUM);
65
        $mform->addHelpButton('coursefield', 'coursefield', 'customcertelement_coursefield');
66
 
67
        parent::render_form_elements($mform);
68
    }
69
 
70
    /**
71
     * This will handle how form data will be saved into the data column in the
72
     * customcert_elements table.
73
     *
74
     * @param \stdClass $data the form data
75
     * @return string the text
76
     */
77
    public function save_unique_data($data) {
78
        return $data->coursefield;
79
    }
80
 
81
    /**
82
     * Handles rendering the element on the pdf.
83
     *
84
     * @param \pdf $pdf the pdf object
85
     * @param bool $preview true if it is a preview, false otherwise
86
     * @param \stdClass $user the user we are rendering this for
87
     */
88
    public function render($pdf, $preview, $user) {
89
 
90
        $courseid = \mod_customcert\element_helper::get_courseid($this->id);
91
        $course = get_course($courseid);
92
 
93
        \mod_customcert\element_helper::render_content($pdf, $this, $this->get_course_field_value($course, $preview));
94
    }
95
 
96
    /**
97
     * Render the element in html.
98
     *
99
     * This function is used to render the element when we are using the
100
     * drag and drop interface to position it.
101
     */
102
    public function render_html() {
103
        global $COURSE;
104
 
105
        return \mod_customcert\element_helper::render_html_content($this, $this->get_course_field_value($COURSE, true));
106
    }
107
 
108
    /**
109
     * Sets the data on the form when editing an element.
110
     *
111
     * @param \MoodleQuickForm $mform the edit form instance
112
     */
113
    public function definition_after_data($mform) {
114
        if (!empty($this->get_data())) {
115
            $element = $mform->getElement('coursefield');
116
            $element->setValue($this->get_data());
117
        }
118
        parent::definition_after_data($mform);
119
    }
120
 
121
    /**
122
     * Helper function that returns the field value in a human-readable format.
123
     *
124
     * @param \stdClass $course the course we are rendering this for
125
     * @param bool $preview Is this a preview?
126
     * @return string
127
     */
128
    protected function get_course_field_value(\stdClass $course, bool $preview) : string {
129
 
130
        // The user field to display.
131
        $field = $this->get_data();
132
        // The value to display - we always want to show a value here so it can be repositioned.
133
        if ($preview) {
134
            $value = $field;
135
        } else {
136
            $value = '';
137
        }
138
        if (is_number($field)) { // Must be a custom course profile field.
139
            $handler = \core_course\customfield\course_handler::create();
140
            $data = $handler->get_instance_data($course->id, true);
141
            if ($preview && empty($data[$field]->export_value())) {
142
                $fields = $handler->get_fields();
143
                $value = $fields[$field]->get('shortname');
144
            } else if (!empty($data[$field])) {
145
                $value = $data[$field]->export_value();
146
            }
147
 
148
        } else if (!empty($course->$field)) { // Field in the course table.
149
            $value = $course->$field;
150
        }
151
 
152
        $context = \mod_customcert\element_helper::get_context($this->get_id());
153
        return format_string($value, true, ['context' => $context]);
154
    }
155
}