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

Source for file dropbox_functions.inc.php

Documentation is available at dropbox_functions.inc.php

  1. <?php
  2. /*
  3. ==============================================================================
  4.     Dokeos - elearning and course management software
  5.  
  6.     Copyright (c) 2006-2008 Dokeos SPRL
  7.     Copyright (c) 2006 Ghent University (UGent)
  8.     Copyright (c) various contributors
  9.  
  10.     For a full list of contributors, see "credits.txt".
  11.     The full license can be read in "license.txt".
  12.  
  13.     This program is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU General Public License
  15.     as published by the Free Software Foundation; either version 2
  16.     of the License, or (at your option) any later version.
  17.  
  18.     See the GNU General Public License for more details.
  19.  
  20.     Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  21.     Mail: info@dokeos.com
  22. ==============================================================================
  23. */
  24.  
  25. /**
  26. * This file contains additional dropbox functions. Initially there were some
  27. * functions in the init files also but I have moved them over
  28. * to one file         -- Patrick Cool <patrick.cool@UGent.be>, Ghent University
  29. */
  30.  
  31. /**
  32. * This function is a wrapper function for the multiple actions feature.
  33. @return    Mixed    If there is a problem, return a string message, otherwise nothing
  34. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  35. @version march 2006
  36. */
  37. {
  38.     global $_user$is_courseAdmin$is_courseTutor;
  39.  
  40.     // STEP 1: are we performing the actions on the received or on the sent files?
  41.     if($_POST['action']=='delete_received' || $_POST['action']=='download_received')
  42.     {
  43.         $part 'received';
  44.     }
  45.     elseif($_POST['action']=='delete_sent' || $_POST['action']=='download_sent')
  46.     {
  47.         $part 'sent';
  48.     }
  49.  
  50.     // STEP 2: at least one file has to be selected. If not we return an error message
  51.     foreach ($_POST as $key=>$value)
  52.     {
  53.         if (strstr($value,$part.'_'AND $key!='view_received_category' AND $key!='view_sent_category')
  54.         {
  55.             $checked_files=true;
  56.             $checked_file_ids[]=intval(substr($value,strrpos($value,'_')));
  57.         }
  58.     }
  59.     $checked_file_ids $_POST['id'];
  60.  
  61.     if (!is_array($checked_file_ids|| count($checked_file_ids)==0)
  62.     {
  63.         return get_lang('CheckAtLeastOneFile');
  64.     }
  65.  
  66.  
  67.     // STEP 3A: deleting
  68.     if ($_POST['action']=='delete_received' || $_POST['action']=='delete_sent')
  69.     {
  70.         $dropboxfile=new Dropbox_Person$_user['user_id']$is_courseAdmin$is_courseTutor);
  71.         foreach ($checked_file_ids as $key=>$value)
  72.         {
  73.             if ($_GET['view']=='received' OR !$_GET['view'])
  74.             {
  75.                 $dropboxfile->deleteReceivedWork($value);
  76.                 $message=get_lang('ReceivedFileDeleted');
  77.             }
  78.             if ($_GET['view']=='sent')
  79.             {
  80.                 $dropboxfile->deleteSentWork($value);
  81.                 $message=get_lang('SentFileDeleted');
  82.             }
  83.         }
  84.         return $message;
  85.     }
  86.  
  87.     // STEP 3B: giving comment
  88.     if ($_POST['actions']=='comment')
  89.     {
  90.         // This has not been implemented.
  91.         // The idea was that it would be possible to write the same feedback for the selected documents.
  92.     }
  93.  
  94.     // STEP 3C: moving
  95.     if (strstr($_POST['actions']'move_'))
  96.     {
  97.         $target=str_replace('move_''',$_POST['actions']);
  98.  
  99.         foreach ($checked_file_ids as $key=>$value)
  100.         {
  101.             store_move($value$target$part);
  102.         }
  103.         return get_lang('FilesMoved');
  104.     }
  105.  
  106.     // STEP 3D: downloading
  107.     if ($_POST['action']=='download_sent' || $_POST['action']=='download_received')
  108.     {
  109.         zip_download($checked_file_ids);
  110.     }
  111.  
  112. }
  113.  
  114. /**
  115. * This function deletes a dropbox category
  116. *
  117. @todo give the user the possibility what needs to be done with the files in this category: move them to the root, download them as a zip, delete them
  118. *
  119. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  120. @version march 2006
  121. */
  122. function delete_category($action$id)
  123. {
  124.     global $dropbox_cnf;
  125.     global $_user$is_courseAdmin$is_courseTutor;
  126.  
  127.     // an additional check that might not be necessary
  128.     if ($action=='deletereceivedcategory')
  129.     {
  130.         $sentreceived='received';
  131.         $entries_table=$dropbox_cnf['tbl_post'];
  132.         $id_field='file_id';
  133.     }
  134.     elseif ($action=='deletesentcategory')
  135.     {
  136.         $sentreceived='sent';
  137.         $entries_table=$dropbox_cnf['tbl_file'];
  138.         $id_field='id';
  139.     }
  140.     else
  141.     {
  142.         return get_lang('Error');
  143.     }
  144.  
  145.     // step 1: delete the category
  146.     $sql="DELETE FROM ".$dropbox_cnf['tbl_category']." WHERE cat_id='".Database::escape_string($id)."' AND $sentreceived='1'";
  147.     $result=api_sql_query($sql);
  148.  
  149.     // step 2: delete all the documents in this category
  150.     $sql="SELECT * FROM ".$entries_table." WHERE cat_id='".Database::escape_string($id)."'";
  151.     $result=api_sql_query($sql);
  152.  
  153.     while ($row=mysql_fetch_array($result))
  154.     {
  155.         $dropboxfile=new Dropbox_Person$_user['user_id']$is_courseAdmin$is_courseTutor);
  156.         if ($action=='deletereceivedcategory')
  157.         {
  158.             $dropboxfile->deleteReceivedWork($row[$id_field]);
  159.         }
  160.         if ($action=='deletesentcategory')
  161.         {
  162.             $dropboxfile->deleteSentWork($row[$id_field]);
  163.         }
  164.     }
  165. }
  166.  
  167. /**
  168. * Displays the form to move one individual file to a category
  169. *
  170. @return html code of the form that appears in a dokeos message box.
  171. *
  172. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  173. @version march 2006
  174. */
  175. function display_move_form($part$id$target=array())
  176. {
  177.     $message=get_lang('MoveFileTo');
  178.     $message.='<form name="form1" method="post" action="'.api_get_self().'?view_received_category='.$_GET['view_received_category'].'&view_sent_category='.$_GET['view_sent_category'].'&view='.$_GET['view'].'">';
  179.     $message.='<input type="hidden" name="id" value="'.$id.'">';
  180.     $message.='<input type="hidden" name="part" value="'.$part.'">';
  181.     $message.='<select name="move_target">';
  182.     $message.='<option value="0">'.get_lang('Root').'</option>';
  183.     foreach ($target as $key=>$category)
  184.     {
  185.         $message.='<option value="'.$category['cat_id'].'">'.$category['cat_name'].'</option>';
  186.     }
  187.     $message.= '</select>';
  188.     $message.='<input type="submit" name="do_move" value="'.get_lang('Ok').'">';
  189.     $message.='</form>';
  190.     Display :: display_normal_message($message,false);
  191. }
  192.  
  193. /**
  194. * This function moves a file to a different category
  195. *
  196. @param $id the id of the file we are moving
  197. @param $target the id of the folder we are moving to
  198. @param $part are we moving a received file or a sent file?
  199. *
  200. @return language string
  201. *
  202. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  203. @version march 2006
  204. */
  205. function store_move($id$target$part)
  206. {
  207.     global $_user;
  208.     global $dropbox_cnf;
  209.  
  210.     if ((isset($idAND $id<>''AND (isset($targetAND $target<>''AND (isset($partAND $part<>''))
  211.     {
  212.         if ($part=='received')
  213.         {
  214.             $sql="UPDATE ".$dropbox_cnf["tbl_post"]." SET cat_id='".Database::escape_string($target)."'
  215.                         WHERE dest_user_id='".Database::escape_string($_user['user_id'])."'
  216.                         AND file_id='".Database::escape_string($id)."'
  217.                         ";
  218.             api_sql_query($sql,__FILE__,__LINE__);
  219.             $return_message=get_lang('ReceivedFileMoved');
  220.         }
  221.         if ($part=='sent')
  222.         {
  223.             $sql="UPDATE ".$dropbox_cnf["tbl_file"]." SET cat_id='".Database::escape_string($target)."'
  224.                         WHERE uploader_id='".Database::escape_string($_user['user_id'])."'
  225.                         AND id='".Database::escape_string($id)."'
  226.                         ";
  227.             api_sql_query($sql,__FILE__,__LINE__);
  228.             $return_message=get_lang('SentFileMoved');
  229.         }
  230.     }
  231.     else
  232.     {
  233.         $return_message=get_lang('NotMovedError');
  234.     }
  235.     return $return_message;
  236. }
  237.  
  238. /**
  239. * This functions displays all teh possible actions that can be performed on multiple files. This is the dropdown list that
  240. * appears below the sortable table of the sent / or received files.
  241. *
  242. @return html value for the dropdown list
  243. *
  244. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  245. @version march 2006
  246. */
  247. function display_action_options($part$categories$current_category=0)
  248. {
  249.     echo '<select name="actions">';
  250.     echo '<option value="download">'.get_lang('Download').'</option>';
  251.     echo '<option value="delete">'.get_lang('Delete').'</option>';
  252.     if(is_array($categories))
  253.     {
  254.         echo '<optgroup label="'.get_lang('MoveTo').'">';
  255.         if ($current_category<>0)
  256.         {
  257.             echo '<option value="move_0">'.get_lang('Root').'</a>';
  258.         }
  259.         foreach ($categories as $key=>$value)
  260.         {
  261.             if$current_category<>$value['cat_id'])
  262.             {
  263.                 echo '<option value="move_'.$value['cat_id'].'">'.$value['cat_name'].'</option>';
  264.             }
  265.         }
  266.         echo '</optgroup>';
  267.     }
  268.     echo '</select>';
  269.     echo '<input type="submit" name="do_actions_'.$part.'" value="'.get_lang('Ok').'" />';
  270. }
  271.  
  272. /**
  273. * this function returns the html code that displays the checkboxes next to the files so that
  274. * multiple actions on one file are possible.
  275. *
  276. @param $id the unique id of the file
  277. @param $part are we dealing with a sent or with a received file?
  278. *
  279. @return html code
  280. *
  281. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  282. @version march 2006
  283. */
  284. function display_file_checkbox($id$part)
  285. {
  286.     if (isset($_GET['selectall']))
  287.     {
  288.         $checked='checked';
  289.     }
  290.     $return_value='<input type="checkbox" name="'.$part.'_'.$id.'" value="'.$id.'" '.$checked.' />';
  291.     return $return_value;
  292. }
  293.  
  294.  
  295. /**
  296. * This function retrieves all the dropbox categories and returns them as an array
  297. *
  298. @param $filter default '', when we need only the categories of the sent or the received part.
  299. *
  300. @return array 
  301. *
  302. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  303. @version march 2006
  304. */
  305. function get_dropbox_categories($filter='')
  306. {
  307.     global $_user;
  308.     global $dropbox_cnf;
  309.  
  310.     echo '<h1>'.$filter.'</h1>';
  311.  
  312.     $return_array=array();
  313.  
  314.     $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE user_id='".$_user['user_id']."'";
  315.  
  316.     $result=api_sql_query($sql);
  317.     while ($row=mysql_fetch_array($result))
  318.     {
  319.         if(($filter=='sent' AND $row['sent']==1OR ($filter=='received' AND $row['received']==1OR $filter=='')
  320.         {
  321.             $return_array[$row['cat_id']]=$row;
  322.         }
  323.     }
  324.  
  325.     return $return_array;
  326. }
  327.  
  328. /**
  329. * This functions stores a new dropboxcategory
  330. *
  331. @var     it might not seem very elegant if you create a category in sent and in received with the same name that you get two entries in the
  332. *         dropbox_category table but it is the easiest solution. You get
  333. *         cat_name | received | sent | user_id
  334. *         test     |      1        |    0  |    237
  335. *         test     |      0        |    1  |    237
  336. *         more elegant would be
  337. *         test     |      1        |    1  |    237
  338. *
  339. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  340. @version march 2006
  341. */
  342. function store_addcategory()
  343. {
  344.     global $_user;
  345.     global $dropbox_cnf;
  346.  
  347.     // check if the target is valid
  348.     if ($_POST['target']=='sent')
  349.     {
  350.         $sent=1;
  351.         $received=0;
  352.     }
  353.     elseif ($_POST['target']=='received')
  354.     {
  355.         $sent=0;
  356.         $received=1;
  357.     }
  358.     else
  359.     {
  360.         return get_lang('Error');
  361.     }
  362.  
  363.     // check if the category name is valid
  364.     if ($_POST['category_name']=='')
  365.     {
  366.         return get_lang('ErrorPleaseGiveCategoryName');
  367.     }
  368.  
  369.     if (!$_POST['edit_id'])
  370.     {
  371.         // step 3a, we check if the category doesn't already exist
  372.         $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE user_id='".$_user['user_id']."' AND cat_name='".Database::escape_string($_POST['category_name'])."' AND received='".$received."' AND sent='".$sent."'";
  373.         $result=api_sql_query($sql);
  374.  
  375.  
  376.         // step 3b, we add the category if it does not exist yet.
  377.         if (mysql_num_rows($result)==0)
  378.         {
  379.             $sql="INSERT INTO ".$dropbox_cnf['tbl_category']." (cat_name, received, sent, user_id)
  380.                     VALUES ('".Database::escape_string($_POST['category_name'])."', '".Database::escape_string($received)."', '".Database::escape_string($sent)."', '".Database::escape_string($_user['user_id'])."')";
  381.             api_sql_query($sql);
  382.             return get_lang('CategoryStored');
  383.         }
  384.         else
  385.         {
  386.             return get_lang('CategoryAlreadyExistsEditIt');
  387.         }
  388.     }
  389.     else
  390.     {
  391.         $sql="UPDATE ".$dropbox_cnf['tbl_category']." SET cat_name='".Database::escape_string($_POST['category_name'])."', received='".Database::escape_string($received)."' , sent='".Database::escape_string($sent)."'
  392.                 WHERE user_id='".Database::escape_string($_user['user_id'])."'
  393.                 AND cat_id='".Database::escape_string($_POST['edit_id'])."'";
  394.         api_sql_query($sql);
  395.         return get_lang('CategoryModified');
  396.     }
  397. }
  398.  
  399. /**
  400. * This function displays the form to add a new category.
  401. *
  402. @param $category_name this parameter is the name of the category (used when no section is selected)
  403. @param $id this is the id of the category we are editing.
  404. *
  405. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  406. @version march 2006
  407. */
  408. function display_addcategory_form($category_name=''$id='')
  409. {
  410.     global $dropbox_cnf;
  411.  
  412.     $title=get_lang('AddNewCategory');
  413.  
  414.     if (isset($idAND $id<>'')
  415.     {
  416.         // retrieve the category we are editing
  417.         $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE cat_id='".Database::escape_string($id)."'";
  418.         $result=api_sql_query($sql);
  419.         $row=mysql_fetch_array($result);
  420.  
  421.         if ($category_name==''// after an edit with an error we do not want to return to the original name but the name we already modified. (happens when createinrecievedfiles AND createinsentfiles are not checked)
  422.         {
  423.             $category_name=$row['cat_name'];
  424.         }
  425.         if ($row['received']=='1')
  426.         {
  427.             $target='received';
  428.         }
  429.         if ($row['sent']=='1')
  430.         {
  431.             $target='sent';
  432.         }
  433.         $title=get_lang('EditCategory');
  434.  
  435.     }
  436.  
  437.     if ($_GET['action']=='addreceivedcategory')
  438.     {
  439.         $target='received';
  440.     }
  441.     if ($_GET['action']=='addsentcategory')
  442.     {
  443.         $target='sent';
  444.     }
  445.  
  446.  
  447.     echo "<form name=\"add_new_category\" method=\"post\" action=\"".api_get_self()."?view=".$_GET['view']."\">\n";
  448.     echo '<strong>'.$title.'</strong>';
  449.     if (isset($idAND $id<>'')
  450.     {
  451.         echo '<input name="edit_id" type="hidden" value="'.$id.'">';
  452.     }
  453.     echo '<input name="target" type="hidden" value="'.$target.'">';
  454.     echo "<table border=\"0\">\n";
  455.     echo "\t<tr>\n";
  456.     echo "\t<td>\n";
  457.     echo get_lang('CategoryName').': ';
  458.     echo "\t</td>\n";
  459.     echo "\t<td>\n";
  460.     echo "<input type=\"text\" name=\"category_name\" value=\"".$category_name."\" />";
  461.     echo "\t</td>\n";
  462.     echo "\t</tr>\n";
  463.     echo "\t<tr>\n";
  464.     echo "\t<td valign=\"top\">\n";
  465.     echo "\t</td>\n";
  466.     echo "\t<td>\n";
  467.     echo "<input type=\"submit\" name=\"StoreCategory\" value=\"".get_lang('Ok')."\">";
  468.     echo "\t</td>\n";
  469.     echo "\t</tr>\n";
  470.     echo "</table>\n";
  471.     echo "</form>";
  472. }
  473.  
  474. /**
  475. * this function displays the form to upload a new item to the dropbox.
  476. *
  477. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  478. @version march 2006
  479. */
  480. function display_add_form()
  481. {
  482.     global $_user;
  483.     global $is_courseAdmin;
  484.     global $is_courseTutor;
  485.     global $course_info// this
  486.  
  487.     $token Security::get_token();
  488.     $dropbox_person new Dropbox_Person$_user['user_id']$is_courseAdmin$is_courseTutor);
  489.     ?>
  490.     <form method="post" action="index.php?view_received_category=<?php echo $_GET['view_received_category']?>&view_sent_category=<?php echo $_GET['view_sent_category']?>&view=<?php echo $_GET['view']?>&<?php echo "&origin=$origin"?>" enctype="multipart/form-data" onsubmit="return checkForm(this)">
  491.     <table border="0">
  492.         <tr>
  493.             <td  align="right">
  494.                 <?php echo dropbox_lang("uploadFile")?>:
  495.             </td>
  496.             <td>
  497.                 <input type="hidden" name="MAX_FILE_SIZE" value='<?php echo dropbox_cnf("maxFilesize")?>' />
  498.                 <input type="file" name="file" size="20" <?php if (dropbox_cnf("allowOverwrite")) echo 'onChange="checkfile(this.value)"'?> />
  499.                 <input type="hidden" name="dropbox_unid" value="<?php echo $dropbox_unid?>" />
  500.                 <input type="hidden" name="sec_token" value="<?php echo $token?>" />
  501.                 <?php
  502.                 if ($origin=='learnpath')
  503.                 {
  504.                     echo "<input type='hidden' name='origin' value='learnpath' />";
  505.                 }
  506.                 ?>
  507.             </td>
  508.         </tr>
  509.     <?php
  510.     if (dropbox_cnf("allowOverwrite"))
  511.     {
  512.         ?>
  513.         <tr id="overwrite" style="display: none">
  514.             <td valign="top"  align="right">
  515.             </td>
  516.             <td>
  517.                 <input type="checkbox" name="cb_overwrite" id="cb_overwrite" value="true" /><?php echo dropbox_lang("overwriteFile")?>
  518.             </td>
  519.         </tr>
  520.         <?php
  521.     }
  522.     ?>
  523.         <tr>
  524.             <td valign="top"  align="left">
  525.                 <?php echo dropbox_lang("description")?>:
  526.             </td>
  527.             <td>
  528.                 <textarea name="description" cols="24" rows="2" style="width: 350px;"></textarea>
  529.             </td>
  530.         </tr>
  531.         <tr>
  532.             <td valign="top"  align="left">
  533.                 <?php echo dropbox_lang("sendTo")?>:
  534.             </td>
  535.             <td valign="top"  align="left">
  536.  
  537.     <?php
  538.  
  539.     //list of all users in this course and all virtual courses combined with it
  540.     if(isset($_SESSION['id_session'])){
  541.         $complete_user_list_for_dropbox CourseManager :: get_user_list_from_course_code($course_info['code'],true,$_SESSION['id_session']);
  542.         $complete_user_list2 CourseManager :: get_coach_list_from_course_code($course_info['code'],$_SESSION['id_session']);
  543.         $complete_user_list_for_dropbox array_merge($complete_user_list_for_dropbox,$complete_user_list2);
  544.     }
  545.     else{
  546.         $complete_user_list_for_dropbox CourseManager :: get_user_list_from_course_code($course_info['code']);
  547.     }
  548.  
  549.     foreach ($complete_user_list_for_dropbox as $k => $e)
  550.         $complete_user_list_for_dropbox[$k$e +
  551.             array('lastcommafirst' => $e['lastname'', ' $e['firstname']);
  552.  
  553.     $complete_user_list_for_dropbox TableSort::sort_table($complete_user_list_for_dropbox'lastcommafirst');
  554.  
  555.     ?>
  556.  
  557.                 <select name="recipients[]" size="
  558.     <?php
  559.         if $dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin)
  560.         {
  561.             echo 10;
  562.         }
  563.         else
  564.         {
  565.             echo 6;
  566.         }
  567.  
  568.  
  569.     ?>" multiple style="width: 350px;">
  570.     <?php
  571.  
  572.     /*
  573.         Create the options inside the select box:
  574.         List all selected users their user id as value and a name string as display
  575.     */
  576.     foreach ($complete_user_list_for_dropbox as $current_user)
  577.     {
  578.         if ( ($dropbox_person -> isCourseTutor
  579.         || $dropbox_person -> isCourseAdmin
  580.         || dropbox_cnf("allowStudentToStudent")    // RH: also if option is set
  581.         || $current_user['status']!=5                // always allow teachers
  582.         || $current_user['tutor_id']==1                // always allow tutors
  583.         && $current_user['user_id'!= $_user['user_id')     // don't include yourself
  584.         {
  585.             $full_name $current_user['lastcommafirst'];
  586.             echo '<option value="user_' $current_user['user_id''">' $full_name '</option>';
  587.         }
  588.     }
  589.  
  590.     /*
  591.     * Show groups
  592.     */
  593.     if ( ($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin)
  594.     && dropbox_cnf("allowGroup"|| dropbox_cnf("allowStudentToStudent"))
  595.     {
  596.         $complete_group_list_for_dropbox GroupManager::get_group_list(null,dropbox_cnf("courseId"));
  597.  
  598.         if (count($complete_group_list_for_dropbox0)
  599.         {
  600.             foreach ($complete_group_list_for_dropbox as $current_group)
  601.             {
  602.                 if ($current_group['number_of_members'0)
  603.                 {
  604.                     echo '<option value="group_'.$current_group['id'].'">G: '.$current_group['name'].' - '.$current_group['number_of_members'].' '.$langUsers.'</option>';
  605.                 }
  606.             }
  607.         }
  608.     }
  609.  
  610.     if ( ($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin&& dropbox_cnf("allowMailing"))  // RH: Mailing starting point
  611.     {
  612.             // echo '<option value="mailing">'.dropbox_lang("mailingInSelect").'</option>';
  613.     }
  614.  
  615.     if dropbox_cnf("allowJustUpload"))  // RH
  616.     {
  617.       //echo '<option value="upload">'.dropbox_lang("justUploadInSelect").'</option>';
  618.       echo '<option value="user_'.$_user['user_id'].'">'.dropbox_lang("justUploadInSelect").'</option>';
  619.     }
  620.  
  621.     echo "</select>",
  622.         "</td></tr>",
  623.         "<tr><td></td>",
  624.         "<td><input type=\"Submit\" name=\"submitWork\" value=\"".dropbox_lang("ok""noDLTT")."\" />",
  625.         "</td></tr>",
  626.         "</table>",
  627.         "</form>";
  628. }
  629.  
  630. /**
  631. * returns username or false if user isn't registered anymore
  632. @todo check if this function is still necessary. There might be a library function for this.
  633. */
  634. function getUserNameFromId $id)  // RH: Mailing: return 'Mailing ' + id
  635. {
  636.     $mailingId $id dropbox_cnf("mailingIdBase");
  637.     if $mailingId 0)
  638.     {
  639.         return dropbox_lang("mailingAsUsername""noDLTT"$mailingId;
  640.     }
  641.  
  642.     $sql "SELECT CONCAT(lastname,' ', firstname) AS name
  643.             FROM " dropbox_cnf("tbl_user""
  644.             WHERE user_id='" addslashes$id"'";
  645.     $result api_sql_query($sql,__FILE__,__LINE__);
  646.     $res mysql_fetch_array$result);
  647.  
  648.     if $res == FALSEreturn FALSE;
  649.     return stripslashes$res["name"]);
  650. }
  651.  
  652. /**
  653. * returns loginname or false if user isn't registered anymore
  654. @todo check if this function is still necessary. There might be a library function for this.
  655. */
  656. function getLoginFromId $id)
  657. {
  658.     $sql "SELECT username
  659.             FROM " dropbox_cnf("tbl_user""
  660.             WHERE user_id='" addslashes$id"'";
  661.     $result =api_sql_query($sql,__FILE__,__LINE__);
  662.     $res mysql_fetch_array$result);
  663.     if $res == FALSEreturn FALSE;
  664.     return stripslashes$res["username"]);
  665. }
  666.  
  667. /**
  668. @return boolean indicating if user with user_id=$user_id is a course member
  669. @todo eliminate global
  670. @todo check if this function is still necessary. There might be a library function for this.
  671. */
  672. function isCourseMember$user_id)
  673. {
  674.     global $_course;
  675.     $course_code $_course['sysCode'];
  676.     $is_course_member CourseManager::is_user_subscribed_in_course($user_id$course_code,true);
  677.     return $is_course_member;
  678. }
  679.  
  680. /**
  681. * Checks if there are files in the dropbox_file table that aren't used anymore in dropbox_person table.
  682. * If there are, all entries concerning the file are deleted from the db + the file is deleted from the server
  683. */
  684. function removeUnusedFiles)
  685. {
  686.     // select all files that aren't referenced anymore
  687.     $sql "SELECT DISTINCT f.id, f.filename
  688.             FROM " dropbox_cnf("tbl_file"" f
  689.             LEFT JOIN " dropbox_cnf("tbl_person"" p ON f.id = p.file_id
  690.             WHERE p.user_id IS NULL";
  691.     $result api_sql_query($sql,__FILE__,__LINE__);
  692.     while $res mysql_fetch_array$result))
  693.     {
  694.         //delete the selected files from the post and file tables
  695.         $sql "DELETE FROM " dropbox_cnf("tbl_post"" WHERE file_id='" $res['id'"'";
  696.         $result1 api_sql_query($sql,__FILE__,__LINE__);
  697.         $sql "DELETE FROM " dropbox_cnf("tbl_file"" WHERE id='" $res['id'"'";
  698.         $result1 api_sql_query($sql,__FILE__,__LINE__);
  699.  
  700.         //delete file from server
  701.         @unlinkdropbox_cnf("sysPath""/" $res["filename"]);
  702.     }
  703. }
  704.  
  705. /**
  706. * RH: Mailing (2 new functions)
  707. *
  708. * Mailing zip-file is posted to (dest_user_id = ) mailing pseudo_id
  709. * and is only visible to its uploader (user_id).
  710. *
  711. * Mailing content files have uploader_id == mailing pseudo_id, a normal recipient,
  712. * and are visible initially to recipient and pseudo_id.
  713. *
  714. @author Ren� Haentjens, Ghent University
  715. *
  716. @todo check if this function is still necessary.
  717. */
  718. function getUserOwningThisMailing($mailingPseudoId$owner 0$or_die '')
  719. {
  720.     $sql "SELECT f.uploader_id
  721.             FROM " dropbox_cnf("tbl_file"" f
  722.             LEFT JOIN " dropbox_cnf("tbl_post"" p ON f.id = p.file_id
  723.             WHERE p.dest_user_id = '" $mailingPseudoId "'";
  724.     $result api_sql_query($sql,__FILE__,__LINE__);
  725.  
  726.     if (!($res mysql_fetch_array($result)))
  727.         die(dropbox_lang("generalError")." (code 901)");
  728.  
  729.     if ($owner == 0return $res['uploader_id'];
  730.  
  731.     if ($res['uploader_id'== $ownerreturn TRUE;
  732.  
  733.     die(dropbox_lang("generalError")." (code ".$or_die.")");
  734. }
  735. /**
  736. @author Ren� Haentjens, Ghent University
  737. @todo check if this function is still necessary.
  738. */
  739. function removeMoreIfMailing($file_id)
  740. {
  741.     // when deleting a mailing zip-file (posted to mailingPseudoId):
  742.     // 1. the detail window is no longer reachable, so
  743.     //    for all content files, delete mailingPseudoId from person-table
  744.     // 2. finding the owner (getUserOwningThisMailing) is no longer possible, so
  745.     //    for all content files, replace mailingPseudoId by owner as uploader
  746.  
  747.     $sql "SELECT p.dest_user_id
  748.             FROM " dropbox_cnf("tbl_post"" p
  749.             WHERE p.file_id = '" $file_id "'";
  750.     $result api_sql_query($sql,__FILE__,__LINE__);
  751.  
  752.     if $res mysql_fetch_array$result))
  753.     {
  754.         $mailingPseudoId $res['dest_user_id'];
  755.         if $mailingPseudoId dropbox_cnf("mailingIdBase"))
  756.         {
  757.             $sql "DELETE FROM " dropbox_cnf("tbl_person"" WHERE user_id='" $mailingPseudoId "'";
  758.             $result1 api_sql_query($sql,__FILE__,__LINE__);
  759.  
  760.             $sql "UPDATE " dropbox_cnf("tbl_file".
  761.                 " SET uploader_id='" api_get_user_id("' WHERE uploader_id='" $mailingPseudoId "'";
  762.             $result1 api_sql_query($sql,__FILE__,__LINE__);
  763.         }
  764.     }
  765. }
  766.  
  767. /**
  768. * The dropbox has a deviant naming scheme for language files so it needs an additional language function
  769. *
  770. @todo check if this function is still necessary.
  771. *
  772. @author Ren� Haentjens, Ghent University
  773. */
  774. function dropbox_lang($variable$notrans 'DLTT')
  775. {
  776.     return (api_get_setting('server_type'== 'test' ?
  777.         get_lang('dropbox_lang["'.$variable.'"]'$notrans:
  778.         str_replace("\\'""'"$GLOBALS['dropbox_lang'][$variable]));
  779. }
  780. /**
  781. * Function that finds a given config setting
  782. *
  783. @author Ren� Haentjens, Ghent University
  784. */
  785. function dropbox_cnf($variable)
  786. {
  787.     return $GLOBALS['dropbox_cnf'][$variable];
  788. }
  789.  
  790.  
  791.  
  792.  
  793.  
  794. /**
  795. *
  796. */
  797. function store_add_dropbox()
  798. {
  799.     global $dropbox_cnf;
  800.     global $_user;
  801.     global $_course;
  802.  
  803.     // ----------------------------------------------------------
  804.     // Validating the form data
  805.     // ----------------------------------------------------------
  806.  
  807.     // the author is
  808.     /*
  809.     if (!isset( $_POST['authors']))
  810.     {
  811.         return get_lang('AuthorFieldCannotBeEmpty');
  812.     }
  813.     */
  814.  
  815.     // there are no recipients selected
  816.     if !isset$_POST['recipients']|| count$_POST['recipients']<= 0)
  817.     {
  818.         return get_lang('YouMustSelectAtLeastOneDestinee');
  819.     }
  820.     // Check if all the recipients are valid
  821.     else
  822.     {
  823.         $thisIsAMailing FALSE;  // RH: Mailing selected as destination
  824.         $thisIsJustUpload FALSE;  // RH
  825.         foreach$_POST['recipients'as $rec)
  826.         {
  827.             if $rec == 'mailing')
  828.             {
  829.                 $thisIsAMailing TRUE;
  830.             }
  831.             elseif $rec == 'upload')
  832.             {
  833.                 $thisIsJustUpload TRUE;
  834.             }
  835.             elseif (strpos($rec'user_'=== && !isCourseMember(substr($recstrlen('user_') ) ))
  836.             {
  837.                 return get_lang('InvalideUserDetected');
  838.             }
  839.             elseif (strpos($rec'group_'!== && strpos($rec'user_'!== 0)
  840.             {
  841.                 return get_lang('InvalideGroupDetected');
  842.             }
  843.         }
  844.     }
  845.  
  846.     // we are doing a mailing but an additional recipient is selected
  847.     if $thisIsAMailing && count($_POST['recipients']!= 1))
  848.     {
  849.         return get_lang('MailingSelectNoOther');
  850.     }
  851.  
  852.     // we are doing a just upload but an additional recipient is selected.
  853.     // note: why can't this be valid? It is like sending a document to yourself AND to a different person (I do this quite often with my e-mails)
  854.     if $thisIsJustUpload && count($_POST['recipients']!= 1))
  855.     {
  856.         return get_lang('mailingJustUploadSelectNoOther');
  857.     }
  858.  
  859.     if empty$_FILES['file']['name']))
  860.     {
  861.         $error TRUE;
  862.         return  get_lang('NoFileSpecified');
  863.     }
  864.  
  865.     // ----------------------------------------------------------
  866.     // are we overwriting a previous file or sending a new one
  867.     // ----------------------------------------------------------
  868.     $dropbox_overwrite false;
  869.     if isset($_POST['cb_overwrite']&& $_POST['cb_overwrite']==true)
  870.     {
  871.         $dropbox_overwrite true;
  872.     }
  873.  
  874.     // ----------------------------------------------------------
  875.     // doing the upload
  876.     // ----------------------------------------------------------
  877.     $dropbox_filename $_FILES['file']['name'];
  878.     $dropbox_filesize $_FILES['file']['size'];
  879.     $dropbox_filetype $_FILES['file']['type'];
  880.     $dropbox_filetmpname $_FILES['file']['tmp_name'];
  881.  
  882.     // check if the filesize does not exceed the allowed size.
  883.     if $dropbox_filesize <= || $dropbox_filesize $dropbox_cnf["maxFilesize"])
  884.     {
  885.         return get_lang('DropboxFileTooBig');
  886.     }
  887.  
  888.     // check if the file is actually uploaded
  889.     if !is_uploaded_file$dropbox_filetmpname)) // check user fraud : no clean error msg.
  890.     {
  891.         return get_lang('TheFileIsNotUploaded');
  892.     }
  893.  
  894.     // Try to add an extension to the file if it hasn't got one
  895.     $dropbox_filename add_ext_on_mime$dropbox_filename,$dropbox_filetype);
  896.     // Replace dangerous characters
  897.     $dropbox_filename replace_dangerous_char$dropbox_filename);
  898.     // Transform any .php file in .phps fo security
  899.     $dropbox_filename php2phps $dropbox_filename);
  900.     //filter extension
  901.     if(!filter_extension($dropbox_filename))
  902.     {
  903.         return get_lang('UplUnableToSaveFileFilteredExtension');
  904.     }
  905.     
  906.     // set title
  907.     $dropbox_title $dropbox_filename;
  908.     // set author
  909.     if $_POST['authors'== '')
  910.     {
  911.         $_POST['authors'getUserNameFromId$_user['user_id']);
  912.     }
  913.  
  914.     // note: I think we could better migrate everything from here on to separate functions: store_new_dropbox, store_new_mailing, store_just_upload
  915.  
  916.     if ($dropbox_overwrite)  // RH: Mailing: adapted
  917.     {
  918.         $dropbox_person new Dropbox_Person$_user['user_id']api_is_course_admin()api_is_course_tutor());
  919.  
  920.         foreach($dropbox_person->sentWork as $w)
  921.         {
  922.             if ($w->title == $dropbox_filename)
  923.             {
  924.                 if ( ($w->recipients[0]['id'dropbox_cnf("mailingIdBase")) xor $thisIsAMailing)
  925.                 {
  926.                     return get_lang('mailingNonMailingError');
  927.                 }
  928.                 if ( ($w->recipients[0]['id'== $_user['user_id']xor $thisIsJustUpload)
  929.                 {
  930.                     return get_lang('mailingJustUploadNoOther');
  931.                 }
  932.                 $dropbox_filename $w->filename;
  933.                 $found true// note: do we still need this?
  934.                 break;
  935.             }
  936.         }
  937.     }
  938.     else  // rename file to login_filename_uniqueId format
  939.     {
  940.         $dropbox_filename getLoginFromId$_user['user_id']"_" $dropbox_filename "_".uniqid('');
  941.     }
  942.  
  943.     // creating the array that contains all the users who will receive the file
  944.     $new_work_recipients array();
  945.     foreach ($_POST["recipients"as $rec)
  946.     {
  947.         if (strpos($rec'user_'=== 0)
  948.         {
  949.             $new_work_recipients[substr($recstrlen('user_') );
  950.         }
  951.         elseif (strpos($rec'group_'=== )
  952.         {
  953.             $userList GroupManager::get_subscribed_users(substr($recstrlen('group_') ));
  954.             foreach ($userList as $usr)
  955.             {
  956.                 if (in_array($usr['user_id']$new_work_recipients&& $usr['user_id'!= $_user['user_id'])
  957.                 {
  958.                     $new_work_recipients[$usr['user_id'];
  959.                 }
  960.             }
  961.         }
  962.     }
  963.  
  964.     @move_uploaded_file$dropbox_filetmpnamedropbox_cnf("sysPath"'/' $dropbox_filename);
  965.     
  966.     $b_send_mail api_get_course_setting('email_alert_on_new_doc_dropbox');
  967.     
  968.     if($b_send_mail)
  969.     {
  970.         foreach($new_work_recipients as $recipient_id)
  971.         {
  972.             include_once(api_get_path(LIBRARY_PATH'usermanager.lib.php');
  973.             $recipent_temp=UserManager :: get_user_info_by_id($recipient_id);
  974.             api_mail($recipent_temp['lastname'].' '.$recipent_temp['firstname'],$recipent_temp['email'],
  975.                 get_lang('NewDropboxFileUploaded'),
  976.                 get_lang('NewDropboxFileUploadedContent').' '.api_get_path(WEB_CODE_PATH).'dropbox/index.php?cidReq='.$_course['sysCode']."\n\n".$_user['firstName']." ".$_user['lastName']."\n".  get_lang('Email'." : ".$_user['mail']$_user['firstName']." ".$_user['lastName'],$_user['mail']);
  977.                 //get_lang('NewDropboxFileUploadedContent').' '.api_get_path(WEB_CODE_PATH).'dropbox/index.php?cidReq='.$_course['sysCode']."\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('Manager'). " ".get_setting('siteName')."\n" .get_lang('Email') ." : ".get_setting('emailAdministrator'),get_setting('administratorName')." ".get_setting('administratorSurname'),get_setting('emailAdministrator'));
  978.         }
  979.     }
  980.     
  981.     new Dropbox_SentWork$_user['user_id']$dropbox_title$_POST['description']strip_tags($_POST['authors'])$dropbox_filename$dropbox_filesize$new_work_recipients);
  982.  
  983.     Security::clear_token();
  984.     return get_lang('FileUploadSucces');
  985. }
  986.  
  987.  
  988.  
  989. /**
  990. * This function displays the firstname and lastname of the user as a link to the user tool.
  991. *
  992. @see this is the same function as in the new forum, so this probably has to move to a user library.
  993. *
  994. @todo move this function to the user library
  995. *
  996. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  997. @version march 2006
  998. */
  999. function display_user_link($user_id$name='')
  1000. {
  1001.     global $_otherusers;
  1002.  
  1003.     if ($user_id<>0)
  1004.     {
  1005.         if ($name=='')
  1006.         {
  1007.             $table_user Database::get_main_table(TABLE_MAIN_USER);
  1008.             $sql="SELECT * FROM $table_user WHERE user_id='".Database::escape_string($user_id)."'";
  1009.             $result=api_sql_query($sql,__FILE__,__LINE__);
  1010.             $row=mysql_fetch_array($result);
  1011.             return "<a href=\"../user/userInfo.php?uInfo=".$row['user_id']."\">".$row['firstname']." ".$row['lastname']."</a>";
  1012.         }
  1013.         else
  1014.         {
  1015.             return "<a href=\"../user/userInfo.php?uInfo=".$user_id."\">".$name."</a>";
  1016.         }
  1017.     }
  1018.     else
  1019.     {
  1020.         return $name.' ('.get_lang('Anonymous').')';
  1021.     }
  1022. }
  1023.  
  1024. /**
  1025. * this function transforms the array containing all the feedback into something visually attractive.
  1026. *
  1027. @param an array containing all the feedback about the given message.
  1028. *
  1029. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1030. @version march 2006
  1031. */
  1032. function feedback($array)
  1033. {
  1034.  
  1035.     foreach ($array as $key=>$value)
  1036.     {
  1037.         $output.=format_feedback($value);
  1038.     }
  1039.     $output.=feedback_form();
  1040.     return $output;
  1041. }
  1042.  
  1043. /**
  1044. * This function returns the html code to display the feedback messages on a given dropbox file
  1045. @param $feedback_array an array that contains all the feedback messages about the given document.
  1046. @return html code
  1047. @todo add the form for adding new comment (if the other party has not deleted it yet).
  1048. *
  1049. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1050. @version march 2006
  1051. */
  1052. function format_feedback($feedback)
  1053. {
  1054.     $output.=display_user_link($feedback['author_user_id']);
  1055.     $output.='&nbsp;&nbsp;['.$feedback['feedback_date'].']<br>';
  1056.     $output.='<div style="padding-top:6px">'.nl2br($feedback['feedback']).'</div><hr size="1" noshade/><br>';
  1057.     return $output;
  1058. }
  1059.  
  1060. /**
  1061. * this function returns the code for the form for adding a new feedback message to a dropbox file.
  1062. @return html code
  1063. *
  1064. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1065. @version march 2006
  1066. */
  1067. function feedback_form()
  1068. {
  1069.     global $dropbox_cnf;
  1070.  
  1071.     $return get_lang('AddNewFeedback').'<br />';
  1072.  
  1073.     // we now check if the other users have not delete this document yet. If this is the case then it is useless to see the
  1074.     // add feedback since the other users will never get to see the feedback.
  1075.     $sql="SELECT * FROM ".$dropbox_cnf["tbl_person"]." WHERE file_id='".Database::escape_string($_GET['id'])."'";
  1076.     $result=api_sql_query($sql,__LINE____FILE__);
  1077.     $number_users_who_see_file=mysql_num_rows($result);
  1078.     if ($number_users_who_see_file>1)
  1079.     {
  1080.         $return .= '<textarea name="feedback" style="width: 80%; height: 80px;"></textarea><br /><input type="submit" name="store_feedback" value="'.get_lang('Ok').'" 
  1081.             onclick="document.form_tablename.attributes.action.value = document.location;document.form_tablename.submit();"/>';
  1082.     }
  1083.     else
  1084.     {
  1085.         $return .= get_lang('AllUsersHaveDeletedTheFileAndWillNotSeeFeedback');
  1086.     }
  1087.     return $return;
  1088. }
  1089.  
  1090. /**
  1091. @return language string (depending on the success or failure.
  1092. *
  1093. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1094. @version march 2006
  1095. */
  1096. function store_feedback()
  1097. {
  1098.     global $dropbox_cnf;
  1099.     global $_user;
  1100.  
  1101.     if (!is_numeric($_GET['id']))
  1102.     {
  1103.         return get_lang('FeedbackError');
  1104.     }
  1105.  
  1106.     if ($_POST['feedback']=='')
  1107.     {
  1108.         return get_lang('PleaseTypeText');
  1109.     }
  1110.     else
  1111.     {
  1112.         $sql="INSERT INTO ".$dropbox_cnf['tbl_feedback']." (file_id, author_user_id, feedback, feedback_date) VALUES
  1113.                 ('".Database::escape_string($_GET['id'])."','".Database::escape_string($_user['user_id'])."','".Database::escape_string($_POST['feedback'])."',NOW())";
  1114.         api_sql_query($sql);
  1115.         return get_lang('DropboxFeedbackStored');
  1116.     }
  1117. }
  1118.  
  1119. /**
  1120. * This function downloads all the files of the inputarray into one zip
  1121. @param $array an array containing all the ids of the files that have to be downloaded.
  1122. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1123. @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
  1124. @todo integrate some cleanup function that removes zip files that are older than 2 days
  1125. *
  1126. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1127. @version march 2006
  1128. */
  1129. function zip_download ($array)
  1130. {
  1131.     global $_course;
  1132.     global $dropbox_cnf;
  1133.     global $_user;
  1134.     global $files;
  1135.     
  1136.     $sys_course_path api_get_path(SYS_COURSE_PATH);
  1137.     
  1138.     // zip library for creation of the zipfile
  1139.     include(api_get_path(LIBRARY_PATH)."/pclzip/pclzip.lib.php");
  1140.  
  1141.     // place to temporarily stash the zipfiles
  1142.     $temp_zip_dir api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/";
  1143.  
  1144.     // create the directory if it does not exist yet.
  1145.     if(!is_dir($temp_zip_dir))
  1146.     {
  1147.         mkdir($temp_zip_dir);
  1148.     }
  1149.  
  1150.  
  1151.     $files='';
  1152.  
  1153.     // note: we also have to add the check if the user has received or sent this file. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1154.     $sql="SELECT distinct file.filename, file.title, file.author, file.description
  1155.             FROM ".$dropbox_cnf["tbl_file"]." file, ".$dropbox_cnf["tbl_person"]." person
  1156.             WHERE file.id IN (".implode(', ',$array).")
  1157.             AND file.id=person.file_id
  1158.             AND person.user_id='".$_user['user_id']."'";
  1159.     $result=api_sql_query($sql,__FILE__,__LINE__);
  1160.     while ($row=mysql_fetch_array($result))
  1161.     {
  1162.         $files[$row['filename']]=array('filename'=>$row['filename'],'title'=>$row['title']'author'=>$row['author']'description'=>$row['description']);
  1163.     }
  1164.  
  1165.     //$alternative is a variable that uses an alternative method to create the zip
  1166.     // because the renaming of the files inside the zip causes error on php5 (unexpected end of archive)
  1167.     $alternative=true;
  1168.     if ($alternative)
  1169.     {
  1170.         zip_download_alternative($files);
  1171.         exit;
  1172.     }
  1173.  
  1174.     // create the zip file
  1175.     $temp_zip_file=$temp_zip_dir.'/dropboxdownload-'.$_user['user_id'].'-'.mktime().'.zip';
  1176.     $zip_folder=new PclZip($temp_zip_file);
  1177.  
  1178.     foreach ($files as $key=>$value)
  1179.     {
  1180.         // met hernoemen van de files in de zip
  1181.         $zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename'],PCLZIP_OPT_REMOVE_PATHapi_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox"PCLZIP_CB_PRE_ADD'my_pre_add_callback');
  1182.         // zonder hernoemen van de files in de zip
  1183.         //$zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename'],PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox");
  1184.     }
  1185.  
  1186.     // create the overview file
  1187.     $overview_file_content=generate_html_overview($filesarray('filename')array('title'));
  1188.     $overview_file=$temp_zip_dir.'/overview.html';
  1189.     $handle=fopen($overview_file,'w');
  1190.     fwrite($handle,$overview_file_content);
  1191.  
  1192.  
  1193.     // send the zip file
  1194.     DocumentManager::file_send_for_download($temp_zip_file,true,$name);
  1195.     exit;
  1196. }
  1197. /**
  1198. * This is a callback function to decrypt the files in the zip file to their normal filename (as stored in the database)
  1199. @param $p_event a variable of PCLZip
  1200. @param $p_header a variable of PCLZip
  1201. *
  1202. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1203. @version march 2006
  1204. */
  1205. function my_pre_add_callback($p_event&$p_header)
  1206. {
  1207.     global $files;
  1208.  
  1209.     $p_header['stored_filename']=$files[$p_header['stored_filename']]['title'];
  1210.     return 1;
  1211. }
  1212.  
  1213. /**
  1214. * This function is an alternative zip download. It was added because PCLZip causes problems on PHP5 when using PCLZIP_CB_PRE_ADD and a callback function to rename
  1215. * the files inside the zip file (dropbox scrambles the files to prevent
  1216. @todo consider using a htaccess that denies direct access to the file but only allows the php file to access it. This would remove the scrambling requirement
  1217. *         but it would require additional checks to see if the filename of the uploaded file is not used yet.
  1218. @param $files is an associative array that contains the files that the user wants to download (check to see if the user is allowed to download these files already
  1219. *          happened so the array is clean!!. The key is the filename on the filesystem. The value is an array that contains both the filename on the filesystem and
  1220. *          the original filename (that will be used in the zip file)
  1221. @todo when we copy the files there might be two files with the same name. We need a function that (recursively) checks this and changes the name
  1222. *
  1223. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1224. @version march 2006
  1225. */
  1226. function zip_download_alternative($files)
  1227. {
  1228.     global $_course;
  1229.     global $_user;
  1230.  
  1231.     $temp_zip_dir api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/";
  1232.  
  1233.     // Step 1: create the overview file and add it to the zip
  1234.     $overview_file_content=generate_html_overview($filesarray('filename')array('title'));
  1235.     $overview_file=$temp_zip_dir.'overview'.$_user['firstname'].$_user['lastname'].'.html';
  1236.     $handle=fopen($overview_file,'w');
  1237.     fwrite($handle,$overview_file_content);
  1238.     // todo: find a different solution for this because even 2 seconds is no guarantee.
  1239.     sleep(2);
  1240.  
  1241.     // Step 2: we copy all the original dropbox files to the temp folder and change their name into the original name
  1242.     foreach ($files as $key=>$value)
  1243.     {
  1244.         $value['title']=check_file_name($value['title']);
  1245.         $files[$value['filename']]['title']=$value['title'];
  1246.         copy(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename']api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title']);
  1247.     }
  1248.  
  1249.     // Step 3: create the zip file and add all the files to it
  1250.     $temp_zip_file=$temp_zip_dir.'/dropboxdownload-'.$_user['user_id'].'-'.mktime().'.zip';
  1251.     $zip_folder=new PclZip($temp_zip_file);
  1252.     foreach ($files as $key=>$value)
  1253.     {
  1254.         $zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title'],PCLZIP_OPT_REMOVE_PATHapi_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1255.     }
  1256.  
  1257.     // Step 4: we add the overview file
  1258.     $zip_folder->add($overview_file,PCLZIP_OPT_REMOVE_PATHapi_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1259.  
  1260.     // Step 5: send the file for download;
  1261.     DocumentManager::file_send_for_download($temp_zip_file,true,$name);
  1262.  
  1263.     // Step 6: remove the files in the temp dir
  1264.     foreach ($files as $key=>$value)
  1265.     {
  1266.         unlink(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title']);
  1267.     }
  1268.     //unlink($overview_file);
  1269.  
  1270.     exit;
  1271. }
  1272.  
  1273. /**
  1274. @desc This function checks if the real filename of the dropbox files doesn't already exist in the temp folder. If this is the case then
  1275. *         it will generate a different filename;
  1276. *
  1277. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1278. @version march 2006
  1279. */
  1280. function check_file_name($file_name_2_check$counter=0)
  1281. {
  1282.     global $_course;
  1283.  
  1284.     $new_file_name=$file_name_2_check;
  1285.     if ($counter<>0)
  1286.     {
  1287.         $new_file_name=$counter.$new_file_name;
  1288.     }
  1289.  
  1290.     if (!file_exists(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$new_file_name))
  1291.     {
  1292.         return $new_file_name;
  1293.     }
  1294.     else
  1295.     {
  1296.         $counter++;
  1297.         $new_file_name=check_file_name($file_name_2_check,$counter);
  1298.         return $new_file_name;
  1299.     }
  1300. }
  1301.  
  1302.  
  1303. /**
  1304. @desc Cleans the temp zip files that were created when users download several files or a whole folder at once.
  1305. *         T
  1306. @return true 
  1307. @todo
  1308. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1309. @version march 2006
  1310. */
  1311.  
  1312. {
  1313.     global $_course;
  1314.  
  1315.     $handle=opendir(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1316.     while (false !== ($file readdir($handle)))
  1317.     {
  1318.         if ($file<>'.' OR $file<>'..')
  1319.         {
  1320.             $name=str_replace('.zip''',$file);
  1321.             $name_part=explode('-',$name);
  1322.             $timestamp_of_file=$name_part[count($name_part)-1];
  1323.             // if it is a dropboxdownloadfile and the file is older than one day then we delete it
  1324.             if (strstr($file'dropboxdownload'AND $timestamp_of_file<(mktime()-86400))
  1325.             {
  1326.                 unlink(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$file);
  1327.             }
  1328.         }
  1329.  
  1330.     }
  1331.     closedir($handle);
  1332.     return true;
  1333. }
  1334.  
  1335. /**
  1336. @desc generates the contents of a html file that gives an overview of all the files in the zip file.
  1337. *         This is to know the information of the files that are inside the zip file (who send it, the comment, ...)
  1338. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1339. @version march 2006
  1340. */
  1341. function generate_html_overview($files$dont_show_columns=array()$make_link=array())
  1342. {
  1343.     $return="<html>\n<head>\n\t<title>".get_lang('OverviewOfFilesInThisZip')."</title>\n</head>";
  1344.     $return.="\n\n<body>\n<table border=\"1px\">";
  1345.  
  1346.     $counter=0;
  1347.     foreach ($files as $key=>$value)
  1348.     {
  1349.         // We add the header
  1350.         if ($counter==0)
  1351.         {
  1352.             $columns_array=array_keys($value);
  1353.             $return.="\n<tr>";
  1354.             foreach ($columns_array AS $columns_array_key=>$columns_array_value)
  1355.             {
  1356.                 if (!in_array($columns_array_value,$dont_show_columns))
  1357.                 {
  1358.                     $return.="\n\t<th>".$columns_array_value."</th>";
  1359.                 }
  1360.                 $column[]=$columns_array_value;
  1361.             }
  1362.             $return.="</tr><n";
  1363.         }
  1364.         $counter++;
  1365.  
  1366.         // We add the content
  1367.         $return.="\n<tr>";
  1368.         foreach ($column AS $column_key=>$column_value)
  1369.         {
  1370.             if (!in_array($column_value,$dont_show_columns))
  1371.             {
  1372.                 $return.="\n\t<td>";
  1373.                 if (in_array($column_value$make_link))
  1374.                 {
  1375.                     $return.='<a href="'.$value[$column_value].'">'.$value[$column_value].'</a>';
  1376.                 }
  1377.                 else
  1378.                 {
  1379.                     $return.=$value[$column_value];
  1380.                 }
  1381.                 $return.="</td>";
  1382.             }
  1383.         }
  1384.         $return.="</tr><n";
  1385.  
  1386.  
  1387.     }
  1388.     $return.="\n</table>\n\n</body>";
  1389.     $return.="\n</html>";
  1390.  
  1391.     return $return;
  1392. }
  1393.  
  1394. /**
  1395. @desc This function retrieves the number of feedback messages on every document. This function might become obsolete when
  1396. *          the feedback becomes user individual.
  1397. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1398. @version march 2006
  1399. */
  1400. function get_total_number_feedback($file_id='')
  1401. {
  1402.     global $dropbox_cnf;
  1403.  
  1404.     $sql="SELECT COUNT(feedback_id) AS total, file_id FROM ".$dropbox_cnf['tbl_feedback']." GROUP BY file_id";
  1405.     $result=api_sql_query($sql__FILE____LINE__);
  1406.     while ($row=mysql_fetch_array($result))
  1407.     {
  1408.         $return[$row['file_id']]=$row['total'];
  1409.     }
  1410.     return $return;
  1411. }
  1412.  
  1413.  
  1414. /**
  1415. @desc this function checks if the key exists. If this is the case it returns the value, if not it returns 0
  1416. @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1417. @version march 2006
  1418. */
  1419. function check_number_feedback($key$array)
  1420. {
  1421.     if (is_array($array))
  1422.     {    
  1423.         if (key_exists($key,$array))
  1424.         {
  1425.             return $array[$key];
  1426.         }
  1427.         else
  1428.         {
  1429.             return 0;
  1430.         }
  1431.     }
  1432.     else
  1433.     {
  1434.         return 0;
  1435.     }
  1436. }
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443. /**
  1444.  * Get the last access to a given tool of a given user
  1445.  * @param $tool string the tool constant
  1446.  * @param $course_code the course_id
  1447.  * @param $user_id the id of the user
  1448.  * @return string last tool access date
  1449.  *
  1450.  * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1451.  * @version march 2006
  1452.  *
  1453.  * @todo consider moving this function to a more appropriate place.
  1454.  */
  1455. function get_last_tool_access($tool$course_code=''$user_id='')
  1456. {
  1457.     global $_course$_user;
  1458.  
  1459.     // The default values of the parameters
  1460.     if ($course_code=='')
  1461.     {
  1462.         $course_code=$_course['id'];
  1463.     }
  1464.     if ($user_id=='')
  1465.     {
  1466.         $user_id=$_user['user_id'];
  1467.     }
  1468.  
  1469.     // the table where the last tool access is stored (=track_e_lastaccess)
  1470.     $table_last_access=Database::get_statistic_table('track_e_lastaccess');
  1471.  
  1472.     $sql="SELECT access_date FROM $table_last_access WHERE access_user_id='".Database::escape_string($user_id)."'
  1473.                 AND access_cours_code='".Database::escape_string($course_code)."'
  1474.                 AND access_tool='".Database::escape_string($tool)."'";
  1475.     $result=api_sql_query($sql,__FILE__,__LINE__);
  1476.     $row=mysql_fetch_array($result);
  1477.     return $row['access_date'];
  1478. }
  1479. ?>

Documentation generated on Thu, 12 Jun 2008 13:21:41 -0500 by phpDocumentor 1.4.1