3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->libdir
.'/formslib.php');
9 class question_context_move_form
extends moodleform
{
11 function definition() {
13 $mform =& $this->_form
;
15 //--------------------------------------------------------------------------------
16 $urls = $this->_customdata
['urls'];
17 $fromareaname = $this->_customdata
['fromareaname'];
18 $toareaname = $this->_customdata
['toareaname'];
19 $fileoptions = array(QUESTION_FILEDONOTHING
=>get_string('donothing', 'question'),
20 QUESTION_FILECOPY
=>get_string('copy', 'question', $fromareaname),
21 QUESTION_FILEMOVE
=>get_string('move', 'question', $fromareaname),
22 QUESTION_FILEMOVELINKSONLY
=>get_string('movelinksonly', 'question', $fromareaname));
23 $brokenfileoptions = array(QUESTION_FILEDONOTHING
=>get_string('donothing', 'question'),
24 QUESTION_FILEMOVELINKSONLY
=>get_string('movelinksonly', 'question', $fromareaname));
25 $brokenurls = $this->_customdata
['brokenurls'];
28 $mform->addElement('header','general', get_string('filestomove', 'question', $toareaname));
31 foreach (array_keys($urls) as $url){
32 $iconname = mimeinfo('icon', $url);
33 $icontype = mimeinfo('type', $url);
34 $img = "<img src=\"$CFG->pixpath/f/$iconname\" class=\"icon\" alt=\"$icontype\" />";
35 if (in_array($url, $brokenurls)){
36 $mform->addElement('select', "urls[$i]", $img.$url, $brokenfileoptions);
38 $mform->addElement('select', "urls[$i]", $img.$url, $fileoptions);
44 if (count($brokenurls)){
45 $mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
47 //--------------------------------------------------------------------------------
48 $this->add_action_buttons(true, get_string('movecategory', 'question'));
52 function validation($data, $files) {
53 $errors = parent
::validation($data, $files);
54 $tocoursefilesid = $this->_customdata
['tocoursefilesid'];
55 $fromcoursefilesid = $this->_customdata
['fromcoursefilesid'];
56 if (isset($data['urls']) && (count($data['urls']))){
57 foreach ($data['urls'] as $key => $urlaction){
59 case QUESTION_FILEMOVE
:
60 if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE
, $fromcoursefilesid))){
61 $errors["urls[$key]"] = get_string('filecantmovefrom', 'question');
63 //no break; COPY check is also applied to MOVE action
64 case QUESTION_FILECOPY
:
65 if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE
, $tocoursefilesid))){
66 $errors["urls[$key]"] = get_string('filecantmoveto', 'question');
69 case QUESTION_FILEMOVELINKSONLY
:
70 case QUESTION_FILEDONOTHING
:
75 //check that there hasn't been any changes in files between time form was displayed
76 //and now when it has been submitted.
77 if (isset($data['urls']) &&
79 != count($this->_customdata
['urls']))){
80 $errors['urls[0]'] = get_string('errorfileschanged', 'question');
85 * We want these errors to show up on first loading the form which is not the default for
86 * validation method which is not run until submission.
88 function definition_after_data(){
89 $mform = $this->_form
;
90 $brokenurls = $this->_customdata
['brokenurls'];
91 if (count($brokenurls)){
92 $ignoreval = $mform->getElementValue('ignorebroken');
94 $urls = $this->_customdata
['urls'];
96 foreach (array_keys($urls) as $url){
97 if (in_array($url, $brokenurls)){
98 $mform->setElementError("urls[$i]", get_string('broken', 'question'));
100 $mform->setElementError("urls[$i]", '');