/**
  * Convenience method to instantiate the event.
  *
  * @param template $template The template.
  * @return self
  */
 public static final function create_from_template(template $template)
 {
     if (!$template->get_id()) {
         throw new \coding_exception('The template ID must be set.');
     }
     $event = static::create(array('contextid' => $template->get_contextid(), 'objectid' => $template->get_id()));
     $event->add_record_snapshot(template::TABLE, $template->to_record());
     return $event;
 }
Пример #2
0
 /**
  * Create learning plans from a template and cohort.
  *
  * @param  mixed $templateorid The template object or ID.
  * @param  int $cohortid The cohort ID.
  * @param  bool $recreateunlinked When true the plans that were unlinked from this template will be re-created.
  * @return int The number of plans created.
  */
 public static function create_plans_from_template_cohort($templateorid, $cohortid, $recreateunlinked = false)
 {
     global $DB, $CFG;
     static::require_enabled();
     require_once $CFG->dirroot . '/cohort/lib.php';
     $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');
     }
     // Replicate logic in cohort_can_view_cohort() because we can't use it directly as we don't have a course context.
     $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
     $cohortcontext = context::instance_by_id($cohort->contextid);
     if (!$cohort->visible && !has_capability('moodle/cohort:view', $cohortcontext)) {
         throw new required_capability_exception($cohortcontext, 'moodle/cohort:view', 'nopermissions', '');
     }
     // Convert the template to a plan.
     $recordbase = $template->to_record();
     $recordbase->templateid = $recordbase->id;
     $recordbase->name = $recordbase->shortname;
     $recordbase->status = plan::STATUS_ACTIVE;
     unset($recordbase->id);
     unset($recordbase->timecreated);
     unset($recordbase->timemodified);
     unset($recordbase->usermodified);
     // Remove extra keys.
     $properties = plan::properties_definition();
     foreach ($recordbase as $key => $value) {
         if (!array_key_exists($key, $properties)) {
             unset($recordbase->{$key});
         }
     }
     // Create the plans.
     $created = 0;
     $userids = template_cohort::get_missing_plans($template->get_id(), $cohortid, $recreateunlinked);
     foreach ($userids as $userid) {
         $record = (object) (array) $recordbase;
         $record->userid = $userid;
         $plan = new plan(0, $record);
         if (!$plan->can_manage()) {
             // Silently skip members where permissions are lacking.
             continue;
         }
         $plan->create();
         // Trigger created event.
         \core\event\competency_plan_created::create_from_plan($plan)->trigger();
         $created++;
     }
     return $created;
 }