Merge branch 'wip-MDL-41967-master' of git://github.com/abgreeve/moodle
[moodle.git] / badges / index.php
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.
13 //
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 for badges management
19  *
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>
25  */
27 require_once(dirname(dirname(__FILE__)) . '/config.php');
28 require_once($CFG->libdir . '/badgeslib.php');
30 $type       = required_param('type', PARAM_INT);
31 $courseid   = optional_param('id', 0, PARAM_INT);
32 $page       = optional_param('page', 0, PARAM_INT);
33 $activate   = optional_param('activate', 0, PARAM_INT);
34 $deactivate = optional_param('lock', 0, PARAM_INT);
35 $sortby     = optional_param('sort', 'name', PARAM_ALPHA);
36 $sorthow    = optional_param('dir', 'ASC', PARAM_ALPHA);
37 $confirm    = optional_param('confirm', false, PARAM_BOOL);
38 $delete     = optional_param('delete', 0, PARAM_INT);
39 $msg        = optional_param('msg', '', PARAM_TEXT);
41 if (!in_array($sortby, array('name', 'status'))) {
42     $sortby = 'name';
43 }
45 if ($sorthow != 'ASC' and $sorthow != 'DESC') {
46     $sorthow = 'ASC';
47 }
49 if ($page < 0) {
50     $page = 0;
51 }
53 require_login();
55 if (empty($CFG->enablebadges)) {
56     print_error('badgesdisabled', 'badges');
57 }
59 if (empty($CFG->badges_allowcoursebadges) && ($type == BADGE_TYPE_COURSE)) {
60     print_error('coursebadgesdisabled', 'badges');
61 }
63 $err = '';
64 $urlparams = array('sort' => $sortby, 'dir' => $sorthow, 'page' => $page);
66 if ($course = $DB->get_record('course', array('id' => $courseid))) {
67     $urlparams['type'] = $type;
68     $urlparams['id'] = $course->id;
69 } else {
70     $urlparams['type'] = $type;
71 }
73 $hdr = get_string('managebadges', 'badges');
74 $returnurl = new moodle_url('/badges/index.php', $urlparams);
75 $PAGE->set_url($returnurl);
77 if ($type == BADGE_TYPE_SITE) {
78     $title = get_string('sitebadges', 'badges');
79     $PAGE->set_context(context_system::instance());
80     $PAGE->set_pagelayout('admin');
81     $PAGE->set_heading($title . ': ' . $hdr);
82     navigation_node::override_active_url(new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_SITE)));
83 } else {
84     require_login($course);
85     $title = get_string('coursebadges', 'badges');
86     $PAGE->set_context(context_course::instance($course->id));
87     $PAGE->set_pagelayout('course');
88     $PAGE->set_heading($course->fullname . ': ' . $hdr);
89     navigation_node::override_active_url(
90         new moodle_url('/badges/index.php', array('type' => BADGE_TYPE_COURSE, 'id' => $course->id))
91     );
92 }
94 if (!has_any_capability(array(
95         'moodle/badges:viewawarded',
96         'moodle/badges:createbadge',
97         'moodle/badges:awardbadge',
98         'moodle/badges:configuremessages',
99         'moodle/badges:configuredetails',
100         'moodle/badges:deletebadge'), $PAGE->context)) {
101     redirect($CFG->wwwroot);
104 $PAGE->set_title($hdr);
105 $PAGE->requires->js('/badges/backpack.js');
106 $PAGE->requires->js_init_call('check_site_access', null, false);
107 $output = $PAGE->get_renderer('core', 'badges');
109 if ($delete && has_capability('moodle/badges:deletebadge', $PAGE->context)) {
110     $badge = new badge($delete);
111     if (!$confirm) {
112         echo $output->header();
113         echo $output->confirm(
114                     get_string('delconfirm', 'badges', $badge->name),
115                     new moodle_url($PAGE->url, array('delete' => $badge->id, 'confirm' => 1)),
116                     $returnurl
117                 );
118         echo $output->footer();
119         die();
120     } else {
121         require_sesskey();
122         $badge->delete();
123         redirect($returnurl);
124     }
127 if ($deactivate && has_capability('moodle/badges:configuredetails', $PAGE->context)) {
128     require_sesskey();
129     $badge = new badge($deactivate);
130     if ($badge->is_locked()) {
131         $badge->set_status(BADGE_STATUS_INACTIVE_LOCKED);
132     } else {
133         $badge->set_status(BADGE_STATUS_INACTIVE);
134     }
135     $msg = 'deactivatesuccess';
136     $returnurl->param('msg', $msg);
137     redirect($returnurl);
140 echo $OUTPUT->header();
141 if ($type == BADGE_TYPE_SITE) {
142     echo $OUTPUT->heading_with_help($PAGE->heading, 'sitebadges', 'badges');
143 } else {
144     echo $OUTPUT->heading($PAGE->heading);
146 echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection');
148 $totalcount = count(badges_get_badges($type, $courseid, '', '' , '', ''));
149 $records = badges_get_badges($type, $courseid, $sortby, $sorthow, $page, BADGE_PERPAGE);
151 if ($totalcount) {
152     echo $output->heading(get_string('badgestoearn', 'badges', $totalcount), 4);
154     if ($course && $course->startdate > time()) {
155         echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
156     }
158     if ($err !== '') {
159         echo $OUTPUT->notification($err, 'notifyproblem');
160     }
162     if ($msg !== '') {
163         echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess');
164     }
166     $badges             = new badge_management($records);
167     $badges->sort       = $sortby;
168     $badges->dir        = $sorthow;
169     $badges->page       = $page;
170     $badges->perpage    = BADGE_PERPAGE;
171     $badges->totalcount = $totalcount;
173     echo $output->render($badges);
174 } else {
175     echo $output->notification(get_string('nobadges', 'badges'));
177     if (has_capability('moodle/badges:createbadge', $PAGE->context)) {
178         echo $OUTPUT->single_button(new moodle_url('newbadge.php', array('type' => $type, 'id' => $courseid)),
179             get_string('newbadge', 'badges'));
180     }
183 echo $OUTPUT->footer();