Example #1
0
 /**
  * Clone a class
  * @param array $options options for cloning.  Valid options are:
  * - 'moodlecourses': whether or not to clone Moodle courses (if they were
  *   autocreated).  Values can be (default: "copyalways"):
  *   - "copyalways": always copy course
  *   - "copyautocreated": only copy autocreated courses
  *   - "autocreatenew": autocreate new courses from course template
  *   - "link": link to existing course
  * - 'targetcourse': the course id to associate the clones with (default:
  *   same as original class)
  * @return array array of array of object IDs created.  Key in outer array
  * is type of object (plural).  Key in inner array is original object ID,
  * value is new object ID.  Outer array also has an entry called 'errors',
  * which is an array of any errors encountered when duplicating the
  * object.
  */
 function duplicate(array $options = array())
 {
     //needed by the rollover lib
     global $CFG;
     require_once elis::lib('rollover/lib.php');
     $objs = array('errors' => array());
     if (isset($options['targetcluster'])) {
         $userset = $options['targetcluster'];
         if (!is_object($userset) || !is_a($userset, 'userset')) {
             $options['targetcluster'] = $userset = new userset($userset);
         }
     }
     // Due to lazy loading, we need to pre-load this object
     $this->load();
     // clone main class object
     $clone = new pmclass($this);
     unset($clone->id);
     if (isset($options['targetcourse'])) {
         $clone->courseid = $options['targetcourse'];
     }
     $idnumber = $clone->idnumber;
     if (isset($userset)) {
         $idnumber = append_once($idnumber, ' - ' . $userset->name, array('maxlength' => 95));
     }
     //get a unique idnumber
     $clone->idnumber = generate_unique_identifier(pmclass::TABLE, 'idnumber', $idnumber, array('idnumber' => $idnumber));
     $clone->autocreate = false;
     // avoid warnings
     $clone->save();
     $objs['classes'] = array($this->id => $clone->id);
     $cmc = $this->_db->get_record(classmoodlecourse::TABLE, array('classid' => $this->id));
     if ($cmc) {
         if ($cmc->autocreated == -1) {
             $cmc->autocreated = elis::$config->local_elisprogram->autocreated_unknown_is_yes;
         }
         if (empty($options['moodlecourses']) || $options['moodlecourses'] == 'copyalways' || $options['moodlecourses'] == 'copyautocreated' && $cmc->autocreated) {
             // create a new Moodle course based on the current class's Moodle course
             $moodlecourseid = course_rollover($cmc->moodlecourseid);
             //check that the course has rolled over successfully
             if (!$moodlecourseid) {
                 return false;
             }
             // Rename the fullname, shortname and idnumber of the restored course
             $restore = new stdClass();
             $restore->id = $moodlecourseid;
             // ELIS-2941: Don't prepend course name if already present ...
             if (strpos($clone->idnumber, $clone->course->name) !== 0) {
                 $restore->fullname = $clone->course->name . '_' . $clone->idnumber;
             } else {
                 $restore->fullname = $clone->idnumber;
             }
             $restore->shortname = $clone->idnumber;
             $this->_db->update_record('course', $restore);
             moodle_attach_class($clone->id, $moodlecourseid);
         } elseif ($options['moodlecourses'] == 'link' || $options['moodlecourses'] == 'copyautocreated' && !$cmc->autocreated) {
             // link to the current class's Moodle course
             moodle_attach_class($clone->id, $cmc->moodlecourseid);
         } else {
             // $options['moodlecourses'] == 'autocreatenew'
             // create a new course based on the course template
             moodle_attach_class($clone->id, 0, '', false, false, true);
         }
     }
     return $objs;
 }
/**
 * Attach a class record from this system to an existing Moodle course.
 *
 * @param int    $clsid           The class ID.
 * @param int    $mdlid           The Moodle course ID.
 * @param string $siteconfig      The full system path to a Moodle congif.php file (defaults to local).
 * @param bool   $enrolinstructor Flag for enroling instructors into the Moodle course (optional).
 * @param bool   $enrolstudent    Flag for enroling students into the Moodle course (optional).
 * @return bool True on success, False otherwise.
 */
function moodle_attach_class($clsid, $mdlid, $siteconfig = '', $enrolinstructor = false, $enrolstudent = false, $autocreate = false)
{
    //$CFG global is needed by the rollover lib
    global $DB, $CFG;
    $result = true;
    $moodlecourseid = $mdlid;
    /// Look for an existing link for this class.
    if (!($clsmdl = $DB->get_record(classmoodlecourse::TABLE, array('classid' => $clsid)))) {
        /// Make sure the specified Moodle site config file exists.
        if (!empty($siteconfig) && !file_exists($siteconfig)) {
            return false;
        }
        if ($autocreate) {
            // auto create is checked, create connect to moodle course
            $cls = new pmclass($clsid);
            //attempt to obtain the course template
            $template = coursetemplate::find(new field_filter('courseid', $cls->courseid));
            if ($template->valid()) {
                $template = $template->current();
            }
            // no template defined, so do nothing
            if (empty($template->id) || empty($template->location)) {
                print_error('notemplate', 'local_elisprogram');
            }
            $classname = $template->templateclass;
            $obj = new $classname();
            $courseid = $template->location;
            //perform the rollover
            require_once elis::lib('rollover/lib.php');
            $moodlecourseid = course_rollover($courseid);
            //check that the course has rolled over successfully
            if (!$moodlecourseid) {
                return false;
            }
            //set the Moodle course name as expected
            $restoredcourse = new stdClass();
            $restoredcourse->id = $moodlecourseid;
            // ELIS-2941: Don't prepend course name if already present @ start
            if (strpos($cls->idnumber, $cls->course->name) !== 0) {
                $restoredcourse->fullname = $cls->course->name . '_' . $cls->idnumber;
            } else {
                $restoredcourse->fullname = $cls->idnumber;
            }
            $restoredcourse->shortname = $cls->idnumber;
            $DB->update_record('course', $restoredcourse);
        }
        $newrec = array('classid' => $clsid, 'moodlecourseid' => $moodlecourseid, 'siteconfig' => $siteconfig, 'autocreated' => $autocreate ? 1 : 0);
        $clsmdl = new classmoodlecourse($newrec);
        $clsmdl->save();
    } else {
        $clsmdl = new classmoodlecourse($clsmdl->id);
    }
    if ($enrolinstructor) {
        $clsmdl->data_enrol_instructors();
    }
    if ($enrolstudent) {
        $clsmdl->data_enrol_students();
    }
    events_trigger('pm_classinstance_associated', $clsmdl);
    return true;
}
Example #3
0
 /**
  * Create a course
  * @todo: consider factoring this some more once other actions exist
  *
  * @param object $record One record of import data
  * @param string $filename The import file name, used for logging
  * @return boolean true on success, otherwise false
  */
 function course_create($record, $filename)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/course/lib.php';
     //remove invalid fields
     $record = $this->remove_invalid_course_fields($record);
     //field length checking
     $lengthcheck = $this->check_course_field_lengths($record, $filename);
     if (!$lengthcheck) {
         return false;
     }
     //data checking
     if (!$this->validate_core_course_data('create', $record, $filename)) {
         return false;
     }
     //validate and set up the category
     $categoryid = $this->get_category_id($record, $filename);
     if ($categoryid === false) {
         return false;
     }
     $record->category = $categoryid;
     //uniqueness check
     if ($DB->record_exists('course', array('shortname' => $record->shortname))) {
         $identifier = $this->mappings['shortname'];
         $this->fslogger->log_failure("{$identifier} value of \"{$record->shortname}\" refers to a " . "course that already exists.", 0, $filename, $this->linenumber, $record, 'course');
         return false;
     }
     // ID number uniqueness check
     if (isset($record->idnumber) && $record->idnumber !== '' && $DB->record_exists('course', array('idnumber' => $record->idnumber))) {
         $identifier = $this->mappings['idnumber'];
         $this->fslogger->log_failure("{$identifier} value of \"{$record->idnumber}\" already exists " . "in an existing course.", 0, $filename, $this->linenumber, $record, 'course');
         return false;
     }
     //final data sanitization
     if (isset($record->guest)) {
         if ($record->guest == 0) {
             $record->enrol_guest_status_0 = ENROL_INSTANCE_DISABLED;
         } else {
             $record->enrol_guest_status_0 = ENROL_INSTANCE_ENABLED;
             if (isset($record->password)) {
                 $record->enrol_guest_password_0 = $record->password;
             } else {
                 $record->enrol_guest_password_0 = NULL;
             }
         }
     }
     //check that any unset fields are set to course default
     $courseconfig = get_config('moodlecourse');
     //set up an array with all the course fields that have defaults
     $course_defaults = array('format', 'numsections', 'hiddensections', 'newsitems', 'showgrades', 'showreports', 'maxbytes', 'groupmode', 'groupmodeforce', 'visible', 'lang');
     foreach ($course_defaults as $course_default) {
         if (!isset($record->{$course_default}) && isset($courseconfig->{$course_default})) {
             $record->{$course_default} = $courseconfig->{$course_default};
         }
     }
     //write to the database
     if (isset($record->link)) {
         //creating from template
         require_once $CFG->dirroot . '/local/eliscore/lib/setup.php';
         require_once elis::lib('rollover/lib.php');
         $courseid = $DB->get_field('course', 'id', array('shortname' => $record->link));
         //perform the content rollover
         $record->id = course_rollover($courseid);
         //update appropriate fields, such as shortname
         //todo: validate if this fully works with guest enrolments?
         update_course($record);
         //log success
         $this->fslogger->log_success("Course with shortname \"{$record->shortname}\" successfully created from template course with shortname \"{$record->link}\".", 0, $filename, $this->linenumber);
     } else {
         //creating directly (not from template)
         create_course($record);
         //log success
         $this->fslogger->log_success("Course with shortname \"{$record->shortname}\" successfully created.", 0, $filename, $this->linenumber);
     }
     if (!$this->fslogger->get_logfile_status()) {
         return false;
     }
     return true;
 }