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.
   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  /**
  18   * Provides the course_reset_form class.
  19   *
  20   * @package     core
  21   * @copyright   2007 Petr Skoda
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once $CFG->libdir.'/formslib.php';
  28  require_once($CFG->dirroot . '/course/lib.php');
  29  
  30  /**
  31   * Defines the course reset settings form.
  32   *
  33   * @copyright   2007 Petr Skoda
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class course_reset_form extends moodleform {
  37      function definition (){
  38          global $CFG, $COURSE, $DB;
  39  
  40          $mform =& $this->_form;
  41  
  42          $mform->addElement('header', 'generalheader', get_string('general'));
  43  
  44          $mform->addElement('date_time_selector', 'reset_start_date', get_string('startdate'), array('optional' => true));
  45          $mform->addHelpButton('reset_start_date', 'startdate');
  46          $mform->addElement('date_time_selector', 'reset_end_date', get_string('enddate'), array('optional' => true));
  47          $mform->addHelpButton('reset_end_date', 'enddate');
  48          $mform->addElement('checkbox', 'reset_events', get_string('deleteevents', 'calendar'));
  49          $mform->addElement('checkbox', 'reset_notes', get_string('deletenotes', 'notes'));
  50          $mform->addElement('checkbox', 'reset_comments', get_string('deleteallcomments', 'moodle'));
  51          $mform->addElement('checkbox', 'reset_completion', get_string('deletecompletiondata', 'completion'));
  52          $mform->addElement('checkbox', 'delete_blog_associations', get_string('deleteblogassociations', 'blog'));
  53          $mform->addHelpButton('delete_blog_associations', 'deleteblogassociations', 'blog');
  54          $mform->addElement('checkbox', 'reset_competency_ratings', get_string('deletecompetencyratings', 'core_competency'));
  55  
  56          $mform->addElement('header', 'rolesheader', get_string('roles'));
  57  
  58          $roles = get_assignable_roles(context_course::instance($COURSE->id));
  59          $roles[0] = get_string('noroles', 'role');
  60          $roles = array_reverse($roles, true);
  61  
  62          $mform->addElement('select', 'unenrol_users', get_string('unenrolroleusers', 'enrol'), $roles, array('multiple' => 'multiple'));
  63          $mform->addElement('checkbox', 'reset_roles_overrides', get_string('deletecourseoverrides', 'role'));
  64          $mform->setAdvanced('reset_roles_overrides');
  65          $mform->addElement('checkbox', 'reset_roles_local', get_string('deletelocalroles', 'role'));
  66  
  67  
  68          $mform->addElement('header', 'gradebookheader', get_string('gradebook', 'grades'));
  69  
  70          $mform->addElement('checkbox', 'reset_gradebook_items', get_string('removeallcourseitems', 'grades'));
  71          $mform->addHelpButton('reset_gradebook_items', 'removeallcourseitems', 'grades');
  72          $mform->addElement('checkbox', 'reset_gradebook_grades', get_string('removeallcoursegrades', 'grades'));
  73          $mform->addHelpButton('reset_gradebook_grades', 'removeallcoursegrades', 'grades');
  74          $mform->disabledIf('reset_gradebook_grades', 'reset_gradebook_items', 'checked');
  75  
  76  
  77          $mform->addElement('header', 'groupheader', get_string('groups'));
  78  
  79          $mform->addElement('checkbox', 'reset_groups_remove', get_string('deleteallgroups', 'group'));
  80          $mform->addElement('checkbox', 'reset_groups_members', get_string('removegroupsmembers', 'group'));
  81          $mform->disabledIf('reset_groups_members', 'reset_groups_remove', 'checked');
  82  
  83          $mform->addElement('checkbox', 'reset_groupings_remove', get_string('deleteallgroupings', 'group'));
  84          $mform->addElement('checkbox', 'reset_groupings_members', get_string('removegroupingsmembers', 'group'));
  85          $mform->disabledIf('reset_groupings_members', 'reset_groupings_remove', 'checked');
  86  
  87          $unsupported_mods = array();
  88          if ($allmods = $DB->get_records('modules') ) {
  89              foreach ($allmods as $mod) {
  90                  $modname = $mod->name;
  91                  $modfile = $CFG->dirroot."/mod/$modname/lib.php";
  92                  $mod_reset_course_form_definition = $modname.'_reset_course_form_definition';
  93                  $mod_reset__userdata = $modname.'_reset_userdata';
  94                  if (file_exists($modfile)) {
  95                      if (!$DB->count_records($modname, array('course'=>$COURSE->id))) {
  96                          continue; // Skip mods with no instances
  97                      }
  98                      include_once($modfile);
  99                      if (function_exists($mod_reset_course_form_definition)) {
 100                          $mod_reset_course_form_definition($mform);
 101                      } else if (!function_exists($mod_reset__userdata)) {
 102                          $unsupported_mods[] = $mod;
 103                      }
 104                  } else {
 105                      debugging('Missing lib.php in '.$modname.' module');
 106                  }
 107              }
 108          }
 109          // mention unsupported mods
 110          if (!empty($unsupported_mods)) {
 111              $mform->addElement('header', 'unsupportedheader', get_string('resetnotimplemented'));
 112              foreach($unsupported_mods as $mod) {
 113                  $mform->addElement('static', 'unsup'.$mod->name, get_string('modulenameplural', $mod->name));
 114                  $mform->setAdvanced('unsup'.$mod->name);
 115              }
 116          }
 117  
 118          $mform->addElement('hidden', 'id', $COURSE->id);
 119          $mform->setType('id', PARAM_INT);
 120  
 121          $buttonarray = array();
 122          $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('resetcourse'));
 123          $buttonarray[] = &$mform->createElement('submit', 'selectdefault', get_string('selectdefault'));
 124          $buttonarray[] = &$mform->createElement('submit', 'deselectall', get_string('deselectall'));
 125          $buttonarray[] = &$mform->createElement('cancel');
 126          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
 127          $mform->closeHeaderBefore('buttonar');
 128      }
 129  
 130      function load_defaults() {
 131          global $CFG, $COURSE, $DB;
 132  
 133          $mform =& $this->_form;
 134  
 135          $defaults = array ('reset_events'=>1, 'reset_roles_local'=>1, 'reset_gradebook_grades'=>1, 'reset_notes'=>1);
 136  
 137          // Set student as default in unenrol user list, if role with student archetype exist.
 138          if ($studentrole = get_archetype_roles('student')) {
 139              $defaults['unenrol_users'] = array_keys($studentrole);
 140          }
 141  
 142          if ($allmods = $DB->get_records('modules') ) {
 143              foreach ($allmods as $mod) {
 144                  $modname = $mod->name;
 145                  $modfile = $CFG->dirroot."/mod/$modname/lib.php";
 146                  $mod_reset_course_form_defaults = $modname.'_reset_course_form_defaults';
 147                  if (file_exists($modfile)) {
 148                      @include_once($modfile);
 149                      if (function_exists($mod_reset_course_form_defaults)) {
 150                          if ($moddefs = $mod_reset_course_form_defaults($COURSE)) {
 151                              $defaults = $defaults + $moddefs;
 152                          }
 153                      }
 154                  }
 155              }
 156          }
 157  
 158          foreach ($defaults as $element=>$default) {
 159              $mform->setDefault($element, $default);
 160          }
 161      }
 162  
 163      /**
 164       * Validation.
 165       *
 166       * @param array $data
 167       * @param array $files
 168       * @return array the errors that were found
 169       */
 170      public function validation($data, $files) {
 171          global $DB;
 172  
 173          $course = get_course($data['id']);
 174  
 175          $errors = parent::validation($data, $files);
 176  
 177          // We check the values that would be used as start and end.
 178          if ($data['reset_start_date'] != 0) {
 179              $coursedata['startdate'] = $data['reset_start_date'];
 180          } else {
 181              $coursedata['startdate'] = $course->startdate;
 182          }
 183  
 184          if ($data['reset_end_date'] != 0) {
 185              // End date set by the user has preference.
 186              $coursedata['enddate'] = $data['reset_end_date'];
 187          } else if ($data['reset_start_date'] > 0 && $course->enddate != 0) {
 188              // Otherwise, if the current course enddate is set, reset_course_userdata will add the start date time shift to it.
 189              $timeshift = $data['reset_start_date'] - usergetmidnight($course->startdate);
 190              $coursedata['enddate'] = $course->enddate + $timeshift;
 191          } else {
 192              $coursedata['enddate'] = $course->enddate;
 193          }
 194  
 195          if ($errorcode = course_validate_dates($coursedata)) {
 196              $errors['reset_end_date'] = get_string($errorcode, 'error');
 197          }
 198  
 199          return $errors;
 200      }
 201  
 202  }