Skip to content

Commit

Permalink
MDL-32456 mod_label - support course drag and drop upload images to c…
Browse files Browse the repository at this point in the history
…reate labels
  • Loading branch information
davosmith committed Feb 8, 2013
1 parent 6319737 commit 785e09a
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 47 deletions.
106 changes: 60 additions & 46 deletions course/dndupload.js
Expand Up @@ -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 = {
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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 = '<div class="activityinstance" ></div>' + 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
Expand Down Expand Up @@ -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
Expand All @@ -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 = '<div class="activityinstance" ></div>' + 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
Expand Down
7 changes: 6 additions & 1 deletion course/dnduploadlib.php
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions mod/label/lang/en/label.php
Expand Up @@ -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';
Expand Down
52 changes: 52 additions & 0 deletions mod/label/lib.php
Expand Up @@ -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);
}
31 changes: 31 additions & 0 deletions mod/label/settings.php
@@ -0,0 +1,31 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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));
}

0 comments on commit 785e09a

Please sign in to comment.