Skip to content

Commit

Permalink
MDL-20636 Finish off converting question import.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Feb 21, 2011
1 parent ba64384 commit f7c1dfa
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 144 deletions.
4 changes: 4 additions & 0 deletions lang/en/question.php
Expand Up @@ -125,6 +125,7 @@
$string['exportquestions_link'] = 'question/export';
$string['filecantmovefrom'] = 'The questions files cannot be moved because you do not have permission to remove files from the place you are trying to move questions from.';
$string['filecantmoveto'] = 'The question files cannot be moved or copied becuase you do not have permission to add files to the place you are trying to move the questions to.';
$string['fileformat'] = 'File format';
$string['filesareacourse'] = 'the course files area';
$string['filesareasite'] = 'the site files area';
$string['filestomove'] = 'Move / copy files to {$a}?';
Expand All @@ -145,6 +146,7 @@
$string['importquestions'] = 'Import questions from file';
$string['importquestions_help'] = 'This function enables questions in a variety of formats to be imported via text file. Note that the file must use UTF-8 encoding.';
$string['importquestions_link'] = 'question/import';
$string['importwrongfiletype'] = 'The type of the file you selected ({$a->actualtype}) does not match the type expected by this import format ({$a->expectedtype}).';
$string['invalidarg'] = 'No valid arguments supplied or incorrect server configuration';
$string['invalidcategoryidforparent'] = 'Invalid category id for parent!';
$string['invalidcategoryidtomove'] = 'Invalid category id to move!';
Expand All @@ -156,6 +158,8 @@
$string['makechildof'] = 'Make child of \'{$a}\'';
$string['maketoplevelitem'] = 'Move to top level';
$string['matchgrades'] = 'Match grades';
$string['matchgradeserror'] = 'Error if grade not listed';
$string['matchgradesnearest'] = 'Nearest grade if not listed';
$string['matchgrades_help'] = 'Imported grades must match one of the fixed list of valid grades - 100, 90, 80, 75, 70, 66.666, 60, 50, 40, 33.333, 30, 25, 20, 16.666, 14.2857, 12.5, 11.111, 10, 5, 0 (also negative values). If not, there are two options:
* Error if grade not listed - If a question contains any grades not found in the list an error is displayed and that question will not be imported
Expand Down
16 changes: 8 additions & 8 deletions lib/questionlib.php
Expand Up @@ -1255,12 +1255,12 @@ function question_categorylist($categoryid) {
* @return array sorted list of import/export formats available
*/
function get_import_export_formats($type) {

global $CFG;

$fileformats = get_plugin_list('qformat');

$fileformatname=array();
require_once( "{$CFG->dirroot}/question/format.php" );
$fileformatname = array();
require_once($CFG->dirroot . '/question/format.php');
foreach ($fileformats as $fileformat => $fdir) {
$formatfile = $fdir . '/format.php';
if (is_readable($formatfile)) {
Expand All @@ -1269,20 +1269,20 @@ function get_import_export_formats($type) {
continue;
}

$classname = "qformat_$fileformat";
$classname = 'qformat_' . $fileformat;
$formatclass = new $classname();
if ($type == 'import') {
$provided = $format_class->provide_import();
$provided = $formatclass->provide_import();
} else {
$provided = $format_class->provide_export();
$provided = $formatclass->provide_export();
}

if ($provided) {
$fileformatnames[$fileformat] = get_string($fileformat, 'qformat_'.$fileformat);
$fileformatnames[$fileformat] = get_string($fileformat, 'qformat_' . $fileformat);
}
}
natcasesort($fileformatnames);

textlib_get_instance()->asort($fileformatnames);
return $fileformatnames;
}

Expand Down
2 changes: 1 addition & 1 deletion question/editlib.php
Expand Up @@ -26,7 +26,7 @@
*/


require_once($CFG->libdir.'/questionlib.php');
require_once($CFG->libdir . '/questionlib.php');


define('DEFAULT_QUESTIONS_PER_PAGE', 20);
Expand Down
77 changes: 53 additions & 24 deletions question/export_form.php
@@ -1,57 +1,86 @@
<?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/>.

/**
* Defines the export questions form.
*
* @package moodlecore
* @subpackage questionbank
* @copyright 2007 Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
die('Direct access to this script is forbidden.');
}

require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir . '/formslib.php');


/**
* Form to export questions from the question bank.
*
* @copyright 2007 Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_export_form extends moodleform {

function definition() {
$mform =& $this->_form;
$mform = $this->_form;

$defaultcategory = $this->_customdata['defaultcategory'];
$contexts = $this->_customdata['contexts'];
//--------------------------------------------------------------------------------
$mform->addElement('header','fileformat',get_string('fileformat','quiz'));
$defaultcategory = $this->_customdata['defaultcategory'];
$contexts = $this->_customdata['contexts'];

// Choice of format, with help.
$mform->addElement('header', 'fileformat', get_string('fileformat', 'question'));
$fileformatnames = get_import_export_formats('export');
$radioarray = array();
$i = 0 ;
foreach ($fileformatnames as $shortname => $fileformatname) {
$currentgrp1 = array();
$currentgrp1[] = &$mform->createElement('radio','format','',$fileformatname,$shortname);
$mform->addGroup($currentgrp1,"formathelp[$i]",'',array('<br />'),false);
$mform->addHelpButton("formathelp[$i]", $shortname, 'qformat_'.$shortname);
$currentgrp1[] = $mform->createElement('radio', 'format', '', $fileformatname, $shortname);
$mform->addGroup($currentgrp1, "formathelp[$i]", '', array('<br />'), false);
$mform->addHelpButton("formathelp[$i]", $shortname, 'qformat_' . $shortname);
$i++ ;
}
$mform->addRule("formathelp[0]",null,'required',null,'client');
//--------------------------------------------------------------------------------
$mform->addElement('header','general', get_string('general', 'form'));
$mform->addRule("formathelp[0]", null, 'required', null, 'client');

// Export options.
$mform->addElement('header', 'general', get_string('general', 'form'));

$mform->addElement('questioncategory', 'category', get_string('exportcategory', 'question'), compact('contexts'));
$mform->setDefault('category', $defaultcategory);
$mform->addHelpButton('category', 'exportcategory', 'question');

$categorygroup = array();
$categorygroup[] =& $mform->createElement('checkbox', 'cattofile', '', get_string('tofilecategory', 'question'));
$categorygroup[] =& $mform->createElement('checkbox', 'contexttofile', '', get_string('tofilecontext', 'question'));
$categorygroup[] = $mform->createElement('checkbox', 'cattofile', '', get_string('tofilecategory', 'question'));
$categorygroup[] = $mform->createElement('checkbox', 'contexttofile', '', get_string('tofilecontext', 'question'));
$mform->addGroup($categorygroup, 'categorygroup', '', '', false);
$mform->disabledIf('categorygroup', 'cattofile', 'notchecked');
$mform->setDefault('cattofile', 1);
$mform->setDefault('contexttofile', 1);

// $fileformatnames = get_import_export_formats('export');
// $mform->addElement('select', 'format', get_string('fileformat','quiz'), $fileformatnames);
// $mform->setDefault('format', 'gift');

// set a template for the format select elements
$renderer =& $mform->defaultRenderer();
// Set a template for the format select elements
$renderer = $mform->defaultRenderer();
$template = "{help} {element}\n";
$renderer->setGroupElementTemplate($template, 'format');

//--------------------------------------------------------------------------------
$this->add_action_buttons(false, get_string('exportquestions', 'quiz'));
//--------------------------------------------------------------------------------
// Submit buttons.
$this->add_action_buttons(false, get_string('exportquestions', 'question'));
}
}
5 changes: 3 additions & 2 deletions question/format.php
Expand Up @@ -256,7 +256,7 @@ function importprocess($category) {
set_time_limit(0);

// STAGE 1: Parse the file
echo $OUTPUT->notification(get_string('parsingquestions','quiz'));
echo $OUTPUT->notification(get_string('parsingquestions','quiz'), 'notifysuccess');

if (! $lines = $this->readdata($this->filename)) {
echo $OUTPUT->notification(get_string('cannotread','quiz'));
Expand All @@ -269,7 +269,8 @@ function importprocess($category) {
}

// STAGE 2: Write data to database
echo $OUTPUT->notification(get_string('importingquestions','quiz',$this->count_questions($questions)));
echo $OUTPUT->notification(get_string('importingquestions', 'quiz',
$this->count_questions($questions)), 'notifysuccess');

// check for errors before we continue
if ($this->stoponerror and ($this->importerrors>0)) {
Expand Down
129 changes: 70 additions & 59 deletions question/import.php
@@ -1,17 +1,34 @@
<?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/>.

/**
* Import quiz questions into the given category
* Defines the import questions form.
*
* @author Martin Dougiamas, Howard Miller, and many others.
* {@link http://moodle.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questionbank
* @subpackage importexport
* @package moodlecore
* @subpackage questionbank
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once("../config.php");
require_once("editlib.php"); // Includes lib/questionlib.php
require_once("import_form.php");

require_once(dirname(__FILE__) . '/../config.php');
require_once($CFG->dirroot . '/question/editlib.php');
require_once($CFG->dirroot . '/question/import_form.php');
require_once($CFG->dirroot . '/question/format.php');

list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
question_edit_setup('import', '/question/import.php', false, false);
Expand Down Expand Up @@ -72,59 +89,53 @@
$importfile = "{$CFG->dataroot}/temp/questionimport/{$realfilename}";
make_upload_directory('temp/questionimport');
if (!$result = $import_form->save_file('newfile', $importfile, true)) {
print_error('uploadproblem', 'moodle');
}else {
$fileisgood = true;
throw new moodle_exception('uploadproblem');
}

$formatfile = 'format/' . $form->format . '/format.php';
if (!is_readable($formatfile)) {
throw new moodle_exception('formatnotfound', 'quiz', '', $form->format);
}

// process if we are happy file is ok
if ($fileisgood) {

if (! is_readable("format/$form->format/format.php")) {
print_error('formatnotfound','quiz', $form->format);
}

require_once("format.php"); // Parent class
require_once("format/$form->format/format.php");

$classname = "qformat_$form->format";
$qformat = new $classname();

// load data into class
$qformat->setCategory($category);
$qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
$qformat->setCourse($COURSE);
$qformat->setFilename($importfile);
$qformat->setRealfilename($realfilename);
$qformat->setMatchgrades($form->matchgrades);
$qformat->setCatfromfile(!empty($form->catfromfile));
$qformat->setContextfromfile(!empty($form->contextfromfile));
$qformat->setStoponerror($form->stoponerror);

// Do anything before that we need to
if (! $qformat->importpreprocess()) {
//TODO: need more detailed error info
print_error('cannotimport', '', $thispageurl->out());
}

// Process the uploaded file
if (! $qformat->importprocess($category)) {
//TODO: need more detailed error info
print_error('cannotimport', '', $thispageurl->out());
}

// In case anything needs to be done after
if (! $qformat->importpostprocess()) {
//TODO: need more detailed error info
print_error('cannotimport', '', $thispageurl->out());
}

echo '<hr />';
$params = $thispageurl->params() + array('category'=>"{$qformat->category->id},{$qformat->category->contextid}");
echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
echo $OUTPUT->footer();
exit;
require_once($formatfile);

$classname = 'qformat_' . $form->format;
$qformat = new $classname();

// load data into class
$qformat->setCategory($category);
$qformat->setContexts($contexts->having_one_edit_tab_cap('import'));
$qformat->setCourse($COURSE);
$qformat->setFilename($importfile);
$qformat->setRealfilename($realfilename);
$qformat->setMatchgrades($form->matchgrades);
$qformat->setCatfromfile(!empty($form->catfromfile));
$qformat->setContextfromfile(!empty($form->contextfromfile));
$qformat->setStoponerror($form->stoponerror);

// Do anything before that we need to
if (!$qformat->importpreprocess()) {
//TODO: need more detailed error info
print_error('cannotimport', '', $thispageurl->out());
}

// Process the uploaded file
if (!$qformat->importprocess($category)) {
//TODO: need more detailed error info
print_error('cannotimport', '', $thispageurl->out());
}

// In case anything needs to be done after
if (!$qformat->importpostprocess()) {
//TODO: need more detailed error info
print_error('cannotimport', '', $thispageurl->out());
}

$params = $thispageurl->params() + array(
'category' => $qformat->category->id . ',' . $qformat->category->contextid);
echo $OUTPUT->continue_button(new moodle_url('edit.php', $params));
echo $OUTPUT->footer();
exit;
}

echo $OUTPUT->heading_with_help($txt->importquestions, 'importquestions', 'question');
Expand Down

0 comments on commit f7c1dfa

Please sign in to comment.