/**
     * Returns costcenter context instance.
     *
     * @static
     * @param int $instanceid
     * @param int $strictness
     * @return context_costcenter context instance
     */
    public static function instance($instanceid, $strictness = MUST_EXIST) {
        global $DB;

        if ($context = context::cache_get(CONTEXT_COSTCENTER, $instanceid)) {
            return $context;
        }

        if (!$record = $DB->get_record('context', array('contextlevel' => CONTEXT_COSTCENTER, 'instanceid' => $instanceid))) {
            if ($costcenter = $DB->get_record('local_costcenter', array('id' => $instanceid), 'id,parentid', $strictness)) {
                if ($costcenter->parentid) {
                    $parentcontext = context_costcenter::instance($costcenter->parentid);
                    $record = context::insert_context_record(CONTEXT_COSTCENTER, $costcenter->id, $parentcontext->path);
                } else {
                    $record = context::insert_context_record(CONTEXT_COSTCENTER, $costcenter->id, '/' . SYSCONTEXTID, 0);
                }
            }
        }

        if ($record) {
            $context = new context_costcenter($record);
            context::cache_add($context);
            return $context;
        }

        return false;
    }
Exemple #2
0
 /**
  * @method costcenter_delete_instance Delete the costcenter
  * @param int $id costcenter id     
  */
 public function costcenter_delete_instance($id) {
     global $DB, $CFG;
     //$department = new department ();
      $costcenter = $DB->get_field('local_costcenter','fullname',array('id'=>$id));
     $delete_costcenters = $this->get_childitems($id);
     $DB->delete_records('local_costcenter', array('id' => $id));
     $DB->delete_records('local_costcenter_permissions ', array('costcenterid' => $id));
     // --- deleting instance in costcenter context level---------------------- 
     $costcentercontext = context_costcenter::instance($id);
     $costcentercontext->delete();
     //------------------------------------------------------------------------
     $currenturl = "{$CFG->wwwroot}/local/costcenter/index.php";
     $this->set_confirmation(get_string('deletesuccesscostcenter', 'local_costcenter',$costcenter), $currenturl, array('style' => 'notifysuccess'));
 }
             $newdep->path = '';
         } else {
             /* ---parent item must exist--- */
             $parent = $DB->get_record('local_costcenter', array('id' => $newdep->parentid));
             $newdep->depth = $parent->depth + 1;
             $newdep->path = $parent->path;
         }
         /* ---get next child item that need to provide--- */
         if (!$sortorder = $costcenter->get_next_child_sortthread($newdep->parentid, 'local_costcenter')) {
             return false;
         }
 
         $newdep->sortorder = $sortorder;
         $costcenters = $DB->insert_record('local_costcenter', $newdep);
          // used to inserting costcenterinfo to context(mapping to costcenter context level)
         $context = context_costcenter::instance($costcenters, MUST_EXIST);
         
         $DB->set_field('local_costcenter', 'path', $newdep->path . '/' . $costcenters, array('id' => $costcenters));   
         $context->mark_dirty();
         $data->costcenterid = $costcenters;
         $data->zone  = 'Agents';
         $data->zone_code  = 'EXT';
     } else {
         $data->costcenterid = $department->id;
         $data->zone  = $department->fullname;
         $data->zone_code  = $department->shortname;
     }
     $data->timecreated = time();
     $data->timemodified = 0;
     $data->department = '';
     $data->designation = '';