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 * Editing badge details, criteria, messages
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(dirname(dirname(__FILE__
)) . '/config.php');
28 require_once($CFG->libdir
. '/badgeslib.php');
29 require_once($CFG->dirroot
. '/badges/edit_form.php');
31 $badgeid = required_param('id', PARAM_INT
);
32 $action = optional_param('action', 'details', PARAM_TEXT
);
36 if (empty($CFG->enablebadges
)) {
37 print_error('badgesdisabled', 'badges');
40 $badge = new badge($badgeid);
41 $context = $badge->get_context();
42 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type
));
44 if ($action == 'message') {
45 require_capability('moodle/badges:configuremessages', $context);
47 require_capability('moodle/badges:configuredetails', $context);
50 if ($badge->type
== BADGE_TYPE_COURSE
) {
51 if (empty($CFG->badges_allowcoursebadges
)) {
52 print_error('coursebadgesdisabled', 'badges');
54 require_login($badge->courseid
);
55 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type
, 'id' => $badge->courseid
));
58 $currenturl = new moodle_url('/badges/edit.php', array('id' => $badge->id
, 'action' => $action));
60 $PAGE->set_context($context);
61 $PAGE->set_url($currenturl);
62 $PAGE->set_pagelayout('standard');
63 $PAGE->set_heading($badge->name
);
64 $PAGE->set_title($badge->name
);
66 // Set up navigation and breadcrumbs.
67 navigation_node
::override_active_url($navurl);
68 $PAGE->navbar
->add($badge->name
);
70 $output = $PAGE->get_renderer('core', 'badges');
74 $badge->message
= clean_text($badge->message
, FORMAT_HTML
);
75 $editoroptions = array(
80 'context' => $context,
84 $badge = file_prepare_standard_editor($badge, 'message', $editoroptions, $context);
86 $form_class = 'edit_' . $action . '_form';
87 $form = new $form_class($currenturl, array('badge' => $badge, 'action' => $action, 'editoroptions' => $editoroptions));
89 if ($form->is_cancelled()) {
90 redirect(new moodle_url('/badges/overview.php', array('id' => $badgeid)));
91 } else if ($form->is_submitted() && $form->is_validated() && ($data = $form->get_data())) {
92 if ($action == 'details') {
93 $badge->name
= $data->name
;
94 $badge->description
= $data->description
;
95 $badge->usermodified
= $USER->id
;
96 $badge->issuername
= $data->issuername
;
97 $badge->issuerurl
= $data->issuerurl
;
98 $badge->issuercontact
= $data->issuercontact
;
99 $badge->expiredate
= ($data->expiry
== 1) ?
$data->expiredate
: null;
100 $badge->expireperiod
= ($data->expiry
== 2) ?
$data->expireperiod
: null;
102 // Need to unset message_editor options to avoid errors on form edit.
103 unset($badge->messageformat
);
104 unset($badge->message_editor
);
106 if ($badge->save()) {
107 badges_process_badge_image($badge, $form->save_temp_file('image'));
108 $form->set_data($badge);
109 $statusmsg = get_string('changessaved');
111 $errormsg = get_string('error:save', 'badges');
113 } else if ($action == 'message') {
114 // Calculate next message cron if form data is different from original badge data.
115 if ($data->notification
!= $badge->notification
) {
116 if ($data->notification
> BADGE_MESSAGE_ALWAYS
) {
117 $badge->nextcron
= badges_calculate_message_schedule($data->notification
);
119 $badge->nextcron
= null;
123 $badge->message
= clean_text($data->message_editor
['text'], FORMAT_HTML
);
124 $badge->messagesubject
= $data->messagesubject
;
125 $badge->notification
= $data->notification
;
126 $badge->attachment
= $data->attachment
;
128 unset($badge->messageformat
);
129 unset($badge->message_editor
);
130 if ($badge->save()) {
131 $statusmsg = get_string('changessaved');
133 $errormsg = get_string('error:save', 'badges');
138 echo $OUTPUT->header();
139 echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name
);
141 if ($errormsg !== '') {
142 echo $OUTPUT->notification($errormsg);
144 } else if ($statusmsg !== '') {
145 echo $OUTPUT->notification($statusmsg, 'notifysuccess');
148 echo $output->print_badge_status_box($badge);
149 $output->print_badge_tabs($badgeid, $context, $action);
153 echo $OUTPUT->footer();