3 # FILE: ResourceFactory.php
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2011-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
15 # ---- PUBLIC INTERFACE --------------------------------------------------
25 $this->SchemaId = $SchemaId;
27 # set up item factory base class
28 $this->
ItemFactory(
"Resource",
"Resources",
"ResourceId", NULL, FALSE,
29 "SchemaId = ".intval($this->SchemaId));
39 # create new target resource
42 # load up resource to duplicate
43 $SrcResource =
new Resource($ResourceId);
45 # if resource to duplicate was found
46 if ($SrcResource->Status() > 0)
48 # for each metadata field
50 $Fields = $Schema->GetFields();
51 foreach ($Fields as $Field)
53 # skip the cumulative rating field
54 if ($Field->Name() !=
"Cumulative Rating")
56 $NewValue = $SrcResource->GetByField($Field, TRUE);
58 # clear default value from destination resource that is
59 # set when creating a new resource
60 $DstResource->ClearByField($Field);
62 # copy value from source resource to destination resource
63 $DstResource->SetByField($Field, $NewValue);
68 # return new resource to caller
80 # sanitize qualifier ID or retrieve from object
81 $QualifierId = is_object($ObjectOrId)
82 ? $ObjectOrId->Id() : intval($ObjectOrId);
84 # if new qualifier passed in
85 if ($NewObjectOrId !== NULL)
87 # sanitize qualifier ID to change to or retrieve it from object
88 $NewQualifierIdVal = is_object($NewObjectOrId)
89 ? $NewObjectOrId->Id() : intval($NewObjectOrId);
93 # qualifier should be cleared
94 $NewQualifierIdVal =
"NULL";
97 # for each metadata field
99 $Fields = $Schema->GetFields();
100 foreach ($Fields as $Field)
102 # if field uses qualifiers and uses item-level qualifiers
103 $QualColName = $Field->DBFieldName().
"Qualifier";
104 if ($Field->UsesQualifiers()
105 && $Field->HasItemLevelQualifiers()
106 && $this->DB->FieldExists(
"Resources", $QualColName))
108 # set all occurrences to new qualifier value
109 $this->DB->Query(
"UPDATE Resources"
110 .
" SET ".$QualColName.
" = ".$NewQualifierIdVal.
""
111 .
" WHERE ".$QualColName.
" = '".$QualifierId.
"'"
112 .
" AND SchemaId = ".intval($this->SchemaId));
116 # clear or change qualifier association with controlled names
117 # (NOTE: this should probably be done in a controlled name factory object)
118 $this->DB->Query(
"UPDATE ControlledNames"
119 .
" SET QualifierId = ".$NewQualifierIdVal
120 .
" WHERE QualifierId = '".$QualifierId.
"'");
122 # clear or change qualifier association with classifications
123 # (NOTE: this should probably be done in a classification factory object)
124 $this->DB->Query(
"UPDATE Classifications"
125 .
" SET QualifierId = ".$NewQualifierIdVal
126 .
" WHERE QualifierId = '".$QualifierId.
"'");
135 return $this->DB->Query(
136 "SELECT COUNT(DISTINCT ResourceId) AS ResourceCount"
137 .
" FROM ResourceRatings",
147 return $this->DB->Query(
148 "SELECT COUNT(DISTINCT UserId) AS UserCount"
149 .
" FROM ResourceRatings",
164 # assume that no resources will be found
165 $Resources = array();
167 # calculate cutoff date for resources
168 $CutoffDate = date(
"Y-m-d H:i:s", strtotime($MaxDaysToGoBack.
" days ago"));
170 # query for resource IDs
171 $this->DB->Query(
"SELECT ResourceId FROM Resources WHERE"
172 .
" DateOfRecordRelease > '".$CutoffDate.
"'"
173 .
" AND ReleaseFlag = 1"
174 .
" AND ResourceId >= 0"
175 .
" AND SchemaId = ".intval($this->SchemaId)
176 .
" ORDER BY DateOfRecordRelease DESC, DateOfRecordCreation DESC"
177 .
" LIMIT ".intval($Offset).
", ".intval($Count));
178 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
180 # for each resource ID found
181 foreach ($ResourceIds as $ResourceId)
183 # load resource and add to list of found resources
184 $Resources[$ResourceId] =
new Resource($ResourceId);
187 # return found resources to caller
201 # assume no resources will be found
202 $ResourceIds = array();
206 $Field = $Schema->GetFieldByName($FieldName);
211 # construct query based on field type
212 switch ($Field->Type())
217 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
218 .
" FROM Resources WHERE "
219 .$Field->DBFieldName().
" IS NOT NULL"
220 .
" AND LENGTH(LTRIM(RTRIM(".$Field->DBFieldName().
"))) > 0"
221 .
" AND SchemaId = ".intval($this->SchemaId),
225 $Query =
"SELECT ResourceId FROM Resources"
226 .
" WHERE SchemaId = ".intval($this->SchemaId)
227 .
" ORDER BY ".$Field->DBFieldName()
228 .($Ascending ?
" ASC" :
" DESC");
234 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
235 .
" FROM Resources WHERE "
236 .$Field->DBFieldName().
" IS NOT NULL"
237 .
" AND SchemaId = ".intval($this->SchemaId),
241 $Query =
"SELECT ResourceId FROM Resources"
242 .
" WHERE SchemaId = ".intval($this->SchemaId)
243 .
" ORDER BY ".$Field->DBFieldName()
244 .($Ascending ?
" ASC" :
" DESC");
249 $Count = $this->DB->Query(
"SELECT COUNT(*) AS ResourceCount"
250 .
" FROM Resources WHERE "
251 .$Field->DBFieldName().
"Begin IS NOT NULL"
252 .
" AND SchemaId = ".intval($this->SchemaId),
256 $Query =
"SELECT ResourceId FROM Resources"
257 .
" WHERE SchemaId = ".intval($this->SchemaId)
258 .
" ORDER BY ".$Field->DBFieldName().
"Begin"
259 .($Ascending ?
" ASC" :
" DESC");
264 # if appropriate query was found
267 # if limited number of results were requested
271 $Query .=
" LIMIT ".intval($Limit);
274 # perform query and retrieve resource IDs
275 $this->DB->Query($Query);
276 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
280 # return resource IDs to caller
292 $LastChangeDate = $this->DB->Query(
293 "SELECT MAX(DateLastModified) AS LastChangeDate"
295 .
" WHERE SchemaId = ".intval($this->SchemaId)
296 .($OnlyReleasedResources ?
" AND ReleaseFlag = 1" :
""),
298 return ($LastChangeDate ? strtotime($LastChangeDate) : NULL);
308 # retrieve field names from schema
309 $FieldNames = array();
311 $Fields = $Schema->GetFields();
312 foreach ($Fields as $Field)
314 $FieldNames[$Field->Id()] = $Field->Name();
317 # return field names to caller
330 # start out assuming we won't find any resources
331 $Resources = array();
335 $Fields = $Schema->GetFields(
345 foreach ($ValuesToMatch as $FieldId => $Value)
347 # if field can be used for comparison
348 if (isset($Fields[$FieldId]))
350 # add comparison to condition
351 $Condition .= $LinkingTerm.$Fields[$FieldId]->DBFieldName()
352 .
" = '".addslashes($Value).
"'";
353 $LinkingTerm =
" AND ";
357 # if there were valid conditions
358 if (strlen($Condition))
360 # build query statment
361 $Query =
"SELECT ResourceId FROM Resources WHERE ".$Condition
362 .
" AND SchemaId = ".intval($this->SchemaId);
364 # execute query to retrieve matching resource IDs
365 $this->DB->Query($Query);
366 $ResourceIds = $this->DB->FetchColumn(
"ResourceId");
368 # retrieve resource objects
369 foreach ($ResourceIds as $Id)
371 $Resources[$Id] =
new Resource($Id);
375 # return any resources found to caller
379 # Functions for keeping per-field resource counts updated:
397 $Field = $Schema->GetField($FieldId);
401 if ($this->ResourceCount === NULL)
404 "SELECT FieldId, ClassName, CountType, Count FROM ResourceCounts");
406 while ($Row = $this->DB->FetchRow())
408 $R_FieldId = $Row[
"FieldId"];
409 $R_ClassName = $Row[
"ClassName"];
410 $R_CountType = $Row[
"CountType"];
411 $R_Count = $Row[
"Count"];
413 $this->ResourceCount[$R_FieldId][$R_ClassName][$R_CountType] = $R_Count;
420 return isset($this->ResourceCount[$FieldId][$Value][$CountType]) ?
421 $this->ResourceCount[$FieldId][$Value][$CountType] :
436 return $this->DB->Query(
"
437 SELECT COUNT(*) AS ResourceTotal
441 AND SchemaId = ".intval($this->SchemaId),
452 return $this->DB->Query(
"
453 SELECT COUNT(*) AS ResourceTotal
456 AND SchemaId = ".intval($this->SchemaId),
471 # be sure that we're not a gigantic object when the task is queued
472 $TmpResourceCount = $this->ResourceCount;
473 $this->ResourceCount = NULL;
475 $AF->QueueUniqueTask(
476 array($this,
"UpdateResourceCountCallback"), array());
477 $this->ResourceCount = $TmpResourceCount;
491 "CREATE TABLE ResourceCountsNew (FieldId INT, ClassName TEXT, CountType TEXT, Count INT);");
493 $Start = microtime(TRUE);
495 foreach ($this->ResourceCountConditions as $CountType => $CountCondition)
498 "INSERT INTO ResourceCountsNew "
499 .
"SELECT FieldId, ControlledName AS ClassName,"
500 .
"'".$CountType.
"' AS CountType, Count(ResourceId) AS Count "
501 .
"FROM (SELECT * FROM ResourceNameInts WHERE ResourceId IN "
502 .
"(SELECT ResourceId FROM Resources "
503 .
" WHERE SchemaId = ".intval($this->SchemaId)
504 .(($CountCondition!==NULL)
505 ?
" AND ".$CountCondition:
"").
")) AS T0 "
506 .
"JOIN ControlledNames USING(ControlledNameId) GROUP BY ControlledNameId;" );
509 $Stop = microtime(TRUE);
512 "INSERT INTO ResourceCountsNew VALUES (-1, '__LAST_UPDATED__', '', UNIX_TIMESTAMP()); ");
514 "INSERT INTO ResourceCountsNew VALUES (-2, '__UPDATE_RUNTIME__','',".($Stop-$Start).
");");
516 "RENAME TABLE ResourceCounts TO ResourceCountsOld, ResourceCountsNew TO ResourceCounts; ");
518 "DROP TABLE ResourceCountsOld; ");
521 # ---- PRIVATE INTERFACE -------------------------------------------------
523 private $ResourceCount = NULL;
524 private $ResourceCountConditions = array(
"All" => NULL,
"Released" =>
"ReleaseFlag=1");
GetRatedResourceUserCount()
Return number of users who have rated resources.
SQL database abstraction object with smart query caching.
GetTimestampOfLastResourceModification($OnlyReleasedResources=TRUE)
Get date/time of when last a resource was modified.
UpdateResourceCountCallback()
Update the stored counts of resources per controlled name, looking at the private var $ResourceCountC...
GetResourceCount($FieldId, $Value, $CountType="All")
Return the number of resources having a given value set for a specified ControlledName field...
GetResourceIdsSortedBy($FieldName, $Ascending=TRUE, $Limit=NULL)
Get resource IDs sorted by specified field.
ResourceFactory($SchemaId=MetadataSchema::SCHEMAID_DEFAULT)
Class constructor.
GetRecentlyReleasedResources($Count=10, $Offset=0, $MaxDaysToGoBack=90)
Get resources sorted by descending Date of Record Release, with Date of Record Creation as the second...
QueueResourceCountUpdate()
Add a task to the queue which will update the resource counts for ControlledNames.
GetMatchingResources($ValuesToMatch)
Find resources with values that match those specified.
ClearQualifier($ObjectOrId, $NewObjectOrId=NULL)
Clear or change specific qualifier for all resources.
Represents a "resource" in CWIS.
GetPossibleFieldNames()
Get possible field names for resources.
GetReleasedResourceTotal()
Get the total number of released resources in the collection.
static Create($SchemaId)
Create a new resource.
Common factory class for item manipulation.
GetRatedResourceCount()
Return number of resources that have ratings.
Factory for Resource objects.
ItemFactory($ItemClassName, $ItemTableName, $ItemIdFieldName, $ItemNameFieldName=NULL, $OrderOpsAllowed=FALSE, $SqlCondition=NULL)
Class constructor.
GetResourceTotal()
Get the total number of resources in the collection, even if they are not released.
DuplicateResource($ResourceId)
Duplicate the specified resource and return to caller.