/**
  * 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($options = array())
 {
     global $CURMAN;
     $objs = array('errors' => array());
     if (isset($options['targetcluster'])) {
         $cluster = $options['targetcluster'];
         if (!is_object($cluster) || !is_a($cluster, 'cluster')) {
             $options['targetcluster'] = $cluster = new cluster($cluster);
         }
     }
     // clone main class object
     $clone = new cmclass($this);
     unset($clone->id);
     if (isset($options['targetcourse'])) {
         $clone->courseid = $options['targetcourse'];
     }
     if (isset($cluster)) {
         // if cluster specified, append cluster's name to class
         $clone->idnumber = $clone->idnumber . ' - ' . $cluster->name;
     }
     $clone = new cmclass(addslashes_recursive($clone));
     $clone->autocreate = false;
     // avoid warnings
     if (!$clone->add()) {
         $objs['errors'][] = get_string('failclustcpycls', 'block_curr_admin', $this);
         return $objs;
     }
     $objs['classes'] = array($this->id => $clone->id);
     $cmc = $CURMAN->db->get_record(CLSMDLTABLE, 'classid', $this->id);
     if ($cmc) {
         if ($cmc->autocreated == -1) {
             $cmc->autocreated = $CURMAN->config->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 = content_rollover($cmc->moodlecourseid, $clone->startdate);
             // Rename the fullname, shortname and idnumber of the restored course
             $restore->id = $moodlecourseid;
             $restore->fullname = addslashes($clone->course->name . '_' . $clone->idnumber);
             $restore->shortname = addslashes($clone->idnumber);
             $CURMAN->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);
         }
     }
     // FIXME: copy tags
     return $objs;
 }
/**
 * 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;
}
Exemple #3
0
/**
 * Fetch a record from the rollover queue and process it.
 *
 * @param int $id The ID of the request to process.
 * @return bool True on success, False otherwise.
 */
function process_queue($id = FALSE)
{
    global $CFG, $USER;
    require_once $CFG->dirroot . '/message/lib.php';
    begin_sql();
    if ($id) {
        $task = get_record('block_admin_rollover_queue', 'id', $id);
        if (!$task) {
            commit_sql();
            notify(get_string('queue_notfound', 'rollover'), 'notifyproblem errorbox');
            return FALSE;
        }
    } else {
        $task = get_record('block_admin_rollover_queue', '', '');
        if (!$task) {
            commit_sql();
            return FALSE;
        }
    }
    print_heading(get_string('run_queue_record', 'rollover', $task));
    flush();
    // make sure we don't have multiple entries for the course
    $status = delete_records('block_admin_course_template', 'courseid', $task->courseid);
    $status = $status && delete_records('block_admin_rollover_queue', 'id', $task->id);
    $status = $status && content_rollover($task->templateid, $task->courseid);
    $trec = new stdClass();
    $trec->courseid = $task->courseid;
    $trec->templateid = $task->templateid;
    $trec->timemodified = time();
    $status = $status && ($trec->id = insert_record('block_admin_course_template', $trec));
    if ($status) {
        commit_sql();
        $touser = get_record('user', 'id', $task->userid);
        $coursename = get_field('course', 'fullname', 'id', $task->courseid);
        $a = new stdClass();
        $a->coursename = get_field('course', 'fullname', 'id', $task->courseid);
        $a->link = '<a href="' . $CFG->wwwroot . '/course/view.php?id=' . $task->courseid . '">' . $coursename . '</a>';
        print_heading(get_string('rollover_message', 'rollover', $a));
        message_post_message($USER, $touser, get_string('rollover_message', 'rollover', $a), FORMAT_HTML, 'direct');
        notify(get_string('success'), 'notifysuccess');
        return TRUE;
    } else {
        rollback_sql();
        notify(get_string('rollover_failure', 'rollover'), 'notifyproblem errorbox');
        return FALSE;
    }
}