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 grade's core interaction API.
19
 *
20
 * @package    customcertelement_grade
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_grade;
26
 
27
/**
28
 * Grade - Course
29
 */
30
define('CUSTOMCERT_GRADE_COURSE', '0');
31
 
32
/**
33
 * The customcert element grade's core interaction API.
34
 *
35
 * @package    customcertelement_grade
36
 * @copyright  2013 Mark Nelson <markn@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class element extends \mod_customcert\element {
40
 
41
    /**
42
     * This function renders the form elements when adding a customcert element.
43
     *
44
     * @param \MoodleQuickForm $mform the edit_form instance
45
     */
46
    public function render_form_elements($mform) {
47
        global $COURSE;
48
 
49
        // Get the grade items we can display.
50
        $gradeitems = [];
51
        $gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade');
52
        $gradeitems = $gradeitems + \mod_customcert\element_helper::get_grade_items($COURSE);
53
 
54
        // The grade items.
55
        $mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
56
        $mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_grade');
57
 
58
        // The grade format.
59
        $mform->addElement('select', 'gradeformat', get_string('gradeformat', 'customcertelement_grade'),
60
            self::get_grade_format_options());
61
        $mform->setType('gradeformat', PARAM_INT);
62
        $mform->addHelpButton('gradeformat', 'gradeformat', 'customcertelement_grade');
63
 
64
        parent::render_form_elements($mform);
65
    }
66
 
67
    /**
68
     * This will handle how form data will be saved into the data column in the
69
     * customcert_elements table.
70
     *
71
     * @param \stdClass $data the form data.
72
     * @return string the json encoded array
73
     */
74
    public function save_unique_data($data) {
75
        // Array of data we will be storing in the database.
76
        $arrtostore = [
77
            'gradeitem' => $data->gradeitem,
78
            'gradeformat' => $data->gradeformat,
79
        ];
80
 
81
        // Encode these variables before saving into the DB.
82
        return json_encode($arrtostore);
83
    }
84
 
85
    /**
86
     * Handles rendering the element on the pdf.
87
     *
88
     * @param \pdf $pdf the pdf object
89
     * @param bool $preview true if it is a preview, false otherwise
90
     * @param \stdClass $user the user we are rendering this for
91
     */
92
    public function render($pdf, $preview, $user) {
93
        // If there is no element data, we have nothing to display.
94
        if (empty($this->get_data())) {
95
            return;
96
        }
97
 
98
        $courseid = \mod_customcert\element_helper::get_courseid($this->id);
99
 
100
        // Decode the information stored in the database.
101
        $gradeinfo = json_decode($this->get_data());
102
        $gradeitem = $gradeinfo->gradeitem;
103
        $gradeformat = $gradeinfo->gradeformat;
104
 
105
        // If we are previewing this certificate then just show a demonstration grade.
106
        if ($preview) {
107
            $courseitem = \grade_item::fetch_course_item($courseid);
108
            $grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat);;
109
        } else {
110
            if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
111
                $grade = \mod_customcert\element_helper::get_course_grade_info(
112
                    $courseid,
113
                    $gradeformat,
114
                    $user->id
115
                );
116
            } else if (strpos($gradeitem, 'gradeitem:') === 0) {
117
                $gradeitemid = substr($gradeitem, 10);
118
                $grade = \mod_customcert\element_helper::get_grade_item_info(
119
                    $gradeitemid,
120
                    $gradeformat,
121
                    $user->id
122
                );
123
            } else {
124
                $grade = \mod_customcert\element_helper::get_mod_grade_info(
125
                    $gradeitem,
126
                    $gradeformat,
127
                    $user->id
128
                );
129
            }
130
 
131
            if ($grade) {
132
                $grade = $grade->get_displaygrade();
133
            }
134
        }
135
 
136
        \mod_customcert\element_helper::render_content($pdf, $this, $grade);
137
    }
138
 
139
    /**
140
     * Render the element in html.
141
     *
142
     * This function is used to render the element when we are using the
143
     * drag and drop interface to position it.
144
     *
145
     * @return string the html
146
     */
147
    public function render_html() {
148
        global $COURSE;
149
 
150
        // If there is no element data, we have nothing to display.
151
        if (empty($this->get_data())) {
152
            return;
153
        }
154
 
155
        // Decode the information stored in the database.
156
        $gradeinfo = json_decode($this->get_data());
157
 
158
        $courseitem = \grade_item::fetch_course_item($COURSE->id);
159
 
160
        $grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat);
161
 
162
        return \mod_customcert\element_helper::render_html_content($this, $grade);
163
    }
164
 
165
    /**
166
     * Sets the data on the form when editing an element.
167
     *
168
     * @param \MoodleQuickForm $mform the edit_form instance
169
     */
170
    public function definition_after_data($mform) {
171
        // Set the item and format for this element.
172
        if (!empty($this->get_data())) {
173
            $gradeinfo = json_decode($this->get_data());
174
 
175
            $element = $mform->getElement('gradeitem');
176
            $element->setValue($gradeinfo->gradeitem);
177
 
178
            $element = $mform->getElement('gradeformat');
179
            $element->setValue($gradeinfo->gradeformat);
180
        }
181
 
182
        parent::definition_after_data($mform);
183
    }
184
 
185
    /**
186
     * This function is responsible for handling the restoration process of the element.
187
     *
188
     * We will want to update the course module the grade element is pointing to as it will
189
     * have changed in the course restore.
190
     *
191
     * @param \restore_customcert_activity_task $restore
192
     */
193
    public function after_restore($restore) {
194
        global $DB;
195
 
196
        $gradeinfo = json_decode($this->get_data());
197
 
198
        $isgradeitem = false;
199
        $oldid = $gradeinfo->gradeitem;
200
        if (str_starts_with($gradeinfo->gradeitem, 'gradeitem:')) {
201
            $isgradeitem = true;
202
            $oldid = str_replace('gradeitem:', '', $gradeinfo->gradeitem);
203
        }
204
 
205
        $itemname = $isgradeitem ? 'grade_item' : 'course_module';
206
        if ($newitem = \restore_dbops::get_backup_ids_record($restore->get_restoreid(), $itemname, $oldid)) {
207
            $gradeinfo->gradeitem = '';
208
            if ($isgradeitem) {
209
                $gradeinfo->gradeitem = 'gradeitem:';
210
            }
211
            $gradeinfo->gradeitem = $gradeinfo->gradeitem . $newitem->newitemid;
212
            $DB->set_field('customcert_elements', 'data', $this->save_unique_data($gradeinfo), ['id' => $this->get_id()]);
213
        }
214
    }
215
 
216
    /**
217
     * Helper function to return all the possible grade formats.
218
     *
219
     * @return array returns an array of grade formats
220
     */
221
    public static function get_grade_format_options() {
222
        $gradeformat = [];
223
        $gradeformat[GRADE_DISPLAY_TYPE_REAL] = get_string('gradepoints', 'customcertelement_grade');
224
        $gradeformat[GRADE_DISPLAY_TYPE_PERCENTAGE] = get_string('gradepercent', 'customcertelement_grade');
225
        $gradeformat[GRADE_DISPLAY_TYPE_LETTER] = get_string('gradeletter', 'customcertelement_grade');
226
 
227
        return $gradeformat;
228
    }
229
}