2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Provides {@link tool_policy\form\policydoc} class.
20 * @package tool_policy
22 * @copyright 2018 David Mudrák <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace tool_policy\form;
32 use tool_policy\policy_version;
34 defined('MOODLE_INTERNAL') || die();
37 * Defines the form for editing a policy document version.
39 * @copyright 2018 David Mudrak <david@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class policydoc extends moodleform {
45 * Defines the form fields.
47 public function definition() {
49 $mform = $this->_form;
50 $formdata = $this->_customdata['formdata'];
52 $mform->addElement('text', 'name', get_string('policydocname', 'tool_policy'), ['maxlength' => 1333]);
53 $mform->settype('name', PARAM_TEXT);
54 $mform->addRule('name', null, 'required', null, 'client');
55 $mform->addRule('name', get_string('maximumchars', '', 1333), 'maxlength', 1333, 'client');
58 foreach ([policy_version::TYPE_SITE,
59 policy_version::TYPE_PRIVACY,
60 policy_version::TYPE_THIRD_PARTY,
61 policy_version::TYPE_OTHER] as $type) {
62 $options[$type] = get_string('policydoctype'.$type, 'tool_policy');
64 $mform->addElement('select', 'type', get_string('policydoctype', 'tool_policy'), $options);
67 foreach ([policy_version::AUDIENCE_ALL,
68 policy_version::AUDIENCE_LOGGEDIN,
69 policy_version::AUDIENCE_GUESTS] as $audience) {
70 $options[$audience] = get_string('policydocaudience'.$audience, 'tool_policy');
72 $mform->addElement('select', 'audience', get_string('policydocaudience', 'tool_policy'), $options);
74 if (empty($formdata->id)) {
75 $default = userdate(time(), get_string('strftimedate', 'core_langconfig'));
77 $default = userdate($formdata->timecreated, get_string('strftimedate', 'core_langconfig'));
79 $mform->addElement('text', 'revision', get_string('policydocrevision', 'tool_policy'),
80 ['maxlength' => 1333, 'placeholder' => $default]);
81 $mform->settype('revision', PARAM_TEXT);
82 $mform->addRule('revision', get_string('maximumchars', '', 1333), 'maxlength', 1333, 'client');
84 $mform->addElement('editor', 'summary_editor', get_string('policydocsummary', 'tool_policy'), ['rows' => 7],
85 api::policy_summary_field_options());
86 $mform->addRule('summary_editor', null, 'required', null, 'client');
88 $mform->addElement('editor', 'content_editor', get_string('policydoccontent', 'tool_policy'), null,
89 api::policy_content_field_options());
90 $mform->addRule('content_editor', null, 'required', null, 'client');
92 if (!$formdata->id || $formdata->status == policy_version::STATUS_DRAFT) {
93 // Creating a new version or editing a draft/archived version.
94 $mform->addElement('hidden', 'minorchange');
95 $mform->setType('minorchange', PARAM_INT);
98 $mform->createElement('radio', 'status', '', get_string('status'.policy_version::STATUS_ACTIVE, 'tool_policy'),
99 policy_version::STATUS_ACTIVE),
100 $mform->createElement('radio', 'status', '', get_string('status'.policy_version::STATUS_DRAFT, 'tool_policy'),
101 policy_version::STATUS_DRAFT),
102 $mform->createElement('static', 'statusinfo', '', html_writer::div(get_string('statusinfo', 'tool_policy'),
103 'muted text-muted')),
105 $mform->addGroup($statusgrp, null, get_string('status', 'tool_policy'), ['<br>'], false);
108 // Editing an active version.
109 $mform->addElement('hidden', 'status', policy_version::STATUS_ACTIVE);
110 $mform->setType('status', PARAM_INT);
113 $mform->createElement('checkbox', 'minorchange', '', get_string('minorchange', 'tool_policy')),
114 $mform->createElement('static', 'minorchangeinfo', '',
115 html_writer::div(get_string('minorchangeinfo', 'tool_policy'), 'muted text-muted')),
117 $mform->addGroup($statusgrp, null, get_string('status', 'tool_policy'), ['<br>'], false);
120 // Add "Save" button and, optionally, "Save as draft".
122 $buttonarray[] = $mform->createElement('submit', 'save', get_string('save', 'tool_policy'));
123 if ($formdata->id && $formdata->status == policy_version::STATUS_ACTIVE) {
124 $buttonarray[] = $mform->createElement('submit', 'saveasdraft', get_string('saveasdraft', 'tool_policy'));
126 $buttonarray[] = $mform->createElement('cancel');
127 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
128 $mform->closeHeaderBefore('buttonar');
130 $this->set_data($formdata);
136 * @param array $data array of ("fieldname"=>value) of submitted data
137 * @param array $files array of uploaded files "element_name"=>tmp_file_path
138 * @return array of "element_name"=>"error_description" if there are errors,
139 * or an empty array if everything is OK (true allowed for backwards compatibility too).
141 public function validation($data, $files) {
142 $errors = parent::validation($data, $files);
143 if (!empty($data['minorchange']) && !empty($data['saveasdraft'])) {
144 // If minorchange is checked and "save as draft" is pressed - return error.
145 $errors['minorchange'] = get_string('errorsaveasdraft', 'tool_policy');
151 * Return submitted data if properly submitted or returns NULL if validation fails or
152 * if there is no submitted data.
154 * @return object submitted data; NULL if not valid or not submitted or cancelled
156 public function get_data() {
157 if ($data = parent::get_data()) {
158 if (!empty($data->saveasdraft)) {
159 $data->status = policy_version::STATUS_DRAFT;