PageRenderTime 27ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/forumanon/post_form.php

https://github.com/estar/moodle-anon-forum
PHP | 199 lines | 139 code | 34 blank | 26 comment | 39 complexity | dcee04df85d7cba6c86c07337e099489 MD5 | raw file
  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-forum
  18. * @copyright Jamie Pratt <me@jamiep.org>
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. if (!defined('MOODLE_INTERNAL')) {
  22. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  23. }
  24. require_once($CFG->libdir.'/formslib.php');
  25. class mod_forumanon_post_form extends moodleform {
  26. function definition() {
  27. global $CFG, $DB;
  28. $mform =& $this->_form;
  29. $course = $this->_customdata['course'];
  30. $cm = $this->_customdata['cm'];
  31. $coursecontext = $this->_customdata['coursecontext'];
  32. $modcontext = $this->_customdata['modcontext'];
  33. $forum = $this->_customdata['forum'];
  34. $post = $this->_customdata['post'];
  35. // if $forum->maxbytes == '0' means we should use $course->maxbytes
  36. if ($forum->maxbytes == '0') {
  37. $forum->maxbytes = $course->maxbytes;
  38. }
  39. // TODO: add max files and max size support
  40. $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext'=>true, 'context'=>$modcontext);
  41. $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
  42. $mform->addElement('text', 'subject', get_string('subject', 'forumanon'), 'size="48"');
  43. $mform->setType('subject', PARAM_TEXT);
  44. $mform->addRule('subject', get_string('required'), 'required', null, 'client');
  45. $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  46. $mform->addElement('editor', 'message', get_string('message', 'forumanon'), null, $editoroptions);
  47. $mform->setType('message', PARAM_RAW);
  48. $mform->addRule('message', get_string('required'), 'required', null, 'client');
  49. if (isset($forum->id) && forumanon_is_forcesubscribed($forum)) {
  50. $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forumanon'), get_string('everyoneissubscribed', 'forumanon'));
  51. $mform->addElement('hidden', 'subscribe');
  52. $mform->setType('subscribe', PARAM_INT);
  53. $mform->addHelpButton('subscribemessage', 'subscription', 'forumanon');
  54. } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUMANON_DISALLOWSUBSCRIBE ||
  55. has_capability('moodle/course:manageactivities', $coursecontext)) {
  56. $options = array();
  57. $options[0] = get_string('subscribestop', 'forumanon');
  58. $options[1] = get_string('subscribestart', 'forumanon');
  59. $mform->addElement('select', 'subscribe', get_string('subscription', 'forumanon'), $options);
  60. $mform->addHelpButton('subscribe', 'subscription', 'forumanon');
  61. } else if ($forum->forcesubscribe == FORUMANON_DISALLOWSUBSCRIBE) {
  62. $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forumanon'), get_string('disallowsubscribe', 'forumanon'));
  63. $mform->addElement('hidden', 'subscribe');
  64. $mform->setType('subscribe', PARAM_INT);
  65. $mform->addHelpButton('subscribemessage', 'subscription', 'forumanon');
  66. }
  67. if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forumanon:createattachment', $modcontext)) { // 1 = No attachments at all
  68. $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forumanon'), null,
  69. array('subdirs'=>0,
  70. 'maxbytes'=>$forum->maxbytes,
  71. 'maxfiles'=>$forum->maxattachments,
  72. 'accepted_types'=>'*',
  73. 'return_types'=>FILE_INTERNAL));
  74. $mform->addHelpButton('attachments', 'attachment', 'forumanon');
  75. }
  76. if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
  77. $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forumanon'));
  78. }
  79. if (!empty($CFG->forumanon_enabletimedposts) && !$post->parent && has_capability('mod/forumanon:viewhiddentimedposts', $coursecontext)) { // hack alert
  80. $mform->addElement('header', '', get_string('displayperiod', 'forumanon'));
  81. $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forumanon'), array('optional'=>true));
  82. $mform->addHelpButton('timestart', 'displaystart', 'forumanon');
  83. $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forumanon'), array('optional'=>true));
  84. $mform->addHelpButton('timeend', 'displayend', 'forumanon');
  85. } else {
  86. $mform->addElement('hidden', 'timestart');
  87. $mform->setType('timestart', PARAM_INT);
  88. $mform->addElement('hidden', 'timeend');
  89. $mform->setType('timeend', PARAM_INT);
  90. $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
  91. }
  92. if (groups_get_activity_groupmode($cm, $course)) { // hack alert
  93. if (empty($post->groupid)) {
  94. $groupname = get_string('allparticipants');
  95. } else {
  96. $group = groups_get_group($post->groupid);
  97. $groupname = format_string($group->name);
  98. }
  99. $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
  100. }
  101. //-------------------------------------------------------------------------------
  102. // forumanon checkbox
  103. $forumanonon = TRUE;
  104. if ($forumanonon) {
  105. $mform->addElement('advcheckbox', 'anonymize', get_string('anonymize', 'forumanon'), null, array('onchange' => "if(this.checked == true) document.getElementById('anon_con_box').style.display = 'block'; else document.getElementById('anon_con_box').style.display = 'none'; "), array(0, 1));
  106. $mform->addHelpButton('anonymize', 'anonymize', 'forumanon');
  107. $anon_display = $this->_customdata['post']->anonymize ? 'block' : 'none';
  108. $mform->addElement('html', '<div id="anon_con_box" style="display:'.$anon_display.';">');
  109. $mform->addElement('html', '<div class="fitem">');
  110. $mform->addElement('html', '<div class="fitemtitle">'.get_string('anonconditiontitle', 'forumanon').'</div>');
  111. $mform->addElement('html', '<div class="felement">'.get_string('anonconditiontext', 'forumanon').'</div>');
  112. $mform->addElement('html', '</div>');
  113. //$mform->addElement('checkbox', 'anon_confirm', get_string('anonconfirm', 'forumanon'));
  114. $mform->addElement('advcheckbox', 'anon_confirm', get_string('anonconfirm', 'forumanon'), null, null, array(0, 1));
  115. $mform->addElement('html', '</div>');
  116. }
  117. //-------------------------------------------------------------------------------
  118. // buttons
  119. if (isset($post->edit)) { // hack alert
  120. $submit_string = get_string('savechanges');
  121. } else {
  122. $submit_string = get_string('posttoforum', 'forumanon');
  123. }
  124. $this->add_action_buttons(false, $submit_string);
  125. $mform->addElement('hidden', 'course');
  126. $mform->setType('course', PARAM_INT);
  127. $mform->addElement('hidden', 'forum');
  128. $mform->setType('forum', PARAM_INT);
  129. $mform->addElement('hidden', 'discussion');
  130. $mform->setType('discussion', PARAM_INT);
  131. $mform->addElement('hidden', 'parent');
  132. $mform->setType('parent', PARAM_INT);
  133. $mform->addElement('hidden', 'userid');
  134. $mform->setType('userid', PARAM_INT);
  135. $mform->addElement('hidden', 'groupid');
  136. $mform->setType('groupid', PARAM_INT);
  137. $mform->addElement('hidden', 'edit');
  138. $mform->setType('edit', PARAM_INT);
  139. $mform->addElement('hidden', 'reply');
  140. $mform->setType('reply', PARAM_INT);
  141. }
  142. function validation($data, $files) {
  143. global $DB, $USER;
  144. $errors = parent::validation($data, $files);
  145. if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
  146. $errors['timeend'] = get_string('timestartenderror', 'forumanon');
  147. }
  148. if (empty($data['message']['text'])) {
  149. $errors['message'] = get_string('erroremptymessage', 'forumanon');
  150. }
  151. if (empty($data['subject'])) {
  152. $errors['subject'] = get_string('erroremptysubject', 'forumanon');
  153. }
  154. if (($data['anonymize']==1) && ($data['anon_confirm']==0)) {
  155. $errors['anon_confirm'] = get_string('erroremptyanonconfirm', 'forumanon');
  156. } else {
  157. if (0 == $DB->count_records('forumanon_statement',array('userid'=>$USER->id))) {
  158. $statement = new stdClass();
  159. $statement->userid = $USER->id;
  160. $DB->insert_record('forumanon_statement',$statement, FALSE);
  161. }
  162. }
  163. return $errors;
  164. }
  165. }