3 require_once($CFG->libdir
.'/formslib.php');
5 class question_context_move_question_form
extends moodleform
{
7 function definition() {
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'];
25 $mform->addElement('header','general', get_string('filestomove', 'question', $toareaname));
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);
35 $mform->addElement('select', "urls[$i]", $img.$url, $fileoptions);
41 if (count($brokenurls)){
42 $mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
44 //--------------------------------------------------------------------------------
45 $this->add_action_buttons(true, get_string('moveq', 'question'));
48 function validation($data){
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){
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');
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');
64 case QUESTION_FILEMOVELINKSONLY
:
65 case QUESTION_FILEDONOTHING
:
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']) &&
74 != count($this->_customdata
['urls']))){
75 $errors['urls[0]'] = get_string('errorfileschanged', 'question');
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.
84 function definition_after_data(){
87 $mform = $this->_form
;
88 $brokenurls = $this->_customdata
['brokenurls'];
89 if (count($brokenurls)){
90 $ignoreval = $mform->getElementValue('ignorebroken');
92 $urls = $this->_customdata
['urls'];
94 foreach (array_keys($urls) as $url){
95 if (in_array($url, $brokenurls)){
96 $mform->setElementError("urls[$i]", get_string('broken', 'question'));
98 $mform->setElementError("urls[$i]", '');