public function define_execution() { global $DB; $course = $DB->get_record('course', array('id' => $this->get_courseid()), '*', MUST_EXIST); if ($DB->record_exists('enrol', array('courseid' => $this->get_courseid(), 'enrol' => 'manual'))) { // Something already added instances, do not add default instances. $plugins = enrol_get_plugins(true); foreach ($plugins as $plugin) { $plugin->restore_sync_course($course); } } else { // Looks like a newly created course. enrol_course_updated(true, $course, null); } }
/** * Update a course. * * Please note this functions does not verify any access control, * the calling code is responsible for all validation (usually it is the form definition). * * @param object $data - all the data needed for an entry in the 'course' table * @param array $editoroptions course description editor options * @return void */ function update_course($data, $editoroptions = NULL) { global $CFG, $DB; $data->timemodified = time(); $oldcourse = $DB->get_record('course', array('id' => $data->id), '*', MUST_EXIST); $context = get_context_instance(CONTEXT_COURSE, $oldcourse->id); if ($editoroptions) { $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0); } if (!isset($data->category) or empty($data->category)) { // prevent nulls and 0 in category field unset($data->category); } $movecat = (isset($data->category) and $oldcourse->category != $data->category); if (!isset($data->visible)) { // data not from form, add missing visibility info $data->visible = $oldcourse->visible; } if ($data->visible != $oldcourse->visible) { // reset the visibleold flag when manually hiding/unhiding course $data->visibleold = $data->visible; } else { if ($movecat) { $newcategory = $DB->get_record('course_categories', array('id' => $data->category)); if (empty($newcategory->visible)) { // make sure when moving into hidden category the course is hidden automatically $data->visible = 0; } } } // Update with the new data $DB->update_record('course', $data); $course = $DB->get_record('course', array('id' => $data->id)); if ($movecat) { $newparent = get_context_instance(CONTEXT_COURSECAT, $course->category); context_moved($context, $newparent); } fix_course_sortorder(); // Test for and remove blocks which aren't appropriate anymore blocks_remove_inappropriate($course); // Save any custom role names. save_local_role_names($course->id, $data); // update enrol settings enrol_course_updated(false, $course, $data); add_to_log($course->id, "course", "update", "edit.php?id={$course->id}", $course->id); // Trigger events events_trigger('course_updated', $course); }
/** * Update a course. * * Please note this functions does not verify any access control, * the calling code is responsible for all validation (usually it is the form definition). * * @param object $data - all the data needed for an entry in the 'course' table * @param array $editoroptions course description editor options * @return void */ function update_course($data, $editoroptions = NULL) { global $DB; $data->timemodified = time(); $oldcourse = course_get_format($data->id)->get_course(); $context = context_course::instance($oldcourse->id); if ($editoroptions) { $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0); } if ($overviewfilesoptions = course_overviewfiles_options($data->id)) { $data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0); } // Check we don't have a duplicate shortname. if (!empty($data->shortname) && $oldcourse->shortname != $data->shortname) { if ($DB->record_exists_sql('SELECT id from {course} WHERE shortname = ? AND id <> ?', array($data->shortname, $data->id))) { throw new moodle_exception('shortnametaken', '', '', $data->shortname); } } // Check we don't have a duplicate idnumber. if (!empty($data->idnumber) && $oldcourse->idnumber != $data->idnumber) { if ($DB->record_exists_sql('SELECT id from {course} WHERE idnumber = ? AND id <> ?', array($data->idnumber, $data->id))) { throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber); } } if (!isset($data->category) or empty($data->category)) { // prevent nulls and 0 in category field unset($data->category); } $changesincoursecat = $movecat = (isset($data->category) and $oldcourse->category != $data->category); if (!isset($data->visible)) { // data not from form, add missing visibility info $data->visible = $oldcourse->visible; } if ($data->visible != $oldcourse->visible) { // reset the visibleold flag when manually hiding/unhiding course $data->visibleold = $data->visible; $changesincoursecat = true; } else { if ($movecat) { $newcategory = $DB->get_record('course_categories', array('id' => $data->category)); if (empty($newcategory->visible)) { // make sure when moving into hidden category the course is hidden automatically $data->visible = 0; } } } // Update with the new data $DB->update_record('course', $data); // make sure the modinfo cache is reset rebuild_course_cache($data->id); // update course format options with full course data course_get_format($data->id)->update_course_format_options($data, $oldcourse); $course = $DB->get_record('course', array('id' => $data->id)); if ($movecat) { $newparent = context_coursecat::instance($course->category); $context->update_moved($newparent); } $fixcoursesortorder = $movecat || isset($data->sortorder) && $oldcourse->sortorder != $data->sortorder; if ($fixcoursesortorder) { fix_course_sortorder(); } // purge appropriate caches in case fix_course_sortorder() did not change anything cache_helper::purge_by_event('changesincourse'); if ($changesincoursecat) { cache_helper::purge_by_event('changesincoursecat'); } // Test for and remove blocks which aren't appropriate anymore blocks_remove_inappropriate($course); // Save any custom role names. save_local_role_names($course->id, $data); // update enrol settings enrol_course_updated(false, $course, $data); // Trigger a course updated event. $event = \core\event\course_updated::create(array('objectid' => $course->id, 'context' => context_course::instance($course->id), 'other' => array('shortname' => $course->shortname, 'fullname' => $course->fullname))); $event->set_legacy_logdata(array($course->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id)); $event->trigger(); if ($oldcourse->format !== $course->format) { // Remove all options stored for the previous format // We assume that new course format migrated everything it needed watching trigger // 'course_updated' and in method format_XXX::update_course_format_options() $DB->delete_records('course_format_options', array('courseid' => $course->id, 'format' => $oldcourse->format)); } }
/** * Process the group tag. This defines a Moodle course. * @param string $tagconents The raw contents of the XML element */ function process_group_tag($tagcontents) { global $DB; // Get configs $truncatecoursecodes = $this->get_config('truncatecoursecodes'); $createnewcourses = $this->get_config('createnewcourses'); $createnewcategories = $this->get_config('createnewcategories'); // Process tag contents $group = new stdClass(); if (preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)) { $group->coursecode = trim($matches[1]); } if (preg_match('{<description>.*?<long>(.*?)</long>.*?</description>}is', $tagcontents, $matches)) { $group->description = trim($matches[1]); } if (preg_match('{<description>.*?<short>(.*?)</short>.*?</description>}is', $tagcontents, $matches)) { $group->shortName = trim($matches[1]); } if (preg_match('{<description>.*?<full>(.*?)</full>.*?</description>}is', $tagcontents, $matches)) { $group->fulldescription = trim($matches[1]); } if (preg_match('{<org>.*?<orgunit>(.*?)</orgunit>.*?</org>}is', $tagcontents, $matches)) { $group->category = trim($matches[1]); } $recstatus = $this->get_recstatus($tagcontents, 'group'); //echo "<p>get_recstatus for this group returned $recstatus</p>"; if (!(strlen($group->coursecode) > 0)) { $this->log_line('Error at line ' . $line . ': Unable to find course code in \'group\' element.'); } else { // First, truncate the course code if desired if (intval($truncatecoursecodes) > 0) { $group->coursecode = $truncatecoursecodes > 0 ? substr($group->coursecode, 0, intval($truncatecoursecodes)) : $group->coursecode; } /* -----------Course aliasing is DEACTIVATED until a more general method is in place--------------- // Second, look in the course alias table to see if the code should be translated to something else if($aliases = $DB->get_field('enrol_coursealias', 'toids', array('fromid'=>$group->coursecode))){ $this->log_line("Found alias of course code: Translated $group->coursecode to $aliases"); // Alias is allowed to be a comma-separated list, so let's split it $group->coursecode = explode(',', $aliases); } */ // For compatibility with the (currently inactive) course aliasing, we need this to be an array $group->coursecode = array($group->coursecode); // Third, check if the course(s) exist foreach ($group->coursecode as $coursecode) { $coursecode = trim($coursecode); if (!$DB->get_field('course', 'id', array('idnumber' => $coursecode))) { if (!$createnewcourses) { $this->log_line("Course {$coursecode} not found in Moodle's course idnumbers."); } else { // Set shortname to description or description to shortname if one is set but not the other. $nodescription = !isset($group->description); $noshortname = !isset($group->shortName); if ($nodescription && $noshortname) { // If neither short nor long description are set let if fail $this->log_line("Neither long nor short name are set for {$coursecode}"); } else { if ($nodescription) { // If short and ID exist, then give the long short's value, then give short the ID's value $group->description = $group->shortName; $group->shortName = $coursecode; } else { if ($noshortname) { // If long and ID exist, then map long to long, then give short the ID's value. $group->shortName = $coursecode; } } } // Create the (hidden) course(s) if not found $courseconfig = get_config('moodlecourse'); // Load Moodle Course shell defaults $course = new stdClass(); $course->fullname = $group->description; $course->shortname = $group->shortName; if (!empty($group->fulldescription)) { $course->summary = format_text($group->fulldescription, FORMAT_HTML); } $course->idnumber = $coursecode; $course->format = $courseconfig->format; $course->visible = $courseconfig->visible; $course->numsections = $courseconfig->numsections; $course->hiddensections = $courseconfig->hiddensections; $course->newsitems = $courseconfig->newsitems; $course->showgrades = $courseconfig->showgrades; $course->showreports = $courseconfig->showreports; $course->maxbytes = $courseconfig->maxbytes; $course->groupmode = $courseconfig->groupmode; $course->groupmodeforce = $courseconfig->groupmodeforce; $course->enablecompletion = $courseconfig->enablecompletion; $course->completionstartonenrol = $courseconfig->completionstartonenrol; // Insert default names for teachers/students, from the current language // Handle course categorisation (taken from the group.org.orgunit field if present) if (strlen($group->category) > 0) { // If the category is defined and exists in Moodle, we want to store it in that one if ($catid = $DB->get_field('course_categories', 'id', array('name' => $group->category))) { $course->category = $catid; } else { if ($createnewcategories) { // Else if we're allowed to create new categories, let's create this one $newcat = new stdClass(); $newcat->name = $group->category; $newcat->visible = 0; $catid = $DB->insert_record('course_categories', $newcat); $course->category = $catid; $this->log_line("Created new (hidden) category, #{$catid}: {$newcat->name}"); } else { // If not found and not allowed to create, stick with default $this->log_line('Category ' . $group->category . ' not found in Moodle database, so using default category instead.'); $course->category = 1; } } } else { $course->category = 1; } $course->timecreated = time(); $course->startdate = time(); // Choose a sort order that puts us at the start of the list! $course->sortorder = 0; $courseid = $DB->insert_record('course', $course); // Setup default enrolment plugins $course->id = $courseid; enrol_course_updated(true, $course, null); // Setup the blocks $course = $DB->get_record('course', array('id' => $courseid)); blocks_add_default_course_blocks($course); $section = new stdClass(); $section->course = $course->id; // Create a default section. $section->section = 0; $section->summaryformat = FORMAT_HTML; $section->id = $DB->insert_record("course_sections", $section); add_to_log(SITEID, "course", "new", "view.php?id={$course->id}", "{$course->fullname} (ID {$course->id})"); $this->log_line("Created course {$coursecode} in Moodle (Moodle ID is {$course->id})"); } } else { if ($recstatus == 3 && ($courseid = $DB->get_field('course', 'id', array('idnumber' => $coursecode)))) { // If course does exist, but recstatus==3 (delete), then set the course as hidden $DB->set_field('course', 'visible', '0', array('id' => $courseid)); } } } // End of foreach(coursecode) } }
/** * Update a course. * * Please note this functions does not verify any access control, * the calling code is responsible for all validation (usually it is the form definition). * * @param object $data - all the data needed for an entry in the 'course' table * @param array $editoroptions course description editor options * @return void */ function update_course($data, $editoroptions = NULL) { global $CFG, $DB; $data->timemodified = time(); $oldcourse = course_get_format($data->id)->get_course(); $context = context_course::instance($oldcourse->id); if ($editoroptions) { $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0); } if ($overviewfilesoptions = course_overviewfiles_options($data->id)) { $data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0); } if (!isset($data->category) or empty($data->category)) { // prevent nulls and 0 in category field unset($data->category); } $changesincoursecat = $movecat = (isset($data->category) and $oldcourse->category != $data->category); if (!isset($data->visible)) { // data not from form, add missing visibility info $data->visible = $oldcourse->visible; } if ($data->visible != $oldcourse->visible) { // reset the visibleold flag when manually hiding/unhiding course $data->visibleold = $data->visible; $changesincoursecat = true; } else { if ($movecat) { $newcategory = $DB->get_record('course_categories', array('id' => $data->category)); if (empty($newcategory->visible)) { // make sure when moving into hidden category the course is hidden automatically $data->visible = 0; } } } // Update with the new data $DB->update_record('course', $data); // make sure the modinfo cache is reset rebuild_course_cache($data->id); // update course format options with full course data course_get_format($data->id)->update_course_format_options($data, $oldcourse); $course = $DB->get_record('course', array('id' => $data->id)); if ($movecat) { $newparent = context_coursecat::instance($course->category); context_moved($context, $newparent); } fix_course_sortorder(); // purge appropriate caches in case fix_course_sortorder() did not change anything cache_helper::purge_by_event('changesincourse'); if ($changesincoursecat) { cache_helper::purge_by_event('changesincoursecat'); } // Test for and remove blocks which aren't appropriate anymore blocks_remove_inappropriate($course); // Save any custom role names. save_local_role_names($course->id, $data); // update enrol settings enrol_course_updated(false, $course, $data); add_to_log($course->id, "course", "update", "edit.php?id={$course->id}", $course->id); // Trigger events events_trigger('course_updated', $course); if ($oldcourse->format !== $course->format) { // Remove all options stored for the previous format // We assume that new course format migrated everything it needed watching trigger // 'course_updated' and in method format_XXX::update_course_format_options() $DB->delete_records('course_format_options', array('courseid' => $course->id, 'format' => $oldcourse->format)); } }
/** * Process the group tag. This defines a Moodle course. * * @param string $tagcontents The raw contents of the XML element */ protected function process_group_tag($tagcontents) { global $DB; // Get configs. $truncatecoursecodes = $this->get_config('truncatecoursecodes'); $createnewcourses = $this->get_config('createnewcourses'); $createnewcategories = $this->get_config('createnewcategories'); // Process tag contents. $group = new stdClass(); if (preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)) { $group->coursecode = trim($matches[1]); } if (preg_match('{<description>.*?<long>(.*?)</long>.*?</description>}is', $tagcontents, $matches)) { $group->long = trim($matches[1]); } if (preg_match('{<description>.*?<short>(.*?)</short>.*?</description>}is', $tagcontents, $matches)) { $group->short = trim($matches[1]); } if (preg_match('{<description>.*?<full>(.*?)</full>.*?</description>}is', $tagcontents, $matches)) { $group->full = trim($matches[1]); } if (preg_match('{<org>.*?<orgunit>(.*?)</orgunit>.*?</org>}is', $tagcontents, $matches)) { $group->category = trim($matches[1]); } $recstatus = $this->get_recstatus($tagcontents, 'group'); if (empty($group->coursecode)) { $this->log_line('Error: Unable to find course code in \'group\' element.'); } else { // First, truncate the course code if desired. if (intval($truncatecoursecodes) > 0) { $group->coursecode = $truncatecoursecodes > 0 ? substr($group->coursecode, 0, intval($truncatecoursecodes)) : $group->coursecode; } // For compatibility with the (currently inactive) course aliasing, we need this to be an array. $group->coursecode = array($group->coursecode); // Third, check if the course(s) exist. foreach ($group->coursecode as $coursecode) { $coursecode = trim($coursecode); if (!$DB->get_field('course', 'id', array('idnumber' => $coursecode))) { if (!$createnewcourses) { $this->log_line("Course {$coursecode} not found in Moodle's course idnumbers."); } else { // Create the (hidden) course(s) if not found $courseconfig = get_config('moodlecourse'); // Load Moodle Course shell defaults. // New course. $course = new stdClass(); foreach ($this->coursemappings as $courseattr => $imsname) { if ($imsname == 'ignore') { continue; } // Check if the IMS file contains the mapped tag, otherwise fallback on coursecode. if ($imsname == 'coursecode') { $course->{$courseattr} = $coursecode; } else { if (!empty($group->{$imsname})) { $course->{$courseattr} = $group->{$imsname}; } else { $this->log_line('No ' . $imsname . ' description tag found for ' . $coursecode . ' coursecode, using ' . $coursecode . ' instead'); $course->{$courseattr} = $coursecode; } } } $course->idnumber = $coursecode; $course->format = $courseconfig->format; $course->visible = $courseconfig->visible; $course->newsitems = $courseconfig->newsitems; $course->showgrades = $courseconfig->showgrades; $course->showreports = $courseconfig->showreports; $course->maxbytes = $courseconfig->maxbytes; $course->groupmode = $courseconfig->groupmode; $course->groupmodeforce = $courseconfig->groupmodeforce; $course->enablecompletion = $courseconfig->enablecompletion; // Insert default names for teachers/students, from the current language. // Handle course categorisation (taken from the group.org.orgunit field if present). if (!empty($group->category)) { // If the category is defined and exists in Moodle, we want to store it in that one. if ($catid = $DB->get_field('course_categories', 'id', array('name' => $group->category))) { $course->category = $catid; } else { if ($createnewcategories) { // Else if we're allowed to create new categories, let's create this one. $newcat = new stdClass(); $newcat->name = $group->category; $newcat->visible = 0; $catid = $DB->insert_record('course_categories', $newcat); $course->category = $catid; $this->log_line("Created new (hidden) category, #{$catid}: {$newcat->name}"); } else { // If not found and not allowed to create, stick with default. $this->log_line('Category ' . $group->category . ' not found in Moodle database, so using ' . 'default category instead.'); $course->category = $this->get_default_category_id(); } } } else { $course->category = $this->get_default_category_id(); } $course->timecreated = time(); $course->startdate = time(); // Choose a sort order that puts us at the start of the list! $course->sortorder = 0; $courseid = $DB->insert_record('course', $course); // Setup default enrolment plugins. $course->id = $courseid; enrol_course_updated(true, $course, null); // Setup the blocks. $course = $DB->get_record('course', array('id' => $courseid)); blocks_add_default_course_blocks($course); // Create default 0-section. course_create_sections_if_missing($course, 0); add_to_log(SITEID, "course", "new", "view.php?id={$course->id}", "{$course->fullname} (ID {$course->id})"); $this->log_line("Created course {$coursecode} in Moodle (Moodle ID is {$course->id})"); } } else { if ($recstatus == 3 && ($courseid = $DB->get_field('course', 'id', array('idnumber' => $coursecode)))) { // If course does exist, but recstatus==3 (delete), then set the course as hidden. $DB->set_field('course', 'visible', '0', array('id' => $courseid)); } } } } }