forked from MMDevelopment/moodle-block_credly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrantbadge.php
143 lines (123 loc) · 5.07 KB
/
grantbadge.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
<?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/>.
/**
* Script that grants a credly badge to a user
*
* @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
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once(dirname(__FILE__).'/locallib.php');
require_once(dirname(__FILE__).'/lib.php');
require_login();
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$courseid = optional_param('courseid', 0, PARAM_INT);
$id = optional_param('id', 0, PARAM_INT);
$grant = optional_param('grant', false, PARAM_BOOL);
$grantselect = optional_param('grantselect', 0, PARAM_INT);
if ($courseid == SITEID) {
$courseid = 0;
}
if ($courseid) {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$PAGE->set_course($course);
$context = $PAGE->context;
} else {
$context = context_system::instance();
$PAGE->set_context($context);
}
require_capability('block/credly:grantbadge', $context);
$urlparams = array('id' => $id);
if ($courseid) {
$urlparams['courseid'] = $courseid;
}
if ($returnurl) {
$urlparams['returnurl'] = $returnurl;
}
$PAGE->set_url('/blocks/credly/grantbadge.php', $urlparams);
$PAGE->set_pagelayout('standard');
$strviewbadge = get_string('grantbadge', 'block_credly');
$PAGE->set_title($strviewbadge);
$PAGE->set_heading($strviewbadge);
$PAGE->navbar->add(get_string('blocks'));
$PAGE->navbar->add(get_string('pluginname', 'block_credly'));
$PAGE->navbar->add($strviewbadge);
$errormsg = '';
if ($grant && confirm_sesskey()) {
$errormsgs = array();
if (isset($_POST['cancel'])) {
if ($courseid) {
redirect(new moodle_url('/course/view.php?id='.$courseid));
} else {
redirect(new moodle_url('/'));
}
}
if (isset($_POST['submitbutton'])) {
if (!$grantselect) {
$errormsgs[] = get_string('grantuserrequired', 'block_credly');
}
if (!$id) {
$errormsgs[] = get_string('grantbadgerequired', 'block_credly');
}
if (count($errormsgs) == 0) {
$grantresult = block_credly_grant_badge($grantselect, $id);
if (empty($grantresult)) {
$errormsgs[] = get_string('granterror', 'block_credly');
} else {
if ($courseid) {
$redirecturl = new moodle_url('/course/view.php?id='.$courseid);
} else {
$redirecturl = new moodle_url('/');
}
redirect($redirecturl, get_string('grantedtouser', 'block_credly'), 5);
}
}
if (count($errormsgs) > 0) {
$errormsg = implode('<br />', $errormsgs);
}
}
}
echo $OUTPUT->header();
if (!empty($errormsg)) {
echo html_writer::div($errormsg, 'block_credly_errorbox');
}
echo html_writer::start_tag('form', array('action' => 'grantbadge.php', 'method' => 'post'));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'courseid', 'value' => $courseid));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'grant', 'value' => 1));
$options = array('courseid' => $courseid, 'accesscontext' => $context, 'searchanywhere' => true, 'multiselect' => false);
$potentialuserselector = new block_credly_potential_grantee('grantselect', $options);
$potentialuserselector->set_rows(10);
$potentialuserselector->display();
$badgeselectoptions = array();
$badges = block_credly_get_admin_created_badges();
foreach ($badges as $badge) {
$badgeselectoptions[$badge->id] = $badge->title;
}
asort($badgeselectoptions, SORT_STRING);
echo html_writer::start_tag('div');
echo html_writer::label(get_string('selectbadgeaward', 'block_credly'), 'id');
echo html_writer::select($badgeselectoptions, 'id', $id);
echo html_writer::end_tag('div');
echo html_writer::start_tag('div');
echo html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'submitbutton',
'value' => get_string('issuebadge', 'block_credly'), 'id' => 'id_submitbutton'));
echo html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'cancel',
'value' => get_string('cancel'), 'id' => 'id_cancel', 'class' => 'btn-cancel'));
echo html_writer::end_tag('div');
echo html_writer::end_tag('form');
echo $OUTPUT->footer();