/**
  * adds a moodle course selection box to the form
  *
  * @uses $CFG
  * @uses $CURMAN
  * @param $formid string A suffix to put on all 'id' and index for all 'name' attributes.
  *                       This should be unique if being used more than once in a form.
  * @param $extraclass string Any extra class information to add to the output.
  *
  * @return string The form HTML, without the form.
  */
 function add_moodle_course_select()
 {
     global $CFG, $CURMAN;
     $mform =& $this->_form;
     $categoryid = $CURMAN->db->get_field('course_categories', 'id', 'parent', '0');
     $sitename = $CURMAN->db->get_field('course', 'shortname', 'id', SITEID);
     $select = 'id != \'' . SITEID . '\' AND fullname NOT LIKE \'.%\'';
     $cselect = array(get_string('none', 'block_curr_admin'));
     $crss = $CURMAN->db->get_records_select('course', $select, 'fullname', 'id,fullname');
     if (!empty($crss)) {
         foreach ($crss as $crs) {
             $cselect[$crs->id] = $crs->fullname;
         }
     }
     $moodleCourses = array();
     if (count($cselect) != 1) {
         $moodleCourses[] = $mform->createElement('select', 'moodlecourseid', get_string('moodlecourse', 'block_curr_admin'), $cselect);
     } else {
         $mform->addElement('static', 'no_moodle_courses', get_string('moodlecourse', 'block_curr_admin') . ':', get_string('no_moodlecourse', 'block_curr_admin'));
         $mform->setHelpButton('no_moodle_courses', array('cmclassform/moodlecourseid', get_string('moodlecourse', 'block_curr_admin'), 'block_curr_admin'));
     }
     // Add auto create checkbox if CM course uses a template
     if (empty($this->_customdata['obj']->courseid)) {
         $courseid = 0;
     } else {
         $courseid = $this->_customdata['obj']->courseid;
     }
     $template = new coursetemplate();
     if (empty($courseid) || false !== $template->data_load_record($courseid) && !empty($template->location)) {
         $moodleCourses[] = $mform->createElement('checkbox', 'autocreate', '', get_string('autocreate', 'block_curr_admin'));
     }
     if (count($cselect) != 1) {
         $mform->addGroup($moodleCourses, 'moodleCourses', get_string('moodlecourse', 'block_curr_admin') . ':');
         $mform->disabledIf('moodleCourses', 'moodleCourses[autocreate]', 'checked');
         $mform->setHelpButton('moodleCourses', array('cmclassform/moodlecourseid', get_string('moodlecourse', 'block_curr_admin'), 'block_curr_admin'));
     }
 }
/**
 * 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;
}