Ejemplo n.º 1
0
/**
 * Create a course and either return a $course object
 *
 * 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 array $editoroptions course description editor options
 * @param object $data  - all the data needed for an entry in the 'course' table
 * @return object new course instance
 */
function create_course($data, $editoroptions = NULL)
{
    global $DB;
    //check the categoryid - must be given for all new courses
    $category = $DB->get_record('course_categories', array('id' => $data->category), '*', MUST_EXIST);
    // Check if the shortname already exists.
    if (!empty($data->shortname)) {
        if ($DB->record_exists('course', array('shortname' => $data->shortname))) {
            throw new moodle_exception('shortnametaken', '', '', $data->shortname);
        }
    }
    // Check if the idnumber already exists.
    if (!empty($data->idnumber)) {
        if ($DB->record_exists('course', array('idnumber' => $data->idnumber))) {
            throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
        }
    }
    $data->timecreated = time();
    $data->timemodified = $data->timecreated;
    // place at beginning of any category
    $data->sortorder = 0;
    if ($editoroptions) {
        // summary text is updated later, we need context to store the files first
        $data->summary = '';
        $data->summary_format = FORMAT_HTML;
    }
    if (!isset($data->visible)) {
        // data not from form, add missing visibility info
        $data->visible = $category->visible;
    }
    $data->visibleold = $data->visible;
    $newcourseid = $DB->insert_record('course', $data);
    $context = context_course::instance($newcourseid, MUST_EXIST);
    if ($editoroptions) {
        // Save the files used in the summary editor and store
        $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
        $DB->set_field('course', 'summary', $data->summary, array('id' => $newcourseid));
        $DB->set_field('course', 'summaryformat', $data->summary_format, array('id' => $newcourseid));
    }
    if ($overviewfilesoptions = course_overviewfiles_options($newcourseid)) {
        // Save the course overviewfiles
        $data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0);
    }
    // update course format options
    course_get_format($newcourseid)->update_course_format_options($data);
    $course = course_get_format($newcourseid)->get_course();
    // Setup the blocks
    blocks_add_default_course_blocks($course);
    // Create a default section.
    course_create_sections_if_missing($course, 0);
    fix_course_sortorder();
    // purge appropriate caches in case fix_course_sortorder() did not change anything
    cache_helper::purge_by_event('changesincourse');
    // new context created - better mark it as dirty
    $context->mark_dirty();
    // Save any custom role names.
    save_local_role_names($course->id, (array) $data);
    // set up enrolments
    enrol_course_updated(true, $course, $data);
    // Trigger a course created event.
    $event = \core\event\course_created::create(array('objectid' => $course->id, 'context' => context_course::instance($course->id), 'other' => array('shortname' => $course->shortname, 'fullname' => $course->fullname)));
    $event->trigger();
    return $course;
}
Ejemplo n.º 2
0
 /**
  * Observer for \core\event\course_created event.
  *
  * @param \core\event\course_created $event
  * @return void
  */
 public static function course_created(\core\event\course_created $event)
 {
     global $CFG;
     $course = $event->get_record_snapshot('course', $event->objectid);
     $format = course_get_format($course);
     if ($format->supports_news() && !empty($course->newsitems)) {
         require_once $CFG->dirroot . '/mod/forum/lib.php';
         // Auto create the announcements forum.
         forum_get_course_forum($event->objectid, 'news');
     }
 }
 /**
  * Execute scheduled task
  *
  * @return boolean
  */
 public function execute()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/moodlelib.php';
     require_once $CFG->libdir . '/filestorage/zip_packer.php';
     require_once $CFG->dirroot . '/backup/util/includes/restore_includes.php';
     // Get plugin config
     $local_sandbox_config = get_config('local_sandbox');
     // Counter for restored courses
     $count = 0;
     // Do only when sandbox directory is configured
     if ($local_sandbox_config->coursebackupsdirectory != '') {
         // Do only when sandbox directory exists
         if (is_dir($local_sandbox_config->coursebackupsdirectory)) {
             // Open directory and get all .mbz files
             if ($handle = @opendir($local_sandbox_config->coursebackupsdirectory)) {
                 while (false !== ($file = readdir($handle))) {
                     if (substr($file, -4) == '.mbz' && $file != '.' && $file != '..') {
                         // Get course shortname from filename
                         $shortname = substr($file, 0, -4);
                         echo "\n\t" . get_string('nowprocessing', 'local_sandbox', $shortname) . "\n";
                         // Get existing course information
                         if ($oldcourse = $DB->get_record('course', array('shortname' => $shortname))) {
                             $oldcourseid = $oldcourse->id;
                             $categoryid = $oldcourse->category;
                             $fullname = $oldcourse->fullname;
                         } else {
                             // Output error message for cron listing
                             echo "\n\t" . get_string('skippingnocourse', 'local_sandbox', $shortname) . "\n";
                             // Inform admin
                             local_sandbox_inform_admin(get_string('skippingnocourse', 'local_sandbox', $shortname), SANDBOX_LEVEL_WARNING);
                             continue;
                         }
                         // Delete existing course
                         if (!delete_course($oldcourseid, false)) {
                             // Output error message for cron listing
                             echo "\n\t" . get_string('skippingdeletionfailed', 'local_sandbox', $shortname) . "\n";
                             // Inform admin
                             local_sandbox_inform_admin(get_string('skippingdeletionfailed', 'local_sandbox', $shortname), SANDBOX_LEVEL_WARNING);
                             continue;
                         }
                         // Unzip course backup file to temp directory
                         $filepacker = get_file_packer('application/vnd.moodle.backup');
                         check_dir_exists($CFG->dataroot . '/temp/backup');
                         if (!$filepacker->extract_to_pathname($local_sandbox_config->coursebackupsdirectory . '/' . $file, $CFG->dataroot . '/temp/backup/' . $shortname)) {
                             // Output error message for cron listing
                             echo "\n\t" . get_string('skippingunzipfailed', 'local_sandbox', $file) . "\n";
                             // Inform admin
                             local_sandbox_inform_admin(get_string('skippingunzipfailed', 'local_sandbox', $shortname), SANDBOX_LEVEL_WARNING);
                             continue;
                         }
                         // Create new course
                         if (!($newcourseid = \restore_dbops::create_new_course($shortname, $shortname, $categoryid))) {
                             // Output error message for cron listing
                             echo "\n\t" . get_string('skippingcreatefailed', 'local_sandbox', $shortname) . "\n";
                             // Inform admin
                             local_sandbox_inform_admin(get_string('skippingcreatefailed', 'local_sandbox', $shortname), SANDBOX_LEVEL_WARNING);
                             continue;
                         }
                         // Get admin user for restore
                         $admin = get_admin();
                         $restoreuser = $admin->id;
                         // Restore course backup file into new course
                         if ($controller = new \restore_controller($shortname, $newcourseid, \backup::INTERACTIVE_NO, \backup::MODE_SAMESITE, $restoreuser, \backup::TARGET_NEW_COURSE)) {
                             $controller->get_logger()->set_next(new \output_indented_logger(\backup::LOG_INFO, false, true));
                             $controller->execute_precheck();
                             $controller->execute_plan();
                         } else {
                             // Output error message for cron listing
                             echo "\n\t" . get_string('skippingrestorefailed', 'local_sandbox', $shortname) . "\n";
                             // Inform admin
                             local_sandbox_inform_admin(get_string('skippingrestorefailed', 'local_sandbox', $shortname), SANDBOX_LEVEL_WARNING);
                             continue;
                         }
                         // Adjust course start date
                         if ($local_sandbox_config->adjustcoursestartdate == true) {
                             if (!$DB->update_record('course', (object) array('id' => $newcourseid, 'startdate' => time()))) {
                                 // Output error message for cron listing
                                 echo "\n\t" . get_string('skippingadjuststartdatefailed', 'local_sandbox', $shortname) . "\n";
                                 // Inform admin
                                 local_sandbox_inform_admin(get_string('skippingadjuststartdatefailed', 'local_sandbox', $shortname), SANDBOX_LEVEL_WARNING);
                                 continue;
                             }
                         }
                         // Set shortname and fullname back
                         if ($DB->update_record('course', (object) array('id' => $newcourseid, 'shortname' => $shortname, 'fullname' => $fullname))) {
                             // Output info message for cron listing
                             echo "\n\t" . get_string('successrestored', 'local_sandbox', $shortname) . "\n";
                             // Inform admin
                             local_sandbox_inform_admin(get_string('successrestored', 'local_sandbox', $shortname), SANDBOX_LEVEL_NOTICE);
                             // Log the event
                             $logevent = \local_sandbox\event\course_restored::create(array('objectid' => $newcourseid, 'context' => \context_course::instance($newcourseid)));
                             $logevent->trigger();
                             // Fire course_updated event
                             $course = $DB->get_record('course', array('id' => $newcourseid));
                             $ccevent = \core\event\course_created::create(array('objectid' => $course->id, 'context' => \context_course::instance($course->id), 'other' => array('shortname' => $course->shortname, 'fullname' => $course->fullname)));
                             $ccevent->trigger();
                             // Count successfully restored course
                             $count++;
                         } else {
                             // Output error message for cron listing
                             echo "\n\t" . get_string('skippingdbupdatedfailed', 'local_sandbox', $shortname) . "\n";
                             // Inform admin
                             local_sandbox_inform_admin(get_string('skippingdbupdatefailed', 'local_sandbox', $shortname), SANDBOX_LEVEL_WARNING);
                             continue;
                         }
                     }
                 }
                 closedir($handle);
                 // Output info message for cron listing
                 echo "\n\t" . get_string('noticerestorecount', 'local_sandbox', $count) . "\n";
                 // Inform admin
                 local_sandbox_inform_admin(get_string('noticerestorecount', 'local_sandbox', $count), SANDBOX_LEVEL_NOTICE);
                 return true;
             } else {
                 // Output error message for cron listing
                 echo "\n\t" . get_string('errordirectorynotreadable', 'local_sandbox', $local_sandbox_config->coursebackupsdirectory) . "\n";
                 // Inform admin
                 local_sandbox_inform_admin(get_string('errordirectorynotreadable', 'local_sandbox', $local_sandbox_config->coursebackupsdirectory), SANDBOX_LEVEL_ERROR);
                 return false;
             }
         } else {
             // Output error message for cron listing
             echo "\n\t" . get_string('errordirectorynotexist', 'local_sandbox', $local_sandbox_config->coursebackupsdirectory) . "\n";
             // Inform admin
             local_sandbox_inform_admin(get_string('errordirectorynotexist', 'local_sandbox', $local_sandbox_config->coursebackupsdirectory), SANDBOX_LEVEL_ERROR);
             return false;
         }
     } else {
         // Output info message for cron listing
         echo "\n\t" . get_string('noticedirectorynotconfigured', 'local_sandbox') . "\n";
         // Inform admin
         local_sandbox_inform_admin(get_string('noticedirectorynotconfigured', 'local_sandbox'), SANDBOX_LEVEL_NOTICE);
         return true;
     }
 }
Ejemplo n.º 4
0
/**
 * Attempts to insert a course into the moodle database
 *
 * @param type $course
 * @return string
 */
function up_insert_course($course)
{
    global $DB;
    $courserequestnumber = $course->idnumber;
    $category = $course->category;
    $dbinfo = get_db_info();
    $oc = new stdClass();
    try {
        $oc = oci_connect($dbinfo->username, $dbinfo->password, $dbinfo->db);
    } catch (exception $e) {
        print $e->getMessage();
    }
    // Check if the course exists
    if (up_course_exists($courserequestnumber, $category)) {
        add_to_log('1', 'course', 'error', '', "Course already exists: BannerID: {$courserequestnumber}", '', '4');
        // Mark course as added in banner
        $totalrows = 0;
        $query = "INSERT INTO UP_MOODLE.TBL_UPM_COURSE_SYNC VALUES ('{$courserequestnumber}','{$course->termcode}','Y')";
        $sql = oci_parse($oc, $query);
        oci_execute($sql);
        $results = array();
        $rows = oci_fetch_all($sql, $results, 0, $totalrows, OCI_FETCHSTATEMENT_BY_ROW);
        if ($rows = false) {
            print 'Insert to TBL_UPM_COURSE_SYNC failed. Sync may be broken.';
            exit;
        }
        return COURSE_ALREADY_EXISTS;
    } else {
        if ($newcourseid = $DB->insert_record('course', $course)) {
            if (UP_DEBUG) {
                print "New Course ID is: " . $newcourseid . "<br />\n";
            }
            //get the newly inserted course so we can add the default blocks
            $newMoodleCourse = $DB->get_record('course', array('id' => $newcourseid));
            $data = $course;
            $course = course_get_format($newcourseid)->get_course();
            // Setup the blocks
            blocks_add_default_course_blocks($newMoodleCourse);
            //set section information and insert into the section database
            $section = new stdClass();
            $section->course = $newcourseid;
            // Create a default section.
            $section->section = 0;
            $section->summaryformat = FORMAT_HTML;
            try {
                $DB->insert_record('course_sections', $section);
            } catch (coding_exception $e) {
                print $e->getMessage();
            }
            fix_course_sortorder();
            up_add_enrol_plugin('manual', true, $course, $data);
            // Mark course as added in banner
            $totalrows = 0;
            $query = "INSERT INTO UP_MOODLE.TBL_UPM_COURSE_SYNC VALUES ('{$courserequestnumber}','{$data->termcode}','Y')";
            $sql = oci_parse($oc, $query);
            oci_execute($sql);
            $results = array();
            $rows = oci_fetch_all($sql, $results, 0, $totalrows, OCI_FETCHSTATEMENT_BY_ROW);
            if ($rows = false) {
                print 'Insert to TBL_UPM_COURSE_SYNC failed. Sync may be broken.';
                exit;
            }
            $event = \core\event\course_created::create(array('objectid' => $course->id, 'context' => context_course::instance($course->id), 'other' => array('shortname' => $course->shortname, 'fullname' => $course->fullname)));
            $event->trigger();
            return COURSE_CREATION_SUCCEEDED;
        } else {
            add_to_log('1', 'course', 'error', '', "Creating: {$shortname} (BANID {$courserequestnumber})", '', '4');
            return COURSE_CREATION_FAILED;
        }
    }
    // end of else on if ( $newcourseid = $DB->insert_record('course', $form) )
}
Ejemplo n.º 5
0
 public static function course_created(\core\event\course_created $event)
 {
     global $CFG, $DB;
     $course = $event->get_record_snapshot('course', $event->objectid);
     $activities = get_config('auth/joomdle', 'jomsocial_activities');
     $groups = get_config('auth/joomdle', 'jomsocial_groups');
     $autosell = get_config('auth/joomdle', 'auto_sell');
     $joomla_user_groups = get_config('auth/joomdle', 'joomla_user_groups');
     $use_kunena_forums = get_config('auth/joomdle', 'use_kunena_forums');
     $auth_joomdle = new auth_plugin_joomdle();
     /* kludge for the call_method fn to work */
     if (!$course->summary) {
         $course->summary = ' ';
     }
     $conditions = array('id' => $course->category);
     $cat = $DB->get_record('course_categories', $conditions);
     $context = context_course::instance($course->id);
     $course->summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', NULL);
     $course->summary = str_replace('pluginfile.php', '/auth/joomdle/pluginfile_joomdle.php', $course->summary);
     if ($activities) {
         $auth_joomdle->call_method('addActivityCourse', (int) $course->id, $course->fullname, $course->summary, (int) $course->category, $cat->name);
     }
     if ($groups) {
         $auth_joomdle->call_method('addSocialGroup', $course->fullname, get_string('auth_joomla_group_for_course', 'auth_joomdle') . ' ' . $course->fullname, (int) $course->id);
     }
     if ($autosell) {
         $cid = array($course->id);
         $auth_joomdle->call_method("sellCourse", array((int) $course->id));
     }
     if ($joomla_user_groups) {
         $auth_joomdle->call_method('addUserGroups', (int) $course->id, $course->fullname);
     }
     if ($use_kunena_forums) {
         // Create section
         $auth_joomdle->call_method('addForum', (int) $course->id, (int) -2, $course->fullname);
         // Create news forum
     }
     return true;
 }
 /**
  * Called when course_created event happens.
  *
  * @param \core\event\course_created $event Event data object
  */
 public static function course_created(\core\event\course_created $event)
 {
     year_tables::handle_new_course($event->get_data()['objectid']);
 }