2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * First step page for creating a new badge
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>
27 require_once(__DIR__
. '/../config.php');
28 require_once($CFG->libdir
. '/badgeslib.php');
30 $type = required_param('type', PARAM_INT
);
31 $courseid = optional_param('id', 0, PARAM_INT
);
35 if (empty($CFG->enablebadges
)) {
36 print_error('badgesdisabled', 'badges');
39 if (empty($CFG->badges_allowcoursebadges
) && ($type == BADGE_TYPE_COURSE
)) {
40 print_error('coursebadgesdisabled', 'badges');
43 $title = get_string('create', 'badges');
45 if (($type == BADGE_TYPE_COURSE
) && ($course = $DB->get_record('course', array('id' => $courseid)))) {
46 require_login($course);
47 $coursecontext = context_course
::instance($course->id
);
48 $PAGE->set_context($coursecontext);
49 $PAGE->set_pagelayout('incourse');
50 $PAGE->set_url('/badges/newbadge.php', array('type' => $type, 'id' => $course->id
));
51 $heading = format_string($course->fullname
, true, array('context' => $coursecontext)) . ": " . $title;
52 $PAGE->set_heading($heading);
53 $PAGE->set_title($heading);
55 $PAGE->set_context(context_system
::instance());
56 $PAGE->set_pagelayout('admin');
57 $PAGE->set_url('/badges/newbadge.php', array('type' => $type));
58 $PAGE->set_heading($title);
59 $PAGE->set_title($title);
62 require_capability('moodle/badges:createbadge', $PAGE->context
);
64 $fordb = new stdClass();
67 $form = new \core_badges\form\badge
($PAGE->url
, array('action' => 'new'));
69 if ($form->is_cancelled()) {
70 redirect(new moodle_url('/badges/index.php', array('type' => $type, 'id' => $courseid)));
71 } else if ($data = $form->get_data()) {
72 // Creating new badge here.
75 $fordb->name
= $data->name
;
76 $fordb->version
= $data->version
;
77 $fordb->language
= $data->language
;
78 $fordb->description
= $data->description
;
79 $fordb->imageauthorname
= $data->imageauthorname
;
80 $fordb->imageauthoremail
= $data->imageauthoremail
;
81 $fordb->imageauthorurl
= $data->imageauthorurl
;
82 $fordb->imagecaption
= $data->imagecaption
;
83 $fordb->timecreated
= $now;
84 $fordb->timemodified
= $now;
85 $fordb->usercreated
= $USER->id
;
86 $fordb->usermodified
= $USER->id
;
88 if (badges_open_badges_backpack_api() == OPEN_BADGES_V1
) {
89 $fordb->issuername
= $data->issuername
;
90 $fordb->issuerurl
= $data->issuerurl
;
91 $fordb->issuercontact
= $data->issuercontact
;
93 $url = parse_url($CFG->wwwroot
);
94 $fordb->issuerurl
= $url['scheme'] . '://' . $url['host'];
95 $fordb->issuername
= $CFG->badges_defaultissuername
;
96 $fordb->issuercontact
= $CFG->badges_defaultissuercontact
;
99 $fordb->expiredate
= ($data->expiry
== 1) ?
$data->expiredate
: null;
100 $fordb->expireperiod
= ($data->expiry
== 2) ?
$data->expireperiod
: null;
101 $fordb->type
= $type;
102 $fordb->courseid
= ($type == BADGE_TYPE_COURSE
) ?
$courseid : null;
103 $fordb->messagesubject
= get_string('messagesubject', 'badges');
104 $fordb->message
= get_string('messagebody', 'badges',
105 html_writer
::link($CFG->wwwroot
. '/badges/mybadges.php', get_string('managebadges', 'badges')));
106 $fordb->attachment
= 1;
107 $fordb->notification
= BADGE_MESSAGE_NEVER
;
108 $fordb->status
= BADGE_STATUS_INACTIVE
;
110 $newid = $DB->insert_record('badge', $fordb, true);
112 // Trigger event, badge created.
113 $eventparams = array('objectid' => $newid, 'context' => $PAGE->context
);
114 $event = \core\event\badge_created
::create($eventparams);
117 $newbadge = new badge($newid);
118 badges_process_badge_image($newbadge, $form->save_temp_file('image'));
119 // If a user can configure badge criteria, they will be redirected to the criteria page.
120 if (has_capability('moodle/badges:configurecriteria', $PAGE->context
)) {
121 redirect(new moodle_url('/badges/criteria.php', array('id' => $newid)));
123 redirect(new moodle_url('/badges/overview.php', array('id' => $newid)));
126 echo $OUTPUT->header();
127 echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection');
131 echo $OUTPUT->footer();