dokeos-exercise
[ class tree: dokeos-exercise ] [ index: dokeos-exercise ] [ all elements ]

Source for file hotpotatoes.lib.php

Documentation is available at hotpotatoes.lib.php

  1. <?php
  2. /*
  3.     DOKEOS - elearning and course management software
  4.  
  5.     For a full list of contributors, see documentation/credits.html
  6.  
  7.     This program is free software; you can redistribute it and/or
  8.     modify it under the terms of the GNU General Public License
  9.     as published by the Free Software Foundation; either version 2
  10.     of the License, or (at your option) any later version.
  11.     See "documentation/licence.html" more details.
  12.  
  13.     Contact:
  14.         Dokeos
  15.         Rue des Palais 44 Paleizenstraat
  16.         B-1030 Brussels - Belgium
  17.         Tel. +32 (2) 211 34 56
  18. */
  19.  
  20.  
  21. /**
  22. *    Code library for HotPotatoes integration.
  23. *    @package dokeos.exercise
  24. *     @author Istvan Mandak
  25. *     @version $Id: hotpotatoes.lib.php 13384 2007-10-04 09:08:37Z elixir_inter $
  26. */
  27.  
  28.  
  29.  
  30. $dbTable     Database::get_course_table(TABLE_DOCUMENT);
  31.  
  32. /**
  33.  * Creates a hotpotato directory
  34.  *
  35.  * 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
  36.  * @param    string    Wanted path
  37.  * @return boolean    Always true so far
  38.  */
  39. function hotpotatoes_init($baseWorkDir)
  40. {
  41.     //global $_course, $_user;
  42.     $documentPath=$baseWorkDir.'/';
  43.     if (!is_dir($documentPath))
  44.     {
  45.         if (is_file($documentPath))
  46.         {
  47.             @unlink($documentPath);
  48.         }
  49.         @mkdir($documentPath);
  50.         $perm api_get_setting('permissions_for_new_directories');
  51.         $perm octdec(!empty($perm)?$perm:'0770');
  52.         chmod ($documentPath,$perm);
  53.         return true;
  54.     }else{
  55.         //if this directory already exists, return false
  56.         return false;
  57.     }
  58.     //why create a .htaccess here?
  59.     //if (!is_file($documentPath.".htacces"))
  60.     //{
  61.     //        if (!($fp = fopen($documentPath.".htaccess", "w"))) {
  62.     //    }
  63.     //    $str = "order deny,allow\nallow from all";
  64.     //    if (!fwrite($fp,$str)) { }
  65.     //}
  66. }
  67.  
  68. /**
  69.  * Gets the title of the quizz file given as parameter
  70.  * @param    string    File name
  71.  * @param    string    File path
  72.  * @return    string    The exercise title
  73.  */
  74. function GetQuizName($fname,$fpath)
  75. {
  76.  
  77.     $title "";
  78.     $title GetComment($fname);
  79.  
  80.     if($title=="")
  81.     {
  82.         if (!($fp fopen($fpath.$fname"r"))) {
  83.         //die("could not open Quiz input");
  84.             return GetFileName($fname);
  85.         }
  86.  
  87.         $contents fread($fpfilesize($fpath.$fname));
  88.         fclose($fp);
  89.  
  90.         $contents strtolower($contents);
  91.  
  92.         $pattern array => "title>"=> "/title>");
  93.  
  94.         $s_contents substr($contents,0,strpos($contents,$pattern["2"])-1);
  95.         $e_contents substr($s_contents,strpos($contents,$pattern["1"])+strlen($pattern["1"]),strlen($s_contents));
  96.  
  97.         $title $e_contents;
  98.     }
  99.     return $title;
  100.  
  101. }
  102.  
  103. /**
  104.  * Gets the comment about a file from the corresponding database record
  105.  * @param    string    File path
  106.  * @return    string    Comment from the database record
  107.  */
  108. function GetComment($path)
  109. {
  110.     global $dbTable;
  111.     $query "select comment from $dbTable where path='$path'";
  112.     $result api_sql_query($query,__FILE__,__LINE__);
  113.     while($row mysql_fetch_array($result))
  114.     {
  115.         return $row[0];
  116.     }
  117.     return "";
  118. }
  119.  
  120. /**
  121.  * Sets the comment in the database for a particular path
  122.  * @param    string    File path
  123.  * @param    string    Comment to set
  124.  * @return    string    Result of the database operation (api_sql_query will output some message directly on error anyway)
  125.  */
  126. function SetComment($path,$comment)
  127. {
  128.     global $dbTable;
  129.     $query "update $dbTable set comment='$comment' where path='$path'";
  130.     $result api_sql_query($query,__FILE__,__LINE__);
  131.     return "$result";
  132. }
  133.  
  134. /**
  135.  * Get the name of the file from a path (without the extension)
  136.  *
  137.  * This assumes the path is made of elements split by '/', not '\' or '\\'
  138.  * @param    string    Path
  139.  * @return    string    File name
  140.  */
  141. function GetFileName($fname)
  142. {
  143.     $name explode('/',$fname);
  144.     $name $name[sizeof($name)-1];
  145.     return $name;
  146. }
  147.  
  148. /**
  149.  * Reads the file contents into a string
  150.  * @param    string    Urlencoded path
  151.  * @return    string    The file contents
  152.  */
  153. function ReadFileCont($full_file_path)
  154. {
  155.     if (!($fp fopen(urldecode($full_file_path)"r"))) {
  156. //    if (!($fp = fopen($full_file_path, "r"))) {
  157.         return "";
  158.     }
  159.     $contents fread($fpfilesize($full_file_path));
  160.     fclose($fp);
  161.     return $contents;
  162. }
  163.  
  164. /**
  165.  * Writes the file contents into the file given
  166.  * @param    string    Urlencoded path
  167.  * @param     string    The file contents
  168.  */
  169. function WriteFileCont($full_file_path,$content)
  170. {
  171.     if (!($fp fopen(urldecode($full_file_path)"w"))) {
  172.         //die("could not open Quiz input");
  173.     }
  174.     fwrite($fp,$content);
  175.     fclose($fp);
  176. }
  177.  
  178. /**
  179.  * Gets the name of an img whose path is given (without directories or extensions)
  180.  * @param    string    An image tag (<img src="...." ...>)
  181.  * @return    string    The image file name or an empty string
  182.  * @uses GetFileName No comment
  183.  */
  184. function GetImgName($imgtag)
  185. {    // select src tag from img tag
  186.     $match array();
  187.     //preg_match('/(src=(["\'])1.*(["\'])1)/i',$imgtag,$match);            //src
  188.     preg_match('/src(\s)*=(\s)*[\'"]([^\'"]*)[\'"]/i',$imgtag,$match)//get the img src as contained between " or '
  189.     //list($key,$srctag)=each($match);
  190.     $src=$match[3];
  191.     //$src = substr($srctag,5,(strlen($srctag)-7));
  192.     if (stristr($src,"http")===false)                                                            // valid or invalid image name
  193.     {
  194.         if($src=="")
  195.         {
  196.             return "";
  197.         }
  198.         else
  199.         {
  200.             $tmp_src GetFileName($src;
  201.             if ($tmp_src == "")
  202.             {
  203.                 return $src;
  204.             }
  205.             else
  206.             {
  207.                 return $tmp_src;
  208.             }
  209.         }
  210.     }
  211.     else //the img tag contained "http", which means it is probably external. Ignore it.
  212.     {
  213.         return "";
  214.     }
  215. }
  216.  
  217. /**
  218.  * Gets the source path of an image tag
  219.  * @param    string    An image tag
  220.  * @return    string    The image source or ""
  221.  */
  222. function GetSrcName($imgtag)
  223. {    // select src tag from img tag
  224.     $match array();
  225.     preg_match("|(src=\".*\" )|U",$imgtag,$match);            //src
  226.     list($key,$srctag)=each($match);
  227.     $src substr($srctag,5,(strlen($srctag)-7));
  228.     if (stristr($src,"http")==false)                                                            // valid or invalid image name
  229.     {
  230.         return $src;
  231.     }
  232.     else
  233.     {
  234.         return '';
  235.  
  236.     }
  237. }
  238.  
  239. /**
  240.  * Gets the image parameters from an image path
  241.  * @param    string    File name
  242.  * @param    string    File path
  243.  * @param  reference    Reference to a list of image parameters (emptied, then used to return results)
  244.  * @param    reference    Reference to a counter of images (emptied, then used to return results)
  245.  */
  246. function GetImgParams($fname,$fpath,&$imgparams,&$imgcount)
  247. {    //select img tags from context
  248.     $imgparams array();
  249.     //phpinfo();
  250.     $contents ReadFileCont("$fpath"."$fname");
  251.     $matches array();
  252.     preg_match_all("(<img .*>)",$contents,$matches);
  253.     $imgcount 0;
  254.     while (list($int,$match)=each($matches))
  255.     {
  256.         //each match consists of a key and a value
  257.         while(list($key,$imgtag)=each($match))
  258.         {
  259.             $imgname GetImgName($imgtag);
  260.             if ($imgname!="" && !in_array($imgname,$imgparams)){
  261.                 array_push($imgparams,$imgname);  // name (+ type) of the images in the html test
  262.                 $imgcount $imgcount 1;               // number of images in the html test
  263.             }
  264.         }
  265.     }
  266. }
  267.  
  268. /**
  269.  * Generates a list of hidden fields with the image params given as parameter to this function
  270.  * @param    array        List of image parameters
  271.  * @return    string    String containing the hidden parameters built from the list given
  272.  */
  273. function GenerateHiddenList($imgparams)
  274. {
  275.     $list "";
  276.     if(is_array($imgparams)){
  277.        while (list($int,$string)=each($imgparams))
  278.         {
  279.             $list .= "<input type=\"hidden\" name=\"imgparams[]\" value=\"$string\" />\n";
  280.         }
  281.     }
  282.     return $list;
  283. }
  284.  
  285. /**
  286.  * Searches for a node in the given array
  287.  * @param    reference    Reference to the array to search
  288.  * @param    string        Node we are looking for in the array
  289.  * @return    mixed            Node name or false if not found
  290.  */
  291. function myarraysearch(&$array,$node)
  292. {
  293.     $match FALSE;
  294.     $tmp_array array();
  295.     for($i=0;$i<count($array);$i++)
  296.     {
  297.         if (!strcmp($array[$i],$node))
  298.             {
  299.                 $match $node;
  300.             }
  301.         else
  302.             array_push($tmp_array,$array[$i])}
  303.     }
  304.     $array $tmp_array;
  305.     return $match;
  306. }
  307.  
  308. /**
  309.  * Look for the image name into an array
  310.  * @param    reference    Reference to an array to search
  311.  * @param    string        String to look for
  312.  * @return    mixed            String given if found, false otherwise
  313.  * @uses        myarraysearch    This function is just an additional layer on the myarraysearch() function
  314.  */
  315. function CheckImageName(&$imgparams,$string)
  316. {
  317.     $checked myarraysearch($imgparams,$string);
  318.     return $checked;
  319. }
  320.  
  321. /**
  322.  * Replaces an image tag by ???
  323.  * @param    string    The content to replace
  324.  * @return    string    The modified content
  325.  */
  326. function ReplaceImgTag($content)
  327. {
  328.     $newcontent $content;
  329.     $matches array();
  330.     preg_match_all("(<img .*>)",$content,$matches);
  331.     $imgcount 0;
  332.     while (list($int,$match)=each($matches))
  333.     {
  334.         while(list($key,$imgtag)=each($match))
  335.         {
  336.             $imgname GetSrcName($imgtag);
  337.             if ($imgname==""{}                                // valid or invalid image name
  338.             else {
  339.  
  340.                 $prehref $imgname;
  341.                 $posthref GetFileName($imgname);
  342.                 $newcontent str_replace($prehref,$posthref,$newcontent);
  343.             }
  344.         }
  345.     }
  346.     return $newcontent;
  347. }
  348.  
  349. /**
  350.  * Fills the folder name up to a certain length with "0"
  351.  * @param    string    Original folder name
  352.  * @param    integer    Length to reach
  353.  * @return    string    Modified folder name
  354.  */
  355. function FillFolderName($name,$nsize)
  356. {
  357.     $str "";
  358.     for($i=0;$i $nsize-strlen($name);$i++)
  359.     {
  360.         $str .= "0";
  361.     }
  362.     $str .= $name;
  363.     return $str;
  364. }
  365.  
  366. /**
  367.  * Generates the HotPotato folder tree
  368.  * @param    string    Folder path
  369.  * @return    string    Folder name (modified)
  370.  */
  371. function GenerateHpFolder($folder)
  372. {
  373.     $filelist array();
  374.     if ($dir @opendir($folder)) {
  375.         while (($file readdir($dir)) !== false{
  376.             if $file != "."{
  377.                 if ($file != "..")
  378.                 {
  379.                     $full_name $folder."/".$file;
  380.                     if (is_dir($full_name))
  381.                     {
  382.                         $filelist[$file;
  383.                     }
  384.                }
  385.             }
  386.         }
  387.     }
  388.     $w 0;
  389.     do {
  390.         $name FillFolderName(mt_rand(1,99999),6);
  391.         $checked myarraysearch($filelist,$name);
  392.         //as long as we find the name in the array, continue looping. As soon as we have a new element, quit
  393.         if ($checked$w 1;    }
  394.         else $w 0}
  395.     while ($w==1);
  396.  
  397.     return $name;
  398. }
  399.  
  400. /**
  401.  * Gets the folder name (strip down path)
  402.  * @param    string    Path
  403.  *    @return    string    Folder name stripped down
  404.  */
  405. function GetFolderName($fname)
  406. {
  407.     $name explode('/',$fname);
  408.     $name $name[sizeof($name)-2];
  409.     return $name;
  410. }
  411.  
  412. /**
  413.  * Gets the folder path (withouth the name of the folder itself) ?
  414.  * @param    string    Path
  415.  * @return    string    Path stripped down
  416.  */
  417. function GetFolderPath($fname)
  418. {
  419.     $str "";
  420.     $name explode('/',$fname);
  421.     for($i=0;$i sizeof($name)-1$i++)
  422.         $str $str.$name[$i]."/"}
  423.     return $str;
  424. }
  425.  
  426. /**
  427.  * Checks if there are subfolders
  428.  * @param    string    Path
  429.  * @return    integer    1 if a subfolder was found, 0 otherwise
  430.  */
  431. function CheckSubFolder($path)
  432. {
  433.     $folder GetFolderPath($path);
  434.     $dflag 0;
  435.     if ($dir @opendir($folder)) {
  436.         while (($file readdir($dir)) !== false{
  437.             if $file != "."{
  438.                 if ($file != ".."{
  439.                     $full_name $folder."/".$file;
  440.                     if (is_dir($full_name)) {
  441.                         $dflag 1;    // first directory
  442.                     }
  443.                 }
  444.             }
  445.         }
  446.     }
  447.     return $dflag;
  448. }
  449.  
  450. /**
  451.  * Hotpotato Garbage Collector
  452.  * @param    string    Path
  453.  * @param    integer    Flag
  454.  * @param    integer    User id
  455.  * @return    void    No return value, but echoes results
  456.  */
  457. function HotPotGCt($folder,$flag,$userID)
  458. // Garbage Collector
  459.     $filelist array();
  460.     if ($dir @opendir($folder)) {
  461.         while (($file readdir($dir)) !== false{
  462.             if $file != "."{
  463.                 if ($file != "..")
  464.                 {
  465.                     $full_name $folder."/".$file;
  466.                     if (is_dir($full_name))
  467.                     {
  468.                         HotPotGCt($folder."/".$file,$flag,$userID);
  469.                     }
  470.                     else
  471.                     {
  472.                         $filelist[$file;
  473.                     }
  474.                }
  475.             }
  476.         }
  477.         closedir($dir);
  478.     }
  479.     while (list ($key$valeach ($filelist))
  480.     {
  481.         if (stristr($val,$userID.".t.html"))
  482.         {
  483.             if ($flag == 1)
  484.             {
  485.                 my_delete($folder."/".$val);
  486.             }
  487.             else
  488.             {
  489.                 echo $folder."/".$val."<br />";
  490.             }
  491.         }
  492.     }
  493. }
  494. ?>

Documentation generated on Thu, 12 Jun 2008 13:39:08 -0500 by phpDocumentor 1.4.1