MDL-52485 tool_lp: Template due date can be changed at will
[moodle.git] / admin / tool / lp / template_cohorts.php
CommitLineData
926913fe
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * List cohorts linked to a template.
19 *
20 * @package tool_lp
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require(__DIR__ . '/../../../config.php');
26
27$id = required_param('id', PARAM_INT);
28$pagecontextid = required_param('pagecontextid', PARAM_INT); // Reference to the context we came from.
29
30require_login(0, false);
31
32$template = \tool_lp\api::read_template($id);
33$context = $template->get_context();
935e9e90
SG
34$canreadtemplate = $template->can_read();
35$canmanagetemplate = $template->can_manage();
36
37if (!$canreadtemplate) {
38 throw new required_capability_exception($context, 'tool/lp:templateread', 'nopermissions', '');
39}
926913fe
FM
40
41// Set up the page.
42$url = new moodle_url('/admin/tool/lp/template_cohorts.php', array(
43 'id' => $id,
44 'pagecontextid' => $pagecontextid
45));
46list($title, $subtitle) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template,
47 get_string('cohortssyncedtotemplate', 'tool_lp'));
48
13ef20f6 49// Remove cohort.
935e9e90 50if ($canmanagetemplate && ($removecohort = optional_param('removecohort', false, PARAM_INT)) !== false && confirm_sesskey()) {
13ef20f6
FM
51 \tool_lp\api::delete_template_cohort($template, $removecohort);
52}
53
dd1df082
FM
54// Capture the form submission.
55$form = new \tool_lp\form\template_cohorts($url->out(false), array('pagecontextid' => $pagecontextid));
935e9e90 56if ($canmanagetemplate && ($data = $form->get_data()) && !empty($data->cohorts)) {
dd1df082
FM
57 $i = 0;
58 foreach ($data->cohorts as $cohortid) {
59
60 // Create the template/cohort relationship.
61 $relation = \tool_lp\api::create_template_cohort($template, $cohortid);
62
964afa98
IT
63 // Create a plan for each member if template visible.
64 if ($template->get_visible()) {
65 $i += \tool_lp\api::create_plans_from_template_cohort($template, $cohortid);
66 }
dd1df082
FM
67 }
68 if ($i == 0) {
69 $notification = get_string('noplanswerecreated', 'tool_lp');
70 } else if ($i == 1) {
71 $notification = get_string('oneplanwascreated', 'tool_lp');
72 } else {
73 $notification = get_string('aplanswerecreated', 'tool_lp', $i);
74 }
75 redirect($url, $notification);
76}
77
926913fe
FM
78// Display the page.
79$output = $PAGE->get_renderer('tool_lp');
80echo $output->header();
81echo $output->heading($title);
82echo $output->heading($subtitle, 3);
935e9e90
SG
83if ($canmanagetemplate) {
84 if ($template->get_visible() == false) {
85 // Display message to prevent that cohort will not be synchronzed if the template is hidden.
86 echo $output->notify_message(get_string('templatecohortnotsyncedwhilehidden', 'tool_lp'));
87 }
88 echo $form->display();
964afa98 89}
935e9e90 90
926913fe
FM
91$page = new \tool_lp\output\template_cohorts_page($template, $url);
92echo $output->render($page);
93echo $output->footer();