merging MOODLE_19_QUESTIONS with HEAD
[moodle.git] / question / contextmoveq_form.php
1 <?php  //$Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class question_context_move_question_form extends moodleform {
7     function definition() {
8         global $CFG;
9         $mform    =& $this->_form;
11 //--------------------------------------------------------------------------------
12         $urls   = $this->_customdata['urls'];
13         $fromareaname   = $this->_customdata['fromareaname'];
14         $toareaname   = $this->_customdata['toareaname'];
15         $fileoptions = array(QUESTION_FILEDONOTHING=>get_string('donothing', 'question'),
16                   QUESTION_FILECOPY=>get_string('copy', 'question', $fromareaname),
17                   QUESTION_FILEMOVE=>get_string('move', 'question', $fromareaname),
18                   QUESTION_FILEMOVELINKSONLY=>get_string('movelinksonly', 'question', $fromareaname));
19         $brokenfileoptions = array(QUESTION_FILEDONOTHING=>get_string('donothing', 'question'),
20                   QUESTION_FILEMOVELINKSONLY=>get_string('movelinksonly', 'question', $fromareaname));
22         $brokenurls   = $this->_customdata['brokenurls'];
23         if (count($urls)){
25             $mform->addElement('header','general', get_string('filestomove', 'question', $toareaname));
27             $i = 0;
28             foreach (array_keys($urls) as $url){
29                 $iconname = mimeinfo('icon', $url);
30                 $icontype = mimeinfo('type', $url);
31                 $img = "<img src=\"$CFG->pixpath/f/$iconname\"  class=\"icon\" alt=\"$icontype\" />";
32                 if (in_array($url, $brokenurls)){
33                     $mform->addElement('select', "urls[$i]", $img.$url, $brokenfileoptions);
34                 } else {
35                     $mform->addElement('select', "urls[$i]", $img.$url, $fileoptions);
36                 }
37                 $i++;
38             }
40         }
41         if (count($brokenurls)){
42             $mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
43         }
44 //--------------------------------------------------------------------------------
45         $this->add_action_buttons(true, get_string('moveq', 'question'));
46     }
48     function validation($data){
49         $errors = array();
50         $tocoursefilesid = $this->_customdata['tocoursefilesid'];
51         $fromcoursefilesid = $this->_customdata['fromcoursefilesid'];
52         if (isset($data['urls'])  && (count($data['urls']))){
53             foreach ($data['urls'] as $key => $urlaction){
54                 switch ($urlaction){
55                     case QUESTION_FILEMOVE :
56                         if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $fromcoursefilesid))){
57                             $errors["urls[$key]"] = get_string('filecantmovefrom', 'question');
58                         }
59                     case QUESTION_FILECOPY :
60                         if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $tocoursefilesid))){
61                             $errors["urls[$key]"] = get_string('filecantmoveto', 'question');
62                         }
63                         break;
64                     case QUESTION_FILEMOVELINKSONLY :
65                     case  QUESTION_FILEDONOTHING :
66                         break;
67                 }
68             }
69         }
70         //check that there hasn't been any changes in files between time form was displayed
71         //and now when it has been submitted.
72         if (isset($data['urls'])  &&
73             (count($data['urls'])
74                 != count($this->_customdata['urls']))){
75             $errors['urls[0]'] = get_string('errorfileschanged', 'question');
77         }
78         return $errors;
79     }
80     /*
81      * We want these errors to show up on first loading the form which is not the default for
82      * validation method which is not run until submission.
83      */
84     function definition_after_data(){
85         static $done = false;
86         if (!$done){
87             $mform = $this->_form;
88             $brokenurls   = $this->_customdata['brokenurls'];
89             if (count($brokenurls)){
90                 $ignoreval = $mform->getElementValue('ignorebroken');
91                 if (!$ignoreval){
92                     $urls   = $this->_customdata['urls'];
93                     $i = 0;
94                     foreach (array_keys($urls) as $url){
95                         if (in_array($url, $brokenurls)){
96                             $mform->setElementError("urls[$i]", get_string('broken', 'question'));
97                         } else {
98                             $mform->setElementError("urls[$i]", '');
99                         }
100                         $i++;
101                     }
102                 }
103             }
104             $done = true;
105         }
106     }
108 ?>