Source for file hotpotatoes.lib.php
Documentation is available at hotpotatoes.lib.php
DOKEOS - elearning and course management software
For a full list of contributors, see documentation/credits.html
This program 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 2
of the License, or (at your option) any later version.
See "documentation/licence.html" more details.
Rue des Palais 44 Paleizenstraat
B-1030 Brussels - Belgium
* Code library for HotPotatoes integration.
* @package dokeos.exercise
* @version $Id: hotpotatoes.lib.php 13384 2007-10-04 09:08:37Z elixir_inter $
* Creates a hotpotato directory
* If a directory of that name already exists, don't create any. If a file of that name exists, remove it and create a directory
* @param string Wanted path
* @return boolean Always true so far
//global $_course, $_user;
$documentPath= $baseWorkDir. '/';
$perm = octdec(!empty($perm)? $perm: '0770');
chmod ($documentPath,$perm);
//if this directory already exists, return false
//why create a .htaccess here?
//if (!is_file($documentPath.".htacces"))
// if (!($fp = fopen($documentPath.".htaccess", "w"))) {
// $str = "order deny,allow\nallow from all";
// if (!fwrite($fp,$str)) { }
* Gets the title of the quizz file given as parameter
* @param string File name
* @param string File path
* @return string The exercise title
if (!($fp = fopen($fpath. $fname, "r"))) {
//die("could not open Quiz input");
$pattern = array ( 1 => "title>", 2 => "/title>");
$s_contents = substr($contents,0,strpos($contents,$pattern["2"])- 1);
* Gets the comment about a file from the corresponding database record
* @param string File path
* @return string Comment from the database record
$query = "select comment from $dbTable where path='$path'";
* Sets the comment in the database for a particular path
* @param string File path
* @param string Comment to set
* @return string Result of the database operation (api_sql_query will output some message directly on error anyway)
$query = "update $dbTable set comment='$comment' where path='$path'";
* Get the name of the file from a path (without the extension)
* This assumes the path is made of elements split by '/', not '\' or '\\'
* @return string File name
$name = $name[sizeof($name)- 1];
* Reads the file contents into a string
* @param string Urlencoded path
* @return string The file contents
// if (!($fp = fopen($full_file_path, "r"))) {
* Writes the file contents into the file given
* @param string Urlencoded path
* @param string The file contents
//die("could not open Quiz input");
* Gets the name of an img whose path is given (without directories or extensions)
* @param string An image tag (<img src="...." ...>)
* @return string The image file name or an empty string
* @uses GetFileName No comment
{ // select src tag from img tag
//preg_match('/(src=(["\'])1.*(["\'])1)/i',$imgtag,$match); //src
preg_match('/src(\s)*=(\s)*[\'"]([^\'"]*)[\'"]/i',$imgtag,$match); //get the img src as contained between " or '
//list($key,$srctag)=each($match);
//$src = substr($srctag,5,(strlen($srctag)-7));
if (stristr($src,"http")=== false) // valid or invalid image name
else //the img tag contained "http", which means it is probably external. Ignore it.
* Gets the source path of an image tag
* @param string An image tag
* @return string The image source or ""
{ // select src tag from img tag
preg_match("|(src=\".*\" )|U",$imgtag,$match); //src
list ($key,$srctag)= each($match);
if (stristr($src,"http")== false) // valid or invalid image name
* Gets the image parameters from an image path
* @param string File name
* @param string File path
* @param reference Reference to a list of image parameters (emptied, then used to return results)
* @param reference Reference to a counter of images (emptied, then used to return results)
{ //select img tags from context
while (list ($int,$match)= each($matches))
//each match consists of a key and a value
while(list ($key,$imgtag)= each($match))
if ($imgname!= "" && !in_array($imgname,$imgparams)){
array_push($imgparams,$imgname); // name (+ type) of the images in the html test
$imgcount = $imgcount + 1; // number of images in the html test
* Generates a list of hidden fields with the image params given as parameter to this function
* @param array List of image parameters
* @return string String containing the hidden parameters built from the list given
while (list ($int,$string)= each($imgparams))
$list .= "<input type=\"hidden\" name=\"imgparams[]\" value=\"$string\" />\n";
* Searches for a node in the given array
* @param reference Reference to the array to search
* @param string Node we are looking for in the array
* @return mixed Node name or false if not found
for($i= 0;$i< count($array);$i++ )
if (!strcmp($array[$i],$node))
* Look for the image name into an array
* @param reference Reference to an array to search
* @param string String to look for
* @return mixed String given if found, false otherwise
* @uses myarraysearch This function is just an additional layer on the myarraysearch() function
* Replaces an image tag by ???
* @param string The content to replace
* @return string The modified content
while (list ($int,$match)= each($matches))
while(list ($key,$imgtag)= each($match))
if ($imgname== "") {} // valid or invalid image name
$newcontent = str_replace($prehref,$posthref,$newcontent);
* Fills the folder name up to a certain length with "0"
* @param string Original folder name
* @param integer Length to reach
* @return string Modified folder name
for($i= 0;$i < $nsize- strlen($name);$i++ )
* Generates the HotPotato folder tree
* @param string Folder path
* @return string Folder name (modified)
while (($file = readdir($dir)) !== false) {
$full_name = $folder. "/". $file;
//as long as we find the name in the array, continue looping. As soon as we have a new element, quit
if ($checked) { $w = 1; }
* Gets the folder name (strip down path)
* @return string Folder name stripped down
$name = $name[sizeof($name)- 2];
* Gets the folder path (withouth the name of the folder itself) ?
* @return string Path stripped down
for($i= 0;$i < sizeof($name)- 1; $i++ )
{ $str = $str. $name[$i]. "/"; }
* Checks if there are subfolders
* @return integer 1 if a subfolder was found, 0 otherwise
while (($file = readdir($dir)) !== false) {
$full_name = $folder. "/". $file;
$dflag = 1; // first directory
* Hotpotato Garbage Collector
* @return void No return value, but echoes results
while (($file = readdir($dir)) !== false) {
$full_name = $folder. "/". $file;
while (list ($key, $val) = each ($filelist))
if (stristr($val,$userID. ".t.html"))
echo $folder. "/". $val. "<br />";
|