Skip to content

Commit

Permalink
MDL-22078 course: add end date
Browse files Browse the repository at this point in the history
  • Loading branch information
danielneis authored and David Monllao committed Oct 3, 2016
1 parent ddd8dc0 commit fbcdb0d
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 13 deletions.
11 changes: 10 additions & 1 deletion admin/tool/uploadcourse/classes/course.php
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 @@ -897,6 +902,10 @@ 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));
}

// Add roles.
$roles = tool_uploadcourse_helper::get_role_ids();
$resetdata->unenrol_users = array_values($roles);
Expand Down
4 changes: 4 additions & 0 deletions admin/tool/uploadcourse/classes/step2_form.php
Expand Up @@ -95,6 +95,10 @@ public function definition () {
$mform->addHelpButton('defaults[startdate]', 'startdate');
$mform->setDefault('defaults[startdate]', time() + 3600 * 24);

$mform->addElement('date_selector', 'defaults[enddate]', get_string('enddate'));
$mform->addHelpButton('defaults[enddate]', 'enddate');
$mform->setDefault('defaults[enddate]', 0);

$courseformats = get_sorted_course_formats(true);
$formcourseformats = array();
foreach ($courseformats as $courseformat) {
Expand Down
1 change: 1 addition & 0 deletions admin/tool/uploadcourse/cli/uploadcourse.php
Expand Up @@ -156,6 +156,7 @@
$defaults = array();
$defaults['category'] = $options['category'];
$defaults['startdate'] = time() + 3600 * 24;
$defaults['enddate'] = 0;
$defaults['newsitems'] = $courseconfig->newsitems;
$defaults['showgrades'] = $courseconfig->showgrades;
$defaults['showreports'] = $courseconfig->showreports;
Expand Down
8 changes: 8 additions & 0 deletions admin/tool/uploadcourse/tests/course_test.php
Expand Up @@ -245,6 +245,7 @@ public function test_data_saved() {
'category' => '1',
'visible' => '0',
'startdate' => '8 June 1990',
'enddate' => '18 June 1990',
'idnumber' => '123abc',
'summary' => 'Summary',
'format' => 'weeks',
Expand Down Expand Up @@ -282,6 +283,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 @@ -334,6 +336,7 @@ public function test_data_saved() {
'category' => $cat->id,
'visible' => '1',
'startdate' => '11 June 1984',
'enddate' => '31 June 1984',
'idnumber' => 'changeidn',
'summary' => 'Summary 2',
'format' => 'topics',
Expand Down Expand Up @@ -370,6 +373,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 +429,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 +457,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 +484,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 +512,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
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
11 changes: 11 additions & 0 deletions backup/moodle2/restore_course_task.class.php
Expand Up @@ -172,6 +172,17 @@ protected function define_settings() {
$startdate->set_ui(new backup_setting_ui_dateselector($startdate, get_string('setting_course_startdate', 'backup')));
$this->add_setting($startdate);

// Old versions may not have this data.
if (isset($this->get_info()->original_course_enddate)) {
$courseenddate = $this->get_info()->original_course_enddate;
} else {
$courseenddate = 0;
}
$enddate = new restore_course_generic_text_setting('course_enddate',
base_setting::IS_INTEGER, $courseenddate);
$enddate->set_ui(new backup_setting_ui_dateselector($enddate, get_string('setting_course_enddate', 'backup')));
$this->add_setting($enddate);

$keep_enrols = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, false);
$keep_enrols->set_ui(new backup_setting_ui_select($keep_enrols, $keep_enrols->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$keep_enrols->get_ui()->set_label(get_string('setting_keep_roles_and_enrolments', 'backup'));
Expand Down
4 changes: 4 additions & 0 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -1824,6 +1824,7 @@ public function process_course($data) {
$fullname = $this->get_setting_value('course_fullname');
$shortname = $this->get_setting_value('course_shortname');
$startdate = $this->get_setting_value('course_startdate');
$enddate = $this->get_setting_value('course_enddate');

// Calculate final course names, to avoid dupes.
list($fullname, $shortname) = restore_dbops::calculate_course_names($this->get_courseid(), $fullname, $shortname);
Expand Down Expand Up @@ -1893,6 +1894,9 @@ public function process_course($data) {
$data->format = 'singleactivity';
$data->activitytype = 'scorm';
}
if (isset($data->enddate)) {
$data->enddate = $this->apply_date_offset($data->enddate);
}

// Course record ready, update it
$DB->update_record('course', $data);
Expand Down
4 changes: 4 additions & 0 deletions backup/util/checks/restore_check.class.php
Expand Up @@ -203,6 +203,10 @@ public static function check_security($restore_controller, $apply) {
$overwritesetting = $restore_controller->get_plan()->get_setting('overwrite_conf');
$overwritesetting->set_status(base_setting::LOCKED_BY_PERMISSION);
}
$datesetting = $restore_controller->get_plan()->get_setting('course_enddate');
if ($datesetting) {
$datesetting->set_status(base_setting::LOCKED_BY_PERMISSION);
}
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion backup/util/dbops/backup_controller_dbops.class.php
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
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
8 changes: 8 additions & 0 deletions course/edit_form.php
Expand Up @@ -125,6 +125,9 @@ function definition() {
$mform->addHelpButton('startdate', 'startdate');
$mform->setDefault('startdate', time() + 3600 * 24);

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

$mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
$mform->addHelpButton('idnumber', 'idnumbercourse');
$mform->setType('idnumber', PARAM_RAW);
Expand Down Expand Up @@ -392,6 +395,11 @@ function validation($data, $files) {
}
}

// Add field validation check for end date must be after start date.
if (($data['enddate'] > 0) && ($data['enddate'] < $data['startdate'])) {
$errors['enddate'] = get_string('enddatebeforstartdate', 'error');
}

$errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));

$courseformat = course_get_format((object)array('format' => $data['format']));
Expand Down
7 changes: 7 additions & 0 deletions course/externallib.php
Expand Up @@ -441,6 +441,7 @@ public static function get_courses($options = array()) {
external_format_text($course->summary, $course->summaryformat, $context->id, 'course', 'summary', 0);
$courseinfo['format'] = $course->format;
$courseinfo['startdate'] = $course->startdate;
$courseinfo['enddate'] = $course->enddate;
if (array_key_exists('numsections', $courseformatoptions)) {
// For backward-compartibility
$courseinfo['numsections'] = $courseformatoptions['numsections'];
Expand Down Expand Up @@ -515,6 +516,8 @@ public static function get_courses_returns() {
'number of recent items appearing on the course page', VALUE_OPTIONAL),
'startdate' => new external_value(PARAM_INT,
'timestamp when the course start'),
'enddate' => new external_value(PARAM_INT,
'timestamp when the course end'),
'numsections' => new external_value(PARAM_INT,
'(deprecated, use courseformatoptions) number of weeks/topics',
VALUE_OPTIONAL),
Expand Down Expand Up @@ -590,6 +593,8 @@ public static function create_courses_parameters() {
VALUE_DEFAULT, $courseconfig->newsitems),
'startdate' => new external_value(PARAM_INT,
'timestamp when the course start', VALUE_OPTIONAL),
'enddate' => new external_value(PARAM_INT,
'timestamp when the course end', VALUE_OPTIONAL),
'numsections' => new external_value(PARAM_INT,
'(deprecated, use courseformatoptions) number of weeks/topics',
VALUE_OPTIONAL),
Expand Down Expand Up @@ -765,6 +770,8 @@ public static function update_courses_parameters() {
'number of recent items appearing on the course page', VALUE_OPTIONAL),
'startdate' => new external_value(PARAM_INT,
'timestamp when the course start', VALUE_OPTIONAL),
'enddate' => new external_value(PARAM_INT,
'timestamp when the course end', VALUE_OPTIONAL),
'numsections' => new external_value(PARAM_INT,
'(deprecated, use courseformatoptions) number of weeks/topics', VALUE_OPTIONAL),
'maxbytes' => new external_value(PARAM_INT,
Expand Down
7 changes: 6 additions & 1 deletion course/tests/externallib_test.php
Expand Up @@ -383,7 +383,8 @@ public function test_create_courses() {
$course2['format'] = 'weeks';
$course2['showgrades'] = 1;
$course2['newsitems'] = 3;
$course2['startdate'] = 1420092000; // 01/01/2015
$course2['startdate'] = 1420092000; // 01/01/2015.
$course2['enddate'] = 1422669600; // 01/31/2015.
$course2['numsections'] = 4;
$course2['maxbytes'] = 100000;
$course2['showreports'] = 1;
Expand Down Expand Up @@ -432,6 +433,7 @@ public function test_create_courses() {
$this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
$this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
$this->assertEquals($courseinfo->startdate, $course2['startdate']);
$this->assertEquals($courseinfo->enddate, $course2['enddate']);
$this->assertEquals($courseinfo->numsections, $course2['numsections']);
$this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
$this->assertEquals($courseinfo->showreports, $course2['showreports']);
Expand Down Expand Up @@ -597,6 +599,7 @@ public function test_get_courses () {
$this->assertEquals($course['showgrades'], $dbcourse->showgrades);
$this->assertEquals($course['newsitems'], $dbcourse->newsitems);
$this->assertEquals($course['startdate'], $dbcourse->startdate);
$this->assertEquals($course['enddate'], $dbcourse->enddate);
$this->assertEquals($course['numsections'], $dbcourse->numsections);
$this->assertEquals($course['maxbytes'], $dbcourse->maxbytes);
$this->assertEquals($course['showreports'], $dbcourse->showreports);
Expand Down Expand Up @@ -1051,6 +1054,7 @@ public function test_update_courses() {
$course2['showgrades'] = 1;
$course2['newsitems'] = 3;
$course2['startdate'] = 1420092000; // 01/01/2015.
$course2['enddate'] = 1422669600; // 01/31/2015.
$course2['numsections'] = 4;
$course2['maxbytes'] = 100000;
$course2['showreports'] = 1;
Expand Down Expand Up @@ -1086,6 +1090,7 @@ public function test_update_courses() {
$this->assertEquals($course2['showgrades'], $courseinfo->showgrades);
$this->assertEquals($course2['newsitems'], $courseinfo->newsitems);
$this->assertEquals($course2['startdate'], $courseinfo->startdate);
$this->assertEquals($course2['enddate'], $courseinfo->enddate);
$this->assertEquals($course2['numsections'], $courseinfo->numsections);
$this->assertEquals($course2['maxbytes'], $courseinfo->maxbytes);
$this->assertEquals($course2['showreports'], $courseinfo->showreports);
Expand Down
1 change: 1 addition & 0 deletions lang/en/backup.php
Expand Up @@ -266,6 +266,7 @@
$string['setting_course_fullname'] = 'Course name';
$string['setting_course_shortname'] = 'Course short name';
$string['setting_course_startdate'] = 'Course start date';
$string['setting_course_enddate'] = 'Course end date';
$string['setting_keep_roles_and_enrolments'] = 'Keep current roles and enrolments';
$string['setting_keep_groups_and_groupings'] = 'Keep current groups and groupings';
$string['showtypes'] = 'Show type options';
Expand Down
1 change: 1 addition & 0 deletions lang/en/error.php
Expand Up @@ -223,6 +223,7 @@
$string['duplicateroleshortname'] = 'There is already a role with this short name!';
$string['duplicateusername'] = 'Duplicate username - skipping record';
$string['emailfail'] = 'Emailing failed';
$string['enddatebeforstartdate'] = 'The end date must be after start date.';
$string['error'] = 'Error occurred';
$string['error_question_answers_missing_in_db'] = 'Failed to find an answer matching "{$a->answer}" in the question_answers database table. This occurred while restoring the question with id {$a->filequestionid} in the backup file, which has been matched to the existing question with id {$a->dbquestionid} in the database.';
$string['errorprocessingarchive'] = 'Error processing archive file';
Expand Down

0 comments on commit fbcdb0d

Please sign in to comment.