MDL-69773 block_section_links: Add an option to display section name
[moodle.git] / badges / newbadge.php
CommitLineData
27806552
YB
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * First step page for creating a new badge
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
1fcf0ca8 27require_once(__DIR__ . '/../config.php');
27806552 28require_once($CFG->libdir . '/badgeslib.php');
27806552
YB
29
30$type = required_param('type', PARAM_INT);
31$courseid = optional_param('id', 0, PARAM_INT);
32
33require_login();
34
35if (empty($CFG->enablebadges)) {
36 print_error('badgesdisabled', 'badges');
37}
38
85d7399e
YB
39if (empty($CFG->badges_allowcoursebadges) && ($type == BADGE_TYPE_COURSE)) {
40 print_error('coursebadgesdisabled', 'badges');
41}
42
27806552
YB
43$title = get_string('create', 'badges');
44
45if (($type == BADGE_TYPE_COURSE) && ($course = $DB->get_record('course', array('id' => $courseid)))) {
46 require_login($course);
5dcda2f8
YB
47 $coursecontext = context_course::instance($course->id);
48 $PAGE->set_context($coursecontext);
369484bf 49 $PAGE->set_pagelayout('incourse');
27806552 50 $PAGE->set_url('/badges/newbadge.php', array('type' => $type, 'id' => $course->id));
5dcda2f8
YB
51 $heading = format_string($course->fullname, true, array('context' => $coursecontext)) . ": " . $title;
52 $PAGE->set_heading($heading);
53 $PAGE->set_title($heading);
27806552
YB
54} else {
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);
60}
61
62require_capability('moodle/badges:createbadge', $PAGE->context);
63
aae219ac 64badges_local_backpack_js(true);
27806552
YB
65
66$fordb = new stdClass();
67$fordb->id = null;
68
aae219ac 69$form = new \core_badges\form\badge($PAGE->url, array('action' => 'new'));
27806552
YB
70
71if ($form->is_cancelled()) {
72 redirect(new moodle_url('/badges/index.php', array('type' => $type, 'id' => $courseid)));
73} else if ($data = $form->get_data()) {
74 // Creating new badge here.
75 $now = time();
76
77 $fordb->name = $data->name;
d363a5c2
TT
78 $fordb->version = $data->version;
79 $fordb->language = $data->language;
27806552 80 $fordb->description = $data->description;
d363a5c2
TT
81 $fordb->imageauthorname = $data->imageauthorname;
82 $fordb->imageauthoremail = $data->imageauthoremail;
83 $fordb->imageauthorurl = $data->imageauthorurl;
84 $fordb->imagecaption = $data->imagecaption;
27806552
YB
85 $fordb->timecreated = $now;
86 $fordb->timemodified = $now;
87 $fordb->usercreated = $USER->id;
88 $fordb->usermodified = $USER->id;
aae219ac 89
630a19eb 90 if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
aae219ac
DW
91 $fordb->issuername = $data->issuername;
92 $fordb->issuerurl = $data->issuerurl;
93 $fordb->issuercontact = $data->issuercontact;
94 } else {
95 $url = parse_url($CFG->wwwroot);
96 $fordb->issuerurl = $url['scheme'] . '://' . $url['host'];
97 $fordb->issuername = $CFG->badges_defaultissuername;
98 $fordb->issuercontact = $CFG->badges_defaultissuercontact;
99 }
100
27806552
YB
101 $fordb->expiredate = ($data->expiry == 1) ? $data->expiredate : null;
102 $fordb->expireperiod = ($data->expiry == 2) ? $data->expireperiod : null;
103 $fordb->type = $type;
104 $fordb->courseid = ($type == BADGE_TYPE_COURSE) ? $courseid : null;
105 $fordb->messagesubject = get_string('messagesubject', 'badges');
106 $fordb->message = get_string('messagebody', 'badges',
4887d152 107 html_writer::link($CFG->wwwroot . '/badges/mybadges.php', get_string('managebadges', 'badges')));
27806552
YB
108 $fordb->attachment = 1;
109 $fordb->notification = BADGE_MESSAGE_NEVER;
110 $fordb->status = BADGE_STATUS_INACTIVE;
111
112 $newid = $DB->insert_record('badge', $fordb, true);
113
a0e13871
SB
114 // Trigger event, badge created.
115 $eventparams = array('objectid' => $newid, 'context' => $PAGE->context);
116 $event = \core\event\badge_created::create($eventparams);
117 $event->trigger();
118
27806552
YB
119 $newbadge = new badge($newid);
120 badges_process_badge_image($newbadge, $form->save_temp_file('image'));
a72c2cd6
YB
121 // If a user can configure badge criteria, they will be redirected to the criteria page.
122 if (has_capability('moodle/badges:configurecriteria', $PAGE->context)) {
123 redirect(new moodle_url('/badges/criteria.php', array('id' => $newid)));
124 }
125 redirect(new moodle_url('/badges/overview.php', array('id' => $newid)));
27806552
YB
126}
127
128echo $OUTPUT->header();
129echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection');
130
131$form->display();
132
aae219ac 133echo $OUTPUT->footer();