| 1 | efrain | 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 |   | 
        
           |  |  | 25 | require(__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 |   | 
        
           |  |  | 30 | require_login(0, false);
 | 
        
           |  |  | 31 | \core_competency\api::require_enabled();
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 | $template = \core_competency\api::read_template($id);
 | 
        
           |  |  | 34 | $context = $template->get_context();
 | 
        
           |  |  | 35 | $canreadtemplate = $template->can_read();
 | 
        
           |  |  | 36 | $canmanagetemplate = $template->can_manage();
 | 
        
           |  |  | 37 | $duedatereached = $template->get('duedate') > 0 && $template->get('duedate') < time();
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 | if (!$canreadtemplate) {
 | 
        
           |  |  | 40 |     throw new required_capability_exception($context, 'moodle/competency:templateview', 'nopermissions', '');
 | 
        
           |  |  | 41 | }
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 | // Set up the page.
 | 
        
           |  |  | 44 | $url = new moodle_url('/admin/tool/lp/template_cohorts.php', array(
 | 
        
           |  |  | 45 |     'id' => $id,
 | 
        
           |  |  | 46 |     'pagecontextid' => $pagecontextid
 | 
        
           |  |  | 47 | ));
 | 
        
           |  |  | 48 | list($title, $subtitle) = \tool_lp\page_helper::setup_for_template($pagecontextid, $url, $template,
 | 
        
           |  |  | 49 |     get_string('cohortssyncedtotemplate', 'tool_lp'));
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 | // Remove cohort.
 | 
        
           |  |  | 52 | if ($canmanagetemplate && ($removecohort = optional_param('removecohort', false, PARAM_INT)) !== false && confirm_sesskey()) {
 | 
        
           |  |  | 53 |     \core_competency\api::delete_template_cohort($template, $removecohort);
 | 
        
           |  |  | 54 | }
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 | // Capture the form submission.
 | 
        
           |  |  | 57 | $existingcohortsql =
 | 
        
           |  |  | 58 |     'SELECT c.id
 | 
        
           |  |  | 59 |        FROM {' . \core_competency\template_cohort::TABLE . '} tc
 | 
        
           |  |  | 60 |        JOIN {cohort} c ON c.id = tc.cohortid
 | 
        
           |  |  | 61 |       WHERE tc.templateid = :templateid';
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 | $existingcohorts = $DB->get_records_sql_menu($existingcohortsql, ['templateid' => $template->get('id')]);
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 | $form = new \tool_lp\form\template_cohorts($url->out(false), [
 | 
        
           |  |  | 66 |     'pagecontextid' => $pagecontextid,
 | 
        
           |  |  | 67 |     'excludecohorts' => array_keys($existingcohorts),
 | 
        
           |  |  | 68 | ]);
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 | if ($canmanagetemplate && ($data = $form->get_data()) && !empty($data->cohorts)) {
 | 
        
           |  |  | 71 |     $maxtocreate = 50;
 | 
        
           |  |  | 72 |     $maxreached = false;
 | 
        
           |  |  | 73 |     $i = 0;
 | 
        
           |  |  | 74 |     foreach ($data->cohorts as $cohortid) {
 | 
        
           |  |  | 75 |   | 
        
           |  |  | 76 |         // Create the template/cohort relationship.
 | 
        
           |  |  | 77 |         $relation = \core_competency\api::create_template_cohort($template, $cohortid);
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |         // Create a plan for each member if template visible, and the due date is not reached, and we didn't reach our limit yet.
 | 
        
           |  |  | 80 |         if ($template->get('visible') && $i < $maxtocreate && !$duedatereached) {
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |             // Only create a few plans right now.
 | 
        
           |  |  | 83 |             $tocreate = \core_competency\template_cohort::get_missing_plans($template->get('id'), $cohortid);
 | 
        
           |  |  | 84 |             if ($i + count($tocreate) <= $maxtocreate) {
 | 
        
           |  |  | 85 |                 $i += \core_competency\api::create_plans_from_template_cohort($template, $cohortid);
 | 
        
           |  |  | 86 |             } else {
 | 
        
           |  |  | 87 |                 $maxreached = true;
 | 
        
           |  |  | 88 |             }
 | 
        
           |  |  | 89 |         }
 | 
        
           |  |  | 90 |     }
 | 
        
           |  |  | 91 |     if ($i == 0) {
 | 
        
           |  |  | 92 |         $notification = get_string('noplanswerecreated', 'tool_lp');
 | 
        
           |  |  | 93 |     } else if ($i == 1) {
 | 
        
           |  |  | 94 |         $notification = get_string('oneplanwascreated', 'tool_lp');
 | 
        
           |  |  | 95 |     } else if ($maxreached) {
 | 
        
           |  |  | 96 |         $notification = get_string('aplanswerecreatedmoremayrequiresync', 'tool_lp', $i);
 | 
        
           |  |  | 97 |     } else {
 | 
        
           |  |  | 98 |         $notification = get_string('aplanswerecreated', 'tool_lp', $i);
 | 
        
           |  |  | 99 |     }
 | 
        
           |  |  | 100 |     redirect($url, $notification);
 | 
        
           |  |  | 101 | }
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 | // Display the page.
 | 
        
           |  |  | 104 | $output = $PAGE->get_renderer('tool_lp');
 | 
        
           |  |  | 105 | echo $output->header();
 | 
        
           |  |  | 106 | echo $output->heading($title);
 | 
        
           |  |  | 107 | echo $output->heading($subtitle, 3);
 | 
        
           |  |  | 108 | if ($canmanagetemplate) {
 | 
        
           |  |  | 109 |     if ($template->get('visible') == false) {
 | 
        
           |  |  | 110 |         // Display message to prevent that cohort will not be synchronzed if the template is hidden.
 | 
        
           |  |  | 111 |         echo $output->notify_message(get_string('templatecohortnotsyncedwhilehidden', 'tool_lp'));
 | 
        
           |  |  | 112 |     } else if ($duedatereached) {
 | 
        
           |  |  | 113 |         echo $output->notify_message(get_string('templatecohortnotsyncedwhileduedateispassed', 'tool_lp'));
 | 
        
           |  |  | 114 |     }
 | 
        
           |  |  | 115 |     echo $form->display();
 | 
        
           |  |  | 116 | }
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 | $page = new \tool_lp\output\template_cohorts_page($template, $url);
 | 
        
           |  |  | 119 | echo $output->render($page);
 | 
        
           |  |  | 120 | echo $output->footer();
 |