Skip to content

Commit

Permalink
Merge branch 'wip-MDL-35260-master' of git://github.com/marinaglancy/…
Browse files Browse the repository at this point in the history
…moodle
  • Loading branch information
danpoltawski committed Nov 12, 2012
2 parents 05f2bde + 3776335 commit 3c9b489
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 13 deletions.
131 changes: 131 additions & 0 deletions admin/courseformats.php
@@ -0,0 +1,131 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Allows the admin to enable, disable and uninstall course formats
*
* @package core_admin
* @copyright 2012 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/pluginlib.php');

$action = required_param('action', PARAM_ALPHANUMEXT);
$formatname = required_param('format', PARAM_PLUGIN);
$confirm = optional_param('confirm', 0, PARAM_BOOL);

$syscontext = context_system::instance();
$PAGE->set_url('/admin/courseformats.php');
$PAGE->set_context($syscontext);

require_login();
require_capability('moodle/site:config', $syscontext);
require_sesskey();

$return = new moodle_url('/admin/settings.php', array('section' => 'manageformats'));

$allplugins = plugin_manager::instance()->get_plugins();
$formatplugins = $allplugins['format'];
$sortorder = array_flip(array_keys($formatplugins));

if (!isset($formatplugins[$formatname])) {
print_error('courseformatnotfound', 'error', $return, $formatname);
}

switch ($action) {
case 'disable':
if ($formatplugins[$formatname]->is_enabled()) {
if (get_config('moodlecourse', 'format') === $formatname) {
print_error('cannotdisableformat', 'error', $return);
}
set_config('disabled', 1, 'format_'. $formatname);
}
break;
case 'enable':
if (!$formatplugins[$formatname]->is_enabled()) {
unset_config('disabled', 'format_'. $formatname);
}
break;
case 'up':
if ($sortorder[$formatname]) {
$currentindex = $sortorder[$formatname];
$seq = array_keys($formatplugins);
$seq[$currentindex] = $seq[$currentindex-1];
$seq[$currentindex-1] = $formatname;
set_config('format_plugins_sortorder', implode(',', $seq));
}
break;
case 'down':
if ($sortorder[$formatname] < count($sortorder)-1) {
$currentindex = $sortorder[$formatname];
$seq = array_keys($formatplugins);
$seq[$currentindex] = $seq[$currentindex+1];
$seq[$currentindex+1] = $formatname;
set_config('format_plugins_sortorder', implode(',', $seq));
}
break;
case 'uninstall':
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('courseformats', 'moodle'));

$coursecount = $DB->count_records('course', array('format' => $formatname));
if ($coursecount) {
// Check that default format is set. It will be used to convert courses
// using this format
$defaultformat = get_config('moodlecourse', 'format');
$defaultformat = $formatplugins[get_config('moodlecourse', 'format')];
if (!$defaultformat) {
echo $OUTPUT->error_text(get_string('defaultformatnotset', 'admin'));
echo $OUTPUT->footer();
exit;
}
}

$format = $formatplugins[$formatname];
$deleteurl = $format->get_uninstall_url();
if (!$deleteurl) {
// somebody was trying to cheat and type non-existing link
echo $OUTPUT->error_text(get_string('cannotuninstall', 'admin', $format->displayname));
echo $OUTPUT->footer();
exit;
}

if (!$confirm) {
if ($coursecount) {
$message = get_string('formatuninstallwithcourses', 'admin',
(object)array('count' => $coursecount, 'format' => $format->displayname,
'defaultformat' => $defaultformat->displayname));
} else {
$message = get_string('formatuninstallconfirm', 'admin', $format->displayname);
}
$deleteurl->param('confirm', 1);
echo $OUTPUT->confirm($message, $deleteurl, $return);
} else {
$a = new stdClass();
$a->plugin = $format->displayname;
$a->directory = $format->rootdir;
uninstall_plugin('format', $formatname);
echo $OUTPUT->notification(get_string('formatuninstalled', 'admin', $a), 'notifysuccess');
echo $OUTPUT->continue_button($return);
}

echo $OUTPUT->footer();
exit;
}
redirect($return);
5 changes: 3 additions & 2 deletions admin/settings/courses.php
Expand Up @@ -15,9 +15,10 @@
/// NOTE: these settings must be applied after all other settings because they depend on them
///main course settings
$temp = new admin_settingpage('coursesettings', new lang_string('coursesettings'));
$courseformats = get_plugin_list('format');
require_once($CFG->dirroot.'/course/lib.php');
$courseformats = get_sorted_course_formats(true);
$formcourseformats = array();
foreach ($courseformats as $courseformat => $courseformatdir) {
foreach ($courseformats as $courseformat) {
$formcourseformats[$courseformat] = new lang_string('pluginname', "format_$courseformat");
}
$temp->add(new admin_setting_configselect('moodlecourse/format', new lang_string('format'), new lang_string('coursehelpformat'), 'weeks',$formcourseformats));
Expand Down
9 changes: 9 additions & 0 deletions admin/settings/plugins.php
Expand Up @@ -20,6 +20,15 @@
// hidden script for converting journals to online assignments (or something like that) linked from elsewhere
$ADMIN->add('modsettings', new admin_externalpage('oacleanup', 'Online Assignment Cleanup', $CFG->wwwroot.'/'.$CFG->admin.'/oacleanup.php', 'moodle/site:config', true));

// course formats
$ADMIN->add('modules', new admin_category('formatsettings', new lang_string('courseformats')));
$temp = new admin_settingpage('manageformats', new lang_string('manageformats', 'core_admin'));
$temp->add(new admin_setting_manageformats());
$ADMIN->add('formatsettings', $temp);
foreach ($allplugins['format'] as $format) {
$format->load_settings($ADMIN, 'formatsettings', $hassiteconfig);
}

// blocks
$ADMIN->add('modules', new admin_category('blocksettings', new lang_string('blocks')));
$ADMIN->add('blocksettings', new admin_page_manageblocks());
Expand Down
13 changes: 11 additions & 2 deletions course/edit_form.php
Expand Up @@ -113,11 +113,20 @@ function definition() {
$mform->hardFreeze('summary_editor');
}

$courseformats = get_plugin_list('format');
$courseformats = get_sorted_course_formats(true);
$formcourseformats = array();
foreach ($courseformats as $courseformat => $formatdir) {
foreach ($courseformats as $courseformat) {
$formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
}
if (isset($course->format)) {
$course->format = course_get_format($course)->get_format(); // replace with default if not found
if (!in_array($course->format, $courseformats)) {
// this format is disabled. Still display it in the dropdown
$formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
get_string('pluginname', 'format_'.$course->format));
}
}

$mform->addElement('select', 'format', get_string('format'), $formcourseformats);
$mform->addHelpButton('format', 'format');
$mform->setDefault('format', $courseconfig->format);
Expand Down
18 changes: 15 additions & 3 deletions course/format/formatlegacy.php
Expand Up @@ -339,9 +339,21 @@ public function update_course_format_options($data, $oldcourse = null) {
if ($oldcourse !== null) {
$data = (array)$data;
$oldcourse = (array)$oldcourse;
foreach ($this->course_format_options() as $key => $unused) {
if (array_key_exists($key, $oldcourse) && !array_key_exists($key, $data)) {
$data[$key] = $oldcourse[$key];
$options = $this->course_format_options();
foreach ($options as $key => $unused) {
if (!array_key_exists($key, $data)) {
if (array_key_exists($key, $oldcourse)) {
$data[$key] = $oldcourse[$key];
} else if ($key === 'numsections') {
// If previous format does not have the field 'numsections' and this one does,
// and $data['numsections'] is not set fill it with the maximum section number from the DB
$maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
WHERE course = ?', array($this->courseid));
if ($maxsection) {
// If there are no sections, or just default 0-section, 'numsections' will be set to default
$data['numsections'] = $maxsection;
}
}
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions course/format/lib.php
Expand Up @@ -97,13 +97,21 @@ protected static final function get_format_or_default($format) {
if ($format === 'site') {
return $format;
}
$plugins = get_plugin_list('format'); // TODO MDL-35260 filter only enabled
if (isset($plugins[$format])) {
$plugins = get_sorted_course_formats();
if (in_array($format, $plugins)) {
return $format;
}
// Else return default format
$defaultformat = reset($plugins); // TODO MDL-35260 get default format from config
debugging('Format plugin format_'.$format.' is not found or is not enabled. Using default format_'.$defaultformat, DEBUG_DEVELOPER);
$defaultformat = get_config('moodlecourse', 'format');
if (!in_array($defaultformat, $plugins)) {
// when default format is not set correctly, use the first available format
$defaultformat = reset($plugins);
}
static $warningprinted = array();
if (empty($warningprinted[$format])) {
debugging('Format plugin format_'.$format.' is not found. Using default format_'.$defaultformat, DEBUG_DEVELOPER);
$warningprinted[$format] = true;
}
return $defaultformat;
}

Expand Down
1 change: 1 addition & 0 deletions course/format/upgrade.txt
Expand Up @@ -12,6 +12,7 @@ format.
functions callback_XXXX_request_key() are no longer used (where XXXX is the course format name)
* functions get_generic_section_name(), get_all_sections(), add_mod_to_section(), get_all_mods()
are deprecated. See their phpdocs in lib/deprecatedlib.php on how to replace them
* Course formats may now have their settings.php file as the most of other plugin types

=== 2.3 ===

Expand Down
29 changes: 29 additions & 0 deletions course/lib.php
Expand Up @@ -4512,6 +4512,35 @@ function include_course_ajax($course, $usedmodules = array(), $enabledmodules =
return true;
}

/**
* Returns the sorted list of available course formats, filtered by enabled if necessary
*
* @param bool $enabledonly return only formats that are enabled
* @return array array of sorted format names
*/
function get_sorted_course_formats($enabledonly = false) {
global $CFG;
$formats = get_plugin_list('format');

if (!empty($CFG->format_plugins_sortorder)) {
$order = explode(',', $CFG->format_plugins_sortorder);
$order = array_merge(array_intersect($order, array_keys($formats)),
array_diff(array_keys($formats), $order));
} else {
$order = array_keys($formats);
}
if (!$enabledonly) {
return $order;
}
$sortedformats = array();
foreach ($order as $formatname) {
if (!get_config('format_'.$formatname, 'disabled')) {
$sortedformats[] = $formatname;
}
}
return $sortedformats;
}

/**
* The URL to use for the specified course (with section)
*
Expand Down
7 changes: 7 additions & 0 deletions lang/en/admin.php
Expand Up @@ -95,6 +95,7 @@
$string['calendarsettings'] = 'Calendar';
$string['calendar_weekend'] = 'Weekend days';
$string['cannotdeletemodfilter'] = 'You cannot uninstall the \'{$a->filter}\' because it is part of the \'{$a->module}\' module.';
$string['cannotuninstall'] = '{$a} can not be uninstalled.';
$string['cfgwwwrootslashwarning'] = 'You have defined $CFG->wwwroot incorrectly in your config.php file. You have included a \'/\' character at the end. Please remove it, or you will experience strange bugs like <a href=\'http://tracker.moodle.org/browse/MDL-11061\'>MDL-11061</a>.';
$string['cfgwwwrootwarning'] = 'You have defined $CFG->wwwroot incorrectly in your config.php file. It does not match the URL you are using to access this page. Please correct it, or you will experience strange bugs like <a href=\'http://tracker.moodle.org/browse/MDL-11061\'>MDL-11061</a>.';
$string['clamfailureonupload'] = 'On clam AV failure';
Expand Down Expand Up @@ -417,6 +418,7 @@
$string['debugvalidators'] = 'Show validator links';
$string['defaultcity'] = 'Default city';
$string['defaultcity_help'] = 'A city entered here will be the default city when creating new user accounts.';
$string['defaultformatnotset'] = 'Error determining default course format. Please check site settings.';
$string['defaulthomepage'] = 'Default home page for users';
$string['defaultrequestcategory'] = 'Default category for course requests';
$string['defaultsettinginfo'] = 'Default: {$a}';
Expand Down Expand Up @@ -533,6 +535,9 @@
$string['forceloginforprofileimage_help'] = 'If enabled, users must login in order to view user profile pictures and the default user picture will be used in all notification emails.';
$string['forceloginforprofiles'] = 'Force users to login for profiles';
$string['forcetimezone'] = 'Force default timezone';
$string['formatuninstallwithcourses'] = 'There are {$a->count} courses using {$a->format}. Their format will be changed to {$a->defaultformat} (default format for this site). Some format-specific data may be lost. Are you sure you want to proceed?';
$string['formatuninstallconfirm'] = '{$a} will be uninstalled. No courses currently use it. Continue?';
$string['formatuninstalled'] = 'All data associated with the format plugin \'{$a->plugin}\' has been deleted from the database. To complete the deletion (and prevent the plugin re-installing itself), you should now delete this directory from your server: {$a->directory}';
$string['frontpage'] = 'Front page';
$string['frontpagebackup'] = 'Front page backup';
$string['frontpagedefaultrole'] = 'Default frontpage role';
Expand Down Expand Up @@ -641,6 +646,8 @@
$string['maintenancemode'] = 'In maintenance mode';
$string['maintfileopenerror'] = 'Error opening maintenance files!';
$string['maintinprogress'] = 'Maintenance is in progress...';
$string['manageformats'] = 'Manage course formats';
$string['manageformatsgotosettings'] = 'Default format can be changed in {$a}';
$string['managelang'] = 'Manage';
$string['managelicenses'] = 'Manage licences';
$string['manageqbehaviours'] = 'Manage question behaviours';
Expand Down
1 change: 1 addition & 0 deletions lang/en/error.php
Expand Up @@ -65,6 +65,7 @@
$string['cannotdeleterole'] = 'It cannot be deleted, because {$a}';
$string['cannotdeleterolewithid'] = 'Could not delete role with ID {$a}';
$string['cannotdeletethisrole'] = 'You cannot delete this role because it is used by the system, or because it is the last role with administrator capabilities.';
$string['cannotdisableformat'] = 'You can not disable the default format';
$string['cannotdownloadcomponents'] = 'Cannot download components';
$string['cannotdownloadlanguageupdatelist'] = 'Cannot download list of language updates from download.moodle.org';
$string['cannotdownloadzipfile'] = 'Cannot download ZIP file';
Expand Down
3 changes: 2 additions & 1 deletion lang/en/moodle.php
Expand Up @@ -297,7 +297,7 @@
$string['coursecreators'] = 'Course creator';
$string['coursecreatorsdescription'] = 'Course creators can create new courses.';
$string['coursedisplay'] = 'Course layout';
$string['coursedisplay_help'] = 'This setting determines whether the whole course is displayed on one page or split over several pages. The setting has no affect on certain course formats, such as SCORM format.';
$string['coursedisplay_help'] = 'This setting determines whether the whole course is displayed on one page or split over several pages.';
$string['coursedisplay_single'] = 'Show all sections on one page';
$string['coursedisplay_multi'] = 'Show one section per page';
$string['coursedeleted'] = 'Deleted course {$a}';
Expand Down Expand Up @@ -1805,6 +1805,7 @@
$string['whattodo'] = 'What to do';
$string['windowclosing'] = 'This window should close automatically. If not, please close it now.';
$string['withchosenfiles'] = 'With chosen files';
$string['withdisablednote'] = '{$a} (disabled)';
$string['withoutuserdata'] = 'without user data';
$string['withselectedusers'] = 'With selected users...';
$string['withselectedusers_help'] = '* Send message - For sending a message to one or more participants
Expand Down

0 comments on commit 3c9b489

Please sign in to comment.