PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/editpost_form.php

https://github.com/NigelCunningham/moodle-mod_forumng
PHP | 312 lines | 222 code | 28 blank | 62 comment | 57 complexity | 947329fc542479c628b3f513b7d35162 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. require_once($CFG->libdir.'/formslib.php');
  17. /**
  18. * Form for editing a post or discussion.
  19. * @package mod
  20. * @subpackage forumng
  21. * @copyright 2011 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. class mod_forumng_editpost_form extends moodleform {
  25. function definition() {
  26. global $CFG, $USER;
  27. $mform = $this->_form;
  28. $params = $this->_customdata['params'];
  29. $forum = $this->_customdata['forum'];
  30. $edit = $this->_customdata['edit'];
  31. $isdiscussion = $this->_customdata['isdiscussion'];
  32. $isroot = $this->_customdata['isroot'];
  33. $ispost = $this->_customdata['ispost'];
  34. $islock = $this->_customdata['islock'];
  35. $post = $this->_customdata['post'];
  36. $ajaxversion = $this->_customdata['ajaxversion'];
  37. $timelimit = isset($this->_customdata['timelimit'])
  38. ? $this->_customdata['timelimit'] : 0;
  39. $draft = isset($this->_customdata['draft'])
  40. ? $this->_customdata['draft'] : null;
  41. if (!$ajaxversion) {
  42. $ajaxversion = '';
  43. }
  44. if ($ajaxversion && !array_key_exists('draft', $params)) {
  45. $params['draft'] = 0;
  46. }
  47. // Keeps track of whether we add a group selector box
  48. $groupselector = false;
  49. if ($ispost) {
  50. $mform->addElement('header', 'general'.$ajaxversion, '');
  51. if ($edit && ($timelimit || $ajaxversion)) {
  52. // For AJAX version, add empty string, which will be
  53. // populated when retrieving each message. Otherwise,
  54. // display a 'slightly safer' version of the time limit. (30
  55. // seconds will display as 1 minute before the real one usually;
  56. // I used 30 seconds becuase it makes more logical, if not
  57. // practical, sense compared to the option for a 1-minute timeout.)
  58. $mform->addElement('static', '', '',
  59. '<div id="id_editlimit' . $ajaxversion . '">' .
  60. ($timelimit ? get_string('editlimited', 'forumng',
  61. userdate($timelimit-30,
  62. get_string('strftimetime', 'langconfig'))) : '') .
  63. '</div>');
  64. }
  65. $quotaleft = $forum->get_remaining_post_quota();
  66. if (!$edit && $quotaleft != mod_forumng::QUOTA_DOES_NOT_APPLY &&
  67. ($quotaleft <= 2 || $ajaxversion)) {
  68. $a = (object)array(
  69. 'posts' => $quotaleft,
  70. 'period' => $forum->get_max_posts_period(true, true));
  71. $text = '';
  72. $script = '';
  73. if ($ajaxversion) {
  74. $script =
  75. '<script type="text/javascript">forumng_quotaleft = ' .
  76. $quotaleft . '</script>';
  77. } else {
  78. $text = ($quotaleft <= 2 ? get_string(
  79. $quotaleft == 1 ? 'quotaleft_singular' : 'quotaleft_plural',
  80. 'forumng', $a) : '');
  81. }
  82. $mform->addElement('static', '', '',
  83. '<div id="id_postlimit' . $ajaxversion .
  84. '">' . $text . $script . '</div>');
  85. }
  86. $mform->addElement('text', 'subject',
  87. $isroot ? get_string('subject', 'forumng')
  88. : get_string('optionalsubject', 'forumng'),
  89. array('size'=>48, 'id'=>'id_subject' . $ajaxversion));
  90. $mform->setType('subject', PARAM_TEXT);
  91. $mform->addRule('subject', get_string('maximumchars', '', 255),
  92. 'maxlength', 255, 'client');
  93. if ($isroot) {
  94. $mform->addRule('subject', get_string('required'),
  95. 'required', null, 'client');
  96. $mform->addRule('subject', get_string('required'),
  97. 'regex', '/\S+/', 'client');
  98. }
  99. if ($islock) {
  100. $mform->setDefault('subject',
  101. get_string('locksubject', 'forumngfeature_lock'));
  102. }
  103. // Special field just to tell javascript that we're trying to use the
  104. // html editor
  105. $mform->addElement('hidden', 'tryinghtmleditor', can_use_html_editor() ? 1 : 0);
  106. $editorattributes = array('id'=>'id_message' . $ajaxversion,
  107. 'cols'=>50, 'rows'=> $ajaxversion ? 15 : 30);
  108. if (!$ajaxversion) {
  109. $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES,
  110. 'context'=>$forum->get_context(true));
  111. $mform->addElement('editor', 'message', get_string('message', 'forumng'),
  112. $editorattributes, $editoroptions);
  113. $gotmessage = true;
  114. } else {
  115. // AJAX version has a placeholder, to be installed by live JavaScript
  116. if (can_use_html_editor()) {
  117. $mform->addElement('text', 'message', get_string('message', 'forumng'),
  118. $editorattributes);
  119. $gotmessage = true;
  120. } else {
  121. $mform->addElement('textarea', 'message[text]', get_string('message', 'forumng'),
  122. $editorattributes);
  123. $mform->addElement('hidden', 'message[format]', FORMAT_MOODLE);
  124. $gotmessage = false;
  125. }
  126. }
  127. if ($gotmessage) {
  128. $mform->setType('message', PARAM_CLEANHTML);
  129. $mform->addRule('message', get_string('required'),
  130. 'required', null, 'client');
  131. } else {
  132. $mform->setType('message[text]', PARAM_RAW);
  133. $mform->addRule('message[text]', get_string('required'),
  134. 'required', null, 'client');
  135. }
  136. // If you can create attachments...
  137. if ($forum->can_create_attachments()) {
  138. if (!$ajaxversion) {
  139. // Non-AJAX version has normal file manager
  140. $mform->addElement('filemanager', 'attachments',
  141. get_string('attachments', 'forumng'), null,
  142. array('subdirs'=>false, 'maxbytes'=>$forum->get_max_bytes()));
  143. } else {
  144. // Ajax version has a placeholder field only, be filled live
  145. $mform->addElement('text', 'attachments', get_string('attachments', 'forumng'),
  146. array('id'=>'id_attachments' . $ajaxversion));
  147. }
  148. }
  149. // If you can mail now, we show this option
  150. $mform->addElement('header', 'id_importance' . $ajaxversion, '');
  151. $attachmentlist = '';
  152. if (!$edit && $forum->can_mail_now()) {
  153. $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forumng'));
  154. $mform->addHelpButton('mailnow', 'mailnow', 'forumng');
  155. }
  156. if ($forum->can_set_important() && !$isdiscussion && !$isroot && !$islock) {
  157. $mform->addElement('checkbox', 'setimportant',
  158. get_string('setimportant', 'forumng'));
  159. }
  160. }
  161. // Additional options apply only to discussion
  162. if ($isdiscussion && $forum->can_manage_discussions()) {
  163. // Restrict to specific time period (only if you are allowed to
  164. // see hidden posts, otherwise stupid to let people hide it and
  165. // then not see)
  166. if ($forum->can_view_hidden()) {
  167. $mform->addElement('header', 'id_displayperiod',
  168. get_string('displayperiod', 'forumng'));
  169. $mform->addElement('date_selector', 'timestart',
  170. get_string('timestart', 'forumng'), array('optional'=>true));
  171. $mform->addHelpButton('timestart', 'displayperiod', 'forumng');
  172. $mform->addElement('date_selector', 'timeend',
  173. get_string('timeend', 'forumng'), array('optional'=>true));
  174. }
  175. // Discussion options...
  176. $mform->addElement('header', 'id_stickyoptions',
  177. get_string('discussionoptions', 'forumng'));
  178. // Sticky discussion
  179. $options = array();
  180. $options[0] = get_string('sticky_no', 'forumng');
  181. $options[1] = get_string('sticky_yes', 'forumng');
  182. $mform->addElement('select', 'sticky',
  183. get_string('sticky', 'forumng'), $options);
  184. $mform->addHelpButton('sticky', 'sticky', 'forumng');
  185. // Group
  186. if ($forum->get_group_mode()) {
  187. // Group ID comes from the post (if provided) or the params
  188. if ($post) {
  189. $groupid = $post->get_discussion()->get_group_id();
  190. } else {
  191. $groupid = $params['group'];
  192. }
  193. // Display as static or dropdown
  194. if (has_capability('moodle/site:accessallgroups',
  195. $forum->get_context())) {
  196. // Users with 'access all groups' can move discussions, so
  197. // show dropdown with all groups
  198. $cm = $forum->get_course_module();
  199. $groups = groups_get_all_groups(
  200. $cm->course,
  201. has_capability('moodle/site:accessallgroups',
  202. $forum->get_context()) ? 0 : $USER->id,
  203. $cm->groupingid);
  204. $options = array();
  205. $options[mod_forumng::ALL_GROUPS] = get_string('allparticipants');
  206. foreach ($groups as $group) {
  207. $options[$group->id] = format_string($group->name);
  208. }
  209. $mform->addElement('select', 'group', get_string('group'),
  210. $options);
  211. $mform->setDefault('group', $groupid);
  212. $groupselector = true;
  213. } else {
  214. // Users without 'access all groups' only see the current
  215. // group of the discussion
  216. if ($groupid == mod_forumng::ALL_GROUPS) {
  217. $groupname = get_string('allparticipants');
  218. } else {
  219. $group = groups_get_group($groupid);
  220. $groupname = format_string($group->name);
  221. }
  222. $mform->addElement('static', 'groupinfo',
  223. get_string('group'), $groupname);
  224. }
  225. }
  226. // Note: Lock/unlock is not available here. When locking a
  227. // discussion you are prompted to give a reason (=new post).
  228. // This is available from the discussion page. Unlocking is
  229. // available from a link in the special 'discussion is locked'
  230. // message that appears at the top of the discussion page.
  231. }
  232. // Post / save changes button
  233. if ($edit) {
  234. $submitlabel = get_string('savechanges');
  235. } else if ($islock) {
  236. $submitlabel = get_string('lockdiscussionbutton', 'forumngfeature_lock');
  237. } else if ($isdiscussion) {
  238. $submitlabel = get_string('postdiscussion', 'forumng');
  239. } else {
  240. $submitlabel = get_string('postreply', 'forumng');
  241. }
  242. $buttonarray = array();
  243. $buttonarray[] = &$mform->createElement('submit', 'submitbutton',
  244. $submitlabel, array('id'=>'id_submitbutton' . $ajaxversion));
  245. $buttonarray[] = &$mform->createElement('cancel', '', '',
  246. array('id'=>'id_cancel' . $ajaxversion));
  247. if (!$edit) {
  248. // Can't save draft while editing
  249. $buttonarray[] = &$mform->createElement('submit', 'savedraft',
  250. get_string('savedraft', 'forumng'),
  251. array('id'=>'id_savedraft' . $ajaxversion));
  252. }
  253. $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  254. $mform->closeHeaderBefore('buttonar');
  255. // Hidden fields
  256. foreach ($params as $param => $value) {
  257. // If there's a group selector, don't duplicate the group param
  258. if ($param == 'group' && $groupselector) {
  259. continue;
  260. }
  261. $mform->addElement('hidden', $param, $value);
  262. }
  263. if (!$ajaxversion) {
  264. // Prevent multiple submits (except of AJAX version)
  265. $mform->addElement('hidden', 'random', rand());
  266. }
  267. }
  268. function validation($data, $files) {
  269. $errors = parent::validation($data, $files);
  270. if (isset($data['timeend'])
  271. && ($data['timeend']!=0) && ($data['timestart']!=0)
  272. && ($data['timeend'] < $data['timestart'])) {
  273. $errors['timeend'] = get_string('timestartenderror', 'forumng');
  274. }
  275. return $errors;
  276. }
  277. /**
  278. * Obtains HTML for form; needed so that this can be printed for AJAX version.
  279. * @return string HTML for form
  280. */
  281. public function get_html() {
  282. return $this->_form->toHtml();
  283. }
  284. }