Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
17
/**
1441 ariadna 18
 * Editing badge details, criteria, messages.
1 efrain 19
 *
1441 ariadna 20
 * @package    core_badges
1 efrain 21
 * @copyright  2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
24
 */
25
 
26
require_once(__DIR__ . '/../config.php');
27
require_once($CFG->libdir . '/badgeslib.php');
28
require_once($CFG->libdir . '/filelib.php');
29
 
1441 ariadna 30
// Used only for creating new badge.
31
$courseid = optional_param('courseid', 0, PARAM_INT);
32
if ($courseid === 0 ) {
33
    $courseid = null;
34
}
35
 
36
// Used for editing existing badge.
37
$badgeid = optional_param('id', null, PARAM_INT);
1 efrain 38
$action = optional_param('action', 'badge', PARAM_TEXT);
39
 
40
require_login();
41
 
42
if (empty($CFG->enablebadges)) {
43
    throw new \moodle_exception('badgesdisabled', 'badges');
44
}
45
 
1441 ariadna 46
if (!empty($badgeid)) {
47
    // Existing badge.
48
    $badge = new badge($badgeid);
1 efrain 49
 
1441 ariadna 50
    if ($badge->courseid) {
51
        $course = get_course($badge->courseid);
52
    }
53
    $params = ['id' => $badgeid, 'action' => $action];
54
    $badgename = $badge->name;
55
 
56
    // Check capabilities.
57
    $context = $badge->get_context();
58
    if ($action == 'message') {
59
        $title = [get_string('configuremessage', 'badges'), $badge->name];
60
        require_capability('moodle/badges:configuremessages', $context);
61
    } else {
62
        $title = [get_string('badgedetails', 'badges'), $badge->name];
63
        require_capability('moodle/badges:configuredetails', $context);
64
    }
65
 
66
    $cancelurl = new moodle_url('/badges/overview.php', ['id' => $badgeid]);
1 efrain 67
} else {
1441 ariadna 68
    // New badge.
69
    if ($courseid) {
70
        $course = get_course($courseid);
71
        $context = context_course::instance($course->id);
72
    } else {
73
        $context = context_system::instance();
74
    }
75
 
76
    $badge = new stdClass();
77
    $badge->id = null;
78
    $badge->type = $courseid ? BADGE_TYPE_COURSE : BADGE_TYPE_SITE;
79
    $badge->courseid = $courseid;
80
 
81
    $params = ['courseid' => $courseid];
82
    $badgename = get_string('create', 'badges');
83
    $title = [$badgename];
84
 
85
    // Check capabilities.
86
    require_capability('moodle/badges:createbadge', $context);
87
 
88
    $cancelurl = new moodle_url('/badges/index.php', ['type' => $badge->type, 'id' => $courseid]);
1 efrain 89
}
90
 
1441 ariadna 91
// Check if course badges are enabled.
92
if (empty($CFG->badges_allowcoursebadges) && ($badge->type == BADGE_TYPE_COURSE)) {
93
    throw new \moodle_exception('coursebadgesdisabled', 'badges');
94
}
95
 
96
$navurl = new moodle_url('/badges/index.php', ['type' => $badge->type]);
1 efrain 97
if ($badge->type == BADGE_TYPE_COURSE) {
98
    require_login($badge->courseid);
99
    $heading = format_string($course->fullname, true, ['context' => $context]);
1441 ariadna 100
    $title[] = $heading;
101
    $navurl = new moodle_url('/badges/index.php', ['type' => $badge->type, 'id' => $badge->courseid]);
1 efrain 102
    $PAGE->set_pagelayout('incourse');
103
    navigation_node::override_active_url($navurl);
104
} else {
105
    $PAGE->set_pagelayout('admin');
106
    $heading = get_string('administrationsite');
107
    navigation_node::override_active_url($navurl, true);
108
}
109
 
1441 ariadna 110
$currenturl = new moodle_url('/badges/edit.php', $params);
1 efrain 111
 
112
$PAGE->set_context($context);
113
$PAGE->set_url($currenturl);
114
$PAGE->set_heading($heading);
1441 ariadna 115
$PAGE->set_title(implode(\moodle_page::TITLE_SEPARATOR, $title));
1 efrain 116
$PAGE->add_body_class('limitedwidth');
1441 ariadna 117
$PAGE->navbar->add($badgename);
1 efrain 118
 
1441 ariadna 119
/** @var \core_badges_renderer $output*/
1 efrain 120
$output = $PAGE->get_renderer('core', 'badges');
121
$statusmsg = '';
122
$errormsg  = '';
123
 
1441 ariadna 124
$editoroptions = [];
125
if ($badge->id && $action == 'message') {
126
    $badge->message = clean_text($badge->message, FORMAT_HTML);
127
    $editoroptions = [
1 efrain 128
        'subdirs' => 0,
129
        'maxbytes' => 0,
130
        'maxfiles' => 0,
131
        'changeformat' => 0,
132
        'context' => $context,
133
        'noclean' => false,
1441 ariadna 134
        'trusttext' => false,
135
    ];
136
    $badge = file_prepare_standard_editor($badge, 'message', $editoroptions, $context);
137
}
1 efrain 138
 
1441 ariadna 139
$formclass = '\core_badges\form' . '\\' . ($action == 'new' ? 'badge' : $action);
140
$params = [
141
    'action' => $action,
142
];
143
if ($badge->id) {
144
    $params['badge'] = $badge;
145
    $params['editoroptions'] = $editoroptions;
146
} else {
147
    $params['courseid'] = $courseid;
148
}
1 efrain 149
 
1441 ariadna 150
$form = new $formclass($currenturl, $params);
1 efrain 151
if ($form->is_cancelled()) {
1441 ariadna 152
    redirect($cancelurl);
1 efrain 153
} else if ($form->is_submitted() && $form->is_validated() && ($data = $form->get_data())) {
1441 ariadna 154
    switch ($action) {
155
        case 'new':
156
            // Create new badge.
157
            $badge = badge::create_badge($data, $courseid);
158
            $badgeid = $badge->id;
159
            badges_process_badge_image($badge, $form->save_temp_file('image'));
1 efrain 160
 
1441 ariadna 161
            // If a user can configure badge criteria, they will be redirected to the criteria page.
162
            if (has_capability('moodle/badges:configurecriteria', $context)) {
163
                redirect(new moodle_url('/badges/criteria.php', ['id' => $badgeid]));
164
            }
165
            redirect(new moodle_url('/badges/overview.php', ['id' => $badgeid]));
166
            break;
1 efrain 167
 
1441 ariadna 168
        case 'badge':
169
            // Edit existing badge.
170
            if ($badge->update($data)) {
171
                badges_process_badge_image($badge, $form->save_temp_file('image'));
172
                $form->set_data($badge);
173
                $statusmsg = get_string('changessaved');
1 efrain 174
            } else {
1441 ariadna 175
                $errormsg = get_string('error:save', 'badges');
1 efrain 176
            }
1441 ariadna 177
            break;
1 efrain 178
 
1441 ariadna 179
        case 'message':
180
            // Update badge message.
181
            if ($badge->update_message($data)) {
182
                $statusmsg = get_string('changessaved');
183
            } else {
184
                $errormsg = get_string('error:save', 'badges');
185
            }
186
            break;
1 efrain 187
    }
188
}
189
 
1441 ariadna 190
echo $output->header();
1 efrain 191
 
1441 ariadna 192
if ($badge->id) {
193
    $actionbar = new \core_badges\output\manage_badge_action_bar($badge, $PAGE);
194
    echo $output->render_tertiary_navigation($actionbar);
195
    echo $output->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
1 efrain 196
 
1441 ariadna 197
    if ($errormsg !== '') {
198
        echo $output->notification($errormsg);
1 efrain 199
 
1441 ariadna 200
    } else if ($statusmsg !== '') {
201
        echo $output->notification($statusmsg, 'notifysuccess');
202
    }
203
    echo $output->print_badge_status_box($badge);
204
} else {
205
    echo $output->heading($badgename);
1 efrain 206
}
207
 
208
$form->display();
209
 
1441 ariadna 210
echo $output->footer();