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 context;
20
use core_form\dynamic_form;
21
use moodle_url;
22
use tool_courserating\api;
23
use tool_courserating\local\models\rating;
24
use tool_courserating\permission;
25
 
26
/**
27
 * Form for deleting a rating (by manager)
28
 *
29
 * @package     tool_courserating
30
 * @copyright   2022 Marina Glancy <marina.glancy@gmail.com>
31
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class deleterating extends dynamic_form {
34
    /** @var rating */
35
    protected $rating;
36
 
37
    /**
38
     * Id of the current rating
39
     *
40
     * @return int
41
     */
42
    protected function get_rating_id(): int {
43
        $id = $this->optional_param('ratingid', 0, PARAM_INT);
44
        if ($id <= 0) {
45
            throw new \moodle_exception('missingparam', '', '', 'ratingid');
46
        }
47
        return $id;
48
    }
49
 
50
    /**
51
     * Current rating
52
     *
53
     * @return rating
54
     */
55
    protected function get_rating(): rating {
56
        if (!$this->rating) {
57
            $this->rating = new rating($this->get_rating_id());
58
        }
59
        return $this->rating;
60
    }
61
 
62
    /**
63
     * Current context
64
     *
65
     * @return \context
66
     */
67
    protected function get_context_for_dynamic_submission(): context {
68
        return \context_course::instance($this->get_rating()->get('courseid'));
69
    }
70
 
71
    /**
72
     * Check access and throw exception if not allowed
73
     *
74
     * @return void
75
     * @throws \moodle_exception
76
     */
77
    protected function check_access_for_dynamic_submission(): void {
78
        permission::require_can_delete_rating($this->get_rating_id(), $this->get_rating()->get('courseid'));
79
    }
80
 
81
    /**
82
     * Process submission
83
     *
84
     * @return mixed|void
85
     */
86
    public function process_dynamic_submission() {
87
        $rv = ['ratingid' => $this->get_rating_id(), 'courseid' => $this->get_rating()->get('courseid')];
88
        api::delete_rating($this->get_rating_id(), $this->get_data()->reason);
89
        return $rv;
90
    }
91
 
92
    /**
93
     * Load in existing data as form defaults
94
     */
95
    public function set_data_for_dynamic_submission(): void {
96
        $this->set_data(['ratingid' => $this->get_rating_id()]);
97
    }
98
 
99
    /**
100
     * Fake URL for atto auto-save
101
     *
102
     * @return moodle_url
103
     */
104
    protected function get_page_url_for_dynamic_submission(): moodle_url {
105
        return new moodle_url('/course/view.php',
106
            ['id' => $this->get_rating()->get('courseid'), 'deleterating' => $this->get_rating_id()]);
107
    }
108
 
109
    /**
110
     * Form definition
111
     */
112
    protected function definition() {
113
        $mform = $this->_form;
114
 
115
        $mform->addElement('hidden', 'ratingid');
116
        $mform->setType('ratingid', PARAM_INT);
117
 
118
        $mform->addElement('textarea', 'reason', get_string('deletereason', 'tool_courserating'));
119
        $mform->setType('reason', PARAM_TEXT);
120
        $mform->addRule('reason', get_string('required'), 'required', null, 'client');
121
    }
122
}