Merge branch 'wip-MDL-28948-MOODLE_19_STABLE' of git://github.com/abgreeve/moodle...
[moodle.git] / question / contextmove_form.php
blob5a6b2799d8bf1089ece99aed5a0b9410e9ba69fa
1 <?php // $Id$
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->libdir.'/formslib.php');
9 class question_context_move_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));
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 $iconname = mimeinfo('icon', $url);
33 $icontype = mimeinfo('type', $url);
34 $img = "<img src=\"$CFG->pixpath/f/$iconname\" 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);
40 $i++;
44 if (count($brokenurls)){
45 $mform->addElement('advcheckbox','ignorebroken', get_string('ignorebroken', 'question'));
47 //--------------------------------------------------------------------------------
48 $this->add_action_buttons(true, get_string('movecategory', 'question'));
52 function validation($data, $files) {
53 $errors = parent::validation($data, $files);
54 $tocoursefilesid = $this->_customdata['tocoursefilesid'];
55 $fromcoursefilesid = $this->_customdata['fromcoursefilesid'];
56 if (isset($data['urls']) && (count($data['urls']))){
57 foreach ($data['urls'] as $key => $urlaction){
58 switch ($urlaction){
59 case QUESTION_FILEMOVE :
60 if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $fromcoursefilesid))){
61 $errors["urls[$key]"] = get_string('filecantmovefrom', 'question');
63 //no break; COPY check is also applied to MOVE action
64 case QUESTION_FILECOPY :
65 if (!has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $tocoursefilesid))){
66 $errors["urls[$key]"] = get_string('filecantmoveto', 'question');
68 break;
69 case QUESTION_FILEMOVELINKSONLY :
70 case QUESTION_FILEDONOTHING :
71 break;
75 //check that there hasn't been any changes in files between time form was displayed
76 //and now when it has been submitted.
77 if (isset($data['urls']) &&
78 (count($data['urls'])
79 != count($this->_customdata['urls']))){
80 $errors['urls[0]'] = get_string('errorfileschanged', 'question');
82 return $errors;
85 * We want these errors to show up on first loading the form which is not the default for
86 * validation method which is not run until submission.
88 function definition_after_data(){
89 $mform = $this->_form;
90 $brokenurls = $this->_customdata['brokenurls'];
91 if (count($brokenurls)){
92 $ignoreval = $mform->getElementValue('ignorebroken');
93 if (!$ignoreval){
94 $urls = $this->_customdata['urls'];
95 $i = 0;
96 foreach (array_keys($urls) as $url){
97 if (in_array($url, $brokenurls)){
98 $mform->setElementError("urls[$i]", get_string('broken', 'question'));
99 } else {
100 $mform->setElementError("urls[$i]", '');
102 $i++;