Proyectos de Subversion Moodle

Rev

| 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
// This program 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
// This program 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
/**
18
 * The main edusharing configuration form
19
 *
20
 * It uses the standard core Moodle formslib. For more info about them, please
21
 * visit: http://docs.moodle.org/en/Development:lib/formslib.php
22
 *
23
 * @package    mod_edusharing
24
 * @copyright  metaVentis GmbH — http://metaventis.com
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
use mod_edusharing\Constants;
29
use mod_edusharing\EduSharingService;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
global $CFG;
34
 
35
require_once($CFG->dirroot . '/course/moodleform_mod.php');
36
 
37
/**
38
 * The main edusharing configuration form
39
 *
40
 * @copyright  metaVentis GmbH — http://metaventis.com
41
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
43
class mod_edusharing_mod_form extends moodleform_mod {
44
    /**
45
     * (non-PHPdoc)
46
     * @see lib/moodleform::definition()
47
     */
48
    public function definition(): void {
49
        try {
50
            $edusharingservice = new EduSharingService();
51
            $ticket            = $edusharingservice->get_ticket();
52
            // Adding the "general" fieldset, where all the common settings are shown.
53
            $this->_form->addElement('header', 'general', get_string('general', 'form'));
54
            // Adding the standard "name" field.
55
            $this->_form->addElement('text', 'name',
56
                get_string('edusharingname', Constants::EDUSHARING_MODULE_NAME), ['size' => '64']);
57
            $this->_form->setType('name', PARAM_TEXT);
58
            $this->_form->addRule('name', null, 'required', null, 'client');
59
            $this->_form->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
60
            $this->standard_intro_elements(get_string('description', Constants::EDUSHARING_MODULE_NAME));
61
            // Repo button and version select are not to be shown for edit form.
62
            if (!isset($_GET['update'])) {
63
                $this->_form->addElement('header', 'object_url_fieldset',
64
                    get_string('object_url_fieldset', Constants::EDUSHARING_MODULE_NAME,
65
                        get_config('edusharing', 'application_appname')));
66
                $this->_form->addElement('static', 'object_title',
67
                    get_string('object_title', Constants::EDUSHARING_MODULE_NAME),
68
                    get_string('object_title_help', Constants::EDUSHARING_MODULE_NAME));
69
                $this->_form->addElement('text', 'object_url',
70
                    get_string('object_url', Constants::EDUSHARING_MODULE_NAME), ['readonly' => 'true']);
71
                $this->_form->setType('object_url', PARAM_RAW_TRIMMED);
72
                $this->_form->addRule('object_url', null, 'required', null, 'client');
73
                $this->_form->addRule('object_url', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
74
                $this->_form->addHelpButton('object_url', 'object_url', Constants::EDUSHARING_MODULE_NAME);
75
                $searchurl    = get_config('edusharing', 'application_cc_gui_url');
76
                $reposearch   = trim($searchurl, '/') . '/components/search?&applyDirectories=false&reurl=WINDOW&ticket=' . $ticket;
77
                $searchbutton = $this->_form->addElement('button', 'searchbutton',
78
                    get_string('searchrec', Constants::EDUSHARING_MODULE_NAME,
79
                        get_config('edusharing', 'application_appname')));
80
                // phpcs:disable -- just messy html and js.
81
                $repoonclick  = "
82
                            function openRepo(){
83
                                window.addEventListener('message', function handleRepo(event) {
84
                                    if (event.data.event == 'APPLY_NODE') {
85
                                        const node = event.data.data;
86
                                        window.console.log(node);
87
                                        window.win.close();
88
                                        window.document.getElementById('id_object_url').value = node.objectUrl;
89
                                        let title = node.title;
90
                                        if(!title){
91
                                            title = node.properties['cm:name'];
92
                                        }
93
                                        let version = -1;
94
                                        let versionArray = node.properties['cclom:version'];
95
                                        if (versionArray !== undefined) {
96
                                            version = node.properties['cclom:version'][0];
97
                                            window.document.getElementById('id_object_version_1').value = version;
98
                                        }
99
                                        let aspects = node.aspects;
100
                                        if (aspects.includes('ccm:published') || aspects.includes('ccm:collection_io_reference') || version === -1) {
101
                                            window.document.getElementById('id_object_version_0').checked = true;
102
                                            window.document.getElementById('id_object_version_1').closest('label').hidden = true;
103
                                        }
104
                                        window.document.getElementById('fitem_id_object_title')
105
                                            .getElementsByClassName('form-control-static')[0].innerHTML = title;
106
                                        if(window.document.getElementById('id_name').value === ''){
107
                                            window.document.getElementById('id_name').value = title;
108
                                        }
109
                                        window.removeEventListener('message', handleRepo, false );
110
                                    }
111
                                }, false);
112
                                window.win = window.open('" . $reposearch . "');
113
                            }
114
                            openRepo();
115
                        ";
116
                // phpcs:enable
117
                $searchbutton->updateAttributes(
118
                    [
119
                        'title' => get_string('uploadrec', Constants::EDUSHARING_MODULE_NAME,
120
                            get_config('edusharing', 'application_appname')),
121
                        'onclick' => $repoonclick,
122
                    ]
123
                );
124
                $this->_form->addElement('header', 'version_fieldset',
125
                    get_string('object_version_fieldset', Constants::EDUSHARING_MODULE_NAME));
126
                $radiogroup   = [];
127
                $radiogroup[] = $this->_form->createElement('radio', 'object_version', '',
128
                    get_string('object_version_use_latest', Constants::EDUSHARING_MODULE_NAME), 0, []);
129
                $radiogroup[] = $this->_form->createElement('radio', 'object_version', '',
130
                    get_string('object_version_use_exact', Constants::EDUSHARING_MODULE_NAME), 1, []);
131
                $this->_form->addGroup($radiogroup, 'object_version',
132
                    get_string('object_version', Constants::EDUSHARING_MODULE_NAME), [' '], false);
133
                $this->_form->setDefault('object_version', 0);
134
                $this->_form->addHelpButton('object_version', 'object_version', Constants::EDUSHARING_MODULE_NAME);
135
            }
136
            // Display-section.
137
            $this->_form->addElement('header', 'object_display_fieldset',
138
                get_string('object_display_fieldset', Constants::EDUSHARING_MODULE_NAME));
139
            $windowoptions =
140
                [
141
 
142
                    1 => get_string('newwindow', Constants::EDUSHARING_MODULE_NAME),
143
                ];
144
            $this->_form->addElement('select', 'popup_window',
145
                get_string('display', Constants::EDUSHARING_MODULE_NAME), $windowoptions);
146
            $this->_form->setDefault('popup_window', !empty($CFG->resource_popup));
147
            // Add standard elements, common to all modules.
148
            $this->standard_coursemodule_elements();
149
            $submit2label = get_string('savechangesandreturntocourse');
150
        } catch (Exception $e) {
151
            debugging($e->getLine() . ': ' . $e->getMessage());
152
        }
153
        $buttons = [];
154
        if (!empty($submit2label) && $this->courseformat->has_view_page()) {
155
            $buttons[] = $this->_form->createElement('submit', 'submitbutton2', $submit2label);
156
        }
157
        $buttons[] = $this->_form->createElement('cancel');
158
        $this->_form->addGroup($buttons, 'buttonar', '', [' '], false);
159
        $this->_form->setType('buttonar', PARAM_RAW);
160
        $this->_form->closeHeaderBefore('buttonar');
161
    }
162
}