MOODLE_INTERNAL is not defined

MOODLE_INTERNAL is not defined

by Miles Fletcher -
Number of replies: 6

Hello everyone!

Just started as a Learning Technologist at Cranfield Uni. My role doesn't give me backend access to Moodle so I've set up my own copy of Moodle (3.1) on a web server I own so that I can develop some scripts to improve our automation there before submitting them to our actual VLE.


I've hit a problem while trying to make a script that makes a database query using the moodle_database.php file in /lib/dml/ . After a lot of debugging it looks like the moodle_database.php file (which comes as part of the installation process for moodle - it's not a file I made) hangs and gets stuck at the very top on the 

defined('MOODLE_INTERNAL') || die();
line. I can only assume that this means MOODLE_INTERNAL is not defined. However I don't know why. Does anyone know why this part of a file that comes with Moodle could be having a problem?


My copy of the VLE seems otherwise able to create courses etc which makes me think it's able to make database calls fine.

The script file I'm writing IS in a different directory and is using a relative path to include the file like so:

include_once("../lib/dml/moodle_database.php");
The file is definitely being found and loaded.


Thanks for your help!


Miles



Average of ratings: -
In reply to Miles Fletcher

Re: MOODLE_INTERNAL is not defined

by Miles Fletcher -

Could it be that I need to require config.php?


Here's my code:

<?php
//return list of short-names based on query
require("../config.php");
require_once("../lib/dml/moodle_database.php");
echo("reached");
global $DB;
if($_GET['caller']=="CFLSCMANAGEREXE"){
	if(isset($_GET['queryList'])){
		//sanitize user input for database query.
		$queryList = preg_replace(
		  array(
			'/[^\d,]/',    // Matches anything that's not a comma or number.
			'/(?<=,),+/',  // Matches consecutive commas.
			'/^,+/',       // Matches leading commas.
			'/,+$/'        // Matches trailing commas.
		  ),
		  '',              // Remove all matched substrings.
		  $_GET['queryList']
		);
		$shortnames = $DB->get_record_sql('SELECT * FROM {mdl_course} WHERE ' . $querylist,array());
		$result = "";
		while(mysql_fetch_array($result)){
			$result += ",".$row['shortname'];
		}
		echo substr($result,1,strlen($result)-1);
		
	}
	else{
		echo("No queryList specified");
	}
}
else{
	echo("No caller param specified");
}

In reply to Miles Fletcher

Re: MOODLE_INTERNAL is not defined

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Yes, you must always require config.php in any Moodle script.  After loading your config settings, this then runs setup.php which bootstraps your environment with Moodle's libraries, database connection, global variables and other things.

Average of ratings: Useful (1)
In reply to Miles Fletcher

Re: MOODLE_INTERNAL is not defined

by Juho Jaakkola -

Instead of including:

include_once("../lib/dml/moodle_database.php");

You should be including:

include_once("../config.php");

The message you're getting is a security precaution that prevents web server from serving files directly. The files should be used only when loaded through Moodle core, which defines a value for the MOODLE_INTERNAL, therefore allowing the file contents to be used.

Average of ratings: Useful (1)
In reply to Juho Jaakkola

Re: MOODLE_INTERNAL is not defined

by Miles Fletcher -

Very helpful, thank you! I'm very new to using Moodle as a development platform so I apologise for the n00b mistake!

Miles

In reply to Miles Fletcher

Re: MOODLE_INTERNAL is not defined

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

As a general point of style, you should always do

require(__DIR__ . '/../config.php');

to load config.php (__DIR__ is a PHP constant which holds the directory of the current file), then

require_once($CFG->dirroot . '/path/to/file.php');

to load any subsequent libraries that aren't loaded by setup.php, and don't use autoloading.

Average of ratings: Useful (2)
In reply to Mark Johnson

Re: MOODLE_INTERNAL is not defined

by Miles Fletcher -

Thanks for the tips - this code was only for my uni because it has a very specific use-case but you're probably right, I should familiarise myself with the coding style used by Moodle in case I ever want to contribute a plugin. I've taken a read through the Coding Styles documentation now and fixed a few of my other 'transgressions' smile