MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / question / contextmoveq_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_question_form extends moodleform {
11     function definition() {
12         global $CFG;
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));
26         $brokenurls   = $this->_customdata['brokenurls'];
27         if (count($urls)){
29             $mform->addElement('header','general', get_string('filestomove', 'question', $toareaname));
31             $i = 0;
32             foreach (array_keys($urls) as $url){
33                 $icontype = mimeinfo('type', $url);
34                 $img = "<img src=\"" . $OUTPUT->pix_url(file_extension_icon($url)) . "\"  class=\"icon\" alt=\"$icontype\" />";
35                 if (in_array($url, $brokenurls)){
36                     $mform->addElement('select', "urls[$i]", $img.$url, $brokenfileoptions);
37                 } else {
38                     $mform->addElement('select', "urls[$i]", $img.$url, $fileoptions);
39                 }
40                 $i++;
41             }
43         }
44         if (count($brokenurls)){
45             $mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
46         }
47 //--------------------------------------------------------------------------------
48         $this->add_action_buttons(true, get_string('moveq', '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                     case QUESTION_FILECOPY :
63                         if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $tocoursefilesid))){
64                             $errors["urls[$key]"] = get_string('filecantmoveto', 'question');
65                         }
66                         break;
67                     case QUESTION_FILEMOVELINKSONLY :
68                     case  QUESTION_FILEDONOTHING :
69                         break;
70                 }
71             }
72         }
73         //check that there hasn't been any changes in files between time form was displayed
74         //and now when it has been submitted.
75         if (isset($data['urls'])  &&
76             (count($data['urls'])
77                 != count($this->_customdata['urls']))){
78             $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         static $done = false;
89         if (!$done){
90             $mform = $this->_form;
91             $brokenurls   = $this->_customdata['brokenurls'];
92             if (count($brokenurls)){
93                 $ignoreval = $mform->getElementValue('ignorebroken');
94                 if (!$ignoreval){
95                     $urls   = $this->_customdata['urls'];
96                     $i = 0;
97                     foreach (array_keys($urls) as $url){
98                         if (in_array($url, $brokenurls)){
99                             $mform->setElementError("urls[$i]", get_string('broken', 'question'));
100                         } else {
101                             $mform->setElementError("urls[$i]", '');
102                         }
103                         $i++;
104                     }
105                 }
106             }
107             $done = true;
108         }
109     }