Moodle plugin, allowing collaborative annotations in PDF files
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

159 lines
7.2 KiB

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package mod_pdfannotator
  18. * @copyright 2018 RWTH Aachen (see README.md)
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. *
  21. */
  22. if (!defined('MOODLE_INTERNAL')) {
  23. die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
  24. }
  25. require_once($CFG->dirroot . '/course/moodleform_mod.php');
  26. require_once($CFG->dirroot . '/mod/pdfannotator/lib.php');
  27. require_once($CFG->libdir . '/filelib.php');
  28. class mod_pdfannotator_mod_form extends moodleform_mod {
  29. public function definition() {
  30. global $CFG, $USER, $COURSE;
  31. $mform =& $this->_form;
  32. $config = get_config('mod_pdfannotator');
  33. $mform->addElement('hidden', 'idcreator', $USER->id);
  34. $mform->setType('idcreator', PARAM_INT);
  35. $mform->addElement('hidden', 'idCourse', $COURSE->id);
  36. $mform->setType('idCourse', PARAM_INT);
  37. $mform->addElement('header', 'general', get_string('general', 'form'));
  38. $mform->setType('general', PARAM_TEXT);
  39. $mform->addElement('text', 'name', get_string('setting_alternative_name', 'pdfannotator'), array('size' => '48'));
  40. $mform->addHelpButton('name', 'setting_alternative_name', 'pdfannotator');
  41. if (!empty($CFG->formatstringstriptags)) {
  42. $mform->setType('name', PARAM_TEXT);
  43. } else {
  44. $mform->setType('name', PARAM_CLEANHTML);
  45. }
  46. $mform->addRule('name', null, 'required', null, 'client');
  47. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  48. // Description.
  49. $this->standard_intro_elements();
  50. $element = $mform->getElement('introeditor');
  51. $attributes = $element->getAttributes();
  52. $attributes['rows'] = 5;
  53. $element->setAttributes($attributes);
  54. // Add a filemanager for drag-and-drop file upload.
  55. $filemanageroptions = array();
  56. $filemanageroptions['accepted_types'] = '.pdf';
  57. $filemanageroptions['maxbytes'] = 0;
  58. $filemanageroptions['maxfiles'] = 1; // Upload only one file.
  59. $filemanageroptions['mainfile'] = true;
  60. // Params: 1. type of the element, 2. (html) elementname, 3. label.
  61. $mform->addElement('filemanager', 'files', get_string('setting_fileupload', 'pdfannotator'), null, $filemanageroptions);
  62. $mform->addHelpButton('files', 'setting_fileupload', 'pdfannotator');
  63. $mform->addElement('advcheckbox', 'usevotes', get_string('setting_usevotes', 'pdfannotator'),
  64. get_string('usevotes', 'pdfannotator'), null, array(0, 1));
  65. $mform->setType('usevotes', PARAM_BOOL);
  66. $mform->setDefault('usevotes', $config->usevotes);
  67. $mform->addHelpButton('usevotes', 'setting_usevotes', 'pdfannotator');
  68. $mform->addElement('advcheckbox', 'use_studenttextbox', get_string('setting_use_studenttextbox', 'pdfannotator'),
  69. get_string('use_studenttextbox', 'pdfannotator'), null, array(0, 1));
  70. $mform->setType('use_studenttextbox', PARAM_BOOL);
  71. $mform->setDefault('use_studenttextbox', $config->use_studenttextbox);
  72. $mform->addHelpButton('use_studenttextbox', 'setting_use_studenttextbox', 'pdfannotator');
  73. $mform->addElement('advcheckbox', 'use_studentdrawing', get_string('setting_use_studentdrawing', 'pdfannotator'),
  74. get_string('use_studentdrawing', 'pdfannotator'), null, array(0, 1));
  75. $mform->setType('use_studentdrawing', PARAM_BOOL);
  76. $mform->setDefault('use_studentdrawing', $config->use_studentdrawing);
  77. $mform->addHelpButton('use_studentdrawing', 'setting_use_studentdrawing', 'pdfannotator');
  78. // XXX second checkbox or change to select.
  79. $mform->addElement('advcheckbox', 'useprint', get_string('setting_useprint', 'pdfannotator'),
  80. get_string('useprint', 'pdfannotator'), null, array(0, 1));
  81. $mform->setType('useprint', PARAM_BOOL);
  82. $mform->setDefault('useprint', $config->useprint);
  83. $mform->addHelpButton('useprint', 'setting_useprint', 'pdfannotator');
  84. // Add legacy files flag only if used.
  85. if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
  86. $options = array(RESOURCELIB_LEGACYFILES_DONE => get_string('legacyfilesdone', 'pdfannotator'),
  87. RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'pdfannotator'));
  88. $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'pdfannotator'), $options);
  89. }
  90. $this->standard_coursemodule_elements();
  91. $this->add_action_buttons();
  92. $mform->addElement('hidden', 'revision'); // Hard-coded as 1; should be changed if version becomes important.
  93. $mform->setType('revision', PARAM_INT);
  94. $mform->setDefault('revision', 1);
  95. }
  96. // Loads the old file in the filemanager.
  97. public function data_preprocessing(&$defaultvalues) {
  98. if ($this->current->instance) {
  99. $contextid = $this->context->id;
  100. $draftitemid = file_get_submitted_draft_itemid('files');
  101. file_prepare_draft_area($draftitemid, $contextid, 'mod_pdfannotator', 'content', 0, array('subdirs' => true));
  102. $defaultvalues['files'] = $draftitemid;
  103. $this->_form->disabledIf('files', 'update', 'notchecked', 2);
  104. }
  105. }
  106. public function validation($data, $files) {
  107. global $USER;
  108. $errors = parent::validation($data, $files);
  109. $usercontext = context_user::instance($USER->id);
  110. $fs = get_file_storage();
  111. if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
  112. $errors['files'] = get_string('required');
  113. return $errors;
  114. }
  115. if (count($files) == 1) {
  116. // No need to select main file if only one picked.
  117. return $errors;
  118. } else if (count($files) > 1) {
  119. $mainfile = false;
  120. foreach ($files as $file) {
  121. if ($file->get_sortorder() == 1) {
  122. $mainfile = true;
  123. break;
  124. }
  125. }
  126. // Set a default main file.
  127. if (!$mainfile) {
  128. $file = reset($files);
  129. file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(),
  130. $file->get_filepath(), $file->get_filename(), 1);
  131. }
  132. }
  133. return $errors;
  134. }
  135. }