Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/course/edit_form.php
Go to the documentation of this file.
00001 <?php
00002 
00003 defined('MOODLE_INTERNAL') || die;
00004 
00005 require_once($CFG->libdir.'/formslib.php');
00006 require_once($CFG->libdir.'/completionlib.php');
00007 
00008 class course_edit_form extends moodleform {
00009     protected $course;
00010     protected $context;
00011 
00012     function definition() {
00013         global $USER, $CFG, $DB;
00014 
00015         $mform    = $this->_form;
00016 
00017         $course        = $this->_customdata['course']; // this contains the data of this form
00018         $category      = $this->_customdata['category'];
00019         $editoroptions = $this->_customdata['editoroptions'];
00020         $returnto = $this->_customdata['returnto'];
00021 
00022         $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
00023         $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
00024 
00025         if (!empty($course->id)) {
00026             $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00027             $context = $coursecontext;
00028         } else {
00029             $coursecontext = null;
00030             $context = $categorycontext;
00031         }
00032 
00033         $courseconfig = get_config('moodlecourse');
00034 
00035         $this->course  = $course;
00036         $this->context = $context;
00037 
00039 //--------------------------------------------------------------------------------
00040         $mform->addElement('header','general', get_string('general', 'form'));
00041 
00042         $mform->addElement('hidden', 'returnto', null);
00043         $mform->setType('returnto', PARAM_ALPHANUM);
00044         $mform->setConstant('returnto', $returnto);
00045 
00046         // verify permissions to change course category or keep current
00047         if (empty($course->id)) {
00048             if (has_capability('moodle/course:create', $categorycontext)) {
00049                 $displaylist = array();
00050                 $parentlist = array();
00051                 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
00052                 $mform->addElement('select', 'category', get_string('category'), $displaylist);
00053                 $mform->addHelpButton('category', 'category');
00054                 $mform->setDefault('category', $category->id);
00055             } else {
00056                 $mform->addElement('hidden', 'category', null);
00057                 $mform->setType('category', PARAM_INT);
00058                 $mform->setConstant('category', $category->id);
00059             }
00060         } else {
00061             if (has_capability('moodle/course:changecategory', $coursecontext)) {
00062                 $displaylist = array();
00063                 $parentlist = array();
00064                 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
00065                 if (!isset($displaylist[$course->category])) {
00066                     //always keep current
00067                     $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
00068                 }
00069                 $mform->addElement('select', 'category', get_string('category'), $displaylist);
00070                 $mform->addHelpButton('category', 'category');
00071             } else {
00072                 //keep current
00073                 $mform->addElement('hidden', 'category', null);
00074                 $mform->setType('category', PARAM_INT);
00075                 $mform->setConstant('category', $course->category);
00076             }
00077         }
00078 
00079         $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
00080         $mform->addHelpButton('fullname', 'fullnamecourse');
00081         $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
00082         $mform->setType('fullname', PARAM_MULTILANG);
00083         if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
00084             $mform->hardFreeze('fullname');
00085             $mform->setConstant('fullname', $course->fullname);
00086         }
00087 
00088         $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
00089         $mform->addHelpButton('shortname', 'shortnamecourse');
00090         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
00091         $mform->setType('shortname', PARAM_MULTILANG);
00092         if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
00093             $mform->hardFreeze('shortname');
00094             $mform->setConstant('shortname', $course->shortname);
00095         }
00096 
00097         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
00098         $mform->addHelpButton('idnumber', 'idnumbercourse');
00099         $mform->setType('idnumber', PARAM_RAW);
00100         if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
00101             $mform->hardFreeze('idnumber');
00102             $mform->setConstants('idnumber', $course->idnumber);
00103         }
00104 
00105 
00106         $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
00107         $mform->addHelpButton('summary_editor', 'coursesummary');
00108         $mform->setType('summary_editor', PARAM_RAW);
00109 
00110         if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
00111             $mform->hardFreeze('summary_editor');
00112         }
00113 
00114         $courseformats = get_plugin_list('format');
00115         $formcourseformats = array();
00116         foreach ($courseformats as $courseformat => $formatdir) {
00117             $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
00118         }
00119         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
00120         $mform->addHelpButton('format', 'format');
00121         $mform->setDefault('format', $courseconfig->format);
00122 
00123         for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
00124             $sectionmenu[$i] = "$i";
00125         }
00126         $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
00127         $mform->setDefault('numsections', $courseconfig->numsections);
00128 
00129         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
00130         $mform->addHelpButton('startdate', 'startdate');
00131         $mform->setDefault('startdate', time() + 3600 * 24);
00132 
00133         $choices = array();
00134         $choices['0'] = get_string('hiddensectionscollapsed');
00135         $choices['1'] = get_string('hiddensectionsinvisible');
00136         $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
00137         $mform->addHelpButton('hiddensections', 'hiddensections');
00138         $mform->setDefault('hiddensections', $courseconfig->hiddensections);
00139 
00140         $options = range(0, 10);
00141         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
00142         $mform->addHelpButton('newsitems', 'newsitemsnumber');
00143         $mform->setDefault('newsitems', $courseconfig->newsitems);
00144 
00145         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
00146         $mform->addHelpButton('showgrades', 'showgrades');
00147         $mform->setDefault('showgrades', $courseconfig->showgrades);
00148 
00149         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
00150         $mform->addHelpButton('showreports', 'showreports');
00151         $mform->setDefault('showreports', $courseconfig->showreports);
00152 
00153         $choices = get_max_upload_sizes($CFG->maxbytes);
00154         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
00155         $mform->addHelpButton('maxbytes', 'maximumupload');
00156         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
00157 
00158         if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
00159             if (empty($course->legacyfiles)) {
00160                 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
00161                 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
00162             } else {
00163                 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
00164             }
00165             $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
00166             $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
00167             if (!isset($courseconfig->legacyfiles)) {
00168                 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
00169                 $courseconfig->legacyfiles = 0;
00170             }
00171             $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
00172         }
00173 
00174         if (!empty($CFG->allowcoursethemes)) {
00175             $themeobjects = get_list_of_themes();
00176             $themes=array();
00177             $themes[''] = get_string('forceno');
00178             foreach ($themeobjects as $key=>$theme) {
00179                 if (empty($theme->hidefromselector)) {
00180                     $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
00181                 }
00182             }
00183             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
00184         }
00185 
00186 //--------------------------------------------------------------------------------
00187         enrol_course_edit_form($mform, $course, $context);
00188 
00189 //--------------------------------------------------------------------------------
00190         $mform->addElement('header','', get_string('groups', 'group'));
00191 
00192         $choices = array();
00193         $choices[NOGROUPS] = get_string('groupsnone', 'group');
00194         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
00195         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
00196         $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
00197         $mform->addHelpButton('groupmode', 'groupmode', 'group');
00198         $mform->setDefault('groupmode', $courseconfig->groupmode);
00199 
00200         $choices = array();
00201         $choices['0'] = get_string('no');
00202         $choices['1'] = get_string('yes');
00203         $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
00204         $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
00205         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
00206 
00207         //default groupings selector
00208         $options = array();
00209         $options[0] = get_string('none');
00210         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
00211 
00212 //--------------------------------------------------------------------------------
00213         $mform->addElement('header','', get_string('availability'));
00214 
00215         $choices = array();
00216         $choices['0'] = get_string('courseavailablenot');
00217         $choices['1'] = get_string('courseavailable');
00218         $mform->addElement('select', 'visible', get_string('availability'), $choices);
00219         $mform->addHelpButton('visible', 'availability');
00220         $mform->setDefault('visible', $courseconfig->visible);
00221         if (!has_capability('moodle/course:visibility', $context)) {
00222             $mform->hardFreeze('visible');
00223             if (!empty($course->id)) {
00224                 $mform->setConstant('visible', $course->visible);
00225             } else {
00226                 $mform->setConstant('visible', $category->visible);
00227             }
00228         }
00229 
00230 //--------------------------------------------------------------------------------
00231         $mform->addElement('header','', get_string('language'));
00232 
00233         $languages=array();
00234         $languages[''] = get_string('forceno');
00235         $languages += get_string_manager()->get_list_of_translations();
00236         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
00237         $mform->setDefault('lang', $courseconfig->lang);
00238 
00239 //--------------------------------------------------------------------------------
00240         if (completion_info::is_enabled_for_site()) {
00241             $mform->addElement('header','', get_string('progress','completion'));
00242             $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
00243                 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
00244             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
00245 
00246             $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
00247             $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
00248             $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
00249         } else {
00250             $mform->addElement('hidden', 'enablecompletion');
00251             $mform->setType('enablecompletion', PARAM_INT);
00252             $mform->setDefault('enablecompletion',0);
00253 
00254             $mform->addElement('hidden', 'completionstartonenrol');
00255             $mform->setType('completionstartonenrol', PARAM_INT);
00256             $mform->setDefault('completionstartonenrol',0);
00257         }
00258 
00259 //--------------------------------------------------------------------------------
00260         if (has_capability('moodle/site:config', $systemcontext)) {
00261             if (((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
00262                 $mform->addElement('header', '', get_string('restrictmodules'));
00263 
00264                 $options = array();
00265                 $options['0'] = get_string('no');
00266                 $options['1'] = get_string('yes');
00267                 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
00268                 if (!empty($CFG->restrictbydefault)) {
00269                     $mform->setDefault('restrictmodules', 1);
00270                 }
00271 
00272                 $mods = array(0=>get_string('allownone'));
00273                 $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name');
00274                 $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10'));
00275                 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
00276                 // defaults are already in $course
00277             } else {
00278                 // remove any mod restriction
00279                 $mform->addElement('hidden', 'restrictmodules', 0);
00280                 $mform->setType('restrictmodules', PARAM_INT);
00281             }
00282         } else {
00283             $mform->addElement('hidden', 'restrictmodules');
00284             $mform->setType('restrictmodules', PARAM_INT);
00285             if (empty($course->id)) {
00286                 $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor == 'all'));
00287             } else {
00288                 // keep previous
00289                 $mform->setConstant('restrictmodules', $course->restrictmodules);
00290             }
00291         }
00292 
00294 //--------------------------------------------------------------------------------
00295         $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
00296         $mform->addHelpButton('rolerenaming', 'rolerenaming');
00297 
00298         if ($roles = get_all_roles()) {
00299             if ($coursecontext) {
00300                 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
00301             }
00302             $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
00303             foreach ($roles as $role) {
00304                 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
00305                 if (isset($role->localname)) {
00306                     $mform->setDefault('role_'.$role->id, $role->localname);
00307                 }
00308                 $mform->setType('role_'.$role->id, PARAM_TEXT);
00309                 if (!in_array($role->id, $assignableroles)) {
00310                     $mform->setAdvanced('role_'.$role->id);
00311                 }
00312             }
00313         }
00314 
00315 //--------------------------------------------------------------------------------
00316         $this->add_action_buttons();
00317 //--------------------------------------------------------------------------------
00318         $mform->addElement('hidden', 'id', null);
00319         $mform->setType('id', PARAM_INT);
00320 
00322 //--------------------------------------------------------------------------------
00323         $this->set_data($course);
00324     }
00325 
00326     function definition_after_data() {
00327         global $DB;
00328 
00329         $mform = $this->_form;
00330 
00331         // add available groupings
00332         if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
00333             $options = array();
00334             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
00335                 foreach ($groupings as $grouping) {
00336                     $options[$grouping->id] = format_string($grouping->name);
00337                 }
00338             }
00339             $gr_el =& $mform->getElement('defaultgroupingid');
00340             $gr_el->load($options);
00341         }
00342     }
00343 
00344 
00346     function validation($data, $files) {
00347         global $DB, $CFG;
00348 
00349         $errors = parent::validation($data, $files);
00350         if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
00351             if (!empty($data['id'])) {
00352                 unset($foundcourses[$data['id']]);
00353             }
00354             if (!empty($foundcourses)) {
00355                 foreach ($foundcourses as $foundcourse) {
00356                     $foundcoursenames[] = $foundcourse->fullname;
00357                 }
00358                 $foundcoursenamestring = implode(',', $foundcoursenames);
00359                 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
00360             }
00361         }
00362 
00363         $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
00364 
00365         return $errors;
00366     }
00367 }
00368 
 All Data Structures Namespaces Files Functions Variables Enumerations