/question/contextmoveq_form.php

https://github.com/jarednipper/HSU-common-code · PHP · 112 lines · 93 code · 11 blank · 8 comment · 17 complexity · 4c43c1d25dbde713a62d105384085d7c MD5 · raw file

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