[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/question/ -> import.php (source)

   1  <?php // $Id: import.php,v 1.46.2.4 2008/09/15 14:21:02 thepurpleblob Exp $
   2  /**
   3   * Import quiz questions into the given category
   4   *
   5   * @author Martin Dougiamas, Howard Miller, and many others.
   6   *         {@link http://moodle.org}
   7   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   8   * @package questionbank
   9   * @subpackage importexport
  10   */
  11  
  12      require_once("../config.php");
  13      require_once ("editlib.php");
  14      require_once($CFG->libdir . '/uploadlib.php');
  15      require_once($CFG->libdir . '/questionlib.php');
  16      require_once ("import_form.php");
  17  
  18      list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('import', false, false);
  19  
  20     // get display strings
  21      $txt = new stdClass();
  22      $txt->importquestions = get_string("importquestions", "quiz");
  23  
  24      list($catid, $catcontext) = explode(',', $pagevars['cat']);
  25      if (!$category = get_record("question_categories", "id", $catid)) {
  26          print_error('nocategory','quiz');
  27      }
  28  
  29      //this page can be called without courseid or cmid in which case
  30      //we get the context from the category object.
  31      if ($contexts === null) { // need to get the course from the chosen category
  32          $contexts = new question_edit_contexts(get_context_instance_by_id($category->contextid));
  33          $thiscontext = $contexts->lowest();
  34          if ($thiscontext->contextlevel == CONTEXT_COURSE){
  35              require_login($thiscontext->instanceid, false);
  36          } elseif ($thiscontext->contextlevel == CONTEXT_MODULE){
  37              list($module, $cm) = get_module_from_cmid($thiscontext->instanceid);
  38              require_login($cm->course, false, $cm);
  39          }
  40          $contexts->require_one_edit_tab_cap($edittab);
  41      }
  42  
  43      // ensure the files area exists for this course
  44      make_upload_directory("$COURSE->id");
  45  
  46      $import_form = new question_import_form($thispageurl, array('contexts'=>$contexts->having_one_edit_tab_cap('import'),
  47                                                          'defaultcategory'=>$pagevars['cat']));
  48  
  49      if ($import_form->is_cancelled()){
  50          redirect($thispageurl);
  51      }
  52      //==========
  53      // PAGE HEADER
  54      //==========
  55  
  56      if ($cm!==null) {
  57          $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
  58              ? update_module_button($cm->id, $COURSE->id, get_string('modulename', $cm->modname))
  59              : "";
  60          $navlinks = array();
  61          $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id", 'type' => 'activity');
  62          $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
  63          $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
  64          $navigation = build_navigation($navlinks);
  65          print_header_simple($txt->importquestions, '', $navigation, "", "", true, $strupdatemodule);
  66  
  67          $currenttab = 'edit';
  68          $mode = 'import';
  69          ${$cm->modname} = $module;
  70          include($CFG->dirroot."/mod/$cm->modname/tabs.php");
  71      } else {
  72          // Print basic page layout.
  73          $navlinks = array();
  74          $navlinks[] = array('name' => $txt->importquestions, 'link' => '', 'type' => 'title');
  75          $navigation = build_navigation($navlinks);
  76  
  77          print_header_simple($txt->importquestions, '', $navigation);
  78          // print tabs
  79          $currenttab = 'import';
  80          include ('tabs.php');
  81      }
  82  
  83  
  84      // file upload form sumitted
  85      if ($form = $import_form->get_data()) {
  86  
  87          // file checks out ok
  88          $fileisgood = false;
  89  
  90          // work out if this is an uploaded file
  91          // or one from the filesarea.
  92          if (!empty($form->choosefile)) {
  93              $importfile = "{$CFG->dataroot}/{$COURSE->id}/{$form->choosefile}";
  94              $realfilename = $form->choosefile;
  95              if (file_exists($importfile)) {
  96                  $fileisgood = true;
  97              } else {
  98                  print_error('uploadproblem', 'moodle', $form->choosefile);
  99              }
 100          } else {
 101              // must be upload file
 102              $realfilename = $import_form->get_importfile_realname();
 103              if (!$importfile = $import_form->get_importfile_name()) {
 104                  print_error('uploadproblem', 'moodle');
 105              }else {
 106                  $fileisgood = true;
 107              }
 108          }
 109  
 110          // process if we are happy file is ok
 111          if ($fileisgood) {
 112  
 113              if (! is_readable("format/$form->format/format.php")) {
 114                  print_error('formatnotfound','quiz', $form->format);
 115              }
 116  
 117              require_once ("format.php");  // Parent class
 118              require_once("format/$form->format/format.php");
 119  
 120              $classname = "qformat_$form->format";
 121              $qformat = new $classname();
 122  
 123              // load data into class
 124              $qformat->setCategory($category);
 125              $qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
 126              $qformat->setCourse($COURSE);
 127              $qformat->setFilename($importfile);
 128              $qformat->setRealfilename($realfilename);
 129              $qformat->setMatchgrades($form->matchgrades);
 130              $qformat->setCatfromfile(!empty($form->catfromfile));
 131              $qformat->setContextfromfile(!empty($form->contextfromfile));
 132              $qformat->setStoponerror($form->stoponerror);
 133  
 134              // Do anything before that we need to
 135              if (! $qformat->importpreprocess()) {
 136                  print_error('importerror', 'quiz', $thispageurl->out());
 137              }
 138  
 139              // Process the uploaded file
 140              if (! $qformat->importprocess()) {
 141                  print_error('importerror', 'quiz', $thispageurl->out());
 142              }
 143  
 144              // In case anything needs to be done after
 145              if (! $qformat->importpostprocess()) {
 146                  print_error('importerror', 'quiz', $thispageurl->out());
 147              }
 148  
 149              echo "<hr />";
 150              print_continue("edit.php?".($thispageurl->get_query_string(array('category'=>"{$qformat->category->id},{$qformat->category->contextid}"))));
 151              print_footer($COURSE);
 152              exit;
 153          }
 154      }
 155  
 156      print_heading_with_help($txt->importquestions, "import", "quiz");
 157  
 158      /// Print upload form
 159      $import_form->display();
 160      print_footer($COURSE);
 161  
 162  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7