merging MOODLE_19_QUESTIONS with HEAD
[moodle.git] / question / contextmove_form.php
1 <?php  //$Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class question_context_move_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));
21         $brokenurls   = $this->_customdata['brokenurls'];
22         if (count($urls)){
24             $mform->addElement('header','general', get_string('filestomove', 'question', $toareaname));
26             $i = 0;
27             foreach (array_keys($urls) as $url){
28                 $iconname = mimeinfo('icon', $url);
29                 $icontype = mimeinfo('type', $url);
30                 $img = "<img src=\"$CFG->pixpath/f/$iconname\"  class=\"icon\" alt=\"$icontype\" />";
31                 if (in_array($url, $brokenurls)){
32                     $mform->addElement('select', "urls[$i]", $img.$url, $brokenfileoptions);
33                 } else {
34                     $mform->addElement('select', "urls[$i]", $img.$url, $fileoptions);
35                 }
36                 $i++;
37             }
39         }
40         if (count($brokenurls)){
41             $mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
42         }
43         //--------------------------------------------------------------------------------
44         $this->add_action_buttons(true, get_string('movecategory', '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                         //no break; COPY check is also applied to MOVE action
60                     case QUESTION_FILECOPY :
61                         if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $tocoursefilesid))){
62                             $errors["urls[$key]"] = get_string('filecantmoveto', 'question');
63                         }
64                         break;
65                     case QUESTION_FILEMOVELINKSONLY :
66                     case  QUESTION_FILEDONOTHING :
67                         break;
68                 }
69             }
70         }
71         //check that there hasn't been any changes in files between time form was displayed
72         //and now when it has been submitted.
73         if (isset($data['urls'])  &&
74             (count($data['urls'])
75                 != count($this->_customdata['urls']))){
76             $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         $mform = $this->_form;
86         $brokenurls   = $this->_customdata['brokenurls'];
87         if (count($brokenurls)){
88             $ignoreval = $mform->getElementValue('ignorebroken');
89             if (!$ignoreval){
90                 $urls   = $this->_customdata['urls'];
91                 $i = 0;
92                 foreach (array_keys($urls) as $url){
93                     if (in_array($url, $brokenurls)){
94                         $mform->setElementError("urls[$i]", get_string('broken', 'question'));
95                     } else {
96                         $mform->setElementError("urls[$i]", '');
97                     }
98                     $i++;
99                 }
100             }
101         }
102     }
104 ?>