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 * Page for badges management
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 use core_badges\reportbuilder\local\systemreports\badges
;
28 use core_reportbuilder\system_report_factory
;
30 require_once(__DIR__
. '/../config.php');
31 require_once($CFG->libdir
. '/badgeslib.php');
33 $type = required_param('type', PARAM_INT
);
34 $courseid = optional_param('id', 0, PARAM_INT
);
35 $deactivate = optional_param('lock', 0, PARAM_INT
);
36 $confirm = optional_param('confirm', false, PARAM_BOOL
);
37 $delete = optional_param('delete', 0, PARAM_INT
);
38 $archive = optional_param('archive', 0, PARAM_INT
);
39 $msg = optional_param('msg', '', PARAM_TEXT
);
43 if (empty($CFG->enablebadges
)) {
44 throw new \
moodle_exception('badgesdisabled', 'badges');
47 if (empty($CFG->badges_allowcoursebadges
) && ($type == BADGE_TYPE_COURSE
)) {
48 throw new \
moodle_exception('coursebadgesdisabled', 'badges');
51 $urlparams = ['type' => $type];
53 if ($course = $DB->get_record('course', ['id' => $courseid])) {
54 $urlparams['id'] = $course->id
;
57 $hdr = get_string('managebadges', 'badges');
58 $returnurl = new moodle_url('/badges/index.php', $urlparams);
59 $PAGE->set_url($returnurl);
60 $PAGE->add_body_class('limitedwidth');
62 if ($type == BADGE_TYPE_SITE
) {
63 $title = get_string('sitebadges', 'badges');
64 $PAGE->set_context(context_system
::instance());
65 $PAGE->set_pagelayout('admin');
66 $PAGE->set_heading(get_string('administrationsite'));
67 navigation_node
::override_active_url(new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_SITE
)), true);
69 require_login($course);
70 $coursecontext = context_course
::instance($course->id
);
71 $title = get_string('coursebadges', 'badges');
72 $PAGE->set_context($coursecontext);
73 $PAGE->set_pagelayout('incourse');
74 $PAGE->set_heading(format_string($course->fullname
, true, array('context' => $coursecontext)));
75 navigation_node
::override_active_url(
76 new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_COURSE
, 'id' => $course->id
))
80 if (!has_any_capability(array(
81 'moodle/badges:viewawarded',
82 'moodle/badges:createbadge',
83 'moodle/badges:awardbadge',
84 'moodle/badges:configurecriteria',
85 'moodle/badges:configuremessages',
86 'moodle/badges:configuredetails',
87 'moodle/badges:deletebadge'), $PAGE->context
)) {
88 redirect($CFG->wwwroot
);
91 $PAGE->set_title($hdr);
92 $output = $PAGE->get_renderer('core', 'badges');
94 if (($delete ||
$archive) && has_capability('moodle/badges:deletebadge', $PAGE->context
)) {
95 $badgeid = ($archive != 0) ?
$archive : $delete;
96 $badge = new badge($badgeid);
98 echo $output->header();
99 // Archive this badge?
100 echo $output->heading(get_string('archivebadge', 'badges', $badge->name
));
101 $archivebutton = $output->single_button(
102 new moodle_url($PAGE->url
, array('archive' => $badge->id
, 'confirm' => 1)),
103 get_string('archiveconfirm', 'badges'));
104 echo $output->box(get_string('archivehelp', 'badges') . $archivebutton, 'generalbox');
106 // Delete this badge?
107 echo $output->heading(get_string('delbadge', 'badges', $badge->name
));
108 $deletebutton = $output->single_button(
109 new moodle_url($PAGE->url
, array('delete' => $badge->id
, 'confirm' => 1)),
110 get_string('delconfirm', 'badges'));
111 echo $output->box(get_string('deletehelp', 'badges') . $deletebutton, 'generalbox');
114 echo $output->action_link($returnurl, get_string('cancel'));
116 echo $output->footer();
120 $archiveonly = ($archive != 0) ?
true : false;
121 $badge->delete($archiveonly);
122 redirect($returnurl);
126 if ($deactivate && has_capability('moodle/badges:configuredetails', $PAGE->context
)) {
128 $badge = new badge($deactivate);
129 if ($badge->is_locked()) {
130 $badge->set_status(BADGE_STATUS_INACTIVE_LOCKED
);
132 $badge->set_status(BADGE_STATUS_INACTIVE
);
134 $msg = 'deactivatesuccess';
135 $returnurl->param('msg', $msg);
136 redirect($returnurl);
139 echo $OUTPUT->header();
140 $backurl = $type == BADGE_TYPE_SITE ?
null : new moodle_url('/badges/view.php', ['type' => $type, 'id' => $courseid]);
141 $actionbar = new \core_badges\output\
standard_action_bar($PAGE, $type, false, true, $backurl);
142 echo $output->render_tertiary_navigation($actionbar);
144 if ($type == BADGE_TYPE_SITE
) {
145 echo $OUTPUT->heading_with_help($hdr, 'sitebadges', 'badges');
147 echo $OUTPUT->heading($hdr);
150 echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection');
152 if ($course && $course->startdate
> time()) {
153 echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
157 echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess');
160 $report = system_report_factory
::create(badges
::class, $PAGE->context
);
161 $report->set_default_no_results_notice(new lang_string('nobadges', 'badges'));
163 echo $report->output();
165 echo $OUTPUT->footer();