forked from ishinegirl/moodle-mod_flashcards
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmanagewords.php
215 lines (177 loc) · 7.82 KB
/
managewords.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Displays the set-up phase.
*
* @package mod_wordcards
* @author Frédéric Massart - FMCorz.net
*/
require_once(__DIR__ . '/../../config.php');
use \mod_wordcards\utils;
use \mod_wordcards\constants;
$cmid = required_param('id', PARAM_INT);
$termid = optional_param('termid', null, PARAM_INT);
$action = optional_param('action', null, PARAM_ALPHA);
$termdeleteids = optional_param_array('termdeleteid', [], PARAM_INT);
$mod = mod_wordcards_module::get_by_cmid($cmid);
$course = $mod->get_course();
$cm = $mod->get_cm();
$modulecontext = context_module::instance($cm->id);
require_login($course, true, $cm);
$mod->require_manage();
$modid = $mod->get_id();
$pagetitle = format_string($mod->get_mod()->name, true, $course->id);
$pagetitle .= ': ' . get_string('managewords', 'mod_wordcards');
$baseurl = new moodle_url('/mod/wordcards/managewords.php', ['id' => $cmid]);
$formurl = new moodle_url($baseurl);
$term = null;
$PAGE->set_url($baseurl);
$PAGE->navbar->add($pagetitle, $PAGE->url);
$PAGE->set_heading(format_string($course->fullname, true, $course->id));
$PAGE->set_title($pagetitle);
//Get admin settings
$config = get_config(constants::M_COMPONENT);
if($config->enablesetuptab){
$PAGE->set_pagelayout('popup');
}else{
$PAGE->set_pagelayout('incourse');
}
$output = $PAGE->get_renderer('mod_wordcards');
if ($action == 'delete') {
confirm_sesskey();
$mod->delete_term($termid);
// Uncomment when migrating to 3.1.
// redirect($PAGE->url, get_string('termdeleted', 'mod_wordcards'));
redirect($PAGE->url);
} else if ($action == 'edit') {
// Adding those parameters ensures that we confirm that the term belongs to the right module after submission.
$formurl->param('action', 'edit');
$formurl->param('termid', 'termid');
$term = $DB->get_record(constants::M_TERMSTABLE, ['modid' => $modid, 'id' => $termid], '*', MUST_EXIST);
}else if($action =='export') {
utils::export_terms_to_csv($modid);
exit;
} else if ($action == 'bulkdelete') {
confirm_sesskey();
if (!empty($termdeleteids)) {
foreach ($termdeleteids as $termdeleteid) {
$mod->delete_term($termdeleteid);
}
}
redirect($baseurl);
}
$form = new mod_wordcards_form_term($formurl->out(false), ['termid' => $term ? $term->id : 0,'ttslanguage'=>$mod->get_mod()->ttslanguage]);
if (!$term) {
$term = new stdClass();
$term->id=null;
}
//prepare filemanager
$audiooptions= utils::fetch_filemanager_opts('audio');
$imageoptions= utils::fetch_filemanager_opts('image');
file_prepare_standard_filemanager($term, 'audio', $audiooptions, $modulecontext, constants::M_COMPONENT, 'audio', $term->id);
file_prepare_standard_filemanager($term, 'image', $imageoptions, $modulecontext, constants::M_COMPONENT, 'image', $term->id);
file_prepare_standard_filemanager($term, 'model_sentence_audio', $audiooptions, $modulecontext, constants::M_COMPONENT, 'model_sentence_audio', $term->id);
//set data to form
$form->set_data($term);
if ($data = $form->get_data()) {
//if this new add and collect data->id
$needsupdating = false;
if (empty($data->termid)) {
$data->modid = $modid;
$data->id = $DB->insert_record('wordcards_terms', $data);
//else set id to termid
}else{
$data->id = $data->termid;
$needsupdating = true;
}
//audio data
if(!empty( $data->audio_filemanager)){
$audiooptions = utils::fetch_filemanager_opts('audio');
//$data->audio_filemanager = $audioitemid;
$data = file_postupdate_standard_filemanager($data, 'audio', $audiooptions, $modulecontext, constants::M_COMPONENT, 'audio',
$data->id);
$needsupdating = true;
//in the case a user has deleted all files, we will still have the draftid in the audio column, we want to set it to 0
$fs = get_file_storage();
$areafiles = $fs->get_area_files($modulecontext->id,'mod_wordcards','audio',$data->id);
if(!$areafiles || count($areafiles)==0){
$data->audio='';
}elseif(count($areafiles)==1) {
$file = array_pop($areafiles);
if ($file->is_directory()) {
$data->audio='';
}
}
}
//model sentence audio data
if(!empty($data->model_sentence_audio_filemanager)){
$audiooptions = utils::fetch_filemanager_opts('audio');
//$data->audio_filemanager = $audioitemid;
$data = file_postupdate_standard_filemanager($data, 'model_sentence_audio', $audiooptions, $modulecontext, constants::M_COMPONENT, 'model_sentence_audio',
$data->id);
$needsupdating = true;
//in the case a user has deleted all files, we will still have the draftid in the audio column, we want to set it to 0
$fs = get_file_storage();
$areafiles = $fs->get_area_files($modulecontext->id,'mod_wordcards','model_sentence_audio',$data->id);
if(!$areafiles || count($areafiles)==0){
$data->model_sentence_audio='';
}elseif(count($areafiles)==1) {
$file = array_pop($areafiles);
if ($file->is_directory()) {
$data->model_sentence_audio='';
}
}
}
if(!empty($data->image_filemanager)){
$imageoptions = utils::fetch_filemanager_opts('image');
$data = file_postupdate_standard_filemanager($data, 'image', $imageoptions, $modulecontext, constants::M_COMPONENT, 'image',
$data->id);
$needsupdating = true;
//in the case a user has deleted all files, we will still have the draftid in the image column, we want to set it to ''
$fs = get_file_storage();
$areafiles = $fs->get_area_files($modulecontext->id,'mod_wordcards','image',$data->id);
if(!$areafiles || count($areafiles)==0){
$data->image='';
}elseif(count($areafiles)==1) {
$file = array_pop($areafiles);
if ($file->is_directory()) {
$data->image='';
}
}
}
if ($needsupdating) {
$DB->update_record('wordcards_terms', $data);
//also update our passagehash update flag
$DB->update_record('wordcards', array('id' => $modid, 'hashisold' => 1));
}
//finally redirect
// Uncomment when migrating to 3.1.
// redirect($PAGE->url, get_string('termsaved', 'mod_wordcards', $data->term));
redirect($PAGE->url);
}
echo $output->header();
echo $output->heading($pagetitle);
echo $output->navigation($mod, 'managewords');
echo $output->box(get_string('managewordsinstructions',constants::M_COMPONENT), 'generalbox', 'intro');
echo html_writer::start_tag('form', array('method' => 'POST', 'id' => 'bulkdeleteform'));
echo html_writer::link('#',get_string('addnewterm',constants::M_COMPONENT),
array('class'=>'btn btn-primary mod_wordcards_item_row_addlink','data-id'=>0,'data-type'=>"add"));
echo html_writer::empty_tag('input', ['type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()]);
$confirmation = new confirm_action(get_string('reallybulkdelete', 'mod_wordcards'));
$confirmationcontext = $confirmation->export_for_template($OUTPUT);
$confirmationcontext->id = 'deleteconfirmation';
echo $OUTPUT->render_from_template('core/actions', ['actions' => [$confirmationcontext]]);
echo html_writer::tag('button', get_string('bulkdelete', 'mod_wordcards'), [
'type' => 'submit',
'name' => 'action',
'value' => 'bulkdelete',
'id' => 'deleteconfirmation',
'class' => 'btn btn-primary ml-2 d-none',
]);
$table = new mod_wordcards_table_terms('tblterms', $mod);
$table->define_baseurl($PAGE->url);
$table->out(25, false);
echo html_writer::end_tag('form');
$props=array('contextid'=>$modulecontext->id);
$PAGE->requires->js_call_amd(constants::M_COMPONENT . '/managewordshelper', 'init', array($props));
$PAGE->requires->js_call_amd(constants::M_COMPONENT . '/bulkselect');
echo $output->footer();