From 12fab7081eb8df4efdd799692952d776c0798e88 Mon Sep 17 00:00:00 2001 From: skodak Date: Wed, 19 Nov 2008 21:53:37 +0000 Subject: [PATCH] fooMDL-16698 formslib filenamager element: forum converted to use new filemanaer element (no JS/picker integration for now sorry) --- lib/filelib.php | 100 +++++----------------- lib/form/filemanager.php | 18 ++-- mod/forum/lib.php | 67 +++------------ mod/forum/post.php | 175 ++++++++++++++++++--------------------- mod/forum/post_form.php | 9 +- user/editadvanced.php | 3 - 6 files changed, 124 insertions(+), 248 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index e801bdd052..fbd52fd1e9 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -93,7 +93,7 @@ function file_get_new_draftitemid() { * @param int &$draftitemid * @param int $contextid * @param string $filearea - * @param int $itemid + * @param int $itemid (null menas no existing files yet) * @param bool subdirs allow directory structure * @param string $text usually html text with embedded links to draft area * @param boolean $forcehttps force https @@ -109,9 +109,9 @@ function file_prepare_draftarea(&$draftitemid, $contextid, $filearea, $itemid, $ // create a new area and copy existing files into $draftitemid = file_get_new_draftitemid(); $file_record = array('contextid'=>$usercontext->id, 'filearea'=>'user_draft', 'itemid'=>$draftitemid); - if ($files = $fs->get_area_files($contextid, $filearea, $itemid)) { + if (!is_null($itemid) and $files = $fs->get_area_files($contextid, $filearea, $itemid)) { foreach ($files as $file) { - if (!$subdirs and $file->get_filepath() !== '/') { + if (!$subdirs and ($file->is_directory() or $file->get_filepath() !== '/')) { continue; } $fs->create_file_from_storedfile($file_record, $file); @@ -142,6 +142,24 @@ function file_prepare_draftarea(&$draftitemid, $contextid, $filearea, $itemid, $ return $text; } +/** + * Returns information about files in draft area + * @param $draftitemid + * @return array TODO: count=>n + */ +function get_draftarea_info($draftitemid) { + + global $CFG, $USER; + + $usercontext = get_context_instance(CONTEXT_USER, $USER->id); + $fs = get_file_storage(); + + // number of files + $draftfiles = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id', false); + + return array('filecount'=>count($draftfiles)); +} + /** * Converts absolute links in text and merges draft files to target area. * @param int $draftitemid @@ -280,82 +298,6 @@ function file_get_upload_error($errorcode) { return $errmessage; } -/** - * Finds occurences of a link to "draftfile.php" in text and replaces the - * address based on passed information. Matching is performed using the given - * current itemid, contextid and filearea and $CFG->wwwroot. This function - * replaces all the urls for one file. If more than one files were sent, it - * must be called once for each file. - * - * @uses $CFG - * @see file_storage::move_draft_to_final() - * - * @param $text string text to modify - * @param $contextid int context that the files should be assigned to - * @param $filepath string filepath under which the files should be saved - * @param $filearea string filearea into which the files should be saved - * @param $itemid int the itemid to assign to the files - * @param $currentcontextid int the current contextid of the files - * @param $currentfilearea string the current filearea of the files (defaults - * to "user_draft") - * @return string modified $text, or null if an error occured. - */ -function file_rewrite_urls($text, $contextid, $filepath, $filearea, $itemid, $currentcontextid, $currentfilearea = 'user_draft') { - global $CFG; - - $context = get_context_instance_by_id($contextid); - $currentcontext = get_context_instance_by_id($currentcontextid); - $fs = get_file_storage(); - - //Make sure this won't match wrong stuff, as much as possible (can probably be improved) - // * using $currentcontextid in here ensures that we're only matching files belonging to current user - // * placeholders: {wwwroot}/draftfile.php/{currentcontextid}/{currentfilearea}/{itemid}{/filepath}/{filename} - // * filepath is optional, everything else is guaranteed to be there. - $re = '|'. preg_quote($CFG->wwwroot) .'/draftfile.php/'. $currentcontextid .'/'. $currentfilearea .'/([0-9]+)(/[A-Fa-f0-9]+)?/([^\'^"^\>]+)|'; - $matches = array(); - if (!preg_match_all($re, $text, $matches, PREG_SET_ORDER)) { - return $text; // no draftfile url in text, no replacement necessary. - } - - $replacedfiles = array(); - foreach($matches as $file) { - - $currenturl = $file[0]; - $currentitemid = $file[1]; - if (!empty($file[2])) { - $currentfilepath = $file[2]; - } - $currentfilepath .= '/'; - $currentfilename = $file[3]; - - // if a new upload has the same file path/name as an existing file, but different content, we put it in a distinct path. - $existingfile = $fs->get_file($currentcontextid, $currentfilearea, $currentitemid, $currentfilepath, $currentfilename); - $uploadedfile = $fs->get_file($contextid, $filearea, $itemid, $filepath, $currentfilename); - if ($existingfile && $uploadedfile && ($existingfile->get_contenthash() != $uploadedfile->get_contenthash())) { - $filepath .= $currentitemid .'/'; - } - - if ($newfiles = $fs->move_draft_to_final($currentitemid, $contextid, $filearea, $itemid, $filepath, false)) { - foreach($newfiles as $newfile) { - if (in_array($newfile, $replacedfiles)) { - // if a file is being used more than once, all occurences will be replaced the first time, so ignore it when it comes back. - // ..it wouldn't be in user_draft anymore anyway! - continue; - } - $replacedfiles[] = $newfile; - if ($context->contextlevel == CONTEXT_USER) { - $newurl = $CFG->wwwroot .'/userfile.php/'. $contextid .'/'. $filearea . $newfile->get_filepath() . $newfile->get_filename(); - } else { - $newurl = $CFG->wwwroot .'/pluginfile.php/'. $contextid .'/'. $filearea .'/'. $itemid . $newfile->get_filepath() . $newfile->get_filename(); - } - $text = str_replace('"'. $currenturl .'"', '"'. $newurl .'"', $text); - } - } // else file not found, wrong file, or string is just not a file so we leave it alone. - - } - return $text; -} - /** * Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present. * Due to security concerns only downloads from http(s) sources are supported. diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php index b86acb0ca5..2b3bfcfe7b 100644 --- a/lib/form/filemanager.php +++ b/lib/form/filemanager.php @@ -62,19 +62,19 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { $this->_options['maxfiles'] = $num; } - function setHelpButton($_helpbuttonargs, $function='_helpbutton') { - if (!is_array($_helpbuttonargs)) { - $_helpbuttonargs = array($_helpbuttonargs); - } else { - $_helpbuttonargs = $_helpbuttonargs; + function setHelpButton($helpbuttonargs, $function='helpbutton'){ + if (!is_array($helpbuttonargs)){ + $helpbuttonargs=array($helpbuttonargs); + }else{ + $helpbuttonargs=$helpbuttonargs; } //we do this to to return html instead of printing it //without having to specify it in every call to make a button. - if ('_helpbutton' == $function){ - $defaultargs = array('', '', 'moodle', true, false, '', true); - $_helpbuttonargs = $_helpbuttonargs + $defaultargs ; + if ('helpbutton' == $function){ + $defaultargs=array('', '', 'moodle', true, false, '', true); + $helpbuttonargs=$helpbuttonargs + $defaultargs ; } - $this->_helpbutton=call_user_func_array($function, $_helpbuttonargs); + $this->_helpbutton=call_user_func_array($function, $helpbuttonargs); } function getHelpButton() { diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 31e34a5abc..d581b2cd39 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -4047,64 +4047,22 @@ function forum_pluginfile($course, $cminfo, $context, $filearea, $args) { * @param $newfile is a full upload array from $_FILES * @param $message is a string to hold the messages. */ -function forum_add_attachment($post, $forum, $cm, $mform=null, &$message=null, $remove_previous=true) { - global $CFG, $DB, $USER; +function forum_add_attachment($post, $forum, $cm, $mform=null, &$message=null) { + global $DB; if (empty($mform)) { return false; } - $filearea = 'forum_attachment'; - - $fs = get_file_storage(); $context = get_context_instance(CONTEXT_MODULE, $cm->id); - // The logic here really needs work to handle re-editing properly TODO XXX - - if ($remove_previous) { // Remove ALL previous files for this post - $fs->delete_area_files($context->id, $filearea, $post->id); - $post->attachment = 0; - $DB->set_field('forum_posts', 'attachment', 0, array('id'=>$post->id)); // Update it now in case we fail later on - } - - $values = $mform->get_data(); + $info = get_draftarea_info($post->attachments); + $present = ($info['filecount']>0) ? '1' : ''; + file_convert_draftarea($post->attachments, $context->id, 'forum_attachment', $post->id, false); - if (!isset($forum->maxattachments)) { // TODO - delete this once we add a field to the forum table - $forum->maxattachments = 3; - } + $DB->set_field('forum_posts', 'attachment', $present, array('id'=>$post->id)); - for ($i=0; $i<$forum->maxattachments; $i++) { - $elementname = 'attachment'.$i; - if (empty($values->$elementname)) { // Nothing defined - continue; - } - if (!is_numeric($values->$elementname)) { // Pre-existing file reference, skip it - continue; - } - if ($mform->save_stored_file($elementname, $context->id, $filearea, $post->id, '/', null, true, $USER->id)) { - $post->attachment = '1'; - $DB->set_field('forum_posts', 'attachment', 1, array('id'=>$post->id)); // Update it now in case we fail later on - } else { - return false; - } - } -} - -/** - * Relinks urls linked to draftfile.php in $post->message. - */ -function forum_relink_inline_attachments($post, $forum, $cm){ - global $DB; - - $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $authorcontext = get_context_instance(CONTEXT_USER, $post->userid); - - if ($post->message = file_rewrite_urls($post->message, $context->id, '/', 'forum_post', $post->id, $authorcontext->id)) { - $DB->set_field("forum_posts", "message", $post->message, array("id" => $post->id)); - return true; - } else { - return false; - } + return true; } /** @@ -4126,8 +4084,7 @@ function forum_add_new_post($post, $mform, &$message) { return false; } - forum_relink_inline_attachments($post, $forum, $cm); - forum_add_attachment($post, $forum, $cm, $mform, $message, false); + forum_add_attachment($post, $forum, $cm, $mform, $message); // Update discussion modified date $DB->set_field("forum_discussions", "timemodified", $post->modified, array("id" => $post->discussion)); @@ -4169,8 +4126,7 @@ function forum_update_post($post, $mform, &$message) { return false; } - forum_relink_inline_attachments($post, $forum, $cm); - forum_add_attachment($post, $forum, $cm, $mform, $message, false); + forum_add_attachment($post, $forum, $cm, $mform, $message); if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { forum_tp_mark_post_read($post->userid, $post, $post->forum); @@ -4203,7 +4159,7 @@ function forum_add_discussion($discussion, $mform=null, &$message=null) { $post->mailed = 0; $post->subject = $discussion->name; $post->message = $discussion->intro; - $post->attachment = 0; + $post->attachments = $discussion->attachments; $post->forum = $forum->id; // speedup $post->course = $forum->course; // speedup $post->format = $discussion->format; @@ -4233,8 +4189,7 @@ function forum_add_discussion($discussion, $mform=null, &$message=null) { } if (!empty($cm->id)) { - forum_relink_inline_attachments($post, $forum, $cm); - forum_add_attachment($post, $forum, $cm, $mform, $message, false); + forum_add_attachment($post, $forum, $cm, $mform, $message); } if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) { diff --git a/mod/forum/post.php b/mod/forum/post.php index 69fccd5c67..9c7684c5f3 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -14,6 +14,7 @@ $name = optional_param('name', '', PARAM_CLEAN); $confirm = optional_param('confirm', 0, PARAM_INT); $groupid = optional_param('groupid', null, PARAM_INT); + $draftitemid = optional_param('attachments', 0, PARAM_INT); //these page_params will be passed as hidden variables later in the form. @@ -436,10 +437,19 @@ $coursecontext = get_context_instance(CONTEXT_COURSE, $forum->course); } + +// from now on user must be logged on properly + if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs error('Could not get the course module for the forum instance.'); } $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + require_login($course->id, false, $cm); + + if (isguestuser()) { + // just in case + print_error('noguest'); + } if (!isset($forum->maxattachments)) { // TODO - delete this once we add a field to the forum table $forum->maxattachments = 3; @@ -447,9 +457,76 @@ $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post)); - if ($fromform = $mform_post->get_data()) { + file_prepare_draftarea($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id , false); - require_login($course, false, $cm); + //load data into form NOW! + + if ($USER->id != $post->userid) { // Not the original author, so add a message to the end + $data->date = userdate($post->modified); + if ($post->format == FORMAT_HTML) { + $data->name = ''. + fullname($USER).''; + $post->message .= '

('.get_string('editedby', 'forum', $data).')

'; + } else { + $data->name = fullname($USER); + $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')'; + } + } + + if (!empty($parent)) { + $heading = get_string("yourreply", "forum"); + } else { + if ($forum->type == 'qanda') { + $heading = get_string('yournewquestion', 'forum'); + } else { + $heading = get_string('yournewtopic', 'forum'); + } + } + + if (forum_is_subscribed($USER->id, $forum->id)) { + $subscribe = true; + + } else if (forum_user_has_posted($forum->id, 0, $USER->id)) { + $subscribe = false; + + } else { + // user not posted yet - use subscription default specified in profile + $subscribe = !empty($USER->autosubscribe); + } + + $mform_post->set_data(array( 'attachments'=>$draftitemid, + 'general'=>$heading, + 'subject'=>$post->subject, + 'message'=>$post->message, + 'subscribe'=>$subscribe?1:0, + 'mailnow'=>!empty($post->mailnow), + 'userid'=>$post->userid, + 'parent'=>$post->parent, + 'discussion'=>$post->discussion, + 'course'=>$course->id) + + $page_params + + + (isset($post->format)?array( + 'format'=>$post->format): + array())+ + + (isset($discussion->timestart)?array( + 'timestart'=>$discussion->timestart): + array())+ + + (isset($discussion->timeend)?array( + 'timeend'=>$discussion->timeend): + array())+ + + (isset($post->groupid)?array( + 'groupid'=>$post->groupid): + array())+ + + (isset($discussion->id)? + array('discussion'=>$discussion->id): + array())); + + if ($fromform = $mform_post->get_data()) { if (empty($SESSION->fromurl)) { $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id"; @@ -638,12 +715,6 @@ // $course, $forum are defined. $discussion is for edit and reply only. - $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id); - - require_login($course->id, false, $cm); - - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); - if ($post->discussion) { if (! $toppost = $DB->get_record("forum_posts", array("discussion" => $post->discussion, "parent" => 0))) { error("Could not find top parent of post $post->id"); @@ -718,103 +789,15 @@ forum_print_posts_threaded($course, $cm, $forum, $discussion, $parent, 0, false, false, $forumtracked, $posts); } } - $heading = get_string("yourreply", "forum"); } else { $forum->intro = trim($forum->intro); if (!empty($forum->intro)) { print_box(format_text($forum->intro), 'generalbox', 'intro'); } - if ($forum->type == 'qanda') { - $heading = get_string('yournewquestion', 'forum'); - } else { - $heading = get_string('yournewtopic', 'forum'); - } } - if ($USER->id != $post->userid) { // Not the original author, so add a message to the end - $data->date = userdate($post->modified); - if ($post->format == FORMAT_HTML) { - $data->name = ''. - fullname($USER).''; - $post->message .= '

('.get_string('editedby', 'forum', $data).')

'; - } else { - $data->name = fullname($USER); - $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')'; - } - } - - //load data into form - - if (forum_is_subscribed($USER->id, $forum->id)) { - $subscribe = true; - - } else if (forum_user_has_posted($forum->id, 0, $USER->id)) { - $subscribe = false; - - } else { - // user not posted yet - use subscription default specified in profile - $subscribe = !empty($USER->autosubscribe); - } - - $defaultattachments = array(); - - if ($forum->maxattachments) { - for ($i=0; $i<$forum->maxattachments; $i++) { - $defaultattachments['attachment'.$i] = ''; - } - - if (!empty($post->attachment)) { // We already have some attachments, so show them - $fs = get_file_storage(); - - $i = 0; - if ($files = $fs->get_area_files($modcontext->id, 'forum_attachment', $post->id, "timemodified ASC", false)) { - foreach ($files as $file) { - $defaultattachments['attachment'.$i] = $file->get_filename(); - $i++; - } - } - } - } - - // HACK ALERT: this is very wrong, the defaults should be always initialized before calling $mform->get_data() !!! - $mform_post->set_data(array( 'general'=>$heading, - 'subject'=>$post->subject, - 'message'=>$post->message, - 'subscribe'=>$subscribe?1:0, - 'mailnow'=>!empty($post->mailnow), - 'userid'=>$post->userid, - 'parent'=>$post->parent, - 'discussion'=>$post->discussion, - 'course'=>$course->id) + - - $defaultattachments + - - $page_params + - - (isset($post->format)?array( - 'format'=>$post->format): - array())+ - - (isset($discussion->timestart)?array( - 'timestart'=>$discussion->timestart): - array())+ - - (isset($discussion->timeend)?array( - 'timeend'=>$discussion->timeend): - array())+ - - (isset($post->groupid)?array( - 'groupid'=>$post->groupid): - array())+ - - (isset($discussion->id)? - array('discussion'=>$discussion->id): - array())); - - $mform_post->display(); - print_footer($course); diff --git a/mod/forum/post_form.php b/mod/forum/post_form.php index 16af6384b2..5c0036f25d 100644 --- a/mod/forum/post_form.php +++ b/mod/forum/post_form.php @@ -21,7 +21,7 @@ class mod_forum_post_form extends moodleform { $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"'); $mform->setType('subject', PARAM_TEXT); $mform->addRule('subject', get_string('required'), 'required', null, 'client'); - $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); + $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); $mform->addElement('htmleditor', 'message', get_string('message', 'forum'), array('cols'=>50, 'rows'=>30, 'filearea'=>'forumpost')); $mform->setType('message', PARAM_RAW); @@ -53,10 +53,9 @@ class mod_forum_post_form extends moodleform { } if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all - for ($i=0; $i<$forum->maxattachments; $i++) { - $mform->addElement('filepicker', 'attachment'.$i, get_string('attachment', 'forum')); - $mform->setHelpButton('attachment'.$i, array('attachment2', get_string('attachment', 'forum'), 'forum')); - } + $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), + array('subdirs'=>0, 'maxbytes'=>$forum->maxbytes, 'maxfiles'=>$forum->maxattachments)); + $mform->setHelpButton('attachments', array('attachment2', get_string('attachment', 'forum'), 'forum')); } if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert diff --git a/user/editadvanced.php b/user/editadvanced.php index c9c03e4453..61dd786150 100644 --- a/user/editadvanced.php +++ b/user/editadvanced.php @@ -125,9 +125,6 @@ } $usercontext = get_context_instance(CONTEXT_USER, $usernew->id); -/* if ($usernew->description = file_rewrite_urls($usernew->description, $usercontext->id, '/gudu/', 'user_profile', 0, $usercontext->id)) { - $DB->set_field("user", "description", $usernew->description, array("id" => $usernew->id)); - }*/ //update preferences useredit_update_user_preference($usernew); -- 2.39.5