public static function create_plan_from_template($templateorid, $userid) {
        static::require_enabled();
        $template = $templateorid;
        if (!is_object($template)) {
            $template = new template($template);
        }

        // The user must be able to view the template to use it as a base for a plan.
        if (!$template->can_read()) {
            throw new required_capability_exception($template->get_context(), 'moodle/competency:templateview',
                'nopermissions', '');
        }
        // Can not create plan from a hidden template.
        if ($template->get('visible') == false) {
            throw new coding_exception('A plan can not be created from a hidden template');
        }

        // Convert the template to a plan.
        $record = $template->to_record();
        $record->templateid = $record->id;
        $record->userid = $userid;
        $record->name = $record->shortname;
        $record->status = plan::STATUS_ACTIVE;

        unset($record->id);
        unset($record->timecreated);
        unset($record->timemodified);
        unset($record->usermodified);

        // Remove extra keys.
        $properties = plan::properties_definition();
        foreach ($record as $key => $value) {
            if (!array_key_exists($key, $properties)) {
                unset($record->$key);
            }
        }

        $plan = new plan(0, $record);
        if (!$plan->can_manage()) {
            throw new required_capability_exception($plan->get_context(), 'moodle/competency:planmanage',
                'nopermissions', '');
        }

        // We first apply the permission checks as we wouldn't want to leak information by returning early that
        // the plan already exists.
        if (plan::record_exists_select('templateid = :templateid AND userid = :userid', array(
                'templateid' => $template->get('id'), 'userid' => $userid))) {
            return false;
        }

        $plan->create();

        // Trigger created event.
        \core\event\competency_plan_created::create_from_plan($plan)->trigger();
        return $plan;
    }
public static function create_plan_from_template($templateorid, $userid) {
        static::require_enabled();
        $template = $templateorid;
        if (!is_object($template)) {
            $template = new template($template);
        }

        // The user must be able to view the template to use it as a base for a plan.
        if (!$template->can_read()) {
            throw new required_capability_exception($template->get_context(), 'moodle/competency:templateview',
                'nopermissions', '');
        }
        // Can not create plan from a hidden template.
        if ($template->get('visible') == false) {
            throw new coding_exception('A plan can not be created from a hidden template');
        }

        // Convert the template to a plan.
        $record = $template->to_record();
        $record->templateid = $record->id;
        $record->userid = $userid;
        $record->name = $record->shortname;
        $record->status = plan::STATUS_ACTIVE;

        unset($record->id);
        unset($record->timecreated);
        unset($record->timemodified);
        unset($record->usermodified);

        // Remove extra keys.
        $properties = plan::properties_definition();
        foreach ($record as $key => $value) {
            if (!array_key_exists($key, $properties)) {
                unset($record->$key);
            }
        }

        $plan = new plan(0, $record);
        if (!$plan->can_manage()) {
            throw new required_capability_exception($plan->get_context(), 'moodle/competency:planmanage',
                'nopermissions', '');
        }

        // We first apply the permission checks as we wouldn't want to leak information by returning early that
        // the plan already exists.
        if (plan::record_exists_select('templateid = :templateid AND userid = :userid', array(
                'templateid' => $template->get('id'), 'userid' => $userid))) {
            return false;
        }

        $plan->create();

        // Trigger created event.
        \core\event\competency_plan_created::create_from_plan($plan)->trigger();
        return $plan;
    }
public static function create_plan_from_template ( $ templateorid , $ userid ) { static :: require_enabled ( ) ; $ template = $ templateorid ; if ( ! is_object ( $ template ) ) { $ template = new template ( $ template ) ; } // The user must be able to view the template to use it as a base for a plan. if ( ! $ template -> can_read ( ) ) { throw new required_capability_exception ( $ template -> get_context ( ) , 'moodle/competency:templateview' , 'nopermissions' , '' ) ; } // Can not create plan from a hidden template. if ( $ template -> get ( 'visible' ) == false ) { throw new coding_exception ( 'A plan can not be created from a hidden template' ) ; } // Convert the template to a plan. $ record = $ template -> to_record ( ) ; $ record -> templateid = $ record -> id ; $ record -> userid = $ userid ; $ record -> name = $ record -> shortname ; $ record -> status = plan :: STATUS_ACTIVE ; unset ( $ record -> id ) ; unset ( $ record -> timecreated ) ; unset ( $ record -> timemodified ) ; unset ( $ record -> usermodified ) ; // Remove extra keys. $ properties = plan :: properties_definition ( ) ; foreach ( $ record as $ key => $ value ) { if ( ! array_key_exists ( $ key , $ properties ) ) { unset ( $ record -> $ key ) ; } } $ plan = new plan ( 0 , $ record ) ; if ( ! $ plan -> can_manage ( ) ) { throw new required_capability_exception ( $ plan -> get_context ( ) , 'moodle/competency:planmanage' , 'nopermissions' , '' ) ; } // We first apply the permission checks as we wouldn't want to leak information by returning early that // the plan already exists. if ( plan :: record_exists_select ( 'templateid = :templateid AND userid = :userid' , array ( 'templateid' => $ template -> get ( 'id' ) , 'userid' => $ userid ) ) ) { return false ; } $ plan -> create ( ) ; // Trigger created event. \ core \ event \ competency_plan_created :: create_from_plan ( $ plan ) -> trigger ( ) ; return $ plan ; }
Create a learning plan from a template.

@param  mixed $templateorid The template object or ID.
@param  int $userid
@return false|\core_competency\plan Returns false when the plan already exists.
Create a learning plan from a template .