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 |
/**
|
|
|
18 |
* Provides {@see \core_contentbank\form\edit_content} class.
|
|
|
19 |
*
|
|
|
20 |
* @package core_contentbank
|
|
|
21 |
* @copyright 2020 Victor Deniz <victor@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_contentbank\form;
|
|
|
26 |
|
|
|
27 |
use moodleform;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
require_once($CFG->libdir.'/formslib.php');
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Defines the form for editing a content.
|
|
|
35 |
*
|
|
|
36 |
* @package core_contentbank
|
|
|
37 |
* @copyright 2020 Victor Deniz <victor@moodle.com>
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
*/
|
|
|
40 |
abstract class edit_content extends moodleform {
|
|
|
41 |
|
|
|
42 |
/** @var int Context the content belongs to. */
|
|
|
43 |
protected $contextid;
|
|
|
44 |
|
|
|
45 |
/** @var string Content type plugin name. */
|
|
|
46 |
protected $plugin;
|
|
|
47 |
|
|
|
48 |
/** @var int Content id in the content bank. */
|
|
|
49 |
protected $id;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Constructor.
|
|
|
53 |
*
|
|
|
54 |
* @param string $action The action attribute for the form.
|
|
|
55 |
* @param array $customdata Data to set during instance creation.
|
|
|
56 |
* @param string $method Form method.
|
|
|
57 |
*/
|
1441 |
ariadna |
58 |
public function __construct(?string $action = null, ?array $customdata = null, string $method = 'post') {
|
1 |
efrain |
59 |
$this->contextid = $customdata['contextid'];
|
|
|
60 |
$this->plugin = $customdata['plugin'];
|
1441 |
ariadna |
61 |
$this->id = $customdata['id'] ?? 0;
|
|
|
62 |
parent::__construct($action, $customdata, $method);
|
1 |
efrain |
63 |
|
|
|
64 |
$mform =& $this->_form;
|
|
|
65 |
$mform->addElement('hidden', 'contextid', $this->contextid);
|
|
|
66 |
$this->_form->setType('contextid', PARAM_INT);
|
|
|
67 |
|
|
|
68 |
$mform->addElement('hidden', 'plugin', $this->plugin);
|
|
|
69 |
$this->_form->setType('plugin', PARAM_PLUGIN);
|
|
|
70 |
|
|
|
71 |
$mform->addElement('hidden', 'id', $this->id);
|
|
|
72 |
$this->_form->setType('id', PARAM_INT);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
1441 |
ariadna |
76 |
* Add elements to form
|
|
|
77 |
*/
|
|
|
78 |
protected function definition() {
|
|
|
79 |
global $DB;
|
|
|
80 |
// Add custom fields to the form.
|
|
|
81 |
$content = $DB->get_record('contentbank_content', ['id' => $this->id]);
|
|
|
82 |
$handler = \core_contentbank\customfield\content_handler::create();
|
|
|
83 |
$handler->instance_form_definition($this->_form, $this->id);
|
|
|
84 |
if ($content) {
|
|
|
85 |
$handler->instance_form_before_set_data($content);
|
|
|
86 |
}
|
|
|
87 |
$this->set_data($content);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
1 |
efrain |
91 |
* Overrides formslib's add_action_buttons() method.
|
|
|
92 |
*
|
|
|
93 |
*
|
|
|
94 |
* @param bool $cancel
|
|
|
95 |
* @param string|null $submitlabel
|
|
|
96 |
*
|
|
|
97 |
* @return void
|
|
|
98 |
*/
|
|
|
99 |
public function add_action_buttons($cancel = true, $submitlabel = null): void {
|
|
|
100 |
if (is_null($submitlabel)) {
|
|
|
101 |
$submitlabel = get_string('save');
|
|
|
102 |
}
|
|
|
103 |
parent::add_action_buttons($cancel, $submitlabel);
|
|
|
104 |
}
|
|
|
105 |
}
|