/**
  *
  * @param unknown $user
  * @param unknown $courseid
  * @param unknown $sectionid
  * @param unknown $itemUuid
  * @param unknown $itemVersion
  * @param unknown $url
  * @param unknown $title
  * @param unknown $description
  * @param unknown $attachmentUuid
  * @return array
  */
 public static function add_item_to_course($username, $courseid, $sectionnum, $itemUuid, $itemVersion, $url, $title, $description, $attachmentUuid)
 {
     global $DB, $USER;
     $params = self::validate_parameters(self::add_item_to_course_parameters(), array('user' => $username, 'courseid' => $courseid, 'sectionid' => $sectionnum, 'itemUuid' => $itemUuid, 'itemVersion' => $itemVersion, 'url' => $url, 'title' => $title, 'description' => $description, 'attachmentUuid' => $attachmentUuid));
     self::check_modify_permissions($username, $courseid);
     $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
     $modname = 'equella';
     $module = $DB->get_record('modules', array('name' => $modname));
     $eq = new stdClass();
     $eq->course = $courseid;
     $eq->module = $module->id;
     $eq->name = $title;
     $eq->intro = $description;
     $eq->introformat = FORMAT_HTML;
     $eq->url = $url;
     $eq->uuid = $itemUuid;
     $eq->version = $itemVersion;
     $eq->mimetype = mimeinfo('type', $title);
     if (!empty($attachmentUuid)) {
         $eq->filename = $title;
         $eq->attachmentuuid = $attachmentUuid;
     }
     $eqid = equella_add_instance($eq);
     $cmid = null;
     $mod = new stdClass();
     $mod->course = $courseid;
     $mod->module = $module->id;
     $mod->instance = $eqid;
     $mod->modulename = $modname;
     $mod->section = 0;
     if (!($cmid = add_course_module($mod))) {
         throw new moodle_exception('cannotaddcoursemodule');
     }
     if (!($addedsectionid = course_add_cm_to_section($courseid, $cmid, $sectionnum))) {
         throw new moodle_exception('cannotaddcoursemoduletosection');
     }
     set_coursemodule_visible($cmid, true);
     if (class_exists('core\\event\\course_module_created')) {
         $cm = get_coursemodule_from_id('equella', $cmid, 0, false, MUST_EXIST);
         $event = \core\event\course_module_created::create_from_cm($cm);
         $event->trigger();
     } else {
         $eventdata = new stdClass();
         $eventdata->modulename = $modname;
         $eventdata->name = $eq->name;
         $eventdata->cmid = $cmid;
         $eventdata->courseid = $eq->course;
         $eventdata->userid = $USER->id;
         events_trigger('mod_created', $eventdata);
         add_to_log($eq->course, "course", "add mod", "../mod/{$modname}/view.php?id={$cmid}", "{$modname} {$eqid}");
         add_to_log($eq->course, $modname, "add equella resource", "view.php?id={$cmid}", "{$eqid}", $cmid);
     }
     $result = array('courseid' => $courseid, 'coursename' => $course->fullname, 'sectionid' => $params['sectionid'], 'sectionname' => get_section_name($courseid, $sectionnum));
     //flog("DB queries: " . $DB->perf_get_queries());
     return $result;
 }
Exemple #2
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 equella_dndupload_handle($uploadinfo)
{
    global $USER;
    $fs = get_file_storage();
    // Gather the required info.
    $courseid = $uploadinfo->course->id;
    $coursemodule = $uploadinfo->coursemodule;
    $usercontext = context_user::instance($USER->id);
    $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $uploadinfo->draftitemid, 'id', false);
    $moduleid = null;
    foreach ($draftfiles as $file) {
        $handle = $file->get_content_file_handle();
        // pushing files to equella
        $params = array();
        $params['moodlecoursefullname'] = $uploadinfo->course->fullname;
        $params['moodlecourseshortname'] = $uploadinfo->course->shortname;
        $params['moodlecourseid'] = $uploadinfo->course->id;
        $params['moodlecourseidnumber'] = $uploadinfo->course->idnumber;
        $params['filesize'] = $file->get_filesize();
        $mimetype = $file->get_mimetype();
        $info = equella_rest_api::contribute_file_with_shared_secret($file->get_filename(), $handle, $params);
        if (isset($info->error)) {
            throw new equella_exception($info->error_description);
        }
        $modulename = '';
        if (!empty($info->name)) {
            $modulename = $info->name;
        } else {
            if (!empty($info->description)) {
                $modulename = $info->description;
            } else {
                $modulename = $info->uuid;
            }
        }
        $eqresource = new stdClass();
        $eqresource->course = $courseid;
        $eqresource->name = $modulename;
        $eqresource->intro = $info->description;
        $eqresource->introformat = FORMAT_HTML;
        $eqresource->mimetype = $mimetype;
        $eqresource->filename = $file->get_filename();
        $item = array_pop($info->attachments);
        $eqresource->attachmentuuid = $item->uuid;
        $eqresource->url = $item->links->view;
        try {
            $eqresourceid = equella_add_instance($eqresource, null);
        } catch (Exception $ex) {
            throw new equella_exception('Failed to create EQUELLA resource.');
        }
    }
    return $eqresourceid;
}
 $count = $data->count;
 $attachmentuuid = $data->uuid;
 $module = $DB->get_record('modules', array('name' => 'equella'));
 for ($i = 1; $i <= $count; $i++) {
     $eq = new stdclass();
     $eq->name = uniqid() . '. ' . $equrl;
     $eq->intro = $equrl;
     $eq->introformat = FORMAT_HTML;
     $eq->url = $equrl;
     $eq->course = $courseid;
     $eq->modulename = 'equella';
     $eq->module = $module->id;
     $eq->attachmentuuid = $attachmentuuid;
     $eq->mimetype = mimeinfo('type', $equrl);
     try {
         $moduleid = equella_add_instance($eq);
     } catch (Exception $ex) {
         throw new equella_exception('Failed to create EQUELLA resource.');
     }
     $eq->instance = $moduleid;
     if (!($eq->coursemodule = add_course_module($eq))) {
         print_error('cannotaddcoursemodule');
     }
     if (!($addedsectionid = course_add_cm_to_section($eq->course, $eq->coursemodule, 0))) {
         print_error('cannotaddcoursemoduletosection');
     }
     if (!$DB->set_field('course_modules', 'section', $addedsectionid, array('id' => $eq->coursemodule))) {
         print_error('Could not update the course module with the correct section');
     }
     set_coursemodule_visible($eq->coursemodule, true);
 }
 // if equella returns section id, overwrite moodle section parameter
 if (isset($link['folder']) && $link['folder'] != null) {
     $targetsection = clean_param($link['folder'], PARAM_INT);
 }
 if (isset($link['filename'])) {
     $mod->filename = clean_param($link['filename'], PARAM_FILE);
 }
 if (isset($link['mimetype'])) {
     $mod->mimetype = $link['mimetype'];
 } else {
     $mod->mimetype = mimeinfo('type', $mod->url);
 }
 if (isset($link['activationUuid'])) {
     $mod->activation = $link['activationUuid'];
 }
 $equellaid = equella_add_instance($mod);
 $mod->instance = $equellaid;
 // course_modules and course_sections each contain a reference
 // to each other, so we have to update one of them twice.
 if (!($mod->coursemodule = add_course_module($mod))) {
     print_error('cannotaddcoursemodule');
 }
 if (!($addedsectionid = course_add_cm_to_section($mod->course, $mod->coursemodule, $targetsection))) {
     print_error('cannotaddcoursemoduletosection');
 }
 if (!$DB->set_field('course_modules', 'section', $addedsectionid, array('id' => $mod->coursemodule))) {
     print_error('Could not update the course module with the correct section');
 }
 set_coursemodule_visible($mod->coursemodule, true);
 if (class_exists('core\\event\\course_module_created')) {
     $cm = get_coursemodule_from_id('equella', $mod->coursemodule, 0, false, MUST_EXIST);