function xmldb_label_upgrade($oldversion) { global $CFG, $DB; $dbman = $DB->get_manager(); // Moodle v2.2.0 release upgrade line // Put any upgrade step following this // Moodle v2.3.0 release upgrade line // Put any upgrade step following this // Moodle v2.4.0 release upgrade line // Put any upgrade step following this if ($oldversion < 2013021400) { // find all courses that contain labels and reset their cache $modid = $DB->get_field_sql("SELECT id FROM {modules} WHERE name=?", array('label')); if ($modid) { $courses = $DB->get_fieldset_sql('SELECT DISTINCT course ' . 'FROM {course_modules} WHERE module=?', array($modid)); foreach ($courses as $courseid) { rebuild_course_cache($courseid, true); } } // label savepoint reached upgrade_mod_savepoint(true, 2013021400, 'label'); } // Moodle v2.5.0 release upgrade line. // Put any upgrade step following this. // Moodle v2.6.0 release upgrade line. // Put any upgrade step following this. return true; }
/** * Migrate page module data from 1.9 resource_old table to new page table * @return void */ function page_20_migrate() { global $CFG, $DB; require_once "{$CFG->libdir}/filelib.php"; require_once "{$CFG->libdir}/resourcelib.php"; require_once "{$CFG->dirroot}/course/lib.php"; if (!file_exists("{$CFG->dirroot}/mod/resource/db/upgradelib.php")) { // bad luck, somebody deleted resource module return; } require_once "{$CFG->dirroot}/mod/resource/db/upgradelib.php"; // create resource_old table and copy resource table there if needed if (!resource_20_prepare_migration()) { // no modules or fresh install return; } $fs = get_file_storage(); if ($candidates = $DB->get_recordset('resource_old', array('type' => 'html', 'migrated' => 0))) { foreach ($candidates as $candidate) { page_20_migrate_candidate($candidate, $fs, FORMAT_HTML); } $candidates->close(); } if ($candidates = $DB->get_recordset('resource_old', array('type' => 'text', 'migrated' => 0))) { foreach ($candidates as $candidate) { page_20_migrate_candidate($candidate, $fs, $candidate->reference); } $candidates->close(); } // clear all course modinfo caches rebuild_course_cache(0, true); }
function xmldb_ezproxy_upgrade($oldversion = 0) { global $CFG, $THEME, $db; $result = true; /// And upgrade begins here. For each one, you'll need one /// block of code similar to the next one. Please, delete /// this comment lines once this file start handling proper /// upgrade code. if ($result && $oldversion < 2009042403) { /// Rebuild the course cache of every course which uses one of these modules in it to get /// the new link. if ($courseids = get_records_menu('ezproxy', '', '', 'course ASC', 'id, course')) { /// Just get the unique course ID values. $courseids = array_unique(array_values($courseids)); if (!empty($courseids)) { require_once $CFG->dirroot . '/course/lib.php'; foreach ($courseids as $courseid) { rebuild_course_cache($courseid); // Does not return a bool } } } } if ($result && $oldversion < 2009042404) { $table = new XMLDBTable('ezproxy'); $field = new XMLDBField('serverurl'); $field->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, '', 'name'); $result = change_field_type($table, $field); } return $result; }
/** * Migrate widgetspace module data from 1.9 resource_old table to new widgetspace table * @return void */ function widgetspace_20_migrate() { global $CFG, $DB; require_once "{$CFG->libdir}/filelib.php"; require_once "{$CFG->libdir}/resourcelib.php"; require_once "{$CFG->dirroot}/course/lib.php"; if (!file_exists("{$CFG->dirroot}/mod/resource/db/upgradelib.php")) { // bad luck, somebody deleted resource module return; } require_once "{$CFG->dirroot}/mod/resource/db/upgradelib.php"; // create resource_old table and copy resource table there if needed if (!resource_20_prepare_migration()) { // no modules or fresh install return; } $fs = get_file_storage(); $candidates = $DB->get_recordset('resource_old', array('type' => 'html', 'migrated' => 0)); foreach ($candidates as $candidate) { widgetspace_20_migrate_candidate($candidate, $fs, FORMAT_HTML); } $candidates->close(); $candidates = $DB->get_recordset('resource_old', array('type' => 'text', 'migrated' => 0)); foreach ($candidates as $candidate) { //there might be some rubbish instead of format int value $format = (int) $candidate->reference; if ($format < 0 or $format > 4) { $format = FORMAT_MOODLE; } widgetspace_20_migrate_candidate($candidate, $fs, $format); } $candidates->close(); // clear all course modinfo caches rebuild_course_cache(0, true); }
/** * This function tests the render_menu() method's ability to display * the menus properly and handle a number of special cases that should * arise. * * Note: for some reason (I think the cache gets flushed), all of the * section tests must go in here. * */ public function test_renderarray() { global $DB; $courseid = $this->testcourseid; $course = $DB->get_record('course', array('id' => $courseid)); rebuild_course_cache($courseid); $section = 1; $thissection = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $section)); $cm = new collapsed_menu($course, $section, false); $cm->render_menu(); // Answers are below. $labelids = array($this->section1labelids[1], $this->section1labelids[2], $this->section1labelids[3], $this->section1labelids[0], $this->section1labelids[4]); $closeids = array("N" . $this->section1labelids[2], "N" . $this->section1labelids[3], "N" . $this->section1labelids[4], "N" . $this->section1labelids[4], "N" . $this->section1contentids[8]); $depthindices = array(2, 2, 2, 1, 1); $depthvalues = array(2, 2, 2, 0, 0); $numlabels = 5; // Test labelinfo values. $labelinfo = $cm->get_label_info(); for ($i = 0; $i < $numlabels; $i++) { $this->assertEquals($labelinfo->labelid[$i], $labelids[$i]); $this->assertEquals($labelinfo->closeid[$i], $closeids[$i]); $this->assertEquals($labelinfo->depthindex[$i], $depthindices[$i]); $this->assertEquals($labelinfo->depthvalue[$i], $depthvalues[$i]); } // Now for mod depths: answers. $modids = array($this->section1contentids[0], $this->section1contentids[1], $this->section1contentids[2], $this->section1contentids[3], $this->section1contentids[4], $this->section1contentids[5], $this->section1contentids[6], $this->section1contentids[7], $this->section1contentids[8]); $depths = array(0, 0, 0, 1, 0, 0, 0, 0, 0); $nummods = 9; // Test the moddepth values. $moddepths = $cm->get_mod_depths(); for ($i = 0; $i < $nummods; $i++) { $this->assertEquals($moddepths->modid[$i], $modids[$i]); $this->assertEquals($moddepths->moddepth[$i], $depths[$i]); } // Change section to 2, it tests the "inclusive" label end. $section = 2; $thissection = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $section)); $cm = new collapsed_menu($course, $section, false); $cm->render_menu(); $labelinfo = $cm->get_label_info(); $this->assertEquals($labelinfo->labelid[0], $this->section2labelids[0]); $this->assertEquals($labelinfo->closeid[0], "I" . $this->section2contentids[0]); // Go with section 3 next -> this section "reverses" the last entries. $section = 3; $thissection = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $section)); $cm = new collapsed_menu($course, $section, false); $cm->render_menu(); $labelids = array($this->section3labelids[0], $this->section3labelids[2], $this->section3labelids[3], $this->section3labelids[1], $this->section3labelids[4], $this->section3labelids[6], $this->section3labelids[5], $this->section3labelids[7], $this->section3labelids[8]); $closeids = array("N" . $this->section3labelids[1], "N" . $this->section3labelids[3], "N" . $this->section3labelids[4], "N" . $this->section3labelids[4], "N" . $this->section3labelids[5], "N" . $this->section3labelids[7], "I" . $this->section3contentids[0], "I" . $this->section3contentids[0], "I" . $this->section3contentids[0]); $depthindices = array(1, 2, 2, 1, 1, 2, 1, 2, 3); $depthvalues = array(0, 1, 1, 0, 0, 1, 0, 1, 2); $numlabels = 9; $labelinfo = $cm->get_label_info(); for ($i = 0; $i < $numlabels; $i++) { $this->assertEquals($labelinfo->labelid[$i], $labelids[$i]); $this->assertEquals($labelinfo->closeid[$i], $closeids[$i]); $this->assertEquals($labelinfo->depthindex[$i], $depthindices[$i]); $this->assertEquals($labelinfo->depthvalue[$i], $depthvalues[$i]); } }
/** * Create a section. */ public static function create_section($course, $section) { global $DB; $section = (object) $section; $section->course = $course->id; $DB->insert_record('course_sections', $section); rebuild_course_cache($course->id, true); }
function resource_upgrade($oldversion) { // This function does anything necessary to upgrade // older versions to match current functionality global $CFG; if ($oldversion < 2004013101) { modify_database("", "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('resource', 'update', 'resource', 'name');"); modify_database("", "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('resource', 'add', 'resource', 'name');"); } if ($oldversion < 2004071000) { table_column("resource", "", "popup", "text", "", "", "", "", "alltext"); if ($resources = get_records_select("resource", "type='3' OR type='5'", "", "id, alltext")) { foreach ($resources as $resource) { $resource->popup = addslashes($resource->alltext); $resource->alltext = ""; if (!update_record("resource", $resource)) { notify("Error updating popup field for resource id = {$resource->id}"); } } } require_once "{$CFG->dirroot}/course/lib.php"; rebuild_course_cache(); } if ($oldversion < 2004071300) { table_column("resource", "", "options", "varchar", "255", "", "", "", "popup"); } if ($oldversion < 2004071303) { table_column("resource", "type", "type", "varchar", "30", "", "", "", ""); modify_database("", "UPDATE prefix_resource SET type='reference' WHERE type='1';"); modify_database("", "UPDATE prefix_resource SET type='file', options='frame' WHERE type='2';"); modify_database("", "UPDATE prefix_resource SET type='file' WHERE type='3';"); modify_database("", "UPDATE prefix_resource SET type='text', options='0' WHERE type='4';"); modify_database("", "UPDATE prefix_resource SET type='file' WHERE type='5';"); modify_database("", "UPDATE prefix_resource SET type='html' WHERE type='6';"); modify_database("", "UPDATE prefix_resource SET type='file' WHERE type='7';"); modify_database("", "UPDATE prefix_resource SET type='text', options='3' WHERE type='8';"); modify_database("", "UPDATE prefix_resource SET type='directory' WHERE type='9';"); } if ($oldversion < 2004080801) { modify_database("", "UPDATE prefix_resource SET alltext=reference,type='html' WHERE type='reference';"); rebuild_course_cache(); } if ($oldversion < 2004111200) { //drop first to avoid conflicts when upgrading execute_sql("DROP INDEX {$CFG->prefix}resource_course_idx;", false); modify_database('', 'CREATE INDEX prefix_resource_course_idx ON prefix_resource (course);'); } if ($oldversion < 2005041100) { // replace wiki-like with markdown include_once "{$CFG->dirroot}/lib/wiki_to_markdown.php"; $wtm = new WikiToMarkdown(); $wtm->update('resource', 'alltext', 'options'); } ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions. return true; }
/** * Migrate folder module data from 1.9 resource_old table to new older table * @return void */ function folder_20_migrate() { global $CFG, $DB; require_once "{$CFG->libdir}/filelib.php"; require_once "{$CFG->dirroot}/course/lib.php"; if (!file_exists("{$CFG->dirroot}/mod/resource/db/upgradelib.php")) { // bad luck, somebody deleted resource module return; } require_once "{$CFG->dirroot}/mod/resource/db/upgradelib.php"; // create resource_old table and copy resource table there if needed if (!resource_20_prepare_migration()) { // no modules or fresh install return; } if (!($candidates = $DB->get_recordset('resource_old', array('type' => 'directory', 'migrated' => 0)))) { return; } $fs = get_file_storage(); foreach ($candidates as $candidate) { upgrade_set_timeout(); $directory = '/' . trim($candidate->reference, '/') . '/'; $directory = str_replace('//', '/', $directory); $folder = new object(); $folder->course = $candidate->course; $folder->name = $candidate->name; $folder->intro = $candidate->intro; $folder->introformat = $candidate->introformat; $folder->revision = 1; $folder->timemodified = time(); if (!($folder = resource_migrate_to_module('folder', $candidate, $folder))) { continue; } // copy files in given directory, skip moddata and backups! $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid); $coursecontext = get_context_instance(CONTEXT_COURSE, $candidate->course); $files = $fs->get_directory_files($coursecontext->id, 'course_content', 0, $directory, true, true); $file_record = array('contextid' => $context->id, 'filearea' => 'folder_content', 'itemid' => 0); foreach ($files as $file) { $path = $file->get_filepath(); if (stripos($path, '/backupdata/') === 0 or stripos($path, '/moddata/') === 0) { // do not publish protected data! continue; } $relpath = substr($path, strlen($directory) - 1); // keep only subfolder paths $file_record['filepath'] = $relpath; $fs->create_file_from_storedfile($file_record, $file); } } $candidates->close(); // clear all course modinfo caches rebuild_course_cache(0, true); }
/** * Create new assign module instance * @param array|stdClass $record * @param array $options (mostly course_module properties) * @return stdClass activity record with extra cmid field */ public function create_instance($record = null, array $options = null) { global $CFG; require_once("$CFG->dirroot/mod/assign/lib.php"); $this->instancecount++; $i = $this->instancecount; $record = (object)(array)$record; $options = (array)$options; if (empty($record->course)) { throw new coding_exception('module generator requires $record->course'); } $defaultsettings = array( 'name' => get_string('pluginname', 'assign').' '.$i, 'intro' => 'Test assign ' . $i, 'introformat' => FORMAT_MOODLE, 'alwaysshowdescription' => 1, 'submissiondrafts' => 1, 'requiresubmissionstatement' => 0, 'sendnotifications' => 0, 'sendlatenotifications' => 0, 'duedate' => 0, 'allowsubmissionsfromdate' => 0, 'grade' => 100, 'cutoffdate' => 0, 'teamsubmission' => 0, 'requireallteammemberssubmit' => 0, 'teamsubmissiongroupingid' => 0, 'blindmarking' => 0, 'cmidnumber' => '', 'attemptreopenmethod' => 'none', 'maxattempts' => -1 ); foreach ($defaultsettings as $name => $value) { if (!isset($record->{$name})) { $record->{$name} = $value; } } $record->coursemodule = $this->precreate_course_module($record->course, $options); $id = assign_add_instance($record, null); rebuild_course_cache($record->course, true); return $this->post_add_instance($id, $record->coursemodule); }
function FN_update_course($form, $oldformat = false) { global $CFG; /// Updates course specific variables. /// Variables are: 'showsection0', 'showannouncements'. $config_vars = array('showsection0', 'showannouncements', 'sec0title', 'showhelpdoc', 'showclassforum', 'showclasschat', 'logo', 'mycourseblockdisplay', 'showgallery', 'gallerydefault', 'usesitegroups', 'mainheading', 'topicheading', 'activitytracking', 'ttmarking', 'ttgradebook', 'ttdocuments', 'ttstaff', 'defreadconfirmmess', 'usemandatory', 'expforumsec'); foreach ($config_vars as $config_var) { if ($varrec = get_record('course_config_FN', 'courseid', $form->id, 'variable', $config_var)) { $varrec->value = $form->{$config_var}; update_record('course_config_FN', $varrec); } else { $varrec->courseid = $form->id; $varrec->variable = $config_var; $varrec->value = $form->{$config_var}; insert_record('course_config_FN', $varrec); } } /// We need to have the sections created ahead of time for the weekly nav to work, /// so check and create here. if (!($sections = get_all_sections($form->id))) { $sections = array(); } for ($i = 0; $i <= $form->numsections; $i++) { if (empty($sections[$i])) { $section = new Object(); $section->course = $form->id; // Create a new section structure $section->section = $i; $section->summary = ""; $section->visible = 1; if (!($section->id = insert_record("course_sections", $section))) { notify("Error inserting new section!"); } } } /// Check for a change to an FN format. If so, set some defaults as well... if ($oldformat != 'FN') { /// Set the news (announcements) forum to no force subscribe, and no posts or discussions. require_once $CFG->dirroot . '/mod/forum/lib.php'; $news = forum_get_course_forum($form->id, 'news'); $news->open = 0; $news->forcesubscribe = 0; update_record('forum', $news); } rebuild_course_cache($form->id); }
public function execute() { global $CFG; require_once $CFG->dirroot . '/lib/modinfolib.php'; $options = $this->expandedOptions; if (!isset($this->arguments[0]) && !$options['all']) { cli_error("Either run with -a for all courses or provide course id as an argument."); } if (isset($this->arguments[0])) { rebuild_course_cache($this->arguments[0]); echo "Succesfully rebuilt cache for course " . $this->arguments[0] . "\n"; } if ($options['all']) { rebuild_course_cache(); exit("Succesfully rebuilt all course caches\n"); } }
function FN_update_course($form, $oldformat = false, $resubmission = false) { global $CFG, $DB, $OUTPUT; $config_vars = array('showsection0', 'sec0title', 'mainheading', 'topicheading', 'maxtabs'); foreach ($config_vars as $config_var) { if ($varrec = $DB->get_record('course_config_fn', array('courseid' => $form->id, 'variable' => $config_var))) { $varrec->value = $form->{$config_var}; $DB->update_record('course_config_fn', $varrec); } else { $varrec->courseid = $form->id; $varrec->variable = $config_var; $varrec->value = $form->{$config_var}; $DB->insert_record('course_config_fn', $varrec); } } /// We need to have the sections created ahead of time for the weekly nav to work, /// so check and create here. if (!($sections = get_all_sections($form->id))) { $sections = array(); } for ($i = 0; $i <= $form->numsections; $i++) { if (empty($sections[$i])) { $section = new Object(); $section->course = $form->id; // Create a new section structure $section->section = $i; $section->summary = ""; $section->visible = 1; if (!($section->id = $DB->insert_record("course_sections", $section))) { $OUTPUT->notification("Error inserting new section!"); } } } /// Check for a change to an FN format. If so, set some defaults as well... if ($oldformat != 'FN') { /// Set the news (announcements) forum to no force subscribe, and no posts or discussions. require_once $CFG->dirroot . '/mod/forum/lib.php'; $news = forum_get_course_forum($form->id, 'news'); $news->open = 0; $news->forcesubscribe = 0; $DB->update_record('forum', $news); } rebuild_course_cache($form->id); }
/** * Add a new section with the provided title and (optional) summary * * @return string */ public function addsection_action() { global $CFG, $PAGE, $DB; require_once $CFG->dirroot . '/course/lib.php'; $sectioname = required_param('newsection', PARAM_TEXT); $summary = optional_param('summary', '', PARAM_RAW); require_sesskey(); $courseid = $PAGE->context->get_course_context()->instanceid; $course = course_get_format($courseid)->get_course(); $course->numsections++; course_get_format($course)->update_course_format_options(array('numsections' => $course->numsections)); course_create_sections_if_missing($course, range(0, $course->numsections)); $modinfo = get_fast_modinfo($course); $section = $modinfo->get_section_info($course->numsections, MUST_EXIST); $DB->set_field('course_sections', 'name', $sectioname, array('id' => $section->id)); $DB->set_field('course_sections', 'summary', $summary, array('id' => $section->id)); $DB->set_field('course_sections', 'summaryformat', FORMAT_HTML, array('id' => $section->id)); rebuild_course_cache($course->id); redirect(course_get_url($course, $section->section)); }
/** * Given an object containing all the necessary data, * (defined by the form in mod.html) this function * will update an existing instance with new data. */ function customlabel_update_instance($customlabel) { global $CFG; $oldinstance = get_record('customlabel', 'id', $customlabel->instance); if ($oldinstance->labelclass != $customlabel->labelclass) { $customlabel->content = ''; $customlabel->name = ''; } else { $customlabel->content = json_encode($customlabel); $instance = customlabel_load_class($customlabel); $instance->process_form_fields(); $instance->preprocess_data(); $customlabel->name = $instance->get_name(); } $customlabel->timemodified = time(); $customlabel->id = $customlabel->instance; $customlabel = customlabel_addslashes_fields($customlabel); $result = update_record('customlabel', $customlabel); // needed to update modinfo rebuild_course_cache(); return $result; }
/** * Forum database upgrade script. * @package mod * @subpackage forumng * @copyright 2011 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ function xmldb_forumng_upgrade($oldversion = 0) { global $CFG, $THEME, $DB; $dbman = $DB->get_manager(); if ($oldversion < 2012070900) { // Changed format of modinfo cache, so need to rebuild all courses. rebuild_course_cache(0, true); upgrade_mod_savepoint(true, 2012070900, 'forumng'); } if ($oldversion < 2012102601) { // Define field gradingscale to be added to forumng. $table = new xmldb_table('forumng'); $field = new xmldb_field('gradingscale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'grading'); // Launch add field gradingscale. if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Changed format of modinfo cache, so need to rebuild all courses. rebuild_course_cache(0, true); // ForumNG savepoint reached. upgrade_mod_savepoint(true, 2012102601, 'forumng'); } return true; }
require_capability('moodle/course:manageactivities', $modcontext); require_once $CFG->libdir . '/gradelib.php'; $cm = get_coursemodule_from_id('', $id, 0, false, MUST_EXIST); $module = new stdClass(); $module->id = $cm->instance; // Escape strings as they would be by mform if (!empty($CFG->formatstringstriptags)) { $module->name = clean_param($title, PARAM_TEXT); } else { $module->name = clean_param($title, PARAM_CLEANHTML); } if (!empty($module->name)) { $DB->update_record($cm->modname, $module); $cm->name = $module->name; \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); rebuild_course_cache($cm->course); } else { $module->name = $cm->name; } // Attempt to update the grade item if relevant $grademodule = $DB->get_record($cm->modname, array('id' => $cm->instance)); $grademodule->cmidnumber = $cm->idnumber; $grademodule->modname = $cm->modname; grade_update_mod_grades($grademodule); // We need to return strings after they've been through filters for multilang $stringoptions = new stdClass(); $stringoptions->context = $coursecontext; echo json_encode(array('instancename' => html_entity_decode(format_string($module->name, true, $stringoptions)))); break; } break;
// Form cancelled, return to course. redirect(course_get_url($course, $section, array('sr' => $sectionreturn))); } else { if ($data = $mform->get_data()) { // Data submitted and validated, update and return to course. $DB->update_record('course_sections', $data); rebuild_course_cache($course->id, true); if (isset($data->section)) { // Usually edit form does not change relative section number but just in case. $sectionnum = $data->section; } if (!empty($CFG->enableavailability)) { // Update grade and completion conditions. $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum); condition_info_section::update_section_from_form($sectioninfo, $data); rebuild_course_cache($course->id, true); } course_get_format($course->id)->update_section_format_options($data); // Set section info, as this might not be present in form_data. if (!isset($data->section)) { $data->section = $sectionnum; } // Trigger an event for course section update. /*$event = \core\event\course_section_updated::create( array( 'objectid' => $data->id, 'courseid' => $course->id, 'context' => $context, 'other' => array('sectionnum' => $data->section) ) );
function data_upgrade($oldversion) { /// This function does anything necessary to upgrade /// older versions to match current functionality global $CFG; if ($oldversion < 2006011900) { table_column("data_content", "", "content1", "longtext", "", "", "", "not null"); table_column("data_content", "", "content2", "longtext", "", "", "", "not null"); table_column("data_content", "", "content3", "longtext", "", "", "", "not null"); table_column("data_content", "", "content4", "longtext", "", "", "", "not null"); } if ($oldversion < 2006011901) { table_column("data", "", "approval", "tinyint", "4"); table_column("data_records", "", "approved", "tinyint", "4"); } if ($oldversion < 2006020801) { table_column("data", "", "scale", "integer", "10", "signed"); table_column("data", "", "assessed", "integer", "10"); table_column("data", "", "assesspublic", "integer", "4"); } if ($oldversion < 2006022700) { table_column("data_comments", "", "created", "integer", "10"); table_column("data_comments", "", "modified", "integer", "10"); } if ($oldversion < 2006030700) { modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('data', 'add', 'data', 'name')"); modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('data', 'update', 'data', 'name')"); modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('data', 'record delete', 'data', 'name')"); modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('data', 'fields add', 'data_fields', 'name')"); modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('data', 'fields update', 'data_fields', 'name')"); modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('data', 'templates saved', 'data', 'name')"); modify_database('', "INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('data', 'templates def', 'data', 'name')"); } if ($oldversion < 2006032700) { table_column('data', '', 'defaultsort', 'integer', '10', 'unsigned', '0'); table_column('data', '', 'defaultsortdir', 'tinyint', '4', 'unsigned', '0', 'not null', 'defaultsort'); table_column('data', '', 'editany', 'tinyint', '4', 'unsigned', '0', 'not null', 'defaultsortdir'); } if ($oldversion < 2006032900) { table_column('data', '', 'csstemplate', 'text', '', '', '', 'not null', 'rsstemplate'); } if ($oldversion < 2006050500) { // 2 fields have got no default null values table_column('data_comments', 'content', 'content', 'text', '', '', '', 'not null'); table_column('data_fields', 'description', 'description', 'text', '', '', '', 'not null'); table_column('data_fields', 'param1', 'param1', 'text', '', '', '', 'not null'); table_column('data_fields', 'param2', 'param2', 'text', '', '', '', 'not null'); table_column('data_fields', 'param3', 'param3', 'text', '', '', '', 'not null'); table_column('data_fields', 'param4', 'param4', 'text', '', '', '', 'not null'); table_column('data_fields', 'param5', 'param5', 'text', '', '', '', 'not null'); table_column('data_fields', 'param6', 'param6', 'text', '', '', '', 'not null'); table_column('data_fields', 'param7', 'param7', 'text', '', '', '', 'not null'); table_column('data_fields', 'param8', 'param8', 'text', '', '', '', 'not null'); table_column('data_fields', 'param9', 'param9', 'text', '', '', '', 'not null'); table_column('data_fields', 'param10', 'param10', 'text', '', '', '', 'not null'); } if ($oldversion < 2006052400) { table_column('data', '', 'rsstitletemplate', 'text', '', '', '', 'not null', 'rsstemplate'); } if ($oldversion < 2006081700) { table_column('data', '', 'jstemplate', 'text', '', '', '', 'not null', 'csstemplate'); } if ($oldversion < 2006092000) { // Upgrades for new roles and capabilities support. require_once $CFG->dirroot . '/mod/data/lib.php'; $datamod = get_record('modules', 'name', 'data'); if ($data = get_records('data')) { if (!($teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW))) { notify('Default teacher role was not found. Roles and permissions ' . 'for all your forums will have to be manually set after ' . 'this upgrade.'); } if (!($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW))) { notify('Default student role was not found. Roles and permissions ' . 'for all your forums will have to be manually set after ' . 'this upgrade.'); } foreach ($data as $d) { if (!data_convert_to_roles($d, $teacherroles, $studentroles)) { notify('Data with id ' . $d->id . ' was not upgraded'); } } // We need to rebuild all the course caches to refresh the state of // the forum modules. include_once "{$CFG->dirroot}/course/lib.php"; rebuild_course_cache(); } // End if. modify_database('', 'ALTER TABLE prefix_data DROP COLUMN participants;'); modify_database('', 'ALTER TABLE prefix_data DROP COLUMN assesspublic;'); modify_database('', 'ALTER TABLE prefix_data DROP COLUMN ratings;'); } if ($oldversion < 2006092302) { // Changing some TEXT fields to NULLable and no default execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE singletemplate singletemplate text NULL AFTER rssarticles"); execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE listtemplate listtemplate text NULL AFTER singletemplate"); execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE listtemplateheader listtemplateheader text NULL AFTER listtemplate"); execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE listtemplatefooter listtemplatefooter text NULL AFTER listtemplateheader"); execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE addtemplate addtemplate text NULL AFTER listtemplatefooter"); execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE rsstemplate rsstemplate text NULL AFTER addtemplate"); execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE rsstitletemplate rsstitletemplate text NULL AFTER rsstemplate"); execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE csstemplate csstemplate text NULL AFTER rsstitletemplate"); execute_sql("ALTER TABLE {$CFG->prefix}data CHANGE jstemplate jstemplate text NULL AFTER csstemplate"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param1 param1 text NULL AFTER description"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param2 param2 text NULL AFTER param1"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param3 param3 text NULL AFTER param2"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param4 param4 text NULL AFTER param3"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param5 param5 text NULL AFTER param4"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param6 param6 text NULL AFTER param5"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param7 param7 text NULL AFTER param6"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param8 param8 text NULL AFTER param7"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param9 param9 text NULL AFTER param8"); execute_sql("ALTER TABLE {$CFG->prefix}data_fields CHANGE param10 param10 text NULL AFTER param9"); execute_sql("ALTER TABLE {$CFG->prefix}data_content CHANGE content content longtext NULL AFTER recordid"); execute_sql("ALTER TABLE {$CFG->prefix}data_content CHANGE content1 content1 longtext NULL AFTER content"); execute_sql("ALTER TABLE {$CFG->prefix}data_content CHANGE content2 content2 longtext NULL AFTER content1"); execute_sql("ALTER TABLE {$CFG->prefix}data_content CHANGE content3 content3 longtext NULL AFTER content2"); execute_sql("ALTER TABLE {$CFG->prefix}data_content CHANGE content4 content4 longtext NULL AFTER content3"); } ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions. return true; }
/** * Delete everything related to this instance if you have been using persistent storage other than the configdata field. * * @return bool Status result */ public function instance_delete() { global $CFG, $DB; if (empty($this->instance) || !isset($this->config->section)) { return true; } // Cleanup the section created by this block and any course modules. $sql = "SELECT cm.id, cm.instance, mm.name AS modname\n FROM {course_sections} cs\n INNER JOIN {course_modules} cm ON cm.section = cs.id\n INNER JOIN {modules} mm ON mm.id = cm.module\n WHERE cs.section = :section\n AND cs.course = :course"; $params = array('section' => $this->config->section, 'course' => $this->page->course->id); if ($mods = $DB->get_records_sql($sql, $params)) { foreach ($mods as $mod) { $mod_lib = $CFG->dirroot . '/mod/' . $mod->modname . '/lib.php'; if (file_exists($mod_lib)) { require_once $mod_lib; $delete_func = $mod->modname . '_delete_instance'; if (function_exists($delete_func)) { $delete_func($mod->instance); } } } } // Delete the course section for this block instance $DB->delete_records('course_sections_availability', array('coursesectionid' => $this->config->section_id)); $DB->delete_records('course_sections_avail_fields', array('coursesectionid' => $this->config->section_id)); $DB->delete_records('course_sections', array('id' => $this->config->section_id)); rebuild_course_cache($this->page->course->id, true); }
protected function define_execution() { global $CFG, $DB; // Note: This code runs even if availability is disabled when restoring. // That will ensure that if you later turn availability on for the site, // there will be no incorrect IDs. (It doesn't take long if the restored // data does not contain any availability information.) // Get modinfo with all data after resetting cache. rebuild_course_cache($this->get_courseid(), true); $modinfo = get_fast_modinfo($this->get_courseid()); // Update all sections that were restored. $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'course_section'); $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'newitemid'); $sectionsbyid = null; foreach ($rs as $rec) { if (is_null($sectionsbyid)) { $sectionsbyid = array(); foreach ($modinfo->get_section_info_all() as $section) { $sectionsbyid[$section->id] = $section; } } if (!array_key_exists($rec->newitemid, $sectionsbyid)) { // If the section was not fully restored for some reason // (e.g. due to an earlier error), skip it. $this->get_logger()->process('Section not fully restored: id ' . $rec->newitemid, backup::LOG_WARNING); continue; } $section = $sectionsbyid[$rec->newitemid]; if (!is_null($section->availability)) { $info = new \core_availability\info_section($section); $info->update_after_restore($this->get_restoreid(), $this->get_courseid(), $this->get_logger()); } } $rs->close(); // Update all modules that were restored. $params = array('backupid' => $this->get_restoreid(), 'itemname' => 'course_module'); $rs = $DB->get_recordset('backup_ids_temp', $params, '', 'newitemid'); foreach ($rs as $rec) { if (!array_key_exists($rec->newitemid, $modinfo->cms)) { // If the module was not fully restored for some reason // (e.g. due to an earlier error), skip it. $this->get_logger()->process('Module not fully restored: id ' . $rec->newitemid, backup::LOG_WARNING); continue; } $cm = $modinfo->get_cm($rec->newitemid); if (!is_null($cm->availability)) { $info = new \core_availability\info_module($cm); $info->update_after_restore($this->get_restoreid(), $this->get_courseid(), $this->get_logger()); } } $rs->close(); }
/** * Tests the filter_user_list() and get_user_list_sql() functions. */ public function test_filter_user_list() { global $CFG, $DB; require_once $CFG->dirroot . '/course/lib.php'; $this->resetAfterTest(); $CFG->enableavailability = true; // Create a course with 2 sections and 2 pages and 3 users. // Availability is set up initially on the 'page/section 2' items. $generator = $this->getDataGenerator(); $course = $generator->create_course(array('numsections' => 2), array('createsections' => true)); $u1 = $generator->create_user(); $u2 = $generator->create_user(); $u3 = $generator->create_user(); $allusers = array($u1->id => $u1, $u2->id => $u2, $u3->id => $u3); $generator->enrol_user($u1->id, $course->id); $generator->enrol_user($u2->id, $course->id); $generator->enrol_user($u3->id, $course->id); $pagegen = $generator->get_plugin_generator('mod_page'); $page = $pagegen->create_instance(array('course' => $course)); $page2 = $pagegen->create_instance(array('course' => $course, 'availability' => '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u2->id . ',' . $u3->id . ']}]}')); $modinfo = get_fast_modinfo($course); $section = $modinfo->get_section_info(1); $section2 = $modinfo->get_section_info(2); $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u1->id . ',' . $u2->id . ']}]}', array('id' => $section2->id)); moveto_module($modinfo->get_cm($page2->cmid), $section2); // With no restrictions, returns full list. $info = new info_module($modinfo->get_cm($page->cmid)); $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers))); $this->assertEquals(array('', array()), $info->get_user_list_sql(true)); // Set an availability restriction in database for section 1. // For the section we set it so it doesn't support filters; for the // module we have a filter. $DB->set_field('course_sections', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","a":false}]}', array('id' => $section->id)); $DB->set_field('course_modules', 'availability', '{"op":"|","show":true,"c":[{"type":"mock","filter":[' . $u3->id . ']}]}', array('id' => $page->cmid)); rebuild_course_cache($course->id, true); $modinfo = get_fast_modinfo($course); // Now it should work (for the module). $info = new info_module($modinfo->get_cm($page->cmid)); $expected = array($u3->id); $this->assertEquals($expected, array_keys($info->filter_user_list($allusers))); list($sql, $params) = $info->get_user_list_sql(); $result = $DB->get_fieldset_sql($sql, $params); sort($result); $this->assertEquals($expected, $result); $info = new info_section($modinfo->get_section_info(1)); $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers))); $this->assertEquals(array('', array()), $info->get_user_list_sql(true)); // With availability disabled, module returns full list too. $CFG->enableavailability = false; $info = new info_module($modinfo->get_cm($page->cmid)); $this->assertEquals(array($u1->id, $u2->id, $u3->id), array_keys($info->filter_user_list($allusers))); $this->assertEquals(array('', array()), $info->get_user_list_sql(true)); // Check the other section... $CFG->enableavailability = true; $info = new info_section($modinfo->get_section_info(2)); $expected = array($u1->id, $u2->id); $this->assertEquals($expected, array_keys($info->filter_user_list($allusers))); list($sql, $params) = $info->get_user_list_sql(true); $result = $DB->get_fieldset_sql($sql, $params); sort($result); $this->assertEquals($expected, $result); // And the module in that section - which has combined the section and // module restrictions. $info = new info_module($modinfo->get_cm($page2->cmid)); $expected = array($u2->id); $this->assertEquals($expected, array_keys($info->filter_user_list($allusers))); list($sql, $params) = $info->get_user_list_sql(true); $result = $DB->get_fieldset_sql($sql, $params); sort($result); $this->assertEquals($expected, $result); }
/** * Moves section to the specified position * * @param int|section_info $section * @param int|section_info $parent * @param null|int|section_info $before * @return int new section number */ protected function move_section($section, $parent, $before = null) { global $DB; $section = $this->get_section($section); $parent = $this->get_section($parent); $newsectionnumber = $section->section; if (!$this->can_move_section_to($section, $parent, $before)) { return $newsectionnumber; } if ($section->visible != $parent->visible && $section->parent != $parent->section) { // section is changing parent and new parent has different visibility than the section if ($section->visible) { // visible section is moved under hidden parent $updatesectionvisible = 0; $updatesectionvisibleold = 1; } else { // hidden section is moved under visible parent if ($section->visibleold) { $updatesectionvisible = 1; $updatesectionvisibleold = 1; } } } // find the changes in the sections numbering $origorder = array(); foreach ($this->get_sections() as $subsection) { $origorder[$subsection->id] = $subsection->section; } $neworder = array(); $this->reorder_sections($neworder, 0, $section->section, $parent, $before); if (count($origorder) != count($neworder)) { die('Error in sections hierarchy'); // TODO } $changes = array(); foreach ($origorder as $id => $num) { if ($num == $section->section) { $newsectionnumber = $neworder[$id]; } if ($num != $neworder[$id]) { $changes[$id] = array('old' => $num, 'new' => $neworder[$id]); if ($num && $this->get_course()->marker == $num) { $changemarker = $neworder[$id]; } } if ($this->get_section_number($parent) === $num) { $newparentnum = $neworder[$id]; } } if (empty($changes) && $newparentnum == $section->parent) { return $newsectionnumber; } // build array of required changes in field 'parent' // $changeparent[sectionid] = newsectionnum; $changeparent = array(); foreach ($this->get_sections() as $subsection) { foreach ($changes as $id => $change) { if ($subsection->parent == $change['old']) { $changeparent[$subsection->id] = $change['new']; } } } $changeparent[$section->id] = $newparentnum; // Update all in database in one transaction $transaction = $DB->start_delegated_transaction(); // Update sections numbers in 2 steps to avoid breaking database uniqueness constraint foreach ($changes as $id => $change) { $DB->set_field('course_sections', 'section', -$change['new'], array('id' => $id)); } foreach ($changes as $id => $change) { $DB->set_field('course_sections', 'section', $change['new'], array('id' => $id)); } // change parents of their subsections foreach ($changeparent as $id => $newnum) { $this->update_section_format_options(array('id' => $id, 'parent' => $newnum)); } $transaction->allow_commit(); rebuild_course_cache($this->courseid, true); if (isset($changemarker)) { course_set_marker($this->courseid, $changemarker); } if (isset($updatesectionvisible)) { $this->set_section_visible($newsectionnumber, $updatesectionvisible, $updatesectionvisibleold); } return $newsectionnumber; }
/** * Used to set a config pair and log change * * @param string $name * @param mixed $value Gets converted to string if not null * @return bool Write setting to config table */ public function config_write($name, $value) { global $DB, $USER, $CFG; if ($this->nosave) { return true; } // make sure it is a real change $oldvalue = get_config($this->plugin, $name); $oldvalue = $oldvalue === false ? null : $oldvalue; // normalise $value = is_null($value) ? null : (string) $value; if ($oldvalue === $value) { return true; } // store change set_config($name, $value, $this->plugin); // Some admin settings affect course modinfo if ($this->affectsmodinfo) { // Clear course cache for all courses rebuild_course_cache(0, true); } // log change $log = new stdClass(); $log->userid = during_initial_install() ? 0 : $USER->id; // 0 as user id during install $log->timemodified = time(); $log->plugin = $this->plugin; $log->name = $name; $log->value = $value; $log->oldvalue = $oldvalue; $DB->insert_record('config_log', $log); return true; // BC only }
/** * Used to set a config pair and log change * * @param string $name * @param mixed $value Gets converted to string if not null * @return bool Write setting to config table */ public function config_write($name, $value) { global $DB, $USER, $CFG; if ($this->nosave) { return true; } // make sure it is a real change $oldvalue = get_config($this->plugin, $name); $oldvalue = $oldvalue === false ? null : $oldvalue; // normalise $value = is_null($value) ? null : (string) $value; if ($oldvalue === $value) { return true; } // store change set_config($name, $value, $this->plugin); // Some admin settings affect course modinfo if ($this->affectsmodinfo) { // Clear course cache for all courses rebuild_course_cache(0, true); } $this->add_to_config_log($name, $oldvalue, $value); return true; // BC only }
if (!empty($CFG->customfrontpageinclude)) { include($CFG->customfrontpageinclude); } else if ($SITE->numsections > 0) { if (!$section = $DB->get_record('course_sections', array('course'=>$SITE->id, 'section'=>1))) { $DB->delete_records('course_sections', array('course'=>$SITE->id, 'section'=>1)); // Just in case $section = new stdClass(); $section->course = $SITE->id; $section->section = 1; $section->summary = ''; $section->summaryformat = FORMAT_HTML; $section->sequence = ''; $section->visible = 1; $section->id = $DB->insert_record('course_sections', $section); rebuild_course_cache($SITE->id, true); } if (!empty($section->sequence) or !empty($section->summary) or $editing) { echo $OUTPUT->box_start('generalbox sitetopic'); /// If currently moving a file then show the current clipboard if (ismoving($SITE->id)) { $stractivityclipboard = strip_tags(get_string('activityclipboard', '', $USER->activitycopyname)); echo '<p><font size="2">'; echo "$stractivityclipboard (<a href=\"course/mod.php?cancelcopy=true&sesskey=".sesskey()."\">". get_string('cancel') .'</a>)'; echo '</font></p>'; } $context = get_context_instance(CONTEXT_COURSE, SITEID); $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php', $context->id, 'course', 'section', $section->id);
/** * @global object * @global object * @param int $courseid * @param string $type */ function forum_get_course_forum($courseid, $type) { // How to set up special 1-per-course forums global $CFG, $DB, $OUTPUT; if ($forums = $DB->get_records_select("forum", "course = ? AND type = ?", array($courseid, $type), "id ASC")) { // There should always only be ONE, but with the right combination of // errors there might be more. In this case, just return the oldest one (lowest ID). foreach ($forums as $forum) { return $forum; // ie the first one } } // Doesn't exist, so create one now. $forum->course = $courseid; $forum->type = "$type"; switch ($forum->type) { case "news": $forum->name = get_string("namenews", "forum"); $forum->intro = get_string("intronews", "forum"); $forum->forcesubscribe = FORUM_FORCESUBSCRIBE; $forum->assessed = 0; if ($courseid == SITEID) { $forum->name = get_string("sitenews"); $forum->forcesubscribe = 0; } break; case "social": $forum->name = get_string("namesocial", "forum"); $forum->intro = get_string("introsocial", "forum"); $forum->assessed = 0; $forum->forcesubscribe = 0; break; case "blog": $forum->name = get_string('blogforum', 'forum'); $forum->intro = get_string('introblog', 'forum'); $forum->assessed = 0; $forum->forcesubscribe = 0; break; default: echo $OUTPUT->notification("That forum type doesn't exist!"); return false; break; } $forum->timemodified = time(); $forum->id = $DB->insert_record("forum", $forum); if (! $module = $DB->get_record("modules", array("name" => "forum"))) { echo $OUTPUT->notification("Could not find forum module!!"); return false; } $mod = new stdClass(); $mod->course = $courseid; $mod->module = $module->id; $mod->instance = $forum->id; $mod->section = 0; if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded echo $OUTPUT->notification("Could not add a new course module to the course '" . $courseid . "'"); return false; } if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded echo $OUTPUT->notification("Could not add the new course module to that section"); return false; } $DB->set_field("course_modules", "section", $sectionid, array("id" => $mod->coursemodule)); include_once("$CFG->dirroot/course/lib.php"); rebuild_course_cache($courseid); return $DB->get_record("forum", array("id" => "$forum->id")); }
/** * Create a migrated turnitin assignment in Moodle * * @global type $DB * @global type $CFG * @param array $partids the ids of turnitin assignment to create as parts of new assignment * @param int $courseid * @param string $assignmentname * @return boolean false if failed */ public static function create_migration_assignment($partids, $courseid, $assignmentname) { global $DB, $CFG, $OUTPUT; $config = turnitintooltwo_admin_config(); $partids = (array) $partids; $tempassignment = new turnitintooltwo_assignment(0, '', 'M'); $newassignment = $tempassignment->update_assignment_from_tii($partids); $newassignment["turnitintooltwo"]->course = $courseid; $newassignment["turnitintooltwo"]->name = $assignmentname; $newassignment["turnitintooltwo"]->numparts = count($partids); $newassignment["turnitintooltwo"]->gradedisplay = 1; $newassignment["turnitintooltwo"]->shownonsubmission = 1; $newassignment["turnitintooltwo"]->usegrademark = $config->usegrademark; // Get maximum grade. $newassignment["turnitintooltwo"]->grade = 0; foreach ($newassignment["parts"] as $part) { if ($newassignment["turnitintooltwo"]->grade < $part->maxmarks) { $newassignment["turnitintooltwo"]->grade = $part->maxmarks; } } $turnitintooltwoassignment = new turnitintooltwo_assignment(0, $newassignment["turnitintooltwo"]); if (!($toolid = $DB->insert_record("turnitintooltwo", $turnitintooltwoassignment->turnitintooltwo))) { turnitintooltwo_activitylog(get_string('migrationassignmentcreationerror', 'turnitintooltwo', $courseid), "REQUEST"); return false; } else { turnitintooltwo_activitylog(get_string('migrationassignmentcreated', 'turnitintooltwo', $toolid), "REQUEST"); } $module = $DB->get_record("modules", array("name" => "turnitintooltwo")); $coursemodule = new stdClass(); $coursemodule->course = $courseid; $coursemodule->module = $module->id; $coursemodule->added = time(); $coursemodule->instance = $toolid; $coursemodule->section = 0; include_once $CFG->dirroot . "/course/lib.php"; // Add Course module and get course section. if (!($coursemodule->coursemodule = add_course_module($coursemodule))) { echo $OUTPUT->notification(get_string('migrationassignmenterror1', 'turnitintooltwo', $courseid)); turnitintooltwo_activitylog(get_string('migrationassignmenterror1', 'turnitintooltwo', $courseid), "REQUEST"); return false; } if (is_callable('course_add_cm_to_section')) { if (!($sectionid = course_add_cm_to_section($coursemodule->course, $coursemodule->coursemodule, $coursemodule->section))) { echo $OUTPUT->notification(get_string('migrationassignmenterror2', 'turnitintooltwo', $courseid)); turnitintooltwo_activitylog(get_string('migrationassignmenterror2', 'turnitintooltwo', $courseid), "REQUEST"); return false; } } else { if (!($sectionid = add_mod_to_section($coursemodule))) { echo $OUTPUT->notification(get_string('migrationassignmenterror2', 'turnitintooltwo', $courseid)); turnitintooltwo_activitylog(get_string('migrationassignmenterror2', 'turnitintooltwo', $courseid), "REQUEST"); return false; } } $DB->set_field("course_modules", "section", $sectionid, array("id" => $coursemodule->coursemodule)); rebuild_course_cache($courseid); foreach ($newassignment["parts"] as $part) { $part->turnitintooltwoid = $toolid; $part->deleted = 0; $part->migrated = -1; if ($part->id = $DB->insert_record("turnitintooltwo_parts", $part)) { turnitintooltwo_activitylog(get_string('migrationassignmentpartcreated', 'turnitintooltwo', $part->id), "REQUEST"); } if ($turnitintooltwoassignment->create_event($toolid, $part->partname, $part->dtdue)) { $part->migrated = 1; $DB->update_record("turnitintooltwo_parts", $part); } else { echo $OUTPUT->notification(get_string('migrationassignmenterror3', 'turnitintooltwo', $courseid)); turnitintooltwo_activitylog(get_string('migrationassignmenterror3', 'turnitintooltwo', $courseid), "REQUEST"); } } }
rebuild_course_cache($course->id); $modinfo = get_fast_modinfo($COURSE); debugging('Rebuilding course cache', DEBUG_DEVELOPER); break; } } if (!($sections = $modinfo->get_section_info_all())) { // No sections found $section = new stdClass(); $section->course = $course->id; // Create a default section. $section->section = 0; $section->visible = 1; $section->summaryformat = FORMAT_HTML; $section->id = $DB->insert_record('course_sections', $section); rebuild_course_cache($course->id); $modinfo = get_fast_modinfo($COURSE); if (!($sections = $modinfo->get_section_info_all())) { // Try again print_error('cannotcreateorfindstructs', 'error'); } } // CAUTION, hacky fundamental variable defintion to follow! // Note that because of the way course fromats are constructed though // inclusion we pass parameters around this way.. $displaysection = $section; // Include the actual course format. require $CFG->dirroot . '/course/format/' . $course->format . '/format.php'; // Content wrapper end. echo html_writer::end_tag('div'); // Include course AJAX
/** * Deletes a section * * Do not call this function directly, instead call {@link course_delete_section()} * * @param int|stdClass|section_info $section * @param bool $forcedeleteifnotempty if set to false section will not be deleted if it has modules in it. * @return bool whether section was deleted */ public function delete_section($section, $forcedeleteifnotempty = false) { global $DB; if (!$this->uses_sections()) { // Not possible to delete section if sections are not used. return false; } if (!is_object($section)) { $section = $DB->get_record('course_sections', array('course' => $this->get_courseid(), 'section' => $section), 'id,section,sequence,summary'); } if (!$section || !$section->section) { // Not possible to delete 0-section. return false; } if (!$forcedeleteifnotempty && (!empty($section->sequence) || !empty($section->summary))) { return false; } $course = $this->get_course(); // Remove the marker if it points to this section. if ($section->section == $course->marker) { course_set_marker($course->id, 0); } $lastsection = $DB->get_field_sql('SELECT max(section) from {course_sections} WHERE course = ?', array($course->id)); // Find out if we need to descrease the 'numsections' property later. $courseformathasnumsections = array_key_exists('numsections', $this->get_format_options()); $decreasenumsections = $courseformathasnumsections && $section->section <= $course->numsections; // Move the section to the end. move_section_to($course, $section->section, $lastsection, true); // Delete all modules from the section. foreach (preg_split('/,/', $section->sequence, -1, PREG_SPLIT_NO_EMPTY) as $cmid) { course_delete_module($cmid); } // Delete section and it's format options. $DB->delete_records('course_format_options', array('sectionid' => $section->id)); $DB->delete_records('course_sections', array('id' => $section->id)); rebuild_course_cache($course->id, true); // Descrease 'numsections' if needed. if ($decreasenumsections) { $this->update_course_format_options(array('numsections' => $course->numsections - 1)); } return true; }
protected function define_execution() { global $DB; // Although there is some sort of auto-recovery of missing sections // present in course/formats... here we check that all the sections // from 0 to MAX(section->section) exist, creating them if necessary $maxsection = $DB->get_field('course_sections', 'MAX(section)', array('course' => $this->get_courseid())); // Iterate over all sections for ($i = 0; $i <= $maxsection; $i++) { // If the section $i doesn't exist, create it if (!$DB->record_exists('course_sections', array('course' => $this->get_courseid(), 'section' => $i))) { $sectionrec = array( 'course' => $this->get_courseid(), 'section' => $i); $DB->insert_record('course_sections', $sectionrec); // missing section created } } // Rebuild cache now that all sections are in place rebuild_course_cache($this->get_courseid()); }