forked from drroad09/moodle-block_credly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
218 lines (185 loc) · 8.13 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Helper forms for integrating with Credly Open Credit API
*
* @package block_credly
* @copyright 2014-2017 Deds Castillo, MM Development Services (http://michaelmino.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->dirroot . '/user/selector/lib.php');
/**
* Form for adding or editing badges.
*
* @package block_credly
* @copyright 2014-2017 Deds Castillo, MM Development Services (http://michaelmino.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_credly_badge_edit_form extends moodleform {
/** @var bool This variable tracks whether adding or editing a badge */
protected $_isadding;
/** @var int This variable specifies the maximum image file size (in bytes) for badge */
protected $_imagemaxbytes = 1048576;
/**
* This specifies the maximum time in seconds from date of granting
* of a badge before it expires. The Credly maximum is 51 years.
* @var int
*/
protected $_maxexpiresin = 1609462800;
/**
* Constructor for the badge edit form
*
* @param null|string $actionurl action to call for the form, null uses default
* @param null|mixed $customdata the custom data for the moodleform class
* @param bool $isadding true if creating new badge, false if updating an existing badge
*/
public function __construct($actionurl=null, $customdata=null, $isadding=false) {
$this->_isadding = $isadding;
parent::__construct($actionurl, $customdata);
}
/**
* The edit form definition
*
*/
protected function definition() {
$mform =& $this->_form;
if (!$this->_isadding) {
$mform->addElement('static', 'imagepreview', get_string('image_preview_current', 'block_credly'),
$this->_customdata['imagepreview']);
}
$mform->addElement('filepicker', 'badgeimage', get_string('badgeimage', 'block_credly'),
null, array('maxbytes' => $this->_imagemaxbytes, 'accepted_types' => array('image')));
if ($this->_isadding) {
$mform->addRule('badgeimage', get_string('missingbadgeimage', 'block_credly'), 'required', null, 'client');
}
$mform->addHelpButton('badgeimage', 'badgeimage', 'block_credly');
$mform->addElement('text', 'title', get_string('title', 'block_credly'), 'maxlength="128" size="80"');
$mform->setType('title', PARAM_RAW);
$mform->addRule('title', get_string('missingtitle', 'block_credly'), 'required', null, 'client');
$mform->addHelpButton('title', 'title', 'block_credly');
$mform->addElement('text', 'short_description', get_string('short_description', 'block_credly'),
'maxlength="128" size="80"');
$mform->setType('short_description', PARAM_RAW);
$mform->addHelpButton('short_description', 'short_description', 'block_credly');
$mform->addElement('text', 'description', get_string('description', 'block_credly'), 'maxlength="500" size="80"');
$mform->setType('description', PARAM_RAW);
$mform->addHelpButton('description', 'description', 'block_credly');
$mform->addElement('text', 'criteria', get_string('criteria', 'block_credly'), 'maxlength="500" size="80"');
$mform->setType('criteria', PARAM_RAW);
$mform->addHelpButton('criteria', 'criteria', 'block_credly');
$mform->addElement('selectyesno', 'is_giveable', get_string('is_giveable', 'block_credly'));
$mform->addHelpButton('is_giveable', 'is_giveable', 'block_credly');
$mform->setDefault('is_giveable', 0);
$mform->addElement('text', 'expires_in', get_string('expires_in', 'block_credly'), 'maxlength="10" size="11"');
$mform->setType('expires_in', PARAM_INT);
$mform->addRule('expires_in', get_string('errorexpiresin', 'block_credly', $this->_maxexpiresin),
'numeric', null, 'client');
$mform->setDefault('expires_in', 0);
$mform->addHelpButton('expires_in', 'expires_in', 'block_credly');
$submitlabal = null; // The default label.
if ($this->_isadding) {
$submitlabal = get_string('addnewbadge', 'block_credly');
}
$this->add_action_buttons(true, $submitlabal);
}
/**
* The edit form validation code
*
* @param array $data form data
* @param array $files
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (intval($data['expires_in']) > $this->_maxexpiresin) {
$errors['expires_in'] = get_string('errorexpiresin', 'block_credly', $this->_maxexpiresin);
}
return $errors;
}
}
/**
* A user selector widget for fetching potential grantees of a badge
*
* @package block_credly
* @copyright 2014-2017 Deds Castillo, MM Development Services (http://michaelmino.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_credly_potential_grantee extends user_selector_base {
/** @var int The course id where to search potential grantees */
protected $_courseid;
/**
* Constructor for the user selector form
*
* @param null|string $name name of the widget
* @param array $options options passed to the widget
*/
public function __construct($name, $options) {
$this->_courseid = $options['courseid'];
parent::__construct($name, $options);
}
/**
* Candidate users
*
* @param string $search
* @return array
*/
public function find_users($search) {
global $DB;
// By default wherecondition retrieves all users except the deleted, not confirmed and guest.
list($wherecondition, $params) = $this->search_sql($search, 'u');
$fields = 'SELECT ' . $this->required_fields_sql('u');
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
JOIN {user_enrolments} ue ON (ue.userid = u.id)
JOIN {enrol} e ON (ue.enrolid = e.id)
WHERE $wherecondition";
if ($this->_courseid != 0) {
$sql .= " AND e.courseid = :courseid";
$params['courseid'] = $this->_courseid;
}
list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
$order = ' ORDER BY ' . $sort;
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > $this->maxusersperpage) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
if (empty($availableusers)) {
return array();
}
if ($search) {
$groupname = get_string('granttocandidatesmatching', 'block_credly', $search);
} else {
$groupname = get_string('granttocandidates', 'block_credly');
}
return array($groupname => $availableusers);
}
/**
* Get options for the widget
*
* @return array
*/
protected function get_options() {
$options = parent::get_options();
$options['courseid'] = $this->_courseid;
$options['file'] = 'blocks/credly/locallib.php';
return $options;
}
}