/**
 * Attach a class record from this system to an existing Moodle course.
 *
 * @uses $CURMAN
 * @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)
{
    global $CURMAN;
    $result = true;
    $moodlecourseid = $mdlid;
    /// Look for an existing link for this class.
    if (!($clsmdl = $CURMAN->db->get_record(CLSMDLTABLE, '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 cmclass($clsid);
            $temp = new coursetemplate();
            $temp->data_load_record($cls->courseid);
            // no template defined, so do nothing
            if (empty($temp->id) || empty($temp->location)) {
                print_error('notemplate', 'block_curr_admin');
            }
            $classname = $temp->templateclass;
            $obj = new $classname();
            $courseId = $temp->location;
            //$obj->parseCourseId($temp->location);//print_object($courseid);die();
            $moodlecourseid = content_rollover($courseId, $cls->startdate);
            // Rename the fullname, shortname and idnumber of the restored course
            $restore->id = $moodlecourseid;
            $restore->fullname = addslashes($cls->course->name . '_' . $cls->idnumber);
            $restore->shortname = addslashes($cls->idnumber);
            $CURMAN->db->update_record('course', $restore);
        }
        $newrec = array('classid' => $clsid, 'moodlecourseid' => $moodlecourseid, 'siteconfig' => $siteconfig, 'autocreated' => $autocreate ? 1 : 0);
        $clsmdl = new classmoodlecourse($newrec);
        $result = $clsmdl->data_insert_record() === true;
    } else {
        $clsmdl = new classmoodlecourse($clsmdl->id);
    }
    if ($enrolinstructor) {
        $result = $result && $clsmdl->data_enrol_instructors();
    }
    if ($enrolstudent) {
        $result = $result && $clsmdl->data_enrol_students();
    }
    events_trigger('crlm_class_associated', $clsmdl);
    return $result;
}
/**
 * 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;
}