Skip to content

Commit

Permalink
MDL-22504 Course drag and drop upload - now correctly ignores disable…
Browse files Browse the repository at this point in the history
…d modules and allows upload to orphaned sections
  • Loading branch information
davosmith committed May 14, 2012
1 parent abed5d6 commit 5103b5e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
6 changes: 4 additions & 2 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4445,13 +4445,15 @@ function course_page_type_list($pagetype, $parentcontext, $currentcontext) {
* @param integer $id The ID of the course being applied to
* @param array $modules An array containing the names of the modules in
* use on the page
* @param array $allmodules An array containing the names of the enabled (visible)
* modules on this site
* @param object $config An object containing configuration parameters for ajax modules including:
* * resourceurl The URL to post changes to for resource changes
* * sectionurl The URL to post changes to for section changes
* * pageparams Additional parameters to pass through in the post
* @return void
*/
function include_course_ajax($course, $modules = array(), $config = null) {
function include_course_ajax($course, $modules = array(), $allmodules = null, $config = null) {
global $PAGE, $CFG, $USER;

// Ensure that ajax should be included
Expand Down Expand Up @@ -4560,5 +4562,5 @@ function include_course_ajax($course, $modules = array(), $config = null) {
}

// Load drag and drop upload AJAX.
dndupload_add_to_course($course);
dndupload_add_to_course($course, $allmodules);
}
2 changes: 1 addition & 1 deletion course/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,6 @@
echo html_writer::end_tag('div');

// Include the command toolbox YUI module
include_course_ajax($course, $modnamesused);
include_course_ajax($course, $modnamesused, $modnames);

echo $OUTPUT->footer();
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
echo $OUTPUT->box_end();
}
}
include_course_ajax($SITE, $modnamesused);
include_course_ajax($SITE, $modnamesused, $modnames);


if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) {
Expand Down
3 changes: 3 additions & 0 deletions lib/ajax/dndupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ M.course_dndupload = {
*/
drag_type: function(e) {
if (this.types_includes(e, 'Files')) {
if (this.handlers.filehandlers.length == 0) {
return false; // No available file handlers - ignore this drag.
}
return {
realtype: 'Files',
addmessage: M.util.get_string('addfilehere', 'core_dndupload'),
Expand Down
24 changes: 19 additions & 5 deletions lib/dnduploadlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@
* Add the Javascript to enable drag and drop upload to a course page
*
* @param object $course The currently displayed course
* @param array $modnames The list of enabled (visible) modules on this site
* @return void
*/
function dndupload_add_to_course($course) {
function dndupload_add_to_course($course, $modnames) {
global $CFG, $PAGE;

// Get all handlers.
$handler = new dndupload_handler($course);
$handler = new dndupload_handler($course, $modnames);
$jsdata = $handler->get_js_data();
if (empty($jsdata->types) && empty($jsdata->filehandlers)) {
return; // No valid handlers - don't enable drag and drop.
}

// Add the javascript to the page.
$jsmodule = array(
Expand Down Expand Up @@ -93,7 +99,7 @@ class dndupload_handler {
*
* @param object $course The course this is being added to (to check course_allowed_module() )
*/
public function __construct($course) {
public function __construct($course, $modnames = null) {
global $CFG;

// Add some default types to handle.
Expand All @@ -110,8 +116,11 @@ public function __construct($course) {
$mods = get_plugin_list_with_function('mod', 'dndupload_register');
foreach ($mods as $component => $funcname) {
list($modtype, $modname) = normalize_component($component);
if ($modnames && !array_key_exists($modname, $modnames)) {
continue; // Module is deactivated (hidden) at the site level.
}
if (!course_allowed_module($course, $modname)) {
continue;
continue; // User does not have permission to add this module to the course.
}
$resp = $funcname();
if (!$resp) {
Expand Down Expand Up @@ -387,7 +396,7 @@ public function __construct($courseid, $section, $type, $modulename) {
require_login($this->course, false);
$this->context = context_course::instance($this->course->id);

if (!is_number($section) || $section < 0 || $section > $this->course->numsections) {
if (!is_number($section) || $section < 0) {
throw new coding_exception("Invalid section number $section");
}
$this->section = $section;
Expand Down Expand Up @@ -592,6 +601,11 @@ protected function finish_setup_course_module($instanceid) {
rebuild_course_cache($this->course->id, true);
$this->course->modinfo = null; // Otherwise we will just get the old version back again.
$info = get_fast_modinfo($this->course);
if (!isset($info->cms[$this->cm->id])) {
// The course module has not been properly created in the course - undo everything.
delete_course_module($this->cm->id);
throw new moodle_exception('errorcreatingactivity', 'core_dndupload', '', $this->module->name);
}
$mod = $info->cms[$this->cm->id];

// Trigger mod_created event with information about this module.
Expand Down

0 comments on commit 5103b5e

Please sign in to comment.