weekly release 2.5+
[moodle.git] / badges / action.php
blobae38fa7f445663acb12f70997ba27bf79bb62b93
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));
49 $PAGE->set_context($context);
50 $PAGE->set_url('/badges/action.php', array('id' => $badge->id));
51 $PAGE->set_pagelayout('standard');
52 navigation_node::override_active_url($navurl);
54 if ($return !== 0) {
55 $returnurl = new moodle_url($return);
56 } else {
57 $returnurl = new moodle_url('/badges/overview.php', array('id' => $badge->id));
59 $returnurl->remove_params('awards');
61 if ($delete) {
62 require_capability('moodle/badges:deletebadge', $context);
64 $PAGE->url->param('delete', 1);
65 if ($confirm) {
66 require_sesskey();
67 $badge->delete();
68 redirect(new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid)));
71 $strheading = get_string('delbadge', 'badges');
72 $PAGE->navbar->add($strheading);
73 $PAGE->set_title($strheading);
74 $PAGE->set_heading($badge->name);
75 echo $OUTPUT->header();
76 echo $OUTPUT->heading($strheading);
78 $urlparams = array(
79 'id' => $badge->id,
80 'delete' => 1,
81 'confirm' => 1,
82 'sesskey' => sesskey()
84 $continue = new moodle_url('/badges/action.php', $urlparams);
85 $cancel = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
87 $message = get_string('delconfirm', 'badges', $badge->name);
88 echo $OUTPUT->confirm($message, $continue, $cancel);
89 echo $OUTPUT->footer();
90 die;
93 if ($copy) {
94 require_sesskey();
95 require_capability('moodle/badges:createbadge', $context);
97 $cloneid = $badge->make_clone();
98 redirect(new moodle_url('/badges/edit.php', array('id' => $cloneid, 'action' => 'details')));
101 if ($activate) {
102 require_capability('moodle/badges:configurecriteria', $context);
104 $PAGE->url->param('activate', 1);
105 $status = ($badge->status == BADGE_STATUS_INACTIVE) ? BADGE_STATUS_ACTIVE : BADGE_STATUS_ACTIVE_LOCKED;
106 if ($confirm == 1) {
107 require_sesskey();
108 $badge->set_status($status);
110 if ($badge->type == BADGE_TYPE_SITE) {
111 // Review on cron if there are more than 1000 users who can earn a site-level badge.
112 $sql = 'SELECT COUNT(u.id) as num
113 FROM {user} u
114 LEFT JOIN {badge_issued} bi
115 ON u.id = bi.userid AND bi.badgeid = :badgeid
116 WHERE bi.badgeid IS NULL AND u.id != :guestid AND u.deleted = 0';
117 $toearn = $DB->get_record_sql($sql, array('badgeid' => $badge->id, 'guestid' => $CFG->siteguest));
119 if ($toearn->num < 1000) {
120 $awards = $badge->review_all_criteria();
121 $returnurl->param('awards', $awards);
122 } else {
123 $returnurl->param('awards', 'cron');
125 } else {
126 $awards = $badge->review_all_criteria();
127 $returnurl->param('awards', $awards);
129 redirect($returnurl);
132 $strheading = get_string('reviewbadge', 'badges');
133 $PAGE->navbar->add($strheading);
134 $PAGE->set_title($strheading);
135 $PAGE->set_heading($badge->name);
136 echo $OUTPUT->header();
137 echo $OUTPUT->heading($strheading);
139 $params = array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(), 'confirm' => 1, 'return' => $return);
140 $url = new moodle_url('/badges/action.php', $params);
142 if (!$badge->has_criteria()) {
143 echo $OUTPUT->notification(get_string('error:cannotact', 'badges') . get_string('nocriteria', 'badges'));
144 echo $OUTPUT->continue_button($returnurl);
145 } else {
146 $message = get_string('reviewconfirm', 'badges', $badge->name);
147 echo $OUTPUT->confirm($message, $url, $returnurl);
149 echo $OUTPUT->footer();
150 die;
153 if ($deactivate) {
154 require_sesskey();
155 require_capability('moodle/badges:configurecriteria', $context);
157 $status = ($badge->status == BADGE_STATUS_ACTIVE) ? BADGE_STATUS_INACTIVE : BADGE_STATUS_INACTIVE_LOCKED;
158 $badge->set_status($status);
159 redirect($returnurl);