/** * 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; }
/** * Construct this renderable. * * @param template $template The learning plan template. * @param context $pagecontext The page context. */ public function __construct(template $template, context $pagecontext) { $this->pagecontext = $pagecontext; $this->template = $template; $this->templatestatistics = new template_statistics($template->get_id()); $this->competencies = api::list_competencies_in_template($template); $this->canmanagecompetencyframeworks = has_capability('moodle/competency:competencymanage', $this->pagecontext); $this->canmanagetemplatecompetencies = has_capability('moodle/competency:templatemanage', $this->pagecontext); $this->manageurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', array('pagecontextid' => $this->pagecontext->id)); }
/** * Get the most often not completed competency for this template. * * Requires moodle/competency:templateview capability at the system context. * * @param mixed $templateorid The id or the template. * @param int $skip The number of records to skip * @param int $limit The max number of records to return * @return competency[] */ public static function get_least_proficient_competencies_for_template($templateorid, $skip = 0, $limit = 100) { static::require_enabled(); $template = $templateorid; if (!is_object($template)) { $template = new template($template); } // First we do a permissions check. if (!$template->can_read()) { throw new required_capability_exception($template->get_context(), 'moodle/competency:templateview', 'nopermissions', ''); } return user_competency_plan::get_least_proficient_competencies_for_template($template->get_id(), $skip, $limit); }
/** * Set-up a template page. * * Example: * list($title, $subtitle) = page_helper::setup_for_template($pagecontextid, $url, $template, $pagetitle); * echo $OUTPUT->heading($title); * echo $OUTPUT->heading($subtitle, 3); * * @param int $pagecontextid The page context ID. * @param moodle_url $url The current page. * @param \core_competency\template $template The template, if any. * @param string $subtitle The title of the subpage, if any. * @param string $returntype The desired return page. * @return array With the following: * - Page title * - Page sub title * - Return URL */ public static function setup_for_template($pagecontextid, moodle_url $url, $template = null, $subtitle = '', $returntype = null) { global $PAGE, $SITE; $pagecontext = context::instance_by_id($pagecontextid); $context = $pagecontext; if (!empty($template)) { $context = $template->get_context(); } $templatesurl = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid)); $templateurl = null; if ($template) { $templateurl = new moodle_url('/admin/tool/lp/templatecompetencies.php', ['templateid' => $template->get_id(), 'pagecontextid' => $pagecontextid]); } $returnurl = $templatesurl; if ($returntype != 'templates' && $templateurl) { $returnurl = $templateurl; } $PAGE->navigation->override_active_url($templatesurl); $PAGE->set_context($pagecontext); if (!empty($template)) { $title = format_string($template->get_shortname(), true, array('context' => $context)); } else { $title = get_string('templates', 'tool_lp'); } if ($pagecontext->contextlevel == CONTEXT_SYSTEM) { $heading = $SITE->fullname; } else { if ($pagecontext->contextlevel == CONTEXT_COURSECAT) { $heading = $pagecontext->get_context_name(); } else { throw new coding_exception('Unexpected context!'); } } $PAGE->set_pagelayout('admin'); $PAGE->set_url($url); $PAGE->set_title($title); $PAGE->set_heading($heading); if (!empty($template)) { $PAGE->navbar->add($title, $templateurl); if (!empty($subtitle)) { $PAGE->navbar->add($subtitle, $url); } } else { if (!empty($subtitle)) { // We're in a sub page without a specific template. $PAGE->navbar->add($subtitle, $url); } } return array($title, $subtitle, $returnurl); }
/** * Update from template. * * Bulk update a lot of plans from a template * * @param template $template * @return bool */ public static function update_multiple_from_template(template $template) { global $DB; if (!$template->is_valid()) { // As we will bypass this model's validation we rely on the template being validated. throw new \coding_exception('The template must be validated before updating plans.'); } $params = array('templateid' => $template->get_id(), 'status' => self::STATUS_COMPLETE, 'name' => $template->get_shortname(), 'description' => $template->get_description(), 'descriptionformat' => $template->get_descriptionformat(), 'duedate' => $template->get_duedate()); $sql = "UPDATE {" . self::TABLE . "}\n SET name = :name,\n description = :description,\n descriptionformat = :descriptionformat,\n duedate = :duedate\n WHERE templateid = :templateid\n AND status != :status"; return $DB->execute($sql, $params); }
/** * Return an array of templates persistent with their missing userids. * * Note that only cohorts associated with visible templates are considered, * as well as only templates with a due date in the future, or no due date. * * @param int $lastruntime The last time the Cohort ssync task ran. * @param bool $unlinkedaremissing When true, unlinked plans are considered as missing. * @return array( array( * 'template' => \core_competency\template, * 'userids' => array * )) */ public static function get_all_missing_plans($lastruntime = 0, $unlinkedaremissing = false) { global $DB; $planwhereclause = " WHERE (p.id is NULL\n AND (cm.timeadded >= :lastruntime1\n OR tc.timecreated >= :lastruntime3\n OR t.timemodified >= :lastruntime4))"; if ($unlinkedaremissing) { $planwhereclause .= " OR (p.origtemplateid IS NOT NULL AND cm.timeadded < :lastruntime2)"; } $sql = "SELECT " . $DB->sql_concat('cm.userid', 'tc.templateid') . " as uniqueid, cm.userid, t.*\n FROM {cohort_members} cm\n JOIN {" . self::TABLE . "} tc ON cm.cohortid = tc.cohortid\n JOIN {" . template::TABLE . "} t\n ON (tc.templateid = t.id AND t.visible = 1)\n AND (t.duedate = 0 OR t.duedate > :time1)\n LEFT JOIN {" . plan::TABLE . "} p ON (cm.userid = p.userid AND (t.id = p.templateid OR t.id = p.origtemplateid))\n {$planwhereclause}\n ORDER BY t.id"; $params = array('time1' => time(), 'time2' => time(), 'lastruntime1' => $lastruntime, 'lastruntime2' => $lastruntime, 'lastruntime3' => $lastruntime, 'lastruntime4' => $lastruntime); $results = $DB->get_records_sql($sql, $params); $missingplans = array(); foreach ($results as $usertemplate) { $userid = $usertemplate->userid; // Check if template already exist in the array. if (isset($missingplans[$usertemplate->id])) { $missingplans[$usertemplate->id]['userids'][] = $userid; } else { unset($usertemplate->userid); unset($usertemplate->uniqueid); $template = new template(0, $usertemplate); $missingplans[$template->get_id()]['template'] = $template; $missingplans[$template->get_id()]['userids'][] = $userid; } } return array_values($missingplans); }