forked from bostelm/moodle-mod_scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappointmentforms.php
158 lines (136 loc) · 6.22 KB
/
appointmentforms.php
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
<?php
/**
* Appointment-related forms of the scheduler module
* (using Moodle formslib)
*
* @package mod_scheduler
* @copyright 2016 Henning Bostelmann and others (see README.txt)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
/**
* Form to edit one appointment
*
* @package mod_scheduler
* @copyright 2016 Henning Bostelmann and others (see README.txt)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scheduler_editappointment_form extends moodleform {
/**
* @var scheduler_appointment the appointment being edited
*/
protected $appointment;
/**
* @var bool whether to distribute grade to all group members
*/
protected $distribute;
/**
* @var whether the teacher can edit grades
*/
protected $editgrade;
/**
* @var array options for notes fields
*/
public $noteoptions;
/**
* Create a new edit appointment form
*
* @param scheduler_appointment $appointment the appointment to edit
* @param mixed $action the action attribute for the form
* @param bool $editgrade whether the grade can be edited
* @param bool $distribute whether to distribute grades to all group members
*/
public function __construct(scheduler_appointment $appointment, $action, $editgrade, $distribute) {
$this->appointment = $appointment;
$this->distribute = $distribute;
$this->editgrade = $editgrade;
$this->noteoptions = array('trusttext' => true, 'maxfiles' => -1, 'maxbytes' => 0,
'context' => $appointment->get_scheduler()->get_context(),
'subdirs' => false, 'collapsed' => true);
parent::__construct($action, null);
}
protected function definition() {
global $output;
$mform = $this->_form;
$scheduler = $this->appointment->get_scheduler();
// Seen tickbox.
$mform->addElement('checkbox', 'attended', get_string('attended', 'scheduler'));
// Grade.
if ($scheduler->scale != 0) {
if ($this->editgrade) {
$gradechoices = $output->grading_choices($scheduler);
$mform->addElement('select', 'grade', get_string('grade', 'scheduler'), $gradechoices);
} else {
$gradetext = $output->format_grade($scheduler, $this->appointment->grade);
$mform->addElement('static', 'gradedisplay', get_string('grade', 'scheduler'), $gradetext);
}
}
// Appointment notes (visible to teacher and/or student).
if ($scheduler->uses_appointmentnotes()) {
$mform->addElement('editor', 'appointmentnote_editor', get_string('appointmentnote', 'scheduler'),
array('rows' => 3, 'columns' => 60), $this->noteoptions);
$mform->setType('appointmentnote', PARAM_RAW); // Must be PARAM_RAW for rich text editor content.
}
if ($scheduler->uses_teachernotes()) {
$mform->addElement('editor', 'teachernote_editor', get_string('teachernote', 'scheduler'),
array('rows' => 3, 'columns' => 60), $this->noteoptions);
$mform->setType('teachernote', PARAM_RAW); // Must be PARAM_RAW for rich text editor content.
}
if ($this->distribute && ($scheduler->uses_appointmentnotes() || $scheduler->uses_teachernotes() || $this->editgrade) ) {
$mform->addElement('checkbox', 'distribute', get_string('distributetoslot', 'scheduler'));
$mform->setDefault('distribute', false);
}
$this->add_action_buttons();
}
public function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
/**
* Prepare form data from an appointment record
*
* @param scheduler_appointment $appointment appointment to edit
* @return stdClass form data
*/
public function prepare_appointment_data(scheduler_appointment $appointment) {
$newdata = clone($appointment->get_data());
$context = $this->appointment->get_scheduler()->get_context();
$newdata = file_prepare_standard_editor($newdata, 'appointmentnote', $this->noteoptions, $context,
'mod_scheduler', 'appointmentnote', $this->appointment->id);
$newdata = file_prepare_standard_editor($newdata, 'teachernote', $this->noteoptions, $context,
'mod_scheduler', 'teachernote', $this->appointment->id);
return $newdata;
}
/**
* Save form data into appointment record
*
* @param stdClass $formdata data extracted from form
* @param scheduler_appointment $appointment appointment to update
*/
public function save_appointment_data(stdClass $formdata, scheduler_appointment $appointment) {
$scheduler = $appointment->get_scheduler();
$cid = $scheduler->context->id;
$appointment->set_data($formdata);
$appointment->attended = isset($formdata->attended);
if ($scheduler->uses_appointmentnotes() && isset($formdata->appointmentnote_editor)) {
$editor = $formdata->appointmentnote_editor;
$appointment->appointmentnote = file_save_draft_area_files($editor['itemid'], $cid,
'mod_scheduler', 'appointmentnote', $appointment->id,
$this->noteoptions, $editor['text']);
$appointment->appointmentnoteformat = $editor['format'];
}
if ($scheduler->uses_teachernotes() && isset($formdata->teachernote_editor)) {
$editor = $formdata->teachernote_editor;
$appointment->teachernote = file_save_draft_area_files($editor['itemid'], $cid,
'mod_scheduler', 'teachernote', $appointment->id,
$this->noteoptions, $editor['text']);
$appointment->teachernoteformat = $editor['format'];
}
$appointment->save();
if (isset($formdata->distribute)) {
$slot = $appointment->get_slot();
$slot->distribute_appointment_data($appointment);
}
}
}