MDL-42068 Filepicker course listing use setting courselistshortname to show shortname...
[moodle.git] / badges / newbadge.php
blob27f827ae3bc07429fcd3f02647e851141fe17e28
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 * First step page for creating a new badge
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');
29 require_once($CFG->dirroot . '/badges/edit_form.php');
31 $type = required_param('type', PARAM_INT);
32 $courseid = optional_param('id', 0, PARAM_INT);
34 require_login();
36 if (empty($CFG->enablebadges)) {
37 print_error('badgesdisabled', 'badges');
40 if (empty($CFG->badges_allowcoursebadges) && ($type == BADGE_TYPE_COURSE)) {
41 print_error('coursebadgesdisabled', 'badges');
44 $title = get_string('create', 'badges');
46 if (($type == BADGE_TYPE_COURSE) && ($course = $DB->get_record('course', array('id' => $courseid)))) {
47 require_login($course);
48 $PAGE->set_context(context_course::instance($course->id));
49 $PAGE->set_pagelayout('course');
50 $PAGE->set_url('/badges/newbadge.php', array('type' => $type, 'id' => $course->id));
51 $PAGE->set_heading($course->fullname . ": " . $title);
52 $PAGE->set_title($course->fullname . ": " . $title);
53 } else {
54 $PAGE->set_context(context_system::instance());
55 $PAGE->set_pagelayout('admin');
56 $PAGE->set_url('/badges/newbadge.php', array('type' => $type));
57 $PAGE->set_heading($title);
58 $PAGE->set_title($title);
61 require_capability('moodle/badges:createbadge', $PAGE->context);
63 $PAGE->requires->js('/badges/backpack.js');
64 $PAGE->requires->js_init_call('check_site_access', null, false);
66 $fordb = new stdClass();
67 $fordb->id = null;
69 $form = new edit_details_form($PAGE->url, array('action' => 'new'));
71 if ($form->is_cancelled()) {
72 redirect(new moodle_url('/badges/index.php', array('type' => $type, 'id' => $courseid)));
73 } else if ($data = $form->get_data()) {
74 // Creating new badge here.
75 $now = time();
77 $fordb->name = $data->name;
78 $fordb->description = $data->description;
79 $fordb->timecreated = $now;
80 $fordb->timemodified = $now;
81 $fordb->usercreated = $USER->id;
82 $fordb->usermodified = $USER->id;
83 $fordb->image = 0;
84 $fordb->issuername = $data->issuername;
85 $fordb->issuerurl = $data->issuerurl;
86 $fordb->issuercontact = $data->issuercontact;
87 $fordb->expiredate = ($data->expiry == 1) ? $data->expiredate : null;
88 $fordb->expireperiod = ($data->expiry == 2) ? $data->expireperiod : null;
89 $fordb->type = $type;
90 $fordb->courseid = ($type == BADGE_TYPE_COURSE) ? $courseid : null;
91 $fordb->messagesubject = get_string('messagesubject', 'badges');
92 $fordb->message = get_string('messagebody', 'badges',
93 html_writer::link($CFG->wwwroot . '/badges/mybadges.php', get_string('mybadges', 'badges')));
94 $fordb->attachment = 1;
95 $fordb->notification = BADGE_MESSAGE_NEVER;
96 $fordb->status = BADGE_STATUS_INACTIVE;
98 $newid = $DB->insert_record('badge', $fordb, true);
100 $newbadge = new badge($newid);
101 badges_process_badge_image($newbadge, $form->save_temp_file('image'));
102 // If a user can configure badge criteria, they will be redirected to the criteria page.
103 if (has_capability('moodle/badges:configurecriteria', $PAGE->context)) {
104 redirect(new moodle_url('/badges/criteria.php', array('id' => $newid)));
106 redirect(new moodle_url('/badges/overview.php', array('id' => $newid)));
109 echo $OUTPUT->header();
110 echo $OUTPUT->box('', 'notifyproblem hide', 'check_connection');
112 $form->display();
114 echo $OUTPUT->footer();