MDL-69773 block_section_links: Add an option to display section name
[moodle.git] / badges / edit.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 * Editing badge details, criteria, messages
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');
aae219ac 29require_once($CFG->libdir . '/filelib.php');
27806552
YB
30
31$badgeid = required_param('id', PARAM_INT);
aae219ac 32$action = optional_param('action', 'badge', PARAM_TEXT);
27806552
YB
33
34require_login();
35
36if (empty($CFG->enablebadges)) {
37 print_error('badgesdisabled', 'badges');
38}
39
40$badge = new badge($badgeid);
41$context = $badge->get_context();
42$navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
43
a72c2cd6
YB
44if ($action == 'message') {
45 require_capability('moodle/badges:configuremessages', $context);
46} else {
47 require_capability('moodle/badges:configuredetails', $context);
48}
27806552
YB
49
50if ($badge->type == BADGE_TYPE_COURSE) {
85d7399e
YB
51 if (empty($CFG->badges_allowcoursebadges)) {
52 print_error('coursebadgesdisabled', 'badges');
53 }
27806552
YB
54 require_login($badge->courseid);
55 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
369484bf 56 $PAGE->set_pagelayout('incourse');
0d206054 57 navigation_node::override_active_url($navurl);
e56c107d
PS
58} else {
59 $PAGE->set_pagelayout('admin');
0d206054 60 navigation_node::override_active_url($navurl, true);
27806552
YB
61}
62
63$currenturl = new moodle_url('/badges/edit.php', array('id' => $badge->id, 'action' => $action));
64
65$PAGE->set_context($context);
66$PAGE->set_url($currenturl);
27806552
YB
67$PAGE->set_heading($badge->name);
68$PAGE->set_title($badge->name);
27806552
YB
69$PAGE->navbar->add($badge->name);
70
71$output = $PAGE->get_renderer('core', 'badges');
72$statusmsg = '';
73$errormsg = '';
74
75$badge->message = clean_text($badge->message, FORMAT_HTML);
76$editoroptions = array(
77 'subdirs' => 0,
78 'maxbytes' => 0,
79 'maxfiles' => 0,
80 'changeformat' => 0,
81 'context' => $context,
82 'noclean' => false,
83 'trusttext' => false
84 );
85$badge = file_prepare_standard_editor($badge, 'message', $editoroptions, $context);
86
aae219ac 87$formclass = '\core_badges\form' . '\\' . $action;
d363a5c2 88$form = new $formclass($currenturl, array('badge' => $badge, 'action' => $action, 'editoroptions' => $editoroptions));
27806552
YB
89
90if ($form->is_cancelled()) {
91 redirect(new moodle_url('/badges/overview.php', array('id' => $badgeid)));
92} else if ($form->is_submitted() && $form->is_validated() && ($data = $form->get_data())) {
aae219ac 93 if ($action == 'badge') {
27806552 94 $badge->name = $data->name;
d363a5c2
TT
95 $badge->version = trim($data->version);
96 $badge->language = $data->language;
27806552 97 $badge->description = $data->description;
d363a5c2
TT
98 $badge->imageauthorname = $data->imageauthorname;
99 $badge->imageauthoremail = $data->imageauthoremail;
100 $badge->imageauthorurl = $data->imageauthorurl;
101 $badge->imagecaption = $data->imagecaption;
27806552 102 $badge->usermodified = $USER->id;
cbe0bb91
SA
103 if (badges_open_badges_backpack_api() == OPEN_BADGES_V1) {
104 $badge->issuername = $data->issuername;
105 $badge->issuerurl = $data->issuerurl;
106 $badge->issuercontact = $data->issuercontact;
107 }
27806552
YB
108 $badge->expiredate = ($data->expiry == 1) ? $data->expiredate : null;
109 $badge->expireperiod = ($data->expiry == 2) ? $data->expireperiod : null;
110
111 // Need to unset message_editor options to avoid errors on form edit.
112 unset($badge->messageformat);
113 unset($badge->message_editor);
114
115 if ($badge->save()) {
116 badges_process_badge_image($badge, $form->save_temp_file('image'));
117 $form->set_data($badge);
118 $statusmsg = get_string('changessaved');
119 } else {
120 $errormsg = get_string('error:save', 'badges');
121 }
122 } else if ($action == 'message') {
123 // Calculate next message cron if form data is different from original badge data.
124 if ($data->notification != $badge->notification) {
125 if ($data->notification > BADGE_MESSAGE_ALWAYS) {
126 $badge->nextcron = badges_calculate_message_schedule($data->notification);
127 } else {
128 $badge->nextcron = null;
129 }
130 }
131
132 $badge->message = clean_text($data->message_editor['text'], FORMAT_HTML);
133 $badge->messagesubject = $data->messagesubject;
134 $badge->notification = $data->notification;
135 $badge->attachment = $data->attachment;
136
137 unset($badge->messageformat);
138 unset($badge->message_editor);
139 if ($badge->save()) {
140 $statusmsg = get_string('changessaved');
141 } else {
142 $errormsg = get_string('error:save', 'badges');
143 }
144 }
145}
146
147echo $OUTPUT->header();
148echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
149
150if ($errormsg !== '') {
151 echo $OUTPUT->notification($errormsg);
152
153} else if ($statusmsg !== '') {
154 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
155}
156
157echo $output->print_badge_status_box($badge);
158$output->print_badge_tabs($badgeid, $context, $action);
159
160$form->display();
161
aae219ac 162echo $OUTPUT->footer();