[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/blog/ -> edit_form.php (source)

   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', 0);
  67  
  68          if (!empty($CFG->usetags)) {
  69              $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
  70              $mform->addElement('tags', 'tags', get_string('tags'));
  71          }
  72  
  73          $allmodnames = array();
  74  
  75          if (!empty($CFG->useblogassociations)) {
  76              if ((!empty($entry->courseassoc) || (!empty($courseid) && empty($modid)))) {
  77                  if (!empty($courseid)) {
  78                      $course = $DB->get_record('course', array('id' => $courseid));
  79                      $context = context_course::instance($courseid);
  80                      $a = new stdClass();
  81                      $a->coursename = format_string($course->fullname, true, array('context' => $context));
  82                      $contextid = $context->id;
  83                  } else {
  84                      $context = context::instance_by_id($entry->courseassoc);
  85                      $sql = 'SELECT fullname FROM {course} cr LEFT JOIN {context} ct ON ct.instanceid = cr.id WHERE ct.id = ?';
  86                      $a = new stdClass();
  87                      $a->coursename = $DB->get_field_sql($sql, array($entry->courseassoc));
  88                      $contextid = $entry->courseassoc;
  89                  }
  90  
  91                  $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
  92                  $mform->addElement('advcheckbox',
  93                                     'courseassoc',
  94                                     get_string('associatewithcourse', 'blog', $a),
  95                                     null,
  96                                     null,
  97                                     array(0, $contextid));
  98                  $mform->setDefault('courseassoc', $contextid);
  99  
 100              } else if ((!empty($entry->modassoc) || !empty($modid))) {
 101                  if (!empty($modid)) {
 102                      $mod = get_coursemodule_from_id(false, $modid);
 103                      $a = new stdClass();
 104                      $a->modtype = get_string('modulename', $mod->modname);
 105                      $a->modname = $mod->name;
 106                      $context = context_module::instance($modid);
 107                  } else {
 108                      $context = context::instance_by_id($entry->modassoc);
 109                      $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
 110                      $a = new stdClass();
 111                      $a->modtype = $DB->get_field('modules', 'name', array('id' => $cm->module));
 112                      $a->modname = $DB->get_field($a->modtype, 'name', array('id' => $cm->instance));
 113                      $modid = $context->instanceid;
 114                  }
 115  
 116                  $mform->addElement('header', 'assochdr', get_string('associations', 'blog'));
 117                  $mform->addElement('advcheckbox',
 118                                     'modassoc',
 119                                     get_string('associatewithmodule', 'blog', $a),
 120                                     null,
 121                                     null,
 122                                     array(0, $context->id));
 123                  $mform->setDefault('modassoc', $context->id);
 124              }
 125          }
 126  
 127          $this->add_action_buttons();
 128          $mform->addElement('hidden', 'action');
 129          $mform->setType('action', PARAM_ALPHANUMEXT);
 130          $mform->setDefault('action', '');
 131  
 132          $mform->addElement('hidden', 'entryid');
 133          $mform->setType('entryid', PARAM_INT);
 134          $mform->setDefault('entryid', $entry->id);
 135  
 136          $mform->addElement('hidden', 'modid');
 137          $mform->setType('modid', PARAM_INT);
 138          $mform->setDefault('modid', $modid);
 139  
 140          $mform->addElement('hidden', 'courseid');
 141          $mform->setType('courseid', PARAM_INT);
 142          $mform->setDefault('courseid', $courseid);
 143      }
 144  
 145      /**
 146       * Validate the blog form data.
 147       * @param array $data Data to be validated
 148       * @param array $files unused
 149       * @return array|bool
 150       */
 151      public function validation($data, $files) {
 152          global $CFG, $DB, $USER;
 153  
 154          $errors = array();
 155  
 156          // Validate course association.
 157          if (!empty($data['courseassoc'])) {
 158              $coursecontext = context::instance_by_id($data['courseassoc']);
 159  
 160              if ($coursecontext->contextlevel != CONTEXT_COURSE) {
 161                  $errors['courseassoc'] = get_string('error');
 162              }
 163          }
 164  
 165          // Validate mod association.
 166          if (!empty($data['modassoc'])) {
 167              $modcontextid = $data['modassoc'];
 168              $modcontext = context::instance_by_id($modcontextid);
 169  
 170              if ($modcontext->contextlevel == CONTEXT_MODULE) {
 171                  // Get context of the mod's course.
 172                  $coursecontext = $modcontext->get_course_context(true);
 173  
 174                  // Ensure only one course is associated.
 175                  if (!empty($data['courseassoc'])) {
 176                      if ($data['courseassoc'] != $coursecontext->id) {
 177                          $errors['modassoc'] = get_string('onlyassociateonecourse', 'blog');
 178                      }
 179                  } else {
 180                      $data['courseassoc'] = $coursecontext->id;
 181                  }
 182              } else {
 183                  $errors['modassoc'] = get_string('error');
 184              }
 185          }
 186  
 187          if ($errors) {
 188              return $errors;
 189          }
 190          return true;
 191      }
 192  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1