-
Notifications
You must be signed in to change notification settings - Fork 4
/
contextmove_form.php
107 lines (96 loc) · 4.69 KB
/
contextmove_form.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
<?php
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir.'/formslib.php');
class question_context_move_form extends moodleform {
function definition() {
global $CFG, $OUTPUT;
$mform =& $this->_form;
//--------------------------------------------------------------------------------
$urls = $this->_customdata['urls'];
$fromareaname = $this->_customdata['fromareaname'];
$toareaname = $this->_customdata['toareaname'];
$fileoptions = array(QUESTION_FILEDONOTHING=>get_string('donothing', 'question'),
QUESTION_FILECOPY=>get_string('copy', 'question', $fromareaname),
QUESTION_FILEMOVE=>get_string('move', 'question', $fromareaname),
QUESTION_FILEMOVELINKSONLY=>get_string('movelinksonly', 'question', $fromareaname));
$brokenfileoptions = array(QUESTION_FILEDONOTHING=>get_string('donothing', 'question'),
QUESTION_FILEMOVELINKSONLY=>get_string('movelinksonly', 'question', $fromareaname));
$brokenurls = $this->_customdata['brokenurls'];
if (count($urls)){
$mform->addElement('header','general', get_string('filestomove', 'question', $toareaname));
$i = 0;
foreach (array_keys($urls) as $url){
$icontype = mimeinfo('type', $url);
$img = "<img src=\"" . $OUTPUT->pix_url(file_extension_icon($url)) . "\" class=\"icon\" alt=\"$icontype\" />";
if (in_array($url, $brokenurls)){
$mform->addElement('select', "urls[$i]", $img.$url, $brokenfileoptions);
} else {
$mform->addElement('select', "urls[$i]", $img.$url, $fileoptions);
}
$i++;
}
}
if (count($brokenurls)){
$mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
}
//--------------------------------------------------------------------------------
$this->add_action_buttons(true, get_string('movecategory', 'question'));
}
function validation($data, $files) {
$errors = parent::validation($data, $files);
$tocoursefilesid = $this->_customdata['tocoursefilesid'];
$fromcoursefilesid = $this->_customdata['fromcoursefilesid'];
if (isset($data['urls']) && (count($data['urls']))){
foreach ($data['urls'] as $key => $urlaction){
switch ($urlaction){
case QUESTION_FILEMOVE :
if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $fromcoursefilesid))){
$errors["urls[$key]"] = get_string('filecantmovefrom', 'question');
}
//no break; COPY check is also applied to MOVE action
case QUESTION_FILECOPY :
if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $tocoursefilesid))){
$errors["urls[$key]"] = get_string('filecantmoveto', 'question');
}
break;
case QUESTION_FILEMOVELINKSONLY :
case QUESTION_FILEDONOTHING :
break;
}
}
}
//check that there hasn't been any changes in files between time form was displayed
//and now when it has been submitted.
if (isset($data['urls']) &&
(count($data['urls'])
!= count($this->_customdata['urls']))){
$errors['urls[0]'] = get_string('errorfileschanged', 'question');
}
return $errors;
}
/*
* We want these errors to show up on first loading the form which is not the default for
* validation method which is not run until submission.
*/
function definition_after_data(){
$mform = $this->_form;
$brokenurls = $this->_customdata['brokenurls'];
if (count($brokenurls)){
$ignoreval = $mform->getElementValue('ignorebroken');
if (!$ignoreval){
$urls = $this->_customdata['urls'];
$i = 0;
foreach (array_keys($urls) as $url){
if (in_array($url, $brokenurls)){
$mform->setElementError("urls[$i]", get_string('broken', 'question'));
} else {
$mform->setElementError("urls[$i]", '');
}
$i++;
}
}
}
}
}