matrixpoint.json.php 8.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 *
 * @package    mahara
 * @subpackage module-framework
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */

define('INTERNAL', 1);
13
define('PUBLIC', 1);
14 15 16 17 18 19 20 21 22
define('JSON', 1);

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('module', 'framework');
safe_require('artefact', 'annotation');
safe_require('blocktype', 'annotation');

global $USER;

23 24 25 26
if (!is_plugin_active('annotation','blocktype')) {
    json_reply(true, get_string('needtoactivate', 'module.framework'));
}

27 28
$framework  = param_integer('framework');
$option     = param_integer('option');
29
$viewid       = param_integer('view');
30
$action     = param_alphanum('action', 'form');
Robert Lyon's avatar
Robert Lyon committed
31
form_validate(param_variable('sesskey', null));
32
$evidence = get_record('framework_evidence', 'framework', $framework, 'element', $option, 'view', $viewid);
33 34

if ($action == 'update') {
35
    // When we click a dot on the matrix and add an annotation
36
    require_once(get_config('docroot') . 'blocktype/lib.php');
37 38 39 40
    $shortname = get_field_sql("SELECT fse.shortname FROM {framework_standard_element} fse
                                JOIN {framework_standard} fs ON fs.id = fse.standard
                                WHERE fs.framework = ? and fse.id = ?", array($framework, $option));
    $title = get_string('Annotation', 'artefact.annotation') . ': ' . $shortname;
41
    $text = clean_html(param_variable('text', ''));
42 43 44 45 46 47 48 49
    $allowfeedback = param_boolean('allowfeedback');
    $retractable = param_integer('retractable', 0);
    $blockid = param_integer('blockconfig', 0);
    $tags = param_variable('tags', '');
    $tags = explode(',', $tags);
    $values = array('title' => $title,
                    'text' => $text,
                    'tags' => $tags,
50
                    'allowfeedback' => $allowfeedback,
51 52 53 54
                    'retractable' => $retractable,
                    'retractedonload' => 0,
                    );
    $bi = new BlockInstance($blockid);
55 56 57 58 59
    $view = $bi->get_view();
    if (!$USER->can_edit_view($view)) {
        json_reply(true, get_string('accessdenied', 'error'));
        exit;
    }
60 61 62 63 64 65 66 67 68

    $values = call_static_method(generate_class_name('blocktype', $bi->get('blocktype')), 'instance_config_save', $values, $bi);
    $title = (isset($values['title'])) ? $values['title'] : '';
    unset($values['title']);
    unset($values['_redrawblocks']);

    $bi->set('configdata', $values);
    $bi->set('title', $title);
    $bi->commit();
69

70 71 72 73 74
    if ($evidence) {
        $id = Framework::save_evidence($evidence->id, null, null, null, $bi->get('id'));
        $message = get_string('matrixpointupdated', 'module.framework');
    }
    else {
75
        $id = Framework::save_evidence(null, $framework, $option, $view->get('id'), $bi->get('id'));
76 77 78
        $message = get_string('matrixpointinserted', 'module.framework');
    }

79
    $class = 'icon icon-regular icon-circle begun';
80
    $choices = Framework::get_evidence_statuses($framework);
81 82
    $data = (object) array('id' => $id,
                           'class' => $class,
83
                           'view' => $view->get('id'),
84
                           'readyforassessment' => 1,
85 86
                           'option' => $option,
                           'title' => $choices[0]
87 88 89 90 91
                           );
    json_reply(false, array('message' => $message, 'data' => $data));
}
else if ($action == 'evidence') {
    global $USER;
92
    // When we click on one of the begun/incomplete/partialcomplete/completed symbols and submit that form
93 94
    if (!$evidence->id) {
        // problem need to return error
95 96 97 98 99
        json_reply(true, get_string('accessdenied', 'error'));
        exit;
    }
    require_once('view.php');
    $view = new View($evidence->view);
100
    if (!Framework::can_assess_user($view, $evidence->framework)) {
101 102
        json_reply(true, get_string('accessdenied', 'error'));
        exit;
103
    }
104

Robert Lyon's avatar
Robert Lyon committed
105
    $oldstate = $evidence->state;
106
    $reviewer = null;
107
    $assessment = param_alphanum('assessment', 0);
108 109
    $assessment = (int) $assessment;
    if (Framework::EVIDENCE_COMPLETED === $assessment) {
110
        $reviewer = $USER->get('id');
111
    }
Robert Lyon's avatar
Robert Lyon committed
112

113 114 115
    $id = Framework::save_evidence($evidence->id, null, null, null, $evidence->annotation, $assessment, $USER->get('id'));
    $message = get_string('matrixpointupdated', 'module.framework');

116
    // If we are changing to/from completed we need to change $completed to adjust the count on screen
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    $readyforassessment = 0;
    if ((Framework::EVIDENCE_BEGUN === (int) $oldstate) && (Framework::EVIDENCE_BEGUN !== $assessment)) {
        $readyforassessment = -1;
    }
    else if (Framework::EVIDENCE_BEGUN === $assessment) {
        $readyforassessment = 1;
    }
    $dontmatch = 0;
    if ((Framework::EVIDENCE_INCOMPLETE === (int) $oldstate) && (Framework::EVIDENCE_INCOMPLETE !== $assessment)) {
        $dontmatch = -1;
    }
    else if (Framework::EVIDENCE_INCOMPLETE === $assessment) {
        $dontmatch = 1;
    }
    $partiallycomplete = 0;
    if ((Framework::EVIDENCE_PARTIALCOMPLETE === (int) $oldstate) && (Framework::EVIDENCE_PARTIALCOMPLETE !== $assessment)) {
        $partiallycomplete = -1;
    }
    else if (Framework::EVIDENCE_PARTIALCOMPLETE === $assessment) {
        $partiallycomplete = 1;
    }
    $completed = 0;
139
    if ((Framework::EVIDENCE_COMPLETED === (int) $oldstate) && (Framework::EVIDENCE_COMPLETED !== $assessment)) {
Robert Lyon's avatar
Robert Lyon committed
140 141
        $completed = -1;
    }
142
    else if (Framework::EVIDENCE_COMPLETED === $assessment) {
Robert Lyon's avatar
Robert Lyon committed
143
        $completed = 1;
144
    }
145

146 147 148
    $currentstate = Framework::get_state_array($assessment, true);
    $class = $currentstate['classes'];
    $choices = Framework::get_evidence_statuses($framework);
149 150
    $data = (object) array('id' => $id,
                           'class' => $class,
151
                           'view' => $view->get('id'),
152 153 154
                           'readyforassessment' => $readyforassessment,
                           'dontmatch' => $dontmatch,
                           'partiallycomplete' => $partiallycomplete,
Robert Lyon's avatar
Robert Lyon committed
155
                           'completed' => $completed,
156 157
                           'option' => $option,
                           'title' => $choices[$assessment]
158 159 160
                           );
    json_reply(false, array('message' => $message, 'data' => $data));
}
161 162 163 164
else if ($action == 'feedback') {
    $annotationid = param_integer('annotationid');
    $annotation = new ArtefactTypeAnnotation((int) $annotationid);
    $blockid = param_integer('blockid');
165
    $bi = new BlockInstance($blockid);
166 167
    $message = param_variable('message');
    $ispublic = param_boolean('ispublic');
168 169 170 171 172
    $view = $bi->get_view();
    if (!can_view_view($view->get('id')) || !PluginBlocktypeAnnotation::has_feedback_allowed($bi->get('id'))) {
        json_reply(true, get_string('accessdenied', 'error'));
        exit;
    }
173 174 175 176 177 178
    $newlist = ArtefactTypeAnnotationfeedback::save_matrix_feedback($annotation, $view, $blockid, $message, $ispublic);
    $message = get_string('annotationfeedbacksubmitted', 'artefact.annotation');
    $data = (object) array('id' => $evidence->id, 'tablerows' => $newlist);
    json_reply(false, array('message' => $message, 'data' => $data));
}
else if ($action == 'delete') {
179 180 181 182
    // Clean up partial annotation block instance
    require_once(get_config('docroot') . 'blocktype/lib.php');
    $blockid = param_integer('blockconfig', 0);
    $bi = new BlockInstance($blockid);
183 184 185 186 187
    $view = $bi->get_view();
    if (!$USER->can_edit_view($view)) {
        json_reply(true, get_string('accessdenied', 'error'));
        exit;
    }
188 189
    $bi->delete();
    $data = (object) array('class' => false,
190
                           'view' => $view->get('id'),
191 192 193 194 195
                           'option' => $option
                           );
    json_reply(false, array('message' => '', 'data' => $data));
}
else {
196 197 198 199
    if (!can_view_view($viewid)) {
        json_reply(true, get_string('accessdenied', 'error'));
        exit;
    }
200 201 202 203 204 205
    $message = null;
    $state = ($evidence) ? $evidence->state : -1;
    $states = Framework::get_state_array($state);
    $params = (object) array(
        'framework' => $framework,
        'option' => $option,
206
        'view' => $viewid,
207 208
        'id' => ($evidence) ? $evidence->id : null,
        'annotation' => ($evidence) ? $evidence->annotation : null,
209 210 211 212
        'begun' => $states['begun']['state'],
        'incomplete' => $states['incomplete']['state'],
        'partialcomplete' => $states['partialcomplete']['state'],
        'completed' => $states['completed']['state'],
213
    );
214

215
    if ($evidence) {
216 217 218 219 220 221
        // There is an annotation in play
        $form = Framework::annotation_feedback_form($params);
    }
    else {
        $form = Framework::annotation_config_form($params);
    }
222 223
    $data = (object) array('form' => $form);
    json_reply(false, (object) array('message' => $message, 'data' => $data));
224
}