MDL-23202 group icons now stored in file storage pool
This commit is contained in:
+4
-4
@@ -58,7 +58,7 @@ require_login($course);
|
||||
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
||||
require_capability('moodle/course:managegroups', $context);
|
||||
|
||||
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&group='.$id;
|
||||
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&group='.$id;
|
||||
|
||||
if ($id and $delete) {
|
||||
if (!$confirm) {
|
||||
@@ -100,10 +100,10 @@ if ($editform->is_cancelled()) {
|
||||
} elseif ($data = $editform->get_data()) {
|
||||
|
||||
if ($data->id) {
|
||||
groups_update_group($data, $editform);
|
||||
groups_update_group($data, $editform, $editoroptions);
|
||||
} else {
|
||||
$id = groups_create_group($data, $editform);
|
||||
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&group='.$id;
|
||||
$id = groups_create_group($data, $editform, $editoroptions);
|
||||
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id.'&group='.$id;
|
||||
}
|
||||
|
||||
redirect($returnurl);
|
||||
|
||||
@@ -10,9 +10,7 @@
|
||||
* @package groups
|
||||
*/
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
||||
}
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
require_once($CFG->dirroot.'/lib/formslib.php');
|
||||
|
||||
@@ -37,9 +35,7 @@ class group_form extends moodleform {
|
||||
$mform->setHelpButton('enrolmentkey', array('groupenrolmentkey', get_string('enrolmentkey', 'group')), true);
|
||||
$mform->setType('enrolmentkey', PARAM_RAW);
|
||||
|
||||
$maxbytes = get_max_upload_file_size($CFG->maxbytes, $COURSE->maxbytes);
|
||||
|
||||
if (!empty($CFG->gdversion) and $maxbytes) {
|
||||
if (!empty($CFG->gdversion)) {
|
||||
$options = array(get_string('no'), get_string('yes'));
|
||||
$mform->addElement('select', 'hidepicture', get_string('hidepicture'), $options);
|
||||
|
||||
|
||||
+65
-42
@@ -112,56 +112,53 @@ function groups_remove_member($grouporid, $userorid) {
|
||||
|
||||
/**
|
||||
* Add a new group
|
||||
* @param object $data group properties (with magic quotes)
|
||||
* @param object $data group properties
|
||||
* @param object $um upload manager with group picture
|
||||
* @return id of group or false if error
|
||||
*/
|
||||
function groups_create_group($data, $editform=false, $editoroptions=null) {
|
||||
function groups_create_group($data, $editform = false, $editoroptions = false) {
|
||||
global $CFG, $DB;
|
||||
require_once("$CFG->libdir/gdlib.php");
|
||||
|
||||
//check that courseid exists
|
||||
$course = $DB->get_record('course', array('id' => $data->courseid), '*', MUST_EXIST);
|
||||
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
||||
|
||||
$data->timecreated = time();
|
||||
$data->timemodified = $data->timecreated;
|
||||
$data->name = trim($data->name);
|
||||
|
||||
if ($editform) {
|
||||
if ($editform and $editoroptions) {
|
||||
$data->description = $data->description_editor['text'];
|
||||
$data->descriptionformat = $data->description_editor['format'];
|
||||
}
|
||||
|
||||
$id = $DB->insert_record('groups', $data);
|
||||
$data->id = $DB->insert_record('groups', $data);
|
||||
|
||||
if ($editform and $editoroptions) {
|
||||
// Update description from editor with fixed files
|
||||
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context, 'group', 'description', $data->id);
|
||||
$upd = new object();
|
||||
$upd->id = $data->id;
|
||||
$upd->description = $data->description;
|
||||
$upd->descriptionformat = $data->descriptionformat;
|
||||
$DB->update_record('groups', $upd);
|
||||
}
|
||||
|
||||
$group = $DB->get_record('groups', array('id'=>$data->id));
|
||||
|
||||
$data->id = $id;
|
||||
if ($editform) {
|
||||
//update image
|
||||
if (save_profile_image($id, $editform, 'groups')) {
|
||||
$DB->set_field('groups', 'picture', 1, array('id'=>$id));
|
||||
}
|
||||
$data->picture = 1;
|
||||
|
||||
if (method_exists($editform, 'get_editor_options')) {
|
||||
// Update description from editor with fixed files
|
||||
$editoroptions = $editform->get_editor_options();
|
||||
$description = new stdClass;
|
||||
$description->id = $data->id;
|
||||
$description->description_editor = $data->description_editor;
|
||||
$description = file_postupdate_standard_editor($description, 'description', $editoroptions, $editoroptions['context'], 'group', 'description', $description->id);
|
||||
$DB->update_record('groups', $description);
|
||||
}
|
||||
groups_update_group_icon($group, $data, $editform);
|
||||
}
|
||||
|
||||
//trigger groups events
|
||||
events_trigger('groups_group_created', $data);
|
||||
events_trigger('groups_group_created', $group);
|
||||
|
||||
return $id;
|
||||
return $group->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new grouping
|
||||
* @param object $data grouping properties (with magic quotes)
|
||||
* @param object $data grouping properties
|
||||
* @return id of grouping or false if error
|
||||
*/
|
||||
function groups_create_grouping($data, $editoroptions=null) {
|
||||
@@ -195,35 +192,63 @@ function groups_create_grouping($data, $editoroptions=null) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update group
|
||||
* @param object $data group properties (with magic quotes)
|
||||
* @param object $um upload manager with group picture
|
||||
* @return boolean true or exception
|
||||
* Update the group icon from form data
|
||||
* @param $group
|
||||
* @param $data
|
||||
* @param $editform
|
||||
*/
|
||||
function groups_update_group($data, $editform=false) {
|
||||
function groups_update_group_icon($group, $data, $editform) {
|
||||
global $CFG, $DB;
|
||||
require_once("$CFG->libdir/gdlib.php");
|
||||
|
||||
$fs = get_file_storage();
|
||||
$context = get_context_instance(CONTEXT_COURSE, $group->courseid, MUST_EXIST);
|
||||
|
||||
//TODO: it would make sense to allow picture deleting too (skodak)
|
||||
|
||||
if ($iconfile = $editform->save_temp_file('imagefile')) {
|
||||
if (process_new_icon($context, 'group', 'icon', $group->id, $iconfile)) {
|
||||
$DB->set_field('groups', 'picture', 1, array('id'=>$group->id));
|
||||
$group->picture = 1;
|
||||
} else {
|
||||
$fs->delete_area_files($context->id, 'group', 'icon', $group->id);
|
||||
$DB->set_field('groups', 'picture', 0, array('id'=>$group->id));
|
||||
$group->picture = 0;
|
||||
}
|
||||
@unlink($iconfile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update group
|
||||
* @param object $data group properties (with magic quotes)
|
||||
* @param object $editform
|
||||
* @param array $editoroptions
|
||||
* @return boolean true or exception
|
||||
*/
|
||||
function groups_update_group($data, $editform = false, $editoroptions = false) {
|
||||
global $CFG, $DB;
|
||||
|
||||
$context = get_context_instance(CONTEXT_COURSE, $data->courseid);
|
||||
|
||||
$data->timemodified = time();
|
||||
$data->name = trim($data->name);
|
||||
|
||||
if ($editform && method_exists($editform, 'get_editor_options')) {
|
||||
$editoroptions = $editform->get_editor_options();
|
||||
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $editoroptions['context'], 'group', 'description', $data->id);
|
||||
if ($editform and $editoroptions) {
|
||||
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context, 'group', 'description', $data->id);
|
||||
}
|
||||
|
||||
$DB->update_record('groups', $data);
|
||||
|
||||
$group = $DB->get_record('groups', array('id'=>$data->id));
|
||||
|
||||
if ($editform) {
|
||||
//update image
|
||||
if (save_profile_image($data->id, $editform, 'groups')) {
|
||||
$DB->set_field('groups', 'picture', 1, array('id'=>$data->id));
|
||||
$data->picture = 1;
|
||||
}
|
||||
groups_update_group_icon($group, $data, $editform);
|
||||
}
|
||||
|
||||
//trigger groups events
|
||||
events_trigger('groups_group_updated', $data);
|
||||
events_trigger('groups_group_updated', $group);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -282,10 +307,8 @@ function groups_delete_group($grouporid) {
|
||||
// Delete all files associated with this group
|
||||
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
||||
$fs = get_file_storage();
|
||||
$files = $fs->get_area_files($context->id, 'group', 'description', $groupid);
|
||||
foreach ($files as $file) {
|
||||
$file->delete();
|
||||
}
|
||||
$fs->delete_area_files($context->id, 'group', 'description', $groupid);
|
||||
$fs->delete_area_files($context->id, 'group', 'icon', $groupid);
|
||||
|
||||
//trigger groups events
|
||||
events_trigger('groups_group_deleted', $group);
|
||||
|
||||
+2
-2
@@ -100,7 +100,7 @@ if ($rs = $DB->get_recordset_sql($sql, $params)) {
|
||||
$rs->close();
|
||||
}
|
||||
|
||||
$PAGE->settingsnav->get('courseadmin')->get('groups')->make_active();
|
||||
//$PAGE->settingsnav->get('courseadmin')->get('groups')->make_active(); // TODO: this breaks stuff
|
||||
$PAGE->navbar->add(get_string('overview', 'group'));
|
||||
|
||||
/// Print header
|
||||
@@ -158,7 +158,7 @@ foreach ($members as $gpgid=>$groupdata) {
|
||||
continue;
|
||||
}
|
||||
$line = array();
|
||||
$name = format_string($groups[$gpid]->name);
|
||||
$name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
|
||||
$description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
|
||||
$options = new stdClass;
|
||||
$options->noclean = true;
|
||||
|
||||
@@ -4735,6 +4735,12 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
|
||||
upgrade_main_savepoint(true, 2010071100);
|
||||
}
|
||||
|
||||
if ($oldversion < 2010071101) {
|
||||
// move user icons to file storage pool
|
||||
upgrade_migrate_group_icons();
|
||||
upgrade_main_savepoint(true, 2010071101);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
+49
-1
@@ -97,7 +97,7 @@ function upgrade_migrate_user_icons() {
|
||||
foreach ($rs as $user) {
|
||||
$i++;
|
||||
upgrade_set_timeout(60); /// Give upgrade at least 60 more seconds
|
||||
$pbar->update($i, $count, "Migrated course files - course $i/$count.");
|
||||
$pbar->update($i, $count, "Migrated user icons $i/$count.");
|
||||
|
||||
$context = get_context_instance(CONTEXT_USER, $user->id);
|
||||
|
||||
@@ -131,6 +131,54 @@ function upgrade_migrate_user_icons() {
|
||||
remove_dir("$CFG->dataroot/users");
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function - do not use directly
|
||||
*/
|
||||
function upgrade_migrate_group_icons() {
|
||||
global $CFG, $OUTPUT, $DB;
|
||||
|
||||
$fs = get_file_storage();
|
||||
|
||||
$icon = array('component'=>'group', 'filearea'=>'icon', 'filepath'=>'/');
|
||||
|
||||
$count = $DB->count_records('groups', array('picture'=>1));
|
||||
$pbar = new progress_bar('migrategroupfiles', 500, true);
|
||||
|
||||
$rs = $DB->get_recordset('groups', array('picture'=>1), 'courseid ASC', 'id, picture, courseid');
|
||||
$i = 0;
|
||||
foreach ($rs as $group) {
|
||||
$i++;
|
||||
upgrade_set_timeout(60); /// Give upgrade at least 60 more seconds
|
||||
$pbar->update($i, $count, "Migrated group icons $i/$count.");
|
||||
|
||||
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
||||
|
||||
if ($fs->file_exists($context->id, 'group', 'icon', $group->id, '/', 'f1.jpg')) {
|
||||
// already converted!
|
||||
continue;
|
||||
}
|
||||
|
||||
$groupdir = "$CFG->dataroot/groups/$group->id";
|
||||
if (!file_exists("$groupdir/f1.jpg") or !file_exists("$groupdir/f2.jpg")) {
|
||||
// no image found, sorry
|
||||
$group->picture = 0;
|
||||
$DB->update_record('groups', $group);
|
||||
continue;
|
||||
}
|
||||
|
||||
$icon['contextid'] = $context->id;
|
||||
$icon['itemid'] = $group->id;
|
||||
$icon['filename'] = 'f1.jpg';
|
||||
$fs->create_file_from_pathname($icon, "$groupdir/f1.jpg");
|
||||
$icon['filename'] = 'f2.jpg';
|
||||
$fs->create_file_from_pathname($icon, "$groupdir/f2.jpg");
|
||||
}
|
||||
$rs->close();
|
||||
|
||||
// purge all old group image dirs
|
||||
remove_dir("$CFG->dataroot/groups");
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal function - do not use directly
|
||||
*/
|
||||
|
||||
+1
-3
@@ -2214,9 +2214,7 @@ function print_group_picture($group, $courseid, $large=false, $return=false, $li
|
||||
$file = 'f2';
|
||||
}
|
||||
|
||||
// Print custom group picture
|
||||
require_once($CFG->libdir.'/filelib.php');
|
||||
$grouppictureurl = get_file_url($group->id.'/'.$file.'.jpg', null, 'usergroup');
|
||||
$grouppictureurl = moodle_url::make_pluginfile_url($context->id, 'group', 'icon', $group->id, '/', $file);
|
||||
$output .= '<img class="grouppicture" src="'.$grouppictureurl.'"'.
|
||||
' alt="'.s(get_string('group').' '.$group->name).'" title="'.s($group->name).'"/>';
|
||||
|
||||
|
||||
+14
-4
@@ -524,10 +524,7 @@ if ($component === 'blog') {
|
||||
send_file_not_found();
|
||||
}
|
||||
|
||||
if ($filearea === 'description' or $filearea === 'icon') {
|
||||
|
||||
//TODO: implement group image storage in file pool
|
||||
|
||||
if ($filearea === 'description') {
|
||||
$filename = array_pop($args);
|
||||
$filepath = $args ? '/'.implode('/', $args).'/' : '/';
|
||||
if (!$file = $fs->get_file($context->id, 'group', 'description', $group->id, $filepath, $filename) or $file->is_directory()) {
|
||||
@@ -537,6 +534,19 @@ if ($component === 'blog') {
|
||||
session_get_instance()->write_close(); // unlock session during fileserving
|
||||
send_stored_file($file, 60*60, 0, $forcedownload);
|
||||
|
||||
} else if ($filearea === 'icon') {
|
||||
$filename = array_pop($args);
|
||||
|
||||
if ($filename !== 'f1' and $filename !== 'f2') {
|
||||
send_file_not_found();
|
||||
}
|
||||
if (!$file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename.'.jpg')) {
|
||||
send_file_not_found();
|
||||
}
|
||||
|
||||
session_get_instance()->write_close(); // unlock session during fileserving
|
||||
send_stored_file($file, 60*60);
|
||||
|
||||
} else {
|
||||
send_file_not_found();
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ function useredit_update_user_preference($usernew) {
|
||||
|
||||
function useredit_update_picture(&$usernew, $userform) {
|
||||
global $CFG, $DB;
|
||||
require_once("$CFG->libdir/gdlib.php");
|
||||
|
||||
$fs = get_file_storage();
|
||||
$context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST);
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
// This is compared against the values stored in the database to determine
|
||||
// whether upgrades should be performed (see lib/db/*.php)
|
||||
|
||||
$version = 2010071100; // YYYYMMDD = date of the last version bump
|
||||
$version = 2010071101; // YYYYMMDD = date of the last version bump
|
||||
// XX = daily increments
|
||||
|
||||
$release = '2.0 Preview 4+ (Build: 20100711)'; // Human-friendly version name
|
||||
|
||||
Reference in New Issue
Block a user