* @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
// 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);
return $text;
}
+/**
+ * Returns information about files in draft area
+ * @param <type> $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
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.
$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() {
* @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;
}
/**
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));
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);
$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;
}
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)) {
$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.
$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;
$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 = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
+ fullname($USER).'</a>';
+ $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>';
+ } 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";
// $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");
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 = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
- fullname($USER).'</a>';
- $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>';
- } 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);
$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);
}
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
}
$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);