Commit 93601409ffc3250bbcac1a2f0a82100c58a827b8

Authored by Paul Barrett
1 parent ac7fc794

KTS-4308. Added support for time created, last modified user, last modified time, owner

For easier access to Create and Modify information for folders, Knowledgetree should save this information on the folders table

Fixed

Committed by: Paul Barrett

Reviewed by: Megan Watson
ktapi/KTAPIFolder.inc.php
... ... @@ -78,7 +78,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
78 78 * @param int $folderid
79 79 * @return KTAPI_Folder
80 80 */
81   - function &get(&$ktapi, $folderid)
  81 + function get(&$ktapi, $folderid)
82 82 {
83 83 assert(!is_null($ktapi));
84 84 assert(is_a($ktapi, 'KTAPI'));
... ... @@ -157,14 +157,13 @@ class KTAPI_Folder extends KTAPI_FolderItem
157 157 * @access protected
158 158 * @return Folder
159 159 */
160   - function &get_folder()
  160 + function get_folder()
161 161 {
162 162 return $this->folder;
163 163 }
164 164  
165   -
166 165 /**
167   - * This returns detailed information on the document.
  166 + * This returns detailed information on the folder object.
168 167 *
169 168 * @author KnowledgeTree Team
170 169 * @access public
... ... @@ -190,10 +189,47 @@ class KTAPI_Folder extends KTAPI_FolderItem
190 189 unset($detail['linked_folder_id']);
191 190 }
192 191  
  192 + $folder = $this->folder;
  193 +
  194 + // get the creator
  195 + $userid = $folder->getCreatorID();
  196 + $username='n/a';
  197 + if (is_numeric($userid))
  198 + {
  199 + $username = '* unknown *';
  200 + $user = User::get($userid);
  201 + if (!is_null($user) && !PEAR::isError($user))
  202 + {
  203 + $username = $user->getName();
  204 + }
  205 + }
  206 + $detail['created_by'] = $username;
  207 +
  208 + // get the creation date
  209 + $detail['created_date'] = $folder->getCreatedDateTime();
  210 +
  211 + // get the modified user
  212 + $userid = $folder->getModifiedUserId();
  213 + $username='n/a';
  214 + if (is_numeric($userid))
  215 + {
  216 + $username = '* unknown *';
  217 + $user = User::get($userid);
  218 + if (!is_null($user) && !PEAR::isError($user))
  219 + {
  220 + $username = $user->getName();
  221 + }
  222 + }
  223 + $detail['modified_by'] = $detail['updated_by'] = $username;
  224 +
  225 + // get the modified date
  226 + $detail['updated_date'] = $detail['modified_date'] = $folder->getLastModifiedDate();
  227 +
193 228 return $detail;
194 229 }
  230 +
195 231 /**
196   - * This clears the global object cashe of the folder class.
  232 + * This clears the global object cache of the folder class.
197 233 *
198 234 * @author KnowledgeTree Team
199 235 * @access public
... ... @@ -255,7 +291,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
255 291 * @param int $folderid
256 292 * @return KTAPI_Folder
257 293 */
258   - function &_get_folder_by_name($ktapi, $foldername, $folderid)
  294 + function _get_folder_by_name($ktapi, $foldername, $folderid)
259 295 {
260 296 $foldername=trim($foldername);
261 297 if (empty($foldername))
... ... @@ -296,7 +332,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
296 332 * @param string $foldername
297 333 * @return KTAPI_Folder
298 334 */
299   - function &get_folder_by_name($foldername)
  335 + function get_folder_by_name($foldername)
300 336 {
301 337 return KTAPI_Folder::_get_folder_by_name($this->ktapi, $foldername, $this->folderid);
302 338 }
... ... @@ -325,7 +361,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
325 361 * @param string $function
326 362 * @return KTAPI_Document
327 363 */
328   - function &_get_document_by_name($documentname, $function='getByNameAndFolder')
  364 + function _get_document_by_name($documentname, $function='getByNameAndFolder')
329 365 {
330 366 $documentname=trim($documentname);
331 367 if (empty($documentname))
... ... @@ -389,7 +425,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
389 425 * @param string $documentname
390 426 * @return KTAPI_Document
391 427 */
392   - function &get_document_by_name($documentname)
  428 + function get_document_by_name($documentname)
393 429 {
394 430 return $this->_get_document_by_name($documentname,'getByNameAndFolder');
395 431 }
... ... @@ -402,7 +438,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
402 438 * @param string $documentname
403 439 * @return KTAPI_Document
404 440 */
405   - function &get_document_by_filename($documentname)
  441 + function get_document_by_filename($documentname)
406 442 {
407 443 return $this->_get_document_by_name($documentname,'getByFilenameAndFolder');
408 444 }
... ... @@ -760,7 +796,6 @@ class KTAPI_Folder extends KTAPI_FolderItem
760 796 }
761 797 }
762 798  
763   -
764 799 return $contents;
765 800 }
766 801  
... ... @@ -773,7 +808,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
773 808 * @return KTAPI_Document
774 809 *
775 810 */
776   - function &add_document_shortcut($document_id){
  811 + function add_document_shortcut($document_id){
777 812 $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_WRITE);
778 813 if (PEAR::isError($user))
779 814 {
... ... @@ -806,7 +841,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
806 841 * @param int $folder_id The ID of the folder to create a shortcut to
807 842 * @return KTAPI_Folder
808 843 */
809   - function &add_folder_shortcut($folder_id){
  844 + function add_folder_shortcut($folder_id){
810 845 $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_WRITE);
811 846 if (PEAR::isError($user))
812 847 {
... ... @@ -849,7 +884,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
849 884 * @param string $tempfilename This is a reference to the file that is accessible locally on the file system.
850 885 * @return KTAPI_Document
851 886 */
852   - function &add_document($title, $filename, $documenttype, $tempfilename)
  887 + function add_document($title, $filename, $documenttype, $tempfilename)
853 888 {
854 889 if (!is_file($tempfilename))
855 890 {
... ... @@ -924,7 +959,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
924 959 * @param string $foldername
925 960 * @return KTAPI_Folder
926 961 */
927   - function &add_folder($foldername)
  962 + function add_folder($foldername)
928 963 {
929 964 $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_ADD_FOLDER);
930 965  
... ...
lib/foldermanagement/Folder.inc
... ... @@ -53,6 +53,12 @@ class Folder extends KTEntity {
53 53 var $iParentID;
54 54 /** primary key of user who created folder */
55 55 var $iCreatorID;
  56 + /** The creation time of the folder */
  57 + var $dCreated;
  58 + /** The user that last modified the folder */
  59 + var $iModifiedUserID;
  60 + /** The time of the last modification to the folder */
  61 + var $dModified;
56 62 /** public status of folder */
57 63 var $bIsPublic = false;
58 64 /** comma deliminated string of parent ids */
... ... @@ -65,6 +71,8 @@ class Folder extends KTEntity {
65 71 var $iPermissionLookupID;
66 72 /** whether to restrict to only certain document types */
67 73 var $bRestrictDocumentTypes = false;
  74 + /** ID of folder owner */
  75 + var $iOwnerID;
68 76 /** ID of the folder this is a shortcut to(if any) */
69 77 var $iLinkedFolderId;
70 78  
... ... @@ -75,6 +83,9 @@ class Folder extends KTEntity {
75 83 'sDescription' => 'description',
76 84 'iParentID' => 'parent_id',
77 85 'iCreatorID' => 'creator_id',
  86 + 'dCreated' => 'created',
  87 + 'iModifiedUserID' => 'modified_user_id',
  88 + 'dModified' => 'modified',
78 89 'bIsPublic' => 'is_public',
79 90 'sFullPath' => 'full_path',
80 91 'sParentFolderIDs' => 'parent_folder_ids',
... ... @@ -82,6 +93,7 @@ class Folder extends KTEntity {
82 93 'iPermissionLookupID' => 'permission_lookup_id',
83 94 'bRestrictDocumentTypes' => 'restrict_document_types',
84 95 'iLinkedFolderId' => 'linked_folder_id',
  96 + 'iOwnerID' => 'owner_id',
85 97 );
86 98 // }}}
87 99  
... ... @@ -94,6 +106,15 @@ class Folder extends KTEntity {
94 106 function setParentID($iNewValue) { $this->iParentID = $iNewValue; }
95 107 function getCreatorID() { return $this->iCreatorID; }
96 108 function setCreatorID($iNewValue) { $this->iCreatorID = $iNewValue; }
  109 +
  110 + function getCreatedDateTime() { return $this->dCreated; }
  111 + function getModifiedUserID() { return $this->iModifiedUserID; }
  112 + function setModifiedUserID($iNewValue) { $this->iModifiedUserID = $iNewValue; }
  113 + function getLastModifiedDate() { return $this->dModified; }
  114 + function setLastModifiedDate($dNewValue) { $this->dModified = $dNewValue; }
  115 + function getOwnerID() { return $this->iOwnerID; }
  116 + function setOwnerID($iNewValue) { $this->iOwnerID = $iNewValue; }
  117 +
97 118 function getIsPublic() { return $this->bIsPublic; }
98 119 function setIsPublic($bNewValue) { $this->bIsPublic = $bNewValue; }
99 120 function getFullPath() { return $this->sFullPath; }
... ... @@ -110,12 +131,24 @@ class Folder extends KTEntity {
110 131 function getLinkedFolderId(){ return $this->iLinkedFolderId;}
111 132 function setLinkedFolderId($iNewValue){ $this->iLinkedFolderId = $iNewValue;}
112 133  
113   -
114   -
115 134 // {{{ create()
116 135 function create () {
117 136 $oParentFolder =& Folder::get($this->iParentID);
118 137 $this->iPermissionObjectID = $oParentFolder->getPermissionObjectID();
  138 +
  139 + if (empty($this->dCreated)) {
  140 + $this->dCreated = getCurrentDateTime();
  141 + }
  142 + if (empty($this->dModified)) {
  143 + $this->dModified = getCurrentDateTime();
  144 + }
  145 + if (empty($this->iModifiedUserID)) {
  146 + $this->iModifiedUserID = $this->iCreatorID;
  147 + }
  148 + if (empty($this->iOwnerID)) {
  149 + $this->iOwnerID = $this->iCreatorID;
  150 + }
  151 +
119 152 $res = parent::create();
120 153 if ($res === true) {
121 154 KTPermissionUtil::updatePermissionLookup(Folder::get($this->getId()));
... ... @@ -144,7 +177,7 @@ class Folder extends KTEntity {
144 177 /**
145 178 * Returns a comma delimited string containing the parent folder ids, strips leading /
146 179 *
147   - * @return String comma delimited string containing the parent folder ids
  180 + * @return String comma delimited string containing the parent folder ids
148 181 */
149 182 function generateFolderIDs($iFolderId) {
150 183 if (empty($iFolderId)) {
... ... @@ -225,11 +258,15 @@ class Folder extends KTEntity {
225 258 $this->sParentFolderIDs .= $this->iParentID;
226 259 }
227 260 return parent::_fieldValues();
  261 + // NOTE below no longer needed?
228 262 return array(
229 263 'name' => $this->sName,
230 264 'description' => $this->sDescription,
231 265 'parent_id' => $this->iParentID,
232 266 'creator_id' => $this->iCreatorID,
  267 + 'created' => $this->dCreated,
  268 + 'iModifiedUserId' => $this->iModifiedUserId,
  269 + 'modified' => $this->dModified,
233 270 'is_public' => KTUtil::anyToBool($this->bIsPublic),
234 271 'full_path' => $this->sFullPath,
235 272 'parent_folder_ids' => $this->sParentFolderIDs,
... ... @@ -428,7 +465,6 @@ class Folder extends KTEntity {
428 465 function getFolderPathNamesAsArray($iFolderID) {
429 466 global $default;
430 467  
431   -
432 468 $oFolder = Folder::get($iFolderID);
433 469 $aPathArray = array();
434 470 if ($oFolder) {
... ... @@ -534,7 +570,6 @@ class Folder extends KTEntity {
534 570 $sQuery = "SELECT id, name FROM " . KTUtil::getTableName('folders') . " WHERE name = ? AND parent_id = ?";/*ok*/
535 571 $aParams = array($sName, $iParentID);
536 572 $res = DBUtil::getResultArray(array($sQuery, $aParams));
537   - //var_dump($res);
538 573 if (count($res) != 0) {
539 574  
540 575 // mysql is case-insensitive - check using php
... ... @@ -581,7 +616,6 @@ class Folder extends KTEntity {
581 616 }
582 617 }
583 618  
584   -
585 619 function getByParentIDAndLookupID($iParentID, $iLookupID) {
586 620 return KTEntityUtil::getByDict('Folder', array(
587 621 'parent_id' => $iParentID,
... ...
lib/foldermanagement/folderutil.inc.php
... ... @@ -58,12 +58,14 @@ class KTFolderUtil {
58 58 return $oUser;
59 59 }
60 60 $oStorage =& KTStorageManagerUtil::getSingleton();
  61 +
61 62 $oFolder =& Folder::createFromArray(array(
62 63 'name' => ($sFolderName),
63 64 'description' => ($sFolderName),
64 65 'parentid' => $oParentFolder->getID(),
65 66 'creatorid' => $oUser->getID(),
66 67 ));
  68 +
67 69 if (PEAR::isError($oFolder)) {
68 70 return $oFolder;
69 71 }
... ... @@ -87,7 +89,6 @@ class KTFolderUtil {
87 89 */
88 90 function add($oParentFolder, $sFolderName, $oUser, $bulk_action = false) {
89 91  
90   -
91 92 $folderid=$oParentFolder->getId();
92 93 // check for conflicts first
93 94 if (Folder::folderExistsName($sFolderName,$folderid)) {
... ... @@ -99,7 +100,6 @@ class KTFolderUtil {
99 100 return $oFolder;
100 101 }
101 102  
102   -
103 103 $oTransaction = KTFolderTransaction::createFromArray(array(
104 104 'folderid' => $oFolder->getId(),
105 105 'comment' => _kt('Folder created'),
... ... @@ -289,6 +289,9 @@ class KTFolderUtil {
289 289  
290 290 $oFolder->setName($sNewName);
291 291 $oFolder->setDescription($sNewName);
  292 + $oFolder->setLastModifiedDate(getCurrentDateTime());
  293 + $oFolder->setModifiedUserId($oUser->getId());
  294 +
292 295 $res = $oFolder->update();
293 296  
294 297 $oTransaction = KTFolderTransaction::createFromArray(array(
... ... @@ -614,7 +617,6 @@ class KTFolderUtil {
614 617 // now we can go ahead.
615 618 foreach ($aDocuments as $oDocument) {
616 619 $oChildDestinationFolder = Folder::get($aFolderMap[$oDocument->getFolderID()]['parent_id']);
617   - // var_dump($oDocument->getFolderID());
618 620 $res = KTDocumentUtil::copy($oDocument, $oChildDestinationFolder);
619 621 if (PEAR::isError($res) || ($res === false)) {
620 622 $oStorage->removeFolder($oNewBaseFolder);
... ...
sql/mysql/install/structure.sql
... ... @@ -842,6 +842,9 @@ CREATE TABLE `folders` (
842 842 `description` varchar(255) default NULL,
843 843 `parent_id` int(11) default NULL,
844 844 `creator_id` int(11) default NULL,
  845 + `created` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' ,
  846 + `modified_user_id` INT( 11 ) NULL DEFAULT NULL ,
  847 + `modified` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' ,
845 848 `is_public` tinyint(1) NOT NULL default '0',
846 849 `parent_folder_ids` mediumtext,
847 850 `full_path` mediumtext,
... ...
sql/mysql/upgrade/3.6.2/folders.sql 0 → 100644
  1 +ALTER TABLE `folders` ADD `created` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `creator_id` ,
  2 +ADD `modified_user_id` INT( 11 ) NULL DEFAULT NULL AFTER `created` ,
  3 +ADD `modified` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `modified_user_id` ;
  4 +
  5 +#The following lines are for inserting the data which should have been there if this table had always stored this data
  6 +
  7 +UPDATE folders f set f.created =
  8 + (SELECT datetime FROM folder_transactions ft
  9 + WHERE ft.transaction_namespace = 'ktcore.transactions.create' AND ft.folder_id = f.id AND ft.user_id = f.creator_id
  10 + ORDER BY datetime DESC LIMIT 1) ;
  11 +
  12 +#TODO check whether there are additional actions which should trigger a modified date/user change
  13 +
  14 +UPDATE folders f set f.modified =
  15 + (SELECT datetime FROM folder_transactions ft
  16 + WHERE (ft.transaction_namespace = 'ktcore.transactions.move' OR ft.transaction_namespace = 'ktcore.transactions.rename')
  17 + AND ft.folder_id = f.id
  18 + AND ft.datetime > f.created
  19 + ORDER BY datetime DESC LIMIT 1) ;
  20 +
  21 +UPDATE folders f set f.modified_user_id =
  22 + (SELECT user_id FROM folder_transactions ft
  23 + WHERE (ft.transaction_namespace = 'ktcore.transactions.move' OR ft.transaction_namespace = 'ktcore.transactions.rename')
  24 + AND ft.folder_id = f.id
  25 + AND ft.datetime > f.created
  26 + ORDER BY datetime DESC LIMIT 1) ;
  27 +
  28 +UPDATE folders f set f.modified = f.created WHERE f.modified IS NULL OR f.modified = '0000-00-00 00:00:00' ;
  29 +
  30 +UPDATE folders f set f.modified_user_id = f.creator_id WHERE f.modified_user_id IS NULL ;
0 31 \ No newline at end of file
... ...