PageRenderTime 67ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/edit_form.php

https://bitbucket.org/moodle/moodle
PHP | 193 lines | 133 code | 32 blank | 28 comment | 25 complexity | 1d1cc78a924fb27cdb6c21297f11b20a MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. if (!defined('MOODLE_INTERNAL')) {
  17. die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
  18. }
  19. require_once($CFG->libdir.'/formslib.php');
  20. class blog_edit_form extends moodleform {
  21. public $modnames = array();
  22. /**
  23. * Blog form definition.
  24. */
  25. public function definition() {
  26. global $CFG, $DB;
  27. $mform =& $this->_form;
  28. $entry = $this->_customdata['entry'];
  29. $courseid = $this->_customdata['courseid'];
  30. $modid = $this->_customdata['modid'];
  31. $summaryoptions = $this->_customdata['summaryoptions'];
  32. $attachmentoptions = $this->_customdata['attachmentoptions'];
  33. $sitecontext = $this->_customdata['sitecontext'];
  34. $mform->addElement('header', 'general', get_string('general', 'form'));
  35. $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), array('size' => 60, 'maxlength' => 128));
  36. $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions);
  37. $mform->setType('subject', PARAM_TEXT);
  38. $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
  39. $mform->addRule('subject', get_string('maximumchars', '', 128), 'maxlength', 128, 'client');
  40. $mform->setType('summary_editor', PARAM_RAW);
  41. $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
  42. $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
  43. // Disable publishstate options that are not allowed.
  44. $publishstates = array();
  45. $i = 0;
  46. foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
  47. $publishstates[$state] = $desc; // No maximum was set.
  48. $i++;
  49. }
  50. $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
  51. $mform->addHelpButton('publishstate', 'publishto', 'blog');
  52. $mform->setDefault('publishstate', 'site');
  53. if (core_tag_tag::is_enabled('core', 'post')) {
  54. $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
  55. }
  56. $mform->addElement('tags', 'tags', get_string('tags'),
  57. array('itemtype' => 'post', 'component' => 'core'));
  58. $allmodnames = array();
  59. if (!empty($CFG->useblogassociations)) {
  60. if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid)))) {
  61. if (!empty($courseid)) {
  62. $course = $DB->get_record('course', array('id' => $courseid));
  63. $context = context_course::instance($courseid);
  64. $a = new stdClass();
  65. $a->coursename = format_string($course->fullname, true, array('context' => $context));
  66. $contextid = $context->id;
  67. } else {
  68. $context = context::instance_by_id($entry->courseassoc);
  69. $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
  70. $a = new stdClass();
  71. $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
  72. $contextid = $entry->courseassoc;
  73. }
  74. $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
  75. $mform->addElement('advcheckbox',
  76. 'courseassoc',
  77. get_string('associatewithcourse', 'blog', $a),
  78. null,
  79. null,
  80. array(0, $contextid));
  81. $mform->setDefault('courseassoc', $contextid);
  82. } else if ((!empty($entry->modassoc) || !empty($modid))) {
  83. if (!empty($modid)) {
  84. $mod = get_coursemodule_from_id(false, $modid);
  85. $a = new stdClass();
  86. $a->modtype = get_string('modulename', $mod->modname);
  87. $a->modname = $mod->name;
  88. $context = context_module::instance($modid);
  89. } else {
  90. $context = context::instance_by_id($entry->modassoc);
  91. $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
  92. $a = new stdClass();
  93. $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
  94. $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
  95. $modid = $context->instanceid;
  96. }
  97. $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
  98. $mform->addElement('advcheckbox',
  99. 'modassoc',
  100. get_string('associatewithmodule', 'blog', $a),
  101. null,
  102. null,
  103. array(0, $context->id));
  104. $mform->setDefault('modassoc', $context->id);
  105. }
  106. }
  107. $this->add_action_buttons();
  108. $mform->addElement('hidden', 'action');
  109. $mform->setType('action', PARAM_ALPHANUMEXT);
  110. $mform->setDefault('action', '');
  111. $mform->addElement('hidden', 'entryid');
  112. $mform->setType('entryid', PARAM_INT);
  113. $mform->setDefault('entryid', $entry->id);
  114. $mform->addElement('hidden', 'modid');
  115. $mform->setType('modid', PARAM_INT);
  116. $mform->setDefault('modid', $modid);
  117. $mform->addElement('hidden', 'courseid');
  118. $mform->setType('courseid', PARAM_INT);
  119. $mform->setDefault('courseid', $courseid);
  120. }
  121. /**
  122. * Validate the blog form data.
  123. * @param array $data Data to be validated
  124. * @param array $files unused
  125. * @return array|bool
  126. */
  127. public function validation($data, $files) {
  128. global $CFG, $DB, $USER;
  129. $errors = parent::validation($data, $files);
  130. // Validate course association.
  131. if (!empty($data['courseassoc'])) {
  132. $coursecontext = context::instance_by_id($data['courseassoc']);
  133. if ($coursecontext->contextlevel != CONTEXT_COURSE) {
  134. $errors['courseassoc'] = get_string('error');
  135. }
  136. }
  137. // Validate mod association.
  138. if (!empty($data['modassoc'])) {
  139. $modcontextid = $data['modassoc'];
  140. $modcontext = context::instance_by_id($modcontextid);
  141. if ($modcontext->contextlevel == CONTEXT_MODULE) {
  142. // Get context of the mod's course.
  143. $coursecontext = $modcontext->get_course_context(true);
  144. // Ensure only one course is associated.
  145. if (!empty($data['courseassoc'])) {
  146. if ($data['courseassoc'] != $coursecontext->id) {
  147. $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
  148. }
  149. } else {
  150. $data['courseassoc'] = $coursecontext->id;
  151. }
  152. } else {
  153. $errors['modassoc'] = get_string('error');
  154. }
  155. }
  156. if ($errors) {
  157. return $errors;
  158. }
  159. return true;
  160. }
  161. }