MDL-42737 ActionMenu: Close the menu after selecting actions
[moodle.git] / badges / action.php
blob929d9a0c8199c6b5742d5ff6a4db0d5fd88cfe5b
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.
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/>.
17 /**
18 * Page to handle actions associated with badges management.
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>
27 require_once(dirname(dirname(__FILE__)) . '/config.php');
28 require_once($CFG->libdir . '/badgeslib.php');
30 $badgeid = required_param('id', PARAM_INT);
31 $copy = optional_param('copy', 0, PARAM_BOOL);
32 $delete = optional_param('delete', 0, PARAM_BOOL);
33 $activate = optional_param('activate', 0, PARAM_BOOL);
34 $deactivate = optional_param('lock', 0, PARAM_BOOL);
35 $confirm = optional_param('confirm', 0, PARAM_BOOL);
36 $return = optional_param('return', 0, PARAM_LOCALURL);
38 require_login();
40 $badge = new badge($badgeid);
41 $context = $badge->get_context();
42 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
44 if ($badge->type == BADGE_TYPE_COURSE) {
45 require_login($badge->courseid);
46 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
47 $PAGE->set_pagelayout('standard');
48 navigation_node::override_active_url($navurl);
49 } else {
50 $PAGE->set_pagelayout('admin');
51 navigation_node::override_active_url($navurl, true);
54 $PAGE->set_context($context);
55 $PAGE->set_url('/badges/action.php', array('id' => $badge->id));
57 if ($return !== 0) {
58 $returnurl = new moodle_url($return);
59 } else {
60 $returnurl = new moodle_url('/badges/overview.php', array('id' => $badge->id));
62 $returnurl->remove_params('awards');
64 if ($delete) {
65 require_capability('moodle/badges:deletebadge', $context);
67 $PAGE->url->param('delete', 1);
68 if ($confirm) {
69 require_sesskey();
70 $badge->delete();
71 redirect(new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid)));
74 $strheading = get_string('delbadge', 'badges');
75 $PAGE->navbar->add($strheading);
76 $PAGE->set_title($strheading);
77 $PAGE->set_heading($badge->name);
78 echo $OUTPUT->header();
79 echo $OUTPUT->heading($strheading);
81 $urlparams = array(
82 'id' => $badge->id,
83 'delete' => 1,
84 'confirm' => 1,
85 'sesskey' => sesskey()
87 $continue = new moodle_url('/badges/action.php', $urlparams);
88 $cancel = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
90 $message = get_string('delconfirm', 'badges', $badge->name);
91 echo $OUTPUT->confirm($message, $continue, $cancel);
92 echo $OUTPUT->footer();
93 die;
96 if ($copy) {
97 require_sesskey();
98 require_capability('moodle/badges:createbadge', $context);
100 $cloneid = $badge->make_clone();
101 // If a user can edit badge details, they will be redirected to the edit page.
102 if (has_capability('moodle/badges:configuredetails', $context)) {
103 redirect(new moodle_url('/badges/edit.php', array('id' => $cloneid, 'action' => 'details')));
105 redirect(new moodle_url('/badges/overview.php', array('id' => $cloneid)));
108 if ($activate) {
109 require_capability('moodle/badges:configurecriteria', $context);
111 $PAGE->url->param('activate', 1);
112 $status = ($badge->status == BADGE_STATUS_INACTIVE) ? BADGE_STATUS_ACTIVE : BADGE_STATUS_ACTIVE_LOCKED;
113 if ($confirm == 1) {
114 require_sesskey();
115 $badge->set_status($status);
116 $returnurl->param('msg', 'activatesuccess');
118 if ($badge->type == BADGE_TYPE_SITE) {
119 // Review on cron if there are more than 1000 users who can earn a site-level badge.
120 $sql = 'SELECT COUNT(u.id) as num
121 FROM {user} u
122 LEFT JOIN {badge_issued} bi
123 ON u.id = bi.userid AND bi.badgeid = :badgeid
124 WHERE bi.badgeid IS NULL AND u.id != :guestid AND u.deleted = 0';
125 $toearn = $DB->get_record_sql($sql, array('badgeid' => $badge->id, 'guestid' => $CFG->siteguest));
127 if ($toearn->num < 1000) {
128 $awards = $badge->review_all_criteria();
129 $returnurl->param('awards', $awards);
130 } else {
131 $returnurl->param('awards', 'cron');
133 } else {
134 $awards = $badge->review_all_criteria();
135 $returnurl->param('awards', $awards);
137 redirect($returnurl);
140 $strheading = get_string('reviewbadge', 'badges');
141 $PAGE->navbar->add($strheading);
142 $PAGE->set_title($strheading);
143 $PAGE->set_heading($badge->name);
144 echo $OUTPUT->header();
145 echo $OUTPUT->heading($strheading);
147 $params = array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(), 'confirm' => 1, 'return' => $return);
148 $url = new moodle_url('/badges/action.php', $params);
150 if (!$badge->has_criteria()) {
151 echo $OUTPUT->notification(get_string('error:cannotact', 'badges') . get_string('nocriteria', 'badges'));
152 echo $OUTPUT->continue_button($returnurl);
153 } else {
154 $message = get_string('reviewconfirm', 'badges', $badge->name);
155 echo $OUTPUT->confirm($message, $url, $returnurl);
157 echo $OUTPUT->footer();
158 die;
161 if ($deactivate) {
162 require_sesskey();
163 require_capability('moodle/badges:configurecriteria', $context);
165 $status = ($badge->status == BADGE_STATUS_ACTIVE) ? BADGE_STATUS_INACTIVE : BADGE_STATUS_INACTIVE_LOCKED;
166 $badge->set_status($status);
167 redirect($returnurl);