Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       1  <?php
       2  require_once ($CFG->libdir.'/formslib.php');
       3  require_once($CFG->libdir.'/completionlib.php');
       4  
       5  /**
       6   * This class adds extra methods to form wrapper specific to be used for module
       7   * add / update forms mod/{modname}/mod_form.php replaced deprecated mod/{modname}/mod.html
       8   */
       9  abstract class moodleform_mod extends moodleform {
      10      /** Current data */
      11      protected $current;
      12      /**
      13       * Instance of the module that is being updated. This is the id of the {prefix}{modulename}
      14       * record. Can be used in form definition. Will be "" if this is an 'add' form and not an
      15       * update one.
      16       *
      17       * @var mixed
      18       */
      19      protected $_instance;
      20      /**
      21       * Section of course that module instance will be put in or is in.
      22       * This is always the section number itself (column 'section' from 'course_sections' table).
      23       *
      24       * @var mixed
      25       */
      26      protected $_section;
      27      /**
      28       * Course module record of the module that is being updated. Will be null if this is an 'add' form and not an
      29       * update one.
      30        *
      31       * @var mixed
      32       */
      33      protected $_cm;
      34      /**
      35       * List of modform features
      36       */
      37      protected $_features;
      38      /**
      39       * @var array Custom completion-rule elements, if enabled
      40       */
      41      protected $_customcompletionelements;
      42      /**
      43       * @var string name of module
      44       */
      45      protected $_modname;
      46      /** current context, course or module depends if already exists*/
      47      protected $context;
      48  
      49      /** a flag indicating whether outcomes are being used*/
      50      protected $_outcomesused;
      51  
      52      /**
      53       * @var bool A flag used to indicate that this module should lock settings
      54       *           based on admin settings flags in definition_after_data.
      55       */
      56      protected $applyadminlockedflags = false;
      57  
      58      /** @var object The course format of the current course. */
      59      protected $courseformat;
      60  
      61      function moodleform_mod($current, $section, $cm, $course) {
      62          global $CFG;
      63  
      64          $this->current   = $current;
      65          $this->_instance = $current->instance;
      66          $this->_section  = $section;
      67          $this->_cm       = $cm;
      68          if ($this->_cm) {
      69              $this->context = context_module::instance($this->_cm->id);
      70          } else {
      71              $this->context = context_course::instance($course->id);
      72          }
      73  
      74          // Set the course format.
      75          require_once($CFG->dirroot . '/course/format/lib.php');
      76          $this->courseformat = course_get_format($course);
      77  
      78          // Guess module name
      79          $matches = array();
      80          if (!preg_match('/^mod_([^_]+)_mod_form$/', get_class($this), $matches)) {
      81              debugging('Use $modname parameter or rename form to mod_xx_mod_form, where xx is name of your module');
      82              print_error('unknownmodulename');
      83          }
      84          $this->_modname = $matches[1];
      85          $this->init_features();
      86          parent::moodleform('modedit.php');
      87      }
      88  
      89      protected function init_features() {
      90          global $CFG;
      91  
      92          $this->_features = new stdClass();
      93          $this->_features->groups            = plugin_supports('mod', $this->_modname, FEATURE_GROUPS, true);
      94          $this->_features->groupings         = plugin_supports('mod', $this->_modname, FEATURE_GROUPINGS, false);
      95          $this->_features->outcomes          = (!empty($CFG->enableoutcomes) and plugin_supports('mod', $this->_modname, FEATURE_GRADE_OUTCOMES, true));
      96          $this->_features->hasgrades         = plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false);
      97          $this->_features->idnumber          = plugin_supports('mod', $this->_modname, FEATURE_IDNUMBER, true);
      98          $this->_features->introeditor       = plugin_supports('mod', $this->_modname, FEATURE_MOD_INTRO, true);
      99          $this->_features->defaultcompletion = plugin_supports('mod', $this->_modname, FEATURE_MODEDIT_DEFAULT_COMPLETION, true);
     100          $this->_features->rating            = plugin_supports('mod', $this->_modname, FEATURE_RATE, false);
     101          $this->_features->showdescription   = plugin_supports('mod', $this->_modname, FEATURE_SHOW_DESCRIPTION, false);
     102  
     103          $this->_features->gradecat          = ($this->_features->outcomes or $this->_features->hasgrades);
     104          $this->_features->advancedgrading   = plugin_supports('mod', $this->_modname, FEATURE_ADVANCED_GRADING, false);
     105      }
     106  
     107      /**
     108       * Only available on moodleform_mod.
     109       *
     110       * @param array $default_values passed by reference
     111       */
     112      function data_preprocessing(&$default_values){
     113          if (empty($default_values['scale'])) {
     114              $default_values['assessed'] = 0;
     115          }
     116  
     117          if (empty($default_values['assessed'])){
     118              $default_values['ratingtime'] = 0;
     119          } else {
     120              $default_values['ratingtime']=
     121                  ($default_values['assesstimestart'] && $default_values['assesstimefinish']) ? 1 : 0;
     122          }
     123      }
     124  
     125      /**
     126       * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
     127       */
     128      function definition_after_data() {
     129          global $CFG, $COURSE;
     130          $mform =& $this->_form;
     131  
     132          if ($id = $mform->getElementValue('update')) {
     133              $modulename = $mform->getElementValue('modulename');
     134              $instance   = $mform->getElementValue('instance');
     135  
     136              if ($this->_features->gradecat) {
     137                  $gradecat = false;
     138                  if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
     139                      $outcomes = grade_outcome::fetch_all_available($COURSE->id);
     140                      if (!empty($outcomes)) {
     141                          $gradecat = true;
     142                      }
     143                  }
     144  
     145                  $items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,'iteminstance'=>$instance, 'courseid'=>$COURSE->id));
     146                  //will be no items if, for example, this activity supports ratings but rating aggregate type == no ratings
     147                  if (!empty($items)) {
     148                      foreach ($items as $item) {
     149                          if (!empty($item->outcomeid)) {
     150                              $elname = 'outcome_'.$item->outcomeid;
     151                              if ($mform->elementExists($elname)) {
     152                                  $mform->hardFreeze($elname); // prevent removing of existing outcomes
     153                              }
     154                          }
     155                      }
     156  
     157                      foreach ($items as $item) {
     158                          if (is_bool($gradecat)) {
     159                              $gradecat = $item->categoryid;
     160                              continue;
     161                          }
     162                          if ($gradecat != $item->categoryid) {
     163                              //mixed categories
     164                              $gradecat = false;
     165                              break;
     166                          }
     167                      }
     168                  }
     169  
     170                  if ($gradecat === false) {
     171                      // items and outcomes in different categories - remove the option
     172                      // TODO: add a "Mixed categories" text instead of removing elements with no explanation
     173                      if ($mform->elementExists('gradecat')) {
     174                          $mform->removeElement('gradecat');
     175                          if ($this->_features->rating) {
     176                              //if supports ratings then the max grade dropdown wasnt added so the grade box can be removed entirely
     177                              $mform->removeElement('modstandardgrade');
     178                          }
     179                      }
     180                  }
     181              }
     182          }
     183  
     184          if ($COURSE->groupmodeforce) {
     185              if ($mform->elementExists('groupmode')) {
     186                  $mform->hardFreeze('groupmode'); // groupmode can not be changed if forced from course settings
     187              }
     188          }
     189  
     190          // Don't disable/remove groupingid if it is currently set to something,
     191          // otherwise you cannot turn it off at same time as turning off other
     192          // option (MDL-30764)
     193          if (empty($this->_cm) || !$this->_cm->groupingid) {
     194              if ($mform->elementExists('groupmode') && empty($COURSE->groupmodeforce)) {
     195                  $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
     196  
     197              } else if (!$mform->elementExists('groupmode')) {
     198                  // Groupings have no use without groupmode.
     199                  if ($mform->elementExists('groupingid')) {
     200                      $mform->removeElement('groupingid');
     201                  }
     202              }
     203          }
     204  
     205          // Completion: If necessary, freeze fields
     206          $completion = new completion_info($COURSE);
     207          if ($completion->is_enabled()) {
     208              // If anybody has completed the activity, these options will be 'locked'
     209              $completedcount = empty($this->_cm)
     210                  ? 0
     211                  : $completion->count_user_data($this->_cm);
     212  
     213              $freeze = false;
     214              if (!$completedcount) {
     215                  if ($mform->elementExists('unlockcompletion')) {
     216                      $mform->removeElement('unlockcompletion');
     217                  }
     218                  // Automatically set to unlocked (note: this is necessary
     219                  // in order to make it recalculate completion once the option
     220                  // is changed, maybe someone has completed it now)
     221                  $mform->getElement('completionunlocked')->setValue(1);
     222              } else {
     223                  // Has the element been unlocked, either by the button being pressed
     224                  // in this request, or the field already being set from a previous one?
     225                  if ($mform->exportValue('unlockcompletion') ||
     226                          $mform->exportValue('completionunlocked')) {
     227                      // Yes, add in warning text and set the hidden variable
     228                      $mform->insertElementBefore(
     229                          $mform->createElement('static', 'completedunlocked',
     230                              get_string('completedunlocked', 'completion'),
     231                              get_string('completedunlockedtext', 'completion')),
     232                          'unlockcompletion');
     233                      $mform->removeElement('unlockcompletion');
     234                      $mform->getElement('completionunlocked')->setValue(1);
     235                  } else {
     236                      // No, add in the warning text with the count (now we know
     237                      // it) before the unlock button
     238                      $mform->insertElementBefore(
     239                          $mform->createElement('static', 'completedwarning',
     240                              get_string('completedwarning', 'completion'),
     241                              get_string('completedwarningtext', 'completion', $completedcount)),
     242                          'unlockcompletion');
     243                      $freeze = true;
     244                  }
     245              }
     246  
     247              if ($freeze) {
     248                  $mform->freeze('completion');
     249                  if ($mform->elementExists('completionview')) {
     250                      $mform->freeze('completionview'); // don't use hardFreeze or checkbox value gets lost
     251                  }
     252                  if ($mform->elementExists('completionusegrade')) {
     253                      $mform->freeze('completionusegrade');
     254                  }
     255                  $mform->freeze($this->_customcompletionelements);
     256              }
     257          }
     258  
     259          // Freeze admin defaults if required (and not different from default)
     260          $this->apply_admin_locked_flags();
     261      }
     262  
     263      // form verification
     264      function validation($data, $files) {
     265          global $COURSE, $DB, $CFG;
     266          $errors = parent::validation($data, $files);
     267  
     268          $mform =& $this->_form;
     269  
     270          $errors = array();
     271  
     272          if ($mform->elementExists('name')) {
     273              $name = trim($data['name']);
     274              if ($name == '') {
     275                  $errors['name'] = get_string('required');
     276              }
     277          }
     278  
     279          $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
     280                       'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
     281          if ($data['coursemodule']) {
     282              $cm = $DB->get_record('course_modules', array('id'=>$data['coursemodule']));
     283          } else {
     284              $cm = null;
     285          }
     286  
     287          if ($mform->elementExists('cmidnumber')) {
     288              // verify the idnumber
     289              if (!grade_verify_idnumber($data['cmidnumber'], $COURSE->id, $grade_item, $cm)) {
     290                  $errors['cmidnumber'] = get_string('idnumbertaken');
     291              }
     292          }
     293  
     294          // Ratings: Don't let them select an aggregate type without selecting a scale.
     295          // If the user has selected to use ratings but has not chosen a scale or set max points then the form is
     296          // invalid. If ratings have been selected then the user must select either a scale or max points.
     297          // This matches (horrible) logic in data_preprocessing.
     298          if (isset($data['assessed']) && $data['assessed'] > 0 && empty($data['scale'])) {
     299              $errors['assessed'] = get_string('scaleselectionrequired', 'rating');
     300          }
     301  
     302          // Completion: Don't let them choose automatic completion without turning
     303          // on some conditions. Ignore this check when completion settings are
     304          // locked, as the options are then disabled.
     305          if (array_key_exists('completion', $data) &&
     306                  $data['completion'] == COMPLETION_TRACKING_AUTOMATIC &&
     307                  !empty($data['completionunlocked'])) {
     308              if (empty($data['completionview']) && empty($data['completionusegrade']) &&
     309                  !$this->completion_rule_enabled($data)) {
     310                  $errors['completion'] = get_string('badautocompletion', 'completion');
     311              }
     312          }
     313  
     314          // Availability: Check availability field does not have errors.
     315          if (!empty($CFG->enableavailability)) {
     316              \core_availability\frontend::report_validation_errors($data, $errors);
     317          }
     318  
     319          return $errors;
     320      }
     321  
     322      /**
     323       * Load in existing data as form defaults. Usually new entry defaults are stored directly in
     324       * form definition (new entry form); this function is used to load in data where values
     325       * already exist and data is being edited (edit entry form).
     326       *
     327       * @param mixed $default_values object or array of default values
     328       */
     329      function set_data($default_values) {
     330          if (is_object($default_values)) {
     331              $default_values = (array)$default_values;
     332          }
     333  
     334          $this->data_preprocessing($default_values);
     335          parent::set_data($default_values);
     336      }
     337  
     338      /**
     339       * Adds all the standard elements to a form to edit the settings for an activity module.
     340       */
     341      function standard_coursemodule_elements(){
     342          global $COURSE, $CFG, $DB;
     343          $mform =& $this->_form;
     344  
     345          $this->_outcomesused = false;
     346          if ($this->_features->outcomes) {
     347              if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
     348                  $this->_outcomesused = true;
     349                  $mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
     350                  foreach($outcomes as $outcome) {
     351                      $mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
     352                  }
     353              }
     354          }
     355  
     356  
     357          if ($this->_features->rating) {
     358              require_once($CFG->dirroot.'/rating/lib.php');
     359              $rm = new rating_manager();
     360  
     361              $mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating'));
     362  
     363              $permission=CAP_ALLOW;
     364              $rolenamestring = null;
     365              if (!empty($this->_cm)) {
     366                  $context = context_module::instance($this->_cm->id);
     367  
     368                  $rolenames = get_role_names_with_caps_in_context($context, array('moodle/rating:rate', 'mod/'.$this->_cm->modname.':rate'));
     369                  $rolenamestring = implode(', ', $rolenames);
     370              } else {
     371                  $rolenamestring = get_string('capabilitychecknotavailable','rating');
     372              }
     373              $mform->addElement('static', 'rolewarning', get_string('rolewarning','rating'), $rolenamestring);
     374              $mform->addHelpButton('rolewarning', 'rolewarning', 'rating');
     375  
     376              $mform->addElement('select', 'assessed', get_string('aggregatetype', 'rating') , $rm->get_aggregate_types());
     377              $mform->setDefault('assessed', 0);
     378              $mform->addHelpButton('assessed', 'aggregatetype', 'rating');
     379  
     380              $mform->addElement('modgrade', 'scale', get_string('scale'), false);
     381              $mform->disabledIf('scale', 'assessed', 'eq', 0);
     382              $mform->addHelpButton('scale', 'modgrade', 'grades');
     383              $mform->setDefault('scale', $CFG->gradepointdefault);
     384  
     385              $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'rating'));
     386              $mform->disabledIf('ratingtime', 'assessed', 'eq', 0);
     387  
     388              $mform->addElement('date_time_selector', 'assesstimestart', get_string('from'));
     389              $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0);
     390              $mform->disabledIf('assesstimestart', 'ratingtime');
     391  
     392              $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to'));
     393              $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0);
     394              $mform->disabledIf('assesstimefinish', 'ratingtime');
     395          }
     396  
     397          //doing this here means splitting up the grade related settings on the lesson settings page
     398          //$this->standard_grading_coursemodule_elements();
     399  
     400          $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
     401  
     402          $mform->addElement('modvisible', 'visible', get_string('visible'));
     403          if (!empty($this->_cm)) {
     404              $context = context_module::instance($this->_cm->id);
     405              if (!has_capability('moodle/course:activityvisibility', $context)) {
     406                  $mform->hardFreeze('visible');
     407              }
     408          }
     409  
     410          if ($this->_features->idnumber) {
     411              $mform->addElement('text', 'cmidnumber', get_string('idnumbermod'));
     412              $mform->setType('cmidnumber', PARAM_RAW);
     413              $mform->addHelpButton('cmidnumber', 'idnumbermod');
     414          }
     415  
     416          if ($this->_features->groups) {
     417              $options = array(NOGROUPS       => get_string('groupsnone'),
     418                               SEPARATEGROUPS => get_string('groupsseparate'),
     419                               VISIBLEGROUPS  => get_string('groupsvisible'));
     420              $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $options, NOGROUPS);
     421              $mform->addHelpButton('groupmode', 'groupmode', 'group');
     422          }
     423  
     424          if ($this->_features->groupings) {
     425              // Groupings selector - used to select grouping for groups in activity.
     426              $options = array();
     427              if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id))) {
     428                  foreach ($groupings as $grouping) {
     429                      $options[$grouping->id] = format_string($grouping->name);
     430                  }
     431              }
     432              core_collator::asort($options);
     433              $options = array(0 => get_string('none')) + $options;
     434              $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
     435              $mform->addHelpButton('groupingid', 'grouping', 'group');
     436          }
     437  
     438          if (!empty($CFG->enableavailability)) {
     439              // Availability field. This is just a textarea; the user interface
     440              // interaction is all implemented in JavaScript.
     441              $mform->addElement('header', 'availabilityconditionsheader',
     442                      get_string('restrictaccess', 'availability'));
     443              // Note: This field cannot be named 'availability' because that
     444              // conflicts with fields in existing modules (such as assign).
     445              // So it uses a long name that will not conflict.
     446              $mform->addElement('textarea', 'availabilityconditionsjson',
     447                      get_string('accessrestrictions', 'availability'));
     448              // The _cm variable may not be a proper cm_info, so get one from modinfo.
     449              if ($this->_cm) {
     450                  $modinfo = get_fast_modinfo($COURSE);
     451                  $cm = $modinfo->get_cm($this->_cm->id);
     452              } else {
     453                  $cm = null;
     454              }
     455              \core_availability\frontend::include_all_javascript($COURSE, $cm);
     456          }
     457  
     458          // Conditional activities: completion tracking section
     459          if(!isset($completion)) {
     460              $completion = new completion_info($COURSE);
     461          }
     462          if ($completion->is_enabled()) {
     463              $mform->addElement('header', 'activitycompletionheader', get_string('activitycompletion', 'completion'));
     464  
     465              // Unlock button for if people have completed it (will
     466              // be removed in definition_after_data if they haven't)
     467              $mform->addElement('submit', 'unlockcompletion', get_string('unlockcompletion', 'completion'));
     468              $mform->registerNoSubmitButton('unlockcompletion');
     469              $mform->addElement('hidden', 'completionunlocked', 0);
     470              $mform->setType('completionunlocked', PARAM_INT);
     471  
     472              $trackingdefault = COMPLETION_TRACKING_NONE;
     473              // If system and activity default is on, set it.
     474              if ($CFG->completiondefault && $this->_features->defaultcompletion) {
     475                  $trackingdefault = COMPLETION_TRACKING_MANUAL;
     476              }
     477  
     478              $mform->addElement('select', 'completion', get_string('completion', 'completion'),
     479                  array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'),
     480                  COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
     481              $mform->setDefault('completion', $trackingdefault);
     482              $mform->addHelpButton('completion', 'completion', 'completion');
     483  
     484              // Automatic completion once you view it
     485              $gotcompletionoptions = false;
     486              if (plugin_supports('mod', $this->_modname, FEATURE_COMPLETION_TRACKS_VIEWS, false)) {
     487                  $mform->addElement('checkbox', 'completionview', get_string('completionview', 'completion'),
     488                      get_string('completionview_desc', 'completion'));
     489                  $mform->disabledIf('completionview', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
     490                  $gotcompletionoptions = true;
     491              }
     492  
     493              // Automatic completion once it's graded
     494              if (plugin_supports('mod', $this->_modname, FEATURE_GRADE_HAS_GRADE, false)) {
     495                  $mform->addElement('checkbox', 'completionusegrade', get_string('completionusegrade', 'completion'),
     496                      get_string('completionusegrade_desc', 'completion'));
     497                  $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
     498                  $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
     499                  $gotcompletionoptions = true;
     500  
     501                  // If using the rating system, there is no grade unless ratings are enabled.
     502                  if ($this->_features->rating) {
     503                      $mform->disabledIf('completionusegrade', 'assessed', 'eq', 0);
     504                  }
     505              }
     506  
     507              // Automatic completion according to module-specific rules
     508              $this->_customcompletionelements = $this->add_completion_rules();
     509              foreach ($this->_customcompletionelements as $element) {
     510                  $mform->disabledIf($element, 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
     511              }
     512  
     513              $gotcompletionoptions = $gotcompletionoptions ||
     514                  count($this->_customcompletionelements)>0;
     515  
     516              // Automatic option only appears if possible
     517              if ($gotcompletionoptions) {
     518                  $mform->getElement('completion')->addOption(
     519                      get_string('completion_automatic', 'completion'),
     520                      COMPLETION_TRACKING_AUTOMATIC);
     521              }
     522  
     523              // Completion expected at particular date? (For progress tracking)
     524              $mform->addElement('date_selector', 'completionexpected', get_string('completionexpected', 'completion'), array('optional'=>true));
     525              $mform->addHelpButton('completionexpected', 'completionexpected', 'completion');
     526              $mform->disabledIf('completionexpected', 'completion', 'eq', COMPLETION_TRACKING_NONE);
     527          }
     528  
     529          $this->standard_hidden_coursemodule_elements();
     530      }
     531  
     532      /**
     533       * Can be overridden to add custom completion rules if the module wishes
     534       * them. If overriding this, you should also override completion_rule_enabled.
     535       * <p>
     536       * Just add elements to the form as needed and return the list of IDs. The
     537       * system will call disabledIf and handle other behaviour for each returned
     538       * ID.
     539       * @return array Array of string IDs of added items, empty array if none
     540       */
     541      function add_completion_rules() {
     542          return array();
     543      }
     544  
     545      /**
     546       * Called during validation. Override to indicate, based on the data, whether
     547       * a custom completion rule is enabled (selected).
     548       *
     549       * @param array $data Input data (not yet validated)
     550       * @return bool True if one or more rules is enabled, false if none are;
     551       *   default returns false
     552       */
     553      function completion_rule_enabled($data) {
     554          return false;
     555      }
     556  
     557      function standard_hidden_coursemodule_elements(){
     558          $mform =& $this->_form;
     559          $mform->addElement('hidden', 'course', 0);
     560          $mform->setType('course', PARAM_INT);
     561  
     562          $mform->addElement('hidden', 'coursemodule', 0);
     563          $mform->setType('coursemodule', PARAM_INT);
     564  
     565          $mform->addElement('hidden', 'section', 0);
     566          $mform->setType('section', PARAM_INT);
     567  
     568          $mform->addElement('hidden', 'module', 0);
     569          $mform->setType('module', PARAM_INT);
     570  
     571          $mform->addElement('hidden', 'modulename', '');
     572          $mform->setType('modulename', PARAM_PLUGIN);
     573  
     574          $mform->addElement('hidden', 'instance', 0);
     575          $mform->setType('instance', PARAM_INT);
     576  
     577          $mform->addElement('hidden', 'add', 0);
     578          $mform->setType('add', PARAM_ALPHA);
     579  
     580          $mform->addElement('hidden', 'update', 0);
     581          $mform->setType('update', PARAM_INT);
     582  
     583          $mform->addElement('hidden', 'return', 0);
     584          $mform->setType('return', PARAM_BOOL);
     585  
     586          $mform->addElement('hidden', 'sr', 0);
     587          $mform->setType('sr', PARAM_INT);
     588      }
     589  
     590      public function standard_grading_coursemodule_elements() {
     591          global $COURSE, $CFG;
     592          $mform =& $this->_form;
     593  
     594          if ($this->_features->hasgrades) {
     595  
     596              if (!$this->_features->rating || $this->_features->gradecat) {
     597                  $mform->addElement('header', 'modstandardgrade', get_string('grade'));
     598              }
     599  
     600              //if supports grades and grades arent being handled via ratings
     601              if (!$this->_features->rating) {
     602                  $mform->addElement('modgrade', 'grade', get_string('grade'));
     603                  $mform->addHelpButton('grade', 'modgrade', 'grades');
     604                  $mform->setDefault('grade', $CFG->gradepointdefault);
     605              }
     606  
     607              if ($this->_features->advancedgrading
     608                      and !empty($this->current->_advancedgradingdata['methods'])
     609                      and !empty($this->current->_advancedgradingdata['areas'])) {
     610  
     611                  if (count($this->current->_advancedgradingdata['areas']) == 1) {
     612                      // if there is just one gradable area (most cases), display just the selector
     613                      // without its name to make UI simplier
     614                      $areadata = reset($this->current->_advancedgradingdata['areas']);
     615                      $areaname = key($this->current->_advancedgradingdata['areas']);
     616                      $mform->addElement('select', 'advancedgradingmethod_'.$areaname,
     617                          get_string('gradingmethod', 'core_grading'), $this->current->_advancedgradingdata['methods']);
     618                      $mform->addHelpButton('advancedgradingmethod_'.$areaname, 'gradingmethod', 'core_grading');
     619  
     620                  } else {
     621                      // the module defines multiple gradable areas, display a selector
     622                      // for each of them together with a name of the area
     623                      $areasgroup = array();
     624                      foreach ($this->current->_advancedgradingdata['areas'] as $areaname => $areadata) {
     625                          $areasgroup[] = $mform->createElement('select', 'advancedgradingmethod_'.$areaname,
     626                              $areadata['title'], $this->current->_advancedgradingdata['methods']);
     627                          $areasgroup[] = $mform->createElement('static', 'advancedgradingareaname_'.$areaname, '', $areadata['title']);
     628                      }
     629                      $mform->addGroup($areasgroup, 'advancedgradingmethodsgroup', get_string('gradingmethods', 'core_grading'),
     630                          array(' ', '<br />'), false);
     631                  }
     632              }
     633  
     634              if ($this->_features->gradecat) {
     635                  $mform->addElement('select', 'gradecat',
     636                          get_string('gradecategoryonmodform', 'grades'),
     637                          grade_get_categories_menu($COURSE->id, $this->_outcomesused));
     638                  $mform->addHelpButton('gradecat', 'gradecategoryonmodform', 'grades');
     639              }
     640          }
     641      }
     642  
     643      function add_intro_editor($required=false, $customlabel=null) {
     644          if (!$this->_features->introeditor) {
     645              // intro editor not supported in this module
     646              return;
     647          }
     648  
     649          $mform = $this->_form;
     650          $label = is_null($customlabel) ? get_string('moduleintro') : $customlabel;
     651  
     652          $mform->addElement('editor', 'introeditor', $label, array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES,
     653              'noclean' => true, 'context' => $this->context, 'subdirs' => true));
     654          $mform->setType('introeditor', PARAM_RAW); // no XSS prevention here, users must be trusted
     655          if ($required) {
     656              $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
     657          }
     658  
     659          // If the 'show description' feature is enabled, this checkbox appears below the intro.
     660          // We want to hide that when using the singleactivity course format because it is confusing.
     661          if ($this->_features->showdescription  && $this->courseformat->has_view_page()) {
     662              $mform->addElement('checkbox', 'showdescription', get_string('showdescription'));
     663              $mform->addHelpButton('showdescription', 'showdescription');
     664          }
     665      }
     666  
     667      /**
     668       * Overriding formslib's add_action_buttons() method, to add an extra submit "save changes and return" button.
     669       *
     670       * @param bool $cancel show cancel button
     671       * @param string $submitlabel null means default, false means none, string is label text
     672       * @param string $submit2label  null means default, false means none, string is label text
     673       * @return void
     674       */
     675      function add_action_buttons($cancel=true, $submitlabel=null, $submit2label=null) {
     676          if (is_null($submitlabel)) {
     677              $submitlabel = get_string('savechangesanddisplay');
     678          }
     679  
     680          if (is_null($submit2label)) {
     681              $submit2label = get_string('savechangesandreturntocourse');
     682          }
     683  
     684          $mform = $this->_form;
     685  
     686          // elements in a row need a group
     687          $buttonarray = array();
     688  
     689          // Label for the submit button to return to the course.
     690          // Ignore this button in single activity format because it is confusing.
     691          if ($submit2label !== false && $this->courseformat->has_view_page()) {
     692              $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
     693          }
     694  
     695          if ($submitlabel !== false) {
     696              $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitlabel);
     697          }
     698  
     699          if ($cancel) {
     700              $buttonarray[] = &$mform->createElement('cancel');
     701          }
     702  
     703          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
     704          $mform->setType('buttonar', PARAM_RAW);
     705          $mform->closeHeaderBefore('buttonar');
     706      }
     707  
     708      /**
     709       * Get the list of admin settings for this module and apply any locked settings.
     710       * This cannot happen in apply_admin_defaults because we do not the current values of the settings
     711       * in that function because set_data has not been called yet.
     712       *
     713       * @return void
     714       */
     715      protected function apply_admin_locked_flags() {
     716          global $OUTPUT;
     717  
     718          if (!$this->applyadminlockedflags) {
     719              return;
     720          }
     721  
     722          $settings = get_config($this->_modname);
     723          $mform = $this->_form;
     724          $lockedicon = html_writer::tag('span',
     725                                         $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin')),
     726                                         array('class' => 'action-icon'));
     727          $isupdate = !empty($this->_cm);
     728  
     729          foreach ($settings as $name => $value) {
     730              if (strpos('_', $name) !== false) {
     731                  continue;
     732              }
     733              if ($mform->elementExists($name)) {
     734                  $element = $mform->getElement($name);
     735                  $lockedsetting = $name . '_locked';
     736                  if (!empty($settings->$lockedsetting)) {
     737                      // Always lock locked settings for new modules,
     738                      // for updates, only lock them if the current value is the same as the default (or there is no current value).
     739                      $value = $settings->$name;
     740                      if ($isupdate && isset($this->current->$name)) {
     741                          $value = $this->current->$name;
     742                      }
     743                      if ($value == $settings->$name) {
     744                          $mform->setConstant($name, $settings->$name);
     745                          $element->setLabel($element->getLabel() . $lockedicon);
     746                          // Do not use hardfreeze because we need the hidden input to check dependencies.
     747                          $element->freeze();
     748                      }
     749                  }
     750              }
     751          }
     752      }
     753  
     754      /**
     755       * Get the list of admin settings for this module and apply any defaults/advanced/locked settings.
     756       *
     757       * @param $datetimeoffsets array - If passed, this is an array of fieldnames => times that the
     758       *                         default date/time value should be relative to. If not passed, all
     759       *                         date/time fields are set relative to the users current midnight.
     760       * @return void
     761       */
     762      public function apply_admin_defaults($datetimeoffsets = array()) {
     763          // This flag triggers the settings to be locked in apply_admin_locked_flags().
     764          $this->applyadminlockedflags = true;
     765  
     766          $settings = get_config($this->_modname);
     767          $mform = $this->_form;
     768          $usermidnight = usergetmidnight(time());
     769          $isupdate = !empty($this->_cm);
     770  
     771          foreach ($settings as $name => $value) {
     772              if (strpos('_', $name) !== false) {
     773                  continue;
     774              }
     775              if ($mform->elementExists($name)) {
     776                  $element = $mform->getElement($name);
     777                  if (!$isupdate) {
     778                      if ($element->getType() == 'date_time_selector') {
     779                          $enabledsetting = $name . '_enabled';
     780                          if (empty($settings->$enabledsetting)) {
     781                              $mform->setDefault($name, 0);
     782                          } else {
     783                              $relativetime = $usermidnight;
     784                              if (isset($datetimeoffsets[$name])) {
     785                                  $relativetime = $datetimeoffsets[$name];
     786                              }
     787                              $mform->setDefault($name, $relativetime + $settings->$name);
     788                          }
     789                      } else {
     790                          $mform->setDefault($name, $settings->$name);
     791                      }
     792                  }
     793                  $advancedsetting = $name . '_adv';
     794                  if (!empty($settings->$advancedsetting)) {
     795                      $mform->setAdvanced($name);
     796                  }
     797              }
     798          }
     799      }
     800  }
     801  
     802  
    

    Search This Site: