original_string: protected function calculate_context() {
          global $USER, $DB;
  
          $context = null;
          if (isset($this->properties->categoryid) && $this->properties->categoryid > 0) {
              $context = \context_coursecat::instance($this->properties->categoryid);
          } else if (isset($this->properties->courseid) && $this->properties->courseid > 0) {
              $context = \context_course::instance($this->properties->courseid);
          } else if (isset($this->properties->course) && $this->properties->course > 0) {
              $context = \context_course::instance($this->properties->course);
          } else if (isset($this->properties->groupid) && $this->properties->groupid > 0) {
              $group = $DB->get_record('groups', array('id' => $this->properties->groupid));
              $context = \context_course::instance($group->courseid);
          } else if (isset($this->properties->userid) && $this->properties->userid > 0
              && $this->properties->userid == $USER->id) {
              $context = \context_user::instance($this->properties->userid);
          } else if (isset($this->properties->userid) && $this->properties->userid > 0
              && $this->properties->userid != $USER->id &&
              isset($this->properties->instance) && $this->properties->instance > 0) {
              $cm = get_coursemodule_from_instance($this->properties->modulename, $this->properties->instance, 0,
                  false, MUST_EXIST);
              $context = \context_course::instance($cm->course);
          } else {
              $context = \context_user::instance($this->properties->userid);
          }
  
          return $context;
      }
code: protected function calculate_context() {
          global $USER, $DB;
  
          $context = null;
          if (isset($this->properties->categoryid) && $this->properties->categoryid > 0) {
              $context = \context_coursecat::instance($this->properties->categoryid);
          } else if (isset($this->properties->courseid) && $this->properties->courseid > 0) {
              $context = \context_course::instance($this->properties->courseid);
          } else if (isset($this->properties->course) && $this->properties->course > 0) {
              $context = \context_course::instance($this->properties->course);
          } else if (isset($this->properties->groupid) && $this->properties->groupid > 0) {
              $group = $DB->get_record('groups', array('id' => $this->properties->groupid));
              $context = \context_course::instance($group->courseid);
          } else if (isset($this->properties->userid) && $this->properties->userid > 0
              && $this->properties->userid == $USER->id) {
              $context = \context_user::instance($this->properties->userid);
          } else if (isset($this->properties->userid) && $this->properties->userid > 0
              && $this->properties->userid != $USER->id &&
              isset($this->properties->instance) && $this->properties->instance > 0) {
              $cm = get_coursemodule_from_instance($this->properties->modulename, $this->properties->instance, 0,
                  false, MUST_EXIST);
              $context = \context_course::instance($cm->course);
          } else {
              $context = \context_user::instance($this->properties->userid);
          }
  
          return $context;
      }
code_tokens: protected function calculate_context ( ) { global $ USER , $ DB ; $ context = null ; if ( isset ( $ this -> properties -> categoryid ) && $ this -> properties -> categoryid > 0 ) { $ context = \ context_coursecat :: instance ( $ this -> properties -> categoryid ) ; } else if ( isset ( $ this -> properties -> courseid ) && $ this -> properties -> courseid > 0 ) { $ context = \ context_course :: instance ( $ this -> properties -> courseid ) ; } else if ( isset ( $ this -> properties -> course ) && $ this -> properties -> course > 0 ) { $ context = \ context_course :: instance ( $ this -> properties -> course ) ; } else if ( isset ( $ this -> properties -> groupid ) && $ this -> properties -> groupid > 0 ) { $ group = $ DB -> get_record ( 'groups' , array ( 'id' => $ this -> properties -> groupid ) ) ; $ context = \ context_course :: instance ( $ group -> courseid ) ; } else if ( isset ( $ this -> properties -> userid ) && $ this -> properties -> userid > 0 && $ this -> properties -> userid == $ USER -> id ) { $ context = \ context_user :: instance ( $ this -> properties -> userid ) ; } else if ( isset ( $ this -> properties -> userid ) && $ this -> properties -> userid > 0 && $ this -> properties -> userid != $ USER -> id && isset ( $ this -> properties -> instance ) && $ this -> properties -> instance > 0 ) { $ cm = get_coursemodule_from_instance ( $ this -> properties -> modulename , $ this -> properties -> instance , 0 , false , MUST_EXIST ) ; $ context = \ context_course :: instance ( $ cm -> course ) ; } else { $ context = \ context_user :: instance ( $ this -> properties -> userid ) ; } return $ context ; }
docstring: Calculate the context value needed for an event.
  
  Event's type can be determine by the available value store in $data
  It is important to check for the existence of course/courseid to determine
  the course event.
  Default value is set to CONTEXT_USER
  
  @return \stdClass The context object.
docstring_tokens: Calculate the context value needed for an event .