Skip to content

Commit

Permalink
Merge branch 'MDL-22078_master' of https://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Oct 5, 2016
2 parents 4f11002 + 8643c57 commit adc77f0
Show file tree
Hide file tree
Showing 34 changed files with 682 additions and 27 deletions.
3 changes: 3 additions & 0 deletions admin/settings/courses.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
$temp->add(new admin_setting_configselect('moodlecourse/coursedisplay', new lang_string('coursedisplay'),
new lang_string('coursedisplay_help'), COURSE_DISPLAY_SINGLEPAGE, $choices));

$temp->add(new admin_setting_configduration('moodlecourse/courseduration', get_string('courseduration'),
get_string('courseduration_desc'), YEARSECS));

// Appearance.
$temp->add(new admin_setting_heading('appearancehdr', new lang_string('appearance'), ''));

Expand Down
28 changes: 27 additions & 1 deletion admin/tool/uploadcourse/classes/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class tool_uploadcourse_course {
protected $updatemode;

/** @var array fields allowed as course data. */
static protected $validfields = array('fullname', 'shortname', 'idnumber', 'category', 'visible', 'startdate',
static protected $validfields = array('fullname', 'shortname', 'idnumber', 'category', 'visible', 'startdate', 'enddate',
'summary', 'format', 'theme', 'lang', 'newsitems', 'showgrades', 'showreports', 'legacyfiles', 'maxbytes',
'groupmode', 'groupmodeforce', 'groupmodeforce', 'enablecompletion');

Expand Down Expand Up @@ -587,6 +587,11 @@ public function prepare() {
$coursedata['startdate'] = strtotime($coursedata['startdate']);
}

// Course end date.
if (!empty($coursedata['enddate'])) {
$coursedata['enddate'] = strtotime($coursedata['enddate']);
}

// Ultimate check mode vs. existence.
switch ($mode) {
case tool_uploadcourse_processor::MODE_CREATE_NEW:
Expand Down Expand Up @@ -636,6 +641,22 @@ public function prepare() {
$this->do = self::DO_CREATE;
}

// Validate course start and end dates.
if ($exists) {
// We also check existing start and end dates if we are updating an existing course.
$existingdata = $DB->get_record('course', array('shortname' => $this->shortname));
if (empty($coursedata['startdate'])) {
$coursedata['startdate'] = $existingdata->startdate;
}
if (empty($coursedata['enddate'])) {
$coursedata['enddate'] = $existingdata->enddate;
}
}
if ($errorcode = course_validate_dates($coursedata)) {
$this->error($errorcode, new lang_string($errorcode, 'error'));
return false;
}

// Add role renaming.
$errors = array();
$rolenames = tool_uploadcourse_helper::get_role_names($this->rawdata, $errors);
Expand Down Expand Up @@ -897,6 +918,11 @@ protected function reset($course) {
}
$resetdata->reset_start_date_old = $course->startdate;

if (empty($course->enddate)) {
$course->enddate = $DB->get_field_select('course', 'enddate', 'id = :id', array('id' => $course->id));
}
$resetdata->reset_end_date_old = $course->enddate;

// Add roles.
$roles = tool_uploadcourse_helper::get_role_ids();
$resetdata->unenrol_users = array_values($roles);
Expand Down
38 changes: 38 additions & 0 deletions admin/tool/uploadcourse/classes/step2_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/course/lib.php');

/**
* Specify course upload details.
*
Expand Down Expand Up @@ -95,6 +97,9 @@ public function definition () {
$mform->addHelpButton('defaults[startdate]', 'startdate');
$mform->setDefault('defaults[startdate]', time() + 3600 * 24);

$mform->addElement('date_selector', 'defaults[enddate]', get_string('enddate'), array('optional' => true));
$mform->addHelpButton('defaults[enddate]', 'enddate');

$courseformats = get_sorted_course_formats(true);
$formcourseformats = array();
foreach ($courseformats as $courseformat) {
Expand Down Expand Up @@ -190,4 +195,37 @@ public function add_action_buttons($cancel = true, $submitlabel = null) {
$mform->closeHeaderBefore('buttonar');
}

/**
* Sets the enddate default after set_data is called.
*/
public function definition_after_data() {

$mform = $this->_form;

// The default end date depends on the course format.
$format = course_get_format((object)array('format' => get_config('moodlecourse', 'format')));

$enddate = $format->get_default_course_enddate($mform, array('startdate' => 'defaults[startdate]'));
// We add 1 day like we do above in startdate.
$mform->setDefault('defaults[enddate]', $enddate + 3600 * 24);
}

/**
* Validation.
*
* @param array $data
* @param array $files
* @return array the errors that were found
*/
public function validation($data, $files) {
global $DB;

$errors = parent::validation($data, $files);

if ($errorcode = course_validate_dates($data['defaults'])) {
$errors['defaults[enddate]'] = get_string($errorcode, 'error');
}

return $errors;
}
}
1 change: 1 addition & 0 deletions admin/tool/uploadcourse/cli/uploadcourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
$defaults = array();
$defaults['category'] = $options['category'];
$defaults['startdate'] = time() + 3600 * 24;
$defaults['enddate'] = $defaults['startdate'] + intval(get_config('moodlecourse', 'courseduration'));
$defaults['newsitems'] = $courseconfig->newsitems;
$defaults['showgrades'] = $courseconfig->showgrades;
$defaults['showreports'] = $courseconfig->showreports;
Expand Down
10 changes: 9 additions & 1 deletion admin/tool/uploadcourse/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@
}

} else {
$processor = new tool_uploadcourse_processor($cir, $form1data->options, array());
if (!empty($form1data)) {
$options = $form1data->options;
} else if ($submitteddata = $mform2->get_submitted_data()) {
$options = (array)$submitteddata->options;
} else {
// Weird but we still need to provide a value, setting the default step1_form one.
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
}
$processor = new tool_uploadcourse_processor($cir, $options, array());
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('uploadcoursespreview', 'tool_uploadcourse'));
$processor->preview($previewrows, new tool_uploadcourse_tracker(tool_uploadcourse_tracker::OUTPUT_HTML));
Expand Down
37 changes: 35 additions & 2 deletions admin/tool/uploadcourse/tests/course_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ public function test_data_saved() {
'fullname' => 'Fullname',
'category' => '1',
'visible' => '0',
'startdate' => '8 June 1990',
'idnumber' => '123abc',
'summary' => 'Summary',
'format' => 'weeks',
Expand All @@ -270,6 +269,20 @@ public function test_data_saved() {
'enrolment_3_disable' => '1',
);

// There should be a start date if there is a end date.
$data['enddate'] = '7 June 1990';
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertFalse($co->prepare());
$this->assertArrayHasKey('nostartdatenoenddate', $co->get_errors());

$data['startdate'] = '8 June 1990';
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertFalse($co->prepare());
$this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());

// They are correct now.
$data['enddate'] = '18 June 1990';

$this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertTrue($co->prepare());
Expand All @@ -282,6 +295,7 @@ public function test_data_saved() {
$this->assertEquals($data['category'], $course->category);
$this->assertEquals($data['visible'], $course->visible);
$this->assertEquals(mktime(0, 0, 0, 6, 8, 1990), $course->startdate);
$this->assertEquals(mktime(0, 0, 0, 6, 18, 1990), $course->enddate);
$this->assertEquals($data['idnumber'], $course->idnumber);
$this->assertEquals($data['summary'], $course->summary);
$this->assertEquals($data['format'], $course->format);
Expand Down Expand Up @@ -333,7 +347,6 @@ public function test_data_saved() {
'fullname' => 'Fullname 2',
'category' => $cat->id,
'visible' => '1',
'startdate' => '11 June 1984',
'idnumber' => 'changeidn',
'summary' => 'Summary 2',
'format' => 'topics',
Expand All @@ -360,6 +373,21 @@ public function test_data_saved() {
);

$this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));

$data['enddate'] = '31 June 1984';
// Previous start and end dates are 8 and 18 June 1990.
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertFalse($co->prepare());
$this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());

$data['startdate'] = '19 June 1990';
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertFalse($co->prepare());
$this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());

// They are correct now.
$data['startdate'] = '11 June 1984';

$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertTrue($co->prepare());
$co->proceed();
Expand All @@ -370,6 +398,7 @@ public function test_data_saved() {
$this->assertEquals($data['category'], $course->category);
$this->assertEquals($data['visible'], $course->visible);
$this->assertEquals(mktime(0, 0, 0, 6, 11, 1984), $course->startdate);
$this->assertEquals(mktime(0, 0, 0, 6, 31, 1984), $course->enddate);
$this->assertEquals($data['idnumber'], $course->idnumber);
$this->assertEquals($data['summary'], $course->summary);
$this->assertEquals($data['format'], $course->format);
Expand Down Expand Up @@ -425,6 +454,7 @@ public function test_default_data_saved() {
'category' => '1',
'visible' => '0',
'startdate' => 644803200,
'enddate' => 645667200,
'idnumber' => '123abc',
'summary' => 'Summary',
'format' => 'weeks',
Expand Down Expand Up @@ -452,6 +482,7 @@ public function test_default_data_saved() {
$this->assertEquals($defaultdata['category'], $course->category);
$this->assertEquals($defaultdata['visible'], $course->visible);
$this->assertEquals($defaultdata['startdate'], $course->startdate);
$this->assertEquals($defaultdata['enddate'], $course->enddate);
$this->assertEquals($defaultdata['idnumber'], $course->idnumber);
$this->assertEquals($defaultdata['summary'], $course->summary);
$this->assertEquals($defaultdata['format'], $course->format);
Expand All @@ -478,6 +509,7 @@ public function test_default_data_saved() {
'category' => $cat->id,
'visible' => '1',
'startdate' => 455760000,
'enddate' => 457488000,
'idnumber' => 'changedid',
'summary' => 'Summary 2',
'format' => 'topics',
Expand Down Expand Up @@ -505,6 +537,7 @@ public function test_default_data_saved() {
$this->assertEquals($defaultdata['category'], $course->category);
$this->assertEquals($defaultdata['visible'], $course->visible);
$this->assertEquals($defaultdata['startdate'], $course->startdate);
$this->assertEquals($defaultdata['enddate'], $course->enddate);
$this->assertEquals($defaultdata['idnumber'], $course->idnumber);
$this->assertEquals($defaultdata['summary'], $course->summary);
$this->assertEquals($defaultdata['format'], $course->format);
Expand Down
5 changes: 3 additions & 2 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ protected function define_structure() {
$course = new backup_nested_element('course', array('id', 'contextid'), array(
'shortname', 'fullname', 'idnumber',
'summary', 'summaryformat', 'format', 'showgrades',
'newsitems', 'startdate',
'newsitems', 'startdate', 'enddate',
'marker', 'maxbytes', 'legacyfiles', 'showreports',
'visible', 'groupmode', 'groupmodeforce',
'defaultgroupingid', 'lang', 'theme',
Expand Down Expand Up @@ -1780,6 +1780,7 @@ protected function define_structure() {
$info['original_course_fullname'] = $originalcourseinfo->fullname;
$info['original_course_shortname'] = $originalcourseinfo->shortname;
$info['original_course_startdate'] = $originalcourseinfo->startdate;
$info['original_course_enddate'] = $originalcourseinfo->enddate;
$info['original_course_contextid'] = context_course::instance($this->get_courseid())->id;
$info['original_system_contextid'] = context_system::instance()->id;

Expand All @@ -1795,7 +1796,7 @@ protected function define_structure() {
'name', 'moodle_version', 'moodle_release', 'backup_version',
'backup_release', 'backup_date', 'mnet_remoteusers', 'include_files', 'include_file_references_to_external_content', 'original_wwwroot',
'original_site_identifier_hash', 'original_course_id', 'original_course_format',
'original_course_fullname', 'original_course_shortname', 'original_course_startdate',
'original_course_fullname', 'original_course_shortname', 'original_course_startdate', 'original_course_enddate',
'original_course_contextid', 'original_system_contextid'));

$details = new backup_nested_element('details');
Expand Down
4 changes: 4 additions & 0 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,10 @@ public function process_course($data) {
$this->legacyrestrictmodules = !empty($data->restrictmodules);

$data->startdate= $this->apply_date_offset($data->startdate);
if (isset($data->enddate)) {
$data->enddate = $this->apply_date_offset($data->enddate);
}

if ($data->defaultgroupingid) {
$data->defaultgroupingid = $this->get_mappingid('grouping', $data->defaultgroupingid);
}
Expand Down
7 changes: 6 additions & 1 deletion backup/moodle2/tests/moodle2_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ public function test_restore_dates() {
// Create a course with specific start date.
$generator = $this->getDataGenerator();
$course = $generator->create_course(array(
'startdate' => strtotime('1 Jan 2014 00:00 GMT')));
'startdate' => strtotime('1 Jan 2014 00:00 GMT'),
'enddate' => strtotime('3 Aug 2014 00:00 GMT')
));

// Add a forum with conditional availability date restriction, including
// one of them nested inside a tree.
Expand All @@ -361,6 +363,9 @@ public function test_restore_dates() {
// Do backup and restore.
$newcourseid = $this->backup_and_restore($course, strtotime('3 Jan 2015 00:00 GMT'));

$newcourse = $DB->get_record('course', array('id' => $newcourseid));
$this->assertEquals(strtotime('5 Aug 2015 00:00 GMT'), $newcourse->enddate);

$modinfo = get_fast_modinfo($newcourseid);

// Check forum dates are modified by the same amount as the course start.
Expand Down
6 changes: 3 additions & 3 deletions backup/util/checks/restore_check.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ public static function check_security($restore_controller, $apply) {
// settings so that they cannot change it.
$hasrolldatescap = has_capability('moodle/restore:rolldates', $coursectx, $userid);
if (!$hasrolldatescap) {
$datesetting = $restore_controller->get_plan()->get_setting('course_startdate');
if ($datesetting) {
$datesetting->set_status(base_setting::LOCKED_BY_PERMISSION);
$startdatesetting = $restore_controller->get_plan()->get_setting('course_startdate');
if ($startdatesetting) {
$startdatesetting->set_status(base_setting::LOCKED_BY_PERMISSION);
}
}

Expand Down
2 changes: 1 addition & 1 deletion backup/util/dbops/backup_controller_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ public static function backup_includes_file_references($backupid) {
*/
public static function backup_get_original_course_info($courseid) {
global $DB;
return $DB->get_record('course', array('id' => $courseid), 'fullname, shortname, startdate, format');
return $DB->get_record('course', array('id' => $courseid), 'fullname, shortname, startdate, enddate, format');
}

/**
Expand Down
16 changes: 10 additions & 6 deletions backup/util/helper/backup_general_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ public static function get_backup_information($tempdir) {
$info->mnet_remoteusers = $infoarr['mnet_remoteusers'];
$info->original_wwwroot = $infoarr['original_wwwroot'];
$info->original_site_identifier_hash = $infoarr['original_site_identifier_hash'];
$info->original_course_id = $infoarr['original_course_id'];
$info->original_course_fullname = $infoarr['original_course_fullname'];
$info->original_course_shortname= $infoarr['original_course_shortname'];
$info->original_course_startdate= $infoarr['original_course_startdate'];
$info->original_course_contextid= $infoarr['original_course_contextid'];
$info->original_system_contextid= $infoarr['original_system_contextid'];
$info->original_course_id = $infoarr['original_course_id'];
$info->original_course_fullname = $infoarr['original_course_fullname'];
$info->original_course_shortname = $infoarr['original_course_shortname'];
$info->original_course_startdate = $infoarr['original_course_startdate'];
// Old versions may not have this.
if (isset($infoarr['original_course_enddate'])) {
$info->original_course_enddate = $infoarr['original_course_enddate'];
}
$info->original_course_contextid = $infoarr['original_course_contextid'];
$info->original_system_contextid = $infoarr['original_system_contextid'];
// Moodle backup file don't have this option before 2.3
if (!empty($infoarr['include_file_references_to_external_content'])) {
$info->include_file_references_to_external_content = 1;
Expand Down
Loading

0 comments on commit adc77f0

Please sign in to comment.