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

/blog/edit_form.php

https://bitbucket.org/andrewdavidson/sl-clone
PHP | 181 lines | 128 code | 33 blank | 20 comment | 31 complexity | f45ae29926fe20a1944adf30900a20e6 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, GPL-3.0, BSD-3-Clause, LGPL-2.1
  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. function definition() {
  23. global $CFG, $DB;
  24. $mform =& $this->_form;
  25. $entry = $this->_customdata['entry'];
  26. $courseid = $this->_customdata['courseid'];
  27. $modid = $this->_customdata['modid'];
  28. $summaryoptions = $this->_customdata['summaryoptions'];
  29. $attachmentoptions = $this->_customdata['attachmentoptions'];
  30. $sitecontext = $this->_customdata['sitecontext'];
  31. $mform->addElement('header', 'general', get_string('general', 'form'));
  32. $mform->addElement('text', 'subject', get_string('entrytitle', 'blog'), array('size' => 60, 'maxlength' => 128));
  33. $mform->addElement('editor', 'summary_editor', get_string('entrybody', 'blog'), null, $summaryoptions);
  34. $mform->setType('subject', PARAM_TEXT);
  35. $mform->addRule('subject', get_string('emptytitle', 'blog'), 'required', null, 'client');
  36. $mform->addRule('subject', get_string('maximumchars', '', 128), 'maxlength', 128, 'client');
  37. $mform->setType('summary_editor', PARAM_RAW);
  38. $mform->addRule('summary_editor', get_string('emptybody', 'blog'), 'required', null, 'client');
  39. $mform->addElement('filemanager', 'attachment_filemanager', get_string('attachment', 'forum'), null, $attachmentoptions);
  40. //disable publishstate options that are not allowed
  41. $publishstates = array();
  42. $i = 0;
  43. foreach (blog_entry::get_applicable_publish_states() as $state => $desc) {
  44. $publishstates[$state] = $desc; //no maximum was set
  45. $i++;
  46. }
  47. $mform->addElement('select', 'publishstate', get_string('publishto', 'blog'), $publishstates);
  48. $mform->addHelpButton('publishstate', 'publishto', 'blog');
  49. $mform->setDefault('publishstate', 0);
  50. if (!empty($CFG->usetags)) {
  51. $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
  52. $mform->addElement('tags', 'tags', get_string('tags'));
  53. }
  54. $allmodnames = array();
  55. if (!empty($CFG->useblogassociations)) {
  56. if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid))) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
  57. if (!empty($courseid)) {
  58. $course = $DB->get_record('course', array('id' => $courseid));
  59. $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
  60. $context = get_context_instance(CONTEXT_COURSE, $courseid);
  61. $a = new stdClass();
  62. $a->coursename = format_string($course->fullname, true, array('context' => $context));
  63. $contextid = $context->id;
  64. } else {
  65. $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
  66. $a = new stdClass();
  67. $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
  68. $contextid = $entry->courseassoc;
  69. }
  70. $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
  71. $mform->setDefault('courseassoc', $contextid);
  72. } else if ((!empty($entry->modassoc) || !empty($modid)) && has_capability('moodle/blog:associatemodule', $sitecontext)) {
  73. if (!empty($modid)) {
  74. $mod = get_coursemodule_from_id(false, $modid);
  75. $a = new stdClass();
  76. $a->modtype = get_string('modulename', $mod->modname);
  77. $a->modname = $mod->name;
  78. $context = get_context_instance(CONTEXT_MODULE, $modid);
  79. } else {
  80. $context = get_context_instance_by_id($entry->modassoc);
  81. $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
  82. $a = new stdClass();
  83. $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
  84. $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
  85. }
  86. $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
  87. $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
  88. $mform->setDefault('modassoc', $context->id);
  89. }
  90. }
  91. $this->add_action_buttons();
  92. $mform->addElement('hidden', 'action');
  93. $mform->setType('action', PARAM_ACTION);
  94. $mform->setDefault('action', '');
  95. $mform->addElement('hidden', 'entryid');
  96. $mform->setType('entryid', PARAM_INT);
  97. $mform->setDefault('entryid', $entry->id);
  98. $mform->addElement('hidden', 'modid');
  99. $mform->setType('modid', PARAM_INT);
  100. $mform->setDefault('modid', $modid);
  101. $mform->addElement('hidden', 'courseid');
  102. $mform->setType('courseid', PARAM_INT);
  103. $mform->setDefault('courseid', $courseid);
  104. }
  105. function validation($data, $files) {
  106. global $CFG, $DB, $USER;
  107. $errors = array();
  108. $sitecontext = get_context_instance(CONTEXT_SYSTEM);
  109. // validate course association
  110. if (!empty($data['courseassoc']) && has_capability('moodle/blog:associatecourse', $sitecontext)) {
  111. $coursecontext = context::instance_by_id($data['courseassoc'], IGNORE_MISSING);
  112. if ($coursecontext and $coursecontext->contextlevel == CONTEXT_COURSE) {
  113. if (!is_enrolled($coursecontext) and !is_viewing($coursecontext)) {
  114. $errors['courseassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
  115. }
  116. } else {
  117. $errors['courseassoc'] = get_string('error');
  118. }
  119. }
  120. // validate mod association
  121. if (!empty($data['modassoc'])) {
  122. $modcontextid = $data['modassoc'];
  123. $modcontext = context::instance_by_id($modcontextid, IGNORE_MISSING);
  124. if ($modcontext and $modcontext->contextlevel == CONTEXT_MODULE) {
  125. // get context of the mod's course
  126. $coursecontext = $modcontext->get_course_context(true);
  127. // ensure only one course is associated
  128. if (!empty($data['courseassoc'])) {
  129. if ($data['courseassoc'] != $coursecontext->id) {
  130. $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
  131. }
  132. } else {
  133. $data['courseassoc'] = $coursecontext->id;
  134. }
  135. // ensure the user has access to each mod's course
  136. if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
  137. $errors['modassoc'] = get_string('studentnotallowed', '', fullname($USER, true));
  138. }
  139. } else {
  140. $errors['modassoc'] = get_string('error');
  141. }
  142. }
  143. if ($errors) {
  144. return $errors;
  145. }
  146. return true;
  147. }
  148. }