Beispiel #1
0
/**
 * Purpose: add a Moodle page resource to a course
 *
 * @author Andrew Zoltay
 * date    2011-04-27
 * @global $CFG - configuration settings for CACE
 * @global object $DB Moodle database object
 * @global $CACE_CFG - configuration settings for CACE
 * @param int $courseid
 * @param object $pagedata
 */
function cace_add_page_resource($course, $pagedata)
{
    global $CFG, $DB, $CACE_CFG, $USER;
    require_once "{$CFG->dirroot}/mod/page/lib.php";
    try {
        // Create a new course module for the page.
        $newcm = new stdClass();
        $newcm->course = $course->id;
        $newcm->module = $pagedata->module;
        $newcm->instance = 0;
        // Don't have this value yet, but will update later.
        $newcm->visible = $pagedata->visible;
        $newcm->groupmode = $pagedata->groupmode;
        $newcm->groupingid = $pagedata->groupingid;
        $newcm->groupmembersonly = $pagedata->groupmembersonly;
        $pagedata->coursemodule = add_course_module($newcm);
        // This is lame, but need to create a dummy object for page_add_instance()
        // useless $mform parameter that isn't used in the function.
        $dummyform = new stdClass();
        // Create the page instance.
        $pageid = page_add_instance($pagedata, $dummyform);
        $pagedata->instance = $pageid;
        // Update course_modules table with new instance.
        $DB->set_field('course_modules', 'instance', $pageid, array('id' => $pagedata->coursemodule));
        // Add the module to the correct section of the course
        // course_modules and course_sections each contain a reference
        // to each other, so we have to update one of them twice.
        $sectionid = course_add_cm_to_section($pagedata->course, $pagedata->coursemodule, $pagedata->section);
        $DB->set_field('course_modules', 'section', $sectionid, array('id' => $pagedata->coursemodule));
        set_coursemodule_visible($pagedata->coursemodule, $pagedata->visible);
        // Trigger mod_created event with information about the new module.
        // Use the new events system to log the creation of a resource.
        $eventdata = clone $pagedata;
        $eventdata->modname = $pagedata->modulename;
        $eventdata->name = $pagedata->name;
        $eventdata->id = $pagedata->coursemodule;
        $eventdata->courseid = $course->id;
        $eventdata->userid = $CACE_CFG->admin_user;
        // Use Admin account for event data.
        $event = \core\event\course_module_created::create_from_cm($eventdata);
        $event->trigger();
        return true;
    } catch (Exception $e) {
        return false;
    }
}
        // Create the url database entry
        unset($data->id);
        $data->externalurl = $contents;
        $data->instance = url_add_instance($data, $data);
    } else {
        // Create the page database entry
        unset($data->id);
        $data->page = array('text' => $contents, 'itemid' => false);
        if ($type == 'text') {
            $data->page['format'] = FORMAT_PLAIN;
        } else {
            $data->page['format'] = FORMAT_HTML;
        }
        $data->printheading = true;
        $data->printintro = false;
        $data->instance = page_add_instance($data, $data);
    }
}
// Update the 'instance' field for the course module
$DB->set_field('course_modules', 'instance', $data->instance, array('id' => $data->coursemodule));
// Add the resource to the correct section
$sectionid = add_mod_to_section($data);
$DB->set_field('course_modules', 'section', $sectionid, array('id' => $data->coursemodule));
set_coursemodule_visible($data->coursemodule, $data->visible);
// START UCLA MOD - CCLE-2769 - Drag and drop file upload block on M2
// default drag/drop uploads to be private
$groupingid = false;
if (class_exists('PublicPrivate_Course') && PublicPrivate_Site::is_enabled()) {
    $publicprivate_course = new PublicPrivate_Course($course->id);
    if ($publicprivate_course->is_activated()) {
        $pp = new PublicPrivate_Module($data->coursemodule);
Beispiel #3
0
/**
 * Handle a file that has been uploaded
 * @param object $uploadinfo details of the file / content that has been uploaded
 * @return int instance id of the newly created mod
 */
function page_dndupload_handle($uploadinfo) {
    // Gather the required info.
    $data = new stdClass();
    $data->course = $uploadinfo->course->id;
    $data->name = $uploadinfo->displayname;
    $data->intro = '<p>'.$uploadinfo->displayname.'</p>';
    $data->introformat = FORMAT_HTML;
    if ($uploadinfo->type == 'text/html') {
        $data->contentformat = FORMAT_HTML;
        $data->content = clean_param($uploadinfo->content, PARAM_CLEANHTML);
    } else {
        $data->contentformat = FORMAT_PLAIN;
        $data->content = clean_param($uploadinfo->content, PARAM_TEXT);
    }
    $data->coursemodule = $uploadinfo->coursemodule;

    // Set the display options to the site defaults.
    $config = get_config('page');
    $data->display = $config->display;
    $data->popupheight = $config->popupheight;
    $data->popupwidth = $config->popupwidth;
    $data->printheading = $config->printheading;
    $data->printintro = $config->printintro;

    return page_add_instance($data, null);
}
Beispiel #4
0
    /**
     * Create new page module instance
     * @param array|stdClass $record
     * @param array $options
     * @return stdClass activity record with extra cmid field
     */
    public function create_instance($record = null, array $options = null) {
        global $CFG;
        require_once("$CFG->dirroot/mod/page/locallib.php");

        $this->instancecount++;
        $i = $this->instancecount;

        $record = (object)(array)$record;
        $options = (array)$options;

        if (empty($record->course)) {
            throw new coding_exception('module generator requires $record->course');
        }
        if (!isset($record->name)) {
            $record->name = get_string('pluginname', 'page').' '.$i;
        }
        if (!isset($record->intro)) {
            $record->intro = 'Test page '.$i;
        }
        if (!isset($record->introformat)) {
            $record->introformat = FORMAT_MOODLE;
        }
        if (!isset($record->content)) {
            $record->content = 'Test page content';
        }
        if (!isset($record->contentformat)) {
            $record->contentformat = FORMAT_MOODLE;
        }
        if (!isset($record->display)) {
            $record->display = RESOURCELIB_DISPLAY_AUTO;
        }
        if (isset($options['idnumber'])) {
            $record->cmidnumber = $options['idnumber'];
        } else {
            $record->cmidnumber = '';
        }
        if (!isset($record->printheading)) {
            $record->printheading = 1;
        }
        if (!isset($record->printintro)) {
            $record->printintro = 0;
        }

        $record->coursemodule = $this->precreate_course_module($record->course, $options);
        $id = page_add_instance($record, null);
        return $this->post_add_instance($id, $record->coursemodule);
    }