MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / question / contextmove_form.php
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
5 }
7 require_once($CFG->libdir.'/formslib.php');
9 class question_context_move_form extends moodleform {
11     function definition() {
12         global $CFG, $OUTPUT;
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'];
26         if (count($urls)){
28             $mform->addElement('header','general', get_string('filestomove', 'question', $toareaname));
30             $i = 0;
31             foreach (array_keys($urls) as $url){
32                 $icontype = mimeinfo('type', $url);
33                 $img = "<img src=\"" . $OUTPUT->pix_url(file_extension_icon($url)) . "\"  class=\"icon\" alt=\"$icontype\" />";
34                 if (in_array($url, $brokenurls)){
35                     $mform->addElement('select', "urls[$i]", $img.$url, $brokenfileoptions);
36                 } else {
37                     $mform->addElement('select', "urls[$i]", $img.$url, $fileoptions);
38                 }
39                 $i++;
40             }
42         }
43         if (count($brokenurls)){
44             $mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
45         }
46         //--------------------------------------------------------------------------------
47         $this->add_action_buttons(true, get_string('movecategory', 'question'));
49     }
51     function validation($data, $files) {
52         $errors = parent::validation($data, $files);
53         $tocoursefilesid = $this->_customdata['tocoursefilesid'];
54         $fromcoursefilesid = $this->_customdata['fromcoursefilesid'];
55         if (isset($data['urls'])  && (count($data['urls']))){
56             foreach ($data['urls'] as $key => $urlaction){
57                 switch ($urlaction){
58                     case QUESTION_FILEMOVE :
59                         if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $fromcoursefilesid))){
60                             $errors["urls[$key]"] = get_string('filecantmovefrom', 'question');
61                         }
62                         //no break; COPY check is also applied to MOVE action
63                     case QUESTION_FILECOPY :
64                         if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $tocoursefilesid))){
65                             $errors["urls[$key]"] = get_string('filecantmoveto', 'question');
66                         }
67                         break;
68                     case QUESTION_FILEMOVELINKSONLY :
69                     case  QUESTION_FILEDONOTHING :
70                         break;
71                 }
72             }
73         }
74         //check that there hasn't been any changes in files between time form was displayed
75         //and now when it has been submitted.
76         if (isset($data['urls'])  &&
77             (count($data['urls'])
78                 != count($this->_customdata['urls']))){
79             $errors['urls[0]'] = get_string('errorfileschanged', 'question');
80         }
81         return $errors;
82     }
83     /*
84      * We want these errors to show up on first loading the form which is not the default for
85      * validation method which is not run until submission.
86      */
87     function definition_after_data(){
88         $mform = $this->_form;
89         $brokenurls   = $this->_customdata['brokenurls'];
90         if (count($brokenurls)){
91             $ignoreval = $mform->getElementValue('ignorebroken');
92             if (!$ignoreval){
93                 $urls   = $this->_customdata['urls'];
94                 $i = 0;
95                 foreach (array_keys($urls) as $url){
96                     if (in_array($url, $brokenurls)){
97                         $mform->setElementError("urls[$i]", get_string('broken', 'question'));
98                     } else {
99                         $mform->setElementError("urls[$i]", '');
100                     }
101                     $i++;
102                 }
103             }
104         }
105     }