How to debug an external function!?

How to debug an external function!?

by Christian Pfisterer -
Number of replies: 1

Hi,

I've successfully added 6 new external functions but one of them is not working.

Before creating an external function I always create a local script (CLI) where I try to figure out if the code works. If it does I implement it into an external function.

I've created one function which restores a backup file into an existing course. This works fine in the local script but when I try to access the external function all I get is an "unknown error".

Is there any way how you can debug web services not only on the client but also on the server side????

here is my code:

/**
*
* Restores course content into existing course
* @param int $courseid
* @param string $shortname
* @throws Exception
*/
public static function create_course_from_template($courseid, $shortname) {

global $CFG,$USER;
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
require_once($CFG->libdir . '/filestorage/zip_packer.php');

//validate parameter
$params = self::validate_parameters(self::create_course_from_template_parameters(),
array('courseid'=>$courseid,
'shortname'=>$shortname)
);

//Shortname is alway YY/CW PRODUCT_SHORT LANGUAGE
//example 09/42 LPD DE
//template files follow this standard: template_product_language.zip
//example template_lpd_en.zip
$trainingname = explode(" ", strtolower($fullname));
$filename = 'template_' . $trainingname[1] . '_' .$trainingname[2] . '.mbz';

$archivefile = $CFG->dataroot . '/course_templates/' . $filename;

if (!file_exists($archivefile)) {
throw new Exception("Datei gibt's nicht");
}

//create tempfolder to extract zip file into that folder
$tempfoldername = restore_controller::get_tempdir_name($courseid, $USER->id);
$tempfolderpath = $CFG->dataroot . '/temp/backup/' .$tempfoldername;

//now extract zip file into the temp folder
$archive = zip_packer::extract_to_pathname($archivefile, $tempfolderpath);

//Check if unzipping worked out ok
if(!$archive) {
throw new Exception($archive);
}

//Restore the course into a new course
$rc = new restore_controller($tempfoldername, $courseid, backup::INTERACTIVE_NO,
backup::MODE_GENERAL, $USER->id, backup::TARGET_NEW_COURSE);
//print_object($rc); // DONOTCOMMIT

if($rc->execute_precheck()) {
$rc->execute_plan();
}
}

Average of ratings: -
In reply to Christian Pfisterer

Re: How to debug an external function!?

by Jérôme Mouneyrac -

Hi Christian,

instead to throw new Exception(), throw new moodle_exception().

moodle_exception are recognized by our Zend SOAP server if ever it is the one you are using (see MDL-23555)

Otherwise to debug on the server I print into apache error log (error_log('Adding hub info'); error_log(print_r($hubinfo, true));)

Regards,

Jerome