Merge branch 'MDL-81472' of https://github.com/paulholden/moodle
[moodle.git] / badges / alignment.php
blobcfd4a460fa878e45a454ff2cff7a2e06b694d100
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/>.
16 /**
17 * List alignments, skills, or standards are targeted by a BadgeClass.
19 * @package core
20 * @subpackage badges
21 * @copyright 2018 Tung Thai
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @author Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
25 require_once(__DIR__ . '/../config.php');
26 require_once($CFG->libdir . '/badgeslib.php');
27 require_once($CFG->dirroot . '/badges/alignment_form.php');
29 $badgeid = required_param('id', PARAM_INT);
30 $alignmentid = optional_param('alignmentid', 0, PARAM_INT);
31 $action = optional_param('action', '', PARAM_TEXT);
32 $lang = current_language();
34 require_login();
35 if (empty($CFG->enablebadges)) {
36 throw new \moodle_exception('badgesdisabled', 'badges');
38 $badge = new badge($badgeid);
39 $context = $badge->get_context();
40 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type));
41 require_capability('moodle/badges:configuredetails', $context);
43 if ($badge->type == BADGE_TYPE_COURSE) {
44 if (empty($CFG->badges_allowcoursebadges)) {
45 throw new \moodle_exception('coursebadgesdisabled', 'badges');
47 require_login($badge->courseid);
48 $course = get_course($badge->courseid);
49 $heading = format_string($course->fullname, true, ['context' => $context]);
50 $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type, 'id' => $badge->courseid));
51 $PAGE->set_pagelayout('standard');
52 navigation_node::override_active_url($navurl);
53 } else {
54 $PAGE->set_pagelayout('admin');
55 $heading = get_string('administrationsite');
56 navigation_node::override_active_url($navurl, true);
59 $currenturl = new moodle_url('/badges/alignment.php', array('id' => $badge->id));
60 $PAGE->set_context($context);
61 $PAGE->set_url($currenturl);
62 $PAGE->set_heading($heading);
63 $PAGE->set_title($badge->name);
64 $PAGE->navbar->add($badge->name);
66 $output = $PAGE->get_renderer('core', 'badges');
67 $msg = optional_param('msg', '', PARAM_TEXT);
68 $emsg = optional_param('emsg', '', PARAM_TEXT);
69 $url = new moodle_url('/badges/alignment.php', array('id' => $badge->id, 'action' => $action, 'alignmentid' => $alignmentid));
70 $mform = new alignment_form($url, array('badge' => $badge, 'action' => $action, 'alignmentid' => $alignmentid));
71 if ($mform->is_cancelled()) {
72 redirect($currenturl);
73 } else if ($mform->is_submitted() && $mform->is_validated() && ($data = $mform->get_data())) {
74 $alignment = new stdClass();
75 $alignment->badgeid = $badgeid;
76 $alignment->targetname = $data->targetname;
77 $alignment->targeturl = $data->targeturl;
78 $alignment->targetframework = $data->targetframework;
79 $alignment->targetcode = $data->targetcode;
80 $alignment->targetdescription = trim($data->targetdescription);
81 $badge->save_alignment($alignment, $alignmentid);
82 redirect($currenturl);
85 echo $OUTPUT->header();
86 $actionbar = new \core_badges\output\manage_badge_action_bar($badge, $PAGE);
87 echo $output->render_tertiary_navigation($actionbar);
88 echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name);
89 echo $output->print_badge_status_box($badge);
90 if ($emsg !== '') {
91 echo $OUTPUT->notification($emsg);
92 } else if ($msg !== '') {
93 echo $OUTPUT->notification(get_string($msg, 'badges'), 'notifysuccess');
95 echo $output->notification(get_string('notealignment', 'badges'), 'info');
97 if ($alignmentid || $action == 'add' || $action == 'edit') {
98 $mform->display();
99 } else if (empty($action)) {
100 if (!$badge->is_active() && !$badge->is_locked()) {
101 $urlbutton = new moodle_url('/badges/alignment.php', array('id' => $badge->id, 'action' => 'add'));
102 echo $OUTPUT->box($OUTPUT->single_button($urlbutton, get_string('addalignment', 'badges')), 'clearfix mdl-align');
104 $alignments = $badge->get_alignments();
105 if (count($alignments) > 0) {
106 $renderrelated = new \core_badges\output\badge_alignments($alignments, $badgeid);
107 echo $output->render($renderrelated);
108 } else {
109 echo $output->notification(get_string('noalignment', 'badges'));
113 echo $OUTPUT->footer();