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
// 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
namespace tool_courserating\form;
18
 
19
use moodle_exception;
20
use moodle_url;
21
use tool_courserating\api;
22
use tool_courserating\constants;
23
use tool_courserating\helper;
24
use tool_courserating\local\models\summary;
25
use tool_courserating\permission;
26
 
27
/**
28
 * Form to add or change a rating
29
 *
30
 * @package     tool_courserating
31
 * @copyright   2022 Marina Glancy <marina.glancy@gmail.com>
32
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class addrating extends \core_form\dynamic_form {
35
 
36
    /**
37
     * Current course id
38
     *
39
     * @return int
40
     */
41
    protected function get_course_id(): int {
42
        $courseid = $this->optional_param('courseid', 0, PARAM_INT);
43
        if (!$courseid) {
44
            throw new moodle_exception('missingparam', '', '', 'courseid');
45
        }
46
        return $courseid;
47
    }
48
 
49
    /**
50
     * Form definition
51
     */
52
    protected function definition() {
53
        global $OUTPUT;
54
        $mform = $this->_form;
55
        $mform->addElement('hidden', 'courseid', $this->get_course_id());
56
        $mform->setType('courseid', PARAM_INT);
57
 
58
        $summary = summary::get_for_course($this->get_course_id());
59
        if ($summary->get('cntall')) {
60
            $courseid = $this->get_course_id();
61
            $str = get_string('viewallreviews', 'tool_courserating');
62
            $mform->addElement('html', <<<EOF
63
<p class="mdl-align"><a href="#" data-action="tool_courserating-viewratings" data-courseid="$courseid">$str</a></p>
64
EOF
65
            );
66
        }
67
 
68
        $radioarray = [];
69
        foreach ([1, 2, 3, 4, 5] as $r) {
70
            $label = $OUTPUT->pix_icon('star', $r, 'tool_courserating', ['class' => 'star-on tool_courserating-stars']);
71
            $label .= $OUTPUT->pix_icon('star-o', $r, 'tool_courserating', ['class' => 'star-off tool_courserating-stars']);
72
            $label = \html_writer::span($label);
73
            /** @var \MoodleQuickForm_radio $el */
74
            $el = $mform->createElement('radio', 'rating', '', $label, $r);
75
            $el->setAttributes($el->getAttributes() + ['class' => ' stars-' . $r]);
76
            $radioarray[] = $el;
77
        }
78
        $el = $mform->addGroup($radioarray, 'ratinggroup', get_string('rating', 'tool_courserating'), [' ', ' '], false);
79
        $el->setAttributes($el->getAttributes() + ['class' => 'tool_courserating-form-stars-group']);
80
 
81
        if (helper::get_setting(constants::SETTING_USEHTML)) {
82
            $options = helper::review_editor_options($this->get_context_for_dynamic_submission());
83
            $mform->addElement('editor', 'review_editor', get_string('review', 'tool_courserating'),
84
                ['rows' => 4], $options);
85
        } else {
86
            $mform->addElement('textarea', 'review', get_string('review', 'tool_courserating'),
87
                ['rows' => 4]);
88
            $mform->setType('review', PARAM_TEXT);
89
        }
90
    }
91
 
92
    /**
93
     * Form validation
94
     *
95
     * @param array $data
96
     * @param array $files
97
     * @return array
98
     */
99
    public function validation($data, $files) {
100
        $errors = [];
101
        if (empty($data['rating'])) {
102
            $errors['ratinggroup'] = get_string('required');
103
        }
104
        return $errors;
105
    }
106
 
107
    /**
108
     * Display the form
109
     *
110
     * @return void
111
     */
112
    public function display() {
113
        parent::display();
114
        global $PAGE;
115
        $PAGE->requires->js_call_amd('tool_courserating/rating', 'setupAddRatingForm',
116
        [$this->_form->getElement('ratinggroup')->getAttribute('id')]);
117
    }
118
 
119
    /**
120
     * Current context
121
     *
122
     * @return \context
123
     */
124
    protected function get_context_for_dynamic_submission(): \context {
125
        return \context_course::instance($this->get_course_id());
126
    }
127
 
128
    /**
129
     * Check access and throw exception if not allowed
130
     *
131
     * @return void
132
     * @throws moodle_exception
133
     */
134
    protected function check_access_for_dynamic_submission(): void {
135
        permission::require_can_add_rating($this->get_course_id());
136
    }
137
 
138
    /**
139
     * Process form submission
140
     */
141
    public function process_dynamic_submission() {
142
        $data = $this->get_data();
143
        api::set_rating($this->get_course_id(), $data);
144
    }
145
 
146
    /**
147
     * Load in existing data as form defaults
148
     */
149
    public function set_data_for_dynamic_submission(): void {
150
        $this->set_data(api::prepare_rating_for_form($this->get_course_id()));
151
    }
152
 
153
    /**
154
     * Fake URL for atto auto-save
155
     *
156
     * @return moodle_url
157
     */
158
    protected function get_page_url_for_dynamic_submission(): moodle_url {
159
        return new moodle_url('/course/view.php', ['id' => $this->get_course_id(), 'addrating' => 1]);
160
    }
161
}