From 785e09a7e326157f1d6aa95bb9e8e25911986167 Mon Sep 17 00:00:00 2001 From: Davo Smith Date: Fri, 4 Jan 2013 15:39:14 +0000 Subject: [PATCH] MDL-32456 mod_label - support course drag and drop upload images to create labels --- course/dndupload.js | 106 ++++++++++++++++++++---------------- course/dnduploadlib.php | 7 ++- mod/label/lang/en/label.php | 3 + mod/label/lib.php | 52 ++++++++++++++++++ mod/label/settings.php | 31 +++++++++++ 5 files changed, 152 insertions(+), 47 deletions(-) create mode 100644 mod/label/settings.php diff --git a/course/dndupload.js b/course/dndupload.js index 1ee839546f428..ee8eb7097c10f 100644 --- a/course/dndupload.js +++ b/course/dndupload.js @@ -435,7 +435,7 @@ M.course_dndupload = { * @param section the DOM element reperesenting the course section * @return DOM element containing the new item */ - add_resource_element: function(name, section) { + add_resource_element: function(name, section, module) { var modsel = this.get_mods_element(section); var resel = { @@ -451,7 +451,7 @@ M.course_dndupload = { progress: document.createElement('span') }; - resel.li.className = 'activity resource modtype_resource'; + resel.li.className = 'activity ' + module + ' modtype_' + module; resel.indentdiv.className = 'mod-indent'; resel.li.appendChild(resel.indentdiv); @@ -707,7 +707,7 @@ M.course_dndupload = { } // Add the file to the display - var resel = this.add_resource_element(file.name, section); + var resel = this.add_resource_element(file.name, section, module); // Update the progress bar as the file is uploaded xhr.upload.addEventListener('progress', function(e) { @@ -726,30 +726,37 @@ M.course_dndupload = { if (result) { if (result.error == 0) { // All OK - update the dummy element - resel.icon.src = result.icon; - resel.a.href = result.link; - resel.namespan.innerHTML = result.name; - if (!parseInt(result.visible, 10)) { - resel.a.className = 'dimmed'; - } - - if (result.groupingname) { - resel.groupingspan.innerHTML = '(' + result.groupingname + ')'; + if (result.content) { + // A label + resel.indentdiv.innerHTML = '
' + result.content + result.commands; } else { - resel.div.removeChild(resel.groupingspan); + // Not a label + resel.icon.src = result.icon; + resel.a.href = result.link; + resel.namespan.innerHTML = result.name; + + if (!parseInt(result.visible, 10)) { + resel.a.className = 'dimmed'; + } + + if (result.groupingname) { + resel.groupingspan.innerHTML = '(' + result.groupingname + ')'; + } else { + resel.div.removeChild(resel.groupingspan); + } + + resel.div.removeChild(resel.progressouter); + resel.indentdiv.innerHTML += result.commands; + if (result.onclick) { + resel.a.onclick = result.onclick; + } + if (self.Y.UA.gecko > 0) { + // Fix a Firefox bug which makes sites with a '~' in their wwwroot + // log the user out when clicking on the link (before refreshing the page). + resel.div.innerHTML = unescape(resel.div.innerHTML); + } } - - resel.div.removeChild(resel.progressouter); resel.li.id = result.elementid; - resel.indentdiv.innerHTML += result.commands; - if (result.onclick) { - resel.a.onclick = result.onclick; - } - if (self.Y.UA.gecko > 0) { - // Fix a Firefox bug which makes sites with a '~' in their wwwroot - // log the user out when clicking on the link (before refreshing the page). - resel.div.innerHTML = unescape(resel.div.innerHTML); - } self.add_editing(result.elementid); } else { // Error - remove the dummy element @@ -905,7 +912,7 @@ M.course_dndupload = { var self = this; // Add the item to the display - var resel = this.add_resource_element(name, section); + var resel = this.add_resource_element(name, section, module); // Wait for the AJAX call to complete, then update the // dummy element with the returned details @@ -916,30 +923,37 @@ M.course_dndupload = { if (result) { if (result.error == 0) { // All OK - update the dummy element - resel.icon.src = result.icon; - resel.a.href = result.link; - resel.namespan.innerHTML = result.name; - if (!parseInt(result.visible, 10)) { - resel.a.className = 'dimmed'; - } - - if (result.groupingname) { - resel.groupingspan.innerHTML = '(' + result.groupingname + ')'; + if (result.content) { + // A label + resel.indentdiv.innerHTML = '
' + result.content + result.commands; } else { - resel.div.removeChild(resel.groupingspan); + // Not a label + resel.icon.src = result.icon; + resel.a.href = result.link; + resel.namespan.innerHTML = result.name; + + if (!parseInt(result.visible, 10)) { + resel.a.className = 'dimmed'; + } + + if (result.groupingname) { + resel.groupingspan.innerHTML = '(' + result.groupingname + ')'; + } else { + resel.div.removeChild(resel.groupingspan); + } + + resel.div.removeChild(resel.progressouter); + resel.div.innerHTML += result.commands; + if (result.onclick) { + resel.a.onclick = result.onclick; + } + if (self.Y.UA.gecko > 0) { + // Fix a Firefox bug which makes sites with a '~' in their wwwroot + // log the user out when clicking on the link (before refreshing the page). + resel.div.innerHTML = unescape(resel.div.innerHTML); + } } - - resel.div.removeChild(resel.progressouter); resel.li.id = result.elementid; - resel.div.innerHTML += result.commands; - if (result.onclick) { - resel.a.onclick = result.onclick; - } - if (self.Y.UA.gecko > 0) { - // Fix a Firefox bug which makes sites with a '~' in their wwwroot - // log the user out when clicking on the link (before refreshing the page). - resel.div.innerHTML = unescape(resel.div.innerHTML); - } self.add_editing(result.elementid, sectionnumber); } else { // Error - remove the dummy element diff --git a/course/dnduploadlib.php b/course/dnduploadlib.php index 73c8a79ce0f24..0cdd8bc5915b2 100644 --- a/course/dnduploadlib.php +++ b/course/dnduploadlib.php @@ -678,7 +678,12 @@ protected function send_response($mod) { $resp->error = self::ERROR_OK; $resp->icon = $mod->get_icon_url()->out(); $resp->name = $mod->name; - $resp->link = $mod->get_url()->out(); + if ($mod->has_view()) { + $resp->link = $mod->get_url()->out(); + } else { + $resp->link = null; + } + $resp->content = $mod->get_content(); $resp->elementid = 'module-'.$mod->id; $actions = course_get_cm_edit_actions($mod, 0, $mod->sectionnum); $resp->commands = ' '. $courserenderer->course_section_cm_edit_actions($actions); diff --git a/mod/label/lang/en/label.php b/mod/label/lang/en/label.php index 508e43be1c037..b99b6ddced6bc 100644 --- a/mod/label/lang/en/label.php +++ b/mod/label/lang/en/label.php @@ -24,6 +24,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['configdndmedia'] = 'Offer to create a label when media files are dragged & dropped onto a course'; +$string['dndmedia'] = 'Media drag and drop'; +$string['dnduploadlabel'] = 'Create a label'; $string['label:addinstance'] = 'Add a new label'; $string['labeltext'] = 'Label text'; $string['modulename'] = 'Label'; diff --git a/mod/label/lib.php b/mod/label/lib.php index 2ca3cc976acee..e8fd3f315f2d6 100644 --- a/mod/label/lib.php +++ b/mod/label/lib.php @@ -202,3 +202,55 @@ function label_supports($feature) { } } +/** + * Register the ability to handle drag and drop file uploads + * @return array containing details of the files / types the mod can handle + */ +function label_dndupload_register() { + if (get_config('label', 'dndmedia')) { + $mediaextensions = file_get_typegroup('extension', 'web_image'); + $strdnd = get_string('dnduploadlabel', 'mod_label'); + $files = array(); + foreach ($mediaextensions as $extn) { + $extn = trim($extn, '.'); + $files[] = array('extension' => $extn, 'message' => $strdnd); + } + return array('files' => $files); + } else { + return array(); + } +} + +/** + * 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 label_dndupload_handle($uploadinfo) { + global $USER; + + // Gather the required info. + $data = new stdClass(); + $data->course = $uploadinfo->course->id; + $data->name = $uploadinfo->displayname; + $data->intro = ''; + $data->introformat = FORMAT_HTML; + $data->coursemodule = $uploadinfo->coursemodule; + + if (!empty($uploadinfo->draftitemid)) { + $fs = get_file_storage(); + $draftcontext = context_user::instance($USER->id); + $context = context_module::instance($uploadinfo->coursemodule); + $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false); + if ($file = reset($files)) { + $filelink = moodle_url::make_draftfile_url($uploadinfo->draftitemid, $file->get_filepath(), $file->get_filename()); + if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) { + $data->intro = html_writer::empty_tag('img', array('src' => $filelink, 'alt' => $file->get_filename())); + $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0, + null, $data->intro); + } + } + } + + return label_add_instance($data, null); +} diff --git a/mod/label/settings.php b/mod/label/settings.php new file mode 100644 index 0000000000000..7ef8d7526a272 --- /dev/null +++ b/mod/label/settings.php @@ -0,0 +1,31 @@ +. + +/** + * Resource module admin settings and defaults + * + * @package mod_label + * @copyright 2013 Davo Smith, Synergy Learning + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +if ($ADMIN->fulltree) { + $settings->add(new admin_setting_configcheckbox('label/dndmedia', + get_string('dndmedia', 'mod_label'), get_string('configdndmedia', 'mod_label'), 1)); +}