on-demand release 4.0beta+
[moodle.git] / badges / action.php
blobc2a152db32043d048d0041d2376e291c529ed506
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 $course = get_course($badge->courseid);
46 $heading = format_string($course->fullname, true, ['context' => $context]);
47 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
48 $PAGE->set_pagelayout('standard');
49 navigation_node::override_active_url($navurl);
50 } else {
51 $PAGE->set_pagelayout('admin');
52 $heading = get_string('administrationsite');
53 navigation_node::override_active_url($navurl, true);
56 $PAGE->set_context($context);
57 $PAGE->set_url('/badges/action.php', array('id' => $badge->id));
59 if ($return !== 0) {
60 $returnurl = new moodle_url($return);
61 } else {
62 $returnurl = new moodle_url('/badges/overview.php', array('id' => $badge->id));
64 $returnurl->remove_params('awards');
66 if ($copy) {
67 require_sesskey();
68 require_capability('moodle/badges:createbadge', $context);
70 $cloneid = $badge->make_clone();
71 // If a user can edit badge details, they will be redirected to the edit page.
72 if (has_capability('moodle/badges:configuredetails', $context)) {
73 redirect(new moodle_url('/badges/edit.php', array('id' => $cloneid, 'action' => 'badge')));
75 redirect(new moodle_url('/badges/overview.php', array('id' => $cloneid)));
78 if ($activate) {
79 require_capability('moodle/badges:configurecriteria', $context);
81 $PAGE->url->param('activate', 1);
82 $status = ($badge->status == BADGE_STATUS_INACTIVE) ? BADGE_STATUS_ACTIVE : BADGE_STATUS_ACTIVE_LOCKED;
83 if ($confirm == 1) {
84 require_sesskey();
85 $badge->set_status($status);
86 $returnurl->param('msg', 'activatesuccess');
88 if ($badge->type == BADGE_TYPE_SITE) {
89 // Review on cron if there are more than 1000 users who can earn a site-level badge.
90 $sql = 'SELECT COUNT(u.id) as num
91 FROM {user} u
92 LEFT JOIN {badge_issued} bi
93 ON u.id = bi.userid AND bi.badgeid = :badgeid
94 WHERE bi.badgeid IS NULL AND u.id != :guestid AND u.deleted = 0';
95 $toearn = $DB->get_record_sql($sql, array('badgeid' => $badge->id, 'guestid' => $CFG->siteguest));
97 if ($toearn->num < 1000) {
98 $awards = $badge->review_all_criteria();
99 $returnurl->param('awards', $awards);
100 } else {
101 $returnurl->param('awards', 'cron');
103 } else {
104 $awards = $badge->review_all_criteria();
105 $returnurl->param('awards', $awards);
107 redirect($returnurl);
110 $strheading = get_string('reviewbadge', 'badges');
111 $PAGE->navbar->add($strheading);
112 $PAGE->set_title($strheading);
113 $PAGE->set_heading($heading);
114 echo $OUTPUT->header();
115 echo $OUTPUT->heading($strheading);
117 $params = array('id' => $badge->id, 'activate' => 1, 'sesskey' => sesskey(), 'confirm' => 1, 'return' => $return);
118 $url = new moodle_url('/badges/action.php', $params);
120 if (!$badge->has_criteria()) {
121 redirect($returnurl, get_string('error:cannotact', 'badges') . get_string('nocriteria', 'badges'), null, \core\output\notification::NOTIFY_ERROR);
122 } else {
123 $message = get_string('reviewconfirm', 'badges', $badge->name);
124 echo $OUTPUT->confirm($message, $url, $returnurl);
126 echo $OUTPUT->footer();
127 die;
130 if ($deactivate) {
131 require_sesskey();
132 require_capability('moodle/badges:configurecriteria', $context);
134 $status = ($badge->status == BADGE_STATUS_ACTIVE) ? BADGE_STATUS_INACTIVE : BADGE_STATUS_INACTIVE_LOCKED;
135 $badge->set_status($status);
136 redirect($returnurl);