Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/blog/ -> edit_form.php (source)

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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