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 teachername's core interaction API.
19
 *
20
 * @package    customcertelement_teachername
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_teachername;
26
 
27
/**
28
 * The customcert element teachername's core interaction API.
29
 *
30
 * @package    customcertelement_teachername
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
     * This function renders the form elements when adding a customcert element.
38
     *
39
     * @param \MoodleQuickForm $mform the edit_form instance
40
     */
41
    public function render_form_elements($mform) {
42
        $mform->addElement('select', 'teacher', get_string('teacher', 'customcertelement_teachername'),
43
            $this->get_list_of_teachers());
44
        $mform->addHelpButton('teacher', 'teacher', 'customcertelement_teachername');
45
 
46
        parent::render_form_elements($mform);
47
    }
48
 
49
    /**
50
     * This will handle how form data will be saved into the data column in the
51
     * customcert_elements table.
52
     *
53
     * @param \stdClass $data the form data
54
     * @return string the text
55
     */
56
    public function save_unique_data($data) {
57
        if (!empty($data->teacher)) {
58
            return $data->teacher;
59
        }
60
    }
61
 
62
    /**
63
     * Handles rendering the element on the pdf.
64
     *
65
     * @param \pdf $pdf the pdf object
66
     * @param bool $preview true if it is a preview, false otherwise
67
     * @param \stdClass $user the user we are rendering this for
68
     */
69
    public function render($pdf, $preview, $user) {
70
        global $DB;
71
 
72
        $teacher = $DB->get_record('user', ['id' => $this->get_data()]);
73
        $teachername = fullname($teacher);
74
 
75
        \mod_customcert\element_helper::render_content($pdf, $this, $teachername);
76
    }
77
 
78
    /**
79
     * Render the element in html.
80
     *
81
     * This function is used to render the element when we are using the
82
     * drag and drop interface to position it.
83
     *
84
     * @return string the html
85
     */
86
    public function render_html() {
87
        global $DB;
88
 
89
        $teacher = $DB->get_record('user', ['id' => $this->get_data()]);
90
        $teachername = fullname($teacher);
91
 
92
        return \mod_customcert\element_helper::render_html_content($this, $teachername);
93
    }
94
 
95
    /**
96
     * Helper function to return the teachers for this course.
97
     *
98
     * @return array the list of teachers
99
     */
100
    protected function get_list_of_teachers() {
101
        global $PAGE;
102
 
103
        // Return early if we are in a site template.
104
        if ($PAGE->context->id == \context_system::instance()->id) {
105
            return [];
106
        }
107
 
108
        // The list of teachers to return.
109
        $teachers = [];
110
 
111
        // Now return all users who can manage the customcert in this context.
112
        if ($users = get_enrolled_users($PAGE->context, 'mod/customcert:manage')) {
113
            foreach ($users as $user) {
114
                $teachers[$user->id] = fullname($user);
115
            }
116
        }
117
 
118
        return $teachers;
119
    }
120
 
121
    /**
122
     * Sets the data on the form when editing an element.
123
     *
124
     * @param \MoodleQuickForm $mform the edit_form instance
125
     */
126
    public function definition_after_data($mform) {
127
        if (!empty($this->get_data())) {
128
            $element = $mform->getElement('teacher');
129
            $element->setValue($this->get_data());
130
        }
131
        parent::definition_after_data($mform);
132
    }
133
}