/**
  * Do the job.
  */
 public function execute()
 {
     if (!api::is_enabled()) {
         return;
     }
     $missingplans = template_cohort::get_all_missing_plans(self::get_last_run_time());
     foreach ($missingplans as $missingplan) {
         foreach ($missingplan['userids'] as $userid) {
             try {
                 api::create_plan_from_template($missingplan['template'], $userid);
             } catch (\Exception $e) {
                 debugging(sprintf('Exception caught while creating plan for user %d from template %d. Message: %s', $userid, $missingplan['template']->get_id(), $e->getMessage()), DEBUG_DEVELOPER);
             }
         }
     }
 }
Example #2
0
 public function test_hook_cohort_deleted()
 {
     $this->resetAfterTest();
     $this->setAdminUser();
     $datagen = $this->getDataGenerator();
     $corecompgen = $datagen->get_plugin_generator('core_competency');
     $c1 = $datagen->create_cohort();
     $c2 = $datagen->create_cohort();
     $t1 = $corecompgen->create_template();
     $t2 = $corecompgen->create_template();
     // Create the template cohorts.
     core_competency\api::create_template_cohort($t1->get_id(), $c1->id);
     core_competency\api::create_template_cohort($t1->get_id(), $c2->id);
     core_competency\api::create_template_cohort($t2->get_id(), $c1->id);
     // Check that the association was made.
     $this->assertEquals(2, \core_competency\template_cohort::count_records(array('templateid' => $t1->get_id())));
     $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t2->get_id())));
     // Delete the first cohort.
     cohort_delete_cohort($c1);
     // Check that the association was removed.
     $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t1->get_id())));
     $this->assertEquals(0, \core_competency\template_cohort::count_records(array('templateid' => $t2->get_id())));
 }
Example #3
0
 public function test_delete_template_cohort()
 {
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $dg = $this->getDataGenerator();
     $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
     $c1 = $dg->create_cohort();
     $c2 = $dg->create_cohort();
     $t1 = $lpg->create_template();
     $t2 = $lpg->create_template();
     $tc1 = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id));
     $tc1 = $lpg->create_template_cohort(array('templateid' => $t2->get_id(), 'cohortid' => $c2->id));
     $this->assertEquals(2, \core_competency\template_cohort::count_records());
     $this->assertEquals(1, \core_competency\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
     $this->assertEquals(1, \core_competency\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
     // Delete existing.
     $result = api::delete_template_cohort($t1->get_id(), $c1->id);
     $this->assertTrue($result);
     $this->assertEquals(1, \core_competency\template_cohort::count_records());
     $this->assertEquals(0, \core_competency\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
     $this->assertEquals(1, \core_competency\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
     // Delete non-existant.
     $result = api::delete_template_cohort($t1->get_id(), $c1->id);
     $this->assertTrue($result);
     $this->assertEquals(1, \core_competency\template_cohort::count_records());
     $this->assertEquals(0, \core_competency\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
     $this->assertEquals(1, \core_competency\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
 }
Example #4
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;
 }
Example #5
0
 /**
  * Create a new template cohort.
  *
  * @param array|stdClass $record
  * @return template_cohort
  */
 public function create_template_cohort($record = null)
 {
     $record = (object) $record;
     if (!isset($record->templateid)) {
         throw new coding_exception('The templateid value is required.');
     }
     if (!isset($record->cohortid)) {
         throw new coding_exception('The cohortid value is required.');
     }
     $tplcohort = new template_cohort(0, $record);
     $tplcohort->create();
     return $tplcohort;
 }
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);
Example #7
0
 public function test_create_template_cohort()
 {
     $this->resetAfterTest(true);
     $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
     $c1 = $this->getDataGenerator()->create_cohort();
     $c2 = $this->getDataGenerator()->create_cohort();
     $t1 = $lpg->create_template();
     $this->assertEquals(0, template_cohort::count_records());
     $tc = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id));
     $this->assertEquals(1, template_cohort::count_records());
     $tc = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c2->id));
     $this->assertEquals(2, template_cohort::count_records());
     $this->assertInstanceOf('\\core_competency\\template_cohort', $tc);
 }
Example #8
0
 protected function get_other_values(renderer_base $output)
 {
     $context = $this->persistent->get_context();
     return array('duedateformatted' => userdate($this->persistent->get_duedate()), 'cohortscount' => template_cohort::count_records(array('templateid' => $this->persistent->get_id())), 'planscount' => plan::count_records(array('templateid' => $this->persistent->get_id())), 'canmanage' => $this->persistent->can_manage(), 'canread' => $this->persistent->can_read(), 'contextname' => $context->get_context_name(), 'contextnamenoprefix' => $context->get_context_name(false));
 }