Commit 5f78ba40 authored by Astor Bizard's avatar Astor Bizard
Browse files

Updated variation management (picked from original VPL).

parent 42c6d2c9
......@@ -30,5 +30,9 @@ class variation_added extends variation_base {
protected function init() {
parent::init();
$this->data ['crud'] = 'c';
$this->legacyaction = 'added variation';
}
public function get_description() {
return $this->get_description_mod( 'added' );
}
}
<?php
// This file is part of VPL for Moodle - http://vpl.dis.ulpgc.es/
//
// VPL for Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VPL for Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with VPL for Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class for logging of assigned variation events
*
* @package mod_vpl
* @copyright 2020 onwards Juan Carlos Rodrguez-del-Pino
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Juan Carlos Rodrguez-del-Pino <jcrodriguez@dis.ulpgc.es>
*/
namespace mod_vpl\event;
defined( 'MOODLE_INTERNAL' ) || die();
require_once(dirname( __FILE__ ) . '/../../locallib.php');
class variation_assigned extends variation_base {
protected function init() {
parent::init();
$this->legacyaction = 'assigned variation';
$this->data ['crud'] = 'c';
}
public function get_description() {
return $this->get_description_mod( 'assigned' );
}
public static function log($vpl, $varid = null, $userid = null) {
$vplinstance = $vpl->get_instance();
$info = array (
'objectid' => $varid,
'contextid' => $vpl->get_context()->id,
'relateduserid' => $userid,
'courseid' => $vplinstance->course,
'other' => array('vplid' => $vplinstance->id),
);
parent::log( $info );
}
}
\ No newline at end of file
......@@ -26,20 +26,42 @@ namespace mod_vpl\event;
defined( 'MOODLE_INTERNAL' ) || die();
require_once(dirname( __FILE__ ) . '/../../locallib.php');
class variation_base extends vpl_base {
class variation_base extends base {
public static function get_objectid_mapping() {
return array('db' => VPL_VARIATIONS, 'restore' => VPL_VARIATIONS);
}
public static function get_other_mapping() {
// Nothing to map.
return false;
}
protected function init() {
parent::init();
$this->data ['crud'] = 'u';
$this->data ['edulevel'] = self::LEVEL_TEACHING;
$this->data ['objecttable'] = VPL_VARIATIONS;
$this->legacyaction = 'variations form';
}
public function get_description() {
return $this->get_description_mod( 'variation' );
}
public static function log($info) {
if (is_array( $info )) {
parent::log( $info );
public static function log($vpl, $varid = null) {
if (is_array($vpl)) {
$info = $vpl;
} else {
throw new \coding_exception( 'Parameter must be an array' );
$vplinstance = $vpl->get_instance();
$info = array (
'objectid' => $varid,
'contextid' => $vpl->get_context()->id,
'courseid' => $vplinstance->course,
'other' => array('vplid' => $vplinstance->id),
);
}
parent::log( $info );
}
public function get_url() {
return $this->get_url_base( 'view.php' );
}
public function get_description_mod($mod) {
$desc = 'The user with id ' . $this->userid . ' ' . $mod;
$desc .= ' variation with id ' . $this->objectid . ' of VPL activity with id ' . $this->other['vplid'];
if ($this->relateduserid) {
$desc .= ' for user with id ' . $this->relateduserid;
}
return $desc;
}
}
......@@ -28,5 +28,9 @@ class variation_deleted extends variation_base {
protected function init() {
parent::init();
$this->data['crud'] = 'd';
$this->legacyaction = 'deleted variation';
}
public function get_description() {
return $this->get_description_mod( 'deleted' );
}
}
......@@ -30,5 +30,9 @@ class variation_updated extends variation_base {
protected function init() {
parent::init();
$this->data ['crud'] = 'u';
$this->legacyaction = 'updated variation';
}
public function get_description() {
return $this->get_description_mod( 'updated' );
}
}
......@@ -45,7 +45,7 @@ class vpl_base extends base {
} else {
$einfo = array (
'objectid' => $vpl->get_instance()->id,
'context' => $vpl->get_context()
'contextid' => $vpl->get_context()->id
);
parent::log( $einfo );
}
......
......@@ -51,14 +51,14 @@ class mod_vpl_variation_form extends moodleform {
protected $varid;
protected $number;
// Parm $varid = -1 new variation.
public function __construct($page, $number, $varid = -1) {
public function __construct($page, $number = 0, $varid = 0) {
$this->number = $number;
$this->varid = $varid;
parent::__construct( $page );
}
protected function definition() {
$mform = & $this->_form;
if ($this->number >= 0) {
if ($this->number > 0) {
$title = get_string( 'variation', VPL, "{$this->number}" );
} else {
$title = get_string( 'add' );
......@@ -68,21 +68,21 @@ class mod_vpl_variation_form extends moodleform {
$mform->setType( 'id', PARAM_INT );
$mform->addElement( 'hidden', 'varid', $this->varid );
$mform->setType( 'varid', PARAM_INT );
$mform->addElement( 'text', 'identification', get_string( 'varidentification', VPL ), array (
'size' => '20'
) );
$mform->setDefault( 'identification', '' );
$mform->setType( 'identification', PARAM_RAW );
$mform->addElement( 'textarea', 'description', get_string( 'description', VPL ), array (
'cols' => 45,
'rows' => 5
) );
$mform->setType( 'description', PARAM_CLEANHTML );
$mform->setDefault( 'description', '' );
$mform->setType( 'identification', PARAM_ALPHANUM );
$fieldname = 'description' . $this->varid; // Allows multile editors in page.
$mform->addElement('editor', $fieldname, get_string('description', VPL));
$mform->setType($fieldname, PARAM_RAW);
$mform->setDefault( $fieldname, '' );
$buttongroup = array ();
$buttongroup [] = $mform->createElement( 'submit', 'save', get_string( 'save', VPL ) );
$buttongroup [] = $mform->createElement( 'submit', 'cancel', get_string( 'cancel' ) );
if ($this->number >= 0) {
if ($this->number > 0) {
$menssage = addslashes( get_string( 'delete' ) );
$onclick = 'onclick="return confirm(\'' . $menssage . '\')"';
$buttongroup [] = $mform->createElement( 'submit', 'delete', get_string( 'delete' ), $onclick );
......@@ -95,20 +95,15 @@ require_login();
$id = required_param( 'id', PARAM_INT );
$vpl = new mod_vpl( $id );
$vpl->prepare_page( 'forms/variations.php', array ( 'id' => $id ) );
vpl_include_jsfile( 'hideshow.js' );
$vplid = $vpl->get_instance()->id;
$vpl->prepare_page( 'forms/variations.php', array ( 'id' => $id ) );
$vpl->require_capability( VPL_MANAGE_CAPABILITY );
global $PAGE;
$PAGE->force_settings_menu();
$href = vpl_mod_href( 'forms/variations.php', 'id', $id );
$vpl->print_header( get_string( 'variations', VPL ) );
$vpl->print_heading_with_help( 'variations' );
// Generate default form and check for action.
if (optional_param( 'varid', - 13, PARAM_INT ) == - 13) { // No variation saved.
if (optional_param( 'varid', null, PARAM_INT ) === null) { // No variation saved.
$oform = new mod_vpl_variation_option_form( $href, $vpl );
if ($oform->is_cancelled()) {
vpl_inmediate_redirect( $href ); // Reload page.
if (isset($_POST['cancel'])) {
redirect( $href, get_string('cancelled') );
} else if ($fromform = $oform->get_data()) {
vpl_truncate_string( $fromform->variationtitle, 255 );
$instance = $vpl->get_instance();
......@@ -116,41 +111,43 @@ if (optional_param( 'varid', - 13, PARAM_INT ) == - 13) { // No variation saved.
$instance->variationtitle = $fromform->variationtitle;
$vpl->update();
\mod_vpl\event\vpl_variation_updated::log( $vpl );
vpl_inmediate_redirect( $href );
redirect( $href, get_string('updated', '', $instance->variationtitle) );
}
$vplinstmod = clone $vpl->get_instance();
$vplinstmod->id = $id;
$oform->set_data( $vplinstmod );
}
$mform = new mod_vpl_variation_form( $href, 0 );
if ($mform->is_cancelled()) {
vpl_inmediate_redirect( $href ); // Reload page.
$varid = optional_param( 'varid', 0, PARAM_INT );
$mform = new mod_vpl_variation_form( $href, 0, $varid );
if ( isset($_POST['cancel']) ) {
redirect( $href, get_string('cancelled') ); // Reload page.
} else if ($fromform = $mform->get_data()) {
if (isset( $fromform->delete )) { // Delete variation and its assignned variations to users.
if ( isset($_POST['delete']) ) { // Deletes variation and its assignned variations.
if ($DB->delete_records( VPL_VARIATIONS, array (
'id' => $fromform->varid,
'vpl' => $vplid
) )) {
\mod_vpl\event\variation_deleted::log( array (
'objectid' => $fromform->varid,
'context' => $vpl->get_context()
) );
\mod_vpl\event\variation_deleted::log( $vpl, $fromform->varid );
$DB->delete_records( VPL_ASSIGNED_VARIATIONS, array (
'id' => $fromform->varid
'variation' => $fromform->varid
) );
} else {
print_error( VPL_VARIATIONS . ' record not deleted' . $fromform->varid . ' ' . $vplid, VPL, $href );
}
redirect( $href, get_string('deleted') );
} else {
if ($fromform->varid == - 1) { // New record.
if ($fromform->varid <= 0) { // New record.
$fromform->vpl = $vplid;
unset( $fromform->id );
$fromform->description = $fromform->description0['text'];
vpl_truncate_variations( $fromform );
if ($vid = $DB->insert_record( VPL_VARIATIONS, $fromform )) {
\mod_vpl\event\variation_added::log( array (
'objectid' => $vid,
'context' => $vpl->get_context()
) );
\mod_vpl\event\variation_added::log( $vpl, $vid );
} else {
print_error( VPL_VARIATIONS . ' record not inserted' . $fromform->varid . ' ' . $vplid, VPL, $href );
}
redirect( $href, get_string('saved', VPL) );
} else { // Update record.
if ($DB->get_record( VPL_VARIATIONS, array (
'id' => $fromform->varid,
......@@ -158,21 +155,24 @@ if ($mform->is_cancelled()) {
) )) { // Check consistence.
$fromform->vpl = $vplid;
$fromform->id = $fromform->varid;
$fieldname = 'description' . $fromform->varid;
$fromform->description = $fromform->{$fieldname}['text'];
vpl_truncate_variations( $fromform );
$DB->update_record( VPL_VARIATIONS, $fromform );
\mod_vpl\event\variation_updated::log( array (
'objectid' => $fromform->varid,
'context' => $vpl->get_context()
) );
\mod_vpl\event\variation_updated::log( $vpl, $fromform->varid );
redirect( $href, get_string('updated', '', $fromform->identification) );
} else {
$vpl->print_header( get_string( 'variations', VPL ) );
$vpl->print_heading_with_help( 'variations' );
print_error( VPL_VARIATIONS . ' record inconsistence', VPL, $href );
print_error( VPL_VARIATIONS . ' record inconsistence ' . $id . ' ' . $vplid, VPL, $href );
}
}
}
vpl_inmediate_redirect( $href );
}
global $PAGE;
$PAGE->force_settings_menu();
$vpl->print_header( get_string( 'variations', VPL ) );
$vpl->print_heading_with_help( 'variations' );
// Display page.
if (isset( $oform )) {
$oform->display();
......@@ -189,10 +189,12 @@ foreach ($list as $variation) {
$aform = new mod_vpl_variation_form( $href, $number, $variation->id );
$variation->varid = $variation->id;
$variation->id = $id;
$fieldname = 'description' . $variation->varid;
$variation->$fieldname = array('text' => $variation->description);
$aform->set_data( $variation );
$aform->display();
$number ++;
}
$lastform = new mod_vpl_variation_form( $href, - 1 );
$lastform = new mod_vpl_variation_form( $href );
$lastform->display();
$vpl->print_footer();
......@@ -1895,15 +1895,19 @@ class mod_vpl {
if (! $DB->insert_record( VPL_ASSIGNED_VARIATIONS, $assign )) {
print_error( 'vpl variation not assigned' );
}
\mod_vpl\event\variation_assigned::log( $this, $variation->id, $userid);
} else {
if ($varassigned === false || $varassigned->vpl != $this->instance->id) { // Test consistency.
// TODO repair inconsistence?
print_error( 'vpl assigned variation inconsistency' );
}
$variation = $DB->get_record( VPL_VARIATIONS,
array (
'id' => $varassigned->variation
) );
if ($variation == false || $variation->vpl != $varassigned->vpl) { // Checks consistency.
$DB->delete_records( VPL_ASSIGNED_VARIATIONS,
array (
'id' => $varassigned->id
) );
print_error( 'vpl assigned variation inconsistency. Fixed removing the current assigment.' );
}
}
return $variation;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment