2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * File containing the course class.
20 * @package tool_uploadcourse
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
30 * @package tool_uploadcourse
31 * @copyright 2013 Frédéric Massart
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class tool_uploadcourse_course {
36 /** Outcome of the process: creating the course */
39 /** Outcome of the process: updating the course */
42 /** Outcome of the process: deleting the course */
45 /** @var array final import data. */
46 protected $data = array();
48 /** @var array default values. */
49 protected $defaults = array();
51 /** @var array enrolment data. */
52 protected $enrolmentdata;
54 /** @var array errors. */
55 protected $errors = array();
57 /** @var array containing options passed from the processor. */
58 protected $importoptions = array();
60 /** @var int import mode. Matches tool_uploadcourse_processor::MODE_* */
63 /** @var array course import options. */
64 protected $options = array();
66 /** @var int constant value of self::DO_*, what to do with that course */
69 /** @var bool set to true once we have prepared the course */
70 protected $prepared = false;
72 /** @var array course import data. */
73 protected $rawdata = array();
75 /** @var array restore directory. */
76 protected $restoredata;
78 /** @var string course shortname. */
81 /** @var array errors. */
82 protected $statuses = array();
84 /** @var int update mode. Matches tool_uploadcourse_processor::UPDATE_* */
85 protected $updatemode;
87 /** @var array fields allowed as course data. */
88 static protected $validfields = array('fullname', 'shortname', 'idnumber', 'category', 'visible', 'startdate',
89 'summary', 'format', 'theme', 'lang', 'newsitems', 'showgrades', 'showreports', 'legacyfiles', 'maxbytes',
90 'groupmode', 'groupmodeforce', 'groupmodeforce', 'enablecompletion');
92 /** @var array fields required on course creation. */
93 static protected $mandatoryfields = array('fullname', 'summary', 'category');
95 /** @var array fields which are considered as options. */
96 static protected $optionfields = array('delete' => false, 'rename' => null, 'backupfile' => null,
97 'templatecourse' => null, 'reset' => false);
99 /** @var array options determining what can or cannot be done at an import level. */
100 static protected $importoptionsdefaults = array('canrename' => false, 'candelete' => false, 'canreset' => false,
101 'reset' => false, 'restoredir' => null, 'shortnametemplate' => null);
106 * @param int $mode import mode, constant matching tool_uploadcourse_processor::MODE_*
107 * @param int $updatemode update mode, constant matching tool_uploadcourse_processor::UPDATE_*
108 * @param array $rawdata raw course data.
109 * @param array $defaults default course data.
110 * @param array $importoptions import options.
112 public function __construct($mode, $updatemode, $rawdata, $defaults = array(), $importoptions = array()) {
114 if ($mode !== tool_uploadcourse_processor::MODE_CREATE_NEW &&
115 $mode !== tool_uploadcourse_processor::MODE_CREATE_ALL &&
116 $mode !== tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE &&
117 $mode !== tool_uploadcourse_processor::MODE_UPDATE_ONLY) {
118 throw new coding_exception('Incorrect mode.');
119 } else if ($updatemode !== tool_uploadcourse_processor::UPDATE_NOTHING &&
120 $updatemode !== tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY &&
121 $updatemode !== tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS &&
122 $updatemode !== tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS) {
123 throw new coding_exception('Incorrect update mode.');
127 $this->updatemode = $updatemode;
129 if (isset($rawdata['shortname'])) {
130 $this->shortname = $rawdata['shortname'];
132 $this->rawdata = $rawdata;
133 $this->defaults = $defaults;
135 // Extract course options.
136 foreach (self::$optionfields as $option => $default) {
137 $this->options[$option] = isset($rawdata[$option]) ? $rawdata[$option] : $default;
141 foreach (self::$importoptionsdefaults as $option => $default) {
142 $this->importoptions[$option] = isset($importoptions[$option]) ? $importoptions[$option] : $default;
147 * Does the mode allow for course creation?
151 public function can_create() {
152 return in_array($this->mode, array(tool_uploadcourse_processor::MODE_CREATE_ALL,
153 tool_uploadcourse_processor::MODE_CREATE_NEW,
154 tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE)
159 * Does the mode allow for course deletion?
163 public function can_delete() {
164 return $this->importoptions['candelete'];
168 * Does the mode only allow for course creation?
172 public function can_only_create() {
173 return in_array($this->mode, array(tool_uploadcourse_processor::MODE_CREATE_ALL,
174 tool_uploadcourse_processor::MODE_CREATE_NEW));
178 * Does the mode allow for course rename?
182 public function can_rename() {
183 return $this->importoptions['canrename'];
187 * Does the mode allow for course reset?
191 public function can_reset() {
192 return $this->importoptions['canreset'];
196 * Does the mode allow for course update?
200 public function can_update() {
201 return in_array($this->mode, array(tool_uploadcourse_processor::MODE_UPDATE_ONLY,
202 tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE)) && $this->updatemode != tool_uploadcourse_processor::UPDATE_NOTHING;
206 * Can we use default values?
210 public function can_use_defaults() {
211 return in_array($this->updatemode, array(tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS,
212 tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS));
216 * Delete the current course.
220 protected function delete() {
222 $id = $DB->get_field_select('course', 'id', 'shortname = :shortname', array('shortname' => $this->shortname), MUST_EXIST);
223 return delete_course($id, false);
229 * @param string $code error code.
230 * @param lang_string $message error message.
233 protected function error($code, lang_string $message) {
234 if (array_key_exists($code, $this->errors)) {
235 throw new coding_exception('Error code already defined');
237 $this->errors[$code] = $message;
241 * Return whether the course exists or not.
245 protected function exists($shortname = null) {
247 if (is_null($shortname)) {
248 $shortname = $this->shortname;
250 if (!empty($shortname) || is_numeric($shortname)) {
251 return $DB->record_exists('course', array('shortname' => $shortname));
257 * Return the data that will be used upon saving.
261 public function get_data() {
266 * Return the errors found during preparation.
270 public function get_errors() {
271 return $this->errors;
275 * Assemble the course data based on defaults.
277 * This returns the final data to be passed to create_course().
279 * @param array $data current data.
282 protected function get_final_create_data($data) {
283 foreach (self::$validfields as $field) {
284 if (!isset($data[$field]) && isset($this->defaults[$field])) {
285 $data[$field] = $this->defaults[$field];
288 $data['shortname'] = $this->shortname;
293 * Assemble the course data based on defaults.
295 * This returns the final data to be passed to update_course().
297 * @param array $data current data.
298 * @param bool $usedefaults are defaults allowed?
299 * @param bool $missingonly ignore fields which are already set.
302 protected function get_final_update_data($data, $usedefaults = false, $missingonly = false) {
305 $existingdata = $DB->get_record('course', array('shortname' => $this->shortname));
306 foreach (self::$validfields as $field) {
308 if (!is_null($existingdata->$field) and $existingdata->$field !== '') {
312 if (isset($data[$field])) {
313 $newdata[$field] = $data[$field];
314 } else if ($usedefaults && isset($this->defaults[$field])) {
315 $newdata[$field] = $this->defaults[$field];
318 $newdata['id'] = $existingdata->id;
323 * Get the directory of the object to restore.
325 * @return string|false subdirectory in $CFG->tempdir/backup/...
327 protected function get_restore_content_dir() {
331 if (!empty($this->options['backupfile'])) {
332 $backupfile = $this->options['backupfile'];
333 } else if (!empty($this->options['templatecourse']) || is_numeric($this->options['templatecourse'])) {
334 $shortname = $this->options['templatecourse'];
338 $dir = tool_uploadcourse_helper::get_restore_content_dir($backupfile, $shortname, $errors);
339 if (!empty($errors)) {
340 foreach ($errors as $key => $message) {
341 $this->error($key, $message);
346 if (empty($dir) && !empty($this->importoptions['restoredir'])) {
347 $dir = $this->importoptions['restoredir'];
354 * Return the errors found during preparation.
358 public function get_statuses() {
359 return $this->statuses;
363 * Return whether there were errors with this course.
367 public function has_errors() {
368 return !empty($this->errors);
372 * Validates and prepares the data.
374 * @return bool false is any error occured.
376 public function prepare() {
377 $this->prepared = true;
379 // Validate the shortname.
380 if (!empty($this->shortname) || is_numeric($this->shortname)) {
381 if ($this->shortname !== clean_param($this->shortname, PARAM_TEXT)) {
382 $this->error('invalidshortname', new lang_string('invalidshortname', 'tool_uploadcourse'));
387 $exists = $this->exists();
389 // Do we want to delete the course?
390 if ($this->options['delete']) {
392 $this->error('cannotdeletecoursenotexist', new lang_string('cannotdeletecoursenotexist', 'tool_uploadcourse'));
394 } else if (!$this->can_delete()) {
395 $this->error('coursedeletionnotallowed', new lang_string('coursedeletionnotallowed', 'tool_uploadcourse'));
399 $this->do = self::DO_DELETE;
403 // Can we create/update the course under those conditions?
405 if ($this->mode === tool_uploadcourse_processor::MODE_CREATE_NEW) {
406 $this->error('courseexistsanduploadnotallowed', new lang_string('courseexistsanduploadnotallowed', 'tool_uploadcourse'));
410 if (!$this->can_create()) {
411 $this->error('coursedoesnotexistandcreatenotallowed',
412 new lang_string('coursedoesnotexistandcreatenotallowed', 'tool_uploadcourse'));
418 $coursedata = array();
419 foreach ($this->rawdata as $field => $value) {
420 if (!in_array($field, self::$validfields)) {
422 } else if ($field == 'shortname') {
423 // Let's leave it apart from now, use $this->shortname only.
426 $coursedata[$field] = $value;
430 $updatemode = $this->updatemode;
431 $usedefaults = $this->can_use_defaults();
433 // Resolve the category, and fail if not found.
435 $catid = tool_uploadcourse_helper::resolve_category($this->rawdata, $errors);
436 if (empty($errors)) {
437 $coursedata['category'] = $catid;
439 foreach ($errors as $key => $message) {
440 $this->error($key, $message);
445 // If the course does not exist, or will be forced created.
446 if (!$exists || $mode === tool_uploadcourse_processor::MODE_CREATE_ALL) {
448 // Mandatory fields upon creation.
450 foreach (self::$mandatoryfields as $field) {
451 if ((!isset($coursedata[$field]) || $coursedata[$field] === '') &&
452 (!isset($this->defaults[$field]) || $this->defaults[$field] === '')) {
456 if (!empty($errors)) {
457 $this->error('missingmandatoryfields', new lang_string('missingmandatoryfields', 'tool_uploadcourse',
458 implode(', ', $errors)));
463 // Should the course be renamed?
464 if (!empty($this->options['rename']) || is_numeric($this->options['rename'])) {
465 if (!$this->can_update()) {
466 $this->error('canonlyrenameinupdatemode', new lang_string('canonlyrenameinupdatemode', 'tool_uploadcourse'));
468 } else if (!$exists) {
469 $this->error('cannotrenamecoursenotexist', new lang_string('cannotrenamecoursenotexist', 'tool_uploadcourse'));
471 } else if (!$this->can_rename()) {
472 $this->error('courserenamingnotallowed', new lang_string('courserenamingnotallowed', 'tool_uploadcourse'));
474 } else if ($this->options['rename'] !== clean_param($this->options['rename'], PARAM_TEXT)) {
475 $this->error('invalidshortname', new lang_string('invalidshortname', 'tool_uploadcourse'));
477 } else if ($this->exists($this->options['rename'])) {
478 $this->error('cannotrenameshortnamealreadyinuse',
479 new lang_string('cannotrenameshortnamealreadyinuse', 'tool_uploadcourse'));
481 } else if (isset($coursedata['idnumber']) &&
482 $DB->count_records_select('course', 'idnumber = :idn AND shortname != :sn',
483 array('idn' => $coursedata['idnumber'], 'sn' => $this->shortname)) > 0) {
484 $this->error('cannotrenameidnumberconflict', new lang_string('cannotrenameidnumberconflict', 'tool_uploadcourse'));
487 $coursedata['shortname'] = $this->options['rename'];
488 $this->status('courserenamed', new lang_string('courserenamed', 'tool_uploadcourse',
489 array('from' => $this->shortname, 'to' => $coursedata['shortname'])));
492 // Should we generate a shortname?
493 if (empty($this->shortname) && !is_numeric($this->shortname)) {
494 if (empty($this->importoptions['shortnametemplate'])) {
495 $this->error('missingshortnamenotemplate', new lang_string('missingshortnamenotemplate', 'tool_uploadcourse'));
497 } else if (!$this->can_only_create()) {
498 $this->error('cannotgenerateshortnameupdatemode',
499 new lang_string('cannotgenerateshortnameupdatemode', 'tool_uploadcourse'));
502 $newshortname = tool_uploadcourse_helper::generate_shortname($coursedata, $this->importoptions['shortnametemplate']);
503 if (is_null($newshortname)) {
504 $this->error('generatedshortnameinvalid', new lang_string('generatedshortnameinvalid', 'tool_uploadcourse'));
506 } else if ($this->exists($newshortname)) {
507 if ($mode === tool_uploadcourse_processor::MODE_CREATE_NEW) {
508 $this->error('generatedshortnamealreadyinuse',
509 new lang_string('generatedshortnamealreadyinuse', 'tool_uploadcourse'));
514 $this->status('courseshortnamegenerated', new lang_string('courseshortnamegenerated', 'tool_uploadcourse',
516 $this->shortname = $newshortname;
520 // If exists, but we only want to create courses, increment the shortname.
521 if ($exists && $mode === tool_uploadcourse_processor::MODE_CREATE_ALL) {
522 $original = $this->shortname;
523 $this->shortname = tool_uploadcourse_helper::increment_shortname($this->shortname);
525 if ($this->shortname != $original) {
526 $this->status('courseshortnameincremented', new lang_string('courseshortnameincremented', 'tool_uploadcourse',
527 array('from' => $original, 'to' => $this->shortname)));
528 if (isset($coursedata['idnumber'])) {
529 $originalidn = $coursedata['idnumber'];
530 $coursedata['idnumber'] = tool_uploadcourse_helper::increment_idnumber($coursedata['idnumber']);
531 if ($originalidn != $coursedata['idnumber']) {
532 $this->status('courseidnumberincremented', new lang_string('courseidnumberincremented', 'tool_uploadcourse',
533 array('from' => $originalidn, 'to' => $coursedata['idnumber'])));
539 // Ultimate check mode vs. existence.
541 case tool_uploadcourse_processor::MODE_CREATE_NEW:
542 case tool_uploadcourse_processor::MODE_CREATE_ALL:
544 $this->error('courseexistsanduploadnotallowed',
545 new lang_string('courseexistsanduploadnotallowed', 'tool_uploadcourse'));
549 case tool_uploadcourse_processor::MODE_UPDATE_ONLY:
551 $this->error('coursedoesnotexistandcreatenotallowed',
552 new lang_string('coursedoesnotexistandcreatenotallowed', 'tool_uploadcourse'));
556 case tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE:
558 if ($updatemode === tool_uploadcourse_processor::UPDATE_NOTHING) {
559 $this->error('updatemodedoessettonothing',
560 new lang_string('updatemodedoessettonothing', 'tool_uploadcourse'));
566 // O_o Huh?! This should really never happen here!
567 $this->error('unknownimportmode', new lang_string('unknownimportmode', 'tool_uploadcourse'));
573 $missingonly = ($updatemode === tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS);
574 $coursedata = $this->get_final_update_data($coursedata, $usedefaults, $missingonly);
575 $this->do = self::DO_UPDATE;
577 $coursedata = $this->get_final_create_data($coursedata);
578 $this->do = self::DO_CREATE;
581 // Course start date.
582 if (!empty($coursedata['startdate'])) {
583 $coursedata['startdate'] = strtotime($coursedata['startdate']);
586 // Add role renaming.
588 $rolenames = tool_uploadcourse_helper::get_role_names($this->rawdata, $errors);
589 if (!empty($errors)) {
590 foreach ($errors as $key => $message) {
591 $this->error($key, $message);
595 foreach ($rolenames as $rolekey => $rolename) {
596 $coursedata[$rolekey] = $rolename;
600 if (!empty($coursedata['format']) && !in_array($coursedata['format'], tool_uploadcourse_helper::get_course_formats())) {
601 $this->error('invalidcourseformat', new lang_string('invalidcourseformat', 'tool_uploadcourse'));
606 $this->data = $coursedata;
607 $this->enrolmentdata = tool_uploadcourse_helper::get_enrolment_data($this->rawdata);
610 // TODO log warnings.
611 $this->restoredata = $this->get_restore_content_dir();
614 if ($this->importoptions['reset'] || $this->options['reset']) {
615 if ($this->do !== self::DO_UPDATE) {
616 $this->error('canonlyresetcourseinupdatemode',
617 new lang_string('canonlyresetcourseinupdatemode', 'tool_uploadcourse'));
619 } else if (!$this->can_reset()) {
620 $this->error('courseresetnotallowed', new lang_string('courseresetnotallowed', 'tool_uploadcourse'));
629 * Proceed with the import of the course.
633 public function proceed() {
636 if (!$this->prepared) {
637 throw new coding_exception('The course has not been prepared.');
638 } else if ($this->has_errors()) {
639 throw new moodle_exception('Cannot proceed, errors were detected.');
642 if ($this->do === self::DO_DELETE) {
643 if ($this->delete()) {
644 $this->status('coursedeleted', new lang_string('coursedeleted', 'tool_uploadcourse'));
646 $this->error('errorwhiledeletingcourse', new lang_string('errorwhiledeletingcourse', 'tool_uploadcourse'));
649 } else if ($this->do === self::DO_CREATE) {
650 $course = create_course((object) $this->data);
651 $this->status('coursecreated', new lang_string('coursecreated', 'tool_uploadcourse'));
652 } else if ($this->do === self::DO_UPDATE) {
653 $course = (object) $this->data;
654 update_course($course);
655 $this->status('courseupdated', new lang_string('courseupdated', 'tool_uploadcourse'));
657 // Strangely the outcome has not been defined, or is unknown!
658 throw new coding_exception('Unknown outcome!');
662 if (!empty($this->restoredata)) {
663 $rc = new restore_controller($this->restoredata, $course->id, backup::INTERACTIVE_NO,
664 backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
666 // Check if the format conversion must happen first.
667 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
670 if ($rc->execute_precheck()) {
672 $this->status('courserestored', new lang_string('courserestored', 'tool_uploadcourse'));
674 $this->error('errorwhilerestoringcourse', new lang_string('errorwhilerestoringthecourse', 'tool_uploadcourse'));
679 // Proceed with enrolment data.
680 $this->process_enrolment_data($course);
683 if ($this->importoptions['reset'] || $this->options['reset']) {
684 if ($this->do === self::DO_UPDATE && $this->can_reset()) {
685 $this->reset($course);
686 $this->status('coursereset', new lang_string('coursereset', 'tool_uploadcourse'));
690 // Mark context as dirty.
691 $context = context_course::instance($course->id);
692 $context->mark_dirty();
696 * Add the enrolment data for the course.
698 * @param object $course course record.
701 protected function process_enrolment_data($course) {
704 $enrolmentdata = $this->enrolmentdata;
705 if (empty($enrolmentdata)) {
709 $enrolmentplugins = tool_uploadcourse_helper::get_enrolment_plugins();
710 $instances = enrol_get_instances($course->id, false);
711 foreach ($enrolmentdata as $enrolmethod => $method) {
714 foreach ($instances as $i) {
715 if ($i->enrol == $enrolmethod) {
721 $todelete = isset($method['delete']) && $method['delete'];
722 $todisable = isset($method['disable']) && $method['disable'];
723 unset($method['delete']);
724 unset($method['disable']);
726 if (!empty($instance) && $todelete) {
727 // Remove the enrolment method.
728 foreach ($instances as $instance) {
729 if ($instance->enrol == $enrolmethod) {
730 $plugin = $enrolmentplugins[$instance->enrol];
731 $plugin->delete_instance($instance);
735 } else if (!empty($instance) && $todisable) {
736 // Disable the enrolment.
737 foreach ($instances as $instance) {
738 if ($instance->enrol == $enrolmethod) {
739 $plugin = $enrolmentplugins[$instance->enrol];
740 $plugin->update_status($instance, ENROL_INSTANCE_DISABLED);
741 $enrol_updated = true;
747 if (empty($instance)) {
748 $plugin = $enrolmentplugins[$enrolmethod];
749 $instance = new stdClass();
750 $instance->id = $plugin->add_default_instance($course);
751 $instance->roleid = $plugin->get_config('roleid');
752 $instance->status = ENROL_INSTANCE_ENABLED;
754 $plugin = $enrolmentplugins[$instance->enrol];
755 $plugin->update_status($instance, ENROL_INSTANCE_ENABLED);
758 // Now update values.
759 foreach ($method as $k => $v) {
760 $instance->{$k} = $v;
763 // Sort out the start, end and date.
764 $instance->enrolstartdate = (isset($method['startdate']) ? strtotime($method['startdate']) : 0);
765 $instance->enrolenddate = (isset($method['enddate']) ? strtotime($method['enddate']) : 0);
767 // Is the enrolment period set?
768 if (isset($method['enrolperiod']) && ! empty($method['enrolperiod'])) {
769 if (preg_match('/^\d+$/', $method['enrolperiod'])) {
770 $method['enrolperiod'] = (int) $method['enrolperiod'];
772 // Try and convert period to seconds.
773 $method['enrolperiod'] = strtotime('1970-01-01 GMT + ' . $method['enrolperiod']);
775 $instance->enrolperiod = $method['enrolperiod'];
777 if ($instance->enrolstartdate > 0 && isset($method['enrolperiod'])) {
778 $instance->enrolenddate = $instance->enrolstartdate + $method['enrolperiod'];
780 if ($instance->enrolenddate > 0) {
781 $instance->enrolperiod = $instance->enrolenddate - $instance->enrolstartdate;
783 if ($instance->enrolenddate < $instance->enrolstartdate) {
784 $instance->enrolenddate = $instance->enrolstartdate;
787 // Sort out the given role. This does not filter the roles allowed in the course.
788 if (isset($method['role'])) {
789 $roleids = tool_uploadcourse_helper::get_role_ids();
790 if (isset($roleids[$method['role']])) {
791 $instance->roleid = $roleids[$method['role']];
795 $instance->timemodified = time();
796 $DB->update_record('enrol', $instance);
802 * Reset the current course.
804 * This does not reset any of the content of the activities.
806 * @return array status array of array component, item, error.
808 protected function reset($course) {
811 $resetdata = new stdClass();
812 $resetdata->id = $course->id;
813 $resetdata->reset_start_date = time();
814 $resetdata->reset_logs = true;
815 $resetdata->reset_events = true;
816 $resetdata->reset_notes = true;
817 $resetdata->delete_blog_associations = true;
818 $resetdata->reset_completion = true;
819 $resetdata->reset_roles_overrides = true;
820 $resetdata->reset_roles_local = true;
821 $resetdata->reset_groups_members = true;
822 $resetdata->reset_groups_remove = true;
823 $resetdata->reset_groupings_members = true;
824 $resetdata->reset_groupings_remove = true;
825 $resetdata->reset_gradebook_items = true;
826 $resetdata->reset_gradebook_grades = true;
827 $resetdata->reset_comments = true;
829 if (empty($course->startdate)) {
830 $course->startdate = $DB->get_field_select('course', 'startdate', 'id = :id', array('id' => $course->id));
832 $resetdata->reset_start_date_old = $course->startdate;
835 $roles = tool_uploadcourse_helper::get_role_ids();
836 $resetdata->unenrol_users = array_values($roles);
837 $resetdata->unenrol_users[] = 0; // Enrolled without role.
839 return reset_course_userdata($resetdata);
845 * @param string $code status code.
846 * @param lang_string $message status message.
849 protected function status($code, lang_string $message) {
850 if (array_key_exists($code, $this->statuses)) {
851 throw new coding_exception('Status code already defined');
853 $this->statuses[$code] = $message;