Merge branch 'MDL-56880-m' of https://github.com/micaherne/moodle
[moodle.git] / badges / action.php
blob05dafddb7442e34be43c3b1820621a7257e0afb5
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(__DIR__ . '/../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 $activate = optional_param('activate', 0, PARAM_BOOL);
33 $deactivate = optional_param('lock', 0, PARAM_BOOL);
34 $confirm = optional_param('confirm', 0, PARAM_BOOL);
35 $return = optional_param('return', 0, PARAM_LOCALURL);
37 require_login();
39 $badge = new badge($badgeid);
40 $context = $badge->get_context();
41 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
43 if ($badge->type == BADGE_TYPE_COURSE) {
44 require_login($badge->courseid);
45 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
46 $PAGE->set_pagelayout('standard');
47 navigation_node::override_active_url($navurl);
48 } else {
49 $PAGE->set_pagelayout('admin');
50 navigation_node::override_active_url($navurl, true);
53 $PAGE->set_context($context);
54 $PAGE->set_url('/badges/action.php', array('id' => $badge->id));
56 if ($return !== 0) {
57 $returnurl = new moodle_url($return);
58 } else {
59 $returnurl = new moodle_url('/badges/overview.php', array('id' => $badge->id));
61 $returnurl->remove_params('awards');
63 if ($copy) {
64 require_sesskey();
65 require_capability('moodle/badges:createbadge', $context);
67 $cloneid = $badge->make_clone();
68 // If a user can edit badge details, they will be redirected to the edit page.
69 if (has_capability('moodle/badges:configuredetails', $context)) {
70 redirect(new moodle_url('/badges/edit.php', array('id' => $cloneid, 'action' => 'details')));
72 redirect(new moodle_url('/badges/overview.php', array('id' => $cloneid)));
75 if ($activate) {
76 require_capability('moodle/badges:configurecriteria', $context);
78 $PAGE->url->param('activate', 1);
79 $status = ($badge->status == BADGE_STATUS_INACTIVE) ? BADGE_STATUS_ACTIVE : BADGE_STATUS_ACTIVE_LOCKED;
80 if ($confirm == 1) {
81 require_sesskey();
82 $badge->set_status($status);
83 $returnurl->param('msg', 'activatesuccess');
85 if ($badge->type == BADGE_TYPE_SITE) {
86 // Review on cron if there are more than 1000 users who can earn a site-level badge.
87 $sql = 'SELECT COUNT(u.id) as num
88 FROM {user} u
89 LEFT JOIN {badge_issued} bi
90 ON u.id = bi.userid AND bi.badgeid = :badgeid
91 WHERE bi.badgeid IS NULL AND u.id != :guestid AND u.deleted = 0';
92 $toearn = $DB->get_record_sql($sql, array('badgeid' => $badge->id, 'guestid' => $CFG->siteguest));
94 if ($toearn->num < 1000) {
95 $awards = $badge->review_all_criteria();
96 $returnurl->param('awards', $awards);
97 } else {
98 $returnurl->param('awards', 'cron');
100 } else {
101 $awards = $badge->review_all_criteria();
102 $returnurl->param('awards', $awards);
104 redirect($returnurl);
107 $strheading = get_string('reviewbadge', 'badges');
108 $PAGE->navbar->add($strheading);
109 $PAGE->set_title($strheading);
110 $PAGE->set_heading($badge->name);
111 echo $OUTPUT->header();
112 echo $OUTPUT->heading($strheading);
114 $params = array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(), 'confirm' => 1, 'return' => $return);
115 $url = new moodle_url('/badges/action.php', $params);
117 if (!$badge->has_criteria()) {
118 redirect($returnurl, get_string('error:cannotact', 'badges') . get_string('nocriteria', 'badges'), null, \core\output\notification::NOTIFY_ERROR);
119 } else {
120 $message = get_string('reviewconfirm', 'badges', $badge->name);
121 echo $OUTPUT->confirm($message, $url, $returnurl);
123 echo $OUTPUT->footer();
124 die;
127 if ($deactivate) {
128 require_sesskey();
129 require_capability('moodle/badges:configurecriteria', $context);
131 $status = ($badge->status == BADGE_STATUS_ACTIVE) ? BADGE_STATUS_INACTIVE : BADGE_STATUS_INACTIVE_LOCKED;
132 $badge->set_status($status);
133 redirect($returnurl);