+++ /dev/null
-<?php
- // Make sure all variables are defined
- if (!isset($form->showreports)) {
- $form->showreports = 0;
- }
- if (!isset($form->maxbytes)) {
- $form->maxbytes = 0;
- }
- if (!isset($form->hiddensections)) {
- $form->hiddensections = 0;
- }
- if (!isset($form->lang)) {
- $form->lang = '';
- }
- if (!isset($form->theme)) {
- $form->theme = '';
- }
- if (!isset($form->enrol)) {
- $form->enrol = '';
- }
- if (!isset($form->enrollable)) {
- $form->enrollable = 1;
- }
- if (!isset($form->enrolstartdate)) {
- $form->enrolstartdate = 0;
- }
- if (!$form->enrolstartdate) {
- $form->enrolstartdisabled = 1;
- }
- if (!isset($form->enrolenddate)) {
- $form->enrolenddate = 0;
- }
- if (!$form->enrolenddate) {
- $form->enrolenddisabled = 1;
- }
- if (!isset($form->enrolperiod)) {
- $form->enrolperiod = 0;
- }
- if (!isset($form->expirynotify)) {
- $form->expirynotify = 0;
- }
- if (!isset($form->notifystudents)) {
- $form->notifystudents = 0;
- }
- if (!isset($form->expirythreshold)) {
- $form->expirythreshold = 10 * 86400;
- }
- if (!isset($form->metacourse)) {
- $form->metacourse = 0;
- }
- if(!isset($form->restrictmodules)) {
- $form->restrictmodules = 0;
- }
- if(!isset($form->defaultrole)) {
- $form->defaultrole = 0; // Use site default
- }
-?>
-<form method="post" action="edit.php" name="form">
-<table cellpadding="9" cellspacing="0" >
-<?php if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $form->category))) { ?>
-<tr valign="top">
- <td align="right"><?php print_string("category") ?>:</td>
- <td><?php
- $displaylist = array();
- $parentlist = array();
- make_categories_list($displaylist, $parentlist);
- choose_from_menu($displaylist, "category", "$form->category", "");
- helpbutton("coursecategory", get_string("category"));
- ?>
- </td>
-</tr>
-<?php } ?>
-<tr valign="top">
- <td align="right"><?php print_string("fullname") ?>:</td>
- <td><input type="text" name="fullname" maxlength="254" size="50" value="<?php p($form->fullname) ?>" alt="<?php print_string("fullname") ?>" />
- <?php helpbutton("coursefullname", get_string("fullname")) ?>
- <?php if (isset($err["fullname"])) formerr($err["fullname"]); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("shortname") ?>:</td>
- <td><input type="text" name="shortname" maxlength="15" size="10" value="<?php p($form->shortname) ?>" alt="<?php print_string("shortname") ?>" />
- <?php helpbutton("courseshortname", get_string("shortname")) ?>
- <?php if (isset($err["shortname"])) formerr($err["shortname"]); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("idnumbercourse") ?>:</td>
- <td><input type="text" name="idnumber" maxlength="100" size="10" value="<?php p($form->idnumber) ?>" alt="><?php print_string("idnumbercourse") ?>" />
- <?php helpbutton("courseidnumber", get_string("idnumbercourse")) ?>
- <?php if (isset($err["idnumber"])) formerr($err["idnumber"]); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("summary") ?>:</td>
- <td><?php
- print_textarea($usehtmleditor, 10, 50, 660, 200, "summary", $form->summary);
- helpbutton("text", get_string("helptext"));
- if (isset($err["summary"])) formerr($err["summary"]);
- ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("format") ?>:</td>
- <td><?php
- choose_from_menu ($form->courseformats, "format", "$form->format", "");
- helpbutton("courseformats", get_string("courseformats"));
- ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("startdate") ?>:</td>
- <td><?php
- print_date_selector("startday", "startmonth", "startyear", $form->startdate);
- helpbutton("coursestartdate", get_string("startdate"));
- ?></td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("enrolmentplugins") ?>:</td>
- <td><?php
- unset($choices);
- $modules = explode(',', $CFG->enrol_plugins_enabled);
- foreach ($modules as $module) {
- $name = get_string("enrolname", "enrol_$module");
- $plugin = enrolment_factory::factory($module);
- if (method_exists($plugin, 'print_entry')) {
- $choices[$name] = $module;
- }
- }
- asort($choices);
- $choices = array_flip($choices);
- $choices = array('' => get_string('sitedefault').' ('.get_string("enrolname", "enrol_$CFG->enrol").')') + $choices;
- choose_from_menu ($choices, "enrol", "$form->enrol", "");
- helpbutton("courseenrolmentplugins", get_string("enrolmentplugins"));
- ?></td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string('defaultrole', 'role') ?>:</td>
- <td><?php
- $roles = get_assignable_roles($context);
- asort($roles);
- $choices = array();
-
- if ($form->defaultrole && !isset($roles[$form->defaultrole])) { // Existing setting is one we can't choose
- if ($coursedefaultrole = get_record('role', 'id', $form->defaultrole)) {
- $choices[-1] = get_string('currentrole', 'role').' ('.$coursedefaultrole->name.')';
- } else {
- $choices[-1] = get_string('currentrole', 'role');
- }
- }
-
- if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
- $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
- } else {
- $choices[0] = get_string('sitedefault');
- }
-
- $choices = $choices + $roles;
-
- choose_from_menu ($choices, 'defaultrole', $form->defaultrole, '');
- helpbutton("coursedefaultrole", get_string("defaultrole"));
- ?></td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("enrollable") ?>:</td>
- <td><?php
- unset($choices);
- $choices["0"] = get_string("no");
- $choices["1"] = get_string("yes");
- $choices["2"] = get_string("enroldate");
- choose_from_radio ($choices, "enrollable", "$form->enrollable", "");
- helpbutton("courseenrollable", get_string("enrollable"));
- ?></td>
-</tr>
-<tr valign="top" id="daterange">
- <td align="right"><?php print_string("enroldate") ?>:</td>
- <td>
- <table cellpadding="0" cellspacing="0">
- <tr>
- <td align="right"><?php print_string('enrolstartdate') ?>: </td>
- <td><?php
- print_date_selector("enrolstartday", "enrolstartmonth", "enrolstartyear", $form->enrolstartdate);
- print_string('disable');
- echo ': ';
- print_checkbox('enrolstartdisabled', 1, "$form->enrolstartdisabled");
- ?></td>
- </tr>
- <tr>
- <td height="18" colspan="2"> </td>
- </tr>
- <tr>
- <td align="right"><?php print_string('enrolenddate') ?>: </td>
- <td><?php
- print_date_selector("enrolendday", "enrolendmonth", "enrolendyear", $form->enrolenddate);
- print_string('disable');
- echo ': ';
- print_checkbox('enrolenddisabled', 1, "$form->enrolenddisabled");
- if (isset($err["enroldate"])) formerr($err["enroldate"]);
- ?></td>
- </tr>
- </table>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("enrolperiod") ?>:</td>
- <td>
- <?php
- $periodmenu[0] = get_string('unlimited');
- for ($i=1; $i<=365; $i++) {
- $seconds = $i * 86400;
- $periodmenu[$seconds] = get_string('numdays', '', $i);
- }
- choose_from_menu ($periodmenu, "enrolperiod", "$form->enrolperiod", "");
- helpbutton("enrolperiod", get_string("enrolperiod"));
- ?>
- </td>
-</tr><tr valign="top">
- <td align="right"><?php print_string("expirynotify") ?>:</td>
- <td>
- <?php
- unset($choices);
- $choices["0"] = get_string("no");
- $choices["1"] = get_string("yes");
- choose_from_menu ($choices, "expirynotify", "$form->expirynotify", "");
- helpbutton("expirynotify", get_string("expirynotify"));
-
- echo ' ';
- print_string('expirynotifystudents');
- echo ': ';
- choose_from_menu ($choices, "notifystudents", "$form->notifystudents", "");
- helpbutton("expirynotifystudents", get_string("expirynotifystudents"));
-
- echo ' ';
- print_string('expirythreshold');
- echo ': ';
- for ($i=1; $i<=30; $i++) {
- $seconds = $i * 86400;
- $thresholdmenu[$seconds] = get_string('numdays', '', $i);
- }
- choose_from_menu ($thresholdmenu, "expirythreshold", "$form->expirythreshold", "");
- helpbutton("expirythreshold", get_string("expirythreshold"));
- ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("numberweeks") ?>:</td>
- <td><?php
- for ($i=1; $i<=52; $i++) {
- $sectionmenu[$i] = "$i";
- }
- choose_from_menu ($sectionmenu, "numsections", "$form->numsections", "");
- helpbutton("coursenumsections", get_string("numberweeks"));
- ?></td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("groupmode") ?>:</td>
- <td><?php
- unset($choices);
- $choices[NOGROUPS] = get_string("groupsnone");
- $choices[SEPARATEGROUPS] = get_string("groupsseparate");
- $choices[VISIBLEGROUPS] = get_string("groupsvisible");
- choose_from_menu ($choices, "groupmode", $form->groupmode, "");
- helpbutton("groupmode", get_string("groupmode"));
-
- echo ' ';
- print_string('force');
- echo ': ';
- unset($choices);
- $choices["0"] = get_string("no");
- $choices["1"] = get_string("yes");
- choose_from_menu ($choices, "groupmodeforce", $form->groupmodeforce, "");
- helpbutton("groupmodeforce", get_string("groupmodeforce")); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("availability") ?>:</td>
- <td><?php
- unset($choices);
- $choices["0"] = get_string("courseavailablenot");
- $choices["1"] = get_string("courseavailable");
- choose_from_menu ($choices, "visible", $form->visible, "");
- helpbutton("courseavailability", get_string("availability")); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("enrolmentkey") ?>:</td>
- <td><input type="text" name="password" size="25" value="<?php p($form->password) ?>" alt="<?php print_string("enrolmentkey") ?>" />
- <?php helpbutton("enrolmentkey", get_string("enrolmentkey")) ?>
- <?php if (isset($err["password"])) formerr($err["password"]); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("opentoguests") ?>:</td>
- <td><?php
- unset($choices);
- $choices["0"] = get_string("guestsno");
- $choices["1"] = get_string("guestsyes");
- $choices["2"] = get_string("guestskey");
- choose_from_menu ($choices, "guest", $form->guest, "");
- helpbutton("guestaccess", get_string("opentoguests")); ?>
- </td>
-</tr>
-<?php
- if (isset($course) && method_exists(enrolment_factory::factory($course->enrol), 'print_entry') && $course->enrol != 'manual') {
-?>
-<tr valign="top">
- <td align="right"><?php print_string("cost") ?>:</td>
- <td><input type="text" name="cost" maxlength="6" size="6" value="<?php p($form->cost) ?>" alt="<?php print_string("cost") ?>" />
- <?php
- $currencies = get_list_of_currencies();
- choose_from_menu($currencies, 'currency', $form->currency, '');
- helpbutton("cost", get_string("cost"));
- ?>
- </td>
-</tr>
-<?php
- }
-?>
-<tr valign="top">
- <td align="right"><?php print_string("hiddensections") ?>:</td>
- <td><?php
- unset($choices);
- $choices["0"] = get_string("hiddensectionscollapsed");
- $choices["1"] = get_string("hiddensectionsinvisible");
- choose_from_menu ($choices, "hiddensections", $form->hiddensections, "");
- helpbutton("coursehiddensections", get_string("hiddensections")); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("newsitemsnumber") ?>:</td>
- <td><?php
- $newsitem = get_string("newsitem");
- $newsitems = get_string("newsitems");
-
- $options = array("0" => "0 $newsitems",
- "1" => "1 $newsitem",
- "2" => "2 $newsitems",
- "3" => "3 $newsitems",
- "4" => "4 $newsitems",
- "5" => "5 $newsitems",
- "6" => "6 $newsitems",
- "7" => "7 $newsitems",
- "8" => "8 $newsitems",
- "9" => "9 $newsitems",
- "10" => "10 $newsitems");
- choose_from_menu ($options, "newsitems", "$form->newsitems", "");
- helpbutton("coursenewsitems", get_string("newsitemsnumber"));
- ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("showgrades") ?>:</td>
- <td><?php
- unset($choices);
- $choices["0"] = get_string("no");
- $choices["1"] = get_string("yes");
- choose_from_menu ($choices, "showgrades", $form->showgrades, "");
- helpbutton("coursegrades", get_string("grades")); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("showreports") ?>:</td>
- <td><?php
- unset($choices);
- $choices["0"] = get_string("no");
- $choices["1"] = get_string("yes");
- choose_from_menu ($choices, "showreports", $form->showreports, "");
- helpbutton("coursereports", get_string("activityreport")); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("maximumupload") ?>:</td>
- <td><?php
- $choices = get_max_upload_sizes($CFG->maxbytes);
- choose_from_menu ($choices, "maxbytes", $form->maxbytes, "");
- helpbutton("courseuploadsize", get_string("maximumupload")); ?>
- </td>
-</tr>
-<tr valign="top">
- <td align="right"><?php print_string("forcelanguage") ?>:</td>
- <td>
- <?php
- $languages[''] = get_string("forceno");
- $languages += get_list_of_languages();
- choose_from_menu ($languages, "lang", $form->lang, "", "", "");
- ?>
- </td>
-</tr>
-<?php if (!empty($CFG->allowcoursethemes)) { ?>
-<tr valign="top">
- <td align="right"> <?php print_string("forcetheme") ?>: </td>
- <td>
- <?php
- $themes[''] = get_string("forceno");
- $themes += get_list_of_themes();
- choose_from_menu($themes, 'theme', $form->theme, "", "", "");
- ?>
- </td>
-</tr>
-<?php } ?>
-<tr valign="top">
- <td align="right"><?php print_string("managemeta") ?>:</td>
- <td>
- <?php
- if (empty($disable_meta)) {
- $meta[0] = get_string('no');
- $meta[1] = get_string('yes');
- choose_from_menu($meta,"metacourse",$form->metacourse,"","","");
- } else {
- echo ((empty($form->metacourse)) ? get_string("no") : get_string("yes"));
- echo " - $disable_meta ";
- }
- helpbutton("metacourse", get_string("metacourse"));?>
- </td>
-</tr>
-<?php if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) { ?>
-<tr valign="top">
- <td align="right"><?php print_string("restrictmodules") ?>:</td>
- <td valign="top"><table cellpadding="0" border="0"><tr valign="top"><td>
- <script language="JavaScript">
- function togglemodules(index) {
- if (index == 0) {
- document.getElementById('allowedmods').disabled=true;
- }
- else {
- document.getElementById('allowedmods').disabled=false;
- }
- }
- </script>
- <?php
- unset($options);
- $options[0] = get_string("no");
- $options[1] = get_string("yes");
- choose_from_menu($options,"restrictmodules",$form->restrictmodules,"","togglemodules(this.selectedIndex);","");
-?> </td><td> <?php p(moodle_strtolower(get_string('to'))); ?> </td><td>
- <select name="allowedmods[]" id="allowedmods" multiple="multiple" size="10" <?php echo ((empty($form->restrictmodules)) ? "disabled=\"disabled\"" : ""); ?>>
- <?php
- $mods = get_records("modules");
- $s = "selected=\"selected\"";
- echo '<option value="0" '.((empty($allowedmods)) ? $s : '').'>'.get_string('allownone').'</option>'."\n";
- foreach ($mods as $mod) {
- $selected = "";
- if (in_array($mod->id,$allowedmods))
- $selected = $s;
- echo '<option '.$selected.' value="'.$mod->id.'">'.$mod->name.'</option>'."\n";
- }
- ?>
- </select></td></tr></table></td>
-</tr>
-<?php } else { ?>
- <input type="hidden" name="restrictmodules" value="<?php p($form->restrictmodules); ?>" />
-<?php } ?>
-<tr>
- <td></td>
- <td><input type="submit" value="<?php print_string("savechanges") ?>" /></td>
-</tr>
-</table>
-<?php if(!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $form->category))) { ?>
-<input type="hidden" name="category" value="<?php echo $form->category ?>" />
-<?php } ?>
-<input type="hidden" name="id" value="<?php echo $form->id ?>" />
-<input type="hidden" name="sesskey" value="<?php echo $form->sesskey ?>" />
-</form>
<?php // $Id$
// Edit course settings
- require_once("../config.php");
- require_once("lib.php");
- require_once("$CFG->libdir/blocklib.php");
- require_once("$CFG->dirroot/enrol/enrol.class.php");
+ require_once('../config.php');
+ require_once($CFG->dirroot.'/enrol/enrol.class.php');
+ require_once($CFG->libdir.'/blocklib.php');
+ require_once($CFG->libdir.'/formslib.php');
+ require_once('lib.php');
+ require_once('edit_form.php');
- $id = optional_param('id', 0, PARAM_INT); // course id
- $category = optional_param('category', 0, PARAM_INT); // possible default category
-
- // redirect to new form if testing as developer
- if (!empty($CFG->use_new_forms)) {
- redirect('edit2.php?id='.$id.'&category='.$category);
- }
+ $id = optional_param('id', 0, PARAM_INT); // course id
+ $categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form
require_login();
-
- $disable_meta = false;
- $focus = "";
- if ($id) {
- if (! $course = get_record('course', 'id', $id)) {
+/// basic access control checks
+ if ($id) { // editing course
+ if (!$course = get_record('course', 'id', $id)) {
error('Course ID was incorrect');
}
+ $category = get_record('course_categories', 'id', $course->category);
+ require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
- $context = get_context_instance(CONTEXT_COURSE, $course->id);
-
- if (!has_capability('moodle/course:update', $context)) {
- error("You do not currently have editing privileges!");
+ } else if ($categoryid) { // creating new course in this category
+ $course = null;
+ if (!$category = get_record('course_categories', 'id', $categoryid)) {
+ error('Category ID was incorrect');
}
-
- if (course_in_meta($course)) {
- $disable_meta = get_string('metaalreadyinmeta');
+ require_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $category->id));
- } else if ($course->metacourse) {
- if (count_records("course_meta","parent_course",$course->id) > 0) {
- $disable_meta = get_string('metaalreadyhascourses');
- }
+ } else {
+ error('Either course id or category must be specified');
+ }
- } else {
- $managers = count(get_users_by_capability($context, 'moodle/course:managemetacourse'));
- $participants = count(get_users_by_capability($context, 'moodle/course:view'));
- if ($participants > $managers) {
- $disable_meta = get_string('metaalreadyhasenrolments');
+/// prepare course
+if (!empty($course)) {
+ $allowedmods = array();
+ if (!empty($course)) {
+ if ($am = get_records('course_allowed_modules','course',$course->id)) {
+ foreach ($am as $m) {
+ $allowedmods[] = $m->module;
}
+ } else {
+ if (empty($course->restrictmodules)) {
+ $allowedmods = explode(',',$CFG->defaultallowedmodules);
+ } // it'll be greyed out but we want these by default anyway.
}
- } else { // Admin is creating a new course
+ $course->allowedmods = $allowedmods;
- $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
- if (!has_capability('moodle/course:create',$context)) {
- error("You do not currently have course creation privileges!");
+ if ($course->enrolstartdate){
+ $course->enrolstartdisabled = 0;
}
- $course = NULL;
+ if ($course->enrolenddate) {
+ $course->enrolenddisabled = 0;
+ }
}
- if (! $site = get_site()) {
- redirect("$CFG->wwwroot/$CFG->admin/index.php");
- }
+}
-/// If data submitted, then process and store.
+/// first create the form
+ $editform = new course_edit_form('edit.php', compact('course', 'category'));
- if ($form = data_submitted() and confirm_sesskey()) {
- $form->startdate = make_timestamp($form->startyear, $form->startmonth, $form->startday);
- $form->category = clean_param($form->category, PARAM_INT);
+/// process data if submitted
+ if ($data = $editform->data_submitted()) {
- if (empty($form->enrolstartdisabled)) {
- $form->enrolstartdate = make_timestamp($form->enrolstartyear, $form->enrolstartmonth, $form->enrolstartday);
- } else {
- $form->enrolstartdate = 0;
+ //preprocess data
+ if ($data->enrolstartdisabled){
+ $data->enrolstartdate = 0;
}
- if (empty($form->enrolenddisabled)) {
- $form->enrolenddate = make_timestamp($form->enrolendyear, $form->enrolendmonth, $form->enrolendday);
- } else {
- $form->enrolenddate = 0;
+ if ($data->enrolenddisabled) {
+ $data->enrolenddate = 0;
}
- $form->format = optional_param('format', 'social', PARAM_ALPHA);
-
- $form->defaultrole = optional_param('defaultrole', 0, PARAM_INT);
- if ($form->defaultrole == -1) { // Just leave it however it is
- unset($form->defaultrole);
- }
-
- $err = array();
- validate_form($course, $form, $err);
-
- if (count($err) == 0) {
+ $data->timemodified = time();
- $allowedmods = array();
- if (!empty($form->allowedmods)) {
- $allowedmods = $form->allowedmods;
- unset($form->allowedmods);
- }
-
- $form->timemodified = time();
-
- if (!empty($course)) {
- // Test for and remove blocks which aren't appropriate anymore
- $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
- blocks_remove_inappropriate($page);
-
- // Update with the new data
- if (update_record('course', $form)) {
- add_to_log($course->id, "course", "update", "edit.php?id=$id", "");
- if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
- $course->restrictmodules = $form->restrictmodules;
- update_restricted_mods($course,$allowedmods);
- }
- fix_course_sortorder();
- // everything ok, no need to display any message in redirect
- redirect("view.php?id=$course->id");
- } else {
- error("Serious Error! Could not update the course record! (id = $form->id)");
- }
- } else {
- $form->timecreated = time();
-
- // place at beginning of category
- fix_course_sortorder();
- $form->sortorder = get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$form->category");
- if (empty($form->sortorder)) {
- $form->sortorder = 100;
- }
- // fill in default teacher and student names to keep backwards compatibility
- $form->teacher = addslashes(get_string('defaultcourseteacher'));
- $form->teachers = addslashes(get_string('defaultcourseteachers'));
- $form->student = addslashes(get_string('defaultcoursestudent'));
- $form->students = addslashes(get_string('defaultcoursestudents'));
-
- if ($newcourseid = insert_record('course', $form)) { // Set up new course
-
- // Setup the blocks
- $page = page_create_object(PAGE_COURSE_VIEW, $newcourseid);
- blocks_repopulate_page($page); // Return value not checked because you can always edit later
-
- if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
- $course = get_record("course","id",$newcourseid);
- update_restricted_mods($course,$allowedmods);
- }
-
- $section = NULL;
- $section->course = $newcourseid; // Create a default section.
- $section->section = 0;
- $section->id = insert_record("course_sections", $section);
-
- fix_course_sortorder();
- add_to_log(SITEID, "course", "new", "view.php?id=$newcourseid", "$form->fullname (ID $newcourseid)") ;
- $context = get_context_instance(CONTEXT_COURSE, $newcourseid);
-
- if ($form->metacourse and has_capability('moodle/course:managemetacourse', $context)) { // Redirect users with metacourse capability to student import
- redirect($CFG->wwwroot."/course/importstudents.php?id=$newcourseid");
-
- } else if (has_capability('moodle/role:assign', $context)) { // Redirect users with assign capability to assign users to different roles
- redirect($CFG->wwwroot."/$CFG->admin/roles/assign.php?contextid=$context->id");
-
- } else { // Add current teacher and send to course
-
- // find a role with legacy:edittingteacher
- if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, $context)) {
- // assign the role to this user
- $teachereditrole = array_shift($teacherroles);
- role_assign($teachereditrole->id, $USER->id, 0, $context->id);
- }
-
- redirect("view.php?id=$newcourseid");
- }
-
- } else {
- error("Serious Error! Could not create the new course!");
- }
- }
- die;
- } else {
- foreach ($err as $key => $value) {
- $focus = "form.$key";
- }
-
- }
- }
-
-/// Otherwise fill and print the form.
-
- if (empty($form)) {
- if (!empty($course)) {
- $form = $course;
+ if (empty($course)) {
+ create_course($data);
} else {
- $form->startdate = time() + 3600 * 24;
- $form->fullname = get_string("defaultcoursefullname");
- $form->shortname = get_string("defaultcourseshortname");
- $form->summary = get_string("defaultcoursesummary");
- $form->format = "weeks";
- $form->password = "";
- $form->guest = 0;
- $form->numsections = 10;
- $form->idnumber = '';
- $form->cost = '';
- $form->currency = empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency;
- $form->newsitems = 5;
- $form->showgrades = 1;
- $form->groupmode = 0;
- $form->groupmodeforce = 0;
- $form->category = $category;
- $form->id = "";
- $form->visible = 1;
-
+ update_course($data);
}
- } else {
- $form = stripslashes_safe($form);
}
- // !! no db access using data from $form beyond this point !!
- $form->categories = get_records_select_menu("course_categories", "", "name", "id,name");
+///print the form
- $courseformats = get_list_of_plugins("course/format");
- $form->courseformats = array();
-
- foreach ($courseformats as $courseformat) {
- $form->courseformats["$courseformat"] = get_string("format$courseformat");
- }
-
- if (empty($allowedmods)) {
- $allowedmods = array();
- if (!empty($course)) {
- if ($am = get_records("course_allowed_modules","course",$course->id)) {
- foreach ($am as $m) {
- $allowedmods[] = $m->module;
- }
- } else {
- if (empty($course->restrictmodules)) {
- $allowedmods = explode(',',$CFG->defaultallowedmodules);
- } // it'll be greyed out but we want these by default anyway.
- }
- } else {
- if ($CFG->restrictmodulesfor == 'all') {
- $allowedmods = explode(',',$CFG->defaultallowedmodules);
- if (!empty($CFG->restrictbydefault)) {
- $form->restrictmodules = 1;
- }
- }
- }
- }
-
-
- $usehtmleditor = can_use_html_editor();
+ $site = get_site();
$streditcoursesettings = get_string("editcoursesettings");
$straddnewcourse = get_string("addnewcourse");
if (!empty($course)) {
print_header($streditcoursesettings, "$course->fullname",
"<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
- -> $streditcoursesettings", $focus);
+ -> $streditcoursesettings", $editform->focus());
} else {
print_header("$site->shortname: $straddnewcourse", "$site->fullname",
- "<a href=\"../$CFG->admin/index.php\">$stradministration</a> -> ".
- "<a href=\"index.php\">$strcategories</a> -> $straddnewcourse", $focus);
+ "<a href=\"$CFG->wwwroot/$CFG->admin/index.php\">$stradministration</a> -> ".
+ "<a href=\"index.php\">$strcategories</a> -> $straddnewcourse", $editform->focus());
}
- $form->sesskey = !empty($USER->id) ? $USER->sesskey : '';
-
print_heading($streditcoursesettings);
- print_simple_box_start("center");
- include("edit.html");
- print_simple_box_end();
+ $editform->display();
+ print_footer($course);
- if ($usehtmleditor) {
- use_html_editor("summary");
- }
+ die;
- print_footer($course);
- exit;
+/// internal functions
+
+function create_course($data) {
+ global $CFG, $USER;
-/// Functions /////////////////////////////////////////////////////////////////
+ // preprocess allowed mods
+ $allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
+ unset($data->allowedmods);
+ if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
+ if ($CFG->restrictmodulesfor == 'all') {
+ $data->restrictmodules = 1;
+ } else {
+ $data->restrictmodules = 0;
+ }
+ }
-function validate_form($course, &$form, &$err) {
- if (empty($form->enrolenddisabled) && $form->enrolenddate <= $form->enrolstartdate) {
- $err["enroldate"] = get_string("enrolenddaterror");
+ $data->timecreated = time();
+
+ // place at beginning of category
+ fix_course_sortorder();
+ $data->sortorder = get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$data->category");
+ if (empty($data->sortorder)) {
+ $data->sortorder = 100;
}
- if (empty($form->fullname))
- $err["fullname"] = get_string("missingfullname");
+ if ($newcourseid = insert_record('course', $data)) { // Set up new course
+
+ $course = get_record('course', 'id', $newcourseid);
- if (empty($form->shortname))
- $err["shortname"] = get_string("missingshortname");
+ // Setup the blocks
+ $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
+ blocks_repopulate_page($page); // Return value not checked because you can always edit later
- if ($foundcourses = get_records("course", "shortname", $form->shortname)) {
- if (!empty($course->id)) {
- unset($foundcourses[$course->id]);
+ if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
+ update_restricted_mods($course, $allowedmods);
}
- if (!empty($foundcourses)) {
- foreach ($foundcourses as $foundcourse) {
- $foundcoursenames[] = $foundcourse->fullname;
- }
- $foundcoursenamestring = addslashes(implode(',', $foundcoursenames));
- $err["shortname"] = get_string("shortnametaken", "", $foundcoursenamestring);
+ $section = new object();
+ $section->course = $course->id; // Create a default section.
+ $section->section = 0;
+ $section->id = insert_record('course_sections', $section);
+
+ fix_course_sortorder();
+ add_to_log(SITEID, "course", "new", "view.php?id=$course->id", "$data->fullname (ID $course->id)") ;
+ $context = get_context_instance(CONTEXT_COURSE, $course->id);
+
+ if ($data->metacourse and has_capability('moodle/course:managemetacourse', $context)) { // Redirect users with metacourse capability to student import
+ redirect($CFG->wwwroot."/course/importstudents.php?id=$course->id");
+
+ } else if (has_capability('moodle/role:assign', $context)) { // Redirect users with assign capability to assign users to different roles
+ redirect($CFG->wwwroot."/$CFG->admin/roles/assign.php?contextid=$context->id");
+
+ } else { // Add current teacher and send to course
+ // find a role with legacy:edittingteacher
+ if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, $context)) {
+ // assign the role to this user
+ $teachereditrole = array_shift($teacherroles);
+ role_assign($teachereditrole->id, $USER->id, 0, $context->id);
+ }
+ redirect($CFG->wwwroot."/course/view.php?id=$course->id");
}
+
+ } else {
+ error("Serious Error! Could not create the new course!");
}
+ die;
+}
- if (empty($form->summary))
- $err["summary"] = get_string("missingsummary");
+function update_course($data) {
+ global $USER, $CFG;
- if (! $form->category)
- $err["category"] = get_string("missingcategory");
+ // preprocess allowed mods
+ $allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
+ unset($data->allowedmods);
+ if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
+ unset($data->restrictmodules);
+ }
- return;
-}
+ $oldcourse = get_record('course', 'id', $data->id); // should not fail, already tested above
+ if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $oldcourse->category))
+ or !has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $data->category))) {
+ // can not move to new category, keep the old one
+ unset($data->category);
+ }
+ // Update with the new data
+ if (update_record('course', $data)) {
+
+ $course = get_record('course', 'id', $data->id);
+
+ add_to_log($course->id, "course", "update", "edit.php?id=$course->id", "");
+ if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
+ update_restricted_mods($course, $allowedmods);
+ }
+ fix_course_sortorder();
+
+ // Test for and remove blocks which aren't appropriate anymore
+ $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
+ blocks_remove_inappropriate($page);
+
+ redirect($CFG->wwwroot."/course/view.php?id=$course->id");
+
+ } else {
+ error("Serious Error! Could not update the course record! (id = $form->id)");
+ }
+ die;
+}
?>
+++ /dev/null
-<?php // $Id$
- // Edit course settings
-
- require_once('../config.php');
- require_once($CFG->dirroot.'/enrol/enrol.class.php');
- require_once($CFG->libdir.'/blocklib.php');
- require_once($CFG->libdir.'/formslib.php');
- require_once('lib.php');
- require_once('edit_form.php');
-
- $id = optional_param('id', 0, PARAM_INT); // course id
- $categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form
-
- require_login();
-
-/// basic access control checks
- if ($id) { // editing course
- if (!$course = get_record('course', 'id', $id)) {
- error('Course ID was incorrect');
- }
- $category = get_record('course_categories', 'id', $course->category);
- require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
-
- } else if ($categoryid) { // creating new course in this category
- $course = null;
- if (!$category = get_record('course_categories', 'id', $categoryid)) {
- error('Category ID was incorrect');
- }
- require_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $category->id));
-
- } else {
- error('Either course id or category must be specified');
- }
-
-/// prepare course
-if (!empty($course)) {
- $allowedmods = array();
- if (!empty($course)) {
- if ($am = get_records('course_allowed_modules','course',$course->id)) {
- foreach ($am as $m) {
- $allowedmods[] = $m->module;
- }
- } else {
- if (empty($course->restrictmodules)) {
- $allowedmods = explode(',',$CFG->defaultallowedmodules);
- } // it'll be greyed out but we want these by default anyway.
- }
- $course->allowedmods = $allowedmods;
-
- if ($course->enrolstartdate){
- $course->enrolstartdisabled = 0;
- }
-
- if ($course->enrolenddate) {
- $course->enrolenddisabled = 0;
- }
- }
-
-}
-
-
-/// first create the form
- $editform = new course_edit_form('edit2.php', compact('course', 'category'));
-
-
-/// process data if submitted
- if ($data = $editform->data_submitted()) {
-
- //preprocess data
- if ($data->enrolstartdisabled){
- $data->enrolstartdate = 0;
- }
-
- if ($data->enrolenddisabled) {
- $data->enrolenddate = 0;
- }
-
- $data->timemodified = time();
-
- if (empty($course)) {
- create_course($data);
- } else {
- update_course($data);
- }
- }
-
-
-///print the form
-
- $site = get_site();
-
- $streditcoursesettings = get_string("editcoursesettings");
- $straddnewcourse = get_string("addnewcourse");
- $stradministration = get_string("administration");
- $strcategories = get_string("categories");
-
- if (!empty($course)) {
- print_header($streditcoursesettings, "$course->fullname",
- "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
- -> $streditcoursesettings", $editform->focus());
- } else {
- print_header("$site->shortname: $straddnewcourse", "$site->fullname",
- "<a href=\"$CFG->wwwroot/$CFG->admin/index.php\">$stradministration</a> -> ".
- "<a href=\"index.php\">$strcategories</a> -> $straddnewcourse", $editform->focus());
- }
-
- print_heading($streditcoursesettings);
- $editform->display();
- print_footer($course);
-
- die;
-
-
-/// internal functions
-
-function create_course($data) {
- global $CFG, $USER;
-
- // preprocess allowed mods
- $allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
- unset($data->allowedmods);
- if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
- if ($CFG->restrictmodulesfor == 'all') {
- $data->restrictmodules = 1;
- } else {
- $data->restrictmodules = 0;
- }
- }
-
-
- $data->timecreated = time();
-
- // place at beginning of category
- fix_course_sortorder();
- $data->sortorder = get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$data->category");
- if (empty($data->sortorder)) {
- $data->sortorder = 100;
- }
-
- if ($newcourseid = insert_record('course', $data)) { // Set up new course
-
- $course = get_record('course', 'id', $newcourseid);
-
- // Setup the blocks
- $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
- blocks_repopulate_page($page); // Return value not checked because you can always edit later
-
- if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
- update_restricted_mods($course, $allowedmods);
- }
-
- $section = new object();
- $section->course = $course->id; // Create a default section.
- $section->section = 0;
- $section->id = insert_record('course_sections', $section);
-
- fix_course_sortorder();
- add_to_log(SITEID, "course", "new", "view.php?id=$course->id", "$data->fullname (ID $course->id)") ;
- $context = get_context_instance(CONTEXT_COURSE, $course->id);
-
- if ($data->metacourse and has_capability('moodle/course:managemetacourse', $context)) { // Redirect users with metacourse capability to student import
- redirect($CFG->wwwroot."/course/importstudents.php?id=$course->id");
-
- } else if (has_capability('moodle/role:assign', $context)) { // Redirect users with assign capability to assign users to different roles
- redirect($CFG->wwwroot."/$CFG->admin/roles/assign.php?contextid=$context->id");
-
- } else { // Add current teacher and send to course
- // find a role with legacy:edittingteacher
- if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, $context)) {
- // assign the role to this user
- $teachereditrole = array_shift($teacherroles);
- role_assign($teachereditrole->id, $USER->id, 0, $context->id);
- }
- redirect($CFG->wwwroot."/course/view.php?id=$course->id");
- }
-
- } else {
- error("Serious Error! Could not create the new course!");
- }
- die;
-}
-
-function update_course($data) {
- global $USER, $CFG;
-
- // preprocess allowed mods
- $allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
- unset($data->allowedmods);
- if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
- unset($data->restrictmodules);
- }
-
- $oldcourse = get_record('course', 'id', $data->id); // should not fail, already tested above
- if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $oldcourse->category))
- or !has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $data->category))) {
- // can not move to new category, keep the old one
- unset($data->category);
- }
-
- // Update with the new data
- if (update_record('course', $data)) {
-
- $course = get_record('course', 'id', $data->id);
-
- add_to_log($course->id, "course", "update", "edit.php?id=$course->id", "");
- if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
- update_restricted_mods($course, $allowedmods);
- }
- fix_course_sortorder();
-
- // Test for and remove blocks which aren't appropriate anymore
- $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
- blocks_remove_inappropriate($page);
-
- redirect($CFG->wwwroot."/course/view.php?id=$course->id");
-
- } else {
- error("Serious Error! Could not update the course record! (id = $form->id)");
- }
- die;
-}
-
-?>