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 gradeitemname's core interaction API.
|
|
|
19 |
*
|
|
|
20 |
* @package customcertelement_gradeitemname
|
|
|
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_gradeitemname;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* The customcert element gradeitemname's core interaction API.
|
|
|
29 |
*
|
|
|
30 |
* @package customcertelement_gradeitemname
|
|
|
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 |
global $COURSE;
|
|
|
43 |
|
|
|
44 |
$mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_gradeitemname'),
|
|
|
45 |
\mod_customcert\element_helper::get_grade_items($COURSE));
|
|
|
46 |
$mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_gradeitemname');
|
|
|
47 |
|
|
|
48 |
parent::render_form_elements($mform);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* This will handle how form data will be saved into the data column in the
|
|
|
53 |
* customcert_elements table.
|
|
|
54 |
*
|
|
|
55 |
* @param \stdClass $data the form data
|
|
|
56 |
* @return string the text
|
|
|
57 |
*/
|
|
|
58 |
public function save_unique_data($data) {
|
|
|
59 |
if (!empty($data->gradeitem)) {
|
|
|
60 |
return $data->gradeitem;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
return '';
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Handles rendering the element on the pdf.
|
|
|
68 |
*
|
|
|
69 |
* @param \pdf $pdf the pdf object
|
|
|
70 |
* @param bool $preview true if it is a preview, false otherwise
|
|
|
71 |
* @param \stdClass $user the user we are rendering this for
|
|
|
72 |
*/
|
|
|
73 |
public function render($pdf, $preview, $user) {
|
|
|
74 |
// Check that the grade item is not empty.
|
|
|
75 |
if (!empty($this->get_data())) {
|
|
|
76 |
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_grade_item_name());
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Render the element in html.
|
|
|
82 |
*
|
|
|
83 |
* This function is used to render the element when we are using the
|
|
|
84 |
* drag and drop interface to position it.
|
|
|
85 |
*
|
|
|
86 |
* @return string the html
|
|
|
87 |
*/
|
|
|
88 |
public function render_html() {
|
|
|
89 |
// Check that the grade item is not empty.
|
|
|
90 |
if (!empty($this->get_data())) {
|
|
|
91 |
return \mod_customcert\element_helper::render_html_content($this, $this->get_grade_item_name());
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
return '';
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Sets the data on the form when editing an element.
|
|
|
99 |
*
|
|
|
100 |
* @param \MoodleQuickForm $mform the edit_form instance
|
|
|
101 |
*/
|
|
|
102 |
public function definition_after_data($mform) {
|
|
|
103 |
if (!empty($this->get_data())) {
|
|
|
104 |
$element = $mform->getElement('gradeitem');
|
|
|
105 |
$element->setValue($this->get_data());
|
|
|
106 |
}
|
|
|
107 |
parent::definition_after_data($mform);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* This function is responsible for handling the restoration process of the element.
|
|
|
112 |
*
|
|
|
113 |
* We will want to update the course module the grade element is pointing to as it will
|
|
|
114 |
* have changed in the course restore.
|
|
|
115 |
*
|
|
|
116 |
* @param \restore_customcert_activity_task $restore
|
|
|
117 |
*/
|
|
|
118 |
public function after_restore($restore) {
|
|
|
119 |
global $DB;
|
|
|
120 |
|
|
|
121 |
$gradeinfo = $this->get_data();
|
|
|
122 |
|
|
|
123 |
$isgradeitem = false;
|
|
|
124 |
$oldid = $gradeinfo;
|
|
|
125 |
if (str_starts_with($gradeinfo, 'gradeitem:')) {
|
|
|
126 |
$isgradeitem = true;
|
|
|
127 |
$oldid = str_replace('gradeitem:', '', $gradeinfo);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$itemname = $isgradeitem ? 'grade_item' : 'course_module';
|
|
|
131 |
if ($newitem = \restore_dbops::get_backup_ids_record($restore->get_restoreid(), $itemname, $oldid)) {
|
|
|
132 |
$gradeinfo = new \stdClass();
|
|
|
133 |
$gradeinfo->gradeitem = '';
|
|
|
134 |
if ($isgradeitem) {
|
|
|
135 |
$gradeinfo->gradeitem = 'gradeitem:';
|
|
|
136 |
}
|
|
|
137 |
$gradeinfo->gradeitem = $gradeinfo->gradeitem . $newitem->newitemid;
|
|
|
138 |
$DB->set_field('customcert_elements', 'data', $this->save_unique_data($gradeinfo), ['id' => $this->get_id()]);
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Helper function that returns the grade item name.
|
|
|
144 |
*
|
|
|
145 |
* @return string
|
|
|
146 |
*/
|
|
|
147 |
protected function get_grade_item_name() : string {
|
|
|
148 |
global $DB;
|
|
|
149 |
|
|
|
150 |
$gradeitem = $this->get_data();
|
|
|
151 |
|
|
|
152 |
if (strpos($gradeitem, 'gradeitem:') === 0) {
|
|
|
153 |
$gradeitemid = substr($gradeitem, 10);
|
|
|
154 |
$gradeitem = \grade_item::fetch(['id' => $gradeitemid]);
|
|
|
155 |
|
|
|
156 |
// If the gradeitem was not found, return an empty string.
|
|
|
157 |
// This will effectively prevent the element from rendering.
|
|
|
158 |
return $gradeitem ? $gradeitem->get_name() : '';
|
|
|
159 |
} else {
|
|
|
160 |
if (!$cm = $DB->get_record('course_modules', ['id' => $gradeitem])) {
|
|
|
161 |
return '';
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
if (!$module = $DB->get_record('modules', ['id' => $cm->module])) {
|
|
|
165 |
return '';
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
$params = [
|
|
|
169 |
'itemtype' => 'mod',
|
|
|
170 |
'itemmodule' => $module->name,
|
|
|
171 |
'iteminstance' => $cm->instance,
|
|
|
172 |
'courseid' => $cm->course,
|
|
|
173 |
'itemnumber' => 0,
|
|
|
174 |
];
|
|
|
175 |
|
|
|
176 |
$gradeitem = \grade_item::fetch($params);
|
|
|
177 |
|
|
|
178 |
// If the gradeitem was not found, return an empty string.
|
|
|
179 |
// This will effectively prevent the element from rendering.
|
|
|
180 |
return $gradeitem ? $gradeitem->get_name() : '';
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
}
|