Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * Form classes for editing badges
-
 
19
 *
-
 
20
 * @package    core
-
 
21
 * @subpackage badges
-
 
22
 * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
-
 
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
24
 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
-
 
25
 */
-
 
26
 
16
 
Línea 27... Línea 17...
27
namespace core_badges\form;
17
namespace core_badges\form;
Línea 28... Línea 18...
28
 
18
 
Línea 33... Línea 23...
33
require_once($CFG->libdir . '/filelib.php');
23
require_once($CFG->libdir . '/filelib.php');
Línea 34... Línea 24...
34
 
24
 
Línea 35... Línea 25...
35
use moodleform;
25
use moodleform;
36
 
26
 
37
/**
27
/**
-
 
28
 * Form classes for editing badges
-
 
29
 *
-
 
30
 * @package    core_badges
-
 
31
 * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
38
 * Form to edit badge details.
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 *
33
 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
Línea 40... Línea 34...
40
 */
34
 */
41
class badge extends moodleform {
35
class badge extends moodleform {
42
 
36
 
43
    /**
37
    /**
44
     * Defines the form
38
     * Defines the form
Línea 45... Línea 39...
45
     */
39
     */
46
    public function definition() {
40
    public function definition() {
47
        global $CFG;
41
        global $CFG, $SITE;
-
 
42
 
-
 
43
        $mform = $this->_form;
-
 
44
        $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
-
 
45
        $action = $this->_customdata['action'];
-
 
46
        if (array_key_exists('courseid', $this->_customdata)) {
-
 
47
            $courseid = $this->_customdata['courseid'];
-
 
48
        } else if (array_key_exists('badge', $this->_customdata)) {
-
 
49
            $courseid = $this->_customdata['badge']->courseid;
-
 
50
        }
Línea 48... Línea 51...
48
 
51
        if (!empty($courseid)) {
49
        $mform = $this->_form;
52
            $mform->addElement('hidden', 'courseid', $courseid);
50
        $badge = (isset($this->_customdata['badge'])) ? $this->_customdata['badge'] : false;
53
            $mform->setType('courseid', PARAM_INT);
51
        $action = $this->_customdata['action'];
54
        }
52
 
55
 
53
        $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
56
        $mform->addElement('header', 'badgedetails', get_string('badgedetails', 'badges'));
Línea 54... Línea 57...
54
        $mform->addElement('text', 'name', get_string('name'), array('size' => '70'));
57
        $mform->addElement('text', 'name', get_string('name'), ['size' => '70']);
55
        // When downloading badge, it will be necessary to clean the name as PARAM_FILE.
58
        // When downloading badge, it will be necessary to clean the name as PARAM_FILE.
56
        $mform->setType('name', PARAM_TEXT);
-
 
Línea 57... Línea 59...
57
        $mform->addRule('name', null, 'required');
59
        $mform->setType('name', PARAM_TEXT);
58
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
60
        $mform->addRule('name', null, 'required');
59
 
-
 
Línea -... Línea 61...
-
 
61
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
-
 
62
 
-
 
63
        $mform->addElement('text', 'version', get_string('version', 'badges'), ['size' => '70']);
-
 
64
        $mform->setType('version', PARAM_TEXT);
60
        $mform->addElement('text', 'version', get_string('version', 'badges'), array('size' => '70'));
65
 
-
 
66
        $languages = get_string_manager()->get_list_of_languages();
61
        $mform->setType('version', PARAM_TEXT);
67
        $mform->addElement('select', 'language', get_string('language'), $languages);
62
        $mform->addHelpButton('version', 'version', 'badges');
68
 
Línea 63... Línea 69...
63
 
69
        $mform->addElement(
64
        $languages = get_string_manager()->get_list_of_languages();
70
            'textarea',
65
        $mform->addElement('select', 'language', get_string('language'), $languages);
71
            'description',
Línea 66... Línea 72...
66
        $mform->addHelpButton('language', 'language', 'badges');
72
            get_string('description', 'badges'),
67
 
73
            'wrap="virtual" rows="8" cols="70" placeholder="' . s(get_string('descriptioninfo', 'badges')) . '"',
68
        $mform->addElement('textarea', 'description', get_string('description', 'badges'), 'wrap="virtual" rows="8" cols="70"');
74
        );
69
        $mform->setType('description', PARAM_NOTAGS);
75
        $mform->setType('description', PARAM_NOTAGS);
70
        $mform->addRule('description', null, 'required');
76
        $mform->addRule('description', null, 'required');
71
 
77
 
72
        $str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
-
 
73
        $imageoptions = array('maxbytes' => 262144, 'accepted_types' => array('optimised_image'));
-
 
74
        $mform->addElement('filepicker', 'image', $str, null, $imageoptions);
-
 
75
 
-
 
76
        if ($action == 'new') {
-
 
77
            $mform->addRule('image', null, 'required');
-
 
78
        } else {
-
 
79
            $currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
78
        $str = $action == 'new' ? get_string('badgeimage', 'badges') : get_string('newimage', 'badges');
80
            $mform->insertElementBefore($currentimage, 'image');
-
 
81
        }
-
 
-
 
79
        $imageoptions = ['maxbytes' => 262144, 'accepted_types' => ['optimised_image']];
82
        $mform->addHelpButton('image', 'badgeimage', 'badges');
80
        $mform->addElement('filepicker', 'image', $str, null, $imageoptions);
83
        $mform->addElement('text', 'imageauthorname', get_string('imageauthorname', 'badges'), array('size' => '70'));
81
 
84
        $mform->setType('imageauthorname', PARAM_TEXT);
-
 
85
        $mform->addHelpButton('imageauthorname', 'imageauthorname', 'badges');
-
 
Línea 86... Línea -...
86
        $mform->addElement('text', 'imageauthoremail', get_string('imageauthoremail', 'badges'), array('size' => '70'));
-
 
87
        $mform->setType('imageauthoremail', PARAM_TEXT);
82
        if ($action == 'new') {
Línea 88... Línea 83...
88
        $mform->addHelpButton('imageauthoremail', 'imageauthoremail', 'badges');
83
            $mform->addRule('image', null, 'required');
89
        $mform->addElement('text', 'imageauthorurl', get_string('imageauthorurl', 'badges'), array('size' => '70'));
-
 
90
        $mform->setType('imageauthorurl', PARAM_URL);
-
 
91
        $mform->addHelpButton('imageauthorurl', 'imageauthorurl', 'badges');
-
 
92
        $mform->addElement('text', 'imagecaption', get_string('imagecaption', 'badges'), array('size' => '70'));
-
 
93
        $mform->setType('imagecaption', PARAM_TEXT);
-
 
94
        $mform->addHelpButton('imagecaption', 'imagecaption', 'badges');
-
 
Línea -... Línea 84...
-
 
84
        } else {
-
 
85
            $currentimage = $mform->createElement('static', 'currentimage', get_string('currentimage', 'badges'));
-
 
86
            $mform->insertElementBefore($currentimage, 'image');
-
 
87
        }
-
 
88
        $mform->addElement('static', 'imageinfo', null, get_string('badgeimageinfo', 'badges'));
-
 
89
 
-
 
90
        $mform->addElement('text', 'imagecaption', get_string('imagecaption', 'badges'), ['size' => '70']);
95
        $mform->addElement('tags', 'tags', get_string('tags', 'badges'), ['itemtype' => 'badge', 'component' => 'core_badges']);
91
        $mform->setType('imagecaption', PARAM_TEXT);
96
 
92
 
97
        if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
93
        $mform->addElement('tags', 'tags', get_string('tags', 'badges'), ['itemtype' => 'badge', 'component' => 'core_badges']);
98
            $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
94
 
99
 
95
        $mform->addElement('header', 'issuerdetails', get_string('issuerdetails', 'badges'));
100
            $mform->addElement('text', 'issuername', get_string('name'), array('size' => '70'));
96
 
-
 
97
        $mform->addElement('text', 'issuername', get_string('issuername', 'badges'), ['size' => '70']);
101
            $mform->setType('issuername', PARAM_NOTAGS);
98
        $mform->setType('issuername', PARAM_NOTAGS);
102
            $mform->addRule('issuername', null, 'required');
99
        $mform->addRule('issuername', null, 'required');
103
            if (isset($CFG->badges_defaultissuername)) {
100
        $site = get_site();
104
                $mform->setDefault('issuername', $CFG->badges_defaultissuername);
101
        $issuername = $CFG->badges_defaultissuername ?: $site->fullname;
105
            }
102
        $mform->setDefault('issuername', $issuername);
106
            $mform->addHelpButton('issuername', 'issuername', 'badges');
-
 
Línea 107... Línea 103...
107
 
103
 
Línea 108... Línea 104...
108
            $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), array('size' => '70'));
104
        $mform->addElement('text', 'issuercontact', get_string('contact', 'badges'), ['size' => '70']);
109
            if (isset($CFG->badges_defaultissuercontact)) {
105
        if (isset($CFG->badges_defaultissuercontact)) {
110
                $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
106
            $mform->setDefault('issuercontact', $CFG->badges_defaultissuercontact);
111
            }
107
        }
112
            $mform->setType('issuercontact', PARAM_RAW);
108
        $mform->setType('issuercontact', PARAM_RAW);
113
            $mform->addHelpButton('issuercontact', 'contact', 'badges');
109
        $mform->addRule('issuercontact', null, 'email');
114
            // Set issuer URL.
110
 
115
            // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
111
        // Set issuer URL.
116
            $url = parse_url($CFG->wwwroot);
112
        // Have to parse URL because badge issuer origin cannot be a subfolder in wwwroot.
Línea 117... Línea 113...
117
            $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
113
        $url = parse_url($CFG->wwwroot);
118
            $mform->setType('issuerurl', PARAM_URL);
-
 
119
        }
114
        $mform->addElement('hidden', 'issuerurl', $url['scheme'] . '://' . $url['host']);
120
 
115
        $mform->setType('issuerurl', PARAM_URL);
121
        $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
116
 
122
 
117
        $mform->addElement('header', 'issuancedetails', get_string('issuancedetails', 'badges'));
123
        $issuancedetails = array();
118
 
Línea 163... Línea 158...
163
            $this->add_action_buttons();
158
            $this->add_action_buttons();
164
            $this->set_data($badge);
159
            $this->set_data($badge);
Línea 165... Línea 160...
165
 
160
 
166
            // Freeze all elements if badge is active or locked.
161
            // Freeze all elements if badge is active or locked.
167
            if ($badge->is_active() || $badge->is_locked()) {
162
            if ($badge->is_active() || $badge->is_locked()) {
168
                $mform->hardFreezeAllVisibleExcept(array());
163
                $mform->hardFreezeAllVisibleExcept([]);
169
            }
164
            }
170
        }
165
        }
Línea 171... Línea 166...
171
    }
166
    }
172
 
167
 
173
    /**
168
    /**
174
     * Load in existing data as form defaults
169
     * Load in existing data as form defaults
175
     *
170
     *
176
     * @param stdClass|array $badge object or array of default values
171
     * @param \core_badges\badge $badge object or array of default values
177
     */
172
     */
178
    public function set_data($badge) {
173
    public function set_data($badge) {
Línea 179... Línea 174...
179
        $defaultvalues = [];
174
        $defaultvalues = [];
180
        parent::set_data($badge);
175
        parent::set_data((object) $badge);
181
 
176
 
182
        if (!empty($badge->expiredate)) {
177
        if (!empty($badge->expiredate)) {
183
            $defaultvalues['expiry'] = 1;
178
            $defaultvalues['expiry'] = 1;
184
            $defaultvalues['expiredate'] = $badge->expiredate;
179
            $defaultvalues['expiredate'] = $badge->expiredate;
185
        } else if (!empty($badge->expireperiod)) {
180
        } else if (!empty($badge->expireperiod)) {
-
 
181
            $defaultvalues['expiry'] = 2;
-
 
182
            $defaultvalues['expireperiod'] = $badge->expireperiod;
-
 
183
        }
-
 
184
 
-
 
185
        if (!empty($badge->name)) {
186
            $defaultvalues['expiry'] = 2;
186
            $defaultvalues['name'] = trim($badge->name);
187
            $defaultvalues['expireperiod'] = $badge->expireperiod;
187
        }
Línea 188... Línea 188...
188
        }
188
 
189
        $defaultvalues['tags'] = \core_tag_tag::get_item_tags_array('core_badges', 'badge', $badge->id);
189
        $defaultvalues['tags'] = \core_tag_tag::get_item_tags_array('core_badges', 'badge', $badge->id);
Línea 190... Línea 190...
190
        $defaultvalues['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
190
        $defaultvalues['currentimage'] = print_badge_image($badge, $badge->get_context(), 'large');
191
 
191
 
192
        parent::set_data($defaultvalues);
192
        parent::set_data($defaultvalues);
193
    }
193
    }
194
 
194
 
195
    /**
-
 
Línea 196... Línea 195...
196
     * Validates form data
195
    /**
197
     */
196
     * Validates form data
-
 
197
     */
198
    public function validation($data, $files) {
198
    public function validation($data, $files) {
199
        global $DB;
-
 
200
        $errors = parent::validation($data, $files);
-
 
Línea 201... Línea 199...
201
 
199
        global $DB, $SITE;
202
        if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
200
 
203
            if (!empty($data['issuercontact']) && !validate_email($data['issuercontact'])) {
201
        // Trim badge name (to guarantee no badges are created with the same name but some extra spaces).
Línea 204... Línea 202...
204
                $errors['issuercontact'] = get_string('invalidemail');
202
        $data['name'] = trim($data['name']);
205
            }
203
 
206
        }
204
        $errors = parent::validation($data, $files);
Línea 207... Línea -...
207
 
-
 
208
        if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
-
 
209
            $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
-
 
210
        }
-
 
211
 
-
 
212
        if ($data['expiry'] == 1 && $data['expiredate'] <= time()) {
-
 
213
            $errors['expirydategr'] = get_string('error:invalidexpiredate', 'badges');
-
 
214
        }
-
 
215
 
-
 
216
        if ($data['imageauthoremail'] && !validate_email($data['imageauthoremail'])) {
-
 
217
            $errors['imageauthoremail'] = get_string('invalidemail');
-
 
218
        }
-
 
219
 
-
 
220
        // Check for duplicate badge names.
-
 
221
        if ($data['action'] == 'new') {
-
 
222
            $duplicate = $DB->record_exists_select('badge', 'name = :name AND status != :deleted',
-
 
223
                array('name' => $data['name'], 'deleted' => BADGE_STATUS_ARCHIVED));
-
 
224
        } else {
-
 
225
            $duplicate = $DB->record_exists_select('badge', 'name = :name AND id != :badgeid AND status != :deleted',
-
 
226
                array('name' => $data['name'], 'badgeid' => $data['id'], 'deleted' => BADGE_STATUS_ARCHIVED));
-
 
227
        }
-
 
228
 
205
 
229
        if ($duplicate) {
206
        if ($data['expiry'] == 2 && $data['expireperiod'] <= 0) {
230
            $errors['name'] = get_string('error:duplicatename', 'badges');
207
            $errors['expirydategr'] = get_string('error:invalidexpireperiod', 'badges');
231
        }
-