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 {@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 |
$mform->addElement('selectyesno', 'agreementstyle', get_string('policypriorityagreement', 'tool_policy'));
|
|
|
93 |
|
|
|
94 |
$mform->addElement('selectyesno', 'optional', get_string('policydocoptional', 'tool_policy'));
|
|
|
95 |
|
|
|
96 |
if (!$formdata->id || $formdata->status == policy_version::STATUS_DRAFT) {
|
|
|
97 |
// Creating a new version or editing a draft/archived version.
|
|
|
98 |
$mform->addElement('hidden', 'minorchange');
|
|
|
99 |
$mform->setType('minorchange', PARAM_INT);
|
|
|
100 |
|
|
|
101 |
$statusgrp = [
|
|
|
102 |
$mform->createElement('radio', 'status', '', get_string('status'.policy_version::STATUS_ACTIVE, 'tool_policy'),
|
|
|
103 |
policy_version::STATUS_ACTIVE),
|
|
|
104 |
$mform->createElement('radio', 'status', '', get_string('status'.policy_version::STATUS_DRAFT, 'tool_policy'),
|
|
|
105 |
policy_version::STATUS_DRAFT),
|
|
|
106 |
$mform->createElement('static', 'statusinfo', '', html_writer::div(get_string('statusinfo', 'tool_policy'),
|
|
|
107 |
'muted text-muted')),
|
|
|
108 |
];
|
|
|
109 |
$mform->addGroup($statusgrp, null, get_string('status', 'tool_policy'), ['<br>'], false);
|
|
|
110 |
|
|
|
111 |
} else {
|
|
|
112 |
// Editing an active version.
|
|
|
113 |
$mform->addElement('hidden', 'status', policy_version::STATUS_ACTIVE);
|
|
|
114 |
$mform->setType('status', PARAM_INT);
|
|
|
115 |
|
|
|
116 |
$statusgrp = [
|
|
|
117 |
$mform->createElement('checkbox', 'minorchange', '', get_string('minorchange', 'tool_policy')),
|
|
|
118 |
$mform->createElement('static', 'minorchangeinfo', '',
|
|
|
119 |
html_writer::div(get_string('minorchangeinfo', 'tool_policy'), 'muted text-muted')),
|
|
|
120 |
];
|
|
|
121 |
$mform->addGroup($statusgrp, null, get_string('status', 'tool_policy'), ['<br>'], false);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// Add "Save" button and, optionally, "Save as draft".
|
|
|
125 |
$buttonarray = [];
|
|
|
126 |
$buttonarray[] = $mform->createElement('submit', 'save', get_string('save', 'tool_policy'));
|
|
|
127 |
if ($formdata->id && $formdata->status == policy_version::STATUS_ACTIVE) {
|
|
|
128 |
$buttonarray[] = $mform->createElement('submit', 'saveasdraft', get_string('saveasdraft', 'tool_policy'));
|
|
|
129 |
}
|
|
|
130 |
$buttonarray[] = $mform->createElement('cancel');
|
|
|
131 |
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
|
|
|
132 |
$mform->closeHeaderBefore('buttonar');
|
|
|
133 |
|
|
|
134 |
$this->set_data($formdata);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Form validation
|
|
|
139 |
*
|
|
|
140 |
* @param array $data array of ("fieldname"=>value) of submitted data
|
|
|
141 |
* @param array $files array of uploaded files "element_name"=>tmp_file_path
|
|
|
142 |
* @return array of "element_name"=>"error_description" if there are errors,
|
|
|
143 |
* or an empty array if everything is OK (true allowed for backwards compatibility too).
|
|
|
144 |
*/
|
|
|
145 |
public function validation($data, $files) {
|
|
|
146 |
$errors = parent::validation($data, $files);
|
|
|
147 |
if (!empty($data['minorchange']) && !empty($data['saveasdraft'])) {
|
|
|
148 |
// If minorchange is checked and "save as draft" is pressed - return error.
|
|
|
149 |
$errors['minorchange'] = get_string('errorsaveasdraft', 'tool_policy');
|
|
|
150 |
}
|
|
|
151 |
return $errors;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Return submitted data if properly submitted or returns NULL if validation fails or
|
|
|
156 |
* if there is no submitted data.
|
|
|
157 |
*
|
|
|
158 |
* @return object submitted data; NULL if not valid or not submitted or cancelled
|
|
|
159 |
*/
|
|
|
160 |
public function get_data() {
|
|
|
161 |
if ($data = parent::get_data()) {
|
|
|
162 |
if (!empty($data->saveasdraft)) {
|
|
|
163 |
$data->status = policy_version::STATUS_DRAFT;
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
return $data;
|
|
|
167 |
}
|
|
|
168 |
}
|