From 5103b5e66790047586b52516f852fe751c1e6a50 Mon Sep 17 00:00:00 2001 From: Davo Smith Date: Mon, 14 May 2012 09:48:04 +0100 Subject: [PATCH] MDL-22504 Course drag and drop upload - now correctly ignores disabled modules and allows upload to orphaned sections --- course/lib.php | 6 ++++-- course/view.php | 2 +- index.php | 2 +- lib/ajax/dndupload.js | 3 +++ lib/dnduploadlib.php | 24 +++++++++++++++++++----- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/course/lib.php b/course/lib.php index 8dca6d7dfc842..27afd07740e0f 100644 --- a/course/lib.php +++ b/course/lib.php @@ -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 @@ -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); } diff --git a/course/view.php b/course/view.php index bc2fb37c758a2..e17b779445703 100644 --- a/course/view.php +++ b/course/view.php @@ -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(); diff --git a/index.php b/index.php index e07f11ad685f2..20f70ca352b37 100644 --- a/index.php +++ b/index.php @@ -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)) { diff --git a/lib/ajax/dndupload.js b/lib/ajax/dndupload.js index a12842377729e..72140330e3af9 100644 --- a/lib/ajax/dndupload.js +++ b/lib/ajax/dndupload.js @@ -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'), diff --git a/lib/dnduploadlib.php b/lib/dnduploadlib.php index e6bddc12bc363..3416dfd770fa3 100644 --- a/lib/dnduploadlib.php +++ b/lib/dnduploadlib.php @@ -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( @@ -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. @@ -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) { @@ -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; @@ -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.