<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Surya Pratap</title>
    <description>The latest articles on DEV Community by Surya Pratap (@suryapratap_bit).</description>
    <link>https://dev.to/suryapratap_bit</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F873082%2F525fc957-7a88-4cf6-8db1-0efd6b701b44.jpg</url>
      <title>DEV Community: Surya Pratap</title>
      <link>https://dev.to/suryapratap_bit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suryapratap_bit"/>
    <language>en</language>
    <item>
      <title>Enrol student in a course programmatically.</title>
      <dc:creator>Surya Pratap</dc:creator>
      <pubDate>Thu, 30 Jun 2022 17:06:59 +0000</pubDate>
      <link>https://dev.to/suryapratap_bit/enrol-student-in-a-course-programmatically-2heo</link>
      <guid>https://dev.to/suryapratap_bit/enrol-student-in-a-course-programmatically-2heo</guid>
      <description>&lt;p&gt;Hello there,&lt;/p&gt;

&lt;p&gt;I am back with an easiest way to enrol a student in a course programmatically. Just five line of code to get it done.&lt;br&gt;
Try the below code:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step-1: Must include the below code at the top of your file.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require_once($CFG-&amp;gt;dirroot . '/lib/enrollib.php');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step-2: Put the &lt;code&gt;enrol_user&lt;/code&gt; function in your PHP script.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function enrol_user($courseid, $userid){
    global $DB;
    // Enrol the user in the course.
    $studentrole = $DB-&amp;gt;get_record('role', array('shortname' =&amp;gt; 'student'));
    $maninstance1 = $DB-&amp;gt;get_record('enrol', array('courseid' =&amp;gt; $courseid, 'enrol' =&amp;gt; 'manual'), '*', MUST_EXIST);
    $manual = enrol_get_plugin('manual');
    $manual-&amp;gt;enrol_user($maninstance1, $userid, $studentrole-&amp;gt;id);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Step-3: Call the enrol_user function.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enrol_user($courseid, $userid)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy learning!&lt;/p&gt;

</description>
      <category>moodle</category>
      <category>php</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to create and install moodle local plugin</title>
      <dc:creator>Surya Pratap</dc:creator>
      <pubDate>Mon, 06 Jun 2022 10:26:12 +0000</pubDate>
      <link>https://dev.to/suryapratap_bit/how-to-create-and-install-moodle-local-plugin-1c81</link>
      <guid>https://dev.to/suryapratap_bit/how-to-create-and-install-moodle-local-plugin-1c81</guid>
      <description>&lt;p&gt;&lt;strong&gt;Moodle Local Plugin:&lt;/strong&gt; &lt;em&gt;Local plugins are used in cases when no standard plugin fits the requirement, here are a few examples:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Event consumers communicating with external systems.&lt;/li&gt;
&lt;li&gt;Custom definitions of web services and external functions.&lt;/li&gt;
&lt;li&gt;Applications that extend moodle at the system level (hub server, amos server, etc.).&lt;/li&gt;
&lt;li&gt;New database tables used in core hacks (discouraged).&lt;/li&gt;
&lt;li&gt;New capability definitions used in core hacks.&lt;/li&gt;
&lt;li&gt;Custom admin settings.&lt;/li&gt;
&lt;li&gt;Extending the navigation block with custom menus.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;How to create moodle local plugins?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To create local plugin you can follow following folder structure according to Moodle documentation (&lt;a href="https://docs.moodle.org/dev/Local_plugins"&gt;https://docs.moodle.org/dev/Local_plugins&lt;/a&gt;)&lt;br&gt;
local/&lt;br&gt;
     yourplugin/&lt;br&gt;
       db/&lt;br&gt;
         access.php&lt;br&gt;
       lang/&lt;br&gt;
         en/&lt;br&gt;
           yourplugin.php&lt;br&gt;
       settings.php&lt;br&gt;
       version.php&lt;/p&gt;

&lt;p&gt;Now, create all the files and put some necessary code into it.&lt;/p&gt;

&lt;p&gt;1.Open &lt;strong&gt;version.php&lt;/strong&gt; file and put below code into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php 
/**
 * Version details.
 *
 * @package   local_yourplugin
 * @copyright 2014 Surya Pratap
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

$plugin-&amp;gt;version   = 2014061101;      // The current module version (Date: YYYYMMDDXX).
$plugin-&amp;gt;requires  = 2014050800;      // Requires this Moodle version.
$plugin-&amp;gt;component = 'local_yourplugin';// Full name of the plugin (used for diagnostics).
$plugin-&amp;gt;cron      = 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Open the &lt;strong&gt;settings.php&lt;/strong&gt; file and put the below code into it.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
/**
 * You may have settings in your plugin
 *
 * @package    local_yourplugin
 * @copyright  2014 Surya Pratap
 * @license    http://www.gnu.org/copyleft/gpl.html gnu gpl v3 or later
 */

defined('MOODLE_INTERNAL') || die();

if ($ADMIN-&amp;gt;fulltree) {
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Open &lt;strong&gt;access.php&lt;/strong&gt; file under 'db' directory and put the below code into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;defined('MOODLE_INTERNAL') || die();

$capabilities = array(
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Open &lt;strong&gt;local_yourplugin.php&lt;/strong&gt; file under 'lang/en/' directory and put the below code into it.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
/**
 * You may localized strings in your plugin
 *
 * @package    local_yourplugin
 * @copyright  2014 Surya Pratap
 * @license    http://www.gnu.org/copyleft/gpl.html gnu gpl v3 or later
 */

$string['pluginname'] = 'New local plugin';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The necessary code has been inserted into all the files, now zip these files and start the installation process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing via uploaded ZIP file:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;log in to your Moodle site as an admin and go to Administration &amp;gt; Site administration &amp;gt; Plugins &amp;gt; Install plugins.&lt;/li&gt;
&lt;li&gt;Upload the ZIP file. You should only be prompted to add extra details (in the Show more section) if your plugin is not automatically detected.&lt;/li&gt;
&lt;li&gt;If your target directory is not writeable, you will see a warning message as "Check the plugin validation report" then grant the writer access to the directory and reload the page and continue the installation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Installing via command line:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;log in to your server/machine&lt;/li&gt;
&lt;li&gt;Go to moodle root directory and place the code in the respective directory. In our case, we have to put our code in the local directory.&lt;/li&gt;
&lt;li&gt;Run &lt;strong&gt;php admin/cli/upgrade.php&lt;/strong&gt; then a message prompt message will be printed on the terminal. Read the instruction and follow the process as suggested. In my case Here is the message:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;"== Upgrading Moodle database from version 3.4.4 (Build: 20180709) (2017111304) to 3.4.4 (Build: 20180709) (2017111304) ==&lt;/p&gt;

&lt;p&gt;Your Moodle files have been changed, and you are about to automatically&lt;br&gt;
upgrade your server to this version:&lt;/p&gt;

&lt;p&gt;3.4.4 (BUILD: 20180709) (2017111304)&lt;/p&gt;

&lt;p&gt;Once you do this you can not go back again. Please note that this process&lt;br&gt;
can take a long time.&lt;/p&gt;

&lt;p&gt;Are you sure you want to upgrade this server to this version?&lt;/p&gt;

&lt;p&gt;type y (means yes) or n (means no)&lt;br&gt;
"&lt;br&gt;
type 'Y' and hit the 'Enter' button, this process may take some time. If any error occurred then fix it at your end.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;strong&gt;php admin/cli/purge_caches.php&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, the cache has been purged. You can make changes to the plugin file as per your requirement/assignment.&lt;/p&gt;

&lt;p&gt;Cheers, Happy Moodleing!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
