2009-05-27 04:06:19 +00:00
< ? php
// This file is part of Moodle - http://moodle.org/
//
// 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.
//
// 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2009-05-27 04:06:19 +00:00
/**
2014-02-14 16:59:20 +13:00
* @ package mod_data
2009-05-27 04:06:19 +00:00
* @ copyright 1999 onwards Martin Dougiamas { @ link http :// moodle . com }
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
*/
2022-07-07 20:59:42 +02:00
use mod_data\manager ;
2022-07-07 17:03:07 +02:00
use mod_data\preset ;
2022-07-07 20:59:42 +02:00
2016-07-15 17:01:43 -04:00
defined ( 'MOODLE_INTERNAL' ) || die ();
2008-05-31 00:30:00 +00:00
// Some constants
2006-03-22 08:07:26 +00:00
define ( 'DATA_MAX_ENTRIES' , 50 );
define ( 'DATA_PERPAGE_SINGLE' , 1 );
2008-06-12 14:13:51 +00:00
2008-04-16 11:51:50 +00:00
define ( 'DATA_FIRSTNAME' , - 1 );
define ( 'DATA_LASTNAME' , - 2 );
2008-04-19 20:48:48 +00:00
define ( 'DATA_APPROVED' , - 3 );
2008-04-21 14:17:02 +00:00
define ( 'DATA_TIMEADDED' , 0 );
define ( 'DATA_TIMEMODIFIED' , - 4 );
2017-07-31 08:45:11 +01:00
define ( 'DATA_TAGS' , - 5 );
2008-04-16 11:51:50 +00:00
2008-06-12 15:18:13 +00:00
define ( 'DATA_CAP_EXPORT' , 'mod/data:viewalluserpresets' );
2021-03-30 09:53:19 +08:00
// Users having assigned the default role "Non-editing teacher" can export database records
// Using the mod/data capability "viewalluserpresets" existing in Moodle 1.9.x.
// In Moodle >= 2, new roles may be introduced and used instead.
2010-08-25 01:22:15 +00:00
define ( 'DATA_PRESET_COMPONENT' , 'mod_data' );
define ( 'DATA_PRESET_FILEAREA' , 'site_presets' );
define ( 'DATA_PRESET_CONTEXT' , SYSCONTEXTID );
2017-03-02 13:45:22 +08:00
define ( 'DATA_EVENT_TYPE_OPEN' , 'open' );
define ( 'DATA_EVENT_TYPE_CLOSE' , 'close' );
2021-03-30 09:53:19 +08:00
require_once ( __DIR__ . '/deprecatedlib.php' );
2008-06-12 15:18:13 +00:00
2009-05-27 04:06:19 +00:00
/**
2014-02-14 16:59:20 +13:00
* @ package mod_data
2009-05-27 04:06:19 +00:00
* @ copyright 1999 onwards Martin Dougiamas { @ link http :// moodle . com }
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
*/
2008-05-31 09:51:48 +00:00
class data_field_base { // Base class for Database Field Types (see field/*/field.class.php)
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2009-05-27 04:06:19 +00:00
/** @var string Subclasses must override the type with their name */
var $type = 'unknown' ;
/** @var object The database object that this field belongs to */
var $data = NULL ;
/** @var object The field object itself, if we know it */
2009-08-20 13:16:08 +00:00
var $field = NULL ;
2009-05-27 04:06:19 +00:00
/** @var int Width of the icon for this fieldtype */
var $iconwidth = 16 ;
/** @var int Width of the icon for this fieldtype */
var $iconheight = 16 ;
/** @var object course module or cmifno */
var $cm ;
/** @var object activity context */
var $context ;
2016-08-19 13:58:19 +05:30
/** @var priority for globalsearch indexing */
protected static $priority = self :: NO_PRIORITY ;
/** priority value for invalid fields regarding indexing */
const NO_PRIORITY = 0 ;
/** priority value for minimum priority */
const MIN_PRIORITY = 1 ;
/** priority value for low priority */
const LOW_PRIORITY = 2 ;
/** priority value for high priority */
const HIGH_PRIORITY = 3 ;
/** priority value for maximum priority */
const MAX_PRIORITY = 4 ;
2009-05-27 04:06:19 +00:00
2022-08-05 14:38:01 +02:00
/** @var bool whether the field is used in preview mode. */
protected $preview = false ;
2009-05-27 04:06:19 +00:00
/**
* Constructor function
*
* @ global object
* @ uses CONTEXT_MODULE
* @ param int $field
* @ param int $data
* @ param int $cm
*/
2008-09-14 08:22:44 +00:00
function __construct ( $field = 0 , $data = 0 , $cm = 0 ) { // Field or data or both, each can be id or object
2008-06-06 08:04:22 +00:00
global $DB ;
2006-03-22 08:07:26 +00:00
if ( empty ( $field ) && empty ( $data )) {
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'missingfield' , 'data' );
2006-03-22 08:07:26 +00:00
}
2008-06-12 14:13:51 +00:00
2006-03-22 08:07:26 +00:00
if ( ! empty ( $field )) {
if ( is_object ( $field )) {
$this -> field = $field ; // Programmer knows what they are doing, we hope
2008-06-06 08:04:22 +00:00
} else if ( ! $this -> field = $DB -> get_record ( 'data_fields' , array ( 'id' => $field ))) {
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'invalidfieldid' , 'data' );
2006-03-22 08:07:26 +00:00
}
if ( empty ( $data )) {
2008-06-06 08:04:22 +00:00
if ( ! $this -> data = $DB -> get_record ( 'data' , array ( 'id' => $this -> field -> dataid ))) {
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'invalidid' , 'data' );
2006-03-22 08:07:26 +00:00
}
}
}
2008-06-12 14:13:51 +00:00
2006-03-22 08:07:26 +00:00
if ( empty ( $this -> data )) { // We need to define this properly
if ( ! empty ( $data )) {
if ( is_object ( $data )) {
$this -> data = $data ; // Programmer knows what they are doing, we hope
2008-06-06 08:04:22 +00:00
} else if ( ! $this -> data = $DB -> get_record ( 'data' , array ( 'id' => $data ))) {
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'invalidid' , 'data' );
2006-03-22 08:07:26 +00:00
}
} else { // No way to define it!
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'missingdata' , 'data' );
2006-03-22 08:07:26 +00:00
}
}
2008-06-12 14:13:51 +00:00
2008-09-14 08:22:44 +00:00
if ( $cm ) {
$this -> cm = $cm ;
} else {
$this -> cm = get_coursemodule_from_instance ( 'data' , $this -> data -> id );
}
2006-03-22 08:07:26 +00:00
if ( empty ( $this -> field )) { // We need to define some default values
$this -> define_default_field ();
}
2008-09-14 08:22:44 +00:00
2012-07-26 13:57:56 +08:00
$this -> context = context_module :: instance ( $this -> cm -> id );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-03-22 08:07:26 +00:00
2022-07-07 20:59:42 +02:00
/**
* Return the field type name .
*
* @ return string the filed type .
*/
public function get_name () : string {
return $this -> field -> name ;
}
2022-08-05 14:38:01 +02:00
/**
* Return if the field type supports preview .
*
* Fields without a preview cannot be displayed in the preset preview .
*
* @ return bool if the plugin supports preview .
*/
public function supports_preview () : bool {
return false ;
}
/**
* Generate a fake data_content for this field to be used in preset previews .
*
* Data plugins must override this method and support_preview in order to enable
* preset preview for this field .
*
* @ param int $recordid the fake record id
* @ return stdClass the fake record
*/
public function get_data_content_preview ( int $recordid ) : stdClass {
$message = get_string ( 'nopreviewavailable' , 'mod_data' , $this -> field -> name );
return ( object )[
'id' => 0 ,
'fieldid' => $this -> field -> id ,
'recordid' => $recordid ,
'content' => " <span class= \" nopreview \" > $message </span> " ,
'content1' => null ,
'content2' => null ,
'content3' => null ,
'content4' => null ,
];
}
/**
* Set the field to preview mode .
*
* @ param bool $preview the new preview value
*/
public function set_preview ( bool $preview ) {
$this -> preview = $preview ;
}
/**
* Get the field preview value .
*
* @ return bool
*/
public function get_preview () : bool {
return $this -> preview ;
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* This field just sets up a default field object
*
* @ return bool
*/
2006-03-22 08:07:26 +00:00
function define_default_field () {
2009-08-18 05:13:51 +00:00
global $OUTPUT ;
2006-03-22 08:07:26 +00:00
if ( empty ( $this -> data -> id )) {
2009-08-18 05:13:51 +00:00
echo $OUTPUT -> notification ( 'Programmer error: dataid not defined in field class' );
2006-03-22 08:07:26 +00:00
}
2010-09-21 08:37:36 +00:00
$this -> field = new stdClass ();
2006-03-22 08:07:26 +00:00
$this -> field -> id = 0 ;
$this -> field -> dataid = $this -> data -> id ;
$this -> field -> type = $this -> type ;
$this -> field -> param1 = '' ;
$this -> field -> param2 = '' ;
$this -> field -> param3 = '' ;
$this -> field -> name = '' ;
$this -> field -> description = '' ;
2012-07-11 13:01:30 +08:00
$this -> field -> required = false ;
2008-06-12 14:13:51 +00:00
2006-03-22 08:07:26 +00:00
return true ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-03-22 08:07:26 +00:00
2009-05-27 04:06:19 +00:00
/**
* Set up the field object according to data in an object . Now is the time to clean it !
*
* @ return bool
*/
2006-03-22 08:07:26 +00:00
function define_field ( $data ) {
$this -> field -> type = $this -> type ;
$this -> field -> dataid = $this -> data -> id ;
$this -> field -> name = trim ( $data -> name );
$this -> field -> description = trim ( $data -> description );
2015-02-17 13:30:45 +08:00
$this -> field -> required = ! empty ( $data -> required ) ? 1 : 0 ;
2006-03-22 08:07:26 +00:00
if ( isset ( $data -> param1 )) {
$this -> field -> param1 = trim ( $data -> param1 );
}
if ( isset ( $data -> param2 )) {
2006-03-23 03:48:58 +00:00
$this -> field -> param2 = trim ( $data -> param2 );
2006-03-22 08:07:26 +00:00
}
if ( isset ( $data -> param3 )) {
$this -> field -> param3 = trim ( $data -> param3 );
}
if ( isset ( $data -> param4 )) {
$this -> field -> param4 = trim ( $data -> param4 );
}
if ( isset ( $data -> param5 )) {
$this -> field -> param5 = trim ( $data -> param5 );
}
return true ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* Insert a new field in the database
* We assume the field object is already defined as $this -> field
*
* @ global object
* @ return bool
*/
2006-03-22 08:07:26 +00:00
function insert_field () {
2009-08-18 05:13:51 +00:00
global $DB , $OUTPUT ;
2008-06-06 08:04:22 +00:00
2006-03-22 08:07:26 +00:00
if ( empty ( $this -> field )) {
2009-08-18 05:13:51 +00:00
echo $OUTPUT -> notification ( 'Programmer error: Field has not been defined yet! See define_field()' );
2006-03-22 08:07:26 +00:00
return false ;
}
2009-06-13 18:16:08 +00:00
$this -> field -> id = $DB -> insert_record ( 'data_fields' , $this -> field );
2014-02-09 16:09:33 -08:00
// Trigger an event for creating this field.
$event = \mod_data\event\field_created :: create ( array (
'objectid' => $this -> field -> id ,
'context' => $this -> context ,
'other' => array (
'fieldname' => $this -> field -> name ,
'dataid' => $this -> data -> id
)
));
$event -> trigger ();
2006-03-22 08:07:26 +00:00
return true ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-03-22 08:07:26 +00:00
2009-05-27 04:06:19 +00:00
/**
* Update a field in the database
*
* @ global object
* @ return bool
*/
2006-03-22 08:07:26 +00:00
function update_field () {
2008-06-06 08:04:22 +00:00
global $DB ;
2009-06-13 17:35:10 +00:00
$DB -> update_record ( 'data_fields' , $this -> field );
2014-02-09 16:35:59 -08:00
// Trigger an event for updating this field.
$event = \mod_data\event\field_updated :: create ( array (
'objectid' => $this -> field -> id ,
'context' => $this -> context ,
'other' => array (
'fieldname' => $this -> field -> name ,
'dataid' => $this -> data -> id
)
));
$event -> trigger ();
2006-03-22 08:07:26 +00:00
return true ;
}
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2009-05-27 04:06:19 +00:00
/**
* Delete a field completely
*
* @ global object
* @ return bool
*/
2006-03-22 08:07:26 +00:00
function delete_field () {
2008-06-06 08:04:22 +00:00
global $DB ;
2006-03-22 08:07:26 +00:00
if ( ! empty ( $this -> field -> id )) {
2014-02-09 15:04:17 -08:00
// Get the field before we delete it.
$field = $DB -> get_record ( 'data_fields' , array ( 'id' => $this -> field -> id ));
2006-03-22 08:07:26 +00:00
$this -> delete_content ();
2008-09-14 08:22:44 +00:00
$DB -> delete_records ( 'data_fields' , array ( 'id' => $this -> field -> id ));
2014-02-09 15:04:17 -08:00
// Trigger an event for deleting this field.
$event = \mod_data\event\field_deleted :: create ( array (
'objectid' => $this -> field -> id ,
'context' => $this -> context ,
'other' => array (
'fieldname' => $this -> field -> name ,
'dataid' => $this -> data -> id
)
));
$event -> add_record_snapshot ( 'data_fields' , $field );
$event -> trigger ();
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2014-02-09 15:04:17 -08:00
2006-03-22 08:07:26 +00:00
return true ;
}
2009-05-27 04:06:19 +00:00
/**
* Print the relevant form element in the ADD template for this field
*
* @ global object
* @ param int $recordid
* @ return string
*/
2015-02-17 13:30:45 +08:00
function display_add_field ( $recordid = 0 , $formdata = null ) {
global $DB , $OUTPUT ;
2008-06-06 08:04:22 +00:00
2012-07-11 13:01:30 +08:00
if ( $formdata ) {
$fieldname = 'field_' . $this -> field -> id ;
$content = $formdata -> $fieldname ;
2015-02-17 13:30:45 +08:00
} else if ( $recordid ) {
2008-06-06 08:04:22 +00:00
$content = $DB -> get_field ( 'data_content' , 'content' , array ( 'fieldid' => $this -> field -> id , 'recordid' => $recordid ));
2006-03-22 08:07:26 +00:00
} else {
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
$content = '' ;
}
2006-11-09 19:44:20 +00:00
2009-03-24 11:12:33 +00:00
// beware get_field returns false for new, empty records MDL-18567
if ( $content === false ) {
$content = '' ;
}
2015-02-17 13:30:45 +08:00
$str = '<div title="' . s ( $this -> field -> description ) . '">' ;
$str .= '<label for="field_' . $this -> field -> id . '"><span class="accesshide">' . $this -> field -> name . '</span>' ;
2012-07-11 13:01:30 +08:00
if ( $this -> field -> required ) {
2017-01-19 16:20:27 +08:00
$image = $OUTPUT -> pix_icon ( 'req' , get_string ( 'requiredelement' , 'form' ));
2015-05-06 16:51:12 +08:00
$str .= html_writer :: div ( $image , 'inline-req' );
2012-07-11 13:01:30 +08:00
}
2016-11-04 11:30:11 +08:00
$str .= '</label><input class="basefieldinput form-control d-inline mod-data-input" ' .
'type="text" name="field_' . $this -> field -> id . '" ' .
'id="field_' . $this -> field -> id . '" value="' . s ( $content ) . '" />' ;
2006-02-09 04:44:56 +00:00
$str .= '</div>' ;
2006-03-22 08:07:26 +00:00
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
return $str ;
}
2009-05-27 04:06:19 +00:00
/**
* Print the relevant form element to define the attributes for this field
* viewable by teachers only .
*
* @ global object
* @ global object
* @ return void Output is echo ' d
*/
2006-03-22 08:07:26 +00:00
function display_edit_field () {
2009-08-06 08:21:01 +00:00
global $CFG , $DB , $OUTPUT ;
2006-03-22 08:07:26 +00:00
if ( empty ( $this -> field )) { // No field has been defined yet, try and make one
$this -> define_default_field ();
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2022-09-05 17:18:44 +02:00
// Throw an exception if field type doen't exist. Anyway user should never access to edit a field with an unknown fieldtype.
if ( $this -> type === 'unknown' ) {
throw new \moodle_exception ( get_string ( 'missingfieldtype' , 'data' , ( object )[ 'name' => $this -> field -> name ]));
}
2009-08-18 05:13:51 +00:00
echo $OUTPUT -> box_start ( 'generalbox boxaligncenter boxwidthwide' );
2006-03-22 08:07:26 +00:00
2007-01-04 21:32:36 +00:00
echo '<form id="editfield" action="' . $CFG -> wwwroot . '/mod/data/field.php" method="post">' . " \n " ;
2006-03-22 08:07:26 +00:00
echo '<input type="hidden" name="d" value="' . $this -> data -> id . '" />' . " \n " ;
if ( empty ( $this -> field -> id )) {
echo '<input type="hidden" name="mode" value="add" />' . " \n " ;
} else {
echo '<input type="hidden" name="fid" value="' . $this -> field -> id . '" />' . " \n " ;
echo '<input type="hidden" name="mode" value="update" />' . " \n " ;
}
echo '<input type="hidden" name="type" value="' . $this -> type . '" />' . " \n " ;
echo '<input name="sesskey" value="' . sesskey () . '" type="hidden" />' . " \n " ;
2006-11-09 19:44:20 +00:00
2013-08-22 12:03:23 +07:00
echo $OUTPUT -> heading ( $this -> name (), 3 );
2006-03-22 08:07:26 +00:00
2022-09-05 17:18:44 +02:00
$filepath = $CFG -> dirroot . '/mod/data/field/' . $this -> type . '/mod.html' ;
if ( ! file_exists ( $filepath )) {
throw new \moodle_exception ( get_string ( 'missingfieldtype' , 'data' , ( object )[ 'name' => $this -> field -> name ]));
} else {
require_once ( $filepath );
}
2006-03-22 08:07:26 +00:00
2022-10-19 12:14:52 +02:00
$actionbuttons = html_writer :: start_div ();
$actionbuttons .= html_writer :: tag ( 'input' , null , [
'type' => 'submit' ,
'name' => 'cancel' ,
'value' => get_string ( 'cancel' ),
2022-12-27 16:06:18 +01:00
'class' => 'btn btn-secondary mx-1'
2022-10-19 12:14:52 +02:00
]);
$actionbuttons .= html_writer :: tag ( 'input' , null , [
'type' => 'submit' ,
'value' => get_string ( 'save' ),
2022-12-27 16:06:18 +01:00
'class' => 'btn btn-primary mx-1'
2022-10-19 12:14:52 +02:00
]);
$actionbuttons .= html_writer :: end_div ();
$stickyfooter = new core\output\sticky_footer ( $actionbuttons );
echo $OUTPUT -> render ( $stickyfooter );
2006-03-22 08:07:26 +00:00
echo '</form>' ;
2009-08-18 05:13:51 +00:00
echo $OUTPUT -> box_end ();
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-11-09 19:44:20 +00:00
2022-12-28 19:14:30 +01:00
/**
* Validates params of fieldinput data . Overwrite to validate fieldtype specific data .
*
* You are expected to return an array like [ 'paramname' => 'Error message for paramname param' ] if there is an error ,
* return an empty array if everything is fine .
*
* @ param stdClass $fieldinput The field input data to check
* @ return array $errors if empty validation was fine , otherwise contains one or more error messages
*/
public function validate ( stdClass $fieldinput ) : array {
return [];
}
2022-08-05 14:38:01 +02:00
/**
* Return the data_content of the field , or generate it if it is in preview mode .
*
* @ param int $recordid the record id
* @ return stdClass | bool the record data or false if none
*/
protected function get_data_content ( int $recordid ) {
global $DB ;
if ( $this -> preview ) {
return $this -> get_data_content_preview ( $recordid );
}
return $DB -> get_record (
'data_content' ,
[ 'fieldid' => $this -> field -> id , 'recordid' => $recordid ]
);
}
2009-05-27 04:06:19 +00:00
/**
* Display the content of the field in browse mode
*
* @ global object
* @ param int $recordid
* @ param object $template
* @ return bool | string
*/
2006-03-22 08:07:26 +00:00
function display_browse_field ( $recordid , $template ) {
2008-06-06 08:04:22 +00:00
global $DB ;
2022-08-05 14:38:01 +02:00
$content = $this -> get_data_content ( $recordid );
if ( ! $content || ! isset ( $content -> content )) {
return '' ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2022-08-05 14:38:01 +02:00
$options = new stdClass ();
if ( $this -> field -> param1 == '1' ) {
// We are autolinking this field, so disable linking within us.
$options -> filter = false ;
}
$options -> para = false ;
$str = format_text ( $content -> content , $content -> content1 , $options );
return $str ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* Update the content of one data field in the data_content table
* @ global object
* @ param int $recordid
* @ param mixed $value
* @ param string $name
* @ return bool
*/
2006-03-22 08:07:26 +00:00
function update_content ( $recordid , $value , $name = '' ){
2008-06-06 08:04:22 +00:00
global $DB ;
2010-09-21 08:37:36 +00:00
$content = new stdClass ();
2006-03-22 08:07:26 +00:00
$content -> fieldid = $this -> field -> id ;
2006-02-02 06:28:17 +00:00
$content -> recordid = $recordid ;
$content -> content = clean_param ( $value , PARAM_NOTAGS );
2006-03-22 08:07:26 +00:00
2008-06-06 08:04:22 +00:00
if ( $oldcontent = $DB -> get_record ( 'data_content' , array ( 'fieldid' => $this -> field -> id , 'recordid' => $recordid ))) {
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
$content -> id = $oldcontent -> id ;
2008-06-06 08:04:22 +00:00
return $DB -> update_record ( 'data_content' , $content );
2006-03-22 08:07:26 +00:00
} else {
2008-06-06 08:04:22 +00:00
return $DB -> insert_record ( 'data_content' , $content );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-03-22 08:07:26 +00:00
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* Delete all content associated with the field
*
* @ global object
* @ param int $recordid
* @ return bool
*/
2006-03-22 08:07:26 +00:00
function delete_content ( $recordid = 0 ) {
2008-06-06 08:04:22 +00:00
global $DB ;
2006-03-22 08:07:26 +00:00
if ( $recordid ) {
2008-09-14 08:22:44 +00:00
$conditions = array ( 'fieldid' => $this -> field -> id , 'recordid' => $recordid );
2006-03-22 08:07:26 +00:00
} else {
2008-09-14 08:22:44 +00:00
$conditions = array ( 'fieldid' => $this -> field -> id );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-03-22 08:07:26 +00:00
2011-01-07 11:12:50 +01:00
$rs = $DB -> get_recordset ( 'data_content' , $conditions );
if ( $rs -> valid ()) {
2008-09-14 08:22:44 +00:00
$fs = get_file_storage ();
foreach ( $rs as $content ) {
2010-07-03 13:37:13 +00:00
$fs -> delete_area_files ( $this -> context -> id , 'mod_data' , 'content' , $content -> id );
2008-09-14 08:22:44 +00:00
}
2006-03-22 08:07:26 +00:00
}
2011-01-07 11:12:50 +01:00
$rs -> close ();
2006-03-22 08:07:26 +00:00
2008-09-14 08:22:44 +00:00
return $DB -> delete_records ( 'data_content' , $conditions );
2006-03-22 08:07:26 +00:00
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* Check if a field from an add form is empty
*
* @ param mixed $value
* @ param mixed $name
* @ return bool
*/
2006-02-06 09:13:37 +00:00
function notemptyfield ( $value , $name ) {
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
return ! empty ( $value );
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* Just in case a field needs to print something before the whole form
*/
2006-03-22 08:07:26 +00:00
function print_before_form () {
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-03-22 08:07:26 +00:00
2009-05-27 04:06:19 +00:00
/**
* Just in case a field needs to print something after the whole form
*/
2006-01-31 03:58:42 +00:00
function print_after_form () {
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* Returns the sortable field for the content . By default , it ' s just content
* but for some plugins , it could be content 1 - content4
*
* @ return string
*/
2006-02-03 08:11:36 +00:00
function get_sort_field () {
return 'content' ;
}
2006-03-22 08:07:26 +00:00
2009-05-27 04:06:19 +00:00
/**
* Returns the SQL needed to refer to the column . Some fields may need to CAST () etc .
*
* @ param string $fieldname
* @ return string $fieldname
*/
2006-04-05 01:38:06 +00:00
function get_sort_sql ( $fieldname ) {
return $fieldname ;
}
2009-05-27 04:06:19 +00:00
/**
* Returns the name / type of the field
*
* @ return string
*/
2008-05-31 00:30:00 +00:00
function name () {
MDL-52230 mod_data: Take fieldname from field:name()
AMOS BEGIN
CPY [namedate, mod_data],[fieldtypelable, datafield_date]
CPY [namefile, mod_data],[fieldtypelable, datafield_file]
CPY [namecheckbox, mod_data],[fieldtypelable, datafield_checkbox]
CPY [namelatlong, mod_data],[fieldtypelable, datafield_latlong]
CPY [namemenu, mod_data],[fieldtypelable, datafield_menu]
CPY [namemultimenu, mod_data],[fieldtypelable, datafield_multimenu]
CPY [namenumber, mod_data],[fieldtypelable, datafield_number]
CPY [namepicture, mod_data],[fieldtypelable, datafield_picture]
CPY [nameradiobutton, mod_data],[fieldtypelable, datafield_radiobutton]
CPY [nametext, mod_data],[fieldtypelable, datafield_text]
CPY [nametextarea, mod_data],[fieldtypelable, datafield_textarea]
CPY [nameurl, mod_data],[fieldtypelable, datafield_url]
AMOS END
2015-11-19 13:49:06 +00:00
return get_string ( 'fieldtypelabel' , " datafield_ $this->type " );
2006-03-22 08:07:26 +00:00
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* Prints the respective type icon
*
* @ global object
* @ return string
*/
2006-03-22 08:07:26 +00:00
function image () {
2009-07-02 12:25:19 +00:00
global $OUTPUT ;
2006-03-22 08:07:26 +00:00
2022-10-05 17:03:23 +02:00
return $OUTPUT -> pix_icon ( 'field/' . $this -> type , $this -> type , 'data' );
2006-03-22 08:07:26 +00:00
}
2009-05-27 04:06:19 +00:00
/**
* Per default , it is assumed that fields support text exporting .
* Override this ( return false ) on fields not supporting text exporting .
*
* @ return bool true
*/
2008-05-31 00:30:00 +00:00
function text_export_supported () {
return true ;
}
2006-11-09 19:44:20 +00:00
2009-05-27 04:06:19 +00:00
/**
* Per default , return the record ' s text value only from the " content " field .
* Override this in fields class if necesarry .
*
* @ param string $record
* @ return string
*/
2008-05-31 00:30:00 +00:00
function export_text_value ( $record ) {
if ( $this -> text_export_supported ()) {
return $record -> content ;
}
}
2006-03-22 08:07:26 +00:00
2009-05-27 04:06:19 +00:00
/**
* @ param string $relativepath
* @ return bool false
*/
2008-09-14 08:22:44 +00:00
function file_ok ( $relativepath ) {
return false ;
}
2016-08-19 13:58:19 +05:30
/**
* Returns the priority for being indexed by globalsearch
*
* @ return int
*/
public static function get_priority () {
return static :: $priority ;
}
/**
* Returns the presentable string value for a field content .
2016-08-25 16:50:15 +08:00
*
* The returned string should be plain text .
*
* @ param stdClass $content
2016-08-19 13:58:19 +05:30
* @ return string
*/
2016-08-25 16:50:15 +08:00
public static function get_content_value ( $content ) {
return trim ( $content -> content , " \r \n " );
2016-08-19 13:58:19 +05:30
}
2017-02-13 12:38:56 +01:00
/**
* Return the plugin configs for external functions ,
* in some cases the configs will need formatting or be returned only if the current user has some capabilities enabled .
*
* @ return array the list of config parameters
* @ since Moodle 3.3
*/
public function get_config_for_external () {
// Return all the field configs to null (maybe there is a private key for a service or something similar there).
$configs = [];
for ( $i = 1 ; $i <= 10 ; $i ++ ) {
$configs [ " param $i " ] = null ;
}
return $configs ;
}
2008-05-31 00:30:00 +00:00
}
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2009-05-27 04:06:19 +00:00
/**
* Given a template and a dataid , generate a default case template
*
2022-07-08 18:07:38 +02:00
* @ param stdClass $data the mod_data record .
* @ param string $template the template name
* @ param int $recordid the entry record
* @ param bool $form print a form instead of data
* @ param bool $update if the function update the $data object or not
2022-08-09 16:04:44 +02:00
* @ return string the template content or an empty string if no content is available ( for instance , when database has no fields ) .
2009-05-27 04:06:19 +00:00
*/
2022-07-08 18:07:38 +02:00
function data_generate_default_template ( & $data , $template , $recordid = 0 , $form = false , $update = true ) {
2008-06-06 08:04:22 +00:00
global $DB ;
2006-03-23 10:01:21 +00:00
2022-08-09 16:04:44 +02:00
if ( ! $data || ! $template ) {
return '' ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2022-07-19 18:49:06 +02:00
2022-08-09 16:04:44 +02:00
// These templates are empty by default (they have no content).
2022-08-26 16:46:14 +02:00
$emptytemplates = [
2022-07-19 18:49:06 +02:00
'csstemplate' ,
'jstemplate' ,
'listtemplateheader' ,
'listtemplatefooter' ,
'rsstitletemplate' ,
];
2022-08-26 16:46:14 +02:00
if ( in_array ( $template , $emptytemplates )) {
2006-06-02 04:29:11 +00:00
return '' ;
}
2006-11-09 19:44:20 +00:00
2022-08-26 16:46:14 +02:00
$manager = manager :: create_from_instance ( $data );
if ( empty ( $manager -> get_fields ())) {
// No template will be returned if there are no fields.
return '' ;
}
2013-07-22 16:19:08 +08:00
2022-08-26 16:46:14 +02:00
$templateclass = \mod_data\template :: create_default_template ( $manager , $template , $form );
$templatecontent = $templateclass -> get_template_content ();
2006-02-06 09:13:37 +00:00
2022-08-26 16:46:14 +02:00
if ( $update ) {
// Update the database instance.
$newdata = new stdClass ();
$newdata -> id = $data -> id ;
$newdata -> { $template } = $templatecontent ;
$DB -> update_record ( 'data' , $newdata );
$data -> { $template } = $templatecontent ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2022-08-09 16:04:44 +02:00
2022-08-26 16:46:14 +02:00
return $templatecontent ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2017-09-14 16:57:00 +08:00
/**
* Build the form elements to manage tags for a record .
*
2017-07-31 08:45:11 +01:00
* @ param int | bool $recordid
* @ param string [] $selected raw tag names
2017-09-14 16:57:00 +08:00
* @ return string
*/
2017-07-31 08:45:11 +01:00
function data_generate_tag_form ( $recordid = false , $selected = []) {
2017-10-10 18:01:27 +08:00
global $CFG , $DB , $OUTPUT , $PAGE ;
2017-09-14 16:57:00 +08:00
$tagtypestoshow = \core_tag_area :: get_showstandard ( 'mod_data' , 'data_records' );
$showstandard = ( $tagtypestoshow != core_tag_tag :: HIDE_STANDARD );
$typenewtags = ( $tagtypestoshow != core_tag_tag :: STANDARD_ONLY );
$str = html_writer :: start_tag ( 'div' , array ( 'class' => 'datatagcontrol' ));
$namefield = empty ( $CFG -> keeptagnamecase ) ? 'name' : 'rawname' ;
2017-07-31 08:45:11 +01:00
$tagcollid = \core_tag_area :: get_collection ( 'mod_data' , 'data_records' );
$tags = [];
$selectedtags = [];
2017-09-14 16:57:00 +08:00
if ( $showstandard ) {
2017-07-31 08:45:11 +01:00
$tags += $DB -> get_records_menu ( 'tag' , array ( 'isstandard' => 1 , 'tagcollid' => $tagcollid ),
$namefield , 'id,' . $namefield . ' as fieldname' );
}
2017-09-14 16:57:00 +08:00
2017-07-31 08:45:11 +01:00
if ( $recordid ) {
$selectedtags += core_tag_tag :: get_item_tags_array ( 'mod_data' , 'data_records' , $recordid );
}
if ( ! empty ( $selected )) {
list ( $sql , $params ) = $DB -> get_in_or_equal ( $selected , SQL_PARAMS_NAMED );
$params [ 'tagcollid' ] = $tagcollid ;
$sql = " SELECT id, $namefield FROM { tag} WHERE tagcollid = :tagcollid AND rawname $sql " ;
$selectedtags += $DB -> get_records_sql_menu ( $sql , $params );
2017-09-14 16:57:00 +08:00
}
2017-07-31 08:45:11 +01:00
$tags += $selectedtags ;
2017-09-14 16:57:00 +08:00
$str .= '<select class="custom-select" name="tags[]" id="tags" multiple>' ;
foreach ( $tags as $tagid => $tag ) {
2017-07-31 08:45:11 +01:00
$selected = key_exists ( $tagid , $selectedtags ) ? 'selected' : '' ;
2017-09-14 16:57:00 +08:00
$str .= " <option value=' $tag ' $selected > $tag </option> " ;
}
$str .= '</select>' ;
2017-10-10 18:01:27 +08:00
if ( has_capability ( 'moodle/tag:manage' , context_system :: instance ()) && $showstandard ) {
$url = new moodle_url ( '/tag/manage.php' , array ( 'tc' => core_tag_area :: get_collection ( 'mod_data' ,
'data_records' )));
$str .= ' ' . $OUTPUT -> action_link ( $url , get_string ( 'managestandardtags' , 'tag' ));
}
2017-09-14 16:57:00 +08:00
$PAGE -> requires -> js_call_amd ( 'core/form-autocomplete' , 'enhance' , $params = array (
'#tags' ,
$typenewtags ,
'' ,
get_string ( 'entertags' , 'tag' ),
false ,
$showstandard ,
get_string ( 'noselection' , 'form' )
)
);
$str .= html_writer :: end_tag ( 'div' );
return $str ;
}
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2009-05-27 04:06:19 +00:00
/**
2009-08-20 13:16:08 +00:00
* Search for a field name and replaces it with another one in all the
* form templates . Set $newfieldname as '' if you want to delete the
2009-05-27 04:06:19 +00:00
* field from the form .
*
* @ global object
* @ param object $data
* @ param string $searchfieldname
* @ param string $newfieldname
* @ return bool
*/
2006-03-22 08:07:26 +00:00
function data_replace_field_in_templates ( $data , $searchfieldname , $newfieldname ) {
2008-06-06 08:04:22 +00:00
global $DB ;
2006-02-06 09:13:37 +00:00
if ( ! empty ( $newfieldname )) {
$prestring = '[[' ;
$poststring = ']]' ;
2006-12-12 20:10:32 +00:00
$idpart = '#id' ;
2006-02-27 04:13:03 +00:00
} else {
2006-02-06 09:13:37 +00:00
$prestring = '' ;
$poststring = '' ;
2006-12-12 20:10:32 +00:00
$idpart = '' ;
2006-02-06 09:13:37 +00:00
}
2006-11-09 19:44:20 +00:00
2010-09-21 08:37:36 +00:00
$newdata = new stdClass ();
2006-02-27 04:13:03 +00:00
$newdata -> id = $data -> id ;
2008-06-06 08:04:22 +00:00
$newdata -> singletemplate = str_ireplace ( '[[' . $searchfieldname . ']]' ,
2022-11-17 16:04:24 +01:00
$prestring . $newfieldname . $poststring , $data -> singletemplate ? ? '' );
2006-11-09 19:44:20 +00:00
2008-06-06 08:04:22 +00:00
$newdata -> listtemplate = str_ireplace ( '[[' . $searchfieldname . ']]' ,
2022-11-17 16:04:24 +01:00
$prestring . $newfieldname . $poststring , $data -> listtemplate ? ? '' );
2006-11-09 19:44:20 +00:00
2008-06-06 08:04:22 +00:00
$newdata -> addtemplate = str_ireplace ( '[[' . $searchfieldname . ']]' ,
2022-11-17 16:04:24 +01:00
$prestring . $newfieldname . $poststring , $data -> addtemplate ? ? '' );
2006-11-09 19:44:20 +00:00
2008-06-06 08:04:22 +00:00
$newdata -> addtemplate = str_ireplace ( '[[' . $searchfieldname . '#id]]' ,
2022-11-17 16:04:24 +01:00
$prestring . $newfieldname . $idpart . $poststring , $data -> addtemplate ? ? '' );
2006-12-12 20:10:32 +00:00
2008-06-06 08:04:22 +00:00
$newdata -> rsstemplate = str_ireplace ( '[[' . $searchfieldname . ']]' ,
2022-11-17 16:04:24 +01:00
$prestring . $newfieldname . $poststring , $data -> rsstemplate ? ? '' );
2006-11-09 19:44:20 +00:00
2008-06-06 08:04:22 +00:00
return $DB -> update_record ( 'data' , $newdata );
2006-02-06 09:13:37 +00:00
}
2009-05-27 04:06:19 +00:00
/**
* Appends a new field at the end of the form template .
*
* @ global object
* @ param object $data
* @ param string $newfieldname
*/
2006-03-22 08:07:26 +00:00
function data_append_new_field_to_templates ( $data , $newfieldname ) {
2008-06-06 08:04:22 +00:00
global $DB ;
2006-03-22 08:07:26 +00:00
2010-09-21 08:37:36 +00:00
$newdata = new stdClass ();
2006-03-22 08:07:26 +00:00
$newdata -> id = $data -> id ;
2006-03-22 08:32:54 +00:00
$change = false ;
2006-03-22 08:07:26 +00:00
2006-02-06 09:13:37 +00:00
if ( ! empty ( $data -> singletemplate )) {
2008-06-06 08:04:22 +00:00
$newdata -> singletemplate = $data -> singletemplate . ' [[' . $newfieldname . ']]' ;
2006-03-22 08:32:54 +00:00
$change = true ;
2006-03-22 08:07:26 +00:00
}
if ( ! empty ( $data -> addtemplate )) {
2008-06-06 08:04:22 +00:00
$newdata -> addtemplate = $data -> addtemplate . ' [[' . $newfieldname . ']]' ;
2006-03-22 08:32:54 +00:00
$change = true ;
2006-02-06 09:13:37 +00:00
}
if ( ! empty ( $data -> rsstemplate )) {
2008-06-06 08:04:22 +00:00
$newdata -> rsstemplate = $data -> singletemplate . ' [[' . $newfieldname . ']]' ;
2006-03-22 08:32:54 +00:00
$change = true ;
}
if ( $change ) {
2008-06-06 08:04:22 +00:00
$DB -> update_record ( 'data' , $newdata );
2006-02-06 09:13:37 +00:00
}
}
2009-05-27 04:06:19 +00:00
/**
* given a field name
* this function creates an instance of the particular subfield class
*
* @ global object
* @ param string $name
* @ param object $data
* @ return object | bool
*/
2006-03-22 08:07:26 +00:00
function data_get_field_from_name ( $name , $data ){
2008-06-06 08:04:22 +00:00
global $DB ;
2009-09-28 16:08:17 +00:00
$field = $DB -> get_record ( 'data_fields' , array ( 'name' => $name , 'dataid' => $data -> id ));
2006-03-22 08:07:26 +00:00
if ( $field ) {
return data_get_field ( $field , $data );
} else {
return false ;
}
}
2009-05-27 04:06:19 +00:00
/**
* given a field id
* this function creates an instance of the particular subfield class
*
* @ global object
* @ param int $fieldid
* @ param object $data
* @ return bool | object
*/
2006-03-22 08:07:26 +00:00
function data_get_field_from_id ( $fieldid , $data ){
2008-06-06 08:04:22 +00:00
global $DB ;
2009-09-28 16:08:17 +00:00
$field = $DB -> get_record ( 'data_fields' , array ( 'id' => $fieldid , 'dataid' => $data -> id ));
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2006-03-22 08:07:26 +00:00
if ( $field ) {
return data_get_field ( $field , $data );
2006-02-06 05:31:52 +00:00
} else {
2006-02-06 05:36:34 +00:00
return false ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
}
2009-05-27 04:06:19 +00:00
/**
* given a field id
* this function creates an instance of the particular subfield class
*
* @ global object
* @ param string $type
* @ param object $data
* @ return object
*/
2006-03-22 08:07:26 +00:00
function data_get_field_new ( $type , $data ) {
global $CFG ;
2008-06-12 14:13:51 +00:00
2022-09-05 17:18:44 +02:00
$filepath = $CFG -> dirroot . '/mod/data/field/' . $type . '/field.class.php' ;
// It should never access this method if the subfield class doesn't exist.
if ( ! file_exists ( $filepath )) {
throw new \moodle_exception ( 'invalidfieldtype' , 'data' );
}
require_once ( $filepath );
2006-03-22 08:07:26 +00:00
$newfield = 'data_field_' . $type ;
$newfield = new $newfield ( 0 , $data );
return $newfield ;
}
2009-05-27 04:06:19 +00:00
/**
* returns a subclass field object given a record of the field , used to
* invoke plugin methods
* input : $param $field - record from db
*
* @ global object
2022-09-05 17:18:44 +02:00
* @ param stdClass $field the field record
* @ param stdClass $data the data instance
* @ param stdClass | null $cm optional course module data
* @ return data_field_base the field object instance or data_field_base if unkown type
2009-05-27 04:06:19 +00:00
*/
2008-09-14 08:22:44 +00:00
function data_get_field ( $field , $data , $cm = null ) {
2006-03-22 08:07:26 +00:00
global $CFG ;
2022-09-05 17:18:44 +02:00
if ( ! isset ( $field -> type )) {
return new data_field_base ( $field );
}
$filepath = $CFG -> dirroot . '/mod/data/field/' . $field -> type . '/field.class.php' ;
if ( ! file_exists ( $filepath )) {
return new data_field_base ( $field );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2022-09-05 17:18:44 +02:00
require_once ( $filepath );
$newfield = 'data_field_' . $field -> type ;
$newfield = new $newfield ( $field , $data , $cm );
return $newfield ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2008-09-14 08:22:44 +00:00
/**
* Given record object ( or id ), returns true if the record belongs to the current user
2009-05-27 04:06:19 +00:00
*
* @ global object
* @ global object
* @ param mixed $record record object or id
2008-09-14 08:22:44 +00:00
* @ return bool
*/
function data_isowner ( $record ) {
2008-06-06 08:04:22 +00:00
global $USER , $DB ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2010-03-31 07:41:31 +00:00
if ( ! isloggedin ()) { // perf shortcut
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
return false ;
}
2008-09-14 08:22:44 +00:00
if ( ! is_object ( $record )) {
if ( ! $record = $DB -> get_record ( 'data_records' , array ( 'id' => $record ))) {
return false ;
}
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2008-09-14 08:22:44 +00:00
return ( $record -> userid == $USER -> id );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2009-05-27 04:06:19 +00:00
/**
* has a user reached the max number of entries ?
*
* @ param object $data
* @ return bool
*/
2008-06-12 14:13:51 +00:00
function data_atmaxentries ( $data ){
if ( ! $data -> maxentries ){
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
return false ;
2008-06-12 14:13:51 +00:00
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
} else {
return ( data_numentries ( $data ) >= $data -> maxentries );
}
}
2009-05-27 04:06:19 +00:00
/**
* returns the number of entries already made by this user
2009-08-20 13:16:08 +00:00
*
2009-05-27 04:06:19 +00:00
* @ global object
* @ global object
2009-08-20 13:16:08 +00:00
* @ param object $data
2009-05-27 04:06:19 +00:00
* @ return int
*/
2017-03-19 16:41:30 +00:00
function data_numentries ( $data , $userid = null ) {
2008-09-14 08:22:44 +00:00
global $USER , $DB ;
2017-03-19 16:41:30 +00:00
if ( $userid === null ) {
$userid = $USER -> id ;
}
2008-06-06 08:04:22 +00:00
$sql = 'SELECT COUNT(*) FROM {data_records} WHERE dataid=? AND userid=?' ;
2017-03-19 16:41:30 +00:00
return $DB -> count_records_sql ( $sql , array ( $data -> id , $userid ));
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2009-05-27 04:06:19 +00:00
/**
2009-08-20 13:16:08 +00:00
* function that takes in a dataid and adds a record
2009-05-27 04:06:19 +00:00
* this is used everytime an add template is submitted
*
* @ global object
* @ global object
* @ param object $data
* @ param int $groupid
2019-08-26 15:18:52 +02:00
* @ param int $userid
2009-05-27 04:06:19 +00:00
* @ return bool
*/
2019-10-17 12:20:54 +08:00
function data_add_record ( $data , $groupid = 0 , $userid = null ) {
2008-06-06 08:04:22 +00:00
global $USER , $DB ;
2006-11-09 19:44:20 +00:00
2006-12-06 20:17:58 +00:00
$cm = get_coursemodule_from_instance ( 'data' , $data -> id );
2012-07-26 13:57:56 +08:00
$context = context_module :: instance ( $cm -> id );
2008-06-12 14:13:51 +00:00
2010-09-21 08:37:36 +00:00
$record = new stdClass ();
2019-08-26 15:18:52 +02:00
$record -> userid = $userid ? ? $USER -> id ;
2006-03-22 08:07:26 +00:00
$record -> dataid = $data -> id ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
$record -> groupid = $groupid ;
2006-03-22 08:07:26 +00:00
$record -> timecreated = $record -> timemodified = time ();
2006-08-14 05:55:40 +00:00
if ( has_capability ( 'mod/data:approve' , $context )) {
2006-02-06 05:24:02 +00:00
$record -> approved = 1 ;
} else {
$record -> approved = 0 ;
}
2014-02-10 20:37:37 -08:00
$record -> id = $DB -> insert_record ( 'data_records' , $record );
// Trigger an event for creating this record.
$event = \mod_data\event\record_created :: create ( array (
'objectid' => $record -> id ,
'context' => $context ,
'other' => array (
'dataid' => $data -> id
)
));
$event -> trigger ();
2017-03-19 16:41:30 +00:00
$course = get_course ( $cm -> course );
2017-04-03 10:44:32 +08:00
data_update_completion_state ( $data , $course , $cm );
2017-03-19 16:41:30 +00:00
2014-02-10 20:37:37 -08:00
return $record -> id ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2009-05-27 04:06:19 +00:00
/**
* check the multple existence any tag in a template
*
* check to see if there are 2 or more of the same tag being used .
*
* @ global object
* @ param int $dataid ,
2009-08-20 13:16:08 +00:00
* @ param string $template
2009-05-27 04:06:19 +00:00
* @ return bool
*/
2008-06-06 08:04:22 +00:00
function data_tags_check ( $dataid , $template ) {
2009-08-18 05:13:51 +00:00
global $DB , $OUTPUT ;
2008-06-06 08:04:22 +00:00
2008-05-31 00:30:00 +00:00
// first get all the possible tags
2008-06-06 08:04:22 +00:00
$fields = $DB -> get_records ( 'data_fields' , array ( 'dataid' => $dataid ));
2008-05-31 00:30:00 +00:00
// then we generate strings to replace
$tagsok = true ; // let's be optimistic
2008-06-12 14:13:51 +00:00
foreach ( $fields as $field ){
2015-05-06 16:29:48 +08:00
$pattern = " / \ [ \ [ " . preg_quote ( $field -> name , '/' ) . " \ ] \ ]/i " ;
2008-06-12 14:13:51 +00:00
if ( preg_match_all ( $pattern , $template , $dummy ) > 1 ){
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
$tagsok = false ;
2009-08-18 05:13:51 +00:00
echo $OUTPUT -> notification ( '[[' . $field -> name . ']] - ' . get_string ( 'multipletags' , 'data' ));
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
}
2008-05-31 00:30:00 +00:00
// else return true
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
return $tagsok ;
}
2009-05-27 04:06:19 +00:00
/**
* Adds an instance of a data
*
2012-04-15 14:07:21 +02:00
* @ param stdClass $data
* @ param mod_data_mod_form $mform
* @ return int intance id
2009-05-27 04:06:19 +00:00
*/
2012-04-15 14:07:21 +02:00
function data_add_instance ( $data , $mform = null ) {
2016-07-18 18:35:37 -04:00
global $DB , $CFG ;
require_once ( $CFG -> dirroot . '/mod/data/locallib.php' );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2006-09-24 13:58:54 +00:00
if ( empty ( $data -> assessed )) {
$data -> assessed = 0 ;
2006-02-08 04:58:21 +00:00
}
2015-09-08 11:47:44 +08:00
if ( empty ( $data -> ratingtime ) || empty ( $data -> assessed )) {
$data -> assesstimestart = 0 ;
$data -> assesstimefinish = 0 ;
}
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
$data -> timemodified = time ();
2009-06-13 18:16:08 +00:00
$data -> id = $DB -> insert_record ( 'data' , $data );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2016-07-18 18:35:37 -04:00
// Add calendar events if necessary.
data_set_events ( $data );
2017-07-03 12:04:58 +08:00
if ( ! empty ( $data -> completionexpected )) {
\core_completion\api :: update_completion_date_event ( $data -> coursemodule , 'data' , $data -> id , $data -> completionexpected );
}
2016-07-18 18:35:37 -04:00
2007-06-05 22:58:37 +00:00
data_grade_item_update ( $data );
2008-06-12 14:13:51 +00:00
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
return $data -> id ;
}
2009-05-27 04:06:19 +00:00
/**
* updates an instance of a data
*
* @ global object
* @ param object $data
* @ return bool
*/
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
function data_update_instance ( $data ) {
2016-07-18 18:35:37 -04:00
global $DB , $CFG ;
require_once ( $CFG -> dirroot . '/mod/data/locallib.php' );
2006-11-09 19:44:20 +00:00
2007-06-03 19:36:20 +00:00
$data -> timemodified = time ();
2022-10-06 14:03:55 +02:00
if ( ! empty ( $data -> instance )) {
$data -> id = $data -> instance ;
}
2006-11-09 19:44:20 +00:00
2006-09-24 13:58:54 +00:00
if ( empty ( $data -> assessed )) {
$data -> assessed = 0 ;
2006-02-08 04:56:44 +00:00
}
2008-06-12 14:13:51 +00:00
2010-04-23 09:44:19 +00:00
if ( empty ( $data -> ratingtime ) or empty ( $data -> assessed )) {
$data -> assesstimestart = 0 ;
$data -> assesstimefinish = 0 ;
}
2008-04-24 17:11:40 +00:00
if ( empty ( $data -> notification )) {
$data -> notification = 0 ;
}
2009-06-13 17:55:51 +00:00
$DB -> update_record ( 'data' , $data );
2007-06-03 19:36:20 +00:00
2016-07-18 18:35:37 -04:00
// Add calendar events if necessary.
data_set_events ( $data );
2017-07-03 12:04:58 +08:00
$completionexpected = ( ! empty ( $data -> completionexpected )) ? $data -> completionexpected : null ;
\core_completion\api :: update_completion_date_event ( $data -> coursemodule , 'data' , $data -> id , $completionexpected );
2016-07-18 18:35:37 -04:00
2007-06-03 19:36:20 +00:00
data_grade_item_update ( $data );
2008-06-12 14:13:51 +00:00
2007-06-03 19:36:20 +00:00
return true ;
2008-06-12 14:13:51 +00:00
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2009-05-27 04:06:19 +00:00
/**
* deletes an instance of a data
*
* @ global object
* @ param int $id
* @ return bool
*/
2008-05-31 00:30:00 +00:00
function data_delete_instance ( $id ) { // takes the dataid
2010-08-02 07:44:45 +00:00
global $DB , $CFG ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2008-09-14 08:22:44 +00:00
if ( ! $data = $DB -> get_record ( 'data' , array ( 'id' => $id ))) {
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
return false ;
}
2008-09-14 08:22:44 +00:00
$cm = get_coursemodule_from_instance ( 'data' , $data -> id );
2012-07-26 13:57:56 +08:00
$context = context_module :: instance ( $cm -> id );
2008-06-12 14:13:51 +00:00
2008-09-14 08:22:44 +00:00
/// Delete all the associated information
2006-11-09 19:44:20 +00:00
2008-09-14 08:22:44 +00:00
// files
$fs = get_file_storage ();
2010-07-03 13:37:13 +00:00
$fs -> delete_area_files ( $context -> id , 'mod_data' );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2008-09-14 08:22:44 +00:00
// get all the records in this data
$sql = " SELECT r.id
FROM { data_records } r
WHERE r . dataid = ? " ;
2006-11-09 19:44:20 +00:00
2008-09-14 08:22:44 +00:00
$DB -> delete_records_select ( 'data_content' , " recordid IN ( $sql ) " , array ( $id ));
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
// delete all the records and fields
2008-06-01 21:36:11 +00:00
$DB -> delete_records ( 'data_records' , array ( 'dataid' => $id ));
$DB -> delete_records ( 'data_fields' , array ( 'dataid' => $id ));
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
2016-07-18 18:35:37 -04:00
// Remove old calendar events.
$events = $DB -> get_records ( 'event' , array ( 'modulename' => 'data' , 'instance' => $id ));
foreach ( $events as $event ) {
$event = calendar_event :: load ( $event );
$event -> delete ();
}
2008-09-14 08:22:44 +00:00
// cleanup gradebook
2007-07-08 14:58:23 +00:00
data_grade_item_delete ( $data );
2008-06-12 14:13:51 +00:00
2019-02-27 10:35:42 +08:00
// Delete the instance itself
// We must delete the module record after we delete the grade item.
$result = $DB -> delete_records ( 'data' , array ( 'id' => $id ));
2007-06-03 19:36:20 +00:00
return $result ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2009-05-27 04:06:19 +00:00
/**
* returns a summary of data activity of this user
*
* @ global object
* @ param object $course
* @ param object $user
* @ param object $mod
* @ param object $data
* @ return object | null
*/
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
function data_user_outline ( $course , $user , $mod , $data ) {
2009-10-27 12:37:15 +00:00
global $DB , $CFG ;
require_once ( " $CFG->libdir /gradelib.php " );
$grades = grade_get_grades ( $course -> id , 'mod' , 'data' , $data -> id , $user -> id );
if ( empty ( $grades -> items [ 0 ] -> grades )) {
$grade = false ;
} else {
$grade = reset ( $grades -> items [ 0 ] -> grades );
}
2008-06-06 08:04:22 +00:00
if ( $countrecords = $DB -> count_records ( 'data_records' , array ( 'dataid' => $data -> id , 'userid' => $user -> id ))) {
2010-09-21 08:37:36 +00:00
$result = new stdClass ();
2006-04-07 07:02:53 +00:00
$result -> info = get_string ( 'numrecords' , 'data' , $countrecords );
2008-06-06 08:04:22 +00:00
$lastrecord = $DB -> get_record_sql ( ' SELECT id , timemodified FROM { data_records }
WHERE dataid = ? AND userid = ?
ORDER BY timemodified DESC ' , array ( $data -> id , $user -> id ), true );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
$result -> time = $lastrecord -> timemodified ;
2009-10-27 12:37:15 +00:00
if ( $grade ) {
2019-01-31 15:39:32 +10:30
if ( ! $grade -> hidden || has_capability ( 'moodle/grade:viewhidden' , context_course :: instance ( $course -> id ))) {
2021-03-16 12:03:39 +01:00
$result -> info .= ', ' . get_string ( 'gradenoun' ) . ': ' . $grade -> str_long_grade ;
2019-01-31 15:39:32 +10:30
} else {
2021-03-16 12:03:39 +01:00
$result -> info = get_string ( 'gradenoun' ) . ': ' . get_string ( 'hidden' , 'grades' );
2019-01-31 15:39:32 +10:30
}
2009-10-27 12:37:15 +00:00
}
return $result ;
} else if ( $grade ) {
2019-08-28 09:24:16 +08:00
$result = ( object ) [
'time' => grade_get_date_for_user_grade ( $grade , $user ),
];
2019-01-31 15:39:32 +10:30
if ( ! $grade -> hidden || has_capability ( 'moodle/grade:viewhidden' , context_course :: instance ( $course -> id ))) {
2021-03-16 12:03:39 +01:00
$result -> info = get_string ( 'gradenoun' ) . ': ' . $grade -> str_long_grade ;
2019-01-31 15:39:32 +10:30
} else {
2021-03-16 12:03:39 +01:00
$result -> info = get_string ( 'gradenoun' ) . ': ' . get_string ( 'hidden' , 'grades' );
2019-01-31 15:39:32 +10:30
}
2011-05-06 16:15:49 +08:00
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
return $result ;
}
return NULL ;
}
2009-05-27 04:06:19 +00:00
/**
* Prints all the records uploaded by this user
*
* @ global object
* @ param object $course
* @ param object $user
* @ param object $mod
* @ param object $data
*/
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
function data_user_complete ( $course , $user , $mod , $data ) {
2009-10-27 12:37:15 +00:00
global $DB , $CFG , $OUTPUT ;
require_once ( " $CFG->libdir /gradelib.php " );
$grades = grade_get_grades ( $course -> id , 'mod' , 'data' , $data -> id , $user -> id );
if ( ! empty ( $grades -> items [ 0 ] -> grades )) {
$grade = reset ( $grades -> items [ 0 ] -> grades );
2019-01-31 15:39:32 +10:30
if ( ! $grade -> hidden || has_capability ( 'moodle/grade:viewhidden' , context_course :: instance ( $course -> id ))) {
2021-03-16 12:03:39 +01:00
echo $OUTPUT -> container ( get_string ( 'gradenoun' ) . ': ' . $grade -> str_long_grade );
2019-01-31 15:39:32 +10:30
if ( $grade -> str_feedback ) {
echo $OUTPUT -> container ( get_string ( 'feedback' ) . ': ' . $grade -> str_feedback );
}
} else {
2021-03-16 12:03:39 +01:00
echo $OUTPUT -> container ( get_string ( 'gradenoun' ) . ': ' . get_string ( 'hidden' , 'grades' ));
2009-10-27 12:37:15 +00:00
}
}
2022-07-08 18:07:38 +02:00
$records = $DB -> get_records (
'data_records' ,
[ 'dataid' => $data -> id , 'userid' => $user -> id ],
'timemodified DESC'
);
if ( $records ) {
$manager = manager :: create_from_instance ( $data );
$parser = $manager -> get_template ( 'singletemplate' );
echo $parser -> parse_entries ( $records );
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
}
2007-06-03 19:36:20 +00:00
/**
* Return grade for given user or all users .
*
2009-05-27 04:06:19 +00:00
* @ global object
* @ param object $data
2007-06-03 19:36:20 +00:00
* @ param int $userid optional user id , 0 means all users
* @ return array array of grades , false if none
*/
2007-06-05 22:58:37 +00:00
function data_get_user_grades ( $data , $userid = 0 ) {
2010-04-23 09:44:19 +00:00
global $CFG ;
require_once ( $CFG -> dirroot . '/rating/lib.php' );
2007-06-03 19:36:20 +00:00
2011-05-11 13:32:36 +08:00
$ratingoptions = new stdClass ;
$ratingoptions -> component = 'mod_data' ;
$ratingoptions -> ratingarea = 'entry' ;
2010-04-23 09:44:19 +00:00
$ratingoptions -> modulename = 'data' ;
$ratingoptions -> moduleid = $data -> id ;
2007-06-03 19:36:20 +00:00
2010-04-23 09:44:19 +00:00
$ratingoptions -> userid = $userid ;
$ratingoptions -> aggregationmethod = $data -> assessed ;
$ratingoptions -> scaleid = $data -> scale ;
$ratingoptions -> itemtable = 'data_records' ;
$ratingoptions -> itemtableusercolumn = 'userid' ;
2007-06-03 19:36:20 +00:00
2011-05-11 13:32:36 +08:00
$rm = new rating_manager ();
2010-04-23 09:44:19 +00:00
return $rm -> get_user_grades ( $ratingoptions );
2007-06-03 19:36:20 +00:00
}
/**
2008-08-16 12:16:01 +00:00
* Update activity grades
2007-06-03 19:36:20 +00:00
*
2012-01-06 11:52:46 +07:00
* @ category grade
2008-08-16 12:16:01 +00:00
* @ param object $data
* @ param int $userid specific user only , 0 means all
2009-05-27 04:06:19 +00:00
* @ param bool $nullifnone
2007-06-03 19:36:20 +00:00
*/
2008-08-16 12:16:01 +00:00
function data_update_grades ( $data , $userid = 0 , $nullifnone = true ) {
2008-06-06 08:04:22 +00:00
global $CFG , $DB ;
require_once ( $CFG -> libdir . '/gradelib.php' );
2007-06-03 19:36:20 +00:00
2008-08-16 12:16:01 +00:00
if ( ! $data -> assessed ) {
data_grade_item_update ( $data );
2008-06-12 14:13:51 +00:00
2008-08-16 12:16:01 +00:00
} else if ( $grades = data_get_user_grades ( $data , $userid )) {
data_grade_item_update ( $data , $grades );
2008-06-12 14:13:51 +00:00
2008-08-16 12:16:01 +00:00
} else if ( $userid and $nullifnone ) {
2010-09-21 08:37:36 +00:00
$grade = new stdClass ();
2008-08-16 12:16:01 +00:00
$grade -> userid = $userid ;
$grade -> rawgrade = NULL ;
data_grade_item_update ( $data , $grade );
2008-06-12 14:13:51 +00:00
2007-06-03 19:36:20 +00:00
} else {
2008-08-16 12:16:01 +00:00
data_grade_item_update ( $data );
}
}
2007-06-03 19:36:20 +00:00
/**
2007-06-05 22:58:37 +00:00
* Update / create grade item for given data
2007-06-03 19:36:20 +00:00
*
2012-01-06 11:52:46 +07:00
* @ category grade
* @ param stdClass $data A database instance with extra cmidnumber property
* @ param mixed $grades Optional array / object of grade ( s ); 'reset' means reset grades in gradebook
2007-06-03 19:36:20 +00:00
* @ return object grade_item
*/
2007-11-29 14:43:04 +00:00
function data_grade_item_update ( $data , $grades = NULL ) {
2007-06-05 22:58:37 +00:00
global $CFG ;
2008-06-06 08:04:22 +00:00
require_once ( $CFG -> libdir . '/gradelib.php' );
2007-06-03 19:36:20 +00:00
2007-08-20 10:52:59 +00:00
$params = array ( 'itemname' => $data -> name , 'idnumber' => $data -> cmidnumber );
2008-06-12 14:13:51 +00:00
2007-06-03 19:36:20 +00:00
if ( ! $data -> assessed or $data -> scale == 0 ) {
2007-06-05 22:58:37 +00:00
$params [ 'gradetype' ] = GRADE_TYPE_NONE ;
2008-06-12 14:13:51 +00:00
2007-06-03 19:36:20 +00:00
} else if ( $data -> scale > 0 ) {
$params [ 'gradetype' ] = GRADE_TYPE_VALUE ;
$params [ 'grademax' ] = $data -> scale ;
$params [ 'grademin' ] = 0 ;
2008-06-12 14:13:51 +00:00
2007-06-03 19:36:20 +00:00
} else if ( $data -> scale < 0 ) {
$params [ 'gradetype' ] = GRADE_TYPE_SCALE ;
$params [ 'scaleid' ] = - $data -> scale ;
}
2008-06-12 14:13:51 +00:00
2007-11-29 14:43:04 +00:00
if ( $grades === 'reset' ) {
$params [ 'reset' ] = true ;
$grades = NULL ;
}
return grade_update ( 'mod/data' , $data -> course , 'mod' , 'data' , $data -> id , 0 , $grades , $params );
2007-06-03 19:36:20 +00:00
}
/**
* Delete grade item for given data
*
2012-01-06 11:52:46 +07:00
* @ category grade
2007-06-03 19:36:20 +00:00
* @ param object $data object
* @ return object grade_item
*/
function data_grade_item_delete ( $data ) {
2007-06-05 22:58:37 +00:00
global $CFG ;
require_once ( $CFG -> libdir . '/gradelib.php' );
2008-06-12 14:13:51 +00:00
2007-06-06 23:04:24 +00:00
return grade_update ( 'mod/data' , $data -> course , 'mod' , 'data' , $data -> id , 0 , NULL , array ( 'deleted' => 1 ));
2007-06-03 19:36:20 +00:00
}
2008-05-31 00:30:00 +00:00
// junk functions
2009-05-27 04:06:19 +00:00
/**
2009-08-20 13:16:08 +00:00
* takes a list of records , the current data , a search string ,
2009-05-27 04:06:19 +00:00
* and mode to display prints the translated template
*
2022-07-08 18:07:38 +02:00
* @ deprecated since Moodle 4.1 MDL - 75146 - please do not use this function any more .
* @ todo MDL - 75189 Final deprecation in Moodle 4.5 .
* @ param string $templatename the template name
* @ param array $records the entries records
* @ param stdClass $data the database instance object
* @ param string $search the current search term
* @ param int $page page number for pagination
* @ param bool $return if the result should be returned ( true ) or printed ( false )
* @ param moodle_url | null $jumpurl a moodle_url by which to jump back to the record list ( can be null )
* @ return mixed string with all parsed entries or nothing if $return is false
2009-05-27 04:06:19 +00:00
*/
2022-07-08 18:07:38 +02:00
function data_print_template ( $templatename , $records , $data , $search = '' , $page = 0 , $return = false , moodle_url $jumpurl = null ) {
debugging (
'data_print_template is deprecated. Use mod_data\\manager::get_template and mod_data\\template::parse_entries instead' ,
DEBUG_DEVELOPER
);
2008-06-12 14:13:51 +00:00
2022-07-08 18:07:38 +02:00
$options = [
'search' => $search ,
'page' => $page ,
];
if ( $jumpurl ) {
$options [ 'baseurl' ] = $jumpurl ;
2006-04-05 01:38:06 +00:00
}
2022-07-08 18:07:38 +02:00
$manager = manager :: create_from_instance ( $data );
$parser = $manager -> get_template ( $templatename , $options );
$content = $parser -> parse_entries ( $records );
if ( $return ) {
return $content ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2022-07-08 18:07:38 +02:00
echo $content ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2010-04-23 09:44:19 +00:00
/**
* Return rating related permissions
2011-05-11 13:32:36 +08:00
*
* @ param string $contextid the context id
* @ param string $component the component to get rating permissions for
* @ param string $ratingarea the rating area to get permissions for
2010-04-23 09:44:19 +00:00
* @ return array an associative array of the user ' s rating permissions
*/
2011-05-11 13:32:36 +08:00
function data_rating_permissions ( $contextid , $component , $ratingarea ) {
2012-08-21 14:20:30 +08:00
$context = context :: instance_by_id ( $contextid , MUST_EXIST );
2011-05-11 13:32:36 +08:00
if ( $component != 'mod_data' || $ratingarea != 'entry' ) {
2010-04-23 09:44:19 +00:00
return null ;
}
2011-05-11 13:32:36 +08:00
return array (
'view' => has_capability ( 'mod/data:viewrating' , $context ),
'viewany' => has_capability ( 'mod/data:viewanyrating' , $context ),
'viewall' => has_capability ( 'mod/data:viewallratings' , $context ),
'rate' => has_capability ( 'mod/data:rate' , $context )
);
2010-04-23 09:44:19 +00:00
}
2010-05-21 07:50:38 +00:00
/**
2011-04-01 17:20:33 +08:00
* Validates a submitted rating
* @ param array $params submitted data
* context => object the context in which the rated items exists [ required ]
* itemid => int the ID of the object being rated
* scaleid => int the scale from which the user can select a rating . Used for bounds checking . [ required ]
* rating => int the submitted rating
* rateduserid => int the id of the user whose items have been rated . NOT the user who submitted the ratings . 0 to update all . [ required ]
* aggregation => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [ required ]
2011-05-04 15:00:16 +08:00
* @ return boolean true if the rating is valid . Will throw rating_exception if not
2010-05-21 07:50:38 +00:00
*/
2011-05-04 15:00:16 +08:00
function data_rating_validate ( $params ) {
2011-04-01 17:20:33 +08:00
global $DB , $USER ;
2011-05-11 13:32:36 +08:00
// Check the component is mod_data
if ( $params [ 'component' ] != 'mod_data' ) {
throw new rating_exception ( 'invalidcomponent' );
2011-04-01 17:20:33 +08:00
}
2011-05-11 13:32:36 +08:00
// Check the ratingarea is entry (the only rating area in data module)
if ( $params [ 'ratingarea' ] != 'entry' ) {
throw new rating_exception ( 'invalidratingarea' );
}
// Check the rateduserid is not the current user .. you can't rate your own entries
if ( $params [ 'rateduserid' ] == $USER -> id ) {
throw new rating_exception ( 'nopermissiontorate' );
}
$datasql = " SELECT d.id as dataid, d.scale, d.course, r.userid as userid, d.approval, r.approved, r.timecreated, d.assesstimestart, d.assesstimefinish, r.groupid
2011-04-01 17:20:33 +08:00
FROM { data_records } r
JOIN { data } d ON r . dataid = d . id
WHERE r . id = : itemid " ;
$dataparams = array ( 'itemid' => $params [ 'itemid' ]);
if ( ! $info = $DB -> get_record_sql ( $datasql , $dataparams )) {
2011-05-04 15:00:16 +08:00
//item doesn't exist
throw new rating_exception ( 'invaliditemid' );
2011-04-01 17:20:33 +08:00
}
2011-05-09 16:34:58 +08:00
if ( $info -> scale != $params [ 'scaleid' ]) {
//the scale being submitted doesnt match the one in the database
throw new rating_exception ( 'invalidscaleid' );
}
//check that the submitted rating is valid for the scale
2011-05-25 12:00:16 +02:00
// lower limit
if ( $params [ 'rating' ] < 0 && $params [ 'rating' ] != RATING_UNSET_RATING ) {
2011-05-09 16:34:58 +08:00
throw new rating_exception ( 'invalidnum' );
2011-05-25 12:00:16 +02:00
}
// upper limit
if ( $info -> scale < 0 ) {
2011-05-09 16:34:58 +08:00
//its a custom scale
2011-05-11 13:32:36 +08:00
$scalerecord = $DB -> get_record ( 'scale' , array ( 'id' => - $info -> scale ));
2011-05-09 16:34:58 +08:00
if ( $scalerecord ) {
$scalearray = explode ( ',' , $scalerecord -> scale );
if ( $params [ 'rating' ] > count ( $scalearray )) {
throw new rating_exception ( 'invalidnum' );
}
} else {
throw new rating_exception ( 'invalidscaleid' );
}
} else if ( $params [ 'rating' ] > $info -> scale ) {
//if its numeric and submitted rating is above maximum
throw new rating_exception ( 'invalidnum' );
}
2011-04-01 17:20:33 +08:00
if ( $info -> approval && ! $info -> approved ) {
//database requires approval but this item isnt approved
2011-05-04 15:00:16 +08:00
throw new rating_exception ( 'nopermissiontorate' );
2011-04-01 17:20:33 +08:00
}
2011-05-11 13:32:36 +08:00
// check the item we're rating was created in the assessable time window
2011-04-01 17:20:33 +08:00
if ( ! empty ( $info -> assesstimestart ) && ! empty ( $info -> assesstimefinish )) {
if ( $info -> timecreated < $info -> assesstimestart || $info -> timecreated > $info -> assesstimefinish ) {
2011-05-04 15:00:16 +08:00
throw new rating_exception ( 'notavailable' );
2011-04-01 17:20:33 +08:00
}
}
2013-08-21 13:42:30 +08:00
$course = $DB -> get_record ( 'course' , array ( 'id' => $info -> course ), '*' , MUST_EXIST );
2011-05-11 13:32:36 +08:00
$cm = get_coursemodule_from_instance ( 'data' , $info -> dataid , $course -> id , false , MUST_EXIST );
2012-07-26 13:57:56 +08:00
$context = context_module :: instance ( $cm -> id );
2011-04-01 17:20:33 +08:00
2011-05-11 13:32:36 +08:00
// if the supplied context doesnt match the item's context
if ( $context -> id != $params [ 'context' ] -> id ) {
2011-05-04 15:00:16 +08:00
throw new rating_exception ( 'invalidcontext' );
2011-04-01 17:20:33 +08:00
}
// Make sure groups allow this user to see the item they're rating
2011-05-11 13:32:36 +08:00
$groupid = $info -> groupid ;
2011-04-01 17:20:33 +08:00
if ( $groupid > 0 and $groupmode = groups_get_activity_groupmode ( $cm , $course )) { // Groups are being used
if ( ! groups_group_exists ( $groupid )) { // Can't find group
2011-05-04 15:00:16 +08:00
throw new rating_exception ( 'cannotfindgroup' ); //something is wrong
2011-04-01 17:20:33 +08:00
}
if ( ! groups_is_member ( $groupid ) and ! has_capability ( 'moodle/site:accessallgroups' , $context )) {
// do not allow rating of posts from other groups when in SEPARATEGROUPS or VISIBLEGROUPS
2011-05-04 15:00:16 +08:00
throw new rating_exception ( 'notmemberofgroup' );
2011-04-01 17:20:33 +08:00
}
}
return true ;
2010-05-21 07:50:38 +00:00
}
2015-06-08 14:33:26 +05:30
/**
* Can the current user see ratings for a given itemid ?
*
* @ param array $params submitted data
* contextid => int contextid [ required ]
* component => The component for this module - should always be mod_data [ required ]
* ratingarea => object the context in which the rated items exists [ required ]
* itemid => int the ID of the object being rated [ required ]
* scaleid => int scale id [ optional ]
* @ return bool
* @ throws coding_exception
* @ throws rating_exception
*/
function mod_data_rating_can_see_item_ratings ( $params ) {
global $DB ;
// Check the component is mod_data.
if ( ! isset ( $params [ 'component' ]) || $params [ 'component' ] != 'mod_data' ) {
throw new rating_exception ( 'invalidcomponent' );
}
// Check the ratingarea is entry (the only rating area in data).
if ( ! isset ( $params [ 'ratingarea' ]) || $params [ 'ratingarea' ] != 'entry' ) {
throw new rating_exception ( 'invalidratingarea' );
}
if ( ! isset ( $params [ 'itemid' ])) {
throw new rating_exception ( 'invaliditemid' );
}
$datasql = " SELECT d.id as dataid, d.course, r.groupid
FROM { data_records } r
JOIN { data } d ON r . dataid = d . id
WHERE r . id = : itemid " ;
$dataparams = array ( 'itemid' => $params [ 'itemid' ]);
if ( ! $info = $DB -> get_record_sql ( $datasql , $dataparams )) {
// Item doesn't exist.
throw new rating_exception ( 'invaliditemid' );
}
2015-11-02 14:47:15 +08:00
// User can see ratings of all participants.
if ( $info -> groupid == 0 ) {
return true ;
}
2015-06-08 14:33:26 +05:30
$course = $DB -> get_record ( 'course' , array ( 'id' => $info -> course ), '*' , MUST_EXIST );
$cm = get_coursemodule_from_instance ( 'data' , $info -> dataid , $course -> id , false , MUST_EXIST );
// Make sure groups allow this user to see the item they're rating.
return groups_group_visible ( $info -> groupid , $course , $cm );
}
2006-02-13 07:57:54 +00:00
2009-05-27 04:06:19 +00:00
/**
2009-08-20 13:16:08 +00:00
* function that takes in the current data , number of items per page ,
* a search string and prints a preference box in view . php
*
* This preference box prints a searchable advanced search template if
* a ) A template is defined
* b ) The advanced search checkbox is checked .
2009-05-27 04:06:19 +00:00
*
* @ global object
* @ global object
2009-08-20 13:16:08 +00:00
* @ param object $data
* @ param int $perpage
2009-05-27 04:06:19 +00:00
* @ param string $search
* @ param string $sort
* @ param string $order
* @ param array $search_array
* @ param int $advanced
* @ param string $mode
* @ return void
*/
2007-02-26 06:56:05 +00:00
function data_print_preference_form ( $data , $perpage , $search , $sort = '' , $order = 'ASC' , $search_array = '' , $advanced = 0 , $mode = '' ){
2009-08-17 15:19:49 +00:00
global $CFG , $DB , $PAGE , $OUTPUT ;
2008-09-14 08:22:44 +00:00
2007-02-26 06:56:05 +00:00
$cm = get_coursemodule_from_instance ( 'data' , $data -> id );
2012-07-26 13:57:56 +08:00
$context = context_module :: instance ( $cm -> id );
2022-10-05 17:03:23 +02:00
echo '<div class="datapreferences my-5">' ;
2007-01-04 21:32:36 +00:00
echo '<form id="options" action="view.php" method="get">' ;
2022-10-05 17:03:23 +02:00
echo '<div class="d-flex">' ;
2007-02-26 06:56:05 +00:00
echo '<div>' ;
2006-03-26 05:03:10 +00:00
echo '<input type="hidden" name="d" value="' . $data -> id . '" />' ;
2007-02-26 06:56:05 +00:00
if ( $mode == 'asearch' ) {
$advanced = 1 ;
echo '<input type="hidden" name="mode" value="list" />' ;
}
2006-12-13 09:22:47 +00:00
echo '<label for="pref_perpage">' . get_string ( 'pagesize' , 'data' ) . '</label> ' ;
2006-12-13 09:38:46 +00:00
$pagesizes = array ( 2 => 2 , 3 => 3 , 4 => 4 , 5 => 5 , 6 => 6 , 7 => 7 , 8 => 8 , 9 => 9 , 10 => 10 , 15 => 15 ,
2006-02-06 05:24:02 +00:00
20 => 20 , 30 => 30 , 40 => 40 , 50 => 50 , 100 => 100 , 200 => 200 , 300 => 300 , 400 => 400 , 500 => 500 , 1000 => 1000 );
2016-11-04 11:30:11 +08:00
echo html_writer :: select ( $pagesizes , 'perpage' , $perpage , false , array ( 'id' => 'pref_perpage' , 'class' => 'custom-select' ));
2012-08-20 17:16:15 +08:00
2007-02-26 06:56:05 +00:00
if ( $advanced ) {
2012-08-20 17:16:15 +08:00
$regsearchclass = 'search_none' ;
$advancedsearchclass = 'search_inline' ;
} else {
$regsearchclass = 'search_inline' ;
$advancedsearchclass = 'search_none' ;
2007-02-26 06:56:05 +00:00
}
2016-11-03 17:03:03 +08:00
echo '<div id="reg_search" class="' . $regsearchclass . ' form-inline" > ' ;
2016-11-04 11:30:11 +08:00
echo '<label for="pref_search">' . get_string ( 'search' ) . '</label> <input type="text" ' .
'class="form-control" size="16" name="search" id= "pref_search" value="' . s ( $search ) . '" /></div>' ;
2006-12-13 09:22:47 +00:00
echo ' <label for="pref_sortby">' . get_string ( 'sortby' ) . '</label> ' ;
2008-05-31 00:30:00 +00:00
// foreach field, print the option
2019-03-29 11:39:23 +08:00
echo '<select name="sort" id="pref_sortby" class="custom-select mr-1">' ;
2008-06-06 08:04:22 +00:00
if ( $fields = $DB -> get_records ( 'data_fields' , array ( 'dataid' => $data -> id ), 'name' )) {
2008-04-21 14:17:02 +00:00
echo '<optgroup label="' . get_string ( 'fields' , 'data' ) . '">' ;
foreach ( $fields as $field ) {
if ( $field -> id == $sort ) {
echo '<option value="' . $field -> id . '" selected="selected">' . $field -> name . '</option>' ;
} else {
echo '<option value="' . $field -> id . '">' . $field -> name . '</option>' ;
}
}
echo '</optgroup>' ;
2008-04-16 11:51:50 +00:00
}
2008-04-21 14:17:02 +00:00
$options = array ();
$options [ DATA_TIMEADDED ] = get_string ( 'timeadded' , 'data' );
$options [ DATA_TIMEMODIFIED ] = get_string ( 'timemodified' , 'data' );
$options [ DATA_FIRSTNAME ] = get_string ( 'authorfirstname' , 'data' );
$options [ DATA_LASTNAME ] = get_string ( 'authorlastname' , 'data' );
2008-04-19 20:48:48 +00:00
if ( $data -> approval and has_capability ( 'mod/data:approve' , $context )) {
$options [ DATA_APPROVED ] = get_string ( 'approved' , 'data' );
}
2008-04-21 14:17:02 +00:00
echo '<optgroup label="' . get_string ( 'other' , 'data' ) . '">' ;
2008-04-16 11:51:50 +00:00
foreach ( $options as $key => $name ) {
if ( $key == $sort ) {
echo '<option value="' . $key . '" selected="selected">' . $name . '</option>' ;
2006-02-03 08:11:36 +00:00
} else {
2008-04-16 11:51:50 +00:00
echo '<option value="' . $key . '">' . $name . '</option>' ;
2006-02-03 08:11:36 +00:00
}
}
2008-04-21 14:17:02 +00:00
echo '</optgroup>' ;
2006-02-03 08:11:36 +00:00
echo '</select>' ;
2006-12-13 09:22:47 +00:00
echo '<label for="pref_order" class="accesshide">' . get_string ( 'order' ) . '</label>' ;
2019-03-29 11:39:23 +08:00
echo '<select id="pref_order" name="order" class="custom-select mr-1">' ;
2006-03-26 15:54:41 +00:00
if ( $order == 'ASC' ) {
2006-02-20 02:15:45 +00:00
echo '<option value="ASC" selected="selected">' . get_string ( 'ascending' , 'data' ) . '</option>' ;
2006-02-03 08:11:36 +00:00
} else {
echo '<option value="ASC">' . get_string ( 'ascending' , 'data' ) . '</option>' ;
}
2006-03-26 15:54:41 +00:00
if ( $order == 'DESC' ) {
2006-02-20 02:15:45 +00:00
echo '<option value="DESC" selected="selected">' . get_string ( 'descending' , 'data' ) . '</option>' ;
2006-02-03 08:11:36 +00:00
} else {
echo '<option value="DESC">' . get_string ( 'descending' , 'data' ) . '</option>' ;
}
2006-02-20 02:15:45 +00:00
echo '</select>' ;
2007-02-28 06:23:25 +00:00
2007-02-26 06:56:05 +00:00
if ( $advanced ) {
$checked = ' checked="checked" ' ;
}
else {
$checked = '' ;
}
2010-01-18 20:57:32 +00:00
$PAGE -> requires -> js ( '/mod/data/data.js' );
2008-04-16 13:54:01 +00:00
echo ' <input type="hidden" name="advanced" value="0" />' ;
2008-10-24 20:17:10 +00:00
echo ' <input type="hidden" name="filter" value="1" />' ;
2016-11-04 11:30:11 +08:00
echo ' <input type="checkbox" id="advancedcheckbox" name="advanced" value="1" ' . $checked . ' ' .
2019-03-29 11:39:23 +08:00
'onchange="showHideAdvSearch(this.checked);" class="mx-1" />' .
2016-11-04 11:30:11 +08:00
'<label for="advancedcheckbox">' . get_string ( 'advancedsearch' , 'data' ) . '</label>' ;
2022-10-05 17:03:23 +02:00
echo '</div>' ;
echo '<div id="advsearch-save-sec" class="ml-auto ' . $regsearchclass . '">' ;
2016-11-04 11:30:11 +08:00
echo ' <input type="submit" class="btn btn-secondary" value="' . get_string ( 'savesettings' , 'data' ) . '" />' ;
2022-10-05 17:03:23 +02:00
echo '</div>' ;
echo '</div>' ;
echo '<div>' ;
2008-09-14 08:22:44 +00:00
2007-02-26 06:56:05 +00:00
echo '<br />' ;
2012-08-20 17:16:15 +08:00
echo '<div class="' . $advancedsearchclass . '" id="data_adv_form">' ;
2007-02-26 06:56:05 +00:00
echo '<table class="boxaligncenter">' ;
2008-09-14 08:22:44 +00:00
2007-02-26 06:56:05 +00:00
// print ASC or DESC
echo '<tr><td colspan="2"> </td></tr>' ;
$i = 0 ;
// Determine if we are printing all fields for advanced search, or the template for advanced search
// If a template is not defined, use the deafault template and display all fields.
2022-08-29 19:23:30 +02:00
$asearchtemplate = $data -> asearchtemplate ;
if ( empty ( $asearchtemplate )) {
$asearchtemplate = data_generate_default_template ( $data , 'asearchtemplate' , 0 , false , false );
2007-02-26 06:56:05 +00:00
}
2016-05-10 17:09:37 +08:00
static $fields = array ();
static $dataid = null ;
2008-06-12 14:13:51 +00:00
2007-02-26 06:56:05 +00:00
if ( empty ( $dataid )) {
$dataid = $data -> id ;
} else if ( $dataid != $data -> id ) {
2016-05-10 17:09:37 +08:00
$fields = array ();
2007-02-26 06:56:05 +00:00
}
if ( empty ( $fields )) {
2008-06-06 08:04:22 +00:00
$fieldrecords = $DB -> get_records ( 'data_fields' , array ( 'dataid' => $data -> id ));
2007-02-26 06:56:05 +00:00
foreach ( $fieldrecords as $fieldrecord ) {
$fields [] = data_get_field ( $fieldrecord , $data );
}
}
2008-05-31 00:30:00 +00:00
// Replacing tags
2007-02-26 06:56:05 +00:00
$patterns = array ();
$replacement = array ();
2008-05-31 00:30:00 +00:00
// Then we generate strings to replace for normal tags
2007-02-26 06:56:05 +00:00
foreach ( $fields as $field ) {
2008-05-24 11:09:42 +00:00
$fieldname = $field -> field -> name ;
$fieldname = preg_quote ( $fieldname , '/' );
$patterns [] = " / \ [ \ [ $fieldname\ ] \ ]/i " ;
2008-04-16 11:51:50 +00:00
$searchfield = data_get_field_from_id ( $field -> field -> id , $data );
2022-09-05 17:18:44 +02:00
if ( $searchfield -> type === 'unknown' ) {
continue ;
}
2007-02-26 06:56:05 +00:00
if ( ! empty ( $search_array [ $field -> field -> id ] -> data )) {
$replacement [] = $searchfield -> display_search_field ( $search_array [ $field -> field -> id ] -> data );
} else {
$replacement [] = $searchfield -> display_search_field ();
}
}
2008-04-16 11:51:50 +00:00
$fn = ! empty ( $search_array [ DATA_FIRSTNAME ] -> data ) ? $search_array [ DATA_FIRSTNAME ] -> data : '' ;
$ln = ! empty ( $search_array [ DATA_LASTNAME ] -> data ) ? $search_array [ DATA_LASTNAME ] -> data : '' ;
$patterns [] = '/##firstname##/' ;
2016-11-04 11:30:11 +08:00
$replacement [] = '<label class="accesshide" for="u_fn">' . get_string ( 'authorfirstname' , 'data' ) . '</label>' .
'<input type="text" class="form-control" size="16" id="u_fn" name="u_fn" value="' . s ( $fn ) . '" />' ;
2008-04-16 11:51:50 +00:00
$patterns [] = '/##lastname##/' ;
2016-11-04 11:30:11 +08:00
$replacement [] = '<label class="accesshide" for="u_ln">' . get_string ( 'authorlastname' , 'data' ) . '</label>' .
'<input type="text" class="form-control" size="16" id="u_ln" name="u_ln" value="' . s ( $ln ) . '" />' ;
2008-04-16 11:51:50 +00:00
2017-07-31 08:45:11 +01:00
if ( core_tag_tag :: is_enabled ( 'mod_data' , 'data_records' )) {
$patterns [] = " /##tags##/ " ;
$selectedtags = isset ( $search_array [ DATA_TAGS ] -> rawtagnames ) ? $search_array [ DATA_TAGS ] -> rawtagnames : [];
$replacement [] = data_generate_tag_form ( false , $selectedtags );
}
2008-05-31 09:51:48 +00:00
// actual replacement of the tags
2008-06-12 14:13:51 +00:00
2010-09-21 08:37:36 +00:00
$options = new stdClass ();
2007-02-26 06:56:05 +00:00
$options -> para = false ;
$options -> noclean = true ;
echo '<tr><td>' ;
2022-08-29 19:23:30 +02:00
echo preg_replace ( $patterns , $replacement , format_text ( $asearchtemplate , FORMAT_HTML , $options ));
2007-02-26 06:56:05 +00:00
echo '</td></tr>' ;
2008-06-12 14:13:51 +00:00
2016-11-04 11:30:11 +08:00
echo '<tr><td colspan="4"><br/>' .
2019-03-29 11:39:23 +08:00
'<input type="submit" class="btn btn-primary mr-1" value="' . get_string ( 'savesettings' , 'data' ) . '" />' .
2016-11-04 11:30:11 +08:00
'<input type="submit" class="btn btn-secondary" name="resetadv" value="' . get_string ( 'resetsettings' , 'data' ) . '" />' .
'</td></tr>' ;
2007-02-26 06:56:05 +00:00
echo '</table>' ;
2006-03-26 15:54:41 +00:00
echo '</div>' ;
2007-02-26 06:56:05 +00:00
echo '</form>' ;
2008-05-31 00:30:00 +00:00
echo '</div>' ;
2022-10-05 17:03:23 +02:00
echo '<hr/>' ;
NEW MODULE FROM MOODLE.COM - DATABASE !
Finally, we have an early version good enough for everyone to
start banging on to help us polish it up and find bugs.
Please take a look and file bugs in the bug tracker under "Database module".
We urgently need
- new icons for existing field types
- testing on PostgreSQL install
Coming soon (?):
- Many more field types: calculation, checkbox, relation, date, datetime,
time, email, group, list, user, number, richtext
(Please let us know if you are interested in developing any of these)
- A way to save and restore "presets", which are field/template sets
- Backup/Restore support
- Groups Support
- RSS support
Many thanks to Yu for all the hard work under my whip.
2005-12-02 07:50:26 +00:00
}
2006-01-31 03:58:42 +00:00
2009-05-27 04:06:19 +00:00
/**
* @ global object
* @ global object
* @ param object $data
* @ param object $record
2022-07-08 18:07:38 +02:00
* @ param bool $print if the result must be printed or returner .
2009-05-27 04:06:19 +00:00
* @ return void Output echo ' d
*/
2022-07-08 18:07:38 +02:00
function data_print_ratings ( $data , $record , bool $print = true ) {
2009-08-20 13:16:08 +00:00
global $OUTPUT ;
2022-07-08 18:07:38 +02:00
$result = '' ;
2011-05-11 13:32:36 +08:00
if ( ! empty ( $record -> rating )){
2022-07-08 18:07:38 +02:00
$result = $OUTPUT -> render ( $record -> rating );
}
if ( ! $print ) {
return $result ;
2006-02-14 03:17:21 +00:00
}
2022-07-08 18:07:38 +02:00
echo $result ;
2006-02-14 03:17:21 +00:00
}
2009-05-27 04:06:19 +00:00
/**
2014-04-03 13:42:25 +08:00
* List the actions that correspond to a view of this module .
* This is used by the participation report .
*
* Note : This is not used by new logging system . Event with
* crud = 'r' and edulevel = LEVEL_PARTICIPATING will
* be considered as view action .
2009-05-27 04:06:19 +00:00
*
* @ return array
*/
2006-03-09 06:26:42 +00:00
function data_get_view_actions () {
return array ( 'view' );
}
2009-05-27 04:06:19 +00:00
/**
2014-04-03 13:42:25 +08:00
* List the actions that correspond to a post of this module .
* This is used by the participation report .
*
* Note : This is not used by new logging system . Event with
* crud = ( 'c' || 'u' || 'd' ) and edulevel = LEVEL_PARTICIPATING
* will be considered as post action .
*
2009-05-27 04:06:19 +00:00
* @ return array
*/
2006-03-09 06:26:42 +00:00
function data_get_post_actions () {
return array ( 'add' , 'update' , 'record delete' );
}
2009-05-27 04:06:19 +00:00
/**
* @ param string $name
* @ param int $dataid
* @ param int $fieldid
* @ return bool
*/
2010-09-21 21:57:03 +00:00
function data_fieldname_exists ( $name , $dataid , $fieldid = 0 ) {
global $DB ;
2006-03-22 08:07:26 +00:00
2010-09-21 21:57:03 +00:00
if ( ! is_numeric ( $name )) {
$like = $DB -> sql_like ( 'df.name' , ':name' , false );
2010-09-16 02:56:26 +00:00
} else {
2010-09-21 21:57:03 +00:00
$like = " df.name = :name " ;
2010-09-16 02:56:26 +00:00
}
2010-09-21 21:57:03 +00:00
$params = array ( 'name' => $name );
2006-11-09 19:44:20 +00:00
if ( $fieldid ) {
2010-09-21 21:57:03 +00:00
$params [ 'dataid' ] = $dataid ;
$params [ 'fieldid1' ] = $fieldid ;
$params [ 'fieldid2' ] = $fieldid ;
2008-06-06 08:04:22 +00:00
return $DB -> record_exists_sql ( " SELECT * FROM { data_fields} df
2010-09-21 21:57:03 +00:00
WHERE $like AND df . dataid = : dataid
AND (( df . id < : fieldid1 ) OR ( df . id > : fieldid2 )) " , $params );
2006-03-22 08:07:26 +00:00
} else {
2010-09-21 21:57:03 +00:00
$params [ 'dataid' ] = $dataid ;
2008-06-06 08:04:22 +00:00
return $DB -> record_exists_sql ( " SELECT * FROM { data_fields} df
2010-09-21 21:57:03 +00:00
WHERE $like AND df . dataid = : dataid " , $params );
2006-03-22 08:07:26 +00:00
}
}
2009-05-27 04:06:19 +00:00
/**
* @ param array $fieldinput
*/
2006-03-22 08:07:26 +00:00
function data_convert_arrays_to_strings ( & $fieldinput ) {
foreach ( $fieldinput as $key => $val ) {
if ( is_array ( $val )) {
$str = '' ;
foreach ( $val as $inner ) {
$str .= $inner . ',' ;
}
$str = substr ( $str , 0 , - 1 );
2008-06-12 14:13:51 +00:00
2006-03-22 08:07:26 +00:00
$fieldinput -> $key = $str ;
}
}
}
2006-08-15 08:42:06 +00:00
2006-09-20 16:57:01 +00:00
/**
* Converts a database ( module instance ) to use the Roles System
2009-05-27 04:06:19 +00:00
*
* @ global object
* @ global object
* @ uses CONTEXT_MODULE
* @ uses CAP_PREVENT
* @ uses CAP_ALLOW
* @ param object $data a data object with the same attributes as a record
* from the data database table
* @ param int $datamodid the id of the data module , from the modules table
2010-03-31 07:41:31 +00:00
* @ param array $teacherroles array of roles that have archetype teacher
* @ param array $studentroles array of roles that have archetype student
* @ param array $guestroles array of roles that have archetype guest
2009-05-27 04:06:19 +00:00
* @ param int $cmid the course_module id for this data instance
* @ return boolean data module was converted or not
2006-09-20 16:57:01 +00:00
*/
function data_convert_to_roles ( $data , $teacherroles = array (), $studentroles = array (), $cmid = NULL ) {
2009-08-18 05:13:51 +00:00
global $CFG , $DB , $OUTPUT ;
2006-11-09 19:44:20 +00:00
2006-09-20 16:57:01 +00:00
if ( ! isset ( $data -> participants ) && ! isset ( $data -> assesspublic )
&& ! isset ( $data -> groupmode )) {
// We assume that this database has already been converted to use the
// Roles System. above fields get dropped the data module has been
// upgraded to use Roles.
return false ;
}
2008-06-12 14:13:51 +00:00
2006-09-20 16:57:01 +00:00
if ( empty ( $cmid )) {
// We were not given the course_module id. Try to find it.
2006-09-20 17:46:20 +00:00
if ( ! $cm = get_coursemodule_from_instance ( 'data' , $data -> id )) {
2009-08-18 05:13:51 +00:00
echo $OUTPUT -> notification ( 'Could not get the course module for the data' );
2006-09-20 16:57:01 +00:00
return false ;
} else {
$cmid = $cm -> id ;
}
}
2012-07-26 13:57:56 +08:00
$context = context_module :: instance ( $cmid );
2006-11-09 19:44:20 +00:00
2008-06-12 14:13:51 +00:00
2006-09-20 16:57:01 +00:00
// $data->participants:
// 1 - Only teachers can add entries
// 3 - Teachers and students can add entries
switch ( $data -> participants ) {
case 1 :
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'mod/data:writeentry' , CAP_PREVENT , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'mod/data:writeentry' , CAP_ALLOW , $teacherrole -> id , $context -> id );
}
break ;
case 3 :
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'mod/data:writeentry' , CAP_ALLOW , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'mod/data:writeentry' , CAP_ALLOW , $teacherrole -> id , $context -> id );
}
break ;
}
2006-11-09 19:44:20 +00:00
2006-09-20 16:57:01 +00:00
// $data->assessed:
// 2 - Only teachers can rate posts
// 1 - Everyone can rate posts
// 0 - No one can rate posts
switch ( $data -> assessed ) {
case 0 :
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'mod/data:rate' , CAP_PREVENT , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'mod/data:rate' , CAP_PREVENT , $teacherrole -> id , $context -> id );
}
break ;
case 1 :
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'mod/data:rate' , CAP_ALLOW , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'mod/data:rate' , CAP_ALLOW , $teacherrole -> id , $context -> id );
}
break ;
case 2 :
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'mod/data:rate' , CAP_PREVENT , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'mod/data:rate' , CAP_ALLOW , $teacherrole -> id , $context -> id );
}
break ;
}
2006-11-09 19:44:20 +00:00
2006-09-20 16:57:01 +00:00
// $data->assesspublic:
// 0 - Students can only see their own ratings
// 1 - Students can see everyone's ratings
switch ( $data -> assesspublic ) {
case 0 :
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'mod/data:viewrating' , CAP_PREVENT , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'mod/data:viewrating' , CAP_ALLOW , $teacherrole -> id , $context -> id );
}
break ;
case 1 :
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'mod/data:viewrating' , CAP_ALLOW , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'mod/data:viewrating' , CAP_ALLOW , $teacherrole -> id , $context -> id );
}
break ;
}
if ( empty ( $cm )) {
2008-06-06 08:04:22 +00:00
$cm = $DB -> get_record ( 'course_modules' , array ( 'id' => $cmid ));
2006-09-20 16:57:01 +00:00
}
2006-11-09 19:44:20 +00:00
2006-09-20 16:57:01 +00:00
switch ( $cm -> groupmode ) {
2007-08-20 14:04:10 +00:00
case NOGROUPS :
2006-09-20 16:57:01 +00:00
break ;
2007-08-20 14:04:10 +00:00
case SEPARATEGROUPS :
2006-09-20 16:57:01 +00:00
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'moodle/site:accessallgroups' , CAP_PREVENT , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'moodle/site:accessallgroups' , CAP_ALLOW , $teacherrole -> id , $context -> id );
}
break ;
2007-08-20 14:04:10 +00:00
case VISIBLEGROUPS :
2006-09-20 16:57:01 +00:00
foreach ( $studentroles as $studentrole ) {
assign_capability ( 'moodle/site:accessallgroups' , CAP_ALLOW , $studentrole -> id , $context -> id );
}
foreach ( $teacherroles as $teacherrole ) {
assign_capability ( 'moodle/site:accessallgroups' , CAP_ALLOW , $teacherrole -> id , $context -> id );
}
break ;
}
return true ;
}
2009-05-27 04:06:19 +00:00
/**
2006-10-02 17:24:54 +00:00
* Returns the best name to show for a preset
2009-05-27 04:06:19 +00:00
*
* @ param string $shortname
* @ param string $path
* @ return string
2022-07-07 17:03:07 +02:00
* @ deprecated since Moodle 4.1 MDL - 75148 - please , use the preset :: get_name_from_plugin () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see preset :: get_name_from_plugin ()
2006-10-02 17:24:54 +00:00
*/
function data_preset_name ( $shortname , $path ) {
2022-07-07 17:03:07 +02:00
debugging ( 'data_preset_name() is deprecated. Please use preset::get_name_from_plugin() instead.' , DEBUG_DEVELOPER );
2006-10-02 17:24:54 +00:00
2022-07-07 17:03:07 +02:00
return preset :: get_name_from_plugin ( $shortname );
2006-10-02 17:24:54 +00:00
}
2009-05-27 04:06:19 +00:00
/**
2010-08-25 01:22:15 +00:00
* Returns an array of all the available presets .
2009-05-27 04:06:19 +00:00
*
* @ return array
2022-07-07 17:03:07 +02:00
* @ deprecated since Moodle 4.1 MDL - 75148 - please , use the manager :: get_available_presets () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see manager :: get_available_presets ()
2006-10-02 17:24:54 +00:00
*/
function data_get_available_presets ( $context ) {
2022-07-07 17:03:07 +02:00
debugging ( 'data_get_available_presets() is deprecated. Please use manager::get_available_presets() instead.' , DEBUG_DEVELOPER );
$cm = get_coursemodule_from_id ( '' , $context -> instanceid , 0 , false , MUST_EXIST );
$manager = manager :: create_from_coursemodule ( $cm );
return $manager -> get_available_presets ();
2010-08-25 01:22:15 +00:00
}
2006-10-02 17:24:54 +00:00
2010-08-25 01:22:15 +00:00
/**
* Gets an array of all of the presets that users have saved to the site .
*
* @ param stdClass $context The context that we are looking from .
* @ param array $presets
* @ return array An array of presets
2022-07-07 17:03:07 +02:00
* @ deprecated since Moodle 4.1 MDL - 75148 - please , use the manager :: get_available_saved_presets () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see manager :: get_available_saved_presets ()
2010-08-25 01:22:15 +00:00
*/
function data_get_available_site_presets ( $context , array $presets = array ()) {
2022-07-07 17:03:07 +02:00
debugging (
'data_get_available_site_presets() is deprecated. Please use manager::get_available_saved_presets() instead.' ,
DEBUG_DEVELOPER
);
2010-09-18 12:40:24 +00:00
2022-07-07 17:03:07 +02:00
$cm = get_coursemodule_from_id ( '' , $context -> instanceid , 0 , false , MUST_EXIST );
$manager = manager :: create_from_coursemodule ( $cm );
$savedpresets = $manager -> get_available_saved_presets ();
return array_merge ( $presets , $savedpresets );
2010-08-25 01:22:15 +00:00
}
/**
* Deletes a saved preset .
*
* @ param string $name
* @ return bool
2022-07-20 16:54:43 +02:00
* @ deprecated since Moodle 4.1 MDL - 75187 - please , use the preset :: delete () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see preset :: delete ()
2010-08-25 01:22:15 +00:00
*/
function data_delete_site_preset ( $name ) {
2022-07-20 16:54:43 +02:00
debugging ( 'data_delete_site_preset() is deprecated. Please use preset::delete() instead.' , DEBUG_DEVELOPER );
2010-08-25 01:22:15 +00:00
$fs = get_file_storage ();
$files = $fs -> get_directory_files ( DATA_PRESET_CONTEXT , DATA_PRESET_COMPONENT , DATA_PRESET_FILEAREA , 0 , '/' . $name . '/' );
if ( ! empty ( $files )) {
foreach ( $files as $file ) {
$file -> delete ();
2006-10-02 17:24:54 +00:00
}
}
2008-06-12 14:13:51 +00:00
2010-08-25 01:22:15 +00:00
$dir = $fs -> get_file ( DATA_PRESET_CONTEXT , DATA_PRESET_COMPONENT , DATA_PRESET_FILEAREA , 0 , '/' . $name . '/' , '.' );
if ( ! empty ( $dir )) {
$dir -> delete ();
}
return true ;
2006-10-02 17:24:54 +00:00
}
2009-05-27 04:06:19 +00:00
/**
2010-08-25 01:22:15 +00:00
* Prints the heads for a page
*
* @ param stdClass $course
* @ param stdClass $cm
* @ param stdClass $data
2009-05-27 04:06:19 +00:00
* @ param string $currenttab
2021-07-13 13:31:47 +08:00
* @ param string $actionbar
2009-05-27 04:06:19 +00:00
*/
2021-07-13 13:31:47 +08:00
function data_print_header ( $course , $cm , $data , $currenttab = '' , string $actionbar = '' ) {
2008-06-12 14:13:51 +00:00
2021-04-22 10:03:40 +08:00
global $CFG , $displaynoticegood , $displaynoticebad , $OUTPUT , $PAGE , $USER ;
2008-06-12 14:13:51 +00:00
2009-09-07 05:43:15 +00:00
$PAGE -> set_title ( $data -> name );
echo $OUTPUT -> header ();
2021-07-13 13:31:47 +08:00
echo $actionbar ;
2008-06-12 14:13:51 +00:00
2008-05-31 00:30:00 +00:00
// Print any notices
2008-06-12 14:13:51 +00:00
2006-10-02 17:24:54 +00:00
if ( ! empty ( $displaynoticegood )) {
2009-08-18 05:13:51 +00:00
echo $OUTPUT -> notification ( $displaynoticegood , 'notifysuccess' ); // good (usually green)
2006-10-02 17:24:54 +00:00
} else if ( ! empty ( $displaynoticebad )) {
2009-08-18 05:13:51 +00:00
echo $OUTPUT -> notification ( $displaynoticebad ); // bad (usuually red)
2006-10-02 17:24:54 +00:00
}
}
2006-09-20 16:57:01 +00:00
2009-05-27 04:06:19 +00:00
/**
2011-03-14 20:47:45 +01:00
* Can user add more entries ?
*
2009-05-27 04:06:19 +00:00
* @ param object $data
* @ param mixed $currentgroup
* @ param int $groupmode
2011-03-14 20:47:45 +01:00
* @ param stdClass $context
2009-05-27 04:06:19 +00:00
* @ return bool
*/
2011-03-11 10:03:14 +08:00
function data_user_can_add_entry ( $data , $currentgroup , $groupmode , $context = null ) {
2022-07-14 11:08:46 +02:00
global $DB ;
// Don't let add entry to a database that has no fields.
if ( ! $DB -> record_exists ( 'data_fields' , [ 'dataid' => $data -> id ])) {
return false ;
}
2008-06-12 14:13:51 +00:00
2011-03-11 10:03:14 +08:00
if ( empty ( $context )) {
2011-03-14 20:47:45 +01:00
$cm = get_coursemodule_from_instance ( 'data' , $data -> id , 0 , false , MUST_EXIST );
2012-07-26 13:57:56 +08:00
$context = context_module :: instance ( $cm -> id );
2011-03-11 10:03:14 +08:00
}
2011-03-14 20:47:45 +01:00
if ( has_capability ( 'mod/data:manageentries' , $context )) {
// no entry limits apply if user can manage
2011-03-11 10:03:14 +08:00
2011-03-14 20:47:45 +01:00
} else if ( ! has_capability ( 'mod/data:writeentry' , $context )) {
2011-03-11 10:03:14 +08:00
return false ;
2008-06-12 14:13:51 +00:00
2011-03-14 20:47:45 +01:00
} else if ( data_atmaxentries ( $data )) {
2006-12-10 20:16:03 +00:00
return false ;
2012-02-28 17:15:28 +00:00
} else if ( data_in_readonly_period ( $data )) {
// Check whether we're in a read-only period
2010-07-29 03:26:02 +00:00
return false ;
}
2007-06-03 19:36:20 +00:00
if ( ! $groupmode or has_capability ( 'moodle/site:accessallgroups' , $context )) {
return true ;
}
2006-12-10 20:16:03 +00:00
if ( $currentgroup ) {
2007-08-15 20:21:01 +00:00
return groups_is_member ( $currentgroup );
2006-12-10 20:16:03 +00:00
} else {
//else it might be group 0 in visible mode
if ( $groupmode == VISIBLEGROUPS ){
return true ;
2007-06-03 19:36:20 +00:00
} else {
return false ;
2006-12-10 20:16:03 +00:00
}
}
}
2015-06-25 17:10:34 +02:00
/**
* Check whether the current user is allowed to manage the given record considering manageentries capability ,
* data_in_readonly_period () result , ownership ( determined by data_isowner ()) and manageapproved setting .
* @ param mixed $record record object or id
* @ param object $data data object
* @ param object $context context object
* @ return bool returns true if the user is allowd to edit the entry , false otherwise
*/
function data_user_can_manage_entry ( $record , $data , $context ) {
global $DB ;
if ( has_capability ( 'mod/data:manageentries' , $context )) {
return true ;
}
// Check whether this activity is read-only at present.
$readonly = data_in_readonly_period ( $data );
if ( ! $readonly ) {
// Get record object from db if just id given like in data_isowner.
// ...done before calling data_isowner() to avoid querying db twice.
if ( ! is_object ( $record )) {
if ( ! $record = $DB -> get_record ( 'data_records' , array ( 'id' => $record ))) {
return false ;
}
}
if ( data_isowner ( $record )) {
if ( $data -> approval && $record -> approved ) {
return $data -> manageapproved == 1 ;
} else {
return true ;
}
}
}
return false ;
}
2012-02-28 17:15:28 +00:00
/**
* Check whether the specified database activity is currently in a read - only period
*
* @ param object $data
* @ return bool returns true if the time fields in $data indicate a read - only period ; false otherwise
*/
function data_in_readonly_period ( $data ) {
$now = time ();
if ( ! $data -> timeviewfrom && ! $data -> timeviewto ) {
return false ;
} else if (( $data -> timeviewfrom && $now < $data -> timeviewfrom ) || ( $data -> timeviewto && $now > $data -> timeviewto )) {
return false ;
}
return true ;
}
2007-04-26 22:04:43 +00:00
2009-05-27 04:06:19 +00:00
/**
2022-07-07 17:03:07 +02:00
* Check if the files in a directory are the expected for a preset .
*
* @ return bool Wheter the defined $directory has or not all the expected preset files .
*
* @ deprecated since Moodle 4.1 MDL - 75148 - please , use the preset :: is_directory_a_preset () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see manager :: is_directory_a_preset ()
2009-05-27 04:06:19 +00:00
*/
2007-04-26 22:04:43 +00:00
function is_directory_a_preset ( $directory ) {
2022-07-07 17:03:07 +02:00
debugging ( 'is_directory_a_preset() is deprecated. Please use preset::is_directory_a_preset() instead.' , DEBUG_DEVELOPER );
2008-06-12 14:13:51 +00:00
2022-07-07 17:03:07 +02:00
return preset :: is_directory_a_preset ( $directory );
2007-04-26 22:04:43 +00:00
}
2009-05-27 04:06:19 +00:00
/**
2010-08-04 08:23:52 +00:00
* Abstract class used for data preset importers
2022-10-06 14:03:55 +02:00
*
* @ deprecated since Moodle 4.1 MDL - 75140 - please do not use this class any more .
* @ todo MDL - 75189 Final deprecation in Moodle 4.5 .
2009-05-27 04:06:19 +00:00
*/
2010-08-04 08:23:52 +00:00
abstract class data_preset_importer {
2008-06-12 14:13:51 +00:00
2010-08-04 08:23:52 +00:00
protected $course ;
protected $cm ;
protected $module ;
protected $directory ;
2007-04-26 22:04:43 +00:00
2009-05-27 04:06:19 +00:00
/**
2010-08-04 08:23:52 +00:00
* Constructor
*
* @ param stdClass $course
* @ param stdClass $cm
* @ param stdClass $module
* @ param string $directory
2009-05-27 04:06:19 +00:00
*/
2010-08-04 08:23:52 +00:00
public function __construct ( $course , $cm , $module , $directory ) {
2022-10-06 14:03:55 +02:00
debugging (
'data_preset_importer is deprecated. Please use mod\\data\\local\\importer\\preset_importer instead' ,
DEBUG_DEVELOPER
);
2007-04-26 22:04:43 +00:00
$this -> course = $course ;
$this -> cm = $cm ;
2010-08-04 08:23:52 +00:00
$this -> module = $module ;
$this -> directory = $directory ;
}
/**
* Returns the name of the directory the preset is located in
* @ return string
*/
public function get_directory () {
return basename ( $this -> directory );
2007-04-26 22:04:43 +00:00
}
2010-10-01 02:06:56 +00:00
/**
* Retreive the contents of a file . That file may either be in a conventional directory of the Moodle file storage
* @ param file_storage $filestorage . should be null if using a conventional directory
* @ param stored_file $fileobj the directory to look in . null if using a conventional directory
* @ param string $dir the directory to look in . null if using the Moodle file storage
* @ param string $filename the name of the file we want
2012-11-14 12:22:51 +08:00
* @ return string the contents of the file or null if the file doesn ' t exist .
2010-10-01 02:06:56 +00:00
*/
public function data_preset_get_file_contents ( & $filestorage , & $fileobj , $dir , $filename ) {
if ( empty ( $filestorage ) || empty ( $fileobj )) {
if ( substr ( $dir , - 1 ) != '/' ) {
$dir .= '/' ;
}
2012-11-14 12:22:51 +08:00
if ( file_exists ( $dir . $filename )) {
return file_get_contents ( $dir . $filename );
} else {
return null ;
}
2010-10-01 02:06:56 +00:00
} else {
2012-11-14 12:22:51 +08:00
if ( $filestorage -> file_exists ( DATA_PRESET_CONTEXT , DATA_PRESET_COMPONENT , DATA_PRESET_FILEAREA , 0 , $fileobj -> get_filepath (), $filename )) {
$file = $filestorage -> get_file ( DATA_PRESET_CONTEXT , DATA_PRESET_COMPONENT , DATA_PRESET_FILEAREA , 0 , $fileobj -> get_filepath (), $filename );
return $file -> get_content ();
} else {
return null ;
}
2010-10-01 02:06:56 +00:00
}
}
2009-05-27 04:06:19 +00:00
/**
2010-08-04 08:23:52 +00:00
* Gets the preset settings
* @ global moodle_database $DB
* @ return stdClass
2009-05-27 04:06:19 +00:00
*/
2010-08-04 08:23:52 +00:00
public function get_preset_settings () {
2021-07-13 13:31:47 +08:00
global $DB , $CFG ;
require_once ( $CFG -> libdir . '/xmlize.php' );
2007-04-26 22:04:43 +00:00
2010-10-01 02:06:56 +00:00
$fs = $fileobj = null ;
2022-07-07 17:03:07 +02:00
if ( ! preset :: is_directory_a_preset ( $this -> directory )) {
2010-10-01 02:06:56 +00:00
//maybe the user requested a preset stored in the Moodle file storage
2010-10-03 13:15:49 +00:00
2010-10-01 02:06:56 +00:00
$fs = get_file_storage ();
$files = $fs -> get_area_files ( DATA_PRESET_CONTEXT , DATA_PRESET_COMPONENT , DATA_PRESET_FILEAREA );
2010-10-18 08:45:45 +00:00
//preset name to find will be the final element of the directory
2012-11-14 12:22:51 +08:00
$explodeddirectory = explode ( '/' , $this -> directory );
$presettofind = end ( $explodeddirectory );
2010-10-18 08:45:45 +00:00
//now go through the available files available and see if we can find it
2010-10-01 02:06:56 +00:00
foreach ( $files as $file ) {
if (( $file -> is_directory () && $file -> get_filepath () == '/' ) || ! $file -> is_directory ()) {
continue ;
}
$presetname = trim ( $file -> get_filepath (), '/' );
2010-10-18 08:45:45 +00:00
if ( $presetname == $presettofind ) {
2010-10-01 02:06:56 +00:00
$this -> directory = $presetname ;
$fileobj = $file ;
}
}
if ( empty ( $fileobj )) {
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'invalidpreset' , 'data' , '' , $this -> directory );
2010-10-01 02:06:56 +00:00
}
2007-04-26 22:04:43 +00:00
}
2010-08-04 08:23:52 +00:00
$allowed_settings = array (
'intro' ,
'comments' ,
'requiredentries' ,
'requiredentriestoview' ,
'maxentries' ,
'rssarticles' ,
'approval' ,
'defaultsortdir' ,
'defaultsort' );
$result = new stdClass ;
$result -> settings = new stdClass ;
$result -> importfields = array ();
$result -> currentfields = $DB -> get_records ( 'data_fields' , array ( 'dataid' => $this -> module -> id ));
if ( ! $result -> currentfields ) {
$result -> currentfields = array ();
}
2007-04-26 22:04:43 +00:00
/* Grab XML */
2010-10-01 02:06:56 +00:00
$presetxml = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , 'preset.xml' );
2008-04-11 03:59:41 +00:00
$parsedxml = xmlize ( $presetxml , 0 );
2007-04-26 22:04:43 +00:00
/* First, do settings. Put in user friendly array. */
$settingsarray = $parsedxml [ 'preset' ][ '#' ][ 'settings' ][ 0 ][ '#' ];
2010-08-04 08:23:52 +00:00
$result -> settings = new StdClass ();
2007-04-26 22:04:43 +00:00
foreach ( $settingsarray as $setting => $value ) {
2010-08-04 08:23:52 +00:00
if ( ! is_array ( $value ) || ! in_array ( $setting , $allowed_settings )) {
2008-04-20 19:22:06 +00:00
// unsupported setting
continue ;
}
2010-08-04 08:23:52 +00:00
$result -> settings -> $setting = $value [ 0 ][ '#' ];
2007-04-26 22:04:43 +00:00
}
/* Now work out fields to user friendly array */
$fieldsarray = $parsedxml [ 'preset' ][ '#' ][ 'field' ];
foreach ( $fieldsarray as $field ) {
2008-04-13 23:02:31 +00:00
if ( ! is_array ( $field )) {
continue ;
}
2007-04-26 22:04:43 +00:00
$f = new StdClass ();
foreach ( $field [ '#' ] as $param => $value ) {
2008-04-13 23:02:31 +00:00
if ( ! is_array ( $value )) {
continue ;
}
2008-06-06 08:04:22 +00:00
$f -> $param = $value [ 0 ][ '#' ];
2007-04-26 22:04:43 +00:00
}
2010-08-04 08:23:52 +00:00
$f -> dataid = $this -> module -> id ;
2007-04-26 22:04:43 +00:00
$f -> type = clean_param ( $f -> type , PARAM_ALPHA );
2010-08-04 08:23:52 +00:00
$result -> importfields [] = $f ;
2007-04-26 22:04:43 +00:00
}
/* Now add the HTML templates to the settings array so we can update d */
2010-10-01 02:06:56 +00:00
$result -> settings -> singletemplate = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " singletemplate.html " );
$result -> settings -> listtemplate = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " listtemplate.html " );
$result -> settings -> listtemplateheader = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " listtemplateheader.html " );
$result -> settings -> listtemplatefooter = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " listtemplatefooter.html " );
$result -> settings -> addtemplate = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " addtemplate.html " );
$result -> settings -> rsstemplate = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " rsstemplate.html " );
$result -> settings -> rsstitletemplate = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " rsstitletemplate.html " );
$result -> settings -> csstemplate = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " csstemplate.css " );
$result -> settings -> jstemplate = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " jstemplate.js " );
2012-11-14 12:22:51 +08:00
$result -> settings -> asearchtemplate = $this -> data_preset_get_file_contents ( $fs , $fileobj , $this -> directory , " asearchtemplate.html " );
2007-04-26 22:04:43 +00:00
2010-08-04 08:23:52 +00:00
$result -> settings -> instance = $this -> module -> id ;
return $result ;
2007-04-26 22:04:43 +00:00
}
2009-05-27 04:06:19 +00:00
/**
2010-08-04 08:23:52 +00:00
* Import the preset into the given database module
2009-05-27 04:06:19 +00:00
* @ return bool
*/
2010-08-04 08:23:52 +00:00
function import ( $overwritesettings ) {
2022-09-05 17:18:44 +02:00
global $DB , $CFG , $OUTPUT ;
2007-04-26 22:04:43 +00:00
2010-08-04 08:23:52 +00:00
$params = $this -> get_preset_settings ();
$settings = $params -> settings ;
$newfields = $params -> importfields ;
$currentfields = $params -> currentfields ;
2007-04-26 22:04:43 +00:00
$preservedfields = array ();
2008-06-12 14:13:51 +00:00
2007-04-26 22:04:43 +00:00
/* Maps fields and makes new ones */
if ( ! empty ( $newfields )) {
/* We require an injective mapping, and need to know what to protect */
foreach ( $newfields as $nid => $newfield ) {
$cid = optional_param ( " field_ $nid " , - 1 , PARAM_INT );
2010-08-04 08:23:52 +00:00
if ( $cid == - 1 ) {
continue ;
}
2008-06-25 14:52:39 +00:00
if ( array_key_exists ( $cid , $preservedfields )){
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'notinjectivemap' , 'data' );
2008-09-14 08:22:44 +00:00
}
2007-04-26 22:04:43 +00:00
else $preservedfields [ $cid ] = true ;
}
2022-09-05 17:18:44 +02:00
$missingfieldtypes = [];
2007-04-26 22:04:43 +00:00
foreach ( $newfields as $nid => $newfield ) {
$cid = optional_param ( " field_ $nid " , - 1 , PARAM_INT );
2008-06-12 14:13:51 +00:00
2007-04-26 22:04:43 +00:00
/* A mapping. Just need to change field params. Data kept. */
if ( $cid != - 1 and isset ( $currentfields [ $cid ])) {
2010-08-04 08:23:52 +00:00
$fieldobject = data_get_field_from_id ( $currentfields [ $cid ] -> id , $this -> module );
2007-04-26 22:04:43 +00:00
foreach ( $newfield as $param => $value ) {
if ( $param != " id " ) {
$fieldobject -> field -> $param = $value ;
}
}
unset ( $fieldobject -> field -> similarfield );
$fieldobject -> update_field ();
unset ( $fieldobject );
2010-08-04 08:23:52 +00:00
} else {
/* Make a new field */
2022-09-05 17:18:44 +02:00
$filepath = " field/ $newfield->type /field.class.php " ;
if ( ! file_exists ( $filepath )) {
$missingfieldtypes [] = $newfield -> name ;
continue ;
}
include_once ( $filepath );
2008-06-12 14:13:51 +00:00
2007-04-26 22:04:43 +00:00
if ( ! isset ( $newfield -> description )) {
$newfield -> description = '' ;
}
$classname = 'data_field_' . $newfield -> type ;
2010-08-04 08:23:52 +00:00
$fieldclass = new $classname ( $newfield , $this -> module );
2007-04-26 22:04:43 +00:00
$fieldclass -> insert_field ();
unset ( $fieldclass );
}
}
2022-09-05 17:18:44 +02:00
if ( ! empty ( $missingfieldtypes )) {
echo $OUTPUT -> notification ( get_string ( 'missingfieldtypeimport' , 'data' ) . html_writer :: alist ( $missingfieldtypes ));
}
2007-04-26 22:04:43 +00:00
}
/* Get rid of all old unused data */
2022-11-16 13:08:08 +01:00
foreach ( $currentfields as $cid => $currentfield ) {
if ( ! array_key_exists ( $cid , $preservedfields )) {
/* Data not used anymore so wipe! */
echo " Deleting field $currentfield->name <br /> " ;
$id = $currentfield -> id ;
// Why delete existing data records and related comments/ratings??
$DB -> delete_records ( 'data_content' , [ 'fieldid' => $id ]);
$DB -> delete_records ( 'data_fields' , [ 'id' => $id ]);
2007-04-26 22:04:43 +00:00
}
}
2010-08-04 08:23:52 +00:00
// handle special settings here
2008-04-20 19:22:06 +00:00
if ( ! empty ( $settings -> defaultsort )) {
if ( is_numeric ( $settings -> defaultsort )) {
2008-05-31 09:51:48 +00:00
// old broken value
2008-04-20 19:22:06 +00:00
$settings -> defaultsort = 0 ;
} else {
2010-08-04 08:23:52 +00:00
$settings -> defaultsort = ( int ) $DB -> get_field ( 'data_fields' , 'id' , array ( 'dataid' => $this -> module -> id , 'name' => $settings -> defaultsort ));
2008-04-20 19:22:06 +00:00
}
} else {
$settings -> defaultsort = 0 ;
}
// do we want to overwrite all current database settings?
2008-04-20 10:09:28 +00:00
if ( $overwritesettings ) {
2008-04-20 19:22:06 +00:00
// all supported settings
2008-04-20 10:09:28 +00:00
$overwrite = array_keys (( array ) $settings );
} else {
2008-04-20 19:22:06 +00:00
// only templates and sorting
2008-04-20 10:09:28 +00:00
$overwrite = array ( 'singletemplate' , 'listtemplate' , 'listtemplateheader' , 'listtemplatefooter' ,
'addtemplate' , 'rsstemplate' , 'rsstitletemplate' , 'csstemplate' , 'jstemplate' ,
2008-04-20 19:22:06 +00:00
'asearchtemplate' , 'defaultsortdir' , 'defaultsort' );
2008-04-20 10:09:28 +00:00
}
2008-04-20 19:22:06 +00:00
// now overwrite current data settings
2010-08-04 08:23:52 +00:00
foreach ( $this -> module as $prop => $unused ) {
2008-04-20 10:09:28 +00:00
if ( in_array ( $prop , $overwrite )) {
2010-08-04 08:23:52 +00:00
$this -> module -> $prop = $settings -> $prop ;
2007-08-20 10:52:59 +00:00
}
}
2007-12-05 06:13:17 +00:00
2010-08-04 08:23:52 +00:00
data_update_instance ( $this -> module );
2007-04-26 22:04:43 +00:00
2010-08-04 08:23:52 +00:00
return $this -> cleanup ();
}
2008-06-12 14:13:51 +00:00
2010-08-04 08:23:52 +00:00
/**
* Any clean up routines should go here
* @ return bool
*/
public function cleanup () {
2007-04-26 22:04:43 +00:00
return true ;
}
}
2010-08-04 08:23:52 +00:00
/**
* Data preset importer for uploaded presets
2022-10-06 14:03:55 +02:00
*
* @ deprecated since Moodle 4.1 MDL - 75140 - please do not use this class any more .
* @ todo MDL - 75189 Final deprecation in Moodle 4.5 .
2010-08-04 08:23:52 +00:00
*/
class data_preset_upload_importer extends data_preset_importer {
public function __construct ( $course , $cm , $module , $filepath ) {
global $USER ;
2022-10-06 14:03:55 +02:00
debugging (
'data_preset_upload_importer is deprecated. Please use mod\\data\\local\\importer\\preset_upload_importer instead' ,
DEBUG_DEVELOPER
);
2010-08-04 08:23:52 +00:00
if ( is_file ( $filepath )) {
$fp = get_file_packer ();
if ( $fp -> extract_to_pathname ( $filepath , $filepath . '_extracted' )) {
fulldelete ( $filepath );
}
$filepath .= '_extracted' ;
}
parent :: __construct ( $course , $cm , $module , $filepath );
}
2022-08-22 09:55:56 +02:00
2010-08-04 08:23:52 +00:00
public function cleanup () {
return fulldelete ( $this -> directory );
}
}
/**
* Data preset importer for existing presets
2022-10-06 14:03:55 +02:00
*
* @ deprecated since Moodle 4.1 MDL - 75140 - please do not use this class any more .
* @ todo MDL - 75189 Final deprecation in Moodle 4.5 .
2010-08-04 08:23:52 +00:00
*/
class data_preset_existing_importer extends data_preset_importer {
protected $userid ;
public function __construct ( $course , $cm , $module , $fullname ) {
global $USER ;
2022-10-06 14:03:55 +02:00
debugging (
'data_preset_existing_importer is deprecated. Please use mod\\data\\local\\importer\\preset_existing_importer instead' ,
DEBUG_DEVELOPER
);
2010-08-04 08:23:52 +00:00
list ( $userid , $shortname ) = explode ( '/' , $fullname , 2 );
2012-07-26 13:57:56 +08:00
$context = context_module :: instance ( $cm -> id );
2010-08-04 08:23:52 +00:00
if ( $userid && ( $userid != $USER -> id ) && ! has_capability ( 'mod/data:manageuserpresets' , $context ) && ! has_capability ( 'mod/data:viewalluserpresets' , $context )) {
throw new coding_exception ( 'Invalid preset provided' );
}
$this -> userid = $userid ;
$filepath = data_preset_path ( $course , $userid , $shortname );
parent :: __construct ( $course , $cm , $module , $filepath );
}
public function get_userid () {
return $this -> userid ;
}
}
2009-05-27 04:06:19 +00:00
/**
* @ global object
* @ global object
* @ param object $course
* @ param int $userid
* @ param string $shortname
* @ return string
*/
2007-04-26 22:04:43 +00:00
function data_preset_path ( $course , $userid , $shortname ) {
global $USER , $CFG ;
2008-06-12 14:13:51 +00:00
2012-07-26 13:57:56 +08:00
$context = context_course :: instance ( $course -> id );
2008-06-12 14:13:51 +00:00
2007-04-26 22:04:43 +00:00
$userid = ( int ) $userid ;
2008-06-12 14:13:51 +00:00
2010-10-01 02:06:56 +00:00
$path = null ;
2007-04-26 22:04:43 +00:00
if ( $userid > 0 && ( $userid == $USER -> id || has_capability ( 'mod/data:viewalluserpresets' , $context ))) {
2010-10-01 02:06:56 +00:00
$path = $CFG -> dataroot . '/data/preset/' . $userid . '/' . $shortname ;
2007-04-26 22:04:43 +00:00
} else if ( $userid == 0 ) {
2010-10-01 02:06:56 +00:00
$path = $CFG -> dirroot . '/mod/data/preset/' . $shortname ;
2007-04-26 22:04:43 +00:00
} else if ( $userid < 0 ) {
2011-08-11 03:03:58 +09:30
$path = $CFG -> tempdir . '/data/' .- $userid . '/' . $shortname ;
2007-04-26 22:04:43 +00:00
}
2008-06-12 14:13:51 +00:00
2010-10-01 02:06:56 +00:00
return $path ;
2007-04-26 22:04:43 +00:00
}
2007-11-29 14:43:04 +00:00
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the data .
2009-05-27 04:06:19 +00:00
*
2007-11-29 14:43:04 +00:00
* @ param $mform form passed by reference
*/
function data_reset_course_form_definition ( & $mform ) {
$mform -> addElement ( 'header' , 'dataheader' , get_string ( 'modulenameplural' , 'data' ));
$mform -> addElement ( 'checkbox' , 'reset_data' , get_string ( 'deleteallentries' , 'data' ));
$mform -> addElement ( 'checkbox' , 'reset_data_notenrolled' , get_string ( 'deletenotenrolled' , 'data' ));
$mform -> disabledIf ( 'reset_data_notenrolled' , 'reset_data' , 'checked' );
$mform -> addElement ( 'checkbox' , 'reset_data_ratings' , get_string ( 'deleteallratings' ));
$mform -> disabledIf ( 'reset_data_ratings' , 'reset_data' , 'checked' );
$mform -> addElement ( 'checkbox' , 'reset_data_comments' , get_string ( 'deleteallcomments' ));
$mform -> disabledIf ( 'reset_data_comments' , 'reset_data' , 'checked' );
2017-09-20 16:45:38 +08:00
$mform -> addElement ( 'checkbox' , 'reset_data_tags' , get_string ( 'removealldatatags' , 'data' ));
$mform -> disabledIf ( 'reset_data_tags' , 'reset_data' , 'checked' );
2007-11-29 14:43:04 +00:00
}
/**
* Course reset form defaults .
2009-05-27 04:06:19 +00:00
* @ return array
2007-11-29 14:43:04 +00:00
*/
function data_reset_course_form_defaults ( $course ) {
return array ( 'reset_data' => 0 , 'reset_data_ratings' => 1 , 'reset_data_comments' => 1 , 'reset_data_notenrolled' => 0 );
}
/**
* Removes all grades from gradebook
2009-05-27 04:06:19 +00:00
*
* @ global object
* @ global object
2007-11-29 14:43:04 +00:00
* @ param int $courseid
2009-05-27 04:06:19 +00:00
* @ param string $type optional type
2007-11-29 14:43:04 +00:00
*/
function data_reset_gradebook ( $courseid , $type = '' ) {
2008-06-06 08:04:22 +00:00
global $CFG , $DB ;
2007-11-29 14:43:04 +00:00
$sql = " SELECT d.*, cm.idnumber as cmidnumber, d.course as courseid
2008-06-06 08:04:22 +00:00
FROM { data } d , { course_modules } cm , { modules } m
WHERE m . name = 'data' AND m . id = cm . module AND cm . instance = d . id AND d . course = ? " ;
2007-11-29 14:43:04 +00:00
2008-06-06 08:04:22 +00:00
if ( $datas = $DB -> get_records_sql ( $sql , array ( $courseid ))) {
2007-11-29 14:43:04 +00:00
foreach ( $datas as $data ) {
data_grade_item_update ( $data , 'reset' );
}
}
}
/**
2010-09-18 12:08:16 +00:00
* Actual implementation of the reset course functionality , delete all the
2007-11-29 14:43:04 +00:00
* data responses for course $data -> courseid .
2009-05-27 04:06:19 +00:00
*
* @ global object
* @ global object
* @ param object $data the data submitted from the reset course .
2007-11-29 14:43:04 +00:00
* @ return array status array
*/
function data_reset_userdata ( $data ) {
2008-06-06 08:04:22 +00:00
global $CFG , $DB ;
2007-11-29 14:43:04 +00:00
require_once ( $CFG -> libdir . '/filelib.php' );
2010-04-23 09:44:19 +00:00
require_once ( $CFG -> dirroot . '/rating/lib.php' );
2008-06-12 14:13:51 +00:00
2007-11-29 14:43:04 +00:00
$componentstr = get_string ( 'modulenameplural' , 'data' );
$status = array ();
2008-06-12 14:13:51 +00:00
2007-11-29 14:43:04 +00:00
$allrecordssql = " SELECT r.id
2008-06-06 08:04:22 +00:00
FROM { data_records } r
INNER JOIN { data } d ON r . dataid = d . id
WHERE d . course = ? " ;
2007-11-29 14:43:04 +00:00
$alldatassql = " SELECT d.id
2008-06-06 08:04:22 +00:00
FROM { data } d
WHERE d . course = ? " ;
2007-11-29 14:43:04 +00:00
2010-04-23 09:44:19 +00:00
$rm = new rating_manager ();
2011-05-11 13:32:36 +08:00
$ratingdeloptions = new stdClass ;
$ratingdeloptions -> component = 'mod_data' ;
$ratingdeloptions -> ratingarea = 'entry' ;
2010-04-23 09:44:19 +00:00
2014-11-09 21:04:27 -08:00
// Set the file storage - may need it to remove files later.
$fs = get_file_storage ();
2007-11-29 14:43:04 +00:00
// delete entries if requested
if ( ! empty ( $data -> reset_data )) {
2009-11-18 06:00:48 +00:00
$DB -> delete_records_select ( 'comments' , " itemid IN ( $allrecordssql ) AND commentarea='database_entry' " , array ( $data -> courseid ));
2008-06-06 08:04:22 +00:00
$DB -> delete_records_select ( 'data_content' , " recordid IN ( $allrecordssql ) " , array ( $data -> courseid ));
$DB -> delete_records_select ( 'data_records' , " dataid IN ( $alldatassql ) " , array ( $data -> courseid ));
2007-11-29 14:43:04 +00:00
2008-06-06 08:04:22 +00:00
if ( $datas = $DB -> get_records_sql ( $alldatassql , array ( $data -> courseid ))) {
2007-11-29 14:43:04 +00:00
foreach ( $datas as $dataid => $unused ) {
2010-04-23 09:44:19 +00:00
if ( ! $cm = get_coursemodule_from_instance ( 'data' , $dataid )) {
continue ;
}
2012-07-26 13:57:56 +08:00
$datacontext = context_module :: instance ( $cm -> id );
2010-04-23 09:44:19 +00:00
2014-11-09 21:04:27 -08:00
// Delete any files that may exist.
$fs -> delete_area_files ( $datacontext -> id , 'mod_data' , 'content' );
2010-04-23 09:44:19 +00:00
$ratingdeloptions -> contextid = $datacontext -> id ;
$rm -> delete_ratings ( $ratingdeloptions );
2017-09-20 16:45:38 +08:00
core_tag_tag :: delete_instances ( 'mod_data' , null , $datacontext -> id );
2007-11-29 14:43:04 +00:00
}
}
2008-06-12 14:13:51 +00:00
2007-11-29 14:43:04 +00:00
if ( empty ( $data -> reset_gradebook_grades )) {
// remove all grades from gradebook
data_reset_gradebook ( $data -> courseid );
}
$status [] = array ( 'component' => $componentstr , 'item' => get_string ( 'deleteallentries' , 'data' ), 'error' => false );
}
// remove entries by users not enrolled into course
if ( ! empty ( $data -> reset_data_notenrolled )) {
$recordssql = " SELECT r.id, r.userid, r.dataid, u.id AS userexists, u.deleted AS userdeleted
2008-06-06 08:04:22 +00:00
FROM { data_records } r
JOIN { data } d ON r . dataid = d . id
LEFT JOIN { user } u ON r . userid = u . id
WHERE d . course = ? AND r . userid > 0 " ;
2007-11-29 14:43:04 +00:00
2012-07-26 13:57:56 +08:00
$course_context = context_course :: instance ( $data -> courseid );
2007-11-29 14:43:04 +00:00
$notenrolled = array ();
$fields = array ();
2011-01-07 11:12:50 +01:00
$rs = $DB -> get_recordset_sql ( $recordssql , array ( $data -> courseid ));
foreach ( $rs as $record ) {
if ( array_key_exists ( $record -> userid , $notenrolled ) or ! $record -> userexists or $record -> userdeleted
or ! is_enrolled ( $course_context , $record -> userid )) {
//delete ratings
if ( ! $cm = get_coursemodule_from_instance ( 'data' , $record -> dataid )) {
continue ;
}
2012-07-26 13:57:56 +08:00
$datacontext = context_module :: instance ( $cm -> id );
2011-01-07 11:12:50 +01:00
$ratingdeloptions -> contextid = $datacontext -> id ;
$ratingdeloptions -> itemid = $record -> id ;
$rm -> delete_ratings ( $ratingdeloptions );
2014-11-09 21:04:27 -08:00
// Delete any files that may exist.
if ( $contents = $DB -> get_records ( 'data_content' , array ( 'recordid' => $record -> id ), '' , 'id' )) {
foreach ( $contents as $content ) {
$fs -> delete_area_files ( $datacontext -> id , 'mod_data' , 'content' , $content -> id );
2007-11-29 14:43:04 +00:00
}
}
2011-01-07 11:12:50 +01:00
$notenrolled [ $record -> userid ] = true ;
2014-11-09 21:04:27 -08:00
2017-09-20 16:45:38 +08:00
core_tag_tag :: remove_all_item_tags ( 'mod_data' , 'data_records' , $record -> id );
2014-11-09 21:04:27 -08:00
$DB -> delete_records ( 'comments' , array ( 'itemid' => $record -> id , 'commentarea' => 'database_entry' ));
$DB -> delete_records ( 'data_content' , array ( 'recordid' => $record -> id ));
$DB -> delete_records ( 'data_records' , array ( 'id' => $record -> id ));
2007-11-29 14:43:04 +00:00
}
}
2011-01-07 11:12:50 +01:00
$rs -> close ();
$status [] = array ( 'component' => $componentstr , 'item' => get_string ( 'deletenotenrolled' , 'data' ), 'error' => false );
2007-11-29 14:43:04 +00:00
}
// remove all ratings
if ( ! empty ( $data -> reset_data_ratings )) {
2010-04-23 09:44:19 +00:00
if ( $datas = $DB -> get_records_sql ( $alldatassql , array ( $data -> courseid ))) {
foreach ( $datas as $dataid => $unused ) {
if ( ! $cm = get_coursemodule_from_instance ( 'data' , $dataid )) {
continue ;
}
2012-07-26 13:57:56 +08:00
$datacontext = context_module :: instance ( $cm -> id );
2010-04-23 09:44:19 +00:00
$ratingdeloptions -> contextid = $datacontext -> id ;
$rm -> delete_ratings ( $ratingdeloptions );
}
}
2007-11-29 14:43:04 +00:00
if ( empty ( $data -> reset_gradebook_grades )) {
// remove all grades from gradebook
data_reset_gradebook ( $data -> courseid );
}
2008-06-12 14:13:51 +00:00
2007-11-29 14:43:04 +00:00
$status [] = array ( 'component' => $componentstr , 'item' => get_string ( 'deleteallratings' ), 'error' => false );
}
// remove all comments
if ( ! empty ( $data -> reset_data_comments )) {
2009-11-18 06:00:48 +00:00
$DB -> delete_records_select ( 'comments' , " itemid IN ( $allrecordssql ) AND commentarea='database_entry' " , array ( $data -> courseid ));
2007-11-29 14:43:04 +00:00
$status [] = array ( 'component' => $componentstr , 'item' => get_string ( 'deleteallcomments' ), 'error' => false );
}
2017-09-20 16:45:38 +08:00
// Remove all the tags.
if ( ! empty ( $data -> reset_data_tags )) {
if ( $datas = $DB -> get_records_sql ( $alldatassql , array ( $data -> courseid ))) {
foreach ( $datas as $dataid => $unused ) {
if ( ! $cm = get_coursemodule_from_instance ( 'data' , $dataid )) {
continue ;
}
$context = context_module :: instance ( $cm -> id );
core_tag_tag :: delete_instances ( 'mod_data' , null , $context -> id );
}
}
$status [] = array ( 'component' => $componentstr , 'item' => get_string ( 'tagsdeleted' , 'data' ), 'error' => false );
}
2008-05-31 00:30:00 +00:00
// updating dates - shift may be negative too
2007-11-29 14:43:04 +00:00
if ( $data -> timeshift ) {
2017-06-29 13:38:47 +05:30
// Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
// See MDL-9367.
shift_course_mod_dates ( 'data' , array ( 'timeavailablefrom' , 'timeavailableto' ,
'timeviewfrom' , 'timeviewto' , 'assesstimestart' , 'assesstimefinish' ), $data -> timeshift , $data -> courseid );
2007-11-29 14:43:04 +00:00
$status [] = array ( 'component' => $componentstr , 'item' => get_string ( 'datechanged' ), 'error' => false );
}
2008-06-12 14:13:51 +00:00
2007-11-29 14:43:04 +00:00
return $status ;
}
2008-07-24 21:59:13 +00:00
/**
* Returns all other caps used in module
2009-05-27 04:06:19 +00:00
*
* @ return array
2008-07-24 21:59:13 +00:00
*/
function data_get_extra_capabilities () {
2018-10-30 12:00:12 +00:00
return [ 'moodle/rating:view' , 'moodle/rating:viewany' , 'moodle/rating:viewall' , 'moodle/rating:rate' ,
'moodle/comment:view' , 'moodle/comment:post' , 'moodle/comment:delete' ];
2008-07-24 21:59:13 +00:00
}
2008-07-29 17:22:47 +00:00
/**
* @ param string $feature FEATURE_xx constant for requested feature
2021-09-20 11:53:23 +02:00
* @ return mixed True if module supports feature , false if not , null if doesn ' t know or string for the module purpose .
2008-07-29 17:22:47 +00:00
*/
function data_supports ( $feature ) {
switch ( $feature ) {
2009-04-21 08:57:20 +00:00
case FEATURE_GROUPS : return true ;
case FEATURE_GROUPINGS : return true ;
2009-04-21 21:17:21 +00:00
case FEATURE_MOD_INTRO : return true ;
2008-07-29 17:22:47 +00:00
case FEATURE_COMPLETION_TRACKS_VIEWS : return true ;
2017-04-03 10:44:32 +08:00
case FEATURE_COMPLETION_HAS_RULES : return true ;
2009-04-21 08:57:20 +00:00
case FEATURE_GRADE_HAS_GRADE : return true ;
case FEATURE_GRADE_OUTCOMES : return true ;
2010-04-23 09:44:19 +00:00
case FEATURE_RATE : return true ;
2010-09-07 08:32:41 +00:00
case FEATURE_BACKUP_MOODLE2 : return true ;
2011-07-21 15:06:47 +01:00
case FEATURE_SHOW_DESCRIPTION : return true ;
2016-10-11 13:06:00 +01:00
case FEATURE_COMMENT : return true ;
2021-09-20 11:53:23 +02:00
case FEATURE_MOD_PURPOSE : return MOD_PURPOSE_COLLABORATION ;
2009-04-21 08:57:20 +00:00
2008-07-29 17:22:47 +00:00
default : return null ;
2009-06-19 14:25:56 +00:00
}
2008-07-29 17:22:47 +00:00
}
2019-10-04 19:13:52 +02:00
/**
* Import records for a data instance from csv data .
*
* @ param object $cm Course module of the data instance .
* @ param object $data The data instance .
* @ param string $csvdata The csv data to be imported .
* @ param string $encoding The encoding of csv data .
* @ param string $fielddelimiter The delimiter of the csv data .
* @ return int Number of records added .
*/
function data_import_csv ( $cm , $data , & $csvdata , $encoding , $fielddelimiter ) {
global $CFG , $DB ;
// Large files are likely to take their time and memory. Let PHP know
// that we'll take longer, and that the process should be recycled soon
// to free up memory.
core_php_time_limit :: raise ();
raise_memory_limit ( MEMORY_EXTRA );
$iid = csv_import_reader :: get_new_iid ( 'moddata' );
$cir = new csv_import_reader ( $iid , 'moddata' );
$context = context_module :: instance ( $cm -> id );
$readcount = $cir -> load_csv_content ( $csvdata , $encoding , $fielddelimiter );
$csvdata = null ; // Free memory.
if ( empty ( $readcount )) {
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'csvfailed' , 'data' , " { $CFG -> wwwroot } /mod/data/edit.php?d= { $data -> id } " );
2019-10-04 19:13:52 +02:00
} else {
if ( ! $fieldnames = $cir -> get_columns ()) {
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'cannotreadtmpfile' , 'error' );
2019-10-04 19:13:52 +02:00
}
// Check the fieldnames are valid.
$rawfields = $DB -> get_records ( 'data_fields' , array ( 'dataid' => $data -> id ), '' , 'name, id, type' );
$fields = array ();
$errorfield = '' ;
$usernamestring = get_string ( 'username' );
$safetoskipfields = array ( get_string ( 'user' ), get_string ( 'email' ),
get_string ( 'timeadded' , 'data' ), get_string ( 'timemodified' , 'data' ),
get_string ( 'approved' , 'data' ), get_string ( 'tags' , 'data' ));
$userfieldid = null ;
foreach ( $fieldnames as $id => $name ) {
if ( ! isset ( $rawfields [ $name ])) {
if ( $name == $usernamestring ) {
$userfieldid = $id ;
} else if ( ! in_array ( $name , $safetoskipfields )) {
$errorfield .= " ' $name ' " ;
}
} else {
// If this is the second time, a field with this name comes up, it must be a field not provided by the user...
// like the username.
if ( isset ( $fields [ $name ])) {
if ( $name == $usernamestring ) {
$userfieldid = $id ;
}
unset ( $fieldnames [ $id ]); // To ensure the user provided content fields remain in the array once flipped.
} else {
$field = $rawfields [ $name ];
2022-09-05 17:18:44 +02:00
$filepath = " $CFG->dirroot /mod/data/field/ $field->type /field.class.php " ;
if ( ! file_exists ( $filepath )) {
$errorfield .= " ' $name ' " ;
continue ;
}
require_once ( $filepath );
2019-10-04 19:13:52 +02:00
$classname = 'data_field_' . $field -> type ;
$fields [ $name ] = new $classname ( $field , $data , $cm );
}
}
}
if ( ! empty ( $errorfield )) {
2022-04-12 09:38:41 +05:30
throw new \moodle_exception ( 'fieldnotmatched' , 'data' ,
2019-10-04 19:13:52 +02:00
" { $CFG -> wwwroot } /mod/data/edit.php?d= { $data -> id } " , $errorfield );
}
$fieldnames = array_flip ( $fieldnames );
$cir -> init ();
$recordsadded = 0 ;
while ( $record = $cir -> next ()) {
$authorid = null ;
if ( $userfieldid ) {
if ( ! ( $author = core_user :: get_user_by_username ( $record [ $userfieldid ], 'id' ))) {
$authorid = null ;
} else {
$authorid = $author -> id ;
}
}
if ( $recordid = data_add_record ( $data , 0 , $authorid )) { // Add instance to data_record.
foreach ( $fields as $field ) {
$fieldid = $fieldnames [ $field -> field -> name ];
if ( isset ( $record [ $fieldid ])) {
$value = $record [ $fieldid ];
} else {
$value = '' ;
}
if ( method_exists ( $field , 'update_content_import' )) {
$field -> update_content_import ( $recordid , $value , 'field_' . $field -> field -> id );
} else {
$content = new stdClass ();
$content -> fieldid = $field -> field -> id ;
$content -> content = $value ;
$content -> recordid = $recordid ;
$DB -> insert_record ( 'data_content' , $content );
}
}
if ( core_tag_tag :: is_enabled ( 'mod_data' , 'data_records' ) &&
isset ( $fieldnames [ get_string ( 'tags' , 'data' )])) {
$columnindex = $fieldnames [ get_string ( 'tags' , 'data' )];
$rawtags = $record [ $columnindex ];
$tags = explode ( ',' , $rawtags );
foreach ( $tags as $tag ) {
$tag = trim ( $tag );
if ( empty ( $tag )) {
continue ;
}
core_tag_tag :: add_item_tag ( 'mod_data' , 'data_records' , $recordid , $context , $tag );
}
}
$recordsadded ++ ;
print get_string ( 'added' , 'moodle' , $recordsadded ) . " . " . get_string ( 'entry' , 'data' ) . " (ID $recordid )<br /> \n " ;
}
}
$cir -> close ();
$cir -> cleanup ( true );
return $recordsadded ;
}
return 0 ;
}
2009-05-27 04:06:19 +00:00
/**
* @ global object
* @ param array $export
* @ param string $delimiter_name
* @ param object $database
* @ param int $count
* @ param bool $return
* @ return string | void
*/
2012-07-27 09:37:33 +08:00
function data_export_csv ( $export , $delimiter_name , $database , $count , $return = false ) {
2008-08-01 12:30:26 +00:00
global $CFG ;
require_once ( $CFG -> libdir . '/csvlib.class.php' );
2012-07-27 09:37:33 +08:00
$filename = $database . '-' . $count . '-record' ;
2008-07-31 12:21:25 +00:00
if ( $count > 1 ) {
$filename .= 's' ;
}
2012-07-27 09:37:33 +08:00
if ( $return ) {
return csv_export_writer :: print_array ( $export , $delimiter_name , '"' , true );
} else {
csv_export_writer :: download_array ( $filename , $export , $delimiter_name );
2008-07-31 12:21:25 +00:00
}
}
2009-05-27 04:06:19 +00:00
/**
* @ global object
* @ param array $export
* @ param string $dataname
* @ param int $count
* @ return string
*/
2008-08-11 10:40:04 +00:00
function data_export_xls ( $export , $dataname , $count ) {
2008-07-31 12:21:25 +00:00
global $CFG ;
require_once ( " $CFG->libdir /excellib.class.php " );
2010-09-18 12:46:11 +00:00
$filename = clean_filename ( " { $dataname } - { $count } _record " );
2008-07-31 12:21:25 +00:00
if ( $count > 1 ) {
$filename .= 's' ;
}
$filename .= clean_filename ( '-' . gmdate ( " Ymd_Hi " ));
$filename .= '.xls' ;
2008-08-01 12:30:26 +00:00
$filearg = '-' ;
$workbook = new MoodleExcelWorkbook ( $filearg );
2008-08-11 10:40:04 +00:00
$workbook -> send ( $filename );
2008-07-31 12:21:25 +00:00
$worksheet = array ();
2012-12-15 12:36:40 +01:00
$worksheet [ 0 ] = $workbook -> add_worksheet ( '' );
2008-07-31 12:21:25 +00:00
$rowno = 0 ;
foreach ( $export as $row ) {
$colno = 0 ;
foreach ( $row as $col ) {
$worksheet [ 0 ] -> write ( $rowno , $colno , $col );
$colno ++ ;
}
$rowno ++ ;
}
$workbook -> close ();
2008-08-01 12:30:26 +00:00
return $filename ;
2008-07-31 12:21:25 +00:00
}
2009-05-27 04:06:19 +00:00
/**
* @ global object
* @ param array $export
* @ param string $dataname
* @ param int $count
* @ param string
*/
2008-08-11 10:40:04 +00:00
function data_export_ods ( $export , $dataname , $count ) {
2008-07-31 12:21:25 +00:00
global $CFG ;
require_once ( " $CFG->libdir /odslib.class.php " );
2010-09-18 12:46:11 +00:00
$filename = clean_filename ( " { $dataname } - { $count } _record " );
2008-07-31 12:21:25 +00:00
if ( $count > 1 ) {
$filename .= 's' ;
}
$filename .= clean_filename ( '-' . gmdate ( " Ymd_Hi " ));
$filename .= '.ods' ;
2008-08-01 12:30:26 +00:00
$filearg = '-' ;
2008-08-11 10:40:04 +00:00
$workbook = new MoodleODSWorkbook ( $filearg );
$workbook -> send ( $filename );
2008-07-31 12:21:25 +00:00
$worksheet = array ();
2012-12-15 12:36:40 +01:00
$worksheet [ 0 ] = $workbook -> add_worksheet ( '' );
2008-07-31 12:21:25 +00:00
$rowno = 0 ;
foreach ( $export as $row ) {
$colno = 0 ;
foreach ( $row as $col ) {
$worksheet [ 0 ] -> write ( $rowno , $colno , $col );
$colno ++ ;
}
$rowno ++ ;
}
$workbook -> close ();
2008-08-01 12:30:26 +00:00
return $filename ;
}
2009-05-27 04:06:19 +00:00
/**
* @ global object
* @ param int $dataid
* @ param array $fields
* @ param array $selectedfields
2012-01-30 15:52:20 +08:00
* @ param int $currentgroup group ID of the current group . This is used for
* exporting data while maintaining group divisions .
2012-08-15 10:04:24 +08:00
* @ param object $context the context in which the operation is performed ( for capability checks )
* @ param bool $userdetails whether to include the details of the record author
* @ param bool $time whether to include time created / modified
* @ param bool $approval whether to include approval status
2017-07-31 11:09:08 +01:00
* @ param bool $tags whether to include tags
2009-05-27 04:06:19 +00:00
* @ return array
*/
2012-04-17 20:15:55 +01:00
function data_get_exportdata ( $dataid , $fields , $selectedfields , $currentgroup = 0 , $context = null ,
2017-07-31 11:09:08 +01:00
$userdetails = false , $time = false , $approval = false , $tags = false ) {
2008-08-01 12:30:26 +00:00
global $DB ;
2012-04-17 20:15:55 +01:00
if ( is_null ( $context )) {
$context = context_system :: instance ();
}
// exporting user data needs special permission
$userdetails = $userdetails && has_capability ( 'mod/data:exportuserinfo' , $context );
2008-08-01 12:30:26 +00:00
$exportdata = array ();
// populate the header in first row of export
foreach ( $fields as $key => $field ) {
if ( ! in_array ( $field -> field -> id , $selectedfields )) {
// ignore values we aren't exporting
unset ( $fields [ $key ]);
} else {
$exportdata [ 0 ][] = $field -> field -> name ;
}
}
2017-10-18 15:14:41 +08:00
if ( $tags ) {
$exportdata [ 0 ][] = get_string ( 'tags' , 'data' );
}
2012-04-17 20:15:55 +01:00
if ( $userdetails ) {
$exportdata [ 0 ][] = get_string ( 'user' );
$exportdata [ 0 ][] = get_string ( 'username' );
$exportdata [ 0 ][] = get_string ( 'email' );
}
if ( $time ) {
$exportdata [ 0 ][] = get_string ( 'timeadded' , 'data' );
$exportdata [ 0 ][] = get_string ( 'timemodified' , 'data' );
}
if ( $approval ) {
$exportdata [ 0 ][] = get_string ( 'approved' , 'data' );
}
2008-08-01 12:30:26 +00:00
$datarecords = $DB -> get_records ( 'data_records' , array ( 'dataid' => $dataid ));
ksort ( $datarecords );
$line = 1 ;
foreach ( $datarecords as $record ) {
// get content indexed by fieldid
2012-01-30 15:52:20 +08:00
if ( $currentgroup ) {
$select = 'SELECT c.fieldid, c.content, c.content1, c.content2, c.content3, c.content4 FROM {data_content} c, {data_records} r WHERE c.recordid = ? AND r.id = c.recordid AND r.groupid = ?' ;
$where = array ( $record -> id , $currentgroup );
} else {
$select = 'SELECT fieldid, content, content1, content2, content3, content4 FROM {data_content} WHERE recordid = ?' ;
$where = array ( $record -> id );
}
if ( $content = $DB -> get_records_sql ( $select , $where ) ) {
2008-08-01 12:30:26 +00:00
foreach ( $fields as $field ) {
$contents = '' ;
if ( isset ( $content [ $field -> field -> id ])) {
$contents = $field -> export_text_value ( $content [ $field -> field -> id ]);
}
$exportdata [ $line ][] = $contents ;
}
2017-10-18 15:14:41 +08:00
if ( $tags ) {
$itemtags = \core_tag_tag :: get_item_tags_array ( 'mod_data' , 'data_records' , $record -> id );
$exportdata [ $line ][] = implode ( ', ' , $itemtags );
}
2012-04-17 20:15:55 +01:00
if ( $userdetails ) { // Add user details to the export data
$userdata = get_complete_user_data ( 'id' , $record -> userid );
$exportdata [ $line ][] = fullname ( $userdata );
$exportdata [ $line ][] = $userdata -> username ;
$exportdata [ $line ][] = $userdata -> email ;
}
if ( $time ) { // Add time added / modified
$exportdata [ $line ][] = userdate ( $record -> timecreated );
$exportdata [ $line ][] = userdate ( $record -> timemodified );
}
if ( $approval ) { // Add approval status
$exportdata [ $line ][] = ( int ) $record -> approved ;
}
2008-08-01 12:30:26 +00:00
}
$line ++ ;
}
$line -- ;
return $exportdata ;
}
2012-05-09 01:56:39 +02:00
////////////////////////////////////////////////////////////////////////////////
// File API //
////////////////////////////////////////////////////////////////////////////////
2008-09-14 08:22:44 +00:00
/**
* Lists all browsable file areas
2009-05-27 04:06:19 +00:00
*
2012-02-15 12:48:57 +08:00
* @ package mod_data
* @ category files
* @ param stdClass $course course object
* @ param stdClass $cm course module object
* @ param stdClass $context context object
2009-05-27 04:06:19 +00:00
* @ return array
2008-09-14 08:22:44 +00:00
*/
function data_get_file_areas ( $course , $cm , $context ) {
2012-05-09 01:56:39 +02:00
return array ( 'content' => get_string ( 'areacontent' , 'mod_data' ));
2008-09-14 08:22:44 +00:00
}
2012-02-23 12:04:10 +08:00
/**
* File browsing support for data module .
*
* @ param file_browser $browser
* @ param array $areas
* @ param stdClass $course
* @ param cm_info $cm
* @ param context $context
* @ param string $filearea
* @ param int $itemid
* @ param string $filepath
* @ param string $filename
* @ return file_info_stored file_info_stored instance or null if not found
*/
2012-06-11 13:34:47 +08:00
function data_get_file_info ( $browser , $areas , $course , $cm , $context , $filearea , $itemid , $filepath , $filename ) {
2012-05-25 11:45:08 +08:00
global $CFG , $DB , $USER ;
2012-02-23 12:04:10 +08:00
if ( $context -> contextlevel != CONTEXT_MODULE ) {
return null ;
}
2012-05-09 01:56:39 +02:00
if ( ! isset ( $areas [ $filearea ])) {
return null ;
}
2012-02-23 12:04:10 +08:00
2012-05-09 01:56:39 +02:00
if ( is_null ( $itemid )) {
require_once ( $CFG -> dirroot . '/mod/data/locallib.php' );
return new data_file_info_container ( $browser , $course , $cm , $context , $areas , $filearea );
}
2012-02-23 12:04:10 +08:00
2012-05-09 01:56:39 +02:00
if ( ! $content = $DB -> get_record ( 'data_content' , array ( 'id' => $itemid ))) {
return null ;
}
2012-02-23 12:04:10 +08:00
2012-05-09 01:56:39 +02:00
if ( ! $field = $DB -> get_record ( 'data_fields' , array ( 'id' => $content -> fieldid ))) {
return null ;
}
2012-02-23 12:04:10 +08:00
2012-05-09 01:56:39 +02:00
if ( ! $record = $DB -> get_record ( 'data_records' , array ( 'id' => $content -> recordid ))) {
return null ;
}
if ( ! $data = $DB -> get_record ( 'data' , array ( 'id' => $field -> dataid ))) {
return null ;
}
//check if approved
if ( $data -> approval and ! $record -> approved and ! data_isowner ( $record ) and ! has_capability ( 'mod/data:approve' , $context )) {
return null ;
}
// group access
if ( $record -> groupid ) {
$groupmode = groups_get_activity_groupmode ( $cm , $course );
if ( $groupmode == SEPARATEGROUPS and ! has_capability ( 'moodle/site:accessallgroups' , $context )) {
if ( ! groups_is_member ( $record -> groupid )) {
return null ;
2012-02-23 12:04:10 +08:00
}
}
2012-05-09 01:56:39 +02:00
}
2012-02-23 12:04:10 +08:00
2012-05-09 01:56:39 +02:00
$fieldobj = data_get_field ( $field , $data , $cm );
2012-02-23 12:04:10 +08:00
2012-05-09 01:56:39 +02:00
$filepath = is_null ( $filepath ) ? '/' : $filepath ;
$filename = is_null ( $filename ) ? '.' : $filename ;
if ( ! $fieldobj -> file_ok ( $filepath . $filename )) {
return null ;
}
2012-02-23 12:04:10 +08:00
2012-05-09 01:56:39 +02:00
$fs = get_file_storage ();
if ( ! ( $storedfile = $fs -> get_file ( $context -> id , 'mod_data' , $filearea , $itemid , $filepath , $filename ))) {
return null ;
2012-02-23 12:04:10 +08:00
}
2012-05-25 11:45:08 +08:00
// Checks to see if the user can manage files or is the owner.
// TODO MDL-33805 - Do not use userid here and move the capability check above.
if ( ! has_capability ( 'moodle/course:managefiles' , $context ) && $storedfile -> get_userid () != $USER -> id ) {
return null ;
}
2012-05-09 01:56:39 +02:00
$urlbase = $CFG -> wwwroot . '/pluginfile.php' ;
2012-02-23 12:04:10 +08:00
2012-05-09 01:56:39 +02:00
return new file_info_stored ( $browser , $context , $storedfile , $urlbase , $itemid , true , true , false , false );
2012-02-23 12:04:10 +08:00
}
2008-09-14 08:22:44 +00:00
/**
* Serves the data attachments . Implements needed access control ; - )
2009-05-27 04:06:19 +00:00
*
2012-02-15 12:48:57 +08:00
* @ package mod_data
* @ category files
* @ param stdClass $course course object
* @ param stdClass $cm course module object
* @ param stdClass $context context object
* @ param string $filearea file area
* @ param array $args extra arguments
* @ param bool $forcedownload whether or not force download
2012-04-18 15:56:09 +02:00
* @ param array $options additional options affecting the file serving
2009-07-03 13:40:37 +00:00
* @ return bool false if file not found , does not return if found - justsend the file
2008-09-14 08:22:44 +00:00
*/
2012-04-18 15:56:09 +02:00
function data_pluginfile ( $course , $cm , $context , $filearea , $args , $forcedownload , array $options = array ()) {
2008-09-14 08:22:44 +00:00
global $CFG , $DB ;
2010-07-03 13:37:13 +00:00
if ( $context -> contextlevel != CONTEXT_MODULE ) {
2008-09-14 08:22:44 +00:00
return false ;
}
2010-10-03 13:15:49 +00:00
require_course_login ( $course , true , $cm );
2010-07-03 13:37:13 +00:00
if ( $filearea === 'content' ) {
2008-09-14 08:22:44 +00:00
$contentid = ( int ) array_shift ( $args );
if ( ! $content = $DB -> get_record ( 'data_content' , array ( 'id' => $contentid ))) {
return false ;
}
if ( ! $field = $DB -> get_record ( 'data_fields' , array ( 'id' => $content -> fieldid ))) {
return false ;
}
if ( ! $record = $DB -> get_record ( 'data_records' , array ( 'id' => $content -> recordid ))) {
return false ;
}
if ( ! $data = $DB -> get_record ( 'data' , array ( 'id' => $field -> dataid ))) {
return false ;
}
2010-10-13 17:19:17 +00:00
if ( $data -> id != $cm -> instance ) {
// hacker attempt - context does not match the contentid
return false ;
}
2008-09-14 08:22:44 +00:00
//check if approved
2010-10-13 17:19:17 +00:00
if ( $data -> approval and ! $record -> approved and ! data_isowner ( $record ) and ! has_capability ( 'mod/data:approve' , $context )) {
2008-09-14 08:22:44 +00:00
return false ;
}
// group access
if ( $record -> groupid ) {
2009-07-19 08:56:45 +00:00
$groupmode = groups_get_activity_groupmode ( $cm , $course );
2008-09-14 08:22:44 +00:00
if ( $groupmode == SEPARATEGROUPS and ! has_capability ( 'moodle/site:accessallgroups' , $context )) {
if ( ! groups_is_member ( $record -> groupid )) {
return false ;
}
}
}
2009-07-19 08:56:45 +00:00
$fieldobj = data_get_field ( $field , $data , $cm );
2008-09-14 08:22:44 +00:00
2010-07-03 13:37:13 +00:00
$relativepath = implode ( '/' , $args );
$fullpath = " / $context->id /mod_data/content/ $content->id / $relativepath " ;
2008-09-14 08:22:44 +00:00
if ( ! $fieldobj -> file_ok ( $relativepath )) {
return false ;
}
$fs = get_file_storage ();
if ( ! $file = $fs -> get_file_by_hash ( sha1 ( $fullpath )) or $file -> is_directory ()) {
return false ;
}
// finally send the file
2012-04-18 15:56:09 +02:00
send_stored_file ( $file , 0 , 0 , true , $options ); // download MUST be forced - security!
2008-09-14 08:22:44 +00:00
}
return false ;
}
2009-09-11 03:14:42 +00:00
function data_extend_navigation ( $navigation , $course , $module , $cm ) {
global $CFG , $OUTPUT , $USER , $DB ;
2017-02-09 16:37:59 +01:00
require_once ( $CFG -> dirroot . '/mod/data/locallib.php' );
2009-09-11 03:14:42 +00:00
$rid = optional_param ( 'rid' , 0 , PARAM_INT );
$data = $DB -> get_record ( 'data' , array ( 'id' => $cm -> instance ));
$currentgroup = groups_get_activity_group ( $cm );
$groupmode = groups_get_activity_groupmode ( $cm );
2017-03-02 13:45:22 +08:00
$numentries = data_numentries ( $data );
2017-02-09 16:37:59 +01:00
$canmanageentries = has_capability ( 'mod/data:manageentries' , context_module :: instance ( $cm -> id ));
if ( $data -> entriesleft = data_get_entries_left_to_add ( $data , $numentries , $canmanageentries )) {
2010-04-19 06:30:30 +00:00
$entriesnode = $navigation -> add ( get_string ( 'entrieslefttoadd' , 'data' , $data ));
$entriesnode -> add_class ( 'note' );
2009-09-11 03:14:42 +00:00
}
2010-01-16 15:39:56 +00:00
$navigation -> add ( get_string ( 'list' , 'data' ), new moodle_url ( '/mod/data/view.php' , array ( 'd' => $cm -> instance )));
2009-09-11 03:14:42 +00:00
if ( ! empty ( $rid )) {
2010-01-16 15:39:56 +00:00
$navigation -> add ( get_string ( 'single' , 'data' ), new moodle_url ( '/mod/data/view.php' , array ( 'd' => $cm -> instance , 'rid' => $rid )));
2009-09-11 03:14:42 +00:00
} else {
2010-01-16 15:39:56 +00:00
$navigation -> add ( get_string ( 'single' , 'data' ), new moodle_url ( '/mod/data/view.php' , array ( 'd' => $cm -> instance , 'mode' => 'single' )));
2009-09-11 03:14:42 +00:00
}
2011-07-06 14:06:02 +08:00
$navigation -> add ( get_string ( 'search' , 'data' ), new moodle_url ( '/mod/data/view.php' , array ( 'd' => $cm -> instance , 'mode' => 'asearch' )));
2009-09-11 03:14:42 +00:00
}
2010-03-22 03:04:00 +00:00
/**
* Adds module specific settings to the settings block
*
* @ param settings_navigation $settings The settings navigation object
* @ param navigation_node $datanode The node to add module settings to
*/
function data_extend_settings_navigation ( settings_navigation $settings , navigation_node $datanode ) {
2022-01-19 16:06:46 +08:00
global $DB , $CFG , $USER ;
2009-12-24 02:06:05 +00:00
2022-01-19 16:06:46 +08:00
$data = $DB -> get_record ( 'data' , array ( " id " => $settings -> get_page () -> cm -> instance ));
2009-09-11 03:14:42 +00:00
2022-01-19 16:06:46 +08:00
$currentgroup = groups_get_activity_group ( $settings -> get_page () -> cm );
$groupmode = groups_get_activity_groupmode ( $settings -> get_page () -> cm );
2009-09-11 03:14:42 +00:00
2022-01-19 16:06:46 +08:00
// Took out participation list here!
if ( data_user_can_add_entry ( $data , $currentgroup , $groupmode , $settings -> get_page () -> cm -> context )) {
2010-09-18 12:46:11 +00:00
if ( empty ( $editentry )) { //TODO: undefined
2009-09-11 03:14:42 +00:00
$addstring = get_string ( 'add' , 'data' );
} else {
$addstring = get_string ( 'editentry' , 'data' );
}
2021-07-13 13:31:47 +08:00
$addentrynode = $datanode -> add ( $addstring ,
2022-01-19 16:06:46 +08:00
new moodle_url ( '/mod/data/edit.php' , array ( 'd' => $settings -> get_page () -> cm -> instance )));
2021-07-13 13:31:47 +08:00
$addentrynode -> set_show_in_secondary_navigation ( false );
2009-09-11 03:14:42 +00:00
}
2022-01-19 16:06:46 +08:00
if ( has_capability ( DATA_CAP_EXPORT , $settings -> get_page () -> cm -> context )) {
2009-09-11 03:14:42 +00:00
// The capability required to Export database records is centrally defined in 'lib.php'
// and should be weaker than those required to edit Templates, Fields and Presets.
2021-07-13 13:31:47 +08:00
$exportentriesnode = $datanode -> add ( get_string ( 'exportentries' , 'data' ),
new moodle_url ( '/mod/data/export.php' , array ( 'd' => $data -> id )));
$exportentriesnode -> set_show_in_secondary_navigation ( false );
2009-09-11 03:14:42 +00:00
}
2022-01-19 16:06:46 +08:00
if ( has_capability ( 'mod/data:manageentries' , $settings -> get_page () -> cm -> context )) {
2021-07-13 13:31:47 +08:00
$importentriesnode = $datanode -> add ( get_string ( 'importentries' , 'data' ),
new moodle_url ( '/mod/data/import.php' , array ( 'd' => $data -> id )));
$importentriesnode -> set_show_in_secondary_navigation ( false );
2010-06-04 08:21:24 +00:00
}
2009-09-11 03:14:42 +00:00
2022-01-19 16:06:46 +08:00
if ( has_capability ( 'mod/data:managetemplates' , $settings -> get_page () -> cm -> context )) {
2009-09-11 03:14:42 +00:00
$currenttab = '' ;
if ( $currenttab == 'list' ) {
$defaultemplate = 'listtemplate' ;
} else if ( $currenttab == 'add' ) {
$defaultemplate = 'addtemplate' ;
} else if ( $currenttab == 'asearch' ) {
$defaultemplate = 'asearchtemplate' ;
} else {
$defaultemplate = 'singletemplate' ;
}
2022-10-04 17:40:45 +02:00
$datanode -> add ( get_string ( 'presets' , 'data' ), new moodle_url ( '/mod/data/preset.php' , array ( 'd' => $data -> id )));
2021-07-13 13:31:47 +08:00
$datanode -> add ( get_string ( 'fields' , 'data' ),
new moodle_url ( '/mod/data/field.php' , array ( 'd' => $data -> id )));
2021-12-22 09:23:24 +08:00
$datanode -> add ( get_string ( 'templates' , 'data' ),
2021-07-13 13:31:47 +08:00
new moodle_url ( '/mod/data/templates.php' , array ( 'd' => $data -> id )));
2009-09-11 03:14:42 +00:00
}
2010-05-13 08:44:35 +00:00
if ( ! empty ( $CFG -> enablerssfeeds ) && ! empty ( $CFG -> data_enablerssfeeds ) && $data -> rssarticles > 0 ) {
require_once ( " $CFG->libdir /rsslib.php " );
2020-06-02 10:22:23 +02:00
$string = get_string ( 'rsstype' , 'data' );
2010-05-13 08:44:35 +00:00
2022-01-19 16:06:46 +08:00
$url = new moodle_url ( rss_get_url ( $settings -> get_page () -> cm -> context -> id , $USER -> id , 'mod_data' , $data -> id ));
2010-05-13 08:44:35 +00:00
$datanode -> add ( $string , $url , settings_navigation :: TYPE_SETTING , null , null , new pix_icon ( 'i/rss' , '' ));
}
MDL-21695 Database module now uses new help strings
AMOS BEGIN
HLP data/exportzip.html,[exportaszip_help,mod_data]
HLP data/fields.html,[newfield_help,mod_data]
HLP data/fieldmappings.html,[fieldmappings_help,mod_data]
HLP data/importcsv.html,[csvimport_help,mod_data]
HLP data/importfromfile.html,[fromfile_help,mod_data]
HLP data/maxentries.html,[maxentries_help,mod_data]
HLP data/requireapproval.html,[requireapproval_help,mod_data]
HLP data/requiredentries.html,[requiredentries_help,mod_data]
HLP data/requiredentriestoview.html,[requiredentriestoview_help,mod_data]
HLP data/savepreset.html,[saveaspreset_help,mod_data]
HLP data/tags.html,[availabletags_help,mod_data]
HLP data/usepreset.html,[usestandard_help,mod_data]
AMOS END
2010-04-30 14:56:41 +00:00
}
2010-08-04 08:23:52 +00:00
2010-08-25 01:22:15 +00:00
/**
* Save the database configuration as a preset .
*
* @ param stdClass $course The course the database module belongs to .
* @ param stdClass $cm The course module record
* @ param stdClass $data The database record
* @ param string $path
* @ return bool
2022-07-19 17:47:11 +02:00
* @ deprecated since Moodle 4.1 MDL - 75142 - please , use the preset :: save () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see preset :: save ()
2010-08-25 01:22:15 +00:00
*/
function data_presets_save ( $course , $cm , $data , $path ) {
2022-07-19 17:47:11 +02:00
debugging ( 'data_presets_save() is deprecated. Please use preset::save() instead.' , DEBUG_DEVELOPER );
2010-08-25 01:22:15 +00:00
2022-07-19 17:47:11 +02:00
$manager = manager :: create_from_instance ( $data );
$preset = preset :: create_from_instance ( $manager , $path );
return $preset -> save ();
2010-08-25 01:22:15 +00:00
}
2010-08-04 08:23:52 +00:00
2010-08-25 01:22:15 +00:00
/**
* Generates the XML for the database module provided
*
* @ global moodle_database $DB
* @ param stdClass $course The course the database module belongs to .
* @ param stdClass $cm The course module record
* @ param stdClass $data The database record
* @ return string The XML for the preset
2022-07-19 17:47:11 +02:00
* @ deprecated since Moodle 4.1 MDL - 75142 - please , use the protected preset :: generate_preset_xml () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see preset :: generate_preset_xml ()
2010-08-25 01:22:15 +00:00
*/
function data_presets_generate_xml ( $course , $cm , $data ) {
2022-07-19 17:47:11 +02:00
debugging (
'data_presets_generate_xml() is deprecated. Please use the protected preset::generate_preset_xml() instead.' ,
DEBUG_DEVELOPER
2010-08-04 08:23:52 +00:00
);
2022-07-19 17:47:11 +02:00
$manager = manager :: create_from_instance ( $data );
$preset = preset :: create_from_instance ( $manager , $data -> name );
$reflection = new \ReflectionClass ( preset :: class );
$method = $reflection -> getMethod ( 'generate_preset_xml' );
$method -> setAccessible ( true );
return $method -> invokeArgs ( $preset , []);
2010-08-25 01:22:15 +00:00
}
2022-07-19 17:47:11 +02:00
/**
* Export current fields and presets .
*
* @ param stdClass $course The course the database module belongs to .
* @ param stdClass $cm The course module record
* @ param stdClass $data The database record
* @ param bool $tostorage
* @ return string the full path to the exported preset file .
* @ deprecated since Moodle 4.1 MDL - 75142 - please , use the preset :: export () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see preset :: export ()
*/
2010-08-25 01:22:15 +00:00
function data_presets_export ( $course , $cm , $data , $tostorage = false ) {
2022-07-19 17:47:11 +02:00
debugging ( 'data_presets_export() is deprecated. Please use preset::export() instead.' , DEBUG_DEVELOPER );
2010-08-25 01:22:15 +00:00
2022-07-19 17:47:11 +02:00
$manager = manager :: create_from_instance ( $data );
$preset = preset :: create_from_instance ( $manager , $data -> name );
return $preset -> export ();
2010-08-25 03:50:19 +00:00
}
2011-05-04 17:23:46 +08:00
/**
* Running addtional permission check on plugin , for example , plugins
* may have switch to turn on / off comments option , this callback will
* affect UI display , not like pluginname_comment_validate only throw
* exceptions .
* Capability check has been done in comment -> check_permissions (), we
* don ' t need to do it again here .
*
2012-02-15 11:21:35 +08:00
* @ package mod_data
* @ category comment
*
2011-05-04 17:23:46 +08:00
* @ param stdClass $comment_param {
* context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid
* }
* @ return array
*/
function data_comment_permissions ( $comment_param ) {
global $CFG , $DB ;
if ( ! $record = $DB -> get_record ( 'data_records' , array ( 'id' => $comment_param -> itemid ))) {
throw new comment_exception ( 'invalidcommentitemid' );
}
if ( ! $data = $DB -> get_record ( 'data' , array ( 'id' => $record -> dataid ))) {
throw new comment_exception ( 'invalidid' , 'data' );
}
if ( $data -> comments ) {
return array ( 'post' => true , 'view' => true );
} else {
return array ( 'post' => false , 'view' => false );
}
}
/**
* Validate comment parameter before perform other comments actions
*
2012-02-15 11:21:35 +08:00
* @ package mod_data
* @ category comment
*
2011-05-04 17:23:46 +08:00
* @ param stdClass $comment_param {
* context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid
* }
* @ return boolean
*/
function data_comment_validate ( $comment_param ) {
global $DB ;
// validate comment area
if ( $comment_param -> commentarea != 'database_entry' ) {
throw new comment_exception ( 'invalidcommentarea' );
}
// validate itemid
if ( ! $record = $DB -> get_record ( 'data_records' , array ( 'id' => $comment_param -> itemid ))) {
throw new comment_exception ( 'invalidcommentitemid' );
}
if ( ! $data = $DB -> get_record ( 'data' , array ( 'id' => $record -> dataid ))) {
throw new comment_exception ( 'invalidid' , 'data' );
}
if ( ! $course = $DB -> get_record ( 'course' , array ( 'id' => $data -> course ))) {
throw new comment_exception ( 'coursemisconf' );
}
if ( ! $cm = get_coursemodule_from_instance ( 'data' , $data -> id , $course -> id )) {
throw new comment_exception ( 'invalidcoursemodule' );
}
if ( ! $data -> comments ) {
throw new comment_exception ( 'commentsoff' , 'data' );
}
2012-07-26 13:57:56 +08:00
$context = context_module :: instance ( $cm -> id );
2011-05-04 17:23:46 +08:00
//check if approved
if ( $data -> approval and ! $record -> approved and ! data_isowner ( $record ) and ! has_capability ( 'mod/data:approve' , $context )) {
2022-09-12 18:37:24 +02:00
throw new comment_exception ( 'notapprovederror' , 'data' );
2011-05-04 17:23:46 +08:00
}
// group access
if ( $record -> groupid ) {
$groupmode = groups_get_activity_groupmode ( $cm , $course );
if ( $groupmode == SEPARATEGROUPS and ! has_capability ( 'moodle/site:accessallgroups' , $context )) {
if ( ! groups_is_member ( $record -> groupid )) {
throw new comment_exception ( 'notmemberofgroup' );
}
}
}
// validate context id
if ( $context -> id != $comment_param -> context -> id ) {
throw new comment_exception ( 'invalidcontext' );
}
// validation for comment deletion
if ( ! empty ( $comment_param -> commentid )) {
if ( $comment = $DB -> get_record ( 'comments' , array ( 'id' => $comment_param -> commentid ))) {
if ( $comment -> commentarea != 'database_entry' ) {
throw new comment_exception ( 'invalidcommentarea' );
}
if ( $comment -> contextid != $comment_param -> context -> id ) {
throw new comment_exception ( 'invalidcontext' );
}
if ( $comment -> itemid != $comment_param -> itemid ) {
throw new comment_exception ( 'invalidcommentitemid' );
}
} else {
throw new comment_exception ( 'invalidcommentid' );
}
}
return true ;
}
MDL-26105 Block settings should contains less options, and be more user friendly
AMOS BEGIN
MOV [page-blog-index, pagetype], [page-blog-index, blog]
MOV [page-blog-x, pagetype], [page-blog-x, blog]
MOV [page-tag-x, pagetype], [page-tag-x, tag]
MOV [page-course-view-weeks, pagetype], [page-course-view-weeks, format_weeks]
MOV [page-course-view-weeks-x, pagetype], [page-course-view-weeks-x, format_weeks]
MOV [page-course-view-topics, pagetype], [page-course-view-topics, format_topics]
MOV [page-course-view-topics-x, pagetype], [page-course-view-topics-x, format_topics]
AMOS END
2011-04-28 11:20:30 +08:00
/**
* Return a list of page types
* @ param string $pagetype current page type
* @ param stdClass $parentcontext Block ' s parent context
* @ param stdClass $currentcontext Current context of block
*/
2011-06-17 16:23:10 +08:00
function data_page_type_list ( $pagetype , $parentcontext , $currentcontext ) {
MDL-26105 Block settings should contains less options, and be more user friendly
AMOS BEGIN
MOV [page-blog-index, pagetype], [page-blog-index, blog]
MOV [page-blog-x, pagetype], [page-blog-x, blog]
MOV [page-tag-x, pagetype], [page-tag-x, tag]
MOV [page-course-view-weeks, pagetype], [page-course-view-weeks, format_weeks]
MOV [page-course-view-weeks-x, pagetype], [page-course-view-weeks-x, format_weeks]
MOV [page-course-view-topics, pagetype], [page-course-view-topics, format_topics]
MOV [page-course-view-topics-x, pagetype], [page-course-view-topics-x, format_topics]
AMOS END
2011-04-28 11:20:30 +08:00
$module_pagetype = array ( 'mod-data-*' => get_string ( 'page-mod-data-x' , 'data' ));
return $module_pagetype ;
}
2011-12-13 10:45:48 +08:00
/**
* Get all of the record ids from a database activity .
*
2012-10-02 15:52:00 +08:00
* @ param int $dataid The dataid of the database module .
* @ param object $selectdata Contains an additional sql statement for the
* where clause for group and approval fields .
* @ param array $params Parameters that coincide with the sql statement .
* @ return array $idarray An array of record ids
2011-12-13 10:45:48 +08:00
*/
2012-10-02 15:52:00 +08:00
function data_get_all_recordids ( $dataid , $selectdata = '' , $params = null ) {
2011-12-13 10:45:48 +08:00
global $DB ;
2012-10-02 15:52:00 +08:00
$initsql = ' SELECT r . id
FROM { data_records } r
WHERE r . dataid = : dataid ' ;
if ( $selectdata != '' ) {
$initsql .= $selectdata ;
$params = array_merge ( array ( 'dataid' => $dataid ), $params );
} else {
$params = array ( 'dataid' => $dataid );
}
$initsql .= ' GROUP BY r.id' ;
$initrecord = $DB -> get_recordset_sql ( $initsql , $params );
2011-12-13 10:45:48 +08:00
$idarray = array ();
foreach ( $initrecord as $data ) {
2012-10-02 15:52:00 +08:00
$idarray [] = $data -> id ;
2011-12-13 10:45:48 +08:00
}
// Close the record set and free up resources.
$initrecord -> close ();
return $idarray ;
}
/**
* Get the ids of all the records that match that advanced search criteria
* This goes and loops through each criterion one at a time until it either
* runs out of records or returns a subset of records .
*
* @ param array $recordids An array of record ids .
2012-05-08 09:21:24 +08:00
* @ param array $searcharray Contains information for the advanced search criteria
2011-12-13 10:45:48 +08:00
* @ param int $dataid The data id of the database .
* @ return array $recordids An array of record ids .
*/
function data_get_advance_search_ids ( $recordids , $searcharray , $dataid ) {
2016-03-18 09:54:44 +08:00
// Check to see if we have any record IDs.
if ( empty ( $recordids )) {
// Send back an empty search.
return array ();
}
2011-12-13 10:45:48 +08:00
$searchcriteria = array_keys ( $searcharray );
// Loop through and reduce the IDs one search criteria at a time.
foreach ( $searchcriteria as $key ) {
$recordids = data_get_recordids ( $key , $searcharray , $dataid , $recordids );
// If we don't have anymore IDs then stop.
if ( ! $recordids ) {
break ;
}
}
return $recordids ;
}
/**
* Gets the record IDs given the search criteria
*
2012-05-08 09:21:24 +08:00
* @ param string $alias Record alias .
* @ param array $searcharray Criteria for the search .
2011-12-13 10:45:48 +08:00
* @ param int $dataid Data ID for the database
* @ param array $recordids An array of record IDs .
* @ return array $nestarray An arry of record IDs
*/
function data_get_recordids ( $alias , $searcharray , $dataid , $recordids ) {
global $DB ;
2017-02-14 11:05:30 +01:00
$searchcriteria = $alias ; // Keep the criteria.
2011-12-13 10:45:48 +08:00
$nestsearch = $searcharray [ $alias ];
// searching for content outside of mdl_data_content
if ( $alias < 0 ) {
$alias = '' ;
}
list ( $insql , $params ) = $DB -> get_in_or_equal ( $recordids , SQL_PARAMS_NAMED );
$nestselect = 'SELECT c' . $alias . ' . recordid
2017-07-31 08:45:11 +01:00
FROM { data_content } c ' . $alias . '
INNER JOIN { data_fields } f
ON f . id = c ' . $alias . ' . fieldid
INNER JOIN { data_records } r
ON r . id = c ' . $alias . ' . recordid
INNER JOIN { user } u
ON u . id = r . userid ' ;
$nestwhere = ' WHERE r . dataid = : dataid
2011-12-13 10:45:48 +08:00
AND c ' . $alias .' . recordid ' . $insql . '
AND ' ;
$params [ 'dataid' ] = $dataid ;
if ( count ( $nestsearch -> params ) != 0 ) {
$params = array_merge ( $params , $nestsearch -> params );
$nestsql = $nestselect . $nestwhere . $nestsearch -> sql ;
2017-02-14 11:05:30 +01:00
} else if ( $searchcriteria == DATA_TIMEMODIFIED ) {
$nestsql = $nestselect . $nestwhere . $nestsearch -> field . ' >= :timemodified GROUP BY c' . $alias . '.recordid' ;
$params [ 'timemodified' ] = $nestsearch -> data ;
2017-07-31 08:45:11 +01:00
} else if ( $searchcriteria == DATA_TAGS ) {
if ( empty ( $nestsearch -> rawtagnames )) {
return [];
}
$i = 0 ;
$tagwhere = [];
$tagselect = '' ;
foreach ( $nestsearch -> rawtagnames as $tagrawname ) {
2017-10-16 19:16:17 +02:00
$tagselect .= " INNER JOIN { tag_instance} ti_ $i
2017-07-31 08:45:11 +01:00
ON ti_ $i . component = 'mod_data'
AND ti_ $i . itemtype = 'data_records'
AND ti_ $i . itemid = r . id
2017-10-16 19:16:17 +02:00
INNER JOIN { tag } t_ $i
2017-07-31 08:45:11 +01:00
ON ti_ $i . tagid = t_ $i . id " ;
$tagwhere [] = " t_ $i .rawname = :trawname_ $i " ;
$params [ " trawname_ $i " ] = $tagrawname ;
$i ++ ;
}
$nestsql = $nestselect . $tagselect . $nestwhere . implode ( ' AND ' , $tagwhere );
2017-02-14 11:05:30 +01:00
} else { // First name or last name.
2011-12-13 10:45:48 +08:00
$thing = $DB -> sql_like ( $nestsearch -> field , ':search1' , false );
$nestsql = $nestselect . $nestwhere . $thing . ' GROUP BY c' . $alias . '.recordid' ;
$params [ 'search1' ] = " % $nestsearch->data % " ;
}
$nestrecords = $DB -> get_recordset_sql ( $nestsql , $params );
$nestarray = array ();
foreach ( $nestrecords as $data ) {
$nestarray [] = $data -> recordid ;
}
// Close the record set and free up resources.
$nestrecords -> close ();
return $nestarray ;
}
/**
* Returns an array with an sql string for advanced searches and the parameters that go with them .
*
* @ param int $sort DATA_ *
2012-05-08 09:21:24 +08:00
* @ param stdClass $data Data module object
2011-12-13 10:45:48 +08:00
* @ param array $recordids An array of record IDs .
2012-11-07 19:43:42 +01:00
* @ param string $selectdata Information for the where and select part of the sql statement .
2011-12-13 10:45:48 +08:00
* @ param string $sortorder Additional sort parameters
2012-05-08 09:21:24 +08:00
* @ return array sqlselect sqlselect [ 'sql' ] has the sql string , sqlselect [ 'params' ] contains an array of parameters .
2011-12-13 10:45:48 +08:00
*/
function data_get_advanced_search_sql ( $sort , $data , $recordids , $selectdata , $sortorder ) {
global $DB ;
2013-04-24 10:12:42 +08:00
2021-03-15 15:36:32 +00:00
$userfieldsapi = \core_user\fields :: for_userpic () -> excluding ( 'id' );
2020-10-12 17:51:20 +01:00
$namefields = $userfieldsapi -> get_sql ( 'u' , false , '' , '' , false ) -> selects ;
2015-01-05 09:38:17 +08:00
2011-12-13 10:45:48 +08:00
if ( $sort == 0 ) {
2013-04-24 10:12:42 +08:00
$nestselectsql = 'SELECT r.id, r.approved, r.timecreated, r.timemodified, r.userid, ' . $namefields . '
2011-12-13 10:45:48 +08:00
FROM { data_content } c ,
{ data_records } r ,
{ user } u ' ;
2013-04-24 10:12:42 +08:00
$groupsql = ' GROUP BY r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, ' . $namefields ;
2011-12-13 10:45:48 +08:00
} else {
2012-05-08 09:21:24 +08:00
// Sorting through 'Other' criteria
if ( $sort <= 0 ) {
switch ( $sort ) {
case DATA_LASTNAME :
$sortcontentfull = " u.lastname " ;
break ;
case DATA_FIRSTNAME :
$sortcontentfull = " u.firstname " ;
break ;
case DATA_APPROVED :
$sortcontentfull = " r.approved " ;
break ;
case DATA_TIMEMODIFIED :
$sortcontentfull = " r.timemodified " ;
break ;
case DATA_TIMEADDED :
default :
$sortcontentfull = " r.timecreated " ;
}
} else {
$sortfield = data_get_field_from_id ( $sort , $data );
$sortcontent = $DB -> sql_compare_text ( 'c.' . $sortfield -> get_sort_field ());
$sortcontentfull = $sortfield -> get_sort_sql ( $sortcontent );
}
2013-04-24 10:12:42 +08:00
$nestselectsql = 'SELECT r.id, r.approved, r.timecreated, r.timemodified, r.userid, ' . $namefields . ' ,
' . $sortcontentfull . '
2012-05-18 16:39:53 +08:00
AS sortorder
2011-12-13 10:45:48 +08:00
FROM { data_content } c ,
{ data_records } r ,
{ user } u ' ;
2013-04-24 10:12:42 +08:00
$groupsql = ' GROUP BY r.id, r.approved, r.timecreated, r.timemodified, r.userid, ' . $namefields . ', ' . $sortcontentfull ;
2011-12-13 10:45:48 +08:00
}
2012-11-07 19:43:42 +01:00
// Default to a standard Where statement if $selectdata is empty.
if ( $selectdata == '' ) {
$selectdata = ' WHERE c . recordid = r . id
AND r . dataid = : dataid
AND r . userid = u . id ' ;
}
2011-12-13 10:45:48 +08:00
// Find the field we are sorting on
if ( $sort > 0 or data_get_field_from_id ( $sort , $data )) {
2022-07-28 14:57:22 -04:00
$selectdata .= ' AND c.fieldid = :sort AND s.recordid = r.id' ;
$nestselectsql .= ',{data_content} s ' ;
2011-12-13 10:45:48 +08:00
}
// If there are no record IDs then return an sql statment that will return no rows.
if ( count ( $recordids ) != 0 ) {
list ( $insql , $inparam ) = $DB -> get_in_or_equal ( $recordids , SQL_PARAMS_NAMED );
} else {
list ( $insql , $inparam ) = $DB -> get_in_or_equal ( array ( '-1' ), SQL_PARAMS_NAMED );
}
2012-11-07 19:43:42 +01:00
$nestfromsql = $selectdata . ' AND c.recordid ' . $insql . $groupsql ;
2011-12-13 10:45:48 +08:00
$sqlselect [ 'sql' ] = " $nestselectsql $nestfromsql $sortorder " ;
$sqlselect [ 'params' ] = $inparam ;
return $sqlselect ;
2011-09-16 11:44:14 +08:00
}
2012-05-02 09:03:22 +08:00
/**
* Checks to see if the user has permission to delete the preset .
* @ param stdClass $context Context object .
* @ param stdClass $preset The preset object that we are checking for deletion .
* @ return bool Returns true if the user can delete , otherwise false .
2022-07-20 16:54:43 +02:00
* @ deprecated since Moodle 4.1 MDL - 75187 - please , use the preset :: can_manage () function instead .
* @ todo MDL - 75189 This will be deleted in Moodle 4.5 .
* @ see preset :: can_manage ()
2012-05-02 09:03:22 +08:00
*/
function data_user_can_delete_preset ( $context , $preset ) {
global $USER ;
2022-07-20 16:54:43 +02:00
debugging ( 'data_user_can_delete_preset() is deprecated. Please use manager::can_manage() instead.' , DEBUG_DEVELOPER );
if ( $context -> contextlevel == CONTEXT_MODULE && isset ( $preset -> name )) {
$cm = get_coursemodule_from_id ( '' , $context -> instanceid , 0 , false , MUST_EXIST );
$manager = manager :: create_from_coursemodule ( $cm );
$todelete = preset :: create_from_instance ( $manager , $preset -> name );
return $todelete -> can_manage ();
}
2012-05-02 09:03:22 +08:00
if ( has_capability ( 'mod/data:manageuserpresets' , $context )) {
return true ;
} else {
$candelete = false ;
2022-07-19 17:47:11 +02:00
$userid = $preset instanceof preset ? $preset -> get_userid () : $preset -> userid ;
if ( $userid == $USER -> id ) {
2012-05-02 09:03:22 +08:00
$candelete = true ;
}
return $candelete ;
}
2012-05-10 14:11:24 +12:00
}
2013-07-22 16:19:08 +08:00
/**
* Delete a record entry .
*
* @ param int $recordid The ID for the record to be deleted .
* @ param object $data The data object for this activity .
* @ param int $courseid ID for the current course ( for logging ) .
* @ param int $cmid The course module ID .
* @ return bool True if the record deleted , false if not .
*/
function data_delete_record ( $recordid , $data , $courseid , $cmid ) {
2014-06-20 12:58:48 +08:00
global $DB , $CFG ;
2013-07-22 16:19:08 +08:00
if ( $deleterecord = $DB -> get_record ( 'data_records' , array ( 'id' => $recordid ))) {
if ( $deleterecord -> dataid == $data -> id ) {
if ( $contents = $DB -> get_records ( 'data_content' , array ( 'recordid' => $deleterecord -> id ))) {
foreach ( $contents as $content ) {
if ( $field = data_get_field_from_id ( $content -> fieldid , $data )) {
$field -> delete_content ( $content -> recordid );
}
}
$DB -> delete_records ( 'data_content' , array ( 'recordid' => $deleterecord -> id ));
$DB -> delete_records ( 'data_records' , array ( 'id' => $deleterecord -> id ));
2014-06-20 12:58:48 +08:00
// Delete cached RSS feeds.
if ( ! empty ( $CFG -> enablerssfeeds )) {
require_once ( $CFG -> dirroot . '/mod/data/rsslib.php' );
data_rss_delete_file ( $data );
}
2017-09-14 16:57:00 +08:00
core_tag_tag :: remove_all_item_tags ( 'mod_data' , 'data_records' , $recordid );
2014-02-14 18:09:27 -08:00
// Trigger an event for deleting this record.
$event = \mod_data\event\record_deleted :: create ( array (
'objectid' => $deleterecord -> id ,
'context' => context_module :: instance ( $cmid ),
'courseid' => $courseid ,
'other' => array (
'dataid' => $deleterecord -> dataid
)
));
$event -> add_record_snapshot ( 'data_records' , $deleterecord );
$event -> trigger ();
2017-03-19 16:41:30 +00:00
$course = get_course ( $courseid );
$cm = get_coursemodule_from_instance ( 'data' , $data -> id , 0 , false , MUST_EXIST );
2017-04-03 10:44:32 +08:00
data_update_completion_state ( $data , $course , $cm );
2014-02-14 18:09:27 -08:00
2013-07-22 16:19:08 +08:00
return true ;
}
}
}
2017-03-19 16:41:30 +00:00
2013-07-22 16:19:08 +08:00
return false ;
}
2012-07-11 13:01:30 +08:00
/**
* Check for required fields , and build a list of fields to be updated in a
* submission .
*
* @ param $mod stdClass The current recordid - provided as an optimisation .
* @ param $fields array The field data
* @ param $datarecord stdClass The submitted data .
* @ return stdClass containing :
* * string [] generalnotifications Notifications for the form as a whole .
* * string [] fieldnotifications Notifications for a specific field .
* * bool validated Whether the field was validated successfully .
* * data_field_base [] fields The field objects to be update .
*/
function data_process_submission ( stdClass $mod , $fields , stdClass $datarecord ) {
$result = new stdClass ();
// Empty form checking - you can't submit an empty form.
$emptyform = true ;
$requiredfieldsfilled = true ;
2015-09-14 10:03:11 +08:00
$fieldsvalidated = true ;
2012-07-11 13:01:30 +08:00
// Store the notifications.
$result -> generalnotifications = array ();
$result -> fieldnotifications = array ();
// Store the instantiated classes as an optimisation when processing the result.
// This prevents the fields being re-initialised when updating.
$result -> fields = array ();
$submitteddata = array ();
foreach ( $datarecord as $fieldname => $fieldvalue ) {
if ( strpos ( $fieldname , '_' )) {
$namearray = explode ( '_' , $fieldname , 3 );
$fieldid = $namearray [ 1 ];
if ( ! isset ( $submitteddata [ $fieldid ])) {
$submitteddata [ $fieldid ] = array ();
}
if ( count ( $namearray ) === 2 ) {
$subfieldid = 0 ;
} else {
$subfieldid = $namearray [ 2 ];
}
$fielddata = new stdClass ();
$fielddata -> fieldname = $fieldname ;
$fielddata -> value = $fieldvalue ;
$submitteddata [ $fieldid ][ $subfieldid ] = $fielddata ;
}
}
// Check all form fields which have the required are filled.
foreach ( $fields as $fieldrecord ) {
// Check whether the field has any data.
$fieldhascontent = false ;
$field = data_get_field ( $fieldrecord , $mod );
if ( isset ( $submitteddata [ $fieldrecord -> id ])) {
2015-09-14 10:03:11 +08:00
// Field validation check.
if ( method_exists ( $field , 'field_validation' )) {
$errormessage = $field -> field_validation ( $submitteddata [ $fieldrecord -> id ]);
if ( $errormessage ) {
$result -> fieldnotifications [ $field -> field -> name ][] = $errormessage ;
$fieldsvalidated = false ;
}
}
2012-07-11 13:01:30 +08:00
foreach ( $submitteddata [ $fieldrecord -> id ] as $fieldname => $value ) {
if ( $field -> notemptyfield ( $value -> value , $value -> fieldname )) {
// The field has content and the form is not empty.
$fieldhascontent = true ;
$emptyform = false ;
}
}
}
// If the field is required, add a notification to that effect.
if ( $field -> field -> required && ! $fieldhascontent ) {
if ( ! isset ( $result -> fieldnotifications [ $field -> field -> name ])) {
$result -> fieldnotifications [ $field -> field -> name ] = array ();
}
2015-02-17 13:30:45 +08:00
$result -> fieldnotifications [ $field -> field -> name ][] = get_string ( 'errormustsupplyvalue' , 'data' );
2012-07-11 13:01:30 +08:00
$requiredfieldsfilled = false ;
}
2015-04-03 17:34:49 +08:00
// Update the field.
2015-04-08 13:10:09 +08:00
if ( isset ( $submitteddata [ $fieldrecord -> id ])) {
foreach ( $submitteddata [ $fieldrecord -> id ] as $value ) {
$result -> fields [ $value -> fieldname ] = $field ;
}
2012-07-11 13:01:30 +08:00
}
}
if ( $emptyform ) {
// The form is empty.
2015-02-17 13:30:45 +08:00
$result -> generalnotifications [] = get_string ( 'emptyaddform' , 'data' );
2012-07-11 13:01:30 +08:00
}
2015-09-14 10:03:11 +08:00
$result -> validated = $requiredfieldsfilled && ! $emptyform && $fieldsvalidated ;
2012-07-11 13:01:30 +08:00
return $result ;
}
2016-07-18 18:35:37 -04:00
/**
* This standard function will check all instances of this module
* and make sure there are up - to - date events created for each of them .
* If courseid = 0 , then every data event in the site is checked , else
* only data events belonging to the course specified are checked .
* This function is used , in its new format , by restore_refresh_events ()
*
* @ param int $courseid
2017-07-03 12:04:58 +08:00
* @ param int | stdClass $instance Data module instance or ID .
* @ param int | stdClass $cm Course module object or ID ( not used in this module ) .
2016-07-18 18:35:37 -04:00
* @ return bool
*/
2017-07-03 12:04:58 +08:00
function data_refresh_events ( $courseid = 0 , $instance = null , $cm = null ) {
2016-07-18 18:35:37 -04:00
global $DB , $CFG ;
require_once ( $CFG -> dirroot . '/mod/data/locallib.php' );
2017-07-03 12:04:58 +08:00
// If we have instance information then we can just update the one event instead of updating all events.
if ( isset ( $instance )) {
if ( ! is_object ( $instance )) {
$instance = $DB -> get_record ( 'data' , array ( 'id' => $instance ), '*' , MUST_EXIST );
}
data_set_events ( $instance );
return true ;
}
2016-07-18 18:35:37 -04:00
if ( $courseid ) {
if ( ! $data = $DB -> get_records ( " data " , array ( " course " => $courseid ))) {
return true ;
}
} else {
if ( ! $data = $DB -> get_records ( " data " )) {
return true ;
}
}
foreach ( $data as $datum ) {
data_set_events ( $datum );
}
return true ;
}
2016-09-06 12:37:44 +08:00
/**
* Fetch the configuration for this database activity .
*
* @ param stdClass $database The object returned from the database for this instance
* @ param string $key The name of the key to retrieve . If none is supplied , then all configuration is returned
* @ param mixed $default The default value to use if no value was found for the specified key
* @ return mixed The returned value
*/
function data_get_config ( $database , $key = null , $default = null ) {
if ( ! empty ( $database -> config )) {
$config = json_decode ( $database -> config );
} else {
$config = new stdClass ();
}
if ( $key === null ) {
return $config ;
}
if ( property_exists ( $config , $key )) {
return $config -> $key ;
}
return $default ;
}
/**
* Update the configuration for this database activity .
*
* @ param stdClass $database The object returned from the database for this instance
* @ param string $key The name of the key to set
* @ param mixed $value The value to set for the key
*/
function data_set_config ( & $database , $key , $value ) {
// Note: We must pass $database by reference because there may be subsequent calls to update_record and these should
// not overwrite the configuration just set.
global $DB ;
$config = data_get_config ( $database );
if ( ! isset ( $config -> $key ) || $config -> $key !== $value ) {
$config -> $key = $value ;
$database -> config = json_encode ( $config );
$DB -> set_field ( 'data' , 'config' , $database -> config , [ 'id' => $database -> id ]);
}
}
2017-03-19 16:41:30 +00:00
/**
* Sets the automatic completion state for this database item based on the
* count of on its entries .
* @ since Moodle 3.3
2017-04-03 10:44:32 +08:00
* @ param object $data The data object for this activity
2017-03-19 16:41:30 +00:00
* @ param object $course Course
* @ param object $cm course - module
*/
2017-04-03 10:44:32 +08:00
function data_update_completion_state ( $data , $course , $cm ) {
2017-03-19 16:41:30 +00:00
// If completion option is enabled, evaluate it and return true/false.
$completion = new completion_info ( $course );
if ( $data -> completionentries && $completion -> is_enabled ( $cm )) {
$numentries = data_numentries ( $data );
// Check the number of entries required against the number of entries already made.
2017-04-03 10:44:32 +08:00
if ( $numentries >= $data -> completionentries ) {
2017-03-19 16:41:30 +00:00
$completion -> update_state ( $cm , COMPLETION_COMPLETE );
} else {
2017-04-03 10:44:32 +08:00
$completion -> update_state ( $cm , COMPLETION_INCOMPLETE );
2017-03-19 16:41:30 +00:00
}
}
}
2017-02-09 15:04:17 +01:00
/**
* Mark the activity completed ( if required ) and trigger the course_module_viewed event .
*
2022-07-07 16:12:46 +02:00
* @ deprecated since Moodle 4.1 MDL - 75146 - please do not use this function any more .
* @ todo MDL - 75189 Final deprecation in Moodle 4.5 .
2017-02-09 15:04:17 +01:00
* @ param stdClass $data data object
* @ param stdClass $course course object
* @ param stdClass $cm course module object
* @ param stdClass $context context object
* @ since Moodle 3.3
*/
function data_view ( $data , $course , $cm , $context ) {
global $CFG ;
2022-07-07 16:12:46 +02:00
debugging ( 'data_view is deprecated. Use mod_data\\manager::set_module_viewed instead' , DEBUG_DEVELOPER );
2017-02-09 15:04:17 +01:00
require_once ( $CFG -> libdir . '/completionlib.php' );
// Trigger course_module_viewed event.
$params = array (
'context' => $context ,
'objectid' => $data -> id
);
$event = \mod_data\event\course_module_viewed :: create ( $params );
$event -> add_record_snapshot ( 'course_modules' , $cm );
$event -> add_record_snapshot ( 'course' , $course );
$event -> add_record_snapshot ( 'data' , $data );
$event -> trigger ();
// Completion.
$completion = new completion_info ( $course );
$completion -> set_module_viewed ( $cm );
}
2017-03-01 14:09:40 +08:00
/**
* Get icon mapping for font - awesome .
*/
function mod_data_get_fontawesome_icon_map () {
return [
'mod_data:field/checkbox' => 'fa-check-square-o' ,
'mod_data:field/date' => 'fa-calendar-o' ,
'mod_data:field/file' => 'fa-file' ,
'mod_data:field/latlong' => 'fa-globe' ,
'mod_data:field/menu' => 'fa-bars' ,
'mod_data:field/multimenu' => 'fa-bars' ,
'mod_data:field/number' => 'fa-hashtag' ,
'mod_data:field/picture' => 'fa-picture-o' ,
'mod_data:field/radiobutton' => 'fa-circle-o' ,
'mod_data:field/textarea' => 'fa-font' ,
'mod_data:field/text' => 'fa-i-cursor' ,
'mod_data:field/url' => 'fa-link' ,
];
}
2017-03-24 10:22:04 +01:00
/*
* Check if the module has any update that affects the current user since a given time .
*
* @ param cm_info $cm course module data
* @ param int $from the time to check updates from
* @ param array $filter if we need to check only specific updates
* @ return stdClass an object with the different type of areas indicating if they were updated or not
* @ since Moodle 3.2
*/
function data_check_updates_since ( cm_info $cm , $from , $filter = array ()) {
global $DB , $CFG ;
require_once ( $CFG -> dirroot . '/mod/data/locallib.php' );
$updates = course_check_module_updates_since ( $cm , $from , array (), $filter );
// Check for new entries.
$updates -> entries = ( object ) array ( 'updated' => false );
$data = $DB -> get_record ( 'data' , array ( 'id' => $cm -> instance ), '*' , MUST_EXIST );
$searcharray = [];
$searcharray [ DATA_TIMEMODIFIED ] = new stdClass ();
$searcharray [ DATA_TIMEMODIFIED ] -> sql = '' ;
$searcharray [ DATA_TIMEMODIFIED ] -> params = array ();
$searcharray [ DATA_TIMEMODIFIED ] -> field = 'r.timemodified' ;
$searcharray [ DATA_TIMEMODIFIED ] -> data = $from ;
$currentgroup = groups_get_activity_group ( $cm );
2017-03-30 16:16:26 +02:00
// Teachers should retrieve all entries when not in separate groups.
if ( has_capability ( 'mod/data:manageentries' , $cm -> context ) && groups_get_activity_groupmode ( $cm ) != SEPARATEGROUPS ) {
$currentgroup = 0 ;
}
2017-03-24 10:22:04 +01:00
list ( $entries , $maxcount , $totalcount , $page , $nowperpage , $sort , $mode ) =
data_search_entries ( $data , $cm , $cm -> context , 'list' , $currentgroup , '' , null , null , 0 , 0 , true , $searcharray );
if ( ! empty ( $entries )) {
$updates -> entries -> updated = true ;
$updates -> entries -> itemids = array_keys ( $entries );
}
return $updates ;
}
2017-03-02 13:45:22 +08:00
/**
2017-04-06 14:56:35 +08:00
* This function receives a calendar event and returns the action associated with it , or null if there is none .
*
* This is used by block_myoverview in order to display the event appropriately . If null is returned then the event
* is not displayed on the block .
2017-03-02 13:45:22 +08:00
*
2017-03-29 13:33:11 +08:00
* @ param calendar_event $event
2017-03-02 13:45:22 +08:00
* @ param \core_calendar\action_factory $factory
2018-08-20 15:10:16 +10:00
* @ param int $userid User id to use for all capability checks , etc . Set to 0 for current user ( default ) .
2017-04-05 15:15:09 +08:00
* @ return \core_calendar\local\event\entities\action_interface | null
2017-03-02 13:45:22 +08:00
*/
2017-03-29 13:33:11 +08:00
function mod_data_core_calendar_provide_event_action ( calendar_event $event ,
2018-08-20 15:10:16 +10:00
\core_calendar\action_factory $factory ,
int $userid = 0 ) {
global $USER ;
if ( ! $userid ) {
$userid = $USER -> id ;
}
2017-03-02 13:45:22 +08:00
2018-08-20 15:10:16 +10:00
$cm = get_fast_modinfo ( $event -> courseid , $userid ) -> instances [ 'data' ][ $event -> instance ];
2018-09-06 14:19:43 +10:00
if ( ! $cm -> uservisible ) {
// The module is not visible to the user for any reason.
return null ;
}
2017-04-28 10:35:14 +08:00
$now = time ();
if ( ! empty ( $cm -> customdata [ 'timeavailableto' ]) && $cm -> customdata [ 'timeavailableto' ] < $now ) {
// The module has closed so the user can no longer submit anything.
return null ;
2017-03-02 13:45:22 +08:00
}
2017-04-28 10:35:14 +08:00
// The module is actionable if we don't have a start time or the start time is
// in the past.
$actionable = ( empty ( $cm -> customdata [ 'timeavailablefrom' ]) || $cm -> customdata [ 'timeavailablefrom' ] <= $now );
2017-03-02 13:45:22 +08:00
return $factory -> create_instance (
get_string ( 'add' , 'data' ),
new \moodle_url ( '/mod/data/view.php' , array ( 'id' => $cm -> id )),
1 ,
$actionable
);
}
2017-04-03 08:56:27 +08:00
/**
* Add a get_coursemodule_info function in case any database type wants to add 'extra' information
* for the course ( see resource ) .
*
* Given a course_module object , this function returns any " extra " information that may be needed
* when printing this activity in a course listing . See get_array_of_activities () in course / lib . php .
*
* @ param stdClass $coursemodule The coursemodule object ( record ) .
* @ return cached_cm_info An object on information that the courses
* will know about ( most noticeably , an icon ) .
*/
function data_get_coursemodule_info ( $coursemodule ) {
global $DB ;
$dbparams = [ 'id' => $coursemodule -> instance ];
2017-04-28 10:35:14 +08:00
$fields = 'id, name, intro, introformat, completionentries, timeavailablefrom, timeavailableto' ;
2017-04-03 08:56:27 +08:00
if ( ! $data = $DB -> get_record ( 'data' , $dbparams , $fields )) {
return false ;
}
$result = new cached_cm_info ();
2017-04-21 13:00:28 +08:00
$result -> name = $data -> name ;
2017-04-03 08:56:27 +08:00
2017-04-27 11:21:01 +08:00
if ( $coursemodule -> showdescription ) {
// Convert intro to html. Do not filter cached version, filters run at display time.
$result -> content = format_module_intro ( 'data' , $data , $coursemodule -> id , false );
}
2017-04-03 08:56:27 +08:00
// Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
if ( $coursemodule -> completion == COMPLETION_TRACKING_AUTOMATIC ) {
$result -> customdata [ 'customcompletionrules' ][ 'completionentries' ] = $data -> completionentries ;
}
2017-04-28 10:35:14 +08:00
// Other properties that may be used in calendar or on dashboard.
if ( $data -> timeavailablefrom ) {
$result -> customdata [ 'timeavailablefrom' ] = $data -> timeavailablefrom ;
}
if ( $data -> timeavailableto ) {
$result -> customdata [ 'timeavailableto' ] = $data -> timeavailableto ;
}
2017-04-03 08:56:27 +08:00
return $result ;
}
/**
* Callback which returns human - readable strings describing the active completion custom rules for the module instance .
*
* @ param cm_info | stdClass $cm object with fields -> completion and -> customdata [ 'customcompletionrules' ]
* @ return array $descriptions the array of descriptions for the custom rules .
*/
function mod_data_get_completion_active_rule_descriptions ( $cm ) {
// Values will be present in cm_info, and we assume these are up to date.
if ( empty ( $cm -> customdata [ 'customcompletionrules' ])
|| $cm -> completion != COMPLETION_TRACKING_AUTOMATIC ) {
return [];
}
$descriptions = [];
foreach ( $cm -> customdata [ 'customcompletionrules' ] as $key => $val ) {
switch ( $key ) {
case 'completionentries' :
2018-10-07 23:25:45 +02:00
if ( ! empty ( $val )) {
$descriptions [] = get_string ( 'completionentriesdesc' , 'data' , $val );
2017-04-03 08:56:27 +08:00
}
break ;
default :
break ;
}
}
return $descriptions ;
}
2017-11-28 14:14:04 +08:00
/**
* This function calculates the minimum and maximum cutoff values for the timestart of
* the given event .
*
* It will return an array with two values , the first being the minimum cutoff value and
* the second being the maximum cutoff value . Either or both values can be null , which
* indicates there is no minimum or maximum , respectively .
*
* If a cutoff is required then the function must return an array containing the cutoff
* timestamp and error string to display to the user if the cutoff value is violated .
*
* A minimum and maximum cutoff return value will look like :
* [
* [ 1505704373 , 'The due date must be after the sbumission start date' ],
* [ 1506741172 , 'The due date must be before the cutoff date' ]
* ]
*
* @ param calendar_event $event The calendar event to get the time range for
* @ param stdClass $instance The module instance to get the range from
* @ return array
*/
function mod_data_core_calendar_get_valid_event_timestart_range ( \calendar_event $event , \stdClass $instance ) {
$mindate = null ;
$maxdate = null ;
if ( $event -> eventtype == DATA_EVENT_TYPE_OPEN ) {
// The start time of the open event can't be equal to or after the
// close time of the database activity.
if ( ! empty ( $instance -> timeavailableto )) {
$maxdate = [
$instance -> timeavailableto ,
get_string ( 'openafterclose' , 'data' )
];
}
} else if ( $event -> eventtype == DATA_EVENT_TYPE_CLOSE ) {
// The start time of the close event can't be equal to or earlier than the
// open time of the database activity.
if ( ! empty ( $instance -> timeavailablefrom )) {
$mindate = [
$instance -> timeavailablefrom ,
get_string ( 'closebeforeopen' , 'data' )
];
}
}
return [ $mindate , $maxdate ];
}
/**
* This function will update the data module according to the
* event that has been modified .
*
* It will set the timeopen or timeclose value of the data instance
* according to the type of event provided .
*
* @ throws \moodle_exception
* @ param \calendar_event $event
* @ param stdClass $data The module instance to get the range from
*/
function mod_data_core_calendar_event_timestart_updated ( \calendar_event $event , \stdClass $data ) {
global $DB ;
if ( empty ( $event -> instance ) || $event -> modulename != 'data' ) {
return ;
}
if ( $event -> instance != $data -> id ) {
return ;
}
if ( ! in_array ( $event -> eventtype , [ DATA_EVENT_TYPE_OPEN , DATA_EVENT_TYPE_CLOSE ])) {
return ;
}
$courseid = $event -> courseid ;
$modulename = $event -> modulename ;
$instanceid = $event -> instance ;
$modified = false ;
$coursemodule = get_fast_modinfo ( $courseid ) -> instances [ $modulename ][ $instanceid ];
$context = context_module :: instance ( $coursemodule -> id );
// The user does not have the capability to modify this activity.
if ( ! has_capability ( 'moodle/course:manageactivities' , $context )) {
return ;
}
if ( $event -> eventtype == DATA_EVENT_TYPE_OPEN ) {
// If the event is for the data activity opening then we should
// set the start time of the data activity to be the new start
// time of the event.
if ( $data -> timeavailablefrom != $event -> timestart ) {
$data -> timeavailablefrom = $event -> timestart ;
$data -> timemodified = time ();
$modified = true ;
}
} else if ( $event -> eventtype == DATA_EVENT_TYPE_CLOSE ) {
// If the event is for the data activity closing then we should
// set the end time of the data activity to be the new start
// time of the event.
if ( $data -> timeavailableto != $event -> timestart ) {
$data -> timeavailableto = $event -> timestart ;
$modified = true ;
}
}
if ( $modified ) {
$data -> timemodified = time ();
$DB -> update_record ( 'data' , $data );
$event = \core\event\course_module_updated :: create_from_cm ( $coursemodule , $context );
$event -> trigger ();
}
}
2021-09-16 07:03:43 +08:00
/**
* Callback to fetch the activity event type lang string .
*
* @ param string $eventtype The event type .
* @ return lang_string The event type lang string .
*/
function mod_data_core_calendar_get_event_action_string ( string $eventtype ) : string {
$modulename = get_string ( 'modulename' , 'data' );
switch ( $eventtype ) {
case DATA_EVENT_TYPE_OPEN :
$identifier = 'calendarstart' ;
break ;
case DATA_EVENT_TYPE_CLOSE :
$identifier = 'calendarend' ;
break ;
default :
return get_string ( 'requiresaction' , 'calendar' , $modulename );
}
return get_string ( $identifier , 'data' , $modulename );
}