[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lesson/ -> mod_form.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Form to define a new instance of lesson or edit an instance.
  20   * It is used from /course/modedit.php.
  21   *
  22   * @package mod_lesson
  23   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
  25   **/
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot.'/course/moodleform_mod.php');
  30  require_once($CFG->dirroot.'/mod/lesson/locallib.php');
  31  
  32  class mod_lesson_mod_form extends moodleform_mod {
  33  
  34      protected $course = null;
  35  
  36      public function __construct($current, $section, $cm, $course) {
  37          $this->course = $course;
  38          parent::__construct($current, $section, $cm, $course);
  39      }
  40  
  41      /**
  42       * Old syntax of class constructor. Deprecated in PHP7.
  43       *
  44       * @deprecated since Moodle 3.1
  45       */
  46      public function mod_lesson_mod_form($current, $section, $cm, $course) {
  47          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  48          self::__construct($current, $section, $cm, $course);
  49      }
  50  
  51      function definition() {
  52          global $CFG, $COURSE, $DB;
  53  
  54          $mform    = $this->_form;
  55  
  56          $lessonconfig = get_config('mod_lesson');
  57  
  58          $mform->addElement('header', 'general', get_string('general', 'form'));
  59  
  60          /** Legacy slideshow width element to maintain backwards compatibility */
  61          $mform->addElement('hidden', 'width');
  62          $mform->setType('width', PARAM_INT);
  63          $mform->setDefault('width', $lessonconfig->slideshowwidth);
  64  
  65          /** Legacy slideshow height element to maintain backwards compatibility */
  66          $mform->addElement('hidden', 'height');
  67          $mform->setType('height', PARAM_INT);
  68          $mform->setDefault('height', $lessonconfig->slideshowheight);
  69  
  70          /** Legacy slideshow background color element to maintain backwards compatibility */
  71          $mform->addElement('hidden', 'bgcolor');
  72          $mform->setType('bgcolor', PARAM_TEXT);
  73          $mform->setDefault('bgcolor', $lessonconfig->slideshowbgcolor);
  74  
  75          /** Legacy media popup width element to maintain backwards compatibility */
  76          $mform->addElement('hidden', 'mediawidth');
  77          $mform->setType('mediawidth', PARAM_INT);
  78          $mform->setDefault('mediawidth', $lessonconfig->mediawidth);
  79  
  80          /** Legacy media popup height element to maintain backwards compatibility */
  81          $mform->addElement('hidden', 'mediaheight');
  82          $mform->setType('mediaheight', PARAM_INT);
  83          $mform->setDefault('mediaheight', $lessonconfig->mediaheight);
  84  
  85          /** Legacy media popup close button element to maintain backwards compatibility */
  86          $mform->addElement('hidden', 'mediaclose');
  87          $mform->setType('mediaclose', PARAM_BOOL);
  88          $mform->setDefault('mediaclose', $lessonconfig->mediaclose);
  89  
  90          $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
  91          if (!empty($CFG->formatstringstriptags)) {
  92              $mform->setType('name', PARAM_TEXT);
  93          } else {
  94              $mform->setType('name', PARAM_CLEANHTML);
  95          }
  96          $mform->addRule('name', null, 'required', null, 'client');
  97          $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  98          $this->standard_intro_elements();
  99  
 100          // Appearance.
 101          $mform->addElement('header', 'appearancehdr', get_string('appearance'));
 102  
 103          $filemanageroptions = array();
 104          $filemanageroptions['filetypes'] = '*';
 105          $filemanageroptions['maxbytes'] = $this->course->maxbytes;
 106          $filemanageroptions['subdirs'] = 0;
 107          $filemanageroptions['maxfiles'] = 1;
 108  
 109          $mform->addElement('filemanager', 'mediafile', get_string('mediafile', 'lesson'), null, $filemanageroptions);
 110          $mform->addHelpButton('mediafile', 'mediafile', 'lesson');
 111          $mform->setAdvanced('mediafile', $lessonconfig->mediafile_adv);
 112  
 113          $mform->addElement('selectyesno', 'progressbar', get_string('progressbar', 'lesson'));
 114          $mform->addHelpButton('progressbar', 'progressbar', 'lesson');
 115          $mform->setDefault('progressbar', $lessonconfig->progressbar);
 116          $mform->setAdvanced('progressbar', $lessonconfig->progressbar_adv);
 117  
 118          $mform->addElement('selectyesno', 'ongoing', get_string('ongoing', 'lesson'));
 119          $mform->addHelpButton('ongoing', 'ongoing', 'lesson');
 120          $mform->setDefault('ongoing', $lessonconfig->ongoing);
 121          $mform->setAdvanced('ongoing', $lessonconfig->ongoing_adv);
 122  
 123          $mform->addElement('selectyesno', 'displayleft', get_string('displayleftmenu', 'lesson'));
 124          $mform->addHelpButton('displayleft', 'displayleftmenu', 'lesson');
 125          $mform->setDefault('displayleft', $lessonconfig->displayleftmenu);
 126          $mform->setAdvanced('displayleft', $lessonconfig->displayleftmenu_adv);
 127  
 128          $options = array();
 129          for($i = 100; $i >= 0; $i--) {
 130              $options[$i] = $i.'%';
 131          }
 132          $mform->addElement('select', 'displayleftif', get_string('displayleftif', 'lesson'), $options);
 133          $mform->addHelpButton('displayleftif', 'displayleftif', 'lesson');
 134          $mform->setDefault('displayleftif', $lessonconfig->displayleftif);
 135          $mform->setAdvanced('displayleftif', $lessonconfig->displayleftif_adv);
 136  
 137          $mform->addElement('selectyesno', 'slideshow', get_string('slideshow', 'lesson'));
 138          $mform->addHelpButton('slideshow', 'slideshow', 'lesson');
 139          $mform->setDefault('slideshow', $lessonconfig->slideshow);
 140          $mform->setAdvanced('slideshow', $lessonconfig->slideshow_adv);
 141  
 142          $numbers = array();
 143          for ($i = 20; $i > 1; $i--) {
 144              $numbers[$i] = $i;
 145          }
 146  
 147          $mform->addElement('select', 'maxanswers', get_string('maximumnumberofanswersbranches', 'lesson'), $numbers);
 148          $mform->setDefault('maxanswers', $lessonconfig->maxanswers);
 149          $mform->setAdvanced('maxanswers', $lessonconfig->maxanswers_adv);
 150          $mform->setType('maxanswers', PARAM_INT);
 151          $mform->addHelpButton('maxanswers', 'maximumnumberofanswersbranches', 'lesson');
 152  
 153          $mform->addElement('selectyesno', 'feedback', get_string('displaydefaultfeedback', 'lesson'));
 154          $mform->addHelpButton('feedback', 'displaydefaultfeedback', 'lesson');
 155          $mform->setDefault('feedback', $lessonconfig->defaultfeedback);
 156          $mform->setAdvanced('feedback', $lessonconfig->defaultfeedback_adv);
 157  
 158          // Get the modules.
 159          if ($mods = get_course_mods($COURSE->id)) {
 160              $modinstances = array();
 161              foreach ($mods as $mod) {
 162                  // Get the module name and then store it in a new array.
 163                  if ($module = get_coursemodule_from_instance($mod->modname, $mod->instance, $COURSE->id)) {
 164                      // Exclude this lesson, if it's already been saved.
 165                      if (!isset($this->_cm->id) || $this->_cm->id != $mod->id) {
 166                          $modinstances[$mod->id] = $mod->modname.' - '.$module->name;
 167                      }
 168                  }
 169              }
 170              asort($modinstances); // Sort by module name.
 171              $modinstances=array(0=>get_string('none'))+$modinstances;
 172  
 173              $mform->addElement('select', 'activitylink', get_string('activitylink', 'lesson'), $modinstances);
 174              $mform->addHelpButton('activitylink', 'activitylink', 'lesson');
 175              $mform->setDefault('activitylink', 0);
 176              $mform->setAdvanced('activitylink', $lessonconfig->activitylink_adv);
 177          }
 178  
 179          // Availability.
 180          $mform->addElement('header', 'availabilityhdr', get_string('availability'));
 181  
 182          $mform->addElement('date_time_selector', 'available', get_string('available', 'lesson'), array('optional'=>true));
 183          $mform->setDefault('available', 0);
 184  
 185          $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true));
 186          $mform->setDefault('deadline', 0);
 187  
 188          // Time limit.
 189          $mform->addElement('duration', 'timelimit', get_string('timelimit', 'lesson'),
 190                  array('optional' => true));
 191          $mform->addHelpButton('timelimit', 'timelimit', 'lesson');
 192          $mform->setAdvanced('timelimit', $lessonconfig->timelimit_adv);
 193          $mform->setDefault('timelimit', $lessonconfig->timelimit);
 194  
 195          $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson'));
 196          $mform->addHelpButton('usepassword', 'usepassword', 'lesson');
 197          $mform->setDefault('usepassword', $lessonconfig->password);
 198          $mform->setAdvanced('usepassword', $lessonconfig->password_adv);
 199  
 200          $mform->addElement('passwordunmask', 'password', get_string('password', 'lesson'));
 201          $mform->setDefault('password', '');
 202          $mform->setAdvanced('password', $lessonconfig->password_adv);
 203          $mform->setType('password', PARAM_RAW);
 204          $mform->disabledIf('password', 'usepassword', 'eq', 0);
 205          $mform->disabledIf('passwordunmask', 'usepassword', 'eq', 0);
 206  
 207          // Dependent on.
 208          if ($this->current && isset($this->current->dependency) && $this->current->dependency) {
 209              $mform->addElement('header', 'dependencyon', get_string('prerequisitelesson', 'lesson'));
 210              $mform->addElement('static', 'warningobsolete',
 211                  get_string('warning', 'lesson'),
 212                  get_string('prerequisiteisobsolete', 'lesson'));
 213              $options = array(0 => get_string('none'));
 214              if ($lessons = get_all_instances_in_course('lesson', $COURSE)) {
 215                  foreach ($lessons as $lesson) {
 216                      if ($lesson->id != $this->_instance) {
 217                          $options[$lesson->id] = format_string($lesson->name, true);
 218                      }
 219  
 220                  }
 221              }
 222              $mform->addElement('select', 'dependency', get_string('dependencyon', 'lesson'), $options);
 223              $mform->addHelpButton('dependency', 'dependencyon', 'lesson');
 224              $mform->setDefault('dependency', 0);
 225  
 226              $mform->addElement('text', 'timespent', get_string('timespentminutes', 'lesson'));
 227              $mform->setDefault('timespent', 0);
 228              $mform->setType('timespent', PARAM_INT);
 229              $mform->disabledIf('timespent', 'dependency', 'eq', 0);
 230  
 231              $mform->addElement('checkbox', 'completed', get_string('completed', 'lesson'));
 232              $mform->setDefault('completed', 0);
 233              $mform->disabledIf('completed', 'dependency', 'eq', 0);
 234  
 235              $mform->addElement('text', 'gradebetterthan', get_string('gradebetterthan', 'lesson'));
 236              $mform->setDefault('gradebetterthan', 0);
 237              $mform->setType('gradebetterthan', PARAM_INT);
 238              $mform->disabledIf('gradebetterthan', 'dependency', 'eq', 0);
 239          } else {
 240              $mform->addElement('hidden', 'dependency', 0);
 241              $mform->setType('dependency', PARAM_INT);
 242              $mform->addElement('hidden', 'timespent', 0);
 243              $mform->setType('timespent', PARAM_INT);
 244              $mform->addElement('hidden', 'completed', 0);
 245              $mform->setType('completed', PARAM_INT);
 246              $mform->addElement('hidden', 'gradebetterthan', 0);
 247              $mform->setType('gradebetterthan', PARAM_INT);
 248              $mform->setConstants(array('dependency' => 0, 'timespent' => 0,
 249                      'completed' => 0, 'gradebetterthan' => 0));
 250          }
 251  
 252          // Flow control.
 253          $mform->addElement('header', 'flowcontrol', get_string('flowcontrol', 'lesson'));
 254  
 255          $mform->addElement('selectyesno', 'modattempts', get_string('modattempts', 'lesson'));
 256          $mform->addHelpButton('modattempts', 'modattempts', 'lesson');
 257          $mform->setDefault('modattempts', $lessonconfig->modattempts);
 258          $mform->setAdvanced('modattempts', $lessonconfig->modattempts_adv);
 259  
 260          $mform->addElement('selectyesno', 'review', get_string('displayreview', 'lesson'));
 261          $mform->addHelpButton('review', 'displayreview', 'lesson');
 262          $mform->setDefault('review', $lessonconfig->displayreview);
 263          $mform->setAdvanced('review', $lessonconfig->displayreview_adv);
 264  
 265          $numbers = array();
 266          for ($i = 10; $i > 0; $i--) {
 267              $numbers[$i] = $i;
 268          }
 269          $mform->addElement('select', 'maxattempts', get_string('maximumnumberofattempts', 'lesson'), $numbers);
 270          $mform->addHelpButton('maxattempts', 'maximumnumberofattempts', 'lesson');
 271          $mform->setDefault('maxattempts', $lessonconfig->maximumnumberofattempts);
 272          $mform->setAdvanced('maxattempts', $lessonconfig->maximumnumberofattempts_adv);
 273  
 274          $defaultnextpages = array();
 275          $defaultnextpages[0] = get_string('normal', 'lesson');
 276          $defaultnextpages[LESSON_UNSEENPAGE] = get_string('showanunseenpage', 'lesson');
 277          $defaultnextpages[LESSON_UNANSWEREDPAGE] = get_string('showanunansweredpage', 'lesson');
 278          $mform->addElement('select', 'nextpagedefault', get_string('actionaftercorrectanswer', 'lesson'), $defaultnextpages);
 279          $mform->addHelpButton('nextpagedefault', 'actionaftercorrectanswer', 'lesson');
 280          $mform->setDefault('nextpagedefault', $lessonconfig->defaultnextpage);
 281          $mform->setAdvanced('nextpagedefault', $lessonconfig->defaultnextpage_adv);
 282  
 283          $numbers = array();
 284          for ($i = 100; $i >= 0; $i--) {
 285              $numbers[$i] = $i;
 286          }
 287          $mform->addElement('select', 'maxpages', get_string('numberofpagestoshow', 'lesson'), $numbers);
 288          $mform->addHelpButton('maxpages', 'numberofpagestoshow', 'lesson');
 289          $mform->setDefault('maxpages', $lessonconfig->numberofpagestoshow);
 290          $mform->setAdvanced('maxpages', $lessonconfig->numberofpagestoshow_adv);
 291  
 292          // Grade.
 293          $this->standard_grading_coursemodule_elements();
 294  
 295          // No header here, so that the following settings are displayed in the grade section.
 296  
 297          $mform->addElement('selectyesno', 'practice', get_string('practice', 'lesson'));
 298          $mform->addHelpButton('practice', 'practice', 'lesson');
 299          $mform->setDefault('practice', $lessonconfig->practice);
 300          $mform->setAdvanced('practice', $lessonconfig->practice_adv);
 301  
 302          $mform->addElement('selectyesno', 'custom', get_string('customscoring', 'lesson'));
 303          $mform->addHelpButton('custom', 'customscoring', 'lesson');
 304          $mform->setDefault('custom', $lessonconfig->customscoring);
 305          $mform->setAdvanced('custom', $lessonconfig->customscoring_adv);
 306  
 307          $mform->addElement('selectyesno', 'retake', get_string('retakesallowed', 'lesson'));
 308          $mform->addHelpButton('retake', 'retakesallowed', 'lesson');
 309          $mform->setDefault('retake', $lessonconfig->retakesallowed);
 310          $mform->setAdvanced('retake', $lessonconfig->retakesallowed_adv);
 311  
 312          $options = array();
 313          $options[0] = get_string('usemean', 'lesson');
 314          $options[1] = get_string('usemaximum', 'lesson');
 315          $mform->addElement('select', 'usemaxgrade', get_string('handlingofretakes', 'lesson'), $options);
 316          $mform->addHelpButton('usemaxgrade', 'handlingofretakes', 'lesson');
 317          $mform->setDefault('usemaxgrade', $lessonconfig->handlingofretakes);
 318          $mform->setAdvanced('usemaxgrade', $lessonconfig->handlingofretakes_adv);
 319          $mform->disabledIf('usemaxgrade', 'retake', 'eq', '0');
 320  
 321          $numbers = array();
 322          for ($i = 100; $i >= 0; $i--) {
 323              $numbers[$i] = $i;
 324          }
 325          $mform->addElement('select', 'minquestions', get_string('minimumnumberofquestions', 'lesson'), $numbers);
 326          $mform->addHelpButton('minquestions', 'minimumnumberofquestions', 'lesson');
 327          $mform->setDefault('minquestions', $lessonconfig->minimumnumberofquestions);
 328          $mform->setAdvanced('minquestions', $lessonconfig->minimumnumberofquestions_adv);
 329  
 330  //-------------------------------------------------------------------------------
 331          $this->standard_coursemodule_elements();
 332  //-------------------------------------------------------------------------------
 333  // buttons
 334          $this->add_action_buttons();
 335      }
 336  
 337      /**
 338       * Enforce defaults here
 339       *
 340       * @param array $defaultvalues Form defaults
 341       * @return void
 342       **/
 343      public function data_preprocessing(&$defaultvalues) {
 344          if (isset($defaultvalues['conditions'])) {
 345              $conditions = unserialize($defaultvalues['conditions']);
 346              $defaultvalues['timespent'] = $conditions->timespent;
 347              $defaultvalues['completed'] = $conditions->completed;
 348              $defaultvalues['gradebetterthan'] = $conditions->gradebetterthan;
 349          }
 350  
 351          // Set up the completion checkbox which is not part of standard data.
 352          $defaultvalues['completiontimespentenabled'] =
 353              !empty($defaultvalues['completiontimespent']) ? 1 : 0;
 354  
 355          if ($this->current->instance) {
 356              // Editing existing instance - copy existing files into draft area.
 357              $draftitemid = file_get_submitted_draft_itemid('mediafile');
 358              file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
 359              $defaultvalues['mediafile'] = $draftitemid;
 360          }
 361      }
 362  
 363      /**
 364       * Enforce validation rules here
 365       *
 366       * @param object $data Post data to validate
 367       * @return array
 368       **/
 369      function validation($data, $files) {
 370          $errors = parent::validation($data, $files);
 371  
 372          // Check open and close times are consistent.
 373          if ($data['available'] != 0 && $data['deadline'] != 0 &&
 374                  $data['deadline'] < $data['available']) {
 375              $errors['deadline'] = get_string('closebeforeopen', 'lesson');
 376          }
 377  
 378          if (!empty($data['usepassword']) && empty($data['password'])) {
 379              $errors['password'] = get_string('emptypassword', 'lesson');
 380          }
 381  
 382          return $errors;
 383      }
 384  
 385      /**
 386       * Display module-specific activity completion rules.
 387       * Part of the API defined by moodleform_mod
 388       * @return array Array of string IDs of added items, empty array if none
 389       */
 390      public function add_completion_rules() {
 391          $mform = $this->_form;
 392  
 393          $mform->addElement('checkbox', 'completionendreached', get_string('completionendreached', 'lesson'),
 394                  get_string('completionendreached_desc', 'lesson'));
 395  
 396          $group = array();
 397          $group[] =& $mform->createElement('checkbox', 'completiontimespentenabled', '',
 398                  get_string('completiontimespent', 'lesson'));
 399          $group[] =& $mform->createElement('duration', 'completiontimespent', array('optional' => true));
 400          $mform->addGroup($group, 'completiontimespentgroup', get_string('completiontimespentgroup', 'lesson'), array(' '), false);
 401          $mform->disabledIf('completiontimespent[number]', 'completiontimespentenabled', 'notchecked');
 402          $mform->disabledIf('completiontimespent[timeunit]', 'completiontimespentenabled', 'notchecked');
 403  
 404          return array('completionendreached', 'completiontimespentgroup');
 405      }
 406  
 407      /**
 408       * Called during validation. Indicates whether a module-specific completion rule is selected.
 409       *
 410       * @param array $data Input data (not yet validated)
 411       * @return bool True if one or more rules is enabled, false if none are.
 412       */
 413      public function completion_rule_enabled($data) {
 414          return !empty($data['completionendreached']) || $data['completiontimespent'] > 0;
 415      }
 416  
 417      public function get_data() {
 418          $data = parent::get_data();
 419          if (!$data) {
 420              return false;
 421          }
 422          // Turn off completion setting if the checkbox is not ticked.
 423          if (!empty($data->completionunlocked)) {
 424              $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
 425              if (empty($data->completiontimespentenabled) || !$autocompletion) {
 426                  $data->completiontimespent = 0;
 427              }
 428          }
 429          if (!empty($data->completionunlocked)) {
 430              $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
 431              if (empty($data->completionendreached) || !$autocompletion) {
 432                  $data->completionendreached = 0;
 433              }
 434          }
 435          return $data;
 436      }
 437  }
 438  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1