From 012940efe3e1833f8f6a19e0e08f7d2e7d1462d7 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 17 Oct 2012 17:17:13 +0200 Subject: [PATCH] Allowing certificates in gradebook --- main/document/document.php | 2 +- main/inc/lib/document.lib.php | 1871 ++++++++++++++++----------------- 2 files changed, 931 insertions(+), 942 deletions(-) diff --git a/main/document/document.php b/main/document/document.php index 6702eaabc0..a4c16b437d 100644 --- a/main/document/document.php +++ b/main/document/document.php @@ -1056,7 +1056,7 @@ if (isset($docs_and_folders) && is_array($docs_and_folders)) { $row[] = $document_data['path']; } - if (DocumentManager::is_folder_to_avoid($document_data['path'])) { + if (DocumentManager::is_folder_to_avoid($document_data['path'], $is_certificate_mode)) { continue; } diff --git a/main/inc/lib/document.lib.php b/main/inc/lib/document.lib.php index 784c8f1306..d178dac842 100755 --- a/main/inc/lib/document.lib.php +++ b/main/inc/lib/document.lib.php @@ -1,20 +1,23 @@ 4.3, - * but this doesn't work as it should on Windows installations + * Get the content type of a file by checking the extension + * We could use mime_content_type() with php-versions > 4.3, + * but this doesn't work as it should on Windows installations * - * @param string $filename or boolean TRUE to return complete array - * @author ? first version - * @author Bert Vanderkimpen + * @param string $filename or boolean TRUE to return complete array + * @author ? first version + * @author Bert Vanderkimpen * */ public static function file_get_mime_type($filename) { @@ -136,10 +139,10 @@ class DocumentManager { 'mxu' => 'video/vnd.mpegurl', 'nc' => 'application/x-netcdf', 'oda' => 'application/oda', - 'oga'=> 'audio/ogg', - 'ogg'=> 'application/ogg', - 'ogx'=> 'application/ogg', - 'ogv'=> 'video/ogg', + 'oga' => 'audio/ogg', + 'ogg' => 'application/ogg', + 'ogx' => 'application/ogg', + 'ogv' => 'video/ogg', 'pbm' => 'image/x-portable-bitmap', 'pct' => 'image/pict', 'pdb' => 'chemical/x-pdb', @@ -193,7 +196,7 @@ class DocumentManager { 'sv4crc' => 'application/x-sv4crc', 'svf' => 'application/vnd.svf', 'svg' => 'image/svg+xml', - //'svgz' => 'image/svg+xml', + //'svgz' => 'image/svg+xml', 'swf' => 'application/x-shockwave-flash', 'sxc' => 'application/vnd.sun.xml.calc', 'sxi' => 'application/vnd.sun.xml.impress', @@ -238,7 +241,7 @@ class DocumentManager { 'xwd' => 'image/x-windowdump', 'xyz' => 'chemical/x-xyz', 'zip' => 'application/zip' - ); + ); if ($filename === true) { return $mime_types; @@ -265,12 +268,12 @@ class DocumentManager { } /** - * @return true if the user is allowed to see the document, false otherwise - * @author Sergio A Kessler, first version - * @author Roan Embrechts, bugfix + * @return true if the user is allowed to see the document, false otherwise + * @author Sergio A Kessler, first version + * @author Roan Embrechts, bugfix * @todo ??not only check if a file is visible, but also check if the user is allowed to see the file?? */ - public static function file_visible_to_user ($this_course, $doc_url) { + public static function file_visible_to_user($this_course, $doc_url) { $current_session_id = api_get_session_id(); $is_allowed_to_edit = api_is_allowed_to_edit(null, true); @@ -279,7 +282,7 @@ class DocumentManager { return true; } else { $tbl_document = Database::get_course_table(TABLE_DOCUMENT); - $tbl_item_property = $this_course.'item_property'; + $tbl_item_property = $this_course . 'item_property'; $doc_url = Database::escape_string($doc_url); //$doc_url = addslashes($doc_url); $query = "SELECT 1 FROM $tbl_document AS docs,$tbl_item_property AS props @@ -311,18 +314,18 @@ class DocumentManager { header('Content-type: application/octet-stream'); //header('Content-Type: application/force-download'); - header('Content-length: '.$len); + header('Content-length: ' . $len); if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) { - header('Content-Disposition: filename= '.$filename); + header('Content-Disposition: filename= ' . $filename); } else { - header('Content-Disposition: attachment; filename= '.$filename); + header('Content-Disposition: attachment; filename= ' . $filename); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { header('Pragma: '); header('Cache-Control: '); header('Cache-Control: public'); // IE cannot download from sessions without a cache } - header('Content-Description: '.$filename); + header('Content-Description: ' . $filename); header('Content-transfer-encoding: binary'); $fp = fopen($full_file_name, 'r'); @@ -333,7 +336,7 @@ class DocumentManager { $content_type = self::file_get_mime_type($filename); header('Expires: Wed, 01 Jan 1990 00:00:00 GMT'); - header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); + header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // Commented to avoid double caching declaration when playing with IE and HTTPS //header('Cache-Control: no-cache, must-revalidate'); //header('Pragma: no-cache'); @@ -341,23 +344,23 @@ class DocumentManager { case 'text/html': $encoding = @api_detect_encoding_html(file_get_contents($full_file_name)); if (!empty($encoding)) { - $content_type .= '; charset='.$encoding; + $content_type .= '; charset=' . $encoding; } break; case 'text/plain': $encoding = @api_detect_encoding(strip_tags(file_get_contents($full_file_name))); if (!empty($encoding)) { - $content_type .= '; charset='.$encoding; + $content_type .= '; charset=' . $encoding; } break; } - header('Content-type: '.$content_type); - header('Content-Length: '.$len); + header('Content-type: ' . $content_type); + header('Content-Length: ' . $len); $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']); if (strpos($user_agent, 'msie')) { - header('Content-Disposition: ; filename= '.$filename); + header('Content-Disposition: ; filename= ' . $filename); } else { - header('Content-Disposition: inline; filename= '.$filename); + header('Content-Disposition: inline; filename= ' . $filename); } readfile($full_file_name); return true; @@ -384,18 +387,18 @@ class DocumentManager { header('Content-type: application/octet-stream'); //header('Content-Type: application/force-download'); - header('Content-length: '.$len); + header('Content-length: ' . $len); if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) { - header('Content-Disposition: filename= '.$filename); + header('Content-Disposition: filename= ' . $filename); } else { - header('Content-Disposition: attachment; filename= '.$filename); + header('Content-Disposition: attachment; filename= ' . $filename); } if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { header('Pragma: '); header('Cache-Control: '); header('Cache-Control: public'); // IE cannot download from sessions without a cache } - header('Content-Description: '.$filename); + header('Content-Description: ' . $filename); header('Content-transfer-encoding: binary'); //$fp = fopen($full_string, 'r'); @@ -410,30 +413,30 @@ class DocumentManager { $content_type = self::file_get_mime_type($filename); header('Expires: Wed, 01 Jan 1990 00:00:00 GMT'); - header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); + header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); switch ($content_type) { case 'text/html': $encoding = @api_detect_encoding_html($full_string); if (!empty($encoding)) { - $content_type .= '; charset='.$encoding; + $content_type .= '; charset=' . $encoding; } break; case 'text/plain': $encoding = @api_detect_encoding(strip_tags($full_string)); if (!empty($encoding)) { - $content_type .= '; charset='.$encoding; + $content_type .= '; charset=' . $encoding; } break; } - header('Content-type: '.$content_type); - header('Content-Length: '.$len); + header('Content-type: ' . $content_type); + header('Content-Length: ' . $len); $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']); if (strpos($user_agent, 'msie')) { - header('Content-Disposition: ; filename= '.$filename); + header('Content-Disposition: ; filename= ' . $filename); } else { - header('Content-Disposition: inline; filename= '.$filename); + header('Content-Disposition: inline; filename= ' . $filename); } echo($full_string); //You have to ensure that the calling script then stops processing (exit();) @@ -455,8 +458,8 @@ class DocumentManager { */ public static function get_all_document_data($_course, $path = '/', $to_group_id = 0, $to_user_id = NULL, $can_see_invisible = false, $search = false) { $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); - $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); - + $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); + if (!is_null($to_user_id)) { $to_field = 'last.to_user_id'; $to_value = $to_user_id; @@ -466,9 +469,9 @@ class DocumentManager { } //escape underscores in the path so they don't act as a wildcard - $path = Database::escape_string(str_replace('_', '\_', $path)); + $path = Database::escape_string(str_replace('_', '\_', $path)); $to_user_id = Database::escape_string($to_user_id); - $to_value = Database::escape_string($to_value); + $to_value = Database::escape_string($to_value); $visibility_bit = ' <> 2'; @@ -481,7 +484,7 @@ class DocumentManager { $condition_session = " AND (id_session = '$current_session_id' OR id_session = '0')"; //condition for search (get ALL folders and documents) - + $sql = "SELECT docs.id, docs.filetype, docs.path, @@ -494,13 +497,13 @@ class DocumentManager { last.lastedit_date, last.visibility, last.insert_user_id - FROM ".$TABLE_ITEMPROPERTY." AS last INNER JOIN ".$TABLE_DOCUMENT." AS docs - ON (docs.id = last.ref AND last.tool = '".TOOL_DOCUMENT."' AND docs.c_id = {$_course['real_id']} AND last.c_id = {$_course['real_id']}) + FROM " . $TABLE_ITEMPROPERTY . " AS last INNER JOIN " . $TABLE_DOCUMENT . " AS docs + ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND docs.c_id = {$_course['real_id']} AND last.c_id = {$_course['real_id']}) WHERE - docs.path LIKE '".$path.$added_slash."%' AND - docs.path NOT LIKE '".$path.$added_slash."%/%' AND - ".$to_field." = ".$to_value." AND - last.visibility".$visibility_bit.$condition_session; + docs.path LIKE '" . $path . $added_slash . "%' AND + docs.path NOT LIKE '" . $path . $added_slash . "%/%' AND + " . $to_field . " = " . $to_value . " AND + last.visibility" . $visibility_bit . $condition_session; $result = Database::query($sql); $doc_list = array(); @@ -531,9 +534,9 @@ class DocumentManager { //Templates management $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES); $sql_is_template = "SELECT id FROM $table_template - WHERE course_code = '".$_course['code']."' - AND user_id = '".api_get_user_id()."' - AND ref_doc = '".$row['id']."'"; + WHERE course_code = '" . $_course['code'] . "' + AND user_id = '" . api_get_user_id() . "' + AND ref_doc = '" . $row['id'] . "'"; $template_result = Database::query($sql_is_template); $row['is_template'] = (Database::num_rows($template_result) > 0) ? 1 : 0; } @@ -544,21 +547,20 @@ class DocumentManager { //Only for the student we filter the results see BT#1652 if (!api_is_coach() && !$is_allowed_to_edit) { $ids_to_remove = array(); - $my_repeat_ids = $temp= array(); + $my_repeat_ids = $temp = array(); //Selecting repetead ids - foreach($doc_list as $row ) { + foreach ($doc_list as $row) { if (in_array($row['id'], array_keys($temp))) { $my_repeat_ids[] = $row['id']; } $temp[$row['id']] = $row; } - //@todo use the DocumentManager::is_visible function - + //@todo use the DocumentManager::is_visible function //Checking disponibility in a session - foreach($my_repeat_ids as $id) { - foreach($doc_list as $row ) { - if ($id == $row['id']) { + foreach ($my_repeat_ids as $id) { + foreach ($doc_list as $row) { + if ($id == $row['id']) { if ($row['visibility'] == 0 && $row['item_property_session_id'] == 0) { $delete_repeated[$id] = true; } @@ -569,14 +571,14 @@ class DocumentManager { } } - foreach($doc_list as $key=>$row) { - if (in_array($row['visibility'], array('0','2')) && !in_array($row['id'],$my_repeat_ids) ) { + foreach ($doc_list as $key => $row) { + if (in_array($row['visibility'], array('0', '2')) && !in_array($row['id'], $my_repeat_ids)) { $ids_to_remove[] = $row['id']; unset($doc_list[$key]); } } - - foreach($document_data as $row) { + + foreach ($document_data as $row) { if (in_array($row['id'], $ids_to_remove)) { unset($document_data[$row['id']]); } @@ -588,14 +590,14 @@ class DocumentManager { //Checking parents visibility $final_document_data = array(); foreach ($document_data as $row) { - $is_visible = DocumentManager::check_visibility_tree($row['id'], $_course['code'], $current_session_id, api_get_user_id()); - if ($is_visible) { - $final_document_data[$row['id']] = $row; - } + $is_visible = DocumentManager::check_visibility_tree($row['id'], $_course['code'], $current_session_id, api_get_user_id()); + if ($is_visible) { + $final_document_data[$row['id']] = $row; + } } } else { - $final_document_data = $document_data; - } + $final_document_data = $document_data; + } return $final_document_data; } else { //display_error("Error getting document info from database (".Database::error().")!"); @@ -611,9 +613,9 @@ class DocumentManager { * @param int $to_group_id * @return array with paths */ - public static function get_all_document_folders ($_course, $to_group_id = '0', $can_see_invisible = false) { + public static function get_all_document_folders($_course, $to_group_id = '0', $can_see_invisible = false) { $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); - $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); + $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); $to_group_id = intval($to_group_id); @@ -623,10 +625,10 @@ class DocumentManager { $condition_session = api_get_session_condition($session_id); $sql = "SELECT DISTINCT docs.id, path FROM $TABLE_ITEMPROPERTY AS last INNER JOIN $TABLE_DOCUMENT AS docs - ON (docs.id = last.ref AND last.tool = '".TOOL_DOCUMENT."' AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} ) + ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} ) WHERE docs.filetype = 'folder' AND - last.to_group_id = ".$to_group_id." AND + last.to_group_id = " . $to_group_id . " AND last.visibility <> 2 $condition_session "; $result = Database::query($sql); @@ -648,17 +650,16 @@ class DocumentManager { } } else { //no invisible folders - //condition for the session $session_id = api_get_session_id(); $condition_session = api_get_session_condition($session_id); //get visible folders $visible_sql = "SELECT DISTINCT docs.id, path - FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs + FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs WHERE docs.id = last.ref AND docs.filetype = 'folder' - AND last.tool = '".TOOL_DOCUMENT."' - AND last.to_group_id = ".$to_group_id." + AND last.tool = '" . TOOL_DOCUMENT . "' + AND last.to_group_id = " . $to_group_id . " AND last.visibility = 1 $condition_session AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} "; @@ -671,11 +672,11 @@ class DocumentManager { $condition_session = api_get_session_condition($session_id); //get invisible folders $invisible_sql = "SELECT DISTINCT docs.id, path - FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs + FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs WHERE docs.id = last.ref AND docs.filetype = 'folder' - AND last.tool = '".TOOL_DOCUMENT."' - AND last.to_group_id = ".$to_group_id." + AND last.tool = '" . TOOL_DOCUMENT . "' + AND last.to_group_id = " . $to_group_id . " AND last.visibility = 0 $condition_session AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} "; @@ -686,12 +687,12 @@ class DocumentManager { $condition_session = api_get_session_condition($session_id); //get visible folders in the invisible ones -> they are invisible too $folder_in_invisible_sql = "SELECT DISTINCT docs.id, path - FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs + FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs WHERE docs.id = last.ref - AND docs.path LIKE '".Database::escape_string($invisible_folders['path'])."/%' + AND docs.path LIKE '" . Database::escape_string($invisible_folders['path']) . "/%' AND docs.filetype = 'folder' - AND last.tool = '".TOOL_DOCUMENT."' - AND last.to_group_id = ".$to_group_id." + AND last.tool = '" . TOOL_DOCUMENT . "' + AND last.to_group_id = " . $to_group_id . " AND last.visibility = 1 $condition_session AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} "; @@ -707,12 +708,12 @@ class DocumentManager { natsort($document_folders); return $document_folders; } elseif (is_array($visiblefolders)) { - //only visible folders found + //only visible folders found //sort($visiblefolders); natsort($visiblefolders); return $visiblefolders; } else { - //no visible folders found + //no visible folders found return false; } } @@ -727,24 +728,24 @@ class DocumentManager { * @param string $file path stored in the database * @param int $document_id in case you dont have the file path ,insert the id of the file here and leave $file in blank '' * @return boolean true/false - **/ - public static function check_readonly($_course, $user_id, $file,$document_id = '', $to_delete = false) { + * */ + public static function check_readonly($_course, $user_id, $file, $document_id = '', $to_delete = false) { if (!(!empty($document_id) && is_numeric($document_id))) { $document_id = self::get_document_id($_course, $file); } $TABLE_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); - $course_id = $_course['real_id']; + $course_id = $_course['real_id']; if ($to_delete) { if (self::is_folder($_course, $document_id)) { if (!empty($file)) { $path = Database::escape_string($file); - $what_to_check_sql = "SELECT td.id, readonly, tp.insert_user_id FROM ".$TABLE_DOCUMENT." td , $TABLE_PROPERTY tp + $what_to_check_sql = "SELECT td.id, readonly, tp.insert_user_id FROM " . $TABLE_DOCUMENT . " td , $TABLE_PROPERTY tp WHERE td.c_id = $course_id AND tp.c_id = $course_id AND tp.ref= td.id AND - (path='".$path."' OR path LIKE BINARY '".$path."/%' ) "; + (path='" . $path . "' OR path LIKE BINARY '" . $path . "/%' ) "; //get all id's of documents that are deleted $what_to_check_result = Database::query($what_to_check_sql); @@ -771,13 +772,13 @@ class DocumentManager { } if (!empty($document_id)) { - $sql= "SELECT a.insert_user_id, b.readonly FROM $TABLE_PROPERTY a, $TABLE_DOCUMENT b + $sql = "SELECT a.insert_user_id, b.readonly FROM $TABLE_PROPERTY a, $TABLE_DOCUMENT b WHERE a.c_id = $course_id AND b.c_id = $course_id AND a.ref = b.id and a.ref= $document_id LIMIT 1"; - $resultans = Database::query($sql); - $doc_details = Database ::fetch_array($resultans, 'ASSOC'); + $resultans = Database::query($sql); + $doc_details = Database ::fetch_array($resultans, 'ASSOC'); if ($doc_details['readonly'] == 1) { return !($doc_details['insert_user_id'] == $user_id || api_is_platform_admin()); @@ -791,58 +792,57 @@ class DocumentManager { * @param array $_course * @param int $document_id of the item * @return boolean true/false - **/ + * */ public static function is_folder($_course, $document_id) { $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); - $course_id = $_course['real_id']; + $course_id = $_course['real_id']; $document_id = Database::escape_string($document_id); $result = Database::fetch_array(Database::query("SELECT filetype FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id= $document_id"), 'ASSOC'); return $result['filetype'] == 'folder'; } - + public static function delete_document_from_db($document_id, $course_info = array(), $session_id = 0, $remove_content_from_db = false) { - $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); + $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY); - - //Deleting from the DB + + //Deleting from the DB $user_id = api_get_user_id(); - + if (empty($course_info)) { $course_info = api_get_course_info(); } if (empty($session_id)) { $session_id = api_get_session_id(); } - + //Soft DB delete api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'delete', $user_id, null, null, null, null, $session_id); self::delete_document_from_search_engine($course_info['code'], $document_id); self::unset_document_as_template($document_id, $course_info['code'], $user_id); - + //Hard DB delete - if ($remove_content_from_db) { - $sql = "DELETE FROM $TABLE_ITEMPROPERTY WHERE c_id = {$course_info['real_id']} AND ref = ".$document_id." AND tool='".TOOL_DOCUMENT."'"; - Database::query($sql); - - $sql = "DELETE FROM ".$TABLE_DOCUMENT." WHERE c_id = {$course_info['real_id']} AND id = ".$document_id; + if ($remove_content_from_db) { + $sql = "DELETE FROM $TABLE_ITEMPROPERTY WHERE c_id = {$course_info['real_id']} AND ref = " . $document_id . " AND tool='" . TOOL_DOCUMENT . "'"; Database::query($sql); - + + $sql = "DELETE FROM " . $TABLE_DOCUMENT . " WHERE c_id = {$course_info['real_id']} AND id = " . $document_id; + Database::query($sql); + self::delete_document_metadata($document_id); } } - + public static function delete_document_metadata($document_id) { //needed to deleted medadata - require_once api_get_path(SYS_CODE_PATH).'metadata/md_funcs.php'; - require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php'; + require_once api_get_path(SYS_CODE_PATH) . 'metadata/md_funcs.php'; + require_once api_get_path(LIBRARY_PATH) . 'fileManage.lib.php'; $mdStore = new mdstore(true); - + //delete metadata - $eid = 'Document'.'.'.$document_id; + $eid = 'Document' . '.' . $document_id; $mdStore->mds_delete($eid); - $mdStore->mds_delete_offspring($eid); + $mdStore->mds_delete_offspring($eid); } - /** * This deletes a document by changing visibility to 2, renaming it to filename_DELETED_#id @@ -855,37 +855,37 @@ class DocumentManager { * @todo now only files/folders in a folder get visibility 2, we should rename them too. */ public static function delete_document($_course, $path, $base_work_dir) { - $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT); - + $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT); + if (empty($path) || empty($base_work_dir)) { return false; } - + $course_id = $_course['real_id']; //first, delete the actual document... $document_id = self :: get_document_id($_course, $path); - $document_exists_in_disk = file_exists($base_work_dir.$path); - - $new_path = $path.'_DELETED_'.$document_id; + $document_exists_in_disk = file_exists($base_work_dir . $path); + + $new_path = $path . '_DELETED_' . $document_id; $current_session_id = api_get_session_id(); - + $file_deleted_from_db = false; $file_deleted_from_disk = false; $file_renamed_from_disk = false; - + if ($document_id) { - self::delete_document_from_db($document_id); + self::delete_document_from_db($document_id); //checking //$file_exists_in_db = self::get_document_data_by_id($document_id, $_course['code']); $file_deleted_from_db = true; } - + if ($document_exists_in_disk) { - - if (api_get_setting('permanently_remove_deleted_files') == 'true') { + + if (api_get_setting('permanently_remove_deleted_files') == 'true') { //Deleted files are *really* deleted - $what_to_delete_sql = "SELECT id FROM ".$TABLE_DOCUMENT." WHERE c_id = $course_id AND path='".$path."' OR path LIKE BINARY '".$path."/%'"; + $what_to_delete_sql = "SELECT id FROM " . $TABLE_DOCUMENT . " WHERE c_id = $course_id AND path='" . $path . "' OR path LIKE BINARY '" . $path . "/%'"; //get all id's of documents that are deleted $what_to_delete_result = Database::query($what_to_delete_sql); @@ -898,53 +898,52 @@ class DocumentManager { //delete documents, do it like this so metadata get's deleted too //update_db_info('delete', $path); //throw it away - my_delete($base_work_dir.$path); + my_delete($base_work_dir . $path); $file_deleted_from_disk = true; } } else { //Set visibility to 2 and rename file/folder to xxx_DELETED_#id (soft delete) - - if (is_file($base_work_dir.$path) || is_dir($base_work_dir.$path)) { - if (rename($base_work_dir.$path, $base_work_dir.$new_path)) { - - $sql = "UPDATE $TABLE_DOCUMENT set path='".$new_path."' WHERE c_id = $course_id AND id='".$document_id."'"; + + if (is_file($base_work_dir . $path) || is_dir($base_work_dir . $path)) { + if (rename($base_work_dir . $path, $base_work_dir . $new_path)) { + + $sql = "UPDATE $TABLE_DOCUMENT set path='" . $new_path . "' WHERE c_id = $course_id AND id='" . $document_id . "'"; Database::query($sql); - - $sql = "SELECT id, path FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '".$path."/%'"; + + $sql = "SELECT id, path FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '" . $path . "/%'"; $result = Database::query($sql); if ($result && Database::num_rows($result) > 0) { - while ($deleted_items = Database::fetch_array($result, 'ASSOC')) { - self::delete_document_from_db($deleted_items['id']); - + while ($deleted_items = Database::fetch_array($result, 'ASSOC')) { + self::delete_document_from_db($deleted_items['id']); + //Change path of subfolders and documents in database $old_item_path = $deleted_items['path']; - $new_item_path = $new_path.substr($old_item_path, strlen($path)); + $new_item_path = $new_path . substr($old_item_path, strlen($path)); - $sql = "UPDATE $TABLE_DOCUMENT set path = '".$new_item_path."' WHERE c_id = $course_id AND id = ".$deleted_items['id']; + $sql = "UPDATE $TABLE_DOCUMENT set path = '" . $new_item_path . "' WHERE c_id = $course_id AND id = " . $deleted_items['id']; Database::query($sql); } } $file_renamed_from_disk = true; } else { //Couldn't rename - file permissions problem? - error_log(__FILE__.' '.__LINE__.': Error renaming '.$base_work_dir.$path.' to '.$base_work_dir.$new_path.'. This is probably due to file permissions',0); + error_log(__FILE__ . ' ' . __LINE__ . ': Error renaming ' . $base_work_dir . $path . ' to ' . $base_work_dir . $new_path . '. This is probably due to file permissions', 0); } - } - } + } + } } - + //Checking inconsistency - if ($file_deleted_from_db && $file_deleted_from_disk || - $file_deleted_from_db && $file_renamed_from_disk) { + if ($file_deleted_from_db && $file_deleted_from_disk || + $file_deleted_from_db && $file_renamed_from_disk) { return true; } else { //Something went wrong - //The file or directory isn't there anymore (on the filesystem) // This means it has been removed externally. To prevent a // blocking error from happening, we drop the related items from the // item_property and the document table. - error_log(__FILE__.' '.__LINE__.': System inconsistency detected. The file or directory '.$base_work_dir.$path.' seems to have been removed from the filesystem independently from the web platform. To restore consistency, the elements using the same path will be removed from the database',0); + error_log(__FILE__ . ' ' . __LINE__ . ': System inconsistency detected. The file or directory ' . $base_work_dir . $path . ' seems to have been removed from the filesystem independently from the web platform. To restore consistency, the elements using the same path will be removed from the database', 0); return false; } } @@ -955,7 +954,7 @@ class DocumentManager { * @param string $course_id Course code * @param int $document_id Document id to delete */ - public static function delete_document_from_search_engine ($course_id, $document_id) { + public static function delete_document_from_search_engine($course_id, $document_id) { // remove from search engine if enabled if (api_get_setting('search_enabled') == 'true') { $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF); @@ -964,16 +963,16 @@ class DocumentManager { $res = Database::query($sql); if (Database::num_rows($res) > 0) { $row2 = Database::fetch_array($res); - require_once api_get_path(LIBRARY_PATH) .'search/ChamiloIndexer.class.php'; + require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php'; $di = new ChamiloIndexer(); - $di->remove_document((int)$row2['search_did']); + $di->remove_document((int) $row2['search_did']); } $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1'; $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id); Database::query($sql); // remove terms from db - require_once api_get_path(LIBRARY_PATH) .'specific_fields_manager.lib.php'; + require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php'; delete_all_values_for_item($course_id, TOOL_DOCUMENT, $document_id); } } @@ -1010,41 +1009,41 @@ class DocumentManager { */ public static function get_document_data_by_id($id, $course_code, $load_parents = false) { $course_info = api_get_course_info($course_code); - $course_id = $course_info['real_id']; + $course_id = $course_info['real_id']; if (empty($course_info)) { return false; } - $www = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document'; + $www = api_get_path(WEB_COURSE_PATH) . $course_info['path'] . '/document'; $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT); $id = intval($id); $sql = "SELECT * FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id = $id "; $result = Database::query($sql); if ($result && Database::num_rows($result) == 1) { - $row = Database::fetch_array($result,'ASSOC'); + $row = Database::fetch_array($result, 'ASSOC'); //@todo need to clarify the name of the URLs not nice right now $url_path = urlencode($row['path']); - $path = str_replace('%2F', '/',$url_path); - - $row['url'] = api_get_path(WEB_CODE_PATH).'document/showinframes.php?cidReq='.$course_code.'&id='.$id; - $row['document_url'] = api_get_path(WEB_CODE_PATH).'document/document.php?cidReq='.$course_code.'&id='.$id; - $row['absolute_path'] = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$row['path']; - - $row['absolute_path_from_document'] = '/document'.$row['path']; - + $path = str_replace('%2F', '/', $url_path); + + $row['url'] = api_get_path(WEB_CODE_PATH) . 'document/showinframes.php?cidReq=' . $course_code . '&id=' . $id; + $row['document_url'] = api_get_path(WEB_CODE_PATH) . 'document/document.php?cidReq=' . $course_code . '&id=' . $id; + $row['absolute_path'] = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $row['path']; + + $row['absolute_path_from_document'] = '/document' . $row['path']; + $pathinfo = pathinfo($row['path']); - $row['absolute_parent_path'] = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$pathinfo['dirname'].'/'; + $row['absolute_parent_path'] = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $pathinfo['dirname'] . '/'; - $row['direct_url'] = $www.$path; + $row['direct_url'] = $www . $path; if (dirname($row['path']) == '.') { - $row['parent_id'] = '0'; + $row['parent_id'] = '0'; } else { - $row['parent_id'] = self::get_document_id($course_info, dirname($row['path'])); + $row['parent_id'] = self::get_document_id($course_info, dirname($row['path'])); } $parents = array(); @@ -1054,27 +1053,27 @@ class DocumentManager { $visibility = true; if ($load_parents) { - $dir_array = explode('/', $row['path']); - $dir_array = array_filter($dir_array); - $array_len = count($dir_array) +1 ; - $real_dir = ''; - - for ($i = 1; $i < $array_len; $i++) { - //$sub_visibility = true; - $real_dir .= '/'.$dir_array[$i]; - $parent_id = self::get_document_id($course_info, $real_dir); - if (!empty($parent_id)) { - $sub_document_data = self::get_document_data_by_id($parent_id, $course_code, false); - //@todo add visibility here - - /*$sub_visibility = self::is_visible_by_id($parent_id, $course_info, api_get_session_id(), api_get_user_id()); - if ($visibility && $sub_visibility == false) { - $visibility = false; - } - */ - $parents[] = $sub_document_data; - } - } + $dir_array = explode('/', $row['path']); + $dir_array = array_filter($dir_array); + $array_len = count($dir_array) + 1; + $real_dir = ''; + + for ($i = 1; $i < $array_len; $i++) { + //$sub_visibility = true; + $real_dir .= '/' . $dir_array[$i]; + $parent_id = self::get_document_id($course_info, $real_dir); + if (!empty($parent_id)) { + $sub_document_data = self::get_document_data_by_id($parent_id, $course_code, false); + //@todo add visibility here + + /* $sub_visibility = self::is_visible_by_id($parent_id, $course_info, api_get_session_id(), api_get_user_id()); + if ($visibility && $sub_visibility == false) { + $visibility = false; + } + */ + $parents[] = $sub_document_data; + } + } } //$row['visibility_for_user'] = $visibility; $row['parents'] = $parents; @@ -1083,7 +1082,6 @@ class DocumentManager { return false; } - /** * Allow to set a specific document as a new template for FCKEditor for a particular user in a particular course * @@ -1098,21 +1096,20 @@ class DocumentManager { $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES); // creating the sql statement - $sql = "INSERT INTO ".$table_template." + $sql = "INSERT INTO " . $table_template . " (title, description, course_code, user_id, ref_doc, image) VALUES ( - '".Database::escape_string($title)."', - '".Database::escape_string($description)."', - '".Database::escape_string($couse_code)."', - '".Database::escape_string($user_id)."', - '".Database::escape_string($document_id_for_template)."', - '".Database::escape_string($image)."')"; + '" . Database::escape_string($title) . "', + '" . Database::escape_string($description) . "', + '" . Database::escape_string($couse_code) . "', + '" . Database::escape_string($user_id) . "', + '" . Database::escape_string($document_id_for_template) . "', + '" . Database::escape_string($image) . "')"; Database::query($sql); return true; } - /** * Unset a document as template * @@ -1127,14 +1124,14 @@ class DocumentManager { $user_id = Database::escape_string($user_id); $document_id = Database::escape_string($document_id); - $sql = 'SELECT id FROM '.$table_template.' WHERE course_code="'.$course_code.'" AND user_id="'.$user_id.'" AND ref_doc="'.$document_id.'"'; + $sql = 'SELECT id FROM ' . $table_template . ' WHERE course_code="' . $course_code . '" AND user_id="' . $user_id . '" AND ref_doc="' . $document_id . '"'; $result = Database::query($sql); - $template_id = Database::result($result,0,0); + $template_id = Database::result($result, 0, 0); include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php'); - my_delete(api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/'.$template_id.'.jpg'); + my_delete(api_get_path(SYS_CODE_PATH) . 'upload/template_thumbnails/' . $template_id . '.jpg'); - $sql = 'DELETE FROM '.$table_template.' WHERE course_code="'.$course_code.'" AND user_id="'.$user_id.'" AND ref_doc="'.$document_id.'"'; + $sql = 'DELETE FROM ' . $table_template . ' WHERE course_code="' . $course_code . '" AND user_id="' . $user_id . '" AND ref_doc="' . $document_id . '"'; Database::query($sql); } @@ -1146,7 +1143,7 @@ class DocumentManager { * @param array $course the _course array info of the document's course */ public static function is_visible($doc_path, $course, $session_id = 0, $file_type = 'file') { - $docTable = Database::get_course_table(TABLE_DOCUMENT); + $docTable = Database::get_course_table(TABLE_DOCUMENT); $propTable = Database::get_course_table(TABLE_ITEM_PROPERTY); $course_id = $course['real_id']; @@ -1157,16 +1154,16 @@ class DocumentManager { $condition = "AND id_session IN ('$session_id', '0') "; // The " d.filetype='file' " let the user see a file even if the folder is hidden see #2198 - /* - When using hotpotatoes files, a new html files are generated in the hotpotatoes folder - to display the test. - The genuine html file is copied to math4.htm(user_id).t.html - Images files are not copied, and keep same name. - To check the html file visibility, we don't have to check file math4.htm(user_id).t.html but file math4.htm - In this case, we have to remove (user_id).t.html to check the visibility of the file - For images, we just check the path of the image file. - - Exemple of hotpotatoes folder : + /* + When using hotpotatoes files, a new html files are generated in the hotpotatoes folder + to display the test. + The genuine html file is copied to math4.htm(user_id).t.html + Images files are not copied, and keep same name. + To check the html file visibility, we don't have to check file math4.htm(user_id).t.html but file math4.htm + In this case, we have to remove (user_id).t.html to check the visibility of the file + For images, we just check the path of the image file. + + Exemple of hotpotatoes folder : A.jpg maths4-consigne.jpg maths4.htm @@ -1175,26 +1172,26 @@ class DocumentManager { maths4.htm654.t.html omega.jpg theta.jpg - */ + */ if (strpos($doc_path, 'HotPotatoes_files') && preg_match("/\.t\.html$/", $doc_path)) { $doc_path = substr($doc_path, 0, strlen($doc_path) - 7 - strlen(api_get_user_id())); } - if (!in_array($file_type, array('file','folder'))) { + if (!in_array($file_type, array('file', 'folder'))) { $file_type = 'file'; } - $sql = "SELECT visibility FROM $docTable d, $propTable ip + $sql = "SELECT visibility FROM $docTable d, $propTable ip WHERE d.c_id = $course_id AND ip.c_id = $course_id AND d.id = ip.ref AND - ip.tool = '".TOOL_DOCUMENT."' $condition AND - filetype = '$file_type' AND locate(concat(path,'/'),'".$doc_path."/')=1"; + ip.tool = '" . TOOL_DOCUMENT . "' $condition AND + filetype = '$file_type' AND locate(concat(path,'/'),'" . $doc_path . "/')=1"; $result = Database::query($sql); $is_visible = false; if (Database::num_rows($result) > 0) { - $row = Database::fetch_array($result,'ASSOC'); + $row = Database::fetch_array($result, 'ASSOC'); if ($row['visibility'] == 1) { $is_visible = $_SESSION['is_allowed_in_course'] || api_is_platform_admin(); } @@ -1213,107 +1210,105 @@ class DocumentManager { * @return bool */ public static function is_visible_by_id($doc_id, $course_info, $session_id, $user_id, $admins_can_see_everything = true) { - $is_visible = false; - $user_in_course = false; - - //1. Checking the course array - if (empty($course_info)) { - $course_info = api_get_course_info(); - if (empty($course_info)) { - return false; - } - } + $is_visible = false; + $user_in_course = false; - $doc_id = intval($doc_id); - $session_id = intval($session_id); + //1. Checking the course array + if (empty($course_info)) { + $course_info = api_get_course_info(); + if (empty($course_info)) { + return false; + } + } + $doc_id = intval($doc_id); + $session_id = intval($session_id); - //2. Course and Session visibility are handle in local.inc.php/global.inc.php - //3. Checking if user exist in course/session + //2. Course and Session visibility are handle in local.inc.php/global.inc.php + //3. Checking if user exist in course/session - if ($session_id == 0 ) { - if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code']) || api_is_platform_admin()) { - $user_in_course = true; - } - //Check if course is open then we can consider that the student is regitered to the course - if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) { - $user_in_course = true; - } - } else { - $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id); - if (in_array($user_status, array('0', '2', '6'))) { - //is true if is an student, course session teacher or coach - $user_in_course = true; - } - } + if ($session_id == 0) { + if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code']) || api_is_platform_admin()) { + $user_in_course = true; + } + //Check if course is open then we can consider that the student is regitered to the course + if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) { + $user_in_course = true; + } + } else { + $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id); + if (in_array($user_status, array('0', '2', '6'))) { + //is true if is an student, course session teacher or coach + $user_in_course = true; + } + } - //4. Checking document visibility (i'm repeating the code in order to be more clear when reading ) - jm + //4. Checking document visibility (i'm repeating the code in order to be more clear when reading ) - jm - if ($user_in_course) { + if ($user_in_course) { - //4.1 Checking document visibility for a Course + //4.1 Checking document visibility for a Course - if ($session_id == 0) { - $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0); + if ($session_id == 0) { + $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0); - if (isset($item_info['visibility'])) { - // True for admins if document exists - if ($admins_can_see_everything && api_is_platform_admin()) { - return true; - } - if ($item_info['visibility'] == 1) { - return true; - } - } - } else { - //4.2 Checking document visibility for a Course in a Session - $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0); - $item_info_in_session = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, $session_id); - - // True for admins if document exists - if (isset($item_info['visibility'])) { - if ($admins_can_see_everything && api_is_platform_admin()) - return true; - } + if (isset($item_info['visibility'])) { + // True for admins if document exists + if ($admins_can_see_everything && api_is_platform_admin()) { + return true; + } + if ($item_info['visibility'] == 1) { + return true; + } + } + } else { + //4.2 Checking document visibility for a Course in a Session + $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0); + $item_info_in_session = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, $session_id); + + // True for admins if document exists + if (isset($item_info['visibility'])) { + if ($admins_can_see_everything && api_is_platform_admin()) + return true; + } - if (isset($item_info_in_session['visibility'])) { - //if ($doc_id == 85) { var_dump($item_info_in_session);} - if ($item_info_in_session['visibility'] == 1) { - return true; - } - } else { - if ($item_info['visibility'] == 1) { - return true; - } - } - } - } elseif ($admins_can_see_everything && api_is_platform_admin()) { + if (isset($item_info_in_session['visibility'])) { + //if ($doc_id == 85) { var_dump($item_info_in_session);} + if ($item_info_in_session['visibility'] == 1) { + return true; + } + } else { + if ($item_info['visibility'] == 1) { + return true; + } + } + } + } elseif ($admins_can_see_everything && api_is_platform_admin()) { return true; } - return false; + return false; } - /** * Allow attach a certificate to a course * @param string The course id * @param int The document id * @return void() */ - function attach_gradebook_certificate ($course_id, $document_id) { + function attach_gradebook_certificate($course_id, $document_id) { $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); $session_id = api_get_session_id(); - if ($session_id==0 || is_null($session_id)) { - $sql_session='AND (session_id='.Database::escape_string($session_id).' OR isnull(session_id)) '; - } elseif ($session_id>0) { - $sql_session='AND session_id='.Database::escape_string($session_id); + if ($session_id == 0 || is_null($session_id)) { + $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) '; + } elseif ($session_id > 0) { + $sql_session = 'AND session_id=' . Database::escape_string($session_id); } else { - $sql_session=''; + $sql_session = ''; } - $sql='UPDATE '.$tbl_category.' SET document_id="'.Database::escape_string($document_id).'" - WHERE course_code="'.Database::escape_string($course_id).'" '.$sql_session; + $sql = 'UPDATE ' . $tbl_category . ' SET document_id="' . Database::escape_string($document_id) . '" + WHERE course_code="' . Database::escape_string($course_id) . '" ' . $sql_session; Database::query($sql); } @@ -1323,18 +1318,18 @@ class DocumentManager { * @return int The default certificate id */ static function get_default_certificate_id($course_id) { - $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); - $session_id = api_get_session_id(); - if ($session_id==0 || is_null($session_id)) { - $sql_session='AND (session_id='.Database::escape_string($session_id).' OR isnull(session_id)) '; - } elseif ($session_id>0) { - $sql_session='AND session_id='.Database::escape_string($session_id); + $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); + $session_id = api_get_session_id(); + if ($session_id == 0 || is_null($session_id)) { + $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) '; + } elseif ($session_id > 0) { + $sql_session = 'AND session_id=' . Database::escape_string($session_id); } else { - $sql_session=''; + $sql_session = ''; } - $sql = 'SELECT document_id FROM '.$tbl_category.' WHERE course_code="'.Database::escape_string($course_id).'" '.$sql_session; - $rs = Database::query($sql); - $num = Database::num_rows($rs); + $sql = 'SELECT document_id FROM ' . $tbl_category . ' WHERE course_code="' . Database::escape_string($course_id) . '" ' . $sql_session; + $rs = Database::query($sql); + $num = Database::num_rows($rs); if ($num == 0) { return null; } @@ -1348,11 +1343,11 @@ class DocumentManager { * @return string The html content of the certificate */ static function replace_user_info_into_html($user_id, $course_code, $is_preview = false) { - $user_id = intval($user_id); - $course_info = api_get_course_info($course_code); - $tbl_document = Database::get_course_table(TABLE_DOCUMENT); - $course_id = $course_info['real_id']; - $document_id = self::get_default_certificate_id($course_code); + $user_id = intval($user_id); + $course_info = api_get_course_info($course_code); + $tbl_document = Database::get_course_table(TABLE_DOCUMENT); + $course_id = $course_info['real_id']; + $document_id = self::get_default_certificate_id($course_code); if ($document_id) { $sql = "SELECT path FROM $tbl_document WHERE c_id = $course_id AND id = $document_id"; @@ -1361,14 +1356,14 @@ class DocumentManager { $all_user_info = array(); if (Database::num_rows($rs)) { $row = Database::fetch_array($rs); - $filepath = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$row['path']; + $filepath = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $row['path']; if (is_file($filepath)) { $my_content_html = file_get_contents($filepath); } $all_user_info = self::get_all_info_to_certificate($user_id, $course_code, $is_preview); - $info_to_be_replaced_in_content_html=$all_user_info[0]; - $info_to_replace_in_content_html=$all_user_info[1]; - $new_content=str_replace($info_to_be_replaced_in_content_html,$info_to_replace_in_content_html, $my_content_html); + $info_to_be_replaced_in_content_html = $all_user_info[0]; + $info_to_replace_in_content_html = $all_user_info[1]; + $new_content = str_replace($info_to_be_replaced_in_content_html, $info_to_replace_in_content_html, $my_content_html); } return array('content' => $new_content, 'variables' => $all_user_info); } @@ -1379,23 +1374,23 @@ class DocumentManager { * return all content to replace and all content to be replace */ static function get_all_info_to_certificate($user_id, $course_id, $is_preview = false) { - $info_list = array(); - $user_id = intval($user_id); + $info_list = array(); + $user_id = intval($user_id); $course_info = api_get_course_info($course_id); //info portal $organization_name = api_get_setting('Institution'); - $portal_name = api_get_setting('siteName'); + $portal_name = api_get_setting('siteName'); //Extra user data information $extra_user_info_data = UserManager::get_extra_user_data($user_id, false, false, false, true); //Student information - $user_info = api_get_user_info($user_id); - $first_name = $user_info['firstname']; - $last_name = $user_info['lastname']; - $official_code = $user_info['official_code']; + $user_info = api_get_user_info($user_id); + $first_name = $user_info['firstname']; + $last_name = $user_info['lastname']; + $official_code = $user_info['official_code']; //Teacher information $info_teacher_id = UserManager::get_user_id_of_course_admin_or_session_admin($course_id); @@ -1418,76 +1413,77 @@ class DocumentManager { $date_no_time = api_convert_and_format_date(api_get_utc_datetime(), DATE_FORMAT_LONG_NO_DAY); } - $url = api_get_path(WEB_PATH).'certificates/index.php?id='.$info_grade_certificate['id']; + $url = api_get_path(WEB_PATH) . 'certificates/index.php?id=' . $info_grade_certificate['id']; + - //replace content - $info_to_replace_in_content_html = array($first_name, - $last_name, - $organization_name, - $portal_name, - $teacher_first_name, - $teacher_last_name, - $official_code, - $date_long_certificate, - $date_no_time, - $course_id, - $course_info['name'], - $info_grade_certificate['grade'], - $url, - ''.get_lang('CertificateOnlineLink').'', - '((certificate_barcode))', - ); - + $info_to_replace_in_content_html = array($first_name, + $last_name, + $organization_name, + $portal_name, + $teacher_first_name, + $teacher_last_name, + $official_code, + $date_long_certificate, + $date_no_time, + $course_id, + $course_info['name'], + $info_grade_certificate['grade'], + $url, + '' . get_lang('CertificateOnlineLink') . '', + '((certificate_barcode))', + ); + $info_to_be_replaced_in_content_html = array('((user_firstname))', - '((user_lastname))', - '((gradebook_institution))', - '((gradebook_sitename))', - '((teacher_firstname))', - '((teacher_lastname))', - '((official_code))', - '((date_certificate))', - '((date_certificate_no_time))', - '((course_code))', - '((course_title))', - '((gradebook_grade))', - '((certificate_link))', - '((certificate_link_html))', - '((certificate_barcode))', - ); + '((user_lastname))', + '((gradebook_institution))', + '((gradebook_sitename))', + '((teacher_firstname))', + '((teacher_lastname))', + '((official_code))', + '((date_certificate))', + '((date_certificate_no_time))', + '((course_code))', + '((course_title))', + '((gradebook_grade))', + '((certificate_link))', + '((certificate_link_html))', + '((certificate_barcode))', + ); if (!empty($extra_user_info_data)) { - foreach ($extra_user_info_data as $key_extra=>$value_extra) { - $info_to_be_replaced_in_content_html[]='(('.strtolower($key_extra).'))'; - $info_to_replace_in_content_html[]=$value_extra; + foreach ($extra_user_info_data as $key_extra => $value_extra) { + $info_to_be_replaced_in_content_html[] = '((' . strtolower($key_extra) . '))'; + $info_to_replace_in_content_html[] = $value_extra; } } - - $info_list[]=$info_to_be_replaced_in_content_html; - $info_list[]=$info_to_replace_in_content_html; + + $info_list[] = $info_to_be_replaced_in_content_html; + $info_list[] = $info_to_replace_in_content_html; return $info_list; } + /** * Remove default certificate * @param string The course id * @param int The document id of the default certificate * @return void() */ - function remove_attach_certificate ($course_id,$default_certificate_id) { - $default_certificate=self::get_default_certificate_id($course_id); - if ((int)$default_certificate==(int)$default_certificate_id) { - $tbl_category=Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); + function remove_attach_certificate($course_id, $default_certificate_id) { + $default_certificate = self::get_default_certificate_id($course_id); + if ((int) $default_certificate == (int) $default_certificate_id) { + $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); $session_id = api_get_session_id(); - if ($session_id==0 || is_null($session_id)) { - $sql_session='AND (session_id='.Database::escape_string($session_id).' OR isnull(session_id)) '; - } elseif ($session_id>0) { - $sql_session='AND session_id='.Database::escape_string($session_id); + if ($session_id == 0 || is_null($session_id)) { + $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) '; + } elseif ($session_id > 0) { + $sql_session = 'AND session_id=' . Database::escape_string($session_id); } else { - $sql_session=''; + $sql_session = ''; } - $sql='UPDATE '.$tbl_category.' SET document_id=null - WHERE course_code="'.Database::escape_string($course_id).'" AND document_id="'.$default_certificate_id.'" '.$sql_session; + $sql = 'UPDATE ' . $tbl_category . ' SET document_id=null + WHERE course_code="' . Database::escape_string($course_id) . '" AND document_id="' . $default_certificate_id . '" ' . $sql_session; Database::query($sql); } } @@ -1497,22 +1493,22 @@ class DocumentManager { * @param string The course id * @return void() */ - static function create_directory_certificate_in_course ($course_id) { + static function create_directory_certificate_in_course($course_id) { $course_info = api_get_course_info($course_id); if (!empty($course_info)) { - $to_group_id=0; - $to_user_id=null; - $course_dir = $course_info['path']."/document/"; + $to_group_id = 0; + $to_user_id = null; + $course_dir = $course_info['path'] . "/document/"; $sys_course_path = api_get_path(SYS_COURSE_PATH); - $base_work_dir=$sys_course_path.$course_dir; - $base_work_dir_test=$base_work_dir.'certificates'; - $dir_name='/certificates'; - $post_dir_name=get_lang('CertificatesFiles'); + $base_work_dir = $sys_course_path . $course_dir; + $base_work_dir_test = $base_work_dir . 'certificates'; + $dir_name = '/certificates'; + $post_dir_name = get_lang('CertificatesFiles'); $visibility_command = 'invisible'; - + if (!is_dir($base_work_dir_test)) { - $created_dir = create_unexisting_directory($course_info, api_get_user_id(), api_get_session_id(), $to_group_id,$to_user_id,$base_work_dir,$dir_name,$post_dir_name); - $update_id = self::get_document_id_of_directory_certificate(); + $created_dir = create_unexisting_directory($course_info, api_get_user_id(), api_get_session_id(), $to_group_id, $to_user_id, $base_work_dir, $dir_name, $post_dir_name); + $update_id = self::get_document_id_of_directory_certificate(); api_item_property_update($course_info, TOOL_DOCUMENT, $update_id, $visibility_command, api_get_user_id()); } } @@ -1523,8 +1519,8 @@ class DocumentManager { * @param string The course id * @return int The document id of the directory certificate */ - static function get_document_id_of_directory_certificate () { - $tbl_document=Database::get_course_table(TABLE_DOCUMENT); + static function get_document_id_of_directory_certificate() { + $tbl_document = Database::get_course_table(TABLE_DOCUMENT); $course_id = api_get_course_int_id(); $sql = "SELECT id FROM $tbl_document WHERE c_id = $course_id AND path='/certificates' "; $rs = Database::query($sql); @@ -1540,9 +1536,9 @@ class DocumentManager { static function is_certificate_mode($dir) { //I'm in the certification module? $is_certificate_mode = false; - $is_certificate_array = explode('/',$dir); + $is_certificate_array = explode('/', $dir); array_shift($is_certificate_array); - if ($is_certificate_array[0]=='certificates') { + if ($is_certificate_array[0] == 'certificates') { $is_certificate_mode = true; } return $is_certificate_mode; @@ -1576,7 +1572,6 @@ class DocumentManager { if (!$is_file) { $attributes = self::parse_HTML_attributes($source_html, $wanted_attributes); - } else { if (is_file($source_html)) { $abs_path = $source_html; @@ -1584,15 +1579,15 @@ class DocumentManager { $info = pathinfo($abs_path); $ext = $info['extension']; switch (strtolower($ext)) { - case 'html' : - case 'htm' : + case 'html' : + case 'htm' : case 'shtml': - case 'css' : + case 'css' : $file_content = file_get_contents($abs_path); //get an array of attributes from the HTML source $attributes = self::parse_HTML_attributes($file_content, $wanted_attributes); break; - default : + default : break; } } else { @@ -1610,15 +1605,19 @@ class DocumentManager { $sources = $attributes[$attr]; foreach ($sources as $source) { //skip what is obviously not a resource - if (strpos($source, '+this.')) continue; //javascript code - will still work unaltered - if (strpos($source, '.') === false) continue; //no dot, should not be an external file anyway - if (strpos($source, 'mailto:')) continue; //mailto link - if (strpos($source, ';') && !strpos($source, '&')) continue; //avoid code - that should help + if (strpos($source, '+this.')) + continue; //javascript code - will still work unaltered + if (strpos($source, '.') === false) + continue; //no dot, should not be an external file anyway + if (strpos($source, 'mailto:')) + continue; //mailto link + if (strpos($source, ';') && !strpos($source, '&')) + continue; //avoid code - that should help if ($attr == 'value') { - if (strpos($source , 'mp3file')) { + if (strpos($source, 'mp3file')) { $files_list[] = array(substr($source, 0, strpos($source, '.swf') + 4), 'local', 'abs'); - $mp3file = substr($source , strpos($source, 'mp3file=') + 8); + $mp3file = substr($source, strpos($source, 'mp3file=') + 8); if (substr($mp3file, 0, 1) == '/') { $files_list[] = array($mp3file, 'local', 'abs'); } else { @@ -1629,7 +1628,7 @@ class DocumentManager { if (strpos($source, '&') > 0) { $source = substr($source, 0, strpos($source, '&')); } - if (strpos($source,'://') > 0) { + if (strpos($source, '://') > 0) { if (strpos($source, api_get_path(WEB_PATH)) !== false) { //we found the current portal url $files_list[] = array($source, 'local', 'url'); @@ -1646,8 +1645,8 @@ class DocumentManager { if (strpos($source, '://') > 0) { //cut at '?' in a URL with params if (strpos($source, '?') > 0) { - $second_part = substr($source,strpos($source, '?')); - if(strpos($second_part, '://') > 0) { + $second_part = substr($source, strpos($source, '?')); + if (strpos($second_part, '://') > 0) { //if the second part of the url contains a url too, treat the second one before cutting $pos1 = strpos($second_part, '='); $pos2 = strpos($second_part, '&'); @@ -1671,16 +1670,16 @@ class DocumentManager { if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); } - } elseif(strstr($second_part, '..') === 0) { + } elseif (strstr($second_part, '..') === 0) { //link is relative but going back in the hierarchy $files_list[] = array($second_part, 'local', 'rel'); //$dir = api_get_path(SYS_CODE_PATH);//dirname($abs_path); //$new_abs_path = realpath($dir.'/'.$second_part); $dir = ''; if (!empty($abs_path)) { - $dir = dirname($abs_path).'/'; + $dir = dirname($abs_path) . '/'; } - $new_abs_path = realpath($dir.$second_part); + $new_abs_path = realpath($dir . $second_part); $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1); if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); @@ -1693,9 +1692,9 @@ class DocumentManager { $files_list[] = array($second_part, 'local', 'rel'); $dir = ''; if (!empty($abs_path)) { - $dir = dirname($abs_path).'/'; + $dir = dirname($abs_path) . '/'; } - $new_abs_path = realpath($dir.$second_part); + $new_abs_path = realpath($dir . $second_part); $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1); if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); @@ -1708,7 +1707,7 @@ class DocumentManager { if (strpos($source, api_get_path(WEB_PATH)) !== false) { //we found the current portal url $files_list[] = array($source, 'local', 'url'); - $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity+1); + $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1); if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); } @@ -1725,13 +1724,13 @@ class DocumentManager { if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); } - } elseif (strstr($source, '..') === 0) { //link is relative but going back in the hierarchy + } elseif (strstr($source, '..') === 0) { //link is relative but going back in the hierarchy $files_list[] = array($source, 'local', 'rel'); $dir = ''; if (!empty($abs_path)) { - $dir = dirname($abs_path).'/'; + $dir = dirname($abs_path) . '/'; } - $new_abs_path = realpath($dir.$source); + $new_abs_path = realpath($dir . $source); $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1); if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); @@ -1744,9 +1743,9 @@ class DocumentManager { $files_list[] = array($source, 'local', 'rel'); $dir = ''; if (!empty($abs_path)) { - $dir = dirname($abs_path).'/'; + $dir = dirname($abs_path) . '/'; } - $new_abs_path = realpath($dir.$source); + $new_abs_path = realpath($dir . $source); $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1); if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); @@ -1780,9 +1779,9 @@ class DocumentManager { $files_list[] = array($source, 'local', 'rel'); $dir = ''; if (!empty($abs_path)) { - $dir = dirname($abs_path).'/'; + $dir = dirname($abs_path) . '/'; } - $new_abs_path = realpath($dir.$source); + $new_abs_path = realpath($dir . $source); $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1); if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); @@ -1795,9 +1794,9 @@ class DocumentManager { $files_list[] = array($source, 'local', 'rel'); $dir = ''; if (!empty($abs_path)) { - $dir = dirname($abs_path).'/'; + $dir = dirname($abs_path) . '/'; } - $new_abs_path = realpath($dir.$source); + $new_abs_path = realpath($dir . $source); $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1); if (count($in_files_list) > 0) { $files_list = array_merge($files_list, $in_files_list); @@ -1815,7 +1814,7 @@ class DocumentManager { $checked_files_list = array(); $checked_array_list = array(); - if (count($files_list ) > 0) { + if (count($files_list) > 0) { foreach ($files_list as $idx => $file) { if (!empty($file[0])) { if (!in_array($file[0], $checked_files_list)) { @@ -1853,29 +1852,26 @@ class DocumentManager { // then possible closing brackets if we were in the opening bracket case // OR something like @import() $res = preg_match_all( - '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))' . - // '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]|[^\x00-\x7F])*)' . -> seems to be taking too much - // '/(((([A-Za-z_:])([^\x00-\x7F])*)' . -> takes only last letter of parameter name - '([ \n\t\r]+)?(' . - // '(=([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+))' . -> doesn't restrict close enough to the url itself - '(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))' . - '|' . - // '(\(([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)\))' . -> doesn't restrict close enough to the url itself - '(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))' . - '))' . - '|' . - // '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities) - '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/', - $attrString, - $regs + '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))' . + // '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]|[^\x00-\x7F])*)' . -> seems to be taking too much + // '/(((([A-Za-z_:])([^\x00-\x7F])*)' . -> takes only last letter of parameter name + '([ \n\t\r]+)?(' . + // '(=([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+))' . -> doesn't restrict close enough to the url itself + '(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))' . + '|' . + // '(\(([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)\))' . -> doesn't restrict close enough to the url itself + '(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))' . + '))' . + '|' . + // '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities) + '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/', $attrString, $regs ); - } catch (Exception $e) { - error_log('Caught exception: '. $e->getMessage(), 0) ; + error_log('Caught exception: ' . $e->getMessage(), 0); } if ($res) { for ($i = 0; $i < count($regs[1]); $i++) { - $name = trim($regs[3][$i]); + $name = trim($regs[3][$i]); $check = trim($regs[0][$i]); $value = trim($regs[10][$i]); if (empty($value) and !empty($regs[13][$i])) { @@ -1915,88 +1911,85 @@ class DocumentManager { * @return string new content html with replaced urls or return false if content is not a string */ static function replace_urls_inside_content_html_from_copy_course($content_html, $origin_course_code, $destination_course_directory) { - require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php'; + require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php'; if (!is_string($content_html)) { return false; } - $orig_source_html = DocumentManager::get_resources_from_source_html($content_html); - $orig_course_info = api_get_course_info($origin_course_code); - $orig_course_path = api_get_path(SYS_PATH).'courses/'.$orig_course_info['path'].'/'; - $destination_course_code = CourseManager::get_course_id_from_path ($destination_course_directory); + $orig_source_html = DocumentManager::get_resources_from_source_html($content_html); + $orig_course_info = api_get_course_info($origin_course_code); + $orig_course_path = api_get_path(SYS_PATH) . 'courses/' . $orig_course_info['path'] . '/'; + $destination_course_code = CourseManager::get_course_id_from_path($destination_course_directory); $destination_course_info = api_get_course_info($destination_course_code); - $dest_course_path = api_get_path(SYS_COURSE_PATH).$destination_course_directory.'/'; - + $dest_course_path = api_get_path(SYS_COURSE_PATH) . $destination_course_directory . '/'; + $user_id = api_get_user_id(); - + if (!empty($orig_source_html)) { - foreach ($orig_source_html as $source) { + foreach ($orig_source_html as $source) { // get information about source url - $real_orig_url = $source[0]; // url - $scope_url = $source[1]; // scope (local, remote) - $type_url = $source[2]; // tyle (rel, abs, url) - - + $real_orig_url = $source[0]; // url + $scope_url = $source[1]; // scope (local, remote) + $type_url = $source[2]; // tyle (rel, abs, url) // Get path and query from origin url - $orig_parse_url = parse_url($real_orig_url); - $real_orig_path = $orig_parse_url['path']; + $orig_parse_url = parse_url($real_orig_url); + $real_orig_path = $orig_parse_url['path']; $real_orig_query = $orig_parse_url['query']; // Replace origin course code by destination course code from origin url query $dest_url_query = ''; - + if (!empty($real_orig_query)) { - $dest_url_query = '?'.$real_orig_query; - if (strpos($dest_url_query,$origin_course_code) !== false) { + $dest_url_query = '?' . $real_orig_query; + if (strpos($dest_url_query, $origin_course_code) !== false) { $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query); } } if ($scope_url == 'local') { - if ($type_url == 'abs' || $type_url == 'rel') { + if ($type_url == 'abs' || $type_url == 'rel') { $document_file = strstr($real_orig_path, 'document'); - - if (strpos($real_orig_path,$document_file) !== false) { - $origin_filepath = $orig_course_path.$document_file; - $destination_filepath = $dest_course_path.$document_file; + + if (strpos($real_orig_path, $document_file) !== false) { + $origin_filepath = $orig_course_path . $document_file; + $destination_filepath = $dest_course_path . $document_file; // copy origin file inside destination course if (file_exists($origin_filepath)) { $filepath_dir = dirname($destination_filepath); - + if (!is_dir($filepath_dir)) { $perm = api_get_permissions_for_new_directories(); - $result = @mkdir($filepath_dir, $perm, true); + $result = @mkdir($filepath_dir, $perm, true); if ($result) { - $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $filepath_dir); - + $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $filepath_dir); + //Add to item properties to the new folder $doc_id = add_document($destination_course_info, $filepath_to_add, 'folder', 0, basename($filepath_to_add)); api_item_property_update($destination_course_info, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $user_id, null, null, null, null); } } - + if (!file_exists($destination_filepath)) { $result = @copy($origin_filepath, $destination_filepath); - if ($result) { - + if ($result) { + $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $destination_filepath); $size = filesize($destination_filepath); - + //Add to item properties to the file $doc_id = add_document($destination_course_info, $filepath_to_add, 'file', $size, basename($filepath_to_add)); api_item_property_update($destination_course_info, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $user_id, null, null, null, null); } - } } // Replace origin course path by destination course path - if (strpos($content_html,$real_orig_url) !== false) { + if (strpos($content_html, $real_orig_url) !== false) { //$origin_course_code - $url_course_path = str_replace($orig_course_info['path'].'/'.$document_file, '', $real_orig_path); - $destination_url = $url_course_path.$destination_course_directory.'/'.$document_file.$dest_url_query; + $url_course_path = str_replace($orig_course_info['path'] . '/' . $document_file, '', $real_orig_path); + $destination_url = $url_course_path . $destination_course_directory . '/' . $document_file . $dest_url_query; //If the course code doesn't exist in the path? what we do? Nothing! see BT#1985 if (strpos($real_orig_path, $origin_course_code) === false) { @@ -2011,10 +2004,10 @@ class DocumentManager { if (strpos($real_orig_url, '?') === 0) { $dest_url = str_replace($origin_course_code, $destination_course_code, $real_orig_url); $content_html = str_replace($real_orig_url, $dest_url, $content_html); - } + } } else { if ($type_url == 'url') { - + } } } @@ -2032,19 +2025,19 @@ class DocumentManager { * @return string new content html with replaced urls or return false if content is not a string */ function replace_urls_inside_content_html_when_moving_file($file_name, $original_path, $destiny_path) { - if (substr($original_path, strlen($original_path) -1 , strlen($original_path)) == '/') { - $original = $original_path.$file_name; + if (substr($original_path, strlen($original_path) - 1, strlen($original_path)) == '/') { + $original = $original_path . $file_name; } else { - $original = $original_path.'/'.$file_name; + $original = $original_path . '/' . $file_name; } - if (substr($destiny_path, strlen($destiny_path) -1 , strlen($destiny_path)) == '/') { - $destination = $destiny_path.$file_name; + if (substr($destiny_path, strlen($destiny_path) - 1, strlen($destiny_path)) == '/') { + $destination = $destiny_path . $file_name; } else { - $destination = $destiny_path.'/'.$file_name; + $destination = $destiny_path . '/' . $file_name; } //var_dump("From $original ", "to $destination"); - $original_count = count(explode('/', $original)); - $destination_count = count(explode('/', $destination)); + $original_count = count(explode('/', $original)); + $destination_count = count(explode('/', $destination)); if ($original_count == $destination_count) { //Nothing to change return true; @@ -2058,12 +2051,12 @@ class DocumentManager { } //echo $original_count.' '.$destination_count; var_dump($mode); //We do not select the $original_path becayse the file was already moved - $content_html = file_get_contents($destiny_path.'/'.$file_name); - $destination_file = $destiny_path.'/'.$file_name; + $content_html = file_get_contents($destiny_path . '/' . $file_name); + $destination_file = $destiny_path . '/' . $file_name; $pre_original = strstr($original_path, 'document'); - $pre_destin = strstr($destiny_path, 'document'); + $pre_destin = strstr($destiny_path, 'document'); //var_dump ("pre_original $pre_original"); //var_dump ("pre_destin $pre_destin"); @@ -2074,9 +2067,9 @@ class DocumentManager { //var_dump ("pre_original $pre_original"); //var_dump ("pre_destin $pre_destin"); - $levels = count(explode('/', $pre_destin)) - 1 ; + $levels = count(explode('/', $pre_destin)) - 1; $link_to_add = ''; - for ($i=1; $i <=$levels ; $i++) { + for ($i = 1; $i <= $levels; $i++) { $link_to_add .= '../'; } @@ -2089,11 +2082,11 @@ class DocumentManager { } if ($pre_original != '') { - $pre_original = '..'.$pre_original.'/'; + $pre_original = '..' . $pre_original . '/'; } if ($pre_destin != '') { - $pre_destin = '..'.$pre_destin.'/'; + $pre_destin = '..' . $pre_destin . '/'; } //var_dump($pre_original); @@ -2102,53 +2095,51 @@ class DocumentManager { //var_dump($levels); $count_pre_destination_levels = 0; - foreach($levels as $item) { + foreach ($levels as $item) { if (!empty($item) && $item != '..') { $count_pre_destination_levels++; } } $count_pre_destination_levels--; //$count_pre_destination_levels = count() - 3; - if ($count_pre_destination_levels == 0 ) { + if ($count_pre_destination_levels == 0) { $count_pre_destination_levels = 1; } //echo '$count_pre_destination_levels '. $count_pre_destination_levels; $pre_remove = ''; - for ($i=1; $i <= $count_pre_destination_levels; $i++) { + for ($i = 1; $i <= $count_pre_destination_levels; $i++) { $pre_remove .='..\/'; } //var_dump(' link to add '.$link_to_add.' -- remove '.$pre_remove); - $orig_source_html = DocumentManager::get_resources_from_source_html($content_html); + $orig_source_html = DocumentManager::get_resources_from_source_html($content_html); //var_dump($orig_source_html); foreach ($orig_source_html as $source) { // get information about source url - $real_orig_url = $source[0]; // url - $scope_url = $source[1]; // scope (local, remote) - $type_url = $source[2]; // tyle (rel, abs, url) - - + $real_orig_url = $source[0]; // url + $scope_url = $source[1]; // scope (local, remote) + $type_url = $source[2]; // tyle (rel, abs, url) // Get path and query from origin url - $orig_parse_url = parse_url($real_orig_url); - $real_orig_path = $orig_parse_url['path']; + $orig_parse_url = parse_url($real_orig_url); + $real_orig_path = $orig_parse_url['path']; $real_orig_query = $orig_parse_url['query']; // Replace origin course code by destination course code from origin url query /* - $dest_url_query = ''; - if (!empty($real_orig_query)) { - $dest_url_query = '?'.$real_orig_query; - if (strpos($dest_url_query,$origin_course_code) !== false) { - $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query); - } - }*/ + $dest_url_query = ''; + if (!empty($real_orig_query)) { + $dest_url_query = '?'.$real_orig_query; + if (strpos($dest_url_query,$origin_course_code) !== false) { + $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query); + } + } */ if ($scope_url == 'local') { - if ( $type_url == 'abs' || $type_url == 'rel') { + if ($type_url == 'abs' || $type_url == 'rel') { $document_file = strstr($real_orig_path, 'document'); if (strpos($real_orig_path, $document_file) !== false) { @@ -2158,17 +2149,17 @@ class DocumentManager { $real_orig_url_temp = ''; if ($mode == 'inside') { $real_orig_url_temp = str_replace('../', '', $real_orig_url); - $destination_url = $link_to_add.$real_orig_url_temp; + $destination_url = $link_to_add . $real_orig_url_temp; } else { $real_orig_url_temp = $real_orig_url; - $destination_url = preg_replace("/".$pre_remove."/", '', $real_orig_url, 1); + $destination_url = preg_replace("/" . $pre_remove . "/", '', $real_orig_url, 1); } if ($real_orig_url == $destination_url) { echo 'continue2'; continue; } - var_dump($real_orig_url_temp.' - '.$destination_url); + var_dump($real_orig_url_temp . ' - ' . $destination_url); $content_html = str_replace($real_orig_url, $destination_url, $content_html); } } else { @@ -2182,10 +2173,10 @@ class DocumentManager { } public static function export_to_pdf($document_id, $course_code) { - require_once api_get_path(LIBRARY_PATH).'pdf.lib.php'; - $course_data = api_get_course_info($course_code); - $document_data = self::get_document_data_by_id($document_id, $course_code); - $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$document_data['path']; + require_once api_get_path(LIBRARY_PATH) . 'pdf.lib.php'; + $course_data = api_get_course_info($course_code); + $document_data = self::get_document_data_by_id($document_id, $course_code); + $file_path = api_get_path(SYS_COURSE_PATH) . $course_data['path'] . '/document' . $document_data['path']; $pdf = new PDF(); $pdf->html_to_pdf($file_path, $document_data['title'], $course_code); } @@ -2204,56 +2195,56 @@ class DocumentManager { * @return unknown_type */ public static function upload_document($files, $path, $title = null, $comment = null, $unzip = 0, $if_exists = null, $index_document = false, $show_output = false) { - require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php'; + require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php'; - $course_info = api_get_course_info(); - $course_dir = $course_info['path'].'/document'; - $sys_course_path = api_get_path(SYS_COURSE_PATH); - $base_work_dir = $sys_course_path.$course_dir; + $course_info = api_get_course_info(); + $course_dir = $course_info['path'] . '/document'; + $sys_course_path = api_get_path(SYS_COURSE_PATH); + $base_work_dir = $sys_course_path . $course_dir; if (isset($files['file'])) { - $upload_ok = process_uploaded_file($files['file'], $show_output); + $upload_ok = process_uploaded_file($files['file'], $show_output); if ($upload_ok) { // File got on the server without problems, now process it $new_path = handle_uploaded_document($course_info, $files['file'], $base_work_dir, $path, api_get_user_id(), api_get_group_id(), null, $unzip, $if_exists, $show_output); - + if ($new_path) { $docid = DocumentManager::get_document_id($course_info, $new_path); if (!empty($docid)) { - $table_document = Database::get_course_table(TABLE_DOCUMENT); - $params = array(); - + $table_document = Database::get_course_table(TABLE_DOCUMENT); + $params = array(); + if (!empty($title)) { - $params['title'] = get_document_title($title); + $params['title'] = get_document_title($title); } else { if (isset($if_exists) && $if_exists == 'rename') { - $new_path = basename($new_path); - $params['title'] = get_document_title($new_path); + $new_path = basename($new_path); + $params['title'] = get_document_title($new_path); } else { - $params['title'] = get_document_title($files['file']['name']); + $params['title'] = get_document_title($files['file']['name']); } } - + if (!empty($comment)) { - $params['comment'] = trim($comment); + $params['comment'] = trim($comment); } Database::update($table_document, $params, array('id = ? AND c_id = ? ' => array($docid, $course_info['real_id']))); } // Showing message when sending zip files if ($new_path === true && $unzip == 1 && $show_output) { - Display::display_confirmation_message(get_lang('UplUploadSucceeded').'
', false); + Display::display_confirmation_message(get_lang('UplUploadSucceeded') . '
', false); } - /*// Check for missing images in html files - $missing_files = check_for_missing_files($base_work_dir.$new_path); - if ($missing_files && $show_output) { - // Show a form to upload the missing files - Display::display_normal_message(build_missing_files_form($missing_files, $path, $files['file']['name']), false); - }*/ + /* // Check for missing images in html files + $missing_files = check_for_missing_files($base_work_dir.$new_path); + if ($missing_files && $show_output) { + // Show a form to upload the missing files + Display::display_normal_message(build_missing_files_form($missing_files, $path, $files['file']['name']), false); + } */ if ($index_document) { - self::index_document($docid,$course_info['code'],null,$_POST['language'],$_REQUEST,$if_exists); + self::index_document($docid, $course_info['code'], null, $_POST['language'], $_REQUEST, $if_exists); } if (!empty($docid) && is_numeric($docid)) { $document_data = self::get_document_data_by_id($docid, $course_info['code']); @@ -2271,7 +2262,6 @@ class DocumentManager { */ function get_text_content($doc_path, $doc_mime) { // TODO: review w$ compatibility - // Use usual exec output lines array to store stdout instead of a temp file // because we need to store it at RAM anyway before index on ChamiloIndexer object $ret_val = null; @@ -2335,16 +2325,14 @@ class DocumentManager { } if (isset($output)) { foreach ($output as & $line) { - $content .= $line."\n"; + $content .= $line . "\n"; } return $content; - } - else { + } else { return false; } } - /** * Calculates the total size of all documents in a course * @@ -2353,38 +2341,38 @@ class DocumentManager { * @return int total size */ static function documents_total_space($course_id = null, $group_id = null, $session_id = null) { - $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); - $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); - - if (isset($course_id)) { + $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); + $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); + + if (isset($course_id)) { $course_id = intval($course_id); } else { $course_id = api_get_course_int_id(); } - + $group_condition = null; - - if (isset($group_id)) { + + if (isset($group_id)) { $group_id = intval($group_id); - $group_condition = " AND props.to_group_id='".$group_id."' "; + $group_condition = " AND props.to_group_id='" . $group_id . "' "; } - + $session_condition = null; - - if (isset($session_id)) { + + if (isset($session_id)) { $session_id = intval($session_id); - $session_condition = " AND props.id_session='".$session_id."' "; - } - - $sql = "SELECT SUM(size) FROM ".$TABLE_ITEMPROPERTY." AS props, ".$TABLE_DOCUMENT." AS docs + $session_condition = " AND props.id_session='" . $session_id . "' "; + } + + $sql = "SELECT SUM(size) FROM " . $TABLE_ITEMPROPERTY . " AS props, " . $TABLE_DOCUMENT . " AS docs WHERE props.c_id = $course_id AND docs.c_id = $course_id AND docs.id = props.ref AND - props.tool = '".TOOL_DOCUMENT."' AND + props.tool = '" . TOOL_DOCUMENT . "' AND props.visibility <> 2 $group_condition $session_condition - "; + "; $result = Database::query($sql); if ($result && Database::num_rows($result) != 0) { @@ -2395,7 +2383,6 @@ class DocumentManager { } } - /** * Here we count 1 kilobyte = 1000 byte, 12 megabyte = 1000 kilobyte. */ @@ -2403,19 +2390,19 @@ class DocumentManager { $course_quota_m = round($course_quota / 1000000); $already_consumed_space_m = round($already_consumed_space / 1000000); - $message = get_lang('MaximumAllowedQuota') . ' '.$course_quota_m.' megabyte.
'; + $message = get_lang('MaximumAllowedQuota') . ' ' . $course_quota_m . ' megabyte.
'; $message .= get_lang('CourseCurrentlyUses') . ' ' . $already_consumed_space_m . ' megabyte.
'; - $percentage = round( ($already_consumed_space / $course_quota * 100), 1); + $percentage = round(($already_consumed_space / $course_quota * 100), 1); $other_percentage = $percentage < 100 ? 100 - $percentage : 0; // Decide where to place percentage in graph if ($percentage >= 50) { - $text_in_filled = ' '.$other_percentage.'%'; + $text_in_filled = ' ' . $other_percentage . '%'; $text_in_unfilled = ''; } else { - $text_in_unfilled = ' '.$other_percentage.'%'; + $text_in_unfilled = ' ' . $other_percentage . '%'; $text_in_filled = ''; } @@ -2432,17 +2419,16 @@ class DocumentManager { $visual_percentage = 4 * $percentage; $visual_other_percentage = 4 * $other_percentage; - $message .= get_lang('PercentageQuotaInUse') . ': '.$percentage.'%.
' . - get_lang('PercentageQuotaFree') . ': '.$other_percentage.'%.
'; + $message .= get_lang('PercentageQuotaInUse') . ': ' . $percentage . '%.
' . + get_lang('PercentageQuotaFree') . ': ' . $other_percentage . '%.
'; - $show_percentage = ' '.$percentage.'%'; - $message .= '
'. - '
 
'. - ''.$show_percentage.'
'; + $show_percentage = ' ' . $percentage . '%'; + $message .= '
' . + '
 
' . + '' . $show_percentage . '
'; echo $message; } - /** * Display the document quota in a simple way * @@ -2452,13 +2438,12 @@ class DocumentManager { $course_quota_m = round($course_quota / 1000000); $already_consumed_space_m = round($already_consumed_space / 1000000, 2); $percentage = $already_consumed_space / $course_quota * 100; - $percentage = round($percentage, 1); + $percentage = round($percentage, 1); $message = get_lang('YouAreCurrentlyUsingXOfYourX'); - $message = sprintf($message, $already_consumed_space_m, $percentage.'%',$course_quota_m.' '); - echo Display::div($message, array('id'=>'document_quota')); + $message = sprintf($message, $already_consumed_space_m, $percentage . '%', $course_quota_m . ' '); + echo Display::div($message, array('id' => 'document_quota')); } - /** * Checks if there is enough place to add a file on a directory * on the base of a maximum directory size allowed @@ -2480,16 +2465,15 @@ class DocumentManager { } return true; } - + /** * * @param array paremeters: count, url, extension * @return string */ - static function generate_jplayer_jquery($params = array()) { - $js_path = api_get_path(WEB_LIBRARY_PATH).'javascript/'; - + $js_path = api_get_path(WEB_LIBRARY_PATH) . 'javascript/'; + $jplayer_definition = ' $("#jquery_jplayer_' . $params['count'] . '").jPlayer({ ready: function() { $(this).jPlayer("setMedia", { @@ -2519,7 +2503,7 @@ class DocumentManager { */ static function generate_media_preview($i, $type = 'simple') { $i = intval($i); - + $extra_controls = $progress = ''; if ($type == 'advanced') { $extra_controls = '
  • @@ -2531,18 +2515,18 @@ class DocumentManager { '; } - + //Shows only the play button - $html = '
    -
    + $html = ' + '; @@ -2587,14 +2571,14 @@ class DocumentManager {
    - '.get_lang('UpdateRequire').' - '.get_lang("ToPlayTheMediaYouWillNeedToUpdateYourBrowserToARecentVersionYouCanAlsoDownloadTheFile").' + ' . get_lang('UpdateRequire') . ' + ' . get_lang("ToPlayTheMediaYouWillNeedToUpdateYourBrowserToARecentVersionYouCanAlsoDownloadTheFile") . '
    '; @@ -2602,148 +2586,148 @@ class DocumentManager { } static function get_document_preview($course_info, $lp_id = false, $target = '', $session_id = 0, $add_move_button = false, $filter_by_folder = null, $overwrite_url = null) { - if (empty($course_info['real_id']) || empty($course_info['code']) || !is_array($course_info)) { - return ''; - } - $user_id = api_get_user_id(); - $user_in_course = false; - - if (api_is_platform_admin()) { - $user_in_course = true; - } - - if (!$user_in_course) { - if (CourseManager::is_course_teacher($user_id, $course_info['code'])) { - $user_in_course = true; - } - } - - //condition for the session - $session_id = intval($session_id); - - if (!$user_in_course) { - if (empty($session_id)) { - if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'])) { - $user_in_course = true; - } - //Check if course is open then we can consider that the student is regitered to the course - if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) { - $user_in_course = true; - } - } else { - $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id); + if (empty($course_info['real_id']) || empty($course_info['code']) || !is_array($course_info)) { + return ''; + } + $user_id = api_get_user_id(); + $user_in_course = false; + + if (api_is_platform_admin()) { + $user_in_course = true; + } + + if (!$user_in_course) { + if (CourseManager::is_course_teacher($user_id, $course_info['code'])) { + $user_in_course = true; + } + } + + //condition for the session + $session_id = intval($session_id); + + if (!$user_in_course) { + if (empty($session_id)) { + if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'])) { + $user_in_course = true; + } + //Check if course is open then we can consider that the student is regitered to the course + if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) { + $user_in_course = true; + } + } else { + $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id); //is true if is an student, course session teacher or coach - if (in_array($user_status, array('0', '2', '6'))) { - $user_in_course = true; - } - } + if (in_array($user_status, array('0', '2', '6'))) { + $user_in_course = true; + } + } } - $tbl_doc = Database::get_course_table(TABLE_DOCUMENT); - $tbl_item_prop = Database::get_course_table(TABLE_ITEM_PROPERTY); + $tbl_doc = Database::get_course_table(TABLE_DOCUMENT); + $tbl_item_prop = Database::get_course_table(TABLE_ITEM_PROPERTY); - $path = '/'; - $path = Database::escape_string(str_replace('_', '\_', $path)); - $added_slash = ($path == '/') ? '' : '/'; + $path = '/'; + $path = Database::escape_string(str_replace('_', '\_', $path)); + $added_slash = ($path == '/') ? '' : '/'; - //$condition_session = " AND (id_session = '$session_id' OR (id_session = '0' AND insert_date <= (SELECT creation_date FROM $tbl_course WHERE code = '".$course_info['code']."' )))"; - $condition_session = " AND (id_session = '$session_id' OR id_session = '0' )"; - + //$condition_session = " AND (id_session = '$session_id' OR (id_session = '0' AND insert_date <= (SELECT creation_date FROM $tbl_course WHERE code = '".$course_info['code']."' )))"; + $condition_session = " AND (id_session = '$session_id' OR id_session = '0' )"; + $add_folder_filter = null; if (!empty($filter_by_folder)) { - $add_folder_filter = " AND docs.path LIKE '".Database::escape_string($filter_by_folder)."%'"; + $add_folder_filter = " AND docs.path LIKE '" . Database::escape_string($filter_by_folder) . "%'"; } - - $sql_doc = "SELECT last.visibility, docs.* + + $sql_doc = "SELECT last.visibility, docs.* FROM $tbl_item_prop AS last, $tbl_doc AS docs WHERE docs.id = last.ref AND - docs.path LIKE '".$path.$added_slash."%' AND + docs.path LIKE '" . $path . $added_slash . "%' AND docs.path NOT LIKE '%_DELETED_%' AND - last.tool = '".TOOL_DOCUMENT."' $condition_session AND + last.tool = '" . TOOL_DOCUMENT . "' $condition_session AND last.visibility = '1' AND docs.c_id = {$course_info['real_id']} AND last.c_id = {$course_info['real_id']} $add_folder_filter ORDER BY docs.title ASC"; - - $res_doc = Database::query($sql_doc); - $resources = Database::store_result($res_doc, 'ASSOC'); - $resources_sorted = array(); - $return = ''; - - if ($lp_id) { - $return .= '
    '; - $return .= Display::return_icon('new_doc.gif', '', array(), ICON_SIZE_SMALL); - $return .= Display::url(get_lang('NewDocument'), api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_DOCUMENT.'&lp_id='.$_SESSION['oLP']->lp_id); - $return .= '
    '; - } else { - $return .= Display::div(Display::url(Display::return_icon('close.png', get_lang('Close'), array(), ICON_SIZE_SMALL), ' javascript:void(0);', array('id'=>'close_div_'.$course_info['real_id'].'_'.$session_id,'class' =>'close_div')), array('style' => 'position:absolute;right:10px')); - } - - // If you want to debug it, I advise you to do "echo" on the eval statements. - if (!empty($resources) && $user_in_course) { - foreach ($resources as $resource) { - $is_visible = self::is_visible_by_id($resource['id'], $course_info, $session_id, api_get_user_id()); - if (!$is_visible) { - continue; - } - $resource_paths = explode('/', $resource['path']); - array_shift($resource_paths); - $path_to_eval = $last_path = ''; - $is_file = false; - - if ($resource['filetype'] == 'file') { - foreach ($resource_paths as $key => $resource_path) { - if ($key != count($resource_paths)-1) { - // It's a folder. - $path_to_eval .= "['$resource_path']['files']"; - } - $is_file = true; - } - } else { - foreach ($resource_paths as $key => $resource_path) { - if ($key != count($resource_paths) - 1) { - // It's a folder. - $path_to_eval .= "['$resource_path']['files']"; - } - } - } - $last_path = $resource_path; - - //$data = json_encode(array('title'=>$resource['title'], 'path'=>$last_path)); - //@todo not sure if it's a good thing using base64_encode. I tried with json_encode but i received the same error - //Some testing is needed in order to prove the performance - //Also change the explode to value from "/" to "|@j@|" it fixes #3780 - - $data = base64_encode($resource['title'].'|@j@|'.$last_path); - - if ($is_file) { - //for backward compatibility - if (empty($resource['title'])) { - $resource['title'] = basename($resource['path']); - } - eval ('$resources_sorted'.$path_to_eval.'['.$resource['id'].'] = "'.$data.'" ; '); - } else { - eval ('$resources_sorted'.$path_to_eval.'["'.$last_path.'"]["id"]='.$resource['id'].';'); - eval ('$resources_sorted'.$path_to_eval.'["'.$last_path.'"]["title"]= "'.api_htmlentities($resource['title']).'";'); - } - } - } - - $label = get_lang('Documents'); - - $new_array[$label] = array('id' => 0, 'files' => $resources_sorted); - - $write_result = self::write_resources_tree($course_info, $session_id, $new_array, 0, $lp_id, $target, $add_move_button, $overwrite_url); - - $return .= $write_result ; - - $img_path = api_get_path(WEB_IMG_PATH); - - if ($lp_id == false) { - $return .= ""; - } else { - //For LPs - $return .= ""; - } - if(!$user_in_course) { - $return = ''; - } - return $return; + } + if (!$user_in_course) { + $return = ''; + } + return $return; } /** - * Generate and return an HTML list of resources based on a given array. - * This list is used to show the course creator a list of available resources to choose from - * when creating a learning path. - * @param array Array of elements to add to the list - * @param integer Enables the tree display by shifting the new elements a certain distance to the right - * @return string The HTML list - */ + * Generate and return an HTML list of resources based on a given array. + * This list is used to show the course creator a list of available resources to choose from + * when creating a learning path. + * @param array Array of elements to add to the list + * @param integer Enables the tree display by shifting the new elements a certain distance to the right + * @return string The HTML list + */ public static function write_resources_tree($course_info, $session_id, $resources_sorted, $num = 0, $lp_id = false, $target = '', $add_move_button = false, $overwrite_url = null) { - require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php'; + require_once api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php'; - $img_path = api_get_path(WEB_IMG_PATH); - $img_sys_path = api_get_path(SYS_CODE_PATH).'img/'; - $web_code_path = api_get_path(WEB_CODE_PATH); + $img_path = api_get_path(WEB_IMG_PATH); + $img_sys_path = api_get_path(SYS_CODE_PATH) . 'img/'; + $web_code_path = api_get_path(WEB_CODE_PATH); - $return = ''; + $return = ''; - if (count($resources_sorted) > 0) { - foreach ($resources_sorted as $key => $resource) { - $title = isset($resource['title']) ? $resource['title'] : null; - if (empty($title)) { - $title = $key; - } - //echo '
    '; print_r($resource);
    -    			if (isset($resource['id']) && is_int($resource['id'])) {
    -    				// It's a folder.
    -    				//hide some folders
    -    				if (in_array($key, array('shared_folder','chat_files', 'HotPotatoes_files', 'css', 'certificates'))){
    -    					continue;
    -    				} elseif(preg_match('/_groupdocs/', $key)){
    -    					continue;
    -    				} elseif(preg_match('/sf_user_/', $key)){
    -    					continue;
    -    				} elseif(preg_match('/shared_folder_session_/', $key)){
    -    					continue;
    -    				}
    -
    -    				//trad some titles
    -    				if ($key=='images') {
    -    					$key=get_lang('Images');
    -    				} elseif($key=='gallery') {
    -    					$key=get_lang('Gallery');
    -    				} elseif($key=='flash') {
    -    					$key=get_lang('Flash');
    -    				} elseif($key=='audio'){
    -    					$key=get_lang('Audio');
    -    				} elseif($key=='video') {
    -    					$key=get_lang('Video');
    -    				}
    -
    -    				$onclick = '';
    -
    -    				if ($lp_id) {
    -    					$onclick = 'onclick="javascript: testResources(\'res_' . $resource['id'] . '\',\'img_' . $resource['id'] . '\')"';
    -    				}
    -
    -    				$return .= '