Repository navigation
Expand file tree
/
Copy pathmod_form.php
More file actions
190 lines (162 loc) · 7.31 KB
/
Copy pathmod_form.php
File metadata and controls
190 lines (162 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Form structure for mod_journal
*
* @package mod_journal
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/course/moodleform_mod.php');
require_once($CFG->dirroot . '/mod/journal/lib.php');
/**
* The mod_journal_mod_form class.
*
* @package mod_journal
* @copyright 2022 Elearning Software SRL http://elearningsoftware.ro
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_journal_mod_form extends moodleform_mod {
/**
* Set up the form definition.
*/
public function definition() {
global $COURSE;
$mform = &$this->_form;
$mform->addElement('header', 'general', get_string('general', 'form'));
$mform->addElement('text', 'name', get_string('journalname', 'journal'), ['size' => '64']);
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required', null, 'client');
$this->standard_intro_elements(get_string('journalquestion', 'journal'));
$options = [];
$options[0] = get_string('alwaysopen', 'journal');
for ($i = 1; $i <= 13; $i++) {
$options[$i] = get_string('numdays', '', $i);
}
for ($i = 2; $i <= 16; $i++) {
$days = $i * 7;
$options[$days] = get_string('numweeks', '', $i);
}
$options[365] = get_string('numweeks', '', 52);
$mform->addElement('select', 'days', get_string('daysavailable', 'journal'), $options);
if ($COURSE->format === 'weeks') {
$mform->setDefault('days', '7');
} else {
$mform->setDefault('days', '0');
}
$mform->addElement('date_time_selector', 'timeopen', get_string('timeopen', 'journal'), ['optional' => true]);
$mform->addHelpButton('timeopen', 'timeopen', 'journal');
$mform->addElement('date_time_selector', 'timeclose', get_string('timeclose', 'journal'), ['optional' => true]);
// The days available are only used without opening and closing times.
$mform->hideIf('days', 'timeopen[enabled]', 'checked');
$mform->hideIf('days', 'timeclose[enabled]', 'checked');
$mform->addElement(
'select',
'remindbefore',
get_string('remindbefore', 'journal'),
journal_get_remind_before_options()
);
$mform->addHelpButton('remindbefore', 'remindbefore', 'journal');
$mform->setDefault('remindbefore', 0);
$mform->hideIf('remindbefore', 'timeclose[enabled]', 'notchecked');
$mform->addElement('text', 'minwords', get_string('minwords', 'journal'), ['size' => 6]);
$mform->setType('minwords', PARAM_INT);
$mform->setDefault('minwords', 0);
$mform->addHelpButton('minwords', 'minwords', 'journal');
$mform->addElement('text', 'maxwords', get_string('maxwords', 'journal'), ['size' => 6]);
$mform->setType('maxwords', PARAM_INT);
$mform->setDefault('maxwords', 0);
$mform->addHelpButton('maxwords', 'maxwords', 'journal');
// Notifications settings.
$config = get_config('journal');
$mform->addElement(
'select',
'notifyteachers',
get_string('notifyteachers', 'journal'),
journal_get_notify_teachers_options()
);
$mform->addHelpButton('notifyteachers', 'notifyteachers', 'journal');
$mform->setDefault(
'notifyteachers',
$config->notifyteachers_default ?? \mod_journal\manager::NOTIFY_NEEDSGRADING
);
$mform->addElement('checkbox', 'notifystudents', get_string('notifystudents', 'journal'));
$mform->addHelpButton('notifystudents', 'notifystudents', 'journal');
$mform->setDefault('notifystudents', isset($config->notifystudents_default) ? $config->notifystudents_default : 1);
$this->standard_grading_coursemodule_elements();
// Apply default grade from global settings.
// We only apply this for new instances (when $this->_instance is empty).
if (empty($this->_instance)) {
$defaultgrade = isset($config->defaultgrade) ? $config->defaultgrade : false;
// If the setting is missing, fallback to 100 (Standard Moodle default).
if ($defaultgrade === false) {
$defaultgrade = 100;
}
$mform->setDefault('grade', $defaultgrade);
}
$this->standard_coursemodule_elements();
$this->add_action_buttons();
}
/**
* Validates the opening and closing times and the limits of the number of words.
*
* @param array $data Submitted data
* @param array $files Submitted files
* @return array Errors by element name
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (!empty($data['timeopen']) && !empty($data['timeclose']) && $data['timeclose'] <= $data['timeopen']) {
$errors['timeclose'] = get_string('closebeforeopen', 'journal');
}
foreach (['minwords', 'maxwords'] as $field) {
if (isset($data[$field]) && $data[$field] < 0) {
$errors[$field] = get_string('negativewords', 'journal');
}
}
if (!isset($errors['maxwords']) && !empty($data['maxwords']) && $data['maxwords'] < $data['minwords']) {
$errors['maxwords'] = get_string('maxbelowminwords', 'journal');
}
return $errors;
}
/**
* Returns whether the custom completion rules are enabled.
*
* @param array $data form data
* @return bool
*/
public function completion_rule_enabled($data): bool {
// Compatibility check: get_suffix only exists in Moodle 4.3+.
$suffix = method_exists($this, 'get_suffix') ? $this->get_suffix() : '';
return (!empty($data['completion_create_entry' . $suffix]));
}
/**
* Adds the custom completion rules for mod_journal
*
* @return array
*/
public function add_completion_rules(): array {
$mform = $this->_form;
// Compatibility check: get_suffix only exists in Moodle 4.3+.
$suffix = method_exists($this, 'get_suffix') ? $this->get_suffix() : '';
$fieldname = 'completion_create_entry' . $suffix;
$mform->addElement('advcheckbox', $fieldname, get_string('completiondetail:completion_create_entry', 'journal'));
$mform->setType($fieldname, PARAM_INT);
$mform->hideIf($fieldname, 'completion', 'neq', COMPLETION_TRACKING_AUTOMATIC);
return ([$fieldname]);
}
}