PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 174 lines | 122 code | 33 blank | 19 comment | 25 complexity | 70e542cbbd81f02eb7a9602a37dc64fe MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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. 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)))) {
  57. if (!empty($courseid)) {
  58. $course = $DB->get_record('course', array('id' => $courseid));
  59. $context = context_course::instance($courseid);
  60. $a = new stdClass();
  61. $a->coursename = format_string($course->fullname, true, array('context' => $context));
  62. $contextid = $context->id;
  63. } else {
  64. $context = context::instance_by_id($entry->courseassoc);
  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('header', 'assochdr', get_string('associations', 'blog'));
  71. $mform->addElement('advcheckbox', 'courseassoc', get_string('associatewithcourse', 'blog', $a), null, null, array(0, $contextid));
  72. $mform->setDefault('courseassoc', $contextid);
  73. } else if ((!empty($entry->modassoc) || !empty($modid))) {
  74. if (!empty($modid)) {
  75. $mod = get_coursemodule_from_id(false, $modid);
  76. $a = new stdClass();
  77. $a->modtype = get_string('modulename', $mod->modname);
  78. $a->modname = $mod->name;
  79. $context = context_module::instance($modid);
  80. } else {
  81. $context = context::instance_by_id($entry->modassoc);
  82. $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
  83. $a = new stdClass();
  84. $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
  85. $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
  86. $modid = $context->instanceid;
  87. }
  88. $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
  89. $mform->addElement('advcheckbox', 'modassoc', get_string('associatewithmodule', 'blog', $a), null, null, array(0, $context->id));
  90. $mform->setDefault('modassoc', $context->id);
  91. }
  92. }
  93. $this->add_action_buttons();
  94. $mform->addElement('hidden', 'action');
  95. $mform->setType('action', PARAM_ALPHANUMEXT);
  96. $mform->setDefault('action', '');
  97. $mform->addElement('hidden', 'entryid');
  98. $mform->setType('entryid', PARAM_INT);
  99. $mform->setDefault('entryid', $entry->id);
  100. $mform->addElement('hidden', 'modid');
  101. $mform->setType('modid', PARAM_INT);
  102. $mform->setDefault('modid', $modid);
  103. $mform->addElement('hidden', 'courseid');
  104. $mform->setType('courseid', PARAM_INT);
  105. $mform->setDefault('courseid', $courseid);
  106. }
  107. function validation($data, $files) {
  108. global $CFG, $DB, $USER;
  109. $errors = array();
  110. // validate course association
  111. if (!empty($data['courseassoc'])) {
  112. $coursecontext = context::instance_by_id($data['courseassoc']);
  113. if ($coursecontext->contextlevel != CONTEXT_COURSE) {
  114. $errors['courseassoc'] = get_string('error');
  115. }
  116. }
  117. // validate mod association
  118. if (!empty($data['modassoc'])) {
  119. $modcontextid = $data['modassoc'];
  120. $modcontext = context::instance_by_id($modcontextid);
  121. if ($modcontext->contextlevel == CONTEXT_MODULE) {
  122. // get context of the mod's course
  123. $coursecontext = $modcontext->get_course_context(true);
  124. // ensure only one course is associated
  125. if (!empty($data['courseassoc'])) {
  126. if ($data['courseassoc'] != $coursecontext->id) {
  127. $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
  128. }
  129. } else {
  130. $data['courseassoc'] = $coursecontext->id;
  131. }
  132. } else {
  133. $errors['modassoc'] = get_string('error');
  134. }
  135. }
  136. if ($errors) {
  137. return $errors;
  138. }
  139. return true;
  140. }
  141. }