mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 08:26:37 +02:00
MDL-14232 group import moved to group UI, fixed regressions and improved coding style - in future we should definitely improve the navigation structure of groups interface
This commit is contained in:
parent
19eb73cdf3
commit
81ed4632fc
4 changed files with 74 additions and 137 deletions
|
@ -1,43 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
// This file is part of Moodle - http://moodle.org/
|
|
||||||
//
|
|
||||||
// Moodle is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// Moodle is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This file is part of the User section Moodle
|
|
||||||
*
|
|
||||||
* @copyright 1999 Martin Dougiamas http://dougiamas.com
|
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
||||||
* @package course
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (!defined('MOODLE_INTERNAL')) {
|
|
||||||
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
|
||||||
}
|
|
||||||
|
|
||||||
$streditmyprofile = get_string("editmyprofile");
|
|
||||||
$stradministration = get_string("administration");
|
|
||||||
$strchoose = get_string("choose");
|
|
||||||
$struser = get_string("user");
|
|
||||||
$strusers = get_string("users");
|
|
||||||
$strusersnew = get_string("usersnew");
|
|
||||||
$strimportgroups = get_string("importgroups");
|
|
||||||
|
|
||||||
echo $OUTPUT->heading($strimportgroups);
|
|
||||||
|
|
||||||
// use formslib
|
|
||||||
include_once('import_form.php');
|
|
||||||
$mform_post = new course_import_groups_form($CFG->wwwroot.'/course/import/groups/index.php?id='.$id);
|
|
||||||
$mform_post ->display();
|
|
147
course/import/groups/index.php → group/import.php
Executable file → Normal file
147
course/import/groups/index.php → group/import.php
Executable file → Normal file
|
@ -23,33 +23,23 @@
|
||||||
* @package course
|
* @package course
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once('../../../config.php');
|
require_once('../config.php');
|
||||||
require_once($CFG->dirroot.'/course/lib.php');
|
require_once($CFG->dirroot.'/course/lib.php');
|
||||||
require_once($CFG->dirroot.'/group/lib.php');
|
require_once($CFG->dirroot.'/group/lib.php');
|
||||||
include_once('import_form.php');
|
include_once('import_form.php');
|
||||||
|
|
||||||
$id = required_param('id', PARAM_INT); // Course id
|
$id = required_param('id', PARAM_INT); // Course id
|
||||||
|
|
||||||
$PAGE->set_url('/course/import/groups/index.php', array('id'=>$id));
|
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
|
||||||
|
|
||||||
if (!$course = $DB->get_record('course', array('id'=>$id))) {
|
$PAGE->set_url('/group/import.php', array('id'=>$id));
|
||||||
print_error('invalidcourseid');
|
|
||||||
}
|
|
||||||
|
|
||||||
require_login($course->id);
|
require_login($course);
|
||||||
$context = get_context_instance(CONTEXT_COURSE, $id);
|
$context = get_context_instance(CONTEXT_COURSE, $id);
|
||||||
|
|
||||||
if (!has_capability('moodle/course:managegroups', $context)) {
|
require_capability('moodle/course:managegroups', $context);
|
||||||
print_error('nopermissiontomanagegroup');
|
|
||||||
}
|
|
||||||
|
|
||||||
$stradministration = get_string("administration");
|
|
||||||
$strimportgroups = get_string("importgroups");
|
$strimportgroups = get_string("importgroups");
|
||||||
$streditmyprofile = get_string("editmyprofile");
|
|
||||||
$strchoose = get_string("choose");
|
|
||||||
$struser = get_string("user");
|
|
||||||
$strusers = get_string("users");
|
|
||||||
$strusersnew = get_string("usersnew");
|
|
||||||
|
|
||||||
/// Print the header
|
/// Print the header
|
||||||
$PAGE->navbar->add($course->shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
|
$PAGE->navbar->add($course->shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
|
||||||
|
@ -58,18 +48,18 @@ $PAGE->navbar->add($strimportgroups);
|
||||||
|
|
||||||
$PAGE->set_title("$course->shortname: $strimportgroups");
|
$PAGE->set_title("$course->shortname: $strimportgroups");
|
||||||
$PAGE->set_heading($course->fullname);
|
$PAGE->set_heading($course->fullname);
|
||||||
echo $OUTPUT->header();
|
|
||||||
|
|
||||||
$mform_post = new course_import_groups_form($CFG->wwwroot.'/course/import/groups/index.php?id='.$id);
|
$returnurl = new moodle_url('/group/index.php', array('id'=>$id));
|
||||||
|
|
||||||
|
$mform_post = new groups_import_form(null, array('id'=>$id));
|
||||||
|
|
||||||
// If a file has been uploaded, then process it
|
// If a file has been uploaded, then process it
|
||||||
if (!$mform_post->get_data()) {
|
if ($mform_post->is_cancelled()) {
|
||||||
echo $OUTPUT->heading($strimportgroups);
|
redirect($returnurl);
|
||||||
/// Print the form
|
|
||||||
$mform_post ->display();
|
} else if ($mform_post->get_data()) {
|
||||||
echo $OUTPUT->footer();
|
echo $OUTPUT->header();
|
||||||
die;
|
|
||||||
} else {
|
|
||||||
$csv_encode = '/\&\#44/';
|
$csv_encode = '/\&\#44/';
|
||||||
if (isset($CFG->CSV_DELIMITER)) {
|
if (isset($CFG->CSV_DELIMITER)) {
|
||||||
$csv_delimiter = $CFG->CSV_DELIMITER;
|
$csv_delimiter = $CFG->CSV_DELIMITER;
|
||||||
|
@ -81,17 +71,11 @@ die;
|
||||||
$csv_delimiter = ",";
|
$csv_delimiter = ",";
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare temp file
|
|
||||||
$filename = $CFG->dataroot . '/temp/groupimport/importedfile_'.time().'.csv';
|
|
||||||
make_upload_directory('temp/groupimport');
|
|
||||||
//Fix mac/dos newlines
|
|
||||||
$text = $mform_post->get_file_content('userfile');
|
$text = $mform_post->get_file_content('userfile');
|
||||||
$text = preg_replace('!\r\n?!',"\n",$text);
|
$text = preg_replace('!\r\n?!',"\n",$text);
|
||||||
$fp = fopen($filename, "w");
|
|
||||||
fwrite($fp,$text);
|
|
||||||
fclose($fp);
|
|
||||||
|
|
||||||
$fp = fopen($filename, "r");
|
$rawlines = explode("\n", $text);
|
||||||
|
unset($text);
|
||||||
|
|
||||||
// make arrays of valid fields for error checking
|
// make arrays of valid fields for error checking
|
||||||
$required = array("groupname" => 1);
|
$required = array("groupname" => 1);
|
||||||
|
@ -99,34 +83,29 @@ die;
|
||||||
$optional = array("coursename" => 1,
|
$optional = array("coursename" => 1,
|
||||||
"idnumber" => 1,
|
"idnumber" => 1,
|
||||||
"description" => 1,
|
"description" => 1,
|
||||||
"enrolmentkey" => 1,
|
"enrolmentkey" => 1);
|
||||||
"theme" => 1,
|
|
||||||
"picture" => 1,
|
|
||||||
"hidepicture" => 1);
|
|
||||||
|
|
||||||
// --- get header (field names) ---
|
// --- get header (field names) ---
|
||||||
$header = explode($csv_delimiter, fgets($fp,1024));
|
$header = explode($csv_delimiter, array_shift($rawlines));
|
||||||
// check for valid field names
|
// check for valid field names
|
||||||
foreach ($header as $i => $h) {
|
foreach ($header as $i => $h) {
|
||||||
$h = trim($h); $header[$i] = $h; // remove whitespace
|
$h = trim($h); $header[$i] = $h; // remove whitespace
|
||||||
if ( !(isset($required[$h]) or
|
if (!(isset($required[$h]) or isset($optionalDefaults[$h]) or isset($optional[$h]))) {
|
||||||
isset($optionalDefaults[$h]) or
|
print_error('invalidfieldname', 'error', 'import.php?id='.$id, $h);
|
||||||
isset($optional[$h])) ) {
|
|
||||||
print_error('invalidfieldname', 'error', 'index.php?id='.$id.'&sesskey='.sesskey(), $h);
|
|
||||||
}
|
}
|
||||||
if ( isset($required[$h]) ) {
|
if (isset($required[$h])) {
|
||||||
$required[$h] = 2;
|
$required[$h] = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check for required fields
|
// check for required fields
|
||||||
foreach ($required as $key => $value) {
|
foreach ($required as $key => $value) {
|
||||||
if ($value < 2) {
|
if ($value < 2) {
|
||||||
print_error('fieldrequired', 'error', 'uploaduser.php?id='.$id.'&sesskey='.sesskey(), $key);
|
print_error('fieldrequired', 'error', 'import.php?id='.$id, $key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$linenum = 2; // since header is line 1
|
$linenum = 2; // since header is line 1
|
||||||
|
|
||||||
while (!feof ($fp)) {
|
foreach ($rawlines as $rawline) {
|
||||||
|
|
||||||
$newgroup = new stdClass();//to make Martin happy
|
$newgroup = new stdClass();//to make Martin happy
|
||||||
foreach ($optionalDefaults as $key => $value) {
|
foreach ($optionalDefaults as $key => $value) {
|
||||||
|
@ -134,10 +113,10 @@ die;
|
||||||
}
|
}
|
||||||
//Note: commas within a field should be encoded as , (for comma separated csv files)
|
//Note: commas within a field should be encoded as , (for comma separated csv files)
|
||||||
//Note: semicolon within a field should be encoded as ; (for semicolon separated csv files)
|
//Note: semicolon within a field should be encoded as ; (for semicolon separated csv files)
|
||||||
$line = explode($csv_delimiter, fgets($fp,1024));
|
$line = explode($csv_delimiter, $rawline);
|
||||||
foreach ($line as $key => $value) {
|
foreach ($line as $key => $value) {
|
||||||
//decode encoded commas
|
//decode encoded commas
|
||||||
$record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter,trim($value));
|
$record[$header[$key]] = preg_replace($csv_encode, $csv_delimiter, trim($value));
|
||||||
}
|
}
|
||||||
if ($record[$header[0]]) {
|
if ($record[$header[0]]) {
|
||||||
// add a new group to the database
|
// add a new group to the database
|
||||||
|
@ -146,77 +125,69 @@ die;
|
||||||
foreach ($record as $name => $value) {
|
foreach ($record as $name => $value) {
|
||||||
// check for required values
|
// check for required values
|
||||||
if (isset($required[$name]) and !$value) {
|
if (isset($required[$name]) and !$value) {
|
||||||
print_error('missingfield', 'error', 'uploaduser.php?sesskey='.sesskey(), $name);
|
print_error('missingfield', 'error', 'import.php?id='.$id, $name);
|
||||||
}
|
} else if ($name == "groupname") {
|
||||||
else if ($name == "groupname") {
|
|
||||||
$newgroup->name = $value;
|
$newgroup->name = $value;
|
||||||
}
|
} else {
|
||||||
// normal entry
|
// normal entry
|
||||||
else {
|
|
||||||
$newgroup->{$name} = $value;
|
$newgroup->{$name} = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
///Find the courseid of the course with the given shortname
|
|
||||||
|
|
||||||
//if idnumber is set, we use that.
|
|
||||||
//unset invalid courseid
|
|
||||||
if (isset($newgroup->idnumber)){
|
if (isset($newgroup->idnumber)){
|
||||||
|
//if idnumber is set, we use that.
|
||||||
|
//unset invalid courseid
|
||||||
if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) {
|
if (!$mycourse = $DB->get_record('course', array('idnumber'=>$newgroup->idnumber))) {
|
||||||
echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
|
echo $OUTPUT->notification(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
|
||||||
unset($newgroup->courseid);//unset so 0 doesnt' get written to database
|
unset($newgroup->courseid);//unset so 0 doesn't get written to database
|
||||||
}
|
}
|
||||||
$newgroup->courseid = $mycourse->id;
|
$newgroup->courseid = $mycourse->id;
|
||||||
}
|
|
||||||
//else use course short name to look up
|
|
||||||
//unset invalid coursename (if no id)
|
|
||||||
|
|
||||||
else if (isset($newgroup->coursename)){
|
} else if (isset($newgroup->coursename)){
|
||||||
|
//else use course short name to look up
|
||||||
|
//unset invalid coursename (if no id)
|
||||||
if (!$mycourse = $DB->get_record('course', array('shortname', $newgroup->coursename))) {
|
if (!$mycourse = $DB->get_record('course', array('shortname', $newgroup->coursename))) {
|
||||||
echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename));
|
echo $OUTPUT->notification(get_string('unknowncourse', 'error', $newgroup->coursename));
|
||||||
unset($newgroup->courseid);//unset so 0 doesnt' get written to database
|
unset($newgroup->courseid);//unset so 0 doesn't get written to database
|
||||||
}
|
}
|
||||||
$newgroup->courseid = $mycourse->id;
|
$newgroup->courseid = $mycourse->id;
|
||||||
}
|
|
||||||
//else use use current id
|
} else {
|
||||||
else{
|
//else use use current id
|
||||||
$newgroup->courseid = $id;
|
$newgroup->courseid = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if courseid is set
|
//if courseid is set
|
||||||
if (isset($newgroup->courseid)){
|
if (isset($newgroup->courseid)) {
|
||||||
|
|
||||||
$newgroup->courseid = (int)$newgroup->courseid;
|
|
||||||
$newgroup->timecreated = time();
|
|
||||||
$linenum++;
|
$linenum++;
|
||||||
$groupname = $newgroup->name;
|
$groupname = $newgroup->name;
|
||||||
$newgrpcoursecontext = get_context_instance(CONTEXT_COURSE, $newgroup->courseid);
|
$newgrpcoursecontext = get_context_instance(CONTEXT_COURSE, $newgroup->courseid);
|
||||||
|
|
||||||
///Users cannot upload groups in courses they cannot update.
|
///Users cannot upload groups in courses they cannot update.
|
||||||
if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext)){
|
if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext) or (!is_enrolled($newgrpcoursecontext) and !has_capability('moodle/course:view', $newgrpcoursecontext))) {
|
||||||
echo $OUTPUT->notification(get_string('nopermissionforcreation','group',$groupname));
|
echo $OUTPUT->notification(get_string('nopermissionforcreation', 'group', $groupname));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if ( $groupid = groups_get_group_by_name($newgroup->courseid, $groupname) || !($newgroup->id = groups_create_group($newgroup)) ) {
|
if ($groupid = groups_get_group_by_name($newgroup->courseid, $groupname)) {
|
||||||
|
echo $OUTPUT->notification("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
|
||||||
//Record not added - probably because group is already registered
|
} else if (groups_create_group($newgroup)) {
|
||||||
//In this case, output groupname from previous registration
|
echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname), 'notifysuccess');
|
||||||
if ($groupid) {
|
} else {
|
||||||
echo $OUTPUT->notification("$groupname :".get_string('groupexistforcourse', 'error', $groupname));
|
echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname));
|
||||||
} else {
|
|
||||||
echo $OUTPUT->notification(get_string('groupnotaddederror', 'error', $groupname));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
echo $OUTPUT->notification(get_string('groupaddedsuccesfully', 'group', $groupname));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} //close courseid validity check
|
}
|
||||||
unset ($newgroup);
|
unset ($newgroup);
|
||||||
}//close if ($record[$header[0]])
|
}
|
||||||
}//close while($fp)
|
}
|
||||||
fclose($fp);
|
|
||||||
// remove temp file
|
|
||||||
unlink($filename);
|
|
||||||
|
|
||||||
echo '<hr />';
|
echo $OUTPUT->single_button($returnurl, get_string('continue'), 'get');
|
||||||
|
echo $OUTPUT->footer();
|
||||||
|
die;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Print the form
|
||||||
|
echo $OUTPUT->header();
|
||||||
|
echo $OUTPUT->heading($strimportgroups);
|
||||||
|
$mform_post ->display();
|
||||||
|
echo $OUTPUT->footer();
|
14
course/import/groups/import_form.php → group/import_form.php
Executable file → Normal file
14
course/import/groups/import_form.php → group/import_form.php
Executable file → Normal file
|
@ -6,13 +6,11 @@ if (!defined('MOODLE_INTERNAL')) {
|
||||||
|
|
||||||
require_once($CFG->libdir.'/formslib.php');
|
require_once($CFG->libdir.'/formslib.php');
|
||||||
|
|
||||||
class course_import_groups_form extends moodleform {
|
class groups_import_form extends moodleform {
|
||||||
|
|
||||||
function definition() {
|
function definition() {
|
||||||
global $CFG, $USER;
|
|
||||||
$mform =& $this->_form;
|
$mform =& $this->_form;
|
||||||
$strimportgroups = get_string('importgroups');
|
$data = $this->_customdata;
|
||||||
$maxsize = get_max_upload_file_size();
|
|
||||||
|
|
||||||
//fill in the data depending on page params
|
//fill in the data depending on page params
|
||||||
//later using set_data
|
//later using set_data
|
||||||
|
@ -20,10 +18,14 @@ class course_import_groups_form extends moodleform {
|
||||||
|
|
||||||
$filepickeroptions = array();
|
$filepickeroptions = array();
|
||||||
$filepickeroptions['filetypes'] = '*';
|
$filepickeroptions['filetypes'] = '*';
|
||||||
$filepickeroptions['maxbytes'] = $maxsize;
|
$filepickeroptions['maxbytes'] = get_max_upload_file_size();
|
||||||
$mform->addElement('filepicker', 'userfile', get_string('import'), null, $filepickeroptions);
|
$mform->addElement('filepicker', 'userfile', get_string('import'), null, $filepickeroptions);
|
||||||
|
|
||||||
$this->add_action_buttons(false, $strimportgroups);
|
$mform->addElement('hidden', 'id');
|
||||||
|
|
||||||
|
$this->add_action_buttons(true, get_string('importgroups'));
|
||||||
|
|
||||||
|
$this->set_data($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,6 +100,10 @@ switch ($action) {
|
||||||
redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
|
redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'showimportgroups':
|
||||||
|
redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
|
||||||
|
break;
|
||||||
|
|
||||||
case 'showgroupsettingsform':
|
case 'showgroupsettingsform':
|
||||||
redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
|
redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
|
||||||
break;
|
break;
|
||||||
|
@ -210,6 +214,9 @@ echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreat
|
||||||
echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
|
echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
|
||||||
. get_string('autocreategroups', 'group') . '" /></p>'."\n";
|
. get_string('autocreategroups', 'group') . '" /></p>'."\n";
|
||||||
|
|
||||||
|
echo '<p><input type="submit" name="act_showimportgroups" id="showimportgroups" value="'
|
||||||
|
. get_string('importgroups') . '" /></p>'."\n";
|
||||||
|
|
||||||
echo '</td>'."\n";
|
echo '</td>'."\n";
|
||||||
echo '<td>'."\n";
|
echo '<td>'."\n";
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue