Source for file stipendgiver_object.php

Documentation is available at stipendgiver_object.php

  1. <?php                          
  2. /**
  3. * Defines a class to manipilate a stipendGiver
  4. *
  5. @package sloodle
  6. @copyright Copyright (c) 2008 Sloodle (various contributors)
  7. @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  8. @see http://slisweb.sjsu.edu/sl/index.php/Sloodle_Stipend_Giver
  9. *
  10. @author Paul Preibisch - aka Fire Centaur
  11. */
  12.  
  13. require_once(SLOODLE_LIBROOT.'/ipointbank_object.php');
  14.   class stipendGiverObject extends iPointBank{
  15.       
  16.       /*
  17.       * @var $startingBalance - the default value of the stipend
  18.       */
  19.       var $startingBalance = 0;
  20.       
  21.       /*
  22.       * @var $stipendGiverRec - the actual record of the stipendgiver from sloodle_stipengiver
  23.       */
  24.       var $stipendGiverRec = null;      
  25.       /*
  26.       * @var $icurrency - can be either iPoints or Lindens - specified by initial config page
  27.       */  
  28.       var $icurrency = '';
  29.       /*
  30.       * @var $sloodleId - sloodleId of this stipendgiver
  31.       */  
  32.       var $sloodleId = null;
  33.  
  34.       /*
  35.       * The class Contstructor
  36.       * @var $id - the sloodle id of this stipendgiver
  37.       */
  38.       function StipendGiverObject($id){
  39.           //parent::iPointBank($id);
  40.           $instanceid get_field('course_modules''instance''id'$id);   
  41.           $this->stipendGiverRec = get_record('sloodle_stipendgiver','sloodleid',$instanceid);
  42.           $this->startingBalance = $this->stipendGiverRec->amount;        
  43.           $this->itype $this->stipendGiverRec->icurrency;
  44.           $this->icurrency = $this->stipendGiverRec->icurrency;
  45.           $this->sloodleId=$id;
  46.       }
  47.       /**
  48.      * @method updateUserList 
  49.      * @author Paul Preibisch
  50.      * 
  51.      *  updateUserList searches through the list of users in the course,
  52.      *  and adds new stipends for new users to transactions table, and or removes students stipends
  53.      *  who have been removed from the class list
  54.      *  
  55.      * @package sloodle
  56.      * @return true if found
  57.      * @return false if not found
  58.      * 
  59.      * @staticvar $userList users in this course
  60.      * @staticvar $userId - moodle id of user sought
  61.      */
  62.       function updateUserList($userList){
  63.           //Get the existing users
  64.           //for each user who doesnt have a stipend allocated, allocate them 0 - because they have just been added
  65.            $newStipends array();
  66.            foreach ($userList as $u){
  67.                 if ($this->getStipendRec($u->id)==false){
  68.                     $newStipend new stdClass();
  69.                     $newStipend->userid $u->id;
  70.                     $newStipend->sloodleid $this->sloodleId;
  71.                     $newStipend->itype="stipend";
  72.                     $newStipend->amount=0//set amount to zero so teacher must manually add stipends for new students
  73.                     $newStipend->timemodified time();
  74.                     $newStipends[]=$newStipend;
  75.                 }
  76.             }
  77.             //now add to iPoint_trans records for the new students who have not been alocated the default stipend.
  78.             foreach ($newStipends as $nS){
  79.                 $this->makeTransaction($nS);
  80.             }
  81.             //also, check to see if any students have been removed from the class, then we should
  82.             //remove their stipends!
  83.                          //build list of students who have been allocated stipends
  84.               //get existing transactions
  85.                 //compare userlist with all the stipend transactions.  If there is a transactopm 
  86.                 //for a user who is not in the course, remove that stipend allocation
  87.                $tRecs $this->getTransactionRecords();         
  88.                 foreach ($tRecs as $t){
  89.                  if ($t->itype=="stipend"){
  90.                      //check if this user is in this class
  91.                      $found $this->searchUserList($userList$t->userid);
  92.                      //if user of this stipend is not in the userList, remove this stipend
  93.                      if (!$found){
  94.                          $this->removeTransaction($t->userid,"stipend");
  95.                      }                 
  96.                  }
  97.                 }
  98.       }
  99.      /**
  100.      * @method searchUserList 
  101.      * @author Paul Preibisch
  102.      * 
  103.      *  searchUserList searches through the list of users
  104.      *  
  105.      * @package sloodle
  106.      * @return true if found
  107.      * @return false if not found
  108.      * 
  109.      * @staticvar $userList users in this course
  110.      * @staticvar $userId - moodle id of user sought
  111.      */
  112.      
  113.      function searchUserList($userList$userid){
  114.            $found false;
  115.            foreach ($userList as $u){
  116.                if ($u->id == $userid$found=true;
  117.            }
  118.            return $found;
  119.      }
  120.       /*
  121.       * getStartingBalance gets the default stipend property for this stipendgiver object
  122.       */
  123.       function getStartingBalance(){
  124.         return $this->startingBalance;      
  125.       }
  126.       /*
  127.       * setStartingBalance - sets this class property starting balance which is actually the default stipend. DOES NOT write to the db
  128.       * @var $newStartingBalace - x
  129.       */
  130.       function setStartingBalance($newStartingBalance){
  131.         $this->startingBalance = $newStartingBalance;  
  132.       }
  133.       /*
  134.       * geticurrency gets the currency property of this object
  135.       */
  136.       function geticurrency(){
  137.           return $this->icurrency;      
  138.       }
  139.       /*
  140.       * get gets the currency property of this object
  141.       */
  142.       function getSloodleId(){
  143.           return $this->sloodleId;      
  144.       }
  145.  
  146.      
  147.      /**
  148.      * @method getStipendBudget 
  149.      * @author Paul Preibisch
  150.      * 
  151.      *  getStipendBudget gets the stipend amount allocated to this user
  152.      *  
  153.      * @package sloodle
  154.      * @return returns bugeted amount for this user,
  155.      * @return If $userId is null, the default stipend of the stipend is returned
  156.      * @returns false if no stipend was allocated for the userId specified
  157.      * 
  158.      * @staticvar $userId moodle id of the user
  159.      */
  160.      function getStipendBudget($userid,$userList){
  161.           //transactionRecords fields are: 
  162.           //avuuid,userid,avname,amount,type,timemodified
  163.           //first update the transaction list in case new students have been added
  164.           $this->updateUserList($userList);
  165.           $budgetAmount 0;
  166.           if ($userid==null)
  167.             $transRecs $this->getTransactionRecords();
  168.           else  {
  169.             $transRecs $this->getTransactionRecords($userid);          
  170.           }
  171.           if ($transRecs)   {
  172.             foreach ($transRecs as $t)
  173.                if ($t->itype=='stipend')                
  174.                    $budgetAmount +=$t->amount;
  175.             return $budgetAmount
  176.           }
  177.           else return false;
  178.          
  179.      }
  180.      /**
  181.      * @method setStipendBudget 
  182.      * @author Paul Preibisch
  183.      * 
  184.      *  setStipendBudget can be used to set the default stipend,
  185.      *  or a stipend of an individual user
  186.      *  
  187.      * @package sloodle
  188.      * @return true if record can be updates
  189.      * @return false if there was an error updating ipoint_trans table
  190.      * @staticvar $amount is amount of the stipend - defaults to zero
  191.      * @staticvar $userId moodle id of the user
  192.      * 
  193.      * 
  194.      */
  195.      
  196.      function setStipendBudget($amount=0,$userId=null){
  197.         $update new stdClass();  
  198.         if ($userId==null)//modify default stipend                        
  199.             $this->setStartingBalance($amount);            
  200.             $updatedStipendGiver new stdClass();
  201.             $updatedStipendGiver->id $this->stipendGiverRec->id;
  202.             $updatedStipendGiver->amount $amount;
  203.             if (!update_record("sloodle_stipendgiver",$updatedStipendGiver)){
  204.                 $sloodle->response->quick_output(-701'STIPENDGIVER''Failed to update stipendGiver for default stipend in setStipendBudget function'FALSE);
  205.                 return false;
  206.             }
  207.         }else//modify stipend budget of individual user
  208.             
  209.             $stipendRec $this->getStipendRec($userId);            
  210.             $update new stdClass();
  211.             $update->id $stipendRec->id;
  212.             $update->amount $amount;
  213.             if (!update_record('sloodle_ipoint_trans'$update)){
  214.              $sloodle->response->quick_output(-701'STIPENDGIVER''Failed to update stipendGiver in setStipendBudget function'FALSE);
  215.                  return false;
  216.              }
  217.              
  218.         }
  219.             
  220.         return true;
  221.      }
  222.      /**
  223.      * @method getStipendRec 
  224.      * @author Paul Preibisch
  225.      * 
  226.      *  getStipendRec returns the stipend record for this user.
  227.      *  itype in the sloodle_ipoint_trans table must be 'stipend',
  228.      *       
  229.      * @package sloodle
  230.      * @returns a record from the sloodle_ipoint_trans table
  231.      * @returns false if no stipend has been allocated
  232.      * @staticvar $userId moodle id of the user
  233.      * 
  234.      * 
  235.      */
  236.      function getStipendRec($userid){
  237.          global $CFG;
  238.          $userStipendId null;
  239.          $sql 'SELECT * FROM '.$CFG->prefix.'sloodle_ipoint_trans'.
  240.                              ' WHERE itype=\'stipend\' AND sloodleid='.$this->getSloodleId(.
  241.                              ' AND userid='.$userid;
  242.          $userStipendId get_record_sql($sql);                     
  243.          return $userStipendId
  244.         
  245.         
  246.         }
  247.   }
  248. ?>

Documentation generated on Fri, 17 Jul 2009 11:02:33 +0100 by phpDocumentor 1.4.0