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 { require_login(); error('Either course id or category must be specified'); } /// first create the form $editform = new course_edit_form('edit.php', compact('course', 'category')); $editform->_form->_flagSubmitted = true; $elements = new object(); $elements->name = 'SRV ' . date('Ymd H:i'); $Out->print_r($elements->name, '$data (1) = ', 0, true); $elements->time = time(); $data = createNewCourseData($elements); $Out->print_r($data, '$data (1) = ', 0, true); $editform->set_data($data); $elements = $editform->_form->_elements; //$Out->print_r($elements, '$elements (1) = ', 0, true); $submitData = $editform->_form->_submitValues = (array) $data; //$Out->print_r($submitData, '$submitData (1) = ', 0, true); $data = $editform->get_data(); //$Out->print_r($data, '$data (2) = ', 0, true); //exit(); $data->password = $data->enrolpassword; // we need some other name for password field MDL-9929 /// process data if submitted //preprocess data if ($data->enrolstartdisabled) { $data->enrolstartdate = 0;
function createCourse($categoryId) { global $Out; $course = null; $category = null; // Must define as the compact function below expects a category variable if ($categoryId) { // creating new course in this category require_login(); 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 { require_login(); error('Either course id or category must be specified'); } // We're pretending a form has been submitted // Create the form $editform = new course_edit_form('edit.php', compact('course', 'category')); $editform->_form->_flagSubmitted = true; // Set the values for certain elements $elements = new object(); $elements->name = 'SRV ' . date('Ymd H:i'); $elements->time = time(); // Create a data object of elements for the form $data = createNewCourseData($elements); // Set the form with the data object $editform->set_data($data); //$elements = $editform->_form->_elements; //$Out->print_r($elements, '$elements (1) = ', 0, true); // Set the submit data as the form checks these values match the form elements $submitData = $editform->_form->_submitValues = (array) $data; //$Out->print_r($submitData, '$submitData (1) = ', 0, true); $data = $editform->get_data(); // Password fields must match $data->password = $data->enrolpassword; // we need some other name for password field MDL-9929 //preprocess data if ($data->enrolstartdisabled) { $data->enrolstartdate = 0; } if ($data->enrolenddisabled) { $data->enrolenddate = 0; } $data->timemodified = time(); // Now create the course from the form if (!($course = create_course($data))) { print_error('coursenotcreated'); } $context = get_context_instance(CONTEXT_COURSE, $course->id); // assign default role to creator if not already having permission to manage course assignments if (!has_capability('moodle/course:view', $context) or !has_capability('moodle/role:assign', $context)) { role_assign($CFG->creatornewroleid, $USER->id, 0, $context->id); } // ensure we can use the course right after creating it // this means trigger a reload of accessinfo... mark_context_dirty($context->path); print_box('course successfully created.'); return $course; }
if ($categoryid) { // creating new course in this category $course = null; require_login(); 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 { require_login(); error('Either course id or category must be specified'); } /// first create the form $editform = new course_edit_form('edit.php', compact('course', 'category')); $editform->_form->_flagSubmitted = true; $data = createNewCourseData(); //$Out->print_r($data, '$data (1) = ', 0, true); $editform->set_data($data); $elements = $editform->_form->_elements; //$Out->print_r($elements, '$elements (1) = ', 0, true); $submitData = $editform->_form->_submitValues = (array) $data; //$Out->print_r($submitData, '$submitData (1) = ', 0, true); $data = $editform->get_data(); $Out->print_r($data, '$data (2) = ', 0, true); //exit(); $data->password = $data->enrolpassword; // we need some other name for password field MDL-9929 /// process data if submitted //preprocess data if ($data->enrolstartdisabled) { $data->enrolstartdate = 0;