| 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 | 
            * Page to edit a plan.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    tool_lp
  | 
        
        
            | 
            | 
           21 | 
            * @copyright  2015 David Monllao
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           require_once(__DIR__ . '/../../../config.php');
  | 
        
        
            | 
            | 
           26 | 
           require_once($CFG->libdir.'/adminlib.php');
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           $userid = optional_param('userid', false, PARAM_INT);
  | 
        
        
            | 
            | 
           29 | 
           $id = optional_param('id', false, PARAM_INT);
  | 
        
        
            | 
            | 
           30 | 
           $returntype = optional_param('return', null, PARAM_ALPHA);
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
           require_login(0, false);
  | 
        
        
            | 
            | 
           33 | 
           \core_competency\api::require_enabled();
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
           $url = new moodle_url('/admin/tool/lp/editplan.php', array('id' => $id, 'userid' => $userid, 'return' => $returntype));
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
           $plan = null;
  | 
        
        
            | 
            | 
           38 | 
           if (empty($id)) {
  | 
        
        
            | 
            | 
           39 | 
               $pagetitle = get_string('addnewplan', 'tool_lp');
  | 
        
        
            | 
            | 
           40 | 
               list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_plan($userid, $url, null, $pagetitle, $returntype);
  | 
        
        
            | 
            | 
           41 | 
           } else {
  | 
        
        
            | 
            | 
           42 | 
               $plan = \core_competency\api::read_plan($id);
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
               // The userid parameter must be the same as the owner of the plan.
  | 
        
        
            | 
            | 
           45 | 
               if ($userid != $plan->get('userid')) {
  | 
        
        
            | 
            | 
           46 | 
                   throw new coding_exception('Inconsistency between the userid parameter and the userid of the plan');
  | 
        
        
            | 
            | 
           47 | 
               }
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
               $pagetitle = get_string('editplan', 'tool_lp');
  | 
        
        
            | 
            | 
           50 | 
               list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_plan($userid, $url, $plan, $pagetitle, $returntype);
  | 
        
        
            | 
            | 
           51 | 
           }
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
           $output = $PAGE->get_renderer('tool_lp');
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
           // Custom data to pass to the form.
  | 
        
        
            | 
            | 
           56 | 
           $customdata = array('userid' => $userid, 'context' => $PAGE->context, 'persistent' => $plan);
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
           // User can create plan if he can_manage_user with active/complete status
  | 
        
        
            | 
            | 
           59 | 
           // or if he can_manage_user_draft with draft status.
  | 
        
        
            | 
            | 
           60 | 
           $cancreate = \core_competency\plan::can_manage_user_draft($userid) || \core_competency\plan::can_manage_user($userid);
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
           // If editing plan, check if user has permissions to edit it.
  | 
        
        
            | 
            | 
           63 | 
           if ($plan != null) {
  | 
        
        
            | 
            | 
           64 | 
               if (!$plan->can_manage()) {
  | 
        
        
            | 
            | 
           65 | 
                   throw new required_capability_exception($PAGE->context, 'moodle/competency:planmanage', 'nopermissions', '');
  | 
        
        
            | 
            | 
           66 | 
               }
  | 
        
        
            | 
            | 
           67 | 
               if (!$plan->can_be_edited()) {
  | 
        
        
            | 
            | 
           68 | 
                   throw new coding_exception('Completed plan can not be edited');
  | 
        
        
            | 
            | 
           69 | 
               }
  | 
        
        
            | 
            | 
           70 | 
           } else if (!$cancreate) {
  | 
        
        
            | 
            | 
           71 | 
               throw new required_capability_exception($PAGE->context, 'moodle/competency:planmanage', 'nopermissions', '');
  | 
        
        
            | 
            | 
           72 | 
           }
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
           $form = new \tool_lp\form\plan($url->out(false), $customdata);
  | 
        
        
            | 
            | 
           75 | 
           if ($form->is_cancelled()) {
  | 
        
        
            | 
            | 
           76 | 
               redirect($returnurl);
  | 
        
        
            | 
            | 
           77 | 
           }
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
           $data = $form->get_data();
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
           if ($data) {
  | 
        
        
            | 
            | 
           82 | 
               if (empty($data->id)) {
  | 
        
        
            | 
            | 
           83 | 
                   $plan = \core_competency\api::create_plan($data);
  | 
        
        
            | 
            | 
           84 | 
                   $returnurl = new moodle_url('/admin/tool/lp/plan.php', ['id' => $plan->get('id')]);
  | 
        
        
            | 
            | 
           85 | 
                   $returnmsg = get_string('plancreated', 'tool_lp');
  | 
        
        
            | 
            | 
           86 | 
               } else {
  | 
        
        
            | 
            | 
           87 | 
                   \core_competency\api::update_plan($data);
  | 
        
        
            | 
            | 
           88 | 
                   $returnmsg = get_string('planupdated', 'tool_lp');
  | 
        
        
            | 
            | 
           89 | 
               }
  | 
        
        
            | 
            | 
           90 | 
               redirect($returnurl, $returnmsg, null, \core\output\notification::NOTIFY_SUCCESS);
  | 
        
        
            | 
            | 
           91 | 
           }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
           echo $output->header();
  | 
        
        
            | 
            | 
           94 | 
           echo $output->heading($title);
  | 
        
        
            | 
            | 
           95 | 
           if (!empty($subtitle)) {
  | 
        
        
            | 
            | 
           96 | 
               echo $output->heading($subtitle, 3);
  | 
        
        
            | 
            | 
           97 | 
           }
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
           $form->display();
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
           echo $output->footer();
  |