-<?php // $Id$
+<?php
+
+/**
+ * Library of functions for database manipulation.
+ *
+ * Library of functions for database manipulation.
+ * Other main libraries:
+ * - weblib.php - functions that produce web output
+ * - moodlelib.php - general-purpose Moodle functions
+ * @author Martin Dougiamas
+ * @version $Id$
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @package moodlecore
+ */
+
/// GLOBAL CONSTANTS /////////////////////////////////////////////////////////
+
if ($SITE = get_site()) {
+ /**
+ * If $SITE global from get_site() function exists then set it to that id, otherwise set to 1.
+ */
define('SITEID', $SITE->id);
} else {
+ /**
+ * @ignore
+ */
define('SITEID', 1);
}
-
/// FUNCTIONS FOR DATABASE HANDLING ////////////////////////////////
/**
-* execute a given sql command string
-*
-* Completely general function - it just runs some SQL and reports success.
-*
-* @param type description
-*/
+ * Execute a given sql command string
+ *
+ * Completely general function - it just runs some SQL and reports success.
+ *
+ * @param string $command the sql string to be executed
+ * @param boolean $feedback should the output of this command be returned? Default is true.
+ * @return string
+ */
function execute_sql($command, $feedback=true) {
/// Completely general function - it just runs some SQL and reports success.
return false;
}
}
-/**
-* Run an arbitrary sequence of semicolon-delimited SQL commands
-*
-* Assumes that the input text (file or string) consists of
-* a number of SQL statements ENDING WITH SEMICOLONS. The
-* semicolons MUST be the last character in a line.
-* Lines that are blank or that start with "#" are ignored.
-* Only tested with mysql dump files (mysqldump -p -d moodle)
-*
-* @param type description
-*/
+/**
+ * Run an arbitrary sequence of semicolon-delimited SQL commands
+ *
+ * Assumes that the input text (file or string) consists of
+ * a number of SQL statements ENDING WITH SEMICOLONS. The
+ * semicolons MUST be the last character in a line.
+ * Lines that are blank or that start with "#" are ignored.
+ * Only tested with mysql dump files (mysqldump -p -d moodle)
+ *
+ * @param string $sqlfile The path where a file with sql commands can be found on the server.
+ * @param string $sqlstring If no path is supplied then a string with semicolon delimited sql
+ * commands can be supplied in this argument.
+ */
function modify_database($sqlfile='', $sqlstring='') {
global $CFG;
* Add a new field to a table, or modify an existing one (if oldfield is defined).
*
* @param type description
+* @todo Finish documenting this function
*/
function table_column($table, $oldfield, $field, $type='integer', $size='10',
execute_sql('ALTER TABLE '. $CFG->prefix . $table .' DROP COLUMN '. $oldfield);
}
- execute_sql('ALTER TABLE '. $CFG->prefix . $table .' RENAME COLUMN '. $field .' TO '. $realfield');
+ execute_sql('ALTER TABLE '. $CFG->prefix . $table .' RENAME COLUMN '. $field .' TO '. $realfield);
return execute_sql('COMMIT');
break;
* Returns true or false depending on whether the specified record exists
*
* @param type description
+* @todo Finish documenting this function
*/
function record_exists($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') {
* The sql statement is provided as a string.
*
* @param type description
+* @todo Finish documenting this function
*/
function record_exists_sql($sql) {
* Get all the records and count them
*
* @param type description
+* @todo Finish documenting this function
*/
function count_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') {
* Get all the records and count them
*
* @param type description
+* @todo Finish documenting this function
*
*/
function count_records_select($table, $select='', $countitem='COUNT(*)') {
* The sql statement is provided as a string.
*
* @param type description
+* @todo Finish documenting this function
*/
function count_records_sql($sql) {
* A LIMIT is normally added to only look for 1 record
*
* @param type description
+* @todo Finish documenting this function
*/
function get_record_sql($sql) {
* "select" is a fragment of SQL to define the selection criteria
*
* @param type description
+* @todo Finish documenting this function
*/
function get_record_select($table, $select='', $fields='*') {
* limitfrom and limitnum must both be specified or not at all
*
* @param type description
+* @todo Finish documenting this function
*/
function get_records($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
* limitfrom and limitnum must both be specified or not at all
*
* @param type description
+* @todo Finish documenting this function
*/
function get_records_select($table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum='') {
* The "key" is the first column returned, eg usually "id"
*
* @param type description
+* @todo Finish documenting this function
*/
function get_records_list($table, $field='', $values='', $sort='', $fields='*') {
* The sql statement is provided as a string.
*
* @param type description
+* @todo Finish documenting this function
*/
function get_records_sql($sql) {
* The "key" is the first column returned, eg usually "id"
*
* @param type description
+* @todo Finish documenting this function
*/
function get_records_menu($table, $field='', $value='', $sort='', $fields='*') {
* Returns associative array of first two fields
*
* @param type description
+* @todo Finish documenting this function
*/
function get_records_select_menu($table, $select='', $sort='', $fields='*') {
* a form menu.
*
* @param type description
+* @todo Finish documenting this function
*/
function get_records_sql_menu($sql) {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_field($table, $return, $field1, $value1, $field2='', $value2='', $field3='', $value3='') {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_field_sql($sql) {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function set_field($table, $newfield, $newvalue, $field1, $value1, $field2='', $value2='', $field3='', $value3='') {
* Delete one or more records from a table
*
* @param type description
+* @todo Finish documenting this function
*/
function delete_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') {
* "select" is a fragment of SQL to define the selection criteria
*
* @param type description
+* @todo Finish documenting this function
*/
function delete_records_select($table, $select='') {
* $dataobject is an object containing needed data
*
* @param type description
+* @todo Finish documenting this function
*/
function insert_record($table, $dataobject, $returnid=true, $primarykey='id') {
* specify the record to update
*
* @param type description
+* @todo Finish documenting this function
*/
function update_record($table, $dataobject) {
* Suitable for setting as $USER session cookie.
*
* @param type description
+* @todo Finish documenting this function
*/
function get_user_info_from_db($field, $value) {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function adminlogin($username, $md5password) {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_guest() {
return get_user_info_from_db('username', 'guest');
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_admin () {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_admins() {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_creators() {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_teacher($courseid) {
* used to print recent activity
*
* @param type description
+* @todo Finish documenting this function
*/
function get_recent_enrolments($courseid, $timestart) {
* or on this site if courseid is id of site
*
* @param type description
+* @todo Finish documenting this function
*/
function get_course_students($courseid, $sort='s.timeaccess', $dir='', $page=0, $recordsperpage=99999,
$firstinitial='', $lastinitial='', $group=NULL, $search='', $fields='', $exceptions='') {
* Counts the students in a given course (or site), or a subset of them
*
* @param type description
+* @todo Finish documenting this function
*/
function count_course_students($course, $search='', $firstinitial='', $lastinitial='', $group=NULL, $exceptions='') {
* (also works for site)
*
* @param type description
+* @todo Finish documenting this function
*/
function get_course_teachers($courseid, $sort='t.authority ASC', $exceptions='') {
* Returns all the users of a course: students and teachers
*
* @param type description
+* @todo Finish documenting this function
*/
function get_course_users($courseid, $sort='timeaccess DESC', $exceptions='', $fields='') {
* If used for the site course searches through all undeleted, confirmed users
*
* @param type description
+* @todo Finish documenting this function
*/
function search_users($courseid, $groupid, $searchtext, $sort='', $exceptions='') {
global $CFG;
* Obsolete, just calls get_course_users(SITEID)
*
* @param type description
+* @todo Finish documenting this function
*/
function get_site_users($sort='u.lastaccess DESC', $fields='*', $exceptions='') {
*
* longdesc
*
-* @param bookean $get if false then only a count of the records is returned
+* @param boolean $get if false then only a count of the records is returned
* @param string $search a simple string to search for
* @param boolean $confirmed a switch to allow/disallow unconfirmed users
* @param array(int) $exceptions a list of IDs to ignore, eg 2,4,5,8,9,10
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=99999,
- $search='', $firstinitial='', $lastinitial='") {
+ $search='', $firstinitial='', $lastinitial='') {
global $CFG;
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_users_confirmed() {
global $CFG;
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_users_unconfirmed($cutofftime=2000000000) {
global $CFG;
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_users_longtimenosee($cutofftime) {
global $CFG;
* list of all groups in the course.
*
* @param type description
+* @todo Finish documenting this function
*/
function get_groups($courseid, $userid=0) {
global $CFG;
* Returns an array of user objects
*
* @param type description
+* @todo Finish documenting this function
*/
function get_group_users($groupid, $sort='u.lastaccess DESC', $exceptions='') {
global $CFG;
* An efficient way of finding all the users who aren't in groups yet
*
* @param type description
+* @todo Finish documenting this function
*/
function get_users_not_in_group($courseid) {
global $CFG;
* Returns an array of user objects
*
* @param type description
+* @todo Finish documenting this function
*/
function get_group_students($groupid, $sort='u.lastaccess DESC') {
global $CFG;
* Returns the user's group in a particular course
*
* @param type description
+* @todo Finish documenting this function
*/
function user_group($courseid, $userid) {
global $CFG;
* Returns $course object of the top-level site.
*
* @param type description
+* @todo Finish documenting this function
*/
function get_site () {
* Returns list of courses, for whole site, or category
*
* @param type description
+* @todo Finish documenting this function
*/
function get_courses($categoryid='all', $sort='c.sortorder ASC', $fields='c.*') {
* Similar to get_courses, but allows paging
*
* @param type description
+* @todo Finish documenting this function
*/
function get_courses_page($categoryid='all', $sort='c.sortorder ASC', $fields='c.*',
&$totalcount, $limitfrom='', $limitnum='') {
* longdesc
*
* @param type description
+* @todo Finish documenting this function
*/
function get_my_courses($userid, $sort='visible DESC,sortorder ASC') {
* Returns a list of courses that match a search
*
* @param type description
+* @todo Finish documenting this function
*/
function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $recordsperpage=50, &$totalcount) {
* Returns a sorted list of categories
*
* @param type description
+* @todo Finish documenting this function
*/
function get_categories($parent='none', $sort='sortorder ASC') {
* This recursive function makes sure that the courseorder is consecutive
*
* @param type description
+* @todo Finish documenting this function
*/
function fix_course_sortorder($categoryid=0, $n=0) {
* existing language translations and older sites.
*
* @param type description
+* @todo Finish documenting this function
*/
function make_default_scale() {
* Returns a menu of all available scales from the site as well as the given course
*
* @param type description
+* @todo Finish documenting this function
*/
function get_scales_menu($courseid=0) {
* Just gets a raw list of all modules in a course
*
* @param type description
+* @todo Finish documenting this function
*/
function get_course_mods($courseid) {
global $CFG;
* Given an instance of a module, finds the coursemodule description
*
* @param type description
+* @todo Finish documenting this function
*/
function get_coursemodule_from_instance($modulename, $instance, $courseid) {
* is visible or not
*
* @param type description
+* @todo Finish documenting this function
*/
function instance_is_visible($moduletype, $module) {
* select all log records for a given course and user
*
* @param type description
+* @todo Finish documenting this function
*/
function get_logs_usercourse($userid, $courseid, $coursestart) {
global $CFG;
* select all log records for a given course, user, and day
*
* @param type description
+* @todo Finish documenting this function
*/
function get_logs_userday($userid, $courseid, $daystart) {
global $CFG;
* Mostly just for debugging
*
* @param type description
+* @todo Finish documenting this function
*/
function print_object($object) {
-<?php // $Id$
+<?php
///////////////////////////////////////////////////////////////////////////
// //
// //
///////////////////////////////////////////////////////////////////////////
+/**
+ * Moodle main library
+ *
+ * Main library file of miscellaneous general-purpose Moodle functions.
+ * Other main libraries:
+ * <ul><li> weblib.php - functions that produce web output
+ * <li> datalib.php - functions that access the database</ul>
+ * @author Martin Dougiamas
+ * @version $Id$
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @package moodlecore
+ */
/// CONSTANTS /////////////////////////////////////////////////////////////
+/**
+ * No groups used?
+ */
define('NOGROUPS', 0);
+
+/**
+ * Groups used?
+ */
define('SEPARATEGROUPS', 1);
+
+/**
+ * Groups visible?
+ */
define('VISIBLEGROUPS', 2);
/// PARAMETER HANDLING ////////////////////////////////////////////////////
+/**
+ * Ensure that a variable is set or display error
+ *
+ * If $var is undefined display an error message using the {@link error()} function.
+ *
+ * @param mixed $var the variable which may not be set
+ */
function require_variable($var) {
/// Variable must be present
if (! isset($var)) {
}
}
+
+/**
+ * Ensure that a variable is set
+ *
+ * If $var is undefined set it (by reference), otherwise return $var.
+ * This function is very similar to {@link nvl()}
+ *
+ * @param mixed $var the variable which may be unset
+ * @param mixed $default the value to return if $var is unset
+ */
function optional_variable(&$var, $default=0) {
/// Variable may be present, if not then set a default
if (! isset($var)) {
}
}
-
+/**
+ * Set a key in global configuration
+ *
+ * Set a key/value pair in both this session's $CFG global variable
+ * and in the 'config' database table for future sessions.
+ *
+ * @param string $name the key to set
+ * @param string $value the value to set
+ * @uses $CFG
+ * @return bool
+ */
function set_config($name, $value) {
/// No need for get_config because they are usually always available in $CFG
global $CFG;
+
$CFG->$name = $value; // So it's defined for this invocation at least
if (get_field('config', 'name', 'name', $name)) {
}
}
-
+/**
+ * Refresh current $USER session global variable with all their current preferences.
+ * @uses $USER
+ */
function reload_user_preferences() {
/// Refresh current USER with all their current preferences
}
}
+/**
+ * Sets a preference for the current user
+ * Optionally, can set a preference for a different user object
+ * @uses $USER
+ * @todo Add a better description and include usage examples.
+ * @param string $name The key to set as preference for the specified user
+ * @param string $value The value to set forthe $name key in the specified user's record
+ * @param object $user A moodle user object
+ * @todo Add inline links to $USER and user functions in above line.
+ * @return boolean
+ */
function set_user_preference($name, $value, $user=NULL) {
/// Sets a preference for the current user
/// Optionally, can set a preference for a different user object
}
}
+/**
+ * Sets a whole array of preferences for the current user
+ * @param array $prefarray An array of key/value pairs to be set
+ * @return boolean
+ */
function set_user_preferences($prefarray) {
/// Sets a whole array of preferences for the current user
return $return;
}
+/**
+ * If no arguments are supplied this function will return
+ * all of the current user preferences as an array.
+ * If a name is specified then this function
+ * attempts to return that particular preference value. If
+ * none is found, then the optional value $default is returned,
+ * otherwise NULL.
+ * @param string $name Name of the key to use in finding a preference value
+ * @param string $default Value to be returned if the $name key is not set in the user preferences
+ * @uses $USER
+ * @return string
+ */
function get_user_preferences($name=NULL, $default=NULL) {
/// Without arguments, returns all the current user preferences
/// as an array. If a name is specified, then this function
/// FUNCTIONS FOR HANDLING TIME ////////////////////////////////////////////
+/**
+ * Given date parts in user time, produce a GMT timestamp.
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99) {
/// Given date parts in user time, produce a GMT timestamp
}
}
-function format_time($totalsecs, $str=NULL) {
+/**
+ * Given an amount of time in seconds, returns string
+ * formatted nicely as months, days, hours etc as needed
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
+ function format_time($totalsecs, $str=NULL) {
/// Given an amount of time in seconds, returns string
/// formatted nicely as months, days, hours etc as needed
return get_string('now');
}
+/**
+ * Returns a formatted string that represents a date in user time
+ * <b>WARNING: note that the format is for strftime(), not date().</b>
+ * Because of a bug in most Windows time libraries, we can't use
+ * the nicer %e, so we have to use %d which has leading zeroes.
+ * A lot of the fuss in the function is just getting rid of these leading
+ * zeroes as efficiently as possible.
+ *
+ * If parammeter fixday = true (default), then take off leading
+ * zero from %d, else mantain it.
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
function userdate($date, $format='', $timezone=99, $fixday = true) {
/// Returns a formatted string that represents a date in user time
/// WARNING: note that the format is for strftime(), not date().
return $datestring;
}
+/**
+ * Given a $date timestamp in GMT, returns an array
+ * that represents the date in user time
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
function usergetdate($date, $timezone=99) {
/// Given a $date timestamp in GMT, returns an array
/// that represents the date in user time
return $getdate;
}
+/**
+ * Given a GMT timestamp (seconds since epoch), offsets it by
+ * the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
function usertime($date, $timezone=99) {
/// Given a GMT timestamp (seconds since epoch), offsets it by
/// the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
}
+/**
+ * Returns a string that prints the user's timezone
+ *
+ * @param float $timezone The user's timezone
+ * @return string
+ */
function usertimezone($timezone=99) {
/// Returns a string that prints the user's timezone
}
}
+/**
+ * Returns a float which represents the user's timezone difference from GMT in hours
+ * Checks various settings and picks the most dominant of those which have a value
+ *
+ * @param float $tz The user's timezone
+ * @return float
+ * @uses $CFG
+ * @uses $USER
+ */
function get_user_timezone($tz = 99) {
// Returns a float which represents the user's timezone difference from GMT in hours
// Checks various settings and picks the most dominant of those which have a value
/// USER AUTHENTICATION AND LOGIN ////////////////////////////////////////
+/**
+ * This function checks that the current user is logged in, and optionally
+ * whether they are "logged in" or allowed to be in a particular course.
+ * If not, then it redirects them to the site login or course enrolment.
+ * $autologinguest determines whether visitors should automatically be
+ * logged in as guests provide $CFG->autologinguests is set to 1
+ *
+ * @param type description
+ * @uses $CFG
+ * @uses $SESSION
+ * @uses $USER
+ * @uses $FULLME
+ * @uses $MoodleSession
+ * @todo Finish documenting this function
+ */
function require_login($courseid=0, $autologinguest=true) {
/// This function checks that the current user is logged in, and optionally
/// whether they are "logged in" or allowed to be in a particular course.
}
}
+/**
+ * This is a weaker version of {@link require_login()} which only requires login
+ * when called from within a course rather than the site page, unless
+ * the forcelogin option is turned on.
+ *
+ * @uses $CFG
+ * @param type description
+ * @todo Finish documenting this function
+ */
function require_course_login($course, $autologinguest=true) {
// This is a weaker version of require_login which only requires login
// when called from within a course rather than the site page, unless
}
}
+/**
+ * Modify the user table by setting the currently logged in user's
+ * last login to now.
+ *
+ * @uses $USER
+ * @return boolean
+ */
function update_user_login_times() {
global $USER;
return update_record('user', $user);
}
+/**
+ * Determines if a user has completed setting up their account.
+ *
+ * @param object $user A user object to test for the existance of a valid name and email
+ * @return boolean
+ */
function user_not_fully_set_up($user) {
return ($user->username != 'guest' and (empty($user->firstname) or empty($user->lastname) or empty($user->email)));
}
+/**
+ * Keeps track of login attempts
+ *
+ * @uses $SESSION
+ */
function update_login_count() {
/// Keeps track of login attempts
}
}
+/**
+ * Resets login attempts
+ *
+ * @uses $SESSION
+ */
function reset_login_count() {
/// Resets login attempts
global $SESSION;
$SESSION->logincount = 0;
}
+/**
+ * check_for_restricted_user
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
function check_for_restricted_user($username=NULL, $redirect='') {
global $CFG, $USER;
}
}
+/**
+ * Determines if a user an admin
+ *
+ * @uses $USER
+ * @param integer $userid The id of the user as is found in the 'user' table
+ * @return boolean
+ */
function isadmin($userid=0) {
/// Is the user an admin?
global $USER;
}
}
+/**
+ * Determines if a user is a teacher or an admin
+ *
+ * @uses $USER
+ * @param integer $courseid The id of the course that is being viewed, if any
+ * @param integer $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
+ * @param boolean $includeadmin If true this function will return true when it encounters an admin user.
+ * @return boolean
+ * @todo Finish documenting this function
+ */
function isteacher($courseid=0, $userid=0, $includeadmin=true) {
/// Is the user a teacher or admin?
global $USER;
return record_exists('user_teachers', 'userid', $userid, 'course', $courseid);
}
+/**
+ * Determines if a user is allowed to edit a given course
+ *
+ * @uses $USER
+ * @param integer $courseid The id of the course that is being edited
+ * @param integer $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
+ * @return boolean
+ */
function isteacheredit($courseid, $userid=0) {
/// Is the user allowed to edit this course?
global $USER;
return get_field('user_teachers', 'editall', 'userid', $userid, 'course', $courseid);
}
+/**
+ * Determines if a user can create new courses
+ *
+ * @uses $USER
+ * @param integer $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
+ * @return boolean
+ */
function iscreator ($userid=0) {
/// Can user create new courses?
global $USER;
return record_exists('user_coursecreators', 'userid', $userid);
}
+/**
+ * Determines if a user is a student in the specified course
+ *
+ * If the course id specifies the site then the function determines
+ * if the user is a confirmed and valid user of this site.
+ *
+ * @uses $USER
+ * @uses $CFG
+ * @param integer $courseid The id of the course being tested
+ * @param integer $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
+ * @return boolean
+ */
function isstudent($courseid, $userid=0) {
/// Is the user a student in this course?
/// If course is site, is the user a confirmed user on the site?
return record_exists('user_students', 'userid', $userid, 'course', $courseid);
}
+/**
+ * Determines if the specified user is logged in as guest.
+ *
+ * @uses $USER
+ * @param integer $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
+ * @return boolean
+ */
function isguest($userid=0) {
/// Is the user a guest?
global $USER;
return record_exists('user', 'id', $userid, 'username', 'guest');
}
-
+/**
+ * Determines if the currently logged in user is in editing mode
+ *
+ * @uses $USER
+ * @param integer $courseid The id of the course being tested
+ * @param object $user A user object. If null then the currently logged in user is used.
+ * @return boolean
+ */
function isediting($courseid, $user=NULL) {
/// Is the current user in editing mode?
global $USER;
return ($user->editing and isteacher($courseid, $user->id));
}
+/**
+ * Determines if the logged in user is currently moving an activity
+ *
+ * @uses $USER
+ * @param integer $courseid The id of the course being tested
+ * @return boolean
+ */
function ismoving($courseid) {
/// Is the current user currently moving an activity?
global $USER;
return false;
}
+/**
+ * Given an object containing firstname and lastname
+ * values, this function returns a string with the
+ * full name of the person.
+ * The result may depend on system settings
+ * or language. 'override' will force both names
+ * to be used even if system settings specify one.
+ * @uses $CFG
+ * @uses $SESSION
+ * @param type description
+ * @todo Finish documenting this function
+ */
function fullname($user, $override=false) {
/// Given an object containing firstname and lastname
/// values, this function returns a string with the
return get_string('fullnamedisplay', '', $user);
}
-
+/**
+ * Sets a moodle cookie with an encrypted string
+ *
+ * @uses $CFG
+ * @param string $thing The string to encrypt and place in a cookie
+ */
function set_moodle_cookie($thing) {
/// Sets a moodle cookie with an encrypted string
global $CFG;
setCookie($cookiename, rc4encrypt($thing), time()+$seconds, '/');
}
-
+/**
+ * Gets a moodle cookie with an encrypted string
+ *
+ * @uses $CFG
+ * @return string
+ */
function get_moodle_cookie() {
/// Gets a moodle cookie with an encrypted string
global $CFG;
}
}
+/**
+ * Returns true if an internal authentication method is being used.
+ * if method not specified then, global default is assumed
+ *
+ * @uses $CFG
+ * @param string $auth Form of authentication required
+ * @return boolean
+ * @todo Outline auth types and provide code example
+ */
function is_internal_auth($auth='') {
/// Returns true if an internal authentication method is being used.
/// if method not specified then, global default is assumed
return ($method == 'email' || $method == 'none' || $method == 'manual');
}
+/**
+ * Creates a bare-bones user record
+ *
+ * @uses $CFG
+ * @uses $REMOTE_ADDR
+ * @param string $username New user's username to add to record
+ * @param string $password New user's password to add to record
+ * @param string $auth Form of authentication required
+ * @return object
+ * @todo Outline auth types and provide code example
+ */
function create_user_record($username, $password, $auth='') {
/// Creates a bare-bones user record
global $REMOTE_ADDR, $CFG;
return false;
}
+/**
+ * Will update a local user record from an external source
+ *
+ * @uses $CFG
+ * @param string $username New user's username to add to record
+ * @return object
+ */
function update_user_record($username) {
/// will update a local user record from an external source.
global $CFG;
return get_user_info_from_db('username', $username);
}
+/**
+ * Retrieve the guest user object
+ *
+ * @uses $CFG
+ * @return object
+ */
function guest_user() {
global $CFG;
return $newuser;
}
+/**
+ * Given a username and password, this function looks them
+ * up using the currently selected authentication mechanism,
+ * and if the authentication is successful, it returns a
+ * valid $user object from the 'user' table.
+ *
+ * Uses auth_ functions from the currently active auth module
+ *
+ * @uses $CFG
+ * @param string $username user's username
+ * @param string $password user's password
+ * @return object
+ */
function authenticate_user_login($username, $password) {
/// Given a username and password, this function looks them
/// up using the currently selected authentication mechanism,
}
}
+/**
+ * Enrols (or re-enrols) a student in a given course
+ *
+ * @param integer $courseid The id of the course that is being viewed
+ * @param integer $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
+ * @param integer $timestart ?
+ * @param integer $timeend ?
+ * @return boolean
+ * @todo Finish documenting this function
+ */
function enrol_student($userid, $courseid, $timestart=0, $timeend=0) {
/// Enrols (or re-enrols) a student in a given course
}
}
+/**
+ * Unenrols a student from a given course
+ *
+ * @param integer $courseid The id of the course that is being viewed, if any
+ * @param integer $userid The id of the user that is being tested against.
+ * @return boolean
+ */
function unenrol_student($userid, $courseid=0) {
/// Unenrols a student from a given course
}
}
+/**
+ * Add a teacher to a given course
+ *
+ * @uses $USER
+ * @param integer $courseid The id of the course that is being viewed, if any
+ * @param integer $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
+ * @param integer $editall ?
+ * @param string $role ?
+ * @param integer $timestart ?
+ * @param integer $timeend ?
+ * @return boolean
+ * @todo Finish documenting this function
+ */
function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0) {
/// Add a teacher to a given course
global $CFG;
}
+/**
+ * Removes a teacher from a given course (or ALL courses)
+ * Does not delete the user account
+ *
+ * @param integer $courseid The id of the course that is being viewed, if any
+ * @param integer $userid The id of the user that is being tested against.
+ * @return boolean
+ */
function remove_teacher($userid, $courseid=0) {
/// Removes a teacher from a given course (or ALL courses)
/// Does not delete the user account
}
}
-
+/**
+ * Add a creator to the site
+ *
+ * @param integer $userid The id of the user that is being tested against.
+ * @return boolean
+ */
function add_creator($userid) {
/// Add a creator to the site
return true;
}
+/**
+ * Remove a creator from a site
+ *
+ * @uses $db
+ * @param integer $userid The id of the user that is being tested against.
+ * @return boolean
+ */
function remove_creator($userid) {
/// Removes a creator from a site
global $db;
return delete_records('user_coursecreators', 'userid', $userid);
}
+/**
+ * Add an admin to a site
+ *
+ * @uses SITEID
+ * @param integer $userid The id of the user that is being tested against.
+ * @return boolean
+ */
function add_admin($userid) {
/// Add an admin to the site
return true;
}
+/**
+ * Removes an admin from a site
+ *
+ * @uses $db
+ * @uses SITEID
+ * @param integer $userid The id of the user that is being tested against.
+ * @return boolean
+ */
function remove_admin($userid) {
/// Removes an admin from a site
global $db;
return delete_records('user_admins', 'userid', $userid);
}
-
+/**
+ * Clear a course out completely, deleting all content
+ * but don't delete the course itself
+ *
+ * @uses $USER
+ * @uses $SESSION
+ * @uses $CFG
+ * @param integer $courseid The id of the course that is being viewed
+ * @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps.
+ * @return boolean
+ */
function remove_course_contents($courseid, $showfeedback=true) {
/// Clear a course out completely, deleting all content
/// but don't delete the course itself
}
+/**
+ * This function will empty a course of USER data as much as
+/// possible. It will retain the activities and the structure
+/// of the course.
+ *
+ * @uses $USER
+ * @uses $THEME
+ * @uses $SESSION
+ * @uses $CFG
+ * @param integer $courseid The id of the course that is being viewed
+ * @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps.
+ * @param boolean $removestudents ?
+ * @param boolean $removeteachers ?
+ * @param boolean $removegroups ?
+ * @param boolean $removeevents ?
+ * @param boolean $removelogs ?
+ * @return boolean
+ * @todo Finish documenting this function
+ */
function remove_course_userdata($courseid, $showfeedback=true,
$removestudents=true, $removeteachers=false, $removegroups=true,
$removeevents=true, $removelogs=false) {
* Returns a boolean: is the user a member of the given group?
*
* @param type description
+ * @todo Finish documenting this function
*/
function ismember($groupid, $userid=0) {
global $USER;
* Returns the group ID of the current user in the given course
*
* @param type description
+ * @todo Finish documenting this function
*/
function mygroupid($courseid) {
global $USER;
* NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
*
* @param type description
+ * @todo Finish documenting this function
*/
function groupmode($course, $cm=null) {
* Sets the current group in the session variable
*
* @param type description
+ * @todo Finish documenting this function
*/
function set_current_group($courseid, $groupid) {
global $SESSION;
* Gets the current group for the current user as an id or an object
*
* @param type description
+ * @todo Finish documenting this function
*/
function get_current_group($courseid, $full=false) {
global $SESSION, $USER;
* that to reset the current group for the user.
*
* @param type description
+ * @todo Finish documenting this function
*/
function get_and_set_current_group($course, $groupmode, $groupid=-1) {
* Otherwise returns false if groups aren't relevant
*
* @param type description
+ * @todo Finish documenting this function
*/
function setup_and_print_groups($course, $groupmode, $urlroot) {
/// CORRESPONDENCE ////////////////////////////////////////////////
+/**
+ * Send an email to a specified user
+ *
+ * Returns "true" if mail was sent OK, "emailstop" if email was blocked by user
+ * and "false" if there was another sort of error.
+ *
+ * @uses $CFG
+ * @uses $_SERVER
+ * @param object $user a user record as an object
+ * @param object $from a user record as an object
+ * @param string $subject plain text subject line of the email
+ * @param string $messagetext plain text version of the message
+ * @param string $messagehtml complete html version of the message (optional)
+ * @param string $attachment a file on the filesystem, relative to $CFG->dataroot
+ * @param string $attachname the name of the file (extension indicates MIME)
+ * @param boolean $usetrueaddress determines whether $from email address should be sent out. Will be overruled by user profile setting for maildisplay
+ * @return boolean
+ */
function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $attachment='', $attachname='', $usetrueaddress=true) {
/// user - a user record as an object
/// from - a user record as an object
}
}
+/**
+ * Resets specified user's password and send the new password to the user via email.
+ *
+ * @uses $CFG
+ * @param object $user
+ * @return boolean
+ */
function reset_password_and_mail($user) {
global $CFG;
}
-function send_confirmation_email($user) {
+/**
+ * Send email to specified user with confirmation text and activation link.
+ *
+ * @uses $CFG
+ * @param object $user
+ * @return boolean
+ */
+ function send_confirmation_email($user) {
global $CFG;
}
+/**
+ * send_password_change_confirmation_email.
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
function send_password_change_confirmation_email($user) {
global $CFG;
}
-
+/**
+ * Check that an email is allowed. It returns an error message if there
+ * was a problem.
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
function email_is_not_allowed($email) {
/// Check that an email is allowed. It returns an error message if there
/// was a problem.
/// FILE HANDLING /////////////////////////////////////////////
-
+/**
+ * Create a directory.
+ *
+ * @uses $CFG
+ * @param string $directory a string of directory names under $CFG->dataroot
+ * @return object
+ * @todo Finish documenting this function
+ */
function make_upload_directory($directory, $shownotices=true) {
/// $directory = a string of directory names under $CFG->dataroot
/// eg stuff/assignment/1
return $currdir;
}
-
+/**
+ * Makes an upload directory for a particular module.
+ *
+ * @uses $CFG
+ * @param integer $courseid ?
+ * @todo Finish documenting this function
+ */
function make_mod_upload_directory($courseid) {
/// Makes an upload directory for a particular module
global $CFG;
return $moddata;
}
-
+/**
+ * Returns current name of file on disk if true.
+ *
+ * @param string $newfile ?
+ * @todo Finish documenting this function
+ */
function valid_uploaded_file($newfile) {
/// Returns current name of file on disk if true
if (empty($newfile)) {
}
}
+/**
+ * Returns the maximum size for uploading files.
+ *
+ * There are seven possible upload limits:
+ * 1. in Apache using LimitRequestBody (no way of checking or changing this)
+ * 2. in php.ini for 'upload_max_filesize' (can not be changed inside PHP)
+ * 3. in .htaccess for 'upload_max_filesize' (can not be changed inside PHP)
+ * 4. in php.ini for 'post_max_size' (can not be changed inside PHP)
+ * 5. by the Moodle admin in $CFG->maxbytes
+ * 6. by the teacher in the current course $course->maxbytes
+ * 7. by the teacher for the current module, eg $assignment->maxbytes
+ *
+ * These last two are passed to this function as arguments (in bytes).
+ * Anything defined as 0 is ignored.
+ * The smallest of all the non-zero numbers is returned.
+ *
+ * @param type description
+ * @todo Finish documenting this function
+ */
function get_max_upload_file_size($sitebytes=0, $coursebytes=0, $modulebytes=0) {
/// Returns the maximum size for uploading files
/// There are seven possible upload limits:
return $minimumsize;
}
+/**
+ * Related to the above function - this function returns an
+ * array of possible sizes in an array, translated to the
+ * local language.
+ *
+ * @param integer $sizebytes ?
+ * @param integer $coursebytes ?
+ * @param integer $modulebytes
+ * @return integer
+ * @todo Finish documenting this function
+ */
function get_max_upload_sizes($sitebytes=0, $coursebytes=0, $modulebytes=0) {
/// Related to the above function - this function returns an
/// array of possible sizes in an array, translated to the
return $filesize;
}
-
+/**
+ * If there has been an error uploading a file, print the appropriate error message
+ * Numerical constants used as constant definitions not added until PHP version 4.2.0
+ *
+ * filearray is a 1-dimensional sub-array of the $_FILES array
+ * eg $filearray = $_FILES['userfile1']
+ * If left empty then the first element of the $_FILES array will be used *
+ * @param array $filearray ?
+ * @param boolean $returnerror
+ * @return boolean
+ * @todo Finish documenting this function
+ */
function print_file_upload_error($filearray = '', $returnerror = false) {
/// If there has been an error uploading a file, print the appropriate error message
/// Numerical constants used as constant definitions not added until PHP version 4.2.0
}
-
+/**
+ * Returns an array with all the filenames in
+ * all subdirectories, relative to the given rootdir.
+ * If excludefile is defined, then that file/directory is ignored
+ * If getdirs is true, then (sub)directories are included in the output
+ * If getfiles is true, then files are included in the output
+ * (at least one of these must be true!)
+ *
+ * @param type name definition
+ * @return array
+ * @todo Finish documenting this function
+ */
function get_directory_list($rootdir, $excludefile='', $descend=true, $getdirs=false, $getfiles=true) {
/// Returns an array with all the filenames in
/// all subdirectories, relative to the given rootdir.
return $dirs;
}
+/**
+ * Adds up all the files in a directory and works out the size.
+ *
+ * @param string $rootdir ?
+ * @param string $excludefile ?
+ * @return object
+ * @todo Finish documenting this function
+ */
function get_directory_size($rootdir, $excludefile='') {
/// Adds up all the files in a directory and works out the size
return $size;
}
+/**
+ * Converts numbers like 10M into bytes.
+ *
+ * @uses $CFG
+ * @param mixed $size The size to be converted
+ * @return mixed
+ */
function get_real_size($size=0) {
/// Converts numbers like 10M into bytes
if (!$size) {
return $size;
}
+/**
+ * Converts bytes into display form
+ *
+ * @uses $CFG
+ * @param string $directory a string of directory names under $CFG->dataroot
+ * @return object
+ * @todo Finish documenting this function
+ */
function display_size($size) {
/// Converts bytes into display form
- static $gb,$mb,$kb,$b;
+ static $gb, $mb, $kb, $b;
if (empty($gb)) {
$gb = get_string('sizegb');
return $size;
}
+/**
+ * Create a directory.
+ *
+ * @uses $CFG
+ * @param string $directory a string of directory names under $CFG->dataroot
+ * @return object
+ * @todo Finish documenting this function
+ */
function clean_filename($string) {
/// Cleans a given filename by removing suspicious or troublesome characters
/// Only these are allowed:
}
$langpath = $CFG->dirroot .'/lang';
- $langfile = $langpath .'/'. $lang .'/'. $module .'.php'.;
+ $langfile = $langpath .'/'. $lang .'/'. $module .'.php';
// Look for the string - if found then return it
-<?php // $Id$
+<?php
///////////////////////////////////////////////////////////////////////////
// weblib.php - functions for web output
// //
///////////////////////////////////////////////////////////////////////////
+/**
+ * Library of functions for web output
+ *
+ * Library of all general-purpose Moodle PHP functions and constants
+ * that produce HTML output
+ *
+ * Other main libraries:
+ * - datalib.php - functions that access the database.
+ * - moodlelib.php - general-purpose Moodle functions.
+ * @author Martin Dougiamas
+ * @version $Id$
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @package moodlecore
+ */
+
/// Constants
/// Define text formatting types ... eventually we can add Wiki, BBcode etc
+
+/**
+ * Does all sorts of transformations and filtering
+ */
define('FORMAT_MOODLE', '0'); // Does all sorts of transformations and filtering
+
+/**
+ * Plain HTML (with some tags stripped)
+ */
define('FORMAT_HTML', '1'); // Plain HTML (with some tags stripped)
+
+/**
+ * Plain text (even tags are printed in full)
+ */
define('FORMAT_PLAIN', '2'); // Plain text (even tags are printed in full)
+
+/**
+ * Wiki-formatted text
+ */
define('FORMAT_WIKI', '3'); // Wiki-formatted text
+
+/**
+ * Markdown-formatted text http://daringfireball.net/projects/markdown/
+ */
define('FORMAT_MARKDOWN', '4'); // Markdown-formatted text http://daringfireball.net/projects/markdown/
+
+/**
+ * Allowed tags - string of html tags that can be tested against for safe html tags
+ * @global string $ALLOWED_TAGS
+ */
$ALLOWED_TAGS =
'<p><br /><b><i><u><font><table><tbody><span><div><tr><td><th><ol><ul><dl><li><dt><dd><h1><h2><h3><h4><h5><h6><hr><img><a><strong><emphasis><em><sup><sub><address><cite><blockquote><pre><strike><embed><object><param><acronym><nolink><style><lang><tex><algebra><math><mi><mn><mo><mtext><mspace><ms><mrow><mfrac><msqrt><mroot><mstyle><merror><mpadded><mphantom><mfenced><msub><msup><msubsup><munder><mover><munderover><mmultiscripts><mtable><mtr><mtd><maligngroup><malignmark><maction><cn><ci><apply><reln><fn><interval><inverse><sep><condition><declare><lambda><compose><ident><quotient><exp><factorial><divide><max><min><minus><plus><power><rem><times><root><gcd><and><or><xor><not><implies><forall><exists><abs><conjugate><eq><neq><gt><lt><geq><leq><ln><log><int><diff><partialdiff><lowlimit><uplimit><bvar><degree><set><list><union><intersect><in><notin><subset><prsubset><notsubset><notprsubset><setdiff><sum><product><limit><tendsto><mean><sdev><variance><median><mode><moment><vector><matrix><matrixrow><determinant><transpose><selector><annotation><semantics><annotation-xml><tt><code>';
/// Functions
+/**
+ * Add quotes to HTML characters
+ *
+ * Returns $var with HTML characters (like "<", ">", etc.) properly quoted.
+ * This function is very similar to {@link p()}
+ *
+ * @param string $var the string potentially containing HTML characters
+ * @return string
+ */
function s($var) {
/// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
return htmlSpecialChars(stripslashes_safe($var));
}
+/**
+ * Add quotes to HTML characters
+ *
+ * Returns $var with HTML characters (like "<", ">", etc.) properly quoted.
+ * This function is very similar to {@link s()}
+ *
+ * @param string $var the string potentially containing HTML characters
+ * @return string
+ */
function p($var) {
/// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
echo htmlSpecialChars(stripslashes_safe($var));
}
+
+/**
+ * Ensure that a variable is set
+ *
+ * Return $var if it is defined, otherwise return $default,
+ * This function is very similar to {@link optional_variable()}
+ *
+ * @param mixed $var the variable which may be unset
+ * @param mixed $default the value to return if $var is unset
+ * @return mixed
+ */
function nvl(&$var, $default='') {
/// if $var is undefined, return $default, otherwise return $var
return isset($var) ? $var : $default;
}
-function strip_querystring($url) {
+/**
+ * Remove query string from url
+ *
+ * Takes in a URL and returns it without the querystring portion
+ *
+ * @param string $url the url which may have a query string attached
+ * @return string
+ */
+ function strip_querystring($url) {
/// takes a URL and returns it without the querystring portion
if ($commapos = strpos($url, '?')) {
}
}
+/**
+ * Returns the URL of the HTTP_REFERER, less the querystring portion
+ * @return string
+ */
function get_referer() {
/// returns the URL of the HTTP_REFERER, less the querystring portion
}
-function me() {
+/**
+ * Returns the name of the current script, WITH the querystring portion.
+ * this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME
+ * return different things depending on a lot of things like your OS, Web
+ * server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
+ * NOTE: This function returns false if the global variables needed are not set.
+ * @return string
+ */
+ function me() {
/// returns the name of the current script, WITH the querystring portion.
/// this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME
/// return different things depending on a lot of things like your OS, Web
}
}
-
+/**
+ * Like me() but returns a full URL
+ * @see me()
+ * @link me()
+ * @return string
+ */
function qualified_me() {
/// like me() but returns a full URL
return $url_prefix . me();
}
-
+/**
+ * Determine if a web referer is valid
+ *
+ * Returns true if the referer is the same as the goodreferer. If
+ * the referer to test is not specified, use {@link qualified_me()}.
+ * If the admin has not set secure forms ($CFG->secureforms) then
+ * this function returns true regardless of a match.
+ * @uses $CFG
+ * @param string $goodreferer the url to compare to referer
+ * @return boolean
+ */
function match_referer($goodreferer = '') {
/// returns true if the referer is the same as the goodreferer. If
/// goodreferer is not specified, use qualified_me as the goodreferer
return (($referer == $goodreferer) or ($referer == $CFG->wwwroot .'/'));
}
+/**
+ * Determine if there is data waiting to be processed from a form
+ *
+ * Used on most forms in Moodle to check for data
+ * Returns the data as an object, if it's found.
+ * This object can be used in foreach loops without
+ * casting because it's cast to (array) automatically
+ *
+ * Checks that submitted POST data exists, and also
+ * checks the referer against the given url (it uses
+ * the current page if none was specified
+ * @uses $CFG
+ * @param string $url the url to compare to referer for secure forms
+ * @return boolean
+ */
function data_submitted($url='') {
/// Used on most forms in Moodle to check for data
/// Returns the data as an object, if it's found.
}
}
+/**
+ * Moodle replacement for php stripslashes() function
+ *
+ * The standard php stripslashes() removes ALL backslashes
+ * even from strings - so C:\temp becomes C:temp - this isn't good.
+ * This function should work as a fairly safe replacement
+ * to be called on quoted AND unquoted strings (to be sure)
+ * @param string the string to remove unsafe slashes from
+ * @return string
+ */
function stripslashes_safe($string) {
/// stripslashes() removes ALL backslashes even from strings
/// so C:\temp becomes C:temp ... this isn't good.
return $string;
}
-
+/**
+ * Given some normal text, this function will break up any
+ * long words to a given size, by inserting the given character
+ * @param string $string the string to be modified
+ * @param integer $maxsize maximum length of the string to be returned
+ * @param string $cutchar the string used to represent word breaks
+ * @return string
+ */
function break_up_long_words($string, $maxsize=20, $cutchar=' ') {
/// Given some normal text, this function will break up any
/// long words to a given size, by inserting the given character
return $output;
}
-
+/**
+ * This does a search and replace, ignoring case
+ * This function is only used for versions of PHP older than version 5
+ * which do not have a native version of this function.
+ * Taken from the PHP manual, by bradhuizenga @ softhome.net
+ * @param string $find the string to search for
+ * @param string $replace the string to replace $find with
+ * @param string $string the string to search through
+ * return string
+ */
if (!function_exists('str_ireplace')) { /// Only exists in PHP 5
function str_ireplace($find, $replace, $string) {
/// This does a search and replace, ignoring case
}
}
+/**
+ * Locate the position of a string in another string
+ *
+ * This function is only used for versions of PHP older than version 5
+ * which do not have a native version of this function.
+ * Taken from the PHP manual, by dmarsh @ spscc.ctc.edu
+ * @param string $haystack The string to be searched
+ * @param string $needle The string to search for
+ * @param integer $offset The position in $haystack where the search should begin.
+ */
if (!function_exists('stripos')) { /// Only exists in PHP 5
function stripos($haystack, $needle, $offset=0) {
/// This function is only used for versions of PHP older than version 5
}
}
+/**
+ * Load a template from file
+ *
+ * Returns a (big) string containing the contents of a template file with all
+ * the variables interpolated. all the variables must be in the $var[] array or
+ * object (whatever you decide to use).
+ *
+ * <b>WARNING: do not use this on big files!!</b>
+ * @param string $filename Location on the server's filesystem where template can be found.
+ * @param mixed $var Passed in by reference. An array or object which will be loaded with data from the template file.
+ */
function read_template($filename, &$var) {
/// return a (big) string containing the contents of a template file with all
/// the variables interpolated. all the variables must be in the $var[] array or
return $template;
}
+/**
+ * Set a variable's value depending on whether or not it already has a value.
+ *
+ * If variable is set, set it to the set_value otherwise set it to the
+ * unset_value. used to handle checkboxes when you are expecting them from
+ * a form
+ * @param mixed $var Passed in by reference. The variable to check.
+ * @param mixed $set_value The value to set $var to if $var already has a value.
+ * @param mixed $unset_value The value to set $var to if $var does not already have a value.
+ */
function checked(&$var, $set_value = 1, $unset_value = 0) {
/// if variable is set, set it to the set_value otherwise set it to the
/// unset_value. used to handle checkboxes when you are expecting them from
}
}
+/**
+ * Prints the word "checked" if a variable is true, otherwise prints nothing,
+ * used for printing the word "checked" in a checkbox form element
+ * @param boolean $var Variable to be checked for true value
+ * @param string $true_value Value to be printed if $var is true
+ * @param string $false_value Value to be printed if $var is false
+ */
function frmchecked(&$var, $true_value = 'checked', $false_value = '') {
/// prints the word "checked" if a variable is true, otherwise prints nothing,
/// used for printing the word "checked" in a checkbox form input
}
}
-
+/**
+ * This function will create a HTML link that will work on both
+ * Javascript and non-javascript browsers.
+ * Relies on the Javascript function openpopup in javascript.php
+ * $url must be relative to home page eg /mod/survey/stuff.php
+ * @param string $url Web link relative to home page
+ * @param string $name Name to be assigned to the popup window
+ * @param string $linkname Text to be displayed as web link
+ * @param integer $height Height to assign to popup window
+ * @param integer $width Height to assign to popup window
+ * @param string $title Text to be displayed as popup page title
+ * @param string $options List of additional options for popup window
+ * @todo Add code examples and list of some options that might be used.
+ * @param boolean $return Should the link to the popup window be returned as a string (true) or printed immediately (false)?
+ * @return string
+ * @uses $CFG
+ */
function link_to_popup_window ($url, $name='popup', $linkname='click here',
$height=400, $width=500, $title='Popup window', $options='none', $return=false) {
/// This will create a HTML link that will work on both
$fullscreen = 0;
if (!(strpos($url,$CFG->wwwroot) === false)) { // some log url entries contain _SERVER[HTTP_REFERRER] in which case wwwroot is already there.
- $url = substr($url,strlen($CFG->wwwroot)+1);
+ $url = substr($url, strlen($CFG->wwwroot)+1);
}
$link = '<a target="'. $name .'" title="'. $title .'" href="'. $CFG->wwwroot . $url .'" '.
}
}
-
+/**
+ * This function will print a button submit form element
+ * that will work on both Javascript and non-javascript browsers.
+ * Relies on the Javascript function openpopup in javascript.php
+ * $url must be relative to home page eg /mod/survey/stuff.php
+ * @param string $url Web link relative to home page
+ * @param string $name Name to be assigned to the popup window
+ * @param string $linkname Text to be displayed as web link
+ * @param integer $height Height to assign to popup window
+ * @param integer $width Height to assign to popup window
+ * @param string $title Text to be displayed as popup page title
+ * @param string $options List of additional options for popup window
+ * @todo Add code examples and list of some options that might be used.
+ * @return string
+ * @uses $CFG
+ */
function button_to_popup_window ($url, $name='popup', $linkname='click here',
$height=400, $width=500, $title='Popup window', $options='none') {
/// This will create a HTML link that will work on both
}
+/**
+ * Prints a simple button to close a window
+ */
function close_window_button() {
/// Prints a simple button to close a window
$encodedurl = htmlentities($url);
if (empty($message)) {
echo '<meta http-equiv="refresh" content="'. $delay .'; url='. $encodedurl .'" />';
- echo '<script type="text/javascript">'. "\n" .'<!--'. "\n". 'location.replace(\'$url\');'. "\n". '//-->'. "\n". '</script>'; // To cope with Mozilla bug
+ echo '<script type="text/javascript">'. "\n" .'<!--'. "\n". "location.replace('$url');". "\n". '//-->'. "\n". '</script>'; // To cope with Mozilla bug
} else {
if (empty($delay)) {
$delay = 3; // There's no point having a message with no delay
echo '</center>';
flush();
sleep($delay);
- echo '<script type="text/javascript">'."\n".'<!--'."\n".'location.replace(\'$url\');'."\n".'//-->'."\n".'</script>'; // To cope with Mozilla bug
+ echo '<script type="text/javascript">'."\n".'<!--'."\nlocation.replace('$url');\n".'//-->'."\n".'</script>'; // To cope with Mozilla bug
}
die;
}