Commit | Line | Data |
---|---|---|
3d9571d5 DM |
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 {@link tool_policy\form\policydoc} class. | |
19 | * | |
20 | * @package tool_policy | |
21 | * @category output | |
22 | * @copyright 2018 David Mudrák <david@moodle.com> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | namespace tool_policy\form; | |
27 | ||
28 | use context_system; | |
29 | use html_writer; | |
30 | use moodleform; | |
31 | use tool_policy\api; | |
32 | use tool_policy\policy_version; | |
33 | ||
34 | defined('MOODLE_INTERNAL') || die(); | |
35 | ||
36 | /** | |
37 | * Defines the form for editing a policy document version. | |
38 | * | |
39 | * @copyright 2018 David Mudrak <david@moodle.com> | |
40 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
41 | */ | |
42 | class policydoc extends moodleform { | |
43 | ||
44 | /** | |
45 | * Defines the form fields. | |
46 | */ | |
47 | public function definition() { | |
48 | ||
49 | $mform = $this->_form; | |
50 | $formdata = $this->_customdata['formdata']; | |
51 | ||
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'); | |
56 | ||
57 | $options = []; | |
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'); | |
63 | } | |
64 | $mform->addElement('select', 'type', get_string('policydoctype', 'tool_policy'), $options); | |
65 | ||
66 | $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'); | |
71 | } | |
72 | $mform->addElement('select', 'audience', get_string('policydocaudience', 'tool_policy'), $options); | |
73 | ||
74 | if (empty($formdata->id)) { | |
75 | $default = userdate(time(), get_string('strftimedate', 'core_langconfig')); | |
76 | } else { | |
77 | $default = userdate($formdata->timecreated, get_string('strftimedate', 'core_langconfig')); | |
78 | } | |
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'); | |
83 | ||
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'); | |
87 | ||
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'); | |
91 | ||
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); | |
96 | ||
97 | $statusgrp = [ | |
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')), | |
104 | ]; | |
105 | $mform->addGroup($statusgrp, null, get_string('status', 'tool_policy'), ['<br>'], false); | |
106 | ||
107 | } else { | |
108 | // Editing an active version. | |
109 | $mform->addElement('hidden', 'status', policy_version::STATUS_ACTIVE); | |
110 | $mform->setType('status', PARAM_INT); | |
111 | ||
112 | $statusgrp = [ | |
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')), | |
116 | ]; | |
117 | $mform->addGroup($statusgrp, null, get_string('status', 'tool_policy'), ['<br>'], false); | |
118 | } | |
119 | ||
120 | // Add "Save" button and, optionally, "Save as draft". | |
121 | $buttonarray = []; | |
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')); | |
125 | } | |
126 | $buttonarray[] = $mform->createElement('cancel'); | |
127 | $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); | |
128 | $mform->closeHeaderBefore('buttonar'); | |
129 | ||
130 | $this->set_data($formdata); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Form validation | |
135 | * | |
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). | |
140 | */ | |
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'); | |
146 | } | |
147 | return $errors; | |
148 | } | |
149 | ||
150 | /** | |
151 | * Return submitted data if properly submitted or returns NULL if validation fails or | |
152 | * if there is no submitted data. | |
153 | * | |
154 | * @return object submitted data; NULL if not valid or not submitted or cancelled | |
155 | */ | |
156 | public function get_data() { | |
157 | if ($data = parent::get_data()) { | |
158 | if (!empty($data->saveasdraft)) { | |
159 | $data->status = policy_version::STATUS_DRAFT; | |
160 | } | |
161 | } | |
162 | return $data; | |
163 | } | |
164 | } |