forked from MMDevelopment/moodle-block_credly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditbadge.php
115 lines (95 loc) · 3.6 KB
/
editbadge.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
<?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 creates/upload a badge via 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
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once(dirname(__FILE__) . '/lib.php');
require_once(dirname(__FILE__) . '/locallib.php');
require_login();
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
$courseid = optional_param('courseid', 0, PARAM_INT);
$id = optional_param('id', 0, PARAM_INT); // 0 mean create new.
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:managebadge', $context);
$urlparams = array('id' => $id);
if ($courseid) {
$urlparams['courseid'] = $courseid;
}
if ($returnurl) {
$urlparams['returnurl'] = $returnurl;
}
$managebadges = new moodle_url('/blocks/credly/managebadges.php', $urlparams);
$PAGE->set_url('/blocks/credly/editbadge.php', $urlparams);
$PAGE->set_pagelayout('admin');
if ($id) {
$isadding = false;
$badgerecord = block_credly_get_badge_info($id);
if (!$badgerecord) {
print_error('fetcherror', 'block_credly');
}
$badgeimagepreviewsrc = str_replace('.png', '_7.png', $badgerecord->image_url);
$imagepreview = html_writer::img($badgeimagepreviewsrc, $badgerecord->title, array('title' => $badgerecord->title));
$customdata = array('imagepreview' => $imagepreview);
} else {
$isadding = true;
$badgerecord = new stdClass;
$customdata = null;
}
$mform = new block_credly_badge_edit_form($PAGE->url, $customdata, $isadding);
$mform->set_data($badgerecord);
if ($mform->is_cancelled()) {
redirect($managebadges);
} else if ($data = $mform->get_data() and confirm_sesskey()) {
if ($isadding) {
$imagedata = $mform->get_file_content('badgeimage');
$badgeid = block_credly_create_badge($data, $imagedata);
} else {
$imagedata = $mform->get_file_content('badgeimage');
$badgeid = block_credly_update_badge($id, $data, $imagedata);
}
redirect($managebadges);
} else {
if ($isadding) {
$strtitle = get_string('createnewbadge', 'block_credly');
} else {
$strtitle = get_string('editbadge', 'block_credly');
}
$PAGE->set_title($strtitle);
$PAGE->set_heading($strtitle);
$PAGE->navbar->add(get_string('blocks'));
$PAGE->navbar->add(get_string('pluginname', 'block_credly'));
$PAGE->navbar->add(get_string('managebadges', 'block_credly'), $managebadges );
$PAGE->navbar->add($strtitle);
echo $OUTPUT->header();
echo $OUTPUT->heading($strtitle, 2);
$mform->display();
echo $OUTPUT->footer();
}