Permalink
Browse files

New plugin structure for code that imports stuff into a course

Old import.php is now import/activities and we've added a new plugin
for importing groups from an external file
  • Loading branch information...
1 parent 038d8e8 commit f8b3c0f484c624d8b534026f8533ada96a5a2777 moodler committed Nov 5, 2005
View
143 course/import.php
@@ -1,143 +1,26 @@
-<?php // preliminary page to find a course to import data from & interface with the backup/restore functionality
+<?php // $Id$
+ // Display all the interfaces for importing data into a specific course
-require_once('../config.php');
-require_once('lib.php');
-require_once('../backup/restorelib.php');
+ require_once('../config.php');
-$id = required_param('id', PARAM_INT); // course id to import TO
-$fromcourse = optional_param('fromcourse',0,PARAM_INT);
-$fromcoursesearch = optional_param('fromcoursesearch','',PARAM_CLEAN);
-$page = optional_param('page',0,PARAM_INT);
-$filename = optional_param('filename',0,PARAM_INT);
+ $id = required_param('id', PARAM_INT); // course id to import TO
- if (! ($course = get_record("course", "id", $id)) ) {
+ if (!$course = get_record('course', 'id', $id)) {
error("That's an invalid course id");
}
- if (!$site = get_site()){
- error("Couldn't get site course");
- }
-
- require_login($course->id);
-
- if (!isteacheredit($course->id)) {
- error("You need to be a teacher or an admin to use this page");
- }
-
- // if we're not a course creator , we can only import from our own courses.
- if (iscreator()) {
- $creator = true;
- }
-
- if ($from = get_record("course","id",$fromcourse)) {
- if (!isteacheredit($fromcourse)) {
- error("You need to be a course creator, or a teacher in the course you are importing data from, as well");
- }
- if (!empty($filename) && file_exists($CFG->dataroot.'/'.$filename) && !empty($SESSION->import_preferences)) {
- $restore = backup_to_restore_array($SESSION->import_preferences);
- $restore->restoreto = 1;
- $restore->course_id=$id;
- $restore->importing=1; // magic variable so we know that we're importing rather than just restoring.
-
- $SESSION->restore = $restore;
- redirect($CFG->wwwroot.'/backup/restore.php?file='.$filename.'&id='.$fromcourse.'&to='.$id);
- }
- else {
- redirect($CFG->wwwroot.'/backup/backup.php?id='.$from->id.'&to='.$course->id);
- }
- }
-
- $strimport = get_string("importdata");
-
- print_header("$course->fullname: $strimport", "$course->fullname: $strimport", "$course->shortname");
-
- print_heading(get_string("importdatafrom"));
-
- $tcourseids = '';
-
- if ($teachers = get_records_select('user_teachers', "userid = $USER->id AND editall = 1",'','id,course')) {
- foreach ($teachers as $teacher) {
- if ($teacher->course != $course->id && $teacher->course != $site->id){
- $tcourseids .= $teacher->course.',';
- }
- }
- }
-
- $taught_courses = array();
- if (!empty($tcourseids)) {
- $tcourseids = substr($tcourseids,0,-1);
- $taught_courses = get_records_list('course', 'id', $tcourseids);
- }
-
- if (!empty($creator)) {
- $cat_courses = get_courses($course->category);
- } else {
- $cat_courses = array();
- }
-
- $options = array();
- foreach ($taught_courses as $tcourse) {
- if ($tcourse->id != $course->id && $tcourse->id != $site->id){
- $options[$tcourse->id] = $tcourse->fullname;
- }
- }
-
- if (empty($options) && empty($creator)) {
- error(get_string('courseimportnotaught'),$CFG->wwwroot.'/course/view.php?id='.$course->id);
- print_footer();
- die();
- }
+ $strimport = get_string('import');
+ print_header($course->fullname.': '.$strimport, $course->fullname.': '.$strimport,
+ '<a href="view.php?id='.$course->id.'">'.$course->shortname.'</a> -> '.$strimport);
- $fm = '<form method="post" action="'.$CFG->wwwroot.'/course/import.php"><input type="hidden" name="id" value="'.$course->id.'" />';
- $submit = '<input type="submit" value="'.get_string('usethiscourse').'" /></form>';
-
- if (count($options) > 0) {
- $table->data[] = array($fm.'<b>'.get_string('coursestaught').'</b>',
- choose_from_menu($options,"fromcourse","","choose","","0",true),
- $submit);
- }
-
- unset($options);
-
- $options = array();
- foreach ($cat_courses as $ccourse) {
- if ($ccourse->id != $course->id && $ccourse->id != $site->id){
- $options[$ccourse->id] = $ccourse->fullname;
- }
- }
- $cat = get_record("course_categories","id",$course->category);
-
- if (count($options) > 0) {
- $table->data[] = array($fm.'<b>'.get_string('coursescategory').' ('.$cat->name .')</b>',
- choose_from_menu($options,"fromcourse","","choose","","0",true),
- $submit);
- }
-
- if (!empty($creator)) {
- $table->data[] = array($fm.'<b>'.get_string('searchcourses').'</b>',
- '<input type="text" name="fromcoursesearch" />',
- '<input type="submit" value="'.get_string('searchcourses').'" />');
- }
+ $directories = get_list_of_plugins('course/import');
- if (!empty($fromcoursesearch) && !empty($creator)) {
- $totalcount = 0;
- $courses = get_courses_search(explode(" ",$fromcoursesearch),"fullname ASC",$page,50,$totalcount);
- if (is_array($courses) and count($courses) > 0) {
- $table->data[] = array('<b>'.get_string('searchresults').'</b>','','');
- foreach ($courses as $scourse) {
- if ($course->id != $scourse->id) {
- $table->data[] = array('',$scourse->fullname,
- '<a href="'.$CFG->wwwroot.'/course/import.php?id='.$course->id.'&fromcourse='.$scourse->id.'">'
- .get_string('usethiscourse'));
- }
- }
- }
- else {
- $table->data[] = array('',get_string('noresults'),'');
- }
+ foreach ($directories as $directory) {
+ echo '<div class="plugin">';
+ include_once($CFG->dirroot.'/course/import/'.$directory.'/mod.html');
+ echo '</div>';
}
- print_table($table);
print_footer();
?>
View
58 course/import/activities/index.php
@@ -0,0 +1,58 @@
+<?php // preliminary page to find a course to import data from & interface with the backup/restore functionality
+
+ require_once('../../../config.php');
+ require_once('../../lib.php');
+ require_once($CFG->dirroot.'/backup/restorelib.php');
+
+ $strimportothercourses = get_string('importfromothercourses');
+ $id = required_param('id', PARAM_INT); // course id to import TO
+ $fromcourse = optional_param('fromcourse',0,PARAM_INT);
+ $fromcoursesearch = optional_param('fromcoursesearch','',PARAM_CLEAN);
+ $page = optional_param('page',0,PARAM_INT);
+ $filename = optional_param('filename',0,PARAM_PATH);
+
+ if (! ($course = get_record("course", "id", $id)) ) {
+ error("That's an invalid course id");
+ }
+
+ if (!$site = get_site()){
+ error("Couldn't get site course");
+ }
+
+ require_login($course->id);
+
+ if (!isteacheredit($course->id)) {
+ error("You need to be a teacher or an admin to use this page");
+ }
+
+ // if we're not a course creator , we can only import from our own courses.
+ if (iscreator()) {
+ $creator = true;
+ }
+
+ if ($from = get_record("course","id",$fromcourse)) {
+ if (!isteacheredit($fromcourse)) {
+ error("You need to be a course creator, or a teacher in the course you are importing data from, as well");
+ }
+ if (!empty($filename) && file_exists($CFG->dataroot.'/'.$filename) && !empty($SESSION->import_preferences)) {
+ $restore = backup_to_restore_array($SESSION->import_preferences);
+ $restore->restoreto = 1;
+ $restore->course_id=$id;
+ $restore->importing=1; // magic variable so we know that we're importing rather than just restoring.
+
+ $SESSION->restore = $restore;
+ redirect($CFG->wwwroot.'/backup/restore.php?file='.$filename.'&id='.$fromcourse.'&to='.$id);
+ }
+ else {
+ redirect($CFG->wwwroot.'/backup/backup.php?id='.$from->id.'&to='.$course->id);
+ }
+ }
+
+ print_header("$course->shortname: $strimportothercourses", "$course->fullname",
+ "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ".
+ "-> <a href=\"$CFG->wwwroot/course/import.php?id=$course->id\">".get_string('import')."</a> ".
+ "-> $strimportothercourses");
+ require_once('mod.html');
+
+ print_footer();
+?>
View
80 course/import/activities/mod.html
@@ -0,0 +1,80 @@
+<?php // $Id$
+
+ require_once($CFG->dirroot.'/course/lib.php');
+ require_once($CFG->dirroot.'/backup/restorelib.php');
+
+ // if we're not a course creator , we can only import from our own courses.
+ if (iscreator()) {
+ $creator = true;
+ }
+
+ $strimport = get_string("importdata");
+
+ $taught_courses = get_my_courses($USER->id,"visible DESC,sortorder ASC",0,1);
+ if (!empty($creator)) {
+ $cat_courses = get_courses($course->category);
+ } else {
+ $cat_courses = array();
+ }
+
+ print_heading(get_string("importactivities"));
+
+ $options = array();
+ foreach ($taught_courses as $tcourse) {
+ if ($tcourse->id != $course->id && $tcourse->id != SITEID){
+ $options[$tcourse->id] = $tcourse->fullname;
+ }
+ }
+
+ $fm = '<form method="post" action="'.$CFG->wwwroot.'/course/import/activities/index.php"><input type="hidden" name="id" value="'.$course->id.'" />';
+ $submit = '<input type="submit" value="'.get_string('usethiscourse').'" /></form>';
+
+ if (count($options) > 0) {
+ $table->data[] = array($fm.'<b>'.get_string('coursestaught').'</b>',
+ choose_from_menu($options,"fromcourse","","choose","","0",true),
+ $submit);
+ }
+
+ unset($options);
+
+ $options = array();
+ foreach ($cat_courses as $ccourse) {
+ if ($ccourse->id != $course->id && $ccourse->id != SITEID) {
+ $options[$ccourse->id] = $ccourse->fullname;
+ }
+ }
+ $cat = get_record("course_categories","id",$course->category);
+
+ if (count($options) > 0) {
+ $table->data[] = array($fm.'<b>'.get_string('coursescategory').' ('.$cat->name .')</b>',
+ choose_from_menu($options,"fromcourse","","choose","","0",true),
+ $submit);
+ }
+
+ if (!empty($creator)) {
+ $table->data[] = array($fm.'<b>'.get_string('searchcourses').'</b>',
+ '<input type="text" name="fromcoursesearch" />',
+ '<input type="submit" value="'.get_string('searchcourses').'" /></form>');
+ }
+
+ if (!empty($fromcoursesearch) && !empty($creator)) {
+ $totalcount = 0;
+ $courses = get_courses_search(explode(" ",$fromcoursesearch),"fullname ASC",$page,50,$totalcount);
+ if (is_array($courses) and count($courses) > 0) {
+ $table->data[] = array('<b>'.get_string('searchresults').'</b>','','');
+ foreach ($courses as $scourse) {
+ if ($course->id != $scourse->id) {
+ $table->data[] = array('',$scourse->fullname,
+ '<a href="'.$CFG->wwwroot.'/course/import/activities/index.php?id='.$course->id.'&fromcourse='.$scourse->id.'">'
+ .get_string('usethiscourse'));
+ }
+ }
+ }
+ else {
+ $table->data[] = array('',get_string('noresults'),'');
+ }
+ }
+
+ print_table($table);
+
+?>
View
212 course/import/groups/index.php
@@ -0,0 +1,212 @@
+<?php // $Id: uploadgroups.php, 2005/10/31 19:09:31
+
+/// Bulk group creation registration script from a comma separated file
+
+ require_once('../../../config.php');
+ require_once('../../lib.php');
+
+ $mycourseid = required_param('id'); // Course id
+
+ if (! $course = get_record('course', 'id', $mycourseid) ) {
+ error("That's an invalid course id");
+ }
+
+ require_login($course->id);
+
+ if (!isadmin() && !isteacheredit($course->id)) {
+ error("You must be an administrator to edit users this way.");
+ }
+
+ //if (!confirm_sesskey()) {
+ // error(get_string('confirmsesskeybad', 'error'));
+ //}
+
+ $strimportgroups = get_string("importgroups");
+
+ $csv_encode = '/\&\#44/';
+ if (isset($CFG->CSV_DELIMITER)) {
+ $csv_delimiter = '\\' . $CFG->CSV_DELIMITER;
+ $csv_delimiter2 = $CFG->CSV_DELIMITER;
+
+ if (isset($CFG->CSV_ENCODE)) {
+ $csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/';
+ }
+ } else {
+ $csv_delimiter = "\,";
+ $csv_delimiter2 = ",";
+ }
+
+/// Print the header
+
+ print_header("$course->shortname: $struploadgroups", "$course->fullname",
+ "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ".
+ "-> <a href=\"$CFG->wwwroot/course/import.php?id=$course->id\">".get_string('import')."</a> ".
+ "-> $strimportgroups");
+
+/// If a file has been uploaded, then process it
+
+ require_once($CFG->dirroot.'/lib/uploadlib.php');
+ $um = new upload_manager('userfile',false,false,null,false,0);
+ if ($um->preprocess_files()) {
+ $filename = $um->files['userfile']['tmp_name'];
+
+ //Fix mac/dos newlines
+ $text = my_file_get_contents($filename);
+ $text = preg_replace('!\r\n?!',"\n",$text);
+ $fp = fopen($filename, "w");
+ fwrite($fp,$text);
+ fclose($fp);
+
+ $fp = fopen($filename, "r");
+
+ // make arrays of valid fields for error checking
+ $required = array("groupname" => 1, );
+ $optionalDefaults = array("lang" => 1, );
+ $optional = array("coursename" => 1,
+ "idnumber" =>1,
+ "description" => 1,
+ "password" => 1,
+ "theme" => 1,
+ "picture" => 1,
+ "hidepicture" => 1, );
+
+ // --- get header (field names) ---
+ $header = split($csv_delimiter, fgets($fp,1024));
+ // check for valid field names
+ foreach ($header as $i => $h) {
+ $h = trim($h); $header[$i] = $h; // remove whitespace
+ if (!($required[$h] or $optionalDefaults[$h] or $optional[$h])) {
+ error(get_string('invalidfieldname', 'error', $h), 'index.php?id='.$id.'&amp;sesskey='.$USER->sesskey);
+ }
+ if ($required[$h]) {
+ $required[$h] = 2;
+ }
+ }
+ // check for required fields
+ foreach ($required as $key => $value) {
+ if ($value < 2) {
+ error(get_string('fieldrequired', 'error', $key), 'uploaduser.php?id='.$id.'&amp;sesskey='.$USER->sesskey);
+ }
+ }
+ $linenum = 2; // since header is line 1
+
+ while (!feof ($fp)) {
+
+ $newgroup = new object();//to make Martin happy
+ foreach ($optionalDefaults as $key => $value) {
+ $newgroup->$key = current_language(); //defaults to current language
+ }
+ //Note: commas within a field should be encoded as &#44 (for comma separated csv files)
+ //Note: semicolon within a field should be encoded as &#59 (for semicolon separated csv files)
+ $line = split($csv_delimiter, fgets($fp,1024));
+ foreach ($line as $key => $value) {
+ //decode encoded commas
+ $record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter2,trim($value));
+ }
+ if ($record[$header[0]]) {
+ // add a new group to the database
+
+ // add fields to object $user
+ foreach ($record as $name => $value) {
+ // check for required values
+ if ($required[$name] and !$value) {
+ error(get_string('missingfield', 'error', $name). " ".
+ get_string('erroronline', 'error', $linenum) .". ".
+ get_string('processingstops', 'error'),
+ 'uploaduser.php?sesskey='.$USER->sesskey);
+ }
+ // password needs to be encrypted
+ else if ($name == "password") {
+ $newgroup->password = md5($value);
+ }
+ else if ($name == "groupname") {
+ $newgroup->name = addslashes($value);
+ }
+ // normal entry
+ else {
+ $newgroup->{$name} = addslashes($value);
+ }
+ }
+ ///Find the courseid of the course with the given shortname
+
+ //if idnumber is set, we use that.
+ //unset invalid courseid
+ if ($newgroup->idnumber){
+ if (!$mycourse = get_record('course', 'idnumber',$newgroup->idnumber)){
+ notify(get_string('unknowncourseidnumber', 'error', $newgroup->idnumber));
+ unset($newgroup->courseid);//unset so 0 doesnt' get written to database
+ }
+ $newgroup->courseid = $mycourse->id;
+ }
+ //else use course short name to look up
+ //unset invalid coursename (if no id)
+
+ else if ($newgroup->coursename){
+ if (!$mycourse = get_record('course', 'shortname',$newgroup->coursename)){
+ notify(get_string('unknowncourse', 'error', $newgroup->coursename));
+ unset($newgroup->courseid);//unset so 0 doesnt' get written to database
+ }
+ $newgroup->courseid = $mycourse->id;
+ }
+ //else juse use current id
+ else{
+ $newgroup->courseid = $mycourseid;
+ }
+
+ //if courseid is set
+ if (isset($newgroup->courseid)){
+
+ $newgroup->timecreated = time();
+ $linenum++;
+ $groupname = $newgroup->name;
+
+ ///Teachers can not upload groups in courses they are not teaching...
+ if (!isadmin() and !isteacheredit($newgroup->courseid)){
+ notify("$newgroup->name ".get_string('notaddedto').$newgroup->coursename.get_string('notinyourcapacity'));
+ }
+
+ else {
+ if (get_record("groups","name",$groupname,"courseid",$newgroup->courseid) || !($newgroup->id = insert_record("groups", $newgroup))) {
+
+ //Record not added - probably because group is already registered
+ //In this case, output groupname from previous registration
+ if ($group = get_record("groups","name",$groupname)) {
+ notify("$newgroup->name ".get_string('groupexistforcourse', 'error', $groupname));
+ } else {
+ notify(get_string('groupnotaddederror', 'error', $groupname));
+ }
+ }
+ else {
+ notify(get_string('group')." $newgroup->name ".get_string('addedsuccessfully'));
+ }
+ }
+ } //close courseid validity check
+ unset ($newgroup);
+ }//close if ($record[$header[0]])
+ }//close while($fp)
+ fclose($fp);
+
+ echo '<hr />';
+ }
+
+/// Print the form
+ require('mod.html');
+
+ print_footer($course);
+
+function my_file_get_contents($filename, $use_include_path = 0) {
+/// Returns the file as one big long string
+
+ $data = "";
+ $file = @fopen($filename, "rb", $use_include_path);
+ if ($file) {
+ while (!feof($file)) {
+ $data .= fread($file, 1024);
+ }
+ fclose($file);
+ }
+ return $data;
+}
+
+?>
+
View
24 course/import/groups/mod.html
@@ -0,0 +1,24 @@
+<?php
+
+ $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");
+
+ print_heading_with_help($strimportgroups, 'uploadgroups');
+ $maxuploadsize = get_max_upload_file_size();
+ echo '<p align="center">';
+ print_simple_box_start('center','80%');
+ echo '<form method="post" enctype="multipart/form-data" action="'.$CFG->wwwroot.'/course/import/groups/index.php?id='.$id.'">'.
+ $strchoose.':<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxuploadsize.'">'.
+ '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'">'.
+ '<input type="file" name="userfile" size="30">'.
+ '<input type="submit" value="'.$strimportgroups.'">'.
+ '</form></br>';
+ print_simple_box_end();
+ echo '</p>';
+
+?>
View
5 lang/en/moodle.php
@@ -634,10 +634,13 @@
$string['htmlformat'] = 'Pretty HTML format';
$string['icqnumber'] = 'ICQ number';
$string['idnumber'] = 'ID number';
+$string['import'] = 'Import';
$string['importdata'] = 'Import course data';
$string['importdatafrom'] = 'Find a course to import data from:';
+$string['importactivities'] = 'Import activities from another course';
$string['importdataexported'] = 'Exported data from \'from\' course successfully.<br /> Continue to import into your \'to\' course.';
$string['importdatafinished'] = 'Import complete! Continue to your course';
+$string['importgroups'] = 'Import groups';
$string['importmetacoursenote'] = 'Use this form to add courses to your meta course (this will import the enrolments)';
$string['inactive'] = 'Inactive';
$string['include'] = 'Include';
@@ -648,7 +651,7 @@
$string['includelogentries'] = 'Include Log Entries';
$string['includemodules'] = 'Include Modules';
$string['includeneededusers'] = 'Include Needed Users';
-$string['includenoneusers'] = 'Include None Users';
+$string['includenoneusers'] = 'Include No Users';
$string['includeuserfiles'] = 'Include User Files';
$string['info'] = 'Information';
$string['institution'] = 'Institution';

0 comments on commit f8b3c0f

Please sign in to comment.