Skip to content

Commit

Permalink
MDL-23202 group icons now stored in file storage pool
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jul 11, 2010
1 parent c7744f1 commit e88dd87
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 61 deletions.
8 changes: 4 additions & 4 deletions group/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
$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) {
Expand Down Expand Up @@ -100,10 +100,10 @@
} 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);
Expand Down
8 changes: 2 additions & 6 deletions group/group_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -37,9 +35,7 @@ function definition () {
$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);

Expand Down
103 changes: 63 additions & 40 deletions group/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -194,36 +191,64 @@ function groups_create_grouping($data, $editoroptions=null) {
return $id;
}

/**
* Update the group icon from form data
* @param $group
* @param $data
* @param $editform
*/
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 $um upload manager with group picture
* @param object $editform
* @param array $editoroptions
* @return boolean true or exception
*/
function groups_update_group($data, $editform=false) {
function groups_update_group($data, $editform = false, $editoroptions = false) {
global $CFG, $DB;
require_once("$CFG->libdir/gdlib.php");

$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;
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions group/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
$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
Expand Down Expand Up @@ -158,7 +158,7 @@
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;
Expand Down
6 changes: 6 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4735,6 +4735,12 @@ function xmldb_main_upgrade($oldversion) {
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;
}
Expand Down
50 changes: 49 additions & 1 deletion lib/db/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 1 addition & 3 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).'"/>';

Expand Down
18 changes: 14 additions & 4 deletions pluginfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,7 @@
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()) {
Expand All @@ -537,6 +534,19 @@
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();
}
Expand Down
1 change: 1 addition & 0 deletions user/editlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e88dd87

Please sign in to comment.