Source for file HarmoniSchedulingManager.class.php

Documentation is available at HarmoniSchedulingManager.class.php

  1. <?php
  2. require_once(OKI2."/osid/scheduling/SchedulingManager.php");
  3.  
  4. require_once(HARMONI."oki2/scheduling/HarmoniTimespan.class.php");
  5. require_once(HARMONI."oki2/scheduling/HarmoniTimespanIterator.class.php");
  6. require_once(HARMONI."oki2/scheduling/HarmoniAgentCommitment.class.php");
  7. require_once(HARMONI."oki2/scheduling/HarmoniAgentCommitmentIterator.class.php");
  8. require_once(HARMONI."oki2/scheduling/HarmoniScheduleItem.class.php");
  9. require_once(HARMONI."oki2/scheduling/HarmoniScheduleItemIterator.class.php");
  10.  
  11. /**
  12. * <p>
  13. * SchedulingManager creates, deletes, and gets ScheduleItems. Items include
  14. * Agent Commitments (e.g. Calendar events). The Manager also enumerates the
  15. * commitment Status Types supported by the implementation.
  16. * </p>
  17. *
  18. * <p>
  19. * All implementations of OsidManager (manager) provide methods for accessing
  20. * and manipulating the various objects defined in the OSID package. A manager
  21. * defines an implementation of an OSID. All other OSID objects come either
  22. * directly or indirectly from the manager. New instances of the OSID objects
  23. * are created either directly or indirectly by the manager. Because the OSID
  24. * objects are defined using interfaces, create methods must be used instead
  25. * of the new operator to create instances of the OSID objects. Create methods
  26. * are used both to instantiate and persist OSID objects. Using the
  27. * OsidManager class to define an OSID's implementation allows the application
  28. * to change OSID implementations by changing the OsidManager package name
  29. * used to load an implementation. Applications developed using managers
  30. * permit OSID implementation substitution without changing the application
  31. * source code. As with all managers, use the OsidLoader to load an
  32. * implementation of this interface.
  33. * </p>
  34. *
  35. * <p></p>
  36. *
  37. * <p>
  38. * OSID Version: 2.0
  39. * </p>
  40. *
  41. * <p>
  42. * Licensed under the {@link org.osid.SidImplementationLicenseMIT MIT}
  43. * O.K.I&#46; OSID Definition License}.
  44. * </p>
  45. *
  46. * @package org.osid.scheduling
  47. */
  48. class HarmoniSchedulingManager
  49. extends SchedulingManager
  50. {
  51.  
  52.  
  53.  
  54.  
  55.  
  56. /**
  57. * @variable object OsidContext $_osidContext the OSID context.
  58. * @access private
  59. * @variable string $_defaultAuthority the default authority for default types.
  60. * @access private
  61. ***/
  62. var $_osidContext;
  63. var $_defaultAuthority;
  64.  
  65.  
  66.  
  67.  
  68. function HarmoniSchedulingManager () {
  69.  
  70.  
  71. }
  72.  
  73.  
  74.  
  75. /**
  76. * Assign the configuration of this Manager. There are no valid configuration options for
  77. * this manager.
  78. *
  79. * @param object Properties $configuration (original type: java.util.Properties)
  80. *
  81. * @throws object OsidException An exception with one of the following
  82. * messages defined in org.osid.OsidException: {@link }
  83. * org.osid.OsidException#OPERATION_FAILED OPERATION_FAILED},
  84. * {@link org.osid.OsidException#PERMISSION_DENIED}
  85. * PERMISSION_DENIED}, {@link }
  86. * org.osid.OsidException#CONFIGURATION_ERROR
  87. * CONFIGURATION_ERROR}, {@link }
  88. * org.osid.OsidException#UNIMPLEMENTED UNIMPLEMENTED}, {@link }
  89. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  90. *
  91. * @access public
  92. */
  93. function assignConfiguration ( $configuration ) {
  94. $def = $configuration->getProperty('default_authority');
  95.  
  96.  
  97. // ** parameter validation
  98. ArgumentValidator::validate($def, StringValidatorRule::getRule(), true);
  99. // ** end of parameter validation
  100.  
  101. $this->_defaultAuthority =$def;
  102. }
  103.  
  104. /**
  105. * Return context of this OsidManager.
  106. *
  107. * @return object OsidContext
  108. *
  109. * @throws object OsidException
  110. *
  111. * @access public
  112. */
  113. function getOsidContext () {
  114. return $this->_osidContext;
  115. }
  116.  
  117. /**
  118. * Assign the context of this OsidManager.
  119. *
  120. * @param object OsidContext $context
  121. *
  122. * @throws object OsidException An exception with one of the following
  123. * messages defined in org.osid.OsidException: {@link }
  124. * org.osid.OsidException#NULL_ARGUMENT NULL_ARGUMENT}
  125. *
  126. * @access public
  127. */
  128. function assignOsidContext ( $context ) {
  129. $this->_osidContext =$context;
  130. }
  131.  
  132.  
  133.  
  134. /**
  135. * WARNING: NOT IN OSID -- This method is designed to comply with V3 of the OSIDs, at least
  136. * How Tom suggested they were headed
  137. * <a href="http://okicommunity.mit.edu/forum/viewtopic.php?forum=1&showtopic=67&show=10&page=2">
  138. * here </a>. Leave out the agent parameter, and add a statusType instead.
  139. *
  140. * Create a ScheduleItem. The masterIdentifier argument is optional--it
  141. * can be set to null. If master identifier is passed in as null, then the
  142. * id string will be used. Master Identifier is a key, rule, or function that can be used to
  143. * associated more than one ScheduleItem together. An example can be
  144. * recurring items where each recurring item has the same Master
  145. * Identifier. An unique Id is generated for this ScheduleItem by the
  146. * implementation.
  147. *
  148. *
  149. * @param string $displayName
  150. * @param string $description
  151. * @param object Id[] $agents
  152. * @param int $start
  153. * @param int $end
  154. * @param string $masterIdentifier
  155. *
  156. * @return object ScheduleItem
  157. *
  158. * @throws object SchedulingException An exception with one of
  159. * the following messages defined in
  160. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  161. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  162. * OPERATION_FAILED}, {@link }
  163. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  164. * PERMISSION_DENIED}, {@link }
  165. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  166. * CONFIGURATION_ERROR}, {@link }
  167. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  168. * UNIMPLEMENTED}, {@link }
  169. * org.osid.scheduling.SchedulingException#NULL_ARGUMENT
  170. * NULL_ARGUMENT}, {@link }
  171. * org.osid.scheduling.SchedulingException#UNKNOWN_ID UNKNOWN_ID},
  172. * {@link org.osid.scheduling.SchedulingException#END_BEFORE_START}
  173. * END_BEFORE_START}
  174. *
  175. * @access public
  176. */
  177. function createScheduleItem ( $displayName, $description, $scheduleItemStatusType, $start, $end, $masterIdentifier = null ) {
  178.  
  179. if($start>$end){
  180. throwError(new Error("The end of a ScheduleItem cannot come before the end", "HarmoniSchedulingManager", true));
  181. }
  182.  
  183. $idManager = Services::getService("IdManager");
  184. $id=$idManager->createId();
  185.  
  186.  
  187.  
  188. //try to get the creator of this ScheduleItem
  189. $authN = Services::getService("AuthN");
  190. $authNTypesIterator =$authN->getAuthenticationTypes();
  191. if($authNTypesIterator->hasNext()){
  192. $authNType1 =$authNTypesIterator->next();
  193. //hopefully the first one is the right one to choose.
  194. $creatorId =$authN->getUserId($authNType1);
  195. $creatorIdString = $creatorId->getIdString();
  196. }else{
  197. $creatorIdString = "";
  198. }
  199.  
  200.  
  201. if(func_num_args()<6 || is_null($masterIdentifier)){
  202. $masterIdentifier = $id->getIdString();
  203. }
  204.  
  205. //set up a default item status type
  206. $defType = new Type("ScheduleItemStatusType",$this->_defaultAuthority,"default");
  207. $defIndex = $this->_typeToIndex('item_stat',$defType);
  208.  
  209. //set up the query
  210. $dbManager= Services::getService("DBHandler");
  211. $query= new InsertQuery;
  212. $query->setTable('sc_item');
  213. $query->setColumns(array('id','name','description','start_date','end_date','fk_sc_item_stat_type','master_id','fk_creator_id'));
  214. $values[]="'".addslashes($id->getIdString())."'";
  215. $values[]="'".addslashes($displayName)."'";
  216. $values[]="'".addslashes($description)."'";
  217. $values[]="'".addslashes(intval($start))."'";
  218. $values[]="'".addslashes(intval($end))."'";
  219. $values[]="'".$this->_typeToIndex('item_stat',$scheduleItemStatusType)."'";
  220. $values[]="'".addslashes($masterIdentifier)."'";
  221. $values[]="'".addslashes($creatorIdString)."'";
  222. $query->addRowOfValues($values);
  223.  
  224. $dbManager->query($query);
  225.  
  226. $ret = new HarmoniScheduleItem($id);
  227.  
  228.  
  229. return $ret;
  230. }
  231.  
  232. /**
  233. * Delete a ScheduleItem by unique Id.
  234. *
  235. * @param object Id $scheduleItemId
  236. *
  237. * @throws object SchedulingException An exception with one of
  238. * the following messages defined in
  239. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  240. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  241. * OPERATION_FAILED}, {@link }
  242. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  243. * PERMISSION_DENIED}, {@link }
  244. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  245. * CONFIGURATION_ERROR}, {@link }
  246. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  247. * UNIMPLEMENTED}, {@link }
  248. * org.osid.scheduling.SchedulingException#NULL_ARGUMENT
  249. * NULL_ARGUMENT}, {@link }
  250. * org.osid.scheduling.SchedulingException#UNKNOWN_ID UNKNOWN_ID}
  251. *
  252. * @access public
  253. */
  254. function deleteScheduleItem ( $scheduleItemId ) {
  255. $dbHandler = Services::getService("DBHandler");
  256. $query= new DeleteQuery;
  257. $query->setTable('sc_item');
  258. $query->addWhere("id=".addslashes($scheduleItemId->getIdString()));
  259. $dbHandler->query($query);
  260. $query2= new DeleteQuery;
  261. $query2->setTable('sc_commit');
  262. $query2->addWhere("fk_sc_item=".addslashes($scheduleItemId->getIdString()));
  263. $dbHandler->query($query);
  264. }
  265.  
  266. /**
  267. * Get the Timespans during which all Agents are uncommitted.
  268. * The time complexity may not be great on this one.
  269. *
  270. * @param object Id[] $agents
  271. * @param int $start
  272. * @param int $end
  273. *
  274. * @return object TimespanIterator
  275. *
  276. * @throws object SchedulingException An exception with one of
  277. * the following messages defined in
  278. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  279. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  280. * OPERATION_FAILED}, {@link }
  281. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  282. * PERMISSION_DENIED}, {@link }
  283. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  284. * CONFIGURATION_ERROR}, {@link }
  285. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  286. * UNIMPLEMENTED}, {@link }
  287. * org.osid.scheduling.SchedulingException#NULL_ARGUMENT
  288. * NULL_ARGUMENT}, {@link }
  289. * org.osid.scheduling.SchedulingException#UNKNOWN_ID UNKNOWN_ID},
  290. * {@link org.osid.scheduling.SchedulingException#END_BEFORE_START}
  291. * END_BEFORE_START}
  292. *
  293. * @access public
  294. */
  295. function getAvailableTimes ( $agents, $start, $end ) {
  296. if(count($agents)==0){
  297. $array[] =&new HarmoniTimespan($start,$end);
  298. $ret = new HarmoniTimespanIterator($array);
  299. return $ret;
  300. }
  301. //get all schedule item rows with the appropriate time and agents
  302. $dbHandler = Services::getService("DBHandler");
  303. $query= new SelectQuery;
  304. $query->addTable('sc_item');
  305. $query->addTable('sc_commit', INNER_JOIN, "sc_item.id=sc_commit.fk_sc_item");
  306. $query->addColumn('sc_item.id');
  307.  
  308. $where = "(end_date >= '".addslashes($start)."'";
  309. $where .= " OR start_date <= '".addslashes($end)."') AND (";
  310. $firstElement =true;
  311. foreach($agents as $agentId){
  312. if(!$firstElement){
  313. $where .= " OR ";
  314. }else{
  315. $firstElement=false;
  316. }
  317.  
  318.  
  319. $where .= ("sc_commit.fk_agent_id='".addslashes($agentId->getIdString())."'");
  320. }
  321.  
  322. $where .= ")";
  323. $query->addOrderBy('sc_item.id');
  324. $query->addWhere($where);
  325. $res=$dbHandler->query($query);
  326.  
  327. //find times not conflicted by these items
  328. //yes I know $mister11 is a terible name for the variable. Deal with it :-P.
  329. //We'll
  330. $availableTimes = array();
  331. $availableTimes[$start] = new HarmoniTimeSpan($start,$end);
  332.  
  333.  
  334.  
  335. //print_r($thearray);
  336.  
  337. $idManager = Services::getService("IdManager");
  338.  
  339. $lastId = "";
  340. while($res->hasMoreRows()){
  341. $row = $res->getCurrentRow();
  342. $res->advanceRow();
  343. $idString = $row['id'];
  344. if($lastId!=$idString){
  345. $id =$idManager->getId($idString);
  346. $item =$this->getScheduleItem($id);
  347.  
  348. $availableTimes = $this->_restrict($item, $availableTimes);
  349. $lastId=$idString;
  350. }
  351. }
  352. //print_r($thearray);
  353. //ksort($thearray);
  354.  
  355. $ret = new HarmoniTimespanIterator($availableTimes);
  356. return $ret;
  357. }
  358.  
  359. /**
  360. * This helper function takes a ScheduleItem and
  361. * and array of timespans and returns the array of
  362. *timespans that donot conflict with the event, but
  363. * which contain as much of the original time as possible.
  364. **/
  365.  
  366. function _restrict($scheduleItem, $mister11){
  367. //print "in";
  368. $start = $scheduleItem->getStart();
  369. $end = $scheduleItem->getEnd();
  370.  
  371. $arrayOfTimeSpans2 =array();
  372.  
  373. foreach(array_keys($mister11) as $key){
  374. $timespan =$mister11[$key];
  375. $start2 = $timespan->getStart();
  376. $end2 = $timespan->getEnd();
  377. if($start>=$end2 || $end<=$start2){
  378. $arrayOfTimeSpans2[$key]=$timespan;
  379. continue;//do nothing. the time intervals do not overlap.
  380. //this actually should not happen if called from getAvailableTimes()
  381. }
  382. if ($end2 <= $end){
  383. if ($start2 >= $start){//Item fully overlaps item
  384. //do nothing
  385. }else{
  386. $arrayOfTimeSpans2[$end] = new HarmoniTimespan($start2,$start);
  387. }
  388. }else{
  389. if ($start2 >= $start){
  390. $arrayOfTimeSpans2[$start2] = new HarmoniTimespan($end,$end2);
  391. }else{
  392. $arrayOfTimeSpans2[$start2] = new HarmoniTimespan($start2,$start);
  393. $arrayOfTimeSpans2[$end] = new HarmoniTimespan($end,$end2);
  394. }
  395.  
  396. }
  397.  
  398. }
  399. return $arrayOfTimeSpans2;
  400. }
  401.  
  402. /**
  403. * Get a ScheduleItem by unique Id.
  404. *
  405. * @param object Id $scheduleItemId
  406. *
  407. * @return object ScheduleItem
  408. *
  409. * @throws object SchedulingException An exception with one of
  410. * the following messages defined in
  411. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  412. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  413. * OPERATION_FAILED}, {@link }
  414. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  415. * PERMISSION_DENIED}, {@link }
  416. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  417. * CONFIGURATION_ERROR}, {@link }
  418. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  419. * UNIMPLEMENTED}, {@link }
  420. * org.osid.scheduling.SchedulingException#NULL_ARGUMENT
  421. * NULL_ARGUMENT}, {@link }
  422. * org.osid.scheduling.SchedulingException#UNKNOWN_ID UNKNOWN_ID}
  423. *
  424. * @access public
  425. */
  426. function getScheduleItem ( $scheduleItemId ) {
  427. $ret = new HarmoniScheduleItem($scheduleItemId);
  428. return $ret;
  429. }
  430.  
  431. /**
  432. * Get all the ScheduleItems for any Agent, with the specified Item Status
  433. * and that start or end between the start and end specified, inclusive.
  434. *
  435. * Null will select all types of ScheduleItems
  436. *
  437. * @param int $start
  438. * @param int $end
  439. * @param object Type $status
  440. *
  441. * @return object ScheduleItemIterator
  442. *
  443. * @throws object SchedulingException An exception with one of
  444. * the following messages defined in
  445. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  446. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  447. * OPERATION_FAILED}, {@link }
  448. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  449. * PERMISSION_DENIED}, {@link }
  450. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  451. * CONFIGURATION_ERROR}, {@link }
  452. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  453. * UNIMPLEMENTED}, {@link }
  454. * org.osid.scheduling.SchedulingException#NULL_ARGUMENT
  455. * NULL_ARGUMENT}, {@link }
  456. * org.osid.scheduling.SchedulingException#UNKNOWN_TYPE
  457. * UNKNOWN_TYPE}, {@link }
  458. * org.osid.scheduling.SchedulingException#END_BEFORE_START
  459. * END_BEFORE_START}
  460. *
  461. * @access public
  462. */
  463. function getScheduleItems ( $start, $end, $status ) {
  464.  
  465. //get all schedule item rows with the appropriate type
  466. $dbHandler = Services::getService("DBHandler");
  467. $query= new SelectQuery;
  468. $query->addTable('sc_item');
  469. $query->addColumn('id');
  470.  
  471.  
  472. $query->addWhere("end_date >= '".addslashes($start)."'");
  473. $query->addWhere("start_date <= '".addslashes($end)."'");
  474.  
  475. if(!is_null($status)){
  476. //get the index for the type
  477. $typeIndex = $this->_typeToIndex('item_stat',$status);
  478. $query->addWhere("fk_sc_item_stat_type='".addslashes($typeIndex)."'");
  479. }
  480. /*
  481. $where = "(end_date >= '".addslashes($start)."'";
  482. $where .= " OR start_date <= '".addslashes($end)."')";
  483. if(!is_null($status)){
  484. //get the index for the type
  485. $typeIndex = $this->_typeToIndex('item_stat',$status);
  486. $where .= " AND fk_sc_item_stat_type='".addslashes($typeIndex)."'";
  487. }
  488. $query->addWhere($where);*/
  489. //$query->addWhere("start_date <= ".addslashes($typeIndex)."'");
  490. //$query->addWhere("end_date >= ".addslashes($typeIndex)."'");
  491. $res=$dbHandler->query($query);
  492.  
  493. //convert results to array of ScheduleItems
  494. $array = array();
  495. $idManager = Services::getService("IdManager");
  496. while($res->hasMoreRows()){
  497. $row = $res->getCurrentRow();
  498. $res->advanceRow();
  499. $id =$idManager->getId($row['id']);
  500. $array[] =$this->getScheduleItem($id);
  501. }
  502.  
  503. //convert to an iterator
  504. $ret = new HarmoniScheduleItemIterator($array);
  505. return $ret;
  506. }
  507.  
  508. /**
  509. * Get all the ScheduleItems for the specified Agents, with the specified
  510. * Item Status and that start or end between the start and end specified,
  511. * inclusive.
  512. *
  513. * Null will select all types of ScheduleItems
  514. *
  515. * @param int $start
  516. * @param int $end
  517. * @param object Type $status
  518. * @param object Id[] $agents
  519. *
  520. * @return object ScheduleItemIterator
  521. *
  522. * @throws object SchedulingException An exception with one of
  523. * the following messages defined in
  524. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  525. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  526. * OPERATION_FAILED}, {@link }
  527. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  528. * PERMISSION_DENIED}, {@link }
  529. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  530. * CONFIGURATION_ERROR}, {@link }
  531. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  532. * UNIMPLEMENTED}, {@link }
  533. * org.osid.scheduling.SchedulingException#NULL_ARGUMENT
  534. * NULL_ARGUMENT}, {@link }
  535. * org.osid.scheduling.SchedulingException#UNKNOWN_TYPE
  536. * UNKNOWN_TYPE}, {@link }
  537. * org.osid.scheduling.SchedulingException#END_BEFORE_START
  538. * END_BEFORE_START}, {@link }
  539. * org.osid.scheduling.SchedulingException#UNKNOWN_ID UNKNOWN_ID}
  540. *
  541. * @access public
  542. */
  543. function getScheduleItemsForAgents ( $start, $end, $status, $agents ) {
  544.  
  545.  
  546. if(count($agents)==0){
  547. $array =array();
  548. $ret = new HarmoniScheduleItemIterator($array);
  549. return $ret;
  550. }
  551.  
  552. $scheduleItems =$this->getScheduleItems($start,$end,$status);
  553.  
  554.  
  555.  
  556. $array =array();
  557.  
  558. while($scheduleItems->hasNextScheduleItem()){
  559. $scheduleItem =$scheduleItems->nextScheduleItem();
  560.  
  561.  
  562. $dbHandler = Services::getService("DBHandler");
  563. $query= new SelectQuery;
  564. $query->addTable('sc_commit');
  565. $query->addColumn('id');//@TODO switch this to just a count
  566.  
  567.  
  568. $where = "fk_sc_item='".$scheduleItem->_id->getIdString()."' AND (";
  569. $firstElement =true;
  570.  
  571. foreach($agents as $agentId){
  572.  
  573.  
  574. if(!$firstElement){
  575. $where .= "OR ";
  576. }else{
  577. $firstElement=false;
  578. }
  579. $where .= "'".addslashes($agentId->getIdString())."'=fk_agent_id ";
  580. }
  581.  
  582. $where .= ")";
  583. $query->addWhere($where);
  584.  
  585. $res=$dbHandler->query($query);
  586.  
  587. if($res->getNumberOfRows()>0){
  588. $array[] = $scheduleItem;
  589. }
  590.  
  591. }
  592.  
  593.  
  594.  
  595. //convert to an iterator
  596. $ret = new HarmoniScheduleItemIterator($array);
  597. return $ret;
  598.  
  599. /*
  600. //get all schedule item rows with the appropriate type
  601. $dbHandler = Services::getService("DBHandler");
  602. $query= new SelectQuery;
  603. $query->addTable('sc_item');
  604. $query->addColumn('id');
  605.  
  606. $where = "(end_date >= '".addslashes($start)."'";
  607. $where .= " OR start_date <= '".addslashes($end)."')";
  608. if(!is_null($status)){
  609. //get the index for the type
  610. $typeIndex = $this->_typeToIndex('item_stat',$status);
  611. $where .= " AND fk_sc_item_stat_type = '".addslashes($typeIndex)."'";
  612. }
  613. $where .= " AND (";
  614. //$where = "fk_sc_item_stat_type='".addslashes($typeIndex)."'";
  615. //$where .= "AND (end_date >= ".addslashes($start)."'";
  616. //$where .= "OR start_date <= ".addslashes($end)."') AND (";
  617. $firstElement =true;
  618. foreach($agents as $agent){
  619. if(!$firstElement){
  620. $where .= "OR ";
  621. }else{
  622. $firstElement=false;
  623. }
  624. $id =$agent->getId();
  625. $where .= "'".addslashes($id->getIdString())."'=fk_agent_id ";
  626. }
  627.  
  628. $where .= ")";
  629. $query->addWhere($where);
  630. //$query->addWhere("start_date <= ".addslashes($typeIndex)."'");
  631. //$query->addWhere("end_date >= ".addslashes($typeIndex)."'");
  632. $res=$dbHandler->query($query);
  633.  
  634. //convert results to array of ScheduleItems
  635. $array = array();
  636. $idManager = Services::getService("IdManager");
  637. while($res->hasMoreRows()){
  638. $row = $res->getCurrentRow();
  639. $res->advanceRow();
  640. $id =$idManager->getId($row['id']);
  641. $array[] =$this->getScheduleItem($id);
  642. }
  643.  
  644. //convert to an iterator
  645. $ret = new HarmoniScheduleItemIterator($array);
  646. return $ret;
  647.  
  648. */
  649.  
  650. }
  651.  
  652. /**
  653. * Get all ScheduleItems with the specified master identifier reference. A
  654. * Master Identifier is a key, rule, or function that can be used to
  655. * associated more than one ScheduleItem together. An example can be
  656. * recurring items where each recurring item has the same Master
  657. * Identifier.
  658. *
  659. * @param string $masterIdentifier
  660. *
  661. * @return object ScheduleItemIterator
  662. *
  663. * @throws object SchedulingException An exception with one of
  664. * the following messages defined in
  665. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  666. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  667. * OPERATION_FAILED}, {@link }
  668. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  669. * PERMISSION_DENIED}, {@link }
  670. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  671. * CONFIGURATION_ERROR}, {@link }
  672. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  673. * UNIMPLEMENTED}, {@link }
  674. * org.osid.scheduling.SchedulingException#NULL_ARGUMENT
  675. * NULL_ARGUMENT}
  676. *
  677. * @access public
  678. */
  679. function getScheduleItemsByMasterId ( $masterIdentifier ) {
  680. ;
  681.  
  682. //get all schedule item rows with the appropriate type
  683. $dbHandler = Services::getService("DBHandler");
  684. $query= new SelectQuery;
  685. $query->addTable('sc_item');
  686. $query->addColumn('id');
  687. $query->addWhere("master_id='".addslashes($masterIdentifier)."'");
  688. $res=$dbHandler->query($query);
  689.  
  690. //convert results to array of ScheduleItems
  691. $array = array();
  692. $idManager = Services::getService("IdManager");
  693. while($res->hasMoreRows()){
  694. $row = $res->getCurrentRow();
  695. $res->advanceRow();
  696. $id =$idManager->getId($row['id']);
  697. $array[] =$this->getScheduleItem($id);
  698. }
  699.  
  700. //convert to an iterator
  701. $ret = new HarmoniScheduleItemIterator($array);
  702. return $ret;
  703. }
  704.  
  705. /**
  706. * Get the Status Types for ScheduleItem supported by the implementation.
  707. *
  708. * @return object TypeIterator
  709. *
  710. * @throws object SchedulingException An exception with one of
  711. * the following messages defined in
  712. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  713. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  714. * OPERATION_FAILED}, {@link }
  715. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  716. * PERMISSION_DENIED}, {@link }
  717. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  718. * CONFIGURATION_ERROR}, {@link }
  719. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  720. * UNIMPLEMENTED}
  721. *
  722. * @access public
  723. */
  724. function getItemStatusTypes () {
  725. return $this->_getTypes('item_stat');
  726. }
  727.  
  728. /**
  729. * Get the Status Types for Agents' Commitment supported by the
  730. * implementation.
  731. *
  732. * @return object TypeIterator
  733. *
  734. * @throws object SchedulingException An exception with one of
  735. * the following messages defined in
  736. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  737. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  738. * OPERATION_FAILED}, {@link }
  739. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  740. * PERMISSION_DENIED}, {@link }
  741. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  742. * CONFIGURATION_ERROR}, {@link }
  743. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  744. * UNIMPLEMENTED}
  745. *
  746. * @access public
  747. */
  748. function getItemCommitmentStatusTypes () {
  749. return $this->getCommitmentStatusTypes();
  750. }
  751.  
  752. /**
  753. * Get the Status Types for Agents' Commitment supported by the
  754. * implementation.
  755. *
  756. * Warning: not in OSID
  757. *
  758. * @return object TypeIterator
  759. *
  760. * @throws object SchedulingException An exception with one of
  761. * the following messages defined in
  762. * org.osid.scheduling.SchedulingException may be thrown: {@link }
  763. * org.osid.scheduling.SchedulingException#OPERATION_FAILED
  764. * OPERATION_FAILED}, {@link }
  765. * org.osid.scheduling.SchedulingException#PERMISSION_DENIED
  766. * PERMISSION_DENIED}, {@link }
  767. * org.osid.scheduling.SchedulingException#CONFIGURATION_ERROR
  768. * CONFIGURATION_ERROR}, {@link }
  769. * org.osid.scheduling.SchedulingException#UNIMPLEMENTED
  770. * UNIMPLEMENTED}
  771. *
  772. * @access public
  773. */
  774. function getCommitmentStatusTypes () {
  775. return $this->_getTypes('commit_stat');
  776. }
  777.  
  778.  
  779.  
  780.  
  781. /**
  782. * Get all the Types from the table specified
  783. *
  784. * @param string $typename the type of Types to get
  785. *
  786. * @return object HarmoniTypeIterator
  787. *
  788. * @access private
  789. */
  790. function _getTypes($typename){
  791.  
  792. //query
  793. $dbHandler = Services::getService("DBHandler");
  794. $query= new SelectQuery;
  795. $query->addTable('sc_'.$typename."_type");
  796. $query->addColumn('domain');
  797. $query->addColumn('authority');
  798. $query->addColumn('keyword');
  799. $query->addColumn('description');
  800. $res=$dbHandler->query($query);
  801.  
  802. //iterate through results and add to an array
  803. $array=array();
  804. while($res->hasMoreRows()){
  805. $row = $res->getCurrentRow();
  806. $res->advanceRow();
  807. if(is_null($row['description'])){
  808. $the_type = new Type($row['domain'],$row['authority'],$row['keyword']);
  809. }else{
  810. $the_type = new Type($row['domain'],$row['authority'],$row['keyword'],$row['description']);
  811. }
  812. $array[] = $the_type;
  813. }
  814.  
  815. //convert to an iterator
  816. $ret = new HarmoniTypeIterator($array);
  817. return $ret;
  818. }
  819.  
  820. /**
  821. * For object in table $table with id $id, get the Type with type $typename
  822. *
  823. * @param object Id $id the Id of the object in question
  824. * @param string $table the table our object resides in
  825. * @param string $typename the type of Type to get
  826. *
  827. * @return object Type
  828. *
  829. * @access private
  830. */
  831. function _getType($id, $table, $typename){
  832. //the appropriate table names and fields must be given names according to the pattern indicated below
  833.  
  834. //get the index for the type
  835. $index = $this->_getField($id,$table,"fk_sc_".$typename."_type");
  836.  
  837. return $this->_indexToType($index,$typename);
  838.  
  839. }
  840. /**
  841. * For get the Type with type $typename with id $index
  842. *
  843. * @param string $index the index of the type
  844. * @param string $typename the type of Type to get
  845. *
  846. * @return object Type
  847. *
  848. * @access private
  849. */
  850. function _indexToType($index, $typename){
  851. //the appropriate table names and fields must be given names according to the pattern indicated below
  852.  
  853. //query
  854. $dbHandler = Services::getService("DBHandler");
  855. $query= new SelectQuery;
  856. $query->addTable('sc_'.$typename."_type");
  857. $query->addWhere("id=".$index);
  858. $query->addColumn('domain');
  859. $query->addColumn('authority');
  860. $query->addColumn('keyword');
  861. $query->addColumn('description');
  862. $res=$dbHandler->query($query);
  863.  
  864.  
  865. if(!$res->hasMoreRows()){
  866. throwError(new Error("No Type has Id '".$index."' in table 'sc_".$typename."_type'","CourseManagement", true));
  867. }
  868.  
  869. //There should be exactly one result. Convert it to a type and return it
  870. //remember that the description is optional
  871. $row = $res->getCurrentRow();
  872. if(is_null($row['description'])){
  873. $the_type = new Type($row['domain'],$row['authority'],$row['keyword']);
  874. }else{
  875. $the_type = new Type($row['domain'],$row['authority'],$row['keyword'],$row['description']);
  876. }
  877. return $the_type;
  878.  
  879. }
  880.  
  881.  
  882.  
  883. /**
  884. * Find the index for our Type of type $type in its table. If it is not there,
  885. * put it into the table and return the index.
  886. *
  887. * @param string $typename the type of Type that is passed in.
  888. * @param object Type $type the Type itself
  889. *
  890. * @return object Type
  891. *
  892. * @access private
  893. */
  894. function _typeToIndex($typename, $type){
  895. //the appropriate table names and fields must be given names according to the pattern indicated below
  896.  
  897.  
  898. //validate the Type
  899. ArgumentValidator::validate($type, ExtendsValidatorRule::getRule("Type"), true);
  900.  
  901.  
  902.  
  903. //query to see if it exists
  904. $dbHandler = Services::getService("DBHandler");
  905. $query= new SelectQuery;
  906. $query->addTable('sc_'.$typename."_type");
  907. $query->addWhere("domain='".$type->getDomain()."'");
  908. $query->addWhere("authority='".$type->getAuthority()."'");
  909. $query->addWhere("keyword='".$type->getKeyword()."'");
  910. $query->addColumn('id');
  911. $res=$dbHandler->query($query);
  912.  
  913.  
  914.  
  915. if($res->getNumberOfRows()==0){
  916. //if not query to create it
  917. $query= new InsertQuery;
  918. $query->setTable('sc_'.$typename.'_type');
  919. $values[]="'".addslashes($type->getDomain())."'";
  920. $values[]="'".addslashes($type->getAuthority())."'";
  921. $values[]="'".addslashes($type->getKeyword())."'";
  922. if(is_null($type->getDescription())){
  923. $query->setColumns(array('domain','authority','keyword'));
  924. }else{
  925. $query->setColumns(array('domain','authority','keyword','description'));
  926. $values[]="'".addslashes($type->getDescription())."'";
  927. }
  928.  
  929. $query->addRowOfValues($values);
  930. $query->setAutoIncrementColumn('id','sc_'.$typename.'_type_id_seq');
  931.  
  932.  
  933. $result =$dbHandler->query($query);
  934.  
  935. return $result->getLastAutoIncrementValue();
  936. }elseif($res->getNumberOfRows()==1){
  937. //if it does exist, create it
  938. $row = $res->getCurrentRow();
  939. $the_index = $row['id'];
  940. return $the_index;
  941.  
  942. }else{
  943. //print a warning if there is more than one such type. Should never happen.
  944. print "\n<b>Warning!<\b> The Type with domain ".$type->getDomain().", authority ".$type->getAuthority().", and keyword ".$type->getKeyword()." is not unique--there are ".$res->getNumberOfRows()." copies.\n";
  945.  
  946.  
  947. //return either one anyway.
  948. $row = $res->getCurrentRow();
  949. $the_index = $row['id'];
  950. return $the_index;
  951.  
  952. }
  953.  
  954. }
  955.  
  956. /**
  957. * Given the object in table $table with id $id, change the field with name $key to $value
  958. *
  959. * @param object Id $id The Id of the object in question
  960. * @param string $table The table that our object resides in
  961. * @param string $key The name of the field
  962. * @param mixed $value The value to pass in
  963. *
  964. *
  965. * @access private
  966. */
  967. function _setField($id, $table, $key, $value)
  968. {
  969. //just an update query
  970. $dbHandler = Services::getService("DBHandler");
  971. $query= new UpdateQuery;
  972. $query->setTable($table);
  973. $query->addWhere("id='".addslashes($id->getIdString())."'");
  974. $query->setColumns(array(addslashes($key)));
  975. $query->setValues(array("'".addslashes($value)."'"));
  976. $dbHandler->query($query);
  977.  
  978.  
  979. }
  980.  
  981. /**
  982. * Given the object in table $table with id $id, get the field with name $key
  983. *
  984. * @param object Id $id The Id of the object in question
  985. * @param string $table The table that our object resides in
  986. * @param string $key The name of the field
  987. *
  988. * @return string
  989. *
  990. * @access private
  991. */
  992. function _getField($id, $table, $key)
  993. {
  994.  
  995. //just a select query
  996. $dbHandler = Services::getService("DBHandler");
  997. $query= new SelectQuery;
  998. $query->addTable($table);
  999. $query->addWhere("id='".addslashes($id->getIdString())."'");
  1000. $query->addColumn(addslashes($key));
  1001. $res=$dbHandler->query($query);
  1002. $row = $res->getCurrentRow();
  1003. $ret=$row[$key];
  1004. return $ret;
  1005. }
  1006.  
  1007. }
  1008.  
  1009. ?>

Documentation generated on Wed, 19 Sep 2007 10:24:22 -0400 by phpDocumentor 1.3.0RC3