示例#1
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;
 }
示例#2
0
if ($canmanagetemplate && ($removecohort = optional_param('removecohort', false, PARAM_INT)) !== false && confirm_sesskey()) {
    \core_competency\api::delete_template_cohort($template, $removecohort);
}
// Capture the form submission.
$form = new \tool_lp\form\template_cohorts($url->out(false), array('pagecontextid' => $pagecontextid));
if ($canmanagetemplate && ($data = $form->get_data()) && !empty($data->cohorts)) {
    $maxtocreate = 50;
    $maxreached = false;
    $i = 0;
    foreach ($data->cohorts as $cohortid) {
        // Create the template/cohort relationship.
        $relation = \core_competency\api::create_template_cohort($template, $cohortid);
        // Create a plan for each member if template visible, and the due date is not reached, and we didn't reach our limit yet.
        if ($template->get_visible() && $i < $maxtocreate && !$duedatereached) {
            // Only create a few plans right now.
            $tocreate = \core_competency\template_cohort::get_missing_plans($template->get_id(), $cohortid);
            if ($i + count($tocreate) <= $maxtocreate) {
                $i += \core_competency\api::create_plans_from_template_cohort($template, $cohortid);
            } else {
                $maxreached = true;
            }
        }
    }
    if ($i == 0) {
        $notification = get_string('noplanswerecreated', 'tool_lp');
    } else {
        if ($i == 1) {
            $notification = get_string('oneplanwascreated', 'tool_lp');
        } else {
            if ($maxreached) {
                $notification = get_string('aplanswerecreatedmoremayrequiresync', 'tool_lp', $i);