if ($cd = new component_installer('http://download.moodle.org', 'lang16',
$pack.'.zip', 'languages.md5', 'lang')) {
- $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
+ $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
switch ($status) {
- case ERROR:
+ case COMPONENT_ERROR:
if ($cd->get_error() == 'remotedownloaderror') {
$a = new object();
$a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
}
break;
- case INSTALLED:
+ case COMPONENT_INSTALLED:
$notice_ok[] = get_string('langpackinstalled','admin',$pack);
break;
- case UPTODATE:
+ case COMPONENT_UPTODATE:
break;
}
if ($cd = new component_installer('http://download.moodle.org', 'lang16',
$pack.'.zip', 'languages.md5', 'lang')) {
- $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
+ $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
switch ($status) {
- case ERROR:
+ case COMPONENT_ERROR:
if ($cd->get_error() == 'remotedownloaderror') {
$a = new stdClass();
$a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
error(get_string($cd->get_error(), 'error')); // not probable
}
break;
- case UPTODATE:
+ case COMPONENT_UPTODATE:
//Print error string or whatever you want to do
break;
- case INSTALLED:
+ case COMPONENT_INSTALLED:
$notice_ok[] = get_string('langpackupdated', 'admin', $pack);
$updated = true;
//Print/do whatever you want
// require_once($CFG->libdir.'/componentlib.class.php');
// if ($cd = new component_installer('http://download.moodle.org', 'lang16',
// 'es_utf8.zip', 'languages.md5', 'lang')) {
-// $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
+// $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
// switch ($status) {
-// case ERROR:
+// case COMPONENT_ERROR:
// if ($cd->get_error() == 'remotedownloaderror') {
// $a = new stdClass();
// $a->url = 'http://download.moodle.org/lang16/es_utf8.zip';
// error(get_string($cd->get_error(), 'error'));
// }
// break;
-// case UPTODATE:
+// case COMPONENT_UPTODATE:
// //Print error string or whatever you want to do
// break;
-// case INSTALLED:
+// case COMPONENT_INSTALLED:
// //Print/do whatever you want
// break;
// default:
//
// To check if current component needs to be updated
//
-// $status = $cd->need_upgrade(); //returns ERROR | UPTODATE | NEEDUPDATE
+// $status = $cd->need_upgrade(); //returns COMPONENT_(ERROR | UPTODATE | NEEDUPDATE)
//
// To get the 3rd field of the md5 file (optional)
//
require_once($CFG->libdir.'/filelib.php');
// Some needed constants
-define('ERROR', 0);
-define('UPTODATE', 1);
-define('NEEDUPDATE', 2);
-define('INSTALLED', 3);
+define('COMPONENT_ERROR', 0);
+define('COMPONENT_UPTODATE', 1);
+define('COMPONENT_NEEDUPDATE', 2);
+define('COMPONENT_INSTALLED', 3);
/**
* This class is used to check, download and install items from
* compare md5 values, download, unzip, install and regenerate
* local md5 file
*
- * @return int ERROR | UPTODATE | INSTALLED
+ * @return int COMPONENT_(ERROR | UPTODATE | INSTALLED)
*/
function install() {
/// Check requisites are passed
if (!$this->requisitesok) {
- return ERROR;
+ return COMPONENT_ERROR;
}
/// Confirm we need upgrade
- if ($this->need_upgrade() === ERROR) {
- return ERROR;
- } else if ($this->need_upgrade() === UPTODATE) {
+ if ($this->need_upgrade() === COMPONENT_ERROR) {
+ return COMPONENT_ERROR;
+ } else if ($this->need_upgrade() === COMPONENT_UPTODATE) {
$this->errorstring='componentisuptodate';
- return UPTODATE;
+ return COMPONENT_UPTODATE;
}
/// Create temp directory if necesary
if (!make_upload_directory('temp', false)) {
$this->errorstring='cannotcreatetempdir';
- return ERROR;
+ return COMPONENT_ERROR;
}
/// Download zip file and save it to temp
$source = $this->sourcebase.'/'.$this->zippath.'/'.$this->zipfilename;
if (!fwrite($file, $contents)) {
fclose($file);
$this->errorstring='cannotsavezipfile';
- return ERROR;
+ return COMPONENT_ERROR;
}
} else {
$this->errorstring='cannotsavezipfile';
- return ERROR;
+ return COMPONENT_ERROR;
}
fclose($file);
} else {
$this->errorstring='cannotdownloadzipfile';
- return ERROR;
+ return COMPONENT_ERROR;
}
/// Calculate its md5
$new_md5 = md5($contents);
/// Compare it with the remote md5 to check if we have the correct zip file
if (!$remote_md5 = $this->get_component_md5()) {
- return ERROR;
+ return COMPONENT_ERROR;
}
if ($new_md5 != $remote_md5) {
$this->errorstring='downloadedfilecheckfailed';
- return ERROR;
+ return COMPONENT_ERROR;
}
/// Move current revision to a safe place
$destinationdir = $CFG->dataroot.'/'.$this->destpath;
@remove_dir($destinationcomponent);
@rename ($destinationcomponent.'_old', $destinationcomponent);
$this->errorstring='cannotunzipfile';
- return ERROR;
+ return COMPONENT_ERROR;
}
/// Delete old component version
@remove_dir($destinationcomponent.'_old');
if (!fwrite($file, $new_md5)) {
fclose($file);
$this->errorstring='cannotsavemd5file';
- return ERROR;
+ return COMPONENT_ERROR;
}
} else {
$this->errorstring='cannotsavemd5file';
- return ERROR;
+ return COMPONENT_ERROR;
}
fclose($file);
/// Delete temp zip file
@unlink($zipfile);
- return INSTALLED;
+ return COMPONENT_INSTALLED;
}
/**
* This function will detect if remote component needs to be installed
* because it's different from the local one
*
- * @return int ERROR | UPTODATE | NEEDUPDATE
+ * @return int COMPONENT_(ERROR | UPTODATE | NEEDUPDATE)
*/
function need_upgrade() {
/// Check requisites are passed
if (!$this->requisitesok) {
- return ERROR;
+ return COMPONENT_ERROR;
}
/// Get local md5
$local_md5 = $this->get_local_md5();
/// Get remote md5
if (!$remote_md5 = $this->get_component_md5()) {
- return ERROR;
+ return COMPONENT_ERROR;
}
/// Return result
if ($local_md5 == $remote_md5) {
- return UPTODATE;
+ return COMPONENT_UPTODATE;
} else {
- return NEEDUPDATE;
+ return COMPONENT_NEEDUPDATE;
}
}