MDL-69773 block_section_links: Add an option to display section name
[moodle.git] / badges / related_form.php
CommitLineData
d363a5c2
TT
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 * Form related badges.
18 *
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>
24 */
25defined('MOODLE_INTERNAL') || die();
26require_once($CFG->libdir . '/formslib.php');
27require_once($CFG->libdir . '/badgeslib.php');
28
29/**
30 * Form to edit badge details.
31 *
32 * @copyright 2018 Tung Thai
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 * @author Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
35 */
36class edit_relatedbadge_form extends moodleform {
37
38 /**
39 * Defines the form.
40 */
41 public function definition() {
42 $mform = $this->_form;
43 $badge = $this->_customdata['badge'];
44 $badgesarray = $this->get_badges_option($badge);
45 $mform->addElement('header', 'alignment', get_string('relatedbages', 'badges'));
46 if (!$badgesarray) {
47 $badgesarray = array(get_string('none'));
48 $attributes = array('size' => '3', 'disabled' => true, 'style' => 'min-width: 200px');
49 } else {
50 $attributes = array('size' => '10');
51 }
52 $mform->addElement('select', 'relatedbadgeids', get_string('relatedbages', 'badges'), $badgesarray, $attributes);
53 $mform->getElement('relatedbadgeids')->setMultiple(true);
54 $this->add_action_buttons();
55
56 // Freeze all elements if badge is active or locked.
57 if ($badge->is_active() || $badge->is_locked()) {
58 $mform->hardFreezeAllVisibleExcept(array());
59 }
60 }
61
62 /**
63 * Validates form data.
64 *
65 * @param array $data submitted data.
66 * @param array $files submitted files.
67 * @return array $errors An array of errors.
68 */
69 public function validation($data, $files) {
70 $errors = parent::validation($data, $files);
71 return $errors;
72 }
73
74 /**
75 * Return list badge of a course or list badge site.
76 *
77 * @param object $badge Badge object.
78 * @return array $options An array the badges.
79 */
80 public function get_badges_option($badge) {
81 global $DB;
5a1ea828 82 $sql = "SELECT b.id, b.name, b.version, b.language, b.type
d363a5c2
TT
83 FROM {badge} b
84 WHERE b.id <> :badgeid
810da68e
JD
85 AND b.id NOT IN (
86 SELECT DISTINCT b.id
87 FROM {badge_related} br
88 JOIN {badge} b ON (br.relatedbadgeid = b.id OR br.badgeid = b.id)
89 WHERE (br.badgeid = :badgeid2 OR br.relatedbadgeid = :badgeid3) AND b.id != :badgeid4)";
90 $params = ['badgeid' => $badge->id, 'badgeid2' => $badge->id, 'badgeid3' => $badge->id, 'badgeid4' => $badge->id];
d363a5c2
TT
91 if ($badge->type == BADGE_TYPE_COURSE) {
92 $sql .= " AND (b.courseid = :courseid OR b.type = :badgetype)";
93 $params['courseid'] = $badge->courseid;
94 $params['badgetype'] = BADGE_TYPE_SITE;
95 }
96
97 $records = $DB->get_records_sql($sql, $params);
98 $languages = get_string_manager()->get_list_of_languages();
99 $options = array();
100 foreach ($records as $record) {
101 $language = isset($languages[$record->language]) ? $languages[$record->language] : '';
102 $options[$record->id] = $record->name .
103 ' (version: ' . $record->version . ', language: ' . $language . ', ' .
104 ($record->type == BADGE_TYPE_COURSE ? get_string('badgesview', 'badges') : get_string('sitebadges', 'badges')) .
105 ')';
106 }
107 return $options;
108 }
109
110}